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