@sphereon/ssi-sdk-ext.key-utils 0.28.1-feature.oyd.cmsm.improv.21 → 0.28.1-next.53
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/dist/index.cjs +1434 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +231 -0
- package/dist/index.d.ts +228 -9
- package/dist/index.js +1401 -26
- package/dist/index.js.map +1 -1
- package/package.json +26 -13
- package/src/conversion.ts +8 -6
- package/src/digest-methods.ts +12 -11
- package/src/functions.ts +299 -53
- package/src/jwk-jcs.ts +3 -1
- package/src/types/key-util-types.ts +1 -1
- package/dist/conversion.d.ts +0 -12
- package/dist/conversion.d.ts.map +0 -1
- package/dist/conversion.js +0 -206
- package/dist/conversion.js.map +0 -1
- package/dist/digest-methods.d.ts +0 -11
- package/dist/digest-methods.d.ts.map +0 -1
- package/dist/digest-methods.js +0 -106
- package/dist/digest-methods.js.map +0 -1
- package/dist/functions.d.ts +0 -100
- package/dist/functions.d.ts.map +0 -1
- package/dist/functions.js +0 -756
- package/dist/functions.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/jwk-jcs.d.ts +0 -41
- package/dist/jwk-jcs.d.ts.map +0 -1
- package/dist/jwk-jcs.js +0 -182
- package/dist/jwk-jcs.js.map +0 -1
- package/dist/types/index.d.ts +0 -2
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js +0 -18
- package/dist/types/index.js.map +0 -1
- package/dist/types/key-util-types.d.ts +0 -46
- package/dist/types/key-util-types.d.ts.map +0 -1
- package/dist/types/key-util-types.js +0 -19
- package/dist/types/key-util-types.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,28 +1,1403 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/functions.ts
|
|
5
|
+
import { randomBytes } from "@ethersproject/random";
|
|
6
|
+
import { bls12_381 } from "@noble/curves/bls12-381";
|
|
7
|
+
import { ed25519 } from "@noble/curves/ed25519";
|
|
8
|
+
import { p256 } from "@noble/curves/p256";
|
|
9
|
+
import { p384 } from "@noble/curves/p384";
|
|
10
|
+
import { p521 } from "@noble/curves/p521";
|
|
11
|
+
import { secp256k1 } from "@noble/curves/secp256k1";
|
|
12
|
+
import { sha256 as sha2562, sha384 as sha3842, sha512 as sha5122 } from "@noble/hashes/sha2";
|
|
13
|
+
import { cryptoSubtleImportRSAKey, generateRSAKeyAsPEM, hexToBase64, hexToPEM, PEMToJwk, privateKeyHexFromPEM } from "@sphereon/ssi-sdk-ext.x509-utils";
|
|
14
|
+
import { JoseCurve, JoseSignatureAlgorithm, JwkKeyType, Loggers } from "@sphereon/ssi-types";
|
|
15
|
+
import { generateKeyPair as generateSigningKeyPair } from "@stablelib/ed25519";
|
|
16
|
+
import debug from "debug";
|
|
17
|
+
import elliptic from "elliptic";
|
|
18
|
+
import * as rsa from "micro-rsa-dsa-dh/rsa.js";
|
|
19
|
+
import * as u8a2 from "uint8arrays";
|
|
20
|
+
|
|
21
|
+
// src/digest-methods.ts
|
|
22
|
+
import { sha256 } from "@noble/hashes/sha256";
|
|
23
|
+
import { sha384, sha512 } from "@noble/hashes/sha512";
|
|
24
|
+
import * as u8a from "uint8arrays";
|
|
25
|
+
var { fromString, toString, SupportedEncodings } = u8a;
|
|
26
|
+
var digestMethodParams = /* @__PURE__ */ __name((hashAlgorithm) => {
|
|
27
|
+
if (hashAlgorithm === "SHA-256") {
|
|
28
|
+
return {
|
|
29
|
+
hashAlgorithm: "SHA-256",
|
|
30
|
+
digestMethod: sha256DigestMethod,
|
|
31
|
+
hash: sha256
|
|
32
|
+
};
|
|
33
|
+
} else if (hashAlgorithm === "SHA-384") {
|
|
34
|
+
return {
|
|
35
|
+
hashAlgorithm: "SHA-384",
|
|
36
|
+
digestMethod: sha384DigestMethod,
|
|
37
|
+
hash: sha384
|
|
38
|
+
};
|
|
39
|
+
} else {
|
|
40
|
+
return {
|
|
41
|
+
hashAlgorithm: "SHA-512",
|
|
42
|
+
digestMethod: sha512DigestMethod,
|
|
43
|
+
hash: sha512
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
}, "digestMethodParams");
|
|
47
|
+
var shaHasher = /* @__PURE__ */ __name((input, alg) => {
|
|
48
|
+
const hashAlgorithm = alg.includes("384") ? "SHA-384" : alg.includes("512") ? "SHA-512" : "SHA-256";
|
|
49
|
+
return digestMethodParams(hashAlgorithm).hash(typeof input === "string" ? fromString(input, "utf-8") : new Uint8Array(input));
|
|
50
|
+
}, "shaHasher");
|
|
51
|
+
var sha256DigestMethod = /* @__PURE__ */ __name((input, encoding = "base16") => {
|
|
52
|
+
return toString(sha256(fromString(input, "utf-8")), encoding);
|
|
53
|
+
}, "sha256DigestMethod");
|
|
54
|
+
var sha384DigestMethod = /* @__PURE__ */ __name((input, encoding = "base16") => {
|
|
55
|
+
return toString(sha384(fromString(input, "utf-8")), encoding);
|
|
56
|
+
}, "sha384DigestMethod");
|
|
57
|
+
var sha512DigestMethod = /* @__PURE__ */ __name((input, encoding = "base16") => {
|
|
58
|
+
return toString(sha512(fromString(input, "utf-8")), encoding);
|
|
59
|
+
}, "sha512DigestMethod");
|
|
60
|
+
|
|
61
|
+
// src/jwk-jcs.ts
|
|
62
|
+
import { TextDecoder, TextEncoder } from "web-encoding";
|
|
63
|
+
var textEncoder = new TextEncoder();
|
|
64
|
+
var textDecoder = new TextDecoder();
|
|
65
|
+
function check(value, description, optional = false) {
|
|
66
|
+
if (optional && !value) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (typeof value !== "string" || !value) {
|
|
70
|
+
throw new Error(`${description} missing or invalid`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
__name(check, "check");
|
|
74
|
+
function assertObject(value) {
|
|
75
|
+
if (!value || typeof value !== "object") {
|
|
76
|
+
throw new Error("Value must be an object");
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
__name(assertObject, "assertObject");
|
|
80
|
+
function validateJwk(jwk, opts) {
|
|
81
|
+
assertObject(jwk);
|
|
82
|
+
const { crvOptional = false } = opts ?? {};
|
|
83
|
+
check(jwk.kty, '"kty" (Key Type) Parameter', false);
|
|
84
|
+
switch (jwk.kty) {
|
|
85
|
+
/**
|
|
86
|
+
* @see https://www.rfc-editor.org/rfc/rfc7518#section-6.2.1
|
|
87
|
+
*/
|
|
88
|
+
case "EC":
|
|
89
|
+
check(jwk.crv, '"crv" (Curve) Parameter', crvOptional);
|
|
90
|
+
check(jwk.x, '"x" (X Coordinate) Parameter');
|
|
91
|
+
check(jwk.y, '"y" (Y Coordinate) Parameter');
|
|
92
|
+
break;
|
|
93
|
+
/**
|
|
94
|
+
* @see https://www.rfc-editor.org/rfc/rfc8037#section-2
|
|
95
|
+
*/
|
|
96
|
+
case "OKP":
|
|
97
|
+
check(jwk.crv, '"crv" (Subtype of Key Pair) Parameter', crvOptional);
|
|
98
|
+
check(jwk.x, '"x" (Public Key) Parameter');
|
|
99
|
+
break;
|
|
100
|
+
/**
|
|
101
|
+
* @see https://www.rfc-editor.org/rfc/rfc7518#section-6.3.1
|
|
102
|
+
*/
|
|
103
|
+
case "RSA":
|
|
104
|
+
check(jwk.e, '"e" (Exponent) Parameter');
|
|
105
|
+
check(jwk.n, '"n" (Modulus) Parameter');
|
|
106
|
+
break;
|
|
107
|
+
default:
|
|
108
|
+
throw new Error('"kty" (Key Type) Parameter missing or unsupported');
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
__name(validateJwk, "validateJwk");
|
|
112
|
+
function minimalJwk(jwk) {
|
|
113
|
+
switch (jwk.kty) {
|
|
114
|
+
case "EC":
|
|
115
|
+
return {
|
|
116
|
+
...jwk.crv && {
|
|
117
|
+
crv: jwk.crv
|
|
118
|
+
},
|
|
119
|
+
kty: jwk.kty,
|
|
120
|
+
x: jwk.x,
|
|
121
|
+
y: jwk.y
|
|
122
|
+
};
|
|
123
|
+
case "OKP":
|
|
124
|
+
return {
|
|
125
|
+
...jwk.crv && {
|
|
126
|
+
crv: jwk.crv
|
|
127
|
+
},
|
|
128
|
+
kty: jwk.kty,
|
|
129
|
+
x: jwk.x
|
|
130
|
+
};
|
|
131
|
+
case "RSA":
|
|
132
|
+
return {
|
|
133
|
+
e: jwk.e,
|
|
134
|
+
kty: jwk.kty,
|
|
135
|
+
n: jwk.n
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
throw Error(`Unsupported key type (kty) provided: ${jwk.kty}`);
|
|
139
|
+
}
|
|
140
|
+
__name(minimalJwk, "minimalJwk");
|
|
141
|
+
function jwkJcsEncode(jwk) {
|
|
142
|
+
validateJwk(jwk);
|
|
143
|
+
const strippedJwk = minimalJwk(jwk);
|
|
144
|
+
return textEncoder.encode(jcsCanonicalize(strippedJwk));
|
|
145
|
+
}
|
|
146
|
+
__name(jwkJcsEncode, "jwkJcsEncode");
|
|
147
|
+
function jwkJcsDecode(bytes) {
|
|
148
|
+
const jwk = JSON.parse(textDecoder.decode(bytes));
|
|
149
|
+
validateJwk(jwk);
|
|
150
|
+
if (JSON.stringify(jwk) !== jcsCanonicalize(minimalJwk(jwk))) {
|
|
151
|
+
throw new Error("The JWK embedded in the DID is not correctly formatted");
|
|
152
|
+
}
|
|
153
|
+
return jwk;
|
|
154
|
+
}
|
|
155
|
+
__name(jwkJcsDecode, "jwkJcsDecode");
|
|
156
|
+
function jcsCanonicalize(object) {
|
|
157
|
+
let buffer = "";
|
|
158
|
+
serialize(object);
|
|
159
|
+
return buffer;
|
|
160
|
+
function serialize(object2) {
|
|
161
|
+
if (object2 === null || typeof object2 !== "object" || object2.toJSON != null) {
|
|
162
|
+
buffer += JSON.stringify(object2);
|
|
163
|
+
} else if (Array.isArray(object2)) {
|
|
164
|
+
buffer += "[";
|
|
165
|
+
let next = false;
|
|
166
|
+
object2.forEach((element) => {
|
|
167
|
+
if (next) {
|
|
168
|
+
buffer += ",";
|
|
169
|
+
}
|
|
170
|
+
next = true;
|
|
171
|
+
serialize(element);
|
|
172
|
+
});
|
|
173
|
+
buffer += "]";
|
|
174
|
+
} else {
|
|
175
|
+
buffer += "{";
|
|
176
|
+
let next = false;
|
|
177
|
+
Object.keys(object2).sort().forEach((property) => {
|
|
178
|
+
if (next) {
|
|
179
|
+
buffer += ",";
|
|
180
|
+
}
|
|
181
|
+
next = true;
|
|
182
|
+
buffer += JSON.stringify(property);
|
|
183
|
+
buffer += ":";
|
|
184
|
+
serialize(object2[property]);
|
|
185
|
+
});
|
|
186
|
+
buffer += "}";
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
__name(serialize, "serialize");
|
|
190
|
+
}
|
|
191
|
+
__name(jcsCanonicalize, "jcsCanonicalize");
|
|
192
|
+
|
|
193
|
+
// src/types/key-util-types.ts
|
|
194
|
+
var JWK_JCS_PUB_NAME = "jwk_jcs-pub";
|
|
195
|
+
var JWK_JCS_PUB_PREFIX = 60241;
|
|
196
|
+
var Key = /* @__PURE__ */ function(Key2) {
|
|
197
|
+
Key2["Ed25519"] = "Ed25519";
|
|
198
|
+
Key2["Secp256k1"] = "Secp256k1";
|
|
199
|
+
Key2["Secp256r1"] = "Secp256r1";
|
|
200
|
+
return Key2;
|
|
201
|
+
}({});
|
|
202
|
+
var JwkKeyUse = /* @__PURE__ */ function(JwkKeyUse2) {
|
|
203
|
+
JwkKeyUse2["Encryption"] = "enc";
|
|
204
|
+
JwkKeyUse2["Signature"] = "sig";
|
|
205
|
+
return JwkKeyUse2;
|
|
206
|
+
}({});
|
|
207
|
+
var SIG_KEY_ALGS = [
|
|
208
|
+
"ES256",
|
|
209
|
+
"ES384",
|
|
210
|
+
"ES512",
|
|
211
|
+
"EdDSA",
|
|
212
|
+
"ES256K",
|
|
213
|
+
"Ed25519",
|
|
214
|
+
"Secp256k1",
|
|
215
|
+
"Secp256r1",
|
|
216
|
+
"Bls12381G1",
|
|
217
|
+
"Bls12381G2"
|
|
218
|
+
];
|
|
219
|
+
var ENC_KEY_ALGS = [
|
|
220
|
+
"X25519",
|
|
221
|
+
"ECDH_ES_A256KW",
|
|
222
|
+
"RSA_OAEP_256"
|
|
223
|
+
];
|
|
224
|
+
|
|
225
|
+
// src/functions.ts
|
|
226
|
+
var { fromString: fromString2, toString: toString2 } = u8a2;
|
|
227
|
+
var logger = Loggers.DEFAULT.get("sphereon:key-utils");
|
|
228
|
+
var getKms = /* @__PURE__ */ __name(async (context, kms) => {
|
|
229
|
+
if (kms) {
|
|
230
|
+
return kms;
|
|
231
|
+
}
|
|
232
|
+
if (!context.agent.availableMethods().includes("keyManagerGetDefaultKeyManagementSystem")) {
|
|
233
|
+
throw Error("Cannot determine default KMS if not provided and a non Sphereon Key Manager is being used");
|
|
234
|
+
}
|
|
235
|
+
return context.agent.keyManagerGetDefaultKeyManagementSystem();
|
|
236
|
+
}, "getKms");
|
|
237
|
+
var generatePrivateKeyHex = /* @__PURE__ */ __name(async (type) => {
|
|
238
|
+
switch (type) {
|
|
239
|
+
case "Ed25519": {
|
|
240
|
+
const keyPairEd25519 = generateSigningKeyPair();
|
|
241
|
+
return toString2(keyPairEd25519.secretKey, "base16");
|
|
242
|
+
}
|
|
243
|
+
// The Secp256 types use the same method to generate the key
|
|
244
|
+
case "Secp256r1":
|
|
245
|
+
case "Secp256k1": {
|
|
246
|
+
const privateBytes = randomBytes(32);
|
|
247
|
+
return toString2(privateBytes, "base16");
|
|
248
|
+
}
|
|
249
|
+
case "RSA": {
|
|
250
|
+
const pem = await generateRSAKeyAsPEM("RSA-PSS", "SHA-256", 2048);
|
|
251
|
+
return privateKeyHexFromPEM(pem);
|
|
252
|
+
}
|
|
253
|
+
default:
|
|
254
|
+
throw Error(`not_supported: Key type ${type} not yet supported for this did:jwk implementation`);
|
|
255
|
+
}
|
|
256
|
+
}, "generatePrivateKeyHex");
|
|
257
|
+
var keyMetaAlgorithmsFromKeyType = /* @__PURE__ */ __name((type) => {
|
|
258
|
+
switch (type) {
|
|
259
|
+
case "Ed25519":
|
|
260
|
+
return [
|
|
261
|
+
"Ed25519",
|
|
262
|
+
"EdDSA"
|
|
263
|
+
];
|
|
264
|
+
case "ES256K":
|
|
265
|
+
case "Secp256k1":
|
|
266
|
+
return [
|
|
267
|
+
"ES256K",
|
|
268
|
+
"ES256K-R",
|
|
269
|
+
"eth_signTransaction",
|
|
270
|
+
"eth_signTypedData",
|
|
271
|
+
"eth_signMessage",
|
|
272
|
+
"eth_rawSign"
|
|
273
|
+
];
|
|
274
|
+
case "Secp256r1":
|
|
275
|
+
return [
|
|
276
|
+
"ES256"
|
|
277
|
+
];
|
|
278
|
+
case "X25519":
|
|
279
|
+
return [
|
|
280
|
+
"ECDH",
|
|
281
|
+
"ECDH-ES",
|
|
282
|
+
"ECDH-1PU"
|
|
283
|
+
];
|
|
284
|
+
case "RSA":
|
|
285
|
+
return [
|
|
286
|
+
"RS256",
|
|
287
|
+
"RS512",
|
|
288
|
+
"PS256",
|
|
289
|
+
"PS512"
|
|
290
|
+
];
|
|
291
|
+
}
|
|
292
|
+
return [
|
|
293
|
+
type
|
|
294
|
+
];
|
|
295
|
+
}, "keyMetaAlgorithmsFromKeyType");
|
|
296
|
+
async function importProvidedOrGeneratedKey(args, context) {
|
|
297
|
+
const type = args.options?.type ?? args.options?.key?.type ?? args.options?.keyType ?? "Secp256r1";
|
|
298
|
+
const key = args?.options?.key;
|
|
299
|
+
if (args.options?.x509 && key) {
|
|
300
|
+
key.meta = {
|
|
301
|
+
...key.meta,
|
|
302
|
+
x509: {
|
|
303
|
+
...args.options.x509,
|
|
304
|
+
...key.meta?.x509
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
if (args.options && args.options?.use === JwkKeyUse.Encryption && !ENC_KEY_ALGS.includes(type)) {
|
|
309
|
+
throw new Error(`${type} keys are not valid for encryption`);
|
|
310
|
+
}
|
|
311
|
+
let privateKeyHex = void 0;
|
|
312
|
+
if (key) {
|
|
313
|
+
privateKeyHex = key.privateKeyHex ?? key.meta?.x509?.privateKeyHex;
|
|
314
|
+
if ((!privateKeyHex || privateKeyHex.trim() === "") && key?.meta?.x509?.privateKeyPEM) {
|
|
315
|
+
privateKeyHex = privateKeyHexFromPEM(key.meta.x509.privateKeyPEM);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
if (privateKeyHex) {
|
|
319
|
+
return context.agent.keyManagerImport({
|
|
320
|
+
...key,
|
|
321
|
+
kms: args.kms,
|
|
322
|
+
type,
|
|
323
|
+
privateKeyHex
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
return context.agent.keyManagerCreate({
|
|
327
|
+
type,
|
|
328
|
+
kms: args.kms,
|
|
329
|
+
meta: {
|
|
330
|
+
...key?.meta,
|
|
331
|
+
algorithms: keyMetaAlgorithmsFromKeyType(type),
|
|
332
|
+
keyAlias: args.alias
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
__name(importProvidedOrGeneratedKey, "importProvidedOrGeneratedKey");
|
|
337
|
+
var calculateJwkThumbprintForKey = /* @__PURE__ */ __name((args) => {
|
|
338
|
+
const { key } = args;
|
|
339
|
+
const jwk = key.publicKeyHex ? toJwk(key.publicKeyHex, key.type, {
|
|
340
|
+
key,
|
|
341
|
+
isPrivateKey: false
|
|
342
|
+
}) : "privateKeyHex" in key && key.privateKeyHex ? toJwk(key.privateKeyHex, key.type, {
|
|
343
|
+
isPrivateKey: true
|
|
344
|
+
}) : void 0;
|
|
345
|
+
if (!jwk) {
|
|
346
|
+
throw Error(`Could not determine jwk from key ${key.kid}`);
|
|
347
|
+
}
|
|
348
|
+
return calculateJwkThumbprint({
|
|
349
|
+
jwk,
|
|
350
|
+
digestAlgorithm: args.digestAlgorithm
|
|
351
|
+
});
|
|
352
|
+
}, "calculateJwkThumbprintForKey");
|
|
353
|
+
var assertJwkClaimPresent = /* @__PURE__ */ __name((value, description) => {
|
|
354
|
+
if (typeof value !== "string" || !value) {
|
|
355
|
+
throw new Error(`${description} missing or invalid`);
|
|
356
|
+
}
|
|
357
|
+
}, "assertJwkClaimPresent");
|
|
358
|
+
var toBase64url = /* @__PURE__ */ __name((input) => toString2(fromString2(input), "base64url"), "toBase64url");
|
|
359
|
+
var calculateJwkThumbprint = /* @__PURE__ */ __name((args) => {
|
|
360
|
+
const { digestAlgorithm = "sha256" } = args;
|
|
361
|
+
const jwk = sanitizedJwk(args.jwk);
|
|
362
|
+
let components;
|
|
363
|
+
switch (jwk.kty) {
|
|
364
|
+
case "EC":
|
|
365
|
+
assertJwkClaimPresent(jwk.crv, '"crv" (Curve) Parameter');
|
|
366
|
+
assertJwkClaimPresent(jwk.x, '"x" (X Coordinate) Parameter');
|
|
367
|
+
assertJwkClaimPresent(jwk.y, '"y" (Y Coordinate) Parameter');
|
|
368
|
+
components = {
|
|
369
|
+
crv: jwk.crv,
|
|
370
|
+
kty: jwk.kty,
|
|
371
|
+
x: jwk.x,
|
|
372
|
+
y: jwk.y
|
|
373
|
+
};
|
|
374
|
+
break;
|
|
375
|
+
case "OKP":
|
|
376
|
+
assertJwkClaimPresent(jwk.crv, '"crv" (Subtype of Key Pair) Parameter');
|
|
377
|
+
assertJwkClaimPresent(jwk.x, '"x" (Public Key) Parameter');
|
|
378
|
+
components = {
|
|
379
|
+
crv: jwk.crv,
|
|
380
|
+
kty: jwk.kty,
|
|
381
|
+
x: jwk.x
|
|
382
|
+
};
|
|
383
|
+
break;
|
|
384
|
+
case "RSA":
|
|
385
|
+
assertJwkClaimPresent(jwk.e, '"e" (Exponent) Parameter');
|
|
386
|
+
assertJwkClaimPresent(jwk.n, '"n" (Modulus) Parameter');
|
|
387
|
+
components = {
|
|
388
|
+
e: jwk.e,
|
|
389
|
+
kty: jwk.kty,
|
|
390
|
+
n: jwk.n
|
|
391
|
+
};
|
|
392
|
+
break;
|
|
393
|
+
case "oct":
|
|
394
|
+
assertJwkClaimPresent(jwk.k, '"k" (Key Value) Parameter');
|
|
395
|
+
components = {
|
|
396
|
+
k: jwk.k,
|
|
397
|
+
kty: jwk.kty
|
|
398
|
+
};
|
|
399
|
+
break;
|
|
400
|
+
default:
|
|
401
|
+
throw new Error('"kty" (Key Type) Parameter missing or unsupported');
|
|
402
|
+
}
|
|
403
|
+
const data = JSON.stringify(components);
|
|
404
|
+
return digestAlgorithm === "sha512" ? digestMethodParams("SHA-512").digestMethod(data, "base64url") : digestMethodParams("SHA-256").digestMethod(data, "base64url");
|
|
405
|
+
}, "calculateJwkThumbprint");
|
|
406
|
+
var toJwkFromKey = /* @__PURE__ */ __name((key, opts) => {
|
|
407
|
+
const isPrivateKey = "privateKeyHex" in key;
|
|
408
|
+
return toJwk(key.publicKeyHex, key.type, {
|
|
409
|
+
...opts,
|
|
410
|
+
key,
|
|
411
|
+
isPrivateKey
|
|
412
|
+
});
|
|
413
|
+
}, "toJwkFromKey");
|
|
414
|
+
var toJwk = /* @__PURE__ */ __name((publicKeyHex, type, opts) => {
|
|
415
|
+
const { key, noKidThumbprint = false } = opts ?? {};
|
|
416
|
+
if (key && key.publicKeyHex !== publicKeyHex && opts?.isPrivateKey !== true) {
|
|
417
|
+
throw Error(`Provided key with id ${key.kid}, has a different public key hex ${key.publicKeyHex} than supplied public key ${publicKeyHex}`);
|
|
418
|
+
}
|
|
419
|
+
let jwk;
|
|
420
|
+
switch (type) {
|
|
421
|
+
case "Ed25519":
|
|
422
|
+
jwk = toEd25519OrX25519Jwk(publicKeyHex, {
|
|
423
|
+
...opts,
|
|
424
|
+
crv: JoseCurve.Ed25519
|
|
425
|
+
});
|
|
426
|
+
break;
|
|
427
|
+
case "X25519":
|
|
428
|
+
jwk = toEd25519OrX25519Jwk(publicKeyHex, {
|
|
429
|
+
...opts,
|
|
430
|
+
crv: JoseCurve.X25519
|
|
431
|
+
});
|
|
432
|
+
break;
|
|
433
|
+
case "Secp256k1":
|
|
434
|
+
jwk = toSecp256k1Jwk(publicKeyHex, opts);
|
|
435
|
+
break;
|
|
436
|
+
case "Secp256r1":
|
|
437
|
+
jwk = toSecp256r1Jwk(publicKeyHex, opts);
|
|
438
|
+
break;
|
|
439
|
+
case "RSA":
|
|
440
|
+
jwk = toRSAJwk(publicKeyHex, opts);
|
|
441
|
+
break;
|
|
442
|
+
default:
|
|
443
|
+
throw new Error(`not_supported: Key type ${type} not yet supported for this did:jwk implementation`);
|
|
444
|
+
}
|
|
445
|
+
if (!jwk.kid && !noKidThumbprint) {
|
|
446
|
+
jwk["kid"] = calculateJwkThumbprint({
|
|
447
|
+
jwk
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
return sanitizedJwk(jwk);
|
|
451
|
+
}, "toJwk");
|
|
452
|
+
var jwkToRawHexKey = /* @__PURE__ */ __name(async (jwk) => {
|
|
453
|
+
jwk = sanitizedJwk(jwk);
|
|
454
|
+
if (jwk.kty === "RSA") {
|
|
455
|
+
return rsaJwkToRawHexKey(jwk);
|
|
456
|
+
} else if (jwk.kty === "EC") {
|
|
457
|
+
return ecJwkToRawHexKey(jwk);
|
|
458
|
+
} else if (jwk.kty === "OKP") {
|
|
459
|
+
return okpJwkToRawHexKey(jwk);
|
|
460
|
+
} else if (jwk.kty === "oct") {
|
|
461
|
+
return octJwkToRawHexKey(jwk);
|
|
462
|
+
} else {
|
|
463
|
+
throw new Error(`Unsupported key type: ${jwk.kty}`);
|
|
464
|
+
}
|
|
465
|
+
}, "jwkToRawHexKey");
|
|
466
|
+
function rsaJwkToRawHexKey(jwk) {
|
|
467
|
+
function encodeInteger(bytes) {
|
|
468
|
+
if (bytes[0] & 128) {
|
|
469
|
+
bytes = Uint8Array.from([
|
|
470
|
+
0,
|
|
471
|
+
...bytes
|
|
472
|
+
]);
|
|
473
|
+
}
|
|
474
|
+
const len = encodeLength(bytes.length);
|
|
475
|
+
return Uint8Array.from([
|
|
476
|
+
2,
|
|
477
|
+
...len,
|
|
478
|
+
...bytes
|
|
479
|
+
]);
|
|
480
|
+
}
|
|
481
|
+
__name(encodeInteger, "encodeInteger");
|
|
482
|
+
function encodeLength(len) {
|
|
483
|
+
if (len < 128) {
|
|
484
|
+
return Uint8Array.of(len);
|
|
485
|
+
}
|
|
486
|
+
let hex = len.toString(16);
|
|
487
|
+
if (hex.length % 2 === 1) {
|
|
488
|
+
hex = "0" + hex;
|
|
489
|
+
}
|
|
490
|
+
const lenBytes = Uint8Array.from(hex.match(/.{2}/g).map((h) => parseInt(h, 16)));
|
|
491
|
+
return Uint8Array.of(128 | lenBytes.length, ...lenBytes);
|
|
492
|
+
}
|
|
493
|
+
__name(encodeLength, "encodeLength");
|
|
494
|
+
function encodeSequence(elements) {
|
|
495
|
+
const content = elements.reduce((acc, elm) => Uint8Array.from([
|
|
496
|
+
...acc,
|
|
497
|
+
...elm
|
|
498
|
+
]), new Uint8Array());
|
|
499
|
+
const len = encodeLength(content.length);
|
|
500
|
+
return Uint8Array.from([
|
|
501
|
+
48,
|
|
502
|
+
...len,
|
|
503
|
+
...content
|
|
504
|
+
]);
|
|
505
|
+
}
|
|
506
|
+
__name(encodeSequence, "encodeSequence");
|
|
507
|
+
function base64UrlToBytes(b64url) {
|
|
508
|
+
return fromString2(b64url, "base64url");
|
|
509
|
+
}
|
|
510
|
+
__name(base64UrlToBytes, "base64UrlToBytes");
|
|
511
|
+
jwk = sanitizedJwk(jwk);
|
|
512
|
+
if (!jwk.n || !jwk.e) {
|
|
513
|
+
throw new Error("RSA JWK must contain 'n' and 'e' properties.");
|
|
514
|
+
}
|
|
515
|
+
const modulusBytes = base64UrlToBytes(jwk.n);
|
|
516
|
+
const exponentBytes = base64UrlToBytes(jwk.e);
|
|
517
|
+
const sequence = encodeSequence([
|
|
518
|
+
encodeInteger(modulusBytes),
|
|
519
|
+
encodeInteger(exponentBytes)
|
|
520
|
+
]);
|
|
521
|
+
const result = toString2(sequence, "hex");
|
|
522
|
+
return result;
|
|
523
|
+
}
|
|
524
|
+
__name(rsaJwkToRawHexKey, "rsaJwkToRawHexKey");
|
|
525
|
+
function ecJwkToRawHexKey(jwk) {
|
|
526
|
+
jwk = sanitizedJwk(jwk);
|
|
527
|
+
if (!jwk.x || !jwk.y) {
|
|
528
|
+
throw new Error("EC JWK must contain 'x' and 'y' properties.");
|
|
529
|
+
}
|
|
530
|
+
const x = fromString2(jwk.x.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""), "base64url");
|
|
531
|
+
const y = fromString2(jwk.y.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""), "base64url");
|
|
532
|
+
return "04" + toString2(x, "hex") + toString2(y, "hex");
|
|
533
|
+
}
|
|
534
|
+
__name(ecJwkToRawHexKey, "ecJwkToRawHexKey");
|
|
535
|
+
function okpJwkToRawHexKey(jwk) {
|
|
536
|
+
jwk = sanitizedJwk(jwk);
|
|
537
|
+
if (!jwk.x) {
|
|
538
|
+
throw new Error("OKP JWK must contain 'x' property.");
|
|
539
|
+
}
|
|
540
|
+
const x = fromString2(jwk.x.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""), "base64url");
|
|
541
|
+
return toString2(x, "hex");
|
|
542
|
+
}
|
|
543
|
+
__name(okpJwkToRawHexKey, "okpJwkToRawHexKey");
|
|
544
|
+
function octJwkToRawHexKey(jwk) {
|
|
545
|
+
jwk = sanitizedJwk(jwk);
|
|
546
|
+
if (!jwk.k) {
|
|
547
|
+
throw new Error("Octet JWK must contain 'k' property.");
|
|
548
|
+
}
|
|
549
|
+
const key = fromString2(jwk.k.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""), "base64url");
|
|
550
|
+
return toString2(key, "hex");
|
|
551
|
+
}
|
|
552
|
+
__name(octJwkToRawHexKey, "octJwkToRawHexKey");
|
|
553
|
+
var jwkDetermineUse = /* @__PURE__ */ __name((type, suppliedUse) => {
|
|
554
|
+
return suppliedUse ? suppliedUse : SIG_KEY_ALGS.includes(type) ? JwkKeyUse.Signature : ENC_KEY_ALGS.includes(type) ? JwkKeyUse.Encryption : void 0;
|
|
555
|
+
}, "jwkDetermineUse");
|
|
556
|
+
var assertProperKeyLength = /* @__PURE__ */ __name((keyHex, expectedKeyLength) => {
|
|
557
|
+
if (Array.isArray(expectedKeyLength)) {
|
|
558
|
+
if (!expectedKeyLength.includes(keyHex.length)) {
|
|
559
|
+
throw Error(`Invalid key length. Needs to be a hex string with length from ${JSON.stringify(expectedKeyLength)} instead of ${keyHex.length}. Input: ${keyHex}`);
|
|
560
|
+
}
|
|
561
|
+
} else if (keyHex.length !== expectedKeyLength) {
|
|
562
|
+
throw Error(`Invalid key length. Needs to be a hex string with length ${expectedKeyLength} instead of ${keyHex.length}. Input: ${keyHex}`);
|
|
563
|
+
}
|
|
564
|
+
}, "assertProperKeyLength");
|
|
565
|
+
var toSecp256k1Jwk = /* @__PURE__ */ __name((keyHex, opts) => {
|
|
566
|
+
const { use } = opts ?? {};
|
|
567
|
+
logger.debug(`toSecp256k1Jwk keyHex: ${keyHex}, length: ${keyHex.length}`);
|
|
568
|
+
if (opts?.isPrivateKey) {
|
|
569
|
+
assertProperKeyLength(keyHex, [
|
|
570
|
+
64
|
|
571
|
+
]);
|
|
572
|
+
} else {
|
|
573
|
+
assertProperKeyLength(keyHex, [
|
|
574
|
+
66,
|
|
575
|
+
130
|
|
576
|
+
]);
|
|
577
|
+
}
|
|
578
|
+
const secp256k12 = new elliptic.ec("secp256k1");
|
|
579
|
+
const keyBytes = fromString2(keyHex, "base16");
|
|
580
|
+
const keyPair = opts?.isPrivateKey ? secp256k12.keyFromPrivate(keyBytes) : secp256k12.keyFromPublic(keyBytes);
|
|
581
|
+
const pubPoint = keyPair.getPublic();
|
|
582
|
+
return sanitizedJwk({
|
|
583
|
+
alg: JoseSignatureAlgorithm.ES256K,
|
|
584
|
+
...use !== void 0 && {
|
|
585
|
+
use
|
|
586
|
+
},
|
|
587
|
+
kty: JwkKeyType.EC,
|
|
588
|
+
crv: JoseCurve.secp256k1,
|
|
589
|
+
x: hexToBase64(pubPoint.getX().toString("hex"), "base64url"),
|
|
590
|
+
y: hexToBase64(pubPoint.getY().toString("hex"), "base64url"),
|
|
591
|
+
...opts?.isPrivateKey && {
|
|
592
|
+
d: hexToBase64(keyPair.getPrivate("hex"), "base64url")
|
|
593
|
+
}
|
|
594
|
+
});
|
|
595
|
+
}, "toSecp256k1Jwk");
|
|
596
|
+
var toSecp256r1Jwk = /* @__PURE__ */ __name((keyHex, opts) => {
|
|
597
|
+
const { use } = opts ?? {};
|
|
598
|
+
logger.debug(`toSecp256r1Jwk keyHex: ${keyHex}, length: ${keyHex.length}`);
|
|
599
|
+
if (opts?.isPrivateKey) {
|
|
600
|
+
assertProperKeyLength(keyHex, [
|
|
601
|
+
64
|
|
602
|
+
]);
|
|
603
|
+
} else {
|
|
604
|
+
assertProperKeyLength(keyHex, [
|
|
605
|
+
66,
|
|
606
|
+
130
|
|
607
|
+
]);
|
|
608
|
+
}
|
|
609
|
+
const secp256r1 = new elliptic.ec("p256");
|
|
610
|
+
const keyBytes = fromString2(keyHex, "base16");
|
|
611
|
+
logger.debug(`keyBytes length: ${keyBytes}`);
|
|
612
|
+
const keyPair = opts?.isPrivateKey ? secp256r1.keyFromPrivate(keyBytes) : secp256r1.keyFromPublic(keyBytes);
|
|
613
|
+
const pubPoint = keyPair.getPublic();
|
|
614
|
+
return sanitizedJwk({
|
|
615
|
+
alg: JoseSignatureAlgorithm.ES256,
|
|
616
|
+
...use !== void 0 && {
|
|
617
|
+
use
|
|
618
|
+
},
|
|
619
|
+
kty: JwkKeyType.EC,
|
|
620
|
+
crv: JoseCurve.P_256,
|
|
621
|
+
x: hexToBase64(pubPoint.getX().toString("hex"), "base64url"),
|
|
622
|
+
y: hexToBase64(pubPoint.getY().toString("hex"), "base64url"),
|
|
623
|
+
...opts?.isPrivateKey && {
|
|
624
|
+
d: hexToBase64(keyPair.getPrivate("hex"), "base64url")
|
|
625
|
+
}
|
|
626
|
+
});
|
|
627
|
+
}, "toSecp256r1Jwk");
|
|
628
|
+
var toEd25519OrX25519Jwk = /* @__PURE__ */ __name((publicKeyHex, opts) => {
|
|
629
|
+
assertProperKeyLength(publicKeyHex, 64);
|
|
630
|
+
const { use } = opts ?? {};
|
|
631
|
+
return sanitizedJwk({
|
|
632
|
+
alg: JoseSignatureAlgorithm.EdDSA,
|
|
633
|
+
...use !== void 0 && {
|
|
634
|
+
use
|
|
635
|
+
},
|
|
636
|
+
kty: JwkKeyType.OKP,
|
|
637
|
+
crv: opts?.crv ?? JoseCurve.Ed25519,
|
|
638
|
+
x: hexToBase64(publicKeyHex, "base64url")
|
|
639
|
+
});
|
|
640
|
+
}, "toEd25519OrX25519Jwk");
|
|
641
|
+
var toRSAJwk = /* @__PURE__ */ __name((publicKeyHex, opts) => {
|
|
642
|
+
function parseDerIntegers(pubKeyHex) {
|
|
643
|
+
const bytes = Buffer.from(pubKeyHex, "hex");
|
|
644
|
+
let offset = 0;
|
|
645
|
+
if (bytes[offset++] !== 48) throw new Error("Not a SEQUENCE");
|
|
646
|
+
let len = bytes[offset++];
|
|
647
|
+
if (len & 128) {
|
|
648
|
+
const nBytes = len & 127;
|
|
649
|
+
len = 0;
|
|
650
|
+
for (let i = 0; i < nBytes; i++) {
|
|
651
|
+
len = (len << 8) + bytes[offset++];
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
if (bytes[offset] !== 2) {
|
|
655
|
+
if (bytes[offset++] !== 48) throw new Error("Expected alg-ID SEQUENCE");
|
|
656
|
+
let algLen = bytes[offset++];
|
|
657
|
+
if (algLen & 128) {
|
|
658
|
+
const nB = algLen & 127;
|
|
659
|
+
algLen = 0;
|
|
660
|
+
for (let i = 0; i < nB; i++) algLen = (algLen << 8) + bytes[offset++];
|
|
661
|
+
}
|
|
662
|
+
offset += algLen;
|
|
663
|
+
if (bytes[offset++] !== 3) throw new Error("Expected BIT STRING");
|
|
664
|
+
let bitLen = bytes[offset++];
|
|
665
|
+
if (bitLen & 128) {
|
|
666
|
+
const nB = bitLen & 127;
|
|
667
|
+
bitLen = 0;
|
|
668
|
+
for (let i = 0; i < nB; i++) bitLen = (bitLen << 8) + bytes[offset++];
|
|
669
|
+
}
|
|
670
|
+
offset += 1;
|
|
671
|
+
if (bytes[offset++] !== 48) throw new Error("Expected inner SEQUENCE");
|
|
672
|
+
let innerLen = bytes[offset++];
|
|
673
|
+
if (innerLen & 128) {
|
|
674
|
+
const nB = innerLen & 127;
|
|
675
|
+
innerLen = 0;
|
|
676
|
+
for (let i = 0; i < nB; i++) innerLen = (innerLen << 8) + bytes[offset++];
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
if (bytes[offset++] !== 2) throw new Error("Expected INTEGER for modulus");
|
|
680
|
+
let modLen = bytes[offset++];
|
|
681
|
+
if (modLen & 128) {
|
|
682
|
+
const nB = modLen & 127;
|
|
683
|
+
modLen = 0;
|
|
684
|
+
for (let i = 0; i < nB; i++) modLen = (modLen << 8) + bytes[offset++];
|
|
685
|
+
}
|
|
686
|
+
let modulusBytes = bytes.slice(offset, offset + modLen);
|
|
687
|
+
offset += modLen;
|
|
688
|
+
if (modulusBytes[0] === 0) {
|
|
689
|
+
modulusBytes = modulusBytes.slice(1);
|
|
690
|
+
}
|
|
691
|
+
if (bytes[offset++] !== 2) throw new Error("Expected INTEGER for exponent");
|
|
692
|
+
let expLen = bytes[offset++];
|
|
693
|
+
if (expLen & 128) {
|
|
694
|
+
const nB = expLen & 127;
|
|
695
|
+
expLen = 0;
|
|
696
|
+
for (let i = 0; i < nB; i++) expLen = (expLen << 8) + bytes[offset++];
|
|
697
|
+
}
|
|
698
|
+
const exponentBytes = bytes.slice(offset, offset + expLen);
|
|
699
|
+
return {
|
|
700
|
+
modulus: modulusBytes.toString("hex"),
|
|
701
|
+
exponent: exponentBytes.toString("hex")
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
__name(parseDerIntegers, "parseDerIntegers");
|
|
705
|
+
const meta = opts?.key?.meta;
|
|
706
|
+
if (meta?.publicKeyJwk || meta?.publicKeyPEM) {
|
|
707
|
+
if (meta?.publicKeyJwk) {
|
|
708
|
+
return meta.publicKeyJwk;
|
|
709
|
+
}
|
|
710
|
+
const publicKeyPEM = meta?.publicKeyPEM ?? hexToPEM(publicKeyHex, "public");
|
|
711
|
+
const jwk = PEMToJwk(publicKeyPEM, "public");
|
|
712
|
+
return jwk;
|
|
713
|
+
}
|
|
714
|
+
const { modulus, exponent } = parseDerIntegers(publicKeyHex);
|
|
715
|
+
const sanitized = sanitizedJwk({
|
|
716
|
+
kty: "RSA",
|
|
717
|
+
n: hexToBase64(modulus, "base64url"),
|
|
718
|
+
e: hexToBase64(exponent, "base64url")
|
|
719
|
+
});
|
|
720
|
+
return sanitized;
|
|
721
|
+
}, "toRSAJwk");
|
|
722
|
+
var padLeft = /* @__PURE__ */ __name((args) => {
|
|
723
|
+
const { data } = args;
|
|
724
|
+
const size = args.size ?? 32;
|
|
725
|
+
const padString = args.padString ?? "0";
|
|
726
|
+
if (data.length >= size) {
|
|
727
|
+
return data;
|
|
728
|
+
}
|
|
729
|
+
if (padString && padString.length === 0) {
|
|
730
|
+
throw Error(`Pad string needs to have at least a length of 1`);
|
|
731
|
+
}
|
|
732
|
+
const length = padString.length;
|
|
733
|
+
return padString.repeat((size - data.length) / length) + data;
|
|
734
|
+
}, "padLeft");
|
|
735
|
+
var OID = {
|
|
736
|
+
[0]: new Uint8Array([
|
|
737
|
+
6,
|
|
738
|
+
7,
|
|
739
|
+
42,
|
|
740
|
+
134,
|
|
741
|
+
72,
|
|
742
|
+
206,
|
|
743
|
+
61,
|
|
744
|
+
2,
|
|
745
|
+
1
|
|
746
|
+
]),
|
|
747
|
+
[1]: new Uint8Array([
|
|
748
|
+
6,
|
|
749
|
+
8,
|
|
750
|
+
42,
|
|
751
|
+
134,
|
|
752
|
+
72,
|
|
753
|
+
206,
|
|
754
|
+
61,
|
|
755
|
+
3,
|
|
756
|
+
1,
|
|
757
|
+
7
|
|
758
|
+
]),
|
|
759
|
+
[2]: new Uint8Array([
|
|
760
|
+
6,
|
|
761
|
+
3,
|
|
762
|
+
43,
|
|
763
|
+
101,
|
|
764
|
+
112
|
|
765
|
+
])
|
|
766
|
+
};
|
|
767
|
+
var compareUint8Arrays = /* @__PURE__ */ __name((a, b) => {
|
|
768
|
+
if (a.length !== b.length) {
|
|
769
|
+
return false;
|
|
770
|
+
}
|
|
771
|
+
for (let i = 0; i < a.length; i++) {
|
|
772
|
+
if (a[i] !== b[i]) {
|
|
773
|
+
return false;
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
return true;
|
|
777
|
+
}, "compareUint8Arrays");
|
|
778
|
+
var findSubarray = /* @__PURE__ */ __name((haystack, needle) => {
|
|
779
|
+
for (let i = 0; i <= haystack.length - needle.length; i++) {
|
|
780
|
+
if (compareUint8Arrays(haystack.subarray(i, i + needle.length), needle)) {
|
|
781
|
+
return i;
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
return -1;
|
|
785
|
+
}, "findSubarray");
|
|
786
|
+
var getTargetOID = /* @__PURE__ */ __name((keyType) => {
|
|
787
|
+
switch (keyType) {
|
|
788
|
+
case "Secp256k1":
|
|
789
|
+
return OID[0];
|
|
790
|
+
case "Secp256r1":
|
|
791
|
+
return OID[1];
|
|
792
|
+
case "Ed25519":
|
|
793
|
+
return OID[2];
|
|
794
|
+
default:
|
|
795
|
+
throw new Error(`Unsupported key type: ${keyType}`);
|
|
796
|
+
}
|
|
797
|
+
}, "getTargetOID");
|
|
798
|
+
var isAsn1Der = /* @__PURE__ */ __name((key) => key[0] === 48, "isAsn1Der");
|
|
799
|
+
var asn1DerToRawPublicKey = /* @__PURE__ */ __name((derKey, keyType) => {
|
|
800
|
+
if (!isAsn1Der(derKey)) {
|
|
801
|
+
throw new Error("Invalid DER encoding: Expected to start with sequence tag");
|
|
802
|
+
}
|
|
803
|
+
let index = 2;
|
|
804
|
+
if (derKey[1] & 128) {
|
|
805
|
+
const lengthBytesCount = derKey[1] & 127;
|
|
806
|
+
index += lengthBytesCount;
|
|
807
|
+
}
|
|
808
|
+
const targetOid = getTargetOID(keyType);
|
|
809
|
+
const oidIndex = findSubarray(derKey, targetOid);
|
|
810
|
+
if (oidIndex === -1) {
|
|
811
|
+
throw new Error(`OID for ${keyType} not found in DER encoding`);
|
|
812
|
+
}
|
|
813
|
+
index = oidIndex + targetOid.length;
|
|
814
|
+
while (index < derKey.length && derKey[index] !== 3) {
|
|
815
|
+
index++;
|
|
816
|
+
}
|
|
817
|
+
if (index >= derKey.length) {
|
|
818
|
+
throw new Error("Invalid DER encoding: Bit string not found");
|
|
819
|
+
}
|
|
820
|
+
index += 2;
|
|
821
|
+
index++;
|
|
822
|
+
return derKey.slice(index);
|
|
823
|
+
}, "asn1DerToRawPublicKey");
|
|
824
|
+
var isRawCompressedPublicKey = /* @__PURE__ */ __name((key) => key.length === 33 && (key[0] === 2 || key[0] === 3), "isRawCompressedPublicKey");
|
|
825
|
+
var toRawCompressedHexPublicKey = /* @__PURE__ */ __name((rawPublicKey, keyType) => {
|
|
826
|
+
if (isRawCompressedPublicKey(rawPublicKey)) {
|
|
827
|
+
return hexStringFromUint8Array(rawPublicKey);
|
|
828
|
+
}
|
|
829
|
+
if (keyType === "Secp256k1" || keyType === "Secp256r1") {
|
|
830
|
+
if (rawPublicKey[0] === 4 && rawPublicKey.length === 65) {
|
|
831
|
+
const xCoordinate = rawPublicKey.slice(1, 33);
|
|
832
|
+
const yCoordinate = rawPublicKey.slice(33);
|
|
833
|
+
const prefix = new Uint8Array([
|
|
834
|
+
yCoordinate[31] % 2 === 0 ? 2 : 3
|
|
835
|
+
]);
|
|
836
|
+
const resultKey = hexStringFromUint8Array(new Uint8Array([
|
|
837
|
+
...prefix,
|
|
838
|
+
...xCoordinate
|
|
839
|
+
]));
|
|
840
|
+
logger.debug(`converted public key ${hexStringFromUint8Array(rawPublicKey)} to ${resultKey}`);
|
|
841
|
+
return resultKey;
|
|
842
|
+
}
|
|
843
|
+
return toString2(rawPublicKey, "base16");
|
|
844
|
+
} else if (keyType === "Ed25519") {
|
|
845
|
+
return toString2(rawPublicKey, "base16");
|
|
846
|
+
}
|
|
847
|
+
throw new Error(`Unsupported key type: ${keyType}`);
|
|
848
|
+
}, "toRawCompressedHexPublicKey");
|
|
849
|
+
var hexStringFromUint8Array = /* @__PURE__ */ __name((value) => toString2(value, "base16"), "hexStringFromUint8Array");
|
|
850
|
+
var signatureAlgorithmFromKey = /* @__PURE__ */ __name(async (args) => {
|
|
851
|
+
const { key } = args;
|
|
852
|
+
return signatureAlgorithmFromKeyType({
|
|
853
|
+
type: key.type
|
|
854
|
+
});
|
|
855
|
+
}, "signatureAlgorithmFromKey");
|
|
856
|
+
var signatureAlgorithmFromKeyType = /* @__PURE__ */ __name((args) => {
|
|
857
|
+
const { type } = args;
|
|
858
|
+
switch (type) {
|
|
859
|
+
case "Ed25519":
|
|
860
|
+
case "X25519":
|
|
861
|
+
return JoseSignatureAlgorithm.EdDSA;
|
|
862
|
+
case "Secp256r1":
|
|
863
|
+
return JoseSignatureAlgorithm.ES256;
|
|
864
|
+
case "Secp384r1":
|
|
865
|
+
return JoseSignatureAlgorithm.ES384;
|
|
866
|
+
case "Secp521r1":
|
|
867
|
+
return JoseSignatureAlgorithm.ES512;
|
|
868
|
+
case "Secp256k1":
|
|
869
|
+
return JoseSignatureAlgorithm.ES256K;
|
|
870
|
+
case "RSA":
|
|
871
|
+
return JoseSignatureAlgorithm.PS256;
|
|
872
|
+
default:
|
|
873
|
+
throw new Error(`Key type '${type}' not supported`);
|
|
874
|
+
}
|
|
875
|
+
}, "signatureAlgorithmFromKeyType");
|
|
876
|
+
var keyTypeFromCryptographicSuite = /* @__PURE__ */ __name((args) => {
|
|
877
|
+
const { crv, kty, alg } = args;
|
|
878
|
+
switch (alg) {
|
|
879
|
+
case "RSASSA-PSS":
|
|
880
|
+
case "RS256":
|
|
881
|
+
case "RS384":
|
|
882
|
+
case "RS512":
|
|
883
|
+
case "PS256":
|
|
884
|
+
case "PS384":
|
|
885
|
+
case "PS512":
|
|
886
|
+
return "RSA";
|
|
887
|
+
}
|
|
888
|
+
switch (crv) {
|
|
889
|
+
case "EdDSA":
|
|
890
|
+
case "Ed25519":
|
|
891
|
+
case "Ed25519Signature2018":
|
|
892
|
+
case "Ed25519Signature2020":
|
|
893
|
+
case "JcsEd25519Signature2020":
|
|
894
|
+
return "Ed25519";
|
|
895
|
+
case "JsonWebSignature2020":
|
|
896
|
+
case "ES256":
|
|
897
|
+
case "ECDSA":
|
|
898
|
+
case "P-256":
|
|
899
|
+
return "Secp256r1";
|
|
900
|
+
case "ES384":
|
|
901
|
+
case "P-384":
|
|
902
|
+
return "Secp384r1";
|
|
903
|
+
case "ES512":
|
|
904
|
+
case "P-521":
|
|
905
|
+
return "Secp521r1";
|
|
906
|
+
case "EcdsaSecp256k1Signature2019":
|
|
907
|
+
case "secp256k1":
|
|
908
|
+
case "ES256K":
|
|
909
|
+
case "EcdsaSecp256k1VerificationKey2019":
|
|
910
|
+
case "EcdsaSecp256k1RecoveryMethod2020":
|
|
911
|
+
return "Secp256k1";
|
|
912
|
+
}
|
|
913
|
+
if (kty) {
|
|
914
|
+
return kty;
|
|
915
|
+
}
|
|
916
|
+
throw new Error(`Cryptographic suite '${crv}' not supported`);
|
|
917
|
+
}, "keyTypeFromCryptographicSuite");
|
|
918
|
+
function removeNulls(obj) {
|
|
919
|
+
Object.keys(obj).forEach((key) => {
|
|
920
|
+
if (obj[key] && typeof obj[key] === "object") removeNulls(obj[key]);
|
|
921
|
+
else if (obj[key] == null) delete obj[key];
|
|
922
|
+
});
|
|
923
|
+
return obj;
|
|
924
|
+
}
|
|
925
|
+
__name(removeNulls, "removeNulls");
|
|
926
|
+
var globalCrypto = /* @__PURE__ */ __name((setGlobal, suppliedCrypto) => {
|
|
927
|
+
let webcrypto;
|
|
928
|
+
if (typeof suppliedCrypto !== "undefined") {
|
|
929
|
+
webcrypto = suppliedCrypto;
|
|
930
|
+
} else if (typeof crypto !== "undefined") {
|
|
931
|
+
webcrypto = crypto;
|
|
932
|
+
} else if (typeof global.crypto !== "undefined") {
|
|
933
|
+
webcrypto = global.crypto;
|
|
934
|
+
} else {
|
|
935
|
+
if (typeof global.window?.crypto?.subtle !== "undefined") {
|
|
936
|
+
webcrypto = global.window.crypto;
|
|
937
|
+
} else {
|
|
938
|
+
webcrypto = import("crypto");
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
if (setGlobal) {
|
|
942
|
+
global.crypto = webcrypto;
|
|
943
|
+
}
|
|
944
|
+
return webcrypto;
|
|
945
|
+
}, "globalCrypto");
|
|
946
|
+
var sanitizedJwk = /* @__PURE__ */ __name((input) => {
|
|
947
|
+
const inputJwk = typeof input["toJsonDTO"] === "function" ? input["toJsonDTO"]() : {
|
|
948
|
+
...input
|
|
949
|
+
};
|
|
950
|
+
const jwk = {
|
|
951
|
+
...inputJwk,
|
|
952
|
+
...inputJwk.x && {
|
|
953
|
+
x: base64ToBase64Url(inputJwk.x)
|
|
954
|
+
},
|
|
955
|
+
...inputJwk.y && {
|
|
956
|
+
y: base64ToBase64Url(inputJwk.y)
|
|
957
|
+
},
|
|
958
|
+
...inputJwk.d && {
|
|
959
|
+
d: base64ToBase64Url(inputJwk.d)
|
|
960
|
+
},
|
|
961
|
+
...inputJwk.n && {
|
|
962
|
+
n: base64ToBase64Url(inputJwk.n)
|
|
963
|
+
},
|
|
964
|
+
...inputJwk.e && {
|
|
965
|
+
e: base64ToBase64Url(inputJwk.e)
|
|
966
|
+
},
|
|
967
|
+
...inputJwk.k && {
|
|
968
|
+
k: base64ToBase64Url(inputJwk.k)
|
|
969
|
+
}
|
|
970
|
+
};
|
|
971
|
+
return removeNulls(jwk);
|
|
972
|
+
}, "sanitizedJwk");
|
|
973
|
+
var base64ToBase64Url = /* @__PURE__ */ __name((input) => {
|
|
974
|
+
return input.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
975
|
+
}, "base64ToBase64Url");
|
|
976
|
+
async function verifyRawSignature({ data, signature, key: inputKey, opts }) {
|
|
977
|
+
function jwkPropertyToBigInt(jwkProp) {
|
|
978
|
+
const byteArray = fromString2(jwkProp, "base64url");
|
|
979
|
+
const hex = toString2(byteArray, "hex");
|
|
980
|
+
return BigInt(`0x${hex}`);
|
|
981
|
+
}
|
|
982
|
+
__name(jwkPropertyToBigInt, "jwkPropertyToBigInt");
|
|
983
|
+
try {
|
|
984
|
+
debug(`verifyRawSignature for: ${inputKey}`);
|
|
985
|
+
const jwk = sanitizedJwk(inputKey);
|
|
986
|
+
validateJwk(jwk, {
|
|
987
|
+
crvOptional: true
|
|
988
|
+
});
|
|
989
|
+
const keyType = keyTypeFromCryptographicSuite({
|
|
990
|
+
crv: jwk.crv,
|
|
991
|
+
kty: jwk.kty,
|
|
992
|
+
alg: jwk.alg
|
|
993
|
+
});
|
|
994
|
+
const publicKeyHex = await jwkToRawHexKey(jwk);
|
|
995
|
+
switch (keyType) {
|
|
996
|
+
case "Secp256k1":
|
|
997
|
+
return secp256k1.verify(signature, data, publicKeyHex, {
|
|
998
|
+
format: "compact",
|
|
999
|
+
prehash: true
|
|
1000
|
+
});
|
|
1001
|
+
case "Secp256r1":
|
|
1002
|
+
return p256.verify(signature, data, publicKeyHex, {
|
|
1003
|
+
format: "compact",
|
|
1004
|
+
prehash: true
|
|
1005
|
+
});
|
|
1006
|
+
case "Secp384r1":
|
|
1007
|
+
return p384.verify(signature, data, publicKeyHex, {
|
|
1008
|
+
format: "compact",
|
|
1009
|
+
prehash: true
|
|
1010
|
+
});
|
|
1011
|
+
case "Secp521r1":
|
|
1012
|
+
return p521.verify(signature, data, publicKeyHex, {
|
|
1013
|
+
format: "compact",
|
|
1014
|
+
prehash: true
|
|
1015
|
+
});
|
|
1016
|
+
case "Ed25519":
|
|
1017
|
+
return ed25519.verify(signature, data, fromString2(publicKeyHex, "hex"));
|
|
1018
|
+
case "Bls12381G1":
|
|
1019
|
+
case "Bls12381G2":
|
|
1020
|
+
return bls12_381.verify(signature, data, fromString2(publicKeyHex, "hex"));
|
|
1021
|
+
case "RSA": {
|
|
1022
|
+
const signatureAlgorithm = opts?.signatureAlg ?? jwk.alg ?? JoseSignatureAlgorithm.PS256;
|
|
1023
|
+
const hashAlg = signatureAlgorithm === (JoseSignatureAlgorithm.RS512 || JoseSignatureAlgorithm.PS512) ? sha5122 : signatureAlgorithm === (JoseSignatureAlgorithm.RS384 || JoseSignatureAlgorithm.PS384) ? sha3842 : sha2562;
|
|
1024
|
+
switch (signatureAlgorithm) {
|
|
1025
|
+
case JoseSignatureAlgorithm.RS256:
|
|
1026
|
+
return rsa.PKCS1_SHA256.verify({
|
|
1027
|
+
n: jwkPropertyToBigInt(jwk.n),
|
|
1028
|
+
e: jwkPropertyToBigInt(jwk.e)
|
|
1029
|
+
}, data, signature);
|
|
1030
|
+
case JoseSignatureAlgorithm.RS384:
|
|
1031
|
+
return rsa.PKCS1_SHA384.verify({
|
|
1032
|
+
n: jwkPropertyToBigInt(jwk.n),
|
|
1033
|
+
e: jwkPropertyToBigInt(jwk.e)
|
|
1034
|
+
}, data, signature);
|
|
1035
|
+
case JoseSignatureAlgorithm.RS512:
|
|
1036
|
+
return rsa.PKCS1_SHA512.verify({
|
|
1037
|
+
n: jwkPropertyToBigInt(jwk.n),
|
|
1038
|
+
e: jwkPropertyToBigInt(jwk.e)
|
|
1039
|
+
}, data, signature);
|
|
1040
|
+
case JoseSignatureAlgorithm.PS256:
|
|
1041
|
+
case JoseSignatureAlgorithm.PS384:
|
|
1042
|
+
case JoseSignatureAlgorithm.PS512:
|
|
1043
|
+
if (typeof crypto !== "undefined" && typeof crypto.subtle !== "undefined") {
|
|
1044
|
+
const key = await cryptoSubtleImportRSAKey(jwk, "RSA-PSS");
|
|
1045
|
+
const saltLength = signatureAlgorithm === JoseSignatureAlgorithm.PS256 ? 32 : signatureAlgorithm === JoseSignatureAlgorithm.PS384 ? 48 : 64;
|
|
1046
|
+
return crypto.subtle.verify({
|
|
1047
|
+
name: "rsa-pss",
|
|
1048
|
+
hash: hashAlg,
|
|
1049
|
+
saltLength
|
|
1050
|
+
}, key, signature, data);
|
|
1051
|
+
}
|
|
1052
|
+
console.warn(`Using fallback for RSA-PSS verify signature, which is known to be flaky!!`);
|
|
1053
|
+
return rsa.PSS(hashAlg, rsa.mgf1(hashAlg)).verify({
|
|
1054
|
+
n: jwkPropertyToBigInt(jwk.n),
|
|
1055
|
+
e: jwkPropertyToBigInt(jwk.e)
|
|
1056
|
+
}, data, signature);
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
throw Error(`Unsupported key type for signature validation: ${keyType}`);
|
|
1061
|
+
} catch (error) {
|
|
1062
|
+
logger.error(`Error: ${error}`);
|
|
1063
|
+
throw error;
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
__name(verifyRawSignature, "verifyRawSignature");
|
|
1067
|
+
function readLength(bytes, offset) {
|
|
1068
|
+
const first = bytes[offset];
|
|
1069
|
+
if (first < 128) {
|
|
1070
|
+
return {
|
|
1071
|
+
length: first,
|
|
1072
|
+
lengthBytes: 1
|
|
1073
|
+
};
|
|
1074
|
+
}
|
|
1075
|
+
const numBytes = first & 127;
|
|
1076
|
+
let length = 0;
|
|
1077
|
+
for (let i = 0; i < numBytes; i++) {
|
|
1078
|
+
length = length << 8 | bytes[offset + 1 + i];
|
|
1079
|
+
}
|
|
1080
|
+
return {
|
|
1081
|
+
length,
|
|
1082
|
+
lengthBytes: 1 + numBytes
|
|
1083
|
+
};
|
|
1084
|
+
}
|
|
1085
|
+
__name(readLength, "readLength");
|
|
1086
|
+
function toPkcs1(derBytes) {
|
|
1087
|
+
if (derBytes[0] !== 48) {
|
|
1088
|
+
throw new Error("Invalid DER: expected SEQUENCE");
|
|
1089
|
+
}
|
|
1090
|
+
const { lengthBytes: outerLenBytes } = readLength(derBytes, 1);
|
|
1091
|
+
const outerHeaderLen = 1 + outerLenBytes;
|
|
1092
|
+
const innerTag = derBytes[outerHeaderLen];
|
|
1093
|
+
if (innerTag === 2) {
|
|
1094
|
+
return derBytes;
|
|
1095
|
+
}
|
|
1096
|
+
if (innerTag !== 48) {
|
|
1097
|
+
throw new Error("Unexpected DER tag, not PKCS#1 or SPKI");
|
|
1098
|
+
}
|
|
1099
|
+
const { length: algLen, lengthBytes: algLenBytes } = readLength(derBytes, outerHeaderLen + 1);
|
|
1100
|
+
const algHeaderLen = 1 + algLenBytes;
|
|
1101
|
+
const algIdEnd = outerHeaderLen + algHeaderLen + algLen;
|
|
1102
|
+
if (derBytes[algIdEnd] !== 3) {
|
|
1103
|
+
throw new Error("Expected BIT STRING after algId");
|
|
1104
|
+
}
|
|
1105
|
+
const { length: bitStrLen, lengthBytes: bitStrLenBytes } = readLength(derBytes, algIdEnd + 1);
|
|
1106
|
+
const bitStrHeaderLen = 1 + bitStrLenBytes;
|
|
1107
|
+
const bitStrStart = algIdEnd + bitStrHeaderLen;
|
|
1108
|
+
const unusedBits = derBytes[bitStrStart];
|
|
1109
|
+
if (unusedBits !== 0) {
|
|
1110
|
+
throw new Error(`Unexpected unused bits: ${unusedBits}`);
|
|
1111
|
+
}
|
|
1112
|
+
const pkcs1Start = bitStrStart + 1;
|
|
1113
|
+
const pkcs1Len = bitStrLen - 1;
|
|
1114
|
+
return derBytes.slice(pkcs1Start, pkcs1Start + pkcs1Len);
|
|
1115
|
+
}
|
|
1116
|
+
__name(toPkcs1, "toPkcs1");
|
|
1117
|
+
function toPkcs1FromHex(publicKeyHex) {
|
|
1118
|
+
const pkcs1 = toPkcs1(fromString2(publicKeyHex, "hex"));
|
|
1119
|
+
return toString2(pkcs1, "hex");
|
|
1120
|
+
}
|
|
1121
|
+
__name(toPkcs1FromHex, "toPkcs1FromHex");
|
|
1122
|
+
|
|
1123
|
+
// src/conversion.ts
|
|
1124
|
+
import { ICoseCurve, ICoseKeyOperation, ICoseKeyType, ICoseSignatureAlgorithm, JoseCurve as JoseCurve2, JoseKeyOperation, JoseSignatureAlgorithm as JoseSignatureAlgorithm2, JwkKeyType as JwkKeyType2 } from "@sphereon/ssi-types";
|
|
1125
|
+
function coseKeyToJwk(coseKey) {
|
|
1126
|
+
const { x5chain, key_ops, crv, alg, baseIV, kty, ...rest } = coseKey;
|
|
1127
|
+
return removeNulls({
|
|
1128
|
+
...rest,
|
|
1129
|
+
kty: coseToJoseKty(kty),
|
|
1130
|
+
...crv && {
|
|
1131
|
+
crv: coseToJoseCurve(crv)
|
|
1132
|
+
},
|
|
1133
|
+
...key_ops && {
|
|
1134
|
+
key_ops: key_ops.map(coseToJoseKeyOperation)
|
|
1135
|
+
},
|
|
1136
|
+
...alg && {
|
|
1137
|
+
alg: coseToJoseSignatureAlg(alg)
|
|
1138
|
+
},
|
|
1139
|
+
...baseIV && {
|
|
1140
|
+
iv: baseIV
|
|
1141
|
+
},
|
|
1142
|
+
...x5chain && {
|
|
1143
|
+
x5c: x5chain
|
|
1144
|
+
}
|
|
1145
|
+
});
|
|
1146
|
+
}
|
|
1147
|
+
__name(coseKeyToJwk, "coseKeyToJwk");
|
|
1148
|
+
function jwkToCoseKey(jwk) {
|
|
1149
|
+
const { x5c, key_ops, crv, alg, iv, kty, ...rest } = jwk;
|
|
1150
|
+
return removeNulls({
|
|
1151
|
+
...rest,
|
|
1152
|
+
kty: joseToCoseKty(kty),
|
|
1153
|
+
...crv && {
|
|
1154
|
+
crv: joseToCoseCurve(crv)
|
|
1155
|
+
},
|
|
1156
|
+
...key_ops && {
|
|
1157
|
+
key_ops: key_ops.map(joseToCoseKeyOperation)
|
|
1158
|
+
},
|
|
1159
|
+
...alg && {
|
|
1160
|
+
alg: joseToCoseSignatureAlg(alg)
|
|
1161
|
+
},
|
|
1162
|
+
...iv && {
|
|
1163
|
+
baseIV: iv
|
|
1164
|
+
},
|
|
1165
|
+
...x5c && {
|
|
1166
|
+
x5chain: x5c
|
|
1167
|
+
}
|
|
1168
|
+
});
|
|
1169
|
+
}
|
|
1170
|
+
__name(jwkToCoseKey, "jwkToCoseKey");
|
|
1171
|
+
function coseToJoseKty(kty) {
|
|
1172
|
+
switch (kty) {
|
|
1173
|
+
case ICoseKeyType.EC2:
|
|
1174
|
+
return JwkKeyType2.EC;
|
|
1175
|
+
case ICoseKeyType.RSA:
|
|
1176
|
+
return JwkKeyType2.RSA;
|
|
1177
|
+
case ICoseKeyType.Symmetric:
|
|
1178
|
+
return JwkKeyType2.oct;
|
|
1179
|
+
case ICoseKeyType.OKP:
|
|
1180
|
+
return JwkKeyType2.OKP;
|
|
1181
|
+
default:
|
|
1182
|
+
throw Error(`Key type ${kty} not supported in JWA`);
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
__name(coseToJoseKty, "coseToJoseKty");
|
|
1186
|
+
function joseToCoseKty(kty) {
|
|
1187
|
+
switch (kty) {
|
|
1188
|
+
case "EC":
|
|
1189
|
+
return ICoseKeyType.EC2;
|
|
1190
|
+
case "RSA":
|
|
1191
|
+
return ICoseKeyType.RSA;
|
|
1192
|
+
case "oct":
|
|
1193
|
+
return ICoseKeyType.Symmetric;
|
|
1194
|
+
case "OKP":
|
|
1195
|
+
return ICoseKeyType.OKP;
|
|
1196
|
+
default:
|
|
1197
|
+
throw Error(`Key type ${kty} not supported in Cose`);
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
__name(joseToCoseKty, "joseToCoseKty");
|
|
1201
|
+
function coseToJoseSignatureAlg(coseAlg) {
|
|
1202
|
+
switch (coseAlg) {
|
|
1203
|
+
case ICoseSignatureAlgorithm.ES256K:
|
|
1204
|
+
return JoseSignatureAlgorithm2.ES256K;
|
|
1205
|
+
case ICoseSignatureAlgorithm.ES256:
|
|
1206
|
+
return JoseSignatureAlgorithm2.ES256;
|
|
1207
|
+
case ICoseSignatureAlgorithm.ES384:
|
|
1208
|
+
return JoseSignatureAlgorithm2.ES384;
|
|
1209
|
+
case ICoseSignatureAlgorithm.ES512:
|
|
1210
|
+
return JoseSignatureAlgorithm2.ES512;
|
|
1211
|
+
case ICoseSignatureAlgorithm.PS256:
|
|
1212
|
+
return JoseSignatureAlgorithm2.PS256;
|
|
1213
|
+
case ICoseSignatureAlgorithm.PS384:
|
|
1214
|
+
return JoseSignatureAlgorithm2.PS384;
|
|
1215
|
+
case ICoseSignatureAlgorithm.PS512:
|
|
1216
|
+
return JoseSignatureAlgorithm2.PS512;
|
|
1217
|
+
case ICoseSignatureAlgorithm.HS256:
|
|
1218
|
+
return JoseSignatureAlgorithm2.HS256;
|
|
1219
|
+
case ICoseSignatureAlgorithm.HS384:
|
|
1220
|
+
return JoseSignatureAlgorithm2.HS384;
|
|
1221
|
+
case ICoseSignatureAlgorithm.HS512:
|
|
1222
|
+
return JoseSignatureAlgorithm2.HS512;
|
|
1223
|
+
case ICoseSignatureAlgorithm.EdDSA:
|
|
1224
|
+
return JoseSignatureAlgorithm2.EdDSA;
|
|
1225
|
+
default:
|
|
1226
|
+
throw Error(`Signature algorithm ${coseAlg} not supported in Jose`);
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
__name(coseToJoseSignatureAlg, "coseToJoseSignatureAlg");
|
|
1230
|
+
function joseToCoseSignatureAlg(joseAlg) {
|
|
1231
|
+
switch (joseAlg) {
|
|
1232
|
+
case (JoseSignatureAlgorithm2.ES256K, "ES256K"):
|
|
1233
|
+
return ICoseSignatureAlgorithm.ES256K;
|
|
1234
|
+
case (JoseSignatureAlgorithm2.ES256, "ES256"):
|
|
1235
|
+
return ICoseSignatureAlgorithm.ES256;
|
|
1236
|
+
case (JoseSignatureAlgorithm2.ES384, "ES384"):
|
|
1237
|
+
return ICoseSignatureAlgorithm.ES384;
|
|
1238
|
+
case (JoseSignatureAlgorithm2.ES512, "ES512"):
|
|
1239
|
+
return ICoseSignatureAlgorithm.ES512;
|
|
1240
|
+
case (JoseSignatureAlgorithm2.PS256, "PS256"):
|
|
1241
|
+
return ICoseSignatureAlgorithm.PS256;
|
|
1242
|
+
case (JoseSignatureAlgorithm2.PS384, "PS384"):
|
|
1243
|
+
return ICoseSignatureAlgorithm.PS384;
|
|
1244
|
+
case (JoseSignatureAlgorithm2.PS512, "PS512"):
|
|
1245
|
+
return ICoseSignatureAlgorithm.PS512;
|
|
1246
|
+
case (JoseSignatureAlgorithm2.HS256, "HS256"):
|
|
1247
|
+
return ICoseSignatureAlgorithm.HS256;
|
|
1248
|
+
case (JoseSignatureAlgorithm2.HS384, "HS384"):
|
|
1249
|
+
return ICoseSignatureAlgorithm.HS384;
|
|
1250
|
+
case (JoseSignatureAlgorithm2.HS512, "HS512"):
|
|
1251
|
+
return ICoseSignatureAlgorithm.HS512;
|
|
1252
|
+
case (JoseSignatureAlgorithm2.EdDSA, "EdDSA"):
|
|
1253
|
+
return ICoseSignatureAlgorithm.EdDSA;
|
|
1254
|
+
default:
|
|
1255
|
+
throw Error(`Signature algorithm ${joseAlg} not supported in Cose`);
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
__name(joseToCoseSignatureAlg, "joseToCoseSignatureAlg");
|
|
1259
|
+
function joseToCoseKeyOperation(keyOp) {
|
|
1260
|
+
switch (keyOp) {
|
|
1261
|
+
case (JoseKeyOperation.SIGN, "sign"):
|
|
1262
|
+
return ICoseKeyOperation.SIGN;
|
|
1263
|
+
case (JoseKeyOperation.VERIFY, "verify"):
|
|
1264
|
+
return ICoseKeyOperation.VERIFY;
|
|
1265
|
+
case (JoseKeyOperation.ENCRYPT, "encrypt"):
|
|
1266
|
+
return ICoseKeyOperation.ENCRYPT;
|
|
1267
|
+
case (JoseKeyOperation.DECRYPT, "decrypt"):
|
|
1268
|
+
return ICoseKeyOperation.DECRYPT;
|
|
1269
|
+
case (JoseKeyOperation.WRAP_KEY, "wrapKey"):
|
|
1270
|
+
return ICoseKeyOperation.WRAP_KEY;
|
|
1271
|
+
case (JoseKeyOperation.UNWRAP_KEY, "unwrapKey"):
|
|
1272
|
+
return ICoseKeyOperation.UNWRAP_KEY;
|
|
1273
|
+
case (JoseKeyOperation.DERIVE_KEY, "deriveKey"):
|
|
1274
|
+
return ICoseKeyOperation.DERIVE_KEY;
|
|
1275
|
+
case (JoseKeyOperation.DERIVE_BITS, "deriveBits"):
|
|
1276
|
+
return ICoseKeyOperation.DERIVE_BITS;
|
|
1277
|
+
default:
|
|
1278
|
+
throw Error(`Key operation ${keyOp} not supported in Cose`);
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
__name(joseToCoseKeyOperation, "joseToCoseKeyOperation");
|
|
1282
|
+
function coseToJoseKeyOperation(keyOp) {
|
|
1283
|
+
switch (keyOp) {
|
|
1284
|
+
case ICoseKeyOperation.SIGN:
|
|
1285
|
+
return JoseKeyOperation.SIGN;
|
|
1286
|
+
case ICoseKeyOperation.VERIFY:
|
|
1287
|
+
return JoseKeyOperation.VERIFY;
|
|
1288
|
+
case ICoseKeyOperation.ENCRYPT:
|
|
1289
|
+
return JoseKeyOperation.ENCRYPT;
|
|
1290
|
+
case ICoseKeyOperation.DECRYPT:
|
|
1291
|
+
return JoseKeyOperation.DECRYPT;
|
|
1292
|
+
case ICoseKeyOperation.WRAP_KEY:
|
|
1293
|
+
return JoseKeyOperation.WRAP_KEY;
|
|
1294
|
+
case ICoseKeyOperation.UNWRAP_KEY:
|
|
1295
|
+
return JoseKeyOperation.UNWRAP_KEY;
|
|
1296
|
+
case ICoseKeyOperation.DERIVE_KEY:
|
|
1297
|
+
return JoseKeyOperation.DERIVE_KEY;
|
|
1298
|
+
case ICoseKeyOperation.DERIVE_BITS:
|
|
1299
|
+
return JoseKeyOperation.DERIVE_BITS;
|
|
1300
|
+
default:
|
|
1301
|
+
throw Error(`Key operation ${keyOp} not supported in Jose`);
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
__name(coseToJoseKeyOperation, "coseToJoseKeyOperation");
|
|
1305
|
+
function joseToCoseCurve(curve) {
|
|
1306
|
+
switch (curve) {
|
|
1307
|
+
case (JoseCurve2.P_256, "P-256"):
|
|
1308
|
+
return ICoseCurve.P_256;
|
|
1309
|
+
case (JoseCurve2.P_384, "P-384"):
|
|
1310
|
+
return ICoseCurve.P_384;
|
|
1311
|
+
case (JoseCurve2.P_521, "P-521"):
|
|
1312
|
+
return ICoseCurve.P_521;
|
|
1313
|
+
case (JoseCurve2.X25519, "X25519"):
|
|
1314
|
+
return ICoseCurve.X25519;
|
|
1315
|
+
case (JoseCurve2.X448, "X448"):
|
|
1316
|
+
return ICoseCurve.X448;
|
|
1317
|
+
case (JoseCurve2.Ed25519, "Ed25519"):
|
|
1318
|
+
return ICoseCurve.Ed25519;
|
|
1319
|
+
case (JoseCurve2.Ed448, "Ed448"):
|
|
1320
|
+
return ICoseCurve.Ed448;
|
|
1321
|
+
case (JoseCurve2.secp256k1, "secp256k1"):
|
|
1322
|
+
return ICoseCurve.secp256k1;
|
|
1323
|
+
default:
|
|
1324
|
+
throw Error(`Curve ${curve} not supported in Cose`);
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
__name(joseToCoseCurve, "joseToCoseCurve");
|
|
1328
|
+
function coseToJoseCurve(curve) {
|
|
1329
|
+
switch (curve) {
|
|
1330
|
+
case ICoseCurve.P_256:
|
|
1331
|
+
return JoseCurve2.P_256;
|
|
1332
|
+
case ICoseCurve.P_384:
|
|
1333
|
+
return JoseCurve2.P_384;
|
|
1334
|
+
case ICoseCurve.P_521:
|
|
1335
|
+
return JoseCurve2.P_521;
|
|
1336
|
+
case ICoseCurve.X25519:
|
|
1337
|
+
return JoseCurve2.X25519;
|
|
1338
|
+
case ICoseCurve.X448:
|
|
1339
|
+
return JoseCurve2.X448;
|
|
1340
|
+
case ICoseCurve.Ed25519:
|
|
1341
|
+
return JoseCurve2.Ed25519;
|
|
1342
|
+
case ICoseCurve.Ed448:
|
|
1343
|
+
return JoseCurve2.Ed448;
|
|
1344
|
+
case ICoseCurve.secp256k1:
|
|
1345
|
+
return JoseCurve2.secp256k1;
|
|
1346
|
+
default:
|
|
1347
|
+
throw Error(`Curve ${curve} not supported in Jose`);
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
__name(coseToJoseCurve, "coseToJoseCurve");
|
|
1351
|
+
export {
|
|
1352
|
+
ENC_KEY_ALGS,
|
|
1353
|
+
JWK_JCS_PUB_NAME,
|
|
1354
|
+
JWK_JCS_PUB_PREFIX,
|
|
1355
|
+
JwkKeyUse,
|
|
1356
|
+
Key,
|
|
1357
|
+
SIG_KEY_ALGS,
|
|
1358
|
+
asn1DerToRawPublicKey,
|
|
1359
|
+
calculateJwkThumbprint,
|
|
1360
|
+
calculateJwkThumbprintForKey,
|
|
1361
|
+
coseKeyToJwk,
|
|
1362
|
+
coseToJoseCurve,
|
|
1363
|
+
coseToJoseKeyOperation,
|
|
1364
|
+
coseToJoseKty,
|
|
1365
|
+
coseToJoseSignatureAlg,
|
|
1366
|
+
digestMethodParams,
|
|
1367
|
+
generatePrivateKeyHex,
|
|
1368
|
+
getKms,
|
|
1369
|
+
globalCrypto,
|
|
1370
|
+
hexStringFromUint8Array,
|
|
1371
|
+
importProvidedOrGeneratedKey,
|
|
1372
|
+
isAsn1Der,
|
|
1373
|
+
isRawCompressedPublicKey,
|
|
1374
|
+
jcsCanonicalize,
|
|
1375
|
+
joseToCoseCurve,
|
|
1376
|
+
joseToCoseKeyOperation,
|
|
1377
|
+
joseToCoseKty,
|
|
1378
|
+
joseToCoseSignatureAlg,
|
|
1379
|
+
jwkDetermineUse,
|
|
1380
|
+
jwkJcsDecode,
|
|
1381
|
+
jwkJcsEncode,
|
|
1382
|
+
jwkToCoseKey,
|
|
1383
|
+
jwkToRawHexKey,
|
|
1384
|
+
keyTypeFromCryptographicSuite,
|
|
1385
|
+
logger,
|
|
1386
|
+
minimalJwk,
|
|
1387
|
+
padLeft,
|
|
1388
|
+
removeNulls,
|
|
1389
|
+
rsaJwkToRawHexKey,
|
|
1390
|
+
sanitizedJwk,
|
|
1391
|
+
shaHasher,
|
|
1392
|
+
signatureAlgorithmFromKey,
|
|
1393
|
+
signatureAlgorithmFromKeyType,
|
|
1394
|
+
toBase64url,
|
|
1395
|
+
toJwk,
|
|
1396
|
+
toJwkFromKey,
|
|
1397
|
+
toPkcs1,
|
|
1398
|
+
toPkcs1FromHex,
|
|
1399
|
+
toRawCompressedHexPublicKey,
|
|
1400
|
+
validateJwk,
|
|
1401
|
+
verifyRawSignature
|
|
15
1402
|
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
/**
|
|
18
|
-
* Provides `did:jwk` {@link @veramo/did-provider-jwk#JwkDIDProvider | identifier provider }
|
|
19
|
-
* for the {@link @veramo/did-manager#DIDManager}
|
|
20
|
-
*
|
|
21
|
-
* @packageDocumentation
|
|
22
|
-
*/
|
|
23
|
-
__exportStar(require("./functions"), exports);
|
|
24
|
-
__exportStar(require("./conversion"), exports);
|
|
25
|
-
__exportStar(require("./jwk-jcs"), exports);
|
|
26
|
-
__exportStar(require("./types"), exports);
|
|
27
|
-
__exportStar(require("./digest-methods"), exports);
|
|
28
1403
|
//# sourceMappingURL=index.js.map
|