Skip to content

ASN1.Spec().toDER() signed/unsigned int ERR_OUT_OF_RANGE #2

@chrillefkr

Description

@chrillefkr

ASN1.Spec doesn't seem to work correctly when converting to DER.
I'm using Node v12.16.1 on Linux.

Running ASN1.Spec(0, []).toDER() results in an error

RangeError [ERR_OUT_OF_RANGE]: The value of "value" is out of range. It must be >= -128 and <= 127. Received 160
    at writeU_Int8 (internal/buffer.js:728:11)
    at Buffer.writeInt8 (internal/buffer.js:859:10)
    at ASN1.toDER (/asn1-test/node_modules/@fidm/asn1/build/asn1.js:859:13)
    at repl:1:18
    at Script.runInThisContext (vm.js:120:20)
    at REPLServer.defaultEval (repl.js:430:29)
    at bound (domain.js:426:14)
    at REPLServer.runBound [as eval] (domain.js:439:12)
    at REPLServer.onLine (repl.js:758:10)
    at REPLServer.emit (events.js:323:22) {
  code: 'ERR_OUT_OF_RANGE'

Seems to be caused by first byte (b1) in src/asn1.ts:959 being unsigned (which I believe is JS standard behaviour), and later on is expected to a signed by buf.writeInt8.

CONTEXT_SPECIFIC = 0x80
0x80 | 0x20 == 0xa0
0xa0 unsigned is 160, while signed is -96.

Expected results is a <Buffer <a0 00>.

I resolved the issue by changing line 964 in src/asn1.ts to buf.writeUInt8(b1, 0) instead of having buf.writeInt8(b1, 0), so that we're parsing the integer as unsigned.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions