From 5917757dfbdf8c3443be1e12f15d403edffddc01 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 21 Nov 2018 22:27:43 -0700 Subject: [PATCH] Use special rules for Integers and Bit Streams --- lib/uasn1.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/uasn1.js b/lib/uasn1.js index 6b929ca..872a4fe 100644 --- a/lib/uasn1.js +++ b/lib/uasn1.js @@ -11,12 +11,14 @@ var ASN1 = module.exports = function ASN1() { }; ASN1.parse = function parseAsn1(buf, depth) { + console.log(''); if (depth >= 100) { throw new Error(EDEEP); } 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 child; var iters = 0; + var adjust = 0; // Determine how many bytes the length uses, and what it is if (0x80 & asn1.length) { @@ -26,9 +28,18 @@ ASN1.parse = function parseAsn1(buf, depth) { 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 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; }