Truly independent web browser
LibWeb: Leave out the CRT members of an RSA JWK without them
Shannon Boothd0c9e53500b0
diff --git a/Libraries/LibCrypto/Rust/src/rsa.rs b/Libraries/LibCrypto/Rust/src/rsa.rsindex 8d60c2fd886..81c3f3fb752 100644--- a/Libraries/LibCrypto/Rust/src/rsa.rs+++ b/Libraries/LibCrypto/Rust/src/rsa.rs@@ -252,17 +252,27 @@ pub fn rsa_export_der(key: &KeyHandle, is_public: bool) -> Result<Vec<u8>, Crypt))}-pub fn rsa_export_jwk_integer(key: &KeyHandle, component: RsaJwkInteger) -> Result<Vec<u8>, CryptoError> {- match &key.0 {- Key::RsaPublic(public_key) => match component {- RsaJwkInteger::Modulus | RsaJwkInteger::PublicExponent => {- Ok(integer(&public_key.key, component)?.into_vec())- }- _ => panic!("A public key has only n and e"),- },- Key::RsaPrivate(private_key) => Ok(private_key.component(component)?.into_vec()),+/// The integer of an RSA key, or none for a CRT value of a private key that has none.+pub fn rsa_export_jwk_integer(key: &KeyHandle, component: RsaJwkInteger) -> Result<Option<Vec<u8>>, CryptoError> {+ let (key, has_crt_components) = match &key.0 {+ Key::RsaPublic(public_key) => {+ assert!(+ matches!(component, RsaJwkInteger::Modulus | RsaJwkInteger::PublicExponent),+ "A public key has only n and e"+ );+ (&public_key.key, false)+ }+ Key::RsaPrivate(private_key) => (&private_key.key, private_key.has_crt_components),_ => panic!("The key is not an RSA key"),+ };+ let is_crt_component = !matches!(+ component,+ RsaJwkInteger::Modulus | RsaJwkInteger::PublicExponent | RsaJwkInteger::PrivateExponent+ );+ if is_crt_component && !has_crt_components {+ return Ok(None);}+ Ok(Some(integer(key, component)?.into_vec()))}pub fn rsa_oaep_cipher(diff --git a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rsindex 650205719b9..66e03bdfe7c 100644--- a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs@@ -5549,9 +5549,9 @@ fn rsa_export_der(key: Ref<CryptoKey>, is_public: bool) -> WebIdlResult<Bytes> {.map_err(operation_error)}-fn rsa_export_jwk_integer(key: Ref<CryptoKey>, component: RsaJwkInteger) -> WebIdlResult<Utf16String> {+fn rsa_export_jwk_integer(key: Ref<CryptoKey>, component: RsaJwkInteger) -> WebIdlResult<Option<Utf16String>> {let bytes = libcrypto_rust::rsa_export_jwk_integer(key.handle(), component).map_err(operation_error)?;- Ok(base64_url_bytes_encode(&bytes))+ Ok(bytes.map(|bytes| base64_url_bytes_encode(&bytes)))}fn key_format_to_string(format: KeyFormat) -> &'static str {@@ -6226,18 +6226,18 @@ impl AlgorithmMethods for RsaOaep {// 5. Set the attributes n and e of jwk according to the corresponding definitions in JSON Web Algorithms// [JWA], Section 6.3.1.- jwk.n = Some(rsa_export_jwk_integer(key, RsaJwkInteger::Modulus)?);- jwk.e = Some(rsa_export_jwk_integer(key, RsaJwkInteger::PublicExponent)?);+ jwk.n = rsa_export_jwk_integer(key, RsaJwkInteger::Modulus)?;+ jwk.e = rsa_export_jwk_integer(key, RsaJwkInteger::PublicExponent)?;// 6. If the [[type]] internal slot of key is "private":// 1. Set the attributes named d, p, q, dp, dq, and qi of jwk according to the corresponding definitions in JSON Web Algorithms [JWA], Section 6.3.2.if key.key_type() == KeyType::Private {- jwk.d = Some(rsa_export_jwk_integer(key, RsaJwkInteger::PrivateExponent)?);- jwk.p = Some(rsa_export_jwk_integer(key, RsaJwkInteger::Prime1)?);- jwk.q = Some(rsa_export_jwk_integer(key, RsaJwkInteger::Prime2)?);- jwk.dp = Some(rsa_export_jwk_integer(key, RsaJwkInteger::Exponent1)?);- jwk.dq = Some(rsa_export_jwk_integer(key, RsaJwkInteger::Exponent2)?);- jwk.qi = Some(rsa_export_jwk_integer(key, RsaJwkInteger::Coefficient)?);+ jwk.d = rsa_export_jwk_integer(key, RsaJwkInteger::PrivateExponent)?;+ jwk.p = rsa_export_jwk_integer(key, RsaJwkInteger::Prime1)?;+ jwk.q = rsa_export_jwk_integer(key, RsaJwkInteger::Prime2)?;+ jwk.dp = rsa_export_jwk_integer(key, RsaJwkInteger::Exponent1)?;+ jwk.dq = rsa_export_jwk_integer(key, RsaJwkInteger::Exponent2)?;+ jwk.qi = rsa_export_jwk_integer(key, RsaJwkInteger::Coefficient)?;// 2. If the underlying RSA private key represented by the [[handle]] internal slot of key is// represented by more than two primes, set the attribute named oth of jwk according to the@@ -6939,19 +6939,19 @@ impl AlgorithmMethods for RsaPss {}// 5. Set the attributes n and e of jwk according to the corresponding definitions in JSON Web Algorithms [JWA], Section 6.3.1.- jwk.n = Some(rsa_export_jwk_integer(key, RsaJwkInteger::Modulus)?);- jwk.e = Some(rsa_export_jwk_integer(key, RsaJwkInteger::PublicExponent)?);+ jwk.n = rsa_export_jwk_integer(key, RsaJwkInteger::Modulus)?;+ jwk.e = rsa_export_jwk_integer(key, RsaJwkInteger::PublicExponent)?;// 6. If the [[type]] internal slot of key is "private":if key.key_type() == KeyType::Private {// 1. Set the attributes named d, p, q, dp, dq, and qi of jwk according to the corresponding definitions// in JSON Web Algorithms [JWA], Section 6.3.2.- jwk.d = Some(rsa_export_jwk_integer(key, RsaJwkInteger::PrivateExponent)?);- jwk.p = Some(rsa_export_jwk_integer(key, RsaJwkInteger::Prime1)?);- jwk.q = Some(rsa_export_jwk_integer(key, RsaJwkInteger::Prime2)?);- jwk.dp = Some(rsa_export_jwk_integer(key, RsaJwkInteger::Exponent1)?);- jwk.dq = Some(rsa_export_jwk_integer(key, RsaJwkInteger::Exponent2)?);- jwk.qi = Some(rsa_export_jwk_integer(key, RsaJwkInteger::Coefficient)?);+ jwk.d = rsa_export_jwk_integer(key, RsaJwkInteger::PrivateExponent)?;+ jwk.p = rsa_export_jwk_integer(key, RsaJwkInteger::Prime1)?;+ jwk.q = rsa_export_jwk_integer(key, RsaJwkInteger::Prime2)?;+ jwk.dp = rsa_export_jwk_integer(key, RsaJwkInteger::Exponent1)?;+ jwk.dq = rsa_export_jwk_integer(key, RsaJwkInteger::Exponent2)?;+ jwk.qi = rsa_export_jwk_integer(key, RsaJwkInteger::Coefficient)?;// 2. If the underlying RSA private key represented by the [[handle]] internal slot of key is represented by more than two primes,// set the attribute named oth of jwk according to the corresponding definition in JSON Web Algorithms [JWA], Section 6.3.2.7@@ -7643,19 +7643,19 @@ impl AlgorithmMethods for RsassaPkcs1 {}// 5. Set the attributes n and e of jwk according to the corresponding definitions in JSON Web Algorithms [JWA], Section 6.3.1.- jwk.n = Some(rsa_export_jwk_integer(key, RsaJwkInteger::Modulus)?);- jwk.e = Some(rsa_export_jwk_integer(key, RsaJwkInteger::PublicExponent)?);+ jwk.n = rsa_export_jwk_integer(key, RsaJwkInteger::Modulus)?;+ jwk.e = rsa_export_jwk_integer(key, RsaJwkInteger::PublicExponent)?;// 6. If the [[type]] internal slot of key is "private":if key.key_type() == KeyType::Private {// 1. Set the attributes named d, p, q, dp, dq, and qi of jwk according to the corresponding definitions// in JSON Web Algorithms [JWA], Section 6.3.2.- jwk.d = Some(rsa_export_jwk_integer(key, RsaJwkInteger::PrivateExponent)?);- jwk.p = Some(rsa_export_jwk_integer(key, RsaJwkInteger::Prime1)?);- jwk.q = Some(rsa_export_jwk_integer(key, RsaJwkInteger::Prime2)?);- jwk.dp = Some(rsa_export_jwk_integer(key, RsaJwkInteger::Exponent1)?);- jwk.dq = Some(rsa_export_jwk_integer(key, RsaJwkInteger::Exponent2)?);- jwk.qi = Some(rsa_export_jwk_integer(key, RsaJwkInteger::Coefficient)?);+ jwk.d = rsa_export_jwk_integer(key, RsaJwkInteger::PrivateExponent)?;+ jwk.p = rsa_export_jwk_integer(key, RsaJwkInteger::Prime1)?;+ jwk.q = rsa_export_jwk_integer(key, RsaJwkInteger::Prime2)?;+ jwk.dp = rsa_export_jwk_integer(key, RsaJwkInteger::Exponent1)?;+ jwk.dq = rsa_export_jwk_integer(key, RsaJwkInteger::Exponent2)?;+ jwk.qi = rsa_export_jwk_integer(key, RsaJwkInteger::Coefficient)?;// 2. If the underlying RSA private key represented by the [[handle]] internal slot of key is represented by more than two primes,// set the attribute named oth of jwk according to the corresponding definition in JSON Web Algorithms [JWA], Section 6.3.2.7diff --git a/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-RSA-export-jwk-without-primes.txt b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-RSA-export-jwk-without-primes.txtnew file mode 100644index 00000000000..f5d18674ade--- /dev/null+++ b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-RSA-export-jwk-without-primes.txt@@ -0,0 +1,3 @@+RSASSA-PKCS1-v1_5: n, e, d+RSA-PSS: n, e, d+RSA-OAEP: n, e, ddiff --git a/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-RSA-export-jwk-without-primes.html b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-RSA-export-jwk-without-primes.htmlnew file mode 100644index 00000000000..be46d29e28c--- /dev/null+++ b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-RSA-export-jwk-without-primes.html@@ -0,0 +1,23 @@+<!doctype html>+<script src="../include.js"></script>+<script>+ promiseTest(async () => {+ for (const [name, usages, privateUsage] of [+ ["RSASSA-PKCS1-v1_5", ["sign", "verify"], "sign"],+ ["RSA-PSS", ["sign", "verify"], "sign"],+ ["RSA-OAEP", ["encrypt", "decrypt"], "decrypt"],+ ]) {+ const algorithm = { name, hash: "SHA-256" };+ const { privateKey } = await crypto.subtle.generateKey(+ { ...algorithm, modulusLength: 2048, publicExponent: new Uint8Array([1, 0, 1]) },+ true,+ usages+ );+ const { kty, n, e, d } = await crypto.subtle.exportKey("jwk", privateKey);+ const key = await crypto.subtle.importKey("jwk", { kty, n, e, d }, algorithm, true, [privateUsage]);+ const jwk = await crypto.subtle.exportKey("jwk", key);+ const members = ["n", "e", "d", "p", "q", "dp", "dq", "qi"].filter(member => member in jwk);+ println(`${name}: ${members.join(", ")}`);+ }+ });+</script>