-
Notifications
You must be signed in to change notification settings - Fork 5
/
kg.js
220 lines (185 loc) · 4.88 KB
/
kg.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
var bn = require('bn.js');
var brorand = require('brorand');
var primal = require('primal');
var asn1 = require('./asn1');
var rsa = require('./rsa');
var EventEmitter = require('events').EventEmitter;
function KeyGen(options) {
this.options = options || {};
this.rand = new brorand.Rand(this.options.prng);
this.primal = primal.create({ 'miller-rabin': this.rand });
}
module.exports = KeyGen;
KeyGen.create = function create(options) {
return new KeyGen(options);
};
KeyGen.prototype.getPrime = function getPrime(bits, cb) {
var self = this;
var e = new EventEmitter();
var aborted = false;
e.abort = function abort() {
aborted = true;
};
function next() {
do {
var r = self.rand.generate(Math.ceil(bits / 8));
r[0] |= 0xc0;
r[r.length - 1] |= 3;
r = new bn(r);
e.emit('try', 'miller-rabin');
if (!self.primal.test(r))
continue;
e.emit('try', 'prime');
if (cb)
return cb(null, r);
else
return r;
} while (!cb);
if (aborted)
return cb(new Error('aborted'));
setImmediate(next);
}
if (cb)
setImmediate(next);
else
return next();
return e;
};
KeyGen.prototype.getKeyData = function getKeyData(p, q) {
if (p.cmp(q) === 0)
return false;
var p1 = p.subn(1);
var q1 = q.subn(1);
var phi = p1.mul(q1);
var e = new bn(65537);
var d = e.invm(phi);
var exp1 = d.mod(p1);
var exp2 = d.mod(q1);
var lcm = p1.mul(q1).div(p1.gcd(q1));
var check = d.mul(e).mod(lcm);
if (check.cmpn(1) !== 0)
return false;
return {
version: 0,
modulus: p.mul(q),
publicExponent: e,
privateExponent: d,
prime1: p,
prime2: q,
exponent1: exp1,
exponent2: exp2,
coefficient: q.invm(p)
};
};
KeyGen.prototype._pemWrap = function _pemWrap(buf, label) {
var p = buf.toString('base64');
var out = [ '-----BEGIN ' + label + '-----' ];
for (var i = 0; i < p.length; i += 64)
out.push(p.slice(i, i + 64));
out.push('-----END ' + label + '-----');
return out.join('\n');
};
KeyGen.prototype.getPrivate = function getPrivate(data, enc) {
var res = asn1.RSAPrivateKey.encode(data, 'der');
if (enc !== 'pem')
return res;
return this._pemWrap(res, 'RSA PRIVATE KEY');
};
KeyGen.prototype.getPublic = function getPublic(data, enc) {
var res = asn1.RSAPublicKey.encode(data, 'der');
if (enc !== 'pem')
return res;
return this._pemWrap(res, 'RSA PUBLIC KEY');
};
KeyGen.prototype.getCertTBSData = function getCertTBSData(options) {
var now = new Date();
function ia5(str) {
return asn1.IA5Str.encode(str, 'der');
}
var extensions = [];
var dnsNames;
if (options.dnsNames)
dnsNames = options.dnsNames;
else if (options.dnsName)
dnsNames = [ options.dnsName ];
if (dnsNames) {
extensions.push({
extnID: asn1.ALTNAME,
critical: false,
extnValue: asn1.GeneralNames.encode(dnsNames.map(function(dnsName) {
return {
type: 'dNSName',
value: dnsName
};
}), 'der')
});
}
if (options.extensions)
extensions = extensions.concat(options.extensions);
var oneDay = 24 *3600 * 1000;
var tenYears = 10 * 365 * 24 * 3600 * 1000;
return {
version: 'v3',
serialNumber: options.serial || 0x10001,
signature: {
algorithm: asn1.SHA512RSA,
parameters: asn1.EMPTY_PARAMETERS
},
issuer: options.issuer ? options.issuer.tbsCertificate.subject : {
type: 'rdnSequence',
value: [
[ {
type: asn1.COMMONNAME,
value: ia5(options.issuerName || options.commonName)
} ]
]
},
validity: {
notBefore: {
type: 'utcTime',
value: options.notBefore || new Date(+now - oneDay)
},
notAfter: {
type: 'utcTime',
value: options.notAfter ||
new Date(+now + (options.validity || tenYears))
}
},
subject: {
type: 'rdnSequence',
value: [
[ { type: asn1.COMMONNAME, value: ia5(options.commonName) } ]
]
},
subjectPublicKeyInfo: {
algorithm: {
algorithm: asn1.RSA,
parameters: asn1.EMPTY_PARAMETERS
},
subjectPublicKey: {
unused: 0,
data: this.getPublic(options.keyData)
}
},
extensions: extensions
};
};
KeyGen.prototype.getCertData = function getCertData(options) {
var tbsData = this.getCertTBSData(options);
var tbs = asn1.TBSCertificate.encode(tbsData, 'der');
var signature = rsa.sign(tbs, options.issuerKeyData || options.keyData);
return {
tbsCertificate: tbsData,
signatureAlgorithm: {
algorithm: asn1.SHA512RSA,
parameters: asn1.EMPTY_PARAMETERS
},
signature: { unused: 0, data: signature }
};
};
KeyGen.prototype.getCert = function getCert(data, enc) {
var res = asn1.Certificate.encode(data, 'der');
if (enc !== 'pem')
return res;
return this._pemWrap(res, 'CERTIFICATE');
};