-
-
Notifications
You must be signed in to change notification settings - Fork 299
/
import.ts
186 lines (177 loc) · 6.55 KB
/
import.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { decode as decodeBase64URL } from '../runtime/base64url.js'
import { fromSPKI, fromPKCS8, fromX509 } from '../runtime/asn1.js'
import asKeyObject from '../runtime/jwk_to_key.js'
import { JOSENotSupported } from '../util/errors.js'
import isObject from '../lib/is_object.js'
import type { JWK, KeyLike } from '../types.d'
export interface PEMImportOptions {
/**
* (Only effective in Web Crypto API runtimes) The value to use as
* {@link https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey SubtleCrypto.importKey()}
* `extractable` argument. Default is false.
*/
extractable?: boolean
}
/**
* Imports a PEM-encoded SPKI string as a runtime-specific public key representation (KeyObject or
* CryptoKey).
*
* @example
*
* ```js
* const algorithm = 'ES256'
* const spki = `-----BEGIN PUBLIC KEY-----
* MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEFlHHWfLk0gLBbsLTcuCrbCqoHqmM
* YJepMC+Q+Dd6RBmBiA41evUsNMwLeN+PNFqib+xwi9JkJ8qhZkq8Y/IzGg==
* -----END PUBLIC KEY-----`
* const ecPublicKey = await jose.importSPKI(spki, algorithm)
* ```
*
* @param pem PEM-encoded SPKI string
* @param alg (Only effective in Web Crypto API runtimes) JSON Web Algorithm identifier to be used
* with the imported key, its presence is only enforced in Web Crypto API runtimes. See
* {@link https://github.com/panva/jose/issues/210 Algorithm Key Requirements}.
*/
export async function importSPKI<T extends KeyLike = KeyLike>(
spki: string,
alg: string,
options?: PEMImportOptions,
): Promise<T> {
if (typeof spki !== 'string' || spki.indexOf('-----BEGIN PUBLIC KEY-----') !== 0) {
throw new TypeError('"spki" must be SPKI formatted string')
}
// @ts-ignore
return fromSPKI(spki, alg, options)
}
/**
* Imports the SPKI from an X.509 string certificate as a runtime-specific public key representation
* (KeyObject or CryptoKey).
*
* @example
*
* ```js
* const algorithm = 'ES256'
* const x509 = `-----BEGIN CERTIFICATE-----
* MIIBXjCCAQSgAwIBAgIGAXvykuMKMAoGCCqGSM49BAMCMDYxNDAyBgNVBAMMK3Np
* QXBNOXpBdk1VaXhXVWVGaGtjZXg1NjJRRzFyQUhXaV96UlFQTVpQaG8wHhcNMjEw
* OTE3MDcwNTE3WhcNMjIwNzE0MDcwNTE3WjA2MTQwMgYDVQQDDCtzaUFwTTl6QXZN
* VWl4V1VlRmhrY2V4NTYyUUcxckFIV2lfelJRUE1aUGhvMFkwEwYHKoZIzj0CAQYI
* KoZIzj0DAQcDQgAE8PbPvCv5D5xBFHEZlBp/q5OEUymq7RIgWIi7tkl9aGSpYE35
* UH+kBKDnphJO3odpPZ5gvgKs2nwRWcrDnUjYLDAKBggqhkjOPQQDAgNIADBFAiEA
* 1yyMTRe66MhEXID9+uVub7woMkNYd0LhSHwKSPMUUTkCIFQGsfm1ecXOpeGOufAh
* v+A1QWZMuTWqYt+uh/YSRNDn
* -----END CERTIFICATE-----`
* const ecPublicKey = await jose.importX509(x509, algorithm)
* ```
*
* @param pem X.509 certificate string
* @param alg (Only effective in Web Crypto API runtimes) JSON Web Algorithm identifier to be used
* with the imported key, its presence is only enforced in Web Crypto API runtimes. See
* {@link https://github.com/panva/jose/issues/210 Algorithm Key Requirements}.
*/
export async function importX509<T extends KeyLike = KeyLike>(
x509: string,
alg: string,
options?: PEMImportOptions,
): Promise<T> {
if (typeof x509 !== 'string' || x509.indexOf('-----BEGIN CERTIFICATE-----') !== 0) {
throw new TypeError('"x509" must be X.509 formatted string')
}
// @ts-ignore
return fromX509(x509, alg, options)
}
/**
* Imports a PEM-encoded PKCS#8 string as a runtime-specific private key representation (KeyObject
* or CryptoKey).
*
* @example
*
* ```js
* const algorithm = 'ES256'
* const pkcs8 = `-----BEGIN PRIVATE KEY-----
* MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgiyvo0X+VQ0yIrOaN
* nlrnUclopnvuuMfoc8HHly3505OhRANCAAQWUcdZ8uTSAsFuwtNy4KtsKqgeqYxg
* l6kwL5D4N3pEGYGIDjV69Sw0zAt43480WqJv7HCL0mQnyqFmSrxj8jMa
* -----END PRIVATE KEY-----`
* const ecPrivateKey = await jose.importPKCS8(pkcs8, algorithm)
* ```
*
* @param pem PEM-encoded PKCS#8 string
* @param alg (Only effective in Web Crypto API runtimes) JSON Web Algorithm identifier to be used
* with the imported key, its presence is only enforced in Web Crypto API runtimes. See
* {@link https://github.com/panva/jose/issues/210 Algorithm Key Requirements}.
*/
export async function importPKCS8<T extends KeyLike = KeyLike>(
pkcs8: string,
alg: string,
options?: PEMImportOptions,
): Promise<T> {
if (typeof pkcs8 !== 'string' || pkcs8.indexOf('-----BEGIN PRIVATE KEY-----') !== 0) {
throw new TypeError('"pkcs8" must be PKCS#8 formatted string')
}
// @ts-ignore
return fromPKCS8(pkcs8, alg, options)
}
/**
* Imports a JWK to a runtime-specific key representation (KeyLike). Either JWK "alg" (Algorithm)
* Parameter must be present or the optional "alg" argument. When running on a runtime using
* {@link https://www.w3.org/TR/WebCryptoAPI/ Web Cryptography API} the jwk parameters "use",
* "key_ops", and "ext" are also used in the resulting `CryptoKey`.
*
* @example
*
* ```js
* const ecPublicKey = await jose.importJWK(
* {
* crv: 'P-256',
* kty: 'EC',
* x: 'ySK38C1jBdLwDsNWKzzBHqKYEE5Cgv-qjWvorUXk9fw',
* y: '_LeQBw07cf5t57Iavn4j-BqJsAD1dpoz8gokd3sBsOo',
* },
* 'ES256',
* )
*
* const rsaPublicKey = await jose.importJWK(
* {
* kty: 'RSA',
* e: 'AQAB',
* n: '12oBZRhCiZFJLcPg59LkZZ9mdhSMTKAQZYq32k_ti5SBB6jerkh-WzOMAO664r_qyLkqHUSp3u5SbXtseZEpN3XPWGKSxjsy-1JyEFTdLSYe6f9gfrmxkUF_7DTpq0gn6rntP05g2-wFW50YO7mosfdslfrTJYWHFhJALabAeYirYD7-9kqq9ebfFMF4sRRELbv9oi36As6Q9B3Qb5_C1rAzqfao_PCsf9EPsTZsVVVkA5qoIAr47lo1ipfiBPxUCCNSdvkmDTYgvvRm6ZoMjFbvOtgyts55fXKdMWv7I9HMD5HwE9uW839PWA514qhbcIsXEYSFMPMV6fnlsiZvQQ',
* },
* 'PS256',
* )
* ```
*
* @param jwk JSON Web Key.
* @param alg (Only effective in Web Crypto API runtimes) JSON Web Algorithm identifier to be used
* with the imported key. Default is the "alg" property on the JWK, its presence is only enforced
* in Web Crypto API runtimes. See
* {@link https://github.com/panva/jose/issues/210 Algorithm Key Requirements}.
*/
export async function importJWK<T extends KeyLike = KeyLike>(
jwk: JWK,
alg?: string,
): Promise<T | Uint8Array> {
if (!isObject(jwk)) {
throw new TypeError('JWK must be an object')
}
alg ||= jwk.alg
switch (jwk.kty) {
case 'oct':
if (typeof jwk.k !== 'string' || !jwk.k) {
throw new TypeError('missing "k" (Key Value) Parameter value')
}
return decodeBase64URL(jwk.k)
case 'RSA':
if (jwk.oth !== undefined) {
throw new JOSENotSupported(
'RSA JWK "oth" (Other Primes Info) Parameter value is not supported',
)
}
case 'EC':
case 'OKP':
// @ts-ignore
return asKeyObject({ ...jwk, alg })
default:
throw new JOSENotSupported('Unsupported "kty" (Key Type) Parameter value')
}
}