33 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
44
55mod aes_operation;
6+ mod ecdh_operation;
67mod ed25519_operation;
78mod hkdf_operation;
89mod hmac_operation;
@@ -27,9 +28,9 @@ use crate::dom::bindings::codegen::Bindings::CryptoKeyBinding::{
2728} ;
2829use crate :: dom:: bindings:: codegen:: Bindings :: SubtleCryptoBinding :: {
2930 AesCbcParams , AesCtrParams , AesDerivedKeyParams , AesGcmParams , AesKeyAlgorithm ,
30- AesKeyGenParams , Algorithm , AlgorithmIdentifier , HkdfParams , HmacImportParams ,
31- HmacKeyAlgorithm , HmacKeyGenParams , JsonWebKey , KeyAlgorithm , KeyFormat , Pbkdf2Params ,
32- RsaOtherPrimesInfo , SubtleCryptoMethods ,
31+ AesKeyGenParams , Algorithm , AlgorithmIdentifier , EcKeyAlgorithm , EcKeyImportParams , HkdfParams ,
32+ HmacImportParams , HmacKeyAlgorithm , HmacKeyGenParams , JsonWebKey , KeyAlgorithm , KeyFormat ,
33+ Pbkdf2Params , RsaOtherPrimesInfo , SubtleCryptoMethods ,
3334} ;
3435use crate :: dom:: bindings:: codegen:: UnionTypes :: {
3536 ArrayBufferViewOrArrayBuffer , ArrayBufferViewOrArrayBufferOrJsonWebKey , ObjectOrString ,
@@ -89,7 +90,7 @@ static SUPPORTED_ALGORITHMS: &[&str] = &[
8990const NAMED_CURVE_P256 : & str = "P-256" ;
9091const NAMED_CURVE_P384 : & str = "P-384" ;
9192const NAMED_CURVE_P521 : & str = "P-521" ;
92- # [ allow ( dead_code ) ]
93+
9394static SUPPORTED_CURVES : & [ & str ] = & [ NAMED_CURVE_P256 , NAMED_CURVE_P384 , NAMED_CURVE_P521 ] ;
9495
9596/// <https://w3c.github.io/webcrypto/#supported-operation>
@@ -1625,6 +1626,48 @@ impl SafeToJSValConvertible for SubtleKeyAlgorithm {
16251626 }
16261627}
16271628
1629+ /// <https://w3c.github.io/webcrypto/#dfn-EcKeyAlgorithm>
1630+ #[ derive( Clone , Debug , MallocSizeOf ) ]
1631+ pub ( crate ) struct SubtleEcKeyAlgorithm {
1632+ /// <https://w3c.github.io/webcrypto/#dom-keyalgorithm-name>
1633+ name : String ,
1634+
1635+ /// <https://w3c.github.io/webcrypto/#dfn-EcKeyAlgorithm-namedCurve>
1636+ named_curve : String ,
1637+ }
1638+
1639+ impl SafeToJSValConvertible for SubtleEcKeyAlgorithm {
1640+ fn safe_to_jsval ( & self , cx : JSContext , rval : MutableHandleValue , can_gc : CanGc ) {
1641+ let parent = KeyAlgorithm {
1642+ name : self . name . clone ( ) . into ( ) ,
1643+ } ;
1644+ let dictionary = EcKeyAlgorithm {
1645+ parent,
1646+ namedCurve : self . named_curve . clone ( ) . into ( ) ,
1647+ } ;
1648+ dictionary. safe_to_jsval ( cx, rval, can_gc) ;
1649+ }
1650+ }
1651+
1652+ /// <https://w3c.github.io/webcrypto/#dfn-EcKeyImportParams>
1653+ #[ derive( Clone , Debug , MallocSizeOf ) ]
1654+ struct SubtleEcKeyImportParams {
1655+ /// <https://w3c.github.io/webcrypto/#dom-algorithm-name>
1656+ name : String ,
1657+
1658+ /// <https://w3c.github.io/webcrypto/#dfn-EcKeyImportParams-namedCurve>
1659+ named_curve : String ,
1660+ }
1661+
1662+ impl From < EcKeyImportParams > for SubtleEcKeyImportParams {
1663+ fn from ( value : EcKeyImportParams ) -> Self {
1664+ SubtleEcKeyImportParams {
1665+ name : value. parent . name . to_string ( ) ,
1666+ named_curve : value. namedCurve . to_string ( ) ,
1667+ }
1668+ }
1669+ }
1670+
16281671#[ derive( Clone , Debug , MallocSizeOf ) ]
16291672pub ( crate ) struct SubtleAesCbcParams {
16301673 pub ( crate ) name : String ,
@@ -1936,6 +1979,7 @@ pub(crate) enum ExportedKey {
19361979#[ allow( clippy:: enum_variant_names) ]
19371980pub ( crate ) enum KeyAlgorithmAndDerivatives {
19381981 KeyAlgorithm ( SubtleKeyAlgorithm ) ,
1982+ EcKeyAlgorithm ( SubtleEcKeyAlgorithm ) ,
19391983 AesKeyAlgorithm ( SubtleAesKeyAlgorithm ) ,
19401984 HmacKeyAlgorithm ( SubtleHmacKeyAlgorithm ) ,
19411985}
@@ -1944,6 +1988,7 @@ impl KeyAlgorithmAndDerivatives {
19441988 fn name ( & self ) -> & str {
19451989 match self {
19461990 KeyAlgorithmAndDerivatives :: KeyAlgorithm ( algo) => & algo. name ,
1991+ KeyAlgorithmAndDerivatives :: EcKeyAlgorithm ( algo) => & algo. name ,
19471992 KeyAlgorithmAndDerivatives :: AesKeyAlgorithm ( algo) => & algo. name ,
19481993 KeyAlgorithmAndDerivatives :: HmacKeyAlgorithm ( algo) => & algo. name ,
19491994 }
@@ -1962,6 +2007,9 @@ impl SafeToJSValConvertible for KeyAlgorithmAndDerivatives {
19622007 fn safe_to_jsval ( & self , cx : JSContext , rval : MutableHandleValue , can_gc : CanGc ) {
19632008 match self {
19642009 KeyAlgorithmAndDerivatives :: KeyAlgorithm ( algo) => algo. safe_to_jsval ( cx, rval, can_gc) ,
2010+ KeyAlgorithmAndDerivatives :: EcKeyAlgorithm ( algo) => {
2011+ algo. safe_to_jsval ( cx, rval, can_gc)
2012+ } ,
19652013 KeyAlgorithmAndDerivatives :: AesKeyAlgorithm ( algo) => {
19662014 algo. safe_to_jsval ( cx, rval, can_gc)
19672015 } ,
@@ -2129,6 +2177,7 @@ impl JsonWebKeyExt for JsonWebKey {
21292177#[ derive( Clone , Debug , MallocSizeOf ) ]
21302178enum NormalizedAlgorithm {
21312179 Algorithm ( SubtleAlgorithm ) ,
2180+ EcKeyImportParams ( SubtleEcKeyImportParams ) ,
21322181 AesCtrParams ( SubtleAesCtrParams ) ,
21332182 AesKeyGenParams ( SubtleAesKeyGenParams ) ,
21342183 AesDerivedKeyParams ( SubtleAesDerivedKeyParams ) ,
@@ -2223,6 +2272,14 @@ fn normalize_algorithm(
22232272 // NOTE: Step 10.1.3 is done by the `From` and `TryFrom` trait implementation of
22242273 // "subtle" binding structs.
22252274 let normalized_algorithm = match ( alg_name, op) {
2275+ // <https://w3c.github.io/webcrypto/#ecdh-registration>
2276+ ( ALG_ECDH , Operation :: ImportKey ) => {
2277+ let mut params =
2278+ dictionary_from_jsval :: < EcKeyImportParams > ( cx, value. handle ( ) ) ?;
2279+ params. parent . name = DOMString :: from ( alg_name) ;
2280+ NormalizedAlgorithm :: EcKeyImportParams ( params. into ( ) )
2281+ } ,
2282+
22262283 // <https://w3c.github.io/webcrypto/#ed25519-registration>
22272284 ( ALG_ED25519 , Operation :: Sign ) => {
22282285 let mut params = dictionary_from_jsval :: < Algorithm > ( cx, value. handle ( ) ) ?;
@@ -2517,6 +2574,7 @@ impl NormalizedAlgorithm {
25172574 fn name ( & self ) -> & str {
25182575 match self {
25192576 NormalizedAlgorithm :: Algorithm ( algo) => & algo. name ,
2577+ NormalizedAlgorithm :: EcKeyImportParams ( algo) => & algo. name ,
25202578 NormalizedAlgorithm :: AesCtrParams ( algo) => & algo. name ,
25212579 NormalizedAlgorithm :: AesKeyGenParams ( algo) => & algo. name ,
25222580 NormalizedAlgorithm :: AesDerivedKeyParams ( algo) => & algo. name ,
@@ -2702,6 +2760,15 @@ impl NormalizedAlgorithm {
27022760 } ,
27032761 _ => Err ( Error :: NotSupported ) ,
27042762 } ,
2763+ NormalizedAlgorithm :: EcKeyImportParams ( algo) => ecdh_operation:: import_key (
2764+ global,
2765+ algo,
2766+ format,
2767+ key_data,
2768+ extractable,
2769+ usages,
2770+ can_gc,
2771+ ) ,
27052772 NormalizedAlgorithm :: HmacImportParams ( algo) => hmac_operation:: import_key (
27062773 global,
27072774 algo,
0 commit comments