Path: blob/master/node_modules/asn1/lib/ber/reader.js
2593 views
// Copyright 2011 Mark Cavage <[email protected]> All rights reserved.12var assert = require('assert');3var Buffer = require('safer-buffer').Buffer;45var ASN1 = require('./types');6var errors = require('./errors');789// --- Globals1011var newInvalidAsn1Error = errors.newInvalidAsn1Error;12131415// --- API1617function Reader(data) {18if (!data || !Buffer.isBuffer(data))19throw new TypeError('data must be a node Buffer');2021this._buf = data;22this._size = data.length;2324// These hold the "current" state25this._len = 0;26this._offset = 0;27}2829Object.defineProperty(Reader.prototype, 'length', {30enumerable: true,31get: function () { return (this._len); }32});3334Object.defineProperty(Reader.prototype, 'offset', {35enumerable: true,36get: function () { return (this._offset); }37});3839Object.defineProperty(Reader.prototype, 'remain', {40get: function () { return (this._size - this._offset); }41});4243Object.defineProperty(Reader.prototype, 'buffer', {44get: function () { return (this._buf.slice(this._offset)); }45});464748/**49* Reads a single byte and advances offset; you can pass in `true` to make this50* a "peek" operation (i.e., get the byte, but don't advance the offset).51*52* @param {Boolean} peek true means don't move offset.53* @return {Number} the next byte, null if not enough data.54*/55Reader.prototype.readByte = function (peek) {56if (this._size - this._offset < 1)57return null;5859var b = this._buf[this._offset] & 0xff;6061if (!peek)62this._offset += 1;6364return b;65};666768Reader.prototype.peek = function () {69return this.readByte(true);70};717273/**74* Reads a (potentially) variable length off the BER buffer. This call is75* not really meant to be called directly, as callers have to manipulate76* the internal buffer afterwards.77*78* As a result of this call, you can call `Reader.length`, until the79* next thing called that does a readLength.80*81* @return {Number} the amount of offset to advance the buffer.82* @throws {InvalidAsn1Error} on bad ASN.183*/84Reader.prototype.readLength = function (offset) {85if (offset === undefined)86offset = this._offset;8788if (offset >= this._size)89return null;9091var lenB = this._buf[offset++] & 0xff;92if (lenB === null)93return null;9495if ((lenB & 0x80) === 0x80) {96lenB &= 0x7f;9798if (lenB === 0)99throw newInvalidAsn1Error('Indefinite length not supported');100101if (lenB > 4)102throw newInvalidAsn1Error('encoding too long');103104if (this._size - offset < lenB)105return null;106107this._len = 0;108for (var i = 0; i < lenB; i++)109this._len = (this._len << 8) + (this._buf[offset++] & 0xff);110111} else {112// Wasn't a variable length113this._len = lenB;114}115116return offset;117};118119120/**121* Parses the next sequence in this BER buffer.122*123* To get the length of the sequence, call `Reader.length`.124*125* @return {Number} the sequence's tag.126*/127Reader.prototype.readSequence = function (tag) {128var seq = this.peek();129if (seq === null)130return null;131if (tag !== undefined && tag !== seq)132throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +133': got 0x' + seq.toString(16));134135var o = this.readLength(this._offset + 1); // stored in `length`136if (o === null)137return null;138139this._offset = o;140return seq;141};142143144Reader.prototype.readInt = function () {145return this._readTag(ASN1.Integer);146};147148149Reader.prototype.readBoolean = function () {150return (this._readTag(ASN1.Boolean) === 0 ? false : true);151};152153154Reader.prototype.readEnumeration = function () {155return this._readTag(ASN1.Enumeration);156};157158159Reader.prototype.readString = function (tag, retbuf) {160if (!tag)161tag = ASN1.OctetString;162163var b = this.peek();164if (b === null)165return null;166167if (b !== tag)168throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +169': got 0x' + b.toString(16));170171var o = this.readLength(this._offset + 1); // stored in `length`172173if (o === null)174return null;175176if (this.length > this._size - o)177return null;178179this._offset = o;180181if (this.length === 0)182return retbuf ? Buffer.alloc(0) : '';183184var str = this._buf.slice(this._offset, this._offset + this.length);185this._offset += this.length;186187return retbuf ? str : str.toString('utf8');188};189190Reader.prototype.readOID = function (tag) {191if (!tag)192tag = ASN1.OID;193194var b = this.readString(tag, true);195if (b === null)196return null;197198var values = [];199var value = 0;200201for (var i = 0; i < b.length; i++) {202var byte = b[i] & 0xff;203204value <<= 7;205value += byte & 0x7f;206if ((byte & 0x80) === 0) {207values.push(value);208value = 0;209}210}211212value = values.shift();213values.unshift(value % 40);214values.unshift((value / 40) >> 0);215216return values.join('.');217};218219220Reader.prototype._readTag = function (tag) {221assert.ok(tag !== undefined);222223var b = this.peek();224225if (b === null)226return null;227228if (b !== tag)229throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +230': got 0x' + b.toString(16));231232var o = this.readLength(this._offset + 1); // stored in `length`233if (o === null)234return null;235236if (this.length > 4)237throw newInvalidAsn1Error('Integer too long: ' + this.length);238239if (this.length > this._size - o)240return null;241this._offset = o;242243var fb = this._buf[this._offset];244var value = 0;245246for (var i = 0; i < this.length; i++) {247value <<= 8;248value |= (this._buf[this._offset++] & 0xff);249}250251if ((fb & 0x80) === 0x80 && i !== 4)252value -= (1 << (i * 8));253254return value >> 0;255};256257258259// --- Exported API260261module.exports = Reader;262263264