Skip to content

Commit c5169b6

Browse files
author
Mark Cavage
committed
Support for writing OIDs (DER)
1 parent f0e34da commit c5169b6

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

lib/ber/writer.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ Writer.prototype.writeInt = function(i, tag) {
101101
};
102102

103103

104+
Writer.prototype.writeNull = function() {
105+
this.writeByte(ASN1.Null);
106+
this.writeByte(0x00);
107+
};
108+
109+
104110
Writer.prototype.writeEnumeration = function(i, tag) {
105111
if (typeof(i) !== 'number')
106112
throw new TypeError('argument must be a Number');
@@ -151,6 +157,50 @@ Writer.prototype.writeStringArray = function(strings) {
151157
});
152158
};
153159

160+
// This is really to solve DER cases, but whatever for now
161+
Writer.prototype.writeOID = function(s, tag) {
162+
if (typeof(s) !== 'string')
163+
throw new TypeError('argument must be a string');
164+
if (typeof(tag) !== 'number')
165+
tag = ASN1.OID;
166+
167+
if (!/^([0-9]+\.){3,}[0-9]+$/.test(s))
168+
throw new Error('argument is not a valid OID string');
169+
170+
function encodeOctet(bytes, octet) {
171+
if (octet < 128) {
172+
bytes.push(octet);
173+
} else if (octet < 16384) {
174+
bytes.push((octet >>> 7) | 0x80);
175+
bytes.push(octet & 0x7F);
176+
} else if (octet < 2097152) {
177+
bytes.push((octet >>> 14) | 0x80);
178+
bytes.push(((octet >>> 7) | 0x80) & 0xFF);
179+
bytes.push(octet & 0x7F);
180+
} else if (id < 268435456) {
181+
bytes.push((octet >>> 21) | 0x80);
182+
bytes.push(((octet >>> 14) | 0x80) & 0xFF);
183+
bytes.push(((octet >>> 7) | 0x80) & 0xFF);
184+
bytes.push(octet & 0x7F);
185+
}
186+
}
187+
188+
var tmp = s.split('.');
189+
var bytes = [];
190+
bytes.push(parseInt(tmp[0], 10) * 40 + parseInt(tmp[1], 10));
191+
tmp.slice(2).forEach(function(b) {
192+
encodeOctet(bytes, parseInt(b, 10));
193+
});
194+
195+
var self = this;
196+
this._ensure(2 + bytes.length);
197+
this.writeByte(tag);
198+
this.writeLength(bytes.length);
199+
bytes.forEach(function(b) {
200+
self.writeByte(b);
201+
});
202+
};
203+
154204

155205
Writer.prototype.writeLength = function(len) {
156206
if (typeof(len) !== 'number')
@@ -212,7 +262,7 @@ Writer.prototype.endSequence = function() {
212262
this._buf[seq + 2] = len >> 8;
213263
this._buf[seq + 3] = len;
214264
} else {
215-
throw newInvalidAsn1ERror('Sequence too long');
265+
throw newInvalidAsn1Error('Sequence too long');
216266
}
217267
};
218268

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"author": "Mark Cavage <mcavage@gmail.com>",
33
"name": "asn1",
44
"description": "Contains parsers and serializers for ASN.1 (currently BER only)",
5-
"version": "0.1.6",
5+
"version": "0.1.7",
66
"repository": {
77
"type": "git",
88
"url": "git://github.com/mcavage/node-asn1.git"

tst/ber/writer.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,18 @@ test('LDAP bind message', function(t) {
257257
t.end();
258258
});
259259

260+
261+
test('Write OID', function(t) {
262+
var oid = '1.2.840.113549.1.1.1';
263+
var writer = new BerWriter();
264+
writer.writeOID(oid);
265+
266+
var ber = writer.buffer;
267+
t.ok(ber);
268+
console.log(require('util').inspect(ber));
269+
console.log(require('util').inspect(new Buffer([0x06, 0x09, 0x2a, 0x86,
270+
0x48, 0x86, 0xf7, 0x0d,
271+
0x01, 0x01, 0x01])));
272+
273+
t.end();
274+
});

0 commit comments

Comments
 (0)