unjwt 0.8.5 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -21
- package/dist/_chunks/algorithms.mjs +44 -20
- package/dist/_chunks/digest.d.mts +4 -4
- package/dist/_chunks/digest.mjs +23 -15
- package/dist/_chunks/error.d.mts +29 -42
- package/dist/_chunks/export.mjs +1 -4
- package/dist/_chunks/index.d.mts +270 -234
- package/dist/_chunks/index2.d.mts +82 -189
- package/dist/_chunks/index4.d.mts +223 -60
- package/dist/_chunks/index5.d.mts +68 -31
- package/dist/_chunks/index6.d.mts +45 -64
- package/dist/_chunks/jwe.mjs +53 -48
- package/dist/_chunks/jwk.mjs +9 -3
- package/dist/_chunks/jws.mjs +55 -50
- package/dist/_chunks/jwt.d.mts +13 -49
- package/dist/_chunks/jwt.mjs +4 -158
- package/dist/_chunks/jwt2.mjs +162 -0
- package/dist/_chunks/libs/unsecure.d.mts +0 -6
- package/dist/_chunks/limits.d.mts +3 -2
- package/dist/_chunks/payload.mjs +13 -7
- package/dist/_chunks/wrap.mjs +3 -1
- package/dist/core/internal.d.mts +50 -2
- package/dist/core/internal.mjs +3 -1
- package/dist/core/jwe/index.d.mts +1 -1
- package/dist/core/jwk/index.d.mts +2 -2
- package/dist/core/jwk/index.mjs +2 -2
- package/dist/dpop/index.d.mts +77 -18
- package/dist/index.d.mts +6 -6
- package/dist/index.mjs +4 -4
- package/dist/session/index.d.mts +568 -22
- package/dist/session/index.mjs +1386 -34
- package/dist/utils/index.d.mts +4 -4
- package/dist/utils/index.mjs +2 -10
- package/package.json +4 -32
- package/dist/_chunks/codec-jwe.d.mts +0 -280
- package/dist/_chunks/codec-jws.mjs +0 -1005
- package/dist/_chunks/cookie-attributes.d.mts +0 -12
- package/dist/_chunks/cookie.mjs +0 -50
- package/dist/adapters/elysia/index.d.mts +0 -123
- package/dist/adapters/elysia/index.mjs +0 -120
- package/dist/adapters/h3/index.d.mts +0 -135
- package/dist/adapters/h3/index.mjs +0 -117
- package/dist/adapters/h3v1/index.d.mts +0 -148
- package/dist/adapters/h3v1/index.mjs +0 -128
|
@@ -1,206 +1,99 @@
|
|
|
1
1
|
import "./error.mjs";
|
|
2
|
-
import { ContentEncryptionAlgorithm,
|
|
3
|
-
import "./
|
|
2
|
+
import { ContentEncryptionAlgorithm, JOSEPayload, JOSEPayloadMode, JWEDecryptJWK, JWEDecryptOptions, JWEDecryptResult, JWEEncryptJWK, JWEEncryptOptions, JWEFlattenedSerialization, JWEGeneralSerialization, JWEKeyLookup, JWEMultiDecryptKey, JWEMultiDecryptOptions, JWEMultiDecryptResult, JWEMultiEncryptOptions, JWEMultiRecipient, JWKSet, JWTClaims, KeyManagementAlgorithm, PayloadOf } from "./index.mjs";
|
|
3
|
+
import "./limits.mjs";
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
|
|
8
|
-
export declare class WeakMapJWKCache implements JWKCacheAdapter {
|
|
9
|
-
private readonly _map;
|
|
10
|
-
get(jwk: JWK, alg: JWKAlgorithm): CryptoKey | undefined;
|
|
11
|
-
set(jwk: JWK, alg: JWKAlgorithm, key: CryptoKey): void;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Replaces the JWK import cache used by `importKey`, or disables it with `false`.
|
|
15
|
-
*
|
|
16
|
-
* A cache shared across JWK objects keys on the RFC 7638 thumbprint, the pair half (`d` present)
|
|
17
|
-
* and `alg`: the thumbprint covers public members only, and a `kid` may name different keys.
|
|
18
|
-
* `get` and `set` may return promises.
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```ts
|
|
22
|
-
* const cacheKey = async (jwk: JWK, alg: JWKAlgorithm) =>
|
|
23
|
-
* `${await thumbprint(jwk)}:${"d" in jwk ? "private" : "public"}:${alg}`;
|
|
24
|
-
* const map = new Map<string, CryptoKey>();
|
|
25
|
-
* configureJWKCache({
|
|
26
|
-
* get: async (jwk, alg) => map.get(await cacheKey(jwk, alg)),
|
|
27
|
-
* set: async (jwk, alg, key) => {
|
|
28
|
-
* map.set(await cacheKey(jwk, alg), key);
|
|
29
|
-
* },
|
|
30
|
-
* });
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
export declare function configureJWKCache(cache: JWKCacheAdapter | false): void;
|
|
34
|
-
/** Resets the JWK import cache to a fresh `WeakMapJWKCache`. */
|
|
35
|
-
export declare function clearJWKCache(): void;
|
|
36
|
-
/** Imports the secret of an `oct` JWK as a `CryptoKey` under `algorithm` and `usage`. */
|
|
37
|
-
export declare function importKey(key: JWK_oct, options: {
|
|
38
|
-
asCryptoKey: true;
|
|
39
|
-
/** Algorithm to import the key as (e.g. `{ name: "AES-GCM", length: 256 }`). */
|
|
40
|
-
algorithm: Parameters<typeof crypto.subtle.importKey>[2];
|
|
41
|
-
usage: KeyUsage[];
|
|
42
|
-
/** Mark the key as extractable. Defaults to `false`. */
|
|
43
|
-
extractable?: boolean;
|
|
44
|
-
}): Promise<CryptoKey>;
|
|
45
|
-
/**
|
|
46
|
-
* Imports key material: a `CryptoKey` or `Uint8Array` is returned as is, an `oct` JWK as its raw
|
|
47
|
-
* bytes, an asymmetric JWK as a `CryptoKey` under its own `alg` or the hint. A string throws
|
|
48
|
-
* `ERR_JWK_INVALID`.
|
|
49
|
-
*
|
|
50
|
-
* `expect` and `extractable` apply to asymmetric keys only; `expect` rejects the other pair half
|
|
51
|
-
* with `ERR_JWK_INVALID`. A JWK's `alg` is authoritative (RFC 7517 §4.4): a different requested
|
|
52
|
-
* `alg` throws `ERR_JWK_INVALID`, except an `oct` JWK declaring a content encryption `alg` under
|
|
53
|
-
* `"dir"`.
|
|
54
|
-
*/
|
|
55
|
-
export declare function importKey<K extends CryptoKey | JWK | Uint8Array<ArrayBuffer>>(key: K, options?: JWKAlgorithm | {
|
|
56
|
-
/** Algorithm hint. Required when `key.alg` is absent on an asymmetric JWK. */
|
|
57
|
-
alg?: JWKAlgorithm;
|
|
58
|
-
/** Reject on shape mismatch instead of silently importing. */
|
|
59
|
-
expect?: "public" | "private";
|
|
60
|
-
/** Defaults to `false` for private, `true` for public material; JWK `ext: false` wins. */
|
|
61
|
-
extractable?: boolean;
|
|
62
|
-
}): Promise<ImportedKey<K>>;
|
|
63
|
-
/**
|
|
64
|
-
* Returns the JWKs of a JWK Set, dropping non-JWK entries, optionally narrowed by `filter`.
|
|
65
|
-
* An invalid set throws `ERR_JWK_INVALID`.
|
|
66
|
-
*/
|
|
67
|
-
export declare function getJWKsFromSet(jwkSet: JWKSet, filter?: (jwk: JWK) => boolean): JWK[];
|
|
68
|
-
/**
|
|
69
|
-
* Generates a key for a JWA algorithm: a `CryptoKey` for symmetric algorithms, raw bytes for
|
|
70
|
-
* the `A*CBC-HS*` composites, a `CryptoKeyPair` for asymmetric ones; with `toJWK: true`, a JWK
|
|
71
|
-
* (or JWK pair) named by the `kid` option or its RFC 7638 thumbprint. PBES2, `"dir"` and `"none"` are refused.
|
|
72
|
-
*
|
|
73
|
-
* @param options
|
|
74
|
-
* - `extractable?` — defaults to `true`; ignored when `toJWK` is set.
|
|
75
|
-
* - `kid?` — with `toJWK`, a string set verbatim or `false` for no `kid`, either skipping the
|
|
76
|
-
* thumbprint digest; anything else throws `ERR_INVALID_OPTION`.
|
|
77
|
-
* - `keyUsage?` — narrows the usages derived from `alg`; leaving either half of a pair with
|
|
78
|
-
* no usage throws `ERR_INVALID_OPTION`.
|
|
79
|
-
* - `modulusLength?` — RSA only; defaults to `2048`.
|
|
80
|
-
* - `publicExponent?` — RSA only; big-endian bytes, defaults to `0x010001`.
|
|
81
|
-
* - `namedCurve?` — EC/OKP only; defaults to `"P-256"` / `"Ed25519"`. One that contradicts
|
|
82
|
-
* the curve `ES*` or `Ed25519` names throws `ERR_INVALID_OPTION`.
|
|
83
|
-
*/
|
|
84
|
-
export declare function generateKey<TAlg extends GenerateKeyAlgorithm, ToJWK extends boolean = false, Kid extends string | false = string>(alg: TAlg, options?: GenerateKeyOptions<ToJWK, Kid>): Promise<GenerateKeyReturn<TAlg, GenerateKeyOptions<ToJWK, Kid>>>;
|
|
85
|
-
/**
|
|
86
|
-
* Generates a JWK (or JWK pair) for a JWA algorithm, its `kid` the caller's, none with
|
|
87
|
-
* `kid: false`, or else its RFC 7638 thumbprint. PBES2, `"dir"` and `"none"` are refused.
|
|
88
|
-
*
|
|
89
|
-
* @param options
|
|
90
|
-
* - `keyUsage?`, `modulusLength?` (default `2048`), `publicExponent?` (default `0x010001`),
|
|
91
|
-
* `namedCurve?` (default `"P-256"` / `"Ed25519"`); a narrowing that empties a pair half or
|
|
92
|
-
* a curve that contradicts `alg` throws `ERR_INVALID_OPTION`.
|
|
93
|
-
* - `kid?` — a string set verbatim or `false` for no `kid`, either skipping the thumbprint
|
|
94
|
-
* digest; anything else throws `ERR_INVALID_OPTION`.
|
|
95
|
-
* - JWK metadata (`use`, `x5c`, `x5t`, `x5t#S256`, `x5u`) is merged onto the result;
|
|
96
|
-
* `alg`, `kty`, `key_ops`, `ext` and key-material members are reserved (`ERR_INVALID_OPTION`).
|
|
97
|
-
*/
|
|
98
|
-
export declare function generateJWK<TAlg extends GenerateKeyAlgorithm, Kid extends string | false = string>(alg: TAlg, options?: GenerateJWKOptions<Kid> & Omit<JWKMetadata, "kid">): Promise<GenerateJWKReturn<TAlg, Kid>>;
|
|
99
|
-
/**
|
|
100
|
-
* Derives an AES-KW key from a password with PBKDF2 per PBES2 (RFC 7518 §4.8).
|
|
101
|
-
*
|
|
102
|
-
* @param options
|
|
103
|
-
* - `alg` — a PBES2 algorithm; any other throws `ERR_JWK_UNSUPPORTED`.
|
|
104
|
-
* - `salt` — PBKDF2 salt (`p2s`), at least 8 octets (RFC 7518 §4.8.1.1).
|
|
105
|
-
* - `iterations` — PBKDF2 count (`p2c`), an integer of at least 1000 (RFC 7518 §4.8.1.2);
|
|
106
|
-
* anything else throws `ERR_INVALID_OPTION`.
|
|
107
|
-
* - `keyUsage?` — defaults to `["wrapKey", "unwrapKey"]`.
|
|
108
|
-
* - `extractable?` — defaults to `false`; ignored when `toJWK` is set.
|
|
109
|
-
* - `toJWK?` — return a `JWK_oct` carrying the inner AES-KW `alg` instead of a `CryptoKey`.
|
|
110
|
-
*/
|
|
111
|
-
export declare function deriveKeyFromPassword<TOptions extends DeriveKeyOptions>(password: string | Uint8Array<ArrayBuffer>, options: TOptions): Promise<DeriveKeyReturn<TOptions, TOptions["alg"]>>;
|
|
112
|
-
/**
|
|
113
|
-
* Derives a `JWK_oct` from a password per PBES2 (RFC 7518 §4.8); its `alg` is the inner AES-KW
|
|
114
|
-
* variant and its `kid` the caller's, none with `kid: false`, or else its RFC 7638 thumbprint.
|
|
115
|
-
*
|
|
116
|
-
* @param options
|
|
117
|
-
* - `alg` (PBES2 only, else `ERR_JWK_UNSUPPORTED`), `salt` (at least 8 octets), `iterations`
|
|
118
|
-
* (integer of at least 1000, else `ERR_INVALID_OPTION`), `keyUsage?` (default
|
|
119
|
-
* `["wrapKey", "unwrapKey"]`).
|
|
120
|
-
* - `kid?` — a string set verbatim or `false` for no `kid`, either skipping the thumbprint
|
|
121
|
-
* digest; anything else throws `ERR_INVALID_OPTION`.
|
|
122
|
-
* - JWK metadata (`use`, `x5c`, `x5t`, `x5t#S256`, `x5u`) is merged onto the result;
|
|
123
|
-
* `alg`, `kty`, `key_ops`, `ext` and key-material members are reserved (`ERR_INVALID_OPTION`).
|
|
124
|
-
*/
|
|
125
|
-
export declare function deriveJWKFromPassword<TAlg extends JWK_PBES2, Kid extends string | false = string>(password: string | Uint8Array<ArrayBuffer>, options: DeriveJWKOptions<TAlg, Kid> & Omit<JWKMetadata, "kid">): Promise<DeriveJWKReturn<TAlg, Kid>>;
|
|
126
|
-
/**
|
|
127
|
-
* Exports an extractable `CryptoKey` as a JWK, merging optional `jwkParams` metadata.
|
|
128
|
-
*
|
|
129
|
-
* The `alg` is WebCrypto's, which knows no JOSE roles: an AES-GCM key minted for `A128GCMKW`
|
|
130
|
-
* exports as `A128GCM`, so key-wrapping keys are minted with `generateJWK` instead.
|
|
5
|
+
* Encrypts `payload` once for several recipients and returns a JWE General JSON Serialization (RFC 7516 §7.2.1).
|
|
6
|
+
* Each recipient's `alg` comes from its JWK's `alg`. `enc` defaults to `A256GCM`.
|
|
7
|
+
* A non-empty `aad` is authenticated with the content and written as the `aad` member.
|
|
131
8
|
*
|
|
132
|
-
* @throws `
|
|
133
|
-
*
|
|
9
|
+
* @throws `ERR_JWE_INVALID` if `recipients` is empty or not an array, or `enc` is unsupported.
|
|
10
|
+
* @throws `ERR_JWE_RECIPIENT_ALG_INFERENCE` if a recipient key is not a JWK or has no `alg`.
|
|
11
|
+
* @throws `ERR_JWE_ALG_FORBIDDEN_IN_MULTI` if a recipient uses `dir` or bare `ECDH-ES`.
|
|
12
|
+
* @throws `ERR_JWE_ALG_NOT_ALLOWED` if a recipient `alg` is not a supported key management algorithm.
|
|
13
|
+
* @throws `ERR_JWE_HEADER_PARAMS_NOT_DISJOINT` if one parameter is set in more than one header.
|
|
14
|
+
* @throws `ERR_JWK_INVALID` if a recipient key cannot serve its `alg`.
|
|
15
|
+
* @throws `ERR_INVALID_OPTION` if an option is malformed, a header sets `zip`, `cek` or `contentEncryptionIV` has the wrong length, or a time option is set on a string or bytes payload.
|
|
16
|
+
* @throws `ERR_JWT_CLAIM_INVALID` if both members of a time option pair are set, or `cnf.jwk` is not a public JWK.
|
|
134
17
|
*/
|
|
135
|
-
export declare function
|
|
136
|
-
/** @deprecated Use `exportJWK`; removed in 0.9.0. */
|
|
137
|
-
export declare function exportKey<T extends JWK = JWK>(key: CryptoKey, jwkParams?: JWKMetadata): Promise<T>;
|
|
18
|
+
export declare function encryptMulti(payload: JOSEPayload, recipients: JWEMultiRecipient[], options?: JWEMultiEncryptOptions): Promise<JWEGeneralSerialization>;
|
|
138
19
|
/**
|
|
139
|
-
*
|
|
20
|
+
* Decrypts a JWE General (RFC 7516 §7.2.1) or Flattened (§7.2.2) JSON Serialization and returns the first recipient that opens.
|
|
21
|
+
* The protected header, the shared unprotected header and the content segments are checked first and throw at once.
|
|
22
|
+
* Recipients are then tried in order, and a failure scoped to one recipient moves on to the next.
|
|
23
|
+
* Once a recipient opens, a payload or claim failure throws at once.
|
|
24
|
+
* The payload type follows `options.payload`.
|
|
140
25
|
*
|
|
141
|
-
* @
|
|
142
|
-
* @throws `
|
|
143
|
-
* @
|
|
26
|
+
* @throws its own error if the only recipient fails, as `decrypt` would.
|
|
27
|
+
* @throws `ERR_JWE_DECRYPTION_FAILED` if none of several recipients opens, with one `JWTError` per recipient as `cause`, in order.
|
|
28
|
+
* @throws `ERR_JWE_NO_MATCHING_RECIPIENT` if `strictRecipientMatch` is set and no recipient fits the key.
|
|
29
|
+
* @throws `ERR_JWE_INVALID_SERIALIZATION` if the object is not a General or Flattened JWE.
|
|
30
|
+
* @throws `ERR_JWE_INVALID` if the recipients exceed `limits.recipients`, or a shared header or content segment is invalid.
|
|
31
|
+
* @throws `ERR_JWE_ALG_NOT_ALLOWED` if `enc` is outside `encryptionAlgorithms`.
|
|
32
|
+
* @throws `ERR_JWK_KEY_NOT_FOUND` if the key trials exceed `limits.keyAttempts`.
|
|
33
|
+
* @throws `ERR_INVALID_OPTION` if an option is malformed, a claim option is set where claims are not validated, or `returnCek` meets a non-extractable CEK.
|
|
34
|
+
* @throws `ERR_JWT_EXPIRED` if `exp` has passed or the token is older than `maxTokenAge`.
|
|
35
|
+
* @throws `ERR_JWT_NBF` if `nbf` is in the future.
|
|
36
|
+
* @throws `ERR_JWT_CLAIM_INVALID` if a claim does not match its option or has the wrong type.
|
|
37
|
+
* @throws `ERR_JWT_CLAIM_MISSING` if a claim in `requiredClaims` or one an option checks is absent.
|
|
144
38
|
*/
|
|
145
|
-
export declare function
|
|
39
|
+
export declare function decryptMulti<T extends object = JWTClaims>(jwe: JWEGeneralSerialization | JWEFlattenedSerialization, keyOrLookup: JWEMultiDecryptKey, options?: JWEMultiDecryptOptions<"json">): Promise<JWEMultiDecryptResult<T>>;
|
|
40
|
+
/** Decrypts a JWE JSON Serialization whose plaintext decodes under `options.payload`. See {@link PayloadOf}. */
|
|
41
|
+
export declare function decryptMulti<Mode extends JOSEPayloadMode = JOSEPayloadMode>(jwe: JWEGeneralSerialization | JWEFlattenedSerialization, keyOrLookup: JWEMultiDecryptKey, options?: JWEMultiDecryptOptions<Mode>): Promise<JWEMultiDecryptResult<PayloadOf<Mode>>>;
|
|
146
42
|
/**
|
|
147
|
-
*
|
|
43
|
+
* Converts a General JWE with exactly one recipient to the Flattened form (RFC 7516 §7.2.2).
|
|
148
44
|
*
|
|
149
|
-
* @
|
|
150
|
-
* @param options `alg` plus its parameters (`p2s`/`p2c` for PBES2, `ecdh` for ECDH-ES, …).
|
|
45
|
+
* @throws `ERR_JWE_INVALID_SERIALIZATION` if the input has no `recipients` array, or not exactly one recipient.
|
|
151
46
|
*/
|
|
152
|
-
export declare function
|
|
47
|
+
export declare function generalToFlattened(jwe: JWEGeneralSerialization): JWEFlattenedSerialization;
|
|
153
48
|
/**
|
|
154
|
-
*
|
|
49
|
+
* Encrypts `payload` and returns a JWE Compact Serialization (RFC 7516 §7.1).
|
|
50
|
+
* `key` is a `CryptoKey`, a JWK, raw bytes, or a password string for PBES2.
|
|
51
|
+
* Without `alg`, a password uses `PBES2-HS256+A128KW` and an `oct` JWK whose `alg` is an `enc` uses `dir`.
|
|
52
|
+
* Any other JWK uses its own `alg`.
|
|
53
|
+
* Without `enc`, the JWK's `enc` applies, else `A256GCM`. For `dir`, `enc` comes from the CEK JWK.
|
|
155
54
|
*
|
|
156
|
-
* @
|
|
157
|
-
* @
|
|
158
|
-
*
|
|
159
|
-
* @throws `
|
|
160
|
-
*
|
|
161
|
-
*
|
|
55
|
+
* @throws `ERR_JWE_ALG_MISSING` if no `alg` is given and none can be inferred.
|
|
56
|
+
* @throws `ERR_JWE_ENC_MISSING` if `alg` is `dir` and neither `enc` nor the JWK names one.
|
|
57
|
+
* @throws `ERR_JWE_ALG_NOT_ALLOWED` if `alg` is `"none"` or not a supported key management algorithm.
|
|
58
|
+
* @throws `ERR_JWE_INVALID` if `enc` is not a supported content encryption algorithm.
|
|
59
|
+
* @throws `ERR_JWK_INVALID` if the key cannot serve `alg`, or a string key is used without PBES2.
|
|
60
|
+
* @throws `ERR_INVALID_OPTION` if an option is malformed, `cek` or `contentEncryptionIV` has the wrong length, or a time option is set on a string or bytes payload.
|
|
61
|
+
* @throws `ERR_JWT_CLAIM_INVALID` if both members of a time option pair are set, or `cnf.jwk` is not a public JWK.
|
|
162
62
|
*/
|
|
163
|
-
export declare function
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
jwkParams?: JWKMetadata;
|
|
178
|
-
}): Promise<ImportedPEMJWK<A, Type>>;
|
|
179
|
-
/**
|
|
180
|
-
* Exports an asymmetric JWK as PEM: PKCS#8 when it carries `d`, SPKI otherwise.
|
|
181
|
-
*
|
|
182
|
-
* @throws `ERR_JWK_UNSUPPORTED` for `oct` JWKs or an unknown format or algorithm;
|
|
183
|
-
* `ERR_JWK_INVALID` for a missing `alg` or a `pemFormat` that contradicts the JWK's half.
|
|
184
|
-
*/
|
|
185
|
-
export declare function exportPEM(jwk: JWK, options?: {
|
|
186
|
-
/** PEM format. Inferred from the JWK shape when omitted. */
|
|
187
|
-
pemFormat?: "pkcs8" | "spki";
|
|
188
|
-
/** Algorithm hint used when `jwk.alg` is absent. */
|
|
189
|
-
alg?: JWKPEMAlgorithm;
|
|
63
|
+
export declare function encrypt(payload: JOSEPayload, key: JWEEncryptJWK | string | Uint8Array<ArrayBuffer>, options?: JWEEncryptOptions): Promise<string>;
|
|
64
|
+
export declare function encrypt(payload: JOSEPayload, key: CryptoKey | Extract<JWEEncryptJWK, {
|
|
65
|
+
kty: "oct";
|
|
66
|
+
}> | Uint8Array<ArrayBuffer>, options: JWEEncryptOptions & {
|
|
67
|
+
alg: "dir";
|
|
68
|
+
enc: ContentEncryptionAlgorithm;
|
|
69
|
+
}): Promise<string>;
|
|
70
|
+
export declare function encrypt(payload: JOSEPayload, key: CryptoKey, options: JWEEncryptOptions & {
|
|
71
|
+
alg: KeyManagementAlgorithm;
|
|
72
|
+
enc: ContentEncryptionAlgorithm;
|
|
73
|
+
}): Promise<string>;
|
|
74
|
+
export declare function encrypt(payload: JOSEPayload, key: CryptoKey | JWEEncryptJWK | string | Uint8Array<ArrayBuffer>, options: JWEEncryptOptions & {
|
|
75
|
+
alg: KeyManagementAlgorithm;
|
|
76
|
+
enc: ContentEncryptionAlgorithm;
|
|
190
77
|
}): Promise<string>;
|
|
191
78
|
/**
|
|
192
|
-
*
|
|
79
|
+
* Decrypts a JWE Compact Serialization (RFC 7516 §7.1) with a key, a JWK Set, a password or a key lookup function.
|
|
80
|
+
* The header `alg` must be in `algorithms`, else match the JWK's `alg`, else fit the key type.
|
|
81
|
+
* `encryptionAlgorithms` restricts the header `enc`.
|
|
82
|
+
* The payload type follows `options.payload`: claims `T` for `"json"` (default), `string`, or `Uint8Array`.
|
|
83
|
+
* With `returnCek`, the result includes the CEK and the AAD.
|
|
193
84
|
*
|
|
194
|
-
* @
|
|
195
|
-
* @
|
|
85
|
+
* @throws `ERR_JWE_INVALID` if the token is malformed, uses `zip`, has the wrong `typ`, an unrecognized `crit` member, or an unsupported `enc`.
|
|
86
|
+
* @throws `ERR_JWE_INVALID` if a PBES2 `p2c` is outside the iteration bounds or `limits.pbes2Iterations`.
|
|
87
|
+
* @throws `ERR_JWE_ALG_NOT_ALLOWED` if the header `alg` is unsupported or not allowed, or `enc` is outside `encryptionAlgorithms`.
|
|
88
|
+
* @throws `ERR_JWE_DECRYPTION_FAILED` if the CEK does not unwrap or the content does not decrypt.
|
|
89
|
+
* @throws `ERR_JWK_INVALID` if no key is given, or the key cannot serve the header `alg`.
|
|
90
|
+
* @throws `ERR_JWK_KEY_NOT_FOUND` if the lookup returns `undefined` or throws an error that is not a `JWTError` (as `cause`), or a JWK Set has no candidate.
|
|
91
|
+
* @throws `ERR_INVALID_OPTION` if an option is malformed, a claim option is set where claims are not validated, or `returnCek` meets a non-extractable CEK.
|
|
92
|
+
* @throws `ERR_JWT_EXPIRED` if `exp` has passed or the token is older than `maxTokenAge`.
|
|
93
|
+
* @throws `ERR_JWT_NBF` if `nbf` is in the future.
|
|
94
|
+
* @throws `ERR_JWT_CLAIM_INVALID` if a claim does not match its option or has the wrong type.
|
|
95
|
+
* @throws `ERR_JWT_CLAIM_MISSING` if a claim in `requiredClaims` or one an option checks is absent.
|
|
196
96
|
*/
|
|
197
|
-
export declare function
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
/** Derived length in bits. Defaults to `enc`'s length; required for `"ECDH-ES"`. */
|
|
201
|
-
keyLength?: number;
|
|
202
|
-
/** Agreement PartyUInfo (apu). */
|
|
203
|
-
partyUInfo?: Uint8Array<ArrayBuffer>;
|
|
204
|
-
/** Agreement PartyVInfo (apv). */
|
|
205
|
-
partyVInfo?: Uint8Array<ArrayBuffer>;
|
|
206
|
-
}): Promise<Uint8Array<ArrayBuffer>>;
|
|
97
|
+
export declare function decrypt<T extends object = JWTClaims>(jwe: string, key: CryptoKey | JWKSet | JWEDecryptJWK | string | Uint8Array<ArrayBuffer> | JWEKeyLookup, options?: JWEDecryptOptions<"json">): Promise<JWEDecryptResult<T>>;
|
|
98
|
+
/** Decrypts a JWE Compact Serialization whose plaintext decodes under `options.payload`. See {@link PayloadOf}. */
|
|
99
|
+
export declare function decrypt<Mode extends JOSEPayloadMode = JOSEPayloadMode>(jwe: string, key: CryptoKey | JWKSet | JWEDecryptJWK | string | Uint8Array<ArrayBuffer> | JWEKeyLookup, options?: JWEDecryptOptions<Mode>): Promise<JWEDecryptResult<PayloadOf<Mode>>>;
|
|
@@ -1,63 +1,226 @@
|
|
|
1
1
|
import "./error.mjs";
|
|
2
|
-
import { ContentEncryptionAlgorithm,
|
|
3
|
-
import "./
|
|
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
|
-
export declare function
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
2
|
+
import { ContentEncryptionAlgorithm, DeriveJWKOptions, DeriveJWKReturn, DeriveKeyOptions, DeriveKeyReturn, GenerateJWKOptions, GenerateJWKReturn, GenerateKeyAlgorithm, GenerateKeyOptions, GenerateKeyReturn, ImportedKey, ImportedPEMJWK, JWK, JWKAlgorithm, JWKCacheAdapter, JWKMetadata, JWKPEMAlgorithm, JWKSet, JWK_Asymmetric, JWK_ECDH_ES, JWK_EC_Private, JWK_EC_Public, JWK_OKP_Private, JWK_OKP_Public, JWK_PBES2, JWK_oct, KeyManagementAlgorithm, PEMType, ToPublicJWKReturn, UnwrapKeyOptions, UnwrapKeyResult, UnwrappingKeyFor, WrapKeyOptions, WrapKeyResult, WrappingKeyFor } from "./index.mjs";
|
|
3
|
+
import "./index3.mjs";
|
|
4
|
+
/**
|
|
5
|
+
* The default JWK import cache, keyed by JWK object identity and `alg`. A copied JWK misses it.
|
|
6
|
+
* A JWK mutated in place keeps returning the old `CryptoKey`: rotate a key by replacing the object.
|
|
7
|
+
*/
|
|
8
|
+
export declare class WeakMapJWKCache implements JWKCacheAdapter {
|
|
9
|
+
private readonly _map;
|
|
10
|
+
get(jwk: JWK, alg: JWKAlgorithm): CryptoKey | undefined;
|
|
11
|
+
set(jwk: JWK, alg: JWKAlgorithm, key: CryptoKey): void;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Replaces the JWK import cache `importKey` uses, or disables it with `false`.
|
|
15
|
+
*
|
|
16
|
+
* A cache shared across JWK objects keys on the RFC 7638 thumbprint, the pair half (`d` present)
|
|
17
|
+
* and `alg`. The thumbprint covers public members only, and one `kid` may name different keys.
|
|
18
|
+
* `get` and `set` may return promises.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const cacheKey = async (jwk: JWK, alg: JWKAlgorithm) =>
|
|
23
|
+
* `${await thumbprint(jwk)}:${"d" in jwk ? "private" : "public"}:${alg}`;
|
|
24
|
+
* const map = new Map<string, CryptoKey>();
|
|
25
|
+
* configureJWKCache({
|
|
26
|
+
* get: async (jwk, alg) => map.get(await cacheKey(jwk, alg)),
|
|
27
|
+
* set: async (jwk, alg, key) => {
|
|
28
|
+
* map.set(await cacheKey(jwk, alg), key);
|
|
29
|
+
* },
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare function configureJWKCache(cache: JWKCacheAdapter | false): void;
|
|
34
|
+
/** Resets the JWK import cache to a fresh `WeakMapJWKCache`. */
|
|
35
|
+
export declare function clearJWKCache(): void;
|
|
36
|
+
/**
|
|
37
|
+
* Imports the secret of an `oct` JWK as a `CryptoKey` under `algorithm` and `usage`.
|
|
38
|
+
* @throws `ERR_JWK_INVALID` if `algorithm` or `usage` is absent, or the secret is too short for the HMAC hash or the JWK `alg`.
|
|
39
|
+
*/
|
|
40
|
+
export declare function importKey(key: JWK_oct, options: {
|
|
41
|
+
asCryptoKey: true;
|
|
42
|
+
/** The Web Crypto algorithm to import the key as, such as `{ name: "AES-GCM", length: 256 }`. */
|
|
43
|
+
algorithm: Parameters<typeof crypto.subtle.importKey>[2];
|
|
44
|
+
usage: KeyUsage[];
|
|
45
|
+
/** Whether the key is extractable. @default false */
|
|
46
|
+
extractable?: boolean;
|
|
47
|
+
}): Promise<CryptoKey>;
|
|
48
|
+
/**
|
|
49
|
+
* Imports key material. A `CryptoKey` or `Uint8Array` is returned as is, an `oct` JWK as its raw
|
|
50
|
+
* bytes, an asymmetric JWK as a `CryptoKey` under its own `alg` or the requested one.
|
|
51
|
+
*
|
|
52
|
+
* A JWK's `alg` is authoritative (RFC 7517 §4.4). An `oct` JWK with a content encryption `alg`
|
|
53
|
+
* also serves `"dir"`. A JWK without `alg` must have a `kty` that can serve the requested `alg`.
|
|
54
|
+
* An `oct` secret must be long enough for its `alg`. A non-`oct` JWK must not have a `k` member.
|
|
55
|
+
* `expect` and `extractable` apply to asymmetric keys only. Passing `extractable` skips the import cache.
|
|
56
|
+
*
|
|
57
|
+
* @throws `ERR_JWK_INVALID` if `key` is a string or not key material, or breaks one of the rules above.
|
|
58
|
+
*/
|
|
59
|
+
export declare function importKey<K extends CryptoKey | JWK | Uint8Array<ArrayBuffer>>(key: K, options?: JWKAlgorithm | {
|
|
60
|
+
/** The algorithm to import under. Required for an asymmetric JWK without `alg`. */
|
|
61
|
+
alg?: JWKAlgorithm;
|
|
62
|
+
/** The pair half the key must be. The other half is `ERR_JWK_INVALID`. */
|
|
63
|
+
expect?: "public" | "private";
|
|
64
|
+
/** Whether the `CryptoKey` is extractable. A JWK `ext: false` overrides it. @default false for private keys, true for public keys */
|
|
65
|
+
extractable?: boolean;
|
|
66
|
+
}): Promise<ImportedKey<K>>;
|
|
67
|
+
/**
|
|
68
|
+
* Returns the JWKs of a JWK Set without its non-JWK entries, narrowed by `filter` when given.
|
|
69
|
+
*
|
|
70
|
+
* @throws `ERR_JWK_INVALID` if `jwkSet` is not a JWK Set.
|
|
71
|
+
*/
|
|
72
|
+
export declare function getJWKsFromSet(jwkSet: JWKSet, filter?: (jwk: JWK) => boolean): JWK[];
|
|
73
|
+
/**
|
|
74
|
+
* Generates a key for a JWA algorithm. Returns a `CryptoKey` for a symmetric algorithm,
|
|
75
|
+
* raw bytes for the `A*CBC-HS*` composites and a `CryptoKeyPair` for an asymmetric one.
|
|
76
|
+
* With `toJWK: true` it returns a JWK, or a `{ privateKey, publicKey }` JWK pair sharing one `kid`.
|
|
77
|
+
*
|
|
78
|
+
* @param options
|
|
79
|
+
* - `extractable?`: ignored with `toJWK`. Defaults to `true`.
|
|
80
|
+
* - `kid?`: with `toJWK`, a string used as is or `false` for no `kid`. Defaults to the RFC 7638 thumbprint.
|
|
81
|
+
* - `keyUsage?`: narrows the usages `alg` admits and is written as `key_ops`. Without it the JWK has no `key_ops`.
|
|
82
|
+
* - `modulusLength?`: RSA only, at least 2048 bits. Defaults to `2048`.
|
|
83
|
+
* - `publicExponent?`: RSA only, big-endian bytes. Defaults to `0x010001`.
|
|
84
|
+
* - `namedCurve?`: `ECDH-ES*` and `EdDSA` only. Defaults to `"P-256"` and `"Ed25519"`.
|
|
85
|
+
* `ES*` and `Ed25519` fix their curve and accept only that one.
|
|
86
|
+
* @throws `ERR_JWK_UNSUPPORTED` if `alg` cannot be generated (PBES2, `"dir"`, `"none"`) or `namedCurve` is unsupported.
|
|
87
|
+
* @throws `ERR_INVALID_OPTION` if an option is invalid for `alg`, or `keyUsage` leaves either half of a pair with no usage.
|
|
88
|
+
*/
|
|
89
|
+
export declare function generateKey<TAlg extends GenerateKeyAlgorithm, ToJWK extends boolean = false, Kid extends string | false = string>(alg: TAlg, options?: GenerateKeyOptions<ToJWK, Kid>): Promise<GenerateKeyReturn<TAlg, GenerateKeyOptions<ToJWK, Kid>>>;
|
|
90
|
+
/**
|
|
91
|
+
* Generates a JWK for a JWA algorithm, or a `{ privateKey, publicKey }` JWK pair sharing one `kid`.
|
|
92
|
+
*
|
|
93
|
+
* @param options
|
|
94
|
+
* - `keyUsage?`, `modulusLength?`, `publicExponent?`, `namedCurve?`: as on `generateKey`.
|
|
95
|
+
* - `kid?`: a string used as is or `false` for no `kid`. Defaults to the RFC 7638 thumbprint.
|
|
96
|
+
* - JWK metadata (`use`, `x5c`, `x5t`, `x5t#S256`, `x5u`) is copied onto each JWK.
|
|
97
|
+
* @throws `ERR_JWK_UNSUPPORTED` if `alg` cannot be generated (PBES2, `"dir"`, `"none"`) or `namedCurve` is unsupported.
|
|
98
|
+
* @throws `ERR_INVALID_OPTION` if an option is invalid for `alg`, `keyUsage` leaves a pair half with no usage,
|
|
99
|
+
* or the metadata sets `alg`, `kty`, `key_ops`, `ext` or a key material member.
|
|
100
|
+
*/
|
|
101
|
+
export declare function generateJWK<TAlg extends GenerateKeyAlgorithm, Kid extends string | false = string>(alg: TAlg, options?: GenerateJWKOptions<Kid> & Omit<JWKMetadata, "kid">): Promise<GenerateJWKReturn<TAlg, Kid>>;
|
|
102
|
+
/**
|
|
103
|
+
* Derives an AES-KW key from a password with PBKDF2, as PBES2 does (RFC 7518 §4.8).
|
|
104
|
+
* A string password or salt is encoded as UTF-8.
|
|
105
|
+
*
|
|
106
|
+
* @param options
|
|
107
|
+
* - `alg`: a PBES2 algorithm.
|
|
108
|
+
* - `salt`: the PBKDF2 salt (`p2s`), at least 8 octets (RFC 7518 §4.8.1.1).
|
|
109
|
+
* - `iterations`: the PBKDF2 count (`p2c`), an integer of at least 1000 (RFC 7518 §4.8.1.2).
|
|
110
|
+
* - `keyUsage?`: AES-KW usages, written as `key_ops` with `toJWK`. Defaults to `["wrapKey", "unwrapKey"]`.
|
|
111
|
+
* - `extractable?`: ignored with `toJWK`. Defaults to `false`.
|
|
112
|
+
* - `toJWK?`: returns a `JWK_oct` with the inner AES-KW `alg` (`A128KW` for `PBES2-HS256+A128KW`) instead of a `CryptoKey`.
|
|
113
|
+
* @throws `ERR_JWK_UNSUPPORTED` if `alg` is not a PBES2 algorithm.
|
|
114
|
+
* @throws `ERR_INVALID_OPTION` if `password`, `salt` or `iterations` is invalid.
|
|
115
|
+
*/
|
|
116
|
+
export declare function deriveKeyFromPassword<TOptions extends DeriveKeyOptions>(password: string | Uint8Array<ArrayBuffer>, options: TOptions): Promise<DeriveKeyReturn<TOptions, TOptions["alg"]>>;
|
|
117
|
+
/**
|
|
118
|
+
* Derives a `JWK_oct` from a password, as PBES2 does (RFC 7518 §4.8).
|
|
119
|
+
* Its `alg` is the inner AES-KW algorithm (`A128KW` for `PBES2-HS256+A128KW`).
|
|
120
|
+
*
|
|
121
|
+
* @param options
|
|
122
|
+
* - `alg`, `salt`, `iterations`, `keyUsage?`: as on `deriveKeyFromPassword`.
|
|
123
|
+
* - `kid?`: a string used as is or `false` for no `kid`. Defaults to the RFC 7638 thumbprint.
|
|
124
|
+
* - JWK metadata (`use`, `x5c`, `x5t`, `x5t#S256`, `x5u`) is copied onto the JWK.
|
|
125
|
+
* @throws `ERR_JWK_UNSUPPORTED` if `alg` is not a PBES2 algorithm.
|
|
126
|
+
* @throws `ERR_INVALID_OPTION` if `password`, `salt`, `iterations` or `kid` is invalid,
|
|
127
|
+
* or the metadata sets `alg`, `kty`, `key_ops`, `ext` or a key material member.
|
|
128
|
+
*/
|
|
129
|
+
export declare function deriveJWKFromPassword<TAlg extends JWK_PBES2, Kid extends string | false = string>(password: string | Uint8Array<ArrayBuffer>, options: DeriveJWKOptions<TAlg, Kid> & Omit<JWKMetadata, "kid">): Promise<DeriveJWKReturn<TAlg, Kid>>;
|
|
130
|
+
/**
|
|
131
|
+
* Exports an extractable `CryptoKey` as a JWK and copies the `jwkParams` metadata onto it.
|
|
132
|
+
* The JWK has no `ext` or `key_ops` member.
|
|
133
|
+
*
|
|
134
|
+
* The `alg` is the one Web Crypto exports: an AES-GCM key exports as `A128GCM`, even one meant
|
|
135
|
+
* for `A128GCMKW`. Use `generateJWK` for a key wrapping JWK.
|
|
136
|
+
*
|
|
137
|
+
* @throws `ERR_INVALID_OPTION` if `key` is not a `CryptoKey`, or `jwkParams` holds a non-JSON value
|
|
138
|
+
* or sets `alg`, `kty`, `key_ops`, `ext` or a key material member.
|
|
139
|
+
* @throws `ERR_JWK_INVALID` if the key is not extractable.
|
|
140
|
+
*/
|
|
141
|
+
export declare function exportJWK<T extends JWK = JWK>(key: CryptoKey, jwkParams?: JWKMetadata): Promise<T>;
|
|
142
|
+
/**
|
|
143
|
+
* Returns the public half of an asymmetric JWK.
|
|
144
|
+
*
|
|
145
|
+
* @param jwk An asymmetric JWK (EC, OKP, RSA), private or public.
|
|
146
|
+
* @throws `ERR_JWK_INVALID` if `jwk` is not an asymmetric JWK, holds a non-JSON value, or lacks the public members of its key type.
|
|
147
|
+
* @returns A copy without the private members. `key_ops` keeps the public operations only (`sign` becomes `verify`).
|
|
148
|
+
*/
|
|
149
|
+
export declare function toPublicJWK<T extends JWK_Asymmetric>(jwk: T): ToPublicJWKReturn<T>;
|
|
150
|
+
/**
|
|
151
|
+
* Wraps a CEK under a JWA key management algorithm. The result members follow `alg`:
|
|
152
|
+
* `iv` and `tag` for AES-GCMKW, `p2s` and `p2c` for PBES2, `epk`, `apu` and `apv` for ECDH-ES.
|
|
153
|
+
*
|
|
154
|
+
* @param wrappingKey A `CryptoKey` or a JWK. For PBES2, also a password string or bytes.
|
|
155
|
+
* @param options `alg` plus its parameters (`p2s` and `p2c` for PBES2, `ecdh` for ECDH-ES, …).
|
|
156
|
+
* @throws `ERR_INVALID_OPTION` if `keyToWrap` is not bytes or a `CryptoKey`, an AES-KW CEK is not
|
|
157
|
+
* 16 bytes or more in whole 8-byte blocks, or `p2s` or `p2c` is out of range.
|
|
158
|
+
* @throws `ERR_JWE_INVALID` if PBES2 lacks `p2s` or `p2c`, or `ecdh.enc` is unsupported.
|
|
159
|
+
* @throws `ERR_JWK_INVALID` if the key does not fit `alg`, a string key is given outside PBES2, or `"ECDH-ES"` lacks `ecdh.enc`.
|
|
160
|
+
* @throws `ERR_JWK_UNSUPPORTED` if `alg` is not a key management algorithm.
|
|
161
|
+
*/
|
|
162
|
+
export declare function wrapKey<TAlg extends KeyManagementAlgorithm>(keyToWrap: CryptoKey | Uint8Array<ArrayBuffer>, wrappingKey: WrappingKeyFor<TAlg>, options: WrapKeyOptions<TAlg>): Promise<WrapKeyResult<TAlg>>;
|
|
163
|
+
/**
|
|
164
|
+
* Unwraps a CEK under a JWA key management algorithm. Returns an AES-GCM `CryptoKey` unless
|
|
165
|
+
* `format` is `"raw"` or `unwrappedKeyAlgorithm` says otherwise. Pass `format: "raw"` for an `A*CBC-HS*` CEK.
|
|
166
|
+
*
|
|
167
|
+
* @param unwrappingKey A `CryptoKey` or a JWK. For PBES2, also a password string or bytes.
|
|
168
|
+
* @param options `alg`, `format?` (`"cryptokey"` by default, or `"raw"`), plus the algorithm's
|
|
169
|
+
* parameters (`iv` and `tag`, `p2s` and `p2c`, `epk`, …).
|
|
170
|
+
* @throws `ERR_JWE_DECRYPTION_FAILED` if the unwrap fails. The cause is not disclosed (RFC 7516 §11.5).
|
|
171
|
+
* @throws `ERR_INVALID_OPTION` if `wrappedKey` is not a `Uint8Array` or `options` is not an object.
|
|
172
|
+
* @throws `ERR_JWE_INVALID` if an algorithm parameter is missing or malformed.
|
|
173
|
+
* @throws `ERR_JWK_INVALID` if the key does not fit `alg`.
|
|
174
|
+
* @throws `ERR_JWK_UNSUPPORTED` if `alg` is not a key management algorithm.
|
|
175
|
+
*/
|
|
176
|
+
export declare function unwrapKey<TOptions extends UnwrapKeyOptions>(wrappedKey: Uint8Array<ArrayBuffer>, unwrappingKey: UnwrappingKeyFor<TOptions["alg"]>, options: TOptions): Promise<UnwrapKeyResult<TOptions>>;
|
|
177
|
+
/**
|
|
178
|
+
* Imports a PEM key as a JWK with `alg` set. The type follows the label: `PRIVATE KEY` is
|
|
179
|
+
* PKCS#8, `PUBLIC KEY` is SPKI, `CERTIFICATE` is X.509 with only its public key kept.
|
|
180
|
+
* Pass `pemType` to require a type. The returned JWK holds the key material as plain values.
|
|
181
|
+
*
|
|
182
|
+
* @throws `ERR_INVALID_OPTION` if `pem` is not a string, `options` is not an object,
|
|
183
|
+
* or `jwkParams` sets `alg`, `kty`, `key_ops`, `ext` or a key material member.
|
|
184
|
+
* @throws `ERR_JWK_INVALID` if the type cannot be inferred, the label does not match `pemType`, or the body is not a key for `alg`.
|
|
185
|
+
* @throws `ERR_JWK_UNSUPPORTED` if `pemType` is unknown.
|
|
186
|
+
*/
|
|
187
|
+
export declare function importPEM<A extends JWKPEMAlgorithm, Type extends PEMType | undefined = undefined>(pem: string, alg: A, options?: {
|
|
188
|
+
/** The PEM type. Inferred from the label when omitted, so the result type is either half. */
|
|
189
|
+
pemType?: Type;
|
|
190
|
+
/** JWK metadata copied onto the result, such as `kid`. */
|
|
191
|
+
jwkParams?: JWKMetadata;
|
|
192
|
+
}): Promise<ImportedPEMJWK<A, Type>>;
|
|
193
|
+
/**
|
|
194
|
+
* Exports an asymmetric JWK as PEM: PKCS#8 when it has a `d` member, SPKI otherwise.
|
|
195
|
+
*
|
|
196
|
+
* @throws `ERR_JWK_UNSUPPORTED` if `jwk` is an `oct` JWK, or the format or algorithm is unsupported.
|
|
197
|
+
* @throws `ERR_JWK_INVALID` if neither `jwk.alg` nor `options.alg` is set, the algorithm does not fit `kty`,
|
|
198
|
+
* or `pemFormat` names the other half of the pair.
|
|
199
|
+
*/
|
|
200
|
+
export declare function exportPEM(jwk: JWK, options?: {
|
|
201
|
+
/** The PEM format. Inferred from the JWK when omitted. */
|
|
202
|
+
pemFormat?: "pkcs8" | "spki";
|
|
203
|
+
/** The algorithm used when `jwk.alg` is absent. */
|
|
204
|
+
alg?: JWKPEMAlgorithm;
|
|
49
205
|
}): Promise<string>;
|
|
50
206
|
/**
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
|
|
207
|
+
* Derives ECDH-ES key material (RFC 7518 §4.6): ECDH on an EC or OKP pair, then the Concat KDF.
|
|
208
|
+
* A JWK is imported under `enc` when `enc` is an `ECDH-ES*` algorithm, else under `"ECDH-ES"`.
|
|
209
|
+
*
|
|
210
|
+
* @param publicKey The recipient's static public key, or the ephemeral one when decrypting.
|
|
211
|
+
* @param privateKey The ephemeral private key, or the recipient's static one when decrypting.
|
|
212
|
+
* @throws `ERR_JWE_INVALID` if `enc` is neither an `ECDH-ES*` nor a content encryption algorithm.
|
|
213
|
+
* @throws `ERR_INVALID_OPTION` if `keyLength` is not a positive multiple of 8 up to 8192.
|
|
214
|
+
* @throws `ERR_JWK_INVALID` if `enc` is `"ECDH-ES"` without `keyLength`, a key is the wrong half or has another `alg`,
|
|
215
|
+
* or the keys are not agreement keys on one curve.
|
|
216
|
+
*/
|
|
217
|
+
export declare function deriveSharedSecret(publicKey: CryptoKey | JWK_EC_Public | JWK_OKP_Public, privateKey: CryptoKey | JWK_EC_Private | JWK_OKP_Private, options: {
|
|
218
|
+
/** The algorithm identifier the Concat KDF uses as `AlgorithmID`. */
|
|
219
|
+
enc: JWK_ECDH_ES | ContentEncryptionAlgorithm;
|
|
220
|
+
/** The derived length in bits. Required for `"ECDH-ES"`. @default the key length of `enc` */
|
|
221
|
+
keyLength?: number;
|
|
222
|
+
/** The agreement PartyUInfo (`apu`). @default empty */
|
|
223
|
+
partyUInfo?: Uint8Array<ArrayBuffer>;
|
|
224
|
+
/** The agreement PartyVInfo (`apv`). @default empty */
|
|
225
|
+
partyVInfo?: Uint8Array<ArrayBuffer>;
|
|
226
|
+
}): Promise<Uint8Array<ArrayBuffer>>;
|