Giter VIP home page Giter VIP logo

node-asn1's Introduction

node-asn1

node-asn1 is a library for encoding and decoding ASN.1 datatypes in pure JS. Currently BER encoding is supported; at some point I'll likely have to do DER.

Usage

Mostly, if you're actually needing to read and write ASN.1, you probably don't need this readme to explain what and why. If you have no idea what ASN.1 is, see A Layman's Guide to a Subset of ASN.1, BER, and DER.

The source is pretty much self-explanatory, and has read/write methods for the common types out there.

Decoding

The following reads an ASN.1 sequence with a boolean.

var Ber = require('asn1').Ber;

var reader = new Ber.Reader(Buffer.from([0x30, 0x03, 0x01, 0x01, 0xff]));

reader.readSequence();
console.log('Sequence len: ' + reader.length);
if (reader.peek() === Ber.Boolean)
  console.log(reader.readBoolean());

Encoding

The following generates the same payload as above.

var Ber = require('asn1').Ber;

var writer = new Ber.Writer();

writer.startSequence();
writer.writeBoolean(true);
writer.endSequence();

console.log(writer.buffer);

Installation

npm install asn1

License

MIT.

Bugs

See https://github.com/joyent/node-asn1/issues.

node-asn1's People

Contributors

arekinath avatar bahamat avatar cburroughs avatar chalker avatar danbell avatar dgwynne avatar isolus avatar mcavage avatar melloc avatar pfmooney avatar trentm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-asn1's Issues

reader.js is not reading the TAG correctly

I am writing a lambda function in aws using node.js to ssh to an ec2 instance. I want to use public key private key pairs so I don't have to embed a password in the code. I'm using simple-ssh as a wrapper library to ssh2 which in turn calls asn1 reader.js to parse the private key to verify its validity. There appears to be a problem with indexing through the first 8 bytes of the key which manifests itself in the method readString. The first 8 bytes are as follows: 30 82 04 bd 02 01 00 30. By the time readSting is called, the offset variable is 7 which causes the code to read the 8th byte which is 0x30. readString compares this value with the TAG value which is 0x02 and throws an error stating that the key is invalid because 0x30 doesn't match 0x02. If I load my private key into this website https://lapo.it/asn1js/ it can parse the key without fail. I also found some javascript code that also is consistent with how the first 8 bytes are indexed and successfully parses the key. I think that the offsets are being handled incorrectly in the code.

What does InvalidAsn1Error mean?

I'm trying to use a library called node-rsa which uses node-asn1 and it's giving me this error:

Uncaught InvalidAsn1Error: Expected 0x2: got 0x24

I basically have a private key PEM string that works for node-rsa, then I put it in a <textarea>, then I get it back out of the textarea using $('textarea').text() and now it doesn't work with node-rsa, giving me that error.

For example:

      var NodeRSA = require("node-rsa");
      var rsa = new NodeRSA();
      rsa.generateKeyPair(256);
      console.log(rsa.getPrivatePEM())

      $("#public_key").text(rsa.getPrivatePEM());
      console.log($("#public_key").text()); // looks the same as the first output.
      rsa2 = new NodeRSA();
      rsa2.loadFromPEM($("#public_key").text()); // InvalidAsn1Error

What's it mean? Is the new line character format changed or something?

InvalidAsn1Error: 'Expected 0x2: got 0x0'

How to solve this issue?

{ [InvalidAsn1Error: Expected 0x2: got 0x0] name: 'InvalidAsn1Error', message: 'Expected 0x2: got 0x0' }

/var/www/courious/node_modules/net-snmp/index.js:918
var req = this.unregisterRequest (message.pdu.id);
^
TypeError: Cannot read property 'pdu' of undefined
at Session.onMsg (/var/www/courious/node_modules/net-snmp/index.js:918:43)
at Socket.EventEmitter.emit (events.js:98:17)
at UDP.onMessage (dgram.js:440:8)

This error message is displayed in an attempt to capture SNMP information from a printer. It would be wrong of LIB ASN1 or LIB net-snmp that makes use of this?

builds fail on travis because 0.8 has bitrotted away

$ node --version

v0.8.28

$ npm --version

1.2.30

$ nvm --version

0.35.0

5.73s$ npm install 

npm http GET https://registry.npmjs.org/safer-buffer

npm http GET https://registry.npmjs.org/istanbul

npm http GET https://registry.npmjs.org/faucet/0.0.1

npm http GET https://registry.npmjs.org/eslint/2.13.1

npm http GET https://registry.npmjs.org/tape

npm http GET https://registry.npmjs.org/eslint-plugin-joyent

npm http 200 https://registry.npmjs.org/safer-buffer

npm http GET https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz

npm http 200 https://registry.npmjs.org/tape

npm http 200 https://registry.npmjs.org/istanbul

npm http 200 https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz

npm ERR! Error: No compatible version found: tape@'^3.5.0'

Mistype in writer.js

It seems that here is a mistype:

  } else {
    throw new InvalidAsn1ERror('Length too long (> 4 bytes)');
  }

InvalidAsn1ERror -> InvalidAsn1Error

NPM says node-asn1 is not a package

In package.json, using

"dependencies": {
    "asn1": "https://github.com/mcavage/node-asn1"
}

results in:

% npm install [email protected]
npm WARN deprecated [email protected]: use uuid module instead
npm ERR! not a package /tmp/npm-9658-QcJxFsXz/github.com/mcavage/node-asn1
npm ERR! Error: ENOENT, open '/tmp/npm-9658-QcJxFsXz/github.com/mcavage/node-asn1-unpack/package.json'

Errors

  1. when reading Enumeration , it's write
    Catched error: InvalidAsn1Error: Expected 0xa: got 0xa0
    but we know Enumeration != 0xa && Enumeration == 0xa0

  2. when reading DER, must read not base64, but decoded base64:

let crtInPemBfr = Buffer.from(crtPem)
let crtInHexBfr = Buffer.from(crtInPemBfr , 'base64')
let reader = new asn1.BerReader(crtInHexBfr )

Error while reading buffer

I am parsing the packets from hex-dump using pcap-parser and passing the packet buffer to asn onMsg()
But this buffer is not valid for processing.

Any help will be appreciated.

Zero-length strings aren't read correctly

The method readString of ASN1.Reader returns an empty string whenever an empty string is encountered, regardless of whether retbuf is set to true or not, causing unexpected behavior in applications that rely on a Buffer being returned.

Here's a small patch that fixes the issue;

diff --git a/lib/ber/reader.js b/lib/ber/reader.js
index 9745dc3..46fba61 100644
--- a/lib/ber/reader.js
+++ b/lib/ber/reader.js
@@ -171,7 +171,7 @@ Reader.prototype.readString = function(tag, retbuf) {
   this._offset = o;

   if (this.length === 0)
-    return '';
+    return retbuf ? new Buffer(0) : '';

   var str = this._buf.slice(this._offset, this._offset + this.length);
   this._offset += this.length;

Add "license" and "licenses" to package.json

Specifying your license in package.json makes it a lot easier for your users to find and comply with GNU Affero licenses.

Also giving licenses (an array of objects with type and url keys) helps users whose legal departments request both the license name and the text. See this example from async:

  "licenses": [{
    "type": "MIT",
    "url": "https://github.com/caolan/async/raw/master/LICENSE"
  }]

Though licenses isn't specified in the npm package.json spec, it is supported by the NPM site, as you'll see if you check async's NPM page.

Thanks!

OID values should be unsigned int

I think there is a problem in the readOID function when a value in the OID is higher than 2^31. It is returning negative numbers. For example:

var ber = require("asn1").Ber;

// Buffer contains OID 1.3.6.1.4.1.14988.1.1.5.1.1.19.2887117176 
var data = new Buffer([6,18,43,6,1,4,1,245,12,1,1,5,1,1,19,138,224,215,210,120]);
var reader = new ber.Reader (data)
console.log(reader.readOID());
// prints "1.3.6.1.4.1.14988.1.1.5.1.1.19.-1407850120"

A possible fix would be casting each value to unsigned integer before storing. I.e replacing values.push(value) with values.push(value >>> 0)

unable to install asn1

โ˜   sudo npm -g remove asn1.js && sudo npm -g install asn1.js
unbuild [email protected]
npm ERR! peerinvalid The package asn1.js does not satisfy its siblings' peerDependencies requirements!
npm ERR! peerinvalid Peer [email protected] wants asn1.js@^0.6.0

npm ERR! System Darwin 14.0.0
npm ERR! command "node" "/usr/local/bin/npm" "-g" "install" "asn1.js"
npm ERR! cwd /Users/marc/.npm/asn1.js-rfc3280/0.5.1/package
npm ERR! node -v v0.10.31
npm ERR! npm -v 2.0.0-beta.0
npm ERR! code EPEERINVALID
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/marc/.npm/asn1.js-rfc3280/0.5.1/package/npm-debug.log
npm ERR! not ok code 0

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.