@@ -600,52 +600,67 @@ async function importKey(
600600 });
601601
602602 algorithm = normalizeAlgorithm(algorithm, 'importKey');
603+ let result;
603604 switch (algorithm.name) {
604605 case 'RSASSA-PKCS1-v1_5':
605606 // Fall through
606607 case 'RSA-PSS':
607608 // Fall through
608609 case 'RSA-OAEP':
609- return require('internal/crypto/rsa')
610+ result = await require('internal/crypto/rsa')
610611 .rsaImportKey(format, keyData, algorithm, extractable, keyUsages);
612+ break;
611613 case 'ECDSA':
612614 // Fall through
613615 case 'ECDH':
614- return require('internal/crypto/ec')
616+ result = await require('internal/crypto/ec')
615617 .ecImportKey(format, keyData, algorithm, extractable, keyUsages);
618+ break;
616619 case 'Ed25519':
617620 // Fall through
618621 case 'Ed448':
619622 // Fall through
620623 case 'X25519':
621624 // Fall through
622625 case 'X448':
623- return require('internal/crypto/cfrg')
626+ result = await require('internal/crypto/cfrg')
624627 .cfrgImportKey(format, keyData, algorithm, extractable, keyUsages);
628+ break;
625629 case 'HMAC':
626- return require('internal/crypto/mac')
630+ result = await require('internal/crypto/mac')
627631 .hmacImportKey(format, keyData, algorithm, extractable, keyUsages);
632+ break;
628633 case 'AES-CTR':
629634 // Fall through
630635 case 'AES-CBC':
631636 // Fall through
632637 case 'AES-GCM':
633638 // Fall through
634639 case 'AES-KW':
635- return require('internal/crypto/aes')
640+ result = await require('internal/crypto/aes')
636641 .aesImportKey(algorithm, format, keyData, extractable, keyUsages);
642+ break;
637643 case 'HKDF':
638644 // Fall through
639645 case 'PBKDF2':
640- return importGenericSecretKey(
646+ result = await importGenericSecretKey(
641647 algorithm,
642648 format,
643649 keyData,
644650 extractable,
645651 keyUsages);
652+ break;
653+ default:
654+ throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError');
646655 }
647656
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;
649664}
650665
651666// subtle.wrapKey() is essentially a subtle.exportKey() followed
0 commit comments