@enbox/crypto 0.0.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/LICENSE +201 -0
- package/README.md +708 -0
- package/dist/browser.js +64 -0
- package/dist/browser.js.map +7 -0
- package/dist/browser.mjs +64 -0
- package/dist/browser.mjs.map +7 -0
- package/dist/cjs/algorithms/aes-ctr.js +188 -0
- package/dist/cjs/algorithms/aes-ctr.js.map +1 -0
- package/dist/cjs/algorithms/aes-gcm.js +196 -0
- package/dist/cjs/algorithms/aes-gcm.js.map +1 -0
- package/dist/cjs/algorithms/crypto-algorithm.js +13 -0
- package/dist/cjs/algorithms/crypto-algorithm.js.map +1 -0
- package/dist/cjs/algorithms/ecdsa.js +352 -0
- package/dist/cjs/algorithms/ecdsa.js.map +1 -0
- package/dist/cjs/algorithms/eddsa.js +325 -0
- package/dist/cjs/algorithms/eddsa.js.map +1 -0
- package/dist/cjs/algorithms/sha-2.js +119 -0
- package/dist/cjs/algorithms/sha-2.js.map +1 -0
- package/dist/cjs/index.js +41 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/jose/jwe.js +3 -0
- package/dist/cjs/jose/jwe.js.map +1 -0
- package/dist/cjs/jose/jwk.js +278 -0
- package/dist/cjs/jose/jwk.js.map +1 -0
- package/dist/cjs/jose/jws.js +3 -0
- package/dist/cjs/jose/jws.js.map +1 -0
- package/dist/cjs/jose/jwt.js +3 -0
- package/dist/cjs/jose/jwt.js.map +1 -0
- package/dist/cjs/jose/utils.js +60 -0
- package/dist/cjs/jose/utils.js.map +1 -0
- package/dist/cjs/local-key-manager.js +521 -0
- package/dist/cjs/local-key-manager.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/primitives/aes-ctr.js +398 -0
- package/dist/cjs/primitives/aes-ctr.js.map +1 -0
- package/dist/cjs/primitives/aes-gcm.js +425 -0
- package/dist/cjs/primitives/aes-gcm.js.map +1 -0
- package/dist/cjs/primitives/concat-kdf.js +215 -0
- package/dist/cjs/primitives/concat-kdf.js.map +1 -0
- package/dist/cjs/primitives/ed25519.js +651 -0
- package/dist/cjs/primitives/ed25519.js.map +1 -0
- package/dist/cjs/primitives/pbkdf2.js +120 -0
- package/dist/cjs/primitives/pbkdf2.js.map +1 -0
- package/dist/cjs/primitives/secp256k1.js +958 -0
- package/dist/cjs/primitives/secp256k1.js.map +1 -0
- package/dist/cjs/primitives/secp256r1.js +959 -0
- package/dist/cjs/primitives/secp256r1.js.map +1 -0
- package/dist/cjs/primitives/sha256.js +93 -0
- package/dist/cjs/primitives/sha256.js.map +1 -0
- package/dist/cjs/primitives/x25519.js +498 -0
- package/dist/cjs/primitives/x25519.js.map +1 -0
- package/dist/cjs/primitives/xchacha20-poly1305.js +340 -0
- package/dist/cjs/primitives/xchacha20-poly1305.js.map +1 -0
- package/dist/cjs/primitives/xchacha20.js +316 -0
- package/dist/cjs/primitives/xchacha20.js.map +1 -0
- package/dist/cjs/types/cipher.js +3 -0
- package/dist/cjs/types/cipher.js.map +1 -0
- package/dist/cjs/types/crypto-api.js +3 -0
- package/dist/cjs/types/crypto-api.js.map +1 -0
- package/dist/cjs/types/hasher.js +3 -0
- package/dist/cjs/types/hasher.js.map +1 -0
- package/dist/cjs/types/identifier.js +3 -0
- package/dist/cjs/types/identifier.js.map +1 -0
- package/dist/cjs/types/key-compressor.js +3 -0
- package/dist/cjs/types/key-compressor.js.map +1 -0
- package/dist/cjs/types/key-converter.js +3 -0
- package/dist/cjs/types/key-converter.js.map +1 -0
- package/dist/cjs/types/key-deriver.js +3 -0
- package/dist/cjs/types/key-deriver.js.map +1 -0
- package/dist/cjs/types/key-generator.js +3 -0
- package/dist/cjs/types/key-generator.js.map +1 -0
- package/dist/cjs/types/key-io.js +3 -0
- package/dist/cjs/types/key-io.js.map +1 -0
- package/dist/cjs/types/key-wrapper.js +3 -0
- package/dist/cjs/types/key-wrapper.js.map +1 -0
- package/dist/cjs/types/params-direct.js +3 -0
- package/dist/cjs/types/params-direct.js.map +1 -0
- package/dist/cjs/types/params-enclosed.js +3 -0
- package/dist/cjs/types/params-enclosed.js.map +1 -0
- package/dist/cjs/types/params-kms.js +3 -0
- package/dist/cjs/types/params-kms.js.map +1 -0
- package/dist/cjs/types/signer.js +3 -0
- package/dist/cjs/types/signer.js.map +1 -0
- package/dist/cjs/utils.js +173 -0
- package/dist/cjs/utils.js.map +1 -0
- package/dist/esm/algorithms/aes-ctr.js +124 -0
- package/dist/esm/algorithms/aes-ctr.js.map +1 -0
- package/dist/esm/algorithms/aes-gcm.js +132 -0
- package/dist/esm/algorithms/aes-gcm.js.map +1 -0
- package/dist/esm/algorithms/crypto-algorithm.js +6 -0
- package/dist/esm/algorithms/crypto-algorithm.js.map +1 -0
- package/dist/esm/algorithms/ecdsa.js +237 -0
- package/dist/esm/algorithms/ecdsa.js.map +1 -0
- package/dist/esm/algorithms/eddsa.js +213 -0
- package/dist/esm/algorithms/eddsa.js.map +1 -0
- package/dist/esm/algorithms/sha-2.js +57 -0
- package/dist/esm/algorithms/sha-2.js.map +1 -0
- package/dist/esm/index.js +25 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/jose/jwe.js +2 -0
- package/dist/esm/jose/jwe.js.map +1 -0
- package/dist/esm/jose/jwk.js +241 -0
- package/dist/esm/jose/jwk.js.map +1 -0
- package/dist/esm/jose/jws.js +2 -0
- package/dist/esm/jose/jws.js.map +1 -0
- package/dist/esm/jose/jwt.js +2 -0
- package/dist/esm/jose/jwt.js.map +1 -0
- package/dist/esm/jose/utils.js +34 -0
- package/dist/esm/jose/utils.js.map +1 -0
- package/dist/esm/local-key-manager.js +417 -0
- package/dist/esm/local-key-manager.js.map +1 -0
- package/dist/esm/primitives/aes-ctr.js +327 -0
- package/dist/esm/primitives/aes-ctr.js.map +1 -0
- package/dist/esm/primitives/aes-gcm.js +347 -0
- package/dist/esm/primitives/aes-gcm.js.map +1 -0
- package/dist/esm/primitives/concat-kdf.js +185 -0
- package/dist/esm/primitives/concat-kdf.js.map +1 -0
- package/dist/esm/primitives/ed25519.js +521 -0
- package/dist/esm/primitives/ed25519.js.map +1 -0
- package/dist/esm/primitives/pbkdf2.js +78 -0
- package/dist/esm/primitives/pbkdf2.js.map +1 -0
- package/dist/esm/primitives/secp256k1.js +805 -0
- package/dist/esm/primitives/secp256k1.js.map +1 -0
- package/dist/esm/primitives/secp256r1.js +806 -0
- package/dist/esm/primitives/secp256r1.js.map +1 -0
- package/dist/esm/primitives/sha256.js +55 -0
- package/dist/esm/primitives/sha256.js.map +1 -0
- package/dist/esm/primitives/x25519.js +392 -0
- package/dist/esm/primitives/x25519.js.map +1 -0
- package/dist/esm/primitives/xchacha20-poly1305.js +270 -0
- package/dist/esm/primitives/xchacha20-poly1305.js.map +1 -0
- package/dist/esm/primitives/xchacha20.js +246 -0
- package/dist/esm/primitives/xchacha20.js.map +1 -0
- package/dist/esm/types/cipher.js +2 -0
- package/dist/esm/types/cipher.js.map +1 -0
- package/dist/esm/types/crypto-api.js +2 -0
- package/dist/esm/types/crypto-api.js.map +1 -0
- package/dist/esm/types/hasher.js +2 -0
- package/dist/esm/types/hasher.js.map +1 -0
- package/dist/esm/types/identifier.js +2 -0
- package/dist/esm/types/identifier.js.map +1 -0
- package/dist/esm/types/key-compressor.js +2 -0
- package/dist/esm/types/key-compressor.js.map +1 -0
- package/dist/esm/types/key-converter.js +2 -0
- package/dist/esm/types/key-converter.js.map +1 -0
- package/dist/esm/types/key-deriver.js +2 -0
- package/dist/esm/types/key-deriver.js.map +1 -0
- package/dist/esm/types/key-generator.js +2 -0
- package/dist/esm/types/key-generator.js.map +1 -0
- package/dist/esm/types/key-io.js +2 -0
- package/dist/esm/types/key-io.js.map +1 -0
- package/dist/esm/types/key-wrapper.js +2 -0
- package/dist/esm/types/key-wrapper.js.map +1 -0
- package/dist/esm/types/params-direct.js +2 -0
- package/dist/esm/types/params-direct.js.map +1 -0
- package/dist/esm/types/params-enclosed.js +2 -0
- package/dist/esm/types/params-enclosed.js.map +1 -0
- package/dist/esm/types/params-kms.js +2 -0
- package/dist/esm/types/params-kms.js.map +1 -0
- package/dist/esm/types/signer.js +2 -0
- package/dist/esm/types/signer.js.map +1 -0
- package/dist/esm/utils.js +165 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/types/algorithms/aes-ctr.d.ts +121 -0
- package/dist/types/algorithms/aes-ctr.d.ts.map +1 -0
- package/dist/types/algorithms/aes-gcm.d.ts +152 -0
- package/dist/types/algorithms/aes-gcm.d.ts.map +1 -0
- package/dist/types/algorithms/crypto-algorithm.d.ts +6 -0
- package/dist/types/algorithms/crypto-algorithm.d.ts.map +1 -0
- package/dist/types/algorithms/ecdsa.d.ts +154 -0
- package/dist/types/algorithms/ecdsa.d.ts.map +1 -0
- package/dist/types/algorithms/eddsa.d.ts +151 -0
- package/dist/types/algorithms/eddsa.d.ts.map +1 -0
- package/dist/types/algorithms/sha-2.d.ts +51 -0
- package/dist/types/algorithms/sha-2.d.ts.map +1 -0
- package/dist/types/index.d.ts +39 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/jose/jwe.d.ts +135 -0
- package/dist/types/jose/jwe.d.ts.map +1 -0
- package/dist/types/jose/jwk.d.ts +439 -0
- package/dist/types/jose/jwk.d.ts.map +1 -0
- package/dist/types/jose/jws.d.ts +67 -0
- package/dist/types/jose/jws.d.ts.map +1 -0
- package/dist/types/jose/jwt.d.ts +139 -0
- package/dist/types/jose/jwt.d.ts.map +1 -0
- package/dist/types/jose/utils.d.ts +14 -0
- package/dist/types/jose/utils.d.ts.map +1 -0
- package/dist/types/local-key-manager.d.ts +307 -0
- package/dist/types/local-key-manager.d.ts.map +1 -0
- package/dist/types/primitives/aes-ctr.d.ts +219 -0
- package/dist/types/primitives/aes-ctr.d.ts.map +1 -0
- package/dist/types/primitives/aes-gcm.d.ts +245 -0
- package/dist/types/primitives/aes-gcm.d.ts.map +1 -0
- package/dist/types/primitives/concat-kdf.d.ts +160 -0
- package/dist/types/primitives/concat-kdf.d.ts.map +1 -0
- package/dist/types/primitives/ed25519.d.ts +359 -0
- package/dist/types/primitives/ed25519.d.ts.map +1 -0
- package/dist/types/primitives/pbkdf2.d.ts +94 -0
- package/dist/types/primitives/pbkdf2.d.ts.map +1 -0
- package/dist/types/primitives/secp256k1.d.ts +598 -0
- package/dist/types/primitives/secp256k1.d.ts.map +1 -0
- package/dist/types/primitives/secp256r1.d.ts +599 -0
- package/dist/types/primitives/secp256r1.d.ts.map +1 -0
- package/dist/types/primitives/sha256.d.ts +42 -0
- package/dist/types/primitives/sha256.d.ts.map +1 -0
- package/dist/types/primitives/x25519.d.ts +283 -0
- package/dist/types/primitives/x25519.d.ts.map +1 -0
- package/dist/types/primitives/xchacha20-poly1305.d.ts +210 -0
- package/dist/types/primitives/xchacha20-poly1305.d.ts.map +1 -0
- package/dist/types/primitives/xchacha20.d.ts +186 -0
- package/dist/types/primitives/xchacha20.d.ts.map +1 -0
- package/dist/types/types/cipher.d.ts +49 -0
- package/dist/types/types/cipher.d.ts.map +1 -0
- package/dist/types/types/crypto-api.d.ts +40 -0
- package/dist/types/types/crypto-api.d.ts.map +1 -0
- package/dist/types/types/hasher.d.ts +33 -0
- package/dist/types/types/hasher.d.ts.map +1 -0
- package/dist/types/types/identifier.d.ts +16 -0
- package/dist/types/types/identifier.d.ts.map +1 -0
- package/dist/types/types/key-compressor.d.ts +28 -0
- package/dist/types/types/key-compressor.d.ts.map +1 -0
- package/dist/types/types/key-converter.d.ts +57 -0
- package/dist/types/types/key-converter.d.ts.map +1 -0
- package/dist/types/types/key-deriver.d.ts +39 -0
- package/dist/types/types/key-deriver.d.ts.map +1 -0
- package/dist/types/types/key-generator.d.ts +105 -0
- package/dist/types/types/key-generator.d.ts.map +1 -0
- package/dist/types/types/key-io.d.ts +37 -0
- package/dist/types/types/key-io.d.ts.map +1 -0
- package/dist/types/types/key-wrapper.d.ts +38 -0
- package/dist/types/types/key-wrapper.d.ts.map +1 -0
- package/dist/types/types/params-direct.d.ts +90 -0
- package/dist/types/types/params-direct.d.ts.map +1 -0
- package/dist/types/types/params-enclosed.d.ts +47 -0
- package/dist/types/types/params-enclosed.d.ts.map +1 -0
- package/dist/types/types/params-kms.d.ts +131 -0
- package/dist/types/types/params-kms.d.ts.map +1 -0
- package/dist/types/types/signer.d.ts +46 -0
- package/dist/types/types/signer.d.ts.map +1 -0
- package/dist/types/utils.d.ts +112 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/utils.js +7 -0
- package/dist/utils.js.map +7 -0
- package/package.json +103 -0
- package/src/algorithms/aes-ctr.ts +156 -0
- package/src/algorithms/aes-gcm.ts +187 -0
- package/src/algorithms/crypto-algorithm.ts +4 -0
- package/src/algorithms/ecdsa.ts +269 -0
- package/src/algorithms/eddsa.ts +243 -0
- package/src/algorithms/sha-2.ts +65 -0
- package/src/index.ts +42 -0
- package/src/jose/jwe.ts +196 -0
- package/src/jose/jwk.ts +632 -0
- package/src/jose/jws.ts +95 -0
- package/src/jose/jwt.ts +147 -0
- package/src/jose/utils.ts +34 -0
- package/src/local-key-manager.ts +540 -0
- package/src/primitives/aes-ctr.ts +352 -0
- package/src/primitives/aes-gcm.ts +378 -0
- package/src/primitives/concat-kdf.ts +240 -0
- package/src/primitives/ed25519.ts +548 -0
- package/src/primitives/pbkdf2.ts +122 -0
- package/src/primitives/secp256k1.ts +848 -0
- package/src/primitives/secp256r1.ts +850 -0
- package/src/primitives/sha256.ts +47 -0
- package/src/primitives/x25519.ts +403 -0
- package/src/primitives/xchacha20-poly1305.ts +274 -0
- package/src/primitives/xchacha20.ts +247 -0
- package/src/types/cipher.ts +53 -0
- package/src/types/crypto-api.ts +56 -0
- package/src/types/hasher.ts +32 -0
- package/src/types/identifier.ts +16 -0
- package/src/types/key-compressor.ts +25 -0
- package/src/types/key-converter.ts +53 -0
- package/src/types/key-deriver.ts +43 -0
- package/src/types/key-generator.ts +119 -0
- package/src/types/key-io.ts +42 -0
- package/src/types/key-wrapper.ts +42 -0
- package/src/types/params-direct.ts +106 -0
- package/src/types/params-enclosed.ts +50 -0
- package/src/types/params-kms.ts +156 -0
- package/src/types/signer.ts +50 -0
- package/src/utils.ts +181 -0
package/src/jose/jwk.ts
ADDED
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
import { Convert, removeUndefinedProperties } from '@enbox/common';
|
|
2
|
+
|
|
3
|
+
import { canonicalize } from './utils.js';
|
|
4
|
+
import { Sha256 } from '../primitives/sha256.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Constant defining the prefix for JSON Web Keys (JWK) key URIs in this library.
|
|
8
|
+
*
|
|
9
|
+
* The prefix 'urn:jwk:' makes it explicit that a string represents a JWK, referenced by a
|
|
10
|
+
* {@link https://datatracker.ietf.org/doc/html/rfc3986 | URI} (Uniform Resource Identifier),
|
|
11
|
+
* which ensures consistent key referencing across all Web5 Key Management System (KMS)
|
|
12
|
+
* implementations.
|
|
13
|
+
*
|
|
14
|
+
* These key URIs take the form `urn:jwk:<JWK thumbprint>`, where the
|
|
15
|
+
* {@link https://datatracker.ietf.org/doc/html/rfc7638 | JWK thumbprint}, derived from the JWK, is
|
|
16
|
+
* unique to the key's material, unaffected by the order or optional properties in the JWK.
|
|
17
|
+
*/
|
|
18
|
+
export const KEY_URI_PREFIX_JWK = 'urn:jwk:';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* JSON Web Key Operations
|
|
22
|
+
*
|
|
23
|
+
* The "key_ops" (key operations) parameter identifies the operation(s)
|
|
24
|
+
* for which the key is intended to be used. The "key_ops" parameter is
|
|
25
|
+
* intended for use cases in which public, private, or symmetric keys
|
|
26
|
+
* may be present.
|
|
27
|
+
*
|
|
28
|
+
* Its value is an array of key operation values. Values defined by
|
|
29
|
+
* {@link https://www.rfc-editor.org/rfc/rfc7517.html#section-4.3 | RFC 7517 Section 4.3} are:
|
|
30
|
+
*
|
|
31
|
+
* - "decrypt" : Decrypt content and validate decryption, if applicable
|
|
32
|
+
* - "deriveBits" : Derive bits not to be used as a key
|
|
33
|
+
* - "deriveKey" : Derive key
|
|
34
|
+
* - "encrypt" : Encrypt content
|
|
35
|
+
* - "sign" : Compute digital signature or MAC
|
|
36
|
+
* - "unwrapKey" : Decrypt key and validate decryption, if applicable
|
|
37
|
+
* - "verify" : Verify digital signature or MAC
|
|
38
|
+
* - "wrapKey" : Encrypt key
|
|
39
|
+
*
|
|
40
|
+
* Other values MAY be used. The key operation values are case-
|
|
41
|
+
* sensitive strings. Duplicate key operation values MUST NOT be
|
|
42
|
+
* present in the array. Use of the "key_ops" member is OPTIONAL,
|
|
43
|
+
* unless the application requires its presence.
|
|
44
|
+
*
|
|
45
|
+
* The "use" and "key_ops" JWK members SHOULD NOT be used together;
|
|
46
|
+
* however, if both are used, the information they convey MUST be
|
|
47
|
+
* consistent. Applications should specify which of these members they
|
|
48
|
+
* use, if either is to be used by the application.
|
|
49
|
+
*/
|
|
50
|
+
export type JwkOperation = 'encrypt' | 'decrypt' | 'sign' | 'verify' | 'deriveKey' | 'deriveBits' | 'wrapKey' | 'unwrapKey';
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* JSON Web Key Use
|
|
54
|
+
*
|
|
55
|
+
* The "use" (public key use) parameter identifies the intended use of
|
|
56
|
+
* the public key. The "use" parameter is employed to indicate whether
|
|
57
|
+
* a public key is used for encrypting data or verifying the signature
|
|
58
|
+
* on data.
|
|
59
|
+
*
|
|
60
|
+
* Values defined by {@link https://datatracker.ietf.org/doc/html/rfc7517#section-4.2 | RFC 7517 Section 4.2} are:
|
|
61
|
+
*
|
|
62
|
+
* - "sig" (signature)
|
|
63
|
+
* - "enc" (encryption)
|
|
64
|
+
*
|
|
65
|
+
* Other values MAY be used. The "use" value is a case-sensitive
|
|
66
|
+
* string. Use of the "use" member is OPTIONAL, unless the application
|
|
67
|
+
* requires its presence.
|
|
68
|
+
*
|
|
69
|
+
* The "use" and "key_ops" JWK members SHOULD NOT be used together;
|
|
70
|
+
* however, if both are used, the information they convey MUST be
|
|
71
|
+
* consistent. Applications should specify which of these members they
|
|
72
|
+
* use, if either is to be used by the application.
|
|
73
|
+
*
|
|
74
|
+
* When a key is used to wrap another key and a public key use
|
|
75
|
+
* designation for the first key is desired, the "enc" (encryption) key
|
|
76
|
+
* use value is used, since key wrapping is a kind of encryption. The
|
|
77
|
+
* "enc" value is also to be used for public keys used for key agreement
|
|
78
|
+
* operations.
|
|
79
|
+
*/
|
|
80
|
+
export type JwkUse = 'sig' | 'enc' | string;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* JSON Web Key Types
|
|
84
|
+
*/
|
|
85
|
+
export type JwkType =
|
|
86
|
+
/**
|
|
87
|
+
* Elliptic Curve
|
|
88
|
+
* Used with Elliptic Curve Digital Signature Algorithm (ECDSA) and Elliptic
|
|
89
|
+
* Curve Diffie-Hellman (ECDH), including secp256k1, P-256, P-384, and P-521.
|
|
90
|
+
*/
|
|
91
|
+
| 'EC'
|
|
92
|
+
/**
|
|
93
|
+
* RSA
|
|
94
|
+
* Widely used for encryption and digital signatures. RSA keys are used in
|
|
95
|
+
* various algorithms like RS256, RS384, RS512, etc.
|
|
96
|
+
*/
|
|
97
|
+
| 'RSA'
|
|
98
|
+
/**
|
|
99
|
+
* Octet sequence
|
|
100
|
+
* Used with symmetric signing (e.g., HMAC HS256, HS512, etc.) and
|
|
101
|
+
* symmetric encryption (e.g., A256CBC-HS512, A256GCM, etc.) algorithms.
|
|
102
|
+
*/
|
|
103
|
+
| 'oct'
|
|
104
|
+
/**
|
|
105
|
+
* Octet string key pairs (OKP)
|
|
106
|
+
* A type of public key that is used with algorithms such as EdDSA (Ed25519 and
|
|
107
|
+
* Ed448 curves) and ECDH (X25519 and X448 curves).
|
|
108
|
+
*/
|
|
109
|
+
| 'OKP'
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* JSON Web Key Elliptic Curve
|
|
113
|
+
*/
|
|
114
|
+
export type JwkNamedCurves =
|
|
115
|
+
// P-256 Curve
|
|
116
|
+
| 'P-256'
|
|
117
|
+
// P-384 Curve
|
|
118
|
+
| 'P-384'
|
|
119
|
+
// P-521 Curve
|
|
120
|
+
| 'P-521'
|
|
121
|
+
// Ed25519 signature algorithm key pairs
|
|
122
|
+
| 'Ed25519'
|
|
123
|
+
// Ed448 signature algorithm key pairs
|
|
124
|
+
| 'Ed448'
|
|
125
|
+
// X25519 function key pairs
|
|
126
|
+
| 'X25519'
|
|
127
|
+
// X448 function key pairs
|
|
128
|
+
| 'X448'
|
|
129
|
+
// SECG secp256k1 curve
|
|
130
|
+
| 'secp256k1';
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* JSON Web Key Parameters
|
|
134
|
+
*/
|
|
135
|
+
|
|
136
|
+
/** Parameters used with any "kty" (key type) value. */
|
|
137
|
+
export type JwkParamsAnyKeyType = {
|
|
138
|
+
/** JWK Algorithm Parameter. The algorithm intended for use with the key. */
|
|
139
|
+
alg?: string;
|
|
140
|
+
/** JWK Extractable Parameter */
|
|
141
|
+
ext?: 'true' | 'false';
|
|
142
|
+
/** JWK Key Operations Parameter */
|
|
143
|
+
key_ops?: JwkOperation[];
|
|
144
|
+
/** JWK Key ID Parameter */
|
|
145
|
+
kid?: string;
|
|
146
|
+
/** JWK Key Type Parameter */
|
|
147
|
+
kty: JwkType;
|
|
148
|
+
/** JWK Public Key Use Parameter */
|
|
149
|
+
use?: JwkUse;
|
|
150
|
+
/** JWK X.509 Certificate Chain Parameter */
|
|
151
|
+
x5c?: string;
|
|
152
|
+
/** JWK X.509 Certificate SHA-1 Thumbprint Parameter */
|
|
153
|
+
x5t?: string;
|
|
154
|
+
/** JWK X.509 Certificate SHA-256 Thumbprint Parameter */
|
|
155
|
+
'x5t#S256'?: string;
|
|
156
|
+
/** JWK X.509 URL Parameter */
|
|
157
|
+
x5u?: string;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Parameters used with "EC" (elliptic curve) public keys. */
|
|
161
|
+
export type JwkParamsEcPublic = Omit<JwkParamsAnyKeyType, 'alg' | 'kty'> & {
|
|
162
|
+
/**
|
|
163
|
+
* The algorithm intended for use with the key.
|
|
164
|
+
* ES256 : ECDSA using P-256 and SHA-256
|
|
165
|
+
* ES256K : ECDSA using secp256k1 curve and SHA-256
|
|
166
|
+
* ES384 : ECDSA using P-384 and SHA-384
|
|
167
|
+
* ES512 : ECDSA using P-521 and SHA-512
|
|
168
|
+
*/
|
|
169
|
+
alg?: 'ES256' | 'ES256K' | 'ES384' | 'ES512';
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Elliptic Curve key pair.
|
|
173
|
+
*/
|
|
174
|
+
kty: 'EC';
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* The cryptographic curve used with the key.
|
|
178
|
+
* MUST be present for all EC public keys.
|
|
179
|
+
*/
|
|
180
|
+
crv: 'secp256k1' | 'P-256' | 'P-384' | 'P-521';
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* The x-coordinate for the Elliptic Curve point.
|
|
184
|
+
* Represented as the base64url encoding of the octet string
|
|
185
|
+
* representation of the coordinate.
|
|
186
|
+
* MUST be present for all EC public keys
|
|
187
|
+
*/
|
|
188
|
+
x: string;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The y-coordinate for the Elliptic Curve point.
|
|
192
|
+
* Represented as the base64url encoding of the octet string
|
|
193
|
+
* representation of the coordinate.
|
|
194
|
+
* MUST be present only for secp256k1 public keys.
|
|
195
|
+
*/
|
|
196
|
+
y?: string;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Parameters used with "EC" (elliptic curve) private keys. */
|
|
200
|
+
export type JwkParamsEcPrivate = JwkParamsEcPublic & {
|
|
201
|
+
/**
|
|
202
|
+
* The d-coordinate for the Elliptic Curve point.
|
|
203
|
+
* Represented as the base64url encoding of the octet string
|
|
204
|
+
* representation of the coordinate.
|
|
205
|
+
* MUST be present for all EC private keys.
|
|
206
|
+
*/
|
|
207
|
+
d: string;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Parameters used with "OKP" (octet key pair) public keys. */
|
|
211
|
+
export type JwkParamsOkpPublic =
|
|
212
|
+
Omit<JwkParamsAnyKeyType, 'kty' | 'alg' | 'crv'> &
|
|
213
|
+
Pick<JwkParamsEcPublic, 'x'> & {
|
|
214
|
+
/**
|
|
215
|
+
* The algorithm intended for use with the key.
|
|
216
|
+
* EdDSA: Edwards Curve Digital Signature Algorithm
|
|
217
|
+
*/
|
|
218
|
+
alg?: 'EdDSA';
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* The cryptographic curve used with the key.
|
|
222
|
+
* MUST be present for all OKP public keys.
|
|
223
|
+
*/
|
|
224
|
+
crv: 'Ed25519' | 'Ed448' | 'X25519' | 'X448';
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Key type
|
|
228
|
+
* OKP (Octet Key Pair) is defined for public key algorithms that use octet
|
|
229
|
+
* strings as private and public keys.
|
|
230
|
+
*/
|
|
231
|
+
kty: 'OKP';
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Parameters used with "OKP" (octet key pair) private keys. */
|
|
235
|
+
export type JwkParamsOkpPrivate = JwkParamsOkpPublic & {
|
|
236
|
+
/**
|
|
237
|
+
* The d-coordinate for the Edwards Curve point.
|
|
238
|
+
* Represented as the base64url encoding of the octet string
|
|
239
|
+
* representation of the coordinate.
|
|
240
|
+
* MUST be present for all EC private keys.
|
|
241
|
+
*/
|
|
242
|
+
d: string;
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
/** Parameters used with "oct" (octet sequence) private keys. */
|
|
246
|
+
export type JwkParamsOctPrivate = Omit<JwkParamsAnyKeyType, 'alg' | 'kty'> & {
|
|
247
|
+
/**
|
|
248
|
+
* The algorithm intended for use with the key.
|
|
249
|
+
* Used with symmetric signing (e.g., HMAC HS256, etc.) and
|
|
250
|
+
* symmetric encryption (e.g., A256GCM, etc.) algorithms.
|
|
251
|
+
*/
|
|
252
|
+
alg?:
|
|
253
|
+
// AES CBC using 128-bit key
|
|
254
|
+
| 'A128CBC'
|
|
255
|
+
// AES CBC using 192-bit key
|
|
256
|
+
| 'A192CBC'
|
|
257
|
+
// AES CBC using 256-bit key
|
|
258
|
+
| 'A256CBC'
|
|
259
|
+
// AES CTR using 128-bit key
|
|
260
|
+
| 'A128CTR'
|
|
261
|
+
// AES CTR using 192-bit key
|
|
262
|
+
| 'A192CTR'
|
|
263
|
+
// AES CTR using 256-bit key
|
|
264
|
+
| 'A256CTR'
|
|
265
|
+
// AES GCM using a 128-bit key
|
|
266
|
+
| 'A128GCM'
|
|
267
|
+
// AES GCM using a 192-bit key
|
|
268
|
+
| 'A192GCM'
|
|
269
|
+
// AES GCM using a 256-bit key
|
|
270
|
+
| 'A256GCM'
|
|
271
|
+
// HMAC using SHA-256
|
|
272
|
+
| 'HS256'
|
|
273
|
+
// HMAC using SHA-384
|
|
274
|
+
| 'HS384'
|
|
275
|
+
// HMAC using SHA-512
|
|
276
|
+
| 'HS512'
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* The "k" (key value) parameter contains the value of the symmetric
|
|
280
|
+
* (or other single-valued) key. It is represented as the base64url
|
|
281
|
+
* encoding of the octet sequence containing the key value.
|
|
282
|
+
*/
|
|
283
|
+
k: string;
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Key type
|
|
287
|
+
* oct (Octet Sequence) is defined for symmetric encryption and
|
|
288
|
+
* symmetric signature algorithms.
|
|
289
|
+
*/
|
|
290
|
+
kty: 'oct';
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** Parameters Used with "RSA" public keys. */
|
|
294
|
+
export type JwkParamsRsaPublic = Omit<JwkParamsAnyKeyType, 'kty'> & {
|
|
295
|
+
/** Public exponent for RSA */
|
|
296
|
+
e: string;
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Key type
|
|
300
|
+
* RSA is widely used for encryption and digital signatures.
|
|
301
|
+
*/
|
|
302
|
+
kty: 'RSA';
|
|
303
|
+
|
|
304
|
+
/** Modulus for RSA */
|
|
305
|
+
n: string;
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
/** Parameters used with "RSA" private keys. */
|
|
309
|
+
export type JwkParamsRsaPrivate = JwkParamsRsaPublic & {
|
|
310
|
+
/** Private exponent for RSA */
|
|
311
|
+
d: string;
|
|
312
|
+
/** First prime factor for RSA */
|
|
313
|
+
p?: string;
|
|
314
|
+
/** Second prime factor for RSA */
|
|
315
|
+
q?: string;
|
|
316
|
+
/** First factor's CRT exponent for RSA */
|
|
317
|
+
dp?: string;
|
|
318
|
+
/** Second factor's CRT exponent for RSA */
|
|
319
|
+
dq?: string;
|
|
320
|
+
/** First CRT coefficient for RSA */
|
|
321
|
+
qi?: string;
|
|
322
|
+
/** Other primes information (optional in RFC 7518) */
|
|
323
|
+
oth?: {
|
|
324
|
+
/** Other primes' factor */
|
|
325
|
+
r: string;
|
|
326
|
+
/** Other primes' CRT exponent */
|
|
327
|
+
d: string;
|
|
328
|
+
/** Other primes' CRT coefficient */
|
|
329
|
+
t: string;
|
|
330
|
+
}[];
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
/** Parameters used with public keys in JWK format. */
|
|
334
|
+
export type PublicKeyJwk = JwkParamsEcPublic | JwkParamsOkpPublic | JwkParamsRsaPublic;
|
|
335
|
+
|
|
336
|
+
/** Parameters used with private keys in JWK format. */
|
|
337
|
+
export type PrivateKeyJwk = JwkParamsEcPrivate | JwkParamsOkpPrivate | JwkParamsOctPrivate | JwkParamsRsaPrivate;
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* JSON Web Key ({@link https://datatracker.ietf.org/doc/html/rfc7517 | JWK}).
|
|
341
|
+
* "RSA", "EC", "OKP", and "oct" key types are supported.
|
|
342
|
+
*/
|
|
343
|
+
export interface Jwk {
|
|
344
|
+
// Common properties that apply to all key types.
|
|
345
|
+
|
|
346
|
+
/** JWK Algorithm Parameter. The algorithm intended for use with the key. */
|
|
347
|
+
alg?: string;
|
|
348
|
+
/** JWK Extractable Parameter */
|
|
349
|
+
ext?: 'true' | 'false';
|
|
350
|
+
/** JWK Key Operations Parameter */
|
|
351
|
+
key_ops?: JwkOperation[];
|
|
352
|
+
/** JWK Key ID Parameter */
|
|
353
|
+
kid?: string;
|
|
354
|
+
/** JWK Key Type Parameter */
|
|
355
|
+
kty: JwkType;
|
|
356
|
+
/** JWK Public Key Use Parameter */
|
|
357
|
+
use?: JwkUse;
|
|
358
|
+
/** JWK X.509 Certificate Chain Parameter */
|
|
359
|
+
x5c?: string;
|
|
360
|
+
/** JWK X.509 Certificate SHA-1 Thumbprint Parameter */
|
|
361
|
+
x5t?: string;
|
|
362
|
+
/** JWK X.509 Certificate SHA-256 Thumbprint Parameter */
|
|
363
|
+
'x5t#S256'?: string;
|
|
364
|
+
/** JWK X.509 URL Parameter */
|
|
365
|
+
x5u?: string;
|
|
366
|
+
|
|
367
|
+
// Elliptic Curve (EC or OKP) public key properties.
|
|
368
|
+
|
|
369
|
+
/** The cryptographic curve used with the key. */
|
|
370
|
+
crv?: string;
|
|
371
|
+
/** The x-coordinate for the Elliptic Curve point. */
|
|
372
|
+
x?: string;
|
|
373
|
+
/** The y-coordinate for the Elliptic Curve point. */
|
|
374
|
+
y?: string;
|
|
375
|
+
|
|
376
|
+
// Symmetric key properties.
|
|
377
|
+
|
|
378
|
+
/** The "k" (key value) parameter contains the value of the symmetric (or other single-valued) key. */
|
|
379
|
+
k?: string;
|
|
380
|
+
|
|
381
|
+
// RSA public key properties.
|
|
382
|
+
|
|
383
|
+
/** Public exponent for RSA */
|
|
384
|
+
e?: string;
|
|
385
|
+
/** Modulus for RSA */
|
|
386
|
+
n?: string;
|
|
387
|
+
/** First prime factor for RSA */
|
|
388
|
+
p?: string;
|
|
389
|
+
/** Second prime factor for RSA */
|
|
390
|
+
q?: string;
|
|
391
|
+
/** First factor's CRT exponent for RSA */
|
|
392
|
+
dp?: string;
|
|
393
|
+
/** Second factor's CRT exponent for RSA */
|
|
394
|
+
dq?: string;
|
|
395
|
+
/** First CRT coefficient for RSA */
|
|
396
|
+
qi?: string;
|
|
397
|
+
/** Other primes information (optional in RFC 7518) */
|
|
398
|
+
oth?: {
|
|
399
|
+
/** Other primes' factor */
|
|
400
|
+
r: string;
|
|
401
|
+
/** Other primes' CRT exponent */
|
|
402
|
+
d: string;
|
|
403
|
+
/** Other primes' CRT coefficient */
|
|
404
|
+
t: string;
|
|
405
|
+
}[];
|
|
406
|
+
|
|
407
|
+
// Elliptic Curve and RSA private key properties.
|
|
408
|
+
|
|
409
|
+
/** Private key component for EC, OKP, or RSA keys. */
|
|
410
|
+
d?: string;
|
|
411
|
+
|
|
412
|
+
// Additional public or private properties.
|
|
413
|
+
[key: string]: unknown;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* JSON Web Key Set ({@link https://datatracker.ietf.org/doc/html/rfc7517 | JWK Set})
|
|
418
|
+
*
|
|
419
|
+
* @remarks
|
|
420
|
+
* A JWK Set is a JSON object that represents a set of JWKs. The JSON object MUST have a "keys"
|
|
421
|
+
* member, with its value being an array of JWKs.
|
|
422
|
+
*
|
|
423
|
+
* Additional members can be present in the JWK Set but member names MUST be unique. If not
|
|
424
|
+
* understood by implementations encountering them, they MUST be ignored. Parameters for
|
|
425
|
+
* representing additional properties of JWK Sets should either be registered in the IANA
|
|
426
|
+
* "JSON Web Key Set Parameters" registry or be a value that contains a Collision-Resistant Name.
|
|
427
|
+
*/
|
|
428
|
+
export interface JwkSet {
|
|
429
|
+
/** Array of JWKs */
|
|
430
|
+
keys: Jwk[]
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Computes the thumbprint of a JSON Web Key (JWK) using the method
|
|
435
|
+
* specified in RFC 7638. This function accepts RSA, EC, OKP, and oct keys
|
|
436
|
+
* and returns the thumbprint as a base64url encoded SHA-256 hash of the
|
|
437
|
+
* JWK's required members, serialized and sorted lexicographically.
|
|
438
|
+
*
|
|
439
|
+
* Purpose:
|
|
440
|
+
* - Uniquely Identifying Keys: The thumbprint allows the unique
|
|
441
|
+
* identification of a specific JWK within a set of JWKs. It provides a
|
|
442
|
+
* deterministic way to generate a value that can be used as a key
|
|
443
|
+
* identifier (kid) or to match a specific key.
|
|
444
|
+
*
|
|
445
|
+
* - Simplifying Key Management: In systems where multiple keys are used,
|
|
446
|
+
* managing and identifying individual keys can become complex. The
|
|
447
|
+
* thumbprint method simplifies this by creating a standardized, unique
|
|
448
|
+
* identifier for each key.
|
|
449
|
+
*
|
|
450
|
+
* - Enabling Interoperability: By standardizing the method to compute a
|
|
451
|
+
* thumbprint, different systems can compute the same thumbprint value for
|
|
452
|
+
* a given JWK. This enables interoperability among systems that use JWKs.
|
|
453
|
+
*
|
|
454
|
+
* - Secure Comparison: The thumbprint provides a way to securely compare
|
|
455
|
+
* JWKs to determine if they are equivalent.
|
|
456
|
+
*
|
|
457
|
+
* @example
|
|
458
|
+
* ```ts
|
|
459
|
+
* const jwk: PublicKeyJwk = {
|
|
460
|
+
* 'kty': 'EC',
|
|
461
|
+
* 'crv': 'secp256k1',
|
|
462
|
+
* 'x': '61iPYuGefxotzBdQZtDvv6cWHZmXrTTscY-u7Y2pFZc',
|
|
463
|
+
* 'y': '88nPCVLfrAY9i-wg5ORcwVbHWC_tbeAd1JE2e0co0lU'
|
|
464
|
+
* };
|
|
465
|
+
*
|
|
466
|
+
* const thumbprint = jwkThumbprint(jwk);
|
|
467
|
+
* console.log(`JWK thumbprint: ${thumbprint}`);
|
|
468
|
+
* ```
|
|
469
|
+
*
|
|
470
|
+
* @see {@link https://datatracker.ietf.org/doc/html/rfc7638 | RFC7638} for
|
|
471
|
+
* the specification of JWK thumbprint computation.
|
|
472
|
+
*
|
|
473
|
+
* @param jwk - The JSON Web Key for which the thumbprint will be computed.
|
|
474
|
+
* This must be an RSA, EC, OKP, or oct key.
|
|
475
|
+
* @returns The thumbprint as a base64url encoded string.
|
|
476
|
+
* @throws Throws an `Error` if the provided key type is unsupported.
|
|
477
|
+
*/
|
|
478
|
+
export async function computeJwkThumbprint({ jwk }: {
|
|
479
|
+
jwk: Jwk
|
|
480
|
+
}): Promise<string> {
|
|
481
|
+
/** Step 1 - Normalization: The JWK is normalized to include only specific
|
|
482
|
+
* members and in lexicographic order.
|
|
483
|
+
*/
|
|
484
|
+
const keyType = jwk.kty;
|
|
485
|
+
let normalizedJwk: Jwk;
|
|
486
|
+
if (keyType === 'EC') {
|
|
487
|
+
normalizedJwk = { crv: jwk.crv, kty: jwk.kty, x: jwk.x, y: jwk.y };
|
|
488
|
+
} else if (keyType === 'oct') {
|
|
489
|
+
normalizedJwk = { k: jwk.k, kty: jwk.kty };
|
|
490
|
+
} else if (keyType === 'OKP') {
|
|
491
|
+
normalizedJwk = { crv: jwk.crv, kty: jwk.kty, x: jwk.x };
|
|
492
|
+
} else if (keyType === 'RSA') {
|
|
493
|
+
normalizedJwk = { e: jwk.e, kty: jwk.kty, n: jwk.n };
|
|
494
|
+
} else {
|
|
495
|
+
throw new Error(`Unsupported key type: ${keyType}`);
|
|
496
|
+
}
|
|
497
|
+
removeUndefinedProperties(normalizedJwk);
|
|
498
|
+
|
|
499
|
+
/** Step 2 - Serialization: The normalized JWK is serialized to a UTF-8
|
|
500
|
+
* representation of its JSON encoding. */
|
|
501
|
+
const serializedJwk = canonicalize(normalizedJwk);
|
|
502
|
+
|
|
503
|
+
/** Step 3 - Digest Calculation: A cryptographic hash function
|
|
504
|
+
* (SHA-256 is recommended) is applied to the serialized JWK,
|
|
505
|
+
* resulting in the thumbprint. */
|
|
506
|
+
const utf8Bytes = Convert.string(serializedJwk).toUint8Array();
|
|
507
|
+
const digest = await Sha256.digest({ data: utf8Bytes });
|
|
508
|
+
|
|
509
|
+
// Encode as Base64Url.
|
|
510
|
+
const thumbprint = Convert.uint8Array(digest).toBase64Url();
|
|
511
|
+
|
|
512
|
+
return thumbprint;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* Checks if the provided object is a valid elliptic curve private key in JWK format.
|
|
517
|
+
*
|
|
518
|
+
* @param obj - The object to check.
|
|
519
|
+
* @returns True if the object is a valid EC private JWK; otherwise, false.
|
|
520
|
+
*/
|
|
521
|
+
export function isEcPrivateJwk(obj: unknown): obj is JwkParamsEcPrivate {
|
|
522
|
+
if (!obj || typeof obj !== 'object') return false;
|
|
523
|
+
if (!('kty' in obj && 'crv' in obj && 'x' in obj && 'd' in obj)) return false;
|
|
524
|
+
if (obj.kty !== 'EC') return false;
|
|
525
|
+
if (typeof obj.d !== 'string') return false;
|
|
526
|
+
if (typeof obj.x !== 'string') return false;
|
|
527
|
+
return true;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* Checks if the provided object is a valid elliptic curve public key in JWK format.
|
|
532
|
+
*
|
|
533
|
+
* @param obj - The object to check.
|
|
534
|
+
* @returns True if the object is a valid EC public JWK; otherwise, false.
|
|
535
|
+
*/
|
|
536
|
+
export function isEcPublicJwk(obj: unknown): obj is JwkParamsEcPublic {
|
|
537
|
+
if (!obj || typeof obj !== 'object') return false;
|
|
538
|
+
if (!('kty' in obj && 'crv' in obj && 'x' in obj)) return false;
|
|
539
|
+
if ('d' in obj) return false;
|
|
540
|
+
if (obj.kty !== 'EC') return false;
|
|
541
|
+
if (typeof obj.x !== 'string') return false;
|
|
542
|
+
return true;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Checks if the provided object is a valid octet sequence (symmetric key) in JWK format.
|
|
547
|
+
*
|
|
548
|
+
* @param obj - The object to check.
|
|
549
|
+
* @returns True if the object is a valid oct private JWK; otherwise, false.
|
|
550
|
+
*/
|
|
551
|
+
export function isOctPrivateJwk(obj: unknown): obj is JwkParamsOctPrivate {
|
|
552
|
+
if (!obj || typeof obj !== 'object') return false;
|
|
553
|
+
if (!('kty' in obj && 'k' in obj)) return false;
|
|
554
|
+
if (obj.kty !== 'oct') return false;
|
|
555
|
+
if (typeof obj.k !== 'string') return false;
|
|
556
|
+
return true;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* Checks if the provided object is a valid octet key pair private key in JWK format.
|
|
561
|
+
*
|
|
562
|
+
* @param obj - The object to check.
|
|
563
|
+
* @returns True if the object is a valid OKP private JWK; otherwise, false.
|
|
564
|
+
*/
|
|
565
|
+
export function isOkpPrivateJwk(obj: unknown): obj is JwkParamsOkpPrivate {
|
|
566
|
+
if (!obj || typeof obj !== 'object') return false;
|
|
567
|
+
if (!('kty' in obj && 'crv' in obj && 'x' in obj && 'd' in obj)) return false;
|
|
568
|
+
if (obj.kty !== 'OKP') return false;
|
|
569
|
+
if (typeof obj.d !== 'string') return false;
|
|
570
|
+
if (typeof obj.x !== 'string') return false;
|
|
571
|
+
return true;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* Checks if the provided object is a valid octet key pair public key in JWK format.
|
|
576
|
+
*
|
|
577
|
+
* @param obj - The object to check.
|
|
578
|
+
* @returns True if the object is a valid OKP public JWK; otherwise, false.
|
|
579
|
+
*/
|
|
580
|
+
export function isOkpPublicJwk(obj: unknown): obj is JwkParamsOkpPublic {
|
|
581
|
+
if (!obj || typeof obj !== 'object') return false;
|
|
582
|
+
if ('d' in obj) return false;
|
|
583
|
+
if (!('kty' in obj && 'crv' in obj && 'x' in obj)) return false;
|
|
584
|
+
if (obj.kty !== 'OKP') return false;
|
|
585
|
+
if (typeof obj.x !== 'string') return false;
|
|
586
|
+
return true;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Checks if the provided object is a valid private key in JWK format of any supported type.
|
|
591
|
+
*
|
|
592
|
+
* @param obj - The object to check.
|
|
593
|
+
* @returns True if the object is a valid private JWK; otherwise, false.
|
|
594
|
+
*/
|
|
595
|
+
export function isPrivateJwk(obj: unknown): obj is PrivateKeyJwk {
|
|
596
|
+
if (!obj || typeof obj !== 'object') return false;
|
|
597
|
+
|
|
598
|
+
const kty = (obj as { kty: string }).kty;
|
|
599
|
+
|
|
600
|
+
switch (kty) {
|
|
601
|
+
case 'EC':
|
|
602
|
+
case 'OKP':
|
|
603
|
+
case 'RSA':
|
|
604
|
+
return 'd' in obj;
|
|
605
|
+
case 'oct':
|
|
606
|
+
return 'k' in obj;
|
|
607
|
+
default:
|
|
608
|
+
return false;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* Checks if the provided object is a valid public key in JWK format of any supported type.
|
|
614
|
+
*
|
|
615
|
+
* @param obj - The object to check.
|
|
616
|
+
* @returns True if the object is a valid public JWK; otherwise, false.
|
|
617
|
+
*/
|
|
618
|
+
export function isPublicJwk(obj: unknown): obj is PublicKeyJwk {
|
|
619
|
+
if (!obj || typeof obj !== 'object') return false;
|
|
620
|
+
|
|
621
|
+
const kty = (obj as { kty: string }).kty;
|
|
622
|
+
|
|
623
|
+
switch (kty) {
|
|
624
|
+
case 'EC':
|
|
625
|
+
case 'OKP':
|
|
626
|
+
return 'x' in obj && !('d' in obj);
|
|
627
|
+
case 'RSA':
|
|
628
|
+
return 'n' in obj && 'e' in obj && !('d' in obj);
|
|
629
|
+
default:
|
|
630
|
+
return false;
|
|
631
|
+
}
|
|
632
|
+
}
|