Cosmian / crypto_core

Other
1 stars 1 forks source link

Public Key crypto: add support for encoding/decoding to/from Subject Public Key Info (SPKI) and signature #73

Open bgrieder opened 1 year ago

bgrieder commented 1 year ago

Subject Public Key Info [RFC 5280 § 4.1.2.7] is the format of Public Keys found in Certificates Signing Requests and Certificates.

RSA

PKCS#1 is the format used by the BitString of Subject Public Key Infos in Certificate Signing Requests and Certificates.

This is a simple addition; the rsa crate already offers some facilities.

let rsa_public_key =
        rsa::RsaPublicKey::from_pkcs1_der(spki.subject_public_key.as_bytes().unwrap())
            .unwrap();

where spki is a spki::SubjectPublicKeyInfo

Nist Elliptic Curves

NIST EC offers direct deserialization via elliptic_curve::DecodedKey trait and this impl.

impl<T> DecodePublicKey for T
where
    T: for<'a> [TryFrom](https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html)<SubjectPublicKeyInfo<AnyRef<'a>, BitStringRef<'a>>, Error = Error>,

///Deserialize object from ASN.1 DER-encoded [SubjectPublicKeyInfo] (binary format).
fn from_public_key_der(bytes: &[u8]) -> Result<T, Error>
  let pk = p256::PublicKey::from_public_key_der(csr.info.public_key.to_der().unwrap().as_slice())
        .unwrap();

Curve25519

? (TODO)

bgrieder commented 1 year ago

WIP https://github.com/Cosmian/crypto_core/tree/spki

TODO: support for EC signatures + refactor Ed 519 into main PublicKey.try_sign() scheme