Skip to content

Truly independent web browser

LibWeb: Port WebCrypto OKP algorithms to Rust

Shannon Boothaeba5a5134b7
diff --git a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs
index ebb2f86c7d4..eb55c2c2cd1 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs
@@ -146,7 +146,6 @@ pub enum Base64DecodeResult {
pub enum Asn1Structure {
SubjectPublicKeyInfo,
PrivateKeyInfo,
- #[allow(dead_code, reason = "the native ASN.1 callers supply this structure")]
OctetString,
}
@@ -327,10 +326,6 @@ pub unsafe extern "C" fn web_rust_crypto_parse_an_asn1_structure(
#[derive(Clone, Copy)]
#[repr(u8)]
pub(crate) enum AlgorithmId {
- X25519,
- X448,
- ED25519,
- ED448,
MlDsa,
MlKem,
}
@@ -343,8 +338,6 @@ pub(crate) enum AlgorithmId {
)]
pub(crate) enum ParameterId {
AlgorithmParams,
- EcdhKeyDeriveParams,
- Ed448Params,
ContextParams,
}
@@ -11469,6 +11462,2633 @@ impl AlgorithmMethods for Ecdh {
}
}
+#[derive(Clone, Trace)]
+#[gc(crate = libjs_rust)]
+struct Ed448Params {
+ context: Option<Bytes>,
+}
+
+impl Ed448Params {
+ fn from_value(vm: &Vm, object: Gc<Object>) -> WebIdlResult<Self> {
+ let mut context = None;
+ let context_property = PropertyKey::from_utf8("context");
+ if object.has_property(vm, &context_property)? {
+ let context_value = object.get(vm, &context_property)?;
+ let Some(context_source) = buffer_source_type_of(context_value) else {
+ return vm
+ .throw_completion(ErrorKind::TypeError, ErrorType::NotAnObjectOfType, &[&"BufferSource"])
+ .map_err(Into::into);
+ };
+ context = Some(get_a_copy_of_the_bytes(vm, context_source)?);
+ }
+ Ok(Self { context })
+ }
+}
+
+#[derive(Clone, Copy)]
+#[repr(u8)]
+enum OkpCurve {
+ Ed25519,
+ Ed448,
+ X25519,
+ X448,
+}
+
+unsafe extern "C" {
+ fn web_crypto_okp_generate_private_key(curve: OkpCurve, state: *mut c_void, receive: ReceiveBytes) -> bool;
+ fn web_crypto_okp_generate_public_key(
+ curve: OkpCurve,
+ private_key: Span<'_, u8>,
+ state: *mut c_void,
+ receive: ReceiveBytes,
+ ) -> bool;
+ fn web_crypto_okp_compute_coordinate(
+ curve: OkpCurve,
+ private_key: Span<'_, u8>,
+ public_key: Span<'_, u8>,
+ state: *mut c_void,
+ receive: ReceiveBytes,
+ ) -> bool;
+ fn web_crypto_okp_sign(
+ curve: OkpCurve,
+ private_key: Span<'_, u8>,
+ message: Span<'_, u8>,
+ context: Span<'_, u8>,
+ state: *mut c_void,
+ receive: ReceiveBytes,
+ ) -> bool;
+ fn web_crypto_okp_verify(
+ curve: OkpCurve,
+ public_key: Span<'_, u8>,
+ signature: Span<'_, u8>,
+ message: Span<'_, u8>,
+ context: Span<'_, u8>,
+ verified: &mut bool,
+ ) -> usize;
+ fn web_crypto_okp_key_info_has_oid(curve: OkpCurve, structure: Asn1Structure, key_info: *const c_void) -> bool;
+ fn web_crypto_asn1_key_info_raw_key(structure: Asn1Structure, key_info: *const c_void, bytes: &mut Span<'_, u8>);
+ fn web_crypto_asn1_octet_string_bytes(string: *const c_void, bytes: &mut Span<'_, u8>);
+ fn web_crypto_okp_export_der(
+ curve: OkpCurve,
+ is_public: bool,
+ key: Span<'_, u8>,
+ state: *mut c_void,
+ receive: ReceiveBytes,
+ ) -> bool;
+}
+
+impl Asn1KeyInfo {
+ fn has_okp_oid(&self, curve: OkpCurve) -> bool {
+ // SAFETY: This object owns the live structure.
+ unsafe { web_crypto_okp_key_info_has_oid(curve, self.structure, self.key_info.as_ptr()) }
+ }
+
+ fn raw_key(&self) -> &[u8] {
+ let mut bytes = Span::new(&[]);
+ // SAFETY: This object owns the structure that the bytes borrow from.
+ unsafe { web_crypto_asn1_key_info_raw_key(self.structure, self.key_info.as_ptr(), &mut bytes) };
+ bytes.as_slice()
+ }
+}
+
+fn parse_octet_string(vm: &Vm, bytes: &[u8]) -> WebIdlResult<Vec<u8>> {
+ // SAFETY: The service allocates an empty native StringView.
+ let string = unsafe { web_crypto_asn1_allocate_key_info(Asn1Structure::OctetString) };
+ let string = Asn1KeyInfo {
+ structure: Asn1Structure::OctetString,
+ key_info: NonNull::new(string).expect("LibCrypto returns an ASN.1 view"),
+ };
+ // SAFETY: The bytes and the native StringView live for the call.
+ unsafe {
+ parse_an_asn1_structure(
+ Span::new(bytes),
+ Asn1Structure::OctetString,
+ true,
+ string.key_info.as_ptr(),
+ )
+ }
+ .map_err(|message| Exception::Dom("DataError", message))?;
+ let mut octets = Span::new(&[]);
+ // SAFETY: The StringView borrows from the input bytes for the duration of the copy.
+ unsafe { web_crypto_asn1_octet_string_bytes(string.key_info.as_ptr(), &mut octets) };
+ copy_bytes(vm, octets.as_slice())
+}
+
+fn okp_generate_private_key(curve: OkpCurve) -> Result<Bytes, ()> {
+ let mut key: Option<Bytes> = None;
+ // SAFETY: The output slot is lent for the call.
+ if !unsafe { web_crypto_okp_generate_private_key(curve, core::ptr::from_mut(&mut key).cast(), receive_bytes) } {
+ return Err(());
+ }
+ Ok(key.expect("LibCrypto returns the private key"))
+}
+
+fn okp_generate_public_key(curve: OkpCurve, private_key: &[u8]) -> Result<Bytes, ()> {
+ let mut key: Option<Bytes> = None;
+ // SAFETY: The private key and output slot are lent for the call.
+ if !unsafe {
+ web_crypto_okp_generate_public_key(
+ curve,
+ Span::new(private_key),
+ core::ptr::from_mut(&mut key).cast(),
+ receive_bytes,
+ )
+ } {
+ return Err(());
+ }
+ Ok(key.expect("LibCrypto returns the public key"))
+}
+
+fn okp_export_der(vm: &Vm, curve: OkpCurve, is_public: bool, key: &[u8]) -> WebIdlResult<Bytes> {
+ let mut result: Option<Bytes> = None;
+ // SAFETY: The key and the output slot are lent for the call.
+ if !unsafe {
+ web_crypto_okp_export_der(
+ curve,
+ is_public,
+ Span::new(key),
+ core::ptr::from_mut(&mut result).cast(),
+ receive_bytes,
+ )
+ } {
+ return vm
+ .throw_completion(ErrorKind::InternalError, ErrorType::OutOfMemory, &[])
+ .map_err(Into::into);
+ }
+ Ok(result.expect("LibCrypto returns the DER bytes"))
+}
+
+fn okp_encode_jwk_bytes(vm: &Vm, bytes: &[u8]) -> WebIdlResult<Utf16String> {
+ let Ok(encoded) = base64_url_bytes_encode(bytes) else {
+ return vm
+ .throw_completion(ErrorKind::InternalError, ErrorType::OutOfMemory, &[])
+ .map_err(Into::into);
+ };
+ Ok(encoded)
+}
+
+fn out_of_memory(vm: &Vm) -> Exception {
+ vm.throw_completion::<()>(ErrorKind::InternalError, ErrorType::OutOfMemory, &[])
+ .expect_err("creating an out-of-memory exception throws")
+ .into()
+}
+
+#[derive(Clone, Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct Ed25519;
+
+impl AlgorithmMethods for Ed25519 {
+ // https://w3c.github.io/webcrypto/#ed25519-operations-generate-key
+ fn generate_key(
+ &self,
+ cx: &CallContext,
+ _: &AlgorithmParams,
+ extractable: bool,
+ key_usages: &[KeyUsage],
+ ) -> WebIdlResult<CryptoKeyOrCryptoKeyPair> {
+ // 1. If usages contains an entry which is not one of "sign" or "verify", then throw a SyntaxError.
+ for &usage in key_usages {
+ if usage != KeyUsage::Sign && usage != KeyUsage::Verify {
+ return Err(Exception::Dom(
+ "SyntaxError",
+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),
+ ));
+ }
+ }
+
+ // 2. Generate an Ed25519 key pair, as defined in [RFC8032], section 5.1.5.
+ let maybe_private_key = okp_generate_private_key(OkpCurve::Ed25519);
+ let Ok(private_key_data) = maybe_private_key else {
+ return Err(dom_error("OperationError", b"Failed to generate private key"));
+ };
+
+ let maybe_public_key = okp_generate_public_key(OkpCurve::Ed25519, private_key_data.as_slice());
+ let Ok(public_key_data) = maybe_public_key else {
+ return Err(dom_error("OperationError", b"Failed to generate public key"));
+ };
+
+ // 5. Let publicKey be a new CryptoKey associated with the relevant global object of this [HTML],
+ // and representing the public key of the generated key pair.
+ let public_key = CryptoKey::from_okp_handle(cx.vm(), false, public_key_data.as_slice());
+
+ // 6. Set the [[type]] internal slot of publicKey to "public"
+ public_key.set_type(KeyType::Public);
+
+ // 3. Let algorithm be a new KeyAlgorithm object.
+ // 4. Set the name attribute of algorithm to "Ed25519".
+ // 7. Set the [[algorithm]] internal slot of publicKey to algorithm.
+ public_key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"Ed25519"),
+ });
+
+ // 8. Set the [[extractable]] internal slot of publicKey to true.
+ public_key.set_extractable(true);
+
+ // 9. Set the [[usages]] internal slot of publicKey to be the usage intersection of usages and [ "verify" ].
+ public_key.set_usages(usage_intersection(key_usages, &[KeyUsage::Verify]));
+
+ // 10. Let privateKey be a new CryptoKey associated with the relevant global object of this [HTML],
+ // and representing the private key of the generated key pair.
+ let private_key = CryptoKey::from_okp_handle(cx.vm(), true, private_key_data.as_slice());
+
+ // 11. Set the [[type]] internal slot of privateKey to "private"
+ private_key.set_type(KeyType::Private);
+
+ // 12. Set the [[algorithm]] internal slot of privateKey to algorithm.
+ private_key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"Ed25519"),
+ });
+
+ // 13. Set the [[extractable]] internal slot of privateKey to extractable.
+ private_key.set_extractable(extractable);
+
+ // 14. Set the [[usages]] internal slot of privateKey to be the usage intersection of usages and [ "sign" ].
+ private_key.set_usages(usage_intersection(key_usages, &[KeyUsage::Sign]));
+
+ // 15. Let result be a new CryptoKeyPair dictionary.
+ // 16. Set the publicKey attribute of result to be publicKey.
+ // 17. Set the privateKey attribute of result to be privateKey.
+ // 18. Return the result of converting result to an ECMAScript Object, as defined by [WebIDL].
+ Ok(CryptoKeyOrCryptoKeyPair::CryptoKeyPair(CryptoKeyPair {
+ public_key: Some(public_key),
+ private_key: Some(private_key),
+ }))
+ }
+
+ // https://w3c.github.io/webcrypto/#ed25519-operations-import-key
+ #[allow(
+ clippy::collapsible_if,
+ reason = "the specification checks key usage in a separate step"
+ )]
+ fn import_key(
+ &self,
+ cx: &CallContext,
+ _: &AlgorithmParams,
+ format: KeyFormat,
+ key_data: ImportKeyData,
+ extractable: bool,
+ usages: &[KeyUsage],
+ ) -> WebIdlResult<Ref<CryptoKey>> {
+ let key;
+
+ // 1. Let keyData be the key data to be imported.
+
+ // 2. If format is "spki":
+ if format == KeyFormat::Spki {
+ // 1. If usages contains an entry which is not "verify" then throw a SyntaxError.
+ for &usage in usages {
+ if usage != KeyUsage::Verify {
+ return Err(Exception::Dom(
+ "SyntaxError",
+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),
+ ));
+ }
+ }
+
+ // 2. Let spki be the result of running the parse a subjectPublicKeyInfo algorithm over keyData.
+ // 3. If an error occurred while parsing, then throw a DataError.
+ let ImportKeyData::Bytes(key_data) = key_data else {
+ unreachable!("DER key data contains bytes")
+ };
+ let spki = Asn1KeyInfo::parse(&key_data, Asn1Structure::SubjectPublicKeyInfo)?;
+
+ // 4. If the algorithm object identifier field of the algorithm AlgorithmIdentifier field of spki
+ // is not equal to the id-Ed25519 object identifier defined in [RFC8410], then throw a DataError.
+ if !spki.has_okp_oid(OkpCurve::Ed25519) {
+ return Err(dom_error("DataError", b"Invalid algorithm identifier"));
+ }
+
+ // 5. If the parameters field of the algorithm AlgorithmIdentifier field of spki is present, then throw a DataError.
+ if spki.ec_parameters().is_some() {
+ return Err(dom_error("DataError", b"Invalid algorithm parameters"));
+ }
+
+ // 6. Let publicKey be the Ed25519 public key identified by the subjectPublicKey field of spki.
+ let public_key = spki.raw_key();
+
+ // 7. Let key be a new CryptoKey associated with the relevant global object of this [HTML],
+ // and that represents publicKey.
+ key = CryptoKey::from_okp_handle(cx.vm(), false, public_key);
+
+ // 8. Set the [[type]] internal slot of key to "public"
+ key.set_type(KeyType::Public);
+
+ // 9. Let algorithm be a new KeyAlgorithm.
+ // 10. Set the name attribute of algorithm to "Ed25519".
+ // 11. Set the [[algorithm]] internal slot of key to algorithm.
+ key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"Ed25519"),
+ });
+ }
+ // 2. If format is "pkcs8":
+ else if format == KeyFormat::Pkcs8 {
+ // 1. If usages contains an entry which is not "sign" then throw a SyntaxError.
+ for &usage in usages {
+ if usage != KeyUsage::Sign {
+ return Err(Exception::Dom(
+ "SyntaxError",
+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),
+ ));
+ }
+ }
+
+ // 2. Let privateKeyInfo be the result of running the parse a privateKeyInfo algorithm over keyData.
+ // 3. If an error occurs while parsing, then throw a DataError.
+ let ImportKeyData::Bytes(key_data) = key_data else {
+ unreachable!("DER key data contains bytes")
+ };
+ let private_key_info = Asn1KeyInfo::parse(&key_data, Asn1Structure::PrivateKeyInfo)?;
+
+ // 4. If the algorithm object identifier field of the privateKeyAlgorithm PrivateKeyAlgorithm field
+ // of privateKeyInfo is not equal to the id-Ed25519 object identifier defined in [RFC8410], then throw a DataError.
+ if !private_key_info.has_okp_oid(OkpCurve::Ed25519) {
+ return Err(dom_error("DataError", b"Invalid algorithm identifier"));
+ }
+
+ // 5. If the parameters field of the privateKeyAlgorithm PrivateKeyAlgorithmIdentifier field of privateKeyInfo is present,
+ // then throw a DataError.
+ if private_key_info.ec_parameters().is_some() {
+ return Err(dom_error("DataError", b"Invalid algorithm parameters"));
+ }
+
+ // 6. Let curvePrivateKey be the result of performing the parse an ASN.1 structure algorithm,
+ // with data as the privateKey field of privateKeyInfo, structure as the ASN.1 CurvePrivateKey structure
+ // specified in Section 7 of [RFC8410], and exactData set to true.
+ // 7. If an error occurred while parsing, then throw a DataError.
+ let curve_private_key_bytes = parse_octet_string(cx.vm(), private_key_info.raw_key())?;
+
+ // 8. Let key be a new CryptoKey associated with the relevant global object of this [HTML],
+ // and that represents the Ed25519 private key identified by curvePrivateKey.
+ key = CryptoKey::from_okp_handle(cx.vm(), true, curve_private_key_bytes.as_slice());
+
+ // 9. Set the [[type]] internal slot of key to "private"
+ key.set_type(KeyType::Private);
+
+ // 10. Let algorithm be a new KeyAlgorithm.
+ // 11. Set the name attribute of algorithm to "Ed25519".
+ // 12. Set the [[algorithm]] internal slot of key to algorithm.
+ key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"Ed25519"),
+ });
+ }
+ // 2. If format is "jwk":
+ else if format == KeyFormat::Jwk {
+ // 1. If keyData is a JsonWebKey dictionary: Let jwk equal keyData.
+ // Otherwise: Throw a DataError.
+ let ImportKeyData::JsonWebKey(jwk) = key_data else {
+ return Err(dom_error("DataError", b"keyData is not a JsonWebKey dictionary"));
+ };
+
+ // 2. If the d field is present and usages contains a value which is not "sign",
+ // or, if the d field is not present and usages contains a value which is not "verify" then throw a SyntaxError.
+ if jwk.d.is_some() {
+ for &usage in usages {
+ if usage != KeyUsage::Sign {
+ return Err(Exception::Dom(
+ "SyntaxError",
+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),
+ ));
+ }
+ }
+ } else {
+ for &usage in usages {
+ if usage != KeyUsage::Verify {
+ return Err(Exception::Dom(
+ "SyntaxError",
+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),
+ ));
+ }
+ }
+ }
+
+ // 3. If the kty field of jwk is not "OKP", then throw a DataError.
+ if jwk.kty.as_ref() != Some(&Utf16String::from_ascii(b"OKP")) {
+ return Err(dom_error("DataError", b"Invalid key type"));
+ }
+
+ // 4. If the crv field of jwk is not "Ed25519", then throw a DataError.
+ if jwk.crv.as_ref() != Some(&Utf16String::from_ascii(b"Ed25519")) {
+ return Err(dom_error("DataError", b"Invalid curve"));
+ }
+
+ // 5. If the alg field of jwk is present and is not "Ed25519" or "EdDSA", then throw a DataError.
+ if jwk.alg.as_ref().is_some_and(|alg| {
+ *alg != Utf16String::from_ascii(b"Ed25519") && *alg != Utf16String::from_ascii(b"EdDSA")
+ }) {
+ return Err(dom_error("DataError", b"Invalid algorithm"));
+ }
+
+ // 6. If usages is non-empty and the use field of jwk is present and is not "sig", then throw a DataError.
+ if !usages.is_empty()
+ && jwk
+ .r#use
+ .as_ref()
+ .is_some_and(|key_use| *key_use != Utf16String::from_ascii(b"sig"))
+ {
+ return Err(dom_error("DataError", b"Invalid key usage"));
+ }
+
+ // 7. If the key_ops field of jwk is present, and is invalid according to the requirements of JSON Web Key [JWK],
+ // or it does not contain all of the specified usages values, then throw a DataError.
+ validate_jwk_key_ops(&jwk, usages)?;
+
+ // 8. If the ext field of jwk is present and has the value false and extractable is true, then throw a DataError.
+ if jwk.ext == Some(false) && extractable {
+ return Err(dom_error("DataError", b"Invalid extractable"));
+ }
+
+ // 9. If the d field is present:
+ if let Some(d) = &jwk.d {
+ // 1. If jwk does not meet the requirements of the JWK private key format described in Section 2 of [RFC8037],
+ // then throw a DataError.
+ // o The parameter "kty" MUST be "OKP".
+ if jwk.kty.as_ref() != Some(&Utf16String::from_ascii(b"OKP")) {
+ return Err(dom_error("DataError", b"Invalid key type"));
+ }
+
+ // https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve
+ // o The parameter "crv" MUST be present and contain the subtype of the key (from the "JSON Web Elliptic Curve" registry).
+ if jwk.crv.as_ref() != Some(&Utf16String::from_ascii(b"Ed25519")) {
+ return Err(dom_error("DataError", b"Invalid curve"));
+ }
+
+ // o The parameter "x" MUST be present and contain the public key encoded using the base64url [RFC4648] encoding.
+ if jwk.x.is_none() {
+ return Err(dom_error("DataError", b"Missing x field"));
+ }
+ let public_key = base64_url_bytes_decode(cx.vm(), jwk.x.as_ref().expect("the coordinate is present"))?;
+ if public_key.as_slice().len() != 32 {
+ return Err(dom_error("DataError", b"Invalid key length"));
+ }
+
+ // o The parameter "d" MUST be present for private keys and contain the private key encoded using the base64url encoding.
+ // This parameter MUST NOT be present for public keys.
+ if jwk.d.is_none() {
+ return Err(dom_error("DataError", b"Present d field"));
+ }
+ let private_key = base64_url_bytes_decode(cx.vm(), d)?;
+ if private_key.as_slice().len() != 32 {
+ return Err(dom_error("DataError", b"Invalid key length"));
+ }
+
+ let derived_public_key = okp_generate_public_key(OkpCurve::Ed25519, private_key.as_slice())
+ .map_err(|()| out_of_memory(cx.vm()))?;
+ if derived_public_key.as_slice() != public_key.as_slice() {
+ return Err(dom_error("DataError", b"Invalid key pair"));
+ }
+
+ // 2. Let key be a new CryptoKey object that represents the Ed25519 private key identified by interpreting jwk according to Section 2 of [RFC8037].
+ key = CryptoKey::from_okp_handle(cx.vm(), true, private_key.as_slice());
+
+ // 3. Set the [[type]] internal slot of Key to "private".
+ key.set_type(KeyType::Private);
+ }
+ // Otherwise:
+ else {
+ // 1. If jwk does not meet the requirements of the JWK public key format described in Section 2 of [RFC8037], then throw a DataError.
+ // o The parameter "kty" MUST be "OKP".
+ if jwk.kty.as_ref() != Some(&Utf16String::from_ascii(b"OKP")) {
+ return Err(dom_error("DataError", b"Invalid key type"));
+ }
+
+ // https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve
+ // o The parameter "crv" MUST be present and contain the subtype of the key (from the "JSON Web Elliptic Curve" registry).
+ if jwk.crv.as_ref() != Some(&Utf16String::from_ascii(b"Ed25519")) {
+ return Err(dom_error("DataError", b"Invalid curve"));
+ }
+
+ // o The parameter "x" MUST be present and contain the public key encoded using the base64url [RFC4648] encoding.
+ if jwk.x.is_none() {
+ return Err(dom_error("DataError", b"Missing x field"));
+ }
+ let public_key = base64_url_bytes_decode(cx.vm(), jwk.x.as_ref().expect("the coordinate is present"))?;
+ if public_key.as_slice().len() != 32 {
+ return Err(dom_error("DataError", b"Invalid key length"));
+ }
+
+ // o The parameter "d" MUST be present for private keys and contain the private key encoded using the base64url encoding.
+ // This parameter MUST NOT be present for public keys.
+ if jwk.d.is_some() {
+ return Err(dom_error("DataError", b"Present d field"));
+ }
+
+ // 2. Let key be a new CryptoKey object that represents the Ed25519 public key identified by interpreting jwk according to Section 2 of [RFC8037].
+ key = CryptoKey::from_okp_handle(cx.vm(), false, public_key.as_slice());
+
+ // 3. Set the [[type]] internal slot of Key to "public".
+ key.set_type(KeyType::Public);
+ }
+
+ // 10. Let algorithm be a new instance of a KeyAlgorithm object.
+ // 11. Set the name attribute of algorithm to "Ed25519".
+ // 12. Set the [[algorithm]] internal slot of key to algorithm.
+ key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"Ed25519"),
+ });
+ }
+ // 2. If format is "raw":
+ // https://wicg.github.io/webcrypto-modern-algos/#subtlecrypto-interface-keyformat
+ // For all existing asymmetric algorithms in WebCrypto, "raw-public" acts as an alias of "raw".
+ else if format == KeyFormat::Raw || format == KeyFormat::RawPublic {
+ // 1. If usages contains an entry which is not "verify" then throw a SyntaxError.
+ for &usage in usages {
+ if usage != KeyUsage::Verify {
+ return Err(Exception::Dom(
+ "SyntaxError",
+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),
+ ));
+ }
+ }
+
+ // 2. If the length in bits of keyData is not 256 then throw a DataError.
+ let ImportKeyData::Bytes(data) = key_data else {
+ unreachable!("raw key data contains bytes")
+ };
+ if data.as_slice().len() * 8 != 256 {
+ return Err(dom_error("DataError", b"Invalid key length"));
+ }
+
+ // 5. Let key be a new CryptoKey representing the key data provided in keyData.
+ key = CryptoKey::from_okp_handle(cx.vm(), false, data.as_slice());
+
+ // 6. Set the [[type]] internal slot of key to "public"
+ key.set_type(KeyType::Public);
+
+ // 3. Let algorithm be a new KeyAlgorithm object.
+ // 4. Set the name attribute of algorithm to "Ed25519".
+ // 7. Set the [[algorithm]] internal slot of key to algorithm.
+ key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"Ed25519"),
+ });
+ }
+ // 2. Otherwise:
+ else {
+ // throw a NotSupportedError.
+ return Err(dom_error("NotSupportedError", b"Invalid key format"));
+ }
+
+ // 3. Return key.
+ Ok(key)
+ }
+
+ // https://w3c.github.io/webcrypto/#ed25519-operations-export-key
+ #[allow(
+ clippy::field_reassign_with_default,
+ reason = "the algorithm sets JWK members in separate steps"
+ )]
+ fn export_key(&self, cx: &CallContext, format: KeyFormat, key: Ref<CryptoKey>) -> WebIdlResult<ExportKeyResult> {
+ // 1. Let key be the CryptoKey to be exported.
+
+ // 2. If the underlying cryptographic key material represented by the [[handle]] internal slot of key cannot be accessed, then throw an OperationError.
+ // Note: In our impl this is always accessible
+
+ // 3. If format is "spki":
+ if format == KeyFormat::Spki {
+ // 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
+ if key.key_type() != KeyType::Public {
+ return Err(dom_error("InvalidAccessError", b"Key is not a public key"));
+ }
+
+ // 2. Let data be an instance of the subjectPublicKeyInfo ASN.1 structure defined in [RFC5280] with the following properties:
+ // * Set the algorithm field to an AlgorithmIdentifier ASN.1 type with the following properties:
+ // * Set the algorithm object identifier to the id-Ed25519 OID defined in [RFC8410].
+ // * Set the subjectPublicKey field to keyData.
+ let data = okp_export_der(cx.vm(), OkpCurve::Ed25519, true, key.okp_handle_bytes())?;
+
+ // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
+ return Ok(ExportKeyResult::Bytes(data));
+ }
+
+ // 3. If format is "pkcs8":
+ if format == KeyFormat::Pkcs8 {
+ // 1. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.
+ if key.key_type() != KeyType::Private {
+ return Err(dom_error("InvalidAccessError", b"Key is not a private key"));
+ }
+
+ // 2. Let data be an instance of the privateKeyInfo ASN.1 structure defined in [RFC5208] with the following properties:
+ // * Set the version field to 0.
+ // * Set the privateKeyAlgorithm field to a PrivateKeyAlgorithmIdentifier ASN.1 type with the following properties:
+ // * Set the algorithm object identifier to the id-Ed25519 OID defined in [RFC8410].
+ // * Set the privateKey field to the result of DER-encoding a CurvePrivateKey ASN.1 type,
+ // as defined in Section 7 of [RFC8410], that represents the Ed25519 private key
+ // represented by the [[handle]] internal slot of key
+
+ let data = okp_export_der(cx.vm(), OkpCurve::Ed25519, false, key.okp_handle_bytes())?;
+
+ // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
+ return Ok(ExportKeyResult::Bytes(data));
+ }
+
+ // 2. If format is "jwk":
+ if format == KeyFormat::Jwk {
+ // 1. Let jwk be a new JsonWebKey dictionary.
+ let mut jwk = JsonWebKey::default();
+
+ // 2. Set the kty attribute of jwk to "OKP".
+ jwk.kty = Some(Utf16String::from_ascii(b"OKP"));
+
+ // 3. Set the alg attribute of jwk to "Ed25519".
+ jwk.alg = Some(Utf16String::from_ascii(b"Ed25519"));
+
+ // 4. Set the crv attribute of jwk to "Ed25519".
+ jwk.crv = Some(Utf16String::from_ascii(b"Ed25519"));
+
+ // 5. Set the x attribute of jwk according to the definition in Section 2 of [RFC8037].
+ if key.key_type() == KeyType::Public {
+ jwk.x = Some(okp_encode_jwk_bytes(cx.vm(), key.okp_handle_bytes())?);
+ } else {
+ // The "x" parameter of the "epk" field is set as follows:
+ // Apply the appropriate ECDH function to the ephemeral private key (as scalar input)
+ // and the standard base point (as u-coordinate input).
+ // The base64url encoding of the output is the value for the "x" parameter of the "epk" field.
+ let public_key = okp_generate_public_key(OkpCurve::Ed25519, key.okp_handle_bytes())
+ .map_err(|()| out_of_memory(cx.vm()))?;
+ jwk.x = Some(okp_encode_jwk_bytes(cx.vm(), public_key.as_slice())?);
+ }
+
+ // 6. If the [[type]] internal slot of key is "private"
+ if key.key_type() == KeyType::Private {
+ // 1. Set the d attribute of jwk according to the definition in Section 2 of [RFC8037].
+ jwk.d = Some(okp_encode_jwk_bytes(cx.vm(), key.okp_handle_bytes())?);
+ }
+
+ // 7. Set the key_ops attribute of jwk to the usages attribute of key.
+ jwk.key_ops = Some(
+ key.internal_usages()
+ .iter()
+ .map(|usage| Utf16String::from_ascii(KEY_USAGES[*usage as usize]))
+ .collect(),
+ );
+
+ // 8. Set the ext attribute of jwk to the [[extractable]] internal slot of key.
+ jwk.ext = Some(key.is_extractable());
+
+ // 9. Let result be the result of converting jwk to an ECMAScript Object, as defined by [WebIDL].
+ return Ok(ExportKeyResult::JsonWebKey(jwk));
+ }
+
+ // 2. If format is "raw":
+ // https://wicg.github.io/webcrypto-modern-algos/#subtlecrypto-interface-keyformat
+ // For all existing asymmetric algorithms in WebCrypto, "raw-public" acts as an alias of "raw".
+ if format == KeyFormat::Raw || format == KeyFormat::RawPublic {
+ // 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
+ if key.key_type() != KeyType::Public {
+ return Err(dom_error("InvalidAccessError", b"Key is not a public key"));
+ }
+
+ // 2. Let data be an octet string representing the Ed25519 public key represented by the [[handle]] internal slot of key.
+ // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
+ return Ok(ExportKeyResult::Bytes(Bytes::new(key.okp_handle_bytes().to_vec())));
+ }
+
+ // 2. Otherwise:
+ // throw a NotSupportedError.
+ Err(dom_error("NotSupportedError", b"Invalid key format"))
+ }
+
+ fn public_key_usages(&self) -> Option<Vec<KeyUsage>> {
+ Some(vec![KeyUsage::Verify])
+ }
+
+ // https://w3c.github.io/webcrypto/#ed25519-operations-sign
+ fn sign(&self, cx: &CallContext, _: &AlgorithmParams, key: Ref<CryptoKey>, message: &Bytes) -> WebIdlResult<Bytes> {
+ // 1. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.
+ if key.key_type() != KeyType::Private {
+ return Err(dom_error("InvalidAccessError", b"Key is not a private key"));
+ }
+
+ // 2. Perform the Ed25519 signing process, as specified in [RFC8032], Section 5.1.6,
+ // with message as M, using the Ed25519 private key associated with key.
+ let private_key = key.okp_handle_bytes();
+
+ let maybe_public_key = okp_generate_public_key(OkpCurve::Ed25519, private_key);
+ if maybe_public_key.is_err() {
+ return Err(dom_error("OperationError", b"Failed to generate public key"));
+ }
+
+ let mut signature: Option<Bytes> = None;
+ // SAFETY: The input bytes and output slot are lent for the call.
+ let maybe_signature = unsafe {
+ web_crypto_okp_sign(
+ OkpCurve::Ed25519,
+ Span::new(key.okp_handle_bytes()),
+ Span::new(message.as_slice()),
+ Span::new(&[]),
+ core::ptr::from_mut(&mut signature).cast(),
+ receive_bytes,
+ )
+ };
+ if !maybe_signature {
+ return Err(dom_error("OperationError", b"Failed to sign message"));
+ }
+
+ // 3. Return a new ArrayBuffer associated with the relevant global object of this [HTML],
+ // and containing the bytes of the signature resulting from performing the Ed25519 signing process.
+ let signature = signature.expect("LibCrypto returns the signature");
+ let result = Bytes::new(copy_bytes(cx.vm(), signature.as_slice())?);
+ Ok(result)
+ }
+
+ // https://w3c.github.io/webcrypto/#ed25519-operations-verify
+ fn verify(
+ &self,
+ _: &CallContext,
+ _: &AlgorithmParams,
+ key: Ref<CryptoKey>,
+ signature: &Bytes,
+ message: &Bytes,
+ ) -> WebIdlResult<bool> {
+ // 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
+ if key.key_type() != KeyType::Public {
+ return Err(dom_error("InvalidAccessError", b"Key is not a public key"));
+ }
+
+ // NOTE: this is checked by ED25519::verify().
+ // 2. If the key data of key represents an invalid point or a small-order element on the Elliptic Curve of Ed25519, return false.
+ // 3. If the point R, encoded in the first half of signature, represents an invalid point or a small-order element on the Elliptic Curve of Ed25519, return false.
+
+ // 4. Perform the Ed25519 verification steps, as specified in [RFC8032], Section 5.1.7,
+ // using the cofactorless (unbatched) equation, [S]B = R + [k]A', on the signature,
+ // with message as M, using the Ed25519 public key associated with key.
+
+ // 9. Let result be a boolean with the value true if the signature is valid and the value false otherwise.
+ let mut verified = false;
+ // SAFETY: The input bytes and output slot are lent for the call.
+ let maybe_verified = unsafe {
+ web_crypto_okp_verify(
+ OkpCurve::Ed25519,
+ Span::new(key.okp_handle_bytes()),
+ Span::new(signature.as_slice()),
+ Span::new(message.as_slice()),
+ Span::new(&[]),
+ &mut verified,
+ )
+ };
+ if maybe_verified != 0 {
+ // SAFETY: LibCrypto transfers ownership of the error message.
+ let error = unsafe { Utf16String::from_raw_owned(maybe_verified) };
+ return Err(Exception::Dom("OperationError", error));
+ }
+
+ // 10. Return result.
+ Ok(verified)
+ }
+}
+
+#[derive(Clone, Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct Ed448 {
+ parameters: Option<Ed448Params>,
+}
+
+impl Ed448 {
+ pub(crate) fn from_value(vm: &Vm, object: Gc<Object>, has_parameters: bool) -> WebIdlResult<Self> {
+ let parameters = if has_parameters {
+ Some(Ed448Params::from_value(vm, object)?)
+ } else {
+ None
+ };
+ Ok(Self { parameters })
+ }
+}
+
+impl AlgorithmMethods for Ed448 {
+ // https://wicg.github.io/webcrypto-secure-curves/#ed448-operations
+ fn generate_key(
+ &self,
+ cx: &CallContext,
+ _: &AlgorithmParams,
+ extractable: bool,
+ key_usages: &[KeyUsage],
+ ) -> WebIdlResult<CryptoKeyOrCryptoKeyPair> {
+ // 1. If usages contains a value which is not one of "sign" or "verify", then throw a SyntaxError.
+ for &usage in key_usages {
+ if usage != KeyUsage::Sign && usage != KeyUsage::Verify {
+ return Err(Exception::Dom(
+ "SyntaxError",
+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),
+ ));
+ }
+ }
+
+ // 2. Generate an Ed448 key pair, as defined in [RFC8032], section 5.1.5.
+ let maybe_private_key = okp_generate_private_key(OkpCurve::Ed448);
+ let Ok(private_key_data) = maybe_private_key else {
+ return Err(dom_error("OperationError", b"Failed to generate private key"));
+ };
+
+ let maybe_public_key = okp_generate_public_key(OkpCurve::Ed448, private_key_data.as_slice());
+ let Ok(public_key_data) = maybe_public_key else {
+ return Err(dom_error("OperationError", b"Failed to generate public key"));
+ };
+
+ // 5. Let publicKey be a new CryptoKey associated with the relevant global object of this [HTML],
+ // and representing the public key of the generated key pair.
+ let public_key = CryptoKey::from_okp_handle(cx.vm(), false, public_key_data.as_slice());
+
+ // 6. Set the [[type]] internal slot of publicKey to "public"
+ public_key.set_type(KeyType::Public);
+
+ // 3. Let algorithm be a new KeyAlgorithm object.
+ // 4. Set the name attribute of algorithm to "Ed448".
+ // 7. Set the [[algorithm]] internal slot of publicKey to algorithm.
+ public_key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"Ed448"),
+ });
+
+ // 8. Set the [[extractable]] internal slot of publicKey to true.
+ public_key.set_extractable(true);
+
+ // 9. Set the [[usages]] internal slot of publicKey to be the usage intersection of usages and [ "verify" ].
+ public_key.set_usages(usage_intersection(key_usages, &[KeyUsage::Verify]));
+
+ // 10. Let privateKey be a new CryptoKey associated with the relevant global object of this [HTML],
+ // and representing the private key of the generated key pair.
+ let private_key = CryptoKey::from_okp_handle(cx.vm(), true, private_key_data.as_slice());
+
+ // 11. Set the [[type]] internal slot of privateKey to "private"
+ private_key.set_type(KeyType::Private);
+
+ // 12. Set the [[algorithm]] internal slot of privateKey to algorithm.
+ private_key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"Ed448"),
+ });
+
+ // 13. Set the [[extractable]] internal slot of privateKey to extractable.
+ private_key.set_extractable(extractable);
+
+ // 14. Set the [[usages]] internal slot of privateKey to be the usage intersection of usages and [ "sign" ].
+ private_key.set_usages(usage_intersection(key_usages, &[KeyUsage::Sign]));
+
+ // 15. Let result be a new CryptoKeyPair dictionary.
+ // 16. Set the publicKey attribute of result to be publicKey.
+ // 17. Set the privateKey attribute of result to be privateKey.
+ // 18. Return the result of converting result to an ECMAScript Object, as defined by [WebIDL].
+ Ok(CryptoKeyOrCryptoKeyPair::CryptoKeyPair(CryptoKeyPair {
+ public_key: Some(public_key),
+ private_key: Some(private_key),
+ }))
+ }
+
+ // https://wicg.github.io/webcrypto-secure-curves/#ed448-operations
+ #[allow(
+ clippy::collapsible_if,
+ reason = "the specification checks key usage in a separate step"
+ )]
+ fn import_key(
+ &self,
+ cx: &CallContext,
+ _: &AlgorithmParams,
+ format: KeyFormat,
+ key_data: ImportKeyData,
+ extractable: bool,
+ usages: &[KeyUsage],
+ ) -> WebIdlResult<Ref<CryptoKey>> {
+ let key;
+
+ // 1. Let keyData be the key data to be imported.
+
+ // 2. If format is "spki":
+ if format == KeyFormat::Spki {
+ // 1. If usages contains a value which is not "verify" then throw a SyntaxError.
+ for &usage in usages {
+ if usage != KeyUsage::Verify {
+ return Err(Exception::Dom(
+ "SyntaxError",
+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),
+ ));
+ }
+ }
+
+ // 2. Let spki be the result of running the parse a subjectPublicKeyInfo algorithm over keyData.
+ // 3. If an error occurred while parsing, then throw a DataError.
+ let ImportKeyData::Bytes(key_data) = key_data else {
+ unreachable!("DER key data contains bytes")
+ };
+ let spki = Asn1KeyInfo::parse(&key_data, Asn1Structure::SubjectPublicKeyInfo)?;
+
+ // 4. If the algorithm object identifier field of the algorithm AlgorithmIdentifier field of spki
+ // is not equal to the id-Ed448 object identifier defined in [RFC8410], then throw a DataError.
+ if !spki.has_okp_oid(OkpCurve::Ed448) {
+ return Err(dom_error("DataError", b"Invalid algorithm identifier"));
+ }
+
+ // 5. If the parameters field of the algorithm AlgorithmIdentifier field of spki is present, then throw a DataError.
+ if spki.ec_parameters().is_some() {
+ return Err(dom_error("DataError", b"Invalid algorithm parameters"));
+ }
+
+ // 6. Let publicKey be the Ed448 public key identified by the subjectPublicKey field of spki.
+ let public_key = spki.raw_key();
+
+ // 7. Let key be a new CryptoKey associated with the relevant global object of this [HTML],
+ // and that represents publicKey.
+ key = CryptoKey::from_okp_handle(cx.vm(), false, public_key);
+
+ // 8. Set the [[type]] internal slot of key to "public"
+ key.set_type(KeyType::Public);
+
+ // 9. Let algorithm be a new KeyAlgorithm.
+ // 10. Set the name attribute of algorithm to "Ed448".
+ // 11. Set the [[algorithm]] internal slot of key to algorithm.
+ key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"Ed448"),
+ });
+ }
+ // 2. If format is "pkcs8":
+ else if format == KeyFormat::Pkcs8 {
+ // 1. If usages contains a value which is not "sign" then throw a SyntaxError.
+ for &usage in usages {
+ if usage != KeyUsage::Sign {
+ return Err(Exception::Dom(
+ "SyntaxError",
+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),
+ ));
+ }
+ }
+
+ // 2. Let privateKeyInfo be the result of running the parse a privateKeyInfo algorithm over keyData.
+ // 3. If an error occurs while parsing, then throw a DataError.
+ let ImportKeyData::Bytes(key_data) = key_data else {
+ unreachable!("DER key data contains bytes")
+ };
+ let private_key_info = Asn1KeyInfo::parse(&key_data, Asn1Structure::PrivateKeyInfo)?;
+
+ // 4. If the algorithm object identifier field of the privateKeyAlgorithm PrivateKeyAlgorithm field
+ // of privateKeyInfo is not equal to the id-Ed448 object identifier defined in [RFC8410], then throw a DataError.
+ if !private_key_info.has_okp_oid(OkpCurve::Ed448) {
+ return Err(dom_error("DataError", b"Invalid algorithm identifier"));
+ }
+
+ // 5. If the parameters field of the privateKeyAlgorithm PrivateKeyAlgorithmIdentifier field of privateKeyInfo is present,
+ // then throw a DataError.
+ if private_key_info.ec_parameters().is_some() {
+ return Err(dom_error("DataError", b"Invalid algorithm parameters"));
+ }
+
+ // 6. Let curvePrivateKey be the result of performing the parse an ASN.1 structure algorithm,
+ // with data as the privateKey field of privateKeyInfo, structure as the ASN.1 CurvePrivateKey structure
+ // specified in Section 7 of [RFC8410], and exactData set to true.
+ // 7. If an error occurred while parsing, then throw a DataError.
+ let curve_private_key_bytes = parse_octet_string(cx.vm(), private_key_info.raw_key())?;
+
+ // 8. Let key be a new CryptoKey associated with the relevant global object of this [HTML],
+ // and that represents the Ed448 private key identified by curvePrivateKey.
+ key = CryptoKey::from_okp_handle(cx.vm(), true, curve_private_key_bytes.as_slice());
+
+ // 9. Set the [[type]] internal slot of key to "private"
+ key.set_type(KeyType::Private);
+
+ // 10. Let algorithm be a new KeyAlgorithm.
+ // 11. Set the name attribute of algorithm to "Ed448".
+ // 12. Set the [[algorithm]] internal slot of key to algorithm.
+ key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"Ed448"),
+ });
+ }
+ // 2. If format is "jwk":
+ else if format == KeyFormat::Jwk {
+ // 1. If keyData is a JsonWebKey dictionary: Let jwk equal keyData.
+ // Otherwise: Throw a DataError.
+ let ImportKeyData::JsonWebKey(jwk) = key_data else {
+ return Err(dom_error("DataError", b"keyData is not a JsonWebKey dictionary"));
+ };
+
+ // 2. If the d field is present and usages contains a value which is not "sign",
+ // or, if the d field is not present and usages contains a value which is not "verify" then throw a SyntaxError.
+ if jwk.d.is_some() {
+ for &usage in usages {
+ if usage != KeyUsage::Sign {
+ return Err(Exception::Dom(
+ "SyntaxError",
+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),
+ ));
+ }
+ }
+ } else {
+ for &usage in usages {
+ if usage != KeyUsage::Verify {
+ return Err(Exception::Dom(
+ "SyntaxError",
+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),
+ ));
+ }
+ }
+ }
+
+ // 3. If the kty field of jwk is not "OKP", then throw a DataError.
+ if jwk.kty.as_ref() != Some(&Utf16String::from_ascii(b"OKP")) {
+ return Err(dom_error("DataError", b"Invalid key type"));
+ }
+
+ // 4. If the crv field of jwk is not "Ed448", then throw a DataError.
+ if jwk.crv.as_ref() != Some(&Utf16String::from_ascii(b"Ed448")) {
+ return Err(dom_error("DataError", b"Invalid curve"));
+ }
+
+ // 5. If the alg field of jwk is present and is not "Ed448" or "EdDSA", then throw a DataError.
+ if jwk.alg.as_ref().is_some_and(|alg| {
+ *alg != Utf16String::from_ascii(b"Ed448") && *alg != Utf16String::from_ascii(b"EdDSA")
+ }) {
+ return Err(dom_error("DataError", b"Invalid algorithm"));
+ }
+
+ // 6. If usages is non-empty and the use field of jwk is present and is not "sig", then throw a DataError.
+ if !usages.is_empty()
+ && jwk
+ .r#use
+ .as_ref()
+ .is_some_and(|key_use| *key_use != Utf16String::from_ascii(b"sig"))
+ {
+ return Err(dom_error("DataError", b"Invalid key usage"));
+ }
+
+ // 7. If the key_ops field of jwk is present, and is invalid according to the requirements of JSON Web Key [JWK],
+ // or it does not contain all of the specified usages values, then throw a DataError.
+ validate_jwk_key_ops(&jwk, usages)?;
+
+ // 8. If the ext field of jwk is present and has the value false and extractable is true, then throw a DataError.
+ if jwk.ext == Some(false) && extractable {
+ return Err(dom_error("DataError", b"Invalid extractable"));
+ }
+
+ // 9. If the d field is present:
+ if let Some(d) = &jwk.d {
+ // 1. If jwk does not meet the requirements of the JWK private key format described in Section 2 of [RFC8037],
+ // then throw a DataError.
+ // o The parameter "kty" MUST be "OKP".
+ if jwk.kty.as_ref() != Some(&Utf16String::from_ascii(b"OKP")) {
+ return Err(dom_error("DataError", b"Invalid key type"));
+ }
+
+ // https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve
+ // o The parameter "crv" MUST be present and contain the subtype of the key (from the "JSON Web Elliptic Curve" registry).
+ if jwk.crv.as_ref() != Some(&Utf16String::from_ascii(b"Ed448")) {
+ return Err(dom_error("DataError", b"Invalid curve"));
+ }
+
+ // o The parameter "x" MUST be present and contain the public key encoded using the base64url [RFC4648] encoding.
+ if jwk.x.is_none() {
+ return Err(dom_error("DataError", b"Missing x field"));
+ }
+ let public_key = base64_url_bytes_decode(cx.vm(), jwk.x.as_ref().expect("the coordinate is present"))?;
+ if public_key.as_slice().len() != 57 {
+ return Err(dom_error("DataError", b"Invalid key length"));
+ }
+
+ // o The parameter "d" MUST be present for private keys and contain the private key encoded using the base64url encoding.
+ // This parameter MUST NOT be present for public keys.
+ if jwk.d.is_none() {
+ return Err(dom_error("DataError", b"Present d field"));
+ }
+ let private_key = base64_url_bytes_decode(cx.vm(), d)?;
+ if private_key.as_slice().len() != 57 {
+ return Err(dom_error("DataError", b"Invalid key length"));
+ }
+
+ let derived_public_key = okp_generate_public_key(OkpCurve::Ed448, private_key.as_slice())
+ .map_err(|()| out_of_memory(cx.vm()))?;
+ if derived_public_key.as_slice() != public_key.as_slice() {
+ return Err(dom_error("DataError", b"Invalid key pair"));
+ }
+
+ // 2. Let key be a new CryptoKey object that represents the Ed448 private key identified by interpreting jwk according to Section 2 of [RFC8037].
+ key = CryptoKey::from_okp_handle(cx.vm(), true, private_key.as_slice());
+
+ // 3. Set the [[type]] internal slot of Key to "private".
+ key.set_type(KeyType::Private);
+ }
+ // Otherwise:
+ else {
+ // 1. If jwk does not meet the requirements of the JWK public key format described in Section 2 of [RFC8037], then throw a DataError.
+ // o The parameter "kty" MUST be "OKP".
+ if jwk.kty.as_ref() != Some(&Utf16String::from_ascii(b"OKP")) {
+ return Err(dom_error("DataError", b"Invalid key type"));
+ }
+
+ // https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve
+ // o The parameter "crv" MUST be present and contain the subtype of the key (from the "JSON Web Elliptic Curve" registry).
+ if jwk.crv.as_ref() != Some(&Utf16String::from_ascii(b"Ed448")) {
+ return Err(dom_error("DataError", b"Invalid curve"));
+ }
+
+ // o The parameter "x" MUST be present and contain the public key encoded using the base64url [RFC4648] encoding.
+ if jwk.x.is_none() {
+ return Err(dom_error("DataError", b"Missing x field"));
+ }
+ let public_key = base64_url_bytes_decode(cx.vm(), jwk.x.as_ref().expect("the coordinate is present"))?;
+ if public_key.as_slice().len() != 57 {
+ return Err(dom_error("DataError", b"Invalid key length"));
+ }
+
+ // o The parameter "d" MUST be present for private keys and contain the private key encoded using the base64url encoding.
+ // This parameter MUST NOT be present for public keys.
+ if jwk.d.is_some() {
+ return Err(dom_error("DataError", b"Present d field"));
+ }
+
+ // 2. Let key be a new CryptoKey object that represents the Ed448 public key identified by interpreting jwk according to Section 2 of [RFC8037].
+ key = CryptoKey::from_okp_handle(cx.vm(), false, public_key.as_slice());
+
+ // 3. Set the [[type]] internal slot of Key to "public".
+ key.set_type(KeyType::Public);
+ }
+
+ // 10. Let algorithm be a new instance of a KeyAlgorithm object.
+ // 11. Set the name attribute of algorithm to "Ed448".
+ // 12. Set the [[algorithm]] internal slot of key to algorithm.
+ key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"Ed448"),
+ });
+ }
+ // 2. If format is "raw":
+ else if format == KeyFormat::Raw {
+ // 1. If usages contains a value which is not "verify" then throw a SyntaxError.
+ for &usage in usages {
+ if usage != KeyUsage::Verify {
+ return Err(Exception::Dom(
+ "SyntaxError",
+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),
+ ));
+ }
+ }
+
+ // 2. Let data be keyData.
+ let ImportKeyData::Bytes(data) = key_data else {
+ unreachable!("raw key data contains bytes")
+ };
+
+ // 3. If the length in bits of data is not 448 then throw a DataError.
+ // AD-HOC: The spec has a typo with the size of the key length
+ // See spec comment: https://github.com/w3c/webcrypto/pull/425#discussion_r3070135408
+ if data.as_slice().len() * 8 != 456 {
+ return Err(dom_error("DataError", b"Invalid key length"));
+ }
+
+ // 6. Let key be a new CryptoKey associated with the relevant global object of this [HTML], and that represents data.
+ key = CryptoKey::from_okp_handle(cx.vm(), false, data.as_slice());
+
+ // 7. Set the [[type]] internal slot of key to "public"
+ key.set_type(KeyType::Public);
+
+ // 4. Let algorithm be a new KeyAlgorithm object.
+ // 5. Set the name attribute of algorithm to "Ed448".
+ // 8. Set the [[algorithm]] internal slot of key to algorithm.
+ key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"Ed448"),
+ });
+ }
+ // 2. Otherwise:
+ else {
+ // throw a NotSupportedError.
+ return Err(dom_error("NotSupportedError", b"Invalid key format"));
+ }
+
+ Ok(key)
+ }
+
+ // https://wicg.github.io/webcrypto-secure-curves/#ed448-operations
+ #[allow(
+ clippy::field_reassign_with_default,
+ reason = "the algorithm sets JWK members in separate steps"
+ )]
+ fn export_key(&self, cx: &CallContext, format: KeyFormat, key: Ref<CryptoKey>) -> WebIdlResult<ExportKeyResult> {
+ // 1. Let key be the CryptoKey to be exported.
+
+ // 2. If the underlying cryptographic key material represented by the [[handle]] internal slot of key cannot be accessed, then throw an OperationError.
+ // Note: In our impl this is always accessible
+
+ // 3. If format is "spki":
+ if format == KeyFormat::Spki {
+ // 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
+ if key.key_type() != KeyType::Public {
+ return Err(dom_error("InvalidAccessError", b"Key is not a public key"));
+ }
+
+ // 2. Let data be an instance of the subjectPublicKeyInfo ASN.1 structure defined in [RFC5280] with the following properties:
+ // * Set the algorithm field to an AlgorithmIdentifier ASN.1 type with the following properties:
+ // * Set the algorithm object identifier to the id-Ed448 OID defined in [RFC8410].
+ // * Set the subjectPublicKey field to keyData.
+ let data = okp_export_der(cx.vm(), OkpCurve::Ed448, true, key.okp_handle_bytes())?;
+
+ // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
+ return Ok(ExportKeyResult::Bytes(data));
+ }
+
+ // 3. If format is "pkcs8":
+ if format == KeyFormat::Pkcs8 {
+ // 1. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.
+ if key.key_type() != KeyType::Private {
+ return Err(dom_error("InvalidAccessError", b"Key is not a private key"));
+ }
+
+ // 2. Let data be an instance of the privateKeyInfo ASN.1 structure defined in [RFC5208] with the following properties:
+ // * Set the version field to 0.
+ // * Set the privateKeyAlgorithm field to a PrivateKeyAlgorithmIdentifier ASN.1 type with the following properties:
+ // * Set the algorithm object identifier to the id-Ed448 OID defined in [RFC8410].
+ // * Set the privateKey field to the result of DER-encoding a CurvePrivateKey ASN.1 type,
+ // as defined in Section 7 of [RFC8410], that represents the Ed448 private key
+ // represented by the [[handle]] internal slot of key
+
+ let data = okp_export_der(cx.vm(), OkpCurve::Ed448, false, key.okp_handle_bytes())?;
+
+ // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
+ return Ok(ExportKeyResult::Bytes(data));
+ }
+
+ // 2. If format is "jwk":
+ if format == KeyFormat::Jwk {
+ // 1. Let jwk be a new JsonWebKey dictionary.
+ let mut jwk = JsonWebKey::default();
+
+ // 2. Set the kty attribute of jwk to "OKP".
+ jwk.kty = Some(Utf16String::from_ascii(b"OKP"));
+
+ // 3. Set the alg attribute of jwk to "Ed448".
+ jwk.alg = Some(Utf16String::from_ascii(b"Ed448"));
+
+ // 4. Set the crv attribute of jwk to "Ed448".
+ jwk.crv = Some(Utf16String::from_ascii(b"Ed448"));
+
+ // 5. Set the x attribute of jwk according to the definition in Section 2 of [RFC8037].
+ if key.key_type() == KeyType::Public {
+ jwk.x = Some(okp_encode_jwk_bytes(cx.vm(), key.okp_handle_bytes())?);
+ } else {
+ // The "x" parameter of the "epk" field is set as follows:
+ // Apply the appropriate ECDH function to the ephemeral private key (as scalar input)
+ // and the standard base point (as u-coordinate input).
+ // The base64url encoding of the output is the value for the "x" parameter of the "epk" field.
+ let public_key = okp_generate_public_key(OkpCurve::Ed448, key.okp_handle_bytes())
+ .map_err(|()| out_of_memory(cx.vm()))?;
+ jwk.x = Some(okp_encode_jwk_bytes(cx.vm(), public_key.as_slice())?);
+ }
+
+ // 6. If the [[type]] internal slot of key is "private"
+ if key.key_type() == KeyType::Private {
+ // 1. Set the d attribute of jwk according to the definition in Section 2 of [RFC8037].
+ jwk.d = Some(okp_encode_jwk_bytes(cx.vm(), key.okp_handle_bytes())?);
+ }
+
+ // 7. Set the key_ops attribute of jwk to the usages attribute of key.
+ jwk.key_ops = Some(
+ key.internal_usages()
+ .iter()
+ .map(|usage| Utf16String::from_ascii(KEY_USAGES[*usage as usize]))
+ .collect(),
+ );
+
+ // 8. Set the ext attribute of jwk to the [[extractable]] internal slot of key.
+ jwk.ext = Some(key.is_extractable());
+
+ // 9. Let result be the result of converting jwk to an ECMAScript Object, as defined by [WebIDL].
+ return Ok(ExportKeyResult::JsonWebKey(jwk));
+ }
+
+ // 2. If format is "raw":
+ if format == KeyFormat::Raw {
+ // 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
+ if key.key_type() != KeyType::Public {
+ return Err(dom_error("InvalidAccessError", b"Key is not a public key"));
+ }
+
+ // 2. Let data be an octet string representing the Ed448 public key represented by the [[handle]] internal slot of key.
+ // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
+ return Ok(ExportKeyResult::Bytes(Bytes::new(key.okp_handle_bytes().to_vec())));
+ }
+
+ // 2. Otherwise:
+ // throw a NotSupportedError.
+ Err(dom_error("NotSupportedError", b"Invalid key format"))
+ }
+
+ fn public_key_usages(&self) -> Option<Vec<KeyUsage>> {
+ Some(vec![KeyUsage::Verify])
+ }
+
+ // https://wicg.github.io/webcrypto-secure-curves/#ed448-operations
+ fn sign(&self, _: &CallContext, _: &AlgorithmParams, key: Ref<CryptoKey>, message: &Bytes) -> WebIdlResult<Bytes> {
+ // 1. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.
+ if key.key_type() != KeyType::Private {
+ return Err(dom_error("InvalidAccessError", b"Key is not a private key"));
+ }
+
+ // 2. Let context be the contents of the context member of normalizedAlgorithm
+ // or the empty octet string if the context member of normalizedAlgorithm is not present.
+ let algorithm = self.parameters.as_ref().expect("the operation uses Ed448 parameters");
+ let context = algorithm.context.as_ref().map_or(&[][..], Bytes::as_slice);
+
+ // 3. If context has a length greater than 255 bytes, then throw an OperationError.
+ if context.len() > 255 {
+ return Err(dom_error("OperationError", b"Context is too long"));
+ }
+
+ // 4. Perform the Ed448 signing process, as specified in [RFC8032], Section 5.2.6,
+ // with message as M and context as C, using the Ed448 private key associated with key.
+ let mut signature: Option<Bytes> = None;
+ // SAFETY: The input bytes and output slot are lent for the call.
+ let maybe_signature = unsafe {
+ web_crypto_okp_sign(
+ OkpCurve::Ed448,
+ Span::new(key.okp_handle_bytes()),
+ Span::new(message.as_slice()),
+ Span::new(context),
+ core::ptr::from_mut(&mut signature).cast(),
+ receive_bytes,
+ )
+ };
+ if !maybe_signature {
+ return Err(dom_error("OperationError", b"Failed to sign message"));
+ }
+
+ // 5. Return a new ArrayBuffer associated with the relevant global object of this [HTML],
+ // and containing the bytes of the signature resulting from performing the Ed448 signing process.
+ Ok(signature.expect("LibCrypto returns the signature"))
+ }
+
+ // https://wicg.github.io/webcrypto-secure-curves/#ed448-operations
+ fn verify(
+ &self,
+ _: &CallContext,
+ _: &AlgorithmParams,
+ key: Ref<CryptoKey>,
+ signature: &Bytes,
+ message: &Bytes,
+ ) -> WebIdlResult<bool> {
+ // 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
+ if key.key_type() != KeyType::Public {
+ return Err(dom_error("InvalidAccessError", b"Key is not a public key"));
+ }
+
+ // 2. Let context be the contents of the context member of normalizedAlgorithm
+ // or the empty octet string if the context member of normalizedAlgorithm is not present.
+ let algorithm = self.parameters.as_ref().expect("the operation uses Ed448 parameters");
+ let context = algorithm.context.as_ref().map_or(&[][..], Bytes::as_slice);
+
+ // 3. If context has a length greater than 255 bytes, then throw an OperationError.
+ if context.len() > 255 {
+ return Err(dom_error("OperationError", b"Context is too long"));
+ }
+
+ // 4. If the key data of key represents an invalid point or a small-order element
+ // on the Elliptic Curve of Ed448, return false.
+ // NOTE: https://github.com/WICG/webcrypto-secure-curves/issues/27
+
+ // 5. If the point R, encoded in the first half of signature, represents an invalid point
+ // or a small-order element on the Elliptic Curve of Ed448, return false.
+ // NOTE: https://github.com/WICG/webcrypto-secure-curves/issues/27
+
+ // 6. Perform the Ed448 verification steps, as specified in [RFC8032], Section 5.2.7, using
+ // the cofactorless (unbatched) equation, [S]B = R + [k]A', on the signature,
+ // with message as M and context as C, using the Ed448 public key associated with key.
+ let mut verified = false;
+ // SAFETY: The input bytes and output slot are lent for the call.
+ let maybe_verified = unsafe {
+ web_crypto_okp_verify(
+ OkpCurve::Ed448,
+ Span::new(key.okp_handle_bytes()),
+ Span::new(signature.as_slice()),
+ Span::new(message.as_slice()),
+ Span::new(context),
+ &mut verified,
+ )
+ };
+ if maybe_verified != 0 {
+ // SAFETY: LibCrypto transfers ownership of the error message.
+ let error = unsafe { Utf16String::from_raw_owned(maybe_verified) };
+ return Err(Exception::Dom("OperationError", error));
+ }
+
+ // 7. Let result be a boolean with the value true if the signature is valid
+ // and the value false otherwise.
+ // 8. Return result.
+ Ok(verified)
+ }
+}
+
+#[derive(Clone, Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct X25519 {
+ parameters: Option<EcdhKeyDeriveParams>,
+}
+
+impl X25519 {
+ pub(crate) fn from_value(vm: &Vm, object: Gc<Object>, has_parameters: bool) -> WebIdlResult<Self> {
+ let parameters = if has_parameters {
+ Some(EcdhKeyDeriveParams::from_value(vm, object)?)
+ } else {
+ None
+ };
+ Ok(Self { parameters })
+ }
+}
+
+impl AlgorithmMethods for X25519 {
+ // https://w3c.github.io/webcrypto/#x25519-operations-derive-bits
+ fn validate_derive_bits_parameters(
+ &self,
+ _: Gc<Realm>,
+ algorithm: &AlgorithmParams,
+ length_optional: Option<u32>,
+ ) -> WebIdlResult<()> {
+ let normalized_algorithm = self
+ .parameters
+ .as_ref()
+ .expect("the operation uses key agreement parameters");
+
+ // 1. Let publicKey be the public member of normalizedAlgorithm.
+ let public_key = normalized_algorithm.public_key;
+
+ // 2. If the [[type]] internal slot of publicKey is not "public", then throw an InvalidAccessError.
+ if public_key.key_type() != KeyType::Public {
+ return Err(dom_error("InvalidAccessError", b"Public key is not a public key"));
+ }
+
+ // 3. If the name attribute of the [[algorithm]] internal slot of publicKey is not equal to the name member of
+ // normalizedAlgorithm, then throw an InvalidAccessError.
+ if public_key.algorithm_name() != algorithm.name {
+ return Err(dom_error("InvalidAccessError", b"Algorithm mismatch"));
+ }
+
+ // 4. If length is not null and is greater than 256, then throw an OperationError.
+ if length_optional.is_some_and(|length| length > 256) {
+ return Err(dom_error("OperationError", b"Length is too large"));
+ }
+
+ Ok(())
+ }
+
+ // https://w3c.github.io/webcrypto/#x25519-operations-derive-bits
+ fn derive_bits(
+ &self,
+ cx: &CallContext,
+ algorithm: &AlgorithmParams,
+ key: Ref<CryptoKey>,
+ length_optional: Option<u32>,
+ ) -> WebIdlResult<Bytes> {
+ let normalized_algorithm = self
+ .parameters
+ .as_ref()
+ .expect("the operation uses key agreement parameters");
+
+ // NB: Steps 1 to 4 are performed by validate_derive_bits_parameters().
+ self.validate_derive_bits_parameters(cx.realm(), algorithm, length_optional)?;
+ let public_key = normalized_algorithm.public_key;
+
+ // 5. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.
+ if key.key_type() != KeyType::Private {
+ return Err(dom_error("InvalidAccessError", b"Key is not a private key"));
+ }
+
+ // 6. If the name attribute of the [[algorithm]] internal slot of publicKey is not equal to
+ // the name property of the [[algorithm]] internal slot of key, then throw an InvalidAccessError.
+ if key.algorithm_name() != public_key.algorithm_name() {
+ return Err(dom_error("InvalidAccessError", b"Algorithm mismatch"));
+ }
+
+ // 7. Let secret be the result of performing the X25519 function specified in [RFC7748] Section 5 with
+ // key as the X25519 private key k and
+ // the X25519 public key represented by the [[handle]] internal slot of publicKey as the X25519 public key u.
+ let private_key = key.okp_handle_bytes();
+ let public_key_data = public_key.okp_handle_bytes();
+
+ let mut secret: Option<Bytes> = None;
+ // SAFETY: The input bytes and output slot are lent for the call.
+ let maybe_secret = unsafe {
+ web_crypto_okp_compute_coordinate(
+ OkpCurve::X25519,
+ Span::new(private_key),
+ Span::new(public_key_data),
+ core::ptr::from_mut(&mut secret).cast(),
+ receive_bytes,
+ )
+ };
+ if !maybe_secret {
+ return Err(dom_error("OperationError", b"Failed to compute secret"));
+ }
+
+ let secret = secret.expect("LibCrypto returns the shared secret");
+
+ // 8. If secret is the all-zero value, then throw a OperationError.
+ // This check must be performed in constant-time, as per [RFC7748] Section 6.1.
+ // NOTE: The check may be performed by ORing all the bytes together and checking whether the result is zero,
+ // as this eliminates standard side-channels in software implementations.
+ let mut or_bytes = 0;
+ for &byte in secret.as_slice() {
+ or_bytes |= byte;
+ }
+
+ if or_bytes == 0 {
+ return Err(dom_error("OperationError", b"Secret is the all-zero value"));
+ }
+
+ // 9. If length is null: Return secret.
+ let Some(length) = length_optional else {
+ let result = Bytes::new(copy_bytes(cx.vm(), secret.as_slice())?);
+ return Ok(result);
+ };
+
+ // Otherwise: If the length of secret in bits is less than length: throw an OperationError.
+ let length = length as usize;
+ if secret.as_slice().len() * 8 < length {
+ return Err(dom_error("OperationError", b"Secret is too short"));
+ }
+
+ // Otherwise: Return an octet string containing the first length bits of secret.
+ let mut slice = copy_bytes(cx.vm(), &secret.as_slice()[..length.div_ceil(8)])?;
+ if !length.is_multiple_of(8) {
+ // Zero out the last bits
+ *slice.last_mut().expect("a partial byte is present") &= 0xFF << (8 - (length % 8));
+ }
+
+ Ok(Bytes::new(slice))
+ }
+
+ // https://w3c.github.io/webcrypto/#x25519-operations-generate-key
+ fn generate_key(
+ &self,
+ cx: &CallContext,
+ _: &AlgorithmParams,
+ extractable: bool,
+ key_usages: &[KeyUsage],
+ ) -> WebIdlResult<CryptoKeyOrCryptoKeyPair> {
+ // 1. If usages contains an entry which is not "deriveKey" or "deriveBits" then throw a SyntaxError.
+ for &usage in key_usages {
+ if usage != KeyUsage::DeriveKey && usage != KeyUsage::DeriveBits {
+ return Err(Exception::Dom(
+ "SyntaxError",
+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),
+ ));
+ }
+ }
+
+ // 2. Generate an X25519 key pair, with the private key being 32 random bytes,
+ // and the public key being X25519(a, 9), as defined in [RFC7748], section 6.1.
+ let maybe_private_key = okp_generate_private_key(OkpCurve::X25519);
+ let Ok(private_key_data) = maybe_private_key else {
+ return Err(dom_error("OperationError", b"Failed to generate private key"));
+ };
+
+ let maybe_public_key = okp_generate_public_key(OkpCurve::X25519, private_key_data.as_slice());
+ let Ok(public_key_data) = maybe_public_key else {
+ return Err(dom_error("OperationError", b"Failed to generate public key"));
+ };
+
+ // 5. Let publicKey be a new CryptoKey associated with the relevant global object of this [HTML],
+ // and representing the public key of the generated key pair.
+ let public_key = CryptoKey::from_okp_handle(cx.vm(), false, public_key_data.as_slice());
+
+ // 6. Set the [[type]] internal slot of publicKey to "public"
+ public_key.set_type(KeyType::Public);
+
+ // 3. Let algorithm be a new KeyAlgorithm object.
+ // 4. Set the name attribute of algorithm to "X25519".
+ // 7. Set the [[algorithm]] internal slot of publicKey to algorithm.
+ public_key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"X25519"),
+ });
+
+ // 8. Set the [[extractable]] internal slot of publicKey to true.
+ public_key.set_extractable(true);
+
+ // 9. Set the [[usages]] internal slot of publicKey to be the empty list.
+ public_key.set_usages(Vec::new());
+
+ // 10. Let privateKey be a new CryptoKey associated with the relevant global object of this [HTML],
+ // and representing the private key of the generated key pair.
+ let private_key = CryptoKey::from_okp_handle(cx.vm(), true, private_key_data.as_slice());
+
+ // 11. Set the [[type]] internal slot of privateKey to "private"
+ private_key.set_type(KeyType::Private);
+
+ // 12. Set the [[algorithm]] internal slot of privateKey to algorithm.
+ private_key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"X25519"),
+ });
+
+ // 13. Set the [[extractable]] internal slot of privateKey to extractable.
+ private_key.set_extractable(extractable);
+
+ // 14. Set the [[usages]] internal slot of privateKey to be the usage intersection of usages and [ "deriveKey", "deriveBits" ].
+ private_key.set_usages(usage_intersection(
+ key_usages,
+ &[KeyUsage::DeriveKey, KeyUsage::DeriveBits],
+ ));
+
+ // 15. Let result be a new CryptoKeyPair dictionary.
+ // 16. Set the publicKey attribute of result to be publicKey.
+ // 17. Set the privateKey attribute of result to be privateKey.
+ // 18. Return the result of converting result to an ECMAScript Object, as defined by [WebIDL].
+ Ok(CryptoKeyOrCryptoKeyPair::CryptoKeyPair(CryptoKeyPair {
+ public_key: Some(public_key),
+ private_key: Some(private_key),
+ }))
+ }
+
+ // https://w3c.github.io/webcrypto/#x25519-operations-import-key
+ #[allow(
+ clippy::collapsible_if,
+ reason = "the specification checks key usage in a separate step"
+ )]
+ fn import_key(
+ &self,
+ cx: &CallContext,
+ _: &AlgorithmParams,
+ key_format: KeyFormat,
+ key_data: ImportKeyData,
+ extractable: bool,
+ usages: &[KeyUsage],
+ ) -> WebIdlResult<Ref<CryptoKey>> {
+ // NOTE: This is a parameter to the function
+ // 1. Let keyData be the key data to be imported.
+
+ let key;
+
+ // 2. If format is "spki":
+ if key_format == KeyFormat::Spki {
+ // 1. If usages is not empty then throw a SyntaxError.
+ if !usages.is_empty() {
+ return Err(dom_error("SyntaxError", b"Usages must be empty"));
+ }
+
+ // 2. Let spki be the result of running the parse a subjectPublicKeyInfo algorithm over keyData.
+ // 3. If an error occurred while parsing, then throw a DataError.
+ let ImportKeyData::Bytes(key_data) = key_data else {
+ unreachable!("DER key data contains bytes")
+ };
+ let spki = Asn1KeyInfo::parse(&key_data, Asn1Structure::SubjectPublicKeyInfo)?;
+
+ // 4. If the algorithm object identifier field of the algorithm AlgorithmIdentifier field of spki
+ // is not equal to the id-X25519 object identifier defined in [RFC8410], then throw a DataError.
+ if !spki.has_okp_oid(OkpCurve::X25519) {
+ return Err(dom_error("DataError", b"Invalid algorithm"));
+ }
+
+ // 5. If the parameters field of the algorithm AlgorithmIdentifier field of spki is present, then throw a DataError.
+ if spki.ec_parameters().is_some() {
+ return Err(dom_error("DataError", b"Invalid algorithm parameters"));
+ }
+
+ // 6. Let publicKey be the X25519 public key identified by the subjectPublicKey field of spki.
+ let public_key = spki.raw_key();
+
+ // 7. Let key be a new CryptoKey associated with the relevant global object of this [HTML], and that represents publicKey.
+ key = CryptoKey::from_okp_handle(cx.vm(), false, public_key);
+
+ // 8. Set the [[type]] internal slot of key to "public"
+ key.set_type(KeyType::Public);
+
+ // 9. Let algorithm be a new KeyAlgorithm.
+ // 10. Set the name attribute of algorithm to "X25519".
+ // 11. Set the [[algorithm]] internal slot of key to algorithm.
+ key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"X25519"),
+ });
+ }
+ // 2. If format is "pkcs8":
+ else if key_format == KeyFormat::Pkcs8 {
+ // 1. If usages contains an entry which is not "deriveKey" or "deriveBits" then throw a SyntaxError.
+ for &usage in usages {
+ if usage != KeyUsage::DeriveKey && usage != KeyUsage::DeriveBits {
+ return Err(Exception::Dom(
+ "SyntaxError",
+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),
+ ));
+ }
+ }
+
+ // 2. Let privateKeyInfo be the result of running the parse a privateKeyInfo algorithm over keyData.
+ // 3. If an error occurred while parsing, then throw a DataError.
+ let ImportKeyData::Bytes(key_data) = key_data else {
+ unreachable!("DER key data contains bytes")
+ };
+ let private_key_info = Asn1KeyInfo::parse(&key_data, Asn1Structure::PrivateKeyInfo)?;
+
+ // 4. If the algorithm object identifier field of the privateKeyAlgorithm PrivateKeyAlgorithm field of privateKeyInfo
+ // is not equal to the id-X25519 object identifier defined in [RFC8410], then throw a DataError.
+ if !private_key_info.has_okp_oid(OkpCurve::X25519) {
+ return Err(dom_error("DataError", b"Invalid algorithm"));
+ }
+
+ // 5. If the parameters field of the privateKeyAlgorithm PrivateKeyAlgorithmIdentifier field of privateKeyInfo is present, then throw a DataError.
+ if private_key_info.ec_parameters().is_some() {
+ return Err(dom_error("DataError", b"Invalid algorithm parameters"));
+ }
+
+ // 6. Let curvePrivateKey be the result of performing the parse an ASN.1 structure algorithm,
+ // with data as the privateKey field of privateKeyInfo,
+ // structure as the ASN.1 CurvePrivateKey structure specified in Section 7 of [RFC8410], and
+ // exactData set to true.
+ // 7. If an error occurred while parsing, then throw a DataError.
+ let curve_private_key_bytes = parse_octet_string(cx.vm(), private_key_info.raw_key())?;
+
+ // 8. Let key be a new CryptoKey associated with the relevant global object of this [HTML],
+ // and that represents the X25519 private key identified by curvePrivateKey.
+ key = CryptoKey::from_okp_handle(cx.vm(), true, curve_private_key_bytes.as_slice());
+
+ // 9. Set the [[type]] internal slot of key to "private"
+ key.set_type(KeyType::Private);
+
+ // 10. Let algorithm be a new KeyAlgorithm.
+ // 11. Set the name attribute of algorithm to "X25519".
+ // 12. Set the [[algorithm]] internal slot of key to algorithm.
+ key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"X25519"),
+ });
+ }
+ // 2. If format is "jwk":
+ else if key_format == KeyFormat::Jwk {
+ // 1. If keyData is a JsonWebKey dictionary: Let jwk equal keyData.
+ // Otherwise: Throw a DataError.
+ let ImportKeyData::JsonWebKey(jwk) = key_data else {
+ return Err(dom_error("DataError", b"keyData is not a JsonWebKey dictionary"));
+ };
+
+ // 2. If the d field is present and if usages contains an entry which is not "deriveKey" or "deriveBits" then throw a SyntaxError.
+ if jwk.d.is_some() && !usages.is_empty() {
+ for &usage in usages {
+ if usage != KeyUsage::DeriveKey && usage != KeyUsage::DeriveBits {
+ return Err(Exception::Dom(
+ "SyntaxError",
+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),
+ ));
+ }
+ }
+ }
+
+ // 3. If the d field is not present and if usages is not empty then throw a SyntaxError.
+ if jwk.d.is_none() && !usages.is_empty() {
+ return Err(dom_error("SyntaxError", b"Usages must be empty if d is missing"));
+ }
+
+ // 4. If the kty field of jwk is not "OKP", then throw a DataError.
+ if jwk.kty.as_ref() != Some(&Utf16String::from_ascii(b"OKP")) {
+ return Err(dom_error("DataError", b"Invalid key type"));
+ }
+
+ // 5. If the crv field of jwk is not "X25519", then throw a DataError.
+ if jwk.crv.as_ref() != Some(&Utf16String::from_ascii(b"X25519")) {
+ return Err(dom_error("DataError", b"Invalid curve"));
+ }
+
+ // 6. If usages is non-empty and the use field of jwk is present and is not equal to "enc" then throw a DataError.
+ if !usages.is_empty()
+ && jwk
+ .r#use
+ .as_ref()
+ .is_some_and(|key_use| *key_use != Utf16String::from_ascii(b"enc"))
+ {
+ return Err(dom_error("DataError", b"Invalid use"));
+ }
+
+ // 7. If the key_ops field of jwk is present, and is invalid according to the requirements of JSON Web Key [JWK],
+ // or it does not contain all of the specified usages values, then throw a DataError.
+ validate_jwk_key_ops(&jwk, usages)?;
+
+ // 8. If the ext field of jwk is present and has the value false and extractable is true, then throw a DataError.
+ if jwk.ext == Some(false) && extractable {
+ return Err(dom_error("DataError", b"Invalid extractable"));
+ }
+
+ // 9. If the d field is present:
+ if let Some(d) = &jwk.d {
+ // 1. If jwk does not meet the requirements of the JWK private key format described in Section 2 of [RFC8037], then throw a DataError.
+ // o The parameter "kty" MUST be "OKP".
+ if jwk.kty.as_ref() != Some(&Utf16String::from_ascii(b"OKP")) {
+ return Err(dom_error("DataError", b"Invalid key type"));
+ }
+
+ // https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve
+ // o The parameter "crv" MUST be present and contain the subtype of the key (from the "JSON Web Elliptic Curve" registry).
+ if jwk.crv.as_ref() != Some(&Utf16String::from_ascii(b"X25519")) {
+ return Err(dom_error("DataError", b"Invalid curve"));
+ }
+
+ // o The parameter "x" MUST be present and contain the public key encoded using the base64url [RFC4648] encoding.
+ if jwk.x.is_none() {
+ return Err(dom_error("DataError", b"Missing x field"));
+ }
+ let public_key = base64_url_bytes_decode(cx.vm(), jwk.x.as_ref().expect("the coordinate is present"))?;
+ if public_key.as_slice().len() != 32 {
+ return Err(dom_error("DataError", b"Invalid key length"));
+ }
+
+ // o The parameter "d" MUST be present for private keys and contain the private key encoded using the base64url encoding.
+ // This parameter MUST NOT be present for public keys.
+ if jwk.d.is_none() {
+ return Err(dom_error("DataError", b"Missing d field"));
+ }
+ let private_key = base64_url_bytes_decode(cx.vm(), d)?;
+ if private_key.as_slice().len() != 32 {
+ return Err(dom_error("DataError", b"Invalid key length"));
+ }
+
+ let derived_public_key = okp_generate_public_key(OkpCurve::X25519, private_key.as_slice())
+ .map_err(|()| out_of_memory(cx.vm()))?;
+ if derived_public_key.as_slice() != public_key.as_slice() {
+ return Err(dom_error("DataError", b"Invalid key pair"));
+ }
+
+ // 2. Let key be a new CryptoKey object that represents the X25519 private key identified by interpreting jwk according to Section 2 of [RFC8037].
+ key = CryptoKey::from_okp_handle(cx.vm(), true, private_key.as_slice());
+
+ // 3. Set the [[type]] internal slot of Key to "private".
+ key.set_type(KeyType::Private);
+ }
+ // 9. Otherwise:
+ else {
+ // 1. If jwk does not meet the requirements of the JWK public key format described in Section 2 of [RFC8037], then throw a DataError.
+ // o The parameter "kty" MUST be "OKP".
+ if jwk.kty.as_ref() != Some(&Utf16String::from_ascii(b"OKP")) {
+ return Err(dom_error("DataError", b"Invalid key type"));
+ }
+
+ // https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve
+ // o The parameter "crv" MUST be present and contain the subtype of the key (from the "JSON Web Elliptic Curve" registry).
+ if jwk.crv.as_ref() != Some(&Utf16String::from_ascii(b"X25519")) {
+ return Err(dom_error("DataError", b"Invalid curve"));
+ }
+
+ // o The parameter "x" MUST be present and contain the public key encoded using the base64url [RFC4648] encoding.
+ if jwk.x.is_none() {
+ return Err(dom_error("DataError", b"Missing x field"));
+ }
+ let public_key = base64_url_bytes_decode(cx.vm(), jwk.x.as_ref().expect("the coordinate is present"))?;
+ if public_key.as_slice().len() != 32 {
+ return Err(dom_error("DataError", b"Invalid key length"));
+ }
+
+ // o The parameter "d" MUST be present for private keys and contain the private key encoded using the base64url encoding.
+ // This parameter MUST NOT be present for public keys.
+ if jwk.d.is_some() {
+ return Err(dom_error("DataError", b"Present d field"));
+ }
+
+ key = CryptoKey::from_okp_handle(cx.vm(), false, public_key.as_slice());
+
+ // 3. Set the [[type]] internal slot of Key to "public".
+ key.set_type(KeyType::Public);
+ }
+
+ // 10. Let algorithm be a new instance of a KeyAlgorithm object.
+ // 11. Set the name attribute of algorithm to "X25519".
+ // 12. Set the [[algorithm]] internal slot of key to algorithm.
+ key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"X25519"),
+ });
+ }
+ // 2. If format is "raw":
+ // https://wicg.github.io/webcrypto-modern-algos/#subtlecrypto-interface-keyformat
+ // For all existing asymmetric algorithms in WebCrypto, "raw-public" acts as an alias of "raw".
+ else if key_format == KeyFormat::Raw || key_format == KeyFormat::RawPublic {
+ // 1. If usages is not empty then throw a SyntaxError.
+ if !usages.is_empty() {
+ return Err(dom_error("SyntaxError", b"Usages must be empty"));
+ }
+
+ // 2. If the length in bits of keyData is not 256 then throw a DataError.
+ let ImportKeyData::Bytes(data) = key_data else {
+ unreachable!("raw key data contains bytes")
+ };
+ if data.as_slice().len() * 8 != 256 {
+ return Err(dom_error("DataError", b"Invalid key length"));
+ }
+
+ // 5. Let key be a new CryptoKey representing the key data provided in keyData.
+ key = CryptoKey::from_okp_handle(cx.vm(), false, data.as_slice());
+
+ // 6. Set the [[type]] internal slot of key to "public"
+ key.set_type(KeyType::Public);
+
+ // 3. Let algorithm be a new KeyAlgorithm object.
+ // 4. Set the name attribute of algorithm to "X25519".
+ // 7. Set the [[algorithm]] internal slot of key to algorithm.
+ key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"X25519"),
+ });
+ }
+ // 2. Otherwise: throw a NotSupportedError.
+ else {
+ return Err(dom_error("NotSupportedError", b"Invalid key format"));
+ }
+
+ // 3. Return key.
+ Ok(key)
+ }
+
+ // https://w3c.github.io/webcrypto/#x25519-operations-export-key
+ #[allow(
+ clippy::field_reassign_with_default,
+ reason = "the algorithm sets JWK members in separate steps"
+ )]
+ fn export_key(&self, cx: &CallContext, format: KeyFormat, key: Ref<CryptoKey>) -> WebIdlResult<ExportKeyResult> {
+ // NOTE: This is a parameter to the function
+ // 1. Let key be the CryptoKey to be exported.
+
+ // 2. If the underlying cryptographic key material represented by the [[handle]] internal slot of key cannot be accessed, then throw an OperationError.
+ // Note: In our impl this is always accessible
+
+ // 3. If format is "spki":
+ let result = if format == KeyFormat::Spki {
+ // 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
+ if key.key_type() != KeyType::Public {
+ return Err(dom_error("InvalidAccessError", b"Key is not a public key"));
+ }
+
+ // 2. Let data be an instance of the subjectPublicKeyInfo ASN.1 structure defined in [RFC5280] with the following properties:
+ // Set the algorithm field to an AlgorithmIdentifier ASN.1 type with the following properties:
+ // Set the algorithm object identifier to the id-X25519 OID defined in [RFC8410].
+ // Set the subjectPublicKey field to keyData.
+ let data = okp_export_der(cx.vm(), OkpCurve::X25519, true, key.okp_handle_bytes())?;
+
+ // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
+ ExportKeyResult::Bytes(data)
+ }
+ // 3. If format is "pkcs8":
+ else if format == KeyFormat::Pkcs8 {
+ // 1. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.
+ if key.key_type() != KeyType::Private {
+ return Err(dom_error("InvalidAccessError", b"Key is not a private key"));
+ }
+
+ // 2. Let data be an instance of the privateKeyInfo ASN.1 structure defined in [RFC5208] with the following properties:
+ // Set the version field to 0.
+ // Set the privateKeyAlgorithm field to a PrivateKeyAlgorithmIdentifier ASN.1 type with the following properties:
+ // Set the algorithm object identifier to the id-X25519 OID defined in [RFC8410].
+ // Set the privateKey field to the result of DER-encoding a CurvePrivateKey ASN.1 type, as defined in Section 7 of [RFC8410],
+ // that represents the X25519 private key represented by the [[handle]] internal slot of key
+
+ let data = okp_export_der(cx.vm(), OkpCurve::X25519, false, key.okp_handle_bytes())?;
+
+ // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
+ ExportKeyResult::Bytes(data)
+ }
+ // 3. If format is "jwt":
+ else if format == KeyFormat::Jwk {
+ // 1. Let jwk be a new JsonWebKey dictionar1y.
+ let mut jwk = JsonWebKey::default();
+
+ // 2. Set the kty attribute of jwk to "OKP".
+ jwk.kty = Some(Utf16String::from_ascii(b"OKP"));
+
+ // 3. Set the crv attribute of jwk to "X25519".
+ jwk.crv = Some(Utf16String::from_ascii(b"X25519"));
+
+ // 4. Set the x attribute of jwk according to the definition in Section 2 of [RFC8037].
+ if key.key_type() == KeyType::Public {
+ let public_key = key.okp_handle_bytes();
+ jwk.x = Some(okp_encode_jwk_bytes(cx.vm(), public_key)?);
+ } else {
+ // The "x" parameter of the "epk" field is set as follows:
+ // Apply the appropriate ECDH function to the ephemeral private key (as scalar input)
+ // and the standard base point (as u-coordinate input).
+ // The base64url encoding of the output is the value for the "x" parameter of the "epk" field.
+ let public_key = okp_generate_public_key(OkpCurve::X25519, key.okp_handle_bytes())
+ .map_err(|()| out_of_memory(cx.vm()))?;
+ jwk.x = Some(okp_encode_jwk_bytes(cx.vm(), public_key.as_slice())?);
+ }
+
+ // 5. If the [[type]] internal slot of key is "private"
+ if key.key_type() == KeyType::Private {
+ // 1. Set the d attribute of jwk according to the definition in Section 2 of [RFC8037].
+ let private_key = key.okp_handle_bytes();
+ jwk.d = Some(okp_encode_jwk_bytes(cx.vm(), private_key)?);
+ }
+
+ // 6. Set the key_ops attribute of jwk to the usages attribute of key.
+ jwk.key_ops = Some(
+ key.internal_usages()
+ .iter()
+ .map(|usage| Utf16String::from_ascii(KEY_USAGES[*usage as usize]))
+ .collect(),
+ );
+
+ // 7. Set the ext attribute of jwk to the [[extractable]] internal slot of key.
+ jwk.ext = Some(key.is_extractable());
+
+ // 8. Let result be the result of converting jwk to an ECMAScript Object, as defined by [WebIDL].
+ ExportKeyResult::JsonWebKey(jwk)
+ }
+ // 3. If format is "raw":
+ // https://wicg.github.io/webcrypto-modern-algos/#subtlecrypto-interface-keyformat
+ // For all existing asymmetric algorithms in WebCrypto, "raw-public" acts as an alias of "raw".
+ else if format == KeyFormat::Raw || format == KeyFormat::RawPublic {
+ // 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
+ if key.key_type() != KeyType::Public {
+ return Err(dom_error("InvalidAccessError", b"Key is not a public key"));
+ }
+
+ // 2. Let data be an octet string representing the X25519 public key represented by the [[handle]] internal slot of key.
+ let public_key = key.okp_handle_bytes();
+
+ // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
+ ExportKeyResult::Bytes(Bytes::new(public_key.to_vec()))
+ }
+ // 3. Otherwise:
+ else {
+ return Err(dom_error("NotSupportedError", b"Invalid key format"));
+ };
+
+ // 4. Return result.
+ Ok(result)
+ }
+
+ fn public_key_usages(&self) -> Option<Vec<KeyUsage>> {
+ Some(Vec::new())
+ }
+}
+
+#[derive(Clone, Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct X448 {
+ parameters: Option<EcdhKeyDeriveParams>,
+}
+
+impl X448 {
+ pub(crate) fn from_value(vm: &Vm, object: Gc<Object>, has_parameters: bool) -> WebIdlResult<Self> {
+ let parameters = if has_parameters {
+ Some(EcdhKeyDeriveParams::from_value(vm, object)?)
+ } else {
+ None
+ };
+ Ok(Self { parameters })
+ }
+}
+
+impl AlgorithmMethods for X448 {
+ // https://wicg.github.io/webcrypto-secure-curves/#x448-operations
+ fn derive_bits(
+ &self,
+ cx: &CallContext,
+ _: &AlgorithmParams,
+ key: Ref<CryptoKey>,
+ length_optional: Option<u32>,
+ ) -> WebIdlResult<Bytes> {
+ // 1. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.
+ if key.key_type() != KeyType::Private {
+ return Err(dom_error("InvalidAccessError", b"Key is not a private key"));
+ }
+
+ // 2. Let publicKey be the public member of normalizedAlgorithm.
+ let public_key = self
+ .parameters
+ .as_ref()
+ .expect("the operation uses key agreement parameters")
+ .public_key;
+
+ // 3. If the [[type]] internal slot of publicKey is not "public", then throw an InvalidAccessError.
+ if public_key.key_type() != KeyType::Public {
+ return Err(dom_error("InvalidAccessError", b"Public key is not a public key"));
+ }
+
+ // 4. If the name attribute of the [[algorithm]] internal slot of publicKey is not equal to
+ // the name property of the [[algorithm]] internal slot of key, then throw an InvalidAccessError.
+ if key.algorithm_name() != public_key.algorithm_name() {
+ return Err(dom_error("InvalidAccessError", b"Algorithm mismatch"));
+ }
+
+ // 5. Let secret be the result of performing the X448 function specified in [RFC7748] Section 5
+ // with key as the X448 private key k and the X448 public key represented by the [[handle]]
+ // internal slot of publicKey as the X448 public key u.
+ let private_key = key.okp_handle_bytes();
+ let public_key_data = public_key.okp_handle_bytes();
+
+ let mut secret: Option<Bytes> = None;
+ // SAFETY: The input bytes and output slot are lent for the call.
+ let maybe_secret = unsafe {
+ web_crypto_okp_compute_coordinate(
+ OkpCurve::X448,
+ Span::new(private_key),
+ Span::new(public_key_data),
+ core::ptr::from_mut(&mut secret).cast(),
+ receive_bytes,
+ )
+ };
+ if !maybe_secret {
+ return Err(dom_error("OperationError", b"Failed to compute secret"));
+ }
+
+ let secret = secret.expect("LibCrypto returns the shared secret");
+
+ // 6. If secret is the all-zero value, then throw a OperationError. This check must be performed in constant-time, as per [RFC7748] Section 6.2.
+ // NOTE: The check may be performed by ORing all the bytes together and checking whether the result is zero,
+ // as this eliminates standard side-channels in software implementations.
+ let mut or_bytes = 0;
+ for &byte in secret.as_slice() {
+ or_bytes |= byte;
+ }
+
+ if or_bytes == 0 {
+ return Err(dom_error("OperationError", b"Secret is the all-zero value"));
+ }
+
+ // 7. If length is null: Return secret
+ let Some(length) = length_optional else {
+ let result = Bytes::new(copy_bytes(cx.vm(), secret.as_slice())?);
+ return Ok(result);
+ };
+
+ // Otherwise: Return an octet string containing the first length bits of secret.
+ let length = length as usize;
+ if secret.as_slice().len() * 8 < length {
+ return Err(dom_error("OperationError", b"Secret is too short"));
+ }
+
+ let mut slice = copy_bytes(cx.vm(), &secret.as_slice()[..length.div_ceil(8)])?;
+ if !length.is_multiple_of(8) {
+ // Zero out the last bits
+ *slice.last_mut().expect("a partial byte is present") &= 0xFF << (8 - (length % 8));
+ }
+
+ Ok(Bytes::new(slice))
+ }
+
+ // https://wicg.github.io/webcrypto-secure-curves/#x448-operations
+ fn generate_key(
+ &self,
+ cx: &CallContext,
+ _: &AlgorithmParams,
+ extractable: bool,
+ usages: &[KeyUsage],
+ ) -> WebIdlResult<CryptoKeyOrCryptoKeyPair> {
+ // 1. If usages contains an entry which is not "deriveKey" or "deriveBits" then throw a SyntaxError.
+ for &usage in usages {
+ if usage != KeyUsage::DeriveKey && usage != KeyUsage::DeriveBits {
+ return Err(Exception::Dom(
+ "SyntaxError",
+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),
+ ));
+ }
+ }
+
+ // 2. Generate an X448 key pair, with the private key being 56 random bytes, and the public key being X448(a, 5), as defined in [RFC7748], section 6.2.
+ let maybe_private_key = okp_generate_private_key(OkpCurve::X448);
+ let Ok(private_key_data) = maybe_private_key else {
+ return Err(dom_error("OperationError", b"Failed to generate private key"));
+ };
+ let maybe_public_key = okp_generate_public_key(OkpCurve::X448, private_key_data.as_slice());
+ let Ok(public_key_data) = maybe_public_key else {
+ return Err(dom_error("OperationError", b"Failed to generate public key"));
+ };
+
+ // 5. Let publicKey be a new CryptoKey associated with the relevant global object of this [HTML], and representing the public key of the generated key pair.
+ let public_key = CryptoKey::from_okp_handle(cx.vm(), false, public_key_data.as_slice());
+
+ // 6. Set the [[type]] internal slot of publicKey to "public"
+ public_key.set_type(KeyType::Public);
+
+ // 3. Let algorithm be a new KeyAlgorithm object.
+ // 4. Set the name attribute of algorithm to "X448".
+ // 7. Set the [[algorithm]] internal slot of publicKey to algorithm.
+ public_key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"X448"),
+ });
+
+ // 8. Set the [[extractable]] internal slot of publicKey to true.
+ public_key.set_extractable(true);
+
+ // 9. Set the [[usages]] internal slot of publicKey to be the empty list.
+ public_key.set_usages(Vec::new());
+
+ // 10. Let privateKey be a new CryptoKey associated with the relevant global object of this [HTML], and representing the private key of the generated key pair.
+ let private_key = CryptoKey::from_okp_handle(cx.vm(), true, private_key_data.as_slice());
+
+ // 11. Set the [[type]] internal slot of privateKey to "private"
+ private_key.set_type(KeyType::Private);
+
+ // 12. Set the [[algorithm]] internal slot of privateKey to algorithm.
+ private_key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"X448"),
+ });
+
+ // 13. Set the [[extractable]] internal slot of privateKey to extractable.
+ private_key.set_extractable(extractable);
+
+ // 14. Set the [[usages]] internal slot of privateKey to be the usage intersection of usages and [ "deriveKey", "deriveBits" ].
+ private_key.set_usages(usage_intersection(usages, &[KeyUsage::DeriveKey, KeyUsage::DeriveBits]));
+
+ // 15. Let result be a new CryptoKeyPair dictionary.
+ // 16. Set the publicKey attribute of result to be publicKey.
+ // 17. Set the privateKey attribute of result to be privateKey.
+ let result = CryptoKeyPair {
+ public_key: Some(public_key),
+ private_key: Some(private_key),
+ };
+
+ // 18. Return the result of converting result to an ECMAScript Object, as defined by [WebIDL].
+ Ok(CryptoKeyOrCryptoKeyPair::CryptoKeyPair(result))
+ }
+
+ // https://wicg.github.io/webcrypto-secure-curves/#x448-operations
+ #[allow(
+ clippy::field_reassign_with_default,
+ reason = "the algorithm sets JWK members in separate steps"
+ )]
+ fn export_key(&self, cx: &CallContext, format: KeyFormat, key: Ref<CryptoKey>) -> WebIdlResult<ExportKeyResult> {
+ // 1. Let key be the CryptoKey to be exported.
+
+ // 2. If the underlying cryptographic key material represented by the [[handle]] internal slot of key cannot be accessed, then throw an OperationError.
+ // Note: In our impl this is always accessible
+
+ // 3. If format is "spki":
+ if format == KeyFormat::Spki {
+ // 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
+ if key.key_type() != KeyType::Public {
+ return Err(dom_error("InvalidAccessError", b"Key is not a public key"));
+ }
+
+ // 2. Let data be an instance of the subjectPublicKeyInfo ASN.1 structure defined in [RFC5280] with the following properties:
+ // * Set the algorithm field to an AlgorithmIdentifier ASN.1 type with the following properties:
+ // * Set the algorithm object identifier to the id-X448 OID defined in [RFC8410].
+ // * Set the subjectPublicKey field to keyData.
+ let data = okp_export_der(cx.vm(), OkpCurve::X448, true, key.okp_handle_bytes())?;
+
+ // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
+ return Ok(ExportKeyResult::Bytes(data));
+ }
+
+ // 3. If format is "pkcs8":
+ if format == KeyFormat::Pkcs8 {
+ // 1. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.
+ if key.key_type() != KeyType::Private {
+ return Err(dom_error("InvalidAccessError", b"Key is not a private key"));
+ }
+
+ // 2. Let data be an instance of the privateKeyInfo ASN.1 structure defined in [RFC5208] with the following properties:
+ // * Set the version field to 0.
+ // * Set the privateKeyAlgorithm field to a PrivateKeyAlgorithmIdentifier ASN.1 type with the following properties:
+ // * Set the algorithm object identifier to the id-X448 OID defined in [RFC8410].
+ // * Set the privateKey field to the result of DER-encoding a CurvePrivateKey ASN.1 type, as defined in Section 7 of [RFC8410], that represents the X448 private key represented by the [[handle]] internal slot of key
+
+ let data = okp_export_der(cx.vm(), OkpCurve::X448, false, key.okp_handle_bytes())?;
+
+ // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
+ return Ok(ExportKeyResult::Bytes(data));
+ }
+
+ // 3. If format is "jwk":
+ if format == KeyFormat::Jwk {
+ // 1. Let jwk be a new JsonWebKey dictionary.
+ let mut jwk = JsonWebKey::default();
+
+ // 2. Set the kty attribute of jwk to "OKP".
+ jwk.kty = Some(Utf16String::from_ascii(b"OKP"));
+
+ // 3. Set the crv attribute of jwk to "X448".
+ jwk.crv = Some(Utf16String::from_ascii(b"X448"));
+
+ // 4. Set the x attribute of jwk according to the definition in Section 2 of [RFC8037].
+ if key.key_type() == KeyType::Public {
+ jwk.x = Some(okp_encode_jwk_bytes(cx.vm(), key.okp_handle_bytes())?);
+ } else {
+ let public_key = okp_generate_public_key(OkpCurve::X448, key.okp_handle_bytes())
+ .map_err(|()| out_of_memory(cx.vm()))?;
+ jwk.x = Some(okp_encode_jwk_bytes(cx.vm(), public_key.as_slice())?);
+ }
+
+ // 5. If the [[type]] internal slot of key is "private"
+ if key.key_type() == KeyType::Private {
+ // 1. Set the d attribute of jwk according to the definition in Section 2 of [RFC8037].
+ jwk.d = Some(okp_encode_jwk_bytes(cx.vm(), key.okp_handle_bytes())?);
+ }
+
+ // 6. Set the key_ops attribute of jwk to the usages attribute of key.
+ jwk.key_ops = Some(
+ key.internal_usages()
+ .iter()
+ .map(|usage| Utf16String::from_ascii(KEY_USAGES[*usage as usize]))
+ .collect(),
+ );
+
+ // 7. Set the ext attribute of jwk to the [[extractable]] internal slot of key.
+ jwk.ext = Some(key.is_extractable());
+
+ // 8. Let result be the result of converting jwk to an ECMAScript Object, as defined by [WebIDL].
+ return Ok(ExportKeyResult::JsonWebKey(jwk));
+ }
+
+ // 3. If format is "raw":
+ if format == KeyFormat::Raw {
+ // 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
+ if key.key_type() != KeyType::Public {
+ return Err(dom_error("InvalidAccessError", b"Key is not a public key"));
+ }
+
+ // 2. Let data be an octet string representing the X448 public key represented by the [[handle]] internal slot of key.
+ // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
+ return Ok(ExportKeyResult::Bytes(Bytes::new(key.okp_handle_bytes().to_vec())));
+ }
+
+ // 3. Otherwise:
+ // throw a NotSupportedError.
+ Err(dom_error("NotSupportedError", b"Invalid key format"))
+ }
+
+ fn public_key_usages(&self) -> Option<Vec<KeyUsage>> {
+ Some(Vec::new())
+ }
+
+ // https://wicg.github.io/webcrypto-secure-curves/#x448-operations
+ #[allow(
+ clippy::collapsible_if,
+ reason = "the specification checks key usage in a separate step"
+ )]
+ fn import_key(
+ &self,
+ cx: &CallContext,
+ _: &AlgorithmParams,
+ format: KeyFormat,
+ key_data: ImportKeyData,
+ extractable: bool,
+ usages: &[KeyUsage],
+ ) -> WebIdlResult<Ref<CryptoKey>> {
+ // 1. Let keyData be the key data to be imported.
+
+ // 2. If format is "spki":
+ if format == KeyFormat::Spki {
+ // 1. If usages is not empty then throw a SyntaxError.
+ if !usages.is_empty() {
+ return Err(dom_error("SyntaxError", b"Usages must be empty"));
+ }
+
+ // 2. Let spki be the result of running the parse a subjectPublicKeyInfo algorithm over keyData.
+ // 3. If an error occurred while parsing, then throw a DataError.
+ let ImportKeyData::Bytes(key_data) = key_data else {
+ unreachable!("DER key data contains bytes")
+ };
+ let spki = Asn1KeyInfo::parse(&key_data, Asn1Structure::SubjectPublicKeyInfo)?;
+
+ // 4. If the algorithm object identifier field of the algorithm AlgorithmIdentifier field of spki is not equal to the id-X448 object identifier defined in [RFC8410], then throw a DataError.
+ if !spki.has_okp_oid(OkpCurve::X448) {
+ return Err(dom_error("DataError", b"Invalid algorithm"));
+ }
+
+ // 5. If the parameters field of the algorithm AlgorithmIdentifier field of spki is present, then throw a DataError.
+ if spki.ec_parameters().is_some() {
+ return Err(dom_error("DataError", b"Invalid algorithm parameters"));
+ }
+
+ // 6. Let publicKey be the X448 public key identified by the subjectPublicKey field of spki.
+ let public_key = spki.raw_key();
+
+ // 7. Let key be a new CryptoKey associated with the relevant global object of this [HTML], and that represents publicKey.
+ let key = CryptoKey::from_okp_handle(cx.vm(), false, public_key);
+
+ // 8. Set the [[type]] internal slot of key to "public"
+ key.set_type(KeyType::Public);
+
+ // 9. Let algorithm be a new KeyAlgorithm.
+ // 10. Set the name attribute of algorithm to "X448".
+ // 11. Set the [[algorithm]] internal slot of key to algorithm.
+ key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"X448"),
+ });
+
+ return Ok(key);
+ }
+
+ // 3. If format is "pkcs8":
+ if format == KeyFormat::Pkcs8 {
+ // 1. If usages contains an entry which is not "deriveKey" or "deriveBits" then throw a SyntaxError.
+ for &usage in usages {
+ if usage != KeyUsage::DeriveKey && usage != KeyUsage::DeriveBits {
+ return Err(Exception::Dom(
+ "SyntaxError",
+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),
+ ));
+ }
+ }
+
+ // 2. Let privateKeyInfo be the result of running the parse a privateKeyInfo algorithm over keyData.
+ // 3. If an error occurred while parsing, then throw a DataError.
+ let ImportKeyData::Bytes(key_data) = key_data else {
+ unreachable!("DER key data contains bytes")
+ };
+ let private_key_info = Asn1KeyInfo::parse(&key_data, Asn1Structure::PrivateKeyInfo)?;
+
+ // 4. If the algorithm object identifier field of the privateKeyAlgorithm PrivateKeyAlgorithm field of privateKeyInfo is not equal to the id-X448 object identifier defined in [RFC8410], then throw a DataError.
+ if !private_key_info.has_okp_oid(OkpCurve::X448) {
+ return Err(dom_error("DataError", b"Invalid algorithm"));
+ }
+
+ // 5. If the parameters field of the privateKeyAlgorithm PrivateKeyAlgorithmIdentifier field of privateKeyInfo is present, then throw a DataError.
+ if private_key_info.ec_parameters().is_some() {
+ return Err(dom_error("DataError", b"Invalid algorithm parameters"));
+ }
+
+ // 6. Let curvePrivateKey be the result of performing the parse an ASN.1 structure algorithm,
+ // with data as the privateKey field of privateKeyInfo, structure as the ASN.1 CurvePrivateKey
+ // structure specified in Section 7 of [RFC8410], and exactData set to true.
+ // 7. If an error occurred while parsing, then throw a DataError.
+ let curve_private_key_bytes = parse_octet_string(cx.vm(), private_key_info.raw_key())?;
+
+ // 8. Let key be a new CryptoKey associated with the relevant global object of this [HTML], and that represents the X448 private key identified by curvePrivateKey.
+ let key = CryptoKey::from_okp_handle(cx.vm(), true, curve_private_key_bytes.as_slice());
+
+ // 9. Set the [[type]] internal slot of key to "private"
+ key.set_type(KeyType::Private);
+
+ // 10. Let algorithm be a new KeyAlgorithm.
+ // 11. Set the name attribute of algorithm to "X448".
+ // 12. Set the [[algorithm]] internal slot of key to algorithm.
+ key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"X448"),
+ });
+
+ return Ok(key);
+ }
+
+ // 3. If format is "jwk":
+ if format == KeyFormat::Jwk {
+ // 1. If keyData is a JsonWebKey dictionary:
+ // Let jwk equal keyData.
+ // Otherwise:
+ // Throw a DataError.
+ let ImportKeyData::JsonWebKey(jwk) = key_data else {
+ return Err(dom_error("DataError", b"Data is not a JsonWebKey dictionary"));
+ };
+
+ // 2. If the d field is present and if usages contains an entry which is not "deriveKey" or "deriveBits" then throw a SyntaxError.
+ if jwk.d.is_some() {
+ for &usage in usages {
+ if usage != KeyUsage::DeriveKey && usage != KeyUsage::DeriveBits {
+ return Err(Exception::Dom(
+ "SyntaxError",
+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),
+ ));
+ }
+ }
+ }
+
+ // 3. If the d field is not present and if usages is not empty then throw a SyntaxError.
+ if jwk.d.is_none() && !usages.is_empty() {
+ return Err(dom_error("SyntaxError", b"Usages must be empty"));
+ }
+
+ // 4. If the kty field of jwk is not "OKP", then throw a DataError.
+ if jwk.kty.as_ref() != Some(&Utf16String::from_ascii(b"OKP")) {
+ return Err(dom_error("DataError", b"Invalid key type"));
+ }
+
+ // 5. If the crv field of jwk is not "X448", then throw a DataError.
+ if jwk.crv.as_ref() != Some(&Utf16String::from_ascii(b"X448")) {
+ return Err(dom_error("DataError", b"Invalid curve"));
+ }
+
+ // 6. If usages is non-empty and the use field of jwk is present and is not equal to "enc" then throw a DataError.
+ if !usages.is_empty()
+ && jwk
+ .r#use
+ .as_ref()
+ .is_some_and(|key_use| *key_use != Utf16String::from_ascii(b"enc"))
+ {
+ return Err(dom_error("DataError", b"Invalid use"));
+ }
+
+ // 7. If the key_ops field of jwk is present, and is invalid according to the requirements of JSON Web Key [JWK],
+ // or it does not contain all of the specified usages values, then throw a DataError.
+ validate_jwk_key_ops(&jwk, usages)?;
+
+ // 8. If the ext field of jwk is present and has the value false and extractable is true, then throw a DataError.
+ if jwk.ext == Some(false) && extractable {
+ return Err(dom_error("DataError", b"Invalid extractable"));
+ }
+
+ let key;
+
+ // 9. If the d field is present:
+ if let Some(d) = &jwk.d {
+ // 1. If jwk does not meet the requirements of the JWK private key format described in Section 2 of [RFC8037], then throw a DataError.
+
+ // o The parameter "kty" MUST be "OKP".
+ if jwk.kty.as_ref() != Some(&Utf16String::from_ascii(b"OKP")) {
+ return Err(dom_error("DataError", b"Invalid key type"));
+ }
+
+ // https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve
+ // o The parameter "crv" MUST be present and contain the subtype of the key (from the "JSON Web Elliptic Curve" registry).
+ if jwk.crv.as_ref() != Some(&Utf16String::from_ascii(b"X448")) {
+ return Err(dom_error("DataError", b"Invalid curve"));
+ }
+
+ // o The parameter "x" MUST be present and contain the public key encoded using the base64url [RFC4648] encoding.
+ if jwk.x.is_none() {
+ return Err(dom_error("DataError", b"Missing x field"));
+ }
+ let public_key = base64_url_bytes_decode(cx.vm(), jwk.x.as_ref().expect("the coordinate is present"))?;
+ if public_key.as_slice().len() != 56 {
+ return Err(dom_error("DataError", b"Invalid key length"));
+ }
+
+ // o The parameter "d" MUST be present for private keys and contain the private key encoded using the base64url encoding.
+ // This parameter MUST NOT be present for public keys.
+ if jwk.d.is_none() {
+ return Err(dom_error("DataError", b"Missing d field"));
+ }
+ let private_key = base64_url_bytes_decode(cx.vm(), d)?;
+ if private_key.as_slice().len() != 56 {
+ return Err(dom_error("DataError", b"Invalid key length"));
+ }
+
+ let derived_public_key = okp_generate_public_key(OkpCurve::X448, private_key.as_slice())
+ .map_err(|()| out_of_memory(cx.vm()))?;
+ if derived_public_key.as_slice() != public_key.as_slice() {
+ return Err(dom_error("DataError", b"Invalid key pair"));
+ }
+
+ // 2. Let key be a new CryptoKey object that represents the X448 private key identified by interpreting jwk according to Section 2 of [RFC8037].
+ key = CryptoKey::from_okp_handle(cx.vm(), true, private_key.as_slice());
+
+ // 3. Set the [[type]] internal slot of Key to "private".
+ key.set_type(KeyType::Private);
+ }
+ // Otherwise:
+ else {
+ // 1. If jwk does not meet the requirements of the JWK public key format described in Section 2 of [RFC8037], then throw a DataError.
+ // o The parameter "kty" MUST be "OKP".
+ if jwk.kty.as_ref() != Some(&Utf16String::from_ascii(b"OKP")) {
+ return Err(dom_error("DataError", b"Invalid key type"));
+ }
+
+ // https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve
+ // o The parameter "crv" MUST be present and contain the subtype of the key (from the "JSON Web Elliptic Curve" registry).
+ if jwk.crv.as_ref() != Some(&Utf16String::from_ascii(b"X448")) {
+ return Err(dom_error("DataError", b"Invalid curve"));
+ }
+
+ // o The parameter "x" MUST be present and contain the public key encoded using the base64url [RFC4648] encoding.
+ if jwk.x.is_none() {
+ return Err(dom_error("DataError", b"Missing x field"));
+ }
+ let public_key = base64_url_bytes_decode(cx.vm(), jwk.x.as_ref().expect("the coordinate is present"))?;
+ if public_key.as_slice().len() != 56 {
+ return Err(dom_error("DataError", b"Invalid key length"));
+ }
+
+ // o The parameter "d" MUST be present for private keys and contain the private key encoded using the base64url encoding.
+ // This parameter MUST NOT be present for public keys.
+ if jwk.d.is_some() {
+ return Err(dom_error("DataError", b"Present d field"));
+ }
+
+ // 2. Let key be a new CryptoKey object that represents the X448 public key identified by interpreting jwk according to Section 2 of [RFC8037].
+ key = CryptoKey::from_okp_handle(cx.vm(), false, public_key.as_slice());
+
+ // 3. Set the [[type]] internal slot of Key to "public".
+ key.set_type(KeyType::Public);
+ }
+
+ // 10. Let algorithm be a new instance of a KeyAlgorithm object.
+ // 11. Set the name attribute of algorithm to "X448".
+ // 12. Set the [[algorithm]] internal slot of key to algorithm.
+ key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"X448"),
+ });
+
+ return Ok(key);
+ }
+
+ // 3. If format is "raw":
+ if format == KeyFormat::Raw {
+ // 1. If usages is not empty then throw a SyntaxError.
+ if !usages.is_empty() {
+ return Err(dom_error("SyntaxError", b"Usages must be empty"));
+ }
+
+ // 2.Let data be keyData.
+ let ImportKeyData::Bytes(data) = key_data else {
+ unreachable!("raw key data contains bytes")
+ };
+
+ // 3. If the length in bits of data is not 448 then throw a DataError.
+ if data.as_slice().len() * 8 != 448 {
+ return Err(dom_error("DataError", b"Invalid key length"));
+ }
+
+ // 6. Let key be a new CryptoKey associated with the relevant global object of this [HTML], and that represents data.
+ let key = CryptoKey::from_okp_handle(cx.vm(), false, data.as_slice());
+
+ // 7. Set the [[type]] internal slot of key to "public"
+ key.set_type(KeyType::Public);
+
+ // 4. Let algorithm be a new KeyAlgorithm object.
+ // 5. Set the name attribute of algorithm to "X448".
+ // 8. Set the [[algorithm]] internal slot of key to algorithm.
+ key.set_algorithm(InternalAlgorithmData::Key {
+ name: Utf16String::from_ascii(b"X448"),
+ });
+
+ return Ok(key);
+ }
+
+ let _ = extractable;
+
+ // 3. Otherwise:
+ // throw a NotSupportedError.
+ Err(dom_error("NotSupportedError", b"Invalid key format"))
+ }
+}
+
unsafe extern "C" fn receive_bytes(state: *mut c_void, bytes: Span<'_, u8>) {
// SAFETY: The state is the caller's slot, and the service lends the bytes.
unsafe { *state.cast::<Option<Bytes>>() = Some(Bytes::new(bytes.as_slice().to_vec())) };
diff --git a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_key.rs b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_key.rs
index ac0edd9699e..8b76c532ac8 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_key.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_key.rs
@@ -302,6 +302,22 @@ impl CryptoKey {
bytes.as_slice()
}
+ pub(crate) fn from_okp_handle(vm: &Vm, is_private: bool, bytes: &[u8]) -> Ref<Self> {
+ // SAFETY: The bytes are lent to the service for the copy.
+ let handle = unsafe { web_crypto_key_create_okp_handle(is_private, Span::new(bytes)) };
+ Self::create(
+ vm,
+ CryptoKeyHandle(NonNull::new(handle).expect("LibWeb returns an OKP key handle")),
+ )
+ }
+
+ pub(crate) fn okp_handle_bytes(&self) -> &[u8] {
+ let mut bytes = Span::new(&[]);
+ // SAFETY: The service borrows bytes from the handle for as long as the key is borrowed.
+ unsafe { web_crypto_key_okp_handle_bytes(self.handle().as_ptr(), &mut bytes) };
+ bytes.as_slice()
+ }
+
pub(crate) fn handle(&self) -> NonNull<c_void> {
self.handle.borrow().as_ref().expect("a live key has its handle").0
}
@@ -426,6 +442,8 @@ struct RawHandle<'a> {
unsafe extern "C" {
fn web_crypto_key_destroy_handle(handle: *mut c_void);
fn web_crypto_key_create_byte_handle(bytes: Span<'_, u8>) -> *mut c_void;
+ fn web_crypto_key_create_okp_handle(is_private: bool, bytes: Span<'_, u8>) -> *mut c_void;
+ fn web_crypto_key_okp_handle_bytes(handle: *mut c_void, bytes: &mut Span<'_, u8>);
fn web_crypto_key_byte_handle_bytes(handle: *mut c_void, bytes: &mut Span<'_, u8>);
fn web_crypto_key_encode_handle(
handle: *mut c_void,
diff --git a/Libraries/LibWeb/Bindings/Rust/src/crypto/subtle_crypto.rs b/Libraries/LibWeb/Bindings/Rust/src/crypto/subtle_crypto.rs
index 038d6f963d3..2581ecf69dd 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/crypto/subtle_crypto.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/crypto/subtle_crypto.rs
@@ -32,10 +32,10 @@ use crate::bindings::promise::{
use crate::bindings::{CallContext, Exception, MustNotThrow, WebIdlResult};
use crate::crypto::crypto_algorithms::{
AesCbc, AesCtr, AesGcm, AesKw, AesOcb, AesParameterType, AlgorithmId, AlgorithmMethods, AlgorithmParams, Argon2,
- Argon2Params, Bytes, CShakeParams, ChaCha20Poly1305, CppAlgorithm, EcParameterType, Ecdh, Ecdsa,
+ Argon2Params, Bytes, CShakeParams, ChaCha20Poly1305, CppAlgorithm, EcParameterType, Ecdh, Ecdsa, Ed448, Ed25519,
EncapsulatedKeyData, ExportKeyResult, Hkdf, HkdfParams, Hmac, HmacImportParams, HmacKeyGenParams, ImportKeyData,
Kmac, KmacImportParams, KmacKeyGenParams, KmacParams, ParameterId, Pbkdf2, Pbkdf2Params, RsaOaep, RsaParameterType,
- RsaPss, RsassaPkcs1, Sha, normalized_key_usages, public_key_for_private_key,
+ RsaPss, RsassaPkcs1, Sha, X448, X25519, normalized_key_usages, public_key_for_private_key,
};
#[allow(
unused_imports,
@@ -115,6 +115,10 @@ enum RegisteredAlgorithm {
RsassaPkcs1(RsaParameterType),
Ecdsa(EcParameterType),
Ecdh(EcParameterType),
+ Ed25519,
+ Ed448(bool),
+ X25519(bool),
+ X448(bool),
CShake,
}
@@ -150,6 +154,10 @@ enum AlgorithmImplementation {
RsassaPkcs1(RsassaPkcs1),
Ecdsa(Ecdsa),
Ecdh(Ecdh),
+ Ed25519(Ed25519),
+ Ed448(Ed448),
+ X25519(X25519),
+ X448(X448),
CShake(CShakeParams),
}
@@ -184,6 +192,10 @@ impl Deref for AlgorithmImplementation {
Self::RsassaPkcs1(algorithm) => algorithm,
Self::Ecdsa(algorithm) => algorithm,
Self::Ecdh(algorithm) => algorithm,
+ Self::Ed25519(algorithm) => algorithm,
+ Self::Ed448(algorithm) => algorithm,
+ Self::X25519(algorithm) => algorithm,
+ Self::X448(algorithm) => algorithm,
Self::CShake(algorithm) => algorithm,
}
}
@@ -431,6 +443,16 @@ pub(crate) fn normalize_an_algorithm(
RegisteredAlgorithm::Ecdh(parameter_type) => {
AlgorithmImplementation::Ecdh(Ecdh::from_value(vm, realm, *algorithm, parameter_type)?)
}
+ RegisteredAlgorithm::Ed25519 => AlgorithmImplementation::Ed25519(Ed25519),
+ RegisteredAlgorithm::Ed448(has_parameters) => {
+ AlgorithmImplementation::Ed448(Ed448::from_value(vm, *algorithm, has_parameters)?)
+ }
+ RegisteredAlgorithm::X25519(has_parameters) => {
+ AlgorithmImplementation::X25519(X25519::from_value(vm, *algorithm, has_parameters)?)
+ }
+ RegisteredAlgorithm::X448(has_parameters) => {
+ AlgorithmImplementation::X448(X448::from_value(vm, *algorithm, has_parameters)?)
+ }
RegisteredAlgorithm::CShake => AlgorithmImplementation::CShake(CShakeParams::from_value(vm, *algorithm)?),
};
@@ -3283,25 +3305,25 @@ fn supported_algorithms() -> &'static SupportedAlgorithmsMap {
&mut internal_object,
"deriveBits",
"X25519",
- RegisteredAlgorithm::Cpp(AlgorithmId::X25519, ParameterId::EcdhKeyDeriveParams),
+ RegisteredAlgorithm::X25519(true),
);
define_an_algorithm(
&mut internal_object,
"generateKey",
"X25519",
- RegisteredAlgorithm::Cpp(AlgorithmId::X25519, ParameterId::AlgorithmParams),
+ RegisteredAlgorithm::X25519(false),
);
define_an_algorithm(
&mut internal_object,
"importKey",
"X25519",
- RegisteredAlgorithm::Cpp(AlgorithmId::X25519, ParameterId::AlgorithmParams),
+ RegisteredAlgorithm::X25519(false),
);
define_an_algorithm(
&mut internal_object,
"exportKey",
"X25519",
- RegisteredAlgorithm::Cpp(AlgorithmId::X25519, ParameterId::AlgorithmParams),
+ RegisteredAlgorithm::X25519(false),
);
// https://wicg.github.io/webcrypto-secure-curves/#x448-registration
@@ -3309,89 +3331,74 @@ fn supported_algorithms() -> &'static SupportedAlgorithmsMap {
&mut internal_object,
"deriveBits",
"X448",
- RegisteredAlgorithm::Cpp(AlgorithmId::X448, ParameterId::EcdhKeyDeriveParams),
+ RegisteredAlgorithm::X448(true),
);
define_an_algorithm(
&mut internal_object,
"generateKey",
"X448",
- RegisteredAlgorithm::Cpp(AlgorithmId::X448, ParameterId::AlgorithmParams),
+ RegisteredAlgorithm::X448(false),
);
define_an_algorithm(
&mut internal_object,
"importKey",
"X448",
- RegisteredAlgorithm::Cpp(AlgorithmId::X448, ParameterId::AlgorithmParams),
+ RegisteredAlgorithm::X448(false),
);
define_an_algorithm(
&mut internal_object,
"exportKey",
"X448",
- RegisteredAlgorithm::Cpp(AlgorithmId::X448, ParameterId::AlgorithmParams),
+ RegisteredAlgorithm::X448(false),
);
// https://wicg.github.io/webcrypto-secure-curves/#ed25519-registration
- define_an_algorithm(
- &mut internal_object,
- "sign",
- "Ed25519",
- RegisteredAlgorithm::Cpp(AlgorithmId::ED25519, ParameterId::AlgorithmParams),
- );
- define_an_algorithm(
- &mut internal_object,
- "verify",
- "Ed25519",
- RegisteredAlgorithm::Cpp(AlgorithmId::ED25519, ParameterId::AlgorithmParams),
- );
+ define_an_algorithm(&mut internal_object, "sign", "Ed25519", RegisteredAlgorithm::Ed25519);
+ define_an_algorithm(&mut internal_object, "verify", "Ed25519", RegisteredAlgorithm::Ed25519);
define_an_algorithm(
&mut internal_object,
"generateKey",
"Ed25519",
- RegisteredAlgorithm::Cpp(AlgorithmId::ED25519, ParameterId::AlgorithmParams),
+ RegisteredAlgorithm::Ed25519,
);
define_an_algorithm(
&mut internal_object,
"importKey",
"Ed25519",
- RegisteredAlgorithm::Cpp(AlgorithmId::ED25519, ParameterId::AlgorithmParams),
+ RegisteredAlgorithm::Ed25519,
);
define_an_algorithm(
&mut internal_object,
"exportKey",
"Ed25519",
- RegisteredAlgorithm::Cpp(AlgorithmId::ED25519, ParameterId::AlgorithmParams),
+ RegisteredAlgorithm::Ed25519,
);
// https://wicg.github.io/webcrypto-secure-curves/#ed448-registration
- define_an_algorithm(
- &mut internal_object,
- "sign",
- "Ed448",
- RegisteredAlgorithm::Cpp(AlgorithmId::ED448, ParameterId::Ed448Params),
- );
+ define_an_algorithm(&mut internal_object, "sign", "Ed448", RegisteredAlgorithm::Ed448(true));
define_an_algorithm(
&mut internal_object,
"verify",
"Ed448",
- RegisteredAlgorithm::Cpp(AlgorithmId::ED448, ParameterId::Ed448Params),
+ RegisteredAlgorithm::Ed448(true),
);
define_an_algorithm(
&mut internal_object,
"generateKey",
"Ed448",
- RegisteredAlgorithm::Cpp(AlgorithmId::ED448, ParameterId::AlgorithmParams),
+ RegisteredAlgorithm::Ed448(false),
);
define_an_algorithm(
&mut internal_object,
"importKey",
"Ed448",
- RegisteredAlgorithm::Cpp(AlgorithmId::ED448, ParameterId::AlgorithmParams),
+ RegisteredAlgorithm::Ed448(false),
);
define_an_algorithm(
&mut internal_object,
"exportKey",
"Ed448",
- RegisteredAlgorithm::Cpp(AlgorithmId::ED448, ParameterId::AlgorithmParams),
+ RegisteredAlgorithm::Ed448(false),
);
// https://wicg.github.io/webcrypto-modern-algos/#ml-dsa-registration
diff --git a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp
index c69495218ec..fc6bbe922e8 100644
--- a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp
+++ b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp
@@ -335,21 +335,9 @@ static WebIDL::ExceptionOr<void> validate_jwk_key_ops(JS::Realm&, JsonWebKey con
AlgorithmParams::~AlgorithmParams() = default;
-EcdhKeyDeriveParams::~EcdhKeyDeriveParams() = default;
+ContextParams::~ContextParams() = default;
-JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> EcdhKeyDeriveParams::from_value(JS::Realm& realm, JS::Value value)
-{
- auto& vm = realm.vm();
- auto& object = value.as_object();
-
- auto key = TRY(crypto_key_from_value(vm, TRY(object.get("public"_utf16_fly_string))));
-
- return adopt_own<AlgorithmParams>(*new EcdhKeyDeriveParams { key });
-}
-
-Ed448Params::~Ed448Params() = default;
-
-JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> Ed448Params::from_value(JS::Realm& realm, JS::Value value)
+JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> ContextParams::from_value(JS::Realm& realm, JS::Value value)
{
auto& vm = realm.vm();
auto& object = value.as_object();
@@ -362,7 +350,7 @@ JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> Ed448Params::from_value(JS
maybe_context = TRY_OR_THROW_OOM(vm, WebIDL::get_buffer_source_copy(context_value.as_object()));
}
- return adopt_own<AlgorithmParams>(*new Ed448Params { maybe_context });
+ return adopt_own<AlgorithmParams>(*new ContextParams { maybe_context });
}
extern "C" {
@@ -391,6 +379,8 @@ void* web_crypto_asn1_allocate_key_info(Asn1Structure structure)
{
if (structure == Asn1Structure::SubjectPublicKeyInfo)
return new ::Crypto::Certificate::SubjectPublicKey;
+ if (structure == Asn1Structure::OctetString)
+ return new StringView;
VERIFY(structure == Asn1Structure::PrivateKeyInfo);
return new ::Crypto::Certificate::PrivateKey;
}
@@ -401,6 +391,10 @@ void web_crypto_asn1_delete_key_info(Asn1Structure structure, void* key_info)
delete static_cast<::Crypto::Certificate::SubjectPublicKey*>(key_info);
return;
}
+ if (structure == Asn1Structure::OctetString) {
+ delete static_cast<StringView*>(key_info);
+ return;
+ }
VERIFY(structure == Asn1Structure::PrivateKeyInfo);
delete static_cast<::Crypto::Certificate::PrivateKey*>(key_info);
}
@@ -981,2000 +975,143 @@ bool web_crypto_ec_export_private_scalar(CryptoKey::InternalKeyData const* key,
return true;
}
-// https://w3c.github.io/webcrypto/#ed25519-operations-generate-key
-WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> ED25519::generate_key(JS::Realm&, [[maybe_unused]] AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages)
+enum class OkpCurve : u8 {
+ Ed25519,
+ Ed448,
+ X25519,
+ X448,
+};
+
+static ::Crypto::Curves::EdwardsCurve okp_curve(OkpCurve curve)
{
- // 1. If usages contains an entry which is not one of "sign" or "verify", then throw a SyntaxError.
- for (auto const& usage : key_usages) {
- if (usage != KeyUsage::Sign && usage != KeyUsage::Verify) {
- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));
- }
+ switch (curve) {
+ case OkpCurve::Ed25519:
+ return ::Crypto::Curves::Ed25519 {};
+ case OkpCurve::Ed448:
+ return ::Crypto::Curves::Ed448 {};
+ case OkpCurve::X25519:
+ return ::Crypto::Curves::X25519 {};
+ case OkpCurve::X448:
+ return ::Crypto::Curves::X448 {};
}
-
- // 2. Generate an Ed25519 key pair, as defined in [RFC8032], section 5.1.5.
- ::Crypto::Curves::Ed25519 curve;
- auto maybe_private_key = curve.generate_private_key();
- if (maybe_private_key.is_error())
- return WebIDL::OperationError::create("Failed to generate private key"_utf16);
- auto private_key_data = maybe_private_key.release_value();
-
- auto maybe_public_key = curve.generate_public_key(private_key_data);
- if (maybe_public_key.is_error())
- return WebIDL::OperationError::create("Failed to generate public key"_utf16);
- auto public_key_data = maybe_public_key.release_value();
-
- // 5. Let publicKey be a new CryptoKey associated with the relevant global object of this [HTML],
- // and representing the public key of the generated key pair.
- auto public_key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPublicKey { move(public_key_data) } });
-
- // 6. Set the [[type]] internal slot of publicKey to "public"
- public_key->set_type(KeyType::Public);
-
- // 3. Let algorithm be a new KeyAlgorithm object.
- // 4. Set the name attribute of algorithm to "Ed25519".
- // 7. Set the [[algorithm]] internal slot of publicKey to algorithm.
- public_key->set_algorithm(CryptoKey::KeyAlgorithmData { "Ed25519"_utf16 });
-
- // 8. Set the [[extractable]] internal slot of publicKey to true.
- public_key->set_extractable(true);
-
- // 9. Set the [[usages]] internal slot of publicKey to be the usage intersection of usages and [ "verify" ].
- public_key->set_usages(usage_intersection(key_usages, { { KeyUsage::Verify } }));
-
- // 10. Let privateKey be a new CryptoKey associated with the relevant global object of this [HTML],
- // and representing the private key of the generated key pair.
- auto private_key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPrivateKey { move(private_key_data) } });
-
- // 11. Set the [[type]] internal slot of privateKey to "private"
- private_key->set_type(KeyType::Private);
-
- // 12. Set the [[algorithm]] internal slot of privateKey to algorithm.
- private_key->set_algorithm(CryptoKey::KeyAlgorithmData { "Ed25519"_utf16 });
-
- // 13. Set the [[extractable]] internal slot of privateKey to extractable.
- private_key->set_extractable(extractable);
-
- // 14. Set the [[usages]] internal slot of privateKey to be the usage intersection of usages and [ "sign" ].
- private_key->set_usages(usage_intersection(key_usages, { { KeyUsage::Sign } }));
-
- // 15. Let result be a new CryptoKeyPair dictionary.
- // 16. Set the publicKey attribute of result to be publicKey.
- // 17. Set the privateKey attribute of result to be privateKey.
- // 18. Return the result of converting result to an ECMAScript Object, as defined by [WebIDL].
- return Variant<GC::Ref<CryptoKey>, CryptoKeyPair> { CryptoKeyPair { public_key, private_key } };
+ VERIFY_NOT_REACHED();
}
-// https://w3c.github.io/webcrypto/#ed25519-operations-import-key
-WebIDL::ExceptionOr<GC::Ref<CryptoKey>> ED25519::import_key(JS::Realm& realm,
- [[maybe_unused]] Web::Crypto::AlgorithmParams const& params,
- KeyFormat format,
- CryptoKey::ImportKeyData key_data,
- bool extractable,
- Vector<KeyUsage> const& usages)
+static ReadonlySpan<u32> okp_curve_oid(OkpCurve curve)
{
- GC::Ptr<CryptoKey> key = nullptr;
-
- // 1. Let keyData be the key data to be imported.
-
- // 2. If format is "spki":
- if (format == KeyFormat::Spki) {
- // 1. If usages contains an entry which is not "verify" then throw a SyntaxError.
- for (auto const& usage : usages) {
- if (usage != KeyUsage::Verify) {
- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));
- }
- }
-
- // 2. Let spki be the result of running the parse a subjectPublicKeyInfo algorithm over keyData.
- // 3. If an error occurred while parsing, then throw a DataError.
- auto spki = TRY(parse_a_subject_public_key_info(realm, key_data.get<ByteBuffer>()));
-
- // 4. If the algorithm object identifier field of the algorithm AlgorithmIdentifier field of spki
- // is not equal to the id-Ed25519 object identifier defined in [RFC8410], then throw a DataError.
- if (spki.algorithm.identifier != ::Crypto::ASN1::ed25519_oid)
- return WebIDL::DataError::create("Invalid algorithm identifier"_utf16);
-
- // 5. If the parameters field of the algorithm AlgorithmIdentifier field of spki is present, then throw a DataError.
- if (spki.algorithm.ec_parameters.has_value())
- return WebIDL::DataError::create("Invalid algorithm parameters"_utf16);
-
- // 6. Let publicKey be the Ed25519 public key identified by the subjectPublicKey field of spki.
- auto const& public_key = spki.raw_key;
-
- // 7. Let key be a new CryptoKey associated with the relevant global object of this [HTML],
- // and that represents publicKey.
- key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPublicKey { move(public_key) } });
-
- // 8. Set the [[type]] internal slot of key to "public"
- key->set_type(KeyType::Public);
-
- // 9. Let algorithm be a new KeyAlgorithm.
- // 10. Set the name attribute of algorithm to "Ed25519".
- // 11. Set the [[algorithm]] internal slot of key to algorithm.
- key->set_algorithm(CryptoKey::KeyAlgorithmData { "Ed25519"_utf16 });
- }
-
- // 2. If format is "pkcs8":
- else if (format == KeyFormat::Pkcs8) {
- // 1. If usages contains an entry which is not "sign" then throw a SyntaxError.
- for (auto const& usage : usages) {
- if (usage != KeyUsage::Sign) {
- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));
- }
- }
-
- // 2. Let privateKeyInfo be the result of running the parse a privateKeyInfo algorithm over keyData.
- // 3. If an error occurs while parsing, then throw a DataError.
- auto private_key_info = TRY(parse_a_private_key_info(realm, key_data.get<ByteBuffer>()));
-
- // 4. If the algorithm object identifier field of the privateKeyAlgorithm PrivateKeyAlgorithm field
- // of privateKeyInfo is not equal to the id-Ed25519 object identifier defined in [RFC8410], then throw a DataError.
- if (private_key_info.algorithm.identifier != ::Crypto::ASN1::ed25519_oid)
- return WebIDL::DataError::create("Invalid algorithm identifier"_utf16);
-
- // 5. If the parameters field of the privateKeyAlgorithm PrivateKeyAlgorithmIdentifier field of privateKeyInfo is present,
- // then throw a DataError.
- if (private_key_info.algorithm.ec_parameters.has_value())
- return WebIDL::DataError::create("Invalid algorithm parameters"_utf16);
-
- // 6. Let curvePrivateKey be the result of performing the parse an ASN.1 structure algorithm,
- // with data as the privateKey field of privateKeyInfo, structure as the ASN.1 CurvePrivateKey structure
- // specified in Section 7 of [RFC8410], and exactData set to true.
- // 7. If an error occurred while parsing, then throw a DataError.
- auto curve_private_key = TRY(parse_an_ASN1_structure<StringView>(realm, private_key_info.raw_key, true));
- auto curve_private_key_bytes = TRY_OR_THROW_OOM(realm.vm(), ByteBuffer::copy(curve_private_key.bytes()));
-
- // 8. Let key be a new CryptoKey associated with the relevant global object of this [HTML],
- // and that represents the Ed25519 private key identified by curvePrivateKey.
- key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPrivateKey { move(curve_private_key_bytes) } });
-
- // 9. Set the [[type]] internal slot of key to "private"
- key->set_type(KeyType::Private);
-
- // 10. Let algorithm be a new KeyAlgorithm.
- // 11. Set the name attribute of algorithm to "Ed25519".
- // 12. Set the [[algorithm]] internal slot of key to algorithm.
- key->set_algorithm(CryptoKey::KeyAlgorithmData { "Ed25519"_utf16 });
- }
-
- // 2. If format is "jwk":
- else if (format == KeyFormat::Jwk) {
- // 1. If keyData is a JsonWebKey dictionary: Let jwk equal keyData.
- // Otherwise: Throw a DataError.
- if (!key_data.has<JsonWebKey>())
- return WebIDL::DataError::create("keyData is not a JsonWebKey dictionary"_utf16);
- auto& jwk = key_data.get<JsonWebKey>();
-
- // 2. If the d field is present and usages contains a value which is not "sign",
- // or, if the d field is not present and usages contains a value which is not "verify" then throw a SyntaxError.
- if (jwk.d.has_value()) {
- for (auto const& usage : usages) {
- if (usage != KeyUsage::Sign) {
- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));
- }
- }
- } else {
- for (auto const& usage : usages) {
- if (usage != KeyUsage::Verify) {
- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));
- }
- }
- }
-
- // 3. If the kty field of jwk is not "OKP", then throw a DataError.
- if (jwk.kty != "OKP"sv)
- return WebIDL::DataError::create("Invalid key type"_utf16);
-
- // 4. If the crv field of jwk is not "Ed25519", then throw a DataError.
- if (jwk.crv != "Ed25519"sv)
- return WebIDL::DataError::create("Invalid curve"_utf16);
-
- // 5. If the alg field of jwk is present and is not "Ed25519" or "EdDSA", then throw a DataError.
- if (jwk.alg.has_value() && !jwk.alg.value().is_one_of("Ed25519"sv, "EdDSA"sv))
- return WebIDL::DataError::create("Invalid algorithm"_utf16);
-
- // 6. If usages is non-empty and the use field of jwk is present and is not "sig", then throw a DataError.
- if (!usages.is_empty() && jwk.use.has_value() && jwk.use.value() != "sig"_utf16)
- return WebIDL::DataError::create("Invalid key usage"_utf16);
-
- // 7. If the key_ops field of jwk is present, and is invalid according to the requirements of JSON Web Key [JWK],
- // or it does not contain all of the specified usages values, then throw a DataError.
- TRY(validate_jwk_key_ops(realm, jwk, usages));
-
- // 8. If the ext field of jwk is present and has the value false and extractable is true, then throw a DataError.
- if (jwk.ext.has_value() && !jwk.ext.value() && extractable)
- return WebIDL::DataError::create("Invalid extractable"_utf16);
-
- // 9. If the d field is present:
- if (jwk.d.has_value()) {
- // 1. If jwk does not meet the requirements of the JWK private key format described in Section 2 of [RFC8037],
- // then throw a DataError.
- // o The parameter "kty" MUST be "OKP".
- if (jwk.kty != "OKP"sv)
- return WebIDL::DataError::create("Invalid key type"_utf16);
-
- // https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve
- // o The parameter "crv" MUST be present and contain the subtype of the key (from the "JSON Web Elliptic Curve" registry).
- if (jwk.crv != "Ed25519"sv)
- return WebIDL::DataError::create("Invalid curve"_utf16);
-
- // o The parameter "x" MUST be present and contain the public key encoded using the base64url [RFC4648] encoding.
- if (!jwk.x.has_value())
- return WebIDL::DataError::create("Missing x field"_utf16);
- auto public_key = TRY(base64_url_bytes_decode(realm, jwk.x.value()));
- if (public_key.size() != 32)
- return WebIDL::DataError::create("Invalid key length"_utf16);
-
- // o The parameter "d" MUST be present for private keys and contain the private key encoded using the base64url encoding.
- // This parameter MUST NOT be present for public keys.
- if (!jwk.d.has_value())
- return WebIDL::DataError::create("Present d field"_utf16);
- auto private_key = TRY(base64_url_bytes_decode(realm, jwk.d.value()));
- if (private_key.size() != 32)
- return WebIDL::DataError::create("Invalid key length"_utf16);
-
- ::Crypto::Curves::Ed25519 curve;
- auto derived_public_key = TRY_OR_THROW_OOM(realm.vm(), curve.generate_public_key(private_key));
- if (derived_public_key != public_key)
- return WebIDL::DataError::create("Invalid key pair"_utf16);
-
- // 2. Let key be a new CryptoKey object that represents the Ed25519 private key identified by interpreting jwk according to Section 2 of [RFC8037].
- key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPrivateKey { move(private_key) } });
-
- // 3. Set the [[type]] internal slot of Key to "private".
- key->set_type(KeyType::Private);
- }
- // Otherwise:
- else {
- // 1. If jwk does not meet the requirements of the JWK public key format described in Section 2 of [RFC8037], then throw a DataError.
- // o The parameter "kty" MUST be "OKP".
- if (jwk.kty != "OKP"sv)
- return WebIDL::DataError::create("Invalid key type"_utf16);
-
- // https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve
- // o The parameter "crv" MUST be present and contain the subtype of the key (from the "JSON Web Elliptic Curve" registry).
- if (jwk.crv != "Ed25519"sv)
- return WebIDL::DataError::create("Invalid curve"_utf16);
-
- // o The parameter "x" MUST be present and contain the public key encoded using the base64url [RFC4648] encoding.
- if (!jwk.x.has_value())
- return WebIDL::DataError::create("Missing x field"_utf16);
- auto public_key = TRY(base64_url_bytes_decode(realm, jwk.x.value()));
- if (public_key.size() != 32)
- return WebIDL::DataError::create("Invalid key length"_utf16);
-
- // o The parameter "d" MUST be present for private keys and contain the private key encoded using the base64url encoding.
- // This parameter MUST NOT be present for public keys.
- if (jwk.d.has_value())
- return WebIDL::DataError::create("Present d field"_utf16);
-
- // 2. Let key be a new CryptoKey object that represents the Ed25519 public key identified by interpreting jwk according to Section 2 of [RFC8037].
- key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPublicKey { move(public_key) } });
-
- // 3. Set the [[type]] internal slot of Key to "public".
- key->set_type(KeyType::Public);
- }
-
- // 10. Let algorithm be a new instance of a KeyAlgorithm object.
- // 11. Set the name attribute of algorithm to "Ed25519".
- // 12. Set the [[algorithm]] internal slot of key to algorithm.
- key->set_algorithm(CryptoKey::KeyAlgorithmData { "Ed25519"_utf16 });
- }
-
- // 2. If format is "raw":
- // https://wicg.github.io/webcrypto-modern-algos/#subtlecrypto-interface-keyformat
- // For all existing asymmetric algorithms in WebCrypto, "raw-public" acts as an alias of "raw".
- else if (format == KeyFormat::Raw || format == KeyFormat::RawPublic) {
- // 1. If usages contains an entry which is not "verify" then throw a SyntaxError.
- for (auto const& usage : usages) {
- if (usage != KeyUsage::Verify) {
- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));
- }
- }
-
- // 2. If the length in bits of keyData is not 256 then throw a DataError.
- auto& data = key_data.get<ByteBuffer>();
- if (data.size() * 8 != 256)
- return WebIDL::DataError::create("Invalid key length"_utf16);
-
- // 5. Let key be a new CryptoKey representing the key data provided in keyData.
- key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPublicKey { move(data) } });
-
- // 6. Set the [[type]] internal slot of key to "public"
- key->set_type(KeyType::Public);
-
- // 3. Let algorithm be a new KeyAlgorithm object.
- // 4. Set the name attribute of algorithm to "Ed25519".
- // 7. Set the [[algorithm]] internal slot of key to algorithm.
- key->set_algorithm(CryptoKey::KeyAlgorithmData { "Ed25519"_utf16 });
- }
-
- // 2. Otherwise:
- else {
- // throw a NotSupportedError.
- return WebIDL::NotSupportedError::create("Invalid key format"_utf16);
+ switch (curve) {
+ case OkpCurve::Ed25519:
+ return ::Crypto::ASN1::ed25519_oid;
+ case OkpCurve::Ed448:
+ return ::Crypto::ASN1::ed448_oid;
+ case OkpCurve::X25519:
+ return ::Crypto::ASN1::x25519_oid;
+ case OkpCurve::X448:
+ return ::Crypto::ASN1::x448_oid;
}
+ VERIFY_NOT_REACHED();
+}
- // 3. Return key.
- return GC::Ref { *key };
+extern "C" {
+bool web_crypto_okp_generate_private_key(OkpCurve, void*, ReceiveEncodedBytes);
+bool web_crypto_okp_generate_public_key(OkpCurve, ReadonlyBytes, void*, ReceiveEncodedBytes);
+bool web_crypto_okp_compute_coordinate(OkpCurve, ReadonlyBytes, ReadonlyBytes, void*, ReceiveEncodedBytes);
+bool web_crypto_okp_sign(OkpCurve, ReadonlyBytes, ReadonlyBytes, ReadonlyBytes, void*, ReceiveEncodedBytes);
+FlatPtr web_crypto_okp_verify(OkpCurve, ReadonlyBytes, ReadonlyBytes, ReadonlyBytes, ReadonlyBytes, bool*);
+bool web_crypto_okp_key_info_has_oid(OkpCurve, Asn1Structure, void const*);
+void web_crypto_asn1_key_info_raw_key(Asn1Structure, void const*, ReadonlyBytes*);
+void web_crypto_asn1_octet_string_bytes(StringView const*, ReadonlyBytes*);
+bool web_crypto_okp_export_der(OkpCurve, bool, ReadonlyBytes, void*, ReceiveEncodedBytes);
}
-// https://w3c.github.io/webcrypto/#ed25519-operations-export-key
-WebIDL::ExceptionOr<ExportKeyResult> ED25519::export_key(JS::Realm& realm, KeyFormat format, GC::Ref<CryptoKey> key)
+bool web_crypto_okp_generate_private_key(OkpCurve curve, void* state, ReceiveEncodedBytes receive)
{
- auto& vm = realm.vm();
-
- // 1. Let key be the CryptoKey to be exported.
-
- // 2. If the underlying cryptographic key material represented by the [[handle]] internal slot of key cannot be accessed, then throw an OperationError.
- // Note: In our impl this is always accessible
- auto const& handle = key->handle();
-
- // 3. If format is "spki":
- if (format == KeyFormat::Spki) {
- // 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
- if (key->type() != KeyType::Public)
- return WebIDL::InvalidAccessError::create("Key is not a public key"_utf16);
-
- // 2. Let data be an instance of the subjectPublicKeyInfo ASN.1 structure defined in [RFC5280] with the following properties:
- // * Set the algorithm field to an AlgorithmIdentifier ASN.1 type with the following properties:
- // * Set the algorithm object identifier to the id-Ed25519 OID defined in [RFC8410].
- // * Set the subjectPublicKey field to keyData.
- auto ed25519_oid = ::Crypto::ASN1::ed25519_oid;
- auto data = TRY_OR_THROW_OOM(vm, ::Crypto::PK::wrap_in_subject_public_key_info(handle.get<OKPPublicKey>().bytes, ed25519_oid));
-
- // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
- return ExportKeyResult { move(data) };
- }
-
- // 3. If format is "pkcs8":
- if (format == KeyFormat::Pkcs8) {
- // 1. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.
- if (key->type() != KeyType::Private)
- return WebIDL::InvalidAccessError::create("Key is not a private key"_utf16);
-
- // 2. Let data be an instance of the privateKeyInfo ASN.1 structure defined in [RFC5208] with the following properties:
- // * Set the version field to 0.
- // * Set the privateKeyAlgorithm field to a PrivateKeyAlgorithmIdentifier ASN.1 type with the following properties:
- // * Set the algorithm object identifier to the id-Ed25519 OID defined in [RFC8410].
- // * Set the privateKey field to the result of DER-encoding a CurvePrivateKey ASN.1 type,
- // as defined in Section 7 of [RFC8410], that represents the Ed25519 private key
- // represented by the [[handle]] internal slot of key
- ::Crypto::ASN1::Encoder encoder;
- TRY_OR_THROW_OOM(vm, encoder.write(handle.get<OKPPrivateKey>().bytes.bytes()));
-
- auto ed25519_oid = ::Crypto::ASN1::ed25519_oid;
- auto data = TRY_OR_THROW_OOM(vm, ::Crypto::PK::wrap_in_private_key_info(encoder.finish(), ed25519_oid));
-
- // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
- return ExportKeyResult { move(data) };
- }
-
- // 2. If format is "jwk":
- if (format == KeyFormat::Jwk) {
- // 1. Let jwk be a new JsonWebKey dictionary.
- JsonWebKey jwk;
-
- // 2. Set the kty attribute of jwk to "OKP".
- jwk.kty = "OKP"_utf16;
-
- // 3. Set the alg attribute of jwk to "Ed25519".
- jwk.alg = "Ed25519"_utf16;
-
- // 4. Set the crv attribute of jwk to "Ed25519".
- jwk.crv = "Ed25519"_utf16;
-
- // 5. Set the x attribute of jwk according to the definition in Section 2 of [RFC8037].
- if (key->type() == KeyType::Public) {
- jwk.x = Utf16String::from_utf8(TRY_OR_THROW_OOM(vm, encode_base64url(handle.get<OKPPublicKey>().bytes, AK::OmitPadding::Yes)));
- } else {
- // The "x" parameter of the "epk" field is set as follows:
- // Apply the appropriate ECDH function to the ephemeral private key (as scalar input)
- // and the standard base point (as u-coordinate input).
- // The base64url encoding of the output is the value for the "x" parameter of the "epk" field.
- ::Crypto::Curves::Ed25519 curve;
- auto public_key = TRY_OR_THROW_OOM(vm, curve.generate_public_key(handle.get<OKPPrivateKey>().bytes));
- jwk.x = TRY_OR_THROW_OOM(vm, base64_url_bytes_encode(public_key));
- }
-
- // 6. If the [[type]] internal slot of key is "private"
- if (key->type() == KeyType::Private) {
- // 1. Set the d attribute of jwk according to the definition in Section 2 of [RFC8037].
- jwk.d = TRY_OR_THROW_OOM(vm, base64_url_bytes_encode(handle.get<OKPPrivateKey>().bytes));
- }
-
- // 7. Set the key_ops attribute of jwk to the usages attribute of key.
- jwk.key_ops = Vector<Utf16String> {};
- jwk.key_ops->ensure_capacity(key->internal_usages().size());
- for (auto const& usage : key->internal_usages())
- jwk.key_ops->unchecked_append(Utf16String::from_utf8(key_usage_to_string(usage)));
-
- // 8. Set the ext attribute of jwk to the [[extractable]] internal slot of key.
- jwk.ext = key->extractable();
-
- // 9. Let result be the result of converting jwk to an ECMAScript Object, as defined by [WebIDL].
- return ExportKeyResult { move(jwk) };
- }
-
- // 2. If format is "raw":
- // https://wicg.github.io/webcrypto-modern-algos/#subtlecrypto-interface-keyformat
- // For all existing asymmetric algorithms in WebCrypto, "raw-public" acts as an alias of "raw".
- if (format == KeyFormat::Raw || format == KeyFormat::RawPublic) {
- // 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
- if (key->type() != KeyType::Public)
- return WebIDL::InvalidAccessError::create("Key is not a public key"_utf16);
-
- // 2. Let data be an octet string representing the Ed25519 public key represented by the [[handle]] internal slot of key.
- // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
- return ExportKeyResult { handle.get<OKPPublicKey>().bytes };
- }
-
- // 2. Otherwise:
- // throw a NotSupportedError.
- return WebIDL::NotSupportedError::create("Invalid key format"_utf16);
+ auto result = okp_curve(curve).generate_private_key();
+ if (result.is_error())
+ return false;
+ receive(state, result.value());
+ return true;
}
-Optional<ReadonlySpan<KeyUsage>> ED25519::public_key_usages() const
+bool web_crypto_okp_generate_public_key(OkpCurve curve, ReadonlyBytes private_key, void* state, ReceiveEncodedBytes receive)
{
- static constexpr Array usages { KeyUsage::Verify };
- return usages.span();
+ auto result = okp_curve(curve).generate_public_key(private_key);
+ if (result.is_error())
+ return false;
+ receive(state, result.value());
+ return true;
}
-// https://w3c.github.io/webcrypto/#ed25519-operations-sign
-WebIDL::ExceptionOr<ByteBuffer> ED25519::sign(JS::Realm& realm, [[maybe_unused]] AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& message)
+bool web_crypto_okp_compute_coordinate(OkpCurve curve, ReadonlyBytes private_key, ReadonlyBytes public_key, void* state, ReceiveEncodedBytes receive)
{
- auto& vm = realm.vm();
-
- // 1. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.
- if (key->type() != KeyType::Private)
- return WebIDL::InvalidAccessError::create("Key is not a private key"_utf16);
-
- // 2. Perform the Ed25519 signing process, as specified in [RFC8032], Section 5.1.6,
- // with message as M, using the Ed25519 private key associated with key.
- auto const& private_key = key->handle().get<OKPPrivateKey>().bytes;
-
- ::Crypto::Curves::Ed25519 curve;
- auto maybe_public_key = curve.generate_public_key(private_key);
- if (maybe_public_key.is_error())
- return WebIDL::OperationError::create("Failed to generate public key"_utf16);
- auto public_key = maybe_public_key.release_value();
-
- auto maybe_signature = curve.sign(private_key, message);
- if (maybe_signature.is_error())
- return WebIDL::OperationError::create("Failed to sign message"_utf16);
- auto signature = maybe_signature.release_value();
-
- // 3. Return a new ArrayBuffer associated with the relevant global object of this [HTML],
- // and containing the bytes of the signature resulting from performing the Ed25519 signing process.
- auto result = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(signature));
- return result;
+ VERIFY(curve == OkpCurve::X25519 || curve == OkpCurve::X448);
+ auto result = curve == OkpCurve::X25519
+ ? ::Crypto::Curves::X25519 {}.compute_coordinate(private_key, public_key)
+ : ::Crypto::Curves::X448 {}.compute_coordinate(private_key, public_key);
+ if (result.is_error())
+ return false;
+ receive(state, result.value());
+ return true;
}
-// https://w3c.github.io/webcrypto/#ed25519-operations-verify
-WebIDL::ExceptionOr<bool> ED25519::verify(JS::Realm&, [[maybe_unused]] AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& signature, ByteBuffer const& message)
+bool web_crypto_okp_sign(OkpCurve curve, ReadonlyBytes private_key, ReadonlyBytes message, ReadonlyBytes context, void* state, ReceiveEncodedBytes receive)
{
- // 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
- if (key->type() != KeyType::Public)
- return WebIDL::InvalidAccessError::create("Key is not a public key"_utf16);
-
- // NOTE: this is checked by ED25519::verify().
- // 2. If the key data of key represents an invalid point or a small-order element on the Elliptic Curve of Ed25519, return false.
- // 3. If the point R, encoded in the first half of signature, represents an invalid point or a small-order element on the Elliptic Curve of Ed25519, return false.
-
- // 4. Perform the Ed25519 verification steps, as specified in [RFC8032], Section 5.1.7,
- // using the cofactorless (unbatched) equation, [S]B = R + [k]A', on the signature,
- // with message as M, using the Ed25519 public key associated with key.
+ VERIFY(curve == OkpCurve::Ed25519 || curve == OkpCurve::Ed448);
+ auto result = curve == OkpCurve::Ed25519
+ ? ::Crypto::Curves::Ed25519 {}.sign(private_key, message)
+ : ::Crypto::Curves::Ed448 {}.sign(private_key, message, context);
+ if (result.is_error())
+ return false;
+ receive(state, result.value());
+ return true;
+}
- // 9. Let result be a boolean with the value true if the signature is valid and the value false otherwise.
- ::Crypto::Curves::Ed25519 curve;
- auto maybe_verified = curve.verify(key->handle().get<OKPPublicKey>().bytes, signature, message);
- if (maybe_verified.is_error())
- return WebIDL::OperationError::create(Utf16String::from_utf8(maybe_verified.error().string_literal()));
+FlatPtr web_crypto_okp_verify(OkpCurve curve, ReadonlyBytes public_key, ReadonlyBytes signature, ReadonlyBytes message, ReadonlyBytes context, bool* verified)
+{
+ VERIFY(curve == OkpCurve::Ed25519 || curve == OkpCurve::Ed448);
+ auto result = curve == OkpCurve::Ed25519
+ ? ::Crypto::Curves::Ed25519 {}.verify(public_key, signature, message)
+ : ::Crypto::Curves::Ed448 {}.verify(public_key, signature, message, context);
+ if (result.is_error())
+ return Utf16String::from_utf8(result.error().string_literal()).into_raw();
+ *verified = result.value();
+ return 0;
+}
- // 10. Return result.
- return maybe_verified.release_value();
+bool web_crypto_okp_key_info_has_oid(OkpCurve curve, Asn1Structure structure, void const* key_info)
+{
+ return key_info_algorithm(structure, key_info).identifier == okp_curve_oid(curve);
}
-// https://wicg.github.io/webcrypto-secure-curves/#ed448-operations
-WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> ED448::generate_key(JS::Realm&, [[maybe_unused]] AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages)
+void web_crypto_asn1_key_info_raw_key(Asn1Structure structure, void const* key_info, ReadonlyBytes* bytes)
{
- // 1. If usages contains a value which is not one of "sign" or "verify", then throw a SyntaxError.
- for (auto const& usage : key_usages) {
- if (usage != KeyUsage::Sign && usage != KeyUsage::Verify) {
- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));
- }
+ if (structure == Asn1Structure::SubjectPublicKeyInfo) {
+ *bytes = static_cast<::Crypto::Certificate::SubjectPublicKey const*>(key_info)->raw_key;
+ return;
}
-
- // 2. Generate an Ed448 key pair, as defined in [RFC8032], section 5.1.5.
- ::Crypto::Curves::Ed448 curve;
- auto maybe_private_key = curve.generate_private_key();
- if (maybe_private_key.is_error())
- return WebIDL::OperationError::create("Failed to generate private key"_utf16);
- auto private_key_data = maybe_private_key.release_value();
-
- auto maybe_public_key = curve.generate_public_key(private_key_data);
- if (maybe_public_key.is_error())
- return WebIDL::OperationError::create("Failed to generate public key"_utf16);
- auto public_key_data = maybe_public_key.release_value();
-
- // 5. Let publicKey be a new CryptoKey associated with the relevant global object of this [HTML],
- // and representing the public key of the generated key pair.
- auto public_key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPublicKey { move(public_key_data) } });
-
- // 6. Set the [[type]] internal slot of publicKey to "public"
- public_key->set_type(KeyType::Public);
-
- // 3. Let algorithm be a new KeyAlgorithm object.
- // 4. Set the name attribute of algorithm to "Ed448".
- // 7. Set the [[algorithm]] internal slot of publicKey to algorithm.
- public_key->set_algorithm(CryptoKey::KeyAlgorithmData { "Ed448"_utf16 });
-
- // 8. Set the [[extractable]] internal slot of publicKey to true.
- public_key->set_extractable(true);
-
- // 9. Set the [[usages]] internal slot of publicKey to be the usage intersection of usages and [ "verify" ].
- public_key->set_usages(usage_intersection(key_usages, { { KeyUsage::Verify } }));
-
- // 10. Let privateKey be a new CryptoKey associated with the relevant global object of this [HTML],
- // and representing the private key of the generated key pair.
- auto private_key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPrivateKey { move(private_key_data) } });
-
- // 11. Set the [[type]] internal slot of privateKey to "private"
- private_key->set_type(KeyType::Private);
-
- // 12. Set the [[algorithm]] internal slot of privateKey to algorithm.
- private_key->set_algorithm(CryptoKey::KeyAlgorithmData { "Ed448"_utf16 });
-
- // 13. Set the [[extractable]] internal slot of privateKey to extractable.
- private_key->set_extractable(extractable);
-
- // 14. Set the [[usages]] internal slot of privateKey to be the usage intersection of usages and [ "sign" ].
- private_key->set_usages(usage_intersection(key_usages, { { KeyUsage::Sign } }));
-
- // 15. Let result be a new CryptoKeyPair dictionary.
- // 16. Set the publicKey attribute of result to be publicKey.
- // 17. Set the privateKey attribute of result to be privateKey.
- // 18. Return the result of converting result to an ECMAScript Object, as defined by [WebIDL].
- return Variant<GC::Ref<CryptoKey>, CryptoKeyPair> { CryptoKeyPair { public_key, private_key } };
+ VERIFY(structure == Asn1Structure::PrivateKeyInfo);
+ *bytes = static_cast<::Crypto::Certificate::PrivateKey const*>(key_info)->raw_key;
}
-// https://wicg.github.io/webcrypto-secure-curves/#ed448-operations
-WebIDL::ExceptionOr<GC::Ref<CryptoKey>> ED448::import_key(JS::Realm& realm,
- [[maybe_unused]] Web::Crypto::AlgorithmParams const& params,
- KeyFormat format,
- CryptoKey::ImportKeyData key_data,
- bool extractable,
- Vector<KeyUsage> const& usages)
+void web_crypto_asn1_octet_string_bytes(StringView const* string, ReadonlyBytes* bytes)
{
- GC::Ptr<CryptoKey> key = nullptr;
-
- // 1. Let keyData be the key data to be imported.
+ *bytes = string->bytes();
+}
- // 2. If format is "spki":
- if (format == KeyFormat::Spki) {
- // 1. If usages contains a value which is not "verify" then throw a SyntaxError.
- for (auto const& usage : usages) {
- if (usage != KeyUsage::Verify) {
- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));
- }
- }
-
- // 2. Let spki be the result of running the parse a subjectPublicKeyInfo algorithm over keyData.
- // 3. If an error occurred while parsing, then throw a DataError.
- auto spki = TRY(parse_a_subject_public_key_info(realm, key_data.get<ByteBuffer>()));
-
- // 4. If the algorithm object identifier field of the algorithm AlgorithmIdentifier field of spki
- // is not equal to the id-Ed448 object identifier defined in [RFC8410], then throw a DataError.
- if (spki.algorithm.identifier != ::Crypto::ASN1::ed448_oid)
- return WebIDL::DataError::create("Invalid algorithm identifier"_utf16);
-
- // 5. If the parameters field of the algorithm AlgorithmIdentifier field of spki is present, then throw a DataError.
- if (spki.algorithm.ec_parameters.has_value())
- return WebIDL::DataError::create("Invalid algorithm parameters"_utf16);
-
- // 6. Let publicKey be the Ed448 public key identified by the subjectPublicKey field of spki.
- auto const& public_key = spki.raw_key;
-
- // 7. Let key be a new CryptoKey associated with the relevant global object of this [HTML],
- // and that represents publicKey.
- key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPublicKey { move(public_key) } });
-
- // 8. Set the [[type]] internal slot of key to "public"
- key->set_type(KeyType::Public);
-
- // 9. Let algorithm be a new KeyAlgorithm.
- // 10. Set the name attribute of algorithm to "Ed448".
- // 11. Set the [[algorithm]] internal slot of key to algorithm.
- key->set_algorithm(CryptoKey::KeyAlgorithmData { "Ed448"_utf16 });
- }
-
- // 2. If format is "pkcs8":
- else if (format == KeyFormat::Pkcs8) {
- // 1. If usages contains a value which is not "sign" then throw a SyntaxError.
- for (auto const& usage : usages) {
- if (usage != KeyUsage::Sign) {
- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));
- }
- }
-
- // 2. Let privateKeyInfo be the result of running the parse a privateKeyInfo algorithm over keyData.
- // 3. If an error occurs while parsing, then throw a DataError.
- auto private_key_info = TRY(parse_a_private_key_info(realm, key_data.get<ByteBuffer>()));
-
- // 4. If the algorithm object identifier field of the privateKeyAlgorithm PrivateKeyAlgorithm field
- // of privateKeyInfo is not equal to the id-Ed448 object identifier defined in [RFC8410], then throw a DataError.
- if (private_key_info.algorithm.identifier != ::Crypto::ASN1::ed448_oid)
- return WebIDL::DataError::create("Invalid algorithm identifier"_utf16);
-
- // 5. If the parameters field of the privateKeyAlgorithm PrivateKeyAlgorithmIdentifier field of privateKeyInfo is present,
- // then throw a DataError.
- if (private_key_info.algorithm.ec_parameters.has_value())
- return WebIDL::DataError::create("Invalid algorithm parameters"_utf16);
-
- // 6. Let curvePrivateKey be the result of performing the parse an ASN.1 structure algorithm,
- // with data as the privateKey field of privateKeyInfo, structure as the ASN.1 CurvePrivateKey structure
- // specified in Section 7 of [RFC8410], and exactData set to true.
- // 7. If an error occurred while parsing, then throw a DataError.
- auto curve_private_key = TRY(parse_an_ASN1_structure<StringView>(realm, private_key_info.raw_key, true));
- auto curve_private_key_bytes = TRY_OR_THROW_OOM(realm.vm(), ByteBuffer::copy(curve_private_key.bytes()));
-
- // 8. Let key be a new CryptoKey associated with the relevant global object of this [HTML],
- // and that represents the Ed448 private key identified by curvePrivateKey.
- key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPrivateKey { move(curve_private_key_bytes) } });
-
- // 9. Set the [[type]] internal slot of key to "private"
- key->set_type(KeyType::Private);
-
- // 10. Let algorithm be a new KeyAlgorithm.
- // 11. Set the name attribute of algorithm to "Ed448".
- // 12. Set the [[algorithm]] internal slot of key to algorithm.
- key->set_algorithm(CryptoKey::KeyAlgorithmData { "Ed448"_utf16 });
- }
-
- // 2. If format is "jwk":
- else if (format == KeyFormat::Jwk) {
- // 1. If keyData is a JsonWebKey dictionary: Let jwk equal keyData.
- // Otherwise: Throw a DataError.
- if (!key_data.has<JsonWebKey>())
- return WebIDL::DataError::create("keyData is not a JsonWebKey dictionary"_utf16);
- auto& jwk = key_data.get<JsonWebKey>();
-
- // 2. If the d field is present and usages contains a value which is not "sign",
- // or, if the d field is not present and usages contains a value which is not "verify" then throw a SyntaxError.
- if (jwk.d.has_value()) {
- for (auto const& usage : usages) {
- if (usage != KeyUsage::Sign) {
- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));
- }
- }
- } else {
- for (auto const& usage : usages) {
- if (usage != KeyUsage::Verify) {
- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));
- }
- }
- }
-
- // 3. If the kty field of jwk is not "OKP", then throw a DataError.
- if (jwk.kty != "OKP"sv)
- return WebIDL::DataError::create("Invalid key type"_utf16);
-
- // 4. If the crv field of jwk is not "Ed448", then throw a DataError.
- if (jwk.crv != "Ed448"sv)
- return WebIDL::DataError::create("Invalid curve"_utf16);
-
- // 5. If the alg field of jwk is present and is not "Ed448" or "EdDSA", then throw a DataError.
- if (jwk.alg.has_value() && (jwk.alg.value() != "Ed448"sv && jwk.alg.value() != "EdDSA"sv))
- return WebIDL::DataError::create("Invalid algorithm"_utf16);
-
- // 6. If usages is non-empty and the use field of jwk is present and is not "sig", then throw a DataError.
- if (!usages.is_empty() && jwk.use.has_value() && jwk.use.value() != "sig"_utf16)
- return WebIDL::DataError::create("Invalid key usage"_utf16);
-
- // 7. If the key_ops field of jwk is present, and is invalid according to the requirements of JSON Web Key [JWK],
- // or it does not contain all of the specified usages values, then throw a DataError.
- TRY(validate_jwk_key_ops(realm, jwk, usages));
-
- // 8. If the ext field of jwk is present and has the value false and extractable is true, then throw a DataError.
- if (jwk.ext.has_value() && !jwk.ext.value() && extractable)
- return WebIDL::DataError::create("Invalid extractable"_utf16);
-
- // 9. If the d field is present:
- if (jwk.d.has_value()) {
- // 1. If jwk does not meet the requirements of the JWK private key format described in Section 2 of [RFC8037],
- // then throw a DataError.
- // o The parameter "kty" MUST be "OKP".
- if (jwk.kty != "OKP"sv)
- return WebIDL::DataError::create("Invalid key type"_utf16);
-
- // https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve
- // o The parameter "crv" MUST be present and contain the subtype of the key (from the "JSON Web Elliptic Curve" registry).
- if (jwk.crv != "Ed448"sv)
- return WebIDL::DataError::create("Invalid curve"_utf16);
-
- // o The parameter "x" MUST be present and contain the public key encoded using the base64url [RFC4648] encoding.
- if (!jwk.x.has_value())
- return WebIDL::DataError::create("Missing x field"_utf16);
- auto public_key = TRY(base64_url_bytes_decode(realm, jwk.x.value()));
- if (public_key.size() != 57)
- return WebIDL::DataError::create("Invalid key length"_utf16);
-
- // o The parameter "d" MUST be present for private keys and contain the private key encoded using the base64url encoding.
- // This parameter MUST NOT be present for public keys.
- if (!jwk.d.has_value())
- return WebIDL::DataError::create("Present d field"_utf16);
- auto private_key = TRY(base64_url_bytes_decode(realm, jwk.d.value()));
- if (private_key.size() != 57)
- return WebIDL::DataError::create("Invalid key length"_utf16);
-
- ::Crypto::Curves::Ed448 curve;
- auto derived_public_key = TRY_OR_THROW_OOM(realm.vm(), curve.generate_public_key(private_key));
- if (derived_public_key != public_key)
- return WebIDL::DataError::create("Invalid key pair"_utf16);
-
- // 2. Let key be a new CryptoKey object that represents the Ed448 private key identified by interpreting jwk according to Section 2 of [RFC8037].
- key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPrivateKey { move(private_key) } });
-
- // 3. Set the [[type]] internal slot of Key to "private".
- key->set_type(KeyType::Private);
- }
- // Otherwise:
- else {
- // 1. If jwk does not meet the requirements of the JWK public key format described in Section 2 of [RFC8037], then throw a DataError.
- // o The parameter "kty" MUST be "OKP".
- if (jwk.kty != "OKP"sv)
- return WebIDL::DataError::create("Invalid key type"_utf16);
-
- // https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve
- // o The parameter "crv" MUST be present and contain the subtype of the key (from the "JSON Web Elliptic Curve" registry).
- if (jwk.crv != "Ed448"sv)
- return WebIDL::DataError::create("Invalid curve"_utf16);
-
- // o The parameter "x" MUST be present and contain the public key encoded using the base64url [RFC4648] encoding.
- if (!jwk.x.has_value())
- return WebIDL::DataError::create("Missing x field"_utf16);
- auto public_key = TRY(base64_url_bytes_decode(realm, jwk.x.value()));
- if (public_key.size() != 57)
- return WebIDL::DataError::create("Invalid key length"_utf16);
-
- // o The parameter "d" MUST be present for private keys and contain the private key encoded using the base64url encoding.
- // This parameter MUST NOT be present for public keys.
- if (jwk.d.has_value())
- return WebIDL::DataError::create("Present d field"_utf16);
-
- // 2. Let key be a new CryptoKey object that represents the Ed448 public key identified by interpreting jwk according to Section 2 of [RFC8037].
- key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPublicKey { move(public_key) } });
-
- // 3. Set the [[type]] internal slot of Key to "public".
- key->set_type(KeyType::Public);
- }
-
- // 10. Let algorithm be a new instance of a KeyAlgorithm object.
- // 11. Set the name attribute of algorithm to "Ed448".
- // 12. Set the [[algorithm]] internal slot of key to algorithm.
- key->set_algorithm(CryptoKey::KeyAlgorithmData { "Ed448"_utf16 });
- }
-
- // 2. If format is "raw":
- else if (format == KeyFormat::Raw) {
- // 1. If usages contains a value which is not "verify" then throw a SyntaxError.
- for (auto const& usage : usages) {
- if (usage != KeyUsage::Verify) {
- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));
- }
- }
-
- // 2. Let data be keyData.
- auto data = move(key_data.get<ByteBuffer>());
-
- // 3. If the length in bits of data is not 448 then throw a DataError.
- // AD-HOC: The spec has a typo with the size of the key length
- // See spec comment: https://github.com/w3c/webcrypto/pull/425#discussion_r3070135408
- if (data.size() * 8 != 456)
- return WebIDL::DataError::create("Invalid key length"_utf16);
-
- // 6. Let key be a new CryptoKey associated with the relevant global object of this [HTML], and that represents data.
- key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPublicKey { move(data) } });
-
- // 7. Set the [[type]] internal slot of key to "public"
- key->set_type(KeyType::Public);
-
- // 4. Let algorithm be a new KeyAlgorithm object.
- // 5. Set the name attribute of algorithm to "Ed448".
- // 8. Set the [[algorithm]] internal slot of key to algorithm.
- key->set_algorithm(CryptoKey::KeyAlgorithmData { "Ed448"_utf16 });
- }
-
- // 2. Otherwise:
- else {
- // throw a NotSupportedError.
- return WebIDL::NotSupportedError::create("Invalid key format"_utf16);
- }
-
- return GC::Ref { *key };
-}
-
-// https://wicg.github.io/webcrypto-secure-curves/#ed448-operations
-WebIDL::ExceptionOr<ExportKeyResult> ED448::export_key(JS::Realm& realm, KeyFormat format, GC::Ref<CryptoKey> key)
-{
- auto& vm = realm.vm();
-
- // 1. Let key be the CryptoKey to be exported.
-
- // 2. If the underlying cryptographic key material represented by the [[handle]] internal slot of key cannot be accessed, then throw an OperationError.
- // Note: In our impl this is always accessible
- auto const& handle = key->handle();
-
- // 3. If format is "spki":
- if (format == KeyFormat::Spki) {
- // 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
- if (key->type() != KeyType::Public)
- return WebIDL::InvalidAccessError::create("Key is not a public key"_utf16);
-
- // 2. Let data be an instance of the subjectPublicKeyInfo ASN.1 structure defined in [RFC5280] with the following properties:
- // * Set the algorithm field to an AlgorithmIdentifier ASN.1 type with the following properties:
- // * Set the algorithm object identifier to the id-Ed448 OID defined in [RFC8410].
- // * Set the subjectPublicKey field to keyData.
- auto data = TRY_OR_THROW_OOM(vm, ::Crypto::PK::wrap_in_subject_public_key_info(handle.get<OKPPublicKey>().bytes, ::Crypto::ASN1::ed448_oid));
-
- // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
- return ExportKeyResult { move(data) };
- }
-
- // 3. If format is "pkcs8":
- if (format == KeyFormat::Pkcs8) {
- // 1. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.
- if (key->type() != KeyType::Private)
- return WebIDL::InvalidAccessError::create("Key is not a private key"_utf16);
-
- // 2. Let data be an instance of the privateKeyInfo ASN.1 structure defined in [RFC5208] with the following properties:
- // * Set the version field to 0.
- // * Set the privateKeyAlgorithm field to a PrivateKeyAlgorithmIdentifier ASN.1 type with the following properties:
- // * Set the algorithm object identifier to the id-Ed448 OID defined in [RFC8410].
- // * Set the privateKey field to the result of DER-encoding a CurvePrivateKey ASN.1 type,
- // as defined in Section 7 of [RFC8410], that represents the Ed448 private key
- // represented by the [[handle]] internal slot of key
- ::Crypto::ASN1::Encoder encoder;
- TRY_OR_THROW_OOM(vm, encoder.write(handle.get<OKPPrivateKey>().bytes.bytes()));
-
- auto data = TRY_OR_THROW_OOM(vm, ::Crypto::PK::wrap_in_private_key_info(encoder.finish(), ::Crypto::ASN1::ed448_oid));
-
- // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
- return ExportKeyResult { move(data) };
- }
-
- // 2. If format is "jwk":
- if (format == KeyFormat::Jwk) {
- // 1. Let jwk be a new JsonWebKey dictionary.
- JsonWebKey jwk;
-
- // 2. Set the kty attribute of jwk to "OKP".
- jwk.kty = "OKP"_utf16;
-
- // 3. Set the alg attribute of jwk to "Ed448".
- jwk.alg = "Ed448"_utf16;
-
- // 4. Set the crv attribute of jwk to "Ed448".
- jwk.crv = "Ed448"_utf16;
-
- // 5. Set the x attribute of jwk according to the definition in Section 2 of [RFC8037].
- if (key->type() == KeyType::Public) {
- jwk.x = Utf16String::from_utf8(TRY_OR_THROW_OOM(vm, encode_base64url(handle.get<OKPPublicKey>().bytes, AK::OmitPadding::Yes)));
- } else {
- // The "x" parameter of the "epk" field is set as follows:
- // Apply the appropriate ECDH function to the ephemeral private key (as scalar input)
- // and the standard base point (as u-coordinate input).
- // The base64url encoding of the output is the value for the "x" parameter of the "epk" field.
- ::Crypto::Curves::Ed448 curve;
- auto public_key = TRY_OR_THROW_OOM(vm, curve.generate_public_key(handle.get<OKPPrivateKey>().bytes));
- jwk.x = TRY_OR_THROW_OOM(vm, base64_url_bytes_encode(public_key));
- }
-
- // 6. If the [[type]] internal slot of key is "private"
- if (key->type() == KeyType::Private) {
- // 1. Set the d attribute of jwk according to the definition in Section 2 of [RFC8037].
- jwk.d = TRY_OR_THROW_OOM(vm, base64_url_bytes_encode(handle.get<OKPPrivateKey>().bytes));
- }
-
- // 7. Set the key_ops attribute of jwk to the usages attribute of key.
- jwk.key_ops = Vector<Utf16String> {};
- jwk.key_ops->ensure_capacity(key->internal_usages().size());
- for (auto const& usage : key->internal_usages())
- jwk.key_ops->unchecked_append(Utf16String::from_utf8(key_usage_to_string(usage)));
-
- // 8. Set the ext attribute of jwk to the [[extractable]] internal slot of key.
- jwk.ext = key->extractable();
-
- // 9. Let result be the result of converting jwk to an ECMAScript Object, as defined by [WebIDL].
- return ExportKeyResult { move(jwk) };
- }
-
- // 2. If format is "raw":
- if (format == KeyFormat::Raw) {
- // 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
- if (key->type() != KeyType::Public)
- return WebIDL::InvalidAccessError::create("Key is not a public key"_utf16);
-
- // 2. Let data be an octet string representing the Ed448 public key represented by the [[handle]] internal slot of key.
- // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
- return ExportKeyResult { handle.get<OKPPublicKey>().bytes };
- }
-
- // 2. Otherwise:
- // throw a NotSupportedError.
- return WebIDL::NotSupportedError::create("Invalid key format"_utf16);
-}
-
-Optional<ReadonlySpan<KeyUsage>> ED448::public_key_usages() const
-{
- static constexpr Array usages { KeyUsage::Verify };
- return usages.span();
-}
-
-// https://wicg.github.io/webcrypto-secure-curves/#ed448-operations
-WebIDL::ExceptionOr<ByteBuffer> ED448::sign(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& message)
-{
- // 1. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.
- if (key->type() != KeyType::Private)
- return WebIDL::InvalidAccessError::create("Key is not a private key"_utf16);
-
- // 2. Let context be the contents of the context member of normalizedAlgorithm
- // or the empty octet string if the context member of normalizedAlgorithm is not present.
- auto const& algorithm = static_cast<Ed448Params const&>(params);
- auto context = algorithm.context.value_or(ByteBuffer());
-
- // 3. If context has a length greater than 255 bytes, then throw an OperationError.
- if (context.size() > 255)
- return WebIDL::OperationError::create("Context is too long"_utf16);
-
- // 4. Perform the Ed448 signing process, as specified in [RFC8032], Section 5.2.6,
- // with message as M and context as C, using the Ed448 private key associated with key.
- ::Crypto::Curves::Ed448 curve;
- auto maybe_signature = curve.sign(key->handle().get<OKPPrivateKey>().bytes, message, context);
- if (maybe_signature.is_error()) {
- return WebIDL::OperationError::create("Failed to sign message"_utf16);
- }
-
- // 5. Return a new ArrayBuffer associated with the relevant global object of this [HTML],
- // and containing the bytes of the signature resulting from performing the Ed448 signing process.
- return maybe_signature.release_value();
-}
-
-// https://wicg.github.io/webcrypto-secure-curves/#ed448-operations
-WebIDL::ExceptionOr<bool> ED448::verify(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& signature, ByteBuffer const& message)
-{
- // 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
- if (key->type() != KeyType::Public)
- return WebIDL::InvalidAccessError::create("Key is not a public key"_utf16);
-
- // 2. Let context be the contents of the context member of normalizedAlgorithm
- // or the empty octet string if the context member of normalizedAlgorithm is not present.
- auto const& algorithm = static_cast<Ed448Params const&>(params);
- auto context = algorithm.context.value_or(ByteBuffer());
-
- // 3. If context has a length greater than 255 bytes, then throw an OperationError.
- if (context.size() > 255)
- return WebIDL::OperationError::create("Context is too long"_utf16);
-
- // 4. If the key data of key represents an invalid point or a small-order element
- // on the Elliptic Curve of Ed448, return false.
- // NOTE: https://github.com/WICG/webcrypto-secure-curves/issues/27
-
- // 5. If the point R, encoded in the first half of signature, represents an invalid point
- // or a small-order element on the Elliptic Curve of Ed448, return false.
- // NOTE: https://github.com/WICG/webcrypto-secure-curves/issues/27
-
- // 6. Perform the Ed448 verification steps, as specified in [RFC8032], Section 5.2.7, using
- // the cofactorless (unbatched) equation, [S]B = R + [k]A', on the signature,
- // with message as M and context as C, using the Ed448 public key associated with key.
- ::Crypto::Curves::Ed448 curve;
- auto maybe_verified = curve.verify(key->handle().get<OKPPublicKey>().bytes, signature, message, context);
- if (maybe_verified.is_error())
- return WebIDL::OperationError::create(Utf16String::from_utf8(maybe_verified.error().string_literal()));
-
- // 7. Let result be a boolean with the value true if the signature is valid
- // and the value false otherwise.
- // 8. Return result.
- return maybe_verified.release_value();
-}
-
-// https://w3c.github.io/webcrypto/#x25519-operations-derive-bits
-WebIDL::ExceptionOr<void> X25519::validate_derive_bits_parameters(AlgorithmParams const& params, Optional<u32> length_optional) const
-{
- auto const& normalized_algorithm = static_cast<EcdhKeyDeriveParams const&>(params);
-
- // 1. Let publicKey be the public member of normalizedAlgorithm.
- auto& public_key = normalized_algorithm.public_key;
-
- // 2. If the [[type]] internal slot of publicKey is not "public", then throw an InvalidAccessError.
- if (public_key->type() != KeyType::Public)
- return WebIDL::InvalidAccessError::create("Public key is not a public key"_utf16);
-
- // 3. If the name attribute of the [[algorithm]] internal slot of publicKey is not equal to the name member of
- // normalizedAlgorithm, then throw an InvalidAccessError.
- if (public_key->algorithm_name() != normalized_algorithm.name)
- return WebIDL::InvalidAccessError::create("Algorithm mismatch"_utf16);
-
- // 4. If length is not null and is greater than 256, then throw an OperationError.
- if (length_optional.has_value() && *length_optional > 256)
- return WebIDL::OperationError::create("Length is too large"_utf16);
-
- return {};
-}
-
-// https://w3c.github.io/webcrypto/#x25519-operations-derive-bits
-WebIDL::ExceptionOr<ByteBuffer> X25519::derive_bits(JS::Realm& realm, AlgorithmParams const& params, GC::Ref<CryptoKey> key, Optional<u32> length_optional)
-{
- auto const& normalized_algorithm = static_cast<EcdhKeyDeriveParams const&>(params);
-
- // NB: Steps 1 to 4 are performed by validate_derive_bits_parameters().
- TRY(validate_derive_bits_parameters(params, length_optional));
- auto& public_key = normalized_algorithm.public_key;
-
- // 5. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.
- if (key->type() != KeyType::Private)
- return WebIDL::InvalidAccessError::create("Key is not a private key"_utf16);
-
- // 6. If the name attribute of the [[algorithm]] internal slot of publicKey is not equal to
- // the name property of the [[algorithm]] internal slot of key, then throw an InvalidAccessError.
- if (key->algorithm_name() != public_key->algorithm_name())
- return WebIDL::InvalidAccessError::create("Algorithm mismatch"_utf16);
-
- // 7. Let secret be the result of performing the X25519 function specified in [RFC7748] Section 5 with
- // key as the X25519 private key k and
- // the X25519 public key represented by the [[handle]] internal slot of publicKey as the X25519 public key u.
- auto const& private_key = key->handle().get<OKPPrivateKey>().bytes;
- auto const& public_key_data = public_key->handle().get<OKPPublicKey>().bytes;
-
- ::Crypto::Curves::X25519 curve;
- auto maybe_secret = curve.compute_coordinate(private_key, public_key_data);
- if (maybe_secret.is_error())
- return WebIDL::OperationError::create("Failed to compute secret"_utf16);
-
- auto secret = maybe_secret.release_value();
-
- // 8. If secret is the all-zero value, then throw a OperationError.
- // This check must be performed in constant-time, as per [RFC7748] Section 6.1.
- // NOTE: The check may be performed by ORing all the bytes together and checking whether the result is zero,
- // as this eliminates standard side-channels in software implementations.
- auto or_bytes = 0;
- for (auto byte : secret.bytes()) {
- or_bytes |= byte;
- }
-
- if (or_bytes == 0)
- return WebIDL::OperationError::create("Secret is the all-zero value"_utf16);
-
- // 9. If length is null: Return secret.
- if (!length_optional.has_value()) {
- auto result = TRY_OR_THROW_OOM(realm.vm(), ByteBuffer::copy(secret));
- return result;
- }
-
- // Otherwise: If the length of secret in bits is less than length: throw an OperationError.
- auto length = length_optional.value();
- if (secret.size() * 8 < length)
- return WebIDL::OperationError::create("Secret is too short"_utf16);
-
- // Otherwise: Return an octet string containing the first length bits of secret.
- auto slice = TRY_OR_THROW_OOM(realm.vm(), secret.slice(0, ceil_div(length, 8)));
- if (length % 8 != 0) {
- // Zero out the last bits
- slice[slice.size() - 1] &= 0xFF << (8 - (length % 8));
- }
-
- return slice;
-}
-
-// https://w3c.github.io/webcrypto/#x25519-operations-generate-key
-WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> X25519::generate_key(JS::Realm&, [[maybe_unused]] AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages)
-{
- // 1. If usages contains an entry which is not "deriveKey" or "deriveBits" then throw a SyntaxError.
- for (auto const& usage : key_usages) {
- if (usage != KeyUsage::DeriveKey && usage != KeyUsage::DeriveBits) {
- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));
- }
- }
-
- // 2. Generate an X25519 key pair, with the private key being 32 random bytes,
- // and the public key being X25519(a, 9), as defined in [RFC7748], section 6.1.
- ::Crypto::Curves::X25519 curve;
- auto maybe_private_key = curve.generate_private_key();
- if (maybe_private_key.is_error())
- return WebIDL::OperationError::create("Failed to generate private key"_utf16);
-
- auto private_key_data = maybe_private_key.release_value();
-
- auto maybe_public_key = curve.generate_public_key(private_key_data);
- if (maybe_public_key.is_error())
- return WebIDL::OperationError::create("Failed to generate public key"_utf16);
-
- auto public_key_data = maybe_public_key.release_value();
-
- // 5. Let publicKey be a new CryptoKey associated with the relevant global object of this [HTML],
- // and representing the public key of the generated key pair.
- auto public_key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPublicKey { move(public_key_data) } });
-
- // 6. Set the [[type]] internal slot of publicKey to "public"
- public_key->set_type(KeyType::Public);
-
- // 3. Let algorithm be a new KeyAlgorithm object.
- // 4. Set the name attribute of algorithm to "X25519".
- // 7. Set the [[algorithm]] internal slot of publicKey to algorithm.
- public_key->set_algorithm(CryptoKey::KeyAlgorithmData { "X25519"_utf16 });
-
- // 8. Set the [[extractable]] internal slot of publicKey to true.
- public_key->set_extractable(true);
-
- // 9. Set the [[usages]] internal slot of publicKey to be the empty list.
- public_key->set_usages({});
-
- // 10. Let privateKey be a new CryptoKey associated with the relevant global object of this [HTML],
- // and representing the private key of the generated key pair.
- auto private_key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPrivateKey { move(private_key_data) } });
-
- // 11. Set the [[type]] internal slot of privateKey to "private"
- private_key->set_type(KeyType::Private);
-
- // 12. Set the [[algorithm]] internal slot of privateKey to algorithm.
- private_key->set_algorithm(CryptoKey::KeyAlgorithmData { "X25519"_utf16 });
-
- // 13. Set the [[extractable]] internal slot of privateKey to extractable.
- private_key->set_extractable(extractable);
-
- // 14. Set the [[usages]] internal slot of privateKey to be the usage intersection of usages and [ "deriveKey", "deriveBits" ].
- private_key->set_usages(usage_intersection(key_usages, { { KeyUsage::DeriveKey, KeyUsage::DeriveBits } }));
-
- // 15. Let result be a new CryptoKeyPair dictionary.
- // 16. Set the publicKey attribute of result to be publicKey.
- // 17. Set the privateKey attribute of result to be privateKey.
- // 18. Return the result of converting result to an ECMAScript Object, as defined by [WebIDL].
- return Variant<GC::Ref<CryptoKey>, CryptoKeyPair> { CryptoKeyPair { public_key, private_key } };
-}
-
-// https://w3c.github.io/webcrypto/#x25519-operations-import-key
-WebIDL::ExceptionOr<GC::Ref<CryptoKey>> X25519::import_key(JS::Realm& realm, [[maybe_unused]] Web::Crypto::AlgorithmParams const& params, KeyFormat key_format, CryptoKey::ImportKeyData key_data, bool extractable, Vector<KeyUsage> const& usages)
-{
- // NOTE: This is a parameter to the function
- // 1. Let keyData be the key data to be imported.
-
- auto& vm = realm.vm();
- GC::Ptr<CryptoKey> key = nullptr;
-
- // 2. If format is "spki":
- if (key_format == KeyFormat::Spki) {
- // 1. If usages is not empty then throw a SyntaxError.
- if (!usages.is_empty())
- return WebIDL::SyntaxError::create("Usages must be empty"_utf16);
-
- // 2. Let spki be the result of running the parse a subjectPublicKeyInfo algorithm over keyData.
- // 3. If an error occurred while parsing, then throw a DataError.
- auto spki = TRY(parse_a_subject_public_key_info(realm, key_data.get<ByteBuffer>()));
-
- // 4. If the algorithm object identifier field of the algorithm AlgorithmIdentifier field of spki
- // is not equal to the id-X25519 object identifier defined in [RFC8410], then throw a DataError.
- if (spki.algorithm.identifier != ::Crypto::ASN1::x25519_oid)
- return WebIDL::DataError::create("Invalid algorithm"_utf16);
-
- // 5. If the parameters field of the algorithm AlgorithmIdentifier field of spki is present, then throw a DataError.
- if (spki.algorithm.ec_parameters.has_value())
- return WebIDL::DataError::create("Invalid algorithm parameters"_utf16);
-
- // 6. Let publicKey be the X25519 public key identified by the subjectPublicKey field of spki.
- auto public_key = spki.raw_key;
-
- // 7. Let key be a new CryptoKey associated with the relevant global object of this [HTML], and that represents publicKey.
- key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPublicKey { move(public_key) } });
-
- // 8. Set the [[type]] internal slot of key to "public"
- key->set_type(KeyType::Public);
-
- // 9. Let algorithm be a new KeyAlgorithm.
- // 10. Set the name attribute of algorithm to "X25519".
- // 11. Set the [[algorithm]] internal slot of key to algorithm.
- key->set_algorithm(CryptoKey::KeyAlgorithmData { "X25519"_utf16 });
- }
-
- // 2. If format is "pkcs8":
- else if (key_format == KeyFormat::Pkcs8) {
- // 1. If usages contains an entry which is not "deriveKey" or "deriveBits" then throw a SyntaxError.
- for (auto const& usage : usages) {
- if (usage != KeyUsage::DeriveKey && usage != KeyUsage::DeriveBits) {
- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));
- }
- }
-
- // 2. Let privateKeyInfo be the result of running the parse a privateKeyInfo algorithm over keyData.
- // 3. If an error occurred while parsing, then throw a DataError.
- auto private_key_info = TRY(parse_a_private_key_info(realm, key_data.get<ByteBuffer>()));
-
- // 4. If the algorithm object identifier field of the privateKeyAlgorithm PrivateKeyAlgorithm field of privateKeyInfo
- // is not equal to the id-X25519 object identifier defined in [RFC8410], then throw a DataError.
- if (private_key_info.algorithm.identifier != ::Crypto::ASN1::x25519_oid)
- return WebIDL::DataError::create("Invalid algorithm"_utf16);
-
- // 5. If the parameters field of the privateKeyAlgorithm PrivateKeyAlgorithmIdentifier field of privateKeyInfo is present, then throw a DataError.
- if (private_key_info.algorithm.ec_parameters.has_value())
- return WebIDL::DataError::create("Invalid algorithm parameters"_utf16);
-
- // 6. Let curvePrivateKey be the result of performing the parse an ASN.1 structure algorithm,
- // with data as the privateKey field of privateKeyInfo,
- // structure as the ASN.1 CurvePrivateKey structure specified in Section 7 of [RFC8410], and
- // exactData set to true.
- // 7. If an error occurred while parsing, then throw a DataError.
- auto curve_private_key = TRY(parse_an_ASN1_structure<StringView>(realm, private_key_info.raw_key, true));
- auto curve_private_key_bytes = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(curve_private_key.bytes()));
-
- // 8. Let key be a new CryptoKey associated with the relevant global object of this [HTML],
- // and that represents the X25519 private key identified by curvePrivateKey.
- key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPrivateKey { move(curve_private_key_bytes) } });
-
- // 9. Set the [[type]] internal slot of key to "private"
- key->set_type(KeyType::Private);
-
- // 10. Let algorithm be a new KeyAlgorithm.
- // 11. Set the name attribute of algorithm to "X25519".
- // 12. Set the [[algorithm]] internal slot of key to algorithm.
- key->set_algorithm(CryptoKey::KeyAlgorithmData { "X25519"_utf16 });
- }
-
- // 2. If format is "jwk":
- else if (key_format == KeyFormat::Jwk) {
- // 1. If keyData is a JsonWebKey dictionary: Let jwk equal keyData.
- // Otherwise: Throw a DataError.
- if (!key_data.has<JsonWebKey>())
- return WebIDL::DataError::create("keyData is not a JsonWebKey dictionary"_utf16);
- auto& jwk = key_data.get<JsonWebKey>();
-
- // 2. If the d field is present and if usages contains an entry which is not "deriveKey" or "deriveBits" then throw a SyntaxError.
- if (jwk.d.has_value() && !usages.is_empty()) {
- for (auto const& usage : usages) {
- if (usage != KeyUsage::DeriveKey && usage != KeyUsage::DeriveBits) {
- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));
- }
- }
- }
-
- // 3. If the d field is not present and if usages is not empty then throw a SyntaxError.
- if (!jwk.d.has_value() && !usages.is_empty())
- return WebIDL::SyntaxError::create("Usages must be empty if d is missing"_utf16);
-
- // 4. If the kty field of jwk is not "OKP", then throw a DataError.
- if (jwk.kty != "OKP"sv)
- return WebIDL::DataError::create("Invalid key type"_utf16);
-
- // 5. If the crv field of jwk is not "X25519", then throw a DataError.
- if (jwk.crv != "X25519"sv)
- return WebIDL::DataError::create("Invalid curve"_utf16);
-
- // 6. If usages is non-empty and the use field of jwk is present and is not equal to "enc" then throw a DataError.
- if (!usages.is_empty() && jwk.use.has_value() && jwk.use.value() != "enc"sv)
- return WebIDL::DataError::create("Invalid use"_utf16);
-
- // 7. If the key_ops field of jwk is present, and is invalid according to the requirements of JSON Web Key [JWK],
- // or it does not contain all of the specified usages values, then throw a DataError.
- TRY(validate_jwk_key_ops(realm, jwk, usages));
-
- // 8. If the ext field of jwk is present and has the value false and extractable is true, then throw a DataError.
- if (jwk.ext.has_value() && !jwk.ext.value() && extractable)
- return WebIDL::DataError::create("Invalid extractable"_utf16);
-
- // 9. If the d field is present:
- if (jwk.d.has_value()) {
- // 1. If jwk does not meet the requirements of the JWK private key format described in Section 2 of [RFC8037], then throw a DataError.
- // o The parameter "kty" MUST be "OKP".
- if (jwk.kty != "OKP"sv)
- return WebIDL::DataError::create("Invalid key type"_utf16);
-
- // https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve
- // o The parameter "crv" MUST be present and contain the subtype of the key (from the "JSON Web Elliptic Curve" registry).
- if (jwk.crv != "X25519"sv)
- return WebIDL::DataError::create("Invalid curve"_utf16);
-
- // o The parameter "x" MUST be present and contain the public key encoded using the base64url [RFC4648] encoding.
- if (!jwk.x.has_value())
- return WebIDL::DataError::create("Missing x field"_utf16);
- auto public_key = TRY(base64_url_bytes_decode(realm, jwk.x.value()));
- if (public_key.size() != 32)
- return WebIDL::DataError::create("Invalid key length"_utf16);
-
- // o The parameter "d" MUST be present for private keys and contain the private key encoded using the base64url encoding.
- // This parameter MUST NOT be present for public keys.
- if (!jwk.d.has_value())
- return WebIDL::DataError::create("Missing d field"_utf16);
- auto private_key = TRY(base64_url_bytes_decode(realm, jwk.d.value()));
- if (private_key.size() != 32)
- return WebIDL::DataError::create("Invalid key length"_utf16);
-
- ::Crypto::Curves::X25519 curve;
- auto derived_public_key = TRY_OR_THROW_OOM(realm.vm(), curve.generate_public_key(private_key));
- if (derived_public_key != public_key)
- return WebIDL::DataError::create("Invalid key pair"_utf16);
-
- // 2. Let key be a new CryptoKey object that represents the X25519 private key identified by interpreting jwk according to Section 2 of [RFC8037].
- key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPrivateKey { move(private_key) } });
-
- // 3. Set the [[type]] internal slot of Key to "private".
- key->set_type(KeyType::Private);
- }
- // 9. Otherwise:
- else {
- // 1. If jwk does not meet the requirements of the JWK public key format described in Section 2 of [RFC8037], then throw a DataError.
- // o The parameter "kty" MUST be "OKP".
- if (jwk.kty != "OKP"sv)
- return WebIDL::DataError::create("Invalid key type"_utf16);
-
- // https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve
- // o The parameter "crv" MUST be present and contain the subtype of the key (from the "JSON Web Elliptic Curve" registry).
- if (jwk.crv != "X25519"sv)
- return WebIDL::DataError::create("Invalid curve"_utf16);
-
- // o The parameter "x" MUST be present and contain the public key encoded using the base64url [RFC4648] encoding.
- if (!jwk.x.has_value())
- return WebIDL::DataError::create("Missing x field"_utf16);
- auto public_key = TRY(base64_url_bytes_decode(realm, jwk.x.value()));
- if (public_key.size() != 32)
- return WebIDL::DataError::create("Invalid key length"_utf16);
-
- // o The parameter "d" MUST be present for private keys and contain the private key encoded using the base64url encoding.
- // This parameter MUST NOT be present for public keys.
- if (jwk.d.has_value())
- return WebIDL::DataError::create("Present d field"_utf16);
-
- key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPublicKey { move(public_key) } });
-
- // 3. Set the [[type]] internal slot of Key to "public".
- key->set_type(KeyType::Public);
- }
-
- // 10. Let algorithm be a new instance of a KeyAlgorithm object.
- // 11. Set the name attribute of algorithm to "X25519".
- // 12. Set the [[algorithm]] internal slot of key to algorithm.
- key->set_algorithm(CryptoKey::KeyAlgorithmData { "X25519"_utf16 });
- }
-
- // 2. If format is "raw":
- // https://wicg.github.io/webcrypto-modern-algos/#subtlecrypto-interface-keyformat
- // For all existing asymmetric algorithms in WebCrypto, "raw-public" acts as an alias of "raw".
- else if (key_format == KeyFormat::Raw || key_format == KeyFormat::RawPublic) {
- // 1. If usages is not empty then throw a SyntaxError.
- if (!usages.is_empty())
- return WebIDL::SyntaxError::create("Usages must be empty"_utf16);
-
- // 2. If the length in bits of keyData is not 256 then throw a DataError.
- auto& data = key_data.get<ByteBuffer>();
- if (data.size() * 8 != 256)
- return WebIDL::DataError::create("Invalid key length"_utf16);
-
- // 5. Let key be a new CryptoKey representing the key data provided in keyData.
- key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPublicKey { move(data) } });
-
- // 6. Set the [[type]] internal slot of key to "public"
- key->set_type(KeyType::Public);
-
- // 3. Let algorithm be a new KeyAlgorithm object.
- // 4. Set the name attribute of algorithm to "X25519".
- // 7. Set the [[algorithm]] internal slot of key to algorithm.
- key->set_algorithm(CryptoKey::KeyAlgorithmData { "X25519"_utf16 });
- }
-
- // 2. Otherwise: throw a NotSupportedError.
- else {
- return WebIDL::NotSupportedError::create("Invalid key format"_utf16);
- }
-
- // 3. Return key.
- return GC::Ref { *key };
-}
-
-// https://w3c.github.io/webcrypto/#x25519-operations-export-key
-WebIDL::ExceptionOr<ExportKeyResult> X25519::export_key(JS::Realm& realm, KeyFormat format, GC::Ref<CryptoKey> key)
-{
- auto& vm = realm.vm();
-
- // NOTE: This is a parameter to the function
- // 1. Let key be the CryptoKey to be exported.
-
- // 2. If the underlying cryptographic key material represented by the [[handle]] internal slot of key cannot be accessed, then throw an OperationError.
- // Note: In our impl this is always accessible
- auto const& handle = key->handle();
-
- Optional<ExportKeyResult> result;
-
- // 3. If format is "spki":
- if (format == KeyFormat::Spki) {
- // 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
- if (key->type() != KeyType::Public)
- return WebIDL::InvalidAccessError::create("Key is not a public key"_utf16);
-
- // 2. Let data be an instance of the subjectPublicKeyInfo ASN.1 structure defined in [RFC5280] with the following properties:
- // Set the algorithm field to an AlgorithmIdentifier ASN.1 type with the following properties:
- // Set the algorithm object identifier to the id-X25519 OID defined in [RFC8410].
- // Set the subjectPublicKey field to keyData.
- auto public_key = handle.get<OKPPublicKey>().bytes;
- auto data = TRY_OR_THROW_OOM(vm, ::Crypto::PK::wrap_in_subject_public_key_info(public_key, ::Crypto::ASN1::x25519_oid));
-
- // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
- result = ExportKeyResult { data };
- }
-
- // 3. If format is "pkcs8":
- else if (format == KeyFormat::Pkcs8) {
- // 1. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.
- if (key->type() != KeyType::Private)
- return WebIDL::InvalidAccessError::create("Key is not a private key"_utf16);
-
- // 2. Let data be an instance of the privateKeyInfo ASN.1 structure defined in [RFC5208] with the following properties:
- // Set the version field to 0.
- // Set the privateKeyAlgorithm field to a PrivateKeyAlgorithmIdentifier ASN.1 type with the following properties:
- // Set the algorithm object identifier to the id-X25519 OID defined in [RFC8410].
- // Set the privateKey field to the result of DER-encoding a CurvePrivateKey ASN.1 type, as defined in Section 7 of [RFC8410],
- // that represents the X25519 private key represented by the [[handle]] internal slot of key
- auto private_key = handle.get<OKPPrivateKey>().bytes;
-
- ::Crypto::ASN1::Encoder encoder;
- TRY_OR_THROW_OOM(vm, encoder.write(private_key));
- auto data = TRY_OR_THROW_OOM(vm, ::Crypto::PK::wrap_in_private_key_info(encoder.finish(), ::Crypto::ASN1::x25519_oid));
-
- // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
- result = ExportKeyResult { data };
- }
-
- // 3. If format is "jwt":
- else if (format == KeyFormat::Jwk) {
- // 1. Let jwk be a new JsonWebKey dictionar1y.
- JsonWebKey jwk = {};
-
- // 2. Set the kty attribute of jwk to "OKP".
- jwk.kty = "OKP"_utf16;
-
- // 3. Set the crv attribute of jwk to "X25519".
- jwk.crv = "X25519"_utf16;
-
- // 4. Set the x attribute of jwk according to the definition in Section 2 of [RFC8037].
- if (key->type() == KeyType::Public) {
- auto public_key = handle.get<OKPPublicKey>().bytes;
- jwk.x = Utf16String::from_utf8(TRY_OR_THROW_OOM(vm, encode_base64url(public_key, AK::OmitPadding::Yes)));
- } else {
- // The "x" parameter of the "epk" field is set as follows:
- // Apply the appropriate ECDH function to the ephemeral private key (as scalar input)
- // and the standard base point (as u-coordinate input).
- // The base64url encoding of the output is the value for the "x" parameter of the "epk" field.
- ::Crypto::Curves::X25519 curve;
- auto public_key = TRY_OR_THROW_OOM(vm, curve.generate_public_key(handle.get<OKPPrivateKey>().bytes));
- jwk.x = TRY_OR_THROW_OOM(vm, base64_url_bytes_encode(public_key));
- }
-
- // 5. If the [[type]] internal slot of key is "private"
- if (key->type() == KeyType::Private) {
- // 1. Set the d attribute of jwk according to the definition in Section 2 of [RFC8037].
- auto private_key = handle.get<OKPPrivateKey>().bytes;
- jwk.d = TRY_OR_THROW_OOM(vm, base64_url_bytes_encode(private_key));
- }
-
- // 6. Set the key_ops attribute of jwk to the usages attribute of key.
- jwk.key_ops = Vector<Utf16String> {};
- jwk.key_ops->ensure_capacity(key->internal_usages().size());
- for (auto const& usage : key->internal_usages())
- jwk.key_ops->unchecked_append(Utf16String::from_utf8(key_usage_to_string(usage)));
-
- // 7. Set the ext attribute of jwk to the [[extractable]] internal slot of key.
- jwk.ext = key->extractable();
-
- // 8. Let result be the result of converting jwk to an ECMAScript Object, as defined by [WebIDL].
- result = ExportKeyResult { move(jwk) };
- }
-
- // 3. If format is "raw":
- // https://wicg.github.io/webcrypto-modern-algos/#subtlecrypto-interface-keyformat
- // For all existing asymmetric algorithms in WebCrypto, "raw-public" acts as an alias of "raw".
- else if (format == KeyFormat::Raw || format == KeyFormat::RawPublic) {
- // 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
- if (key->type() != KeyType::Public)
- return WebIDL::InvalidAccessError::create("Key is not a public key"_utf16);
-
- // 2. Let data be an octet string representing the X25519 public key represented by the [[handle]] internal slot of key.
- auto public_key = handle.get<OKPPublicKey>().bytes;
-
- // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
- result = ExportKeyResult { public_key };
- }
-
- // 3. Otherwise:
- else {
- return WebIDL::NotSupportedError::create("Invalid key format"_utf16);
- }
-
- // 4. Return result.
- return result.release_value();
-}
-
-Optional<ReadonlySpan<KeyUsage>> X25519::public_key_usages() const
-{
- return ReadonlySpan<KeyUsage> {};
-}
-
-// https://wicg.github.io/webcrypto-secure-curves/#x448-operations
-WebIDL::ExceptionOr<ByteBuffer> X448::derive_bits(JS::Realm& realm,
- AlgorithmParams const& params,
- GC::Ref<CryptoKey> key,
- Optional<u32> length_optional)
-{
- // 1. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.
- if (key->type() != KeyType::Private)
- return WebIDL::InvalidAccessError::create("Key is not a private key"_utf16);
-
- // 2. Let publicKey be the public member of normalizedAlgorithm.
- auto& public_key = static_cast<EcdhKeyDeriveParams const&>(params).public_key;
-
- // 3. If the [[type]] internal slot of publicKey is not "public", then throw an InvalidAccessError.
- if (public_key->type() != KeyType::Public)
- return WebIDL::InvalidAccessError::create("Public key is not a public key"_utf16);
-
- // 4. If the name attribute of the [[algorithm]] internal slot of publicKey is not equal to
- // the name property of the [[algorithm]] internal slot of key, then throw an InvalidAccessError.
- if (key->algorithm_name() != public_key->algorithm_name())
- return WebIDL::InvalidAccessError::create("Algorithm mismatch"_utf16);
-
- // 5. Let secret be the result of performing the X448 function specified in [RFC7748] Section 5
- // with key as the X448 private key k and the X448 public key represented by the [[handle]]
- // internal slot of publicKey as the X448 public key u.
- auto const& private_key = key->handle().get<OKPPrivateKey>().bytes;
- auto const& public_key_data = public_key->handle().get<OKPPublicKey>().bytes;
-
- ::Crypto::Curves::X448 curve;
- auto maybe_secret = curve.compute_coordinate(private_key, public_key_data);
- if (maybe_secret.is_error())
- return WebIDL::OperationError::create("Failed to compute secret"_utf16);
-
- auto secret = maybe_secret.release_value();
-
- // 6. If secret is the all-zero value, then throw a OperationError. This check must be performed in constant-time, as per [RFC7748] Section 6.2.
- // NOTE: The check may be performed by ORing all the bytes together and checking whether the result is zero,
- // as this eliminates standard side-channels in software implementations.
- auto or_bytes = 0;
- for (auto byte : secret.bytes()) {
- or_bytes |= byte;
- }
-
- if (or_bytes == 0)
- return WebIDL::OperationError::create("Secret is the all-zero value"_utf16);
-
- // 7. If length is null: Return secret
- if (!length_optional.has_value()) {
- auto result = TRY_OR_THROW_OOM(realm.vm(), ByteBuffer::copy(secret));
- return result;
- }
-
- // Otherwise: Return an octet string containing the first length bits of secret.
- auto length = length_optional.value();
- if (secret.size() * 8 < length)
- return WebIDL::OperationError::create("Secret is too short"_utf16);
-
- auto slice = TRY_OR_THROW_OOM(realm.vm(), secret.slice(0, ceil_div(length, 8)));
- if (length % 8 != 0) {
- // Zero out the last bits
- slice[slice.size() - 1] &= 0xFF << (8 - (length % 8));
- }
-
- return slice;
-}
-
-// https://wicg.github.io/webcrypto-secure-curves/#x448-operations
-WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> X448::generate_key(JS::Realm&,
- AlgorithmParams const&,
- bool extractable,
- Vector<KeyUsage> const& usages)
-{
- // 1. If usages contains an entry which is not "deriveKey" or "deriveBits" then throw a SyntaxError.
- for (auto const& usage : usages) {
- if (usage != KeyUsage::DeriveKey && usage != KeyUsage::DeriveBits) {
- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));
- }
- }
-
- // 2. Generate an X448 key pair, with the private key being 56 random bytes, and the public key being X448(a, 5), as defined in [RFC7748], section 6.2.
- ::Crypto::Curves::X448 curve;
- auto maybe_private_key = curve.generate_private_key();
- if (maybe_private_key.is_error())
- return WebIDL::OperationError::create("Failed to generate private key"_utf16);
- auto private_key_data = maybe_private_key.release_value();
- auto maybe_public_key = curve.generate_public_key(private_key_data);
- if (maybe_public_key.is_error())
- return WebIDL::OperationError::create("Failed to generate public key"_utf16);
- auto public_key_data = maybe_public_key.release_value();
-
- // 5. Let publicKey be a new CryptoKey associated with the relevant global object of this [HTML], and representing the public key of the generated key pair.
- auto public_key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPublicKey { move(public_key_data) } });
-
- // 6. Set the [[type]] internal slot of publicKey to "public"
- public_key->set_type(KeyType::Public);
-
- // 3. Let algorithm be a new KeyAlgorithm object.
- // 4. Set the name attribute of algorithm to "X448".
- // 7. Set the [[algorithm]] internal slot of publicKey to algorithm.
- public_key->set_algorithm(CryptoKey::KeyAlgorithmData { "X448"_utf16 });
-
- // 8. Set the [[extractable]] internal slot of publicKey to true.
- public_key->set_extractable(true);
-
- // 9. Set the [[usages]] internal slot of publicKey to be the empty list.
- public_key->set_usages({});
-
- // 10. Let privateKey be a new CryptoKey associated with the relevant global object of this [HTML], and representing the private key of the generated key pair.
- auto private_key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPrivateKey { move(private_key_data) } });
-
- // 11. Set the [[type]] internal slot of privateKey to "private"
- private_key->set_type(KeyType::Private);
-
- // 12. Set the [[algorithm]] internal slot of privateKey to algorithm.
- private_key->set_algorithm(CryptoKey::KeyAlgorithmData { "X448"_utf16 });
-
- // 13. Set the [[extractable]] internal slot of privateKey to extractable.
- private_key->set_extractable(extractable);
-
- // 14. Set the [[usages]] internal slot of privateKey to be the usage intersection of usages and [ "deriveKey", "deriveBits" ].
- private_key->set_usages(usage_intersection(usages, { { KeyUsage::DeriveKey, KeyUsage::DeriveBits } }));
-
- // 15. Let result be a new CryptoKeyPair dictionary.
- // 16. Set the publicKey attribute of result to be publicKey.
- // 17. Set the privateKey attribute of result to be privateKey.
- auto result = CryptoKeyPair { public_key, private_key };
-
- // 18. Return the result of converting result to an ECMAScript Object, as defined by [WebIDL].
- return Variant<GC::Ref<CryptoKey>, CryptoKeyPair> { result };
-}
-
-// https://wicg.github.io/webcrypto-secure-curves/#x448-operations
-WebIDL::ExceptionOr<ExportKeyResult> X448::export_key(JS::Realm& realm, KeyFormat format, GC::Ref<CryptoKey> key)
+bool web_crypto_okp_export_der(OkpCurve curve, bool is_public, ReadonlyBytes key, void* state, ReceiveEncodedBytes receive)
{
- // 1. Let key be the CryptoKey to be exported.
-
- // 2. If the underlying cryptographic key material represented by the [[handle]] internal slot of key cannot be accessed, then throw an OperationError.
- // Note: In our impl this is always accessible
- auto const& handle = key->handle();
-
- // 3. If format is "spki":
- if (format == KeyFormat::Spki) {
- // 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
- if (key->type() != KeyType::Public)
- return WebIDL::InvalidAccessError::create("Key is not a public key"_utf16);
-
- // 2. Let data be an instance of the subjectPublicKeyInfo ASN.1 structure defined in [RFC5280] with the following properties:
- // * Set the algorithm field to an AlgorithmIdentifier ASN.1 type with the following properties:
- // * Set the algorithm object identifier to the id-X448 OID defined in [RFC8410].
- // * Set the subjectPublicKey field to keyData.
- auto x448_oid = ::Crypto::ASN1::x448_oid;
- auto data = TRY_OR_THROW_OOM(realm.vm(), ::Crypto::PK::wrap_in_subject_public_key_info(handle.get<OKPPublicKey>().bytes, x448_oid));
-
- // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
- return ExportKeyResult { data };
- }
-
- // 3. If format is "pkcs8":
- if (format == KeyFormat::Pkcs8) {
- // 1. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.
- if (key->type() != KeyType::Private)
- return WebIDL::InvalidAccessError::create("Key is not a private key"_utf16);
-
- // 2. Let data be an instance of the privateKeyInfo ASN.1 structure defined in [RFC5208] with the following properties:
- // * Set the version field to 0.
- // * Set the privateKeyAlgorithm field to a PrivateKeyAlgorithmIdentifier ASN.1 type with the following properties:
- // * Set the algorithm object identifier to the id-X448 OID defined in [RFC8410].
- // * Set the privateKey field to the result of DER-encoding a CurvePrivateKey ASN.1 type, as defined in Section 7 of [RFC8410], that represents the X448 private key represented by the [[handle]] internal slot of key
+ auto result = [&]() -> ErrorOr<ByteBuffer> {
+ auto oid = okp_curve_oid(curve);
+ if (is_public)
+ return ::Crypto::PK::wrap_in_subject_public_key_info(TRY(ByteBuffer::copy(key)), oid);
::Crypto::ASN1::Encoder encoder;
- TRY_OR_THROW_OOM(realm.vm(), encoder.write(handle.get<OKPPrivateKey>().bytes.bytes()));
-
- auto x448_oid = ::Crypto::ASN1::x448_oid;
- auto data = TRY_OR_THROW_OOM(realm.vm(), ::Crypto::PK::wrap_in_private_key_info(encoder.finish(), x448_oid));
-
- // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
- return ExportKeyResult { data };
- }
-
- // 3. If format is "jwk":
- if (format == KeyFormat::Jwk) {
- // 1. Let jwk be a new JsonWebKey dictionary.
- JsonWebKey jwk = {};
-
- // 2. Set the kty attribute of jwk to "OKP".
- jwk.kty = "OKP"_utf16;
-
- // 3. Set the crv attribute of jwk to "X448".
- jwk.crv = "X448"_utf16;
-
- // 4. Set the x attribute of jwk according to the definition in Section 2 of [RFC8037].
- if (key->type() == KeyType::Public) {
- jwk.x = Utf16String::from_utf8(TRY_OR_THROW_OOM(realm.vm(), encode_base64url(handle.get<OKPPublicKey>().bytes, AK::OmitPadding::Yes)));
- } else {
- ::Crypto::Curves::X448 curve;
- auto public_key = TRY_OR_THROW_OOM(realm.vm(), curve.generate_public_key(handle.get<OKPPrivateKey>().bytes));
- jwk.x = Utf16String::from_utf8(TRY_OR_THROW_OOM(realm.vm(), encode_base64url(public_key, AK::OmitPadding::Yes)));
- }
-
- // 5. If the [[type]] internal slot of key is "private"
- if (key->type() == KeyType::Private) {
- // 1. Set the d attribute of jwk according to the definition in Section 2 of [RFC8037].
- jwk.d = Utf16String::from_utf8(TRY_OR_THROW_OOM(realm.vm(), encode_base64url(handle.get<OKPPrivateKey>().bytes, AK::OmitPadding::Yes)));
- }
-
- // 6. Set the key_ops attribute of jwk to the usages attribute of key.
- jwk.key_ops = Vector<Utf16String> {};
- jwk.key_ops->ensure_capacity(key->internal_usages().size());
- for (auto const& usage : key->internal_usages())
- jwk.key_ops->unchecked_append(Utf16String::from_utf8(key_usage_to_string(usage)));
-
- // 7. Set the ext attribute of jwk to the [[extractable]] internal slot of key.
- jwk.ext = key->extractable();
-
- // 8. Let result be the result of converting jwk to an ECMAScript Object, as defined by [WebIDL].
- return ExportKeyResult { move(jwk) };
- }
-
- // 3. If format is "raw":
- if (format == KeyFormat::Raw) {
- // 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
- if (key->type() != KeyType::Public)
- return WebIDL::InvalidAccessError::create("Key is not a public key"_utf16);
-
- // 2. Let data be an octet string representing the X448 public key represented by the [[handle]] internal slot of key.
- // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
- return ExportKeyResult { handle.get<OKPPublicKey>().bytes };
- }
-
- // 3. Otherwise:
- // throw a NotSupportedError.
- return WebIDL::NotSupportedError::create("Invalid key format"_utf16);
-}
-
-Optional<ReadonlySpan<KeyUsage>> X448::public_key_usages() const
-{
- return ReadonlySpan<KeyUsage> {};
-}
-
-// https://wicg.github.io/webcrypto-secure-curves/#x448-operations
-WebIDL::ExceptionOr<GC::Ref<CryptoKey>> X448::import_key(JS::Realm& realm,
- AlgorithmParams const&,
- KeyFormat format,
- CryptoKey::ImportKeyData key_data,
- bool extractable,
- Vector<KeyUsage> const& usages)
-{
- // 1. Let keyData be the key data to be imported.
-
- // 2. If format is "spki":
- if (format == KeyFormat::Spki) {
- // 1. If usages is not empty then throw a SyntaxError.
- if (!usages.is_empty())
- return WebIDL::SyntaxError::create("Usages must be empty"_utf16);
-
- // 2. Let spki be the result of running the parse a subjectPublicKeyInfo algorithm over keyData.
- // 3. If an error occurred while parsing, then throw a DataError.
- auto spki = TRY(parse_a_subject_public_key_info(realm, key_data.get<ByteBuffer>()));
-
- // 4. If the algorithm object identifier field of the algorithm AlgorithmIdentifier field of spki is not equal to the id-X448 object identifier defined in [RFC8410], then throw a DataError.
- if (spki.algorithm.identifier != ::Crypto::ASN1::x448_oid)
- return WebIDL::DataError::create("Invalid algorithm"_utf16);
-
- // 5. If the parameters field of the algorithm AlgorithmIdentifier field of spki is present, then throw a DataError.
- if (spki.algorithm.ec_parameters.has_value())
- return WebIDL::DataError::create("Invalid algorithm parameters"_utf16);
-
- // 6. Let publicKey be the X448 public key identified by the subjectPublicKey field of spki.
- auto const& public_key = spki.raw_key;
-
- // 7. Let key be a new CryptoKey associated with the relevant global object of this [HTML], and that represents publicKey.
- auto key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPublicKey { move(public_key) } });
-
- // 8. Set the [[type]] internal slot of key to "public"
- key->set_type(KeyType::Public);
-
- // 9. Let algorithm be a new KeyAlgorithm.
- // 10. Set the name attribute of algorithm to "X448".
- // 11. Set the [[algorithm]] internal slot of key to algorithm.
- key->set_algorithm(CryptoKey::KeyAlgorithmData { "X448"_utf16 });
-
- return key;
- }
-
- // 3. If format is "pkcs8":
- if (format == KeyFormat::Pkcs8) {
- // 1. If usages contains an entry which is not "deriveKey" or "deriveBits" then throw a SyntaxError.
- for (auto const& usage : usages) {
- if (usage != KeyUsage::DeriveKey && usage != KeyUsage::DeriveBits) {
- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));
- }
- }
-
- // 2. Let privateKeyInfo be the result of running the parse a privateKeyInfo algorithm over keyData.
- // 3. If an error occurred while parsing, then throw a DataError.
- auto private_key_info = TRY(parse_a_private_key_info(realm, key_data.get<ByteBuffer>()));
- auto private_key = private_key_info.raw_key;
-
- // 4. If the algorithm object identifier field of the privateKeyAlgorithm PrivateKeyAlgorithm field of privateKeyInfo is not equal to the id-X448 object identifier defined in [RFC8410], then throw a DataError.
- if (private_key_info.algorithm.identifier != ::Crypto::ASN1::x448_oid)
- return WebIDL::DataError::create("Invalid algorithm"_utf16);
-
- // 5. If the parameters field of the privateKeyAlgorithm PrivateKeyAlgorithmIdentifier field of privateKeyInfo is present, then throw a DataError.
- if (private_key_info.algorithm.ec_parameters.has_value())
- return WebIDL::DataError::create("Invalid algorithm parameters"_utf16);
-
- // 6. Let curvePrivateKey be the result of performing the parse an ASN.1 structure algorithm,
- // with data as the privateKey field of privateKeyInfo, structure as the ASN.1 CurvePrivateKey
- // structure specified in Section 7 of [RFC8410], and exactData set to true.
- // 7. If an error occurred while parsing, then throw a DataError.
- auto curve_private_key = TRY(parse_an_ASN1_structure<StringView>(realm, private_key_info.raw_key, true));
- auto curve_private_key_bytes = TRY_OR_THROW_OOM(realm.vm(), ByteBuffer::copy(curve_private_key.bytes()));
-
- // 8. Let key be a new CryptoKey associated with the relevant global object of this [HTML], and that represents the X448 private key identified by curvePrivateKey.
- auto key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPrivateKey { move(curve_private_key_bytes) } });
-
- // 9. Set the [[type]] internal slot of key to "private"
- key->set_type(KeyType::Private);
-
- // 10. Let algorithm be a new KeyAlgorithm.
- // 11. Set the name attribute of algorithm to "X448".
- // 12. Set the [[algorithm]] internal slot of key to algorithm.
- key->set_algorithm(CryptoKey::KeyAlgorithmData { "X448"_utf16 });
-
- return key;
- }
-
- // 3. If format is "jwk":
- if (format == KeyFormat::Jwk) {
- // 1. If keyData is a JsonWebKey dictionary:
- // Let jwk equal keyData.
- // Otherwise:
- // Throw a DataError.
- if (!key_data.has<JsonWebKey>())
- return WebIDL::DataError::create("Data is not a JsonWebKey dictionary"_utf16);
- auto& jwk = key_data.get<JsonWebKey>();
-
- // 2. If the d field is present and if usages contains an entry which is not "deriveKey" or "deriveBits" then throw a SyntaxError.
- if (jwk.d.has_value()) {
- for (auto const& usage : usages) {
- if (usage != KeyUsage::DeriveKey && usage != KeyUsage::DeriveBits) {
- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));
- }
- }
- }
-
- // 3. If the d field is not present and if usages is not empty then throw a SyntaxError.
- if (!jwk.d.has_value() && !usages.is_empty())
- return WebIDL::SyntaxError::create("Usages must be empty"_utf16);
-
- // 4. If the kty field of jwk is not "OKP", then throw a DataError.
- if (jwk.kty != "OKP"sv)
- return WebIDL::DataError::create("Invalid key type"_utf16);
-
- // 5. If the crv field of jwk is not "X448", then throw a DataError.
- if (jwk.crv != "X448"sv)
- return WebIDL::DataError::create("Invalid curve"_utf16);
-
- // 6. If usages is non-empty and the use field of jwk is present and is not equal to "enc" then throw a DataError.
- if (!usages.is_empty() && jwk.use.has_value() && jwk.use.value() != "enc"sv)
- return WebIDL::DataError::create("Invalid use"_utf16);
-
- // 7. If the key_ops field of jwk is present, and is invalid according to the requirements of JSON Web Key [JWK],
- // or it does not contain all of the specified usages values, then throw a DataError.
- TRY(validate_jwk_key_ops(realm, jwk, usages));
-
- // 8. If the ext field of jwk is present and has the value false and extractable is true, then throw a DataError.
- if (jwk.ext.has_value() && !jwk.ext.value() && extractable)
- return WebIDL::DataError::create("Invalid extractable"_utf16);
-
- GC::Ptr<CryptoKey> key = nullptr;
-
- // 9. If the d field is present:
- if (jwk.d.has_value()) {
- // 1. If jwk does not meet the requirements of the JWK private key format described in Section 2 of [RFC8037], then throw a DataError.
-
- // o The parameter "kty" MUST be "OKP".
- if (jwk.kty != "OKP"sv)
- return WebIDL::DataError::create("Invalid key type"_utf16);
-
- // https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve
- // o The parameter "crv" MUST be present and contain the subtype of the key (from the "JSON Web Elliptic Curve" registry).
- if (jwk.crv != "X448"sv)
- return WebIDL::DataError::create("Invalid curve"_utf16);
-
- // o The parameter "x" MUST be present and contain the public key encoded using the base64url [RFC4648] encoding.
- if (!jwk.x.has_value())
- return WebIDL::DataError::create("Missing x field"_utf16);
- auto public_key = TRY(base64_url_bytes_decode(realm, jwk.x.value()));
- if (public_key.size() != 56)
- return WebIDL::DataError::create("Invalid key length"_utf16);
-
- // o The parameter "d" MUST be present for private keys and contain the private key encoded using the base64url encoding.
- // This parameter MUST NOT be present for public keys.
- if (!jwk.d.has_value())
- return WebIDL::DataError::create("Missing d field"_utf16);
- auto private_key = TRY(base64_url_bytes_decode(realm, jwk.d.value()));
- if (private_key.size() != 56)
- return WebIDL::DataError::create("Invalid key length"_utf16);
-
- ::Crypto::Curves::X448 curve;
- auto derived_public_key = TRY_OR_THROW_OOM(realm.vm(), curve.generate_public_key(private_key));
- if (derived_public_key != public_key)
- return WebIDL::DataError::create("Invalid key pair"_utf16);
-
- // 2. Let key be a new CryptoKey object that represents the X448 private key identified by interpreting jwk according to Section 2 of [RFC8037].
- key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPrivateKey { move(private_key) } });
-
- // 3. Set the [[type]] internal slot of Key to "private".
- key->set_type(KeyType::Private);
- }
- // Otherwise:
- else {
- // 1. If jwk does not meet the requirements of the JWK public key format described in Section 2 of [RFC8037], then throw a DataError.
- // o The parameter "kty" MUST be "OKP".
- if (jwk.kty != "OKP"sv)
- return WebIDL::DataError::create("Invalid key type"_utf16);
-
- // https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve
- // o The parameter "crv" MUST be present and contain the subtype of the key (from the "JSON Web Elliptic Curve" registry).
- if (jwk.crv != "X448"sv)
- return WebIDL::DataError::create("Invalid curve"_utf16);
-
- // o The parameter "x" MUST be present and contain the public key encoded using the base64url [RFC4648] encoding.
- if (!jwk.x.has_value())
- return WebIDL::DataError::create("Missing x field"_utf16);
- auto public_key = TRY(base64_url_bytes_decode(realm, jwk.x.value()));
- if (public_key.size() != 56)
- return WebIDL::DataError::create("Invalid key length"_utf16);
-
- // o The parameter "d" MUST be present for private keys and contain the private key encoded using the base64url encoding.
- // This parameter MUST NOT be present for public keys.
- if (jwk.d.has_value())
- return WebIDL::DataError::create("Present d field"_utf16);
-
- // 2. Let key be a new CryptoKey object that represents the X448 public key identified by interpreting jwk according to Section 2 of [RFC8037].
- key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPublicKey { move(public_key) } });
-
- // 3. Set the [[type]] internal slot of Key to "public".
- key->set_type(KeyType::Public);
- }
-
- // 10. Let algorithm be a new instance of a KeyAlgorithm object.
- // 11. Set the name attribute of algorithm to "X448".
- // 12. Set the [[algorithm]] internal slot of key to algorithm.
- key->set_algorithm(CryptoKey::KeyAlgorithmData { "X448"_utf16 });
-
- return GC::Ref { *key };
- }
-
- // 3. If format is "raw":
- if (format == KeyFormat::Raw) {
- // 1. If usages is not empty then throw a SyntaxError.
- if (!usages.is_empty())
- return WebIDL::SyntaxError::create("Usages must be empty"_utf16);
-
- // 2.Let data be keyData.
- auto data = move(key_data.get<ByteBuffer>());
-
- // 3. If the length in bits of data is not 448 then throw a DataError.
- if (data.size() * 8 != 448)
- return WebIDL::DataError::create("Invalid key length"_utf16);
-
- // 6. Let key be a new CryptoKey associated with the relevant global object of this [HTML], and that represents data.
- auto key = CryptoKey::create(CryptoKey::InternalKeyData { OKPPublicKey { move(data) } });
-
- // 7. Set the [[type]] internal slot of key to "public"
- key->set_type(KeyType::Public);
-
- // 4. Let algorithm be a new KeyAlgorithm object.
- // 5. Set the name attribute of algorithm to "X448".
- // 8. Set the [[algorithm]] internal slot of key to algorithm.
- key->set_algorithm(CryptoKey::KeyAlgorithmData { "X448"_utf16 });
-
- return key;
- }
-
- (void)extractable;
-
- // 3. Otherwise:
- // throw a NotSupportedError.
- return WebIDL::NotSupportedError::create("Invalid key format"_utf16);
+ TRY(encoder.write(key));
+ return ::Crypto::PK::wrap_in_private_key_info(encoder.finish(), oid);
+ }();
+ if (result.is_error())
+ return false;
+ receive(state, result.value());
+ return true;
}
extern "C" {
diff --git a/Libraries/LibWeb/Crypto/CryptoAlgorithms.h b/Libraries/LibWeb/Crypto/CryptoAlgorithms.h
index af47f3d28e1..daa5333f945 100644
--- a/Libraries/LibWeb/Crypto/CryptoAlgorithms.h
+++ b/Libraries/LibWeb/Crypto/CryptoAlgorithms.h
@@ -138,71 +138,6 @@ protected:
AlgorithmMethods() = default;
};
-class ED25519 : public AlgorithmMethods {
-public:
- virtual WebIDL::ExceptionOr<ByteBuffer> sign(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;
- virtual WebIDL::ExceptionOr<bool> verify(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&, ByteBuffer const&) override;
-
- virtual WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> generate_key(JS::Realm& realm, AlgorithmParams const&, bool, Vector<KeyUsage> const&) override;
- virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;
- virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;
-
- virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;
-
- static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new ED25519); }
-
-private:
- ED25519() = default;
-};
-
-class ED448 : public AlgorithmMethods {
-public:
- virtual WebIDL::ExceptionOr<ByteBuffer> sign(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;
- virtual WebIDL::ExceptionOr<bool> verify(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&, ByteBuffer const&) override;
-
- virtual WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> generate_key(JS::Realm& realm, AlgorithmParams const&, bool, Vector<KeyUsage> const&) override;
- virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;
- virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;
-
- virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;
-
- static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new ED448); }
-
-private:
- ED448() = default;
-};
-
-class X25519 : public AlgorithmMethods {
-public:
- virtual WebIDL::ExceptionOr<ByteBuffer> derive_bits(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, Optional<u32>) override;
- virtual WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> generate_key(JS::Realm& realm, AlgorithmParams const&, bool, Vector<KeyUsage> const&) override;
- virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;
- virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;
-
- virtual WebIDL::ExceptionOr<void> validate_derive_bits_parameters(AlgorithmParams const&, Optional<u32>) const override;
- virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;
-
- static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new X25519); }
-
-private:
- X25519() = default;
-};
-
-class X448 : public AlgorithmMethods {
-public:
- virtual WebIDL::ExceptionOr<ByteBuffer> derive_bits(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, Optional<u32>) override;
- virtual WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> generate_key(JS::Realm& realm, AlgorithmParams const&, bool, Vector<KeyUsage> const&) override;
- virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;
- virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;
-
- virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;
-
- static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new X448); }
-
-private:
- X448() = default;
-};
-
class MLDSA : public AlgorithmMethods {
public:
virtual WebIDL::ExceptionOr<ByteBuffer> sign(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;
@@ -238,24 +173,11 @@ private:
MLKEM() = default;
};
-struct EcdhKeyDeriveParams : public AlgorithmParams {
- virtual ~EcdhKeyDeriveParams() override;
-
- EcdhKeyDeriveParams(CryptoKey& public_key)
- : public_key(public_key)
- {
- }
-
- GC::Root<CryptoKey> public_key;
-
- static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::Realm&, JS::Value);
-};
-
-// https://wicg.github.io/webcrypto-secure-curves/#dfn-Ed448Params
-struct Ed448Params : public AlgorithmParams {
- virtual ~Ed448Params() override;
+// https://wicg.github.io/webcrypto-modern-algos/#dfn-ContextParams
+struct ContextParams : public AlgorithmParams {
+ virtual ~ContextParams() override;
- Ed448Params(Optional<ByteBuffer>& context)
+ ContextParams(Optional<ByteBuffer>& context)
: context(context)
{
}
@@ -265,9 +187,6 @@ struct Ed448Params : public AlgorithmParams {
static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::Realm&, JS::Value);
};
-// https://wicg.github.io/webcrypto-modern-algos/#dfn-ContextParams
-using ContextParams = Ed448Params;
-
// https://wicg.github.io/webcrypto-modern-algos/#cshake-params
// https://wicg.github.io/webcrypto-modern-algos/#dfn-AeadParams
// NOTE: The AeadParams dictionary is identical to the AesGcmParams
diff --git a/Libraries/LibWeb/Crypto/CryptoKey.cpp b/Libraries/LibWeb/Crypto/CryptoKey.cpp
index 3e14629c47d..53291b8f0ad 100644
--- a/Libraries/LibWeb/Crypto/CryptoKey.cpp
+++ b/Libraries/LibWeb/Crypto/CryptoKey.cpp
@@ -63,6 +63,8 @@ void web_rust_crypto_key_set_usages(CryptoKey*, ReadonlySpan<KeyUsage>);
void web_crypto_key_destroy_handle(CryptoKey::InternalKeyData*);
CryptoKey::InternalKeyData* web_crypto_key_create_byte_handle(ReadonlyBytes);
+CryptoKey::InternalKeyData* web_crypto_key_create_okp_handle(bool, ReadonlyBytes);
+void web_crypto_key_okp_handle_bytes(CryptoKey::InternalKeyData const*, ReadonlyBytes*);
void web_crypto_key_byte_handle_bytes(CryptoKey::InternalKeyData const*, ReadonlyBytes*);
void web_crypto_key_encode_handle(CryptoKey::InternalKeyData const*, void*, void (*)(void*, CryptoKeyHandleFields const*));
JSCompletion web_crypto_key_decode_handle(void* realm, CryptoKeyHandleFields const*, CryptoKey::InternalKeyData** out_handle);
@@ -93,6 +95,22 @@ void web_crypto_key_byte_handle_bytes(CryptoKey::InternalKeyData const* handle,
*bytes = handle->get<ByteBuffer>().bytes();
}
+CryptoKey::InternalKeyData* web_crypto_key_create_okp_handle(bool is_private, ReadonlyBytes bytes)
+{
+ auto key_bytes = MUST(ByteBuffer::copy(bytes));
+ if (is_private)
+ return new CryptoKey::InternalKeyData(OKPPrivateKey { move(key_bytes) });
+ return new CryptoKey::InternalKeyData(OKPPublicKey { move(key_bytes) });
+}
+
+void web_crypto_key_okp_handle_bytes(CryptoKey::InternalKeyData const* handle, ReadonlyBytes* bytes)
+{
+ if (handle->has<OKPPrivateKey>())
+ *bytes = handle->get<OKPPrivateKey>().bytes;
+ else
+ *bytes = handle->get<OKPPublicKey>().bytes;
+}
+
CryptoKey::InternalKeyData const& CryptoKey::handle() const
{
return *web_rust_crypto_key_handle(this);
diff --git a/Libraries/LibWeb/Crypto/SubtleCrypto.cpp b/Libraries/LibWeb/Crypto/SubtleCrypto.cpp
index 7e2198074c7..77ebdd7bc7c 100644
--- a/Libraries/LibWeb/Crypto/SubtleCrypto.cpp
+++ b/Libraries/LibWeb/Crypto/SubtleCrypto.cpp
@@ -16,18 +16,12 @@
namespace Web::Crypto {
enum class AlgorithmId : u8 {
- X25519,
- X448,
- ED25519,
- ED448,
MLDSA,
MLKEM,
};
enum class ParameterId : u8 {
AlgorithmParams,
- EcdhKeyDeriveParams,
- Ed448Params,
ContextParams,
};
@@ -197,14 +191,6 @@ JSCompletion web_crypto_algorithm_from_value(JS::Realm* realm_pointer, Algorithm
auto& realm = *realm_pointer;
auto methods = [&]() -> NonnullOwnPtr<AlgorithmMethods> {
switch (method_id) {
- case AlgorithmId::X25519:
- return X25519::create();
- case AlgorithmId::X448:
- return X448::create();
- case AlgorithmId::ED25519:
- return ED25519::create();
- case AlgorithmId::ED448:
- return ED448::create();
case AlgorithmId::MLDSA:
return MLDSA::create();
case AlgorithmId::MLKEM:
@@ -217,10 +203,6 @@ JSCompletion web_crypto_algorithm_from_value(JS::Realm* realm_pointer, Algorithm
switch (parameter_id) {
case ParameterId::AlgorithmParams:
return adopt_own(*new AlgorithmParams {});
- case ParameterId::EcdhKeyDeriveParams:
- return EcdhKeyDeriveParams::from_value(realm, object);
- case ParameterId::Ed448Params:
- return Ed448Params::from_value(realm, object);
case ParameterId::ContextParams:
return ContextParams::from_value(realm, object);
}
diff --git a/Meta/Linters/check_libweb_realm_mentions.py b/Meta/Linters/check_libweb_realm_mentions.py
index 9a0463e66aa..292f3191d33 100755
--- a/Meta/Linters/check_libweb_realm_mentions.py
+++ b/Meta/Linters/check_libweb_realm_mentions.py
@@ -46,7 +46,7 @@ ALLOWED_REALM_MENTIONS = {
"CookieStore": (2, 19, "cookie-store async callbacks and promise/value materialization still thread realms"),
"Crypto": (
5,
- 134,
+ 96,
"WebCrypto algorithms still use realms for buffer/key/promise materialization and for normalizing algorithm "
"dictionaries. The native CryptoKey wrapper "
"uses the caller's realm and wrapper world",