Use special rules for Integers and Bit Streams

This commit is contained in:
AJ ONeal 2018-11-21 22:27:43 -07:00
parent 41312c53ac
commit 5917757dfb

View file

@ -11,12 +11,14 @@ var ASN1 = module.exports = function ASN1() {
}; };
ASN1.parse = function parseAsn1(buf, depth) { ASN1.parse = function parseAsn1(buf, depth) {
console.log('');
if (depth >= 100) { throw new Error(EDEEP); } if (depth >= 100) { throw new Error(EDEEP); }
var index = 2; // we know, at minimum, data starts after type (0) and lengthSize (1) var index = 2; // we know, at minimum, data starts after type (0) and lengthSize (1)
var asn1 = { type: buf[0], lengthSize: 0, length: buf[1] }; var asn1 = { type: buf[0], lengthSize: 0, length: buf[1] };
var child; var child;
var iters = 0; var iters = 0;
var adjust = 0;
// Determine how many bytes the length uses, and what it is // Determine how many bytes the length uses, and what it is
if (0x80 & asn1.length) { if (0x80 & asn1.length) {
@ -26,9 +28,18 @@ ASN1.parse = function parseAsn1(buf, depth) {
index += asn1.lengthSize; index += asn1.lengthSize;
} }
// High-order bit Integers have a leading 0x00 to signify that they are positive.
// Bit Streams use the first byte to signify padding, which x.509 doesn't use.
console.log(buf[index], asn1.type);
if (0x00 === buf[index] && (0x02 === asn1.type || 0x03 === asn1.type)) {
console.log('chomp');
index += 1;
adjust = -1;
}
// this is a primitive value type // this is a primitive value type
if (-1 !== VTYPES.indexOf(asn1.type)) { if (-1 !== VTYPES.indexOf(asn1.type)) {
asn1.value = buf.slice(index, index + asn1.length); asn1.value = buf.slice(index, index + asn1.length + adjust);
return asn1; return asn1;
} }