rsa-export-key.html 1.8 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
<!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 exporting an RSA key.");

jsTestIsAsync = true;

var extractable = true;
var nonExtractable = false;

var publicKeyJSON = {
    kty: "RSA",
    alg: "RS256",
    n: "rcCUCv7Oc1HVam1DIhCzqknThWawOp8QLk8Ziy2p10ByjQFCajoFiyuAWl-R1WXZaf4xitLRracT9agpzIzc-MbLSHIGgWQGO21lGiImy5ftZ-D8bHAqRz2y15pzD4c4CEou7XSSLDoRnR0QG5MsDhD6s2gV9mwHkrtkCxtMWdBi-77as8wGmlNRldcOSgZDLK8UnCSgA1OguZ989bFyc8tOOEIb0xUSfPSz3LPSCnyYz68aDjmKVeNH-ig857OScyWbGyEy3Biw64qun3juUlNWsJ3zngkOdteYWytx5Qr4XKNs6R-Myyq72KUp02mJDZiiyiglxML_i3-_CeecCw",
    e: "AQAB"
};

var jwkKeyAsArrayBuffer = asciiToUint8Array(JSON.stringify(publicKeyJSON));

debug("\nImporting a JWK key...");
crypto.subtle.importKey("jwk", jwkKeyAsArrayBuffer, "RSASSA-PKCS1-v1_5", extractable, ['sign', 'verify']).then(function(result) {
    key = result;

    shouldThrow('crypto.subtle.exportKey(null, key)');
    shouldThrow('crypto.subtle.exportKey(undefined, key)');
    shouldThrow('crypto.subtle.exportKey({}, key)');
    shouldThrow('crypto.subtle.exportKey("", key)');
    shouldThrow('crypto.subtle.exportKey("foobar", key)');

    debug("\nExporting the key as JWK...");
    return crypto.subtle.exportKey("jwk", key);
}).then(function(result) {
    exportedJWK = JSON.parse(bytesToASCIIString(result));

    shouldBe("exportedJWK.kty", "'RSA'");
    shouldBe("exportedJWK.n", "publicKeyJSON.n");
    shouldBe("exportedJWK.e", "publicKeyJSON.e");
    shouldBe("exportedJWK.alg", "'RS256'");
47
    shouldBe("exportedJWK.ext", "true");
48 49 50 51 52 53 54 55 56
    shouldBe("exportedJWK.use", "'sig'");

    finishJSTest();
});
</script>

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