This repository has been archived by the owner on Sep 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
405 lines (327 loc) · 7.89 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
Name: ssldecoder
Description: Access SSLDecoder.org API methods (unofficial)
Author: Franklin van de Meent (https://frankl.in)
License: Unlicense (Public Domain, see LICENSE file)
Source code: https://github.com/fvdm/nodejs-ssldecoder
Contact & bugs: https://github.com/fvdm/nodejs-ssldecoder/issues
*/
var httpreq = require ('httpreq');
var dns = require ('dns');
var fs = require ('fs');
var config = {
endpoint: 'https://ssldecoder.org',
timeout: 20000
};
/**
* Callback an error
*
* @callback callback
* @param msg {string} - Error.message
* @param err {mixed} - Error.error
* @param res {object, null} - Client response
* @param callback {function} - `function (err) {}`
* @return {void}
*/
function requestError (msg, err, res, callback) {
var error = new Error (msg);
error.error = err;
error.statusCode = res && res.statusCode;
error.body = res && res.body;
callback (error);
}
/**
* Convert an object to an array
*
* @param obj {object} - The object to convert
* @returns {array} - The array
*/
function Object2Array (obj) {
var key;
var result = [];
if (obj instanceof Object) {
for (key in obj) {
result.push (obj [key]);
}
return result;
}
return obj;
}
/**
* Deep object iteration
* Loosely based on http://stackoverflow.com/a/25334210
*
* @param obj {object} - The object to process
* @param func {function} - `function (val, key) { return val + 2; }`
* @returns {object} - The processed object
*/
function deepMap (obj, func) {
return Object.keys (obj) .reduce (function (acc, k) {
if (typeof obj [k] === 'object') {
acc [k] = deepMap (obj [k], func);
} else {
acc [k] = func (obj [k], k);
}
return acc;
}, {});
}
/**
* Look for certain keys and convert their value to a boolean
*
* '1' -> true
* '' -> false
* 'true' -> true
* 'false' -> false
*
* @param obj {object} - The object to process
* @param keys {array} - Keys to look for
* @returns {object} - The processed object
*/
function boolDozer (obj, keys) {
obj = deepMap (obj, function (val, key) {
if (!!~keys.indexOf (key)) {
if (typeof val === 'string' && !!val.match (/^(1|true)$/)) {
return true;
}
if (typeof val === 'string' && !!val.match (/^(|false)$/)) {
return false;
}
return val;
}
return val;
});
return obj;
}
/**
* Process API response
*
* @callback callback
* @param err {Error, null} - Error
* @param res {object} - Response object
* @param callback {function} - `function (err, data) {}`
* @returns {void}
*/
function processResponse (err, res, callback) {
var data = res && res.body || '';
if (err) {
requestError ('Client error', err, null, callback);
return;
}
try {
data = JSON.parse (data);
} catch (e) {
requestError ('Invalid response', e, res, callback);
return;
}
if (data && data.error) {
requestError ('API error', data.error, res, callback);
return;
}
if (data && !data.data) {
requestError ('Invalid response', null, res, callback);
return;
}
callback (null, data.data);
}
/**
* Send API request
*
* @callback callback
* @param params {object} - Data fields to send
* @param callback {function} - `function (err, data) {}`
* @returns {void}
*/
function sendRequest (params, callback) {
var options = {
url: config.endpoint + '/json.php',
parameters: params,
method: 'GET',
timeout: config.timeout,
headers: {
'User-Agent': 'ssldecoder (https://github.com/fvdm/nodejs-ssldecoder)'
}
};
httpreq.doRequest (options, function (err, res) {
processResponse (err, res, callback);
});
}
/**
* Process host response data
*
* @callback callback
* @param err {Error, null} - Response error
* @param data {object} - Response data
* @param callback {function} - `function (err, data) {}`
*/
function processHost (err, data, callback) {
var itm;
var i;
var bools = [
'tlsv1.2',
'tlsv1.1',
'tlsv1.0',
'sslv3',
'sslv2',
'working',
'heartbeat',
'ca',
'general',
'cert_issued_in_future',
'cert_expired',
'cert_expires_in_less_than_thirty_days',
'issuer_valid'
];
if (err) {
callback (err);
return;
}
// Set boolean values
data = boolDozer (data, bools);
// Convert numbered objects to arrays
data.connection.chain = Object2Array (data.connection.chain);
data.connection.supported_ciphersuites = Object2Array (data.connection.supported_ciphersuites);
data.chain = Object2Array (data.chain);
for (i = 0; i < data.chain.length; i++) {
itm = data.chain [i];
if (itm.ocsp) {
itm.ocsp = Object2Array (itm.ocsp);
}
if (itm.crl) {
itm.crl = Object2Array (itm.crl);
}
data.chain [i] = itm;
}
// All done
callback (null, data);
}
/**
* Method .host
*
* @callback callback
* @param params {object} - Method parameters
* @param callback {function} - `function (err, data) {}`
* @returns {void}
*/
function methodHost (params, callback) {
var send = {
host: '',
port: 443,
fastcheck: 0
};
// "host"
if (typeof params === 'string') {
params = {
host: params
};
}
// set parameters
send.port = params.port || send.port;
send.fastcheck = params.fastcheck || send.fastcheck;
// host:ip
if (typeof params.host === 'string' && !!params.host.match (/:/)) {
send.host = params.host;
sendRequest (send, function (rErr, rData) {
processHost (rErr, rData, callback);
});
return;
}
// host && ip
if (params.host && params.ip) {
send.host = params.host + ':' + params.ip;
sendRequest (send, function (rErr, rData) {
processHost (rErr, rData, callback);
});
return;
}
// host && !ip
if (params.host && !params.ip) {
dns.resolve (params.host, function (err, res) {
if (err) {
callback (err);
return;
}
send.host = params.host + ':' + res [0];
sendRequest (send, function (rErr, rData) {
processHost (rErr, rData, callback);
});
});
return;
}
// !host && ip
if (!params.host && params.ip) {
dns.reverse (params.ip, function (err, res) {
if (err) {
callback (err);
return;
}
send.host = res [0] + ':' + params.ip;
sendRequest (send, function (rErr, rData) {
processHost (rErr, rData, callback);
});
});
return;
}
callback (new Error ('Invalid host or IP address'));
}
/**
* Method .csr
*
* @callback callback
* @param csr {string} - PEM string or filepath
* @param callback {function} - `function (err, data) {}`
* @returns {void}
*/
function methodCsr (csr, callback) {
var params = {
csr: csr
};
if (!csr || typeof csr !== 'string') {
callback (new Error ('CSR must be a string'));
return;
}
if (!!csr.match (/^-----BEGIN CERTIFICATE/)) {
sendRequest (params, function (err, data) {
if (err) {
callback (err);
return;
}
data.chain = Object2Array (data.chain);
callback (null, data);
});
return;
}
fs.readFile (csr, { encoding: 'utf8' }, function (fsErr, file) {
if (fsErr) {
callback (fsErr);
return;
}
params.csr = file;
sendRequest (params, function (err, data) {
if (err) {
callback (err);
return;
}
data.chain = Object2Array (data.chain);
callback (null, data);
});
});
}
/**
* Configuration
*
* @param [cnf] {object} - Config object
* @param [cnf.timeout = 5000] {number} - Time out in ms
* @param [cnf.endpoint = https://ssldecoder.org] {string} - API endpoint
* @returns {object} - Interface methods
*/
function setup (cnf) {
if (cnf instanceof Object) {
config.timeout = cnf.timeout || config.timeout;
config.endpoint = cnf.endpoint || config.endpoint;
}
return {
host: methodHost,
csr: methodCsr
};
}
module.exports = setup;