-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
61 lines (49 loc) · 1.49 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
const util = require('util');
const spawn = require('child_process').spawn;
/**
* sign data using openssl
* @param {object} options Options
* @param {string} options.data The data to be encrypted
* @param {string} options.key Key path
* @param {string} options.cert Cert path
* @param {string} options.ca CA Cert path
* @return {string}
*/
module.exports.sign = function(options) {
return new Promise(function (resolve, reject) {
options = options || {};
if (!options.data)
throw new Error('Invalid data.');
if (!options.key)
throw new Error('Invalid key.');
if (!options.cert)
throw new Error('Invalid certificate.');
if (!options.ca)
throw new Error('Invalid ca.');
let command = util.format(
'openssl smime -sign -inkey %s -signer %s -certfile %s -outform der -nodetach',
options.key,
options.cert,
options.ca
);
let args = command.split(' ');
let childProcess = spawn(args[0], args.splice(1));
let signedData = [];
childProcess.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
childProcess.stdout.on('data', function (chunk) {
signedData.push(chunk);
});
childProcess.on('close', function (code) {
childProcess.kill();
if (code !== 0)
reject(new Error('Process failed.'));
else
resolve(Buffer.concat(signedData));
});
childProcess.stdin.write(options.data);
childProcess.stdin.end();
});
};