@fgv/ts-extras 5.1.0-31 → 5.1.0-33
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/packlets/crypto-utils/index.browser.js +3 -2
- package/dist/packlets/crypto-utils/index.browser.js.map +1 -1
- package/dist/packlets/crypto-utils/keystore/encryptedFilePrivateKeyStorage.js +287 -0
- package/dist/packlets/crypto-utils/keystore/encryptedFilePrivateKeyStorage.js.map +1 -0
- package/dist/packlets/crypto-utils/keystore/index.browser.js +36 -0
- package/dist/packlets/crypto-utils/keystore/index.browser.js.map +1 -0
- package/dist/packlets/crypto-utils/keystore/index.js +2 -0
- package/dist/packlets/crypto-utils/keystore/index.js.map +1 -1
- package/dist/packlets/crypto-utils/keystore/privateKeyStorage.js.map +1 -1
- package/dist/ts-extras.d.ts +153 -3
- package/lib/packlets/crypto-utils/index.browser.d.ts +1 -1
- package/lib/packlets/crypto-utils/index.browser.d.ts.map +1 -1
- package/lib/packlets/crypto-utils/index.browser.js +3 -2
- package/lib/packlets/crypto-utils/index.browser.js.map +1 -1
- package/lib/packlets/crypto-utils/keystore/encryptedFilePrivateKeyStorage.d.ts +148 -0
- package/lib/packlets/crypto-utils/keystore/encryptedFilePrivateKeyStorage.d.ts.map +1 -0
- package/lib/packlets/crypto-utils/keystore/encryptedFilePrivateKeyStorage.js +324 -0
- package/lib/packlets/crypto-utils/keystore/encryptedFilePrivateKeyStorage.js.map +1 -0
- package/lib/packlets/crypto-utils/keystore/index.browser.d.ts +10 -0
- package/lib/packlets/crypto-utils/keystore/index.browser.d.ts.map +1 -0
- package/lib/packlets/crypto-utils/keystore/index.browser.js +76 -0
- package/lib/packlets/crypto-utils/keystore/index.browser.js.map +1 -0
- package/lib/packlets/crypto-utils/keystore/index.d.ts +1 -0
- package/lib/packlets/crypto-utils/keystore/index.d.ts.map +1 -1
- package/lib/packlets/crypto-utils/keystore/index.js +4 -1
- package/lib/packlets/crypto-utils/keystore/index.js.map +1 -1
- package/lib/packlets/crypto-utils/keystore/privateKeyStorage.d.ts +6 -3
- package/lib/packlets/crypto-utils/keystore/privateKeyStorage.d.ts.map +1 -1
- package/lib/packlets/crypto-utils/keystore/privateKeyStorage.js.map +1 -1
- package/package.json +15 -10
|
@@ -26,8 +26,9 @@
|
|
|
26
26
|
export * from './model';
|
|
27
27
|
// Constants
|
|
28
28
|
export { AES_256_KEY_SIZE, DEFAULT_ALGORITHM, ENCRYPTED_FILE_FORMAT, GCM_AUTH_TAG_SIZE, GCM_IV_SIZE } from './constants';
|
|
29
|
-
// KeyStore namespace
|
|
30
|
-
|
|
29
|
+
// KeyStore namespace (browser-safe barrel — omits the Node-only
|
|
30
|
+
// EncryptedFilePrivateKeyStorage so the browser entry stays free of node:crypto)
|
|
31
|
+
import * as KeyStore from './keystore/index.browser';
|
|
31
32
|
export { KeyStore };
|
|
32
33
|
// Converters namespace
|
|
33
34
|
import * as Converters from './converters';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.browser.js","sourceRoot":"","sources":["../../../src/packlets/crypto-utils/index.browser.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,EAAE;AACF,+EAA+E;AAC/E,gFAAgF;AAChF,+EAA+E;AAC/E,4EAA4E;AAC5E,wEAAwE;AACxE,2DAA2D;AAC3D,EAAE;AACF,iFAAiF;AACjF,kDAAkD;AAClD,EAAE;AACF,6EAA6E;AAC7E,2EAA2E;AAC3E,8EAA8E;AAC9E,yEAAyE;AACzE,gFAAgF;AAChF,gFAAgF;AAChF,YAAY;AAEZ;;;;GAIG;AAEH,iCAAiC;AACjC,cAAc,SAAS,CAAC;AAExB,YAAY;AACZ,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,qBAAqB,EACrB,iBAAiB,EACjB,WAAW,EACZ,MAAM,aAAa,CAAC;AAErB,
|
|
1
|
+
{"version":3,"file":"index.browser.js","sourceRoot":"","sources":["../../../src/packlets/crypto-utils/index.browser.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,EAAE;AACF,+EAA+E;AAC/E,gFAAgF;AAChF,+EAA+E;AAC/E,4EAA4E;AAC5E,wEAAwE;AACxE,2DAA2D;AAC3D,EAAE;AACF,iFAAiF;AACjF,kDAAkD;AAClD,EAAE;AACF,6EAA6E;AAC7E,2EAA2E;AAC3E,8EAA8E;AAC9E,yEAAyE;AACzE,gFAAgF;AAChF,gFAAgF;AAChF,YAAY;AAEZ;;;;GAIG;AAEH,iCAAiC;AACjC,cAAc,SAAS,CAAC;AAExB,YAAY;AACZ,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,qBAAqB,EACrB,iBAAiB,EACjB,WAAW,EACZ,MAAM,aAAa,CAAC;AAErB,gEAAgE;AAChE,iFAAiF;AACjF,OAAO,KAAK,QAAQ,MAAM,0BAA0B,CAAC;AACrD,OAAO,EAAE,QAAQ,EAAE,CAAC;AAEpB,uBAAuB;AACvB,OAAO,KAAK,UAAU,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,CAAC;AAEtB,6BAA6B;AAC7B,OAAO,EAAE,wBAAwB,EAAmC,MAAM,4BAA4B,CAAC;AAEvG,8DAA8D;AAC9D,OAAO,EAA2B,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAE3F,8DAA8D;AAC9D,4DAA4D;AAE5D,yBAAyB;AACzB,OAAO,EACL,mBAAmB,EACnB,WAAW,EACX,UAAU,EAEV,QAAQ,EACR,cAAc,EACf,MAAM,iBAAiB,CAAC;AAEzB,yBAAyB;AACzB,OAAO,EACL,8BAA8B,EAC9B,gCAAgC,EAChC,wBAAwB,EACxB,wBAAwB,EACzB,MAAM,eAAe,CAAC;AAEvB,qFAAqF;AACrF,uFAAuF;AACvF,OAAO,EAAE,YAAY,EAAmB,MAAM,gBAAgB,CAAC","sourcesContent":["// Copyright (c) 2024 Erik Fortune\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\n/**\n * Crypto utilities for encrypted file handling and key management (browser version).\n * Note: For browser crypto provider, use \\@fgv/ts-web-extras.\n * @packageDocumentation\n */\n\n// Re-export all types from model\nexport * from './model';\n\n// Constants\nexport {\n AES_256_KEY_SIZE,\n DEFAULT_ALGORITHM,\n ENCRYPTED_FILE_FORMAT,\n GCM_AUTH_TAG_SIZE,\n GCM_IV_SIZE\n} from './constants';\n\n// KeyStore namespace (browser-safe barrel — omits the Node-only\n// EncryptedFilePrivateKeyStorage so the browser entry stays free of node:crypto)\nimport * as KeyStore from './keystore/index.browser';\nexport { KeyStore };\n\n// Converters namespace\nimport * as Converters from './converters';\nexport { Converters };\n\n// Direct encryption provider\nexport { DirectEncryptionProvider, IDirectEncryptionProviderParams } from './directEncryptionProvider';\n\n// WebCrypto parameter table for asymmetric keypair algorithms\nexport { IKeyPairAlgorithmParams, keyPairAlgorithmParams } from './keyPairAlgorithmParams';\n\n// Note: NodeCryptoProvider is NOT exported in browser version\n// Use BrowserCryptoProvider from @fgv/ts-web-extras instead\n\n// Encrypted file helpers\nexport {\n createEncryptedFile,\n decryptFile,\n fromBase64,\n ICreateEncryptedFileParams,\n toBase64,\n tryDecryptFile\n} from './encryptedFile';\n\n// Multibase/SPKI helpers\nexport {\n exportPublicKeyAsMultibaseSpki,\n importPublicKeyFromMultibaseSpki,\n multibaseBase64UrlDecode,\n multibaseBase64UrlEncode\n} from './spkiHelpers';\n\n// HPKE base mode (RFC 9180) — DHKEM(X25519, HKDF-SHA256) + HKDF-SHA256 + AES-256-GCM\n// hpkeProvider.ts has no Node-specific imports and is safe in the browser entry point.\nexport { HpkeProvider, IHpkeSealResult } from './hpkeProvider';\n"]}
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
// Copyright (c) 2026 Erik Fortune
|
|
2
|
+
//
|
|
3
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
// in the Software without restriction, including without limitation the rights
|
|
6
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
// furnished to do so, subject to the following conditions:
|
|
9
|
+
//
|
|
10
|
+
// The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
// copies or substantial portions of the Software.
|
|
12
|
+
//
|
|
13
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
// SOFTWARE.
|
|
20
|
+
import * as crypto from 'crypto';
|
|
21
|
+
import { captureAsyncResult, captureResult, Converters, fail, succeed } from '@fgv/ts-utils';
|
|
22
|
+
import { FileTree } from '@fgv/ts-json-base';
|
|
23
|
+
import { createEncryptedFile, tryDecryptFile } from '../encryptedFile';
|
|
24
|
+
import { keyPairAlgorithmParams } from '../keyPairAlgorithmParams';
|
|
25
|
+
import * as Constants from '../constants';
|
|
26
|
+
import { jsonWebKeyShape, keyPairAlgorithm } from './converters';
|
|
27
|
+
const envelopeConverter = Converters.object({
|
|
28
|
+
algorithm: keyPairAlgorithm,
|
|
29
|
+
jwk: Converters.string
|
|
30
|
+
});
|
|
31
|
+
// WebCrypto key usages that only ever apply to a public key. Filtering these
|
|
32
|
+
// out of the keypair usages yields the usages valid for the private half, which
|
|
33
|
+
// `crypto.subtle.importKey` requires when re-importing a private JWK.
|
|
34
|
+
const PUBLIC_ONLY_USAGES = ['verify', 'encrypt', 'wrapKey'];
|
|
35
|
+
// Safe-filename id production. The keystore mints UUIDv4 handles, which match;
|
|
36
|
+
// arbitrary consumer-supplied ids that could escape the storage directory are
|
|
37
|
+
// rejected rather than silently mangled.
|
|
38
|
+
const SAFE_ID = /^[A-Za-z0-9._-]+$/;
|
|
39
|
+
const FILE_SUFFIX = '.json';
|
|
40
|
+
/**
|
|
41
|
+
* {@link CryptoUtils.KeyStore.IPrivateKeyStorage | IPrivateKeyStorage}
|
|
42
|
+
* implementation that persists each private key as its own AES-256-GCM-encrypted
|
|
43
|
+
* file in a directory. The file content is the key's JWK, encrypted with a
|
|
44
|
+
* consumer-supplied 32-byte key via the supplied
|
|
45
|
+
* {@link CryptoUtils.ICryptoProvider | crypto provider}.
|
|
46
|
+
*
|
|
47
|
+
* `supportsNonExtractable` is `false`: persisting to disk requires exporting the
|
|
48
|
+
* private key to JWK, which only works for `extractable: true` keys. The
|
|
49
|
+
* keystore generates extractable keys when a backend reports `false` here.
|
|
50
|
+
*
|
|
51
|
+
* I/O goes through the {@link FileTree.FileTree | FileTree} abstraction (default
|
|
52
|
+
* `FsTree`), so the same implementation works against an in-memory tree (tests)
|
|
53
|
+
* or any other Node-compatible backend.
|
|
54
|
+
*
|
|
55
|
+
* This backend is **Node-only**: it round-trips private keys through
|
|
56
|
+
* `node:crypto` (`crypto.webcrypto.subtle`), so it is intentionally excluded
|
|
57
|
+
* from the browser entry point. Browser consumers should use
|
|
58
|
+
* `IdbPrivateKeyStorage` from `@fgv/ts-web-extras` instead.
|
|
59
|
+
*
|
|
60
|
+
* Single-process assumption: there is no inter-process locking. Concurrent
|
|
61
|
+
* writers to the same directory may race.
|
|
62
|
+
*
|
|
63
|
+
* @public
|
|
64
|
+
*/
|
|
65
|
+
export class EncryptedFilePrivateKeyStorage {
|
|
66
|
+
constructor(directory, encryptionKey, cryptoProvider) {
|
|
67
|
+
/**
|
|
68
|
+
* `false` — disk persistence round-trips via JWK, which requires extractable
|
|
69
|
+
* keys.
|
|
70
|
+
*/
|
|
71
|
+
this.supportsNonExtractable = false;
|
|
72
|
+
this._directory = directory;
|
|
73
|
+
// Clone so the instance holds an immutable snapshot — callers that later
|
|
74
|
+
// reuse or zero their buffer must not be able to mutate our key.
|
|
75
|
+
this._encryptionKey = Uint8Array.from(encryptionKey);
|
|
76
|
+
this._cryptoProvider = cryptoProvider;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Creates a new {@link CryptoUtils.KeyStore.EncryptedFilePrivateKeyStorage}.
|
|
80
|
+
* @param params - {@link CryptoUtils.KeyStore.IEncryptedFilePrivateKeyStorageCreateParams}.
|
|
81
|
+
* @returns `Success` with the new instance, or `Failure` if the encryption
|
|
82
|
+
* key is the wrong size or the storage directory cannot be opened.
|
|
83
|
+
*/
|
|
84
|
+
static create(params) {
|
|
85
|
+
const { directory, encryptionKey, cryptoProvider, tree } = params;
|
|
86
|
+
if (encryptionKey.length !== Constants.AES_256_KEY_SIZE) {
|
|
87
|
+
return fail(`EncryptedFilePrivateKeyStorage: encryptionKey must be ${Constants.AES_256_KEY_SIZE} bytes, got ${encryptionKey.length}`);
|
|
88
|
+
}
|
|
89
|
+
if (tree !== undefined) {
|
|
90
|
+
return succeed(new EncryptedFilePrivateKeyStorage(tree, encryptionKey, cryptoProvider));
|
|
91
|
+
}
|
|
92
|
+
return FileTree.forFilesystem({ mutable: true })
|
|
93
|
+
.onSuccess((ft) => ft.getDirectory(directory))
|
|
94
|
+
.withErrorFormat((msg) => `EncryptedFilePrivateKeyStorage: failed to open '${directory}': ${msg}`)
|
|
95
|
+
.onSuccess((dir) => succeed(new EncryptedFilePrivateKeyStorage(dir, encryptionKey, cryptoProvider)));
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Stores `key` under `id` as an encrypted JWK file.
|
|
99
|
+
* @param id - Storage handle. Must be a safe filename token
|
|
100
|
+
* (`[A-Za-z0-9._-]+`, not `.`/`..`).
|
|
101
|
+
* @param key - The extractable private `CryptoKey` to persist.
|
|
102
|
+
*/
|
|
103
|
+
async store(id, key) {
|
|
104
|
+
return this._validateKeyToStore(id, key).thenOnSuccess(({ fileName, algorithm }) => this._encryptAndWrite(algorithm, key, id, fileName));
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Loads the private key stored under `id`, decrypting and re-importing it from
|
|
108
|
+
* JWK.
|
|
109
|
+
* @param id - Storage handle.
|
|
110
|
+
*/
|
|
111
|
+
async load(id) {
|
|
112
|
+
return this._fileNameFor(id)
|
|
113
|
+
.onSuccess((fileName) => this._findFile(fileName))
|
|
114
|
+
.onSuccess((file) => file === undefined ? fail(`key not found: '${id}'`) : succeed(file))
|
|
115
|
+
.thenOnSuccess((file) => this._decryptEnvelope(file, id))
|
|
116
|
+
.thenOnSuccess((envelope) => this._importPrivateKey(envelope, id));
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Deletes the entry stored under `id`. Missing ids fail (the read path is
|
|
120
|
+
* keystore-driven and never asks to delete an id it did not store).
|
|
121
|
+
* @param id - Storage handle.
|
|
122
|
+
*/
|
|
123
|
+
async delete(id) {
|
|
124
|
+
const fileResult = this._fileNameFor(id).onSuccess((fileName) => this._findFile(fileName).onSuccess((file) => succeed({ fileName, file })));
|
|
125
|
+
if (fileResult.isFailure()) {
|
|
126
|
+
return fail(fileResult.message);
|
|
127
|
+
}
|
|
128
|
+
if (fileResult.value.file === undefined) {
|
|
129
|
+
return fail(`key not found: '${id}'`);
|
|
130
|
+
}
|
|
131
|
+
/* c8 ignore next 3 - defensive: directory items from read-only adapters lack mutation methods */
|
|
132
|
+
if (!FileTree.isMutableDirectoryItem(this._directory)) {
|
|
133
|
+
return fail(`failed to delete private key '${id}': storage directory is not mutable`);
|
|
134
|
+
}
|
|
135
|
+
return Promise.resolve(this._directory
|
|
136
|
+
.deleteChild(fileResult.value.fileName)
|
|
137
|
+
.withErrorFormat((msg) => `failed to delete private key '${id}': ${msg}`)
|
|
138
|
+
.onSuccess(() => succeed(id)));
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Lists every stored id.
|
|
142
|
+
*/
|
|
143
|
+
async list() {
|
|
144
|
+
return Promise.resolve(this._directory.getChildren().onSuccess((children) => {
|
|
145
|
+
const ids = children
|
|
146
|
+
.filter((child) => child.type === 'file' && child.name.endsWith(FILE_SUFFIX))
|
|
147
|
+
.map((child) => child.name.slice(0, -FILE_SUFFIX.length));
|
|
148
|
+
return succeed(ids);
|
|
149
|
+
}));
|
|
150
|
+
}
|
|
151
|
+
_fileNameFor(id) {
|
|
152
|
+
if (id === '.' || id === '..' || !SAFE_ID.test(id)) {
|
|
153
|
+
return fail(`invalid storage id '${id}': must match ${SAFE_ID.source}`);
|
|
154
|
+
}
|
|
155
|
+
return succeed(`${id}${FILE_SUFFIX}`);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Validates the synchronous preconditions for a store: the id is filename-safe,
|
|
159
|
+
* the key is actually a private key, and its algorithm is one we support.
|
|
160
|
+
* Returns the resolved filename and algorithm so the async pipeline can run
|
|
161
|
+
* without re-deriving them.
|
|
162
|
+
*/
|
|
163
|
+
_validateKeyToStore(id, key) {
|
|
164
|
+
return this._fileNameFor(id).onSuccess((fileName) => {
|
|
165
|
+
if (key.type !== 'private') {
|
|
166
|
+
return fail(`failed to store private key '${id}': expected a private key, got '${key.type}'`);
|
|
167
|
+
}
|
|
168
|
+
return this._algorithmOf(key)
|
|
169
|
+
.withErrorFormat((msg) => `failed to store private key '${id}': ${msg}`)
|
|
170
|
+
.onSuccess((algorithm) => succeed({ fileName, algorithm }));
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Exports `key` to JWK, wraps it in the stored envelope, encrypts it with
|
|
175
|
+
* AES-256-GCM, and writes the resulting file as serialized JSON to `fileName`.
|
|
176
|
+
* Returns the stored `id` on success.
|
|
177
|
+
*/
|
|
178
|
+
async _encryptAndWrite(algorithm, key, id, fileName) {
|
|
179
|
+
return captureAsyncResult(() => crypto.webcrypto.subtle.exportKey('jwk', key))
|
|
180
|
+
.withErrorFormat((msg) => `failed to export private key '${id}' to JWK: ${msg}`)
|
|
181
|
+
.onSuccess((jwk) => succeed({ algorithm, jwk: JSON.stringify(jwk) }))
|
|
182
|
+
.thenOnSuccess(async (envelope) => (await createEncryptedFile({
|
|
183
|
+
content: envelope,
|
|
184
|
+
secretName: id,
|
|
185
|
+
key: this._encryptionKey,
|
|
186
|
+
cryptoProvider: this._cryptoProvider
|
|
187
|
+
})).withErrorFormat((msg) => `failed to encrypt private key '${id}': ${msg}`))
|
|
188
|
+
.onSuccess((encrypted) => this._writeFile(fileName, JSON.stringify(encrypted)))
|
|
189
|
+
.onSuccess(() => succeed(id));
|
|
190
|
+
}
|
|
191
|
+
_algorithmOf(key) {
|
|
192
|
+
const alg = key.algorithm;
|
|
193
|
+
switch (alg.name) {
|
|
194
|
+
case 'ECDSA':
|
|
195
|
+
case 'ECDH': {
|
|
196
|
+
const curve = alg.namedCurve;
|
|
197
|
+
if (curve !== 'P-256') {
|
|
198
|
+
return fail(`unsupported ${alg.name} curve '${curve}' (only P-256 is supported)`);
|
|
199
|
+
}
|
|
200
|
+
return succeed(alg.name === 'ECDSA' ? 'ecdsa-p256' : 'ecdh-p256');
|
|
201
|
+
}
|
|
202
|
+
case 'RSA-OAEP': {
|
|
203
|
+
// Only the hash affects the JWK re-import params, so it is the field
|
|
204
|
+
// that must match; the modulus length is recovered from the key data.
|
|
205
|
+
const hash = alg.hash.name;
|
|
206
|
+
if (hash !== 'SHA-256') {
|
|
207
|
+
return fail(`unsupported RSA-OAEP hash '${hash}' (only SHA-256 is supported)`);
|
|
208
|
+
}
|
|
209
|
+
return succeed('rsa-oaep-2048');
|
|
210
|
+
}
|
|
211
|
+
case 'Ed25519':
|
|
212
|
+
return succeed('ed25519');
|
|
213
|
+
case 'X25519':
|
|
214
|
+
return succeed('x25519');
|
|
215
|
+
default:
|
|
216
|
+
return fail(`unsupported key algorithm '${alg.name}'`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
_findFile(fileName) {
|
|
220
|
+
return this._directory.getChildren().onSuccess((children) => {
|
|
221
|
+
const found = children.find((child) => child.type === 'file' && child.name === fileName);
|
|
222
|
+
return succeed(found);
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
_writeFile(fileName, text) {
|
|
226
|
+
return this._findFile(fileName).onSuccess((existing) => {
|
|
227
|
+
if (existing !== undefined) {
|
|
228
|
+
/* c8 ignore next 3 - defensive: file items from read-only adapters lack mutation methods */
|
|
229
|
+
if (!FileTree.isMutableFileItem(existing)) {
|
|
230
|
+
return fail(`${existing.absolutePath}: not mutable`);
|
|
231
|
+
}
|
|
232
|
+
return existing.setRawContents(text);
|
|
233
|
+
}
|
|
234
|
+
/* c8 ignore next 3 - defensive: directory items from read-only adapters lack mutation methods */
|
|
235
|
+
if (!FileTree.isMutableDirectoryItem(this._directory)) {
|
|
236
|
+
return fail(`${this._directory.absolutePath}: not mutable`);
|
|
237
|
+
}
|
|
238
|
+
return this._directory.createChildFile(fileName, text).onSuccess(() => succeed(text));
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Reads `file`, decrypts the AES-256-GCM envelope, and validates it into the
|
|
243
|
+
* typed `IStoredPrivateKeyEnvelope`. Read, decrypt, and shape failures
|
|
244
|
+
* all surface as a decrypt failure for `id`.
|
|
245
|
+
*/
|
|
246
|
+
async _decryptEnvelope(file, id) {
|
|
247
|
+
return file
|
|
248
|
+
.getContents()
|
|
249
|
+
.thenOnSuccess((json) => tryDecryptFile(json, this._encryptionKey, this._cryptoProvider))
|
|
250
|
+
.onSuccess((decrypted) => envelopeConverter.convert(decrypted))
|
|
251
|
+
.withErrorFormat((msg) => `failed to decrypt private key '${id}': ${msg}`);
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Parses and shape-validates the stored JWK, then re-imports it as a private
|
|
255
|
+
* `CryptoKey` for the envelope's algorithm. The WebCrypto JWK-import algorithm
|
|
256
|
+
* descriptor is shared between public and private keys for every supported
|
|
257
|
+
* algorithm, so `IKeyPairAlgorithmParams.importPublicKey` is reused here;
|
|
258
|
+
* the public/private distinction is carried by the requested `usages`.
|
|
259
|
+
*/
|
|
260
|
+
async _importPrivateKey(envelope, id) {
|
|
261
|
+
const params = keyPairAlgorithmParams[envelope.algorithm];
|
|
262
|
+
return captureResult(() => JSON.parse(envelope.jwk))
|
|
263
|
+
.onSuccess((parsed) => jsonWebKeyShape.validate(parsed))
|
|
264
|
+
.withErrorFormat((msg) => `malformed JWK: ${msg}`)
|
|
265
|
+
.thenOnSuccess((jwk) => captureAsyncResult(() => crypto.webcrypto.subtle.importKey('jwk', jwk, params.importPublicKey, true, this._importUsagesFor(jwk, params))))
|
|
266
|
+
.withErrorFormat((msg) => `failed to import private key '${id}': ${msg}`);
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Computes the key usages to request when re-importing a stored private key.
|
|
270
|
+
* WebCrypto rejects `importKey` if the requested usages include operations
|
|
271
|
+
* absent from the JWK's `key_ops`, so a key originally created with a narrower
|
|
272
|
+
* usage set than the algorithm default (e.g. an ECDH key with only
|
|
273
|
+
* `deriveBits`) would fail to load against the algorithm-wide defaults.
|
|
274
|
+
* Intersect the algorithm's private usages with the JWK's recorded `key_ops`
|
|
275
|
+
* so we request exactly the operations the stored key actually supports;
|
|
276
|
+
* fall back to the algorithm's private usages when `key_ops` is absent.
|
|
277
|
+
*/
|
|
278
|
+
_importUsagesFor(jwk, params) {
|
|
279
|
+
const privateUsages = params.keyPairUsages.filter((usage) => !PUBLIC_ONLY_USAGES.includes(usage));
|
|
280
|
+
const keyOps = jwk.key_ops;
|
|
281
|
+
if (keyOps === undefined) {
|
|
282
|
+
return [...privateUsages];
|
|
283
|
+
}
|
|
284
|
+
return privateUsages.filter((usage) => keyOps.includes(usage));
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
//# sourceMappingURL=encryptedFilePrivateKeyStorage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encryptedFilePrivateKeyStorage.js","sourceRoot":"","sources":["../../../../src/packlets/crypto-utils/keystore/encryptedFilePrivateKeyStorage.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,EAAE;AACF,+EAA+E;AAC/E,gFAAgF;AAChF,+EAA+E;AAC/E,4EAA4E;AAC5E,wEAAwE;AACxE,2DAA2D;AAC3D,EAAE;AACF,iFAAiF;AACjF,kDAAkD;AAClD,EAAE;AACF,6EAA6E;AAC7E,2EAA2E;AAC3E,8EAA8E;AAC9E,yEAAyE;AACzE,gFAAgF;AAChF,gFAAgF;AAChF,YAAY;AAEZ,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,EACL,kBAAkB,EAClB,aAAa,EAEb,UAAU,EACV,IAAI,EAEJ,OAAO,EACR,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,QAAQ,EAAc,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QACD,OAAO,aAAa,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC;IACjE,CAAC;CACF","sourcesContent":["// Copyright (c) 2026 Erik Fortune\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\nimport * as crypto from 'crypto';\nimport {\n captureAsyncResult,\n captureResult,\n Converter,\n Converters,\n fail,\n Result,\n succeed\n} from '@fgv/ts-utils';\nimport { FileTree, JsonObject } from '@fgv/ts-json-base';\nimport { createEncryptedFile, tryDecryptFile } from '../encryptedFile';\nimport { IKeyPairAlgorithmParams, keyPairAlgorithmParams } from '../keyPairAlgorithmParams';\nimport { ICryptoProvider, KeyPairAlgorithm } from '../model';\nimport * as Constants from '../constants';\nimport { jsonWebKeyShape, keyPairAlgorithm } from './converters';\nimport { IPrivateKeyStorage } from './privateKeyStorage';\n\n/**\n * Parameters for {@link CryptoUtils.KeyStore.EncryptedFilePrivateKeyStorage.create}.\n * @public\n */\nexport interface IEncryptedFilePrivateKeyStorageCreateParams {\n /**\n * Filesystem path to the directory that holds the encrypted private-key\n * files. Used only when {@link CryptoUtils.KeyStore.IEncryptedFilePrivateKeyStorageCreateParams.tree}\n * is omitted (the default `FsTree` backing). The directory must already\n * exist.\n */\n readonly directory: string;\n\n /**\n * Raw AES-256-GCM key (32 bytes) used to encrypt each file's JWK content.\n * Consumer-supplied and decoupled from the keystore's password lifecycle —\n * derive it however the application sees fit (typically the same\n * password-derived key material the keystore vault uses).\n */\n readonly encryptionKey: Uint8Array;\n\n /**\n * {@link CryptoUtils.ICryptoProvider | Crypto provider} used for the\n * AES-256-GCM encrypt/decrypt of each file's contents.\n */\n readonly cryptoProvider: ICryptoProvider;\n\n /**\n * Optional {@link FileTree.IFileTreeDirectoryItem | FileTree directory}\n * override. When supplied it is used as the storage directory directly and\n * {@link CryptoUtils.KeyStore.IEncryptedFilePrivateKeyStorageCreateParams.directory} is ignored —\n * pass an in-memory tree for tests, or another Node-compatible backend. When\n * omitted, a mutable `FsTree` rooted at `directory` is used. (This backend is\n * Node-only — it round-trips keys through `node:crypto` — so a browser file\n * tree is not a supported target.)\n */\n readonly tree?: FileTree.IFileTreeDirectoryItem;\n}\n\n/**\n * Decrypted on-disk envelope for a single stored private key. The JWK is held\n * as a serialized JSON string so the whole envelope is a flat record of\n * strings, which round-trips through {@link CryptoUtils.createEncryptedFile}\n * without ambiguity.\n */\ninterface IStoredPrivateKeyEnvelope {\n readonly algorithm: KeyPairAlgorithm;\n readonly jwk: string;\n}\n\nconst envelopeConverter: Converter<IStoredPrivateKeyEnvelope> = Converters.object<IStoredPrivateKeyEnvelope>({\n algorithm: keyPairAlgorithm,\n jwk: Converters.string\n});\n\n// WebCrypto key usages that only ever apply to a public key. Filtering these\n// out of the keypair usages yields the usages valid for the private half, which\n// `crypto.subtle.importKey` requires when re-importing a private JWK.\nconst PUBLIC_ONLY_USAGES: ReadonlyArray<KeyUsage> = ['verify', 'encrypt', 'wrapKey'];\n\n// Safe-filename id production. The keystore mints UUIDv4 handles, which match;\n// arbitrary consumer-supplied ids that could escape the storage directory are\n// rejected rather than silently mangled.\nconst SAFE_ID: RegExp = /^[A-Za-z0-9._-]+$/;\n\nconst FILE_SUFFIX: string = '.json';\n\n/**\n * {@link CryptoUtils.KeyStore.IPrivateKeyStorage | IPrivateKeyStorage}\n * implementation that persists each private key as its own AES-256-GCM-encrypted\n * file in a directory. The file content is the key's JWK, encrypted with a\n * consumer-supplied 32-byte key via the supplied\n * {@link CryptoUtils.ICryptoProvider | crypto provider}.\n *\n * `supportsNonExtractable` is `false`: persisting to disk requires exporting the\n * private key to JWK, which only works for `extractable: true` keys. The\n * keystore generates extractable keys when a backend reports `false` here.\n *\n * I/O goes through the {@link FileTree.FileTree | FileTree} abstraction (default\n * `FsTree`), so the same implementation works against an in-memory tree (tests)\n * or any other Node-compatible backend.\n *\n * This backend is **Node-only**: it round-trips private keys through\n * `node:crypto` (`crypto.webcrypto.subtle`), so it is intentionally excluded\n * from the browser entry point. Browser consumers should use\n * `IdbPrivateKeyStorage` from `@fgv/ts-web-extras` instead.\n *\n * Single-process assumption: there is no inter-process locking. Concurrent\n * writers to the same directory may race.\n *\n * @public\n */\nexport class EncryptedFilePrivateKeyStorage implements IPrivateKeyStorage {\n /**\n * `false` — disk persistence round-trips via JWK, which requires extractable\n * keys.\n */\n public readonly supportsNonExtractable: false = false;\n\n private readonly _directory: FileTree.IFileTreeDirectoryItem;\n private readonly _encryptionKey: Uint8Array;\n private readonly _cryptoProvider: ICryptoProvider;\n\n private constructor(\n directory: FileTree.IFileTreeDirectoryItem,\n encryptionKey: Uint8Array,\n cryptoProvider: ICryptoProvider\n ) {\n this._directory = directory;\n // Clone so the instance holds an immutable snapshot — callers that later\n // reuse or zero their buffer must not be able to mutate our key.\n this._encryptionKey = Uint8Array.from(encryptionKey);\n this._cryptoProvider = cryptoProvider;\n }\n\n /**\n * Creates a new {@link CryptoUtils.KeyStore.EncryptedFilePrivateKeyStorage}.\n * @param params - {@link CryptoUtils.KeyStore.IEncryptedFilePrivateKeyStorageCreateParams}.\n * @returns `Success` with the new instance, or `Failure` if the encryption\n * key is the wrong size or the storage directory cannot be opened.\n */\n public static create(\n params: IEncryptedFilePrivateKeyStorageCreateParams\n ): Result<EncryptedFilePrivateKeyStorage> {\n const { directory, encryptionKey, cryptoProvider, tree } = params;\n if (encryptionKey.length !== Constants.AES_256_KEY_SIZE) {\n return fail(\n `EncryptedFilePrivateKeyStorage: encryptionKey must be ${Constants.AES_256_KEY_SIZE} bytes, got ${encryptionKey.length}`\n );\n }\n if (tree !== undefined) {\n return succeed(new EncryptedFilePrivateKeyStorage(tree, encryptionKey, cryptoProvider));\n }\n return FileTree.forFilesystem({ mutable: true })\n .onSuccess((ft) => ft.getDirectory(directory))\n .withErrorFormat((msg) => `EncryptedFilePrivateKeyStorage: failed to open '${directory}': ${msg}`)\n .onSuccess((dir) => succeed(new EncryptedFilePrivateKeyStorage(dir, encryptionKey, cryptoProvider)));\n }\n\n /**\n * Stores `key` under `id` as an encrypted JWK file.\n * @param id - Storage handle. Must be a safe filename token\n * (`[A-Za-z0-9._-]+`, not `.`/`..`).\n * @param key - The extractable private `CryptoKey` to persist.\n */\n public async store(id: string, key: CryptoKey): Promise<Result<string>> {\n return this._validateKeyToStore(id, key).thenOnSuccess(({ fileName, algorithm }) =>\n this._encryptAndWrite(algorithm, key, id, fileName)\n );\n }\n\n /**\n * Loads the private key stored under `id`, decrypting and re-importing it from\n * JWK.\n * @param id - Storage handle.\n */\n public async load(id: string): Promise<Result<CryptoKey>> {\n return this._fileNameFor(id)\n .onSuccess((fileName) => this._findFile(fileName))\n .onSuccess((file) =>\n file === undefined ? fail<FileTree.IFileTreeFileItem>(`key not found: '${id}'`) : succeed(file)\n )\n .thenOnSuccess((file) => this._decryptEnvelope(file, id))\n .thenOnSuccess((envelope) => this._importPrivateKey(envelope, id));\n }\n\n /**\n * Deletes the entry stored under `id`. Missing ids fail (the read path is\n * keystore-driven and never asks to delete an id it did not store).\n * @param id - Storage handle.\n */\n public async delete(id: string): Promise<Result<string>> {\n const fileResult = this._fileNameFor(id).onSuccess((fileName) =>\n this._findFile(fileName).onSuccess((file) => succeed({ fileName, file }))\n );\n if (fileResult.isFailure()) {\n return fail(fileResult.message);\n }\n if (fileResult.value.file === undefined) {\n return fail(`key not found: '${id}'`);\n }\n /* c8 ignore next 3 - defensive: directory items from read-only adapters lack mutation methods */\n if (!FileTree.isMutableDirectoryItem(this._directory)) {\n return fail(`failed to delete private key '${id}': storage directory is not mutable`);\n }\n return Promise.resolve(\n this._directory\n .deleteChild(fileResult.value.fileName)\n .withErrorFormat((msg) => `failed to delete private key '${id}': ${msg}`)\n .onSuccess(() => succeed(id))\n );\n }\n\n /**\n * Lists every stored id.\n */\n public async list(): Promise<Result<readonly string[]>> {\n return Promise.resolve(\n this._directory.getChildren().onSuccess((children) => {\n const ids = children\n .filter((child) => child.type === 'file' && child.name.endsWith(FILE_SUFFIX))\n .map((child) => child.name.slice(0, -FILE_SUFFIX.length));\n return succeed(ids);\n })\n );\n }\n\n private _fileNameFor(id: string): Result<string> {\n if (id === '.' || id === '..' || !SAFE_ID.test(id)) {\n return fail(`invalid storage id '${id}': must match ${SAFE_ID.source}`);\n }\n return succeed(`${id}${FILE_SUFFIX}`);\n }\n\n /**\n * Validates the synchronous preconditions for a store: the id is filename-safe,\n * the key is actually a private key, and its algorithm is one we support.\n * Returns the resolved filename and algorithm so the async pipeline can run\n * without re-deriving them.\n */\n private _validateKeyToStore(\n id: string,\n key: CryptoKey\n ): Result<{ fileName: string; algorithm: KeyPairAlgorithm }> {\n return this._fileNameFor(id).onSuccess((fileName) => {\n if (key.type !== 'private') {\n return fail(`failed to store private key '${id}': expected a private key, got '${key.type}'`);\n }\n return this._algorithmOf(key)\n .withErrorFormat((msg) => `failed to store private key '${id}': ${msg}`)\n .onSuccess((algorithm) => succeed({ fileName, algorithm }));\n });\n }\n\n /**\n * Exports `key` to JWK, wraps it in the stored envelope, encrypts it with\n * AES-256-GCM, and writes the resulting file as serialized JSON to `fileName`.\n * Returns the stored `id` on success.\n */\n private async _encryptAndWrite(\n algorithm: KeyPairAlgorithm,\n key: CryptoKey,\n id: string,\n fileName: string\n ): Promise<Result<string>> {\n return captureAsyncResult(() => crypto.webcrypto.subtle.exportKey('jwk', key))\n .withErrorFormat((msg) => `failed to export private key '${id}' to JWK: ${msg}`)\n .onSuccess<JsonObject>((jwk) => succeed({ algorithm, jwk: JSON.stringify(jwk) }))\n .thenOnSuccess(async (envelope) =>\n (\n await createEncryptedFile({\n content: envelope,\n secretName: id,\n key: this._encryptionKey,\n cryptoProvider: this._cryptoProvider\n })\n ).withErrorFormat((msg) => `failed to encrypt private key '${id}': ${msg}`)\n )\n .onSuccess((encrypted) => this._writeFile(fileName, JSON.stringify(encrypted)))\n .onSuccess(() => succeed(id));\n }\n\n private _algorithmOf(key: CryptoKey): Result<KeyPairAlgorithm> {\n const alg = key.algorithm;\n switch (alg.name) {\n case 'ECDSA':\n case 'ECDH': {\n const curve = (alg as EcKeyAlgorithm).namedCurve;\n if (curve !== 'P-256') {\n return fail(`unsupported ${alg.name} curve '${curve}' (only P-256 is supported)`);\n }\n return succeed(alg.name === 'ECDSA' ? 'ecdsa-p256' : 'ecdh-p256');\n }\n case 'RSA-OAEP': {\n // Only the hash affects the JWK re-import params, so it is the field\n // that must match; the modulus length is recovered from the key data.\n const hash = (alg as RsaHashedKeyAlgorithm).hash.name;\n if (hash !== 'SHA-256') {\n return fail(`unsupported RSA-OAEP hash '${hash}' (only SHA-256 is supported)`);\n }\n return succeed('rsa-oaep-2048');\n }\n case 'Ed25519':\n return succeed('ed25519');\n case 'X25519':\n return succeed('x25519');\n default:\n return fail(`unsupported key algorithm '${alg.name}'`);\n }\n }\n\n private _findFile(fileName: string): Result<FileTree.IFileTreeFileItem | undefined> {\n return this._directory.getChildren().onSuccess((children) => {\n const found = children.find((child) => child.type === 'file' && child.name === fileName);\n return succeed(found as FileTree.IFileTreeFileItem | undefined);\n });\n }\n\n private _writeFile(fileName: string, text: string): Result<string> {\n return this._findFile(fileName).onSuccess((existing) => {\n if (existing !== undefined) {\n /* c8 ignore next 3 - defensive: file items from read-only adapters lack mutation methods */\n if (!FileTree.isMutableFileItem(existing)) {\n return fail(`${existing.absolutePath}: not mutable`);\n }\n return existing.setRawContents(text);\n }\n /* c8 ignore next 3 - defensive: directory items from read-only adapters lack mutation methods */\n if (!FileTree.isMutableDirectoryItem(this._directory)) {\n return fail(`${this._directory.absolutePath}: not mutable`);\n }\n return this._directory.createChildFile(fileName, text).onSuccess(() => succeed(text));\n });\n }\n\n /**\n * Reads `file`, decrypts the AES-256-GCM envelope, and validates it into the\n * typed `IStoredPrivateKeyEnvelope`. Read, decrypt, and shape failures\n * all surface as a decrypt failure for `id`.\n */\n private async _decryptEnvelope(\n file: FileTree.IFileTreeFileItem,\n id: string\n ): Promise<Result<IStoredPrivateKeyEnvelope>> {\n return file\n .getContents()\n .thenOnSuccess((json) => tryDecryptFile(json, this._encryptionKey, this._cryptoProvider))\n .onSuccess((decrypted) => envelopeConverter.convert(decrypted))\n .withErrorFormat((msg) => `failed to decrypt private key '${id}': ${msg}`);\n }\n\n /**\n * Parses and shape-validates the stored JWK, then re-imports it as a private\n * `CryptoKey` for the envelope's algorithm. The WebCrypto JWK-import algorithm\n * descriptor is shared between public and private keys for every supported\n * algorithm, so `IKeyPairAlgorithmParams.importPublicKey` is reused here;\n * the public/private distinction is carried by the requested `usages`.\n */\n private async _importPrivateKey(\n envelope: IStoredPrivateKeyEnvelope,\n id: string\n ): Promise<Result<CryptoKey>> {\n const params = keyPairAlgorithmParams[envelope.algorithm];\n return captureResult(() => JSON.parse(envelope.jwk) as unknown)\n .onSuccess((parsed) => jsonWebKeyShape.validate(parsed))\n .withErrorFormat((msg) => `malformed JWK: ${msg}`)\n .thenOnSuccess((jwk) =>\n captureAsyncResult(() =>\n crypto.webcrypto.subtle.importKey(\n 'jwk',\n jwk,\n params.importPublicKey,\n true,\n this._importUsagesFor(jwk, params)\n )\n )\n )\n .withErrorFormat((msg) => `failed to import private key '${id}': ${msg}`);\n }\n\n /**\n * Computes the key usages to request when re-importing a stored private key.\n * WebCrypto rejects `importKey` if the requested usages include operations\n * absent from the JWK's `key_ops`, so a key originally created with a narrower\n * usage set than the algorithm default (e.g. an ECDH key with only\n * `deriveBits`) would fail to load against the algorithm-wide defaults.\n * Intersect the algorithm's private usages with the JWK's recorded `key_ops`\n * so we request exactly the operations the stored key actually supports;\n * fall back to the algorithm's private usages when `key_ops` is absent.\n */\n private _importUsagesFor(jwk: JsonWebKey, params: IKeyPairAlgorithmParams): KeyUsage[] {\n const privateUsages = params.keyPairUsages.filter((usage) => !PUBLIC_ONLY_USAGES.includes(usage));\n const keyOps = jwk.key_ops;\n if (keyOps === undefined) {\n return [...privateUsages];\n }\n return privateUsages.filter((usage) => keyOps.includes(usage));\n }\n}\n"]}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Copyright (c) 2026 Erik Fortune
|
|
2
|
+
//
|
|
3
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
// in the Software without restriction, including without limitation the rights
|
|
6
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
// furnished to do so, subject to the following conditions:
|
|
9
|
+
//
|
|
10
|
+
// The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
// copies or substantial portions of the Software.
|
|
12
|
+
//
|
|
13
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
// SOFTWARE.
|
|
20
|
+
/**
|
|
21
|
+
* Key store module for password-protected secret management (browser version).
|
|
22
|
+
* @packageDocumentation
|
|
23
|
+
*/
|
|
24
|
+
// Types and interfaces
|
|
25
|
+
export * from './model';
|
|
26
|
+
export * from './privateKeyStorage';
|
|
27
|
+
// Converters namespace
|
|
28
|
+
import * as Converters from './converters';
|
|
29
|
+
export { Converters };
|
|
30
|
+
// Key store class
|
|
31
|
+
export { KeyStore } from './keyStore';
|
|
32
|
+
// Note: EncryptedFilePrivateKeyStorage is Node-only — it uses `node:crypto`
|
|
33
|
+
// (via `crypto.webcrypto.subtle`) for the private-key JWK round-trip — so it is
|
|
34
|
+
// intentionally NOT exported in the browser entry. Use `IdbPrivateKeyStorage`
|
|
35
|
+
// from `@fgv/ts-web-extras` for browser private-key storage.
|
|
36
|
+
//# sourceMappingURL=index.browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.browser.js","sourceRoot":"","sources":["../../../../src/packlets/crypto-utils/keystore/index.browser.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,EAAE;AACF,+EAA+E;AAC/E,gFAAgF;AAChF,+EAA+E;AAC/E,4EAA4E;AAC5E,wEAAwE;AACxE,2DAA2D;AAC3D,EAAE;AACF,iFAAiF;AACjF,kDAAkD;AAClD,EAAE;AACF,6EAA6E;AAC7E,2EAA2E;AAC3E,8EAA8E;AAC9E,yEAAyE;AACzE,gFAAgF;AAChF,gFAAgF;AAChF,YAAY;AAEZ;;;GAGG;AAEH,uBAAuB;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,qBAAqB,CAAC;AAEpC,uBAAuB;AACvB,OAAO,KAAK,UAAU,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,CAAC;AAEtB,kBAAkB;AAClB,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,4EAA4E;AAC5E,gFAAgF;AAChF,8EAA8E;AAC9E,6DAA6D","sourcesContent":["// Copyright (c) 2026 Erik Fortune\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\n/**\n * Key store module for password-protected secret management (browser version).\n * @packageDocumentation\n */\n\n// Types and interfaces\nexport * from './model';\nexport * from './privateKeyStorage';\n\n// Converters namespace\nimport * as Converters from './converters';\nexport { Converters };\n\n// Key store class\nexport { KeyStore } from './keyStore';\n\n// Note: EncryptedFilePrivateKeyStorage is Node-only — it uses `node:crypto`\n// (via `crypto.webcrypto.subtle`) for the private-key JWK round-trip — so it is\n// intentionally NOT exported in the browser entry. Use `IdbPrivateKeyStorage`\n// from `@fgv/ts-web-extras` for browser private-key storage.\n"]}
|
|
@@ -29,4 +29,6 @@ import * as Converters from './converters';
|
|
|
29
29
|
export { Converters };
|
|
30
30
|
// Key store class
|
|
31
31
|
export { KeyStore } from './keyStore';
|
|
32
|
+
// Private-key storage implementations
|
|
33
|
+
export { EncryptedFilePrivateKeyStorage } from './encryptedFilePrivateKeyStorage';
|
|
32
34
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/packlets/crypto-utils/keystore/index.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,EAAE;AACF,+EAA+E;AAC/E,gFAAgF;AAChF,+EAA+E;AAC/E,4EAA4E;AAC5E,wEAAwE;AACxE,2DAA2D;AAC3D,EAAE;AACF,iFAAiF;AACjF,kDAAkD;AAClD,EAAE;AACF,6EAA6E;AAC7E,2EAA2E;AAC3E,8EAA8E;AAC9E,yEAAyE;AACzE,gFAAgF;AAChF,gFAAgF;AAChF,YAAY;AAEZ;;;GAGG;AAEH,uBAAuB;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,qBAAqB,CAAC;AAEpC,uBAAuB;AACvB,OAAO,KAAK,UAAU,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,CAAC;AAEtB,kBAAkB;AAClB,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC","sourcesContent":["// Copyright (c) 2026 Erik Fortune\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\n/**\n * Key store module for password-protected secret management.\n * @packageDocumentation\n */\n\n// Types and interfaces\nexport * from './model';\nexport * from './privateKeyStorage';\n\n// Converters namespace\nimport * as Converters from './converters';\nexport { Converters };\n\n// Key store class\nexport { KeyStore } from './keyStore';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/packlets/crypto-utils/keystore/index.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,EAAE;AACF,+EAA+E;AAC/E,gFAAgF;AAChF,+EAA+E;AAC/E,4EAA4E;AAC5E,wEAAwE;AACxE,2DAA2D;AAC3D,EAAE;AACF,iFAAiF;AACjF,kDAAkD;AAClD,EAAE;AACF,6EAA6E;AAC7E,2EAA2E;AAC3E,8EAA8E;AAC9E,yEAAyE;AACzE,gFAAgF;AAChF,gFAAgF;AAChF,YAAY;AAEZ;;;GAGG;AAEH,uBAAuB;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,qBAAqB,CAAC;AAEpC,uBAAuB;AACvB,OAAO,KAAK,UAAU,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,CAAC;AAEtB,kBAAkB;AAClB,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,sCAAsC;AACtC,OAAO,EACL,8BAA8B,EAE/B,MAAM,kCAAkC,CAAC","sourcesContent":["// Copyright (c) 2026 Erik Fortune\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\n/**\n * Key store module for password-protected secret management.\n * @packageDocumentation\n */\n\n// Types and interfaces\nexport * from './model';\nexport * from './privateKeyStorage';\n\n// Converters namespace\nimport * as Converters from './converters';\nexport { Converters };\n\n// Key store class\nexport { KeyStore } from './keyStore';\n\n// Private-key storage implementations\nexport {\n EncryptedFilePrivateKeyStorage,\n IEncryptedFilePrivateKeyStorageCreateParams\n} from './encryptedFilePrivateKeyStorage';\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"privateKeyStorage.js","sourceRoot":"","sources":["../../../../src/packlets/crypto-utils/keystore/privateKeyStorage.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,EAAE;AACF,+EAA+E;AAC/E,gFAAgF;AAChF,+EAA+E;AAC/E,4EAA4E;AAC5E,wEAAwE;AACxE,2DAA2D;AAC3D,EAAE;AACF,iFAAiF;AACjF,kDAAkD;AAClD,EAAE;AACF,6EAA6E;AAC7E,2EAA2E;AAC3E,8EAA8E;AAC9E,yEAAyE;AACzE,gFAAgF;AAChF,gFAAgF;AAChF,YAAY","sourcesContent":["// Copyright (c) 2026 Erik Fortune\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\nimport { Result } from '@fgv/ts-utils';\n\n/**\n * Pluggable backend that persists raw asymmetric private keys outside of the\n * encrypted keystore vault. Concrete implementations
|
|
1
|
+
{"version":3,"file":"privateKeyStorage.js","sourceRoot":"","sources":["../../../../src/packlets/crypto-utils/keystore/privateKeyStorage.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,EAAE;AACF,+EAA+E;AAC/E,gFAAgF;AAChF,+EAA+E;AAC/E,4EAA4E;AAC5E,wEAAwE;AACxE,2DAA2D;AAC3D,EAAE;AACF,iFAAiF;AACjF,kDAAkD;AAClD,EAAE;AACF,6EAA6E;AAC7E,2EAA2E;AAC3E,8EAA8E;AAC9E,yEAAyE;AACzE,gFAAgF;AAChF,gFAAgF;AAChF,YAAY","sourcesContent":["// Copyright (c) 2026 Erik Fortune\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\nimport { Result } from '@fgv/ts-utils';\n\n/**\n * Pluggable backend that persists raw asymmetric private keys outside of the\n * encrypted keystore vault. Concrete implementations:\n * - {@link CryptoUtils.KeyStore.EncryptedFilePrivateKeyStorage} in\n * `@fgv/ts-extras` — directory-on-disk, AES-256-GCM-encrypted JWK per key\n * (Node; `supportsNonExtractable: false`).\n * - `IdbPrivateKeyStorage` in `@fgv/ts-web-extras` — IndexedDB-backed, stores\n * `CryptoKey` objects directly (browser; `supportsNonExtractable: true`).\n *\n * The keystore writes storage-first: a private key is always stored here\n * before the corresponding public-key vault entry is committed. Conversely,\n * deletes hit the vault first and then this storage best-effort. As a result,\n * crashes or skipped saves can leave orphaned blobs here; callers are expected\n * to reconcile via {@link CryptoUtils.KeyStore.IPrivateKeyStorage.list} cross-referenced\n * against the keystore's asymmetric entries.\n *\n * @public\n */\nexport interface IPrivateKeyStorage {\n /**\n * Whether keys generated for this backend may be marked\n * `extractable: false`. `true` on backends that store `CryptoKey`\n * objects directly (e.g. IndexedDB). `false` on backends that must\n * round-trip via JWK (e.g. encrypted-file backends).\n */\n readonly supportsNonExtractable: boolean;\n\n /**\n * Stores `key` under `id`. Returns the stored `id` on success so the\n * call can compose into a Result chain.\n * @param id - Storage handle to write under.\n * @param key - The private `CryptoKey` to persist.\n */\n store(id: string, key: CryptoKey): Promise<Result<string>>;\n\n /**\n * Loads the private key previously stored under `id`.\n * @param id - Storage handle to look up.\n */\n load(id: string): Promise<Result<CryptoKey>>;\n\n /**\n * Deletes the entry stored under `id`. Returns the deleted `id` on\n * success so the call can compose into a Result chain.\n * @param id - Storage handle to remove.\n */\n delete(id: string): Promise<Result<string>>;\n\n /**\n * Lists every `id` currently held by the backend. Used by consumers to\n * garbage-collect orphans left by crashes or aborted sessions; the\n * keystore itself does not invoke this automatically.\n */\n list(): Promise<Result<readonly string[]>>;\n}\n"]}
|