-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathssl_cert.js
More file actions
303 lines (280 loc) · 9.24 KB
/
ssl_cert.js
File metadata and controls
303 lines (280 loc) · 9.24 KB
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
/*
* Copyright (c) 2016-2023 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK Runtime.
*
* The Moddable SDK Runtime is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Moddable SDK Runtime is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright (C) 2010-2016 Marvell International Ltd.
* Copyright (C) 2002-2010 Kinoma, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// for MC
import Resource from "Resource";
import Crypt from "crypt";
import X509 from "x509";
import PKCS1_5 from "pkcs1_5";
import DSA from "dsa";
import ECDSA from "ecdsa";
import Bin from "bin";
import BER from "ber";
import PKCS8 from "pkcs8"
const globalCerts = [];
class CertificateManager {
#verify;
#registeredCerts = [];
#clientCertificates;
#clientKey;
constructor(options) {
this.#verify = options.verify ?? true;
if (options.certificate)
this.register(options.certificate);
this.#clientCertificates = options.clientCertificates;
this.#clientKey = options.clientKey;
}
getCerts() {
// return the self certs
return [getResource("srvcert.der")];
}
getKey(cert) {
// look for the key corresponding to the cert
// first, search in the registed cert
for (let i = 0, registeredCerts = this.#registeredCerts.concat(globalCerts); i < registeredCerts.length; i++) {
if (0 === Bin.comp(registeredCerts[i], cert)) {
const x509 = X509.decode(registeredCerts[i]);
return x509.spki; // public key only
}
}
// at the moment there is only one key
if (this.#clientCertificates[0]) {
if (0 === Bin.comp(this.#clientCertificates[0], cert))
return PKCS8.parse(new Uint8Array(this.#clientKey));
}
}
findPreferredCert(types, names) {
return this.#clientCertificates ?? [];
}
getIndex(name, target) {
if (!target)
return -1;
const data = new Resource(name);
for (let i = 0; ((i + 1) * 20) <= data.byteLength; i++) {
if (!Bin.comp(data.slice(i * 20, (i + 1) * 20, false), target))
return i;
}
return -1;
}
findCert(fname, target) {
let data = this.getIndex(fname, target);
if (data < 0)
return; // undefined
data = getResource("ca" + data + ".der");
if (this.#verify) {
const validity = X509.decodeTBS(X509.decode(new Uint8Array(data)).tbs).validity;
const now = Date.now();
if (!((validity.from < now) && (now < validity.to))) {
trace("date validation failed on certificate resource\n");
return;
}
}
return X509.decodeSPKI(data);
}
verify(certs, options) {
if (!this.#verify)
return true;
let length = certs.length - 1, x509, validity, now = Date.now(), spki, match;
const registeredCerts = this.#registeredCerts.concat(globalCerts);
let tls_server_name = options.tls_server_name?.toLowerCase();
if (!tls_server_name) {
trace(`tls_server_name required\n`);
return false;
}
// this approach calls decodeSPKI once more than necessary in favor of minimizing memory use
for (let i = 0; i < length; i++) {
x509 = X509.decode(certs[i]);
const names = X509.decodeSAN(certs[i]);
for (let j = 0; j < names?.length; j++) {
let name = names[j];
if (2 === name.tag) { // dNSName
name = name.value.toLowerCase();
if (42 === name.charCodeAt(0)) { // wildcard ("*")
if (name.indexOf("*", 1) > 0) // only one wild card
continue;
let position = tls_server_name.indexOf(".");
if (position < 0) continue;
match = name.slice(1).toLowerCase() === tls_server_name.slice(position);
}
else
match = name.toLowerCase() === tls_server_name;
}
else if (7 === name.tag) { // iPAddress
name = name.value;
if (4 !== name.byteLength) continue; // only handling IPv4 for now
name = (new Uint8Array(name)).join(".");
match = name === tls_server_name;
}
if (match) break;
}
validity = X509.decodeTBS(x509.tbs).validity;
if (!((validity.from < now) && (now < validity.to))) {
trace("date validation failed on received certificate\n");
continue;
}
if (!this._verify(X509.decodeSPKI(certs[i + 1]), x509))
continue;
x509 = undefined;
let aki = X509.decodeAKI(certs[i + 1]);
for (let j = 0; j < registeredCerts.length; j++) {
if (Bin.comp(X509.decodeSKI(registeredCerts[j]), aki) == 0) {
spki = X509.decodeSPKI(registeredCerts[j]);
if (spki && this._verify(spki, X509.decode(certs[i + 1]))) {
if (!match) trace(`subjectAltName match failed for ${tls_server_name}\n`);
return match;
}
spki = undefined;
}
}
spki = this.findCert("ca.ski", aki);
aki = undefined;
if (spki && this._verify(spki, X509.decode(certs[i + 1]))) {
if (!match) trace(`subjectAltName match failed for ${tls_server_name}\n`);
return match;
}
// else fall thru
spki = undefined;
}
x509 = X509.decode(certs[length]);
validity = X509.decodeTBS(x509.tbs).validity;
if (!((validity.from < now) && (now < validity.to))) {
trace("date validation failed\n");
return false;
}
spki = this.findCert("ca.ski", X509.decodeAKI(certs[length]));
if (spki && this._verify(spki, x509)) {
if (!match) trace(`subjectAltName match failed for ${tls_server_name}\n`);
return match;
}
// else fall thru
for (let i = 0; i < registeredCerts.length; i++) {
spki = X509.decodeSPKI(registeredCerts[i]);
if (spki && this._verify(spki, x509)) {
if (!match) trace(`subjectAltName match failed for ${tls_server_name}\n`);
return match;
}
}
return false;
}
_verify(spki, x509) {
let pk, hash;
let sig = x509.sig;
switch (x509.algo.toString()) {
case "1,2,840,113549,1,1,4": // PKCS-1 MD5 with RSA encryption
hash = "MD5";
pk = PKCS1_5;
break;
case "1,2,840,113549,1,1,5": // PKCS-1 SHA1 with RSA encryption
hash = "SHA1";
pk = PKCS1_5;
break;
case "1,2,840,113549,1,1,11": // PKCS-1 SHA256 with RSA encryption
hash = "SHA256";
pk = PKCS1_5;
break;
case "1,2,840,113549,1,1,12": // PKCS-1 SHA384 with RSA encryption
hash = "SHA384";
pk = PKCS1_5;
break;
case "1,2,840,113549,1,1,13": // PKCS-1 SHA512 with RSA encryption
hash = "SHA512";
pk = PKCS1_5;
break;
case "1,2,840,113549,1,1,14": // PKCS-1 SHA224 with RSA encryption
hash = "SHA224";
pk = PKCS1_5;
break;
case "1,2,840,10040,4,3":
case "1,3,14,3,2,27": {
hash = "SHA1";
pk = DSA;
// need to decode the sig value into <r, s>
const ber = new BER(x509.sig);
if (ber.getTag() !== 0x30)
throw new Error;
ber.getLength();
sig = (ArrayBuffer.fromBigInt(ber.getInteger())).concat(ArrayBuffer.fromBigInt(ber.getInteger()));
} break;
case "1,2,840,10045,4,3,1":
case "1,2,840,10045,4,3,2":
case "1,2,840,10045,4,3,3":
case "1,2,840,10045,4,3,4": { // ECDSA with SHA224, SHA256, SHA384, SHA512
hash = ["SHA224", "SHA256", "SHA384", "SHA512"][x509.algo[6] - 1];
pk = ECDSA;
// need to decode the sig value into <r, s>
const ber = new BER(sig);
if (ber.getTag() !== 0x30)
throw new Error;
ber.getLength();
sig = (ArrayBuffer.fromBigInt(ber.getInteger())).concat(ArrayBuffer.fromBigInt(ber.getInteger()));
return (new pk(spki.pub, spki.curve, false, [] /* any oid */)).verify((new Crypt.Digest(hash)).process(x509.tbs), sig);
} break;
default:
throw new Error("Cert: unsupported algorithm: " + x509.algo.toString());
}
return (new pk(spki, false, [] /* any oid */)).verify((new Crypt.Digest(hash)).process(x509.tbs), sig);
}
register(cert) {
if (this.#verify) {
let validity = X509.decodeTBS(X509.decode(new Uint8Array(cert)).tbs).validity, now = Date.now();
if (!((validity.from < now) && (now < validity.to)))
throw new Error("date validation failed");
}
this.#registeredCerts.push(cert);
}
getDH() {
let dh = getResource("dh.der");
let ber = new BER(dh);
if (ber.getTag() == 0x30) {
ber.getLength();
let p = ber.getInteger();
let g = ber.getInteger();
return {p, g};
}
}
static register(cert) {
for (let i = 0; i < globalCerts.length; i++) {
if (!Bin.comp(globalCerts[i], cert))
return;
}
globalCerts.push(cert);
}
}
function getResource(name)
{
return new Resource(name);
}
export default CertificateManager;