-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
75 lines (65 loc) · 2.18 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
62
63
64
65
66
67
68
69
70
71
72
73
74
var argv = require('optimist').argv;
function execShellCommand (command, args, stdinData, done) {
var spawn = require('child_process').spawn,
child = spawn(command, args), stdoutContent = '', stderrContent = '';
child.stdout.on('data', function (data) {
stdoutContent += data;
});
child.stderr.on('data', function (err) {
stderrContent += err;
});
child.on('exit', function (code) {
done(code, stderrContent, stdoutContent);
});
if(stdinData) {
child.stdin.write(stdinData);
}
return child;
}
function readP12Certificate (path, password, done) {
execShellCommand(
'openssl',
['pkcs12', '-info', '-in', path, '-nodes', '-passin', 'pass:' + password],
null,
function (code, err, data) {
if(code) {
done(err);
} else {
done(null, data);
}
}
);
}
function parseP12Certificate (path, password, done) {
readP12Certificate(path, password, function (err, content) {
if(err) {
return done(err);
}
var start = content.indexOf('-----BEGIN CERTIFICATE-----'), endTag = '-----END CERTIFICATE-----', end = content.indexOf(endTag),
cert = content.substring(start, end + endTag.length) + '\n';
execShellCommand('openssl', ['x509', '-noout', '-startdate', '-enddate'], cert, function (code, err, data) {
if(code) {
return done(err);
}
var lines = data.split('\n'),
startDate = (lines[0] || '').substring((lines[0] || '').indexOf('=') + 1),
endDate = (lines[1] || '').substring((lines[1] || '').indexOf('=') + 1);
startDate = new Date(startDate);
endDate = new Date(endDate);
done(null, cert, content, startDate, endDate);
});
});
}
parseP12Certificate(argv._[0], argv.password, function (err, cert, content, startDate, endDate) {
if(err) {
return console.error(err);
}
console.log('////////////////// copy cert below /////////////');
console.log(cert);
console.log('////////////////// copy content below /////////////');
console.log(content);
console.log('///////////////// startDate /////////////////////');
console.log(startDate);
console.log('///////////////// endDate /////////////////////');
console.log(endDate);
});