Skip to content

Truly independent web browser

LibCrypto: Stop checking an RSA key at its first failed check

Shannon Boothe2b1601a050d
diff --git a/Libraries/LibCrypto/Rust/src/ec.rs b/Libraries/LibCrypto/Rust/src/ec.rs
index a74668a1f0a..7d61c7f182b 100644
--- a/Libraries/LibCrypto/Rust/src/ec.rs
+++ b/Libraries/LibCrypto/Rust/src/ec.rs
@@ -124,7 +124,7 @@ impl EcPublicKey {
end_parameter(),
];
let key = Pkey::from_data(c"EC", sys::EVP_PKEY_PUBLIC_KEY, &params).map_err(invalid)?;
- if !openssl::check_key(&key, false, false).map_err(invalid)?.public {
+ if !openssl::check_key(&key, false, false).map_err(invalid)? {
return Err(invalid(CryptoError::Backend));
}
Ok(Self { key, curve })
diff --git a/Libraries/LibCrypto/Rust/src/openssl.rs b/Libraries/LibCrypto/Rust/src/openssl.rs
index 0b78bc43a93..5c1aa7d998c 100644
--- a/Libraries/LibCrypto/Rust/src/openssl.rs
+++ b/Libraries/LibCrypto/Rust/src/openssl.rs
@@ -462,27 +462,21 @@ impl Pkey {
}
}
-pub(crate) struct KeyChecks {
- pub(crate) public: bool,
- pub(crate) private: bool,
- pub(crate) pairwise: bool,
-}
-
/// Runs OpenSSL's public, private and pairwise checks of `key`, the last two only when asked for.
-pub(crate) fn check_key(key: &Pkey, private: bool, pairwise: bool) -> Result<KeyChecks, CryptoError> {
+pub(crate) fn check_key(key: &Pkey, private: bool, pairwise: bool) -> Result<bool, CryptoError> {
let context = key.context()?;
+ // NB: OpenSSL's pairwise check of RSA factors is unbounded, so it must only run on a key whose modulus passed the
+ // public check.
// SAFETY: The context is live for each call.
- let checks = unsafe {
- KeyChecks {
- public: sys::EVP_PKEY_public_check(context.as_ptr()) == 1,
- private: !private || sys::EVP_PKEY_private_check(context.as_ptr()) == 1,
- pairwise: !pairwise || sys::EVP_PKEY_pairwise_check(context.as_ptr()) == 1,
- }
+ let valid = unsafe {
+ sys::EVP_PKEY_public_check(context.as_ptr()) == 1
+ && (!private || sys::EVP_PKEY_private_check(context.as_ptr()) == 1)
+ && (!pairwise || sys::EVP_PKEY_pairwise_check(context.as_ptr()) == 1)
};
// NB: A failed check leaves its reasons queued.
// SAFETY: Clearing the error queue takes no arguments.
unsafe { sys::ERR_clear_error() };
- Ok(checks)
+ Ok(valid)
}
/// Signs `message` with `key`, hashing it with the digest `digest_name` unless the algorithm takes none. The parameters
diff --git a/Libraries/LibCrypto/Rust/src/rsa.rs b/Libraries/LibCrypto/Rust/src/rsa.rs
index 2b8d218430b..4bcdd80f061 100644
--- a/Libraries/LibCrypto/Rust/src/rsa.rs
+++ b/Libraries/LibCrypto/Rust/src/rsa.rs
@@ -75,7 +75,7 @@ impl RsaPublicKey {
if !public_exponent_is_valid(&self.key) {
return Ok(false);
}
- Ok(openssl::check_key(&self.key, false, false)?.public)
+ openssl::check_key(&self.key, false, false)
}
}
@@ -140,8 +140,7 @@ impl RsaPrivateKey {
return Ok(false);
}
// NB: The pairwise check needs the prime factors, which a key of only n, e and d does not have.
- let checks = openssl::check_key(&self.key, true, self.has_crt_components)?;
- Ok(checks.public && checks.private && checks.pairwise)
+ openssl::check_key(&self.key, true, self.has_crt_components)
}
}
diff --git a/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-RSA-import-oversized-factors.txt b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-RSA-import-oversized-factors.txt
new file mode 100644
index 00000000000..a3a68b80081
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-RSA-import-oversized-factors.txt
@@ -0,0 +1 @@
+DataError
diff --git a/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-RSA-import-oversized-factors.html b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-RSA-import-oversized-factors.html
new file mode 100644
index 00000000000..1a6ae9b5e7b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-RSA-import-oversized-factors.html
@@ -0,0 +1,57 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ const base64url = value => {
+ let hex = value.toString(16);
+ if (hex.length % 2) {
+ hex = "0" + hex;
+ }
+ return btoa(String.fromCharCode(...hex.match(/../g).map(byte => parseInt(byte, 16))))
+ .replace(/\+/g, "-")
+ .replace(/\//g, "_")
+ .replace(/=+$/, "");
+ };
+
+ // A random odd 65536-bit number with no factor below 1000, so that testing it for primality takes minutes.
+ const hasSmallFactor = value => {
+ for (let divisor = 3n; divisor < 1000n; divisor += 2n) {
+ if (value % divisor === 0n) {
+ return true;
+ }
+ }
+ return false;
+ };
+ const largeFactor = () => {
+ for (;;) {
+ const bytes = crypto.getRandomValues(new Uint8Array(8192));
+ bytes[0] |= 0x80;
+ bytes[bytes.length - 1] |= 1;
+ const value = BigInt("0x" + Array.from(bytes, byte => byte.toString(16).padStart(2, "0")).join(""));
+ if (!hasSmallFactor(value)) {
+ return value;
+ }
+ }
+ };
+
+ promiseTest(async () => {
+ const p = largeFactor();
+ const q = largeFactor();
+ const jwk = {
+ kty: "RSA",
+ n: base64url(p * q),
+ e: "AQAB",
+ d: "AQ",
+ p: base64url(p),
+ q: base64url(q),
+ dp: "AQ",
+ dq: "AQ",
+ qi: "AQ",
+ };
+ try {
+ await crypto.subtle.importKey("jwk", jwk, { name: "RSA-OAEP", hash: "SHA-256" }, false, ["decrypt"]);
+ println("imported");
+ } catch (error) {
+ println(error.name);
+ }
+ });
+</script>