diff --git a/CHANGELOG.md b/CHANGELOG.md index a4fd52a8..ad6d4682 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,12 +8,6 @@ Use `matches!` instead. - Remove `TryFrom<[u8]>` and `TryFrom>` for `KeyPair` in favor of allowing `KeyPair::from_der` to take `impl Into>` which allows `Vec` as well as `[u8]`. - Upgrade to `ring` `v0.17`. -- Add `ring::rand::SecureRandom` parameter to: - - `KeyPair::generate` - - `KeyPair::from_der` - - `KeyPair::from_der_and_sign_algo` - - `KeyPair::from_pem` - - `KeyPair::from_pem_and_sign_algo` ## Release 0.11.3 - October 1, 2023 diff --git a/examples/rsa-irc-openssl.rs b/examples/rsa-irc-openssl.rs index f2b72056..0ac72f60 100644 --- a/examples/rsa-irc-openssl.rs +++ b/examples/rsa-irc-openssl.rs @@ -13,7 +13,7 @@ fn main() -> Result<(), Box> { let pkey: openssl::pkey::PKey<_> = openssl::rsa::Rsa::generate(2048)?.try_into()?; let key_pair_pem = String::from_utf8(pkey.private_key_to_pem_pkcs8()?)?; - let key_pair = rcgen::KeyPair::from_pem(&key_pair_pem, &ring::rand::SystemRandom::new())?; + let key_pair = rcgen::KeyPair::from_pem(&key_pair_pem)?; params.key_pair = Some(key_pair); let cert = Certificate::from_params(params)?; diff --git a/examples/rsa-irc.rs b/examples/rsa-irc.rs index 5ce91665..fbd0be3e 100644 --- a/examples/rsa-irc.rs +++ b/examples/rsa-irc.rs @@ -19,9 +19,7 @@ fn main() -> Result<(), Box> { let bits = 2048; let private_key = RsaPrivateKey::new(&mut rng, bits)?; let private_key_der = private_key.to_pkcs8_der()?; - let key_pair = - rcgen::KeyPair::from_der(private_key_der.as_bytes(), &ring::rand::SystemRandom::new()) - .unwrap(); + let key_pair = rcgen::KeyPair::from_der(private_key_der.as_bytes()).unwrap(); params.key_pair = Some(key_pair); let cert = Certificate::from_params(params)?; diff --git a/src/key_pair.rs b/src/key_pair.rs index ac8f6757..7d7342db 100644 --- a/src/key_pair.rs +++ b/src/key_pair.rs @@ -54,8 +54,8 @@ impl KeyPair { /// Parses the key pair from the DER format /// /// Equivalent to using the [`TryFrom`] implementation. - pub fn from_der(der: &[u8], rng: &dyn SecureRandom) -> Result { - Ok(KeyPair::from_raw(der, rng)?) + pub fn from_der(der: &[u8]) -> Result { + Ok(KeyPair::from_raw(der, &SystemRandom::new())?) } /// Returns the key pair's signature algorithm pub fn algorithm(&self) -> &'static SignatureAlgorithm { @@ -63,10 +63,10 @@ impl KeyPair { } /// Parses the key pair from the ASCII PEM format #[cfg(feature = "pem")] - pub fn from_pem(pem_str: &str, rng: &dyn SecureRandom) -> Result { + pub fn from_pem(pem_str: &str) -> Result { let private_key = pem::parse(pem_str)?; let private_key_der: &[_] = private_key.contents(); - Ok(KeyPair::from_raw(private_key_der, rng)?) + Ok(KeyPair::from_raw(private_key_der, &SystemRandom::new())?) } /// Obtains the key pair from a raw public key and a remote private key @@ -86,11 +86,10 @@ impl KeyPair { pub fn from_pem_and_sign_algo( pem_str: &str, alg: &'static SignatureAlgorithm, - rng: &dyn SecureRandom, ) -> Result { let private_key = pem::parse(pem_str)?; let private_key_der: &[_] = private_key.contents(); - Ok(Self::from_der_and_sign_algo(private_key_der, alg, rng)?) + Ok(Self::from_der_and_sign_algo(private_key_der, alg)?) } /// Obtains the key pair from a DER formatted key @@ -105,8 +104,8 @@ impl KeyPair { pub fn from_der_and_sign_algo( pkcs8: &[u8], alg: &'static SignatureAlgorithm, - rng: &dyn SecureRandom, ) -> Result { + let rng = &SystemRandom::new(); let pkcs8_vec = pkcs8.to_vec(); let kind = if alg == &PKCS_ED25519 { @@ -369,7 +368,7 @@ mod test { let pkcs8 = EcdsaKeyPair::generate_pkcs8(&ECDSA_P256_SHA256_FIXED_SIGNING, &rng).unwrap(); let der = pkcs8.as_ref().to_vec(); - let key_pair = KeyPair::from_der(&der, &rng).unwrap(); + let key_pair = KeyPair::from_der(&der).unwrap(); assert_eq!(key_pair.algorithm(), &PKCS_ECDSA_P256_SHA256); } } diff --git a/tests/botan.rs b/tests/botan.rs index 4562729e..a33d51d1 100644 --- a/tests/botan.rs +++ b/tests/botan.rs @@ -102,11 +102,7 @@ fn test_botan_25519_v1_given() { let mut params = default_params(); params.alg = &rcgen::PKCS_ED25519; - let kp = rcgen::KeyPair::from_pem( - util::ED25519_TEST_KEY_PAIR_PEM_V1, - &ring::rand::SystemRandom::new(), - ) - .unwrap(); + let kp = rcgen::KeyPair::from_pem(util::ED25519_TEST_KEY_PAIR_PEM_V1).unwrap(); params.key_pair = Some(kp); let cert = Certificate::from_params(params).unwrap(); @@ -122,11 +118,7 @@ fn test_botan_25519_v2_given() { let mut params = default_params(); params.alg = &rcgen::PKCS_ED25519; - let kp = rcgen::KeyPair::from_pem( - util::ED25519_TEST_KEY_PAIR_PEM_V2, - &ring::rand::SystemRandom::new(), - ) - .unwrap(); + let kp = rcgen::KeyPair::from_pem(util::ED25519_TEST_KEY_PAIR_PEM_V2).unwrap(); params.key_pair = Some(kp); let cert = Certificate::from_params(params).unwrap(); @@ -142,11 +134,7 @@ fn test_botan_rsa_given() { let mut params = default_params(); params.alg = &rcgen::PKCS_RSA_SHA256; - let kp = rcgen::KeyPair::from_pem( - util::RSA_TEST_KEY_PAIR_PEM, - &ring::rand::SystemRandom::new(), - ) - .unwrap(); + let kp = rcgen::KeyPair::from_pem(util::RSA_TEST_KEY_PAIR_PEM).unwrap(); params.key_pair = Some(kp); let cert = Certificate::from_params(params).unwrap(); @@ -193,8 +181,7 @@ fn test_botan_imported_ca() { ca_cert.serialize_private_key_der(), ); - let ca_key_pair = - KeyPair::from_der(ca_key_der.as_slice(), &ring::rand::SystemRandom::new()).unwrap(); + let ca_key_pair = KeyPair::from_der(ca_key_der.as_slice()).unwrap(); let imported_ca_cert_params = CertificateParams::from_ca_cert_der(ca_cert_der.as_slice(), ca_key_pair).unwrap(); let imported_ca_cert = Certificate::from_params(imported_ca_cert_params).unwrap(); @@ -230,8 +217,7 @@ fn test_botan_imported_ca_with_printable_string() { ca_cert.serialize_private_key_der(), ); - let ca_key_pair = - KeyPair::from_der(ca_key_der.as_slice(), &ring::rand::SystemRandom::new()).unwrap(); + let ca_key_pair = KeyPair::from_der(ca_key_der.as_slice()).unwrap(); let imported_ca_cert_params = CertificateParams::from_ca_cert_der(ca_cert_der.as_slice(), ca_key_pair).unwrap(); let imported_ca_cert = Certificate::from_params(imported_ca_cert_params).unwrap(); diff --git a/tests/generic.rs b/tests/generic.rs index 183daf80..c87298a3 100644 --- a/tests/generic.rs +++ b/tests/generic.rs @@ -38,11 +38,7 @@ mod test_key_params_mismatch { wrong_params.key_pair = Some(KeyPair::generate(kalg_1, &ring::rand::SystemRandom::new()).unwrap()); } else { - let kp = KeyPair::from_pem( - util::RSA_TEST_KEY_PAIR_PEM, - &ring::rand::SystemRandom::new(), - ) - .unwrap(); + let kp = KeyPair::from_pem(util::RSA_TEST_KEY_PAIR_PEM).unwrap(); wrong_params.key_pair = Some(kp); } wrong_params.alg = *kalg_2; diff --git a/tests/openssl.rs b/tests/openssl.rs index 45f27e67..b7d82756 100644 --- a/tests/openssl.rs +++ b/tests/openssl.rs @@ -233,11 +233,7 @@ fn test_openssl_25519_v1_given() { let mut params = util::default_params(); params.alg = &rcgen::PKCS_ED25519; - let kp = rcgen::KeyPair::from_pem( - util::ED25519_TEST_KEY_PAIR_PEM_V1, - &ring::rand::SystemRandom::new(), - ) - .unwrap(); + let kp = rcgen::KeyPair::from_pem(util::ED25519_TEST_KEY_PAIR_PEM_V1).unwrap(); params.key_pair = Some(kp); let cert = Certificate::from_params(params).unwrap(); @@ -258,11 +254,7 @@ fn test_openssl_25519_v2_given() { let mut params = util::default_params(); params.alg = &rcgen::PKCS_ED25519; - let kp = rcgen::KeyPair::from_pem( - util::ED25519_TEST_KEY_PAIR_PEM_V2, - &ring::rand::SystemRandom::new(), - ) - .unwrap(); + let kp = rcgen::KeyPair::from_pem(util::ED25519_TEST_KEY_PAIR_PEM_V2).unwrap(); params.key_pair = Some(kp); let cert = Certificate::from_params(params).unwrap(); @@ -280,11 +272,7 @@ fn test_openssl_rsa_given() { let mut params = util::default_params(); params.alg = &rcgen::PKCS_RSA_SHA256; - let kp = rcgen::KeyPair::from_pem( - util::RSA_TEST_KEY_PAIR_PEM, - &ring::rand::SystemRandom::new(), - ) - .unwrap(); + let kp = rcgen::KeyPair::from_pem(util::RSA_TEST_KEY_PAIR_PEM).unwrap(); params.key_pair = Some(kp); let cert = Certificate::from_params(params).unwrap(); @@ -306,12 +294,7 @@ fn test_openssl_rsa_combinations_given() { let mut params = util::default_params(); params.alg = alg; - let kp = rcgen::KeyPair::from_pem_and_sign_algo( - util::RSA_TEST_KEY_PAIR_PEM, - alg, - &ring::rand::SystemRandom::new(), - ) - .unwrap(); + let kp = rcgen::KeyPair::from_pem_and_sign_algo(util::RSA_TEST_KEY_PAIR_PEM, alg).unwrap(); params.key_pair = Some(kp); let cert = Certificate::from_params(params).unwrap(); diff --git a/tests/webpki.rs b/tests/webpki.rs index 1b32ea2c..a12dae9c 100644 --- a/tests/webpki.rs +++ b/tests/webpki.rs @@ -164,11 +164,7 @@ fn test_webpki_25519_v1_given() { let mut params = util::default_params(); params.alg = &rcgen::PKCS_ED25519; - let kp = rcgen::KeyPair::from_pem( - util::ED25519_TEST_KEY_PAIR_PEM_V1, - &ring::rand::SystemRandom::new(), - ) - .unwrap(); + let kp = rcgen::KeyPair::from_pem(util::ED25519_TEST_KEY_PAIR_PEM_V1).unwrap(); params.key_pair = Some(kp); let cert = Certificate::from_params(params).unwrap(); @@ -185,11 +181,7 @@ fn test_webpki_25519_v2_given() { let mut params = util::default_params(); params.alg = &rcgen::PKCS_ED25519; - let kp = rcgen::KeyPair::from_pem( - util::ED25519_TEST_KEY_PAIR_PEM_V2, - &ring::rand::SystemRandom::new(), - ) - .unwrap(); + let kp = rcgen::KeyPair::from_pem(util::ED25519_TEST_KEY_PAIR_PEM_V2).unwrap(); params.key_pair = Some(kp); let cert = Certificate::from_params(params).unwrap(); @@ -206,11 +198,7 @@ fn test_webpki_rsa_given() { let mut params = util::default_params(); params.alg = &rcgen::PKCS_RSA_SHA256; - let kp = rcgen::KeyPair::from_pem( - util::RSA_TEST_KEY_PAIR_PEM, - &ring::rand::SystemRandom::new(), - ) - .unwrap(); + let kp = rcgen::KeyPair::from_pem(util::RSA_TEST_KEY_PAIR_PEM).unwrap(); params.key_pair = Some(kp); let cert = Certificate::from_params(params).unwrap(); @@ -250,12 +238,7 @@ fn test_webpki_rsa_combinations_given() { for c in configs { let mut params = util::default_params(); params.alg = c.0; - let kp = rcgen::KeyPair::from_pem_and_sign_algo( - util::RSA_TEST_KEY_PAIR_PEM, - c.0, - &ring::rand::SystemRandom::new(), - ) - .unwrap(); + let kp = rcgen::KeyPair::from_pem_and_sign_algo(util::RSA_TEST_KEY_PAIR_PEM, c.0).unwrap(); params.key_pair = Some(kp); let cert = Certificate::from_params(params).unwrap(); @@ -434,8 +417,7 @@ fn test_webpki_imported_ca() { ca_cert.serialize_private_key_der(), ); - let ca_key_pair = - KeyPair::from_der(ca_key_der.as_slice(), &ring::rand::SystemRandom::new()).unwrap(); + let ca_key_pair = KeyPair::from_der(ca_key_der.as_slice()).unwrap(); let imported_ca_cert_params = CertificateParams::from_ca_cert_der(ca_cert_der.as_slice(), ca_key_pair).unwrap(); let imported_ca_cert = Certificate::from_params(imported_ca_cert_params).unwrap(); @@ -477,8 +459,7 @@ fn test_webpki_imported_ca_with_printable_string() { ca_cert.serialize_private_key_der(), ); - let ca_key_pair = - KeyPair::from_der(ca_key_der.as_slice(), &ring::rand::SystemRandom::new()).unwrap(); + let ca_key_pair = KeyPair::from_der(ca_key_der.as_slice()).unwrap(); let imported_ca_cert_params = CertificateParams::from_ca_cert_der(ca_cert_der.as_slice(), ca_key_pair).unwrap(); let imported_ca_cert = Certificate::from_params(imported_ca_cert_params).unwrap();