Skip to content

Commit

Permalink
Prefer aws-lc-rs over ring if both are enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Mar 20, 2024
1 parent 1eb31fb commit 9fcf77c
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 27 deletions.
14 changes: 7 additions & 7 deletions rcgen/src/key_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use yasna::{DERWriter, DERWriterSeq};

#[cfg(any(feature = "crypto", feature = "pem"))]
use crate::error::ExternalError;
#[cfg(all(feature = "crypto", feature = "aws_lc_rs", not(feature = "ring")))]
#[cfg(all(feature = "crypto", feature = "aws_lc_rs"))]
use crate::ring_like::rsa::KeySize;
#[cfg(feature = "crypto")]
use crate::ring_like::{
Expand Down Expand Up @@ -109,7 +109,7 @@ impl KeyPair {
serialized_der: key_pair_serialized,
})
},
#[cfg(all(feature = "aws_lc_rs", not(feature = "ring")))]
#[cfg(feature = "aws_lc_rs")]
SignAlgo::Rsa(sign_alg) => Self::generate_rsa_inner(alg, sign_alg, KeySize::Rsa2048),
// Ring doesn't have RSA key generation yet:
// https://github.com/briansmith/ring/issues/219
Expand All @@ -123,7 +123,7 @@ impl KeyPair {
///
/// If passed a signature algorithm that is not RSA, it will return
/// [`Error::KeyGenerationUnavailable`].
#[cfg(all(feature = "crypto", feature = "aws_lc_rs", not(feature = "ring")))]
#[cfg(all(feature = "crypto", feature = "aws_lc_rs"))]
pub fn generate_rsa_for(
alg: &'static SignatureAlgorithm,
key_size: RsaKeySize,
Expand All @@ -141,7 +141,7 @@ impl KeyPair {
}
}

#[cfg(all(feature = "crypto", feature = "aws_lc_rs", not(feature = "ring")))]
#[cfg(all(feature = "crypto", feature = "aws_lc_rs"))]
fn generate_rsa_inner(
alg: &'static SignatureAlgorithm,
sign_alg: &'static dyn RsaEncoding,
Expand Down Expand Up @@ -249,7 +249,7 @@ impl KeyPair {
let rsakp = RsaKeyPair::from_pkcs8(&serialized_der)._err()?;
KeyPairKind::Rsa(rsakp, &signature::RSA_PSS_SHA256)
} else {
#[cfg(all(feature = "aws_lc_rs", not(feature = "ring")))]
#[cfg(feature = "aws_lc_rs")]
if alg == &PKCS_ECDSA_P521_SHA512 {
KeyPairKind::Ec(ecdsa_from_pkcs8(
&signature::ECDSA_P521_SHA512_ASN1_SIGNING,
Expand Down Expand Up @@ -302,7 +302,7 @@ impl KeyPair {
&PKCS_RSA_SHA256,
)
} else {
#[cfg(all(feature = "aws_lc_rs", not(feature = "ring")))]
#[cfg(feature = "aws_lc_rs")]
if let Ok(eckp) =
ecdsa_from_pkcs8(&signature::ECDSA_P521_SHA512_ASN1_SIGNING, pkcs8, &rng)
{
Expand Down Expand Up @@ -497,7 +497,7 @@ impl TryFrom<&PrivatePkcs8KeyDer<'_>> for KeyPair {
}

/// The key size used for RSA key generation
#[cfg(all(feature = "crypto", feature = "aws_lc_rs", not(feature = "ring")))]
#[cfg(all(feature = "crypto", feature = "aws_lc_rs"))]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum RsaKeySize {
Expand Down
2 changes: 1 addition & 1 deletion rcgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub use crl::{
pub use csr::{CertificateSigningRequestParams, PublicKey};
pub use error::{Error, InvalidAsn1String};
use key_pair::PublicKeyData;
#[cfg(all(feature = "crypto", feature = "aws_lc_rs", not(feature = "ring")))]
#[cfg(all(feature = "crypto", feature = "aws_lc_rs"))]
pub use key_pair::RsaKeySize;
pub use key_pair::{KeyPair, RemoteKeyPair};
#[cfg(feature = "crypto")]
Expand Down
2 changes: 1 addition & 1 deletion rcgen/src/oid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(crate) const EC_SECP_256_R1: &[u64] = &[1, 2, 840, 10045, 3, 1, 7];
pub(crate) const EC_SECP_384_R1: &[u64] = &[1, 3, 132, 0, 34];
/// secp521r1 in [RFC 5480](https://datatracker.ietf.org/doc/html/rfc5480#appendix-A)
/// Currently this is only supported with the `aws_lc_rs` feature
#[cfg(all(feature = "aws_lc_rs", not(feature = "ring")))]
#[cfg(feature = "aws_lc_rs")]
pub(crate) const EC_SECP_521_R1: &[u64] = &[1, 3, 132, 0, 35];

/// rsaEncryption in [RFC 4055](https://www.rfc-editor.org/rfc/rfc4055#section-6)
Expand Down
12 changes: 6 additions & 6 deletions rcgen/src/ring_like.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(all(feature = "crypto", not(feature = "ring"), feature = "aws_lc_rs"))]
#[cfg(all(feature = "crypto", feature = "aws_lc_rs"))]
pub(crate) use aws_lc_rs::*;
#[cfg(all(feature = "crypto", feature = "ring"))]
#[cfg(all(feature = "crypto", feature = "ring", not(feature = "aws_lc_rs")))]
pub(crate) use ring::*;

#[cfg(feature = "crypto")]
Expand All @@ -14,25 +14,25 @@ pub(crate) fn ecdsa_from_pkcs8(
pkcs8: &[u8],
_rng: &dyn rand::SecureRandom,
) -> Result<signature::EcdsaKeyPair, Error> {
#[cfg(feature = "ring")]
#[cfg(all(feature = "ring", not(feature = "aws_lc_rs")))]
{
signature::EcdsaKeyPair::from_pkcs8(alg, pkcs8, _rng)._err()
}

#[cfg(all(not(feature = "ring"), feature = "aws_lc_rs"))]
#[cfg(feature = "aws_lc_rs")]
{
Ok(signature::EcdsaKeyPair::from_pkcs8(alg, pkcs8)._err()?)
}
}

#[cfg(feature = "crypto")]
pub(crate) fn rsa_key_pair_public_modulus_len(kp: &signature::RsaKeyPair) -> usize {
#[cfg(feature = "ring")]
#[cfg(all(feature = "ring", not(feature = "aws_lc_rs")))]
{
kp.public().modulus_len()
}

#[cfg(all(not(feature = "ring"), feature = "aws_lc_rs"))]
#[cfg(feature = "aws_lc_rs")]
{
kp.public_modulus_len()
}
Expand Down
6 changes: 3 additions & 3 deletions rcgen/src/sign_algo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl fmt::Debug for SignatureAlgorithm {
} else if self == &PKCS_ED25519 {
write!(f, "PKCS_ED25519")
} else {
#[cfg(all(feature = "aws_lc_rs", not(feature = "ring")))]
#[cfg(feature = "aws_lc_rs")]
if self == &PKCS_ECDSA_P521_SHA512 {
return write!(f, "PKCS_ECDSA_P521_SHA512");
}
Expand Down Expand Up @@ -91,7 +91,7 @@ impl SignatureAlgorithm {
//&PKCS_RSA_PSS_SHA256,
&PKCS_ECDSA_P256_SHA256,
&PKCS_ECDSA_P384_SHA384,
#[cfg(all(feature = "aws_lc_rs", not(feature = "ring")))]
#[cfg(feature = "aws_lc_rs")]
&PKCS_ECDSA_P521_SHA512,
&PKCS_ED25519,
];
Expand Down Expand Up @@ -187,7 +187,7 @@ pub(crate) mod algo {
};
/// ECDSA signing using the P-521 curves and SHA-512 hashing as per [RFC 5758](https://tools.ietf.org/html/rfc5758#section-3.2)
/// Currently this is only supported with the `aws_lc_rs` feature
#[cfg(all(feature = "aws_lc_rs", not(feature = "ring")))]
#[cfg(feature = "aws_lc_rs")]
pub static PKCS_ECDSA_P521_SHA512: SignatureAlgorithm = SignatureAlgorithm {
oids_sign_alg: &[&EC_PUBLIC_KEY, &EC_SECP_521_R1],
#[cfg(feature = "crypto")]
Expand Down
2 changes: 1 addition & 1 deletion rcgen/tests/botan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn test_botan_384() {
}

#[test]
#[cfg(all(feature = "aws_lc_rs", not(feature = "ring")))]
#[cfg(feature = "aws_lc_rs")]
fn test_botan_521() {
let (params, _) = default_params();
let key_pair = KeyPair::generate_for(&rcgen::PKCS_ECDSA_P521_SHA512).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion rcgen/tests/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod test_key_params_mismatch {
&rcgen::PKCS_RSA_SHA256,
&rcgen::PKCS_ECDSA_P256_SHA256,
&rcgen::PKCS_ECDSA_P384_SHA384,
#[cfg(all(feature = "aws_lc_rs", not(feature = "ring")))]
#[cfg(feature = "aws_lc_rs")]
&rcgen::PKCS_ECDSA_P521_SHA512,
&rcgen::PKCS_ED25519,
];
Expand Down
2 changes: 1 addition & 1 deletion rcgen/tests/openssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ fn test_openssl_384() {
}

#[test]
#[cfg(all(feature = "aws_lc_rs", not(feature = "ring")))]
#[cfg(feature = "aws_lc_rs")]
fn test_openssl_521() {
let (params, _) = util::default_params();
let key_pair = KeyPair::generate_for(&rcgen::PKCS_ECDSA_P521_SHA512).unwrap();
Expand Down
12 changes: 6 additions & 6 deletions rustls-cert-gen/src/cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rcgen::{
DnValue::PrintableString, ExtendedKeyUsagePurpose, IsCa, KeyPair, KeyUsagePurpose, SanType,
};

#[cfg(all(feature = "aws_lc_rs", not(feature = "ring")))]
#[cfg(feature = "aws_lc_rs")]
use aws_lc_rs as ring_like;
#[cfg(feature = "ring")]
use ring as ring_like;
Expand Down Expand Up @@ -218,7 +218,7 @@ pub enum KeyPairAlgorithm {
#[default]
EcdsaP256,
EcdsaP384,
#[cfg(all(feature = "aws_lc_rs", not(feature = "ring")))]
#[cfg(feature = "aws_lc_rs")]
EcdsaP521,
}

Expand All @@ -229,7 +229,7 @@ impl fmt::Display for KeyPairAlgorithm {
KeyPairAlgorithm::Ed25519 => write!(f, "ed25519"),
KeyPairAlgorithm::EcdsaP256 => write!(f, "ecdsa-p256"),
KeyPairAlgorithm::EcdsaP384 => write!(f, "ecdsa-p384"),
#[cfg(all(feature = "aws_lc_rs", not(feature = "ring")))]
#[cfg(feature = "aws_lc_rs")]
KeyPairAlgorithm::EcdsaP521 => write!(f, "ecdsa-p521"),
}
}
Expand Down Expand Up @@ -273,7 +273,7 @@ impl KeyPairAlgorithm {

rcgen::KeyPair::from_pkcs8_der_and_sign_algo(&pkcs8_bytes.as_ref().into(), alg)
},
#[cfg(all(feature = "aws_lc_rs", not(feature = "ring")))]
#[cfg(feature = "aws_lc_rs")]
KeyPairAlgorithm::EcdsaP521 => {
use ring_like::signature::EcdsaKeyPair;
use ring_like::signature::ECDSA_P521_SHA512_ASN1_SIGNING;
Expand Down Expand Up @@ -368,7 +368,7 @@ mod tests {
}

#[test]
#[cfg(all(feature = "aws_lc_rs", not(feature = "ring")))]
#[cfg(feature = "aws_lc_rs")]
fn serialize_end_entity_ecdsa_p521_sha512_sig() -> anyhow::Result<()> {
let ca = CertificateBuilder::new().certificate_authority().build()?;
let end_entity = CertificateBuilder::new()
Expand Down Expand Up @@ -488,7 +488,7 @@ mod tests {
"PKCS_ECDSA_P384_SHA384"
);

#[cfg(all(feature = "aws_lc_rs", not(feature = "ring")))]
#[cfg(feature = "aws_lc_rs")]
{
let keypair = KeyPairAlgorithm::EcdsaP521.to_key_pair()?;
assert_eq!(
Expand Down

0 comments on commit 9fcf77c

Please sign in to comment.