@@ -12,56 +12,48 @@ use crate::{
12
12
use core:: convert:: { TryFrom , TryInto } ;
13
13
use crypto_bigint:: { BoxedUint , NonZero , Resize } ;
14
14
use pkcs8:: {
15
- der:: { asn1:: OctetStringRef , Encode } ,
15
+ der:: { asn1:: OctetStringRef , Decode } ,
16
16
Document , EncodePrivateKey , EncodePublicKey , ObjectIdentifier , SecretDocument ,
17
17
} ;
18
18
use zeroize:: Zeroizing ;
19
19
20
20
/// ObjectID for the RSA PSS keys
21
21
pub const ID_RSASSA_PSS : ObjectIdentifier = ObjectIdentifier :: new_unwrap ( "1.2.840.113549.1.1.10" ) ;
22
22
23
- /// Verify that the `AlgorithmIdentifier` for a key is correct.
24
- pub ( crate ) fn verify_algorithm_id ( algorithm : & pkcs8:: AlgorithmIdentifierRef ) -> spki:: Result < ( ) > {
25
- match algorithm. oid {
26
- pkcs1:: ALGORITHM_OID => {
27
- if algorithm. parameters_any ( ) ? != pkcs8:: der:: asn1:: Null . into ( ) {
28
- return Err ( spki:: Error :: KeyMalformed ) ;
29
- }
30
- }
31
- ID_RSASSA_PSS => {
32
- if algorithm. parameters . is_some ( ) {
33
- return Err ( spki:: Error :: KeyMalformed ) ;
34
- }
35
- }
36
- _ => return Err ( spki:: Error :: OidUnknown { oid : algorithm. oid } ) ,
37
- } ;
23
+ // PKCS#1
38
24
39
- Ok ( ( ) )
25
+ fn uint_from_slice ( data : & [ u8 ] , bits : u32 ) -> pkcs1:: Result < BoxedUint > {
26
+ BoxedUint :: from_be_slice ( data, bits) . map_err ( |_| pkcs1:: Error :: KeyMalformed )
40
27
}
41
28
42
- fn uint_from_slice ( data : & [ u8 ] , bits : u32 ) -> pkcs8:: Result < BoxedUint > {
43
- BoxedUint :: from_be_slice ( data, bits) . map_err ( |_| pkcs8:: Error :: KeyMalformed )
29
+ impl pkcs1:: DecodeRsaPrivateKey for RsaPrivateKey {
30
+ fn from_pkcs1_der ( bytes : & [ u8 ] ) -> pkcs1:: Result < Self > {
31
+ pkcs1:: RsaPrivateKey :: from_der ( bytes) ?. try_into ( )
32
+ }
44
33
}
45
34
46
- impl TryFrom < pkcs8:: PrivateKeyInfoRef < ' _ > > for RsaPrivateKey {
47
- type Error = pkcs8:: Error ;
35
+ impl pkcs1:: DecodeRsaPublicKey for RsaPublicKey {
36
+ fn from_pkcs1_der ( bytes : & [ u8 ] ) -> pkcs1:: Result < Self > {
37
+ pkcs1:: RsaPublicKey :: from_der ( bytes) ?. try_into ( )
38
+ }
39
+ }
48
40
49
- fn try_from ( private_key_info : pkcs8:: PrivateKeyInfoRef < ' _ > ) -> pkcs8:: Result < Self > {
50
- use pkcs8:: Error :: KeyMalformed ;
51
- verify_algorithm_id ( & private_key_info. algorithm ) ?;
41
+ impl TryFrom < pkcs1:: RsaPrivateKey < ' _ > > for RsaPrivateKey {
42
+ type Error = pkcs1:: Error ;
52
43
53
- let pkcs1_key = pkcs1:: RsaPrivateKey :: try_from ( private_key_info. private_key ) ?;
44
+ fn try_from ( pkcs1_key : pkcs1:: RsaPrivateKey < ' _ > ) -> pkcs1:: Result < RsaPrivateKey > {
45
+ use pkcs1:: Error :: KeyMalformed ;
54
46
55
47
// Multi-prime RSA keys not currently supported
56
48
if pkcs1_key. version ( ) != pkcs1:: Version :: TwoPrime {
57
- return Err ( pkcs1:: Error :: Version . into ( ) ) ;
49
+ return Err ( pkcs1:: Error :: Version ) ;
58
50
}
59
51
60
52
let bits = u32:: try_from ( pkcs1_key. modulus . as_bytes ( ) . len ( ) ) . map_err ( |_| KeyMalformed ) ? * 8 ;
61
53
62
54
let n = uint_from_slice ( pkcs1_key. modulus . as_bytes ( ) , bits) ?;
63
55
let bits_e = u32:: try_from ( pkcs1_key. public_exponent . as_bytes ( ) . len ( ) )
64
- . map_err ( |_| KeyMalformed ) ?
56
+ . map_err ( |_| pkcs1 :: Error :: KeyMalformed ) ?
65
57
* 8 ;
66
58
let e = uint_from_slice ( pkcs1_key. public_exponent . as_bytes ( ) , bits_e) ?;
67
59
let e = Option :: from ( e) . ok_or ( KeyMalformed ) ?;
@@ -75,16 +67,11 @@ impl TryFrom<pkcs8::PrivateKeyInfoRef<'_>> for RsaPrivateKey {
75
67
}
76
68
}
77
69
78
- impl TryFrom < pkcs8:: SubjectPublicKeyInfoRef < ' _ > > for RsaPublicKey {
79
- type Error = spki:: Error ;
80
-
81
- fn try_from ( spki : pkcs8:: SubjectPublicKeyInfoRef < ' _ > ) -> spki:: Result < Self > {
82
- use spki:: Error :: KeyMalformed ;
83
-
84
- verify_algorithm_id ( & spki. algorithm ) ?;
70
+ impl TryFrom < pkcs1:: RsaPublicKey < ' _ > > for RsaPublicKey {
71
+ type Error = pkcs1:: Error ;
85
72
86
- let pkcs1_key =
87
- pkcs1:: RsaPublicKey :: try_from ( spki . subject_public_key . as_bytes ( ) . ok_or ( KeyMalformed ) ? ) ? ;
73
+ fn try_from ( pkcs1_key : pkcs1 :: RsaPublicKey < ' _ > ) -> pkcs1 :: Result < Self > {
74
+ use pkcs1:: Error :: KeyMalformed ;
88
75
89
76
let bits = u32:: try_from ( pkcs1_key. modulus . as_bytes ( ) . len ( ) ) . map_err ( |_| KeyMalformed ) ? * 8 ;
90
77
let n = uint_from_slice ( pkcs1_key. modulus . as_bytes ( ) , bits) ?;
@@ -98,11 +85,11 @@ impl TryFrom<pkcs8::SubjectPublicKeyInfoRef<'_>> for RsaPublicKey {
98
85
}
99
86
}
100
87
101
- impl EncodePrivateKey for RsaPrivateKey {
102
- fn to_pkcs8_der ( & self ) -> pkcs8 :: Result < SecretDocument > {
88
+ impl pkcs1 :: EncodeRsaPrivateKey for RsaPrivateKey {
89
+ fn to_pkcs1_der ( & self ) -> pkcs1 :: Result < SecretDocument > {
103
90
// Check if the key is multi prime
104
91
if self . primes . len ( ) > 2 {
105
- return Err ( pkcs1:: Error :: Version . into ( ) ) ;
92
+ return Err ( pkcs1:: Error :: Crypto ) ;
106
93
}
107
94
108
95
let modulus = self . n ( ) . to_be_bytes ( ) ;
@@ -134,7 +121,7 @@ impl EncodePrivateKey for RsaPrivateKey {
134
121
. to_be_bytes ( ) ,
135
122
) ;
136
123
137
- let private_key = pkcs1:: RsaPrivateKey {
124
+ Ok ( SecretDocument :: encode_msg ( & pkcs1:: RsaPrivateKey {
138
125
modulus : pkcs1:: UintRef :: new ( & modulus) ?,
139
126
public_exponent : pkcs1:: UintRef :: new ( & public_exponent) ?,
140
127
private_exponent : pkcs1:: UintRef :: new ( & private_exponent) ?,
@@ -144,27 +131,86 @@ impl EncodePrivateKey for RsaPrivateKey {
144
131
exponent2 : pkcs1:: UintRef :: new ( & exponent2) ?,
145
132
coefficient : pkcs1:: UintRef :: new ( & coefficient) ?,
146
133
other_prime_infos : None ,
134
+ } ) ?)
135
+ }
136
+ }
137
+
138
+ impl pkcs1:: EncodeRsaPublicKey for RsaPublicKey {
139
+ fn to_pkcs1_der ( & self ) -> pkcs1:: Result < Document > {
140
+ let modulus = self . n ( ) . to_be_bytes ( ) ;
141
+ let public_exponent = self . e ( ) . to_be_bytes ( ) ;
142
+
143
+ Ok ( Document :: encode_msg ( & pkcs1:: RsaPublicKey {
144
+ modulus : pkcs1:: UintRef :: new ( & modulus) ?,
145
+ public_exponent : pkcs1:: UintRef :: new ( & public_exponent) ?,
146
+ } ) ?)
147
+ }
148
+ }
149
+
150
+ // PKCS#8
151
+
152
+ /// Verify that the `AlgorithmIdentifier` for a key is correct.
153
+ pub ( crate ) fn verify_algorithm_id ( algorithm : & spki:: AlgorithmIdentifierRef ) -> spki:: Result < ( ) > {
154
+ match algorithm. oid {
155
+ pkcs1:: ALGORITHM_OID => {
156
+ if algorithm. parameters_any ( ) ? != pkcs8:: der:: asn1:: Null . into ( ) {
157
+ return Err ( spki:: Error :: KeyMalformed ) ;
158
+ }
159
+ }
160
+ ID_RSASSA_PSS => {
161
+ if algorithm. parameters . is_some ( ) {
162
+ return Err ( spki:: Error :: KeyMalformed ) ;
163
+ }
147
164
}
148
- . to_der ( ) ?;
165
+ _ => return Err ( spki:: Error :: OidUnknown { oid : algorithm. oid } ) ,
166
+ } ;
167
+
168
+ Ok ( ( ) )
169
+ }
170
+
171
+ impl TryFrom < pkcs8:: PrivateKeyInfoRef < ' _ > > for RsaPrivateKey {
172
+ type Error = pkcs8:: Error ;
173
+
174
+ fn try_from ( private_key_info : pkcs8:: PrivateKeyInfoRef < ' _ > ) -> pkcs8:: Result < Self > {
175
+ verify_algorithm_id ( & private_key_info. algorithm ) ?;
176
+
177
+ pkcs1:: RsaPrivateKey :: try_from ( private_key_info. private_key )
178
+ . and_then ( TryInto :: try_into)
179
+ . map_err ( pkcs1_error_to_pkcs8)
180
+ }
181
+ }
182
+
183
+ impl TryFrom < pkcs8:: SubjectPublicKeyInfoRef < ' _ > > for RsaPublicKey {
184
+ type Error = spki:: Error ;
185
+
186
+ fn try_from ( spki : pkcs8:: SubjectPublicKeyInfoRef < ' _ > ) -> spki:: Result < Self > {
187
+ use spki:: Error :: KeyMalformed ;
188
+
189
+ verify_algorithm_id ( & spki. algorithm ) ?;
190
+
191
+ pkcs1:: RsaPublicKey :: try_from ( spki. subject_public_key . as_bytes ( ) . ok_or ( KeyMalformed ) ?)
192
+ . and_then ( TryInto :: try_into)
193
+ . map_err ( pkcs1_error_to_spki)
194
+ }
195
+ }
196
+
197
+ impl EncodePrivateKey for RsaPrivateKey {
198
+ fn to_pkcs8_der ( & self ) -> pkcs8:: Result < SecretDocument > {
199
+ let private_key =
200
+ pkcs1:: EncodeRsaPrivateKey :: to_pkcs1_der ( self ) . map_err ( pkcs1_error_to_pkcs8) ?;
149
201
150
202
pkcs8:: PrivateKeyInfoRef :: new (
151
203
pkcs1:: ALGORITHM_ID ,
152
- OctetStringRef :: new ( private_key. as_ref ( ) ) ?,
204
+ OctetStringRef :: new ( private_key. as_bytes ( ) ) ?,
153
205
)
154
206
. try_into ( )
155
207
}
156
208
}
157
209
158
210
impl EncodePublicKey for RsaPublicKey {
159
211
fn to_public_key_der ( & self ) -> spki:: Result < Document > {
160
- let modulus = self . n ( ) . to_be_bytes ( ) ;
161
- let public_exponent = self . e ( ) . to_be_bytes ( ) ;
162
-
163
- let subject_public_key = pkcs1:: RsaPublicKey {
164
- modulus : pkcs1:: UintRef :: new ( & modulus) ?,
165
- public_exponent : pkcs1:: UintRef :: new ( & public_exponent) ?,
166
- }
167
- . to_der ( ) ?;
212
+ let subject_public_key =
213
+ pkcs1:: EncodeRsaPublicKey :: to_pkcs1_der ( self ) . map_err ( pkcs1_error_to_spki) ?;
168
214
169
215
pkcs8:: SubjectPublicKeyInfoRef {
170
216
algorithm : pkcs1:: ALGORITHM_ID ,
@@ -176,3 +222,19 @@ impl EncodePublicKey for RsaPublicKey {
176
222
. try_into ( )
177
223
}
178
224
}
225
+
226
+ /// Convert `pkcs1::Result` to `pkcs8::Result`.
227
+ fn pkcs1_error_to_pkcs8 ( error : pkcs1:: Error ) -> pkcs8:: Error {
228
+ match error {
229
+ pkcs1:: Error :: Asn1 ( e) => pkcs8:: Error :: Asn1 ( e) ,
230
+ _ => pkcs8:: Error :: KeyMalformed ,
231
+ }
232
+ }
233
+
234
+ /// Convert `pkcs1::Result` to `spki::Result`.
235
+ fn pkcs1_error_to_spki ( error : pkcs1:: Error ) -> spki:: Error {
236
+ match error {
237
+ pkcs1:: Error :: Asn1 ( e) => spki:: Error :: Asn1 ( e) ,
238
+ _ => spki:: Error :: KeyMalformed ,
239
+ }
240
+ }
0 commit comments