Skip to content

Commit 535f134

Browse files
authored
Extract ec_params_to_group from SPKI parsing (#12348)
To be used in #12296
1 parent ad65dc5 commit 535f134

File tree

3 files changed

+67
-57
lines changed

3 files changed

+67
-57
lines changed

Diff for: src/rust/cryptography-key-parsing/src/ec.rs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 crate::{KeyParsingError, KeyParsingResult};
6+
7+
use cryptography_x509::common::EcParameters;
8+
9+
pub(crate) fn ec_params_to_group(
10+
params: &EcParameters<'_>,
11+
) -> KeyParsingResult<openssl::ec::EcGroup> {
12+
match params {
13+
EcParameters::NamedCurve(curve_oid) => {
14+
let curve_nid = match curve_oid {
15+
&cryptography_x509::oid::EC_SECP192R1 => openssl::nid::Nid::X9_62_PRIME192V1,
16+
&cryptography_x509::oid::EC_SECP224R1 => openssl::nid::Nid::SECP224R1,
17+
&cryptography_x509::oid::EC_SECP256R1 => openssl::nid::Nid::X9_62_PRIME256V1,
18+
&cryptography_x509::oid::EC_SECP384R1 => openssl::nid::Nid::SECP384R1,
19+
&cryptography_x509::oid::EC_SECP521R1 => openssl::nid::Nid::SECP521R1,
20+
21+
&cryptography_x509::oid::EC_SECP256K1 => openssl::nid::Nid::SECP256K1,
22+
23+
&cryptography_x509::oid::EC_SECT233R1 => openssl::nid::Nid::SECT233R1,
24+
&cryptography_x509::oid::EC_SECT283R1 => openssl::nid::Nid::SECT283R1,
25+
&cryptography_x509::oid::EC_SECT409R1 => openssl::nid::Nid::SECT409R1,
26+
&cryptography_x509::oid::EC_SECT571R1 => openssl::nid::Nid::SECT571R1,
27+
28+
&cryptography_x509::oid::EC_SECT163R2 => openssl::nid::Nid::SECT163R2,
29+
30+
&cryptography_x509::oid::EC_SECT163K1 => openssl::nid::Nid::SECT163K1,
31+
&cryptography_x509::oid::EC_SECT233K1 => openssl::nid::Nid::SECT233K1,
32+
&cryptography_x509::oid::EC_SECT283K1 => openssl::nid::Nid::SECT283K1,
33+
&cryptography_x509::oid::EC_SECT409K1 => openssl::nid::Nid::SECT409K1,
34+
&cryptography_x509::oid::EC_SECT571K1 => openssl::nid::Nid::SECT571K1,
35+
36+
#[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))]
37+
&cryptography_x509::oid::EC_BRAINPOOLP256R1 => openssl::nid::Nid::BRAINPOOL_P256R1,
38+
#[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))]
39+
&cryptography_x509::oid::EC_BRAINPOOLP384R1 => openssl::nid::Nid::BRAINPOOL_P384R1,
40+
#[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))]
41+
&cryptography_x509::oid::EC_BRAINPOOLP512R1 => openssl::nid::Nid::BRAINPOOL_P512R1,
42+
43+
_ => return Err(KeyParsingError::UnsupportedEllipticCurve(curve_oid.clone())),
44+
};
45+
46+
Ok(openssl::ec::EcGroup::from_curve_name(curve_nid)
47+
.map_err(|_| KeyParsingError::UnsupportedEllipticCurve(curve_oid.clone()))?)
48+
}
49+
EcParameters::ImplicitCurve(_) | EcParameters::SpecifiedCurve(_) => {
50+
Err(KeyParsingError::ExplicitCurveUnsupported)
51+
}
52+
}
53+
}

Diff for: src/rust/cryptography-key-parsing/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#![deny(rust_2018_idioms, clippy::undocumented_unsafe_blocks)]
77
#![allow(unknown_lints, clippy::result_large_err)]
88

9+
mod ec;
910
pub mod rsa;
1011
pub mod spki;
1112

Diff for: src/rust/cryptography-key-parsing/src/spki.rs

+13-57
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// 2.0, and the BSD License. See the LICENSE file in the root of this repository
33
// for complete details.
44

5-
use cryptography_x509::common::{AlgorithmParameters, EcParameters, SubjectPublicKeyInfo};
5+
use cryptography_x509::common::{AlgorithmParameters, SubjectPublicKeyInfo};
66

77
use crate::{KeyParsingError, KeyParsingResult};
88

@@ -12,62 +12,18 @@ pub fn parse_public_key(
1212
let k = asn1::parse_single::<SubjectPublicKeyInfo<'_>>(data)?;
1313

1414
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-
},
15+
AlgorithmParameters::Ec(ec_params) => {
16+
let group = crate::ec::ec_params_to_group(&ec_params)?;
17+
let mut bn_ctx = openssl::bn::BigNumContext::new()?;
18+
let ec_point = openssl::ec::EcPoint::from_bytes(
19+
&group,
20+
k.subject_public_key.as_bytes(),
21+
&mut bn_ctx,
22+
)
23+
.map_err(|_| KeyParsingError::InvalidKey)?;
24+
let ec_key = openssl::ec::EcKey::from_public_key(&group, &ec_point)?;
25+
Ok(openssl::pkey::PKey::from_ec_key(ec_key)?)
26+
}
7127
AlgorithmParameters::Ed25519 => Ok(openssl::pkey::PKey::public_key_from_raw_bytes(
7228
k.subject_public_key.as_bytes(),
7329
openssl::pkey::Id::ED25519,

0 commit comments

Comments
 (0)