-
-
Notifications
You must be signed in to change notification settings - Fork 36.1k
Expand file tree
/
Copy pathgetPublicKey.tentative.https.any.js
More file actions
290 lines (256 loc) Β· 9.48 KB
/
Copy pathgetPublicKey.tentative.https.any.js
File metadata and controls
290 lines (256 loc) Β· 9.48 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// META: title=WebCryptoAPI: getPublicKey() method
// META: timeout=long
// META: script=util/helpers.js
"use strict";
const algorithms = [
{
name: "ECDH",
generateKeyParams: { name: "ECDH", namedCurve: "P-256" },
usages: ["deriveKey", "deriveBits"],
publicKeyUsages: []
},
{
name: "ECDSA",
generateKeyParams: { name: "ECDSA", namedCurve: "P-256" },
usages: ["sign", "verify"],
publicKeyUsages: ["verify"]
},
{
name: "Ed25519",
generateKeyParams: { name: "Ed25519" },
usages: ["sign", "verify"],
publicKeyUsages: ["verify"]
},
{
name: "RSA-OAEP",
generateKeyParams: {
name: "RSA-OAEP",
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256"
},
usages: ["encrypt", "decrypt"],
publicKeyUsages: ["encrypt"]
},
{
name: "RSA-PSS",
generateKeyParams: {
name: "RSA-PSS",
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256"
},
usages: ["sign", "verify"],
publicKeyUsages: ["verify"]
},
{
name: "RSASSA-PKCS1-v1_5",
generateKeyParams: {
name: "RSASSA-PKCS1-v1_5",
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256"
},
usages: ["sign", "verify"],
publicKeyUsages: ["verify"]
},
{
name: "X25519",
generateKeyParams: { name: "X25519" },
usages: ["deriveKey", "deriveBits"],
publicKeyUsages: []
},
{
name: "ML-DSA-44",
generateKeyParams: { name: "ML-DSA-44" },
usages: ["sign", "verify"],
publicKeyUsages: ["verify"]
},
{
name: "ML-DSA-65",
generateKeyParams: { name: "ML-DSA-65" },
usages: ["sign", "verify"],
publicKeyUsages: ["verify"]
},
{
name: "ML-DSA-87",
generateKeyParams: { name: "ML-DSA-87" },
usages: ["sign", "verify"],
publicKeyUsages: ["verify"]
},
{
name: "ML-KEM-512",
generateKeyParams: { name: "ML-KEM-512" },
usages: ["encapsulateBits", "encapsulateKey", "decapsulateBits", "decapsulateKey"],
publicKeyUsages: ["encapsulateBits", "encapsulateKey"]
},
{
name: "ML-KEM-768",
generateKeyParams: { name: "ML-KEM-768" },
usages: ["encapsulateBits", "encapsulateKey", "decapsulateBits", "decapsulateKey"],
publicKeyUsages: ["encapsulateBits", "encapsulateKey"]
},
{
name: "ML-KEM-1024",
generateKeyParams: { name: "ML-KEM-1024" },
usages: ["encapsulateBits", "encapsulateKey", "decapsulateBits", "decapsulateKey"],
publicKeyUsages: ["encapsulateBits", "encapsulateKey"]
}
];
// Test basic functionality for supported algorithms
algorithms.forEach(function(algorithm) {
promise_test(async function(t) {
// Generate a key pair
const keyPair = await crypto.subtle.generateKey(
algorithm.generateKeyParams,
false, // extractable
algorithm.usages
);
assert_true(keyPair.privateKey instanceof CryptoKey, "Generated private key");
assert_equals(keyPair.privateKey.type, "private", "Private key type");
// Test getPublicKey with valid usages
const publicKey = await crypto.subtle.getPublicKey(
keyPair.privateKey,
algorithm.publicKeyUsages
);
// Verify the returned public key
assert_true(publicKey instanceof CryptoKey, "getPublicKey returns a CryptoKey");
assert_equals(publicKey.type, "public", "Returned key is public");
assert_equals(publicKey.algorithm.name, algorithm.name, "Algorithm name matches");
assert_true(publicKey.extractable, "Public key is extractable");
// Verify usages
assert_equals(publicKey.usages.length, algorithm.publicKeyUsages.length, "Usage count matches");
algorithm.publicKeyUsages.forEach(function(usage) {
assert_true(publicKey.usages.includes(usage), `Has ${usage} usage`);
});
// Verify that the derived public key matches the original public key
// by comparing their exported forms
const originalExported = await crypto.subtle.exportKey("spki", keyPair.publicKey);
const derivedExported = await crypto.subtle.exportKey("spki", publicKey);
assert_array_equals(
new Uint8Array(originalExported),
new Uint8Array(derivedExported),
"Exported public keys match"
);
}, `getPublicKey works for ${algorithm.name}`);
});
// Test functional equivalence - ensure derived public key works for crypto operations
promise_test(async function(t) {
const keyPair = await crypto.subtle.generateKey(
{ name: "ECDSA", namedCurve: "P-256" },
false,
["sign", "verify"]
);
const derivedPublicKey = await crypto.subtle.getPublicKey(
keyPair.privateKey,
["verify"]
);
// Create test data
const data = new TextEncoder().encode("test message");
// Sign with private key
const signature = await crypto.subtle.sign(
{ name: "ECDSA", hash: "SHA-256" },
keyPair.privateKey,
data
);
// Verify with both original and derived public keys
const verifyOriginal = await crypto.subtle.verify(
{ name: "ECDSA", hash: "SHA-256" },
keyPair.publicKey,
signature,
data
);
const verifyDerived = await crypto.subtle.verify(
{ name: "ECDSA", hash: "SHA-256" },
derivedPublicKey,
signature,
data
);
assert_true(verifyOriginal, "Original public key verifies signature");
assert_true(verifyDerived, "Derived public key verifies signature");
}, "Derived public key is functionally equivalent to original public key");
// Test with empty usages array
algorithms.forEach(function(algorithm) {
promise_test(async function(t) {
const keyPair = await crypto.subtle.generateKey(
algorithm.generateKeyParams,
false,
algorithm.usages
);
// Test with empty usages array
const publicKey = await crypto.subtle.getPublicKey(
keyPair.privateKey,
[]
);
assert_true(publicKey instanceof CryptoKey, "getPublicKey returns a CryptoKey");
assert_equals(publicKey.type, "public", "Returned key is public");
assert_equals(publicKey.usages.length, 0, "Public key has no usages");
}, `getPublicKey works with empty usages for ${algorithm.name}`);
});
// Test error cases
// Test with non-private key (should throw InvalidAccessError)
promise_test(async function(t) {
const keyPair = await crypto.subtle.generateKey(
{ name: "ECDSA", namedCurve: "P-256" },
false,
["sign", "verify"]
);
await promise_rejects_dom(t, "InvalidAccessError",
crypto.subtle.getPublicKey(keyPair.publicKey, ["verify"]),
"getPublicKey should reject when called with a public key"
);
}, "getPublicKey rejects with InvalidAccessError when given a public key");
// Test with symmetric keys (should throw NotSupportedError for non-private keys)
promise_test(async function(t) {
const aesKey = await crypto.subtle.generateKey(
{ name: "AES-GCM", length: 256 },
false,
["encrypt", "decrypt"]
);
await promise_rejects_dom(t, "NotSupportedError",
crypto.subtle.getPublicKey(aesKey, []),
"getPublicKey should reject AES-GCM keys"
);
}, "getPublicKey rejects with NotSupportedError for AES-GCM symmetric keys");
promise_test(async function(t) {
const hmacKey = await crypto.subtle.generateKey(
{ name: "HMAC", hash: "SHA-256" },
false,
["sign", "verify"]
);
await promise_rejects_dom(t, "NotSupportedError",
crypto.subtle.getPublicKey(hmacKey, []),
"getPublicKey should reject HMAC keys"
);
}, "getPublicKey rejects with NotSupportedError for HMAC symmetric keys");
// Test with invalid usages for the algorithm
promise_test(async function(t) {
const keyPair = await crypto.subtle.generateKey(
{ name: "ECDSA", namedCurve: "P-256" },
false,
["sign", "verify"]
);
// Try to use "encrypt" usage with ECDSA (not supported for ECDSA public keys)
await promise_rejects_dom(t, "SyntaxError",
crypto.subtle.getPublicKey(keyPair.privateKey, ["encrypt"]),
"getPublicKey should reject invalid usages for the algorithm"
);
}, "getPublicKey rejects with SyntaxError for invalid usages");
// Test with mixed valid and invalid usages
promise_test(async function(t) {
const keyPair = await crypto.subtle.generateKey(
{ name: "ECDSA", namedCurve: "P-256" },
false,
["sign", "verify"]
);
// Mix valid ("verify") and invalid ("encrypt") usages
await promise_rejects_dom(t, "SyntaxError",
crypto.subtle.getPublicKey(keyPair.privateKey, ["verify", "encrypt"]),
"getPublicKey should reject when any usage is invalid"
);
}, "getPublicKey rejects with SyntaxError when any usage is invalid for the algorithm");
// Test that the method exists
test(function() {
assert_true("getPublicKey" in crypto.subtle, "getPublicKey method exists on SubtleCrypto");
assert_equals(typeof crypto.subtle.getPublicKey, "function", "getPublicKey is a function");
}, "getPublicKey method is available");