GithubHelp home page GithubHelp logo

fidm / asn1 Goto Github PK

View Code? Open in Web Editor NEW
13.0 1.0 7.0 403 KB

ASN.1/DER, PEM for Node.js

Home Page: https://fidm.github.io/asn1/

License: MIT License

JavaScript 2.66% TypeScript 97.34%
asn1 pem der

asn1's Issues

PKCS#8 private key ASN1 object for ECC Private key pem

Hi, I'm working on p12 generation for ECC keytype pem and certs.

I'm getting error when I'm passing EC private key to below logic, can you guide me if you have any idea?

const { PEM, ASN1, Class, Tag } = require('@fidm/asn1')
 
// ASN.1 Template https://tools.ietf.org/html/rfc5208
const privateKeyValidator = {
    name: 'PrivateKeyInfo',
    class: Class.UNIVERSAL,
    tag: Tag.SEQUENCE,
    capture: 'privateKeyInfo',
    value: [{
      name: 'PrivateKeyInfo.Version',
      class: Class.UNIVERSAL,
      tag: Tag.INTEGER,
      capture: 'privateKeyVersion'
    }, {
      name: 'PrivateKeyInfo.AlgorithmIdentifier',
      class: Class.UNIVERSAL,
      tag: Tag.SEQUENCE,
      value: [{
        name: 'PrivateKeyAlgorithmIdentifier.algorithm',
        class: Class.UNIVERSAL,
        tag: Tag.OID,
        capture: 'privateKeyOID'
      }]
    }, {
      name: 'PrivateKeyInfo.PrivateKey',
      class: Class.UNIVERSAL,
      tag: Tag.OCTETSTRING,
      capture: 'privateKey'
    }]
  }
 
const rootkey = PEM.parse(fs.readFileSync('ecctestPK.pem'))[0];
console.log(rootkey)
const captures = ASN1.parseDERWithTemplate(rootkey.body, privateKeyValidator);

console.log("******************")
console.log(captures)```

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

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.