ds-01 1.0.3 → 1.0.5

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,361 @@
1
+ // cli/src/utils/secureKeyStore.ts
2
+
3
+ import { execFile } from "node:child_process";
4
+ import { promisify } from "node:util";
5
+ import os from "node:os";
6
+ import path from "node:path";
7
+ import { fileURLToPath } from "node:url";
8
+
9
+ const execFileAsync = promisify(execFile);
10
+
11
+ const KEY_NAME = "DS01-CLI-Device-Key";
12
+
13
+ const __filename = fileURLToPath(import.meta.url);
14
+ const __dirname = path.dirname(__filename);
15
+
16
+ function getMacHelperPath(): string {
17
+ // dist/utils/secureKeyStore.js
18
+ // ../native/macos-key-helper.swift
19
+ return path.resolve(
20
+ __dirname,
21
+ "../native/macos-key-helper.swift",
22
+ );
23
+ }
24
+
25
+ function ensureSupportedOS() {
26
+ const platform = os.platform();
27
+
28
+ if (platform !== "win32" && platform !== "darwin") {
29
+ throw new Error(
30
+ "DS01 secure device keys currently support Windows and macOS.",
31
+ );
32
+ }
33
+ }
34
+
35
+ /**
36
+ * ---------------------------------------------------------
37
+ * WINDOWS
38
+ * ---------------------------------------------------------
39
+ */
40
+
41
+ async function createWindowsDeviceKey(): Promise<void> {
42
+ const script = `
43
+ $ErrorActionPreference = "Stop"
44
+
45
+ $existing = Get-ChildItem Cert:\\CurrentUser\\My |
46
+ Where-Object {
47
+ $_.Subject -eq "CN=${KEY_NAME}"
48
+ } |
49
+ Select-Object -First 1
50
+
51
+ if ($existing) {
52
+ Write-Output "EXISTS"
53
+ exit 0
54
+ }
55
+
56
+ $params = @{
57
+ Type = "Custom"
58
+ Subject = "CN=${KEY_NAME}"
59
+
60
+ Provider = "Microsoft Platform Crypto Provider"
61
+
62
+ KeyAlgorithm = "RSA"
63
+ KeyLength = 2048
64
+
65
+ KeyExportPolicy = "NonExportable"
66
+
67
+ KeyUsage = "DigitalSignature"
68
+ KeySpec = "Signature"
69
+
70
+ CertStoreLocation = "Cert:\\CurrentUser\\My"
71
+
72
+ NotAfter = (Get-Date).AddYears(10)
73
+ }
74
+
75
+ New-SelfSignedCertificate @params | Out-Null
76
+
77
+ Write-Output "CREATED"
78
+ `;
79
+
80
+ const { stdout } = await execFileAsync(
81
+ "powershell.exe",
82
+ [
83
+ "-NoProfile",
84
+ "-NonInteractive",
85
+ "-ExecutionPolicy",
86
+ "Bypass",
87
+ "-Command",
88
+ script,
89
+ ],
90
+ {
91
+ windowsHide: true,
92
+ },
93
+ );
94
+
95
+ const result = stdout.trim();
96
+
97
+ if (result !== "CREATED" && result !== "EXISTS") {
98
+ throw new Error("Failed to create Windows DS01 device key.");
99
+ }
100
+ }
101
+
102
+ async function getWindowsPublicKey(): Promise<string> {
103
+ const script = `
104
+ $ErrorActionPreference = "Stop"
105
+
106
+ $cert = Get-ChildItem Cert:\\CurrentUser\\My |
107
+ Where-Object {
108
+ $_.Subject -eq "CN=${KEY_NAME}"
109
+ } |
110
+ Select-Object -First 1
111
+
112
+ if (-not $cert) {
113
+ throw "DS01 device key not found."
114
+ }
115
+
116
+ [Convert]::ToBase64String($cert.RawData)
117
+ `;
118
+
119
+ const { stdout } = await execFileAsync(
120
+ "powershell.exe",
121
+ [
122
+ "-NoProfile",
123
+ "-NonInteractive",
124
+ "-ExecutionPolicy",
125
+ "Bypass",
126
+ "-Command",
127
+ script,
128
+ ],
129
+ {
130
+ windowsHide: true,
131
+ },
132
+ );
133
+
134
+ const certificate = stdout.trim();
135
+
136
+ if (!certificate) {
137
+ throw new Error("Unable to read Windows DS01 public certificate.");
138
+ }
139
+
140
+ return certificate;
141
+ }
142
+
143
+ async function signWindowsDeviceKey(
144
+ data: Uint8Array,
145
+ ): Promise<Uint8Array> {
146
+ const dataBase64 = Buffer.from(data).toString("base64");
147
+
148
+ const script = `
149
+ $ErrorActionPreference = "Stop"
150
+
151
+ $cert = Get-ChildItem Cert:\\CurrentUser\\My |
152
+ Where-Object {
153
+ $_.Subject -eq "CN=${KEY_NAME}"
154
+ } |
155
+ Select-Object -First 1
156
+
157
+ if (-not $cert) {
158
+ throw "DS01 device key not found."
159
+ }
160
+
161
+ if (-not $cert.HasPrivateKey) {
162
+ throw "DS01 device certificate has no private key."
163
+ }
164
+
165
+ $rsa = [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPrivateKey($cert)
166
+
167
+ if (-not $rsa) {
168
+ throw "Unable to access DS01 private signing key."
169
+ }
170
+
171
+ try {
172
+ $data = [Convert]::FromBase64String("${dataBase64}")
173
+
174
+ $signature = $rsa.SignData(
175
+ $data,
176
+ [System.Security.Cryptography.HashAlgorithmName]::SHA256,
177
+ [System.Security.Cryptography.RSASignaturePadding]::Pkcs1
178
+ )
179
+
180
+ [Convert]::ToBase64String($signature)
181
+ }
182
+ finally {
183
+ $rsa.Dispose()
184
+ }
185
+ `;
186
+
187
+ const { stdout } = await execFileAsync(
188
+ "powershell.exe",
189
+ [
190
+ "-NoProfile",
191
+ "-NonInteractive",
192
+ "-ExecutionPolicy",
193
+ "Bypass",
194
+ "-Command",
195
+ script,
196
+ ],
197
+ {
198
+ windowsHide: true,
199
+ },
200
+ );
201
+
202
+ const signature = stdout.trim();
203
+
204
+ if (!signature) {
205
+ throw new Error("Failed to sign data with Windows device key.");
206
+ }
207
+
208
+ return new Uint8Array(Buffer.from(signature, "base64"));
209
+ }
210
+
211
+ async function deleteWindowsDeviceKey(): Promise<void> {
212
+ const script = `
213
+ $ErrorActionPreference = "Stop"
214
+
215
+ $cert = Get-ChildItem Cert:\\CurrentUser\\My |
216
+ Where-Object {
217
+ $_.Subject -eq "CN=${KEY_NAME}"
218
+ } |
219
+ Select-Object -First 1
220
+
221
+ if ($cert) {
222
+ Remove-Item $cert.PSPath
223
+ }
224
+ `;
225
+
226
+ await execFileAsync(
227
+ "powershell.exe",
228
+ [
229
+ "-NoProfile",
230
+ "-NonInteractive",
231
+ "-ExecutionPolicy",
232
+ "Bypass",
233
+ "-Command",
234
+ script,
235
+ ],
236
+ {
237
+ windowsHide: true,
238
+ },
239
+ );
240
+ }
241
+
242
+ /**
243
+ * ---------------------------------------------------------
244
+ * MACOS
245
+ * ---------------------------------------------------------
246
+ */
247
+
248
+ async function runMacHelper(
249
+ operation: "create" | "public" | "sign" | "delete",
250
+ data?: Uint8Array,
251
+ ): Promise<string> {
252
+ const helper = getMacHelperPath();
253
+
254
+ const args = [helper, operation];
255
+
256
+ if (data) {
257
+ args.push(Buffer.from(data).toString("base64"));
258
+ }
259
+
260
+ const { stdout } = await execFileAsync(
261
+ "swift",
262
+ args,
263
+ {
264
+ windowsHide: true,
265
+ },
266
+ );
267
+
268
+ return stdout.trim();
269
+ }
270
+
271
+ async function createMacDeviceKey(): Promise<void> {
272
+ const result = await runMacHelper("create");
273
+
274
+ if (result !== "CREATED" && result !== "EXISTS") {
275
+ throw new Error(
276
+ result || "Failed to create macOS Secure Enclave device key.",
277
+ );
278
+ }
279
+ }
280
+
281
+ async function getMacPublicKey(): Promise<string> {
282
+ const result = await runMacHelper("public");
283
+
284
+ if (!result) {
285
+ throw new Error(
286
+ "Unable to read macOS DS01 public key.",
287
+ );
288
+ }
289
+
290
+ return result;
291
+ }
292
+
293
+ async function signMacDeviceKey(
294
+ data: Uint8Array,
295
+ ): Promise<Uint8Array> {
296
+ const result = await runMacHelper("sign", data);
297
+
298
+ if (!result) {
299
+ throw new Error(
300
+ "Failed to sign data with macOS Secure Enclave key.",
301
+ );
302
+ }
303
+
304
+ return new Uint8Array(
305
+ Buffer.from(result, "base64"),
306
+ );
307
+ }
308
+
309
+ async function deleteMacDeviceKey(): Promise<void> {
310
+ await runMacHelper("delete");
311
+ }
312
+
313
+ /**
314
+ * ---------------------------------------------------------
315
+ * PUBLIC API
316
+ * ---------------------------------------------------------
317
+ */
318
+
319
+ export async function createDeviceKey(): Promise<void> {
320
+ ensureSupportedOS();
321
+
322
+ if (os.platform() === "win32") {
323
+ await createWindowsDeviceKey();
324
+ return;
325
+ }
326
+
327
+ await createMacDeviceKey();
328
+ }
329
+
330
+ export async function getDevicePublicKey(): Promise<string> {
331
+ ensureSupportedOS();
332
+
333
+ if (os.platform() === "win32") {
334
+ return getWindowsPublicKey();
335
+ }
336
+
337
+ return getMacPublicKey();
338
+ }
339
+
340
+ export async function signWithDeviceKey(
341
+ data: Uint8Array,
342
+ ): Promise<Uint8Array> {
343
+ ensureSupportedOS();
344
+
345
+ if (os.platform() === "win32") {
346
+ return signWindowsDeviceKey(data);
347
+ }
348
+
349
+ return signMacDeviceKey(data);
350
+ }
351
+
352
+ export async function deleteDeviceKey(): Promise<void> {
353
+ ensureSupportedOS();
354
+
355
+ if (os.platform() === "win32") {
356
+ await deleteWindowsDeviceKey();
357
+ return;
358
+ }
359
+
360
+ await deleteMacDeviceKey();
361
+ }