reader.js 5.5 KB

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