deno.land / x / jose@v5.2.4 / key / 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
import { decode as decodeBase64URL } from '../runtime/base64url.ts'import { fromSPKI, fromPKCS8, fromX509 } from '../runtime/asn1.ts'import asKeyObject from '../runtime/jwk_to_key.ts'
import { JOSENotSupported } from '../util/errors.ts'import isObject from '../lib/is_object.ts'import type { JWK, KeyLike } from '../types.d.ts'
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). * * Note: The OID id-RSASSA-PSS (1.2.840.113549.1.1.10) is not supported in * {@link https://w3c.github.io/webcrypto/ Web Cryptography API}, use the OID rsaEncryption * (1.2.840.113549.1.1.1) instead for all RSA algorithms. * * @param spki 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<KeyLikeType extends KeyLike = KeyLike>( spki: string, alg: string, options?: PEMImportOptions,): Promise<KeyLikeType> { 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). * * Note: The OID id-RSASSA-PSS (1.2.840.113549.1.1.10) is not supported in * {@link https://w3c.github.io/webcrypto/ Web Cryptography API}, use the OID rsaEncryption * (1.2.840.113549.1.1.1) instead for all RSA algorithms. * * @param x509 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<KeyLikeType extends KeyLike = KeyLike>( x509: string, alg: string, options?: PEMImportOptions,): Promise<KeyLikeType> { 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). * * Note: The OID id-RSASSA-PSS (1.2.840.113549.1.1.10) is not supported in * {@link https://w3c.github.io/webcrypto/ Web Cryptography API}, use the OID rsaEncryption * (1.2.840.113549.1.1.1) instead for all RSA algorithms. * * @param pkcs8 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<KeyLikeType extends KeyLike = KeyLike>( pkcs8: string, alg: string, options?: PEMImportOptions,): Promise<KeyLikeType> { 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 the JWK "alg" * (Algorithm) Parameter, or the optional "alg" argument, must be present. * * Note: When the runtime is using {@link https://w3c.github.io/webcrypto/ Web Cryptography API} the * jwk parameters "use", "key_ops", and "ext" are also used in the resulting `CryptoKey`. * * @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<KeyLikeType extends KeyLike = KeyLike>( jwk: JWK, alg?: string,): Promise<KeyLikeType | 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') }}
jose

Version Info

Tagged at
a month ago