reader.js 5.4 KB

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