1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:57:45 +00:00

LibWeb: Support SubtleCrypto.exportKey for RSA-OAEP in JsonWebKey format

This commit is contained in:
Andrew Kaster 2024-03-13 21:19:57 -06:00 committed by Andrew Kaster
parent 2599142214
commit 1521a60a67
13 changed files with 367 additions and 6 deletions

View file

@ -0,0 +1,52 @@
<script src="../include.js"></script>
<script>
function bufferToHex(buffer) {
return [...new Uint8Array(buffer)].map(b => b.toString(16).padStart(2, "0")).join("");
}
asyncTest(async done => {
let algorithm = {
name: "RSA-OAEP",
modulusLength: 512,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256",
};
println("exportKey with RSA-OAEP algorithm");
let key = await window.crypto.subtle.generateKey(algorithm, true, [
"encrypt",
"decrypt",
"wrapKey",
"unwrapKey",
]);
// FIXME: Create a roundtrip test that starts with a key object and imports it, then exports it.
let exportedPublicKey = await window.crypto.subtle.exportKey("jwk", key.publicKey);
println(`exportedPublicKey kwt: ${exportedPublicKey.kty}`);
println(`exportedPublicKey alg: ${exportedPublicKey.alg}`);
println(`exportedPublicKey exponent: ${exportedPublicKey.e}`);
println(`exportedPublicKey key_ops: ${exportedPublicKey.key_ops}`);
println(`exportedPublicKey ext: ${exportedPublicKey.ext}`);
let exportedPrivateKey = await window.crypto.subtle.exportKey("jwk", key.privateKey);
println(`exportedPrivateKey kwt: ${exportedPrivateKey.kty}`);
println(`exportedPrivateKey alg: ${exportedPrivateKey.alg}`);
println(`exportedPrivateKey exponent: ${exportedPrivateKey.e}`);
println(`exportedPrivateKey key_ops: ${exportedPrivateKey.key_ops}`);
println(`exportedPrivateKey ext: ${exportedPrivateKey.ext}`);
let exportedPublicKey2 = await window.crypto.subtle.exportKey("spki", key.publicKey);
println(`FIXME: exportedPublicKey2: ${bufferToHex(exportedPublicKey2)}`);
try {
let exportedPrivateKey2 = await window.crypto.subtle.exportKey("spki", key.privateKey);
println("FAIL: Shouldn't be able to export private key as spki");
} catch (e) {
println(`exportKey spki private key: ${e.name} ${e.message}`);
}
done();
});
</script>