/
index.js
88 lines (83 loc) · 2.39 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
'use strict'
const asn1 = require('asn1.js')
const asn1Cert = require('asn1.js-rfc3280')
const ASNStr = asn1.define('ASNStr', function () {
this.choice({
bit: this.bitstr(),
bmp: this.bmpstr(),
chr: this.charstr(),
gen: this.genstr(),
grp: this.graphstr(),
ia5: this.ia5str(),
iso: this.iso646str(),
num: this.numstr(),
oct: this.octstr(),
prt: this.printstr(),
t61: this.t61str(),
uni: this.unistr(),
utf: this.utf8str(),
vid: this.videostr()
})
})
function getCommonNames (buffer, encoding) {
if (encoding === undefined) {
encoding = (/^\s*-----\s*BEGIN/.test(buffer.toString())) ? 'pem' : 'der'
}
const cert = asn1Cert.Certificate.decode(buffer, encoding, {label: 'CERTIFICATE'})
var names = []
cert.tbsCertificate.subject.value.forEach((e) => {
if (e[0].type.join('.') !== '2.5.4.3') return
names.push(ASNStr.decode(e[0].value).value)
})
cert.tbsCertificate.extensions.forEach((e) => {
if (e.extnID.join('.') !== '2.5.29.17') return
names.push.apply(names, asn1Cert.GeneralNames.decode(e.extnValue).map((e) => {
switch (e.type) {
case 'dNSName':
return e.value
case 'iPAddress':
return [e.value[0], e.value[1], e.value[2], e.value[3]].join('.')
default:
console.log('Unknown type: %s', e.type)
return null
}
}))
})
return names.filter((e, i, s) => {
if (e === null) return false
return s.indexOf(e) === i
})
}
function toRegEx (names) {
const domains = { wc: [], single: [], both: [] }
names.forEach((e) => {
var wc = false
if (e[0] === '*') {
wc = true
e = e.split('.').splice(1).join('.')
}
e = e.replace(/\./g, '\\.')
if (wc) domains.wc.push(e)
else domains.single.push(e)
})
domains.wc = domains.wc.filter((e) => {
const idx = domains.single.indexOf(e)
if (idx === -1) return true
domains.single.splice(idx, 1)
domains.both.push(e)
return false
})
const entries = []
if (domains.single.length > 0) {
entries.push(domains.single.join('|'))
}
if (domains.wc.length > 0) {
entries.push('[^\\.]+\\.(' + domains.wc.join('|') + ')')
}
if (domains.both.length > 0) {
entries.push('([^\\.]+\\.)?(' + domains.both.join('|') + ')')
}
return new RegExp('^((' + entries.join(')|(') + '))$', 'gm')
}
exports.getCommonNames = getCommonNames
exports.toRegEx = toRegEx