forked from bmayton/node-emberplus
-
Notifications
You must be signed in to change notification settings - Fork 7
/
ber.js
executable file
·340 lines (287 loc) · 10.2 KB
/
ber.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
/****************************************************************************
* This file extends node-asn1's functionality in two main ways.
*
* The first is through getSequence on the reader. This consumes the next
* item entirely, returning a new Reader with its contents. This makes it
* much easier to read containers with an unknown count of items, without
* the application having to keep track of how much it's consumed and how
* long the original container was.
*
* The second is through the addition of readReal/writeReal methods. These
* are a little bit scary to do in JavaScript, which doesn't have real
* integer types. Unfortunately, most of the implementations of BER out
* there are for doing PKI stuff, so I've been able to find few real-world
* examples of real value encoding/decoding for BER. These routines are
* inspired heavily by libember, which works with the bits of an IEEE
* double. Note that this is *not* a complete implementation of X.690,
* but only the subset required by EmBER (only base 2 for the exponent, and
* only binary encoding).
*
* There are a few other methods thrown in to simplify many of the
* structures encountered in EmBER.
***************************************************************************/
const BER = require('asn1').Ber;
const errors = require('./Errors.js');
const util = require('util');
const Long = require('long');
var APPLICATION = function(x) { return x | 0x60; };
var CONTEXT = function(x) { return x | 0xa0; };
var UNIVERSAL = function(x) { return x; };
const EMBER_BOOLEAN = 1;
const EMBER_INTEGER = 2;
const EMBER_BITSTRING = 3;
const EMBER_OCTETSTRING = 4;
const EMBER_NULL = 5;
const EMBER_OBJECTIDENTIFIER = 6;
const EMBER_OBJECTDESCRIPTOR = 7;
const EMBER_EXTERNAL = 8;
const EMBER_REAL = 9;
const EMBER_ENUMERATED = 10;
const EMBER_EMBEDDED = 11;
const EMBER_STRING = 12;
const EMBER_RELATIVE_OID = 13;
const EMBER_SEQUENCE = 0x20 | 16;
const EMBER_SET = 0x20 | 17;
module.exports.APPLICATION = APPLICATION;
module.exports.CONTEXT = CONTEXT;
module.exports.UNIVERSAL = UNIVERSAL;
module.exports.EMBER_SET = EMBER_SET;
module.exports.EMBER_SEQUENCE = EMBER_SEQUENCE;
module.exports.EMBER_BOOLEAN = EMBER_BOOLEAN;
module.exports.EMBER_INTEGER = EMBER_INTEGER;
module.exports.EMBER_BITSTRING = EMBER_BITSTRING;
module.exports.EMBER_OCTETSTRING = EMBER_OCTETSTRING;
module.exports.EMBER_NULL = EMBER_NULL;
module.exports.EMBER_OBJECTIDENTIFIER = EMBER_OBJECTIDENTIFIER;
module.exports.EMBER_OBJECTDESCRIPTOR = EMBER_OBJECTDESCRIPTOR;
module.exports.EMBER_EXTERNAL = EMBER_EXTERNAL;
module.exports.EMBER_REAL = EMBER_REAL;
module.exports.EMBER_ENUMERATED = EMBER_ENUMERATED;
module.exports.EMBER_EMBEDDED = EMBER_EMBEDDED;
module.exports.EMBER_STRING = EMBER_STRING;
module.exports.EMBER_RELATIVE_OID = EMBER_RELATIVE_OID;
function ExtendedReader(data) {
ExtendedReader.super_.call(this, data);
}
util.inherits(ExtendedReader, BER.Reader);
module.exports.Reader = ExtendedReader;
ExtendedReader.prototype.getSequence = function(tag) {
var buf = this.readString(tag, true);
return new ExtendedReader(buf);
}
/**
Value ::=
CHOICE {
integer Integer64,
real REAL,
string EmberString,
boolean BOOLEAN,
octets OCTET STRING,
null NULL
} */
ExtendedReader.prototype.readValue = function() {
var tag = this.peek();
if(tag == EMBER_STRING) {
return this.readString(EMBER_STRING);
} else if(tag == EMBER_INTEGER) {
return this.readInt();
} else if(tag == EMBER_REAL) {
return this.readReal();
} else if(tag == EMBER_BOOLEAN) {
return this.readBoolean();
} else if(tag == EMBER_OCTETSTRING) {
return this.readString(UNIVERSAL(4), true);
} else if (tag === EMBER_RELATIVE_OID) {
return this.readOID(EMBER_RELATIVE_OID);
}
else {
throw new errors.UnimplementedEmberTypeError(tag);
}
}
ExtendedReader.prototype.readReal = function(tag) {
if(tag != null) {
tag = UNIVERSAL(9);
}
var b = this.peek();
if(b === null) {
return null;
}
var buf = this.readString(b, true);
if(buf.length == 0) {
return 0;
}
//console.log(buf);
var preamble = buf.readUInt8(0);
var o = 1;
if(buf.length == 1 && preamble == 0x40) {
return Infinity;
} else if(buf.length == 1 && preamble == 0x41) {
return -Infinity;
} else if(buf.length == 1 && preamble == 0x42) {
return NaN;
}
var sign = (preamble & 0x40)? -1 : 1;
var exponentLength = 1 + (preamble & 3);
var significandShift = (preamble >> 2) & 3;
var exponent = 0;
if(buf.readUInt8(o) & 0x80) {
exponent = -1;
}
if(buf.length - o < exponentLength) {
throw new errors.ASN1Error('Invalid ASN.1; not enough length to contain exponent');
}
for(var i=0; i<exponentLength; i++) {
exponent = (exponent << 8) | buf.readUInt8(o++);
}
var significand = new Long(0, 0, true);
while(o < buf.length) {
significand = significand.shl(8).or(buf.readUInt8(o++));
}
significand = significand.shl(significandShift);
var mask = Long.fromBits(0x00000000, 0x7FFFF000, true)
while(significand.and(mask).eq(0)) {
significand = significand.shl(8);
}
mask = Long.fromBits(0x00000000, 0x7FF00000, true)
while(significand.and(mask).eq(0)) {
significand = significand.shl(1);
}
significand = significand.and(Long.fromBits(0xFFFFFFFF, 0x000FFFFF, true));
exponent = Long.fromNumber(exponent);
var bits = exponent.add(1023).shl(52).or(significand);
if(sign < 0) {
bits = bits.or(Long.fromBits(0x00000000, 0x80000000, true));
}
var fbuf = Buffer.alloc(8);
fbuf.writeUInt32LE(bits.getLowBitsUnsigned(), 0);
fbuf.writeUInt32LE(bits.getHighBitsUnsigned(), 4);
return fbuf.readDoubleLE(0);
}
function ExtendedWriter(options) {
ExtendedWriter.super_.call(this, options);
}
ExtendedWriter._shorten = function(value) {
var size = 4;
while((((value & 0xff800000) === 0) || ((value & 0xff800000) === 0xff800000 >> 0)) &&
(size > 1)) {
size--;
value <<= 8;
}
return {size, value}
}
ExtendedWriter._shortenLong = function(value) {
var mask = Long.fromBits(0x00000000, 0xff800000, true);
value = value.toUnsigned();
var size = 8;
while(value.and(mask).eq(0) || (value.and(mask).eq(mask) && (size > 1))) {
size--;
value = value.shl(8);
}
return {size, value};
}
ExtendedWriter.prototype.writeReal = function(value, tag) {
if(tag === undefined) {
tag = UNIVERSAL(9);
}
this.writeByte(tag);
if(value == 0) {
this.writeLength(0);
return;
} else if(value == Infinity) {
this.writeLength(1);
this.writeByte(0x40);
return;
} else if(value == -Infinity) {
this.writeLength(1);
this.writeByte(0x41);
return;
} else if(isNaN(value)) {
this.writeLength(1);
this.writeByte(0x42);
return;
}
var fbuf = Buffer.alloc(8);
fbuf.writeDoubleLE(value, 0);
var bits = Long.fromBits(fbuf.readUInt32LE(0), fbuf.readUInt32LE(4), true);
//console.log(bits);
var significand = bits.and(Long.fromBits(0xFFFFFFFF, 0x000FFFFF, true)).or(
Long.fromBits(0x00000000, 0x00100000, true));
var exponent = bits.and(Long.fromBits(0x00000000, 0x7FF00000, true)).shru(52)
.sub(1023).toSigned();
while(significand.and(0xFF) == 0)
significand = significand.shru(8);
while(significand.and(0x01) == 0)
significand = significand.shru(1);
//console.log(significand, exponent);
exponent = exponent.toNumber();
//console.log(significand.toNumber(), exponent);
exponent = ExtendedWriter._shorten(exponent);
significand = ExtendedWriter._shortenLong(significand);
this.writeLength(1 + exponent.size + significand.size);
var preamble = 0x80;
if(value < 0) preamble |= 0x40;
this.writeByte(preamble);
for(var i=0; i<exponent.size; i++) {
this.writeByte((exponent.value & 0xFF000000) >> 24);
exponent.value <<= 8;
}
var mask = Long.fromBits(0x00000000, 0xFF000000, true);
for(var i=0; i<significand.size; i++) {
var b = significand.value.and(mask);
//console.log("masked:", b);
this.writeByte(significand.value.and(mask).shru(56).toNumber());
significand.value = significand.value.shl(8);
}
}
ExtendedWriter.prototype.writeValue = function(value, tag) {
// accepts Ember.ParameterContents for enforcing real types
if(typeof value === 'object' && value.type && value.type.key && value.type.key.length && typeof value.type.key === 'string') {
if(value.type.key === 'real') {
this.writeReal(value.value, tag);
return
}
}
if(Number.isInteger(value)) {
if (tag === undefined) {
tag = EMBER_INTEGER;
}
this.writeInt(value, tag);
} else if(typeof value == 'boolean') {
if (tag === undefined) {
tag = EMBER_BOOLEAN;
}
this.writeBoolean(value, tag);
} else if(typeof value == 'number') {
if (tag === undefined) {
tag = EMBER_REAL;
}
this.writeReal(value, tag);
} else if(Buffer.isBuffer(value)) {
this.writeBuffer(value, tag);
} else {
if (tag === undefined) {
tag = EMBER_STRING;
}
this.writeString(value.toString(), tag);
}
}
ExtendedWriter.prototype.writeIfDefined = function(property, writer, outer, inner) {
if(property != null) {
this.startSequence(CONTEXT(outer));
writer.call(this, property, inner);
this.endSequence();
}
}
ExtendedWriter.prototype.writeIfDefinedEnum = function(property, type, writer, outer, inner) {
if(property != null) {
this.startSequence(CONTEXT(outer));
if(property.value != null) {
writer.call(this, property.value, inner);
} else {
writer.call(this, type.get(property), inner);
}
this.endSequence();
}
}
util.inherits(ExtendedWriter, BER.Writer);
module.exports.Writer = ExtendedWriter;