reader.js 5.6 KB

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