Skip to content

Commit 6b4a4de

Browse files
authored
Migrate SPKI parsing from OpenSSL to Rust (#10121)
1 parent 2c56719 commit 6b4a4de

File tree

15 files changed

+332
-84
lines changed

15 files changed

+332
-84
lines changed

src/rust/Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rust/cryptography-key-parsing/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ rust-version = "1.63.0"
99

1010
[dependencies]
1111
asn1 = { version = "0.15.5", default-features = false }
12+
cfg-if = "1"
1213
openssl = "0.10.63"
14+
openssl-sys = "0.9.99"
15+
cryptography-x509 = { path = "../cryptography-x509" }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This file is dual licensed under the terms of the Apache License, Version
2+
// 2.0, and the BSD License. See the LICENSE file in the root of this repository
3+
// for complete details.
4+
5+
use std::env;
6+
7+
fn main() {
8+
if env::var("DEP_OPENSSL_LIBRESSL_VERSION_NUMBER").is_ok() {
9+
println!("cargo:rustc-cfg=CRYPTOGRAPHY_IS_LIBRESSL");
10+
}
11+
12+
if env::var("DEP_OPENSSL_BORINGSSL").is_ok() {
13+
println!("cargo:rustc-cfg=CRYPTOGRAPHY_IS_BORINGSSL");
14+
}
15+
}

src/rust/cryptography-key-parsing/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
// for complete details.
44

55
pub mod rsa;
6+
pub mod spki;
67

78
pub enum KeyParsingError {
9+
InvalidKey,
10+
ExplicitCurveUnsupported,
11+
UnsupportedKeyType(asn1::ObjectIdentifier),
12+
UnsupportedEllipticCurve(asn1::ObjectIdentifier),
813
Parse(asn1::ParseError),
914
OpenSSL(openssl::error::ErrorStack),
1015
}

src/rust/cryptography-key-parsing/src/rsa.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct Pksc1RsaPublicKey<'a> {
1010
e: asn1::BigUint<'a>,
1111
}
1212

13-
pub fn parse_pkcs1_rsa_public_key(
13+
pub fn parse_pkcs1_public_key(
1414
data: &[u8],
1515
) -> KeyParsingResult<openssl::pkey::PKey<openssl::pkey::Public>> {
1616
let k = asn1::parse_single::<Pksc1RsaPublicKey>(data)?;
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// This file is dual licensed under the terms of the Apache License, Version
2+
// 2.0, and the BSD License. See the LICENSE file in the root of this repository
3+
// for complete details.
4+
5+
use cryptography_x509::common::{AlgorithmParameters, EcParameters, SubjectPublicKeyInfo};
6+
7+
use crate::{KeyParsingError, KeyParsingResult};
8+
9+
pub fn parse_public_key(
10+
data: &[u8],
11+
) -> KeyParsingResult<openssl::pkey::PKey<openssl::pkey::Public>> {
12+
let k = asn1::parse_single::<SubjectPublicKeyInfo>(data)?;
13+
14+
match k.algorithm.params {
15+
AlgorithmParameters::Ec(ec_params) => match ec_params {
16+
EcParameters::NamedCurve(curve_oid) => {
17+
let curve_nid = match curve_oid {
18+
cryptography_x509::oid::EC_SECP192R1 => openssl::nid::Nid::X9_62_PRIME192V1,
19+
cryptography_x509::oid::EC_SECP224R1 => openssl::nid::Nid::SECP224R1,
20+
cryptography_x509::oid::EC_SECP256R1 => openssl::nid::Nid::X9_62_PRIME256V1,
21+
cryptography_x509::oid::EC_SECP384R1 => openssl::nid::Nid::SECP384R1,
22+
cryptography_x509::oid::EC_SECP521R1 => openssl::nid::Nid::SECP521R1,
23+
24+
cryptography_x509::oid::EC_SECP256K1 => openssl::nid::Nid::SECP256K1,
25+
26+
cryptography_x509::oid::EC_SECT233R1 => openssl::nid::Nid::SECT233R1,
27+
cryptography_x509::oid::EC_SECT283R1 => openssl::nid::Nid::SECT283R1,
28+
cryptography_x509::oid::EC_SECT409R1 => openssl::nid::Nid::SECT409R1,
29+
cryptography_x509::oid::EC_SECT571R1 => openssl::nid::Nid::SECT571R1,
30+
31+
cryptography_x509::oid::EC_SECT163R2 => openssl::nid::Nid::SECT163R2,
32+
33+
cryptography_x509::oid::EC_SECT163K1 => openssl::nid::Nid::SECT163K1,
34+
cryptography_x509::oid::EC_SECT233K1 => openssl::nid::Nid::SECT233K1,
35+
cryptography_x509::oid::EC_SECT283K1 => openssl::nid::Nid::SECT283K1,
36+
cryptography_x509::oid::EC_SECT409K1 => openssl::nid::Nid::SECT409K1,
37+
cryptography_x509::oid::EC_SECT571K1 => openssl::nid::Nid::SECT571K1,
38+
39+
#[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))]
40+
cryptography_x509::oid::EC_BRAINPOOLP256R1 => {
41+
openssl::nid::Nid::BRAINPOOL_P256R1
42+
}
43+
#[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))]
44+
cryptography_x509::oid::EC_BRAINPOOLP384R1 => {
45+
openssl::nid::Nid::BRAINPOOL_P384R1
46+
}
47+
#[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))]
48+
cryptography_x509::oid::EC_BRAINPOOLP512R1 => {
49+
openssl::nid::Nid::BRAINPOOL_P512R1
50+
}
51+
52+
_ => return Err(KeyParsingError::UnsupportedEllipticCurve(curve_oid)),
53+
};
54+
55+
let group = openssl::ec::EcGroup::from_curve_name(curve_nid)
56+
.map_err(|_| KeyParsingError::UnsupportedEllipticCurve(curve_oid))?;
57+
let mut bn_ctx = openssl::bn::BigNumContext::new()?;
58+
let ec_point = openssl::ec::EcPoint::from_bytes(
59+
&group,
60+
k.subject_public_key.as_bytes(),
61+
&mut bn_ctx,
62+
)
63+
.map_err(|_| KeyParsingError::InvalidKey)?;
64+
let ec_key = openssl::ec::EcKey::from_public_key(&group, &ec_point)?;
65+
Ok(openssl::pkey::PKey::from_ec_key(ec_key)?)
66+
}
67+
EcParameters::ImplicitCurve(_) | EcParameters::SpecifiedCurve(_) => {
68+
Err(KeyParsingError::ExplicitCurveUnsupported)
69+
}
70+
},
71+
AlgorithmParameters::Ed25519 => Ok(openssl::pkey::PKey::public_key_from_raw_bytes(
72+
k.subject_public_key.as_bytes(),
73+
openssl::pkey::Id::ED25519,
74+
)?),
75+
#[cfg(all(not(CRYPTOGRAPHY_IS_LIBRESSL), not(CRYPTOGRAPHY_IS_BORINGSSL)))]
76+
AlgorithmParameters::Ed448 => Ok(openssl::pkey::PKey::public_key_from_raw_bytes(
77+
k.subject_public_key.as_bytes(),
78+
openssl::pkey::Id::ED448,
79+
)?),
80+
AlgorithmParameters::X25519 => Ok(openssl::pkey::PKey::public_key_from_raw_bytes(
81+
k.subject_public_key.as_bytes(),
82+
openssl::pkey::Id::X25519,
83+
)?),
84+
#[cfg(all(not(CRYPTOGRAPHY_IS_LIBRESSL), not(CRYPTOGRAPHY_IS_BORINGSSL)))]
85+
AlgorithmParameters::X448 => Ok(openssl::pkey::PKey::public_key_from_raw_bytes(
86+
k.subject_public_key.as_bytes(),
87+
openssl::pkey::Id::X448,
88+
)?),
89+
AlgorithmParameters::Rsa(_) | AlgorithmParameters::RsaPss(_) => {
90+
// RSA-PSS keys are treated the same as bare RSA keys.
91+
crate::rsa::parse_pkcs1_public_key(k.subject_public_key.as_bytes())
92+
}
93+
AlgorithmParameters::Dsa(dsa_params) => {
94+
let p = openssl::bn::BigNum::from_slice(dsa_params.p.as_bytes())?;
95+
let q = openssl::bn::BigNum::from_slice(dsa_params.q.as_bytes())?;
96+
let g = openssl::bn::BigNum::from_slice(dsa_params.g.as_bytes())?;
97+
98+
let pub_key_int =
99+
asn1::parse_single::<asn1::BigUint<'_>>(k.subject_public_key.as_bytes())?;
100+
let pub_key = openssl::bn::BigNum::from_slice(pub_key_int.as_bytes())?;
101+
102+
let dsa = openssl::dsa::Dsa::from_public_components(p, q, g, pub_key)?;
103+
Ok(openssl::pkey::PKey::from_dsa(dsa)?)
104+
}
105+
#[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))]
106+
AlgorithmParameters::Dh(dh_params) => {
107+
let p = openssl::bn::BigNum::from_slice(dh_params.p.as_bytes())?;
108+
let q = openssl::bn::BigNum::from_slice(dh_params.q.as_bytes())?;
109+
let g = openssl::bn::BigNum::from_slice(dh_params.g.as_bytes())?;
110+
let dh = openssl::dh::Dh::from_pqg(p, Some(q), g)?;
111+
112+
let pub_key_int =
113+
asn1::parse_single::<asn1::BigUint<'_>>(k.subject_public_key.as_bytes())?;
114+
let pub_key = openssl::bn::BigNum::from_slice(pub_key_int.as_bytes())?;
115+
let dh = dh.set_public_key(pub_key)?;
116+
117+
cfg_if::cfg_if! {
118+
if #[cfg(CRYPTOGRAPHY_IS_LIBRESSL)] {
119+
Ok(openssl::pkey::PKey::from_dh(dh)?)
120+
} else {
121+
Ok(openssl::pkey::PKey::from_dhx(dh)?)
122+
}
123+
}
124+
}
125+
#[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))]
126+
AlgorithmParameters::DhKeyAgreement(dh_params) => {
127+
let p = openssl::bn::BigNum::from_slice(dh_params.p.as_bytes())?;
128+
let g = openssl::bn::BigNum::from_slice(dh_params.g.as_bytes())?;
129+
let dh = openssl::dh::Dh::from_pqg(p, None, g)?;
130+
131+
let pub_key_int =
132+
asn1::parse_single::<asn1::BigUint<'_>>(k.subject_public_key.as_bytes())?;
133+
let pub_key = openssl::bn::BigNum::from_slice(pub_key_int.as_bytes())?;
134+
let dh = dh.set_public_key(pub_key)?;
135+
136+
Ok(openssl::pkey::PKey::from_dh(dh)?)
137+
}
138+
_ => Err(KeyParsingError::UnsupportedKeyType(
139+
k.algorithm.oid().clone(),
140+
)),
141+
}
142+
}

src/rust/cryptography-x509/src/common.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ pub enum AlgorithmParameters<'a> {
4545
#[defined_by(oid::ED448_OID)]
4646
Ed448,
4747

48+
#[defined_by(oid::X25519_OID)]
49+
X25519,
50+
#[defined_by(oid::X448_OID)]
51+
X448,
52+
4853
// These encodings are only used in SPKI AlgorithmIdentifiers.
4954
#[defined_by(oid::EC_OID)]
5055
Ec(EcParameters<'a>),
@@ -103,6 +108,9 @@ pub enum AlgorithmParameters<'a> {
103108
#[defined_by(oid::RSASSA_PSS_OID)]
104109
RsaPss(Option<Box<RsaPssParameters<'a>>>),
105110

111+
#[defined_by(oid::DSA_OID)]
112+
Dsa(DssParams<'a>),
113+
106114
#[defined_by(oid::DSA_WITH_SHA224_OID)]
107115
DsaWithSha224(Option<asn1::Null>),
108116
#[defined_by(oid::DSA_WITH_SHA256_OID)]
@@ -112,6 +120,11 @@ pub enum AlgorithmParameters<'a> {
112120
#[defined_by(oid::DSA_WITH_SHA512_OID)]
113121
DsaWithSha512(Option<asn1::Null>),
114122

123+
#[defined_by(oid::DH_OID)]
124+
Dh(DHXParams<'a>),
125+
#[defined_by(oid::DH_KEY_AGREEMENT_OID)]
126+
DhKeyAgreement(BasicDHParams<'a>),
127+
115128
#[default]
116129
Other(asn1::ObjectIdentifier, Option<asn1::Tlv<'a>>),
117130
}
@@ -235,6 +248,38 @@ pub struct DHParams<'a> {
235248
pub g: asn1::BigUint<'a>,
236249
pub q: Option<asn1::BigUint<'a>>,
237250
}
251+
252+
// From PKCS#3 Section 9
253+
// DHParameter ::= SEQUENCE {
254+
// prime INTEGER, -- p
255+
// base INTEGER, -- g
256+
// privateValueLength INTEGER OPTIONAL
257+
// }
258+
#[derive(asn1::Asn1Read, asn1::Asn1Write, Clone, PartialEq, Eq, Debug, Hash)]
259+
pub struct BasicDHParams<'a> {
260+
pub p: asn1::BigUint<'a>,
261+
pub g: asn1::BigUint<'a>,
262+
pub private_value_length: Option<u32>,
263+
}
264+
265+
// From https://www.rfc-editor.org/rfc/rfc3279#section-2.3.3
266+
// DomainParameters ::= SEQUENCE {
267+
// p INTEGER, -- odd prime, p=jq +1
268+
// g INTEGER, -- generator, g
269+
// q INTEGER, -- factor of p-1
270+
// j INTEGER OPTIONAL, -- subgroup factor
271+
// validationParms ValidationParms OPTIONAL
272+
// }
273+
#[derive(asn1::Asn1Read, asn1::Asn1Write, Clone, PartialEq, Eq, Debug, Hash)]
274+
pub struct DHXParams<'a> {
275+
pub p: asn1::BigUint<'a>,
276+
pub g: asn1::BigUint<'a>,
277+
pub q: asn1::BigUint<'a>,
278+
pub j: Option<asn1::BigUint<'a>>,
279+
// No support for this, so don't bother filling out the fields.
280+
pub validation_params: Option<asn1::Sequence<'a>>,
281+
}
282+
238283
// RSA-PSS ASN.1 default hash algorithm
239284
pub const PSS_SHA1_HASH_ALG: AlgorithmIdentifier<'_> = AlgorithmIdentifier {
240285
oid: asn1::DefinedByMarker::marker(),
@@ -327,6 +372,20 @@ pub struct RsaPssParameters<'a> {
327372
pub _trailer_field: u8,
328373
}
329374

375+
// https://datatracker.ietf.org/doc/html/rfc3279#section-2.3.2
376+
//
377+
// Dss-Parms ::= SEQUENCE {
378+
// p INTEGER,
379+
// q INTEGER,
380+
// g INTEGER
381+
// }
382+
#[derive(asn1::Asn1Read, asn1::Asn1Write, Hash, Clone, PartialEq, Eq, Debug)]
383+
pub struct DssParams<'a> {
384+
pub p: asn1::BigUint<'a>,
385+
pub q: asn1::BigUint<'a>,
386+
pub g: asn1::BigUint<'a>,
387+
}
388+
330389
/// A VisibleString ASN.1 element whose contents is not validated as meeting the
331390
/// requirements (visible characters of IA5), and instead is only known to be
332391
/// valid UTF-8.

src/rust/cryptography-x509/src/oid.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,32 @@ pub const ACCEPTABLE_RESPONSES_OID: asn1::ObjectIdentifier =
4747

4848
// Public key identifiers
4949
pub const EC_OID: asn1::ObjectIdentifier = asn1::oid!(1, 2, 840, 10045, 2, 1);
50+
51+
pub const EC_SECP192R1: asn1::ObjectIdentifier = asn1::oid!(1, 2, 840, 10045, 3, 1, 1);
52+
pub const EC_SECP224R1: asn1::ObjectIdentifier = asn1::oid!(1, 3, 132, 0, 33);
5053
pub const EC_SECP256R1: asn1::ObjectIdentifier = asn1::oid!(1, 2, 840, 10045, 3, 1, 7);
5154
pub const EC_SECP384R1: asn1::ObjectIdentifier = asn1::oid!(1, 3, 132, 0, 34);
5255
pub const EC_SECP521R1: asn1::ObjectIdentifier = asn1::oid!(1, 3, 132, 0, 35);
56+
57+
pub const EC_SECP256K1: asn1::ObjectIdentifier = asn1::oid!(1, 3, 132, 0, 10);
58+
59+
pub const EC_SECT233R1: asn1::ObjectIdentifier = asn1::oid!(1, 3, 132, 0, 27);
60+
pub const EC_SECT283R1: asn1::ObjectIdentifier = asn1::oid!(1, 3, 132, 0, 17);
61+
pub const EC_SECT409R1: asn1::ObjectIdentifier = asn1::oid!(1, 3, 132, 0, 37);
62+
pub const EC_SECT571R1: asn1::ObjectIdentifier = asn1::oid!(1, 3, 132, 0, 39);
63+
64+
pub const EC_SECT163R2: asn1::ObjectIdentifier = asn1::oid!(1, 3, 132, 0, 15);
65+
66+
pub const EC_SECT163K1: asn1::ObjectIdentifier = asn1::oid!(1, 3, 132, 0, 1);
67+
pub const EC_SECT233K1: asn1::ObjectIdentifier = asn1::oid!(1, 3, 132, 0, 26);
68+
pub const EC_SECT283K1: asn1::ObjectIdentifier = asn1::oid!(1, 3, 132, 0, 16);
69+
pub const EC_SECT409K1: asn1::ObjectIdentifier = asn1::oid!(1, 3, 132, 0, 36);
70+
pub const EC_SECT571K1: asn1::ObjectIdentifier = asn1::oid!(1, 3, 132, 0, 38);
71+
72+
pub const EC_BRAINPOOLP256R1: asn1::ObjectIdentifier = asn1::oid!(1, 3, 36, 3, 3, 2, 8, 1, 1, 7);
73+
pub const EC_BRAINPOOLP384R1: asn1::ObjectIdentifier = asn1::oid!(1, 3, 36, 3, 3, 2, 8, 1, 1, 11);
74+
pub const EC_BRAINPOOLP512R1: asn1::ObjectIdentifier = asn1::oid!(1, 3, 36, 3, 3, 2, 8, 1, 1, 13);
75+
5376
pub const RSA_OID: asn1::ObjectIdentifier = asn1::oid!(1, 2, 840, 113549, 1, 1, 1);
5477

5578
// Signing methods
@@ -81,11 +104,18 @@ pub const RSA_WITH_SHA3_384_OID: asn1::ObjectIdentifier =
81104
pub const RSA_WITH_SHA3_512_OID: asn1::ObjectIdentifier =
82105
asn1::oid!(2, 16, 840, 1, 101, 3, 4, 3, 16);
83106

107+
pub const DSA_OID: asn1::ObjectIdentifier = asn1::oid!(1, 2, 840, 10040, 4, 1);
84108
pub const DSA_WITH_SHA224_OID: asn1::ObjectIdentifier = asn1::oid!(2, 16, 840, 1, 101, 3, 4, 3, 1);
85109
pub const DSA_WITH_SHA256_OID: asn1::ObjectIdentifier = asn1::oid!(2, 16, 840, 1, 101, 3, 4, 3, 2);
86110
pub const DSA_WITH_SHA384_OID: asn1::ObjectIdentifier = asn1::oid!(2, 16, 840, 1, 101, 3, 4, 3, 3);
87111
pub const DSA_WITH_SHA512_OID: asn1::ObjectIdentifier = asn1::oid!(2, 16, 840, 1, 101, 3, 4, 3, 4);
88112

113+
pub const DH_OID: asn1::ObjectIdentifier = asn1::oid!(1, 2, 840, 10046, 2, 1);
114+
pub const DH_KEY_AGREEMENT_OID: asn1::ObjectIdentifier = asn1::oid!(1, 2, 840, 113549, 1, 3, 1);
115+
116+
pub const X25519_OID: asn1::ObjectIdentifier = asn1::oid!(1, 3, 101, 110);
117+
pub const X448_OID: asn1::ObjectIdentifier = asn1::oid!(1, 3, 101, 111);
118+
89119
pub const ED25519_OID: asn1::ObjectIdentifier = asn1::oid!(1, 3, 101, 112);
90120
pub const ED448_OID: asn1::ObjectIdentifier = asn1::oid!(1, 3, 101, 113);
91121

0 commit comments

Comments
 (0)