@fgv/ts-extras 5.1.0-17 → 5.1.0-18
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 +2 -0
- package/dist/packlets/crypto-utils/index.js +2 -0
- package/dist/packlets/crypto-utils/keyPairAlgorithmParams.js +47 -0
- package/dist/packlets/crypto-utils/keystore/converters.js +101 -9
- package/dist/packlets/crypto-utils/keystore/index.js +1 -0
- package/dist/packlets/crypto-utils/keystore/keyStore.js +271 -46
- package/dist/packlets/crypto-utils/keystore/model.js +22 -1
- package/dist/packlets/crypto-utils/keystore/privateKeyStorage.js +21 -0
- package/dist/packlets/crypto-utils/model.js +5 -0
- package/dist/packlets/crypto-utils/nodeCryptoProvider.js +44 -1
- package/dist/test/unit/crypto/keystore/inMemoryPrivateKeyStorage.js +78 -0
- package/dist/ts-extras.d.ts +577 -32
- package/lib/packlets/crypto-utils/index.browser.d.ts +1 -0
- package/lib/packlets/crypto-utils/index.browser.js +4 -1
- package/lib/packlets/crypto-utils/index.d.ts +1 -0
- package/lib/packlets/crypto-utils/index.js +4 -1
- package/lib/packlets/crypto-utils/keyPairAlgorithmParams.d.ts +39 -0
- package/lib/packlets/crypto-utils/keyPairAlgorithmParams.js +50 -0
- package/lib/packlets/crypto-utils/keystore/converters.d.ts +68 -6
- package/lib/packlets/crypto-utils/keystore/converters.js +100 -8
- package/lib/packlets/crypto-utils/keystore/index.d.ts +1 -0
- package/lib/packlets/crypto-utils/keystore/index.js +1 -0
- package/lib/packlets/crypto-utils/keystore/keyStore.d.ts +77 -9
- package/lib/packlets/crypto-utils/keystore/keyStore.js +271 -46
- package/lib/packlets/crypto-utils/keystore/model.d.ts +238 -19
- package/lib/packlets/crypto-utils/keystore/model.js +24 -2
- package/lib/packlets/crypto-utils/keystore/privateKeyStorage.d.ts +50 -0
- package/lib/packlets/crypto-utils/keystore/privateKeyStorage.js +22 -0
- package/lib/packlets/crypto-utils/model.d.ts +38 -0
- package/lib/packlets/crypto-utils/model.js +6 -1
- package/lib/packlets/crypto-utils/nodeCryptoProvider.d.ts +26 -1
- package/lib/packlets/crypto-utils/nodeCryptoProvider.js +43 -0
- package/package.json +7 -7
|
@@ -10,5 +10,6 @@ export { KeyStore };
|
|
|
10
10
|
import * as Converters from './converters';
|
|
11
11
|
export { Converters };
|
|
12
12
|
export { DirectEncryptionProvider, IDirectEncryptionProviderParams } from './directEncryptionProvider';
|
|
13
|
+
export { IKeyPairAlgorithmParams, keyPairAlgorithmParams } from './keyPairAlgorithmParams';
|
|
13
14
|
export { createEncryptedFile, decryptFile, fromBase64, ICreateEncryptedFileParams, toBase64, tryDecryptFile } from './encryptedFile';
|
|
14
15
|
//# sourceMappingURL=index.browser.d.ts.map
|
|
@@ -55,7 +55,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
55
55
|
};
|
|
56
56
|
})();
|
|
57
57
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
58
|
-
exports.tryDecryptFile = exports.toBase64 = exports.fromBase64 = exports.decryptFile = exports.createEncryptedFile = exports.DirectEncryptionProvider = exports.Converters = exports.KeyStore = exports.GCM_IV_SIZE = exports.GCM_AUTH_TAG_SIZE = exports.ENCRYPTED_FILE_FORMAT = exports.DEFAULT_ALGORITHM = exports.AES_256_KEY_SIZE = void 0;
|
|
58
|
+
exports.tryDecryptFile = exports.toBase64 = exports.fromBase64 = exports.decryptFile = exports.createEncryptedFile = exports.keyPairAlgorithmParams = exports.DirectEncryptionProvider = exports.Converters = exports.KeyStore = exports.GCM_IV_SIZE = exports.GCM_AUTH_TAG_SIZE = exports.ENCRYPTED_FILE_FORMAT = exports.DEFAULT_ALGORITHM = exports.AES_256_KEY_SIZE = void 0;
|
|
59
59
|
/**
|
|
60
60
|
* Crypto utilities for encrypted file handling and key management (browser version).
|
|
61
61
|
* Note: For browser crypto provider, use \@fgv/ts-web-extras.
|
|
@@ -79,6 +79,9 @@ exports.Converters = Converters;
|
|
|
79
79
|
// Direct encryption provider
|
|
80
80
|
var directEncryptionProvider_1 = require("./directEncryptionProvider");
|
|
81
81
|
Object.defineProperty(exports, "DirectEncryptionProvider", { enumerable: true, get: function () { return directEncryptionProvider_1.DirectEncryptionProvider; } });
|
|
82
|
+
// WebCrypto parameter table for asymmetric keypair algorithms
|
|
83
|
+
var keyPairAlgorithmParams_1 = require("./keyPairAlgorithmParams");
|
|
84
|
+
Object.defineProperty(exports, "keyPairAlgorithmParams", { enumerable: true, get: function () { return keyPairAlgorithmParams_1.keyPairAlgorithmParams; } });
|
|
82
85
|
// Note: NodeCryptoProvider is NOT exported in browser version
|
|
83
86
|
// Use BrowserCryptoProvider from @fgv/ts-web-extras instead
|
|
84
87
|
// Encrypted file helpers
|
|
@@ -10,6 +10,7 @@ export { KeyStore };
|
|
|
10
10
|
import * as Converters from './converters';
|
|
11
11
|
export { Converters };
|
|
12
12
|
export { DirectEncryptionProvider, IDirectEncryptionProviderParams } from './directEncryptionProvider';
|
|
13
|
+
export { IKeyPairAlgorithmParams, keyPairAlgorithmParams } from './keyPairAlgorithmParams';
|
|
13
14
|
export { NodeCryptoProvider, nodeCryptoProvider } from './nodeCryptoProvider';
|
|
14
15
|
export { createEncryptedFile, decryptFile, fromBase64, ICreateEncryptedFileParams, toBase64, tryDecryptFile } from './encryptedFile';
|
|
15
16
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -55,7 +55,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
55
55
|
};
|
|
56
56
|
})();
|
|
57
57
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
58
|
-
exports.tryDecryptFile = exports.toBase64 = exports.fromBase64 = exports.decryptFile = exports.createEncryptedFile = exports.nodeCryptoProvider = exports.NodeCryptoProvider = exports.DirectEncryptionProvider = exports.Converters = exports.KeyStore = exports.Constants = void 0;
|
|
58
|
+
exports.tryDecryptFile = exports.toBase64 = exports.fromBase64 = exports.decryptFile = exports.createEncryptedFile = exports.nodeCryptoProvider = exports.NodeCryptoProvider = exports.keyPairAlgorithmParams = exports.DirectEncryptionProvider = exports.Converters = exports.KeyStore = exports.Constants = void 0;
|
|
59
59
|
/**
|
|
60
60
|
* Crypto utilities for encrypted file handling and key management.
|
|
61
61
|
* @packageDocumentation
|
|
@@ -74,6 +74,9 @@ exports.Converters = Converters;
|
|
|
74
74
|
// Direct encryption provider
|
|
75
75
|
var directEncryptionProvider_1 = require("./directEncryptionProvider");
|
|
76
76
|
Object.defineProperty(exports, "DirectEncryptionProvider", { enumerable: true, get: function () { return directEncryptionProvider_1.DirectEncryptionProvider; } });
|
|
77
|
+
// WebCrypto parameter table for asymmetric keypair algorithms
|
|
78
|
+
var keyPairAlgorithmParams_1 = require("./keyPairAlgorithmParams");
|
|
79
|
+
Object.defineProperty(exports, "keyPairAlgorithmParams", { enumerable: true, get: function () { return keyPairAlgorithmParams_1.keyPairAlgorithmParams; } });
|
|
77
80
|
// Node.js crypto provider (Node.js environment only)
|
|
78
81
|
var nodeCryptoProvider_1 = require("./nodeCryptoProvider");
|
|
79
82
|
Object.defineProperty(exports, "NodeCryptoProvider", { enumerable: true, get: function () { return nodeCryptoProvider_1.NodeCryptoProvider; } });
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { KeyPairAlgorithm } from './model';
|
|
2
|
+
/**
|
|
3
|
+
* WebCrypto parameters for a single {@link CryptoUtils.KeyPairAlgorithm}.
|
|
4
|
+
* Implementations of {@link CryptoUtils.ICryptoProvider} use this table to
|
|
5
|
+
* translate the small public algorithm enum into the WebCrypto algorithm
|
|
6
|
+
* objects and key-usage arrays expected by `crypto.subtle`.
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export interface IKeyPairAlgorithmParams {
|
|
10
|
+
/**
|
|
11
|
+
* Algorithm parameters for `crypto.subtle.generateKey`. Always an asymmetric
|
|
12
|
+
* variant — these algorithms produce a `CryptoKeyPair`, not a single key.
|
|
13
|
+
*/
|
|
14
|
+
readonly generateKey: RsaHashedKeyGenParams | EcKeyGenParams;
|
|
15
|
+
/**
|
|
16
|
+
* Algorithm parameters for `crypto.subtle.importKey('jwk', ...)` when
|
|
17
|
+
* importing the public half of a keypair.
|
|
18
|
+
*/
|
|
19
|
+
readonly importPublicKey: RsaHashedImportParams | EcKeyImportParams;
|
|
20
|
+
/**
|
|
21
|
+
* Default key usages for the generated `CryptoKeyPair`. Both halves receive
|
|
22
|
+
* the usages WebCrypto considers valid for their role; the platform filters.
|
|
23
|
+
*/
|
|
24
|
+
readonly keyPairUsages: ReadonlyArray<KeyUsage>;
|
|
25
|
+
/**
|
|
26
|
+
* Key usages applied when re-importing only the public key.
|
|
27
|
+
*/
|
|
28
|
+
readonly publicKeyUsages: ReadonlyArray<KeyUsage>;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Lookup table from {@link CryptoUtils.KeyPairAlgorithm} to the WebCrypto
|
|
32
|
+
* parameters needed to drive `crypto.subtle`. Shared between every
|
|
33
|
+
* {@link CryptoUtils.ICryptoProvider} implementation since both Node and
|
|
34
|
+
* browser providers speak the same WebCrypto API. Exposed for downstream
|
|
35
|
+
* provider implementations (e.g. browser-side providers in `@fgv/ts-web-extras`).
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
export declare const keyPairAlgorithmParams: Readonly<Record<KeyPairAlgorithm, IKeyPairAlgorithmParams>>;
|
|
39
|
+
//# sourceMappingURL=keyPairAlgorithmParams.d.ts.map
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright (c) 2026 Erik Fortune
|
|
3
|
+
//
|
|
4
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
// in the Software without restriction, including without limitation the rights
|
|
7
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
// furnished to do so, subject to the following conditions:
|
|
10
|
+
//
|
|
11
|
+
// The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
// copies or substantial portions of the Software.
|
|
13
|
+
//
|
|
14
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
// SOFTWARE.
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.keyPairAlgorithmParams = void 0;
|
|
23
|
+
/**
|
|
24
|
+
* Lookup table from {@link CryptoUtils.KeyPairAlgorithm} to the WebCrypto
|
|
25
|
+
* parameters needed to drive `crypto.subtle`. Shared between every
|
|
26
|
+
* {@link CryptoUtils.ICryptoProvider} implementation since both Node and
|
|
27
|
+
* browser providers speak the same WebCrypto API. Exposed for downstream
|
|
28
|
+
* provider implementations (e.g. browser-side providers in `@fgv/ts-web-extras`).
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
exports.keyPairAlgorithmParams = {
|
|
32
|
+
'ecdsa-p256': {
|
|
33
|
+
generateKey: { name: 'ECDSA', namedCurve: 'P-256' },
|
|
34
|
+
importPublicKey: { name: 'ECDSA', namedCurve: 'P-256' },
|
|
35
|
+
keyPairUsages: ['sign', 'verify'],
|
|
36
|
+
publicKeyUsages: ['verify']
|
|
37
|
+
},
|
|
38
|
+
'rsa-oaep-2048': {
|
|
39
|
+
generateKey: {
|
|
40
|
+
name: 'RSA-OAEP',
|
|
41
|
+
modulusLength: 2048,
|
|
42
|
+
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
|
|
43
|
+
hash: 'SHA-256'
|
|
44
|
+
},
|
|
45
|
+
importPublicKey: { name: 'RSA-OAEP', hash: 'SHA-256' },
|
|
46
|
+
keyPairUsages: ['encrypt', 'decrypt'],
|
|
47
|
+
publicKeyUsages: ['encrypt']
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
//# sourceMappingURL=keyPairAlgorithmParams.js.map
|
|
@@ -1,21 +1,83 @@
|
|
|
1
|
-
import { Converter } from '@fgv/ts-utils';
|
|
2
|
-
import { IKeyStoreFile,
|
|
1
|
+
import { Converter, Validator } from '@fgv/ts-utils';
|
|
2
|
+
import { IKeyStoreAsymmetricEntryJson, IKeyStoreEntryJson, IKeyStoreFile, IKeyStoreSymmetricEntryJson, IKeyStoreVaultContents, KeyPairAlgorithm, KeyStoreAsymmetricSecretType, KeyStoreFormat, KeyStoreSecretType, KeyStoreSymmetricSecretType } from './model';
|
|
3
3
|
/**
|
|
4
4
|
* Converter for {@link CryptoUtils.KeyStore.KeyStoreFormat | key store format} version.
|
|
5
5
|
* @public
|
|
6
6
|
*/
|
|
7
7
|
export declare const keystoreFormat: Converter<KeyStoreFormat>;
|
|
8
8
|
/**
|
|
9
|
-
* Converter for {@link CryptoUtils.KeyStore.KeyStoreSecretType | key store secret type} discriminator.
|
|
9
|
+
* Converter for {@link CryptoUtils.KeyStore.KeyStoreSecretType | any key store secret type} discriminator.
|
|
10
|
+
* Accepts both symmetric and asymmetric type values.
|
|
10
11
|
* @public
|
|
11
12
|
*/
|
|
12
13
|
export declare const keystoreSecretType: Converter<KeyStoreSecretType>;
|
|
13
14
|
/**
|
|
14
|
-
* Converter for {@link CryptoUtils.KeyStore.
|
|
15
|
-
*
|
|
15
|
+
* Converter for {@link CryptoUtils.KeyStore.KeyStoreSymmetricSecretType | symmetric secret type} discriminator.
|
|
16
|
+
* Accepts only `'encryption-key'` and `'api-key'`.
|
|
16
17
|
* @public
|
|
17
18
|
*/
|
|
18
|
-
export declare const
|
|
19
|
+
export declare const keystoreSymmetricSecretType: Converter<KeyStoreSymmetricSecretType>;
|
|
20
|
+
/**
|
|
21
|
+
* Converter for {@link CryptoUtils.KeyStore.KeyStoreAsymmetricSecretType | asymmetric secret type} discriminator.
|
|
22
|
+
* Accepts only `'asymmetric-keypair'`.
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
export declare const keystoreAsymmetricSecretType: Converter<KeyStoreAsymmetricSecretType>;
|
|
26
|
+
/**
|
|
27
|
+
* Converter for {@link CryptoUtils.KeyStore.KeyPairAlgorithm | key pair algorithm}.
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
export declare const keyPairAlgorithm: Converter<KeyPairAlgorithm>;
|
|
31
|
+
/**
|
|
32
|
+
* In-place shape check for a JSON Web Key. Asserts only that the input is a
|
|
33
|
+
* non-array object whose `kty` discriminator is a string; every other JWK
|
|
34
|
+
* field passes through untouched. This is intentionally **not** a true JWK
|
|
35
|
+
* validator — per-algorithm correctness (RSA `n`/`e`, EC `crv`/`x`/`y`,
|
|
36
|
+
* key-size constraints, etc.) is delegated to `crypto.subtle.importKey` at
|
|
37
|
+
* first use, which is the authoritative checker. The "shape" suffix in the
|
|
38
|
+
* name is the warning sign for readers expecting full validation.
|
|
39
|
+
* @remarks
|
|
40
|
+
* Built with `Validators.object` (in-place, non-strict) so unknown JWK fields
|
|
41
|
+
* survive the round-trip; the cast to `FieldValidators<JsonWebKey>` is required
|
|
42
|
+
* only because TypeScript's mapped type demands an entry for every key in
|
|
43
|
+
* `JsonWebKey`. At runtime the `ObjectValidator` only inspects keys present in
|
|
44
|
+
* the field-validators map.
|
|
45
|
+
* @public
|
|
46
|
+
*/
|
|
47
|
+
export declare const jsonWebKeyShape: Validator<JsonWebKey>;
|
|
48
|
+
/**
|
|
49
|
+
* Converter for {@link CryptoUtils.KeyStore.IKeyStoreSymmetricEntryJson | symmetric secret entry} in JSON form.
|
|
50
|
+
*
|
|
51
|
+
* @remarks
|
|
52
|
+
* Backwards compatibility with vaults written before asymmetric-keypair
|
|
53
|
+
* support: those entries may lack the `type` discriminator on the wire. To
|
|
54
|
+
* keep the model type honest (`type` is required on
|
|
55
|
+
* {@link CryptoUtils.KeyStore.IKeyStoreSymmetricEntryJson}, see its docs),
|
|
56
|
+
* we declare `type` in `optionalFields` so the inner `Converters.object` will
|
|
57
|
+
* accept input without it, then `.map()` injects the default
|
|
58
|
+
* `'encryption-key'` when missing. The output therefore always carries the
|
|
59
|
+
* discriminator and downstream code never sees the legacy missing-type form.
|
|
60
|
+
*
|
|
61
|
+
* @public
|
|
62
|
+
*/
|
|
63
|
+
export declare const keystoreSymmetricEntryJson: Converter<IKeyStoreSymmetricEntryJson>;
|
|
64
|
+
/**
|
|
65
|
+
* Converter for {@link CryptoUtils.KeyStore.IKeyStoreAsymmetricEntryJson | asymmetric keypair entry} in JSON form.
|
|
66
|
+
* The `publicKeyJwk` field passes through {@link CryptoUtils.KeyStore.Converters.jsonWebKeyShape | jsonWebKeyShape}
|
|
67
|
+
* (shape check only — see its docs); cryptographic correctness is enforced by
|
|
68
|
+
* `crypto.subtle.importKey` at use.
|
|
69
|
+
* @public
|
|
70
|
+
*/
|
|
71
|
+
export declare const keystoreAsymmetricEntryJson: Converter<IKeyStoreAsymmetricEntryJson>;
|
|
72
|
+
/**
|
|
73
|
+
* Discriminated-union converter for any {@link CryptoUtils.KeyStore.IKeyStoreEntryJson | key store entry} in JSON form.
|
|
74
|
+
* Routes by the `type` field: `'asymmetric-keypair'` is parsed by
|
|
75
|
+
* {@link CryptoUtils.KeyStore.Converters.keystoreAsymmetricEntryJson | keystoreAsymmetricEntryJson},
|
|
76
|
+
* anything else (including a missing `type` field for backwards compatibility) by
|
|
77
|
+
* {@link CryptoUtils.KeyStore.Converters.keystoreSymmetricEntryJson | keystoreSymmetricEntryJson}.
|
|
78
|
+
* @public
|
|
79
|
+
*/
|
|
80
|
+
export declare const keystoreSecretEntryJson: Converter<IKeyStoreEntryJson>;
|
|
19
81
|
/**
|
|
20
82
|
* Converter for {@link CryptoUtils.KeyStore.IKeyStoreVaultContents | key store vault contents} (decrypted state).
|
|
21
83
|
* @public
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
20
|
// SOFTWARE.
|
|
21
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
-
exports.keystoreFile = exports.keystoreVaultContents = exports.keystoreSecretEntryJson = exports.keystoreSecretType = exports.keystoreFormat = void 0;
|
|
22
|
+
exports.keystoreFile = exports.keystoreVaultContents = exports.keystoreSecretEntryJson = exports.keystoreAsymmetricEntryJson = exports.keystoreSymmetricEntryJson = exports.jsonWebKeyShape = exports.keyPairAlgorithm = exports.keystoreAsymmetricSecretType = exports.keystoreSymmetricSecretType = exports.keystoreSecretType = exports.keystoreFormat = void 0;
|
|
23
23
|
const ts_utils_1 = require("@fgv/ts-utils");
|
|
24
24
|
const converters_1 = require("../converters");
|
|
25
25
|
const model_1 = require("./model");
|
|
@@ -34,31 +34,123 @@ exports.keystoreFormat = ts_utils_1.Converters.enumeratedValue([
|
|
|
34
34
|
model_1.KEYSTORE_FORMAT
|
|
35
35
|
]);
|
|
36
36
|
// ============================================================================
|
|
37
|
-
// Secret Type
|
|
37
|
+
// Secret Type Converters
|
|
38
38
|
// ============================================================================
|
|
39
39
|
/**
|
|
40
|
-
* Converter for {@link CryptoUtils.KeyStore.KeyStoreSecretType | key store secret type} discriminator.
|
|
40
|
+
* Converter for {@link CryptoUtils.KeyStore.KeyStoreSecretType | any key store secret type} discriminator.
|
|
41
|
+
* Accepts both symmetric and asymmetric type values.
|
|
41
42
|
* @public
|
|
42
43
|
*/
|
|
43
44
|
exports.keystoreSecretType = ts_utils_1.Converters.enumeratedValue(model_1.allKeyStoreSecretTypes);
|
|
45
|
+
/**
|
|
46
|
+
* Converter for {@link CryptoUtils.KeyStore.KeyStoreSymmetricSecretType | symmetric secret type} discriminator.
|
|
47
|
+
* Accepts only `'encryption-key'` and `'api-key'`.
|
|
48
|
+
* @public
|
|
49
|
+
*/
|
|
50
|
+
exports.keystoreSymmetricSecretType = ts_utils_1.Converters.enumeratedValue(model_1.allKeyStoreSymmetricSecretTypes);
|
|
51
|
+
/**
|
|
52
|
+
* Converter for {@link CryptoUtils.KeyStore.KeyStoreAsymmetricSecretType | asymmetric secret type} discriminator.
|
|
53
|
+
* Accepts only `'asymmetric-keypair'`.
|
|
54
|
+
* @public
|
|
55
|
+
*/
|
|
56
|
+
exports.keystoreAsymmetricSecretType = ts_utils_1.Converters.enumeratedValue(model_1.allKeyStoreAsymmetricSecretTypes);
|
|
57
|
+
// ============================================================================
|
|
58
|
+
// Key Pair Algorithm Converter
|
|
59
|
+
// ============================================================================
|
|
60
|
+
/**
|
|
61
|
+
* Converter for {@link CryptoUtils.KeyStore.KeyPairAlgorithm | key pair algorithm}.
|
|
62
|
+
* @public
|
|
63
|
+
*/
|
|
64
|
+
exports.keyPairAlgorithm = ts_utils_1.Converters.enumeratedValue(model_1.allKeyPairAlgorithms);
|
|
44
65
|
// ============================================================================
|
|
45
|
-
//
|
|
66
|
+
// JWK Shape Validator
|
|
46
67
|
// ============================================================================
|
|
47
68
|
/**
|
|
48
|
-
*
|
|
49
|
-
*
|
|
69
|
+
* In-place shape check for a JSON Web Key. Asserts only that the input is a
|
|
70
|
+
* non-array object whose `kty` discriminator is a string; every other JWK
|
|
71
|
+
* field passes through untouched. This is intentionally **not** a true JWK
|
|
72
|
+
* validator — per-algorithm correctness (RSA `n`/`e`, EC `crv`/`x`/`y`,
|
|
73
|
+
* key-size constraints, etc.) is delegated to `crypto.subtle.importKey` at
|
|
74
|
+
* first use, which is the authoritative checker. The "shape" suffix in the
|
|
75
|
+
* name is the warning sign for readers expecting full validation.
|
|
76
|
+
* @remarks
|
|
77
|
+
* Built with `Validators.object` (in-place, non-strict) so unknown JWK fields
|
|
78
|
+
* survive the round-trip; the cast to `FieldValidators<JsonWebKey>` is required
|
|
79
|
+
* only because TypeScript's mapped type demands an entry for every key in
|
|
80
|
+
* `JsonWebKey`. At runtime the `ObjectValidator` only inspects keys present in
|
|
81
|
+
* the field-validators map.
|
|
50
82
|
* @public
|
|
51
83
|
*/
|
|
52
|
-
exports.
|
|
84
|
+
exports.jsonWebKeyShape = ts_utils_1.Validators.object({
|
|
85
|
+
kty: ts_utils_1.Validators.string
|
|
86
|
+
});
|
|
87
|
+
// ============================================================================
|
|
88
|
+
// Symmetric Secret Entry Converter
|
|
89
|
+
// ============================================================================
|
|
90
|
+
/**
|
|
91
|
+
* Converter for {@link CryptoUtils.KeyStore.IKeyStoreSymmetricEntryJson | symmetric secret entry} in JSON form.
|
|
92
|
+
*
|
|
93
|
+
* @remarks
|
|
94
|
+
* Backwards compatibility with vaults written before asymmetric-keypair
|
|
95
|
+
* support: those entries may lack the `type` discriminator on the wire. To
|
|
96
|
+
* keep the model type honest (`type` is required on
|
|
97
|
+
* {@link CryptoUtils.KeyStore.IKeyStoreSymmetricEntryJson}, see its docs),
|
|
98
|
+
* we declare `type` in `optionalFields` so the inner `Converters.object` will
|
|
99
|
+
* accept input without it, then `.map()` injects the default
|
|
100
|
+
* `'encryption-key'` when missing. The output therefore always carries the
|
|
101
|
+
* discriminator and downstream code never sees the legacy missing-type form.
|
|
102
|
+
*
|
|
103
|
+
* @public
|
|
104
|
+
*/
|
|
105
|
+
exports.keystoreSymmetricEntryJson = ts_utils_1.Converters.object({
|
|
53
106
|
name: ts_utils_1.Converters.string,
|
|
54
|
-
type: exports.
|
|
107
|
+
type: exports.keystoreSymmetricSecretType,
|
|
55
108
|
key: converters_1.base64String,
|
|
56
109
|
description: ts_utils_1.Converters.string,
|
|
57
110
|
createdAt: ts_utils_1.Converters.string
|
|
58
111
|
}, {
|
|
112
|
+
// `type` is optional at the input layer for legacy-vault compatibility;
|
|
113
|
+
// the .map() below normalizes by injecting the default.
|
|
59
114
|
optionalFields: ['type', 'description']
|
|
115
|
+
}).map((entry) => {
|
|
116
|
+
var _a;
|
|
117
|
+
return (0, ts_utils_1.succeed)(Object.assign(Object.assign({}, entry), { type: (_a = entry.type) !== null && _a !== void 0 ? _a : 'encryption-key' }));
|
|
60
118
|
});
|
|
61
119
|
// ============================================================================
|
|
120
|
+
// Asymmetric Keypair Entry Converter
|
|
121
|
+
// ============================================================================
|
|
122
|
+
/**
|
|
123
|
+
* Converter for {@link CryptoUtils.KeyStore.IKeyStoreAsymmetricEntryJson | asymmetric keypair entry} in JSON form.
|
|
124
|
+
* The `publicKeyJwk` field passes through {@link CryptoUtils.KeyStore.Converters.jsonWebKeyShape | jsonWebKeyShape}
|
|
125
|
+
* (shape check only — see its docs); cryptographic correctness is enforced by
|
|
126
|
+
* `crypto.subtle.importKey` at use.
|
|
127
|
+
* @public
|
|
128
|
+
*/
|
|
129
|
+
exports.keystoreAsymmetricEntryJson = ts_utils_1.Converters.object({
|
|
130
|
+
name: ts_utils_1.Converters.string,
|
|
131
|
+
type: exports.keystoreAsymmetricSecretType,
|
|
132
|
+
id: ts_utils_1.Converters.string,
|
|
133
|
+
algorithm: exports.keyPairAlgorithm,
|
|
134
|
+
publicKeyJwk: exports.jsonWebKeyShape,
|
|
135
|
+
description: ts_utils_1.Converters.string.optional(),
|
|
136
|
+
createdAt: ts_utils_1.Converters.string
|
|
137
|
+
});
|
|
138
|
+
// ============================================================================
|
|
139
|
+
// Discriminated-Union Entry Converter
|
|
140
|
+
// ============================================================================
|
|
141
|
+
/**
|
|
142
|
+
* Discriminated-union converter for any {@link CryptoUtils.KeyStore.IKeyStoreEntryJson | key store entry} in JSON form.
|
|
143
|
+
* Routes by the `type` field: `'asymmetric-keypair'` is parsed by
|
|
144
|
+
* {@link CryptoUtils.KeyStore.Converters.keystoreAsymmetricEntryJson | keystoreAsymmetricEntryJson},
|
|
145
|
+
* anything else (including a missing `type` field for backwards compatibility) by
|
|
146
|
+
* {@link CryptoUtils.KeyStore.Converters.keystoreSymmetricEntryJson | keystoreSymmetricEntryJson}.
|
|
147
|
+
* @public
|
|
148
|
+
*/
|
|
149
|
+
exports.keystoreSecretEntryJson = ts_utils_1.Converters.oneOf([
|
|
150
|
+
exports.keystoreAsymmetricEntryJson,
|
|
151
|
+
exports.keystoreSymmetricEntryJson
|
|
152
|
+
]);
|
|
153
|
+
// ============================================================================
|
|
62
154
|
// Vault Contents Converter
|
|
63
155
|
// ============================================================================
|
|
64
156
|
/**
|
|
@@ -62,6 +62,7 @@ exports.KeyStore = exports.Converters = void 0;
|
|
|
62
62
|
*/
|
|
63
63
|
// Types and interfaces
|
|
64
64
|
__exportStar(require("./model"), exports);
|
|
65
|
+
__exportStar(require("./privateKeyStorage"), exports);
|
|
65
66
|
// Converters namespace
|
|
66
67
|
const Converters = __importStar(require("./converters"));
|
|
67
68
|
exports.Converters = Converters;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { JsonValue } from '@fgv/ts-json-base';
|
|
2
2
|
import { Result } from '@fgv/ts-utils';
|
|
3
3
|
import { ICryptoProvider, IEncryptedFile, IEncryptionConfig, IEncryptionProvider, SecretProvider } from '../model';
|
|
4
|
-
import { IAddSecretFromPasswordOptions, IAddSecretFromPasswordResult, IAddSecretOptions, IAddSecretResult, IImportKeyOptions, IImportSecretOptions, IKeyStoreCreateParams, IKeyStoreFile, IKeyStoreOpenParams,
|
|
4
|
+
import { IAddKeyPairOptions, IAddKeyPairResult, IAddSecretFromPasswordOptions, IAddSecretFromPasswordResult, IAddSecretOptions, IAddSecretResult, IImportKeyOptions, IImportSecretOptions, IKeyStoreCreateParams, IKeyStoreEntry, IKeyStoreFile, IKeyStoreOpenParams, IRemoveSecretResult, KeyStoreLockState, KeyStoreSecretType } from './model';
|
|
5
5
|
/**
|
|
6
6
|
* Password-protected key store for managing encryption secrets.
|
|
7
7
|
*
|
|
@@ -35,6 +35,7 @@ import { IAddSecretFromPasswordOptions, IAddSecretFromPasswordResult, IAddSecret
|
|
|
35
35
|
*/
|
|
36
36
|
export declare class KeyStore implements IEncryptionProvider {
|
|
37
37
|
private readonly _cryptoProvider;
|
|
38
|
+
private readonly _privateKeyStorage;
|
|
38
39
|
private readonly _iterations;
|
|
39
40
|
private _keystoreFile;
|
|
40
41
|
private _salt;
|
|
@@ -133,12 +134,23 @@ export declare class KeyStore implements IEncryptionProvider {
|
|
|
133
134
|
*/
|
|
134
135
|
listSecrets(): Result<readonly string[]>;
|
|
135
136
|
/**
|
|
136
|
-
* Gets a secret by name.
|
|
137
|
+
* Gets a secret by name. Returns the {@link CryptoUtils.KeyStore.IKeyStoreEntry | discriminated union}
|
|
138
|
+
* — callers must check `entry.type` before accessing `key`/`id` since asymmetric
|
|
139
|
+
* entries carry no raw key material.
|
|
137
140
|
* @param name - Name of the secret
|
|
138
141
|
* @returns Success with secret entry, Failure if not found or locked
|
|
139
142
|
* @public
|
|
140
143
|
*/
|
|
141
|
-
getSecret(name: string): Result<
|
|
144
|
+
getSecret(name: string): Result<IKeyStoreEntry>;
|
|
145
|
+
/**
|
|
146
|
+
* Returns the public-key JWK for an asymmetric-keypair entry.
|
|
147
|
+
* Available without {@link CryptoUtils.KeyStore.IPrivateKeyStorage} since the
|
|
148
|
+
* public key lives in the vault metadata directly.
|
|
149
|
+
* @param name - Name of the entry
|
|
150
|
+
* @returns Success with the JWK, Failure if not found, locked, or wrong type
|
|
151
|
+
* @public
|
|
152
|
+
*/
|
|
153
|
+
getPublicKeyJwk(name: string): Result<JsonWebKey>;
|
|
142
154
|
/**
|
|
143
155
|
* Checks if a secret exists.
|
|
144
156
|
* @param name - Name of the secret
|
|
@@ -168,7 +180,7 @@ export declare class KeyStore implements IEncryptionProvider {
|
|
|
168
180
|
* @returns Success with entry, Failure if locked, key invalid, or exists and !replace
|
|
169
181
|
* @public
|
|
170
182
|
*/
|
|
171
|
-
importSecret(name: string, key: Uint8Array, options?: IImportKeyOptions): Result<IAddSecretResult
|
|
183
|
+
importSecret(name: string, key: Uint8Array, options?: IImportKeyOptions): Promise<Result<IAddSecretResult>>;
|
|
172
184
|
/**
|
|
173
185
|
* Adds a secret derived from a password using PBKDF2.
|
|
174
186
|
*
|
|
@@ -185,12 +197,16 @@ export declare class KeyStore implements IEncryptionProvider {
|
|
|
185
197
|
*/
|
|
186
198
|
addSecretFromPassword(name: string, password: string, options?: IAddSecretFromPasswordOptions): Promise<Result<IAddSecretFromPasswordResult>>;
|
|
187
199
|
/**
|
|
188
|
-
* Removes a secret by name.
|
|
200
|
+
* Removes a secret by name. Vault-first: the in-memory vault entry is dropped
|
|
201
|
+
* before any storage cleanup runs. For asymmetric-keypair entries, best-effort
|
|
202
|
+
* calls {@link CryptoUtils.KeyStore.IPrivateKeyStorage}.delete on the entry's
|
|
203
|
+
* `id`; a failure is reported via `warning` on the result but does not roll
|
|
204
|
+
* back the vault removal.
|
|
189
205
|
* @param name - Name of the secret to remove
|
|
190
|
-
* @returns Success with removed entry, Failure if not found or locked
|
|
206
|
+
* @returns Success with removed entry (and optional warning), Failure if not found or locked
|
|
191
207
|
* @public
|
|
192
208
|
*/
|
|
193
|
-
removeSecret(name: string): Result<
|
|
209
|
+
removeSecret(name: string): Promise<Result<IRemoveSecretResult>>;
|
|
194
210
|
/**
|
|
195
211
|
* Imports an API key string into the vault.
|
|
196
212
|
* The string is UTF-8 encoded and stored with type `'api-key'`.
|
|
@@ -200,7 +216,7 @@ export declare class KeyStore implements IEncryptionProvider {
|
|
|
200
216
|
* @returns Success with entry, Failure if locked, empty, or exists and !replace
|
|
201
217
|
* @public
|
|
202
218
|
*/
|
|
203
|
-
importApiKey(name: string, apiKey: string, options?: IImportSecretOptions): Result<IAddSecretResult
|
|
219
|
+
importApiKey(name: string, apiKey: string, options?: IImportSecretOptions): Promise<Result<IAddSecretResult>>;
|
|
204
220
|
/**
|
|
205
221
|
* Retrieves an API key string by name.
|
|
206
222
|
* Only works for secrets with type `'api-key'`.
|
|
@@ -209,6 +225,41 @@ export declare class KeyStore implements IEncryptionProvider {
|
|
|
209
225
|
* @public
|
|
210
226
|
*/
|
|
211
227
|
getApiKey(name: string): Result<string>;
|
|
228
|
+
/**
|
|
229
|
+
* Adds a new asymmetric keypair to the vault. Storage-first: the private key
|
|
230
|
+
* is stored under a freshly-minted `id` before the public-key vault entry is
|
|
231
|
+
* committed. If the storage call fails, no vault entry is written and the
|
|
232
|
+
* operation returns Failure.
|
|
233
|
+
*
|
|
234
|
+
* When `replace: true` displaces an existing entry (asymmetric or symmetric),
|
|
235
|
+
* a fresh `id` is minted; the displaced entry's resources are released
|
|
236
|
+
* best-effort. Failure of the storage delete is reported via `warning` on the
|
|
237
|
+
* result but does not roll back the replacement.
|
|
238
|
+
*
|
|
239
|
+
* Requires a {@link CryptoUtils.KeyStore.IPrivateKeyStorage} backend
|
|
240
|
+
* supplied at construction.
|
|
241
|
+
*
|
|
242
|
+
* @param name - Unique name for the entry
|
|
243
|
+
* @param options - Algorithm, optional description, replace flag
|
|
244
|
+
* @returns Success with the new entry, Failure if locked, no provider, or storage write failed
|
|
245
|
+
* @public
|
|
246
|
+
*/
|
|
247
|
+
addKeyPair(name: string, options: IAddKeyPairOptions): Promise<Result<IAddKeyPairResult>>;
|
|
248
|
+
/**
|
|
249
|
+
* Retrieves the keypair for an asymmetric-keypair entry. The private key is
|
|
250
|
+
* loaded from {@link CryptoUtils.KeyStore.IPrivateKeyStorage} on every call —
|
|
251
|
+
* the keystore never caches private `CryptoKey` references between calls.
|
|
252
|
+
* The public key is re-imported from the vault's JWK so callers always
|
|
253
|
+
* receive a `CryptoKey` rather than the JWK form.
|
|
254
|
+
* @param name - Name of the entry
|
|
255
|
+
* @returns Success with `{ publicKey, privateKey }`, Failure if not found,
|
|
256
|
+
* locked, wrong type, no provider, or storage load failed.
|
|
257
|
+
* @public
|
|
258
|
+
*/
|
|
259
|
+
getKeyPair(name: string): Promise<Result<{
|
|
260
|
+
publicKey: CryptoKey;
|
|
261
|
+
privateKey: CryptoKey;
|
|
262
|
+
}>>;
|
|
212
263
|
/**
|
|
213
264
|
* Lists secret names filtered by type.
|
|
214
265
|
* @param type - The secret type to filter by
|
|
@@ -223,7 +274,7 @@ export declare class KeyStore implements IEncryptionProvider {
|
|
|
223
274
|
* @returns Success with updated entry, Failure if source not found, target exists, or locked
|
|
224
275
|
* @public
|
|
225
276
|
*/
|
|
226
|
-
renameSecret(oldName: string, newName: string): Result<
|
|
277
|
+
renameSecret(oldName: string, newName: string): Result<IKeyStoreEntry>;
|
|
227
278
|
/**
|
|
228
279
|
* Saves the key store, returning the encrypted file content.
|
|
229
280
|
* Requires the master password to encrypt.
|
|
@@ -280,5 +331,22 @@ export declare class KeyStore implements IEncryptionProvider {
|
|
|
280
331
|
* Shared by `unlock()` and `unlockWithKey()`.
|
|
281
332
|
*/
|
|
282
333
|
private _decryptVault;
|
|
334
|
+
/**
|
|
335
|
+
* Releases the resources held by an entry being displaced from the vault.
|
|
336
|
+
* Symmetric entries get their key buffer zeroed in place. Asymmetric entries
|
|
337
|
+
* have their private-key blob best-effort deleted from
|
|
338
|
+
* {@link CryptoUtils.KeyStore.IPrivateKeyStorage}; if the storage call fails,
|
|
339
|
+
* a warning string is returned but the displacement still proceeds — the
|
|
340
|
+
* orphaned blob is left for consumer-side GC. Without a configured provider,
|
|
341
|
+
* asymmetric cleanup is silently skipped.
|
|
342
|
+
* @returns A warning string if storage cleanup failed, otherwise undefined.
|
|
343
|
+
*/
|
|
344
|
+
private _releaseEntryResources;
|
|
345
|
+
/**
|
|
346
|
+
* Mints a fresh UUID v4 storage handle using the crypto provider's
|
|
347
|
+
* {@link CryptoUtils.ICryptoProvider.generateRandomBytes | generateRandomBytes}.
|
|
348
|
+
* Random-bytes failures propagate as Failure.
|
|
349
|
+
*/
|
|
350
|
+
private _generateId;
|
|
283
351
|
}
|
|
284
352
|
//# sourceMappingURL=keyStore.d.ts.map
|