Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
chrillefkr opened this issue May 13, 2020 · 0 comments
Open

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

chrillefkr opened this issue May 13, 2020 · 0 comments

Comments

@chrillefkr
Copy link

chrillefkr commented May 13, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant