forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-crypto-key-objects-raw.js
More file actions
573 lines (493 loc) · 20.9 KB
/
test-crypto-key-objects-raw.js
File metadata and controls
573 lines (493 loc) · 20.9 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const crypto = require('crypto');
const fixtures = require('../common/fixtures');
const { hasOpenSSL } = require('../common/crypto');
// EC: NIST and OpenSSL curve names are both recognized for raw-public and raw-private
{
const pubKeyObj = crypto.createPublicKey(
fixtures.readKey('ec_p256_public.pem', 'ascii'));
const privKeyObj = crypto.createPrivateKey(
fixtures.readKey('ec_p256_private.pem', 'ascii'));
const rawPub = pubKeyObj.export({ format: 'raw-public' });
const rawPriv = privKeyObj.export({ format: 'raw-private' });
for (const namedCurve of ['P-256', 'prime256v1']) {
const importedPub = crypto.createPublicKey({
key: rawPub, format: 'raw-public', asymmetricKeyType: 'ec', namedCurve,
});
assert.strictEqual(importedPub.equals(pubKeyObj), true);
const importedPriv = crypto.createPrivateKey({
key: rawPriv, format: 'raw-private', asymmetricKeyType: 'ec', namedCurve,
});
assert.strictEqual(importedPriv.equals(privKeyObj), true);
}
}
// Raw key imports do not support strings.
{
const pubKeyObj = crypto.createPublicKey(
fixtures.readKey('ed25519_public.pem', 'ascii'));
const privKeyObj = crypto.createPrivateKey(
fixtures.readKey('ed25519_private.pem', 'ascii'));
const rawPub = pubKeyObj.export({ format: 'raw-public' });
const rawPriv = privKeyObj.export({ format: 'raw-private' });
for (const encoding of ['hex', 'base64', 'utf8', 'latin1', 'ascii']) {
assert.throws(() => crypto.createPublicKey({
key: rawPub.toString(encoding),
encoding,
format: 'raw-public',
asymmetricKeyType: 'ed25519',
}), { code: 'ERR_INVALID_ARG_TYPE' });
assert.throws(() => crypto.createPrivateKey({
key: rawPriv.toString(encoding),
encoding,
format: 'raw-private',
asymmetricKeyType: 'ed25519',
}), { code: 'ERR_INVALID_ARG_TYPE' });
}
}
// Raw public keys cannot be imported as private keys.
{
const rawPublicKeys = [
['ec', 'ec_p256_public.pem', { namedCurve: 'P-256' }],
['ed25519', 'ed25519_public.pem'],
['x25519', 'x25519_public.pem'],
];
if (!process.features.openssl_is_boringssl) {
rawPublicKeys.push(
['ed448', 'ed448_public.pem'],
['x448', 'x448_public.pem'],
);
} else {
common.printSkipMessage('Skipping unsupported ed448/x448 test cases');
}
if (hasOpenSSL(3, 5)) {
rawPublicKeys.push(
['ml-dsa-44', 'ml_dsa_44_public.pem'],
['ml-kem-768', 'ml_kem_768_public.pem'],
);
}
if (hasOpenSSL(3, 5)) {
rawPublicKeys.push(
['slh-dsa-sha2-128f', 'slh_dsa_sha2_128f_public.pem'],
);
}
for (const [asymmetricKeyType, fixture, options = {}] of rawPublicKeys) {
const publicKey = crypto.createPublicKey(fixtures.readKey(fixture, 'ascii'));
assert.throws(() => crypto.createPrivateKey({
key: publicKey.export({ format: 'raw-public' }),
format: 'raw-public',
asymmetricKeyType,
...options,
}), { code: 'ERR_INVALID_ARG_VALUE' });
}
}
// Raw seed imports do not support strings.
if (hasOpenSSL(3, 5)) {
const privKeyObj = crypto.createPrivateKey(
fixtures.readKey('ml_dsa_44_private.pem', 'ascii'));
const rawSeed = privKeyObj.export({ format: 'raw-seed' });
for (const encoding of ['hex', 'base64']) {
assert.throws(() => crypto.createPrivateKey({
key: rawSeed.toString(encoding),
encoding,
format: 'raw-seed',
asymmetricKeyType: 'ml-dsa-44',
}), { code: 'ERR_INVALID_ARG_TYPE' });
}
}
// Key types that don't support raw-* formats
{
const unsupportedKeyTypes = [
['rsa', 'rsa_public_2048.pem', 'rsa_private_2048.pem'],
];
if (!process.features.openssl_is_boringssl) {
unsupportedKeyTypes.push(['dsa', 'dsa_public.pem', 'dsa_private.pem']);
} else {
common.printSkipMessage('Skipping unsupported dsa test case');
}
for (const [type, pub, priv] of unsupportedKeyTypes) {
const pubKeyObj = crypto.createPublicKey(
fixtures.readKey(pub, 'ascii'));
const privKeyObj = crypto.createPrivateKey(
fixtures.readKey(priv, 'ascii'));
assert.throws(() => pubKeyObj.export({ format: 'raw-public' }),
{ code: 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS' });
assert.throws(() => privKeyObj.export({ format: 'raw-private' }),
{ code: 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS' });
assert.throws(() => privKeyObj.export({ format: 'raw-seed' }),
{ code: 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS' });
for (const format of ['raw-public', 'raw-private', 'raw-seed']) {
assert.throws(() => crypto.createPublicKey({
key: Buffer.alloc(32), format, asymmetricKeyType: type,
}), { code: 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS' });
}
}
// DH keys also don't support raw formats
if (!process.features.openssl_is_boringssl) {
const privKeyObj = crypto.createPrivateKey(
fixtures.readKey('dh_private.pem', 'ascii'));
assert.throws(() => privKeyObj.export({ format: 'raw-private' }),
{ code: 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS' });
assert.throws(() => crypto.createPrivateKey({
key: Buffer.alloc(32), format: 'raw-public', asymmetricKeyType: 'dh',
}), { code: 'ERR_INVALID_ARG_VALUE' });
for (const format of ['raw-private', 'raw-seed']) {
assert.throws(() => crypto.createPrivateKey({
key: Buffer.alloc(32), format, asymmetricKeyType: 'dh',
}), { code: 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS' });
}
} else {
common.printSkipMessage('Skipping unsupported dh test case');
}
}
// PQC import throws when PQC is not supported
if (!hasOpenSSL(3, 5)) {
for (const asymmetricKeyType of [
'ml-dsa-44', 'ml-dsa-65', 'ml-dsa-87',
'ml-kem-512', 'ml-kem-768', 'ml-kem-1024',
'slh-dsa-sha2-128f', 'slh-dsa-shake-128f',
]) {
for (const format of ['raw-public', 'raw-private', 'raw-seed']) {
assert.throws(() => crypto.createPublicKey({
key: Buffer.alloc(32), format, asymmetricKeyType,
}), { code: 'ERR_INVALID_ARG_VALUE' });
}
}
}
// EC: P-256 and P-384 keys cannot be imported as private/public of the other type
{
const p256Pub = crypto.createPublicKey(
fixtures.readKey('ec_p256_public.pem', 'ascii'));
const p384Pub = crypto.createPublicKey(
fixtures.readKey('ec_p384_public.pem', 'ascii'));
const p256Priv = crypto.createPrivateKey(
fixtures.readKey('ec_p256_private.pem', 'ascii'));
const p384Priv = crypto.createPrivateKey(
fixtures.readKey('ec_p384_private.pem', 'ascii'));
const p256RawPub = p256Pub.export({ format: 'raw-public' });
const p384RawPub = p384Pub.export({ format: 'raw-public' });
const p256RawPriv = p256Priv.export({ format: 'raw-private' });
const p384RawPriv = p384Priv.export({ format: 'raw-private' });
// P-256 public imported as P-384
assert.throws(() => crypto.createPublicKey({
key: p256RawPub, format: 'raw-public',
asymmetricKeyType: 'ec', namedCurve: 'P-384',
}), { code: 'ERR_INVALID_ARG_VALUE' });
// P-384 public imported as P-256
assert.throws(() => crypto.createPublicKey({
key: p384RawPub, format: 'raw-public',
asymmetricKeyType: 'ec', namedCurve: 'P-256',
}), { code: 'ERR_INVALID_ARG_VALUE' });
// P-256 private imported as P-384
assert.throws(() => crypto.createPrivateKey({
key: p256RawPriv, format: 'raw-private',
asymmetricKeyType: 'ec', namedCurve: 'P-384',
}), { code: 'ERR_INVALID_ARG_VALUE' });
// P-384 private imported as P-256
assert.throws(() => crypto.createPrivateKey({
key: p384RawPriv, format: 'raw-private',
asymmetricKeyType: 'ec', namedCurve: 'P-256',
}), { code: 'ERR_INVALID_ARG_VALUE' });
}
// ML-KEM: -768 and -512 public keys cannot be imported as the other type
if (hasOpenSSL(3, 5)) {
const mlKem512Pub = crypto.createPublicKey(
fixtures.readKey('ml_kem_512_public.pem', 'ascii'));
const mlKem768Pub = crypto.createPublicKey(
fixtures.readKey('ml_kem_768_public.pem', 'ascii'));
const mlKem512RawPub = mlKem512Pub.export({ format: 'raw-public' });
const mlKem768RawPub = mlKem768Pub.export({ format: 'raw-public' });
assert.throws(() => crypto.createPublicKey({
key: mlKem512RawPub, format: 'raw-public', asymmetricKeyType: 'ml-kem-768',
}), { code: 'ERR_INVALID_ARG_VALUE' });
assert.throws(() => crypto.createPublicKey({
key: mlKem768RawPub, format: 'raw-public', asymmetricKeyType: 'ml-kem-512',
}), { code: 'ERR_INVALID_ARG_VALUE' });
}
// ML-DSA: -44 and -65 public keys cannot be imported as the other type
if (hasOpenSSL(3, 5)) {
const mlDsa44Pub = crypto.createPublicKey(
fixtures.readKey('ml_dsa_44_public.pem', 'ascii'));
const mlDsa65Pub = crypto.createPublicKey(
fixtures.readKey('ml_dsa_65_public.pem', 'ascii'));
const mlDsa44RawPub = mlDsa44Pub.export({ format: 'raw-public' });
const mlDsa65RawPub = mlDsa65Pub.export({ format: 'raw-public' });
assert.throws(() => crypto.createPublicKey({
key: mlDsa44RawPub, format: 'raw-public', asymmetricKeyType: 'ml-dsa-65',
}), { code: 'ERR_INVALID_ARG_VALUE' });
assert.throws(() => crypto.createPublicKey({
key: mlDsa65RawPub, format: 'raw-public', asymmetricKeyType: 'ml-dsa-44',
}), { code: 'ERR_INVALID_ARG_VALUE' });
}
// SLH-DSA: mismatched key types with different sizes are rejected
if (hasOpenSSL(3, 5)) {
const slh128fPub = crypto.createPublicKey(
fixtures.readKey('slh_dsa_sha2_128f_public.pem', 'ascii'));
const slh192fPub = crypto.createPublicKey(
fixtures.readKey('slh_dsa_sha2_192f_public.pem', 'ascii'));
const slh128fPriv = crypto.createPrivateKey(
fixtures.readKey('slh_dsa_sha2_128f_private.pem', 'ascii'));
const slh192fPriv = crypto.createPrivateKey(
fixtures.readKey('slh_dsa_sha2_192f_private.pem', 'ascii'));
const rawPub128f = slh128fPub.export({ format: 'raw-public' });
const rawPub192f = slh192fPub.export({ format: 'raw-public' });
const rawPriv128f = slh128fPriv.export({ format: 'raw-private' });
const rawPriv192f = slh192fPriv.export({ format: 'raw-private' });
assert.throws(() => crypto.createPublicKey({
key: rawPub128f, format: 'raw-public',
asymmetricKeyType: 'slh-dsa-sha2-192f',
}), { code: 'ERR_INVALID_ARG_VALUE' });
assert.throws(() => crypto.createPublicKey({
key: rawPub192f, format: 'raw-public',
asymmetricKeyType: 'slh-dsa-sha2-128f',
}), { code: 'ERR_INVALID_ARG_VALUE' });
assert.throws(() => crypto.createPrivateKey({
key: rawPriv128f, format: 'raw-private',
asymmetricKeyType: 'slh-dsa-sha2-192f',
}), { code: 'ERR_INVALID_ARG_VALUE' });
assert.throws(() => crypto.createPrivateKey({
key: rawPriv192f, format: 'raw-private',
asymmetricKeyType: 'slh-dsa-sha2-128f',
}), { code: 'ERR_INVALID_ARG_VALUE' });
}
// Passphrase cannot be used with raw formats
{
const privKeyObj = crypto.createPrivateKey(
fixtures.readKey('ed25519_private.pem', 'ascii'));
assert.throws(() => privKeyObj.export({
format: 'raw-private', passphrase: 'test',
}), { code: 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS' });
assert.throws(() => privKeyObj.export({
format: 'raw-seed', passphrase: 'test',
}), { code: 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS' });
}
// raw-seed export is rejected for key types that do not support seeds
{
const ecPriv = crypto.createPrivateKey(
fixtures.readKey('ec_p256_private.pem', 'ascii'));
assert.throws(() => ecPriv.export({ format: 'raw-seed' }),
{ code: 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS' });
assert.throws(() => crypto.createPrivateKey({
key: ecPriv.export({ format: 'raw-private' }),
format: 'raw-seed',
asymmetricKeyType: 'ec',
namedCurve: 'P-256',
}), { code: 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS' });
if (process.features.openssl_is_boringssl) {
common.printSkipMessage('Skipping unsupported ed448/x448 test cases');
}
for (const type of process.features.openssl_is_boringssl ?
['ed25519', 'x25519'] :
['ed25519', 'ed448', 'x25519', 'x448']) {
const priv = crypto.createPrivateKey(
fixtures.readKey(`${type}_private.pem`, 'ascii'));
assert.throws(() => priv.export({ format: 'raw-seed' }),
{ code: 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS' });
assert.throws(() => crypto.createPrivateKey({
key: priv.export({ format: 'raw-private' }),
format: 'raw-seed',
asymmetricKeyType: type,
}), { code: 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS' });
}
if (hasOpenSSL(3, 5)) {
const slhPriv = crypto.createPrivateKey(
fixtures.readKey('slh_dsa_sha2_128f_private.pem', 'ascii'));
assert.throws(() => slhPriv.export({ format: 'raw-seed' }),
{ code: 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS' });
assert.throws(() => crypto.createPrivateKey({
key: slhPriv.export({ format: 'raw-private' }),
format: 'raw-seed',
asymmetricKeyType: 'slh-dsa-sha2-128f',
}), { code: 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS' });
}
}
// raw-private cannot be used for ml-kem and ml-dsa
if (hasOpenSSL(3, 5)) {
for (const type of ['ml-kem-512', 'ml-dsa-44']) {
const priv = crypto.createPrivateKey(
fixtures.readKey(`${type.replaceAll('-', '_')}_private.pem`, 'ascii'));
assert.throws(() => priv.export({ format: 'raw-private' }),
{ code: 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS' });
assert.throws(() => crypto.createPrivateKey({
key: priv.export({ format: 'raw-seed' }),
format: 'raw-private',
asymmetricKeyType: type,
}), { code: 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS' });
}
}
// EC: defaults to uncompressed, can be switched to compressed, both can be imported
{
const pubKeyObj = crypto.createPublicKey(
fixtures.readKey('ec_p256_public.pem', 'ascii'));
const defaultExport = pubKeyObj.export({ format: 'raw-public' });
const compressed = pubKeyObj.export({ format: 'raw-public', type: 'compressed' });
const uncompressed = pubKeyObj.export({ format: 'raw-public', type: 'uncompressed' });
// Default is uncompressed
assert.deepStrictEqual(defaultExport, uncompressed);
// Compressed starts with 0x02 or 0x03 and is 33 bytes for P-256
assert.strictEqual(compressed.byteLength, 33);
assert(compressed[0] === 0x02 || compressed[0] === 0x03);
// Uncompressed starts with 0x04 and is 65 bytes for P-256
assert.strictEqual(uncompressed.byteLength, 65);
assert.strictEqual(uncompressed[0], 0x04);
// Both can be imported
const fromCompressed = crypto.createPublicKey({
key: compressed, format: 'raw-public',
asymmetricKeyType: 'ec', namedCurve: 'P-256',
});
assert.strictEqual(fromCompressed.equals(pubKeyObj), true);
const fromUncompressed = crypto.createPublicKey({
key: uncompressed, format: 'raw-public',
asymmetricKeyType: 'ec', namedCurve: 'P-256',
});
assert.strictEqual(fromUncompressed.equals(pubKeyObj), true);
}
// Compressed and uncompressed are the only recognized options
{
const pubKeyObj = crypto.createPublicKey(
fixtures.readKey('ec_p256_public.pem', 'ascii'));
assert.throws(() => pubKeyObj.export({ format: 'raw-public', type: 'hybrid' }),
{ code: 'ERR_INVALID_ARG_VALUE' });
assert.throws(() => pubKeyObj.export({ format: 'raw-public', type: 'invalid' }),
{ code: 'ERR_INVALID_ARG_VALUE' });
}
// None of the raw types can be used with symmetric key objects
{
const secretKey = crypto.createSecretKey(Buffer.alloc(32));
for (const format of ['raw-public', 'raw-private', 'raw-seed']) {
assert.throws(() => secretKey.export({ format }),
{ code: 'ERR_INVALID_ARG_VALUE' });
}
}
// Private key objects created from raw-seed or raw-private can be passed to createPublicKey()
{
// Ed25519 raw-private -> createPublicKey
const edPriv = crypto.createPrivateKey(
fixtures.readKey('ed25519_private.pem', 'ascii'));
const edPub = crypto.createPublicKey(
fixtures.readKey('ed25519_public.pem', 'ascii'));
const rawPriv = edPriv.export({ format: 'raw-private' });
const importedPriv = crypto.createPrivateKey({
key: rawPriv, format: 'raw-private', asymmetricKeyType: 'ed25519',
});
const derivedPub = crypto.createPublicKey(importedPriv);
assert.strictEqual(derivedPub.equals(edPub), true);
// Private key must not be extractable from the derived public key.
assert.throws(() => derivedPub.export({ format: 'pem', type: 'pkcs8' }),
{ code: 'ERR_INVALID_ARG_VALUE' });
assert.throws(() => derivedPub.export({ format: 'der', type: 'pkcs8' }),
{ code: 'ERR_INVALID_ARG_VALUE' });
// EC raw-private -> createPublicKey
const ecPriv = crypto.createPrivateKey(
fixtures.readKey('ec_p256_private.pem', 'ascii'));
const ecPub = crypto.createPublicKey(
fixtures.readKey('ec_p256_public.pem', 'ascii'));
const ecRawPriv = ecPriv.export({ format: 'raw-private' });
const ecImportedPriv = crypto.createPrivateKey({
key: ecRawPriv, format: 'raw-private',
asymmetricKeyType: 'ec', namedCurve: 'P-256',
});
const ecDerivedPub = crypto.createPublicKey(ecImportedPriv);
assert.strictEqual(ecDerivedPub.equals(ecPub), true);
// Private key must not be extractable from the derived public key.
assert.throws(() => ecDerivedPub.export({ format: 'pem', type: 'pkcs8' }),
{ code: 'ERR_INVALID_ARG_VALUE' });
assert.throws(() => ecDerivedPub.export({ format: 'pem', type: 'sec1' }),
{ code: 'ERR_INVALID_ARG_VALUE' });
// PQC raw-seed -> createPublicKey
if (hasOpenSSL(3, 5)) {
const mlDsaPriv = crypto.createPrivateKey(
fixtures.readKey('ml_dsa_44_private.pem', 'ascii'));
const mlDsaPub = crypto.createPublicKey(
fixtures.readKey('ml_dsa_44_public.pem', 'ascii'));
const mlDsaRawSeed = mlDsaPriv.export({ format: 'raw-seed' });
const mlDsaImportedPriv = crypto.createPrivateKey({
key: mlDsaRawSeed, format: 'raw-seed', asymmetricKeyType: 'ml-dsa-44',
});
const mlDsaDerivedPub = crypto.createPublicKey(mlDsaImportedPriv);
assert.strictEqual(mlDsaDerivedPub.equals(mlDsaPub), true);
// Private key must not be extractable from the derived public key.
assert.throws(() => mlDsaDerivedPub.export({ format: 'pem', type: 'pkcs8' }),
{ code: 'ERR_INVALID_ARG_VALUE' });
}
}
// raw-public EC keys that are garbage/not on curve are rejected
{
const garbage = Buffer.alloc(33, 0xff);
garbage[0] = 0x02; // Valid compressed prefix but invalid point
assert.throws(() => crypto.createPublicKey({
key: garbage, format: 'raw-public',
asymmetricKeyType: 'ec', namedCurve: 'P-256',
}), { code: 'ERR_INVALID_ARG_VALUE' });
// Totally random garbage
assert.throws(() => crypto.createPublicKey({
key: Buffer.alloc(10, 0xab), format: 'raw-public',
asymmetricKeyType: 'ec', namedCurve: 'P-256',
}), { code: 'ERR_INVALID_ARG_VALUE' });
}
// Unrecognized namedCurve values are rejected
{
assert.throws(() => crypto.createPublicKey({
key: Buffer.alloc(33), format: 'raw-public',
asymmetricKeyType: 'ec', namedCurve: 'not-a-curve',
}), { code: 'ERR_CRYPTO_INVALID_CURVE' });
assert.throws(() => crypto.createPrivateKey({
key: Buffer.alloc(32), format: 'raw-private',
asymmetricKeyType: 'ec', namedCurve: 'not-a-curve',
}), { code: 'ERR_CRYPTO_INVALID_CURVE' });
}
// x25519, ed25519, x448, and ed448 cannot be used as 'ec' namedCurve values
{
if (process.features.openssl_is_boringssl) {
common.printSkipMessage('Skipping unsupported ed448/x448 test cases');
}
for (const type of process.features.openssl_is_boringssl ?
['ed25519', 'x25519'] :
['ed25519', 'x25519', 'ed448', 'x448']) {
const priv = crypto.createPrivateKey(
fixtures.readKey(`${type}_private.pem`, 'ascii'));
const pub = crypto.createPublicKey(
fixtures.readKey(`${type}_public.pem`, 'ascii'));
const rawPub = pub.export({ format: 'raw-public' });
const rawPriv = priv.export({ format: 'raw-private' });
// Try to import as EC - must fail
assert.throws(() => crypto.createPublicKey({
key: rawPub, format: 'raw-public',
asymmetricKeyType: 'ec', namedCurve: type,
}), { code: 'ERR_CRYPTO_INVALID_CURVE' });
assert.throws(() => crypto.createPrivateKey({
key: rawPriv, format: 'raw-private',
asymmetricKeyType: 'ec', namedCurve: type,
}), { code: 'ERR_CRYPTO_INVALID_CURVE' });
}
}
// Missing asymmetricKeyType option
{
assert.throws(() => crypto.createPublicKey({
key: Buffer.alloc(32), format: 'raw-public',
}), { code: 'ERR_INVALID_ARG_TYPE' });
}
// Unknown asymmetricKeyType value
{
assert.throws(() => crypto.createPublicKey({
key: Buffer.alloc(32), format: 'raw-public',
asymmetricKeyType: 'unknown',
}), { code: 'ERR_INVALID_ARG_VALUE' });
}
// Non-buffer key data
{
assert.throws(() => crypto.createPublicKey({
key: 12345, format: 'raw-public',
asymmetricKeyType: 'ec', namedCurve: 'P-256',
}), { code: 'ERR_INVALID_ARG_TYPE' });
}
// Missing namedCurve for EC
{
assert.throws(() => crypto.createPublicKey({
key: Buffer.alloc(33), format: 'raw-public',
asymmetricKeyType: 'ec',
}), { code: 'ERR_INVALID_ARG_TYPE' });
}