Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RSA key generation #230

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add RSA key generation
est31 committed Mar 4, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 48ea65ffc1c49a502ca67c886d582512327465c3
21 changes: 8 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rcgen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ name = "simple"
required-features = ["crypto"]

[dependencies]
aws-lc-rs = { version = "1.0.0", optional = true }
aws-lc-rs = { version = "1.6.0", optional = true }
yasna = { version = "0.5.2", features = ["time", "std"] }
ring = { workspace = true, optional = true }
pem = { workspace = true, optional = true }
61 changes: 58 additions & 3 deletions rcgen/src/key_pair.rs
Original file line number Diff line number Diff line change
@@ -5,6 +5,8 @@ use yasna::DERWriter;

#[cfg(any(feature = "crypto", feature = "pem"))]
use crate::error::ExternalError;
#[cfg(all(feature = "crypto", feature = "aws_lc_rs", not(feature = "ring")))]
use crate::ring_like::rsa::KeySize;
#[cfg(feature = "crypto")]
use crate::ring_like::{
error as ring_error,
@@ -74,6 +76,9 @@ impl KeyPair {
/// Generate a new random key pair for the specified signature algorithm
///
/// If you're not sure which algorithm to use, [`PKCS_ECDSA_P256_SHA256`] is a good choice.
/// If passed an RSA signature algorithm, it depends on the backend whether we return
/// a generated key or an error for key generation being unavailable.
/// Currently, only `aws-lc-rs` supports RSA key generation.
#[cfg(feature = "crypto")]
pub fn generate_for(alg: &'static SignatureAlgorithm) -> Result<Self, Error> {
let rng = &SystemRandom::new();
@@ -101,15 +106,55 @@ impl KeyPair {
serialized_der: key_pair_serialized,
})
},
#[cfg(all(feature = "aws_lc_rs", not(feature = "ring")))]
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
// https://github.com/briansmith/ring/pull/733
// Nor does aws-lc-rs:
// https://github.com/aws/aws-lc-rs/issues/296
SignAlgo::Rsa() => Err(Error::KeyGenerationUnavailable),
#[cfg(any(not(feature = "aws_lc_rs"), feature = "ring"))]
SignAlgo::Rsa(_sign_alg) => Err(Error::KeyGenerationUnavailable),
}
}

/// Generates a new random RSA key pair for the specified key size
///
/// 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")))]
pub fn generate_rsa_for(
alg: &'static SignatureAlgorithm,
key_size: RsaKeySize,
) -> Result<Self, Error> {
match alg.sign_alg {
SignAlgo::Rsa(sign_alg) => {
let key_size = match key_size {
RsaKeySize::_2048 => KeySize::Rsa2048,
RsaKeySize::_3072 => KeySize::Rsa3072,
RsaKeySize::_4096 => KeySize::Rsa4096,
};
Self::generate_rsa_inner(alg, sign_alg, key_size)
},
_ => Err(Error::KeyGenerationUnavailable),
}
}

#[cfg(all(feature = "crypto", feature = "aws_lc_rs", not(feature = "ring")))]
fn generate_rsa_inner(
alg: &'static SignatureAlgorithm,
sign_alg: &'static dyn RsaEncoding,
key_size: KeySize,
) -> Result<Self, Error> {
use aws_lc_rs::encoding::AsDer;
let key_pair = RsaKeyPair::generate(key_size)._err()?;
let key_pair_serialized = key_pair.as_der()._err()?.as_ref().to_vec();

Ok(KeyPair {
kind: KeyPairKind::Rsa(key_pair, sign_alg),
alg,
serialized_der: key_pair_serialized,
})
}

/// Parses the key pair from the DER format
///
/// Equivalent to using the [`TryFrom`] implementation.
@@ -377,6 +422,16 @@ impl TryFrom<Vec<u8>> for KeyPair {
}
}

/// The key size used for RSA key generation
#[cfg(all(feature = "crypto", feature = "aws_lc_rs", not(feature = "ring")))]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum RsaKeySize {
_2048,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the _ prefix looks unidiomatic to me, but not sure I have a better idea.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, not really perfect. r#2048 doesn't work, but I like _2048 more than Rsa2048.

_3072,
_4096,
}

impl PublicKeyData for KeyPair {
fn alg(&self) -> &SignatureAlgorithm {
self.alg
12 changes: 6 additions & 6 deletions rcgen/src/sign_algo.rs
Original file line number Diff line number Diff line change
@@ -5,14 +5,14 @@ use yasna::DERWriter;
use yasna::Tag;

#[cfg(feature = "crypto")]
use crate::ring_like::signature::{self, EcdsaSigningAlgorithm, EdDSAParameters};
use crate::ring_like::signature::{self, EcdsaSigningAlgorithm, EdDSAParameters, RsaEncoding};
use crate::Error;

#[cfg(feature = "crypto")]
pub(crate) enum SignAlgo {
EcDsa(&'static EcdsaSigningAlgorithm),
EdDsa(&'static EdDSAParameters),
Rsa(),
Rsa(&'static dyn RsaEncoding),
}

#[derive(PartialEq, Eq, Hash)]
@@ -111,7 +111,7 @@ pub mod algo {
pub static PKCS_RSA_SHA256: SignatureAlgorithm = SignatureAlgorithm {
oids_sign_alg: &[&OID_RSA_ENCRYPTION],
#[cfg(feature = "crypto")]
sign_alg: SignAlgo::Rsa(),
sign_alg: SignAlgo::Rsa(&signature::RSA_PKCS1_SHA256),
// sha256WithRSAEncryption in RFC 4055
oid_components: &[1, 2, 840, 113549, 1, 1, 11],
params: SignatureAlgorithmParams::Null,
@@ -121,7 +121,7 @@ pub mod algo {
pub static PKCS_RSA_SHA384: SignatureAlgorithm = SignatureAlgorithm {
oids_sign_alg: &[&OID_RSA_ENCRYPTION],
#[cfg(feature = "crypto")]
sign_alg: SignAlgo::Rsa(),
sign_alg: SignAlgo::Rsa(&signature::RSA_PKCS1_SHA384),
// sha384WithRSAEncryption in RFC 4055
oid_components: &[1, 2, 840, 113549, 1, 1, 12],
params: SignatureAlgorithmParams::Null,
@@ -131,7 +131,7 @@ pub mod algo {
pub static PKCS_RSA_SHA512: SignatureAlgorithm = SignatureAlgorithm {
oids_sign_alg: &[&OID_RSA_ENCRYPTION],
#[cfg(feature = "crypto")]
sign_alg: SignAlgo::Rsa(),
sign_alg: SignAlgo::Rsa(&signature::RSA_PKCS1_SHA512),
// sha512WithRSAEncryption in RFC 4055
oid_components: &[1, 2, 840, 113549, 1, 1, 13],
params: SignatureAlgorithmParams::Null,
@@ -148,7 +148,7 @@ pub mod algo {
// to use ID-RSASSA-PSS if possible.
oids_sign_alg: &[&OID_RSASSA_PSS],
#[cfg(feature = "crypto")]
sign_alg: SignAlgo::Rsa(),
sign_alg: SignAlgo::Rsa(&signature::RSA_PSS_SHA256),
oid_components: &OID_RSASSA_PSS, //&[1, 2, 840, 113549, 1, 1, 13],
// rSASSA-PSS-SHA256-Params in RFC 4055
params: SignatureAlgorithmParams::RsaPss {