This repository has been archived by the owner on Oct 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
shaaaaa.js
247 lines (206 loc) · 6.5 KB
/
shaaaaa.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
* shaaaaa.js
*
* Checks a domain for its certificate algorithm.
*
*/
var fs = require('fs'); // loads root certs
// yorkie's fork, includes signatureAlgorithm
var x509 = require("x509");
// network cxn handled directly by tls
var tls = require('tls');
var Shaaa = {
// root cert bundle, loaded when this file is required
roots: null,
// load root bundle, parse each cert
loadRoots: function() {
Shaaa.roots = [];
// store a fingerprint of each one
var certs = fs.readFileSync(__dirname + "/ca-bundle.crt", "utf-8").split("\n\n");
for (var i=0; i<certs.length; i++)
Shaaa.roots.push(x509.parseCert(certs[i]).fingerPrint);
},
// takes x509-parsed cert, compares fingerprint
isRoot: function(cert) {
return (Shaaa.roots.indexOf(cert.fingerPrint) > -1);
},
// fingerprints of SHA-1 intermediate certs with known SHA-2 replacements
fingerprints: null,
loadFingerprints: function() {
Shaaa.fingerprints = JSON.parse(fs.readFileSync(__dirname + '/fingerprints.json', 'utf-8')).certificates;
},
algorithms: [
// new gold standards
"sha256", "sha224", "sha384", "sha512",
"sha1", // common, but deprecated
"md5", // old, broken
"md2" // so old, so broken
],
// given e.g. 'sha256WithRSAEncryption', return
// {algorithm: 'sha256', raw: 'sha256WithRSAEncryption', good: true}
algorithm: function(raw) {
var raw_compare = raw.toLowerCase();
var answer;
for (var i=0; i<Shaaa.algorithms.length; i++) {
var algorithm = Shaaa.algorithms[i];
if (raw_compare.indexOf(algorithm) == 0) answer = algorithm;
if (raw_compare == ("ecdsa-with-" + algorithm)) answer = algorithm;
}
if (!answer) answer = "unknown";
var good = (
(answer == "sha256") ||
(answer == "sha224") ||
(answer == "sha384") ||
(answer == "sha512")
);
return {algorithm: answer, raw: raw, good: good};
},
// Convert DER certificate to PEM
derToPem: function(derBuffer) {
if (derBuffer) {
var b64Der = derBuffer.toString('base64');
var b64DerLines = b64Der.match(/.{1,64}/g);
return "-----BEGIN CERTIFICATE-----\n" + b64DerLines.join('\n') + "\n-----END CERTIFICATE-----\n";
}
return null;
},
certs: function(domain, callback, options) {
if (!options) options = {};
var defaultport = 443;
var matchdomain = domain.match(/^[\w\.\-\:]+$/);
// make sure domain looks valid, look for a port otherwise use defaultport
if (matchdomain) {
var matchport = domain.match(/([^:]+):(\d+)$/);
if (matchport) {
domain = matchport[1];
port = matchport[2];
} else {
port = defaultport;
}
} else {
callback({message: "Invalid domain"});
return;
}
var peerCert = {};
var tlsOptions = {
host: domain,
servername: domain,
ca: 'x', // prevents adding known roots to response
port: port,
rejectUnauthorized: false
};
var socket = tls.connect(tlsOptions, function() {
if (options.verbose || options.debug) console.log('[tlsSocket] connected');
peerCert = socket.getPeerCertificate(true);
socket.end();
});
socket.on('close', function() {
if (options.verbose || options.debug) console.log('[tlsSocket] disconnected');
// Walk the depth of the peerCert object. Grab DER-encoded certs. Convert to PEM and push to certsArray.
var certsArray = [];
var maxdepth = 7;
if (peerCert) {
var depth = 0;
while (depth < maxdepth) {
var pem = Shaaa.derToPem(peerCert.raw);
if (pem) {
certsArray.push(x509.parseCert(pem));
if (peerCert.issuerCertificate && (peerCert.issuerCertificate !== peerCert))
peerCert = peerCert.issuerCertificate;
else
break; // no more depth levels
} else
break; // no more certs
++depth;
}
}
if (certsArray.length == 0)
callback({message: "No certs returned"});
else
callback(null, certsArray);
});
socket.on('error', function(error) {
if (options.verbose || options.debug) console.log('[tlsSocket] error ', error);
// callback({message: error});
});
// this is to catch-all for any hangs.
socket.setTimeout(3000, function() {
socket.destroy();
if (options.verbose || options.debug) console.log('[tlsSocket] timeout to '+domain);
// callback({message: "Could not establish a connection to "+domain});
});
},
sha2URL: function(fingerprint) {
for (var i=0; i<Shaaa.fingerprints.length; i++) {
if (Shaaa.fingerprints[i].sha1 == fingerprint)
return Shaaa.fingerprints[i].url;
}
},
cert: function(cert) {
var answer = Shaaa.algorithm(cert.signatureAlgorithm);
var root = Shaaa.isRoot(cert);
var replacement = (root ? null : Shaaa.sha2URL(cert.fingerPrint));
return {
algorithm: answer.algorithm,
raw: answer.raw,
good: (root || answer.good),
root: root,
replacement: replacement,
expires: cert.notAfter,
name: cert.subject.commonName
};
},
/**
* Desired output:
* {
* domain: shaaaaaaaaaaaaa.com,
* cert: {
* algorithm: 'sha256',
* raw: 'sha256WithRSAEncryption',
* good: true,
* root: false,
* replacement: null,
* expires: Tue Aug 18 2015 19:59:59 GMT-0400 (EDT),
* name: "www.konklone.com"
* },
* intermediates: [
* { ... } // same form as above
* ]
* }
*/
from: function(domain, callback, options) {
if (!options) options = {};
var data = {domain: domain};
Shaaa.certs(domain, function(error, certs) {
if (error) return callback(error);
data.cert = Shaaa.cert(certs[0]);
data.intermediates = [];
certs.slice(1).forEach(function(cert) {
data.intermediates.push(Shaaa.cert(cert));
});
var intergood = true;
for (var i=0; i<data.intermediates.length; i++) {
if (!data.intermediates[i].good) {
if (i == 0) {
intergood = false;
break;
}
}
}
if (data.cert.good && intergood)
data.diagnosis = "good";
else if (data.cert.good && !intergood)
data.diagnosis = "almost";
else
data.diagnosis = "bad";
if (callback)
callback(null, data);
else
console.log(data);
}, options);
}
}
// load roots and fingerprints on first require
Shaaa.loadRoots();
Shaaa.loadFingerprints();
module.exports = Shaaa;