|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +if (!common.hasCrypto) |
| 5 | + common.skip('missing crypto'); |
| 6 | + |
| 7 | +const assert = require('assert'); |
| 8 | +const { |
| 9 | + createSecretKey, |
| 10 | + KeyObject, |
| 11 | + randomBytes, |
| 12 | + generateKeyPairSync, |
| 13 | +} = require('crypto'); |
| 14 | + |
| 15 | +function assertCryptoKey(cryptoKey, keyObject, algorithm, extractable, usages) { |
| 16 | + assert.strictEqual(cryptoKey instanceof CryptoKey, true); |
| 17 | + assert.strictEqual(cryptoKey.type, keyObject.type); |
| 18 | + assert.strictEqual(cryptoKey.algorithm.name, algorithm); |
| 19 | + assert.strictEqual(cryptoKey.extractable, extractable); |
| 20 | + assert.deepStrictEqual(cryptoKey.usages, usages); |
| 21 | + assert.strictEqual(keyObject.equals(KeyObject.from(cryptoKey)), true); |
| 22 | +} |
| 23 | + |
| 24 | +{ |
| 25 | + for (const length of [128, 192, 256]) { |
| 26 | + const aes = createSecretKey(randomBytes(length >> 3)); |
| 27 | + for (const algorithm of ['AES-CTR', 'AES-CBC', 'AES-GCM', 'AES-KW']) { |
| 28 | + const usages = algorithm === 'AES-KW' ? ['wrapKey', 'unwrapKey'] : ['encrypt', 'decrypt']; |
| 29 | + for (const extractable of [true, false]) { |
| 30 | + const cryptoKey = aes.toCryptoKey(algorithm, extractable, usages); |
| 31 | + assertCryptoKey(cryptoKey, aes, algorithm, extractable, usages); |
| 32 | + assert.strictEqual(cryptoKey.algorithm.length, length); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +{ |
| 39 | + const pbkdf2 = createSecretKey(randomBytes(16)); |
| 40 | + const algorithm = 'PBKDF2'; |
| 41 | + const usages = ['deriveBits']; |
| 42 | + assert.throws(() => pbkdf2.toCryptoKey(algorithm, true, usages), { |
| 43 | + name: 'SyntaxError', |
| 44 | + message: 'PBKDF2 keys are not extractable' |
| 45 | + }); |
| 46 | + assert.throws(() => pbkdf2.toCryptoKey(algorithm, false, ['wrapKey']), { |
| 47 | + name: 'SyntaxError', |
| 48 | + message: 'Unsupported key usage for a PBKDF2 key' |
| 49 | + }); |
| 50 | + const cryptoKey = pbkdf2.toCryptoKey(algorithm, false, usages); |
| 51 | + assertCryptoKey(cryptoKey, pbkdf2, algorithm, false, usages); |
| 52 | + assert.strictEqual(cryptoKey.algorithm.length, undefined); |
| 53 | +} |
| 54 | + |
| 55 | +{ |
| 56 | + for (const length of [128, 192, 256]) { |
| 57 | + const hmac = createSecretKey(randomBytes(length >> 3)); |
| 58 | + const algorithm = 'HMAC'; |
| 59 | + const usages = ['sign', 'verify']; |
| 60 | + |
| 61 | + assert.throws(() => { |
| 62 | + createSecretKey(Buffer.alloc(0)).toCryptoKey({ name: algorithm, hash: 'SHA-256' }, true, usages); |
| 63 | + }, { |
| 64 | + name: 'DataError', |
| 65 | + message: 'Zero-length key is not supported', |
| 66 | + }); |
| 67 | + |
| 68 | + assert.throws(() => { |
| 69 | + hmac.toCryptoKey({ |
| 70 | + name: algorithm, |
| 71 | + hash: 'SHA-256', |
| 72 | + }, true, []); |
| 73 | + }, { |
| 74 | + name: 'SyntaxError', |
| 75 | + message: 'Usages cannot be empty when importing a secret key.' |
| 76 | + }); |
| 77 | + |
| 78 | + for (const hash of ['SHA-1', 'SHA-256', 'SHA-384', 'SHA-512']) { |
| 79 | + for (const extractable of [true, false]) { |
| 80 | + assert.throws(() => { |
| 81 | + hmac.toCryptoKey({ name: algorithm, hash: 'SHA-256', length: 0 }, true, usages); |
| 82 | + }, { |
| 83 | + name: 'DataError', |
| 84 | + message: 'Zero-length key is not supported', |
| 85 | + }); |
| 86 | + const cryptoKey = hmac.toCryptoKey({ name: algorithm, hash }, extractable, usages); |
| 87 | + assertCryptoKey(cryptoKey, hmac, algorithm, extractable, usages); |
| 88 | + assert.strictEqual(cryptoKey.algorithm.length, length); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +{ |
| 95 | + for (const algorithm of ['Ed25519', 'Ed448', 'X25519', 'X448']) { |
| 96 | + const { publicKey, privateKey } = generateKeyPairSync(algorithm.toLowerCase()); |
| 97 | + assert.throws(() => { |
| 98 | + publicKey.toCryptoKey(algorithm === 'Ed25519' ? 'X25519' : 'Ed25519', true, []); |
| 99 | + }, { |
| 100 | + name: 'DataError', |
| 101 | + message: 'Invalid key type' |
| 102 | + }); |
| 103 | + for (const key of [publicKey, privateKey]) { |
| 104 | + let usages; |
| 105 | + if (algorithm.startsWith('E')) { |
| 106 | + usages = key.type === 'public' ? ['verify'] : ['sign']; |
| 107 | + } else { |
| 108 | + usages = key.type === 'public' ? [] : ['deriveBits']; |
| 109 | + } |
| 110 | + for (const extractable of [true, false]) { |
| 111 | + const cryptoKey = key.toCryptoKey(algorithm, extractable, usages); |
| 112 | + assertCryptoKey(cryptoKey, key, algorithm, extractable, usages); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +{ |
| 119 | + const { publicKey, privateKey } = generateKeyPairSync('rsa', { modulusLength: 2048 }); |
| 120 | + for (const key of [publicKey, privateKey]) { |
| 121 | + for (const algorithm of ['RSASSA-PKCS1-v1_5', 'RSA-PSS', 'RSA-OAEP']) { |
| 122 | + let usages; |
| 123 | + if (algorithm === 'RSA-OAEP') { |
| 124 | + usages = key.type === 'public' ? ['encrypt', 'wrapKey'] : ['decrypt', 'unwrapKey']; |
| 125 | + } else { |
| 126 | + usages = key.type === 'public' ? ['verify'] : ['sign']; |
| 127 | + } |
| 128 | + for (const extractable of [true, false]) { |
| 129 | + for (const hash of ['SHA-1', 'SHA-256', 'SHA-384', 'SHA-512']) { |
| 130 | + const cryptoKey = key.toCryptoKey({ |
| 131 | + name: algorithm, |
| 132 | + hash |
| 133 | + }, extractable, usages); |
| 134 | + assertCryptoKey(cryptoKey, key, algorithm, extractable, usages); |
| 135 | + assert.strictEqual(cryptoKey.algorithm.hash.name, hash); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +{ |
| 143 | + for (const namedCurve of ['P-256', 'P-384', 'P-521']) { |
| 144 | + const { publicKey, privateKey } = generateKeyPairSync('ec', { namedCurve }); |
| 145 | + assert.throws(() => { |
| 146 | + privateKey.toCryptoKey({ |
| 147 | + name: 'ECDH', |
| 148 | + namedCurve, |
| 149 | + }, true, []); |
| 150 | + }, { |
| 151 | + name: 'SyntaxError', |
| 152 | + message: 'Usages cannot be empty when importing a private key.' |
| 153 | + }); |
| 154 | + assert.throws(() => { |
| 155 | + publicKey.toCryptoKey({ |
| 156 | + name: 'ECDH', |
| 157 | + namedCurve: namedCurve === 'P-256' ? 'P-384' : 'P-256' |
| 158 | + }, true, []); |
| 159 | + }, { |
| 160 | + name: 'DataError', |
| 161 | + message: 'Named curve mismatch' |
| 162 | + }); |
| 163 | + for (const key of [publicKey, privateKey]) { |
| 164 | + for (const algorithm of ['ECDH', 'ECDSA']) { |
| 165 | + let usages; |
| 166 | + if (algorithm === 'ECDH') { |
| 167 | + usages = key.type === 'public' ? [] : ['deriveBits']; |
| 168 | + } else { |
| 169 | + usages = key.type === 'public' ? ['verify'] : ['sign']; |
| 170 | + } |
| 171 | + for (const extractable of [true, false]) { |
| 172 | + const cryptoKey = key.toCryptoKey({ |
| 173 | + name: algorithm, |
| 174 | + namedCurve |
| 175 | + }, extractable, usages); |
| 176 | + assertCryptoKey(cryptoKey, key, algorithm, extractable, usages); |
| 177 | + assert.strictEqual(cryptoKey.algorithm.namedCurve, namedCurve); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments