12 releases (3 stable)

1.1.0 Jan 30, 2023
1.0.1 Jan 30, 2023
0.2.2 Jun 3, 2021
0.1.5 May 28, 2021
0.1.1 Apr 29, 2021

#1790 in Encoding

Download history 15/week @ 2024-06-18 27/week @ 2024-06-25 88/week @ 2024-07-02 91/week @ 2024-07-09 81/week @ 2024-07-16 311/week @ 2024-07-23 339/week @ 2024-07-30 340/week @ 2024-08-06 301/week @ 2024-08-13 267/week @ 2024-08-20 190/week @ 2024-08-27 95/week @ 2024-09-03 71/week @ 2024-09-10 84/week @ 2024-09-17 167/week @ 2024-09-24 151/week @ 2024-10-01

492 downloads per month
Used in rust-ipfs

ISC license

36KB
937 lines

Overview

This is a simple library for creating and parsing X509 certificates.

A Library featuring:

  • Build X509 certificates
  • Encode certificates to DER format
  • Signing with external crypto function
  • Decoding of X509 certificates from DER format
  • Verifying with external crypto function
  • Encoding/decoding operations for frequently using extensions

Usage

Create and verify self-signed CA certificate

use simple_x509::*;

fn sign_fn(data: &Vec<u8>, sign_key: &Vec<u8>) -> Option<Vec<u8>> {
    // Signing implementation
}

fn verify_fn(pub_key: &Vec<u8>, data: &Vec<u8>, sign: &Vec<u8>) -> Option<bool> {
    // Verify implementation
}

fn main() {
    let country = "AU";
    let state = "Some-State";
    let organization = "Internet Widgits Pty Ltd";

    // Load Public Key
    let pub_key = std::fs::read("rsa_pub.der").unwrap();

    // Build X509 structure
    let x = X509Builder::new(vec![0xf2, 0xf9, 0xd8, 0x03]) /* SerialNumber */
        .version(2)
        .issuer_prstr(vec![2, 5, 4, 6], country) /* countryName */
        .issuer_utf8(vec![2, 5, 4, 8], state) /* stateOrProvinceName */
        .issuer_utf8(vec![2, 5, 4, 10], organization) /* organizationName */
        .subject_prstr(vec![2, 5, 4, 6], country) /* countryName */
        .subject_utf8(vec![2, 5, 4, 8], state) /* stateOrProvinceName */
        .subject_utf8(vec![2, 5, 4, 10], organization) /* organizationName */
        .not_before_utc(1_619_014_703)
        .not_after_utc(1_650_550_703)
        .pub_key_der(&pub_key)
        .sign_oid(vec![1, 2, 840, 113549, 1, 1, 11]) /* sha256WithRSAEncryption */
        .build();

    // Load Signing Key
    let sign_key = std::fs::read("rsa.pkcs8").unwrap();

    // Signing a certificate with external function
    let cert = x.sign(sign_fn, &sign_key).unwrap());

    // Encode to DER format
    let der = cert.x509_enc().unwrap());

    // Decode
    let x2 = der.x509_dec().unwrap());

    // Getting Public Key in DER format from certificate
    let pub_key2 = x2.pub_key().unwrap();

    // Verify signature with external function
    let res = x2.verify(verify_fn, &pub_key2);
}

Dependencies

~1.6–2.3MB
~44K SLoC