| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -600,52 +600,67 @@ async function importKey( | |||
| 600 | 600 | }); | |
| 601 | 601 | ||
| 602 | 602 | algorithm = normalizeAlgorithm(algorithm, 'importKey'); | |
| 603 | + let result; | ||
| 603 | 604 | switch (algorithm.name) { | |
| 604 | 605 | case 'RSASSA-PKCS1-v1_5': | |
| 605 | 606 | // Fall through | |
| 606 | 607 | case 'RSA-PSS': | |
| 607 | 608 | // Fall through | |
| 608 | 609 | case 'RSA-OAEP': | |
| 609 | - return require('internal/crypto/rsa') | ||
| 610 | + result = await require('internal/crypto/rsa') | ||
| 610 | 611 | .rsaImportKey(format, keyData, algorithm, extractable, keyUsages); | |
| 612 | + break; | ||
| 611 | 613 | case 'ECDSA': | |
| 612 | 614 | // Fall through | |
| 613 | 615 | case 'ECDH': | |
| 614 | - return require('internal/crypto/ec') | ||
| 616 | + result = await require('internal/crypto/ec') | ||
| 615 | 617 | .ecImportKey(format, keyData, algorithm, extractable, keyUsages); | |
| 618 | + break; | ||
| 616 | 619 | case 'Ed25519': | |
| 617 | 620 | // Fall through | |
| 618 | 621 | case 'Ed448': | |
| 619 | 622 | // Fall through | |
| 620 | 623 | case 'X25519': | |
| 621 | 624 | // Fall through | |
| 622 | 625 | case 'X448': | |
| 623 | - return require('internal/crypto/cfrg') | ||
| 626 | + result = await require('internal/crypto/cfrg') | ||
| 624 | 627 | .cfrgImportKey(format, keyData, algorithm, extractable, keyUsages); | |
| 628 | + break; | ||
| 625 | 629 | case 'HMAC': | |
| 626 | - return require('internal/crypto/mac') | ||
| 630 | + result = await require('internal/crypto/mac') | ||
| 627 | 631 | .hmacImportKey(format, keyData, algorithm, extractable, keyUsages); | |
| 632 | + break; | ||
| 628 | 633 | case 'AES-CTR': | |
| 629 | 634 | // Fall through | |
| 630 | 635 | case 'AES-CBC': | |
| 631 | 636 | // Fall through | |
| 632 | 637 | case 'AES-GCM': | |
| 633 | 638 | // Fall through | |
| 634 | 639 | case 'AES-KW': | |
| 635 | - return require('internal/crypto/aes') | ||
| 640 | + result = await require('internal/crypto/aes') | ||
| 636 | 641 | .aesImportKey(algorithm, format, keyData, extractable, keyUsages); | |
| 642 | + break; | ||
| 637 | 643 | case 'HKDF': | |
| 638 | 644 | // Fall through | |
| 639 | 645 | case 'PBKDF2': | |
| 640 | - return importGenericSecretKey( | ||
| 646 | + result = await importGenericSecretKey( | ||
| 641 | 647 | algorithm, | |
| 642 | 648 | format, | |
| 643 | 649 | keyData, | |
| 644 | 650 | extractable, | |
| 645 | 651 | keyUsages); | |
| 652 | + break; | ||
| 653 | + default: | ||
| 654 | + throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError'); | ||
| 646 | 655 | } | |
| 647 | 656 | ||
| 648 | - throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError'); | ||
| 657 | + if ((result.type === 'secret' || result.type === 'private') && result.usages.length === 0) { | ||
| 658 | + throw lazyDOMException( | ||
| 659 | + `Usages cannot be empty when importing a ${result.type} key.`, | ||
| 660 | + 'SyntaxError'); | ||
| 661 | + } | ||
| 662 | + | ||
| 663 | + return result; | ||
| 649 | 664 | } | |
| 650 | 665 | ||
| 651 | 666 | // subtle.wrapKey() is essentially a subtle.exportKey() followed | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -164,6 +164,15 @@ async function testImportPkcs8({ name, privateUsages }, extractable) { | |||
| 164 | 164 | message: /key is not extractable/ | |
| 165 | 165 | }); | |
| 166 | 166 | } | |
| 167 | + | ||
| 168 | + await assert.rejects( | ||
| 169 | + subtle.importKey( | ||
| 170 | + 'pkcs8', | ||
| 171 | + keyData[name].pkcs8, | ||
| 172 | + { name }, | ||
| 173 | + extractable, | ||
| 174 | + [/* empty usages */]), | ||
| 175 | + { name: 'SyntaxError', message: 'Usages cannot be empty when importing a private key.' }); | ||
| 167 | 176 | } | |
| 168 | 177 | ||
| 169 | 178 | async function testImportJwk({ name, publicUsages, privateUsages }, extractable) { | |
@@ -311,6 +320,15 @@ async function testImportJwk({ name, publicUsages, privateUsages }, extractable) | |||
| 311 | 320 | publicUsages), | |
| 312 | 321 | { message: 'JWK "crv" Parameter and algorithm name mismatch' }); | |
| 313 | 322 | } | |
| 323 | + | ||
| 324 | + await assert.rejects( | ||
| 325 | + subtle.importKey( | ||
| 326 | + 'jwk', | ||
| 327 | + { ...jwk }, | ||
| 328 | + { name }, | ||
| 329 | + extractable, | ||
| 330 | + [/* empty usages */]), | ||
| 331 | + { name: 'SyntaxError', message: 'Usages cannot be empty when importing a private key.' }); | ||
| 314 | 332 | } | |
| 315 | 333 | ||
| 316 | 334 | async function testImportRaw({ name, publicUsages }) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -164,6 +164,15 @@ async function testImportPkcs8( | |||
| 164 | 164 | message: /key is not extractable/ | |
| 165 | 165 | }); | |
| 166 | 166 | } | |
| 167 | + | ||
| 168 | + await assert.rejects( | ||
| 169 | + subtle.importKey( | ||
| 170 | + 'pkcs8', | ||
| 171 | + keyData[namedCurve].pkcs8, | ||
| 172 | + { name, namedCurve }, | ||
| 173 | + extractable, | ||
| 174 | + [/* empty usages */]), | ||
| 175 | + { name: 'SyntaxError', message: 'Usages cannot be empty when importing a private key.' }); | ||
| 167 | 176 | } | |
| 168 | 177 | ||
| 169 | 178 | async function testImportJwk( | |
@@ -312,6 +321,15 @@ async function testImportJwk( | |||
| 312 | 321 | privateUsages), | |
| 313 | 322 | { message: 'JWK "crv" does not match the requested algorithm' }); | |
| 314 | 323 | } | |
| 324 | + | ||
| 325 | + await assert.rejects( | ||
| 326 | + subtle.importKey( | ||
| 327 | + 'jwk', | ||
| 328 | + { ...jwk }, | ||
| 329 | + { name, namedCurve }, | ||
| 330 | + extractable, | ||
| 331 | + [/* empty usages */]), | ||
| 332 | + { name: 'SyntaxError', message: 'Usages cannot be empty when importing a private key.' }); | ||
| 315 | 333 | } | |
| 316 | 334 | ||
| 317 | 335 | async function testImportRaw({ name, publicUsages }, namedCurve) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -361,6 +361,15 @@ async function testImportPkcs8( | |||
| 361 | 361 | message: /key is not extractable/ | |
| 362 | 362 | }); | |
| 363 | 363 | } | |
| 364 | + | ||
| 365 | + await assert.rejects( | ||
| 366 | + subtle.importKey( | ||
| 367 | + 'pkcs8', | ||
| 368 | + keyData[size].pkcs8, | ||
| 369 | + { name, hash }, | ||
| 370 | + extractable, | ||
| 371 | + [/* empty usages */]), | ||
| 372 | + { name: 'SyntaxError', message: 'Usages cannot be empty when importing a private key.' }); | ||
| 364 | 373 | } | |
| 365 | 374 | ||
| 366 | 375 | async function testImportJwk( | |
@@ -495,6 +504,15 @@ async function testImportJwk( | |||
| 495 | 504 | privateUsages), | |
| 496 | 505 | { message: 'JWK "alg" does not match the requested algorithm' }); | |
| 497 | 506 | } | |
| 507 | + | ||
| 508 | + await assert.rejects( | ||
| 509 | + subtle.importKey( | ||
| 510 | + 'jwk', | ||
| 511 | + { ...jwk }, | ||
| 512 | + { name, hash }, | ||
| 513 | + extractable, | ||
| 514 | + [/* empty usages */]), | ||
| 515 | + { name: 'SyntaxError', message: 'Usages cannot be empty when importing a private key.' }); | ||
| 498 | 516 | } | |
| 499 | 517 | ||
| 500 | 518 | // combinations to test | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -96,6 +96,18 @@ const { subtle } = webcrypto; | |||
| 96 | 96 | assert.deepStrictEqual( | |
| 97 | 97 | Buffer.from(jwk.k, 'base64').toString('hex'), | |
| 98 | 98 | Buffer.from(raw).toString('hex')); | |
| 99 | + | ||
| 100 | + await assert.rejects( | ||
| 101 | + subtle.importKey( | ||
| 102 | + 'raw', | ||
| 103 | + keyData, | ||
| 104 | + { | ||
| 105 | + name: 'HMAC', | ||
| 106 | + hash: 'SHA-256' | ||
| 107 | + }, | ||
| 108 | + true, | ||
| 109 | + [/* empty usages */]), | ||
| 110 | + { name: 'SyntaxError', message: 'Usages cannot be empty when importing a secret key.' }); | ||
| 99 | 111 | } | |
| 100 | 112 | ||
| 101 | 113 | test().then(common.mustCall()); | |
@@ -126,6 +138,18 @@ const { subtle } = webcrypto; | |||
| 126 | 138 | assert.deepStrictEqual( | |
| 127 | 139 | Buffer.from(jwk.k, 'base64').toString('hex'), | |
| 128 | 140 | Buffer.from(raw).toString('hex')); | |
| 141 | + | ||
| 142 | + await assert.rejects( | ||
| 143 | + subtle.importKey( | ||
| 144 | + 'raw', | ||
| 145 | + keyData, | ||
| 146 | + { | ||
| 147 | + name: 'AES-CTR', | ||
| 148 | + length: 256, | ||
| 149 | + }, | ||
| 150 | + true, | ||
| 151 | + [/* empty usages */]), | ||
| 152 | + { name: 'SyntaxError', message: 'Usages cannot be empty when importing a secret key.' }); | ||
| 129 | 153 | } | |
| 130 | 154 | ||
| 131 | 155 | test().then(common.mustCall()); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -149,7 +149,6 @@ async function testSign({ name, | |||
| 149 | 149 | plaintext }) { | |
| 150 | 150 | const [ | |
| 151 | 151 | publicKey, | |
| 152 | - noSignPrivateKey, | ||
| 153 | 152 | privateKey, | |
| 154 | 153 | hmacKey, | |
| 155 | 154 | rsaKeys, | |
@@ -161,12 +160,6 @@ async function testSign({ name, | |||
| 161 | 160 | { name, namedCurve }, | |
| 162 | 161 | false, | |
| 163 | 162 | ['verify']), | |
| 164 | - subtle.importKey( | ||
| 165 | - 'pkcs8', | ||
| 166 | - privateKeyBuffer, | ||
| 167 | - { name, namedCurve }, | ||
| 168 | - false, | ||
| 169 | - [ /* No usages */ ]), | ||
| 170 | 163 | subtle.importKey( | |
| 171 | 164 | 'pkcs8', | |
| 172 | 165 | privateKeyBuffer, | |
@@ -214,12 +207,6 @@ async function testSign({ name, | |||
| 214 | 207 | message: /Unable to use this key to sign/ | |
| 215 | 208 | }); | |
| 216 | 209 | ||
| 217 | - // Test failure when no sign usage | ||
| 218 | - await assert.rejects( | ||
| 219 | - subtle.sign({ name, hash }, noSignPrivateKey, plaintext), { | ||
| 220 | - message: /Unable to use this key to sign/ | ||
| 221 | - }); | ||
| 222 | - | ||
| 223 | 210 | // Test failure when using the wrong algorithms | |
| 224 | 211 | await assert.rejects( | |
| 225 | 212 | subtle.sign({ name, hash }, hmacKey, plaintext), { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -131,7 +131,6 @@ async function testSign({ name, | |||
| 131 | 131 | data }) { | |
| 132 | 132 | const [ | |
| 133 | 133 | publicKey, | |
| 134 | - noSignPrivateKey, | ||
| 135 | 134 | privateKey, | |
| 136 | 135 | hmacKey, | |
| 137 | 136 | rsaKeys, | |
@@ -143,12 +142,6 @@ async function testSign({ name, | |||
| 143 | 142 | { name }, | |
| 144 | 143 | false, | |
| 145 | 144 | ['verify']), | |
| 146 | - subtle.importKey( | ||
| 147 | - 'pkcs8', | ||
| 148 | - privateKeyBuffer, | ||
| 149 | - { name }, | ||
| 150 | - false, | ||
| 151 | - [ /* No usages */ ]), | ||
| 152 | 145 | subtle.importKey( | |
| 153 | 146 | 'pkcs8', | |
| 154 | 147 | privateKeyBuffer, | |
@@ -197,12 +190,6 @@ async function testSign({ name, | |||
| 197 | 190 | message: /Unable to use this key to sign/ | |
| 198 | 191 | }); | |
| 199 | 192 | ||
| 200 | - // Test failure when no sign usage | ||
| 201 | - await assert.rejects( | ||
| 202 | - subtle.sign({ name }, noSignPrivateKey, data), { | ||
| 203 | - message: /Unable to use this key to sign/ | ||
| 204 | - }); | ||
| 205 | - | ||
| 206 | 193 | // Test failure when using the wrong algorithms | |
| 207 | 194 | await assert.rejects( | |
| 208 | 195 | subtle.sign({ name }, hmacKey, data), { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -31,7 +31,7 @@ async function testVerify({ hash, | |||
| 31 | 31 | keyBuffer, | |
| 32 | 32 | { name, hash }, | |
| 33 | 33 | false, | |
| 34 | - [ /* No usages */ ]), | ||
| 34 | + ['sign']), | ||
| 35 | 35 | subtle.generateKey( | |
| 36 | 36 | { | |
| 37 | 37 | name: 'RSA-PSS', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -132,7 +132,6 @@ async function testSign({ | |||
| 132 | 132 | }) { | |
| 133 | 133 | const [ | |
| 134 | 134 | publicKey, | |
| 135 | - noSignPrivateKey, | ||
| 136 | 135 | privateKey, | |
| 137 | 136 | hmacKey, | |
| 138 | 137 | ecdsaKeys, | |
@@ -143,12 +142,6 @@ async function testSign({ | |||
| 143 | 142 | { name: algorithm.name, hash }, | |
| 144 | 143 | false, | |
| 145 | 144 | ['verify']), | |
| 146 | - subtle.importKey( | ||
| 147 | - 'pkcs8', | ||
| 148 | - privateKeyBuffer, | ||
| 149 | - { name: algorithm.name, hash }, | ||
| 150 | - false, | ||
| 151 | - [ /* No usages */ ]), | ||
| 152 | 145 | subtle.importKey( | |
| 153 | 146 | 'pkcs8', | |
| 154 | 147 | privateKeyBuffer, | |
@@ -189,12 +182,6 @@ async function testSign({ | |||
| 189 | 182 | message: /Unable to use this key to sign/ | |
| 190 | 183 | }); | |
| 191 | 184 | ||
| 192 | - // Test failure when no sign usage | ||
| 193 | - await assert.rejects( | ||
| 194 | - subtle.sign(algorithm, noSignPrivateKey, plaintext), { | ||
| 195 | - message: /Unable to use this key to sign/ | ||
| 196 | - }); | ||
| 197 | - | ||
| 198 | 185 | // Test failure when using the wrong algorithms | |
| 199 | 186 | await assert.rejects( | |
| 200 | 187 | subtle.sign(algorithm, hmacKey, plaintext), { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments