v5
This commit is contained in:
		
							
								
								
									
										13
									
								
								node_modules/asn1/lib/ber/errors.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								node_modules/asn1/lib/ber/errors.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
 | 
			
		||||
  newInvalidAsn1Error: function (msg) {
 | 
			
		||||
    var e = new Error();
 | 
			
		||||
    e.name = 'InvalidAsn1Error';
 | 
			
		||||
    e.message = msg || '';
 | 
			
		||||
    return e;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										27
									
								
								node_modules/asn1/lib/ber/index.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								node_modules/asn1/lib/ber/index.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
			
		||||
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
 | 
			
		||||
 | 
			
		||||
var errors = require('./errors');
 | 
			
		||||
var types = require('./types');
 | 
			
		||||
 | 
			
		||||
var Reader = require('./reader');
 | 
			
		||||
var Writer = require('./writer');
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// --- Exports
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
 | 
			
		||||
  Reader: Reader,
 | 
			
		||||
 | 
			
		||||
  Writer: Writer
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
for (var t in types) {
 | 
			
		||||
  if (types.hasOwnProperty(t))
 | 
			
		||||
    module.exports[t] = types[t];
 | 
			
		||||
}
 | 
			
		||||
for (var e in errors) {
 | 
			
		||||
  if (errors.hasOwnProperty(e))
 | 
			
		||||
    module.exports[e] = errors[e];
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										262
									
								
								node_modules/asn1/lib/ber/reader.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										262
									
								
								node_modules/asn1/lib/ber/reader.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,262 @@
 | 
			
		||||
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
 | 
			
		||||
 | 
			
		||||
var assert = require('assert');
 | 
			
		||||
var Buffer = require('safer-buffer').Buffer;
 | 
			
		||||
 | 
			
		||||
var ASN1 = require('./types');
 | 
			
		||||
var errors = require('./errors');
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// --- Globals
 | 
			
		||||
 | 
			
		||||
var newInvalidAsn1Error = errors.newInvalidAsn1Error;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// --- API
 | 
			
		||||
 | 
			
		||||
function Reader(data) {
 | 
			
		||||
  if (!data || !Buffer.isBuffer(data))
 | 
			
		||||
    throw new TypeError('data must be a node Buffer');
 | 
			
		||||
 | 
			
		||||
  this._buf = data;
 | 
			
		||||
  this._size = data.length;
 | 
			
		||||
 | 
			
		||||
  // These hold the "current" state
 | 
			
		||||
  this._len = 0;
 | 
			
		||||
  this._offset = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Object.defineProperty(Reader.prototype, 'length', {
 | 
			
		||||
  enumerable: true,
 | 
			
		||||
  get: function () { return (this._len); }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
Object.defineProperty(Reader.prototype, 'offset', {
 | 
			
		||||
  enumerable: true,
 | 
			
		||||
  get: function () { return (this._offset); }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
Object.defineProperty(Reader.prototype, 'remain', {
 | 
			
		||||
  get: function () { return (this._size - this._offset); }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
Object.defineProperty(Reader.prototype, 'buffer', {
 | 
			
		||||
  get: function () { return (this._buf.slice(this._offset)); }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Reads a single byte and advances offset; you can pass in `true` to make this
 | 
			
		||||
 * a "peek" operation (i.e., get the byte, but don't advance the offset).
 | 
			
		||||
 *
 | 
			
		||||
 * @param {Boolean} peek true means don't move offset.
 | 
			
		||||
 * @return {Number} the next byte, null if not enough data.
 | 
			
		||||
 */
 | 
			
		||||
Reader.prototype.readByte = function (peek) {
 | 
			
		||||
  if (this._size - this._offset < 1)
 | 
			
		||||
    return null;
 | 
			
		||||
 | 
			
		||||
  var b = this._buf[this._offset] & 0xff;
 | 
			
		||||
 | 
			
		||||
  if (!peek)
 | 
			
		||||
    this._offset += 1;
 | 
			
		||||
 | 
			
		||||
  return b;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Reader.prototype.peek = function () {
 | 
			
		||||
  return this.readByte(true);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Reads a (potentially) variable length off the BER buffer.  This call is
 | 
			
		||||
 * not really meant to be called directly, as callers have to manipulate
 | 
			
		||||
 * the internal buffer afterwards.
 | 
			
		||||
 *
 | 
			
		||||
 * As a result of this call, you can call `Reader.length`, until the
 | 
			
		||||
 * next thing called that does a readLength.
 | 
			
		||||
 *
 | 
			
		||||
 * @return {Number} the amount of offset to advance the buffer.
 | 
			
		||||
 * @throws {InvalidAsn1Error} on bad ASN.1
 | 
			
		||||
 */
 | 
			
		||||
Reader.prototype.readLength = function (offset) {
 | 
			
		||||
  if (offset === undefined)
 | 
			
		||||
    offset = this._offset;
 | 
			
		||||
 | 
			
		||||
  if (offset >= this._size)
 | 
			
		||||
    return null;
 | 
			
		||||
 | 
			
		||||
  var lenB = this._buf[offset++] & 0xff;
 | 
			
		||||
  if (lenB === null)
 | 
			
		||||
    return null;
 | 
			
		||||
 | 
			
		||||
  if ((lenB & 0x80) === 0x80) {
 | 
			
		||||
    lenB &= 0x7f;
 | 
			
		||||
 | 
			
		||||
    if (lenB === 0)
 | 
			
		||||
      throw newInvalidAsn1Error('Indefinite length not supported');
 | 
			
		||||
 | 
			
		||||
    if (lenB > 4)
 | 
			
		||||
      throw newInvalidAsn1Error('encoding too long');
 | 
			
		||||
 | 
			
		||||
    if (this._size - offset < lenB)
 | 
			
		||||
      return null;
 | 
			
		||||
 | 
			
		||||
    this._len = 0;
 | 
			
		||||
    for (var i = 0; i < lenB; i++)
 | 
			
		||||
      this._len = (this._len << 8) + (this._buf[offset++] & 0xff);
 | 
			
		||||
 | 
			
		||||
  } else {
 | 
			
		||||
    // Wasn't a variable length
 | 
			
		||||
    this._len = lenB;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return offset;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Parses the next sequence in this BER buffer.
 | 
			
		||||
 *
 | 
			
		||||
 * To get the length of the sequence, call `Reader.length`.
 | 
			
		||||
 *
 | 
			
		||||
 * @return {Number} the sequence's tag.
 | 
			
		||||
 */
 | 
			
		||||
Reader.prototype.readSequence = function (tag) {
 | 
			
		||||
  var seq = this.peek();
 | 
			
		||||
  if (seq === null)
 | 
			
		||||
    return null;
 | 
			
		||||
  if (tag !== undefined && tag !== seq)
 | 
			
		||||
    throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +
 | 
			
		||||
                              ': got 0x' + seq.toString(16));
 | 
			
		||||
 | 
			
		||||
  var o = this.readLength(this._offset + 1); // stored in `length`
 | 
			
		||||
  if (o === null)
 | 
			
		||||
    return null;
 | 
			
		||||
 | 
			
		||||
  this._offset = o;
 | 
			
		||||
  return seq;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Reader.prototype.readInt = function () {
 | 
			
		||||
  return this._readTag(ASN1.Integer);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Reader.prototype.readBoolean = function () {
 | 
			
		||||
  return (this._readTag(ASN1.Boolean) === 0 ? false : true);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Reader.prototype.readEnumeration = function () {
 | 
			
		||||
  return this._readTag(ASN1.Enumeration);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Reader.prototype.readString = function (tag, retbuf) {
 | 
			
		||||
  if (!tag)
 | 
			
		||||
    tag = ASN1.OctetString;
 | 
			
		||||
 | 
			
		||||
  var b = this.peek();
 | 
			
		||||
  if (b === null)
 | 
			
		||||
    return null;
 | 
			
		||||
 | 
			
		||||
  if (b !== tag)
 | 
			
		||||
    throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +
 | 
			
		||||
                              ': got 0x' + b.toString(16));
 | 
			
		||||
 | 
			
		||||
  var o = this.readLength(this._offset + 1); // stored in `length`
 | 
			
		||||
 | 
			
		||||
  if (o === null)
 | 
			
		||||
    return null;
 | 
			
		||||
 | 
			
		||||
  if (this.length > this._size - o)
 | 
			
		||||
    return null;
 | 
			
		||||
 | 
			
		||||
  this._offset = o;
 | 
			
		||||
 | 
			
		||||
  if (this.length === 0)
 | 
			
		||||
    return retbuf ? Buffer.alloc(0) : '';
 | 
			
		||||
 | 
			
		||||
  var str = this._buf.slice(this._offset, this._offset + this.length);
 | 
			
		||||
  this._offset += this.length;
 | 
			
		||||
 | 
			
		||||
  return retbuf ? str : str.toString('utf8');
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
Reader.prototype.readOID = function (tag) {
 | 
			
		||||
  if (!tag)
 | 
			
		||||
    tag = ASN1.OID;
 | 
			
		||||
 | 
			
		||||
  var b = this.readString(tag, true);
 | 
			
		||||
  if (b === null)
 | 
			
		||||
    return null;
 | 
			
		||||
 | 
			
		||||
  var values = [];
 | 
			
		||||
  var value = 0;
 | 
			
		||||
 | 
			
		||||
  for (var i = 0; i < b.length; i++) {
 | 
			
		||||
    var byte = b[i] & 0xff;
 | 
			
		||||
 | 
			
		||||
    value <<= 7;
 | 
			
		||||
    value += byte & 0x7f;
 | 
			
		||||
    if ((byte & 0x80) === 0) {
 | 
			
		||||
      values.push(value);
 | 
			
		||||
      value = 0;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  value = values.shift();
 | 
			
		||||
  values.unshift(value % 40);
 | 
			
		||||
  values.unshift((value / 40) >> 0);
 | 
			
		||||
 | 
			
		||||
  return values.join('.');
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Reader.prototype._readTag = function (tag) {
 | 
			
		||||
  assert.ok(tag !== undefined);
 | 
			
		||||
 | 
			
		||||
  var b = this.peek();
 | 
			
		||||
 | 
			
		||||
  if (b === null)
 | 
			
		||||
    return null;
 | 
			
		||||
 | 
			
		||||
  if (b !== tag)
 | 
			
		||||
    throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +
 | 
			
		||||
                              ': got 0x' + b.toString(16));
 | 
			
		||||
 | 
			
		||||
  var o = this.readLength(this._offset + 1); // stored in `length`
 | 
			
		||||
  if (o === null)
 | 
			
		||||
    return null;
 | 
			
		||||
 | 
			
		||||
  if (this.length > 4)
 | 
			
		||||
    throw newInvalidAsn1Error('Integer too long: ' + this.length);
 | 
			
		||||
 | 
			
		||||
  if (this.length > this._size - o)
 | 
			
		||||
    return null;
 | 
			
		||||
  this._offset = o;
 | 
			
		||||
 | 
			
		||||
  var fb = this._buf[this._offset];
 | 
			
		||||
  var value = 0;
 | 
			
		||||
 | 
			
		||||
  for (var i = 0; i < this.length; i++) {
 | 
			
		||||
    value <<= 8;
 | 
			
		||||
    value |= (this._buf[this._offset++] & 0xff);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if ((fb & 0x80) === 0x80 && i !== 4)
 | 
			
		||||
    value -= (1 << (i * 8));
 | 
			
		||||
 | 
			
		||||
  return value >> 0;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// --- Exported API
 | 
			
		||||
 | 
			
		||||
module.exports = Reader;
 | 
			
		||||
							
								
								
									
										36
									
								
								node_modules/asn1/lib/ber/types.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								node_modules/asn1/lib/ber/types.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
			
		||||
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
  EOC: 0,
 | 
			
		||||
  Boolean: 1,
 | 
			
		||||
  Integer: 2,
 | 
			
		||||
  BitString: 3,
 | 
			
		||||
  OctetString: 4,
 | 
			
		||||
  Null: 5,
 | 
			
		||||
  OID: 6,
 | 
			
		||||
  ObjectDescriptor: 7,
 | 
			
		||||
  External: 8,
 | 
			
		||||
  Real: 9, // float
 | 
			
		||||
  Enumeration: 10,
 | 
			
		||||
  PDV: 11,
 | 
			
		||||
  Utf8String: 12,
 | 
			
		||||
  RelativeOID: 13,
 | 
			
		||||
  Sequence: 16,
 | 
			
		||||
  Set: 17,
 | 
			
		||||
  NumericString: 18,
 | 
			
		||||
  PrintableString: 19,
 | 
			
		||||
  T61String: 20,
 | 
			
		||||
  VideotexString: 21,
 | 
			
		||||
  IA5String: 22,
 | 
			
		||||
  UTCTime: 23,
 | 
			
		||||
  GeneralizedTime: 24,
 | 
			
		||||
  GraphicString: 25,
 | 
			
		||||
  VisibleString: 26,
 | 
			
		||||
  GeneralString: 28,
 | 
			
		||||
  UniversalString: 29,
 | 
			
		||||
  CharacterString: 30,
 | 
			
		||||
  BMPString: 31,
 | 
			
		||||
  Constructor: 32,
 | 
			
		||||
  Context: 128
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										317
									
								
								node_modules/asn1/lib/ber/writer.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										317
									
								
								node_modules/asn1/lib/ber/writer.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,317 @@
 | 
			
		||||
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
 | 
			
		||||
 | 
			
		||||
var assert = require('assert');
 | 
			
		||||
var Buffer = require('safer-buffer').Buffer;
 | 
			
		||||
var ASN1 = require('./types');
 | 
			
		||||
var errors = require('./errors');
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// --- Globals
 | 
			
		||||
 | 
			
		||||
var newInvalidAsn1Error = errors.newInvalidAsn1Error;
 | 
			
		||||
 | 
			
		||||
var DEFAULT_OPTS = {
 | 
			
		||||
  size: 1024,
 | 
			
		||||
  growthFactor: 8
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// --- Helpers
 | 
			
		||||
 | 
			
		||||
function merge(from, to) {
 | 
			
		||||
  assert.ok(from);
 | 
			
		||||
  assert.equal(typeof (from), 'object');
 | 
			
		||||
  assert.ok(to);
 | 
			
		||||
  assert.equal(typeof (to), 'object');
 | 
			
		||||
 | 
			
		||||
  var keys = Object.getOwnPropertyNames(from);
 | 
			
		||||
  keys.forEach(function (key) {
 | 
			
		||||
    if (to[key])
 | 
			
		||||
      return;
 | 
			
		||||
 | 
			
		||||
    var value = Object.getOwnPropertyDescriptor(from, key);
 | 
			
		||||
    Object.defineProperty(to, key, value);
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  return to;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// --- API
 | 
			
		||||
 | 
			
		||||
function Writer(options) {
 | 
			
		||||
  options = merge(DEFAULT_OPTS, options || {});
 | 
			
		||||
 | 
			
		||||
  this._buf = Buffer.alloc(options.size || 1024);
 | 
			
		||||
  this._size = this._buf.length;
 | 
			
		||||
  this._offset = 0;
 | 
			
		||||
  this._options = options;
 | 
			
		||||
 | 
			
		||||
  // A list of offsets in the buffer where we need to insert
 | 
			
		||||
  // sequence tag/len pairs.
 | 
			
		||||
  this._seq = [];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Object.defineProperty(Writer.prototype, 'buffer', {
 | 
			
		||||
  get: function () {
 | 
			
		||||
    if (this._seq.length)
 | 
			
		||||
      throw newInvalidAsn1Error(this._seq.length + ' unended sequence(s)');
 | 
			
		||||
 | 
			
		||||
    return (this._buf.slice(0, this._offset));
 | 
			
		||||
  }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
Writer.prototype.writeByte = function (b) {
 | 
			
		||||
  if (typeof (b) !== 'number')
 | 
			
		||||
    throw new TypeError('argument must be a Number');
 | 
			
		||||
 | 
			
		||||
  this._ensure(1);
 | 
			
		||||
  this._buf[this._offset++] = b;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Writer.prototype.writeInt = function (i, tag) {
 | 
			
		||||
  if (typeof (i) !== 'number')
 | 
			
		||||
    throw new TypeError('argument must be a Number');
 | 
			
		||||
  if (typeof (tag) !== 'number')
 | 
			
		||||
    tag = ASN1.Integer;
 | 
			
		||||
 | 
			
		||||
  var sz = 4;
 | 
			
		||||
 | 
			
		||||
  while ((((i & 0xff800000) === 0) || ((i & 0xff800000) === 0xff800000 >> 0)) &&
 | 
			
		||||
        (sz > 1)) {
 | 
			
		||||
    sz--;
 | 
			
		||||
    i <<= 8;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (sz > 4)
 | 
			
		||||
    throw newInvalidAsn1Error('BER ints cannot be > 0xffffffff');
 | 
			
		||||
 | 
			
		||||
  this._ensure(2 + sz);
 | 
			
		||||
  this._buf[this._offset++] = tag;
 | 
			
		||||
  this._buf[this._offset++] = sz;
 | 
			
		||||
 | 
			
		||||
  while (sz-- > 0) {
 | 
			
		||||
    this._buf[this._offset++] = ((i & 0xff000000) >>> 24);
 | 
			
		||||
    i <<= 8;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Writer.prototype.writeNull = function () {
 | 
			
		||||
  this.writeByte(ASN1.Null);
 | 
			
		||||
  this.writeByte(0x00);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Writer.prototype.writeEnumeration = function (i, tag) {
 | 
			
		||||
  if (typeof (i) !== 'number')
 | 
			
		||||
    throw new TypeError('argument must be a Number');
 | 
			
		||||
  if (typeof (tag) !== 'number')
 | 
			
		||||
    tag = ASN1.Enumeration;
 | 
			
		||||
 | 
			
		||||
  return this.writeInt(i, tag);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Writer.prototype.writeBoolean = function (b, tag) {
 | 
			
		||||
  if (typeof (b) !== 'boolean')
 | 
			
		||||
    throw new TypeError('argument must be a Boolean');
 | 
			
		||||
  if (typeof (tag) !== 'number')
 | 
			
		||||
    tag = ASN1.Boolean;
 | 
			
		||||
 | 
			
		||||
  this._ensure(3);
 | 
			
		||||
  this._buf[this._offset++] = tag;
 | 
			
		||||
  this._buf[this._offset++] = 0x01;
 | 
			
		||||
  this._buf[this._offset++] = b ? 0xff : 0x00;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Writer.prototype.writeString = function (s, tag) {
 | 
			
		||||
  if (typeof (s) !== 'string')
 | 
			
		||||
    throw new TypeError('argument must be a string (was: ' + typeof (s) + ')');
 | 
			
		||||
  if (typeof (tag) !== 'number')
 | 
			
		||||
    tag = ASN1.OctetString;
 | 
			
		||||
 | 
			
		||||
  var len = Buffer.byteLength(s);
 | 
			
		||||
  this.writeByte(tag);
 | 
			
		||||
  this.writeLength(len);
 | 
			
		||||
  if (len) {
 | 
			
		||||
    this._ensure(len);
 | 
			
		||||
    this._buf.write(s, this._offset);
 | 
			
		||||
    this._offset += len;
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Writer.prototype.writeBuffer = function (buf, tag) {
 | 
			
		||||
  if (typeof (tag) !== 'number')
 | 
			
		||||
    throw new TypeError('tag must be a number');
 | 
			
		||||
  if (!Buffer.isBuffer(buf))
 | 
			
		||||
    throw new TypeError('argument must be a buffer');
 | 
			
		||||
 | 
			
		||||
  this.writeByte(tag);
 | 
			
		||||
  this.writeLength(buf.length);
 | 
			
		||||
  this._ensure(buf.length);
 | 
			
		||||
  buf.copy(this._buf, this._offset, 0, buf.length);
 | 
			
		||||
  this._offset += buf.length;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Writer.prototype.writeStringArray = function (strings) {
 | 
			
		||||
  if ((!strings instanceof Array))
 | 
			
		||||
    throw new TypeError('argument must be an Array[String]');
 | 
			
		||||
 | 
			
		||||
  var self = this;
 | 
			
		||||
  strings.forEach(function (s) {
 | 
			
		||||
    self.writeString(s);
 | 
			
		||||
  });
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// This is really to solve DER cases, but whatever for now
 | 
			
		||||
Writer.prototype.writeOID = function (s, tag) {
 | 
			
		||||
  if (typeof (s) !== 'string')
 | 
			
		||||
    throw new TypeError('argument must be a string');
 | 
			
		||||
  if (typeof (tag) !== 'number')
 | 
			
		||||
    tag = ASN1.OID;
 | 
			
		||||
 | 
			
		||||
  if (!/^([0-9]+\.){3,}[0-9]+$/.test(s))
 | 
			
		||||
    throw new Error('argument is not a valid OID string');
 | 
			
		||||
 | 
			
		||||
  function encodeOctet(bytes, octet) {
 | 
			
		||||
    if (octet < 128) {
 | 
			
		||||
        bytes.push(octet);
 | 
			
		||||
    } else if (octet < 16384) {
 | 
			
		||||
        bytes.push((octet >>> 7) | 0x80);
 | 
			
		||||
        bytes.push(octet & 0x7F);
 | 
			
		||||
    } else if (octet < 2097152) {
 | 
			
		||||
      bytes.push((octet >>> 14) | 0x80);
 | 
			
		||||
      bytes.push(((octet >>> 7) | 0x80) & 0xFF);
 | 
			
		||||
      bytes.push(octet & 0x7F);
 | 
			
		||||
    } else if (octet < 268435456) {
 | 
			
		||||
      bytes.push((octet >>> 21) | 0x80);
 | 
			
		||||
      bytes.push(((octet >>> 14) | 0x80) & 0xFF);
 | 
			
		||||
      bytes.push(((octet >>> 7) | 0x80) & 0xFF);
 | 
			
		||||
      bytes.push(octet & 0x7F);
 | 
			
		||||
    } else {
 | 
			
		||||
      bytes.push(((octet >>> 28) | 0x80) & 0xFF);
 | 
			
		||||
      bytes.push(((octet >>> 21) | 0x80) & 0xFF);
 | 
			
		||||
      bytes.push(((octet >>> 14) | 0x80) & 0xFF);
 | 
			
		||||
      bytes.push(((octet >>> 7) | 0x80) & 0xFF);
 | 
			
		||||
      bytes.push(octet & 0x7F);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  var tmp = s.split('.');
 | 
			
		||||
  var bytes = [];
 | 
			
		||||
  bytes.push(parseInt(tmp[0], 10) * 40 + parseInt(tmp[1], 10));
 | 
			
		||||
  tmp.slice(2).forEach(function (b) {
 | 
			
		||||
    encodeOctet(bytes, parseInt(b, 10));
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  var self = this;
 | 
			
		||||
  this._ensure(2 + bytes.length);
 | 
			
		||||
  this.writeByte(tag);
 | 
			
		||||
  this.writeLength(bytes.length);
 | 
			
		||||
  bytes.forEach(function (b) {
 | 
			
		||||
    self.writeByte(b);
 | 
			
		||||
  });
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Writer.prototype.writeLength = function (len) {
 | 
			
		||||
  if (typeof (len) !== 'number')
 | 
			
		||||
    throw new TypeError('argument must be a Number');
 | 
			
		||||
 | 
			
		||||
  this._ensure(4);
 | 
			
		||||
 | 
			
		||||
  if (len <= 0x7f) {
 | 
			
		||||
    this._buf[this._offset++] = len;
 | 
			
		||||
  } else if (len <= 0xff) {
 | 
			
		||||
    this._buf[this._offset++] = 0x81;
 | 
			
		||||
    this._buf[this._offset++] = len;
 | 
			
		||||
  } else if (len <= 0xffff) {
 | 
			
		||||
    this._buf[this._offset++] = 0x82;
 | 
			
		||||
    this._buf[this._offset++] = len >> 8;
 | 
			
		||||
    this._buf[this._offset++] = len;
 | 
			
		||||
  } else if (len <= 0xffffff) {
 | 
			
		||||
    this._buf[this._offset++] = 0x83;
 | 
			
		||||
    this._buf[this._offset++] = len >> 16;
 | 
			
		||||
    this._buf[this._offset++] = len >> 8;
 | 
			
		||||
    this._buf[this._offset++] = len;
 | 
			
		||||
  } else {
 | 
			
		||||
    throw newInvalidAsn1Error('Length too long (> 4 bytes)');
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
Writer.prototype.startSequence = function (tag) {
 | 
			
		||||
  if (typeof (tag) !== 'number')
 | 
			
		||||
    tag = ASN1.Sequence | ASN1.Constructor;
 | 
			
		||||
 | 
			
		||||
  this.writeByte(tag);
 | 
			
		||||
  this._seq.push(this._offset);
 | 
			
		||||
  this._ensure(3);
 | 
			
		||||
  this._offset += 3;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Writer.prototype.endSequence = function () {
 | 
			
		||||
  var seq = this._seq.pop();
 | 
			
		||||
  var start = seq + 3;
 | 
			
		||||
  var len = this._offset - start;
 | 
			
		||||
 | 
			
		||||
  if (len <= 0x7f) {
 | 
			
		||||
    this._shift(start, len, -2);
 | 
			
		||||
    this._buf[seq] = len;
 | 
			
		||||
  } else if (len <= 0xff) {
 | 
			
		||||
    this._shift(start, len, -1);
 | 
			
		||||
    this._buf[seq] = 0x81;
 | 
			
		||||
    this._buf[seq + 1] = len;
 | 
			
		||||
  } else if (len <= 0xffff) {
 | 
			
		||||
    this._buf[seq] = 0x82;
 | 
			
		||||
    this._buf[seq + 1] = len >> 8;
 | 
			
		||||
    this._buf[seq + 2] = len;
 | 
			
		||||
  } else if (len <= 0xffffff) {
 | 
			
		||||
    this._shift(start, len, 1);
 | 
			
		||||
    this._buf[seq] = 0x83;
 | 
			
		||||
    this._buf[seq + 1] = len >> 16;
 | 
			
		||||
    this._buf[seq + 2] = len >> 8;
 | 
			
		||||
    this._buf[seq + 3] = len;
 | 
			
		||||
  } else {
 | 
			
		||||
    throw newInvalidAsn1Error('Sequence too long');
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Writer.prototype._shift = function (start, len, shift) {
 | 
			
		||||
  assert.ok(start !== undefined);
 | 
			
		||||
  assert.ok(len !== undefined);
 | 
			
		||||
  assert.ok(shift);
 | 
			
		||||
 | 
			
		||||
  this._buf.copy(this._buf, start + shift, start, start + len);
 | 
			
		||||
  this._offset += shift;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
Writer.prototype._ensure = function (len) {
 | 
			
		||||
  assert.ok(len);
 | 
			
		||||
 | 
			
		||||
  if (this._size - this._offset < len) {
 | 
			
		||||
    var sz = this._size * this._options.growthFactor;
 | 
			
		||||
    if (sz - this._offset < len)
 | 
			
		||||
      sz += len;
 | 
			
		||||
 | 
			
		||||
    var buf = Buffer.alloc(sz);
 | 
			
		||||
 | 
			
		||||
    this._buf.copy(buf, 0, 0, this._offset);
 | 
			
		||||
    this._buf = buf;
 | 
			
		||||
    this._size = sz;
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// --- Exported API
 | 
			
		||||
 | 
			
		||||
module.exports = Writer;
 | 
			
		||||
		Reference in New Issue
	
	Block a user