Giter Club home page Giter Club logo

node-asn1-ber's Issues

Values greater than 255 can be written to a Buffer object position representing 1 byte

See: TritonDataCenter/node-asn1#6

@@ -233,14 +233,14 @@ Writer.prototype.writeLength = function(len) {
      this._buf[this._offset++] = len;
    } else if (len <= 0xffff) {
      this._buf[this._offset++] = 0x82;
 -    this._buf[this._offset++] = len >> 8;
 -    this._buf[this._offset++] = len;
 +    this._buf[this._offset++] = (len >> 8) & 0xff;
 +    this._buf[this._offset++] = len & 0xff;
    } else if (len <= 0xffffff) {
      this._shift(start, len, 1);
      this._buf[this._offset++] = 0x83;
 -    this._buf[this._offset++] = len >> 16;
 -    this._buf[this._offset++] = len >> 8;
 -    this._buf[this._offset++] = len;
 +    this._buf[this._offset++] = (len >> 16) & 0xff;
 +    this._buf[this._offset++] = (len >> 8) & 0xff;
 +    this._buf[this._offset++] = len & 0xff;
    } else {
      throw new InvalidAsn1ERror('Length too long (> 4 bytes)');
    }
 @@ -271,14 +271,14 @@ Writer.prototype.endSequence = function() {
      this._buf[seq + 1] = len;
    } else if (len <= 0xffff) {
      this._buf[seq] = 0x82;
 -    this._buf[seq + 1] = len >> 8;
 -    this._buf[seq + 2] = len;
 +    this._buf[seq + 1] = (len >> 8) & 0xff;
 +    this._buf[seq + 2] = len & 0xff;
    } else if (len <= 0xffffff) {
      this._shift(start, len, 1);
      this._buf[seq] = 0x83;
 -    this._buf[seq + 1] = len >> 16;
 -    this._buf[seq + 2] = len >> 8;
 -    this._buf[seq + 3] = len;
 +    this._buf[seq + 1] = (len >> 16) & 0xff;
 +    this._buf[seq + 2] = (len >> 8) & 0xff;
 +    this._buf[seq + 3] = len & 0xff;
    } else {
      throw new InvalidAsn1Error('Sequence too long');
    }

Integers greater than 2147483647 are encoded incorrectly

The writeInt instance method of BerWriter erroneously encodes integers in range 2147483648 (0x80000000) to 4294967295 (0xFFFFFFFF) as if they were in range -2147483648 to -1 instead:

let w = new BerWriter;
w.writeInt( 2147483647 );
w.writeInt( 2147483648 );
w.writeInt( 4294967295 );
let r = new BerReader( w.buffer );
console.log( r.readInt() );  // prints 2147483647
console.log( r.readInt() );  // prints -2147483648
console.log( r.readInt() );  // prints -1

The correct encoding for integers in this range uses 5 bytes (with the first byte being 00).

Cannot parse integers greater than 2147483647

BerReader fails to parse integers greater than 2147483647, e.g. this Counter32 value from a captured SNMP packet:

let r = new BerReader( Buffer.from('410500b7518b1a', 'hex') );
console.log( r.readInt(0x41) );  // throws InvalidAsn1Error

And yes I've read the comment above node-net-snmp's workaround for this, but it is simply wrong, the 5-byte encoding is in fact the correct encoding. BER integers are always interpreted as signed, and therefore a leading 00 byte must be prepended to non-negative integers whose leading byte would otherwise be in range 80-FF.

Wondering

Can I decode ber file via your package? If yes, can you give me an example sir, please

Correct names of error classes imported and used in `lib/ber/writer.js` which result in `not defined` error messages

See: https://github.com/mcavage/node-asn1/pull/20/files

@@ -7,7 +7,7 @@ var errors = require('./errors');
  
  ///--- Globals
  
 -var newInvalidAsn1Error = errors.newInvalidAsn1Error;
 +var InvalidAsn1Error = errors.newInvalidAsn1Error;
  
  var DEFAULT_OPTS = {
    size: 1024,
 @@ -241,7 +241,7 @@ Writer.prototype.writeLength = function(len) {
      this._buf[this._offset++] = len >> 8;
      this._buf[this._offset++] = len;
    } else {
 -    throw new InvalidAsn1ERror('Length too long (> 4 bytes)');
 +    throw new InvalidAsn1Error('Length too long (> 4 bytes)');
    }
  };

InvalidAsn1Error on ans1-ber

Can someone explain the below error?

Critical uncaught exception: Error [InvalidAsn1Error]: Expected 0x4: got 0x0
at InvalidAsn1Error (product/node_modules/asn1-ber/lib/ber/errors.js:4:11)
at Reader.readString (product/node_modules/asn1-ber/lib/ber/reader.js:179:9)
at new ResponseMessage (product/node_modules/net-snmp/index.js:532:26)
at Session.onMsg (product/node_modules/net-snmp/index.js:911:17)
at Socket.emit (events.js:375:28)
at UDP.onMessage [as onmessage] (dgram.js:941:8)
at UDP.callbackTrampoline (internal/async_hooks.js:131:17)

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.