-
Notifications
You must be signed in to change notification settings - Fork 2
/
utilsDer.js
342 lines (248 loc) · 7.35 KB
/
utilsDer.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
let Der = {};
const hexAt = "\x00";
const hexB = "\x02";
const hexC = "\x03";
const hexD = "\x04";
const hexF = "\x06";
const hex0 = "\x30";
const hex31 = 0x1f;
const hex127 = 0x7f;
const hex129 = 0xa0;
const hex160 = 0x80;
const hex224 = 0xe0;
const bytesHex0 = BinaryAscii.binaryFromHex("30");
const bytesHexB = BinaryAscii.binaryFromHex("02");
const bytesHexC = BinaryAscii.binaryFromHex("03");
const bytesHexD = BinaryAscii.binaryFromHex("04");
const bytesHexF = BinaryAscii.binaryFromHex("06");
Der.encodeSequence = function () {
let sequence = [];
let totalLengthLen = 0;
for (i=0; i < arguments.length; i++) {
sequence.push(arguments[i]);
totalLengthLen += arguments[i].length;
}
return hex0 + _encodeLength(totalLengthLen) + sequence.join("");
}
Der.encodeInteger = function (x) {
if (x < BigInt(0)) {
throw new Error("x cannot be negative");
}
let t = x.toString(16);
if ((t.length % 2) == 1) {
t = "0" + t;
}
x = BinaryAscii.binaryFromHex(t);
let num;
if (typeof(x[0]) === "number") {
num = x[0];
} else {
num = x.charCodeAt(0);
}
if (num <= hex127) {
return hexB + String.fromCharCode(x.length) + x;
}
return hexB + String.fromCharCode(x.length + 1) + hexAt + x;
}
Der.encodeOid = function (pieces) {
let first = pieces[0];
let second = pieces[1];
pieces = _sliceArray(pieces, 2);
if (first > 2) {
throw new Error("first has to be <= 2");
}
if (second > 39) {
throw new Error("second has to be <= 39");
}
let encodedPieces = [];
pieces.forEach((x) => {encodedPieces.push(_encodeNumber(x))})
let body = [String.fromCharCode(40 * first + second)].concat(encodedPieces).join("");
return hexF + _encodeLength(body.length) + body;
}
Der.encodeBitstring = function (t) {
return hexC + _encodeLength(t.length) + t;
}
Der.encodeOctetString = function (t) {
return hexD + _encodeLength(t.length) + t;
}
Der.encodeConstructed = function (tag, value) {
return String.fromCharCode(hex129 + tag) + _encodeLength(value.length) + value;
}
Der.removeSequence = function (string) {
_checkSequenceError(string, bytesHex0, "03");
let result = _readLength(string.slice(1));
let length = result[0];
let lengthLen = result[1];
endSeq = 1 + lengthLen + length;
return [string.slice(1 + lengthLen, endSeq), string.slice(endSeq)];
}
Der.removeInteger = function (string) {
_checkSequenceError(string, bytesHexB, "02");
let result = _readLength(string.slice(1));
let length = result[0];
let lengthLen = result[1];
let numberBytes = string.slice(1 + lengthLen, 1 + lengthLen + length);
let rest = string.slice(1 + lengthLen + length);
let nBytes;
if (typeof(numberBytes[0]) === "number") {
nBytes = numberBytes[0];
} else {
nBytes = numberBytes.charCodeAt(0);
}
if (nBytes >= hex160) {
throw new Error("nBytes must be < 160");
}
return [BigInt("0x" + BinaryAscii.hexFromBinary(numberBytes)), rest];
}
Der.removeObject = function (string) {
_checkSequenceError(string, bytesHexF, "06");
let result = _readLength(string.slice(1));
let length = result[0];
let lengthLen = result[1];
let body = string.slice(1 + lengthLen, 1 + lengthLen + length);
let rest = string.slice(1 + lengthLen + length);
let numbers = [];
while (body) {
let results = _readNumber(body);
let n = results[0];
let lengthLength = results[1];
numbers.push(n);
body = body.slice(lengthLength);
}
let n0 = numbers[0];
numbers = numbers.slice(1);
let first = Math.floor(n0 / 40);
let second = n0 - (40 * first);
numbers = [first, second].concat(numbers);
return [numbers, rest];
}
Der.removeBitString = function (string) {
_checkSequenceError(string, bytesHexC, "03");
let result = _readLength(string.slice(1));
let length = result[0];
let lengthLen = result[1];
let body = string.slice(1 + lengthLen, 1 + lengthLen + length);
let rest = string.slice(1 + lengthLen + length);
return [body, rest];
}
Der.removeOctetString = function (string) {
_checkSequenceError(string, bytesHexD, "04");
let result = _readLength(string.slice(1));
let length = result[0];
let lengthLen = result[1];
body = string.slice(1 + lengthLen, 1 + lengthLen + length);
rest = string.slice(1 + lengthLen + length);
return [body, rest];
}
Der.removeConstructed = function (string) {
s0 = _extractFirstInt(string);
if ((s0 & hex224) != hex129) {
throw new Error("wanted constructed tag (0xa0-0xbf), got 0x" + s0);
}
tag = s0 & hex31
let result = _readLength(string.slice(1));
let length = result[0];
let lengthLen = result[1];
let body = string.slice(1 + lengthLen, 1 + lengthLen + length);
let rest = string.slice(1 + lengthLen + length);
return [tag, body, rest];
}
Der.fromPem = function (pem) {
let split = pem.split("\n");
let stripped = "";
let i;
for (i = 0; i < split.length; i++) {
if (split[i].indexOf("-----") != 0) {
stripped += split[i].trim();
}
}
return Base64.decode(stripped);
}
Der.toPem = function (der, name) {
let b64 = Base64.encode(der);
lines = [("-----BEGIN " + name + "-----\n")];
for (start = 0; start <= b64.length; start += 64) {
lines.push(b64.slice(start, start + 64) + "\n")
}
lines.push("-----END " + name + "-----\n");
return lines.join("");
}
function _encodeLength (length) {
if (length < 0) {
throw new Error("length cannot be negative");
}
if (length < hex160) {
return String.fromCharCode(length);
}
let s = length.toString(16);
if (modulo(s.length, 2)) {
s = "0" + s;
}
s = BinaryAscii.binaryFromHex(s);
let lengthLen = s.length;
return String.fromCharCode(hex160 | lengthLen) + toString(lengthLen);
}
function _encodeNumber (n) {
let b128Digits = [];
while (n) {
b128Digits.unshift((n & hex127) | hex160);
n >>= 7;
}
if (!b128Digits.length) {
b128Digits.push(0);
}
b128Digits[b128Digits.length - 1] &= hex127;
let encodedDigits = [];
b128Digits.forEach((d) => {encodedDigits.push(String.fromCharCode(d))})
return encodedDigits.join("");
}
function _readLength (string) {
num = _extractFirstInt(string);
if (!(num & hex160)) {
return [(num & hex127), 1];
}
let lengthLen = num & hex127;
if (lengthLen > string.length - 1) {
throw new Error("ran out of length bytes");
}
return [parseInt(BinaryAscii.hexFromBinary(string.slice(1, 1 + lengthLen)), 16), 1 + lengthLen];
}
function _readNumber (string) {
let number = 0;
let lengthLen = 0;
let d;
while (true) {
if (lengthLen > string.length) {
throw new Error("ran out of length bytes");
}
number <<= 7;
d = string.charAt(lengthLen);
if (typeof(d) !== "number") {
d = d.charCodeAt(0);
}
number += (d & hex127);
lengthLen += 1;
if (!(d & hex160)) {
break;
}
}
return [number, lengthLen];
}
function _checkSequenceError(string, start, expected) {
if (!string.startsWith(start)) {
throw new Error("wanted sequence (0x" + expected + "), got 0x" + _extractFirstInt(string).toString(16));
}
}
function _extractFirstInt(string) {
if (typeof(string[0]) === "number") {
return string[0];
}
return string.charCodeAt(0);
}
function _sliceArray(array, start) {
sliced = [];
for (i = start; i < array.length; i++) {
sliced.push(array[i]);
}
return sliced
}