| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b62d569 commit 788a66e
12 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,10 +1,5 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | - const { | ||
| 4 | - ArrayPrototypePush, | ||
| 5 | - SafeSet, | ||
| 6 | - } = primordials; | ||
| 7 | - | ||
| 8 | 3 | const { | |
| 9 | 4 | AESCipherJob, | |
| 10 | 5 | kCryptoJobWebCrypto, | |
@@ -28,7 +23,6 @@ const { | |||
| 28 | 23 | ||
| 29 | 24 | const { | |
| 30 | 25 | getUsagesMask, | |
| 31 | - hasAnyNotIn, | ||
| 32 | 26 | jobPromise, | |
| 33 | 27 | } = require('internal/crypto/util'); | |
| 34 | 28 | ||
@@ -46,8 +40,22 @@ const { | |||
| 46 | 40 | importJwkSecretKey, | |
| 47 | 41 | importSecretKey, | |
| 48 | 42 | validateJwk, | |
| 43 | + validateKeyUsages, | ||
| 44 | + validateUsagesNotEmpty, | ||
| 49 | 45 | } = require('internal/crypto/webcrypto_util'); | |
| 50 | 46 | ||
| 47 | + const kCipherUsages = ['encrypt', 'decrypt', 'wrapKey', 'unwrapKey']; | ||
| 48 | + const kWrapUsages = ['wrapKey', 'unwrapKey']; | ||
| 49 | + | ||
| 50 | + const kUsages = { | ||
| 51 | + '__proto__': null, | ||
| 52 | + 'AES-CBC': kCipherUsages, | ||
| 53 | + 'AES-CTR': kCipherUsages, | ||
| 54 | + 'AES-GCM': kCipherUsages, | ||
| 55 | + 'AES-KW': kWrapUsages, | ||
| 56 | + 'AES-OCB': kCipherUsages, | ||
| 57 | + }; | ||
| 58 | + | ||
| 51 | 59 | function getAlgorithmName(name, length) { | |
| 52 | 60 | switch (name) { | |
| 53 | 61 | case 'AES-CBC': return `A${length}CBC`; | |
@@ -176,21 +184,8 @@ function aesCipher(mode, key, data, algorithm) { | |||
| 176 | 184 | function aesGenerateKey(algorithm, extractable, usages) { | |
| 177 | 185 | const { name, length } = algorithm; | |
| 178 | 186 | ||
| 179 | - const checkUsages = ['wrapKey', 'unwrapKey']; | ||
| 180 | - if (name !== 'AES-KW') | ||
| 181 | - ArrayPrototypePush(checkUsages, 'encrypt', 'decrypt'); | ||
| 182 | - | ||
| 183 | - const usagesSet = new SafeSet(usages); | ||
| 184 | - if (hasAnyNotIn(usagesSet, checkUsages)) { | ||
| 185 | - throw lazyDOMException( | ||
| 186 | - 'Unsupported key usage for an AES key', | ||
| 187 | - 'SyntaxError'); | ||
| 188 | - } | ||
| 189 | - if (usagesSet.size === 0) { | ||
| 190 | - throw lazyDOMException( | ||
| 191 | - 'Usages cannot be empty when creating a key.', | ||
| 192 | - 'SyntaxError'); | ||
| 193 | - } | ||
| 187 | + const usagesSet = validateUsagesNotEmpty( | ||
| 188 | + validateKeyUsages(usages, kUsages[name], name)); | ||
| 194 | 189 | ||
| 195 | 190 | return jobPromise(() => new SecretKeyGenJob( | |
| 196 | 191 | kCryptoJobWebCrypto, | |
@@ -207,16 +202,7 @@ function aesImportKey( | |||
| 207 | 202 | extractable, | |
| 208 | 203 | usages) { | |
| 209 | 204 | const { name } = algorithm; | |
| 210 | - const checkUsages = ['wrapKey', 'unwrapKey']; | ||
| 211 | - if (name !== 'AES-KW') | ||
| 212 | - ArrayPrototypePush(checkUsages, 'encrypt', 'decrypt'); | ||
| 213 | - | ||
| 214 | - const usagesSet = new SafeSet(usages); | ||
| 215 | - if (hasAnyNotIn(usagesSet, checkUsages)) { | ||
| 216 | - throw lazyDOMException( | ||
| 217 | - 'Unsupported key usage for an AES key', | ||
| 218 | - 'SyntaxError'); | ||
| 219 | - } | ||
| 205 | + const usagesSet = validateKeyUsages(usages, kUsages[name], name); | ||
| 220 | 206 | ||
| 221 | 207 | let handle; | |
| 222 | 208 | let length; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,8 +26,6 @@ const { | |||
| 26 | 26 | ||
| 27 | 27 | const { | |
| 28 | 28 | getUsagesMask, | |
| 29 | - getUsagesUnion, | ||
| 30 | - hasAnyNotIn, | ||
| 31 | 29 | jobPromise, | |
| 32 | 30 | } = require('internal/crypto/util'); | |
| 33 | 31 | ||
@@ -42,60 +40,33 @@ const { | |||
| 42 | 40 | } = require('internal/crypto/keys'); | |
| 43 | 41 | ||
| 44 | 42 | const { | |
| 43 | + createKeyUsages, | ||
| 44 | + getKeyPairUsages, | ||
| 45 | 45 | importDerKey, | |
| 46 | 46 | importJwkKey, | |
| 47 | 47 | importRawKey, | |
| 48 | 48 | validateJwk, | |
| 49 | + validateKeyUsages, | ||
| 50 | + validateUsagesNotEmpty, | ||
| 51 | + verifyAcceptableKeyUse, | ||
| 49 | 52 | } = require('internal/crypto/webcrypto_util'); | |
| 50 | 53 | ||
| 51 | - function verifyAcceptableCfrgKeyUse(name, isPublic, usages) { | ||
| 52 | - let checkSet; | ||
| 53 | - switch (name) { | ||
| 54 | - case 'X25519': | ||
| 55 | - // Fall through | ||
| 56 | - case 'X448': | ||
| 57 | - checkSet = isPublic ? [] : ['deriveKey', 'deriveBits']; | ||
| 58 | - break; | ||
| 59 | - case 'Ed25519': | ||
| 60 | - // Fall through | ||
| 61 | - case 'Ed448': | ||
| 62 | - checkSet = isPublic ? ['verify'] : ['sign']; | ||
| 63 | - break; | ||
| 64 | - default: | ||
| 65 | - throw lazyDOMException( | ||
| 66 | - 'The algorithm is not supported', 'NotSupportedError'); | ||
| 67 | - } | ||
| 68 | - if (hasAnyNotIn(usages, checkSet)) { | ||
| 69 | - throw lazyDOMException( | ||
| 70 | - `Unsupported key usage for a ${name} key`, | ||
| 71 | - 'SyntaxError'); | ||
| 72 | - } | ||
| 73 | - } | ||
| 54 | + const kDeriveUsages = createKeyUsages([], ['deriveKey', 'deriveBits']); | ||
| 55 | + | ||
| 56 | + const kSignVerifyUsages = createKeyUsages(['verify'], ['sign']); | ||
| 57 | + | ||
| 58 | + const kUsages = { | ||
| 59 | + '__proto__': null, | ||
| 60 | + 'X25519': kDeriveUsages, | ||
| 61 | + 'X448': kDeriveUsages, | ||
| 62 | + 'Ed25519': kSignVerifyUsages, | ||
| 63 | + 'Ed448': kSignVerifyUsages, | ||
| 64 | + }; | ||
| 74 | 65 | ||
| 75 | 66 | function cfrgGenerateKey(algorithm, extractable, usages) { | |
| 76 | 67 | const { name } = algorithm; | |
| 77 | - | ||
| 78 | - const usageSet = new SafeSet(usages); | ||
| 79 | - switch (name) { | ||
| 80 | - case 'Ed25519': | ||
| 81 | - // Fall through | ||
| 82 | - case 'Ed448': | ||
| 83 | - if (hasAnyNotIn(usageSet, ['sign', 'verify'])) { | ||
| 84 | - throw lazyDOMException( | ||
| 85 | - `Unsupported key usage for an ${name} key`, | ||
| 86 | - 'SyntaxError'); | ||
| 87 | - } | ||
| 88 | - break; | ||
| 89 | - case 'X25519': | ||
| 90 | - // Fall through | ||
| 91 | - case 'X448': | ||
| 92 | - if (hasAnyNotIn(usageSet, ['deriveKey', 'deriveBits'])) { | ||
| 93 | - throw lazyDOMException( | ||
| 94 | - `Unsupported key usage for an ${name} key`, | ||
| 95 | - 'SyntaxError'); | ||
| 96 | - } | ||
| 97 | - break; | ||
| 98 | - } | ||
| 68 | + const allowedUsages = kUsages[name]; | ||
| 69 | + const usagesSet = validateKeyUsages(usages, allowedUsages.keygen, name); | ||
| 99 | 70 | const nid = { | |
| 100 | 71 | '__proto__': null, | |
| 101 | 72 | 'Ed25519': EVP_PKEY_ED25519, | |
@@ -104,37 +75,16 @@ function cfrgGenerateKey(algorithm, extractable, usages) { | |||
| 104 | 75 | 'X448': EVP_PKEY_X448, | |
| 105 | 76 | }[name]; | |
| 106 | 77 | ||
| 107 | - let publicUsages; | ||
| 108 | - let privateUsages; | ||
| 109 | - switch (name) { | ||
| 110 | - case 'Ed25519': | ||
| 111 | - // Fall through | ||
| 112 | - case 'Ed448': | ||
| 113 | - publicUsages = getUsagesUnion(usageSet, 'verify'); | ||
| 114 | - privateUsages = getUsagesUnion(usageSet, 'sign'); | ||
| 115 | - break; | ||
| 116 | - case 'X25519': | ||
| 117 | - // Fall through | ||
| 118 | - case 'X448': | ||
| 119 | - publicUsages = new SafeSet(); | ||
| 120 | - privateUsages = getUsagesUnion(usageSet, 'deriveKey', 'deriveBits'); | ||
| 121 | - break; | ||
| 122 | - } | ||
| 123 | - | ||
| 124 | 78 | const keyAlgorithm = { name }; | |
| 125 | - | ||
| 126 | - if (privateUsages.size === 0) { | ||
| 127 | - throw lazyDOMException( | ||
| 128 | - 'Usages cannot be empty when creating a key.', | ||
| 129 | - 'SyntaxError'); | ||
| 130 | - } | ||
| 79 | + const keyUsages = getKeyPairUsages(usagesSet, allowedUsages); | ||
| 80 | + validateUsagesNotEmpty(keyUsages.private); | ||
| 131 | 81 | ||
| 132 | 82 | return jobPromise(() => new NidKeyPairGenJob( | |
| 133 | 83 | kCryptoJobWebCrypto, | |
| 134 | 84 | nid, | |
| 135 | 85 | keyAlgorithm, | |
| 136 | - getUsagesMask(publicUsages), | ||
| 137 | - getUsagesMask(privateUsages), | ||
| 86 | + getUsagesMask(keyUsages.public), | ||
| 87 | + getUsagesMask(keyUsages.private), | ||
| 138 | 88 | extractable)); | |
| 139 | 89 | } | |
| 140 | 90 | ||
@@ -173,20 +123,25 @@ function cfrgImportKey( | |||
| 173 | 123 | ||
| 174 | 124 | const { name } = algorithm; | |
| 175 | 125 | let handle; | |
| 126 | + const allowedUsages = kUsages[name]; | ||
| 176 | 127 | const usagesSet = new SafeSet(usages); | |
| 177 | 128 | switch (format) { | |
| 178 | 129 | case 'KeyObjectHandle': | |
| 179 | - verifyAcceptableCfrgKeyUse( | ||
| 180 | - name, keyData.getKeyType() === kKeyTypePublic, usagesSet); | ||
| 130 | + verifyAcceptableKeyUse( | ||
| 131 | + name, | ||
| 132 | + usagesSet, | ||
| 133 | + keyData.getKeyType() === kKeyTypePublic ? | ||
| 134 | + allowedUsages.public : | ||
| 135 | + allowedUsages.private); | ||
| 181 | 136 | handle = keyData; | |
| 182 | 137 | break; | |
| 183 | 138 | case 'spki': { | |
| 184 | - verifyAcceptableCfrgKeyUse(name, true, usagesSet); | ||
| 139 | + verifyAcceptableKeyUse(name, usagesSet, allowedUsages.public); | ||
| 185 | 140 | handle = importDerKey(keyData, true); | |
| 186 | 141 | break; | |
| 187 | 142 | } | |
| 188 | 143 | case 'pkcs8': { | |
| 189 | - verifyAcceptableCfrgKeyUse(name, false, usagesSet); | ||
| 144 | + verifyAcceptableKeyUse(name, usagesSet, allowedUsages.private); | ||
| 190 | 145 | handle = importDerKey(keyData, false); | |
| 191 | 146 | break; | |
| 192 | 147 | } | |
@@ -205,12 +160,15 @@ function cfrgImportKey( | |||
| 205 | 160 | } | |
| 206 | 161 | ||
| 207 | 162 | const isPublic = keyData.d === undefined; | |
| 208 | - verifyAcceptableCfrgKeyUse(name, isPublic, usagesSet); | ||
| 163 | + verifyAcceptableKeyUse( | ||
| 164 | + name, | ||
| 165 | + usagesSet, | ||
| 166 | + isPublic ? allowedUsages.public : allowedUsages.private); | ||
| 209 | 167 | handle = importJwkKey(isPublic, keyData); | |
| 210 | 168 | break; | |
| 211 | 169 | } | |
| 212 | 170 | case 'raw': { | |
| 213 | - verifyAcceptableCfrgKeyUse(name, true, usagesSet); | ||
| 171 | + verifyAcceptableKeyUse(name, usagesSet, allowedUsages.public); | ||
| 214 | 172 | handle = importRawKey(true, keyData, kKeyFormatRawPublic, name); | |
| 215 | 173 | break; | |
| 216 | 174 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,9 +1,5 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | - const { | ||
| 4 | - SafeSet, | ||
| 5 | - } = primordials; | ||
| 6 | - | ||
| 7 | 3 | const { | |
| 8 | 4 | ChaCha20Poly1305CipherJob, | |
| 9 | 5 | SecretKeyGenJob, | |
@@ -12,7 +8,6 @@ const { | |||
| 12 | 8 | ||
| 13 | 9 | const { | |
| 14 | 10 | getUsagesMask, | |
| 15 | - hasAnyNotIn, | ||
| 16 | 11 | jobPromise, | |
| 17 | 12 | } = require('internal/crypto/util'); | |
| 18 | 13 | ||
@@ -29,8 +24,12 @@ const { | |||
| 29 | 24 | importJwkSecretKey, | |
| 30 | 25 | importSecretKey, | |
| 31 | 26 | validateJwk, | |
| 27 | + validateKeyUsages, | ||
| 28 | + validateUsagesNotEmpty, | ||
| 32 | 29 | } = require('internal/crypto/webcrypto_util'); | |
| 33 | 30 | ||
| 31 | + const kUsages = ['encrypt', 'decrypt', 'wrapKey', 'unwrapKey']; | ||
| 32 | + | ||
| 34 | 33 | function validateKeyLength(length) { | |
| 35 | 34 | if (length !== 256) | |
| 36 | 35 | throw lazyDOMException('Invalid key length', 'DataError'); | |
@@ -49,19 +48,8 @@ function c20pCipher(mode, key, data, algorithm) { | |||
| 49 | 48 | function c20pGenerateKey(algorithm, extractable, usages) { | |
| 50 | 49 | const { name } = algorithm; | |
| 51 | 50 | ||
| 52 | - const checkUsages = ['encrypt', 'decrypt', 'wrapKey', 'unwrapKey']; | ||
| 53 | - | ||
| 54 | - const usagesSet = new SafeSet(usages); | ||
| 55 | - if (hasAnyNotIn(usagesSet, checkUsages)) { | ||
| 56 | - throw lazyDOMException( | ||
| 57 | - `Unsupported key usage for a ${algorithm.name} key`, | ||
| 58 | - 'SyntaxError'); | ||
| 59 | - } | ||
| 60 | - if (usagesSet.size === 0) { | ||
| 61 | - throw lazyDOMException( | ||
| 62 | - 'Usages cannot be empty when creating a key.', | ||
| 63 | - 'SyntaxError'); | ||
| 64 | - } | ||
| 51 | + const usagesSet = validateUsagesNotEmpty( | ||
| 52 | + validateKeyUsages(usages, kUsages, name)); | ||
| 65 | 53 | ||
| 66 | 54 | return jobPromise(() => new SecretKeyGenJob( | |
| 67 | 55 | kCryptoJobWebCrypto, | |
@@ -78,14 +66,9 @@ function c20pImportKey( | |||
| 78 | 66 | extractable, | |
| 79 | 67 | usages) { | |
| 80 | 68 | const { name } = algorithm; | |
| 81 | - const checkUsages = ['encrypt', 'decrypt', 'wrapKey', 'unwrapKey']; | ||
| 82 | 69 | ||
| 83 | - const usagesSet = new SafeSet(usages); | ||
| 84 | - if (hasAnyNotIn(usagesSet, checkUsages)) { | ||
| 85 | - throw lazyDOMException( | ||
| 86 | - `Unsupported key usage for a ${algorithm.name} key`, | ||
| 87 | - 'SyntaxError'); | ||
| 88 | - } | ||
| 70 | + const usagesSet = validateKeyUsages( | ||
| 71 | + usages, kUsages, name); | ||
| 89 | 72 | ||
| 90 | 73 | let handle; | |
| 91 | 74 | switch (format) { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments