summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/crypto/importKey.html
blob: 9d6faa8a14af9a9f7a94c2b00929bd441e7590ff (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!DOCTYPE html>
<html>
<head>
<script src="../resources/js-test.js"></script>
<script src="resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>

<script>
description("Tests cypto.subtle.importKey.");

jsTestIsAsync = true;

aesCbc = {name: 'aes-cbc'};

Promise.resolve(null).then(function() {
    keyFormat = "raw";
    data = asciiToUint8Array("raw bytes for key");
    algorithm = { name: 'hmac', hash: { name: 'sha-256' } };
    extractable = true;
    // Note there are duplicates
    keyUsages = ['encrypt', 'encrypt', 'encrypt', 'sign'];

    return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages);
}).then(function(result) {
    key = result;
    shouldBe("key.type", "'secret'")
    shouldBe("key.extractable", "true")
    shouldBe("key.algorithm.name", "'HMAC'")
    shouldBe("key.usages.join(',')", "'encrypt,sign'")

    // Same test as above, but with an keyUsages, and AES-CBC.
    keyFormat = "raw";
    data = asciiToUint8Array("16 bytes of key!");
    algorithm = aesCbc;
    extractable = true;
    keyUsages = [];

    return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages);
}).then(function(result) {
    key = result;
    shouldBe("key.type", "'secret'")
    shouldBe("key.extractable", "true")
    shouldBe("key.algorithm.name", "'AES-CBC'")
    shouldBe("key.usages.join(',')", "''")

    // Same test as above, but with extractable = false.
    keyFormat = "raw";
    data = asciiToUint8Array("16 bytes of key!");
    algorithm = aesCbc;
    extractable = false;
    keyUsages = [];

    return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages);
}).then(function(result) {
    key = result;
    shouldBe("key.type", "'secret'")
    shouldBe("key.extractable", "false")
    shouldBe("key.algorithm.name", "'AES-CBC'")
    shouldBe("key.usages.join(',')", "''")

    // Same test as above, but with keyFormat = spki
    keyFormat = "spki";
    data = asciiToUint8Array("16 bytes of key!");
    algorithm = aesCbc;
    extractable = false;
    keyUsages = [];

    return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages);
}).then(undefined, function(result) {
    // TODO(eroman): Only "raw" key format is supported at the moment.
    debug("rejected with " + result);

    // Import a "raw" key without specifying the algorithm.
    keyFormat = "raw";
    data = asciiToUint8Array("16 bytes of key!");
    algorithm = null;
    extractable = false;
    keyUsages = [];

    return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages);
}).then(undefined, function(result) {
    debug("rejected with " + result);

    keyFormat = "raw";
    data = asciiToUint8Array("");
    algorithm = aesCbc;
    extractable = true;
    keyUsages = [];

    // Invalid format.
    shouldThrow("crypto.subtle.importKey('invalid format', data, algorithm, extractable, keyUsages)");

    // Invalid key usage.
    shouldThrow("crypto.subtle.importKey(keyFormat, data, algorithm, extractable, ['SIGN'])");

    // If both the format and key usage are bogus, should complain about the
    // format first.
    shouldThrow("crypto.subtle.importKey('invalid format', data, algorithm, extractable, ['SIGN'])");

    // Undefined key usage.
    // FIXME: http://crbug.com/262383
    //shouldThrow("crypto.subtle.importKey(keyFormat, data, algorithm, extractable, undefined)");

    // Invalid data
    shouldThrow("crypto.subtle.importKey(keyFormat, [], algorithm, extractable, keyUsages)");
    shouldThrow("crypto.subtle.importKey(keyFormat, null, algorithm, extractable, keyUsages)");

    // Missing hash parameter for HMAC.
    shouldRejectPromiseWithNull("crypto.subtle.importKey(keyFormat, data, {name: 'hmac'}, extractable, keyUsages)");

    // SHA-1 doesn't support the importKey operation.
    shouldThrow("crypto.subtle.importKey(keyFormat, data, {name: 'sha-1'}, extractable, keyUsages)");
}).then(finishJSTest, failAndFinishJSTest);

</script>

</body>
</html>