2
2
// 2.0, and the BSD License. See the LICENSE file in the root of this repository
3
3
// for complete details.
4
4
5
- use cryptography_x509:: common:: { AlgorithmParameters , EcParameters , SubjectPublicKeyInfo } ;
5
+ use cryptography_x509:: common:: { AlgorithmParameters , SubjectPublicKeyInfo } ;
6
6
7
7
use crate :: { KeyParsingError , KeyParsingResult } ;
8
8
@@ -12,62 +12,18 @@ pub fn parse_public_key(
12
12
let k = asn1:: parse_single :: < SubjectPublicKeyInfo < ' _ > > ( data) ?;
13
13
14
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
- } ,
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
+ }
71
27
AlgorithmParameters :: Ed25519 => Ok ( openssl:: pkey:: PKey :: public_key_from_raw_bytes (
72
28
k. subject_public_key . as_bytes ( ) ,
73
29
openssl:: pkey:: Id :: ED25519 ,
0 commit comments