@sphereon/ssi-sdk-ext.kms-local 0.28.1-feature.esm.cjs.9 → 0.28.1-feature.oyd.cmsm.improv.16
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.d.cts → SphereonKeyManagementSystem.d.ts} +3 -22
- package/dist/SphereonKeyManagementSystem.d.ts.map +1 -0
- package/dist/SphereonKeyManagementSystem.js +290 -0
- package/dist/SphereonKeyManagementSystem.js.map +1 -0
- package/dist/index.d.ts +6 -39
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -264
- package/dist/index.js.map +1 -1
- package/dist/ssi-sdk-ext.kms-local.d.ts +2 -1
- package/dist/tsdoc-metadata.json +1 -1
- package/package.json +13 -26
- package/src/SphereonKeyManagementSystem.ts +3 -4
- package/src/__tests__/key-management-system.test.ts +2 -3
- package/src/__tests__/rsa.test.ts +4 -5
- package/dist/index.cjs +0 -267
- package/dist/index.cjs.map +0 -1
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { MinimalImportableKey, ManagedKeyInfo, TKeyType, IKey, KeyMetadata } from '@veramo/core';
|
|
1
|
+
import { IKey, ManagedKeyInfo, MinimalImportableKey, TKeyType } from '@veramo/core';
|
|
3
2
|
import { AbstractPrivateKeyStore } from '@veramo/key-manager';
|
|
4
3
|
import { KeyManagementSystem } from '@veramo/kms-local';
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
declare class SphereonKeyManagementSystem extends KeyManagementSystem {
|
|
4
|
+
export declare class SphereonKeyManagementSystem extends KeyManagementSystem {
|
|
8
5
|
private readonly privateKeyStore;
|
|
9
6
|
constructor(keyStore: AbstractPrivateKeyStore);
|
|
10
7
|
importKey(args: Omit<MinimalImportableKey, 'kms'> & {
|
|
@@ -33,20 +30,4 @@ declare class SphereonKeyManagementSystem extends KeyManagementSystem {
|
|
|
33
30
|
private verifyRSA;
|
|
34
31
|
listKeys(): Promise<Array<ManagedKeyInfo>>;
|
|
35
32
|
}
|
|
36
|
-
|
|
37
|
-
interface ManagedKeyInfoArgs {
|
|
38
|
-
alias?: string;
|
|
39
|
-
type: TKeyType;
|
|
40
|
-
privateKeyHex: string;
|
|
41
|
-
publicKeyHex?: string;
|
|
42
|
-
meta?: ManageKeyInfoMeta | undefined | null;
|
|
43
|
-
}
|
|
44
|
-
interface ManageKeyInfoMeta extends KeyMetadata {
|
|
45
|
-
x509?: X509Opts;
|
|
46
|
-
[x: string]: any;
|
|
47
|
-
}
|
|
48
|
-
declare enum KeyType {
|
|
49
|
-
Bls12381G2 = "Bls12381G2"
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export { KeyType, type ManageKeyInfoMeta, type ManagedKeyInfoArgs, SphereonKeyManagementSystem };
|
|
33
|
+
//# sourceMappingURL=SphereonKeyManagementSystem.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SphereonKeyManagementSystem.d.ts","sourceRoot":"","sources":["../src/SphereonKeyManagementSystem.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,oBAAoB,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AACnF,OAAO,EAAE,uBAAuB,EAAqB,MAAM,qBAAqB,CAAA;AAChF,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AAiBvD,qBAAa,2BAA4B,SAAQ,mBAAmB;IAClE,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAyB;gBAE7C,QAAQ,EAAE,uBAAuB;IAKvC,SAAS,CAAC,IAAI,EAAE,IAAI,CAAC,oBAAoB,EAAE,KAAK,CAAC,GAAG;QAAE,aAAa,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,cAAc,CAAC;IAkCxG,SAAS,CAAC,EAAE,IAAI,EAAE,EAAE;QAAE,IAAI,EAAE,QAAQ,CAAA;KAAE,GAAG,OAAO,CAAC,cAAc,CAAC;IAuChE,IAAI,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,EAAE;QAAE,MAAM,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QAAC,SAAS,CAAC,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,UAAU,CAAA;KAAE,GAAG,OAAO,CAAC,MAAM,CAAC;IAsCvH,MAAM,CAAC,EACX,YAAY,EACZ,IAAI,EACJ,SAAS,EACT,IAAI,EACJ,SAAS,GACV,EAAE;QACD,YAAY,EAAE,MAAM,CAAA;QACpB,IAAI,EAAE,QAAQ,CAAA;QACd,SAAS,CAAC,EAAE,MAAM,CAAA;QAClB,IAAI,EAAE,UAAU,CAAA;QAChB,SAAS,EAAE,MAAM,CAAA;KAClB,GAAG,OAAO,CAAC,OAAO,CAAC;IAOpB,OAAO,CAAC,wBAAwB;IAuGhC;;OAEG;YACW,OAAO;YAOP,SAAS;IAMV,QAAQ,IAAI,OAAO,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC;CAGxD"}
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.SphereonKeyManagementSystem = void 0;
|
|
39
|
+
const ssi_sdk_ext_key_utils_1 = require("@sphereon/ssi-sdk-ext.key-utils");
|
|
40
|
+
const kms_local_1 = require("@veramo/kms-local");
|
|
41
|
+
const debug_1 = __importDefault(require("debug"));
|
|
42
|
+
const elliptic_1 = __importDefault(require("elliptic"));
|
|
43
|
+
const u8a = __importStar(require("uint8arrays"));
|
|
44
|
+
const index_1 = require("./index");
|
|
45
|
+
const ssi_sdk_ext_x509_utils_1 = require("@sphereon/ssi-sdk-ext.x509-utils");
|
|
46
|
+
const debug = (0, debug_1.default)('sphereon:kms:local');
|
|
47
|
+
class SphereonKeyManagementSystem extends kms_local_1.KeyManagementSystem {
|
|
48
|
+
constructor(keyStore) {
|
|
49
|
+
super(keyStore);
|
|
50
|
+
this.privateKeyStore = keyStore;
|
|
51
|
+
}
|
|
52
|
+
importKey(args) {
|
|
53
|
+
const _super = Object.create(null, {
|
|
54
|
+
importKey: { get: () => super.importKey }
|
|
55
|
+
});
|
|
56
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
+
switch (args.type) {
|
|
58
|
+
case index_1.KeyType.Bls12381G2.toString():
|
|
59
|
+
if (!args.privateKeyHex || !args.publicKeyHex) {
|
|
60
|
+
throw new Error('invalid_argument: type, publicKeyHex and privateKeyHex are required to import a key');
|
|
61
|
+
}
|
|
62
|
+
const managedKey = this.asSphereonManagedKeyInfo(Object.assign(Object.assign({}, args), { alias: args.kid, privateKeyHex: args.privateKeyHex, publicKeyHex: args.publicKeyHex, type: args.type }));
|
|
63
|
+
yield this.privateKeyStore.import(Object.assign({ alias: managedKey.kid }, args));
|
|
64
|
+
debug('imported key', managedKey.type, managedKey.publicKeyHex);
|
|
65
|
+
return managedKey;
|
|
66
|
+
case 'Secp256k1':
|
|
67
|
+
case 'Secp256r1':
|
|
68
|
+
// @ts-ignore
|
|
69
|
+
case 'RSA': {
|
|
70
|
+
if (!args.privateKeyHex && !args.privateKeyPEM) {
|
|
71
|
+
throw new Error('invalid_argument: type and privateKeyHex (or privateKeyPEM for RSA) are required to import a key');
|
|
72
|
+
}
|
|
73
|
+
const managedKey = this.asSphereonManagedKeyInfo(Object.assign({ alias: args.kid }, args));
|
|
74
|
+
yield this.privateKeyStore.import(Object.assign({ alias: managedKey.kid }, args));
|
|
75
|
+
debug('imported key', managedKey.type, managedKey.publicKeyHex);
|
|
76
|
+
return managedKey;
|
|
77
|
+
}
|
|
78
|
+
default:
|
|
79
|
+
return yield _super.importKey.call(this, args);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
createKey(_a) {
|
|
84
|
+
const _super = Object.create(null, {
|
|
85
|
+
createKey: { get: () => super.createKey }
|
|
86
|
+
});
|
|
87
|
+
return __awaiter(this, arguments, void 0, function* ({ type }) {
|
|
88
|
+
let key;
|
|
89
|
+
switch (type) {
|
|
90
|
+
case index_1.KeyType.Bls12381G2: {
|
|
91
|
+
throw Error('BLS support not available because upstream is not really providing Windows and React-Native support; giving too much headache. We soon will move to @digitalbazaar/bbs-signatures');
|
|
92
|
+
/*// @ts-ignore
|
|
93
|
+
const bbs = await import('@digitalbazaar/bbs-signatures')
|
|
94
|
+
const keyPairBls12381G2 = await bbs.generateKeyPair({
|
|
95
|
+
ciphersuite: 'BLS12-381-SHA-256'
|
|
96
|
+
})
|
|
97
|
+
key = await this.importKey({
|
|
98
|
+
type,
|
|
99
|
+
privateKeyHex: Buffer.from(keyPairBls12381G2.secretKey).toString('hex'),
|
|
100
|
+
publicKeyHex: Buffer.from(keyPairBls12381G2.publicKey).toString('hex'),
|
|
101
|
+
})
|
|
102
|
+
break*/
|
|
103
|
+
}
|
|
104
|
+
// @ts-ignore
|
|
105
|
+
case 'RSA': {
|
|
106
|
+
const privateKeyHex = yield (0, ssi_sdk_ext_key_utils_1.generatePrivateKeyHex)(type);
|
|
107
|
+
key = yield this.importKey({
|
|
108
|
+
type,
|
|
109
|
+
privateKeyHex,
|
|
110
|
+
});
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
default:
|
|
114
|
+
key = yield _super.createKey.call(this, { type });
|
|
115
|
+
}
|
|
116
|
+
debug('Created key', type, key.publicKeyHex);
|
|
117
|
+
return key;
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
sign(_a) {
|
|
121
|
+
const _super = Object.create(null, {
|
|
122
|
+
sign: { get: () => super.sign }
|
|
123
|
+
});
|
|
124
|
+
return __awaiter(this, arguments, void 0, function* ({ keyRef, algorithm, data }) {
|
|
125
|
+
let privateKey;
|
|
126
|
+
try {
|
|
127
|
+
privateKey = yield this.privateKeyStore.get({ alias: keyRef.kid });
|
|
128
|
+
}
|
|
129
|
+
catch (e) {
|
|
130
|
+
throw new Error(`key_not_found: No key entry found for kid=${keyRef.kid}`);
|
|
131
|
+
}
|
|
132
|
+
if (privateKey.type === index_1.KeyType.Bls12381G2) {
|
|
133
|
+
throw Error('BLS support not available because upstream is not really providing Windows and React-Native support; giving too much headache. We soon will move to @digitalbazaar/bbs-signatures');
|
|
134
|
+
/*// @ts-ignore
|
|
135
|
+
const bbs = await import('@digitalbazaar/bbs-signatures')
|
|
136
|
+
if (!data || Array.isArray(data)) {
|
|
137
|
+
throw new Error('Data must be defined and cannot be an array')
|
|
138
|
+
}
|
|
139
|
+
const keyPair = {
|
|
140
|
+
keyPair: {
|
|
141
|
+
secretKey: Uint8Array.from(Buffer.from(privateKey.privateKeyHex, 'hex')),
|
|
142
|
+
publicKey: Uint8Array.from(Buffer.from(keyRef.kid, 'hex')),
|
|
143
|
+
},
|
|
144
|
+
messages: [data],
|
|
145
|
+
}
|
|
146
|
+
const signature = await bbs.sign({secretKey: privateKey, publicKey, header, messages});
|
|
147
|
+
return signature*/
|
|
148
|
+
}
|
|
149
|
+
else if (
|
|
150
|
+
// @ts-ignore
|
|
151
|
+
privateKey.type === 'RSA' &&
|
|
152
|
+
(typeof algorithm === 'undefined' || algorithm === 'RS256' || algorithm === 'RS512' || algorithm === 'PS256' || algorithm === 'PS512')) {
|
|
153
|
+
return yield this.signRSA(privateKey, data, algorithm !== null && algorithm !== void 0 ? algorithm : 'PS256');
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
return yield _super.sign.call(this, { keyRef, algorithm, data });
|
|
157
|
+
}
|
|
158
|
+
throw Error(`not_supported: Cannot sign using key of type ${privateKey.type}`);
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
verify(_a) {
|
|
162
|
+
return __awaiter(this, arguments, void 0, function* ({ publicKeyHex, type, algorithm, data, signature, }) {
|
|
163
|
+
if (type === 'RSA') {
|
|
164
|
+
return yield this.verifyRSA(publicKeyHex, data, algorithm !== null && algorithm !== void 0 ? algorithm : 'PS256', signature);
|
|
165
|
+
}
|
|
166
|
+
throw Error(`KMS verify is not implemented yet for ${type}`);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
asSphereonManagedKeyInfo(args) {
|
|
170
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
171
|
+
let key;
|
|
172
|
+
switch (args.type) {
|
|
173
|
+
case index_1.KeyType.Bls12381G2:
|
|
174
|
+
key = {
|
|
175
|
+
type: args.type,
|
|
176
|
+
kid: (_a = args.alias) !== null && _a !== void 0 ? _a : args.publicKeyHex,
|
|
177
|
+
publicKeyHex: args.publicKeyHex,
|
|
178
|
+
meta: {
|
|
179
|
+
algorithms: ['BLS'],
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
break;
|
|
183
|
+
case 'Secp256k1': {
|
|
184
|
+
const privateBytes = u8a.fromString(args.privateKeyHex.toLowerCase(), 'base16');
|
|
185
|
+
const secp256k1 = new elliptic_1.default.ec('secp256k1');
|
|
186
|
+
const keyPair = secp256k1.keyFromPrivate(privateBytes, 'hex');
|
|
187
|
+
const publicKeyHex = keyPair.getPublic(true, 'hex');
|
|
188
|
+
key = {
|
|
189
|
+
type: args.type,
|
|
190
|
+
kid: (_b = args.alias) !== null && _b !== void 0 ? _b : publicKeyHex,
|
|
191
|
+
publicKeyHex,
|
|
192
|
+
meta: {
|
|
193
|
+
jwkThumbprint: (0, ssi_sdk_ext_key_utils_1.calculateJwkThumbprint)({ jwk: (0, ssi_sdk_ext_key_utils_1.toJwk)(publicKeyHex, 'Secp256k1') }),
|
|
194
|
+
algorithms: ['ES256K', 'ES256K-R', 'eth_signTransaction', 'eth_signTypedData', 'eth_signMessage', 'eth_rawSign'],
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
case 'Secp256r1': {
|
|
200
|
+
const privateBytes = u8a.fromString(args.privateKeyHex.toLowerCase(), 'base16');
|
|
201
|
+
const secp256r1 = new elliptic_1.default.ec('p256');
|
|
202
|
+
const keyPair = secp256r1.keyFromPrivate(privateBytes, 'hex');
|
|
203
|
+
const publicKeyHex = keyPair.getPublic(true, 'hex');
|
|
204
|
+
key = {
|
|
205
|
+
type: args.type,
|
|
206
|
+
kid: (_c = args.alias) !== null && _c !== void 0 ? _c : publicKeyHex,
|
|
207
|
+
publicKeyHex,
|
|
208
|
+
meta: {
|
|
209
|
+
jwkThumbprint: (0, ssi_sdk_ext_key_utils_1.calculateJwkThumbprint)({ jwk: (0, ssi_sdk_ext_key_utils_1.toJwk)(publicKeyHex, 'Secp256r1') }),
|
|
210
|
+
algorithms: ['ES256'],
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
// @ts-ignore
|
|
216
|
+
case 'RSA': {
|
|
217
|
+
const x509 = (_d = args.meta) === null || _d === void 0 ? void 0 : _d.x509;
|
|
218
|
+
const privateKeyPEM = (_e = x509 === null || x509 === void 0 ? void 0 : x509.privateKeyPEM) !== null && _e !== void 0 ? _e : (args.privateKeyHex.includes('---') ? args.privateKeyHex : (0, ssi_sdk_ext_x509_utils_1.hexToPEM)(args.privateKeyHex, 'private')); // In case we have x509 opts, the private key hex really was a PEM already (yuck)
|
|
219
|
+
const publicKeyJwk = (0, ssi_sdk_ext_x509_utils_1.PEMToJwk)(privateKeyPEM, 'public');
|
|
220
|
+
const publicKeyPEM = (0, ssi_sdk_ext_x509_utils_1.jwkToPEM)(publicKeyJwk, 'public');
|
|
221
|
+
const publicKeyHex = (0, ssi_sdk_ext_x509_utils_1.PEMToHex)(publicKeyPEM);
|
|
222
|
+
const meta = {};
|
|
223
|
+
if (x509) {
|
|
224
|
+
meta.x509 = {
|
|
225
|
+
cn: (_g = (_f = x509.cn) !== null && _f !== void 0 ? _f : args.alias) !== null && _g !== void 0 ? _g : publicKeyHex,
|
|
226
|
+
};
|
|
227
|
+
let certChain = (_h = x509.certificateChainPEM) !== null && _h !== void 0 ? _h : '';
|
|
228
|
+
if (x509.certificatePEM) {
|
|
229
|
+
if (!certChain.includes(x509.certificatePEM)) {
|
|
230
|
+
certChain = `${x509.certificatePEM}\n${certChain}`;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (certChain.length > 0) {
|
|
234
|
+
meta.x509.certificateChainPEM = certChain;
|
|
235
|
+
const x5c = (0, ssi_sdk_ext_x509_utils_1.pemCertChainTox5c)(certChain);
|
|
236
|
+
if (!x509.certificateChainURL) {
|
|
237
|
+
// Do not put the chain in the JWK when the chain is hosted. We do put it in the x509 metadata
|
|
238
|
+
// @ts-ignore
|
|
239
|
+
publicKeyJwk.x5c = x5c;
|
|
240
|
+
}
|
|
241
|
+
meta.x509.x5c = x5c;
|
|
242
|
+
}
|
|
243
|
+
if (x509.certificateChainURL) {
|
|
244
|
+
// @ts-ignore
|
|
245
|
+
publicKeyJwk.x5u = x509.certificateChainURL;
|
|
246
|
+
meta.x509.x5u = x509.certificateChainURL;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
key = {
|
|
250
|
+
type: args.type,
|
|
251
|
+
kid: (_l = (_j = args.alias) !== null && _j !== void 0 ? _j : (_k = meta === null || meta === void 0 ? void 0 : meta.x509) === null || _k === void 0 ? void 0 : _k.cn) !== null && _l !== void 0 ? _l : publicKeyHex,
|
|
252
|
+
publicKeyHex,
|
|
253
|
+
meta: Object.assign(Object.assign({}, meta), {
|
|
254
|
+
// todo: could als be DSA etc
|
|
255
|
+
algorithms: ['RS256', 'RS512', 'PS256', 'PS512'], publicKeyJwk,
|
|
256
|
+
publicKeyPEM }),
|
|
257
|
+
};
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
default:
|
|
261
|
+
throw Error('not_supported: Key type not supported: ' + args.type);
|
|
262
|
+
}
|
|
263
|
+
return key;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* @returns a base64url encoded signature for the `RS256` alg
|
|
267
|
+
*/
|
|
268
|
+
signRSA(privateKey, data, signingAlgorithm) {
|
|
269
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
270
|
+
const { hashAlgorithm, scheme } = (0, ssi_sdk_ext_x509_utils_1.signAlgorithmToSchemeAndHashAlg)(signingAlgorithm);
|
|
271
|
+
const signer = new ssi_sdk_ext_x509_utils_1.RSASigner((0, ssi_sdk_ext_x509_utils_1.PEMToJwk)((0, ssi_sdk_ext_x509_utils_1.hexToPEM)(privateKey.privateKeyHex, 'private'), 'private'), { hashAlgorithm, scheme });
|
|
272
|
+
const signature = yield signer.sign(data);
|
|
273
|
+
return signature;
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
verifyRSA(publicKeyHex, data, signingAlgorithm, signature) {
|
|
277
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
278
|
+
const { hashAlgorithm, scheme } = (0, ssi_sdk_ext_x509_utils_1.signAlgorithmToSchemeAndHashAlg)(signingAlgorithm);
|
|
279
|
+
const signer = new ssi_sdk_ext_x509_utils_1.RSASigner((0, ssi_sdk_ext_x509_utils_1.PEMToJwk)((0, ssi_sdk_ext_x509_utils_1.hexToPEM)(publicKeyHex, 'public'), 'public'), { hashAlgorithm, scheme });
|
|
280
|
+
return yield signer.verify(data, signature);
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
listKeys() {
|
|
284
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
285
|
+
return (yield this.privateKeyStore.list({})).map((privateKey) => this.asSphereonManagedKeyInfo(privateKey));
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
exports.SphereonKeyManagementSystem = SphereonKeyManagementSystem;
|
|
290
|
+
//# sourceMappingURL=SphereonKeyManagementSystem.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SphereonKeyManagementSystem.js","sourceRoot":"","sources":["../src/SphereonKeyManagementSystem.ts"],"names":[],"mappings":";;;;;;;;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a,EAAE,MAAM,EAAE,CAAC,CAAA;YAC7G,OAAO,MAAM,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,SAAS,CAAC,CAAA;QAC7C,CAAC;KAAA;IAEY,QAAQ;;YACnB,OAAO,CAAC,MAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,UAA6B,EAAE,EAAE,CAAC,IAAI,CAAC,wBAAwB,CAAC,UAAU,CAAC,CAAC,CAAA;QAChI,CAAC;KAAA;CACF;AApQD,kEAoQC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,52 +1,19 @@
|
|
|
1
1
|
import { X509Opts } from '@sphereon/ssi-sdk-ext.key-utils';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
import { KeyManagementSystem } from '@veramo/kms-local';
|
|
2
|
+
import { KeyMetadata, TKeyType } from '@veramo/core';
|
|
3
|
+
export { SphereonKeyManagementSystem } from './SphereonKeyManagementSystem';
|
|
5
4
|
export * from '@veramo/kms-local';
|
|
6
|
-
|
|
7
|
-
declare class SphereonKeyManagementSystem extends KeyManagementSystem {
|
|
8
|
-
private readonly privateKeyStore;
|
|
9
|
-
constructor(keyStore: AbstractPrivateKeyStore);
|
|
10
|
-
importKey(args: Omit<MinimalImportableKey, 'kms'> & {
|
|
11
|
-
privateKeyPEM?: string;
|
|
12
|
-
}): Promise<ManagedKeyInfo>;
|
|
13
|
-
createKey({ type }: {
|
|
14
|
-
type: TKeyType;
|
|
15
|
-
}): Promise<ManagedKeyInfo>;
|
|
16
|
-
sign({ keyRef, algorithm, data }: {
|
|
17
|
-
keyRef: Pick<IKey, 'kid'>;
|
|
18
|
-
algorithm?: string;
|
|
19
|
-
data: Uint8Array;
|
|
20
|
-
}): Promise<string>;
|
|
21
|
-
verify({ publicKeyHex, type, algorithm, data, signature, }: {
|
|
22
|
-
publicKeyHex: string;
|
|
23
|
-
type: TKeyType;
|
|
24
|
-
algorithm?: string;
|
|
25
|
-
data: Uint8Array;
|
|
26
|
-
signature: string;
|
|
27
|
-
}): Promise<boolean>;
|
|
28
|
-
private asSphereonManagedKeyInfo;
|
|
29
|
-
/**
|
|
30
|
-
* @returns a base64url encoded signature for the `RS256` alg
|
|
31
|
-
*/
|
|
32
|
-
private signRSA;
|
|
33
|
-
private verifyRSA;
|
|
34
|
-
listKeys(): Promise<Array<ManagedKeyInfo>>;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
interface ManagedKeyInfoArgs {
|
|
5
|
+
export interface ManagedKeyInfoArgs {
|
|
38
6
|
alias?: string;
|
|
39
7
|
type: TKeyType;
|
|
40
8
|
privateKeyHex: string;
|
|
41
9
|
publicKeyHex?: string;
|
|
42
10
|
meta?: ManageKeyInfoMeta | undefined | null;
|
|
43
11
|
}
|
|
44
|
-
interface ManageKeyInfoMeta extends KeyMetadata {
|
|
12
|
+
export interface ManageKeyInfoMeta extends KeyMetadata {
|
|
45
13
|
x509?: X509Opts;
|
|
46
14
|
[x: string]: any;
|
|
47
15
|
}
|
|
48
|
-
declare enum KeyType {
|
|
16
|
+
export declare enum KeyType {
|
|
49
17
|
Bls12381G2 = "Bls12381G2"
|
|
50
18
|
}
|
|
51
|
-
|
|
52
|
-
export { KeyType, type ManageKeyInfoMeta, type ManagedKeyInfoArgs, SphereonKeyManagementSystem };
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,iCAAiC,CAAA;AAC1D,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAEpD,OAAO,EAAE,2BAA2B,EAAE,MAAM,+BAA+B,CAAA;AAE3E,cAAc,mBAAmB,CAAA;AAEjC,MAAM,WAAW,kBAAkB;IACjC,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,QAAQ,CAAA;IACd,aAAa,EAAE,MAAM,CAAA;IACrB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,IAAI,CAAC,EAAE,iBAAiB,GAAG,SAAS,GAAG,IAAI,CAAA;CAC5C;AAED,MAAM,WAAW,iBAAkB,SAAQ,WAAW;IACpD,IAAI,CAAC,EAAE,QAAQ,CAAA;IACf,CAAC,CAAC,EAAE,MAAM,GAAG,GAAG,CAAA;CACjB;AACD,oBAAY,OAAO;IACjB,UAAU,eAAe;CAC1B"}
|
package/dist/index.js
CHANGED
|
@@ -1,267 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import Debug from "debug";
|
|
8
|
-
import elliptic from "elliptic";
|
|
9
|
-
import { fromString } from "uint8arrays/from-string";
|
|
10
|
-
import { hexToPEM, jwkToPEM, pemCertChainTox5c, PEMToHex, PEMToJwk, RSASigner, signAlgorithmToSchemeAndHashAlg } from "@sphereon/ssi-sdk-ext.x509-utils";
|
|
11
|
-
var debug = Debug("sphereon:kms:local");
|
|
12
|
-
var SphereonKeyManagementSystem = class extends KeyManagementSystem {
|
|
13
|
-
static {
|
|
14
|
-
__name(this, "SphereonKeyManagementSystem");
|
|
15
|
-
}
|
|
16
|
-
privateKeyStore;
|
|
17
|
-
constructor(keyStore) {
|
|
18
|
-
super(keyStore);
|
|
19
|
-
this.privateKeyStore = keyStore;
|
|
20
|
-
}
|
|
21
|
-
async importKey(args) {
|
|
22
|
-
switch (args.type) {
|
|
23
|
-
case KeyType.Bls12381G2.toString():
|
|
24
|
-
if (!args.privateKeyHex || !args.publicKeyHex) {
|
|
25
|
-
throw new Error("invalid_argument: type, publicKeyHex and privateKeyHex are required to import a key");
|
|
26
|
-
}
|
|
27
|
-
const managedKey = this.asSphereonManagedKeyInfo({
|
|
28
|
-
...args,
|
|
29
|
-
alias: args.kid,
|
|
30
|
-
privateKeyHex: args.privateKeyHex,
|
|
31
|
-
publicKeyHex: args.publicKeyHex,
|
|
32
|
-
type: args.type
|
|
33
|
-
});
|
|
34
|
-
await this.privateKeyStore.import({
|
|
35
|
-
alias: managedKey.kid,
|
|
36
|
-
...args
|
|
37
|
-
});
|
|
38
|
-
debug("imported key", managedKey.type, managedKey.publicKeyHex);
|
|
39
|
-
return managedKey;
|
|
40
|
-
case "Secp256k1":
|
|
41
|
-
case "Secp256r1":
|
|
42
|
-
// @ts-ignore
|
|
43
|
-
case "RSA": {
|
|
44
|
-
if (!args.privateKeyHex && !args.privateKeyPEM) {
|
|
45
|
-
throw new Error("invalid_argument: type and privateKeyHex (or privateKeyPEM for RSA) are required to import a key");
|
|
46
|
-
}
|
|
47
|
-
const managedKey2 = this.asSphereonManagedKeyInfo({
|
|
48
|
-
alias: args.kid,
|
|
49
|
-
...args
|
|
50
|
-
});
|
|
51
|
-
await this.privateKeyStore.import({
|
|
52
|
-
alias: managedKey2.kid,
|
|
53
|
-
...args
|
|
54
|
-
});
|
|
55
|
-
debug("imported key", managedKey2.type, managedKey2.publicKeyHex);
|
|
56
|
-
return managedKey2;
|
|
57
|
-
}
|
|
58
|
-
default:
|
|
59
|
-
return await super.importKey(args);
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
60
7
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
// @ts-ignore
|
|
69
|
-
case "RSA": {
|
|
70
|
-
const privateKeyHex = await generatePrivateKeyHex(type);
|
|
71
|
-
key = await this.importKey({
|
|
72
|
-
type,
|
|
73
|
-
privateKeyHex
|
|
74
|
-
});
|
|
75
|
-
break;
|
|
76
|
-
}
|
|
77
|
-
default:
|
|
78
|
-
key = await super.createKey({
|
|
79
|
-
type
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
debug("Created key", type, key.publicKeyHex);
|
|
83
|
-
return key;
|
|
84
|
-
}
|
|
85
|
-
async sign({ keyRef, algorithm, data }) {
|
|
86
|
-
let privateKey;
|
|
87
|
-
try {
|
|
88
|
-
privateKey = await this.privateKeyStore.get({
|
|
89
|
-
alias: keyRef.kid
|
|
90
|
-
});
|
|
91
|
-
} catch (e) {
|
|
92
|
-
throw new Error(`key_not_found: No key entry found for kid=${keyRef.kid}`);
|
|
93
|
-
}
|
|
94
|
-
if (privateKey.type === KeyType.Bls12381G2) {
|
|
95
|
-
throw Error("BLS support not available because upstream is not really providing Windows and React-Native support; giving too much headache. We soon will move to @digitalbazaar/bbs-signatures");
|
|
96
|
-
} else if (
|
|
97
|
-
// @ts-ignore
|
|
98
|
-
privateKey.type === "RSA" && (typeof algorithm === "undefined" || algorithm === "RS256" || algorithm === "RS512" || algorithm === "PS256" || algorithm === "PS512")
|
|
99
|
-
) {
|
|
100
|
-
return await this.signRSA(privateKey, data, algorithm ?? "PS256");
|
|
101
|
-
} else {
|
|
102
|
-
return await super.sign({
|
|
103
|
-
keyRef,
|
|
104
|
-
algorithm,
|
|
105
|
-
data
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
throw Error(`not_supported: Cannot sign using key of type ${privateKey.type}`);
|
|
109
|
-
}
|
|
110
|
-
async verify({ publicKeyHex, type, algorithm, data, signature }) {
|
|
111
|
-
if (type === "RSA") {
|
|
112
|
-
return await this.verifyRSA(publicKeyHex, data, algorithm ?? "PS256", signature);
|
|
113
|
-
}
|
|
114
|
-
throw Error(`KMS verify is not implemented yet for ${type}`);
|
|
115
|
-
}
|
|
116
|
-
asSphereonManagedKeyInfo(args) {
|
|
117
|
-
let key;
|
|
118
|
-
switch (args.type) {
|
|
119
|
-
case KeyType.Bls12381G2:
|
|
120
|
-
key = {
|
|
121
|
-
type: args.type,
|
|
122
|
-
kid: args.alias ?? args.publicKeyHex,
|
|
123
|
-
publicKeyHex: args.publicKeyHex,
|
|
124
|
-
meta: {
|
|
125
|
-
algorithms: [
|
|
126
|
-
"BLS"
|
|
127
|
-
]
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
break;
|
|
131
|
-
case "Secp256k1": {
|
|
132
|
-
const privateBytes = fromString(args.privateKeyHex.toLowerCase(), "base16");
|
|
133
|
-
const secp256k1 = new elliptic.ec("secp256k1");
|
|
134
|
-
const keyPair = secp256k1.keyFromPrivate(privateBytes, "hex");
|
|
135
|
-
const publicKeyHex = keyPair.getPublic(true, "hex");
|
|
136
|
-
key = {
|
|
137
|
-
type: args.type,
|
|
138
|
-
kid: args.alias ?? publicKeyHex,
|
|
139
|
-
publicKeyHex,
|
|
140
|
-
meta: {
|
|
141
|
-
jwkThumbprint: calculateJwkThumbprint({
|
|
142
|
-
jwk: toJwk(publicKeyHex, "Secp256k1")
|
|
143
|
-
}),
|
|
144
|
-
algorithms: [
|
|
145
|
-
"ES256K",
|
|
146
|
-
"ES256K-R",
|
|
147
|
-
"eth_signTransaction",
|
|
148
|
-
"eth_signTypedData",
|
|
149
|
-
"eth_signMessage",
|
|
150
|
-
"eth_rawSign"
|
|
151
|
-
]
|
|
152
|
-
}
|
|
153
|
-
};
|
|
154
|
-
break;
|
|
155
|
-
}
|
|
156
|
-
case "Secp256r1": {
|
|
157
|
-
const privateBytes = fromString(args.privateKeyHex.toLowerCase(), "base16");
|
|
158
|
-
const secp256r1 = new elliptic.ec("p256");
|
|
159
|
-
const keyPair = secp256r1.keyFromPrivate(privateBytes, "hex");
|
|
160
|
-
const publicKeyHex = keyPair.getPublic(true, "hex");
|
|
161
|
-
key = {
|
|
162
|
-
type: args.type,
|
|
163
|
-
kid: args.alias ?? publicKeyHex,
|
|
164
|
-
publicKeyHex,
|
|
165
|
-
meta: {
|
|
166
|
-
jwkThumbprint: calculateJwkThumbprint({
|
|
167
|
-
jwk: toJwk(publicKeyHex, "Secp256r1")
|
|
168
|
-
}),
|
|
169
|
-
algorithms: [
|
|
170
|
-
"ES256"
|
|
171
|
-
]
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
|
-
break;
|
|
175
|
-
}
|
|
176
|
-
// @ts-ignore
|
|
177
|
-
case "RSA": {
|
|
178
|
-
const x509 = args.meta?.x509;
|
|
179
|
-
const privateKeyPEM = x509?.privateKeyPEM ?? (args.privateKeyHex.includes("---") ? args.privateKeyHex : hexToPEM(args.privateKeyHex, "private"));
|
|
180
|
-
const publicKeyJwk = PEMToJwk(privateKeyPEM, "public");
|
|
181
|
-
const publicKeyPEM = jwkToPEM(publicKeyJwk, "public");
|
|
182
|
-
const publicKeyHex = PEMToHex(publicKeyPEM);
|
|
183
|
-
const meta = {};
|
|
184
|
-
if (x509) {
|
|
185
|
-
meta.x509 = {
|
|
186
|
-
cn: x509.cn ?? args.alias ?? publicKeyHex
|
|
187
|
-
};
|
|
188
|
-
let certChain = x509.certificateChainPEM ?? "";
|
|
189
|
-
if (x509.certificatePEM) {
|
|
190
|
-
if (!certChain.includes(x509.certificatePEM)) {
|
|
191
|
-
certChain = `${x509.certificatePEM}
|
|
192
|
-
${certChain}`;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
if (certChain.length > 0) {
|
|
196
|
-
meta.x509.certificateChainPEM = certChain;
|
|
197
|
-
const x5c = pemCertChainTox5c(certChain);
|
|
198
|
-
if (!x509.certificateChainURL) {
|
|
199
|
-
publicKeyJwk.x5c = x5c;
|
|
200
|
-
}
|
|
201
|
-
meta.x509.x5c = x5c;
|
|
202
|
-
}
|
|
203
|
-
if (x509.certificateChainURL) {
|
|
204
|
-
publicKeyJwk.x5u = x509.certificateChainURL;
|
|
205
|
-
meta.x509.x5u = x509.certificateChainURL;
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
key = {
|
|
209
|
-
type: args.type,
|
|
210
|
-
kid: args.alias ?? meta?.x509?.cn ?? publicKeyHex,
|
|
211
|
-
publicKeyHex,
|
|
212
|
-
meta: {
|
|
213
|
-
...meta,
|
|
214
|
-
// todo: could als be DSA etc
|
|
215
|
-
algorithms: [
|
|
216
|
-
"RS256",
|
|
217
|
-
"RS512",
|
|
218
|
-
"PS256",
|
|
219
|
-
"PS512"
|
|
220
|
-
],
|
|
221
|
-
publicKeyJwk,
|
|
222
|
-
publicKeyPEM
|
|
223
|
-
}
|
|
224
|
-
};
|
|
225
|
-
break;
|
|
226
|
-
}
|
|
227
|
-
default:
|
|
228
|
-
throw Error("not_supported: Key type not supported: " + args.type);
|
|
229
|
-
}
|
|
230
|
-
return key;
|
|
231
|
-
}
|
|
232
|
-
/**
|
|
233
|
-
* @returns a base64url encoded signature for the `RS256` alg
|
|
234
|
-
*/
|
|
235
|
-
async signRSA(privateKey, data, signingAlgorithm) {
|
|
236
|
-
const { hashAlgorithm, scheme } = signAlgorithmToSchemeAndHashAlg(signingAlgorithm);
|
|
237
|
-
const signer = new RSASigner(PEMToJwk(hexToPEM(privateKey.privateKeyHex, "private"), "private"), {
|
|
238
|
-
hashAlgorithm,
|
|
239
|
-
scheme
|
|
240
|
-
});
|
|
241
|
-
const signature = await signer.sign(data);
|
|
242
|
-
return signature;
|
|
243
|
-
}
|
|
244
|
-
async verifyRSA(publicKeyHex, data, signingAlgorithm, signature) {
|
|
245
|
-
const { hashAlgorithm, scheme } = signAlgorithmToSchemeAndHashAlg(signingAlgorithm);
|
|
246
|
-
const signer = new RSASigner(PEMToJwk(hexToPEM(publicKeyHex, "public"), "public"), {
|
|
247
|
-
hashAlgorithm,
|
|
248
|
-
scheme
|
|
249
|
-
});
|
|
250
|
-
return await signer.verify(data, signature);
|
|
251
|
-
}
|
|
252
|
-
async listKeys() {
|
|
253
|
-
return (await this.privateKeyStore.list({})).map((privateKey) => this.asSphereonManagedKeyInfo(privateKey));
|
|
254
|
-
}
|
|
255
|
-
};
|
|
256
|
-
|
|
257
|
-
// src/index.ts
|
|
258
|
-
export * from "@veramo/kms-local";
|
|
259
|
-
var KeyType = /* @__PURE__ */ function(KeyType2) {
|
|
260
|
-
KeyType2["Bls12381G2"] = "Bls12381G2";
|
|
261
|
-
return KeyType2;
|
|
262
|
-
}({});
|
|
263
|
-
export {
|
|
264
|
-
KeyType,
|
|
265
|
-
SphereonKeyManagementSystem
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
266
15
|
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.KeyType = exports.SphereonKeyManagementSystem = void 0;
|
|
18
|
+
var SphereonKeyManagementSystem_1 = require("./SphereonKeyManagementSystem");
|
|
19
|
+
Object.defineProperty(exports, "SphereonKeyManagementSystem", { enumerable: true, get: function () { return SphereonKeyManagementSystem_1.SphereonKeyManagementSystem; } });
|
|
20
|
+
__exportStar(require("@veramo/kms-local"), exports);
|
|
21
|
+
var KeyType;
|
|
22
|
+
(function (KeyType) {
|
|
23
|
+
KeyType["Bls12381G2"] = "Bls12381G2";
|
|
24
|
+
})(KeyType || (exports.KeyType = KeyType = {}));
|
|
267
25
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/SphereonKeyManagementSystem.ts","../src/index.ts"],"sourcesContent":["import { calculateJwkThumbprint, generatePrivateKeyHex, toJwk, X509Opts } from '@sphereon/ssi-sdk-ext.key-utils'\n\nimport { IKey, ManagedKeyInfo, MinimalImportableKey, TKeyType } from '@veramo/core'\nimport { AbstractPrivateKeyStore, ManagedPrivateKey } from '@veramo/key-manager'\nimport { KeyManagementSystem } from '@veramo/kms-local'\nimport Debug from 'debug'\nimport elliptic from 'elliptic'\n// @ts-ignore\nimport { fromString } from 'uint8arrays/from-string'\nimport { KeyType, ManagedKeyInfoArgs } from './index'\nimport {\n hexToPEM,\n jwkToPEM,\n pemCertChainTox5c,\n PEMToHex,\n PEMToJwk,\n RSASigner,\n signAlgorithmToSchemeAndHashAlg,\n} from '@sphereon/ssi-sdk-ext.x509-utils'\n\nconst debug = Debug('sphereon:kms:local')\n\nexport class SphereonKeyManagementSystem extends KeyManagementSystem {\n private readonly privateKeyStore: AbstractPrivateKeyStore\n\n constructor(keyStore: AbstractPrivateKeyStore) {\n super(keyStore)\n this.privateKeyStore = keyStore\n }\n\n async importKey(args: Omit<MinimalImportableKey, 'kms'> & { privateKeyPEM?: string }): Promise<ManagedKeyInfo> {\n switch (args.type) {\n case KeyType.Bls12381G2.toString():\n if (!args.privateKeyHex || !args.publicKeyHex) {\n throw new Error('invalid_argument: type, publicKeyHex and privateKeyHex are required to import a key')\n }\n const managedKey = this.asSphereonManagedKeyInfo({\n ...args,\n alias: args.kid,\n privateKeyHex: args.privateKeyHex,\n publicKeyHex: args.publicKeyHex,\n type: args.type,\n })\n await this.privateKeyStore.import({ alias: managedKey.kid, ...args })\n debug('imported key', managedKey.type, managedKey.publicKeyHex)\n return managedKey\n\n case 'Secp256k1':\n case 'Secp256r1':\n // @ts-ignore\n case 'RSA': {\n if (!args.privateKeyHex && !args.privateKeyPEM) {\n throw new Error('invalid_argument: type and privateKeyHex (or privateKeyPEM for RSA) are required to import a key')\n }\n const managedKey = this.asSphereonManagedKeyInfo({ alias: args.kid, ...args })\n await this.privateKeyStore.import({ alias: managedKey.kid, ...args })\n debug('imported key', managedKey.type, managedKey.publicKeyHex)\n return managedKey\n }\n default:\n return await super.importKey(args)\n }\n }\n\n async createKey({ type }: { type: TKeyType }): Promise<ManagedKeyInfo> {\n let key: ManagedKeyInfo\n\n switch (type) {\n case KeyType.Bls12381G2: {\n throw Error(\n 'BLS support not available because upstream is not really providing Windows and React-Native support; giving too much headache. We soon will move to @digitalbazaar/bbs-signatures'\n )\n /*// @ts-ignore\n const bbs = await import('@digitalbazaar/bbs-signatures')\n const keyPairBls12381G2 = await bbs.generateKeyPair({\n ciphersuite: 'BLS12-381-SHA-256'\n })\n key = await this.importKey({\n type,\n privateKeyHex: Buffer.from(keyPairBls12381G2.secretKey).toString('hex'),\n publicKeyHex: Buffer.from(keyPairBls12381G2.publicKey).toString('hex'),\n })\n break*/\n }\n\n // @ts-ignore\n case 'RSA': {\n const privateKeyHex = await generatePrivateKeyHex(type)\n key = await this.importKey({\n type,\n privateKeyHex,\n })\n break\n }\n default:\n key = await super.createKey({ type })\n }\n\n debug('Created key', type, key.publicKeyHex)\n\n return key\n }\n\n async sign({ keyRef, algorithm, data }: { keyRef: Pick<IKey, 'kid'>; algorithm?: string; data: Uint8Array }): Promise<string> {\n let privateKey: ManagedPrivateKey\n try {\n privateKey = await this.privateKeyStore.get({ alias: keyRef.kid })\n } catch (e) {\n throw new Error(`key_not_found: No key entry found for kid=${keyRef.kid}`)\n }\n\n if (privateKey.type === KeyType.Bls12381G2) {\n throw Error(\n 'BLS support not available because upstream is not really providing Windows and React-Native support; giving too much headache. We soon will move to @digitalbazaar/bbs-signatures'\n )\n /*// @ts-ignore\n const bbs = await import('@digitalbazaar/bbs-signatures')\n if (!data || Array.isArray(data)) {\n throw new Error('Data must be defined and cannot be an array')\n }\n const keyPair = {\n keyPair: {\n secretKey: Uint8Array.from(Buffer.from(privateKey.privateKeyHex, 'hex')),\n publicKey: Uint8Array.from(Buffer.from(keyRef.kid, 'hex')),\n },\n messages: [data],\n }\n const signature = await bbs.sign({secretKey: privateKey, publicKey, header, messages});\n return signature*/\n } else if (\n // @ts-ignore\n privateKey.type === 'RSA' &&\n (typeof algorithm === 'undefined' || algorithm === 'RS256' || algorithm === 'RS512' || algorithm === 'PS256' || algorithm === 'PS512')\n ) {\n return await this.signRSA(privateKey, data, algorithm ?? 'PS256')\n } else {\n return await super.sign({ keyRef, algorithm, data })\n }\n throw Error(`not_supported: Cannot sign using key of type ${privateKey.type}`)\n }\n\n async verify({\n publicKeyHex,\n type,\n algorithm,\n data,\n signature,\n }: {\n publicKeyHex: string\n type: TKeyType\n algorithm?: string\n data: Uint8Array\n signature: string\n }): Promise<boolean> {\n if (type === 'RSA') {\n return await this.verifyRSA(publicKeyHex, data, algorithm ?? 'PS256', signature)\n }\n throw Error(`KMS verify is not implemented yet for ${type}`)\n }\n\n private asSphereonManagedKeyInfo(args: ManagedKeyInfoArgs): ManagedKeyInfo {\n let key: Partial<ManagedKeyInfo>\n switch (args.type) {\n case KeyType.Bls12381G2:\n key = {\n type: args.type,\n kid: args.alias ?? args.publicKeyHex,\n publicKeyHex: args.publicKeyHex,\n meta: {\n algorithms: ['BLS'],\n },\n }\n break\n case 'Secp256k1': {\n const privateBytes = fromString(args.privateKeyHex.toLowerCase(), 'base16')\n const secp256k1 = new elliptic.ec('secp256k1')\n const keyPair = secp256k1.keyFromPrivate(privateBytes, 'hex')\n const publicKeyHex = keyPair.getPublic(true, 'hex')\n key = {\n type: args.type,\n kid: args.alias ?? publicKeyHex,\n publicKeyHex,\n meta: {\n jwkThumbprint: calculateJwkThumbprint({ jwk: toJwk(publicKeyHex, 'Secp256k1') }),\n algorithms: ['ES256K', 'ES256K-R', 'eth_signTransaction', 'eth_signTypedData', 'eth_signMessage', 'eth_rawSign'],\n },\n }\n break\n }\n case 'Secp256r1': {\n const privateBytes = fromString(args.privateKeyHex.toLowerCase(), 'base16')\n const secp256r1 = new elliptic.ec('p256')\n const keyPair = secp256r1.keyFromPrivate(privateBytes, 'hex')\n const publicKeyHex = keyPair.getPublic(true, 'hex')\n key = {\n type: args.type,\n kid: args.alias ?? publicKeyHex,\n publicKeyHex,\n meta: {\n jwkThumbprint: calculateJwkThumbprint({ jwk: toJwk(publicKeyHex, 'Secp256r1') }),\n algorithms: ['ES256'],\n },\n }\n break\n }\n // @ts-ignore\n case 'RSA': {\n const x509 = args.meta?.x509 as X509Opts\n const privateKeyPEM =\n x509?.privateKeyPEM ?? (args.privateKeyHex.includes('---') ? args.privateKeyHex : hexToPEM(args.privateKeyHex, 'private')) // In case we have x509 opts, the private key hex really was a PEM already (yuck)\n const publicKeyJwk = PEMToJwk(privateKeyPEM, 'public')\n const publicKeyPEM = jwkToPEM(publicKeyJwk, 'public')\n const publicKeyHex = PEMToHex(publicKeyPEM)\n\n const meta = {} as any\n if (x509) {\n meta.x509 = {\n cn: x509.cn ?? args.alias ?? publicKeyHex,\n }\n let certChain: string = x509.certificateChainPEM ?? ''\n if (x509.certificatePEM) {\n if (!certChain.includes(x509.certificatePEM)) {\n certChain = `${x509.certificatePEM}\\n${certChain}`\n }\n }\n if (certChain.length > 0) {\n meta.x509.certificateChainPEM = certChain\n const x5c = pemCertChainTox5c(certChain)\n if (!x509.certificateChainURL) {\n // Do not put the chain in the JWK when the chain is hosted. We do put it in the x509 metadata\n // @ts-ignore\n publicKeyJwk.x5c = x5c\n }\n meta.x509.x5c = x5c\n }\n if (x509.certificateChainURL) {\n // @ts-ignore\n publicKeyJwk.x5u = x509.certificateChainURL\n meta.x509.x5u = x509.certificateChainURL\n }\n }\n\n key = {\n type: args.type,\n kid: args.alias ?? meta?.x509?.cn ?? publicKeyHex,\n publicKeyHex,\n meta: {\n ...meta,\n // todo: could als be DSA etc\n algorithms: ['RS256', 'RS512', 'PS256', 'PS512'],\n publicKeyJwk,\n publicKeyPEM,\n },\n }\n break\n }\n\n default:\n throw Error('not_supported: Key type not supported: ' + args.type)\n }\n return key as ManagedKeyInfo\n }\n\n /**\n * @returns a base64url encoded signature for the `RS256` alg\n */\n private async signRSA(privateKey: ManagedPrivateKey, data: Uint8Array, signingAlgorithm: string): Promise<string> {\n const { hashAlgorithm, scheme } = signAlgorithmToSchemeAndHashAlg(signingAlgorithm)\n const signer = new RSASigner(PEMToJwk(hexToPEM(privateKey.privateKeyHex, 'private'), 'private'), { hashAlgorithm, scheme })\n const signature = await signer.sign(data)\n return signature as string\n }\n\n private async verifyRSA(publicKeyHex: string, data: Uint8Array, signingAlgorithm: string, signature: string) {\n const { hashAlgorithm, scheme } = signAlgorithmToSchemeAndHashAlg(signingAlgorithm)\n const signer = new RSASigner(PEMToJwk(hexToPEM(publicKeyHex, 'public'), 'public'), { hashAlgorithm, scheme })\n return await signer.verify(data, signature)\n }\n\n public async listKeys(): Promise<Array<ManagedKeyInfo>> {\n return (await this.privateKeyStore.list({})).map((privateKey: ManagedPrivateKey) => this.asSphereonManagedKeyInfo(privateKey))\n }\n}\n","import { X509Opts } from '@sphereon/ssi-sdk-ext.key-utils'\nimport { KeyMetadata, TKeyType } from '@veramo/core'\n\nexport { SphereonKeyManagementSystem } from './SphereonKeyManagementSystem'\n\nexport * from '@veramo/kms-local'\n\nexport interface ManagedKeyInfoArgs {\n alias?: string\n type: TKeyType\n privateKeyHex: string\n publicKeyHex?: string\n meta?: ManageKeyInfoMeta | undefined | null\n}\n\nexport interface ManageKeyInfoMeta extends KeyMetadata {\n x509?: X509Opts\n [x: string]: any\n}\nexport enum KeyType {\n Bls12381G2 = 'Bls12381G2',\n}\n"],"mappings":";;;;AAAA,SAASA,wBAAwBC,uBAAuBC,aAAuB;AAI/E,SAASC,2BAA2B;AACpC,OAAOC,WAAW;AAClB,OAAOC,cAAc;AAErB,SAASC,kBAAkB;AAE3B,SACEC,UACAC,UACAC,mBACAC,UACAC,UACAC,WACAC,uCACK;AAEP,IAAMC,QAAQC,MAAM,oBAAA;AAEb,IAAMC,8BAAN,cAA0CC,oBAAAA;EAtBjD,OAsBiDA;;;EAC9BC;EAEjBC,YAAYC,UAAmC;AAC7C,UAAMA,QAAAA;AACN,SAAKF,kBAAkBE;EACzB;EAEA,MAAMC,UAAUC,MAA+F;AAC7G,YAAQA,KAAKC,MAAI;MACf,KAAKC,QAAQC,WAAWC,SA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wE,KAAK,CAAC,CAAA,GAAIC,IAAI,CAAChD,eAAkC,KAAKZ,yBAAyBY,UAAAA,CAAAA;EACpH;AACF;;;ACrRA,cAAc;AAcP,IAAKiD,UAAAA,yBAAAA,UAAAA;;SAAAA;;","names":["calculateJwkThumbprint","generatePrivateKeyHex","toJwk","KeyManagementSystem","Debug","elliptic","fromString","hexToPEM","jwkToPEM","pemCertChainTox5c","PEMToHex","PEMToJwk","RSASigner","signAlgorithmToSchemeAndHashAlg","debug","Debug","SphereonKeyManagementSystem","KeyManagementSystem","privateKeyStore","constructor","keyStore","importKey","args","type","KeyType","Bls12381G2","toString","privateKeyHex","publicKeyHex","Error","managedKey","asSphereonManagedKeyInfo","alias","kid","import","privateKeyPEM","createKey","key","generatePrivateKeyHex","sign","keyRef","algorithm","data","privateKey","get","e","signRSA","verify","signature","verifyRSA","meta","algorithms","privateBytes","fromString","toLowerCase","secp256k1","elliptic","ec","keyPair","keyFromPrivate","getPublic","jwkThumbprint","calculateJwkThumbprint","jwk","toJwk","secp256r1","x509","includes","hexToPEM","publicKeyJwk","PEMToJwk","publicKeyPEM","jwkToPEM","PEMToHex","cn","certChain","certificateChainPEM","certificatePEM","length","x5c","pemCertChainTox5c","certificateChainURL","x5u","signingAlgorithm","hashAlgorithm","scheme","signAlgorithmToSchemeAndHashAlg","signer","RSASigner","listKeys","list","map","KeyType"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAGA,6EAA2E;AAAlE,0IAAA,2BAA2B,OAAA;AAEpC,oDAAiC;AAcjC,IAAY,OAEX;AAFD,WAAY,OAAO;IACjB,oCAAyB,CAAA;AAC3B,CAAC,EAFW,OAAO,uBAAP,OAAO,QAElB"}
|
|
@@ -7,9 +7,10 @@ import { MinimalImportableKey } from '@veramo/core';
|
|
|
7
7
|
import { TKeyType } from '@veramo/core';
|
|
8
8
|
import { X509Opts } from '@sphereon/ssi-sdk-ext.key-utils';
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
declare enum KeyType_2 {
|
|
11
11
|
Bls12381G2 = "Bls12381G2"
|
|
12
12
|
}
|
|
13
|
+
export { KeyType_2 as KeyType }
|
|
13
14
|
|
|
14
15
|
export declare interface ManagedKeyInfoArgs {
|
|
15
16
|
alias?: string;
|
package/dist/tsdoc-metadata.json
CHANGED
package/package.json
CHANGED
|
@@ -1,47 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sphereon/ssi-sdk-ext.kms-local",
|
|
3
3
|
"description": "Sphereon Local Key Management System with support for BLS/BBS+, RSA keys",
|
|
4
|
-
"version": "0.28.1-feature.
|
|
5
|
-
"source": "
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"module": "./dist/index.js",
|
|
9
|
-
"types": "./dist/index.d.ts",
|
|
10
|
-
"exports": {
|
|
11
|
-
"import": {
|
|
12
|
-
"types": "./dist/index.d.ts",
|
|
13
|
-
"import": "./dist/index.js"
|
|
14
|
-
},
|
|
15
|
-
"require": {
|
|
16
|
-
"types": "./dist/index.d.cts",
|
|
17
|
-
"require": "./dist/index.cjs"
|
|
18
|
-
}
|
|
19
|
-
},
|
|
4
|
+
"version": "0.28.1-feature.oyd.cmsm.improv.16+a254c6d",
|
|
5
|
+
"source": "src/index.ts",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
20
8
|
"scripts": {
|
|
21
|
-
"build": "
|
|
9
|
+
"build": "tsc --build",
|
|
22
10
|
"generate-plugin-schema": "sphereon dev generate-plugin-schema"
|
|
23
11
|
},
|
|
24
12
|
"dependencies": {
|
|
25
|
-
"@sphereon/ssi-sdk-ext.did-utils": "
|
|
26
|
-
"@sphereon/ssi-sdk-ext.key-utils": "
|
|
27
|
-
"@sphereon/ssi-sdk-ext.x509-utils": "
|
|
13
|
+
"@sphereon/ssi-sdk-ext.did-utils": "0.28.1-feature.oyd.cmsm.improv.16+a254c6d",
|
|
14
|
+
"@sphereon/ssi-sdk-ext.key-utils": "0.28.1-feature.oyd.cmsm.improv.16+a254c6d",
|
|
15
|
+
"@sphereon/ssi-sdk-ext.x509-utils": "0.28.1-feature.oyd.cmsm.improv.16+a254c6d",
|
|
28
16
|
"@trust/keyto": "2.0.0-alpha1",
|
|
29
17
|
"@veramo/core": "4.2.0",
|
|
30
18
|
"@veramo/key-manager": "4.2.0",
|
|
31
19
|
"@veramo/kms-local": "4.2.0",
|
|
32
|
-
"debug": "^4.4.0",
|
|
33
20
|
"elliptic": "^6.5.4",
|
|
34
|
-
"uint8arrays": "
|
|
21
|
+
"uint8arrays": "3.1.1"
|
|
35
22
|
},
|
|
36
23
|
"devDependencies": {
|
|
37
24
|
"@sphereon/jsencrypt": "3.3.2-unstable.0",
|
|
38
|
-
"@sphereon/ssi-sdk.dev": "
|
|
25
|
+
"@sphereon/ssi-sdk.dev": "0.30.2-feature.SDK.41.oidf.support.286",
|
|
39
26
|
"@types/elliptic": "6.4.14",
|
|
40
27
|
"@veramo/cli": "4.2.0"
|
|
41
28
|
},
|
|
42
29
|
"files": [
|
|
43
|
-
"dist",
|
|
44
|
-
"src",
|
|
30
|
+
"dist/**/*",
|
|
31
|
+
"src/**/*",
|
|
45
32
|
"plugin.schema.json",
|
|
46
33
|
"README.md",
|
|
47
34
|
"LICENSE"
|
|
@@ -57,5 +44,5 @@
|
|
|
57
44
|
"kms",
|
|
58
45
|
"Veramo"
|
|
59
46
|
],
|
|
60
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "a254c6d44af6fbb12419b55054f1db5afbe484f0"
|
|
61
48
|
}
|
|
@@ -5,8 +5,7 @@ import { AbstractPrivateKeyStore, ManagedPrivateKey } from '@veramo/key-manager'
|
|
|
5
5
|
import { KeyManagementSystem } from '@veramo/kms-local'
|
|
6
6
|
import Debug from 'debug'
|
|
7
7
|
import elliptic from 'elliptic'
|
|
8
|
-
|
|
9
|
-
import { fromString } from 'uint8arrays/from-string'
|
|
8
|
+
import * as u8a from 'uint8arrays'
|
|
10
9
|
import { KeyType, ManagedKeyInfoArgs } from './index'
|
|
11
10
|
import {
|
|
12
11
|
hexToPEM,
|
|
@@ -172,7 +171,7 @@ export class SphereonKeyManagementSystem extends KeyManagementSystem {
|
|
|
172
171
|
}
|
|
173
172
|
break
|
|
174
173
|
case 'Secp256k1': {
|
|
175
|
-
const privateBytes = fromString(args.privateKeyHex.toLowerCase(), 'base16')
|
|
174
|
+
const privateBytes = u8a.fromString(args.privateKeyHex.toLowerCase(), 'base16')
|
|
176
175
|
const secp256k1 = new elliptic.ec('secp256k1')
|
|
177
176
|
const keyPair = secp256k1.keyFromPrivate(privateBytes, 'hex')
|
|
178
177
|
const publicKeyHex = keyPair.getPublic(true, 'hex')
|
|
@@ -188,7 +187,7 @@ export class SphereonKeyManagementSystem extends KeyManagementSystem {
|
|
|
188
187
|
break
|
|
189
188
|
}
|
|
190
189
|
case 'Secp256r1': {
|
|
191
|
-
const privateBytes = fromString(args.privateKeyHex.toLowerCase(), 'base16')
|
|
190
|
+
const privateBytes = u8a.fromString(args.privateKeyHex.toLowerCase(), 'base16')
|
|
192
191
|
const secp256r1 = new elliptic.ec('p256')
|
|
193
192
|
const keyPair = secp256r1.keyFromPrivate(privateBytes, 'hex')
|
|
194
193
|
const publicKeyHex = keyPair.getPublic(true, 'hex')
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { ManagedKeyInfo } from '@veramo/core'
|
|
2
|
-
import { MemoryPrivateKeyStore } from '@veramo/key-manager'
|
|
3
|
-
import { describe, expect, it } from 'vitest'
|
|
4
1
|
import { SphereonKeyManagementSystem } from '../SphereonKeyManagementSystem'
|
|
2
|
+
import { MemoryPrivateKeyStore } from '@veramo/key-manager'
|
|
3
|
+
import { ManagedKeyInfo } from '@veramo/core'
|
|
5
4
|
|
|
6
5
|
describe('Key creation', () => {
|
|
7
6
|
it('should create a RSA key', async () => {
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import JSEncrypt from '@sphereon/jsencrypt'
|
|
2
|
+
import { PEM_CERT, PEM_CHAIN, PEM_FULL_CHAIN, PEM_PRIV_KEY } from './certs'
|
|
3
|
+
import { SphereonKeyManagementSystem } from '../SphereonKeyManagementSystem'
|
|
4
|
+
import { MemoryPrivateKeyStore } from '@veramo/key-manager'
|
|
5
|
+
import * as u8a from 'uint8arrays'
|
|
2
6
|
import { digestMethodParams, X509Opts } from '@sphereon/ssi-sdk-ext.key-utils'
|
|
3
7
|
import {
|
|
4
8
|
pemCertChainTox5c,
|
|
@@ -9,11 +13,6 @@ import {
|
|
|
9
13
|
toKeyObject,
|
|
10
14
|
x5cToPemCertChain,
|
|
11
15
|
} from '@sphereon/ssi-sdk-ext.x509-utils'
|
|
12
|
-
import { MemoryPrivateKeyStore } from '@veramo/key-manager'
|
|
13
|
-
import * as u8a from 'uint8arrays'
|
|
14
|
-
import { describe, expect, it } from 'vitest'
|
|
15
|
-
import { SphereonKeyManagementSystem } from '../SphereonKeyManagementSystem'
|
|
16
|
-
import { PEM_CERT, PEM_CHAIN, PEM_FULL_CHAIN, PEM_PRIV_KEY } from './certs'
|
|
17
16
|
|
|
18
17
|
describe('X509 PEMs', () => {
|
|
19
18
|
it('should get public key from private key', () => {
|
package/dist/index.cjs
DELETED
|
@@ -1,267 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _createStarExport(obj) { Object.keys(obj) .filter((key) => key !== "default" && key !== "__esModule") .forEach((key) => { if (exports.hasOwnProperty(key)) { return; } Object.defineProperty(exports, key, {enumerable: true, configurable: true, get: () => obj[key]}); }); } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var __defProp = Object.defineProperty;
|
|
2
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
-
|
|
4
|
-
// src/SphereonKeyManagementSystem.ts
|
|
5
|
-
var _ssisdkextkeyutils = require('@sphereon/ssi-sdk-ext.key-utils');
|
|
6
|
-
var _kmslocal = require('@veramo/kms-local'); _createStarExport(_kmslocal);
|
|
7
|
-
var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug);
|
|
8
|
-
var _elliptic = require('elliptic'); var _elliptic2 = _interopRequireDefault(_elliptic);
|
|
9
|
-
var _fromstring = require('uint8arrays/from-string');
|
|
10
|
-
var _ssisdkextx509utils = require('@sphereon/ssi-sdk-ext.x509-utils');
|
|
11
|
-
var debug = _debug2.default.call(void 0, "sphereon:kms:local");
|
|
12
|
-
var SphereonKeyManagementSystem = class extends _kmslocal.KeyManagementSystem {
|
|
13
|
-
static {
|
|
14
|
-
__name(this, "SphereonKeyManagementSystem");
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
constructor(keyStore) {
|
|
18
|
-
super(keyStore);
|
|
19
|
-
this.privateKeyStore = keyStore;
|
|
20
|
-
}
|
|
21
|
-
async importKey(args) {
|
|
22
|
-
switch (args.type) {
|
|
23
|
-
case KeyType.Bls12381G2.toString():
|
|
24
|
-
if (!args.privateKeyHex || !args.publicKeyHex) {
|
|
25
|
-
throw new Error("invalid_argument: type, publicKeyHex and privateKeyHex are required to import a key");
|
|
26
|
-
}
|
|
27
|
-
const managedKey = this.asSphereonManagedKeyInfo({
|
|
28
|
-
...args,
|
|
29
|
-
alias: args.kid,
|
|
30
|
-
privateKeyHex: args.privateKeyHex,
|
|
31
|
-
publicKeyHex: args.publicKeyHex,
|
|
32
|
-
type: args.type
|
|
33
|
-
});
|
|
34
|
-
await this.privateKeyStore.import({
|
|
35
|
-
alias: managedKey.kid,
|
|
36
|
-
...args
|
|
37
|
-
});
|
|
38
|
-
debug("imported key", managedKey.type, managedKey.publicKeyHex);
|
|
39
|
-
return managedKey;
|
|
40
|
-
case "Secp256k1":
|
|
41
|
-
case "Secp256r1":
|
|
42
|
-
// @ts-ignore
|
|
43
|
-
case "RSA": {
|
|
44
|
-
if (!args.privateKeyHex && !args.privateKeyPEM) {
|
|
45
|
-
throw new Error("invalid_argument: type and privateKeyHex (or privateKeyPEM for RSA) are required to import a key");
|
|
46
|
-
}
|
|
47
|
-
const managedKey2 = this.asSphereonManagedKeyInfo({
|
|
48
|
-
alias: args.kid,
|
|
49
|
-
...args
|
|
50
|
-
});
|
|
51
|
-
await this.privateKeyStore.import({
|
|
52
|
-
alias: managedKey2.kid,
|
|
53
|
-
...args
|
|
54
|
-
});
|
|
55
|
-
debug("imported key", managedKey2.type, managedKey2.publicKeyHex);
|
|
56
|
-
return managedKey2;
|
|
57
|
-
}
|
|
58
|
-
default:
|
|
59
|
-
return await super.importKey(args);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
async createKey({ type }) {
|
|
63
|
-
let key;
|
|
64
|
-
switch (type) {
|
|
65
|
-
case KeyType.Bls12381G2: {
|
|
66
|
-
throw Error("BLS support not available because upstream is not really providing Windows and React-Native support; giving too much headache. We soon will move to @digitalbazaar/bbs-signatures");
|
|
67
|
-
}
|
|
68
|
-
// @ts-ignore
|
|
69
|
-
case "RSA": {
|
|
70
|
-
const privateKeyHex = await _ssisdkextkeyutils.generatePrivateKeyHex.call(void 0, type);
|
|
71
|
-
key = await this.importKey({
|
|
72
|
-
type,
|
|
73
|
-
privateKeyHex
|
|
74
|
-
});
|
|
75
|
-
break;
|
|
76
|
-
}
|
|
77
|
-
default:
|
|
78
|
-
key = await super.createKey({
|
|
79
|
-
type
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
debug("Created key", type, key.publicKeyHex);
|
|
83
|
-
return key;
|
|
84
|
-
}
|
|
85
|
-
async sign({ keyRef, algorithm, data }) {
|
|
86
|
-
let privateKey;
|
|
87
|
-
try {
|
|
88
|
-
privateKey = await this.privateKeyStore.get({
|
|
89
|
-
alias: keyRef.kid
|
|
90
|
-
});
|
|
91
|
-
} catch (e) {
|
|
92
|
-
throw new Error(`key_not_found: No key entry found for kid=${keyRef.kid}`);
|
|
93
|
-
}
|
|
94
|
-
if (privateKey.type === KeyType.Bls12381G2) {
|
|
95
|
-
throw Error("BLS support not available because upstream is not really providing Windows and React-Native support; giving too much headache. We soon will move to @digitalbazaar/bbs-signatures");
|
|
96
|
-
} else if (
|
|
97
|
-
// @ts-ignore
|
|
98
|
-
privateKey.type === "RSA" && (typeof algorithm === "undefined" || algorithm === "RS256" || algorithm === "RS512" || algorithm === "PS256" || algorithm === "PS512")
|
|
99
|
-
) {
|
|
100
|
-
return await this.signRSA(privateKey, data, _nullishCoalesce(algorithm, () => ( "PS256")));
|
|
101
|
-
} else {
|
|
102
|
-
return await super.sign({
|
|
103
|
-
keyRef,
|
|
104
|
-
algorithm,
|
|
105
|
-
data
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
throw Error(`not_supported: Cannot sign using key of type ${privateKey.type}`);
|
|
109
|
-
}
|
|
110
|
-
async verify({ publicKeyHex, type, algorithm, data, signature }) {
|
|
111
|
-
if (type === "RSA") {
|
|
112
|
-
return await this.verifyRSA(publicKeyHex, data, _nullishCoalesce(algorithm, () => ( "PS256")), signature);
|
|
113
|
-
}
|
|
114
|
-
throw Error(`KMS verify is not implemented yet for ${type}`);
|
|
115
|
-
}
|
|
116
|
-
asSphereonManagedKeyInfo(args) {
|
|
117
|
-
let key;
|
|
118
|
-
switch (args.type) {
|
|
119
|
-
case KeyType.Bls12381G2:
|
|
120
|
-
key = {
|
|
121
|
-
type: args.type,
|
|
122
|
-
kid: _nullishCoalesce(args.alias, () => ( args.publicKeyHex)),
|
|
123
|
-
publicKeyHex: args.publicKeyHex,
|
|
124
|
-
meta: {
|
|
125
|
-
algorithms: [
|
|
126
|
-
"BLS"
|
|
127
|
-
]
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
break;
|
|
131
|
-
case "Secp256k1": {
|
|
132
|
-
const privateBytes = _fromstring.fromString.call(void 0, args.privateKeyHex.toLowerCase(), "base16");
|
|
133
|
-
const secp256k1 = new _elliptic2.default.ec("secp256k1");
|
|
134
|
-
const keyPair = secp256k1.keyFromPrivate(privateBytes, "hex");
|
|
135
|
-
const publicKeyHex = keyPair.getPublic(true, "hex");
|
|
136
|
-
key = {
|
|
137
|
-
type: args.type,
|
|
138
|
-
kid: _nullishCoalesce(args.alias, () => ( publicKeyHex)),
|
|
139
|
-
publicKeyHex,
|
|
140
|
-
meta: {
|
|
141
|
-
jwkThumbprint: _ssisdkextkeyutils.calculateJwkThumbprint.call(void 0, {
|
|
142
|
-
jwk: _ssisdkextkeyutils.toJwk.call(void 0, publicKeyHex, "Secp256k1")
|
|
143
|
-
}),
|
|
144
|
-
algorithms: [
|
|
145
|
-
"ES256K",
|
|
146
|
-
"ES256K-R",
|
|
147
|
-
"eth_signTransaction",
|
|
148
|
-
"eth_signTypedData",
|
|
149
|
-
"eth_signMessage",
|
|
150
|
-
"eth_rawSign"
|
|
151
|
-
]
|
|
152
|
-
}
|
|
153
|
-
};
|
|
154
|
-
break;
|
|
155
|
-
}
|
|
156
|
-
case "Secp256r1": {
|
|
157
|
-
const privateBytes = _fromstring.fromString.call(void 0, args.privateKeyHex.toLowerCase(), "base16");
|
|
158
|
-
const secp256r1 = new _elliptic2.default.ec("p256");
|
|
159
|
-
const keyPair = secp256r1.keyFromPrivate(privateBytes, "hex");
|
|
160
|
-
const publicKeyHex = keyPair.getPublic(true, "hex");
|
|
161
|
-
key = {
|
|
162
|
-
type: args.type,
|
|
163
|
-
kid: _nullishCoalesce(args.alias, () => ( publicKeyHex)),
|
|
164
|
-
publicKeyHex,
|
|
165
|
-
meta: {
|
|
166
|
-
jwkThumbprint: _ssisdkextkeyutils.calculateJwkThumbprint.call(void 0, {
|
|
167
|
-
jwk: _ssisdkextkeyutils.toJwk.call(void 0, publicKeyHex, "Secp256r1")
|
|
168
|
-
}),
|
|
169
|
-
algorithms: [
|
|
170
|
-
"ES256"
|
|
171
|
-
]
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
|
-
break;
|
|
175
|
-
}
|
|
176
|
-
// @ts-ignore
|
|
177
|
-
case "RSA": {
|
|
178
|
-
const x509 = _optionalChain([args, 'access', _ => _.meta, 'optionalAccess', _2 => _2.x509]);
|
|
179
|
-
const privateKeyPEM = _nullishCoalesce(_optionalChain([x509, 'optionalAccess', _3 => _3.privateKeyPEM]), () => ( (args.privateKeyHex.includes("---") ? args.privateKeyHex : _ssisdkextx509utils.hexToPEM.call(void 0, args.privateKeyHex, "private"))));
|
|
180
|
-
const publicKeyJwk = _ssisdkextx509utils.PEMToJwk.call(void 0, privateKeyPEM, "public");
|
|
181
|
-
const publicKeyPEM = _ssisdkextx509utils.jwkToPEM.call(void 0, publicKeyJwk, "public");
|
|
182
|
-
const publicKeyHex = _ssisdkextx509utils.PEMToHex.call(void 0, publicKeyPEM);
|
|
183
|
-
const meta = {};
|
|
184
|
-
if (x509) {
|
|
185
|
-
meta.x509 = {
|
|
186
|
-
cn: _nullishCoalesce(_nullishCoalesce(x509.cn, () => ( args.alias)), () => ( publicKeyHex))
|
|
187
|
-
};
|
|
188
|
-
let certChain = _nullishCoalesce(x509.certificateChainPEM, () => ( ""));
|
|
189
|
-
if (x509.certificatePEM) {
|
|
190
|
-
if (!certChain.includes(x509.certificatePEM)) {
|
|
191
|
-
certChain = `${x509.certificatePEM}
|
|
192
|
-
${certChain}`;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
if (certChain.length > 0) {
|
|
196
|
-
meta.x509.certificateChainPEM = certChain;
|
|
197
|
-
const x5c = _ssisdkextx509utils.pemCertChainTox5c.call(void 0, certChain);
|
|
198
|
-
if (!x509.certificateChainURL) {
|
|
199
|
-
publicKeyJwk.x5c = x5c;
|
|
200
|
-
}
|
|
201
|
-
meta.x509.x5c = x5c;
|
|
202
|
-
}
|
|
203
|
-
if (x509.certificateChainURL) {
|
|
204
|
-
publicKeyJwk.x5u = x509.certificateChainURL;
|
|
205
|
-
meta.x509.x5u = x509.certificateChainURL;
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
key = {
|
|
209
|
-
type: args.type,
|
|
210
|
-
kid: _nullishCoalesce(_nullishCoalesce(args.alias, () => ( _optionalChain([meta, 'optionalAccess', _4 => _4.x509, 'optionalAccess', _5 => _5.cn]))), () => ( publicKeyHex)),
|
|
211
|
-
publicKeyHex,
|
|
212
|
-
meta: {
|
|
213
|
-
...meta,
|
|
214
|
-
// todo: could als be DSA etc
|
|
215
|
-
algorithms: [
|
|
216
|
-
"RS256",
|
|
217
|
-
"RS512",
|
|
218
|
-
"PS256",
|
|
219
|
-
"PS512"
|
|
220
|
-
],
|
|
221
|
-
publicKeyJwk,
|
|
222
|
-
publicKeyPEM
|
|
223
|
-
}
|
|
224
|
-
};
|
|
225
|
-
break;
|
|
226
|
-
}
|
|
227
|
-
default:
|
|
228
|
-
throw Error("not_supported: Key type not supported: " + args.type);
|
|
229
|
-
}
|
|
230
|
-
return key;
|
|
231
|
-
}
|
|
232
|
-
/**
|
|
233
|
-
* @returns a base64url encoded signature for the `RS256` alg
|
|
234
|
-
*/
|
|
235
|
-
async signRSA(privateKey, data, signingAlgorithm) {
|
|
236
|
-
const { hashAlgorithm, scheme } = _ssisdkextx509utils.signAlgorithmToSchemeAndHashAlg.call(void 0, signingAlgorithm);
|
|
237
|
-
const signer = new (0, _ssisdkextx509utils.RSASigner)(_ssisdkextx509utils.PEMToJwk.call(void 0, _ssisdkextx509utils.hexToPEM.call(void 0, privateKey.privateKeyHex, "private"), "private"), {
|
|
238
|
-
hashAlgorithm,
|
|
239
|
-
scheme
|
|
240
|
-
});
|
|
241
|
-
const signature = await signer.sign(data);
|
|
242
|
-
return signature;
|
|
243
|
-
}
|
|
244
|
-
async verifyRSA(publicKeyHex, data, signingAlgorithm, signature) {
|
|
245
|
-
const { hashAlgorithm, scheme } = _ssisdkextx509utils.signAlgorithmToSchemeAndHashAlg.call(void 0, signingAlgorithm);
|
|
246
|
-
const signer = new (0, _ssisdkextx509utils.RSASigner)(_ssisdkextx509utils.PEMToJwk.call(void 0, _ssisdkextx509utils.hexToPEM.call(void 0, publicKeyHex, "public"), "public"), {
|
|
247
|
-
hashAlgorithm,
|
|
248
|
-
scheme
|
|
249
|
-
});
|
|
250
|
-
return await signer.verify(data, signature);
|
|
251
|
-
}
|
|
252
|
-
async listKeys() {
|
|
253
|
-
return (await this.privateKeyStore.list({})).map((privateKey) => this.asSphereonManagedKeyInfo(privateKey));
|
|
254
|
-
}
|
|
255
|
-
};
|
|
256
|
-
|
|
257
|
-
// src/index.ts
|
|
258
|
-
|
|
259
|
-
var KeyType = /* @__PURE__ */ function(KeyType2) {
|
|
260
|
-
KeyType2["Bls12381G2"] = "Bls12381G2";
|
|
261
|
-
return KeyType2;
|
|
262
|
-
}({});
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
exports.KeyType = KeyType; exports.SphereonKeyManagementSystem = SphereonKeyManagementSystem;
|
|
267
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["/home/runner/work/SSI-SDK-crypto-extensions/SSI-SDK-crypto-extensions/packages/kms-local/dist/index.cjs","../src/SphereonKeyManagementSystem.ts","../src/index.ts"],"names":["debug","Debug","SphereonKeyManagementSystem","KeyManagementSystem","privateKeyStore","constructor","keyStore","importKey","args","type","KeyType","Bls12381G2","toString","privateKeyHex","publicKeyHex","Error","managedKey","asSphereonManagedKeyInfo","alias","kid","import","privateKeyPEM","createKey","key","generatePrivateKeyHex","sign","keyRef","algorithm","data","privateKey","get","e","signature","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,UAAA;AACQA,YAAAA;AACFA,YAAAA;AACC,YAAA;AAGVC,cAAAA;AACrB,YAAA;AACgBA,YAAAA;AAClB,UAAA;AAC8B,UAAA;AAEJC,YAAAA;AACHA,YAAAA;AACvB,UAAA;AACF,QAAA;AAEM,QAAA;AACO7B,UAAAA;AAC0BK,UAAAA;AACrCA,UAAAA;AACM,UAAA;AACDyB,YAAAA;ADlC+D;ACoCtD,YAAA;AAAC,cAAA;AAAS,cAAA;AAAS,cAAA;AAAS,cAAA;AD9B0B,YAAA;AC+BlEC,YAAAA;AACAL,YAAAA;AACF,UAAA;AACF,QAAA;AACA,QAAA;AACF,MAAA;AAEA,MAAA;AACmE,QAAA;AACrE,IAAA;AACOZ,IAAAA;AACT,EAAA;AD9B4E;AACA;AACA;ACiCsC,EAAA;AAC9CkB,IAAAA;AACO,IAAA;AAA0BC,MAAAA;AAAeC,MAAAA;AAAO,IAAA;AACrFf,IAAAA;AAC7BI,IAAAA;AACT,EAAA;AAE6G,EAAA;AACzCS,IAAAA;AACM,IAAA;AAAaC,MAAAA;AAAeC,MAAAA;AAAO,IAAA;AAC1EX,IAAAA;AACnC,EAAA;AAEwD,EAAA;AACmCf,IAAAA;AAC3F,EAAA;AACF;AD3B8E;AACA;AE3PhE;AAcFP;AFgPkE,EAAA;AEhPlEA,EAAAA;AFkPkE;AACA;AACA;AACA;AACA","file":"/home/runner/work/SSI-SDK-crypto-extensions/SSI-SDK-crypto-extensions/packages/kms-local/dist/index.cjs","sourcesContent":[null,"import { calculateJwkThumbprint, generatePrivateKeyHex, toJwk, X509Opts } from '@sphereon/ssi-sdk-ext.key-utils'\n\nimport { IKey, ManagedKeyInfo, MinimalImportableKey, TKeyType } from '@veramo/core'\nimport { AbstractPrivateKeyStore, ManagedPrivateKey } from '@veramo/key-manager'\nimport { KeyManagementSystem } from '@veramo/kms-local'\nimport Debug from 'debug'\nimport elliptic from 'elliptic'\n// @ts-ignore\nimport { fromString } from 'uint8arrays/from-string'\nimport { KeyType, ManagedKeyInfoArgs } from './index'\nimport {\n hexToPEM,\n jwkToPEM,\n pemCertChainTox5c,\n PEMToHex,\n PEMToJwk,\n RSASigner,\n signAlgorithmToSchemeAndHashAlg,\n} from '@sphereon/ssi-sdk-ext.x509-utils'\n\nconst debug = Debug('sphereon:kms:local')\n\nexport class SphereonKeyManagementSystem extends KeyManagementSystem {\n private readonly privateKeyStore: AbstractPrivateKeyStore\n\n constructor(keyStore: AbstractPrivateKeyStore) {\n super(keyStore)\n this.privateKeyStore = keyStore\n }\n\n async importKey(args: Omit<MinimalImportableKey, 'kms'> & { privateKeyPEM?: string }): Promise<ManagedKeyInfo> {\n switch (args.type) {\n case KeyType.Bls12381G2.toString():\n if (!args.privateKeyHex || !args.publicKeyHex) {\n throw new Error('invalid_argument: type, publicKeyHex and privateKeyHex are required to import a key')\n }\n const managedKey = this.asSphereonManagedKeyInfo({\n ...args,\n alias: args.kid,\n privateKeyHex: args.privateKeyHex,\n publicKeyHex: args.publicKeyHex,\n type: args.type,\n })\n await this.privateKeyStore.import({ alias: managedKey.kid, ...args })\n debug('imported key', managedKey.type, managedKey.publicKeyHex)\n return managedKey\n\n case 'Secp256k1':\n case 'Secp256r1':\n // @ts-ignore\n case 'RSA': {\n if (!args.privateKeyHex && !args.privateKeyPEM) {\n throw new Error('invalid_argument: type and privateKeyHex (or privateKeyPEM for RSA) are required to import a key')\n }\n const managedKey = this.asSphereonManagedKeyInfo({ alias: args.kid, ...args })\n await this.privateKeyStore.import({ alias: managedKey.kid, ...args })\n debug('imported key', managedKey.type, managedKey.publicKeyHex)\n return managedKey\n }\n default:\n return await super.importKey(args)\n }\n }\n\n async createKey({ type }: { type: TKeyType }): Promise<ManagedKeyInfo> {\n let key: ManagedKeyInfo\n\n switch (type) {\n case KeyType.Bls12381G2: {\n throw Error(\n 'BLS support not available because upstream is not really providing Windows and React-Native support; giving too much headache. We soon will move to @digitalbazaar/bbs-signatures'\n )\n /*// @ts-ignore\n const bbs = await import('@digitalbazaar/bbs-signatures')\n const keyPairBls12381G2 = await bbs.generateKeyPair({\n ciphersuite: 'BLS12-381-SHA-256'\n })\n key = await this.importKey({\n type,\n privateKeyHex: Buffer.from(keyPairBls12381G2.secretKey).toString('hex'),\n publicKeyHex: Buffer.from(keyPairBls12381G2.publicKey).toString('hex'),\n })\n break*/\n }\n\n // @ts-ignore\n case 'RSA': {\n const privateKeyHex = await generatePrivateKeyHex(type)\n key = await this.importKey({\n type,\n privateKeyHex,\n })\n break\n }\n default:\n key = await super.createKey({ type })\n }\n\n debug('Created key', type, key.publicKeyHex)\n\n return key\n }\n\n async sign({ keyRef, algorithm, data }: { keyRef: Pick<IKey, 'kid'>; algorithm?: string; data: Uint8Array }): Promise<string> {\n let privateKey: ManagedPrivateKey\n try {\n privateKey = await this.privateKeyStore.get({ alias: keyRef.kid })\n } catch (e) {\n throw new Error(`key_not_found: No key entry found for kid=${keyRef.kid}`)\n }\n\n if (privateKey.type === KeyType.Bls12381G2) {\n throw Error(\n 'BLS support not available because upstream is not really providing Windows and React-Native support; giving too much headache. We soon will move to @digitalbazaar/bbs-signatures'\n )\n /*// @ts-ignore\n const bbs = await import('@digitalbazaar/bbs-signatures')\n if (!data || Array.isArray(data)) {\n throw new Error('Data must be defined and cannot be an array')\n }\n const keyPair = {\n keyPair: {\n secretKey: Uint8Array.from(Buffer.from(privateKey.privateKeyHex, 'hex')),\n publicKey: Uint8Array.from(Buffer.from(keyRef.kid, 'hex')),\n },\n messages: [data],\n }\n const signature = await bbs.sign({secretKey: privateKey, publicKey, header, messages});\n return signature*/\n } else if (\n // @ts-ignore\n privateKey.type === 'RSA' &&\n (typeof algorithm === 'undefined' || algorithm === 'RS256' || algorithm === 'RS512' || algorithm === 'PS256' || algorithm === 'PS512')\n ) {\n return await this.signRSA(privateKey, data, algorithm ?? 'PS256')\n } else {\n return await super.sign({ keyRef, algorithm, data })\n }\n throw Error(`not_supported: Cannot sign using key of type ${privateKey.type}`)\n }\n\n async verify({\n publicKeyHex,\n type,\n algorithm,\n data,\n signature,\n }: {\n publicKeyHex: string\n type: TKeyType\n algorithm?: string\n data: Uint8Array\n signature: string\n }): Promise<boolean> {\n if (type === 'RSA') {\n return await this.verifyRSA(publicKeyHex, data, algorithm ?? 'PS256', signature)\n }\n throw Error(`KMS verify is not implemented yet for ${type}`)\n }\n\n private asSphereonManagedKeyInfo(args: ManagedKeyInfoArgs): ManagedKeyInfo {\n let key: Partial<ManagedKeyInfo>\n switch (args.type) {\n case KeyType.Bls12381G2:\n key = {\n type: args.type,\n kid: args.alias ?? args.publicKeyHex,\n publicKeyHex: args.publicKeyHex,\n meta: {\n algorithms: ['BLS'],\n },\n }\n break\n case 'Secp256k1': {\n const privateBytes = fromString(args.privateKeyHex.toLowerCase(), 'base16')\n const secp256k1 = new elliptic.ec('secp256k1')\n const keyPair = secp256k1.keyFromPrivate(privateBytes, 'hex')\n const publicKeyHex = keyPair.getPublic(true, 'hex')\n key = {\n type: args.type,\n kid: args.alias ?? publicKeyHex,\n publicKeyHex,\n meta: {\n jwkThumbprint: calculateJwkThumbprint({ jwk: toJwk(publicKeyHex, 'Secp256k1') }),\n algorithms: ['ES256K', 'ES256K-R', 'eth_signTransaction', 'eth_signTypedData', 'eth_signMessage', 'eth_rawSign'],\n },\n }\n break\n }\n case 'Secp256r1': {\n const privateBytes = fromString(args.privateKeyHex.toLowerCase(), 'base16')\n const secp256r1 = new elliptic.ec('p256')\n const keyPair = secp256r1.keyFromPrivate(privateBytes, 'hex')\n const publicKeyHex = keyPair.getPublic(true, 'hex')\n key = {\n type: args.type,\n kid: args.alias ?? publicKeyHex,\n publicKeyHex,\n meta: {\n jwkThumbprint: calculateJwkThumbprint({ jwk: toJwk(publicKeyHex, 'Secp256r1') }),\n algorithms: ['ES256'],\n },\n }\n break\n }\n // @ts-ignore\n case 'RSA': {\n const x509 = args.meta?.x509 as X509Opts\n const privateKeyPEM =\n x509?.privateKeyPEM ?? (args.privateKeyHex.includes('---') ? args.privateKeyHex : hexToPEM(args.privateKeyHex, 'private')) // In case we have x509 opts, the private key hex really was a PEM already (yuck)\n const publicKeyJwk = PEMToJwk(privateKeyPEM, 'public')\n const publicKeyPEM = jwkToPEM(publicKeyJwk, 'public')\n const publicKeyHex = PEMToHex(publicKeyPEM)\n\n const meta = {} as any\n if (x509) {\n meta.x509 = {\n cn: x509.cn ?? args.alias ?? publicKeyHex,\n }\n let certChain: string = x509.certificateChainPEM ?? ''\n if (x509.certificatePEM) {\n if (!certChain.includes(x509.certificatePEM)) {\n certChain = `${x509.certificatePEM}\\n${certChain}`\n }\n }\n if (certChain.length > 0) {\n meta.x509.certificateChainPEM = certChain\n const x5c = pemCertChainTox5c(certChain)\n if (!x509.certificateChainURL) {\n // Do not put the chain in the JWK when the chain is hosted. We do put it in the x509 metadata\n // @ts-ignore\n publicKeyJwk.x5c = x5c\n }\n meta.x509.x5c = x5c\n }\n if (x509.certificateChainURL) {\n // @ts-ignore\n publicKeyJwk.x5u = x509.certificateChainURL\n meta.x509.x5u = x509.certificateChainURL\n }\n }\n\n key = {\n type: args.type,\n kid: args.alias ?? meta?.x509?.cn ?? publicKeyHex,\n publicKeyHex,\n meta: {\n ...meta,\n // todo: could als be DSA etc\n algorithms: ['RS256', 'RS512', 'PS256', 'PS512'],\n publicKeyJwk,\n publicKeyPEM,\n },\n }\n break\n }\n\n default:\n throw Error('not_supported: Key type not supported: ' + args.type)\n }\n return key as ManagedKeyInfo\n }\n\n /**\n * @returns a base64url encoded signature for the `RS256` alg\n */\n private async signRSA(privateKey: ManagedPrivateKey, data: Uint8Array, signingAlgorithm: string): Promise<string> {\n const { hashAlgorithm, scheme } = signAlgorithmToSchemeAndHashAlg(signingAlgorithm)\n const signer = new RSASigner(PEMToJwk(hexToPEM(privateKey.privateKeyHex, 'private'), 'private'), { hashAlgorithm, scheme })\n const signature = await signer.sign(data)\n return signature as string\n }\n\n private async verifyRSA(publicKeyHex: string, data: Uint8Array, signingAlgorithm: string, signature: string) {\n const { hashAlgorithm, scheme } = signAlgorithmToSchemeAndHashAlg(signingAlgorithm)\n const signer = new RSASigner(PEMToJwk(hexToPEM(publicKeyHex, 'public'), 'public'), { hashAlgorithm, scheme })\n return await signer.verify(data, signature)\n }\n\n public async listKeys(): Promise<Array<ManagedKeyInfo>> {\n return (await this.privateKeyStore.list({})).map((privateKey: ManagedPrivateKey) => this.asSphereonManagedKeyInfo(privateKey))\n }\n}\n","import { X509Opts } from '@sphereon/ssi-sdk-ext.key-utils'\nimport { KeyMetadata, TKeyType } from '@veramo/core'\n\nexport { SphereonKeyManagementSystem } from './SphereonKeyManagementSystem'\n\nexport * from '@veramo/kms-local'\n\nexport interface ManagedKeyInfoArgs {\n alias?: string\n type: TKeyType\n privateKeyHex: string\n publicKeyHex?: string\n meta?: ManageKeyInfoMeta | undefined | null\n}\n\nexport interface ManageKeyInfoMeta extends KeyMetadata {\n x509?: X509Opts\n [x: string]: any\n}\nexport enum KeyType {\n Bls12381G2 = 'Bls12381G2',\n}\n"]}
|