markabrahams / node-asn1-ber Goto Github PK
View Code? Open in Web Editor NEWGenerate and parse ASN.1 BER objects
Generate and parse ASN.1 BER objects
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
).
See: TritonDataCenter/node-asn1#17
@@ -1,7 +1,6 @@
// Copyright 2011 Mark Cavage <[email protected]> All rights reserved.
var test = require('tap').test;
-var sys = require('sys');
///--- Globals
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
.
Can I decode ber file via your package? If yes, can you give me an example sir, please
There is a ticket in SNMP library that you maintain to add Typescript support however I thought It would be worth creating a ticket here too. Adding Typescript here would not be too tricky as they already exist for the original ANS1 project here: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/asn1/index.d.ts
However, I don't know if there have been significant enough changes that those definitions would no longer work.
See: TritonDataCenter/node-asn1#13
@@ -160,7 +160,7 @@ Writer.prototype.writeBuffer = function(buf, tag) {
Writer.prototype.writeStringArray = function(strings) {
- if ((!strings instanceof Array))
+ if (!(strings instanceof Array))
throw new TypeError('argument must be an Array[String]');
var self = this;
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)');
}
};
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');
}
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)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.