Skip to content

Commit

Permalink
der: support negative integers
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko committed Jul 24, 2019
1 parent 48811c6 commit e5a10a3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/asn1/decoders/der.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ DERNode.prototype._decodeBool = function decodeBool(buffer) {
DERNode.prototype._decodeInt = function decodeInt(buffer, values) {
// Bigint, return as it is (assume big endian)
const raw = buffer.raw();
let res = new bignum(raw);
let res = new bignum(raw).fromTwos(raw.length * 8);

if (values)
res = values[res.toString(10)] || res;
Expand Down
22 changes: 9 additions & 13 deletions lib/asn1/encoders/der.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,22 +199,18 @@ DERNode.prototype._encodeInt = function encodeInt(num, values) {
if (!BN.isBN(num)) {
num = new BN(num);
}
const numArray = num.toArray();
if (!num.sign && numArray[0] & 0x80) {
numArray.unshift(0);
if (num.isZero()) {
num = Buffer.from([0]);
} else {
const numArray = num.toTwos(num.byteLength() * 8).toArray();
if (num.isNeg() != Boolean(numArray[0] & 0x80)) {
numArray.unshift(num.isNeg() ? 0xff : 0);
}
num = Buffer.from(numArray);
}
num = new Buffer(numArray);
}

let size = num.length;
if (num.length === 0)
size++;

const out = new Buffer(size);
num.copy(out);
if (num.length === 0)
out[0] = 0;
return this._createEncoderBuffer(out);
return this._createEncoderBuffer(num);
};

DERNode.prototype._encodeBool = function encodeBool(value) {
Expand Down

0 comments on commit e5a10a3

Please sign in to comment.