|
| 1 | +use super::hash::Algorithm as HashAlgorithm; |
| 2 | +use alloc::vec; |
| 3 | +use mbedtls::pk::Pk; |
| 4 | +use pki_types::{AlgorithmIdentifier, InvalidSignature, SignatureVerificationAlgorithm}; |
| 5 | +use rustls::SignatureScheme; |
| 6 | +use webpki::alg_id; |
| 7 | + |
| 8 | +/// ECDSA signatures using the P-256 curve and SHA-256. |
| 9 | +pub static ECDSA_P256_SHA256: &Algorithm = &Algorithm { |
| 10 | + signature_scheme: SignatureScheme::ECDSA_NISTP256_SHA256, |
| 11 | + hash_algo: &super::hash::MBED_SHA_256, |
| 12 | + public_key_alg_id: alg_id::ECDSA_P256, |
| 13 | + signature_alg_id: alg_id::ECDSA_SHA256, |
| 14 | +}; |
| 15 | +/// ECDSA signatures using the P-384 curve and SHA-384. |
| 16 | +pub static ECDSA_P384_SHA384: &Algorithm = &Algorithm { |
| 17 | + signature_scheme: SignatureScheme::ECDSA_NISTP384_SHA384, |
| 18 | + hash_algo: &super::hash::MBED_SHA_384, |
| 19 | + public_key_alg_id: alg_id::ECDSA_P384, |
| 20 | + signature_alg_id: alg_id::ECDSA_SHA384, |
| 21 | +}; |
| 22 | +/// RSA PKCS#1 1.5 signatures using SHA-256. |
| 23 | +pub static RSA_PKCS1_SHA256: &Algorithm = &Algorithm { |
| 24 | + signature_scheme: SignatureScheme::RSA_PKCS1_SHA256, |
| 25 | + hash_algo: &super::hash::MBED_SHA_256, |
| 26 | + public_key_alg_id: alg_id::RSA_ENCRYPTION, |
| 27 | + signature_alg_id: alg_id::RSA_PKCS1_SHA256, |
| 28 | +}; |
| 29 | +/// RSA PKCS#1 1.5 signatures using SHA-384. |
| 30 | +pub static RSA_PKCS1_SHA384: &Algorithm = &Algorithm { |
| 31 | + signature_scheme: SignatureScheme::RSA_PKCS1_SHA384, |
| 32 | + hash_algo: &super::hash::MBED_SHA_384, |
| 33 | + public_key_alg_id: alg_id::RSA_ENCRYPTION, |
| 34 | + signature_alg_id: alg_id::RSA_PKCS1_SHA384, |
| 35 | +}; |
| 36 | +/// RSA PKCS#1 1.5 signatures using SHA-512. |
| 37 | +pub static RSA_PKCS1_SHA512: &Algorithm = &Algorithm { |
| 38 | + signature_scheme: SignatureScheme::RSA_PKCS1_SHA512, |
| 39 | + hash_algo: &super::hash::MBED_SHA_512, |
| 40 | + public_key_alg_id: alg_id::RSA_ENCRYPTION, |
| 41 | + signature_alg_id: alg_id::RSA_PKCS1_SHA512, |
| 42 | +}; |
| 43 | +/// RSA PSS signatures using SHA-256 and of |
| 44 | +/// type rsaEncryption; see [RFC 4055 Section 1.2]. |
| 45 | +/// |
| 46 | +/// [RFC 4055 Section 1.2]: https://tools.ietf.org/html/rfc4055#section-1.2 |
| 47 | +pub static RSA_PSS_SHA256: &Algorithm = &Algorithm { |
| 48 | + signature_scheme: SignatureScheme::RSA_PSS_SHA256, |
| 49 | + hash_algo: &super::hash::MBED_SHA_256, |
| 50 | + public_key_alg_id: alg_id::RSA_ENCRYPTION, |
| 51 | + signature_alg_id: alg_id::RSA_PSS_SHA256, |
| 52 | +}; |
| 53 | +/// RSA PSS signatures using SHA-384 and of |
| 54 | +/// type rsaEncryption; see [RFC 4055 Section 1.2]. |
| 55 | +/// |
| 56 | +/// [RFC 4055 Section 1.2]: https://tools.ietf.org/html/rfc4055#section-1.2 |
| 57 | +pub static RSA_PSS_SHA384: &Algorithm = &Algorithm { |
| 58 | + signature_scheme: SignatureScheme::RSA_PSS_SHA384, |
| 59 | + hash_algo: &super::hash::MBED_SHA_384, |
| 60 | + public_key_alg_id: alg_id::RSA_ENCRYPTION, |
| 61 | + signature_alg_id: alg_id::RSA_PSS_SHA384, |
| 62 | +}; |
| 63 | +/// RSA PSS signatures using SHA-512 and of |
| 64 | +/// type rsaEncryption; see [RFC 4055 Section 1.2]. |
| 65 | +/// |
| 66 | +/// [RFC 4055 Section 1.2]: https://tools.ietf.org/html/rfc4055#section-1.2 |
| 67 | +pub static RSA_PSS_SHA512: &Algorithm = &Algorithm { |
| 68 | + signature_scheme: SignatureScheme::RSA_PSS_SHA512, |
| 69 | + hash_algo: &super::hash::MBED_SHA_512, |
| 70 | + public_key_alg_id: alg_id::RSA_ENCRYPTION, |
| 71 | + signature_alg_id: alg_id::RSA_PSS_SHA512, |
| 72 | +}; |
| 73 | + |
| 74 | +/// A signature verify algorithm type |
| 75 | +#[derive(Clone, Debug, PartialEq)] |
| 76 | +pub struct Algorithm { |
| 77 | + signature_scheme: SignatureScheme, |
| 78 | + hash_algo: &'static HashAlgorithm, |
| 79 | + public_key_alg_id: AlgorithmIdentifier, |
| 80 | + signature_alg_id: AlgorithmIdentifier, |
| 81 | +} |
| 82 | + |
| 83 | +impl SignatureVerificationAlgorithm for Algorithm { |
| 84 | + fn verify_signature(&self, public_key: &[u8], message: &[u8], signature: &[u8]) -> Result<(), InvalidSignature> { |
| 85 | + let mut pk = Pk::from_public_key(public_key).map_err(|_| InvalidSignature)?; |
| 86 | + let signature_curve = utils::pk::rustls_signature_scheme_to_mbedtls_curve_id(self.signature_scheme); |
| 87 | + match signature_curve { |
| 88 | + mbedtls::pk::EcGroupId::None => (), |
| 89 | + _ => { |
| 90 | + let curves_match = pk |
| 91 | + .curve() |
| 92 | + .is_ok_and(|pk_curve| pk_curve == signature_curve); |
| 93 | + if !curves_match { |
| 94 | + return Err(InvalidSignature); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + if let Some(opts) = utils::pk::rustls_signature_scheme_to_mbedtls_pk_options(self.signature_scheme) { |
| 99 | + pk.set_options(opts); |
| 100 | + } |
| 101 | + let mut hash = vec![0u8; self.hash_algo.output_len]; |
| 102 | + mbedtls::hash::Md::hash(self.hash_algo.hash_type, message, &mut hash).map_err(|_| InvalidSignature)?; |
| 103 | + pk.verify(self.hash_algo.hash_type, &hash, signature) |
| 104 | + .map_err(|_| InvalidSignature) |
| 105 | + } |
| 106 | + |
| 107 | + fn public_key_alg_id(&self) -> AlgorithmIdentifier { |
| 108 | + self.public_key_alg_id |
| 109 | + } |
| 110 | + |
| 111 | + fn signature_alg_id(&self) -> AlgorithmIdentifier { |
| 112 | + self.signature_alg_id |
| 113 | + } |
| 114 | +} |
0 commit comments