Skip to content

Commit bfe34e8

Browse files
authored
Support RsaSubjectPublicKey to/from X509; Bump version (#742)
* Support RsaSubjectPublicKey to/from X509 * Remove redundant check * Also bump the patch version
1 parent 4fe8198 commit bfe34e8

File tree

5 files changed

+51
-9
lines changed

5 files changed

+51
-9
lines changed

aws-lc-rs/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[package]
22
name = "aws-lc-rs"
33
authors = ["AWS-LibCrypto"]
4-
version = "1.12.6"
4+
version = "1.12.7"
55
# this crate re-exports whatever sys crate that was selected
6-
links = "aws_lc_rs_1_12_6_sys"
6+
links = "aws_lc_rs_1_12_7_sys"
77
edition = "2021"
88
rust-version = "1.63.0"
99
keywords = ["crypto", "cryptography", "security"]

aws-lc-rs/Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ UNAME_S := $(shell uname -s)
44

55
AWS_LC_RS_COV_EXTRA_FEATURES := unstable
66

7+
export AWS_LC_RS_DISABLE_SLOW_TESTS := 1
8+
79
asan:
810
# TODO: This build target produces linker error on Mac.
911
# Run specific tests:

aws-lc-rs/src/rsa/encoding.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub(in crate::rsa) mod rfc8017 {
7676
///
7777
/// Encodings that use the `SubjectPublicKeyInfo` structure.
7878
pub(in crate::rsa) mod rfc5280 {
79-
use crate::aws_lc::{EVP_PKEY, EVP_PKEY_RSA};
79+
use crate::aws_lc::{EVP_PKEY, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS};
8080
use crate::buffer::Buffer;
8181
use crate::encoding::PublicKeyX509Der;
8282
use crate::error::{KeyRejected, Unspecified};
@@ -92,6 +92,9 @@ pub(in crate::rsa) mod rfc5280 {
9292
pub(in crate::rsa) fn decode_public_key_der(
9393
value: &[u8],
9494
) -> Result<LcPtr<EVP_PKEY>, KeyRejected> {
95-
LcPtr::<EVP_PKEY>::parse_rfc5280_public_key(value, EVP_PKEY_RSA)
95+
LcPtr::<EVP_PKEY>::parse_rfc5280_public_key(value, EVP_PKEY_RSA).or(
96+
// Does anyone encode with this OID?
97+
LcPtr::<EVP_PKEY>::parse_rfc5280_public_key(value, EVP_PKEY_RSA_PSS),
98+
)
9699
}
97100
}

aws-lc-rs/src/rsa/key.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::aws_lc::{
1212
};
1313
#[cfg(feature = "ring-io")]
1414
use crate::aws_lc::{RSA_get0_e, RSA_get0_n};
15-
use crate::encoding::{AsDer, Pkcs8V1Der};
15+
use crate::encoding::{AsDer, Pkcs8V1Der, PublicKeyX509Der};
1616
use crate::error::{KeyRejected, Unspecified};
1717
#[cfg(feature = "ring-io")]
1818
use crate::io;
@@ -32,6 +32,7 @@ use core::ptr::null_mut;
3232
use std::os::raw::c_int;
3333

3434
use crate::pkcs8::Version;
35+
use crate::rsa::encoding::{rfc5280, rfc8017};
3536
use crate::rsa::signature::configure_rsa_pkcs1_pss_padding;
3637
#[cfg(feature = "ring-io")]
3738
use untrusted::Input;
@@ -288,7 +289,7 @@ impl Drop for PublicKey {
288289
}
289290

290291
impl PublicKey {
291-
pub(super) fn new(evp_pkey: &LcPtr<EVP_PKEY>) -> Result<Self, Unspecified> {
292+
pub(super) fn new(evp_pkey: &LcPtr<EVP_PKEY>) -> Result<Self, KeyRejected> {
292293
let key = encoding::rfc8017::encode_public_key_der(evp_pkey)?;
293294
#[cfg(feature = "ring-io")]
294295
{
@@ -307,6 +308,17 @@ impl PublicKey {
307308
#[cfg(not(feature = "ring-io"))]
308309
Ok(PublicKey { key })
309310
}
311+
312+
/// Parses an RSA public key from either RFC8017 or RFC5280
313+
/// # Errors
314+
/// `KeyRejected` if the encoding is not for a valid RSA key.
315+
pub fn from_der(input: &[u8]) -> Result<Self, KeyRejected> {
316+
// These both invoke `RSA_check_key`:
317+
// https://github.com/aws/aws-lc/blob/4368aaa6975ba41bd76d3bb12fac54c4680247fb/crypto/rsa_extra/rsa_asn1.c#L105-L109
318+
PublicKey::new(
319+
&rfc8017::decode_public_key_der(input).or(rfc5280::decode_public_key_der(input))?,
320+
)
321+
}
310322
}
311323

312324
impl Debug for PublicKey {
@@ -325,6 +337,14 @@ impl AsRef<[u8]> for PublicKey {
325337
}
326338
}
327339

340+
impl AsDer<PublicKeyX509Der<'static>> for PublicKey {
341+
fn as_der(&self) -> Result<PublicKeyX509Der<'static>, Unspecified> {
342+
// TODO: refactor
343+
let evp_pkey = rfc8017::decode_public_key_der(self.as_ref())?;
344+
rfc5280::encode_public_key_der(&evp_pkey)
345+
}
346+
}
347+
328348
#[cfg(feature = "ring-io")]
329349
impl PublicKey {
330350
/// The public modulus (n).

aws-lc-rs/tests/rsa_test.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -819,16 +819,33 @@ fn min_encrypt_key() {
819819
const PUBLIC_KEY: &[u8] = include_bytes!("data/rsa_test_public_key_2048.x509");
820820

821821
let parsed_private_key = PrivateDecryptingKey::from_pkcs8(PRIVATE_KEY).expect("key supported");
822+
let signing_priv_key = RsaKeyPair::from_pkcs8(PRIVATE_KEY).expect("key supported");
822823
let parsed_public_key = PublicEncryptingKey::from_der(PUBLIC_KEY).expect("key supported");
824+
let parsed_signing_public_key =
825+
RsaSubjectPublicKey::from_der(PUBLIC_KEY).expect("key supported");
823826

824-
let public_key = parsed_private_key.public_key();
827+
let derived_public_key = parsed_private_key.public_key();
828+
let derived_signing_public_key = signing_priv_key.public_key();
825829

826830
let private_key_bytes =
827831
AsDer::<Pkcs8V1Der>::as_der(&parsed_private_key).expect("serializeable");
828-
let public_key_bytes = AsDer::<PublicKeyX509Der>::as_der(&public_key).expect("serializeable");
832+
let signing_private_key_bytes =
833+
AsDer::<Pkcs8V1Der>::as_der(&signing_priv_key).expect("serializeable");
834+
let parsed_public_key_bytes =
835+
AsDer::<PublicKeyX509Der>::as_der(&parsed_public_key).expect("serializeable");
836+
let parsed_signing_public_key_bytes =
837+
AsDer::<PublicKeyX509Der>::as_der(&parsed_signing_public_key).expect("serializeable");
838+
let derived_public_key_bytes =
839+
AsDer::<PublicKeyX509Der>::as_der(&derived_public_key).expect("serializeable");
840+
let derived_signing_public_key_bytes =
841+
AsDer::<PublicKeyX509Der>::as_der(derived_signing_public_key).expect("serializeable");
829842

830843
assert_eq!(PRIVATE_KEY, private_key_bytes.as_ref());
831-
assert_eq!(PUBLIC_KEY, public_key_bytes.as_ref());
844+
assert_eq!(PRIVATE_KEY, signing_private_key_bytes.as_ref());
845+
assert_eq!(PUBLIC_KEY, parsed_public_key_bytes.as_ref());
846+
assert_eq!(PUBLIC_KEY, parsed_signing_public_key_bytes.as_ref());
847+
assert_eq!(PUBLIC_KEY, derived_public_key_bytes.as_ref());
848+
assert_eq!(PUBLIC_KEY, derived_signing_public_key_bytes.as_ref());
832849

833850
let oaep_parsed_private =
834851
OaepPrivateDecryptingKey::new(parsed_private_key.clone()).expect("supported key");

0 commit comments

Comments
 (0)