Skip to content

Commit

Permalink
refactor: cleanup NODE-ED25519 workerd workarounds
Browse files Browse the repository at this point in the history
Both workerd and the live service now support the Ed25519 and X25519
identifiers.
  • Loading branch information
panva committed Apr 30, 2023
1 parent 9b234dd commit 072e83d
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 83 deletions.
12 changes: 11 additions & 1 deletion src/jwks/remote.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import fetchJwks from '../runtime/fetch_jwks.js'
import { isCloudflareWorkers } from '../runtime/env.js'

import type { KeyLike, JWSHeaderParameters, FlattenedJWSInput } from '../types.d'
import { JWKSInvalid, JWKSNoMatchingKey } from '../util/errors.js'

import { isJWKSLike, LocalJWKSet } from './local.js'

function isCloudflareWorkers() {
return (
// @ts-ignore
typeof WebSocketPair !== 'undefined' ||
// @ts-ignore
(typeof navigator !== 'undefined' && navigator.userAgent === 'Cloudflare-Workers') ||
// @ts-ignore
(typeof EdgeRuntime !== 'undefined' && EdgeRuntime === 'vercel')
)
}

/** Options for the remote JSON Web Key Set. */
export interface RemoteJWKSetOptions {
/**
Expand Down
6 changes: 0 additions & 6 deletions src/lib/crypto_key.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { isCloudflareWorkers } from '../runtime/env.js'

function unusable(name: string | number, prop = 'algorithm.name') {
return new TypeError(`CryptoKey does not support this operation, its ${prop} must be ${name}`)
}
Expand Down Expand Up @@ -73,10 +71,6 @@ export function checkSigCryptoKey(key: CryptoKey, alg: string, ...usages: KeyUsa
}
case 'EdDSA': {
if (key.algorithm.name !== 'Ed25519' && key.algorithm.name !== 'Ed448') {
if (isCloudflareWorkers()) {
if (isAlgorithm(key.algorithm, 'NODE-ED25519')) break
throw unusable('Ed25519, Ed448, or NODE-ED25519')
}
throw unusable('Ed25519 or Ed448')
}
break
Expand Down
33 changes: 7 additions & 26 deletions src/runtime/browser/asn1.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isCloudflareWorkers } from './env.js'
import crypto, { isCryptoKey } from './webcrypto.js'
import type { PEMExportFunction, PEMImportFunction } from '../interfaces.d'
import invalidKeyInput from '../../lib/invalid_key_input.js'
Expand Down Expand Up @@ -143,31 +142,13 @@ const genericImport = async (
throw new JOSENotSupported('Invalid or unsupported "alg" (Algorithm) value')
}

try {
return await crypto.subtle.importKey(
keyFormat,
keyData,
algorithm,
options?.extractable ?? false,
keyUsages,
)
} catch (err) {
if (
algorithm.name === 'Ed25519' &&
(<Error>err)?.name === 'NotSupportedError' &&
isCloudflareWorkers()
) {
algorithm = { name: 'NODE-ED25519', namedCurve: 'NODE-ED25519' }
return await crypto.subtle.importKey(
keyFormat,
keyData,
algorithm,
options?.extractable ?? false,
keyUsages,
)
}
throw err
}
return crypto.subtle.importKey(
keyFormat,
keyData,
algorithm,
options?.extractable ?? false,
keyUsages,
)
}

export const fromPKCS8: PEMImportFunction = (pem, alg, options?) => {
Expand Down
10 changes: 0 additions & 10 deletions src/runtime/browser/env.ts

This file was deleted.

21 changes: 3 additions & 18 deletions src/runtime/browser/generate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isCloudflareWorkers } from './env.js'
import crypto from './webcrypto.js'
import { JOSENotSupported } from '../../util/errors.js'
import random from './random.js'
Expand Down Expand Up @@ -149,21 +148,7 @@ export async function generateKeyPair(alg: string, options?: GenerateKeyPairOpti
throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value')
}

try {
return <{ publicKey: CryptoKey; privateKey: CryptoKey }>(
await crypto.subtle.generateKey(algorithm, options?.extractable ?? false, keyUsages)
)
} catch (err) {
if (
algorithm.name === 'Ed25519' &&
(<Error>err)?.name === 'NotSupportedError' &&
isCloudflareWorkers()
) {
algorithm = { name: 'NODE-ED25519', namedCurve: 'NODE-ED25519' }
return <{ publicKey: CryptoKey; privateKey: CryptoKey }>(
await crypto.subtle.generateKey(algorithm, options?.extractable ?? false, keyUsages)
)
}
throw err
}
return <Promise<{ publicKey: CryptoKey; privateKey: CryptoKey }>>(
crypto.subtle.generateKey(algorithm, options?.extractable ?? false, keyUsages)
)
}
15 changes: 1 addition & 14 deletions src/runtime/browser/jwk_to_key.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isCloudflareWorkers } from './env.js'
import crypto from './webcrypto.js'
import type { JWKImportFunction } from '../interfaces.d'
import { JOSENotSupported } from '../../util/errors.js'
Expand Down Expand Up @@ -150,18 +149,6 @@ const parse: JWKImportFunction = async (jwk: JWK): Promise<CryptoKey> => {
const keyData: JWK = { ...jwk }
delete keyData.alg
delete keyData.use
try {
return await crypto.subtle.importKey('jwk', keyData, ...rest)
} catch (err) {
if (
algorithm.name === 'Ed25519' &&
(<Error>err)?.name === 'NotSupportedError' &&
isCloudflareWorkers()
) {
rest[0] = { name: 'NODE-ED25519', namedCurve: 'NODE-ED25519' }
return await crypto.subtle.importKey('jwk', keyData, ...rest)
}
throw err
}
return crypto.subtle.importKey('jwk', keyData, ...rest)
}
export default parse
5 changes: 0 additions & 5 deletions src/runtime/browser/subtle_dsa.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isCloudflareWorkers } from './env.js'
import { JOSENotSupported } from '../../util/errors.js'

export default function subtleDsa(alg: string, algorithm: KeyAlgorithm | EcKeyAlgorithm) {
Expand All @@ -22,10 +21,6 @@ export default function subtleDsa(alg: string, algorithm: KeyAlgorithm | EcKeyAl
case 'ES512':
return { hash, name: 'ECDSA', namedCurve: (<EcKeyAlgorithm>algorithm).namedCurve }
case 'EdDSA':
if (isCloudflareWorkers() && algorithm.name === 'NODE-ED25519') {
return { name: 'NODE-ED25519', namedCurve: 'NODE-ED25519' }
}

return { name: algorithm.name }
default:
throw new JOSENotSupported(
Expand Down
3 changes: 0 additions & 3 deletions src/runtime/node/env.ts

This file was deleted.

0 comments on commit 072e83d

Please sign in to comment.