reader.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
  2. var assert = require('assert');
  3. var ASN1 = require('./types');
  4. var errors = require('./errors');
  5. ///--- Globals
  6. var newInvalidAsn1Error = errors.newInvalidAsn1Error;
  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 newInvalidAsn1Error('Indefinite length not supported');
  72. if (lenB > 4)
  73. throw newInvalidAsn1Error('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 newInvalidAsn1Error('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() {
  106. return this._readTag(ASN1.Integer);
  107. };
  108. Reader.prototype.readBoolean = function() {
  109. return (this._readTag(ASN1.Boolean) === 0 ? false : true);
  110. };
  111. Reader.prototype.readEnumeration = function() {
  112. return this._readTag(ASN1.Enumeration);
  113. };
  114. Reader.prototype.readString = function(tag, retbuf) {
  115. if (!tag)
  116. tag = ASN1.OctetString;
  117. var b = this.peek();
  118. if (b === null)
  119. return null;
  120. if (b !== tag)
  121. throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +
  122. ': got 0x' + b.toString(16));
  123. var o = this.readLength(this._offset + 1); // stored in `length`
  124. if (o === null)
  125. return null;
  126. if (this.length > this._size - o)
  127. return null;
  128. this._offset = o;
  129. if (this.length === 0)
  130. return retbuf ? new Buffer(0) : '';
  131. var str = this._buf.slice(this._offset, this._offset + this.length);
  132. this._offset += this.length;
  133. return retbuf ? str : str.toString('utf8');
  134. };
  135. Reader.prototype.readOID = function(tag) {
  136. if (!tag)
  137. tag = ASN1.OID;
  138. var b = this.readString(tag, true);
  139. if (b === null)
  140. return null;
  141. var values = [];
  142. var value = 0;
  143. for (var i = 0; i < b.length; i++) {
  144. var byte = b[i] & 0xff;
  145. value <<= 7;
  146. value += byte & 0x7f;
  147. if ((byte & 0x80) == 0) {
  148. values.push(value);
  149. value = 0;
  150. }
  151. }
  152. value = values.shift();
  153. values.unshift(value % 40);
  154. values.unshift((value / 40) >> 0);
  155. return values.join('.');
  156. };
  157. Reader.prototype._readTag = function(tag) {
  158. assert.ok(tag !== undefined);
  159. var b = this.peek();
  160. if (b === null)
  161. return null;
  162. if (b !== tag)
  163. throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +
  164. ': got 0x' + b.toString(16));
  165. var o = this.readLength(this._offset + 1); // stored in `length`
  166. if (o === null)
  167. return null;
  168. if (this.length > 4)
  169. throw newInvalidAsn1Error('Integer too long: ' + this.length);
  170. if (this.length > this._size - o)
  171. return null;
  172. this._offset = o;
  173. var fb = this._buf[this._offset];
  174. var value = 0;
  175. for (var i = 0; i < this.length; i++) {
  176. value <<= 8;
  177. value |= (this._buf[this._offset++] & 0xff);
  178. }
  179. if ((fb & 0x80) == 0x80 && i !== 4)
  180. value -= (1 << (i * 8));
  181. return value >> 0;
  182. };
  183. ///--- Exported API
  184. module.exports = Reader;