@@ -12,7 +12,7 @@ use crate::aws_lc::{
12
12
} ;
13
13
#[ cfg( feature = "ring-io" ) ]
14
14
use crate :: aws_lc:: { RSA_get0_e , RSA_get0_n } ;
15
- use crate :: encoding:: { AsDer , Pkcs8V1Der } ;
15
+ use crate :: encoding:: { AsDer , Pkcs8V1Der , PublicKeyX509Der } ;
16
16
use crate :: error:: { KeyRejected , Unspecified } ;
17
17
#[ cfg( feature = "ring-io" ) ]
18
18
use crate :: io;
@@ -32,6 +32,7 @@ use core::ptr::null_mut;
32
32
use std:: os:: raw:: c_int;
33
33
34
34
use crate :: pkcs8:: Version ;
35
+ use crate :: rsa:: encoding:: { rfc5280, rfc8017} ;
35
36
use crate :: rsa:: signature:: configure_rsa_pkcs1_pss_padding;
36
37
#[ cfg( feature = "ring-io" ) ]
37
38
use untrusted:: Input ;
@@ -288,7 +289,7 @@ impl Drop for PublicKey {
288
289
}
289
290
290
291
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 > {
292
293
let key = encoding:: rfc8017:: encode_public_key_der ( evp_pkey) ?;
293
294
#[ cfg( feature = "ring-io" ) ]
294
295
{
@@ -307,6 +308,17 @@ impl PublicKey {
307
308
#[ cfg( not( feature = "ring-io" ) ) ]
308
309
Ok ( PublicKey { key } )
309
310
}
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
+ }
310
322
}
311
323
312
324
impl Debug for PublicKey {
@@ -325,6 +337,14 @@ impl AsRef<[u8]> for PublicKey {
325
337
}
326
338
}
327
339
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
+
328
348
#[ cfg( feature = "ring-io" ) ]
329
349
impl PublicKey {
330
350
/// The public modulus (n).
0 commit comments