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