rsa-oaep-key-manipulation.html 3.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<script src="resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>

<script>
description("Test generating, importing and exporting RSA keys for RSA-OAEP. Test that they can't be used with another algorithm.");

jsTestIsAsync = true;

var algorithmKeyGen = {
    name: "RSA-OAEP",
    // RsaKeyGenParams
    modulusLength: 2048,
    publicExponent: new Uint8Array([0x01, 0x00, 0x01]),  // Equivalent to 65537
};
var extractable = true;

debug("Generating a key pair...");
crypto.subtle.generateKey(algorithmKeyGen, extractable, ["encrypt", "decrypt", "wrapKey", "unwrapKey"]).then(function(result) {
    keyPair = result;
    shouldBe("keyPair.toString()", "'[object KeyPair]'");
    shouldBe("keyPair.publicKey.type", "'public'");
29
    shouldBe("keyPair.publicKey.algorithm.name", "'RSA-OAEP'");
30 31 32 33
    shouldBe("keyPair.publicKey.algorithm.modulusLength", "2048");
    shouldBe("bytesToHexString(keyPair.publicKey.algorithm.publicExponent)", "'010001'");
    shouldBeUndefined("keyPair.publicKey.algorithm.hash");
    shouldBe("keyPair.privateKey.type", "'private'");
34
    shouldBe("keyPair.privateKey.algorithm.name", "'RSA-OAEP'");
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    shouldBe("keyPair.privateKey.algorithm.modulusLength", "2048");
    shouldBe("bytesToHexString(keyPair.privateKey.algorithm.publicExponent)", "'010001'");
    shouldBeUndefined("keyPair.privateKey.algorithm.hash");

    debug("\nTesting that the keys can't be used with different algorithms...");
    iv = hexStringToUint8Array("000102030405060708090a0b0c0d0e0f");
    shouldThrow('crypto.subtle.encrypt({name: "aes-cbc", iv: iv}, keyPair.privateKey, hexStringToUint8Array("00"))');
    shouldThrow('crypto.subtle.encrypt({name: "aes-cbc", iv: iv}, keyPair.publicKey, hexStringToUint8Array("00"))');

    debug("\nTrying to export keys to raw...");
    return crypto.subtle.exportKey('raw', keyPair.publicKey);
}).then(undefined, function(result) {
    testPassed("Promise rejected for exporting public key");
    return crypto.subtle.exportKey('raw', keyPair.privateKey);
}).then(undefined, function(result) {
    testPassed("Promise rejected for exporting private key");

    debug("\nExporting public key to JWK...");
    return crypto.subtle.exportKey("jwk", keyPair.publicKey);
}).then(function(result) {
    jwkPublicKeyArray = result;
    jwkPublicKey = JSON.parse(bytesToASCIIString(jwkPublicKeyArray));
    shouldBe("jwkPublicKey.alg", "'RSA-OAEP'");
58
    shouldBe("jwkPublicKey.ext", "true");
59 60 61 62 63 64 65 66 67
    shouldBe("jwkPublicKey.use", "'enc'");
    shouldBe("jwkPublicKey.kty", "'RSA'");
    shouldBe("bytesToHexString(Base64URL.parse(jwkPublicKey.e))", "'010001'");

    debug("\nImporting it back...");
    return crypto.subtle.importKey("jwk", jwkPublicKeyArray, null, extractable, ["encrypt", "decrypt", "wrapKey", "unwrapKey"]);
}).then(function(result) {
    exportedPublicKey = result;
    shouldBe("exportedPublicKey.type", "'public'");
68
    shouldBe("exportedPublicKey.algorithm.name", "'RSA-OAEP'");
69 70
    shouldBe("exportedPublicKey.algorithm.modulusLength", "2048");
    shouldBe("bytesToHexString(exportedPublicKey.algorithm.publicExponent)", "'010001'");
71
    shouldBe("exportedPublicKey.algorithm.hash.name", "'SHA-1'");
72
    shouldBe("exportedPublicKey.extractable", "true");
73
    shouldBe("exportedPublicKey.usages", "['decrypt','encrypt','unwrapKey','wrapKey']");
74 75 76 77 78 79 80 81

    finishJSTest();
});
</script>

<script src="../../resources/js-test-post.js"></script>
</body>
</html>