@@ -7,14 +7,12 @@ use deno_core::JsBuffer;
7
7
use deno_core:: ToJsBuffer ;
8
8
use elliptic_curve:: pkcs8:: PrivateKeyInfo ;
9
9
use p256:: pkcs8:: EncodePrivateKey ;
10
- use ring:: signature:: EcdsaKeyPair ;
11
10
use rsa:: pkcs1:: UintRef ;
12
11
use rsa:: pkcs8:: der:: Encode ;
13
12
use serde:: Deserialize ;
14
13
use serde:: Serialize ;
15
14
use spki:: der:: Decode ;
16
15
17
- use crate :: key:: CryptoNamedCurve ;
18
16
use crate :: shared:: * ;
19
17
20
18
#[ derive( Deserialize ) ]
@@ -45,7 +43,9 @@ pub enum KeyData {
45
43
y : String ,
46
44
} ,
47
45
JwkPrivateEc {
46
+ #[ allow( dead_code) ]
48
47
x : String ,
48
+ #[ allow( dead_code) ]
49
49
y : String ,
50
50
d : String ,
51
51
} ,
@@ -543,9 +543,7 @@ fn import_key_ec_jwk(
543
543
raw_data : RustRawKeyData :: Public ( point_bytes. into ( ) ) ,
544
544
} )
545
545
}
546
- KeyData :: JwkPrivateEc { d, x, y } => {
547
- jwt_b64_int_or_err ! ( private_d, & d, "invalid JWK private key" ) ;
548
- let point_bytes = import_key_ec_jwk_to_point ( x, y, named_curve) ?;
546
+ KeyData :: JwkPrivateEc { d, .. } => {
549
547
let pkcs8_der = match named_curve {
550
548
EcNamedCurve :: P256 => {
551
549
let d = decode_b64url_to_field_bytes :: < p256:: NistP256 > ( & d) ?;
@@ -562,27 +560,14 @@ fn import_key_ec_jwk(
562
560
. map_err ( |_| data_error ( "invalid JWK private key" ) ) ?
563
561
}
564
562
EcNamedCurve :: P521 => {
565
- return Err ( data_error ( "Unsupported named curve" ) )
566
- }
567
- } ;
563
+ let d = decode_b64url_to_field_bytes :: < p521:: NistP521 > ( & d) ?;
564
+ let pk = p521:: SecretKey :: from_bytes ( & d) ?;
568
565
569
- // Import using ring, to validate key
570
- let key_alg = match named_curve {
571
- EcNamedCurve :: P256 => CryptoNamedCurve :: P256 . into ( ) ,
572
- EcNamedCurve :: P384 => CryptoNamedCurve :: P256 . into ( ) ,
573
- EcNamedCurve :: P521 => {
574
- return Err ( data_error ( "Unsupported named curve" ) )
566
+ pk. to_pkcs8_der ( )
567
+ . map_err ( |_| data_error ( "invalid JWK private key" ) ) ?
575
568
}
576
569
} ;
577
570
578
- let rng = ring:: rand:: SystemRandom :: new ( ) ;
579
- let _key_pair = EcdsaKeyPair :: from_private_key_and_public_key (
580
- key_alg,
581
- private_d. as_bytes ( ) ,
582
- point_bytes. as_ref ( ) ,
583
- & rng,
584
- ) ;
585
-
586
571
Ok ( ImportKeyResult :: Ec {
587
572
raw_data : RustRawKeyData :: Private ( pkcs8_der. as_bytes ( ) . to_vec ( ) . into ( ) ) ,
588
573
} )
@@ -649,24 +634,15 @@ fn import_key_ec(
649
634
} )
650
635
}
651
636
KeyData :: Pkcs8 ( data) => {
652
- // 2-7
653
- // Deserialize PKCS8 - validate structure, extracts named_curve
654
- let named_curve_alg = match named_curve {
655
- EcNamedCurve :: P256 | EcNamedCurve :: P384 => {
656
- let pk = PrivateKeyInfo :: from_der ( data. as_ref ( ) )
657
- . map_err ( |_| data_error ( "expected valid PKCS#8 data" ) ) ?;
658
- pk. algorithm
659
- . parameters
660
- . ok_or_else ( || data_error ( "malformed parameters" ) ) ?
661
- . try_into ( )
662
- . unwrap ( )
663
- }
664
- EcNamedCurve :: P521 => {
665
- return Err ( data_error ( "Unsupported named curve" ) )
666
- }
667
- } ;
637
+ let pk = PrivateKeyInfo :: from_der ( data. as_ref ( ) )
638
+ . map_err ( |_| data_error ( "expected valid PKCS#8 data" ) ) ?;
639
+ let named_curve_alg = pk
640
+ . algorithm
641
+ . parameters
642
+ . ok_or_else ( || data_error ( "malformed parameters" ) ) ?
643
+ . try_into ( )
644
+ . unwrap ( ) ;
668
645
669
- // 8-9.
670
646
let pk_named_curve = match named_curve_alg {
671
647
// id-secp256r1
672
648
ID_SECP256R1_OID => Some ( EcNamedCurve :: P256 ) ,
@@ -677,27 +653,8 @@ fn import_key_ec(
677
653
_ => None ,
678
654
} ;
679
655
680
- // 10.
681
- if let Some ( pk_named_curve) = pk_named_curve {
682
- let signing_alg = match pk_named_curve {
683
- EcNamedCurve :: P256 => CryptoNamedCurve :: P256 . into ( ) ,
684
- EcNamedCurve :: P384 => CryptoNamedCurve :: P384 . into ( ) ,
685
- EcNamedCurve :: P521 => {
686
- return Err ( data_error ( "Unsupported named curve" ) )
687
- }
688
- } ;
689
-
690
- let rng = ring:: rand:: SystemRandom :: new ( ) ;
691
- // deserialize pkcs8 using ring crate, to VALIDATE public key
692
- let _private_key = EcdsaKeyPair :: from_pkcs8 ( signing_alg, & data, & rng)
693
- . map_err ( |_| data_error ( "invalid key" ) ) ?;
694
-
695
- // 11.
696
- if named_curve != pk_named_curve {
697
- return Err ( data_error ( "curve mismatch" ) ) ;
698
- }
699
- } else {
700
- return Err ( data_error ( "Unsupported named curve" ) ) ;
656
+ if pk_named_curve != Some ( named_curve) {
657
+ return Err ( data_error ( "curve mismatch" ) ) ;
701
658
}
702
659
703
660
Ok ( ImportKeyResult :: Ec {
0 commit comments