-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
generate.ts
126 lines (120 loc) · 3.98 KB
/
generate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import crypto from './webcrypto.js'
import { JOSENotSupported } from '../../util/errors.js'
import random from './random.js'
import type { GenerateKeyPairOptions } from '../../util/generate_key_pair.js'
import type { GenerateSecretOptions } from '../../util/generate_secret.js'
export async function generateSecret(alg: string, options?: GenerateSecretOptions) {
let length: number
// @deno-expect-error
let algorithm: AesKeyGenParams | HmacKeyGenParams
let keyUsages: KeyUsage[]
switch (alg) {
case 'HS256':
case 'HS384':
case 'HS512':
length = parseInt(alg.substr(-3), 10)
algorithm = { name: 'HMAC', hash: { name: `SHA-${length}` }, length }
keyUsages = ['sign', 'verify']
break
case 'A128CBC-HS256':
case 'A192CBC-HS384':
case 'A256CBC-HS512':
length = parseInt(alg.substr(-3), 10)
return random(new Uint8Array(length >> 3))
case 'A128KW':
case 'A192KW':
case 'A256KW':
length = parseInt(alg.substring(1, 4), 10)
algorithm = { name: 'AES-KW', length }
keyUsages = ['wrapKey', 'unwrapKey']
break
case 'A128GCMKW':
case 'A192GCMKW':
case 'A256GCMKW':
case 'A128GCM':
case 'A192GCM':
case 'A256GCM':
length = parseInt(alg.substring(1, 4), 10)
algorithm = { name: 'AES-GCM', length }
keyUsages = ['encrypt', 'decrypt']
break
default:
throw new JOSENotSupported('unsupported or invalid JWK "alg" (Algorithm) Parameter value')
}
return <Promise<CryptoKey>>(
(<unknown>crypto.subtle.generateKey(algorithm, options?.extractable ?? false, keyUsages))
)
}
function getModulusLengthOption(options?: GenerateKeyPairOptions) {
const modulusLength = options?.modulusLength ?? 2048
if (typeof modulusLength !== 'number' || modulusLength < 2048) {
throw new JOSENotSupported(
'Invalid or unsupported modulusLength option provided, 2048 bits or larger keys must be used',
)
}
return modulusLength
}
export async function generateKeyPair(alg: string, options?: GenerateKeyPairOptions) {
let algorithm: RsaHashedKeyGenParams | EcKeyGenParams
let keyUsages: KeyUsage[]
switch (alg) {
case 'PS256':
case 'PS384':
case 'PS512':
algorithm = {
name: 'RSA-PSS',
hash: { name: `SHA-${alg.substr(-3)}` },
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
modulusLength: getModulusLengthOption(options),
}
keyUsages = ['sign', 'verify']
break
case 'RS256':
case 'RS384':
case 'RS512':
algorithm = {
name: 'RSASSA-PKCS1-v1_5',
hash: { name: `SHA-${alg.substr(-3)}` },
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
modulusLength: getModulusLengthOption(options),
}
keyUsages = ['sign', 'verify']
break
case 'RSA-OAEP':
case 'RSA-OAEP-256':
case 'RSA-OAEP-384':
case 'RSA-OAEP-512':
algorithm = {
name: 'RSA-OAEP',
hash: { name: `SHA-${parseInt(alg.substr(-3), 10) || 1}` },
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
modulusLength: getModulusLengthOption(options),
}
keyUsages = ['decrypt', 'unwrapKey', 'encrypt', 'wrapKey']
break
case 'ES256':
algorithm = { name: 'ECDSA', namedCurve: 'P-256' }
keyUsages = ['sign', 'verify']
break
case 'ES384':
algorithm = { name: 'ECDSA', namedCurve: 'P-384' }
keyUsages = ['sign', 'verify']
break
case 'ES512':
algorithm = { name: 'ECDSA', namedCurve: 'P-521' }
keyUsages = ['sign', 'verify']
break
case 'ECDH-ES':
case 'ECDH-ES+A128KW':
case 'ECDH-ES+A192KW':
case 'ECDH-ES+A256KW':
algorithm = { name: 'ECDH', namedCurve: options?.crv ?? 'P-256' }
keyUsages = ['deriveKey', 'deriveBits']
break
default:
throw new JOSENotSupported('unsupported or invalid JWK "alg" (Algorithm) Parameter value')
}
return <Promise<CryptoKeyPair>>(
crypto.subtle.generateKey(algorithm, options?.extractable ?? false, keyUsages)
)
}