node-opcua-pki 3.0.2 → 3.1.0

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.
Files changed (53) hide show
  1. package/.ignore +6 -6
  2. package/.prettierrc +5 -5
  3. package/LICENSE +22 -22
  4. package/bin/crypto_create_CA.js +0 -0
  5. package/bin/crypto_create_CA_config.example.js +18 -18
  6. package/bin/install_prerequisite.js +9 -9
  7. package/dist/crypto_create_CA.d.ts +2 -2
  8. package/dist/crypto_create_CA.js +897 -897
  9. package/dist/index.d.ts +6 -6
  10. package/dist/index.js +44 -44
  11. package/dist/misc/applicationurn.d.ts +1 -1
  12. package/dist/misc/applicationurn.js +46 -46
  13. package/dist/misc/hostname.d.ts +8 -8
  14. package/dist/misc/hostname.js +102 -102
  15. package/dist/misc/install_prerequisite.d.ts +9 -9
  16. package/dist/misc/install_prerequisite.js +363 -360
  17. package/dist/misc/install_prerequisite.js.map +1 -1
  18. package/dist/misc/subject.d.ts +26 -26
  19. package/dist/misc/subject.js +121 -121
  20. package/dist/pki/certificate_authority.d.ts +61 -61
  21. package/dist/pki/certificate_authority.js +481 -481
  22. package/dist/pki/certificate_manager.d.ts +144 -144
  23. package/dist/pki/certificate_manager.js +883 -883
  24. package/dist/pki/certificate_manager.js.map +1 -1
  25. package/dist/pki/common.d.ts +5 -5
  26. package/dist/pki/common.js +2 -2
  27. package/dist/pki/templates/ca_config_template.cnf.d.ts +2 -2
  28. package/dist/pki/templates/ca_config_template.cnf.js +129 -129
  29. package/dist/pki/templates/simple_config_template.cnf.d.ts +2 -2
  30. package/dist/pki/templates/simple_config_template.cnf.js +75 -75
  31. package/dist/pki/toolbox.d.ts +160 -160
  32. package/dist/pki/toolbox.js +699 -699
  33. package/dist/pki/toolbox_pfx.js +18 -18
  34. package/lib/crypto_create_CA.ts +1135 -1135
  35. package/lib/index.ts +28 -28
  36. package/lib/misc/applicationurn.ts +45 -45
  37. package/lib/misc/hostname.ts +89 -89
  38. package/lib/misc/install_prerequisite.ts +454 -454
  39. package/lib/misc/subject.ts +141 -141
  40. package/lib/pki/certificate_manager.ts +1 -1
  41. package/lib/pki/common.ts +5 -5
  42. package/lib/pki/templates/ca_config_template.cnf.ts +129 -129
  43. package/lib/pki/templates/simple_config_template.cnf.ts +75 -75
  44. package/lib/pki/toolbox_pfx.ts +19 -19
  45. package/package.json +89 -89
  46. package/readme.md +214 -214
  47. package/tsconfig.json +20 -20
  48. package/dist/misc/fs.d.ts +0 -24
  49. package/dist/misc/fs.js +0 -21
  50. package/dist/misc/fs.js.map +0 -1
  51. package/dist/misc/get_default_filesystem.d.ts +0 -2
  52. package/dist/misc/get_default_filesystem.js +0 -9
  53. package/dist/misc/get_default_filesystem.js.map +0 -1
@@ -1,454 +1,454 @@
1
- // ---------------------------------------------------------------------------------------------------------------------
2
- // node-opcua
3
- // ---------------------------------------------------------------------------------------------------------------------
4
- // Copyright (c) 2014-2022 - Etienne Rossignon - etienne.rossignon (at) gadz.org
5
- // Copyright (c) 2022 - Sterfive.com
6
- // ---------------------------------------------------------------------------------------------------------------------
7
- //
8
- // This project is licensed under the terms of the MIT license.
9
- //
10
- // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
11
- // documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
12
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
13
- // permit persons to whom the Software is furnished to do so, subject to the following conditions:
14
- //
15
- // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
16
- // Software.
17
- //
18
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
19
- // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20
- // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21
- // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
- // ---------------------------------------------------------------------------------------------------------------------
23
- // tslint:disable:no-console
24
- // tslint:disable:no-shadowed-variable
25
-
26
- import * as fs from "fs";
27
- import * as os from "os";
28
- import * as path from "path";
29
- import * as url from "url";
30
- import * as assert from "assert";
31
- import * as byline from "byline";
32
- import * as chalk from "chalk";
33
- import * as child_process from "child_process";
34
- import * as ProgressBar from "progress";
35
- import * as yauzl from "yauzl";
36
- import { Readable } from "stream";
37
-
38
- import Table = require("cli-table");
39
-
40
- const doDebug = process.env.NODEOPCUAPKIDEBUG || false;
41
-
42
- declare interface ProxyOptions {
43
- host: string;
44
- port: number;
45
- localAddress?: string;
46
- proxyAuth?: string;
47
- headers?: { [key: string]: any };
48
- protocol: string; // "https" | "http"
49
- }
50
- declare interface WgetOptions {
51
- gunzip?: boolean;
52
- proxy?: ProxyOptions;
53
- }
54
-
55
- declare interface WgetInterface {
56
- download(url: string, outputFilename: string, options: WgetOptions): any;
57
- }
58
-
59
- // tslint:disable-next-line:no-var-requires
60
- // eslint-disable-next-line @typescript-eslint/no-var-requires
61
- const wget = require("wget-improved-2") as WgetInterface;
62
-
63
- type CallbackFunc<T> = (err: Error | null, result?: T) => void;
64
-
65
- interface ExecuteResult {
66
- exitCode: number;
67
- output: string;
68
- }
69
-
70
- function makeOptions(): WgetOptions {
71
- const proxy =
72
- process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy || undefined;
73
- if (proxy) {
74
- const a = new url.URL(proxy);
75
- const auth = a.username ? a.username + ":" + a.password : undefined;
76
-
77
- const options: WgetOptions = {
78
- proxy: {
79
- port: a.port ? parseInt(a.port, 10) : 80,
80
- protocol: a.protocol.replace(":", ""),
81
- host: a.hostname ?? "",
82
- proxyAuth: auth,
83
- },
84
- };
85
- console.log(chalk.green("- using proxy "), proxy);
86
- console.log(options);
87
- return options;
88
- }
89
- return {};
90
- }
91
-
92
- function execute(cmd: string, callback: CallbackFunc<ExecuteResult>, cwd?: string) {
93
- let output = "";
94
-
95
- // xx cwd = cwd ? {cwd: cwd} : {};
96
- const options = {
97
- cwd,
98
- windowsHide: true,
99
- };
100
-
101
- const child = child_process.exec(
102
- cmd,
103
- options,
104
- (err: child_process.ExecException | null /*, stdout: string, stderr: string*/) => {
105
- const exitCode = err === null ? 0 : err!.code!;
106
- callback(err ? err : null, { exitCode, output });
107
- }
108
- );
109
-
110
- const stream1 = byline(child.stdout!);
111
- stream1.on("data", (line: string) => {
112
- output += line + "\n";
113
- // istanbul ignore next
114
- if (doDebug) {
115
- process.stdout.write(" stdout " + chalk.yellow(line) + "\n");
116
- }
117
- });
118
- }
119
-
120
- function quote(str: string): string {
121
- return '"' + str.replace(/\\/g, "/") + "\"";
122
- }
123
-
124
- function is_expected_openssl_version(strVersion: string): boolean {
125
- return !!strVersion.match(/OpenSSL 1|3/);
126
- }
127
-
128
- export function check_system_openssl_version(callback: (err: Error | null, output?: string) => void) {
129
- execute("which openssl", (err: Error | null, result?: ExecuteResult) => {
130
- // istanbul ignore next
131
- if (err) {
132
- console.log("warning: ", err.message);
133
- return callback(new Error("Cannot find openssl"));
134
- }
135
-
136
- const exitCode = result!.exitCode;
137
- const output = result!.output;
138
-
139
- if (exitCode !== 0) {
140
- console.log(
141
- chalk.yellow(" it seems that ") + chalk.cyan("openssl") + chalk.yellow(" is not installed on your computer ")
142
- );
143
- console.log(chalk.yellow("Please install it before running this programs"));
144
-
145
- return callback(new Error("Cannot find openssl"));
146
- }
147
- const opensslExecPath = output.replace(/\n\r/g, "").trim();
148
-
149
- // tslint:disable-next-line:variable-name
150
- const q_opensslExecPath = quote(opensslExecPath);
151
-
152
- // istanbul ignore next
153
- if (doDebug) {
154
- console.log(" OpenSSL found in : " + chalk.yellow(opensslExecPath));
155
- }
156
- // ------------------------ now verify that openssl version is the correct one
157
- execute(q_opensslExecPath + " version", (err: Error | null, result?: ExecuteResult) => {
158
- if (err) {
159
- return callback(err);
160
- }
161
-
162
- const exitCode = result!.exitCode;
163
- const output = result!.output;
164
-
165
- const version = output.trim();
166
-
167
- const versionOK = exitCode === 0 && is_expected_openssl_version(version);
168
- if (!versionOK) {
169
- let message =
170
- chalk.whiteBright("Warning !!!!!!!!!!!! ") +
171
- "\nyour version of openssl is " +
172
- version +
173
- ". It doesn't match the expected version";
174
-
175
- if (process.platform === "darwin") {
176
- message +=
177
- chalk.cyan("\nplease refer to :") +
178
- chalk.yellow(
179
- " https://github.com/node-opcua/node-opcua/" + "wiki/installing-node-opcua-or-node-red-on-MacOS"
180
- );
181
- }
182
-
183
- const table = new Table();
184
- table.push([message]);
185
- console.error(table.toString());
186
- }
187
- return callback(null, output);
188
- });
189
- });
190
- }
191
-
192
- function install_and_check_win32_openssl_version(callback: (err: Error | null, opensslExecPath?: string) => void): void {
193
- const downloadFolder = path.join(os.tmpdir(), ".");
194
-
195
- function get_openssl_folder_win32(): string {
196
- if (process.env.LOCALAPPDATA) {
197
- const userProgramFolder = path.join(process.env.LOCALAPPDATA, "Programs");
198
- if (fs.existsSync(userProgramFolder)) {
199
- return path.join(userProgramFolder, "openssl");
200
- }
201
- }
202
- return path.join(process.cwd(), "openssl");
203
- }
204
-
205
- function get_openssl_exec_path_win32(): string {
206
- const opensslFolder = get_openssl_folder_win32();
207
- return path.join(opensslFolder, "openssl.exe");
208
- }
209
-
210
- function check_openssl_win32(callback: (err: Error | null, opensslOk?: boolean, opensslPath?: string) => void) {
211
- const opensslExecPath = get_openssl_exec_path_win32();
212
-
213
- const exists = fs.existsSync(opensslExecPath);
214
- if (!exists) {
215
- console.log("checking presence of ", opensslExecPath);
216
- console.log(chalk.red(" cannot find file ") + opensslExecPath);
217
- return callback(null, false, "cannot find file " + opensslExecPath);
218
- } else {
219
- // tslint:disable-next-line:variable-name
220
- const q_openssl_exe_path = quote(opensslExecPath);
221
- const cwd = ".";
222
-
223
- execute(
224
- q_openssl_exe_path + " version",
225
- (err: Error | null, result?: ExecuteResult) => {
226
- if (err) {
227
- return callback(err);
228
- }
229
-
230
- const exitCode = result!.exitCode;
231
- const output = result!.output;
232
-
233
- const version = output.trim();
234
- // istanbul ignore next
235
-
236
- if (doDebug) {
237
- console.log(" Version = ", version);
238
- }
239
- callback(null, exitCode === 0 && is_expected_openssl_version(version), version);
240
- },
241
- cwd
242
- );
243
- }
244
- }
245
-
246
- /**
247
- * detect whether windows OS is a 64 bits or 32 bits
248
- * http://ss64.com/nt/syntax-64bit.html
249
- * http://blogs.msdn.com/b/david.wang/archive/2006/03/26/howto-detect-process-bitness.aspx
250
- * @return {number}
251
- */
252
- function win32or64(): 32 | 64 {
253
- if (process.env.PROCESSOR_ARCHITECTURE === "x86" && process.env.PROCESSOR_ARCHITEW6432) {
254
- return 64;
255
- }
256
-
257
- if (process.env.PROCESSOR_ARCHITECTURE === "AMD64") {
258
- return 64;
259
- }
260
-
261
- // check if we are running node x32 on a x64 arch
262
- if (process.env.CURRENT_CPU === "x64") {
263
- return 64;
264
- }
265
- return 32;
266
- }
267
-
268
- function download_openssl(callback: (err: Error | null, downloadedFile?: string) => void) {
269
- // const url = (win32or64() === 64 )
270
- // ? "http://indy.fulgan.com/SSL/openssl-1.0.2o-x64_86-win64.zip"
271
- // : "http://indy.fulgan.com/SSL/openssl-1.0.2o-i386-win32.zip"
272
- // ;
273
- const url =
274
- win32or64() === 64
275
- ? "https://github.com/node-opcua/node-opcua-pki/releases/download/2.14.2/openssl-1.0.2u-x64_86-win64.zip"
276
- : "https://github.com/node-opcua/node-opcua-pki/releases/download/2.14.2/openssl-1.0.2u-i386-win32.zip";
277
- // the zip file
278
- const outputFilename = path.join(downloadFolder, path.basename(url));
279
-
280
- console.log("downloading " + chalk.yellow(url) + " to " + outputFilename);
281
-
282
- if (fs.existsSync(outputFilename)) {
283
- return callback(null, outputFilename);
284
- }
285
- const options = makeOptions();
286
- const bar = new ProgressBar(chalk.cyan("[:bar]") + chalk.cyan(" :percent ") + chalk.white(":etas"), {
287
- complete: "=",
288
- incomplete: " ",
289
- total: 100,
290
- width: 100,
291
- });
292
-
293
- const download = wget.download(url, outputFilename, options);
294
- download.on("error", (err: Error) => {
295
- console.log(err);
296
- setImmediate(() => {
297
- callback(err);
298
- });
299
- });
300
- download.on("end", (output: string) => {
301
- // istanbul ignore next
302
- if (doDebug) {
303
- console.log(output);
304
- }
305
- // console.log("done ...");
306
- setImmediate(() => {
307
- callback(null, outputFilename);
308
- });
309
- });
310
- download.on("progress", (progress: any) => {
311
- bar.update(progress);
312
- });
313
- }
314
-
315
- function unzip_openssl(zipFilename: string, callback: (err?: Error) => void) {
316
- const opensslFolder = get_openssl_folder_win32();
317
-
318
- yauzl.open(zipFilename, { lazyEntries: true }, (err?: Error | null, zipFile?: yauzl.ZipFile) => {
319
- if (err) {
320
- return callback(err);
321
- }
322
- if (!zipFile) {
323
- return callback(new Error("Internal error"));
324
- }
325
-
326
- zipFile.readEntry();
327
-
328
- zipFile.on("end", (err?: Error) => {
329
- setImmediate(() => {
330
- // istanbul ignore next
331
- if (doDebug) {
332
- console.log("unzip done");
333
- }
334
- callback(err);
335
- });
336
- });
337
-
338
- zipFile.on("entry", (entry: yauzl.Entry) => {
339
- zipFile.openReadStream(entry, (err?: Error | null, readStream?: Readable) => {
340
- if (err) {
341
- return callback(err);
342
- }
343
-
344
- const file = path.join(opensslFolder, entry.fileName);
345
-
346
- // istanbul ignore next
347
- if (doDebug) {
348
- console.log(" unzipping :", file);
349
- }
350
-
351
- const writeStream = fs.createWriteStream(file, "binary");
352
- // ensure parent directory exists
353
- readStream!.pipe(writeStream);
354
-
355
- writeStream.on("close", () => {
356
- zipFile.readEntry();
357
- });
358
- });
359
- });
360
- });
361
- }
362
-
363
- const opensslFolder = get_openssl_folder_win32();
364
- const opensslExecPath = get_openssl_exec_path_win32();
365
-
366
- if (!fs.existsSync(opensslFolder)) {
367
- // istanbul ignore next
368
- if (doDebug) {
369
- console.log("creating openssl_folder", opensslFolder);
370
- }
371
- fs.mkdirSync(opensslFolder);
372
- }
373
-
374
- check_openssl_win32((err: Error | null, opensslOK?: boolean) => {
375
- if (err) {
376
- return callback(err);
377
- }
378
- if (!opensslOK) {
379
- console.log(chalk.yellow("openssl seems to be missing and need to be installed"));
380
- download_openssl((err: Error | null, filename?: string) => {
381
- if (err) {
382
- return callback(err);
383
- }
384
-
385
- // istanbul ignore next
386
- if (doDebug) {
387
- console.log("deflating ", chalk.yellow(filename!));
388
- }
389
- unzip_openssl(filename!, (err?: Error) => {
390
- if (err) {
391
- return callback(err);
392
- }
393
- const opensslExists = !!fs.existsSync(opensslExecPath);
394
-
395
- // istanbul ignore next
396
- if (doDebug) {
397
- console.log(
398
- "verifying ",
399
- opensslExists,
400
- opensslExists ? chalk.green("OK ") : chalk.red(" Error"),
401
- opensslExecPath
402
- );
403
- console.log("done ", err ? err : "");
404
- }
405
-
406
- check_openssl_win32((err: Error | null) => {
407
- callback(err, opensslExecPath);
408
- });
409
- });
410
- });
411
- } else {
412
- // istanbul ignore next
413
- if (doDebug) {
414
- console.log(chalk.green("openssl is already installed and have the expected version."));
415
- }
416
- return callback(null, opensslExecPath);
417
- }
418
- });
419
- }
420
-
421
- /**
422
- *
423
- * @param callback {Function}
424
- * @param callback.err {Error|null}
425
- * @param callback.pathToOpenSSL {string}
426
- */
427
- export function install_prerequisite(callback: (err: Error | null, pathToOpenSSL?: string) => void) {
428
- // istanbul ignore else
429
- if (process.platform !== "win32") {
430
- return check_system_openssl_version(callback);
431
- } else {
432
- return install_and_check_win32_openssl_version(callback);
433
- }
434
- }
435
-
436
- export function get_openssl_exec_path(callback: (err: Error | null, execPath?: string) => void) {
437
- assert(typeof callback === "function");
438
-
439
- if (process.platform === "win32") {
440
- install_prerequisite((err: Error | null, opensslExecPath?: string) => {
441
- if (err) {
442
- return callback(err);
443
- }
444
- if (!fs.existsSync(opensslExecPath!)) {
445
- throw new Error("internal error cannot find " + opensslExecPath);
446
- }
447
- callback(err, opensslExecPath);
448
- });
449
- } else {
450
- setImmediate(() => {
451
- callback(null, "openssl");
452
- });
453
- }
454
- }
1
+ // ---------------------------------------------------------------------------------------------------------------------
2
+ // node-opcua
3
+ // ---------------------------------------------------------------------------------------------------------------------
4
+ // Copyright (c) 2014-2022 - Etienne Rossignon - etienne.rossignon (at) gadz.org
5
+ // Copyright (c) 2022 - Sterfive.com
6
+ // ---------------------------------------------------------------------------------------------------------------------
7
+ //
8
+ // This project is licensed under the terms of the MIT license.
9
+ //
10
+ // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
11
+ // documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
12
+ // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
13
+ // permit persons to whom the Software is furnished to do so, subject to the following conditions:
14
+ //
15
+ // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
16
+ // Software.
17
+ //
18
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
19
+ // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20
+ // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21
+ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ // ---------------------------------------------------------------------------------------------------------------------
23
+ // tslint:disable:no-console
24
+ // tslint:disable:no-shadowed-variable
25
+
26
+ import * as fs from "fs";
27
+ import * as os from "os";
28
+ import * as path from "path";
29
+ import * as url from "url";
30
+ import * as assert from "assert";
31
+ import * as byline from "byline";
32
+ import * as chalk from "chalk";
33
+ import * as child_process from "child_process";
34
+ import * as ProgressBar from "progress";
35
+ import * as yauzl from "yauzl";
36
+ import { Readable } from "stream";
37
+
38
+ import Table = require("cli-table");
39
+
40
+ const doDebug = process.env.NODEOPCUAPKIDEBUG || false;
41
+
42
+ declare interface ProxyOptions {
43
+ host: string;
44
+ port: number;
45
+ localAddress?: string;
46
+ proxyAuth?: string;
47
+ headers?: { [key: string]: any };
48
+ protocol: string; // "https" | "http"
49
+ }
50
+ declare interface WgetOptions {
51
+ gunzip?: boolean;
52
+ proxy?: ProxyOptions;
53
+ }
54
+
55
+ declare interface WgetInterface {
56
+ download(url: string, outputFilename: string, options: WgetOptions): any;
57
+ }
58
+
59
+ // tslint:disable-next-line:no-var-requires
60
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
61
+ const wget = require("wget-improved-2") as WgetInterface;
62
+
63
+ type CallbackFunc<T> = (err: Error | null, result?: T) => void;
64
+
65
+ interface ExecuteResult {
66
+ exitCode: number;
67
+ output: string;
68
+ }
69
+
70
+ function makeOptions(): WgetOptions {
71
+ const proxy =
72
+ process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy || undefined;
73
+ if (proxy) {
74
+ const a = new url.URL(proxy);
75
+ const auth = a.username ? a.username + ":" + a.password : undefined;
76
+
77
+ const options: WgetOptions = {
78
+ proxy: {
79
+ port: a.port ? parseInt(a.port, 10) : 80,
80
+ protocol: a.protocol.replace(":", ""),
81
+ host: a.hostname ?? "",
82
+ proxyAuth: auth,
83
+ },
84
+ };
85
+ console.log(chalk.green("- using proxy "), proxy);
86
+ console.log(options);
87
+ return options;
88
+ }
89
+ return {};
90
+ }
91
+
92
+ function execute(cmd: string, callback: CallbackFunc<ExecuteResult>, cwd?: string) {
93
+ let output = "";
94
+
95
+ // xx cwd = cwd ? {cwd: cwd} : {};
96
+ const options = {
97
+ cwd,
98
+ windowsHide: true,
99
+ };
100
+
101
+ const child = child_process.exec(
102
+ cmd,
103
+ options,
104
+ (err: child_process.ExecException | null /*, stdout: string, stderr: string*/) => {
105
+ const exitCode = err === null ? 0 : err!.code!;
106
+ callback(err ? err : null, { exitCode, output });
107
+ }
108
+ );
109
+
110
+ const stream1 = byline(child.stdout!);
111
+ stream1.on("data", (line: string) => {
112
+ output += line + "\n";
113
+ // istanbul ignore next
114
+ if (doDebug) {
115
+ process.stdout.write(" stdout " + chalk.yellow(line) + "\n");
116
+ }
117
+ });
118
+ }
119
+
120
+ function quote(str: string): string {
121
+ return '"' + str.replace(/\\/g, "/") + "\"";
122
+ }
123
+
124
+ function is_expected_openssl_version(strVersion: string): boolean {
125
+ return !!strVersion.match(/OpenSSL 1|3/);
126
+ }
127
+
128
+ export function check_system_openssl_version(callback: (err: Error | null, output?: string) => void) {
129
+ execute("which openssl", (err: Error | null, result?: ExecuteResult) => {
130
+ // istanbul ignore next
131
+ if (err) {
132
+ console.log("warning: ", err.message);
133
+ return callback(new Error("Cannot find openssl"));
134
+ }
135
+
136
+ const exitCode = result!.exitCode;
137
+ const output = result!.output;
138
+
139
+ if (exitCode !== 0) {
140
+ console.log(
141
+ chalk.yellow(" it seems that ") + chalk.cyan("openssl") + chalk.yellow(" is not installed on your computer ")
142
+ );
143
+ console.log(chalk.yellow("Please install it before running this programs"));
144
+
145
+ return callback(new Error("Cannot find openssl"));
146
+ }
147
+ const opensslExecPath = output.replace(/\n\r/g, "").trim();
148
+
149
+ // tslint:disable-next-line:variable-name
150
+ const q_opensslExecPath = quote(opensslExecPath);
151
+
152
+ // istanbul ignore next
153
+ if (doDebug) {
154
+ console.log(" OpenSSL found in : " + chalk.yellow(opensslExecPath));
155
+ }
156
+ // ------------------------ now verify that openssl version is the correct one
157
+ execute(q_opensslExecPath + " version", (err: Error | null, result?: ExecuteResult) => {
158
+ if (err) {
159
+ return callback(err);
160
+ }
161
+
162
+ const exitCode = result!.exitCode;
163
+ const output = result!.output;
164
+
165
+ const version = output.trim();
166
+
167
+ const versionOK = exitCode === 0 && is_expected_openssl_version(version);
168
+ if (!versionOK) {
169
+ let message =
170
+ chalk.whiteBright("Warning !!!!!!!!!!!! ") +
171
+ "\nyour version of openssl is " +
172
+ version +
173
+ ". It doesn't match the expected version";
174
+
175
+ if (process.platform === "darwin") {
176
+ message +=
177
+ chalk.cyan("\nplease refer to :") +
178
+ chalk.yellow(
179
+ " https://github.com/node-opcua/node-opcua/" + "wiki/installing-node-opcua-or-node-red-on-MacOS"
180
+ );
181
+ }
182
+
183
+ const table = new Table();
184
+ table.push([message]);
185
+ console.error(table.toString());
186
+ }
187
+ return callback(null, output);
188
+ });
189
+ });
190
+ }
191
+
192
+ function install_and_check_win32_openssl_version(callback: (err: Error | null, opensslExecPath?: string) => void): void {
193
+ const downloadFolder = path.join(os.tmpdir(), ".");
194
+
195
+ function get_openssl_folder_win32(): string {
196
+ if (process.env.LOCALAPPDATA) {
197
+ const userProgramFolder = path.join(process.env.LOCALAPPDATA, "Programs");
198
+ if (fs.existsSync(userProgramFolder)) {
199
+ return path.join(userProgramFolder, "openssl");
200
+ }
201
+ }
202
+ return path.join(process.cwd(), "openssl");
203
+ }
204
+
205
+ function get_openssl_exec_path_win32(): string {
206
+ const opensslFolder = get_openssl_folder_win32();
207
+ return path.join(opensslFolder, "openssl.exe");
208
+ }
209
+
210
+ function check_openssl_win32(callback: (err: Error | null, opensslOk?: boolean, opensslPath?: string) => void) {
211
+ const opensslExecPath = get_openssl_exec_path_win32();
212
+
213
+ const exists = fs.existsSync(opensslExecPath);
214
+ if (!exists) {
215
+ console.log("checking presence of ", opensslExecPath);
216
+ console.log(chalk.red(" cannot find file ") + opensslExecPath);
217
+ return callback(null, false, "cannot find file " + opensslExecPath);
218
+ } else {
219
+ // tslint:disable-next-line:variable-name
220
+ const q_openssl_exe_path = quote(opensslExecPath);
221
+ const cwd = ".";
222
+
223
+ execute(
224
+ q_openssl_exe_path + " version",
225
+ (err: Error | null, result?: ExecuteResult) => {
226
+ if (err) {
227
+ return callback(err);
228
+ }
229
+
230
+ const exitCode = result!.exitCode;
231
+ const output = result!.output;
232
+
233
+ const version = output.trim();
234
+ // istanbul ignore next
235
+
236
+ if (doDebug) {
237
+ console.log(" Version = ", version);
238
+ }
239
+ callback(null, exitCode === 0 && is_expected_openssl_version(version), version);
240
+ },
241
+ cwd
242
+ );
243
+ }
244
+ }
245
+
246
+ /**
247
+ * detect whether windows OS is a 64 bits or 32 bits
248
+ * http://ss64.com/nt/syntax-64bit.html
249
+ * http://blogs.msdn.com/b/david.wang/archive/2006/03/26/howto-detect-process-bitness.aspx
250
+ * @return {number}
251
+ */
252
+ function win32or64(): 32 | 64 {
253
+ if (process.env.PROCESSOR_ARCHITECTURE === "x86" && process.env.PROCESSOR_ARCHITEW6432) {
254
+ return 64;
255
+ }
256
+
257
+ if (process.env.PROCESSOR_ARCHITECTURE === "AMD64") {
258
+ return 64;
259
+ }
260
+
261
+ // check if we are running node x32 on a x64 arch
262
+ if (process.env.CURRENT_CPU === "x64") {
263
+ return 64;
264
+ }
265
+ return 32;
266
+ }
267
+
268
+ function download_openssl(callback: (err: Error | null, downloadedFile?: string) => void) {
269
+ // const url = (win32or64() === 64 )
270
+ // ? "http://indy.fulgan.com/SSL/openssl-1.0.2o-x64_86-win64.zip"
271
+ // : "http://indy.fulgan.com/SSL/openssl-1.0.2o-i386-win32.zip"
272
+ // ;
273
+ const url =
274
+ win32or64() === 64
275
+ ? "https://github.com/node-opcua/node-opcua-pki/releases/download/2.14.2/openssl-1.0.2u-x64_86-win64.zip"
276
+ : "https://github.com/node-opcua/node-opcua-pki/releases/download/2.14.2/openssl-1.0.2u-i386-win32.zip";
277
+ // the zip file
278
+ const outputFilename = path.join(downloadFolder, path.basename(url));
279
+
280
+ console.log("downloading " + chalk.yellow(url) + " to " + outputFilename);
281
+
282
+ if (fs.existsSync(outputFilename)) {
283
+ return callback(null, outputFilename);
284
+ }
285
+ const options = makeOptions();
286
+ const bar = new ProgressBar(chalk.cyan("[:bar]") + chalk.cyan(" :percent ") + chalk.white(":etas"), {
287
+ complete: "=",
288
+ incomplete: " ",
289
+ total: 100,
290
+ width: 100,
291
+ });
292
+
293
+ const download = wget.download(url, outputFilename, options);
294
+ download.on("error", (err: Error) => {
295
+ console.log(err);
296
+ setImmediate(() => {
297
+ callback(err);
298
+ });
299
+ });
300
+ download.on("end", (output: string) => {
301
+ // istanbul ignore next
302
+ if (doDebug) {
303
+ console.log(output);
304
+ }
305
+ // console.log("done ...");
306
+ setImmediate(() => {
307
+ callback(null, outputFilename);
308
+ });
309
+ });
310
+ download.on("progress", (progress: any) => {
311
+ bar.update(progress);
312
+ });
313
+ }
314
+
315
+ function unzip_openssl(zipFilename: string, callback: (err?: Error) => void) {
316
+ const opensslFolder = get_openssl_folder_win32();
317
+
318
+ yauzl.open(zipFilename, { lazyEntries: true }, (err?: Error | null, zipFile?: yauzl.ZipFile) => {
319
+ if (err) {
320
+ return callback(err);
321
+ }
322
+ if (!zipFile) {
323
+ return callback(new Error("Internal error"));
324
+ }
325
+
326
+ zipFile.readEntry();
327
+
328
+ zipFile.on("end", (err?: Error) => {
329
+ setImmediate(() => {
330
+ // istanbul ignore next
331
+ if (doDebug) {
332
+ console.log("unzip done");
333
+ }
334
+ callback(err);
335
+ });
336
+ });
337
+
338
+ zipFile.on("entry", (entry: yauzl.Entry) => {
339
+ zipFile.openReadStream(entry, (err?: Error | null, readStream?: Readable) => {
340
+ if (err) {
341
+ return callback(err);
342
+ }
343
+
344
+ const file = path.join(opensslFolder, entry.fileName);
345
+
346
+ // istanbul ignore next
347
+ if (doDebug) {
348
+ console.log(" unzipping :", file);
349
+ }
350
+
351
+ const writeStream = fs.createWriteStream(file, "binary");
352
+ // ensure parent directory exists
353
+ readStream!.pipe(writeStream);
354
+
355
+ writeStream.on("close", () => {
356
+ zipFile.readEntry();
357
+ });
358
+ });
359
+ });
360
+ });
361
+ }
362
+
363
+ const opensslFolder = get_openssl_folder_win32();
364
+ const opensslExecPath = get_openssl_exec_path_win32();
365
+
366
+ if (!fs.existsSync(opensslFolder)) {
367
+ // istanbul ignore next
368
+ if (doDebug) {
369
+ console.log("creating openssl_folder", opensslFolder);
370
+ }
371
+ fs.mkdirSync(opensslFolder);
372
+ }
373
+
374
+ check_openssl_win32((err: Error | null, opensslOK?: boolean) => {
375
+ if (err) {
376
+ return callback(err);
377
+ }
378
+ if (!opensslOK) {
379
+ console.log(chalk.yellow("openssl seems to be missing and need to be installed"));
380
+ download_openssl((err: Error | null, filename?: string) => {
381
+ if (err) {
382
+ return callback(err);
383
+ }
384
+
385
+ // istanbul ignore next
386
+ if (doDebug) {
387
+ console.log("deflating ", chalk.yellow(filename!));
388
+ }
389
+ unzip_openssl(filename!, (err?: Error) => {
390
+ if (err) {
391
+ return callback(err);
392
+ }
393
+ const opensslExists = !!fs.existsSync(opensslExecPath);
394
+
395
+ // istanbul ignore next
396
+ if (doDebug) {
397
+ console.log(
398
+ "verifying ",
399
+ opensslExists,
400
+ opensslExists ? chalk.green("OK ") : chalk.red(" Error"),
401
+ opensslExecPath
402
+ );
403
+ console.log("done ", err ? err : "");
404
+ }
405
+
406
+ check_openssl_win32((err: Error | null) => {
407
+ callback(err, opensslExecPath);
408
+ });
409
+ });
410
+ });
411
+ } else {
412
+ // istanbul ignore next
413
+ if (doDebug) {
414
+ console.log(chalk.green("openssl is already installed and have the expected version."));
415
+ }
416
+ return callback(null, opensslExecPath);
417
+ }
418
+ });
419
+ }
420
+
421
+ /**
422
+ *
423
+ * @param callback {Function}
424
+ * @param callback.err {Error|null}
425
+ * @param callback.pathToOpenSSL {string}
426
+ */
427
+ export function install_prerequisite(callback: (err: Error | null, pathToOpenSSL?: string) => void) {
428
+ // istanbul ignore else
429
+ if (process.platform !== "win32") {
430
+ return check_system_openssl_version(callback);
431
+ } else {
432
+ return install_and_check_win32_openssl_version(callback);
433
+ }
434
+ }
435
+
436
+ export function get_openssl_exec_path(callback: (err: Error | null, execPath?: string) => void) {
437
+ assert(typeof callback === "function");
438
+
439
+ if (process.platform === "win32") {
440
+ install_prerequisite((err: Error | null, opensslExecPath?: string) => {
441
+ if (err) {
442
+ return callback(err);
443
+ }
444
+ if (!fs.existsSync(opensslExecPath!)) {
445
+ throw new Error("internal error cannot find " + opensslExecPath);
446
+ }
447
+ callback(err, opensslExecPath);
448
+ });
449
+ } else {
450
+ setImmediate(() => {
451
+ callback(null, "openssl");
452
+ });
453
+ }
454
+ }