@vercel/cli-auth 0.3.13 → 0.3.14-snapshot-dd15927e8ded

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.
@@ -0,0 +1,304 @@
1
+ // Seals a secret with a P-256 key that lives in this Mac's Secure Enclave,
2
+ // through the Security framework's C API (called with koffi, so there's no
3
+ // native code to build). Sealing is ECIES (ECDH with the Secure Enclave key,
4
+ // then AES-GCM), so only that key can unseal it. The key is created outside
5
+ // the keychain, so it needs no keychain entitlement and works from any
6
+ // binary, `node` included. What we save is the key as wrapped by this Mac's
7
+ // Secure Enclave (`kSecAttrTokenOID`), which only it can unwrap: a copy of
8
+ // the file can't unseal anything anywhere else.
9
+
10
+ import { type Koffi, loadKoffi } from './koffi';
11
+
12
+ export type Pointer = bigint;
13
+
14
+ const CORE_FOUNDATION =
15
+ '/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation';
16
+ const SECURITY = '/System/Library/Frameworks/Security.framework/Security';
17
+
18
+ const kCFStringEncodingUTF8 = 0x08000100;
19
+ const kCFNumberSInt32Type = 3;
20
+ const kSecAccessControlPrivateKeyUsage = 1 << 30;
21
+ // `SecBase.h` errSecInteractionNotAllowed: the key can't be used while this
22
+ // Mac is locked (`kSecAttrAccessibleWhenUnlockedThisDeviceOnly`).
23
+ const errSecInteractionNotAllowed = -25308;
24
+
25
+ /** The Secure Enclave can't use the key while this Mac is locked. */
26
+ export class SecureEnclaveLockedError extends Error {}
27
+
28
+ let api: ReturnType<typeof loadApi> | undefined;
29
+
30
+ function loadApi(koffi: Koffi) {
31
+ const cf = koffi.load(CORE_FOUNDATION);
32
+ const sec = koffi.load(SECURITY);
33
+ // The value of an exported constant, such as a `CFStringRef`.
34
+ const constant = (lib: typeof cf, name: string): Pointer =>
35
+ koffi.decode(lib.symbol(name), 'void *');
36
+
37
+ return {
38
+ koffi,
39
+ CFRelease: cf.func('void CFRelease(void *cf)'),
40
+ CFDictionaryCreateMutable: cf.func(
41
+ 'void *CFDictionaryCreateMutable(void *allocator, long capacity, void *keyCallBacks, void *valueCallBacks)'
42
+ ),
43
+ CFDictionarySetValue: cf.func(
44
+ 'void CFDictionarySetValue(void *dict, void *key, void *value)'
45
+ ),
46
+ CFDictionaryGetValue: cf.func(
47
+ 'void *CFDictionaryGetValue(void *dict, void *key)'
48
+ ),
49
+ CFDataCreate: cf.func(
50
+ 'void *CFDataCreate(void *allocator, const uint8_t *bytes, long length)'
51
+ ),
52
+ CFDataGetLength: cf.func('long CFDataGetLength(void *data)'),
53
+ CFDataGetBytePtr: cf.func('void *CFDataGetBytePtr(void *data)'),
54
+ CFNumberCreate: cf.func(
55
+ 'void *CFNumberCreate(void *allocator, long type, const int32_t *value)'
56
+ ),
57
+ CFStringCreateWithCString: cf.func(
58
+ 'void *CFStringCreateWithCString(void *allocator, const char *str, uint32_t encoding)'
59
+ ),
60
+ CFErrorGetCode: cf.func('long CFErrorGetCode(void *error)'),
61
+ SecAccessControlCreateWithFlags: sec.func(
62
+ 'void *SecAccessControlCreateWithFlags(void *allocator, void *protection, unsigned long flags, _Out_ void **error)'
63
+ ),
64
+ SecKeyCreateRandomKey: sec.func(
65
+ 'void *SecKeyCreateRandomKey(void *parameters, _Out_ void **error)'
66
+ ),
67
+ SecKeyCreateWithData: sec.func(
68
+ 'void *SecKeyCreateWithData(void *keyData, void *attributes, _Out_ void **error)'
69
+ ),
70
+ SecKeyCopyAttributes: sec.func('void *SecKeyCopyAttributes(void *key)'),
71
+ SecKeyCopyPublicKey: sec.func('void *SecKeyCopyPublicKey(void *key)'),
72
+ SecKeyCreateEncryptedData: sec.func(
73
+ 'void *SecKeyCreateEncryptedData(void *key, void *algorithm, void *plaintext, _Out_ void **error)'
74
+ ),
75
+ SecKeyCreateDecryptedData: sec.func(
76
+ 'void *SecKeyCreateDecryptedData(void *key, void *algorithm, void *ciphertext, _Out_ void **error)'
77
+ ),
78
+ keyCallBacks: cf.symbol('kCFTypeDictionaryKeyCallBacks') as Pointer,
79
+ valueCallBacks: cf.symbol('kCFTypeDictionaryValueCallBacks') as Pointer,
80
+ kCFBooleanFalse: constant(cf, 'kCFBooleanFalse'),
81
+ kSecAttrKeyType: constant(sec, 'kSecAttrKeyType'),
82
+ kSecAttrKeyTypeECSECPrimeRandom: constant(
83
+ sec,
84
+ 'kSecAttrKeyTypeECSECPrimeRandom'
85
+ ),
86
+ kSecAttrKeySizeInBits: constant(sec, 'kSecAttrKeySizeInBits'),
87
+ kSecAttrKeyClass: constant(sec, 'kSecAttrKeyClass'),
88
+ kSecAttrKeyClassPrivate: constant(sec, 'kSecAttrKeyClassPrivate'),
89
+ kSecAttrTokenID: constant(sec, 'kSecAttrTokenID'),
90
+ kSecAttrTokenIDSecureEnclave: constant(sec, 'kSecAttrTokenIDSecureEnclave'),
91
+ kSecPrivateKeyAttrs: constant(sec, 'kSecPrivateKeyAttrs'),
92
+ kSecAttrIsPermanent: constant(sec, 'kSecAttrIsPermanent'),
93
+ kSecAttrAccessControl: constant(sec, 'kSecAttrAccessControl'),
94
+ kSecAttrAccessibleWhenUnlockedThisDeviceOnly: constant(
95
+ sec,
96
+ 'kSecAttrAccessibleWhenUnlockedThisDeviceOnly'
97
+ ),
98
+ kSecKeyAlgorithmECIESEncryptionCofactorVariableIVX963SHA256AESGCM: constant(
99
+ sec,
100
+ 'kSecKeyAlgorithmECIESEncryptionCofactorVariableIVX963SHA256AESGCM'
101
+ ),
102
+ };
103
+ }
104
+
105
+ type Api = ReturnType<typeof loadApi>;
106
+
107
+ /**
108
+ * Runs `fn` with an `own` function that records each Core Foundation object
109
+ * `fn` creates, and releases them all when it returns or throws.
110
+ */
111
+ function withObjects<T>(
112
+ fn: (a: Api, own: (object: Pointer | null, what: string) => Pointer) => T
113
+ ): T {
114
+ const a = (api ??= loadApi(loadKoffi()));
115
+ const objects: Pointer[] = [];
116
+ try {
117
+ return fn(a, (object, what) => {
118
+ if (!object) {
119
+ throw new Error(`${what} failed.`);
120
+ }
121
+ objects.push(object);
122
+ return object;
123
+ });
124
+ } finally {
125
+ for (const object of objects.reverse()) {
126
+ a.CFRelease(object);
127
+ }
128
+ }
129
+ }
130
+
131
+ /** Calls a function that reports failure with a `CFErrorRef` out argument. */
132
+ function check(
133
+ a: Api,
134
+ what: string,
135
+ call: (error: [Pointer | null]) => Pointer | null
136
+ ): Pointer | null {
137
+ const error: [Pointer | null] = [null];
138
+ const result = call(error);
139
+ if (!result && error[0]) {
140
+ const code = a.CFErrorGetCode(error[0]);
141
+ a.CFRelease(error[0]);
142
+ const message = `${what} failed with error ${code}.`;
143
+ if (Number(code) === errSecInteractionNotAllowed) {
144
+ throw new SecureEnclaveLockedError(message);
145
+ }
146
+ throw new Error(message);
147
+ }
148
+ return result;
149
+ }
150
+
151
+ function dictionary(
152
+ a: Api,
153
+ own: (object: Pointer | null, what: string) => Pointer,
154
+ entries: [Pointer, Pointer][]
155
+ ): Pointer {
156
+ const dict = own(
157
+ a.CFDictionaryCreateMutable(null, 0, a.keyCallBacks, a.valueCallBacks),
158
+ 'CFDictionaryCreateMutable'
159
+ );
160
+ for (const [key, value] of entries) {
161
+ a.CFDictionarySetValue(dict, key, value);
162
+ }
163
+ return dict;
164
+ }
165
+
166
+ function toData(
167
+ a: Api,
168
+ own: (object: Pointer | null, what: string) => Pointer,
169
+ bytes: Buffer | Uint8Array
170
+ ): Pointer {
171
+ return own(a.CFDataCreate(null, bytes, bytes.length), 'CFDataCreate');
172
+ }
173
+
174
+ function fromData(a: Api, data: Pointer): Buffer {
175
+ const length = Number(a.CFDataGetLength(data));
176
+ if (length === 0) {
177
+ return Buffer.alloc(0);
178
+ }
179
+ // Copy out of the CFData before it's released.
180
+ return Buffer.from(
181
+ new Uint8Array(a.koffi.view(a.CFDataGetBytePtr(data), length))
182
+ );
183
+ }
184
+
185
+ // Not exported by the SDK headers: the attribute that holds a Secure Enclave
186
+ // key's wrapped form, which CryptoKit calls `dataRepresentation`.
187
+ function tokenOid(
188
+ a: Api,
189
+ own: (object: Pointer | null, what: string) => Pointer
190
+ ): Pointer {
191
+ return own(
192
+ a.CFStringCreateWithCString(null, 'toid', kCFStringEncodingUTF8),
193
+ 'CFStringCreateWithCString'
194
+ );
195
+ }
196
+
197
+ /**
198
+ * Create a key in this Mac's Secure Enclave and seal `secret` with it.
199
+ * Returns the key as wrapped by the Secure Enclave, and the sealed secret.
200
+ */
201
+ export function createSecureEnclaveKey(secret: Buffer): {
202
+ wrapped: Buffer;
203
+ sealed: Buffer;
204
+ } {
205
+ return withObjects((a, own) => {
206
+ const accessControl = own(
207
+ check(a, 'SecAccessControlCreateWithFlags', error =>
208
+ a.SecAccessControlCreateWithFlags(
209
+ null,
210
+ a.kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
211
+ kSecAccessControlPrivateKeyUsage,
212
+ error
213
+ )
214
+ ),
215
+ 'SecAccessControlCreateWithFlags'
216
+ );
217
+ const keySize = own(
218
+ a.CFNumberCreate(null, kCFNumberSInt32Type, Int32Array.of(256)),
219
+ 'CFNumberCreate'
220
+ );
221
+ const parameters = dictionary(a, own, [
222
+ [a.kSecAttrKeyType, a.kSecAttrKeyTypeECSECPrimeRandom],
223
+ [a.kSecAttrKeySizeInBits, keySize],
224
+ [a.kSecAttrTokenID, a.kSecAttrTokenIDSecureEnclave],
225
+ [
226
+ a.kSecPrivateKeyAttrs,
227
+ dictionary(a, own, [
228
+ // Not in the keychain, which would need a keychain entitlement.
229
+ [a.kSecAttrIsPermanent, a.kCFBooleanFalse],
230
+ [a.kSecAttrAccessControl, accessControl],
231
+ ]),
232
+ ],
233
+ ]);
234
+ const key = own(
235
+ check(a, 'SecKeyCreateRandomKey', error =>
236
+ a.SecKeyCreateRandomKey(parameters, error)
237
+ ),
238
+ 'SecKeyCreateRandomKey'
239
+ );
240
+
241
+ const attributes = own(a.SecKeyCopyAttributes(key), 'SecKeyCopyAttributes');
242
+ const wrapped = a.CFDictionaryGetValue(attributes, tokenOid(a, own));
243
+ if (!wrapped) {
244
+ throw new Error("The Secure Enclave didn't return the wrapped key.");
245
+ }
246
+
247
+ const publicKey = own(a.SecKeyCopyPublicKey(key), 'SecKeyCopyPublicKey');
248
+ const sealed = own(
249
+ check(a, 'SecKeyCreateEncryptedData', error =>
250
+ a.SecKeyCreateEncryptedData(
251
+ publicKey,
252
+ a.kSecKeyAlgorithmECIESEncryptionCofactorVariableIVX963SHA256AESGCM,
253
+ toData(a, own, secret),
254
+ error
255
+ )
256
+ ),
257
+ 'SecKeyCreateEncryptedData'
258
+ );
259
+
260
+ return { wrapped: fromData(a, wrapped), sealed: fromData(a, sealed) };
261
+ });
262
+ }
263
+
264
+ /**
265
+ * Unseal what `createSecureEnclaveKey` sealed, with the key `wrapped` by
266
+ * this Mac's Secure Enclave. The Secure Enclave's private key never leaves
267
+ * it. Fails while the Mac is locked.
268
+ */
269
+ export function unsealWithSecureEnclaveKey(
270
+ wrapped: Buffer,
271
+ sealed: Buffer
272
+ ): Buffer {
273
+ return withObjects((a, own) => {
274
+ const attributes = dictionary(a, own, [
275
+ [a.kSecAttrKeyType, a.kSecAttrKeyTypeECSECPrimeRandom],
276
+ [a.kSecAttrKeyClass, a.kSecAttrKeyClassPrivate],
277
+ [a.kSecAttrTokenID, a.kSecAttrTokenIDSecureEnclave],
278
+ [tokenOid(a, own), toData(a, own, wrapped)],
279
+ ]);
280
+ // With a token ID the key comes from the attributes, not `keyData`.
281
+ const key = own(
282
+ check(a, 'SecKeyCreateWithData', error =>
283
+ a.SecKeyCreateWithData(
284
+ toData(a, own, new Uint8Array(0)),
285
+ attributes,
286
+ error
287
+ )
288
+ ),
289
+ 'SecKeyCreateWithData'
290
+ );
291
+ const secret = own(
292
+ check(a, 'SecKeyCreateDecryptedData', error =>
293
+ a.SecKeyCreateDecryptedData(
294
+ key,
295
+ a.kSecKeyAlgorithmECIESEncryptionCofactorVariableIVX963SHA256AESGCM,
296
+ toData(a, own, sealed),
297
+ error
298
+ )
299
+ ),
300
+ 'SecKeyCreateDecryptedData'
301
+ );
302
+ return fromData(a, secret);
303
+ });
304
+ }
package/tpm.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ export type Handle = number | bigint;
2
+ /**
3
+ * Create an RSA key named `name` in this PC's TPM, which may only decrypt,
4
+ * and seal `secret` with it. Returns the sealed secret.
5
+ */
6
+ export declare function createTpmKey(name: string, secret: Buffer): Buffer;
7
+ /**
8
+ * Unseal what `createTpmKey` sealed, with the key named `name` in this PC's
9
+ * TPM. The TPM's private key never leaves it.
10
+ */
11
+ export declare function unsealWithTpmKey(name: string, sealed: Buffer): Buffer;
12
+ /** Delete the key named `name` from this PC's TPM, if it's there. */
13
+ export declare function deleteTpmKey(name: string): void;
package/tpm.js ADDED
@@ -0,0 +1,160 @@
1
+ "use strict";
2
+ // Seals a secret with an RSA key that lives in this PC's TPM, through CNG's
3
+ // Platform Crypto Provider (called with koffi, so there's no native code to
4
+ // build). The TPM keeps the private key and only hands out what it unseals:
5
+ // what we save is the key's name, which is useless on any other PC.
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.createTpmKey = createTpmKey;
8
+ exports.unsealWithTpmKey = unsealWithTpmKey;
9
+ exports.deleteTpmKey = deleteTpmKey;
10
+ const koffi_1 = require("./koffi");
11
+ // String constants from `ncrypt.h` and `bcrypt.h`.
12
+ const PLATFORM_CRYPTO_PROVIDER = 'Microsoft Platform Crypto Provider';
13
+ const NCRYPT_RSA_ALGORITHM = 'RSA';
14
+ const NCRYPT_LENGTH_PROPERTY = 'Length';
15
+ const NCRYPT_KEY_USAGE_PROPERTY = 'Key Usage';
16
+ const BCRYPT_SHA256_ALGORITHM = 'SHA256';
17
+ const RSA_KEY_BITS = 2048;
18
+ // `ncrypt.h`: the key may only decrypt.
19
+ const NCRYPT_ALLOW_DECRYPT_FLAG = 0x1;
20
+ // `ncrypt.h`: RSA-OAEP, with a `BCRYPT_OAEP_PADDING_INFO`.
21
+ const NCRYPT_PAD_OAEP_FLAG = 0x4;
22
+ // `ncrypt.h`: fail instead of showing UI, since the CLI may not be
23
+ // interactive.
24
+ const NCRYPT_SILENT_FLAG = 0x40;
25
+ // `winerror.h` NTE_BAD_KEYSET: there's no key with that name.
26
+ const NTE_BAD_KEYSET = 0x80090016;
27
+ let api;
28
+ function loadApi(koffi) {
29
+ const ncrypt = koffi.load('ncrypt.dll');
30
+ koffi.struct('BCRYPT_OAEP_PADDING_INFO', {
31
+ pszAlgId: 'str16',
32
+ pbLabel: 'void *',
33
+ cbLabel: 'uint32_t',
34
+ });
35
+ return {
36
+ NCryptOpenStorageProvider: ncrypt.func('int32_t __stdcall NCryptOpenStorageProvider(_Out_ uintptr_t *phProvider, const char16_t *pszProviderName, uint32_t dwFlags)'),
37
+ NCryptCreatePersistedKey: ncrypt.func('int32_t __stdcall NCryptCreatePersistedKey(uintptr_t hProvider, _Out_ uintptr_t *phKey, const char16_t *pszAlgId, const char16_t *pszKeyName, uint32_t dwLegacyKeySpec, uint32_t dwFlags)'),
38
+ NCryptSetProperty: ncrypt.func('int32_t __stdcall NCryptSetProperty(uintptr_t hObject, const char16_t *pszProperty, const uint8_t *pbInput, uint32_t cbInput, uint32_t dwFlags)'),
39
+ NCryptFinalizeKey: ncrypt.func('int32_t __stdcall NCryptFinalizeKey(uintptr_t hKey, uint32_t dwFlags)'),
40
+ NCryptOpenKey: ncrypt.func('int32_t __stdcall NCryptOpenKey(uintptr_t hProvider, _Out_ uintptr_t *phKey, const char16_t *pszKeyName, uint32_t dwLegacyKeySpec, uint32_t dwFlags)'),
41
+ NCryptEncrypt: ncrypt.func('int32_t __stdcall NCryptEncrypt(uintptr_t hKey, const uint8_t *pbInput, uint32_t cbInput, const BCRYPT_OAEP_PADDING_INFO *pPaddingInfo, _Out_ uint8_t *pbOutput, uint32_t cbOutput, _Out_ uint32_t *pcbResult, uint32_t dwFlags)'),
42
+ NCryptDecrypt: ncrypt.func('int32_t __stdcall NCryptDecrypt(uintptr_t hKey, const uint8_t *pbInput, uint32_t cbInput, const BCRYPT_OAEP_PADDING_INFO *pPaddingInfo, _Out_ uint8_t *pbOutput, uint32_t cbOutput, _Out_ uint32_t *pcbResult, uint32_t dwFlags)'),
43
+ NCryptDeleteKey: ncrypt.func('int32_t __stdcall NCryptDeleteKey(uintptr_t hKey, uint32_t dwFlags)'),
44
+ NCryptFreeObject: ncrypt.func('int32_t __stdcall NCryptFreeObject(uintptr_t hObject)'),
45
+ };
46
+ }
47
+ class TpmError extends Error {
48
+ constructor(what, status) {
49
+ super(`${what} failed with 0x${(status >>> 0).toString(16)}.`);
50
+ this.status = status;
51
+ }
52
+ }
53
+ function check(what, status) {
54
+ if (status !== 0) {
55
+ throw new TpmError(what, status);
56
+ }
57
+ }
58
+ function withProvider(fn) {
59
+ const a = (api ??= loadApi((0, koffi_1.loadKoffi)()));
60
+ const provider = [0];
61
+ check('NCryptOpenStorageProvider', a.NCryptOpenStorageProvider(provider, PLATFORM_CRYPTO_PROVIDER, 0));
62
+ try {
63
+ return fn(a, provider[0]);
64
+ }
65
+ finally {
66
+ a.NCryptFreeObject(provider[0]);
67
+ }
68
+ }
69
+ function openKey(a, provider, name) {
70
+ const key = [0];
71
+ check('NCryptOpenKey', a.NCryptOpenKey(provider, key, name, 0, NCRYPT_SILENT_FLAG));
72
+ return key[0];
73
+ }
74
+ /**
75
+ * Create an RSA key named `name` in this PC's TPM, which may only decrypt,
76
+ * and seal `secret` with it. Returns the sealed secret.
77
+ */
78
+ function createTpmKey(name, secret) {
79
+ return withProvider((a, provider) => {
80
+ const handle = [0];
81
+ check('NCryptCreatePersistedKey', a.NCryptCreatePersistedKey(provider, handle, NCRYPT_RSA_ALGORITHM, name, 0, 0));
82
+ const key = handle[0];
83
+ let finalized = false;
84
+ try {
85
+ setDword(a, key, NCRYPT_LENGTH_PROPERTY, RSA_KEY_BITS);
86
+ setDword(a, key, NCRYPT_KEY_USAGE_PROPERTY, NCRYPT_ALLOW_DECRYPT_FLAG);
87
+ check('NCryptFinalizeKey', a.NCryptFinalizeKey(key, NCRYPT_SILENT_FLAG));
88
+ finalized = true;
89
+ const sealed = oaep(a, 'NCryptEncrypt', key, secret);
90
+ a.NCryptFreeObject(key);
91
+ return sealed;
92
+ }
93
+ catch (error) {
94
+ if (finalized) {
95
+ // Also frees the handle.
96
+ a.NCryptDeleteKey(key, NCRYPT_SILENT_FLAG);
97
+ }
98
+ else {
99
+ a.NCryptFreeObject(key);
100
+ }
101
+ throw error;
102
+ }
103
+ });
104
+ }
105
+ /**
106
+ * Unseal what `createTpmKey` sealed, with the key named `name` in this PC's
107
+ * TPM. The TPM's private key never leaves it.
108
+ */
109
+ function unsealWithTpmKey(name, sealed) {
110
+ return withProvider((a, provider) => {
111
+ const key = openKey(a, provider, name);
112
+ try {
113
+ return oaep(a, 'NCryptDecrypt', key, sealed);
114
+ }
115
+ finally {
116
+ a.NCryptFreeObject(key);
117
+ }
118
+ });
119
+ }
120
+ function setDword(a, key, property, value) {
121
+ const bytes = Buffer.alloc(4);
122
+ bytes.writeUInt32LE(value);
123
+ check(`NCryptSetProperty(${property})`, a.NCryptSetProperty(key, property, bytes, bytes.length, 0));
124
+ }
125
+ // Encrypt or decrypt with RSA-OAEP and SHA-256.
126
+ function oaep(a, what, key, input) {
127
+ const output = Buffer.alloc(RSA_KEY_BITS / 8);
128
+ const size = [0];
129
+ check(what, a[what](key, input, input.length, { pszAlgId: BCRYPT_SHA256_ALGORITHM, pbLabel: null, cbLabel: 0 }, output, output.length, size,
130
+ // Only decrypting uses the private key, which may want UI.
131
+ what === 'NCryptDecrypt'
132
+ ? NCRYPT_PAD_OAEP_FLAG | NCRYPT_SILENT_FLAG
133
+ : NCRYPT_PAD_OAEP_FLAG));
134
+ const result = Buffer.from(new Uint8Array(output.subarray(0, size[0])));
135
+ // Don't leave an unsealed secret behind.
136
+ output.fill(0);
137
+ return result;
138
+ }
139
+ /** Delete the key named `name` from this PC's TPM, if it's there. */
140
+ function deleteTpmKey(name) {
141
+ withProvider((a, provider) => {
142
+ let key;
143
+ try {
144
+ key = openKey(a, provider, name);
145
+ }
146
+ catch (error) {
147
+ if (error instanceof TpmError && error.status >>> 0 === NTE_BAD_KEYSET) {
148
+ return;
149
+ }
150
+ throw error;
151
+ }
152
+ // Frees the handle when it succeeds.
153
+ const status = a.NCryptDeleteKey(key, NCRYPT_SILENT_FLAG);
154
+ if (status !== 0) {
155
+ a.NCryptFreeObject(key);
156
+ }
157
+ check('NCryptDeleteKey', status);
158
+ });
159
+ }
160
+ //# sourceMappingURL=tpm.js.map
package/tpm.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tpm.js","sourceRoot":"","sources":["tpm.ts"],"names":[],"mappings":";AAAA,4EAA4E;AAC5E,4EAA4E;AAC5E,4EAA4E;AAC5E,oEAAoE;;AA2GpE,oCAkCC;AAMD,4CASC;AA2CD,oCAkBC;AAvND,mCAAgD;AAIhD,mDAAmD;AACnD,MAAM,wBAAwB,GAAG,oCAAoC,CAAC;AACtE,MAAM,oBAAoB,GAAG,KAAK,CAAC;AACnC,MAAM,sBAAsB,GAAG,QAAQ,CAAC;AACxC,MAAM,yBAAyB,GAAG,WAAW,CAAC;AAC9C,MAAM,uBAAuB,GAAG,QAAQ,CAAC;AACzC,MAAM,YAAY,GAAG,IAAI,CAAC;AAC1B,wCAAwC;AACxC,MAAM,yBAAyB,GAAG,GAAG,CAAC;AACtC,2DAA2D;AAC3D,MAAM,oBAAoB,GAAG,GAAG,CAAC;AACjC,mEAAmE;AACnE,eAAe;AACf,MAAM,kBAAkB,GAAG,IAAI,CAAC;AAChC,8DAA8D;AAC9D,MAAM,cAAc,GAAG,UAAU,CAAC;AAElC,IAAI,GAA2C,CAAC;AAEhD,SAAS,OAAO,CAAC,KAAY;IAC3B,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IACxC,KAAK,CAAC,MAAM,CAAC,0BAA0B,EAAE;QACvC,QAAQ,EAAE,OAAO;QACjB,OAAO,EAAE,QAAQ;QACjB,OAAO,EAAE,UAAU;KACpB,CAAC,CAAC;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