UNPKG

asn1js

Version:

asn1js is a pure JavaScript library implementing this standard. ASN.1 is the basis of all X.509 related data structures and numerous other protocols used on the web

1,370 lines (1,341 loc) 112 kB
/*! * Copyright (c) 2014, GMO GlobalSign * Copyright (c) 2015-2022, Peculiar Ventures * All rights reserved. * * Author 2014-2019, Yury Strozhevsky * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, this * list of conditions and the following disclaimer in the documentation and/or * other materials provided with the distribution. * * * Neither the name of the copyright holder nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ 'use strict'; var pvtsutils = require('pvtsutils'); var pvutils = require('pvutils'); function _interopNamespaceDefault(e) { var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n.default = e; return Object.freeze(n); } var pvtsutils__namespace = /*#__PURE__*/_interopNamespaceDefault(pvtsutils); var pvutils__namespace = /*#__PURE__*/_interopNamespaceDefault(pvutils); function assertBigInt() { if (typeof BigInt === "undefined") { throw new Error("BigInt is not defined. Your environment doesn't implement BigInt."); } } function concat(buffers) { let outputLength = 0; let prevLength = 0; for (let i = 0; i < buffers.length; i++) { const buffer = buffers[i]; outputLength += buffer.byteLength; } const retView = new Uint8Array(outputLength); for (let i = 0; i < buffers.length; i++) { const buffer = buffers[i]; retView.set(new Uint8Array(buffer), prevLength); prevLength += buffer.byteLength; } return retView.buffer; } function checkBufferParams(baseBlock, inputBuffer, inputOffset, inputLength) { if (!(inputBuffer instanceof Uint8Array)) { baseBlock.error = "Wrong parameter: inputBuffer must be 'Uint8Array'"; return false; } if (!inputBuffer.byteLength) { baseBlock.error = "Wrong parameter: inputBuffer has zero length"; return false; } if (inputOffset < 0) { baseBlock.error = "Wrong parameter: inputOffset less than zero"; return false; } if (inputLength < 0) { baseBlock.error = "Wrong parameter: inputLength less than zero"; return false; } if ((inputBuffer.byteLength - inputOffset - inputLength) < 0) { baseBlock.error = "End of input reached before message was fully decoded (inconsistent offset and length values)"; return false; } return true; } class ViewWriter { constructor() { this.items = []; } write(buf) { this.items.push(buf); } final() { return concat(this.items); } } const powers2 = [new Uint8Array([1])]; const digitsString = "0123456789"; const NAME = "name"; const VALUE_HEX_VIEW = "valueHexView"; const IS_HEX_ONLY = "isHexOnly"; const ID_BLOCK = "idBlock"; const TAG_CLASS = "tagClass"; const TAG_NUMBER = "tagNumber"; const IS_CONSTRUCTED = "isConstructed"; const FROM_BER = "fromBER"; const TO_BER = "toBER"; const LOCAL = "local"; const EMPTY_STRING = ""; const EMPTY_BUFFER = new ArrayBuffer(0); const EMPTY_VIEW = new Uint8Array(0); const END_OF_CONTENT_NAME = "EndOfContent"; const OCTET_STRING_NAME = "OCTET STRING"; const BIT_STRING_NAME = "BIT STRING"; function HexBlock(BaseClass) { var _a; return _a = class Some extends BaseClass { get valueHex() { return this.valueHexView.slice().buffer; } set valueHex(value) { this.valueHexView = new Uint8Array(value); } constructor(...args) { var _b; super(...args); const params = args[0] || {}; this.isHexOnly = (_b = params.isHexOnly) !== null && _b !== void 0 ? _b : false; this.valueHexView = params.valueHex ? pvtsutils__namespace.BufferSourceConverter.toUint8Array(params.valueHex) : EMPTY_VIEW; } fromBER(inputBuffer, inputOffset, inputLength) { const view = inputBuffer instanceof ArrayBuffer ? new Uint8Array(inputBuffer) : inputBuffer; if (!checkBufferParams(this, view, inputOffset, inputLength)) { return -1; } const endLength = inputOffset + inputLength; this.valueHexView = view.subarray(inputOffset, endLength); if (!this.valueHexView.length) { this.warnings.push("Zero buffer length"); return inputOffset; } this.blockLength = inputLength; return endLength; } toBER(sizeOnly = false) { if (!this.isHexOnly) { this.error = "Flag 'isHexOnly' is not set, abort"; return EMPTY_BUFFER; } if (sizeOnly) { return new ArrayBuffer(this.valueHexView.byteLength); } return (this.valueHexView.byteLength === this.valueHexView.buffer.byteLength) ? this.valueHexView.buffer : this.valueHexView.slice().buffer; } toJSON() { return { ...super.toJSON(), isHexOnly: this.isHexOnly, valueHex: pvtsutils__namespace.Convert.ToHex(this.valueHexView), }; } }, _a.NAME = "hexBlock", _a; } class LocalBaseBlock { static blockName() { return this.NAME; } get valueBeforeDecode() { return this.valueBeforeDecodeView.slice().buffer; } set valueBeforeDecode(value) { this.valueBeforeDecodeView = new Uint8Array(value); } constructor({ blockLength = 0, error = EMPTY_STRING, warnings = [], valueBeforeDecode = EMPTY_VIEW, } = {}) { this.blockLength = blockLength; this.error = error; this.warnings = warnings; this.valueBeforeDecodeView = pvtsutils__namespace.BufferSourceConverter.toUint8Array(valueBeforeDecode); } toJSON() { return { blockName: this.constructor.NAME, blockLength: this.blockLength, error: this.error, warnings: this.warnings, valueBeforeDecode: pvtsutils__namespace.Convert.ToHex(this.valueBeforeDecodeView), }; } } LocalBaseBlock.NAME = "baseBlock"; class ValueBlock extends LocalBaseBlock { fromBER(_inputBuffer, _inputOffset, _inputLength) { throw TypeError("User need to make a specific function in a class which extends 'ValueBlock'"); } toBER(_sizeOnly, _writer) { throw TypeError("User need to make a specific function in a class which extends 'ValueBlock'"); } } ValueBlock.NAME = "valueBlock"; class LocalIdentificationBlock extends HexBlock(LocalBaseBlock) { constructor({ idBlock = {} } = {}) { var _a, _b, _c, _d; super(); if (idBlock) { this.isHexOnly = (_a = idBlock.isHexOnly) !== null && _a !== void 0 ? _a : false; this.valueHexView = idBlock.valueHex ? pvtsutils__namespace.BufferSourceConverter.toUint8Array(idBlock.valueHex) : EMPTY_VIEW; this.tagClass = (_b = idBlock.tagClass) !== null && _b !== void 0 ? _b : -1; this.tagNumber = (_c = idBlock.tagNumber) !== null && _c !== void 0 ? _c : -1; this.isConstructed = (_d = idBlock.isConstructed) !== null && _d !== void 0 ? _d : false; } else { this.tagClass = -1; this.tagNumber = -1; this.isConstructed = false; } } toBER(sizeOnly = false) { let firstOctet = 0; switch (this.tagClass) { case 1: firstOctet |= 0x00; break; case 2: firstOctet |= 0x40; break; case 3: firstOctet |= 0x80; break; case 4: firstOctet |= 0xC0; break; default: this.error = "Unknown tag class"; return EMPTY_BUFFER; } if (this.isConstructed) firstOctet |= 0x20; if (this.tagNumber < 31 && !this.isHexOnly) { const retView = new Uint8Array(1); if (!sizeOnly) { let number = this.tagNumber; number &= 0x1F; firstOctet |= number; retView[0] = firstOctet; } return retView.buffer; } if (!this.isHexOnly) { const encodedBuf = pvutils__namespace.utilToBase(this.tagNumber, 7); const encodedView = new Uint8Array(encodedBuf); const size = encodedBuf.byteLength; const retView = new Uint8Array(size + 1); retView[0] = (firstOctet | 0x1F); if (!sizeOnly) { for (let i = 0; i < (size - 1); i++) retView[i + 1] = encodedView[i] | 0x80; retView[size] = encodedView[size - 1]; } return retView.buffer; } const retView = new Uint8Array(this.valueHexView.byteLength + 1); retView[0] = (firstOctet | 0x1F); if (!sizeOnly) { const curView = this.valueHexView; for (let i = 0; i < (curView.length - 1); i++) retView[i + 1] = curView[i] | 0x80; retView[this.valueHexView.byteLength] = curView[curView.length - 1]; } return retView.buffer; } fromBER(inputBuffer, inputOffset, inputLength) { const inputView = pvtsutils__namespace.BufferSourceConverter.toUint8Array(inputBuffer); if (!checkBufferParams(this, inputView, inputOffset, inputLength)) { return -1; } const intBuffer = inputView.subarray(inputOffset, inputOffset + inputLength); if (intBuffer.length === 0) { this.error = "Zero buffer length"; return -1; } const tagClassMask = intBuffer[0] & 0xC0; switch (tagClassMask) { case 0x00: this.tagClass = (1); break; case 0x40: this.tagClass = (2); break; case 0x80: this.tagClass = (3); break; case 0xC0: this.tagClass = (4); break; default: this.error = "Unknown tag class"; return -1; } this.isConstructed = (intBuffer[0] & 0x20) === 0x20; this.isHexOnly = false; const tagNumberMask = intBuffer[0] & 0x1F; if (tagNumberMask !== 0x1F) { this.tagNumber = (tagNumberMask); this.blockLength = 1; } else { let count = 1; let intTagNumberBuffer = this.valueHexView = new Uint8Array(255); let tagNumberBufferMaxLength = 255; while (intBuffer[count] & 0x80) { intTagNumberBuffer[count - 1] = intBuffer[count] & 0x7F; count++; if (count >= intBuffer.length) { this.error = "End of input reached before message was fully decoded"; return -1; } if (count === tagNumberBufferMaxLength) { tagNumberBufferMaxLength += 255; const tempBufferView = new Uint8Array(tagNumberBufferMaxLength); for (let i = 0; i < intTagNumberBuffer.length; i++) tempBufferView[i] = intTagNumberBuffer[i]; intTagNumberBuffer = this.valueHexView = new Uint8Array(tagNumberBufferMaxLength); } } this.blockLength = (count + 1); intTagNumberBuffer[count - 1] = intBuffer[count] & 0x7F; const tempBufferView = new Uint8Array(count); for (let i = 0; i < count; i++) tempBufferView[i] = intTagNumberBuffer[i]; intTagNumberBuffer = this.valueHexView = new Uint8Array(count); intTagNumberBuffer.set(tempBufferView); if (this.blockLength <= 9) this.tagNumber = pvutils__namespace.utilFromBase(intTagNumberBuffer, 7); else { this.isHexOnly = true; this.warnings.push("Tag too long, represented as hex-coded"); } } if (((this.tagClass === 1)) && (this.isConstructed)) { switch (this.tagNumber) { case 1: case 2: case 5: case 6: case 9: case 13: case 14: case 23: case 24: case 31: case 32: case 33: case 34: this.error = "Constructed encoding used for primitive type"; return -1; } } return (inputOffset + this.blockLength); } toJSON() { return { ...super.toJSON(), tagClass: this.tagClass, tagNumber: this.tagNumber, isConstructed: this.isConstructed, }; } } LocalIdentificationBlock.NAME = "identificationBlock"; class LocalLengthBlock extends LocalBaseBlock { constructor({ lenBlock = {} } = {}) { var _a, _b, _c; super(); this.isIndefiniteForm = (_a = lenBlock.isIndefiniteForm) !== null && _a !== void 0 ? _a : false; this.longFormUsed = (_b = lenBlock.longFormUsed) !== null && _b !== void 0 ? _b : false; this.length = (_c = lenBlock.length) !== null && _c !== void 0 ? _c : 0; } fromBER(inputBuffer, inputOffset, inputLength) { const view = pvtsutils__namespace.BufferSourceConverter.toUint8Array(inputBuffer); if (!checkBufferParams(this, view, inputOffset, inputLength)) { return -1; } const intBuffer = view.subarray(inputOffset, inputOffset + inputLength); if (intBuffer.length === 0) { this.error = "Zero buffer length"; return -1; } if (intBuffer[0] === 0xFF) { this.error = "Length block 0xFF is reserved by standard"; return -1; } this.isIndefiniteForm = intBuffer[0] === 0x80; if (this.isIndefiniteForm) { this.blockLength = 1; return (inputOffset + this.blockLength); } this.longFormUsed = !!(intBuffer[0] & 0x80); if (this.longFormUsed === false) { this.length = (intBuffer[0]); this.blockLength = 1; return (inputOffset + this.blockLength); } const count = intBuffer[0] & 0x7F; if (count > 8) { this.error = "Too big integer"; return -1; } if ((count + 1) > intBuffer.length) { this.error = "End of input reached before message was fully decoded"; return -1; } const lenOffset = inputOffset + 1; const lengthBufferView = view.subarray(lenOffset, lenOffset + count); if (lengthBufferView[count - 1] === 0x00) this.warnings.push("Needlessly long encoded length"); this.length = pvutils__namespace.utilFromBase(lengthBufferView, 8); if (this.longFormUsed && (this.length <= 127)) this.warnings.push("Unnecessary usage of long length form"); this.blockLength = count + 1; return (inputOffset + this.blockLength); } toBER(sizeOnly = false) { let retBuf; let retView; if (this.length > 127) this.longFormUsed = true; if (this.isIndefiniteForm) { retBuf = new ArrayBuffer(1); if (sizeOnly === false) { retView = new Uint8Array(retBuf); retView[0] = 0x80; } return retBuf; } if (this.longFormUsed) { const encodedBuf = pvutils__namespace.utilToBase(this.length, 8); if (encodedBuf.byteLength > 127) { this.error = "Too big length"; return (EMPTY_BUFFER); } retBuf = new ArrayBuffer(encodedBuf.byteLength + 1); if (sizeOnly) return retBuf; const encodedView = new Uint8Array(encodedBuf); retView = new Uint8Array(retBuf); retView[0] = encodedBuf.byteLength | 0x80; for (let i = 0; i < encodedBuf.byteLength; i++) retView[i + 1] = encodedView[i]; return retBuf; } retBuf = new ArrayBuffer(1); if (sizeOnly === false) { retView = new Uint8Array(retBuf); retView[0] = this.length; } return retBuf; } toJSON() { return { ...super.toJSON(), isIndefiniteForm: this.isIndefiniteForm, longFormUsed: this.longFormUsed, length: this.length, }; } } LocalLengthBlock.NAME = "lengthBlock"; const typeStore = {}; class BaseBlock extends LocalBaseBlock { constructor({ name = EMPTY_STRING, optional = false, primitiveSchema, ...parameters } = {}, valueBlockType) { super(parameters); this.name = name; this.optional = optional; if (primitiveSchema) { this.primitiveSchema = primitiveSchema; } this.idBlock = new LocalIdentificationBlock(parameters); this.lenBlock = new LocalLengthBlock(parameters); this.valueBlock = valueBlockType ? new valueBlockType(parameters) : new ValueBlock(parameters); } fromBER(inputBuffer, inputOffset, inputLength) { const resultOffset = this.valueBlock.fromBER(inputBuffer, inputOffset, (this.lenBlock.isIndefiniteForm) ? inputLength : this.lenBlock.length); if (resultOffset === -1) { this.error = this.valueBlock.error; return resultOffset; } if (!this.idBlock.error.length) this.blockLength += this.idBlock.blockLength; if (!this.lenBlock.error.length) this.blockLength += this.lenBlock.blockLength; if (!this.valueBlock.error.length) this.blockLength += this.valueBlock.blockLength; return resultOffset; } toBER(sizeOnly, writer) { const _writer = writer || new ViewWriter(); if (!writer) { prepareIndefiniteForm(this); } const idBlockBuf = this.idBlock.toBER(sizeOnly); _writer.write(idBlockBuf); if (this.lenBlock.isIndefiniteForm) { _writer.write(new Uint8Array([0x80]).buffer); this.valueBlock.toBER(sizeOnly, _writer); _writer.write(new ArrayBuffer(2)); } else { const valueBlockBuf = this.valueBlock.toBER(sizeOnly); this.lenBlock.length = valueBlockBuf.byteLength; const lenBlockBuf = this.lenBlock.toBER(sizeOnly); _writer.write(lenBlockBuf); _writer.write(valueBlockBuf); } if (!writer) { return _writer.final(); } return EMPTY_BUFFER; } toJSON() { const object = { ...super.toJSON(), idBlock: this.idBlock.toJSON(), lenBlock: this.lenBlock.toJSON(), valueBlock: this.valueBlock.toJSON(), name: this.name, optional: this.optional, }; if (this.primitiveSchema) object.primitiveSchema = this.primitiveSchema.toJSON(); return object; } toString(encoding = "ascii") { if (encoding === "ascii") { return this.onAsciiEncoding(); } return pvtsutils__namespace.Convert.ToHex(this.toBER()); } onAsciiEncoding() { const name = this.constructor.NAME; const value = pvtsutils__namespace.Convert.ToHex(this.valueBlock.valueBeforeDecodeView); return `${name} : ${value}`; } isEqual(other) { if (this === other) { return true; } if (!(other instanceof this.constructor)) { return false; } const thisRaw = this.toBER(); const otherRaw = other.toBER(); return pvutils__namespace.isEqualBuffer(thisRaw, otherRaw); } } BaseBlock.NAME = "BaseBlock"; function prepareIndefiniteForm(baseBlock) { var _a; if (baseBlock instanceof typeStore.Constructed) { for (const value of baseBlock.valueBlock.value) { if (prepareIndefiniteForm(value)) { baseBlock.lenBlock.isIndefiniteForm = true; } } } return !!((_a = baseBlock.lenBlock) === null || _a === void 0 ? void 0 : _a.isIndefiniteForm); } class BaseStringBlock extends BaseBlock { getValue() { return this.valueBlock.value; } setValue(value) { this.valueBlock.value = value; } constructor({ value = EMPTY_STRING, ...parameters } = {}, stringValueBlockType) { super(parameters, stringValueBlockType); if (value) { this.fromString(value); } } fromBER(inputBuffer, inputOffset, inputLength) { const resultOffset = this.valueBlock.fromBER(inputBuffer, inputOffset, (this.lenBlock.isIndefiniteForm) ? inputLength : this.lenBlock.length); if (resultOffset === -1) { this.error = this.valueBlock.error; return resultOffset; } this.fromBuffer(this.valueBlock.valueHexView); if (!this.idBlock.error.length) this.blockLength += this.idBlock.blockLength; if (!this.lenBlock.error.length) this.blockLength += this.lenBlock.blockLength; if (!this.valueBlock.error.length) this.blockLength += this.valueBlock.blockLength; return resultOffset; } onAsciiEncoding() { return `${this.constructor.NAME} : '${this.valueBlock.value}'`; } } BaseStringBlock.NAME = "BaseStringBlock"; class LocalPrimitiveValueBlock extends HexBlock(ValueBlock) { constructor({ isHexOnly = true, ...parameters } = {}) { super(parameters); this.isHexOnly = isHexOnly; } } LocalPrimitiveValueBlock.NAME = "PrimitiveValueBlock"; var _a$w; class Primitive extends BaseBlock { constructor(parameters = {}) { super(parameters, LocalPrimitiveValueBlock); this.idBlock.isConstructed = false; } } _a$w = Primitive; (() => { typeStore.Primitive = _a$w; })(); Primitive.NAME = "PRIMITIVE"; function localChangeType(inputObject, newType) { if (inputObject instanceof newType) { return inputObject; } const newObject = new newType(); newObject.idBlock = inputObject.idBlock; newObject.lenBlock = inputObject.lenBlock; newObject.warnings = inputObject.warnings; newObject.valueBeforeDecodeView = inputObject.valueBeforeDecodeView; return newObject; } function localFromBER(inputBuffer, inputOffset = 0, inputLength = inputBuffer.length) { const incomingOffset = inputOffset; let returnObject = new BaseBlock({}, ValueBlock); const baseBlock = new LocalBaseBlock(); if (!checkBufferParams(baseBlock, inputBuffer, inputOffset, inputLength)) { returnObject.error = baseBlock.error; return { offset: -1, result: returnObject, }; } const intBuffer = inputBuffer.subarray(inputOffset, inputOffset + inputLength); if (!intBuffer.length) { returnObject.error = "Zero buffer length"; return { offset: -1, result: returnObject, }; } let resultOffset = returnObject.idBlock.fromBER(inputBuffer, inputOffset, inputLength); if (returnObject.idBlock.warnings.length) { returnObject.warnings.concat(returnObject.idBlock.warnings); } if (resultOffset === -1) { returnObject.error = returnObject.idBlock.error; return { offset: -1, result: returnObject, }; } inputOffset = resultOffset; inputLength -= returnObject.idBlock.blockLength; resultOffset = returnObject.lenBlock.fromBER(inputBuffer, inputOffset, inputLength); if (returnObject.lenBlock.warnings.length) { returnObject.warnings.concat(returnObject.lenBlock.warnings); } if (resultOffset === -1) { returnObject.error = returnObject.lenBlock.error; return { offset: -1, result: returnObject, }; } inputOffset = resultOffset; inputLength -= returnObject.lenBlock.blockLength; if (!returnObject.idBlock.isConstructed && returnObject.lenBlock.isIndefiniteForm) { returnObject.error = "Indefinite length form used for primitive encoding form"; return { offset: -1, result: returnObject, }; } let newASN1Type = BaseBlock; switch (returnObject.idBlock.tagClass) { case 1: if ((returnObject.idBlock.tagNumber >= 37) && (returnObject.idBlock.isHexOnly === false)) { returnObject.error = "UNIVERSAL 37 and upper tags are reserved by ASN.1 standard"; return { offset: -1, result: returnObject, }; } switch (returnObject.idBlock.tagNumber) { case 0: if ((returnObject.idBlock.isConstructed) && (returnObject.lenBlock.length > 0)) { returnObject.error = "Type [UNIVERSAL 0] is reserved"; return { offset: -1, result: returnObject, }; } newASN1Type = typeStore.EndOfContent; break; case 1: newASN1Type = typeStore.Boolean; break; case 2: newASN1Type = typeStore.Integer; break; case 3: newASN1Type = typeStore.BitString; break; case 4: newASN1Type = typeStore.OctetString; break; case 5: newASN1Type = typeStore.Null; break; case 6: newASN1Type = typeStore.ObjectIdentifier; break; case 10: newASN1Type = typeStore.Enumerated; break; case 12: newASN1Type = typeStore.Utf8String; break; case 13: newASN1Type = typeStore.RelativeObjectIdentifier; break; case 14: newASN1Type = typeStore.TIME; break; case 15: returnObject.error = "[UNIVERSAL 15] is reserved by ASN.1 standard"; return { offset: -1, result: returnObject, }; case 16: newASN1Type = typeStore.Sequence; break; case 17: newASN1Type = typeStore.Set; break; case 18: newASN1Type = typeStore.NumericString; break; case 19: newASN1Type = typeStore.PrintableString; break; case 20: newASN1Type = typeStore.TeletexString; break; case 21: newASN1Type = typeStore.VideotexString; break; case 22: newASN1Type = typeStore.IA5String; break; case 23: newASN1Type = typeStore.UTCTime; break; case 24: newASN1Type = typeStore.GeneralizedTime; break; case 25: newASN1Type = typeStore.GraphicString; break; case 26: newASN1Type = typeStore.VisibleString; break; case 27: newASN1Type = typeStore.GeneralString; break; case 28: newASN1Type = typeStore.UniversalString; break; case 29: newASN1Type = typeStore.CharacterString; break; case 30: newASN1Type = typeStore.BmpString; break; case 31: newASN1Type = typeStore.DATE; break; case 32: newASN1Type = typeStore.TimeOfDay; break; case 33: newASN1Type = typeStore.DateTime; break; case 34: newASN1Type = typeStore.Duration; break; default: { const newObject = returnObject.idBlock.isConstructed ? new typeStore.Constructed() : new typeStore.Primitive(); newObject.idBlock = returnObject.idBlock; newObject.lenBlock = returnObject.lenBlock; newObject.warnings = returnObject.warnings; returnObject = newObject; } } break; case 2: case 3: case 4: default: { newASN1Type = returnObject.idBlock.isConstructed ? typeStore.Constructed : typeStore.Primitive; } } returnObject = localChangeType(returnObject, newASN1Type); resultOffset = returnObject.fromBER(inputBuffer, inputOffset, returnObject.lenBlock.isIndefiniteForm ? inputLength : returnObject.lenBlock.length); returnObject.valueBeforeDecodeView = inputBuffer.subarray(incomingOffset, incomingOffset + returnObject.blockLength); return { offset: resultOffset, result: returnObject, }; } function fromBER(inputBuffer) { if (!inputBuffer.byteLength) { const result = new BaseBlock({}, ValueBlock); result.error = "Input buffer has zero length"; return { offset: -1, result, }; } return localFromBER(pvtsutils__namespace.BufferSourceConverter.toUint8Array(inputBuffer).slice(), 0, inputBuffer.byteLength); } function checkLen(indefiniteLength, length) { if (indefiniteLength) { return 1; } return length; } class LocalConstructedValueBlock extends ValueBlock { constructor({ value = [], isIndefiniteForm = false, ...parameters } = {}) { super(parameters); this.value = value; this.isIndefiniteForm = isIndefiniteForm; } fromBER(inputBuffer, inputOffset, inputLength) { const view = pvtsutils__namespace.BufferSourceConverter.toUint8Array(inputBuffer); if (!checkBufferParams(this, view, inputOffset, inputLength)) { return -1; } this.valueBeforeDecodeView = view.subarray(inputOffset, inputOffset + inputLength); if (this.valueBeforeDecodeView.length === 0) { this.warnings.push("Zero buffer length"); return inputOffset; } let currentOffset = inputOffset; while (checkLen(this.isIndefiniteForm, inputLength) > 0) { const returnObject = localFromBER(view, currentOffset, inputLength); if (returnObject.offset === -1) { this.error = returnObject.result.error; this.warnings.concat(returnObject.result.warnings); return -1; } currentOffset = returnObject.offset; this.blockLength += returnObject.result.blockLength; inputLength -= returnObject.result.blockLength; this.value.push(returnObject.result); if (this.isIndefiniteForm && returnObject.result.constructor.NAME === END_OF_CONTENT_NAME) { break; } } if (this.isIndefiniteForm) { if (this.value[this.value.length - 1].constructor.NAME === END_OF_CONTENT_NAME) { this.value.pop(); } else { this.warnings.push("No EndOfContent block encoded"); } } return currentOffset; } toBER(sizeOnly, writer) { const _writer = writer || new ViewWriter(); for (let i = 0; i < this.value.length; i++) { this.value[i].toBER(sizeOnly, _writer); } if (!writer) { return _writer.final(); } return EMPTY_BUFFER; } toJSON() { const object = { ...super.toJSON(), isIndefiniteForm: this.isIndefiniteForm, value: [], }; for (const value of this.value) { object.value.push(value.toJSON()); } return object; } } LocalConstructedValueBlock.NAME = "ConstructedValueBlock"; var _a$v; class Constructed extends BaseBlock { constructor(parameters = {}) { super(parameters, LocalConstructedValueBlock); this.idBlock.isConstructed = true; } fromBER(inputBuffer, inputOffset, inputLength) { this.valueBlock.isIndefiniteForm = this.lenBlock.isIndefiniteForm; const resultOffset = this.valueBlock.fromBER(inputBuffer, inputOffset, (this.lenBlock.isIndefiniteForm) ? inputLength : this.lenBlock.length); if (resultOffset === -1) { this.error = this.valueBlock.error; return resultOffset; } if (!this.idBlock.error.length) this.blockLength += this.idBlock.blockLength; if (!this.lenBlock.error.length) this.blockLength += this.lenBlock.blockLength; if (!this.valueBlock.error.length) this.blockLength += this.valueBlock.blockLength; return resultOffset; } onAsciiEncoding() { const values = []; for (const value of this.valueBlock.value) { values.push(value.toString("ascii").split("\n").map((o) => ` ${o}`).join("\n")); } const blockName = this.idBlock.tagClass === 3 ? `[${this.idBlock.tagNumber}]` : this.constructor.NAME; return values.length ? `${blockName} :\n${values.join("\n")}` : `${blockName} :`; } } _a$v = Constructed; (() => { typeStore.Constructed = _a$v; })(); Constructed.NAME = "CONSTRUCTED"; class LocalEndOfContentValueBlock extends ValueBlock { fromBER(inputBuffer, inputOffset, _inputLength) { return inputOffset; } toBER(_sizeOnly) { return EMPTY_BUFFER; } } LocalEndOfContentValueBlock.override = "EndOfContentValueBlock"; var _a$u; class EndOfContent extends BaseBlock { constructor(parameters = {}) { super(parameters, LocalEndOfContentValueBlock); this.idBlock.tagClass = 1; this.idBlock.tagNumber = 0; } } _a$u = EndOfContent; (() => { typeStore.EndOfContent = _a$u; })(); EndOfContent.NAME = END_OF_CONTENT_NAME; var _a$t; class Null extends BaseBlock { constructor(parameters = {}) { super(parameters, ValueBlock); this.idBlock.tagClass = 1; this.idBlock.tagNumber = 5; } fromBER(inputBuffer, inputOffset, inputLength) { if (this.lenBlock.length > 0) this.warnings.push("Non-zero length of value block for Null type"); if (!this.idBlock.error.length) this.blockLength += this.idBlock.blockLength; if (!this.lenBlock.error.length) this.blockLength += this.lenBlock.blockLength; this.blockLength += inputLength; if ((inputOffset + inputLength) > inputBuffer.byteLength) { this.error = "End of input reached before message was fully decoded (inconsistent offset and length values)"; return -1; } return (inputOffset + inputLength); } toBER(sizeOnly, writer) { const retBuf = new ArrayBuffer(2); if (!sizeOnly) { const retView = new Uint8Array(retBuf); retView[0] = 0x05; retView[1] = 0x00; } if (writer) { writer.write(retBuf); } return retBuf; } onAsciiEncoding() { return `${this.constructor.NAME}`; } } _a$t = Null; (() => { typeStore.Null = _a$t; })(); Null.NAME = "NULL"; class LocalBooleanValueBlock extends HexBlock(ValueBlock) { get value() { for (const octet of this.valueHexView) { if (octet > 0) { return true; } } return false; } set value(value) { this.valueHexView[0] = value ? 0xFF : 0x00; } constructor({ value, ...parameters } = {}) { super(parameters); if (parameters.valueHex) { this.valueHexView = pvtsutils__namespace.BufferSourceConverter.toUint8Array(parameters.valueHex); } else { this.valueHexView = new Uint8Array(1); } if (value) { this.value = value; } } fromBER(inputBuffer, inputOffset, inputLength) { const inputView = pvtsutils__namespace.BufferSourceConverter.toUint8Array(inputBuffer); if (!checkBufferParams(this, inputView, inputOffset, inputLength)) { return -1; } this.valueHexView = inputView.subarray(inputOffset, inputOffset + inputLength); if (inputLength > 1) this.warnings.push("Boolean value encoded in more then 1 octet"); this.isHexOnly = true; pvutils__namespace.utilDecodeTC.call(this); this.blockLength = inputLength; return (inputOffset + inputLength); } toBER() { return this.valueHexView.slice(); } toJSON() { return { ...super.toJSON(), value: this.value, }; } } LocalBooleanValueBlock.NAME = "BooleanValueBlock"; var _a$s; class Boolean extends BaseBlock { getValue() { return this.valueBlock.value; } setValue(value) { this.valueBlock.value = value; } constructor(parameters = {}) { super(parameters, LocalBooleanValueBlock); this.idBlock.tagClass = 1; this.idBlock.tagNumber = 1; } onAsciiEncoding() { return `${this.constructor.NAME} : ${this.getValue}`; } } _a$s = Boolean; (() => { typeStore.Boolean = _a$s; })(); Boolean.NAME = "BOOLEAN"; class LocalOctetStringValueBlock extends HexBlock(LocalConstructedValueBlock) { constructor({ isConstructed = false, ...parameters } = {}) { super(parameters); this.isConstructed = isConstructed; } fromBER(inputBuffer, inputOffset, inputLength) { let resultOffset = 0; if (this.isConstructed) { this.isHexOnly = false; resultOffset = LocalConstructedValueBlock.prototype.fromBER.call(this, inputBuffer, inputOffset, inputLength); if (resultOffset === -1) return resultOffset; for (let i = 0; i < this.value.length; i++) { const currentBlockName = this.value[i].constructor.NAME; if (currentBlockName === END_OF_CONTENT_NAME) { if (this.isIndefiniteForm) break; else { this.error = "EndOfContent is unexpected, OCTET STRING may consists of OCTET STRINGs only"; return -1; } } if (currentBlockName !== OCTET_STRING_NAME) { this.error = "OCTET STRING may consists of OCTET STRINGs only"; return -1; } } } else { this.isHexOnly = true; resultOffset = super.fromBER(inputBuffer, inputOffset, inputLength); this.blockLength = inputLength; } return resultOffset; } toBER(sizeOnly, writer) { if (this.isConstructed) return LocalConstructedValueBlock.prototype.toBER.call(this, sizeOnly, writer); return sizeOnly ? new ArrayBuffer(this.valueHexView.byteLength) : this.valueHexView.slice().buffer; } toJSON() { return { ...super.toJSON(), isConstructed: this.isConstructed, }; } } LocalOctetStringValueBlock.NAME = "OctetStringValueBlock"; var _a$r; class OctetString extends BaseBlock { constructor({ idBlock = {}, lenBlock = {}, ...parameters } = {}) { var _b, _c; (_b = parameters.isConstructed) !== null && _b !== void 0 ? _b : (parameters.isConstructed = !!((_c = parameters.value) === null || _c === void 0 ? void 0 : _c.length)); super({ idBlock: { isConstructed: parameters.isConstructed, ...idBlock, }, lenBlock: { ...lenBlock, isIndefiniteForm: !!parameters.isIndefiniteForm, }, ...parameters, }, LocalOctetStringValueBlock); this.idBlock.tagClass = 1; this.idBlock.tagNumber = 4; } fromBER(inputBuffer, inputOffset, inputLength) { this.valueBlock.isConstructed = this.idBlock.isConstructed; this.valueBlock.isIndefiniteForm = this.lenBlock.isIndefiniteForm; if (inputLength === 0) { if (this.idBlock.error.length === 0) this.blockLength += this.idBlock.blockLength; if (this.lenBlock.error.length === 0) this.blockLength += this.lenBlock.blockLength; return inputOffset; } if (!this.valueBlock.isConstructed) { const view = inputBuffer instanceof ArrayBuffer ? new Uint8Array(inputBuffer) : inputBuffer; const buf = view.subarray(inputOffset, inputOffset + inputLength); try { if (buf.byteLength) { const asn = localFromBER(buf, 0, buf.byteLength); if (asn.offset !== -1 && asn.offset === inputLength) { this.valueBlock.value = [asn.result]; } } } catch { } } return super.fromBER(inputBuffer, inputOffset, inputLength); } onAsciiEncoding() { if (this.valueBlock.isConstructed || (this.valueBlock.value && this.valueBlock.value.length)) { return Constructed.prototype.onAsciiEncoding.call(this); } const name = this.constructor.NAME; const value = pvtsutils__namespace.Convert.ToHex(this.valueBlock.valueHexView); return `${name} : ${value}`; } getValue() { if (!this.idBlock.isConstructed) { return this.valueBlock.valueHexView.slice().buffer; } const array = []; for (const content of this.valueBlock.value) { if (content instanceof _a$r) { array.push(content.valueBlock.valueHexView); } } return pvtsutils__namespace.BufferSourceConverter.concat(array); } } _a$r = OctetString; (() => { typeStore.OctetString = _a$r; })(); OctetString.NAME = OCTET_STRING_NAME; class LocalBitStringValueBlock extends HexBlock(LocalConstructedValueBlock) { constructor({ unusedBits = 0, isConstructed = false, ...parameters } = {}) { super(parameters); this.unusedBits = unusedBits; this.isConstructed = isConstructed; this.blockLength = this.valueHexView.byteLength; } fromBER(inputBuffer, inputOffset, inputLength) { if (!inputLength) { return inputOffset; } let resultOffset = -1; if (this.isConstructed) { resultOffset = LocalConstructedValueBlock.prototype.fromBER.call(this, inputBuffer, inputOffset, inputLength); if (resultOffset === -1) return resultOffset; for (const value of this.value) { const currentBlockName = value.constructor.NAME; if (currentBlockName === END_OF_CONTENT_NAME) { if (this.isIndefiniteForm) break; else { this.error = "EndOfContent is unexpected, BIT STRING may consists of BIT STRINGs only"; return -1; } } if (currentBlockName !== BIT_STRING_NAME) { this.error = "BIT STRING may consists of BIT STRINGs only"; return -1; } const valueBlock = value.valueBlock; if ((this.unusedBits > 0) && (valueBlock.unusedBits > 0)) { this.error = "Using of \"unused bits\" inside constructive BIT STRING allowed for least one only"; return -1; } this.unusedBits = valueBlock.unusedBits; } return resultOffset; } const inputView = pvtsutils__namespace.BufferSourceConverter.toUint8Array(inputBuffer); if (!checkBufferParams(this, inputView, inputOffset, inputLength)) { return -1; } const intBuffer = inputView.subarray(inputOffset, inputOffset + inputLength); this.unusedBits = intBuffer[0]; if (this.unusedBits > 7) { this.error = "Unused bits for BitString must be in range 0-7"; return -1; } if (!this.unusedBits) { const buf = intBuffer.subarray(1); try { if (buf.byteLength) { const asn = localFromBER(buf, 0, buf.byteLength); if (asn.offset !== -1 && asn.offset === (inputLength - 1)) { this.value = [asn.result]; } } } catch { } } this.valueHexView = intBuffer.subarray(1); this.blockLength = intBuffer.length; return (inputOffset + inputLength); } toBER(sizeOnly, writer) { if (this.isConstructed) { return LocalConstructedValueBlock.prototype.toBER.call(this, sizeOnly, writer); } if (sizeOnly) { return new ArrayBuffer(this.valueHexView.byteLength + 1); } if (!this.valueHexView.byteLength) { return EMPTY_BUFFER; } const retView = new Uint8Array(this.valueHexView.length + 1); retView[0] = this.unusedBits; retView.set(this.valueHexView, 1); return retView.buffer; } toJSON() { return { ...super.toJSON(), unusedBits: this.unusedBits, isConstructed: this.isConstructed, }; } } LocalBitStringValueBlock.NAME = "BitStringValueBlock"; var _a$q; class BitString extends BaseBlock { constructor({ idBlock = {}, lenBlock = {}, ...parameters } = {}) { var _b, _c; (_b = parameters.isConstructed) !== null && _b !== void 0 ? _b : (parameters.isConstructed = !!((_c = parameters.value) === null || _c === void 0 ? void 0 : _c.length)); super({ idBlock: { isConstructed: parameters.isConstructed, ...idBlock, }, lenBlock: { ...lenBlock, isIndefiniteForm: !!parameters.isIndefiniteForm, }, ...parameters, }, LocalBitStringValueBlock); this.idBlock.tagClass = 1; this.idBlock.tagNumber = 3; } fromBER(inputBuffer, inputOffset, inputLength) { this.valueBlock.isConstructed = this.idBlock.isConstructed; this.valueBlock.isIndefiniteForm = this.lenBlock.isIndefiniteForm; return super.fromBER(inputBuffer, inputOffset, inputLength); } onAsciiEncoding() { if (this.valueBlock.isConstructed || (this.valueBlock.value && this.valueBlock.value.length))