forked from Southern/node-x509
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
86 lines (62 loc) · 1.84 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
75
76
77
78
79
80
81
82
83
84
85
86
/*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
let x509 = require('./build/Release/x509');
let fs = require('fs');
exports.version = x509.version;
exports.verify = function (certPath, CABundlePath, cb) {
if (!certPath) {
throw new TypeError('Certificate path is required');
}
if (!CABundlePath) {
throw new TypeError('CA Bundle path is required');
}
fs.stat(certPath, function (certPathErr) {
if (certPathErr) {
return cb(certPathErr);
}
fs.stat(CABundlePath, function (bundlePathErr) {
if (bundlePathErr) {
return cb(bundlePathErr);
}
try {
x509.verify(certPath, CABundlePath);
cb(null);
}
catch (verificationError) {
cb(verificationError);
}
});
});
};
exports.parseCert = function (path) {
let ret = x509.parseCert(path).exports;
let exts = {};
for (let key in ret.extensions) {
let newkey = key.replace('X509v3', '').replace(/ /g, '');
newkey = newkey.slice(0, 1).toLowerCase() + newkey.slice(1);
exts[newkey] = ret.extensions[key].replace('DirName: C','DirName:C');
}
delete ret.extensions;
ret.extensions = exts;
// ensure dates are correct
ret.notBefore = ret.notBefore.toISOString();
ret.notAfter = ret.notAfter.toISOString();
return ret;
};
exports.getSubject = function (path) {
let ret = x509.parseCert(path).exports;
let subject = ret.subject;
return subject;
};
exports.getAltNames = function (path) {
let ret = x509.parseCert(path).exports;
let altNames = ret.altNames;
return altNames;
};
exports.getIssuer = function (path) {
let ret = x509.parseCert(path).exports;
let altNames = ret.issuer;
return altNames;
};