123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434 |
- var forge = require('./forge');
- require('./util');
- require('./oids');
- var asn1 = module.exports = forge.asn1 = forge.asn1 || {};
- asn1.Class = {
- UNIVERSAL: 0x00,
- APPLICATION: 0x40,
- CONTEXT_SPECIFIC: 0x80,
- PRIVATE: 0xC0
- };
- asn1.Type = {
- NONE: 0,
- BOOLEAN: 1,
- INTEGER: 2,
- BITSTRING: 3,
- OCTETSTRING: 4,
- NULL: 5,
- OID: 6,
- ODESC: 7,
- EXTERNAL: 8,
- REAL: 9,
- ENUMERATED: 10,
- EMBEDDED: 11,
- UTF8: 12,
- ROID: 13,
- SEQUENCE: 16,
- SET: 17,
- PRINTABLESTRING: 19,
- IA5STRING: 22,
- UTCTIME: 23,
- GENERALIZEDTIME: 24,
- BMPSTRING: 30
- };
- asn1.create = function(tagClass, type, constructed, value, options) {
-
-
- if(forge.util.isArray(value)) {
- var tmp = [];
- for(var i = 0; i < value.length; ++i) {
- if(value[i] !== undefined) {
- tmp.push(value[i]);
- }
- }
- value = tmp;
- }
- var obj = {
- tagClass: tagClass,
- type: type,
- constructed: constructed,
- composed: constructed || forge.util.isArray(value),
- value: value
- };
- if(options && 'bitStringContents' in options) {
-
- obj.bitStringContents = options.bitStringContents;
-
-
- obj.original = asn1.copy(obj);
- }
- return obj;
- };
- asn1.copy = function(obj, options) {
- var copy;
- if(forge.util.isArray(obj)) {
- copy = [];
- for(var i = 0; i < obj.length; ++i) {
- copy.push(asn1.copy(obj[i], options));
- }
- return copy;
- }
- if(typeof obj === 'string') {
-
- return obj;
- }
- copy = {
- tagClass: obj.tagClass,
- type: obj.type,
- constructed: obj.constructed,
- composed: obj.composed,
- value: asn1.copy(obj.value, options)
- };
- if(options && !options.excludeBitStringContents) {
-
- copy.bitStringContents = obj.bitStringContents;
- }
- return copy;
- };
- asn1.equals = function(obj1, obj2, options) {
- if(forge.util.isArray(obj1)) {
- if(!forge.util.isArray(obj2)) {
- return false;
- }
- if(obj1.length !== obj2.length) {
- return false;
- }
- for(var i = 0; i < obj1.length; ++i) {
- if(!asn1.equals(obj1[i], obj2[i])) {
- return false;
- }
- }
- return true;
- }
- if(typeof obj1 !== typeof obj2) {
- return false;
- }
- if(typeof obj1 === 'string') {
- return obj1 === obj2;
- }
- var equal = obj1.tagClass === obj2.tagClass &&
- obj1.type === obj2.type &&
- obj1.constructed === obj2.constructed &&
- obj1.composed === obj2.composed &&
- asn1.equals(obj1.value, obj2.value);
- if(options && options.includeBitStringContents) {
- equal = equal && (obj1.bitStringContents === obj2.bitStringContents);
- }
- return equal;
- };
- asn1.getBerValueLength = function(b) {
-
-
- var b2 = b.getByte();
- if(b2 === 0x80) {
- return undefined;
- }
-
- var length;
- var longForm = b2 & 0x80;
- if(!longForm) {
-
- length = b2;
- } else {
-
-
- length = b.getInt((b2 & 0x7F) << 3);
- }
- return length;
- };
- function _checkBufferLength(bytes, remaining, n) {
- if(n > remaining) {
- var error = new Error('Too few bytes to parse DER.');
- error.available = bytes.length();
- error.remaining = remaining;
- error.requested = n;
- throw error;
- }
- }
- var _getValueLength = function(bytes, remaining) {
-
-
-
- var b2 = bytes.getByte();
- remaining--;
- if(b2 === 0x80) {
- return undefined;
- }
-
- var length;
- var longForm = b2 & 0x80;
- if(!longForm) {
-
- length = b2;
- } else {
-
-
- var longFormBytes = b2 & 0x7F;
- _checkBufferLength(bytes, remaining, longFormBytes);
- length = bytes.getInt(longFormBytes << 3);
- }
-
- if(length < 0) {
- throw new Error('Negative length: ' + length);
- }
- return length;
- };
- asn1.fromDer = function(bytes, options) {
- if(options === undefined) {
- options = {
- strict: true,
- parseAllBytes: true,
- decodeBitStrings: true
- };
- }
- if(typeof options === 'boolean') {
- options = {
- strict: options,
- parseAllBytes: true,
- decodeBitStrings: true
- };
- }
- if(!('strict' in options)) {
- options.strict = true;
- }
- if(!('parseAllBytes' in options)) {
- options.parseAllBytes = true;
- }
- if(!('decodeBitStrings' in options)) {
- options.decodeBitStrings = true;
- }
-
- if(typeof bytes === 'string') {
- bytes = forge.util.createBuffer(bytes);
- }
- var byteCount = bytes.length();
- var value = _fromDer(bytes, bytes.length(), 0, options);
- if(options.parseAllBytes && bytes.length() !== 0) {
- var error = new Error('Unparsed DER bytes remain after ASN.1 parsing.');
- error.byteCount = byteCount;
- error.remaining = bytes.length();
- throw error;
- }
- return value;
- };
- function _fromDer(bytes, remaining, depth, options) {
-
- var start;
-
- _checkBufferLength(bytes, remaining, 2);
-
- var b1 = bytes.getByte();
-
- remaining--;
-
- var tagClass = (b1 & 0xC0);
-
- var type = b1 & 0x1F;
-
- start = bytes.length();
- var length = _getValueLength(bytes, remaining);
- remaining -= start - bytes.length();
-
- if(length !== undefined && length > remaining) {
- if(options.strict) {
- var error = new Error('Too few bytes to read ASN.1 value.');
- error.available = bytes.length();
- error.remaining = remaining;
- error.requested = length;
- throw error;
- }
-
- length = remaining;
- }
-
- var value;
-
- var bitStringContents;
-
- var constructed = ((b1 & 0x20) === 0x20);
- if(constructed) {
-
- value = [];
- if(length === undefined) {
-
- for(;;) {
- _checkBufferLength(bytes, remaining, 2);
- if(bytes.bytes(2) === String.fromCharCode(0, 0)) {
- bytes.getBytes(2);
- remaining -= 2;
- break;
- }
- start = bytes.length();
- value.push(_fromDer(bytes, remaining, depth + 1, options));
- remaining -= start - bytes.length();
- }
- } else {
-
- while(length > 0) {
- start = bytes.length();
- value.push(_fromDer(bytes, length, depth + 1, options));
- remaining -= start - bytes.length();
- length -= start - bytes.length();
- }
- }
- }
-
- if(value === undefined && tagClass === asn1.Class.UNIVERSAL &&
- type === asn1.Type.BITSTRING) {
- bitStringContents = bytes.bytes(length);
- }
-
-
-
- if(value === undefined && options.decodeBitStrings &&
- tagClass === asn1.Class.UNIVERSAL &&
-
-
- (type === asn1.Type.BITSTRING ) &&
- length > 1) {
-
- var savedRead = bytes.read;
- var savedRemaining = remaining;
- var unused = 0;
- if(type === asn1.Type.BITSTRING) {
-
- _checkBufferLength(bytes, remaining, 1);
- unused = bytes.getByte();
- remaining--;
- }
-
- if(unused === 0) {
- try {
-
-
- start = bytes.length();
- var subOptions = {
-
- strict: true,
- decodeBitStrings: true
- };
- var composed = _fromDer(bytes, remaining, depth + 1, subOptions);
- var used = start - bytes.length();
- remaining -= used;
- if(type == asn1.Type.BITSTRING) {
- used++;
- }
-
-
- var tc = composed.tagClass;
- if(used === length &&
- (tc === asn1.Class.UNIVERSAL || tc === asn1.Class.CONTEXT_SPECIFIC)) {
- value = [composed];
- }
- } catch(ex) {
- }
- }
- if(value === undefined) {
-
- bytes.read = savedRead;
- remaining = savedRemaining;
- }
- }
- if(value === undefined) {
-
-
- if(length === undefined) {
- if(options.strict) {
- throw new Error('Non-constructed ASN.1 object of indefinite length.');
- }
-
- length = remaining;
- }
- if(type === asn1.Type.BMPSTRING) {
- value = '';
- for(; length > 0; length -= 2) {
- _checkBufferLength(bytes, remaining, 2);
- value += String.fromCharCode(bytes.getInt16());
- remaining -= 2;
- }
- } else {
- value = bytes.getBytes(length);
- remaining -= length;
- }
- }
-
- var asn1Options = bitStringContents === undefined ? null : {
- bitStringContents: bitStringContents
- };
-
- return asn1.create(tagClass, type, constructed, value, asn1Options);
- }
- asn1.toDer = function(obj) {
- var bytes = forge.util.createBuffer();
-
- var b1 = obj.tagClass | obj.type;
-
- var value = forge.util.createBuffer();
-
- var useBitStringContents = false;
- if('bitStringContents' in obj) {
- useBitStringContents = true;
- if(obj.original) {
- useBitStringContents = asn1.equals(obj, obj.original);
- }
- }
- if(useBitStringContents) {
- value.putBytes(obj.bitStringContents);
- } else if(obj.composed) {
-
-
-
- if(obj.constructed) {
- b1 |= 0x20;
- } else {
-
- value.putByte(0x00);
- }
-
- for(var i = 0; i < obj.value.length; ++i) {
- if(obj.value[i] !== undefined) {
- value.putBuffer(asn1.toDer(obj.value[i]));
- }
- }
- } else {
-
- if(obj.type === asn1.Type.BMPSTRING) {
- for(var i = 0; i < obj.value.length; ++i) {
- value.putInt16(obj.value.charCodeAt(i));
- }
- } else {
-
-
-
- if(obj.type === asn1.Type.INTEGER &&
- obj.value.length > 1 &&
-
- ((obj.value.charCodeAt(0) === 0 &&
- (obj.value.charCodeAt(1) & 0x80) === 0) ||
-
- (obj.value.charCodeAt(0) === 0xFF &&
- (obj.value.charCodeAt(1) & 0x80) === 0x80))) {
- value.putBytes(obj.value.substr(1));
- } else {
- value.putBytes(obj.value);
- }
- }
- }
-
- bytes.putByte(b1);
-
- if(value.length() <= 127) {
-
-
- bytes.putByte(value.length() & 0x7F);
- } else {
-
-
-
-
- var len = value.length();
- var lenBytes = '';
- do {
- lenBytes += String.fromCharCode(len & 0xFF);
- len = len >>> 8;
- } while(len > 0);
-
-
- bytes.putByte(lenBytes.length | 0x80);
-
-
- for(var i = lenBytes.length - 1; i >= 0; --i) {
- bytes.putByte(lenBytes.charCodeAt(i));
- }
- }
-
- bytes.putBuffer(value);
- return bytes;
- };
- asn1.oidToDer = function(oid) {
-
- var values = oid.split('.');
- var bytes = forge.util.createBuffer();
-
- bytes.putByte(40 * parseInt(values[0], 10) + parseInt(values[1], 10));
-
-
- var last, valueBytes, value, b;
- for(var i = 2; i < values.length; ++i) {
-
-
- last = true;
- valueBytes = [];
- value = parseInt(values[i], 10);
- do {
- b = value & 0x7F;
- value = value >>> 7;
-
- if(!last) {
- b |= 0x80;
- }
- valueBytes.push(b);
- last = false;
- } while(value > 0);
-
- for(var n = valueBytes.length - 1; n >= 0; --n) {
- bytes.putByte(valueBytes[n]);
- }
- }
- return bytes;
- };
- asn1.derToOid = function(bytes) {
- var oid;
-
- if(typeof bytes === 'string') {
- bytes = forge.util.createBuffer(bytes);
- }
-
- var b = bytes.getByte();
- oid = Math.floor(b / 40) + '.' + (b % 40);
-
-
- var value = 0;
- while(bytes.length() > 0) {
- b = bytes.getByte();
- value = value << 7;
-
- if(b & 0x80) {
- value += b & 0x7F;
- } else {
-
- oid += '.' + (value + b);
- value = 0;
- }
- }
- return oid;
- };
- asn1.utcTimeToDate = function(utc) {
-
- var date = new Date();
-
- var year = parseInt(utc.substr(0, 2), 10);
- year = (year >= 50) ? 1900 + year : 2000 + year;
- var MM = parseInt(utc.substr(2, 2), 10) - 1;
- var DD = parseInt(utc.substr(4, 2), 10);
- var hh = parseInt(utc.substr(6, 2), 10);
- var mm = parseInt(utc.substr(8, 2), 10);
- var ss = 0;
-
- if(utc.length > 11) {
-
- var c = utc.charAt(10);
- var end = 10;
-
- if(c !== '+' && c !== '-') {
-
- ss = parseInt(utc.substr(10, 2), 10);
- end += 2;
- }
- }
-
- date.setUTCFullYear(year, MM, DD);
- date.setUTCHours(hh, mm, ss, 0);
- if(end) {
-
- c = utc.charAt(end);
- if(c === '+' || c === '-') {
-
- var hhoffset = parseInt(utc.substr(end + 1, 2), 10);
- var mmoffset = parseInt(utc.substr(end + 4, 2), 10);
-
- var offset = hhoffset * 60 + mmoffset;
- offset *= 60000;
-
- if(c === '+') {
- date.setTime(+date - offset);
- } else {
- date.setTime(+date + offset);
- }
- }
- }
- return date;
- };
- asn1.generalizedTimeToDate = function(gentime) {
-
- var date = new Date();
- var YYYY = parseInt(gentime.substr(0, 4), 10);
- var MM = parseInt(gentime.substr(4, 2), 10) - 1;
- var DD = parseInt(gentime.substr(6, 2), 10);
- var hh = parseInt(gentime.substr(8, 2), 10);
- var mm = parseInt(gentime.substr(10, 2), 10);
- var ss = parseInt(gentime.substr(12, 2), 10);
- var fff = 0;
- var offset = 0;
- var isUTC = false;
- if(gentime.charAt(gentime.length - 1) === 'Z') {
- isUTC = true;
- }
- var end = gentime.length - 5, c = gentime.charAt(end);
- if(c === '+' || c === '-') {
-
- var hhoffset = parseInt(gentime.substr(end + 1, 2), 10);
- var mmoffset = parseInt(gentime.substr(end + 4, 2), 10);
-
- offset = hhoffset * 60 + mmoffset;
- offset *= 60000;
-
- if(c === '+') {
- offset *= -1;
- }
- isUTC = true;
- }
-
- if(gentime.charAt(14) === '.') {
- fff = parseFloat(gentime.substr(14), 10) * 1000;
- }
- if(isUTC) {
- date.setUTCFullYear(YYYY, MM, DD);
- date.setUTCHours(hh, mm, ss, fff);
-
- date.setTime(+date + offset);
- } else {
- date.setFullYear(YYYY, MM, DD);
- date.setHours(hh, mm, ss, fff);
- }
- return date;
- };
- asn1.dateToUtcTime = function(date) {
-
- if(typeof date === 'string') {
- return date;
- }
- var rval = '';
-
- var format = [];
- format.push(('' + date.getUTCFullYear()).substr(2));
- format.push('' + (date.getUTCMonth() + 1));
- format.push('' + date.getUTCDate());
- format.push('' + date.getUTCHours());
- format.push('' + date.getUTCMinutes());
- format.push('' + date.getUTCSeconds());
-
- for(var i = 0; i < format.length; ++i) {
- if(format[i].length < 2) {
- rval += '0';
- }
- rval += format[i];
- }
- rval += 'Z';
- return rval;
- };
- asn1.dateToGeneralizedTime = function(date) {
-
- if(typeof date === 'string') {
- return date;
- }
- var rval = '';
-
- var format = [];
- format.push('' + date.getUTCFullYear());
- format.push('' + (date.getUTCMonth() + 1));
- format.push('' + date.getUTCDate());
- format.push('' + date.getUTCHours());
- format.push('' + date.getUTCMinutes());
- format.push('' + date.getUTCSeconds());
-
- for(var i = 0; i < format.length; ++i) {
- if(format[i].length < 2) {
- rval += '0';
- }
- rval += format[i];
- }
- rval += 'Z';
- return rval;
- };
- asn1.integerToDer = function(x) {
- var rval = forge.util.createBuffer();
- if(x >= -0x80 && x < 0x80) {
- return rval.putSignedInt(x, 8);
- }
- if(x >= -0x8000 && x < 0x8000) {
- return rval.putSignedInt(x, 16);
- }
- if(x >= -0x800000 && x < 0x800000) {
- return rval.putSignedInt(x, 24);
- }
- if(x >= -0x80000000 && x < 0x80000000) {
- return rval.putSignedInt(x, 32);
- }
- var error = new Error('Integer too large; max is 32-bits.');
- error.integer = x;
- throw error;
- };
- asn1.derToInteger = function(bytes) {
-
- if(typeof bytes === 'string') {
- bytes = forge.util.createBuffer(bytes);
- }
- var n = bytes.length() * 8;
- if(n > 32) {
- throw new Error('Integer too large; max is 32-bits.');
- }
- return bytes.getSignedInt(n);
- };
- asn1.validate = function(obj, v, capture, errors) {
- var rval = false;
-
- if((obj.tagClass === v.tagClass || typeof(v.tagClass) === 'undefined') &&
- (obj.type === v.type || typeof(v.type) === 'undefined')) {
-
- if(obj.constructed === v.constructed ||
- typeof(v.constructed) === 'undefined') {
- rval = true;
-
- if(v.value && forge.util.isArray(v.value)) {
- var j = 0;
- for(var i = 0; rval && i < v.value.length; ++i) {
- rval = v.value[i].optional || false;
- if(obj.value[j]) {
- rval = asn1.validate(obj.value[j], v.value[i], capture, errors);
- if(rval) {
- ++j;
- } else if(v.value[i].optional) {
- rval = true;
- }
- }
- if(!rval && errors) {
- errors.push(
- '[' + v.name + '] ' +
- 'Tag class "' + v.tagClass + '", type "' +
- v.type + '" expected value length "' +
- v.value.length + '", got "' +
- obj.value.length + '"');
- }
- }
- }
- if(rval && capture) {
- if(v.capture) {
- capture[v.capture] = obj.value;
- }
- if(v.captureAsn1) {
- capture[v.captureAsn1] = obj;
- }
- if(v.captureBitStringContents && 'bitStringContents' in obj) {
- capture[v.captureBitStringContents] = obj.bitStringContents;
- }
- if(v.captureBitStringValue && 'bitStringContents' in obj) {
- var value;
- if(obj.bitStringContents.length < 2) {
- capture[v.captureBitStringValue] = '';
- } else {
-
- var unused = obj.bitStringContents.charCodeAt(0);
- if(unused !== 0) {
- throw new Error(
- 'captureBitStringValue only supported for zero unused bits');
- }
- capture[v.captureBitStringValue] = obj.bitStringContents.slice(1);
- }
- }
- }
- } else if(errors) {
- errors.push(
- '[' + v.name + '] ' +
- 'Expected constructed "' + v.constructed + '", got "' +
- obj.constructed + '"');
- }
- } else if(errors) {
- if(obj.tagClass !== v.tagClass) {
- errors.push(
- '[' + v.name + '] ' +
- 'Expected tag class "' + v.tagClass + '", got "' +
- obj.tagClass + '"');
- }
- if(obj.type !== v.type) {
- errors.push(
- '[' + v.name + '] ' +
- 'Expected type "' + v.type + '", got "' + obj.type + '"');
- }
- }
- return rval;
- };
- var _nonLatinRegex = /[^\\u0000-\\u00ff]/;
- asn1.prettyPrint = function(obj, level, indentation) {
- var rval = '';
-
- level = level || 0;
- indentation = indentation || 2;
-
- if(level > 0) {
- rval += '\n';
- }
-
- var indent = '';
- for(var i = 0; i < level * indentation; ++i) {
- indent += ' ';
- }
-
- rval += indent + 'Tag: ';
- switch(obj.tagClass) {
- case asn1.Class.UNIVERSAL:
- rval += 'Universal:';
- break;
- case asn1.Class.APPLICATION:
- rval += 'Application:';
- break;
- case asn1.Class.CONTEXT_SPECIFIC:
- rval += 'Context-Specific:';
- break;
- case asn1.Class.PRIVATE:
- rval += 'Private:';
- break;
- }
- if(obj.tagClass === asn1.Class.UNIVERSAL) {
- rval += obj.type;
-
- switch(obj.type) {
- case asn1.Type.NONE:
- rval += ' (None)';
- break;
- case asn1.Type.BOOLEAN:
- rval += ' (Boolean)';
- break;
- case asn1.Type.INTEGER:
- rval += ' (Integer)';
- break;
- case asn1.Type.BITSTRING:
- rval += ' (Bit string)';
- break;
- case asn1.Type.OCTETSTRING:
- rval += ' (Octet string)';
- break;
- case asn1.Type.NULL:
- rval += ' (Null)';
- break;
- case asn1.Type.OID:
- rval += ' (Object Identifier)';
- break;
- case asn1.Type.ODESC:
- rval += ' (Object Descriptor)';
- break;
- case asn1.Type.EXTERNAL:
- rval += ' (External or Instance of)';
- break;
- case asn1.Type.REAL:
- rval += ' (Real)';
- break;
- case asn1.Type.ENUMERATED:
- rval += ' (Enumerated)';
- break;
- case asn1.Type.EMBEDDED:
- rval += ' (Embedded PDV)';
- break;
- case asn1.Type.UTF8:
- rval += ' (UTF8)';
- break;
- case asn1.Type.ROID:
- rval += ' (Relative Object Identifier)';
- break;
- case asn1.Type.SEQUENCE:
- rval += ' (Sequence)';
- break;
- case asn1.Type.SET:
- rval += ' (Set)';
- break;
- case asn1.Type.PRINTABLESTRING:
- rval += ' (Printable String)';
- break;
- case asn1.Type.IA5String:
- rval += ' (IA5String (ASCII))';
- break;
- case asn1.Type.UTCTIME:
- rval += ' (UTC time)';
- break;
- case asn1.Type.GENERALIZEDTIME:
- rval += ' (Generalized time)';
- break;
- case asn1.Type.BMPSTRING:
- rval += ' (BMP String)';
- break;
- }
- } else {
- rval += obj.type;
- }
- rval += '\n';
- rval += indent + 'Constructed: ' + obj.constructed + '\n';
- if(obj.composed) {
- var subvalues = 0;
- var sub = '';
- for(var i = 0; i < obj.value.length; ++i) {
- if(obj.value[i] !== undefined) {
- subvalues += 1;
- sub += asn1.prettyPrint(obj.value[i], level + 1, indentation);
- if((i + 1) < obj.value.length) {
- sub += ',';
- }
- }
- }
- rval += indent + 'Sub values: ' + subvalues + sub;
- } else {
- rval += indent + 'Value: ';
- if(obj.type === asn1.Type.OID) {
- var oid = asn1.derToOid(obj.value);
- rval += oid;
- if(forge.pki && forge.pki.oids) {
- if(oid in forge.pki.oids) {
- rval += ' (' + forge.pki.oids[oid] + ') ';
- }
- }
- }
- if(obj.type === asn1.Type.INTEGER) {
- try {
- rval += asn1.derToInteger(obj.value);
- } catch(ex) {
- rval += '0x' + forge.util.bytesToHex(obj.value);
- }
- } else if(obj.type === asn1.Type.BITSTRING) {
-
- if(obj.value.length > 1) {
-
- rval += '0x' + forge.util.bytesToHex(obj.value.slice(1));
- } else {
- rval += '(none)';
- }
-
- if(obj.value.length > 0) {
- var unused = obj.value.charCodeAt(0);
- if(unused == 1) {
- rval += ' (1 unused bit shown)';
- } else if(unused > 1) {
- rval += ' (' + unused + ' unused bits shown)';
- }
- }
- } else if(obj.type === asn1.Type.OCTETSTRING) {
- if(!_nonLatinRegex.test(obj.value)) {
- rval += '(' + obj.value + ') ';
- }
- rval += '0x' + forge.util.bytesToHex(obj.value);
- } else if(obj.type === asn1.Type.UTF8) {
- try {
- rval += forge.util.decodeUtf8(obj.value);
- } catch(e) {
- if(e.message === 'URI malformed') {
- rval +=
- '0x' + forge.util.bytesToHex(obj.value) + ' (malformed UTF8)';
- } else {
- throw e;
- }
- }
- } else if(obj.type === asn1.Type.PRINTABLESTRING ||
- obj.type === asn1.Type.IA5String) {
- rval += obj.value;
- } else if(_nonLatinRegex.test(obj.value)) {
- rval += '0x' + forge.util.bytesToHex(obj.value);
- } else if(obj.value.length === 0) {
- rval += '[null]';
- } else {
- rval += obj.value;
- }
- }
- return rval;
- };
|