jwk-export-use-values.html 2.02 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
<!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 keys with various usages to JWK.");

jsTestIsAsync = true;

var extractable = true;

var aesKeyAsArrayBuffer = Base64URL.parse("jnOw99oOZFLIEPMrgJB55WL46tJSLGt7");
var hmacKeyAsArrayBuffer = Base64URL.parse("ahjkn-_387fgnsibf23qsvahjkn-_387fgnsibf23qs");

function testWithAESCBC(usages, expectedUse)
{
    return crypto.subtle.importKey("raw", aesKeyAsArrayBuffer, "AES-CBC", extractable, usages).then(function(result) {
        return crypto.subtle.exportKey("jwk", result);
    }).then(function(result) {
        jwk = JSON.parse(bytesToASCIIString(result));
        debug(usages + ":");
        shouldBe("jwk.use", "'" + expectedUse + "'");
        debug("");
    });
}

function testWithHMAC(usages, expectedUse)
{
    return crypto.subtle.importKey("raw", hmacKeyAsArrayBuffer, {name: 'hmac', hash: {name: 'sha-256'}}, extractable, usages).then(function(result) {
        return crypto.subtle.exportKey("jwk", result);
    }).then(function(result) {
        jwk = JSON.parse(bytesToASCIIString(result));
        debug(usages + ":");
        shouldBe("jwk.use", "'" + expectedUse + "'");
        debug("");
    });
}

Promise.all([
    testWithAESCBC(["encrypt"], "enconly"),
    testWithAESCBC(["decrypt"], "deconly"),
    testWithAESCBC(["encrypt", "decrypt"], "enconly,deconly"),
    testWithAESCBC(["wrapKey"], "wrap"),
    testWithAESCBC(["unwrapKey"], "unwrap"),
    testWithAESCBC(["wrapKey", "unwrapKey"], "wrap,unwrap"),
    testWithAESCBC(["encrypt", "decrypt", "wrapKey"], "enconly,deconly,wrap"),
    testWithAESCBC(["encrypt", "decrypt", "wrapKey", "unwrapKey"], "enc"),
    testWithHMAC(["sign"], "sigonly"),
    testWithHMAC(["verify"], "vfyonly"),
    testWithHMAC(["sign", "verify"], "sig"),
]).then(function() { finishJSTest(); } );
</script>

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