Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reader.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. var assert = require('assert');
  2. var ASN1 = require('./types');
  3. var errors = require('./errors');
  4. ///--- Globals
  5. var InvalidAsn1Error = errors.InvalidAsn1Error;
  6. ///--- API
  7. function Reader(data) {
  8. if (!data || !Buffer.isBuffer(data))
  9. throw new TypeError('data must be a node Buffer');
  10. this._buf = data;
  11. this._size = data.length;
  12. // These hold the "current" state
  13. this._len = 0;
  14. this._offset = 0;
  15. }
  16. Object.defineProperty(Reader.prototype, 'length', {
  17. enumerable: true,
  18. get: function () { return (this._len); }
  19. });
  20. Object.defineProperty(Reader.prototype, 'offset', {
  21. enumerable: true,
  22. get: function () { return (this._offset); }
  23. });
  24. Object.defineProperty(Reader.prototype, 'remain', {
  25. get: function () { return (this._size - this._offset); }
  26. });
  27. Object.defineProperty(Reader.prototype, 'buffer', {
  28. get: function () { return (this._buf.slice(this._offset)); }
  29. });
  30. /**
  31. * Reads a single byte and advances offset; you can pass in `true` to make this
  32. * a "peek" operation (i.e., get the byte, but don't advance the offset).
  33. *
  34. * @param {Boolean} peek true means don't move offset.
  35. * @return {Number} the next byte, null if not enough data.
  36. */
  37. Reader.prototype.readByte = function(peek) {
  38. if (this._size - this._offset < 1)
  39. return null;
  40. var b = this._buf[this._offset] & 0xff;
  41. if (!peek)
  42. this._offset += 1;
  43. return b;
  44. };
  45. Reader.prototype.peek = function() {
  46. return this.readByte(true);
  47. };
  48. /**
  49. * Reads a (potentially) variable length off the BER buffer. This call is
  50. * not really meant to be called directly, as callers have to manipulate
  51. * the internal buffer afterwards.
  52. *
  53. * As a result of this call, you can call `Reader.length`, until the
  54. * next thing called that does a readLength.
  55. *
  56. * @return {Number} the amount of offset to advance the buffer.
  57. * @throws {InvalidAsn1Error} on bad ASN.1
  58. */
  59. Reader.prototype.readLength = function(offset) {
  60. if (offset === undefined)
  61. offset = this._offset;
  62. if (offset >= this._size)
  63. return null;
  64. var lenB = this._buf[offset++] & 0xff;
  65. if (lenB === null)
  66. return null;
  67. if ((lenB & 0x80) == 0x80) {
  68. lenB &= 0x7f;
  69. if (lenB == 0)
  70. throw InvalidAsn1Error('Indefinite length not supported');
  71. // Caused problems for node-net-snmp issue #172
  72. // if (lenB > 4)
  73. // throw InvalidAsn1Error('encoding too long');
  74. if (this._size - offset < lenB)
  75. return null;
  76. this._len = 0;
  77. for (var i = 0; i < lenB; i++) {
  78. this._len *= 256;
  79. this._len += (this._buf[offset++] & 0xff);
  80. }
  81. } else {
  82. // Wasn't a variable length
  83. this._len = lenB;
  84. }
  85. return offset;
  86. };
  87. /**
  88. * Parses the next sequence in this BER buffer.
  89. *
  90. * To get the length of the sequence, call `Reader.length`.
  91. *
  92. * @return {Number} the sequence's tag.
  93. */
  94. Reader.prototype.readSequence = function(tag) {
  95. var seq = this.peek();
  96. if (seq === null)
  97. return null;
  98. if (tag !== undefined && tag !== seq)
  99. throw InvalidAsn1Error('Expected 0x' + tag.toString(16) +
  100. ': got 0x' + seq.toString(16));
  101. var o = this.readLength(this._offset + 1); // stored in `length`
  102. if (o === null)
  103. return null;
  104. this._offset = o;
  105. return seq;
  106. };
  107. Reader.prototype.readInt = function(tag) {
  108. // if (typeof(tag) !== 'number')
  109. // tag = ASN1.Integer;
  110. return this._readTag(tag);
  111. };
  112. Reader.prototype.readBoolean = function(tag) {
  113. if (typeof(tag) !== 'number')
  114. tag = ASN1.Boolean;
  115. return (this._readTag(tag) === 0 ? false : true);
  116. };
  117. Reader.prototype.readEnumeration = function(tag) {
  118. if (typeof(tag) !== 'number')
  119. tag = ASN1.Enumeration;
  120. return this._readTag(tag);
  121. };
  122. Reader.prototype.readString = function(tag, retbuf) {
  123. if (!tag)
  124. tag = ASN1.OctetString;
  125. var b = this.peek();
  126. if (b === null)
  127. return null;
  128. if (b !== tag)
  129. throw InvalidAsn1Error('Expected 0x' + tag.toString(16) +
  130. ': got 0x' + b.toString(16));
  131. var o = this.readLength(this._offset + 1); // stored in `length`
  132. if (o === null)
  133. return null;
  134. if (this.length > this._size - o)
  135. return null;
  136. this._offset = o;
  137. if (this.length === 0)
  138. return retbuf ? Buffer.alloc(0) : '';
  139. var str = this._buf.slice(this._offset, this._offset + this.length);
  140. this._offset += this.length;
  141. return retbuf ? str : str.toString('utf8');
  142. };
  143. Reader.prototype.readOID = function(tag) {
  144. if (!tag)
  145. tag = ASN1.OID;
  146. var b = this.readString(tag, true);
  147. if (b === null)
  148. return null;
  149. var values = [];
  150. var value = 0;
  151. for (var i = 0; i < b.length; i++) {
  152. var byte = b[i] & 0xff;
  153. value <<= 7;
  154. value += byte & 0x7f;
  155. if ((byte & 0x80) == 0) {
  156. values.push(value >>> 0);
  157. value = 0;
  158. }
  159. }
  160. value = values.shift();
  161. values.unshift(value % 40);
  162. values.unshift((value / 40) >> 0);
  163. return values.join('.');
  164. };
  165. Reader.prototype.readBitString = function(tag) {
  166. if (!tag)
  167. tag = ASN1.BitString;
  168. var b = this.peek();
  169. if (b === null)
  170. return null;
  171. if (b !== tag)
  172. throw InvalidAsn1Error('Expected 0x' + tag.toString(16) +
  173. ': got 0x' + b.toString(16));
  174. var o = this.readLength(this._offset + 1);
  175. if (o === null)
  176. return null;
  177. if (this.length > this._size - o)
  178. return null;
  179. this._offset = o;
  180. if (this.length === 0)
  181. return '';
  182. var ignoredBits = this._buf[this._offset++];
  183. var bitStringOctets = this._buf.slice(this._offset, this._offset + this.length - 1);
  184. var bitString = (parseInt(bitStringOctets.toString('hex'), 16).toString(2)).padStart(bitStringOctets.length * 8, '0');
  185. this._offset += this.length - 1;
  186. return bitString.substring(0, bitString.length - ignoredBits);
  187. };
  188. Reader.prototype._readTag = function(tag) {
  189. // assert.ok(tag !== undefined);
  190. var b = this.peek();
  191. if (b === null)
  192. return null;
  193. if (tag !== undefined && b !== tag)
  194. throw InvalidAsn1Error('Expected 0x' + tag.toString(16) +
  195. ': got 0x' + b.toString(16));
  196. var o = this.readLength(this._offset + 1); // stored in `length`
  197. if (o === null)
  198. return null;
  199. if (this.length === 0)
  200. throw InvalidAsn1Error('Zero-length integer');
  201. if (this.length > this._size - o)
  202. return null;
  203. this._offset = o;
  204. var value = this._buf.readInt8(this._offset++);
  205. for (var i = 1; i < this.length; i++) {
  206. value *= 256;
  207. value += this._buf[this._offset++];
  208. }
  209. if ( ! Number.isSafeInteger(value) )
  210. throw InvalidAsn1Error('Integer not representable as javascript number');
  211. return value;
  212. };
  213. ///--- Exported API
  214. module.exports = Reader;