Skip to content

Commit ef918be

Browse files
committed
refactor!: drop support for JWK key_ops and CryptoKey usages "(un)wrapKey" and "deriveKey"
1 parent ebda967 commit ef918be

File tree

3 files changed

+12
-20
lines changed

3 files changed

+12
-20
lines changed

src/lib/crypto_key.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,15 @@ function getNamedCurve(alg: string) {
2626
}
2727
}
2828

29-
function checkUsage(key: CryptoKey, usages: KeyUsage[]) {
30-
if (usages.length && !usages.some((expected) => key.usages.includes(expected))) {
31-
let msg = 'CryptoKey does not support this operation, its usages must include '
32-
if (usages.length > 2) {
33-
const last = usages.pop()
34-
msg += `one of ${usages.join(', ')}, or ${last}.`
35-
} else if (usages.length === 2) {
36-
msg += `one of ${usages[0]} or ${usages[1]}.`
37-
} else {
38-
msg += `${usages[0]}.`
39-
}
40-
41-
throw new TypeError(msg)
29+
function checkUsage(key: CryptoKey, usage?: KeyUsage) {
30+
if (usage && !key.usages.includes(usage)) {
31+
throw new TypeError(
32+
`CryptoKey does not support this operation, its usages must include ${usage}.`,
33+
)
4234
}
4335
}
4436

45-
export function checkSigCryptoKey(key: CryptoKey, alg: string, ...usages: KeyUsage[]) {
37+
export function checkSigCryptoKey(key: CryptoKey, alg: string, usage: KeyUsage) {
4638
switch (alg) {
4739
case 'HS256':
4840
case 'HS384':
@@ -95,10 +87,10 @@ export function checkSigCryptoKey(key: CryptoKey, alg: string, ...usages: KeyUsa
9587
throw new TypeError('CryptoKey does not support this operation')
9688
}
9789

98-
checkUsage(key, usages)
90+
checkUsage(key, usage)
9991
}
10092

101-
export function checkEncCryptoKey(key: CryptoKey, alg: string, ...usages: KeyUsage[]) {
93+
export function checkEncCryptoKey(key: CryptoKey, alg: string, usage?: KeyUsage) {
10294
switch (alg) {
10395
case 'A128GCM':
10496
case 'A192GCM':
@@ -148,5 +140,5 @@ export function checkEncCryptoKey(key: CryptoKey, alg: string, ...usages: KeyUsa
148140
throw new TypeError('CryptoKey does not support this operation')
149141
}
150142

151-
checkUsage(key, usages)
143+
checkUsage(key, usage)
152144
}

src/runtime/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export async function generateKeyPair(alg: string, options?: GenerateKeyPairOpti
130130
case 'ECDH-ES+A128KW':
131131
case 'ECDH-ES+A192KW':
132132
case 'ECDH-ES+A256KW': {
133-
keyUsages = ['deriveKey', 'deriveBits']
133+
keyUsages = ['deriveBits']
134134
const crv = options?.crv ?? 'P-256'
135135
switch (crv) {
136136
case 'P-256':

src/runtime/normalize_key.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const handleKeyObject = (key: ConvertableKeyObject, alg: string) => {
6363
cryptoKey = key.toCryptoKey(
6464
key.asymmetricKeyType,
6565
true,
66-
key.type === 'private' ? ['deriveBits', 'deriveKey'] : [],
66+
key.type === 'private' ? ['deriveBits'] : [],
6767
)
6868
}
6969

@@ -176,7 +176,7 @@ const handleKeyObject = (key: ConvertableKeyObject, alg: string) => {
176176
namedCurve,
177177
},
178178
true,
179-
key.type === 'private' ? ['deriveBits', 'deriveKey'] : [],
179+
key.type === 'private' ? ['deriveBits'] : [],
180180
)
181181
}
182182
}

0 commit comments

Comments
 (0)