Skip to content

Commit 7a94cb9

Browse files
committed
refactor: backport the Ed25519 JWS Algorithm Identifier support
1 parent aa590d5 commit 7a94cb9

File tree

12 files changed

+18
-34
lines changed

12 files changed

+18
-34
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"detached",
1414
"ec",
1515
"ecdsa",
16+
"ed25519",
1617
"eddsa",
1718
"edge",
1819
"electron",

src/jwks/local.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,7 @@ class LocalJWKSet {
9393
candidate = jwk.key_ops.includes('verify')
9494
}
9595

96-
// filter out non-applicable OKP Sub Types
97-
if (candidate && alg === 'EdDSA') {
98-
candidate = jwk.crv === 'Ed25519'
99-
}
100-
101-
// filter out non-applicable EC curves
96+
// filter out non-applicable curves / sub types
10297
if (candidate) {
10398
switch (alg) {
10499
case 'ES256':
@@ -110,11 +105,9 @@ class LocalJWKSet {
110105
case 'ES512':
111106
candidate = jwk.crv === 'P-521'
112107
break
113-
case 'Ed25519':
114-
candidate = jwk.crv === 'Ed25519'
115-
break
108+
case 'Ed25519': // Fall through
116109
case 'EdDSA':
117-
candidate = jwk.crv === 'Ed25519' || jwk.crv === 'Ed448'
110+
candidate = jwk.crv === 'Ed25519'
118111
break
119112
}
120113
}

src/key/generate_key_pair.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ export async function generateKeyPair(
125125
algorithm = { name: 'ECDSA', namedCurve: 'P-521' }
126126
keyUsages = ['sign', 'verify']
127127
break
128+
case 'Ed25519': // Fall through
128129
case 'EdDSA': {
129130
keyUsages = ['sign', 'verify']
130131
algorithm = { name: 'Ed25519' }

src/lib/asn1.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,7 @@ const genericImport = async (
129129
keyUsages = isPublic ? [] : ['deriveBits']
130130
break
131131
}
132-
case 'Ed25519':
133-
algorithm = { name: 'Ed25519' }
134-
keyUsages = isPublic ? ['verify'] : ['sign']
135-
break
132+
case 'Ed25519': // Fall through
136133
case 'EdDSA':
137134
algorithm = { name: 'Ed25519' }
138135
keyUsages = isPublic ? ['verify'] : ['sign']

src/lib/crypto_key.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,11 @@ export function checkSigCryptoKey(key: types.CryptoKey, alg: string, usage: KeyU
6666
if (actual !== expected) throw unusable(`SHA-${expected}`, 'algorithm.hash')
6767
break
6868
}
69+
case 'Ed25519': // Fall through
6970
case 'EdDSA': {
7071
if (!isAlgorithm(key.algorithm, 'Ed25519')) throw unusable('Ed25519')
7172
break
7273
}
73-
case 'Ed25519': {
74-
if (!isAlgorithm(key.algorithm, 'Ed25519')) throw unusable('Ed25519')
75-
break
76-
}
7774
case 'ES256':
7875
case 'ES384':
7976
case 'ES512': {

src/lib/jwk_to_key.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ function subtleMapping(jwk: types.JWK): {
6666
}
6767
case 'OKP': {
6868
switch (jwk.alg) {
69-
case 'Ed25519':
70-
algorithm = { name: 'Ed25519' }
71-
keyUsages = jwk.d ? ['sign'] : ['verify']
72-
break
69+
case 'Ed25519': // Fall through
7370
case 'EdDSA':
7471
algorithm = { name: 'Ed25519' }
7572
keyUsages = jwk.d ? ['sign'] : ['verify']

src/lib/normalize_key.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const handleKeyObject = (keyObject: ConvertableKeyObject, alg: string) => {
7676
}
7777

7878
if (keyObject.asymmetricKeyType === 'ed25519') {
79-
if (alg !== 'EdDSA') {
79+
if (alg !== 'EdDSA' && alg !== 'Ed25519') {
8080
throw new TypeError('given KeyObject instance cannot be used for this algorithm')
8181
}
8282

src/lib/subtle_dsa.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ export default (alg: string, algorithm: KeyAlgorithm | EcKeyAlgorithm) => {
1919
case 'ES384':
2020
case 'ES512':
2121
return { hash, name: 'ECDSA', namedCurve: (algorithm as EcKeyAlgorithm).namedCurve }
22-
case 'Ed25519':
23-
return { name: 'Ed25519' }
22+
case 'Ed25519': // Fall through
2423
case 'EdDSA':
2524
return { name: 'Ed25519' }
2625
default:

tap/jwk.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export default (
2828
(env.isGecko && env.isBrowserVersionAtLeast(130)) ||
2929
(env.isBlink && env.isBrowserVersionAtLeast(133)),
3030
],
31+
['Ed25519', KEYS.Ed25519.jwk, !env.isBlink],
3132
['EdDSA', KEYS.Ed25519.jwk, !env.isBlink],
3233
['ES256', KEYS.P256.jwk, true],
3334
['ES384', KEYS.P384.jwk, true],

tap/jws.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default (
1313

1414
type Vector = [string, boolean] | [string, boolean, jose.GenerateKeyPairOptions]
1515
const algorithms: Vector[] = [
16+
['Ed25519', !env.isBlink],
1617
['EdDSA', !env.isBlink],
1718
['ES256', true],
1819
['ES384', true],

tap/keyobject-stub.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,9 @@ const stub: Pick<
8787
return generate('ec', { namedCurve: 'P-384' })
8888
case 'ES512':
8989
return generate('ec', { namedCurve: 'P-521' })
90-
case 'EdDSA': {
91-
switch (options?.crv) {
92-
case undefined:
93-
case 'Ed25519':
94-
return generate('ed25519')
95-
default:
96-
throw new Error('unreachable')
97-
}
98-
}
90+
case 'Ed25519': // Fall through
91+
case 'EdDSA':
92+
return generate('ed25519')
9993
case 'ECDH-ES':
10094
case 'ECDH-ES+A128KW':
10195
case 'ECDH-ES+A192KW':

tap/pem.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ export default (
8787
(env.isGecko && env.isBrowserVersionAtLeast(130)) ||
8888
(env.isBlink && env.isBrowserVersionAtLeast(133)),
8989
],
90+
['Ed25519', KEYS.Ed25519.pkcs8, !env.isBlink],
91+
['Ed25519', KEYS.Ed25519.spki, !env.isBlink],
92+
['Ed25519', KEYS.Ed25519.x509, !env.isBlink],
9093
[['EdDSA', 'Ed25519'], KEYS.Ed25519.pkcs8, !env.isBlink],
9194
[['EdDSA', 'Ed25519'], KEYS.Ed25519.spki, !env.isBlink],
9295
[['EdDSA', 'Ed25519'], KEYS.Ed25519.x509, !env.isBlink],

0 commit comments

Comments
 (0)