index.js 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266
  1. /*!
  2. * Copyright (c) 2014, GMO GlobalSign
  3. * Copyright (c) 2015-2022, Peculiar Ventures
  4. * All rights reserved.
  5. *
  6. * Author 2014-2019, Yury Strozhevsky
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification,
  9. * are permitted provided that the following conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright notice, this
  12. * list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * * Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from
  20. * this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  26. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  29. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. */
  34. 'use strict';
  35. var pvtsutils = require('pvtsutils');
  36. var pvutils = require('pvutils');
  37. function _interopNamespaceDefault(e) {
  38. var n = Object.create(null);
  39. if (e) {
  40. Object.keys(e).forEach(function (k) {
  41. if (k !== 'default') {
  42. var d = Object.getOwnPropertyDescriptor(e, k);
  43. Object.defineProperty(n, k, d.get ? d : {
  44. enumerable: true,
  45. get: function () { return e[k]; }
  46. });
  47. }
  48. });
  49. }
  50. n.default = e;
  51. return Object.freeze(n);
  52. }
  53. var pvtsutils__namespace = /*#__PURE__*/_interopNamespaceDefault(pvtsutils);
  54. var pvutils__namespace = /*#__PURE__*/_interopNamespaceDefault(pvutils);
  55. function assertBigInt() {
  56. if (typeof BigInt === "undefined") {
  57. throw new Error("BigInt is not defined. Your environment doesn't implement BigInt.");
  58. }
  59. }
  60. function concat(buffers) {
  61. let outputLength = 0;
  62. let prevLength = 0;
  63. for (let i = 0; i < buffers.length; i++) {
  64. const buffer = buffers[i];
  65. outputLength += buffer.byteLength;
  66. }
  67. const retView = new Uint8Array(outputLength);
  68. for (let i = 0; i < buffers.length; i++) {
  69. const buffer = buffers[i];
  70. retView.set(new Uint8Array(buffer), prevLength);
  71. prevLength += buffer.byteLength;
  72. }
  73. return retView.buffer;
  74. }
  75. function checkBufferParams(baseBlock, inputBuffer, inputOffset, inputLength) {
  76. if (!(inputBuffer instanceof Uint8Array)) {
  77. baseBlock.error = "Wrong parameter: inputBuffer must be 'Uint8Array'";
  78. return false;
  79. }
  80. if (!inputBuffer.byteLength) {
  81. baseBlock.error = "Wrong parameter: inputBuffer has zero length";
  82. return false;
  83. }
  84. if (inputOffset < 0) {
  85. baseBlock.error = "Wrong parameter: inputOffset less than zero";
  86. return false;
  87. }
  88. if (inputLength < 0) {
  89. baseBlock.error = "Wrong parameter: inputLength less than zero";
  90. return false;
  91. }
  92. if ((inputBuffer.byteLength - inputOffset - inputLength) < 0) {
  93. baseBlock.error = "End of input reached before message was fully decoded (inconsistent offset and length values)";
  94. return false;
  95. }
  96. return true;
  97. }
  98. class ViewWriter {
  99. constructor() {
  100. this.items = [];
  101. }
  102. write(buf) {
  103. this.items.push(buf);
  104. }
  105. final() {
  106. return concat(this.items);
  107. }
  108. }
  109. const powers2 = [new Uint8Array([1])];
  110. const digitsString = "0123456789";
  111. const NAME = "name";
  112. const VALUE_HEX_VIEW = "valueHexView";
  113. const IS_HEX_ONLY = "isHexOnly";
  114. const ID_BLOCK = "idBlock";
  115. const TAG_CLASS = "tagClass";
  116. const TAG_NUMBER = "tagNumber";
  117. const IS_CONSTRUCTED = "isConstructed";
  118. const FROM_BER = "fromBER";
  119. const TO_BER = "toBER";
  120. const LOCAL = "local";
  121. const EMPTY_STRING = "";
  122. const EMPTY_BUFFER = new ArrayBuffer(0);
  123. const EMPTY_VIEW = new Uint8Array(0);
  124. const END_OF_CONTENT_NAME = "EndOfContent";
  125. const OCTET_STRING_NAME = "OCTET STRING";
  126. const BIT_STRING_NAME = "BIT STRING";
  127. function HexBlock(BaseClass) {
  128. var _a;
  129. return _a = class Some extends BaseClass {
  130. get valueHex() {
  131. return this.valueHexView.slice().buffer;
  132. }
  133. set valueHex(value) {
  134. this.valueHexView = new Uint8Array(value);
  135. }
  136. constructor(...args) {
  137. var _b;
  138. super(...args);
  139. const params = args[0] || {};
  140. this.isHexOnly = (_b = params.isHexOnly) !== null && _b !== void 0 ? _b : false;
  141. this.valueHexView = params.valueHex ? pvtsutils__namespace.BufferSourceConverter.toUint8Array(params.valueHex) : EMPTY_VIEW;
  142. }
  143. fromBER(inputBuffer, inputOffset, inputLength, _context) {
  144. const view = inputBuffer instanceof ArrayBuffer ? new Uint8Array(inputBuffer) : inputBuffer;
  145. if (!checkBufferParams(this, view, inputOffset, inputLength)) {
  146. return -1;
  147. }
  148. const endLength = inputOffset + inputLength;
  149. this.valueHexView = view.subarray(inputOffset, endLength);
  150. if (!this.valueHexView.length) {
  151. this.warnings.push("Zero buffer length");
  152. return inputOffset;
  153. }
  154. this.blockLength = inputLength;
  155. return endLength;
  156. }
  157. toBER(sizeOnly = false) {
  158. if (!this.isHexOnly) {
  159. this.error = "Flag 'isHexOnly' is not set, abort";
  160. return EMPTY_BUFFER;
  161. }
  162. if (sizeOnly) {
  163. return new ArrayBuffer(this.valueHexView.byteLength);
  164. }
  165. return (this.valueHexView.byteLength === this.valueHexView.buffer.byteLength)
  166. ? this.valueHexView.buffer
  167. : this.valueHexView.slice().buffer;
  168. }
  169. toJSON() {
  170. return {
  171. ...super.toJSON(),
  172. isHexOnly: this.isHexOnly,
  173. valueHex: pvtsutils__namespace.Convert.ToHex(this.valueHexView),
  174. };
  175. }
  176. },
  177. _a.NAME = "hexBlock",
  178. _a;
  179. }
  180. class LocalBaseBlock {
  181. static blockName() {
  182. return this.NAME;
  183. }
  184. get valueBeforeDecode() {
  185. return this.valueBeforeDecodeView.slice().buffer;
  186. }
  187. set valueBeforeDecode(value) {
  188. this.valueBeforeDecodeView = new Uint8Array(value);
  189. }
  190. constructor({ blockLength = 0, error = EMPTY_STRING, warnings = [], valueBeforeDecode = EMPTY_VIEW, } = {}) {
  191. this.blockLength = blockLength;
  192. this.error = error;
  193. this.warnings = warnings;
  194. this.valueBeforeDecodeView = pvtsutils__namespace.BufferSourceConverter.toUint8Array(valueBeforeDecode);
  195. }
  196. toJSON() {
  197. return {
  198. blockName: this.constructor.NAME,
  199. blockLength: this.blockLength,
  200. error: this.error,
  201. warnings: this.warnings,
  202. valueBeforeDecode: pvtsutils__namespace.Convert.ToHex(this.valueBeforeDecodeView),
  203. };
  204. }
  205. }
  206. LocalBaseBlock.NAME = "baseBlock";
  207. class ValueBlock extends LocalBaseBlock {
  208. fromBER(_inputBuffer, _inputOffset, _inputLength, _context) {
  209. throw TypeError("User need to make a specific function in a class which extends 'ValueBlock'");
  210. }
  211. toBER(_sizeOnly, _writer) {
  212. throw TypeError("User need to make a specific function in a class which extends 'ValueBlock'");
  213. }
  214. }
  215. ValueBlock.NAME = "valueBlock";
  216. class LocalIdentificationBlock extends HexBlock(LocalBaseBlock) {
  217. constructor({ idBlock = {} } = {}) {
  218. var _a, _b, _c, _d;
  219. super();
  220. if (idBlock) {
  221. this.isHexOnly = (_a = idBlock.isHexOnly) !== null && _a !== void 0 ? _a : false;
  222. this.valueHexView = idBlock.valueHex
  223. ? pvtsutils__namespace.BufferSourceConverter.toUint8Array(idBlock.valueHex)
  224. : EMPTY_VIEW;
  225. this.tagClass = (_b = idBlock.tagClass) !== null && _b !== void 0 ? _b : -1;
  226. this.tagNumber = (_c = idBlock.tagNumber) !== null && _c !== void 0 ? _c : -1;
  227. this.isConstructed = (_d = idBlock.isConstructed) !== null && _d !== void 0 ? _d : false;
  228. }
  229. else {
  230. this.tagClass = -1;
  231. this.tagNumber = -1;
  232. this.isConstructed = false;
  233. }
  234. }
  235. toBER(sizeOnly = false) {
  236. let firstOctet = 0;
  237. switch (this.tagClass) {
  238. case 1:
  239. firstOctet |= 0x00;
  240. break;
  241. case 2:
  242. firstOctet |= 0x40;
  243. break;
  244. case 3:
  245. firstOctet |= 0x80;
  246. break;
  247. case 4:
  248. firstOctet |= 0xC0;
  249. break;
  250. default:
  251. this.error = "Unknown tag class";
  252. return EMPTY_BUFFER;
  253. }
  254. if (this.isConstructed)
  255. firstOctet |= 0x20;
  256. if (this.tagNumber < 31 && !this.isHexOnly) {
  257. const retView = new Uint8Array(1);
  258. if (!sizeOnly) {
  259. let number = this.tagNumber;
  260. number &= 0x1F;
  261. firstOctet |= number;
  262. retView[0] = firstOctet;
  263. }
  264. return retView.buffer;
  265. }
  266. if (!this.isHexOnly) {
  267. const encodedBuf = pvutils__namespace.utilToBase(this.tagNumber, 7);
  268. const encodedView = new Uint8Array(encodedBuf);
  269. const size = encodedBuf.byteLength;
  270. const retView = new Uint8Array(size + 1);
  271. retView[0] = (firstOctet | 0x1F);
  272. if (!sizeOnly) {
  273. for (let i = 0; i < (size - 1); i++)
  274. retView[i + 1] = encodedView[i] | 0x80;
  275. retView[size] = encodedView[size - 1];
  276. }
  277. return retView.buffer;
  278. }
  279. const retView = new Uint8Array(this.valueHexView.byteLength + 1);
  280. retView[0] = (firstOctet | 0x1F);
  281. if (!sizeOnly) {
  282. const curView = this.valueHexView;
  283. for (let i = 0; i < (curView.length - 1); i++)
  284. retView[i + 1] = curView[i] | 0x80;
  285. retView[this.valueHexView.byteLength] = curView[curView.length - 1];
  286. }
  287. return retView.buffer;
  288. }
  289. fromBER(inputBuffer, inputOffset, inputLength) {
  290. const inputView = pvtsutils__namespace.BufferSourceConverter.toUint8Array(inputBuffer);
  291. if (!checkBufferParams(this, inputView, inputOffset, inputLength)) {
  292. return -1;
  293. }
  294. const intBuffer = inputView.subarray(inputOffset, inputOffset + inputLength);
  295. if (intBuffer.length === 0) {
  296. this.error = "Zero buffer length";
  297. return -1;
  298. }
  299. const tagClassMask = intBuffer[0] & 0xC0;
  300. switch (tagClassMask) {
  301. case 0x00:
  302. this.tagClass = (1);
  303. break;
  304. case 0x40:
  305. this.tagClass = (2);
  306. break;
  307. case 0x80:
  308. this.tagClass = (3);
  309. break;
  310. case 0xC0:
  311. this.tagClass = (4);
  312. break;
  313. default:
  314. this.error = "Unknown tag class";
  315. return -1;
  316. }
  317. this.isConstructed = (intBuffer[0] & 0x20) === 0x20;
  318. this.isHexOnly = false;
  319. const tagNumberMask = intBuffer[0] & 0x1F;
  320. if (tagNumberMask !== 0x1F) {
  321. this.tagNumber = (tagNumberMask);
  322. this.blockLength = 1;
  323. }
  324. else {
  325. let count = 0;
  326. while (true) {
  327. const tagByteIndex = count + 1;
  328. if (tagByteIndex >= intBuffer.length) {
  329. this.error = "End of input reached before message was fully decoded";
  330. return -1;
  331. }
  332. count++;
  333. if ((intBuffer[tagByteIndex] & 0x80) === 0x00)
  334. break;
  335. }
  336. this.blockLength = (count + 1);
  337. const intTagNumberBuffer = this.valueHexView = new Uint8Array(count);
  338. for (let i = 0; i < count; i++)
  339. intTagNumberBuffer[i] = intBuffer[i + 1] & 0x7F;
  340. if (this.blockLength <= 9)
  341. this.tagNumber = pvutils__namespace.utilFromBase(intTagNumberBuffer, 7);
  342. else {
  343. this.isHexOnly = true;
  344. this.warnings.push("Tag too long, represented as hex-coded");
  345. }
  346. }
  347. if (((this.tagClass === 1))
  348. && (this.isConstructed)) {
  349. switch (this.tagNumber) {
  350. case 1:
  351. case 2:
  352. case 5:
  353. case 6:
  354. case 9:
  355. case 13:
  356. case 14:
  357. case 23:
  358. case 24:
  359. case 31:
  360. case 32:
  361. case 33:
  362. case 34:
  363. this.error = "Constructed encoding used for primitive type";
  364. return -1;
  365. }
  366. }
  367. return (inputOffset + this.blockLength);
  368. }
  369. toJSON() {
  370. return {
  371. ...super.toJSON(),
  372. tagClass: this.tagClass,
  373. tagNumber: this.tagNumber,
  374. isConstructed: this.isConstructed,
  375. };
  376. }
  377. }
  378. LocalIdentificationBlock.NAME = "identificationBlock";
  379. class LocalLengthBlock extends LocalBaseBlock {
  380. constructor({ lenBlock = {} } = {}) {
  381. var _a, _b, _c;
  382. super();
  383. this.isIndefiniteForm = (_a = lenBlock.isIndefiniteForm) !== null && _a !== void 0 ? _a : false;
  384. this.longFormUsed = (_b = lenBlock.longFormUsed) !== null && _b !== void 0 ? _b : false;
  385. this.length = (_c = lenBlock.length) !== null && _c !== void 0 ? _c : 0;
  386. }
  387. fromBER(inputBuffer, inputOffset, inputLength) {
  388. const view = pvtsutils__namespace.BufferSourceConverter.toUint8Array(inputBuffer);
  389. if (!checkBufferParams(this, view, inputOffset, inputLength)) {
  390. return -1;
  391. }
  392. const intBuffer = view.subarray(inputOffset, inputOffset + inputLength);
  393. if (intBuffer.length === 0) {
  394. this.error = "Zero buffer length";
  395. return -1;
  396. }
  397. if (intBuffer[0] === 0xFF) {
  398. this.error = "Length block 0xFF is reserved by standard";
  399. return -1;
  400. }
  401. this.isIndefiniteForm = intBuffer[0] === 0x80;
  402. if (this.isIndefiniteForm) {
  403. this.blockLength = 1;
  404. return (inputOffset + this.blockLength);
  405. }
  406. this.longFormUsed = !!(intBuffer[0] & 0x80);
  407. if (this.longFormUsed === false) {
  408. this.length = (intBuffer[0]);
  409. this.blockLength = 1;
  410. return (inputOffset + this.blockLength);
  411. }
  412. const count = intBuffer[0] & 0x7F;
  413. if (count > 8) {
  414. this.error = "Too big integer";
  415. return -1;
  416. }
  417. if ((count + 1) > intBuffer.length) {
  418. this.error = "End of input reached before message was fully decoded";
  419. return -1;
  420. }
  421. const lenOffset = inputOffset + 1;
  422. const lengthBufferView = view.subarray(lenOffset, lenOffset + count);
  423. if (lengthBufferView[count - 1] === 0x00)
  424. this.warnings.push("Needlessly long encoded length");
  425. this.length = pvutils__namespace.utilFromBase(lengthBufferView, 8);
  426. if (this.longFormUsed && (this.length <= 127))
  427. this.warnings.push("Unnecessary usage of long length form");
  428. this.blockLength = count + 1;
  429. return (inputOffset + this.blockLength);
  430. }
  431. toBER(sizeOnly = false) {
  432. let retBuf;
  433. let retView;
  434. if (this.length > 127)
  435. this.longFormUsed = true;
  436. if (this.isIndefiniteForm) {
  437. retBuf = new ArrayBuffer(1);
  438. if (sizeOnly === false) {
  439. retView = new Uint8Array(retBuf);
  440. retView[0] = 0x80;
  441. }
  442. return retBuf;
  443. }
  444. if (this.longFormUsed) {
  445. const encodedBuf = pvutils__namespace.utilToBase(this.length, 8);
  446. if (encodedBuf.byteLength > 127) {
  447. this.error = "Too big length";
  448. return (EMPTY_BUFFER);
  449. }
  450. retBuf = new ArrayBuffer(encodedBuf.byteLength + 1);
  451. if (sizeOnly)
  452. return retBuf;
  453. const encodedView = new Uint8Array(encodedBuf);
  454. retView = new Uint8Array(retBuf);
  455. retView[0] = encodedBuf.byteLength | 0x80;
  456. for (let i = 0; i < encodedBuf.byteLength; i++)
  457. retView[i + 1] = encodedView[i];
  458. return retBuf;
  459. }
  460. retBuf = new ArrayBuffer(1);
  461. if (sizeOnly === false) {
  462. retView = new Uint8Array(retBuf);
  463. retView[0] = this.length;
  464. }
  465. return retBuf;
  466. }
  467. toJSON() {
  468. return {
  469. ...super.toJSON(),
  470. isIndefiniteForm: this.isIndefiniteForm,
  471. longFormUsed: this.longFormUsed,
  472. length: this.length,
  473. };
  474. }
  475. }
  476. LocalLengthBlock.NAME = "lengthBlock";
  477. const typeStore = {};
  478. class BaseBlock extends LocalBaseBlock {
  479. constructor({ name = EMPTY_STRING, optional = false, primitiveSchema, ...parameters } = {}, valueBlockType) {
  480. super(parameters);
  481. this.name = name;
  482. this.optional = optional;
  483. if (primitiveSchema) {
  484. this.primitiveSchema = primitiveSchema;
  485. }
  486. this.idBlock = new LocalIdentificationBlock(parameters);
  487. this.lenBlock = new LocalLengthBlock(parameters);
  488. this.valueBlock = valueBlockType ? new valueBlockType(parameters) : new ValueBlock(parameters);
  489. }
  490. fromBER(inputBuffer, inputOffset, inputLength, context) {
  491. const resultOffset = this.valueBlock.fromBER(inputBuffer, inputOffset, (this.lenBlock.isIndefiniteForm)
  492. ? inputLength
  493. : this.lenBlock.length, context);
  494. if (resultOffset === -1) {
  495. this.error = this.valueBlock.error;
  496. return resultOffset;
  497. }
  498. if (!this.idBlock.error.length)
  499. this.blockLength += this.idBlock.blockLength;
  500. if (!this.lenBlock.error.length)
  501. this.blockLength += this.lenBlock.blockLength;
  502. if (!this.valueBlock.error.length)
  503. this.blockLength += this.valueBlock.blockLength;
  504. return resultOffset;
  505. }
  506. toBER(sizeOnly, writer) {
  507. const _writer = writer || new ViewWriter();
  508. if (!writer) {
  509. prepareIndefiniteForm(this);
  510. }
  511. const idBlockBuf = this.idBlock.toBER(sizeOnly);
  512. _writer.write(idBlockBuf);
  513. if (this.lenBlock.isIndefiniteForm) {
  514. _writer.write(new Uint8Array([0x80]).buffer);
  515. this.valueBlock.toBER(sizeOnly, _writer);
  516. _writer.write(new ArrayBuffer(2));
  517. }
  518. else {
  519. const valueBlockBuf = this.valueBlock.toBER(sizeOnly);
  520. this.lenBlock.length = valueBlockBuf.byteLength;
  521. const lenBlockBuf = this.lenBlock.toBER(sizeOnly);
  522. _writer.write(lenBlockBuf);
  523. _writer.write(valueBlockBuf);
  524. }
  525. if (!writer) {
  526. return _writer.final();
  527. }
  528. return EMPTY_BUFFER;
  529. }
  530. toJSON() {
  531. const object = {
  532. ...super.toJSON(),
  533. idBlock: this.idBlock.toJSON(),
  534. lenBlock: this.lenBlock.toJSON(),
  535. valueBlock: this.valueBlock.toJSON(),
  536. name: this.name,
  537. optional: this.optional,
  538. };
  539. if (this.primitiveSchema)
  540. object.primitiveSchema = this.primitiveSchema.toJSON();
  541. return object;
  542. }
  543. toString(encoding = "ascii") {
  544. if (encoding === "ascii") {
  545. return this.onAsciiEncoding();
  546. }
  547. return pvtsutils__namespace.Convert.ToHex(this.toBER());
  548. }
  549. onAsciiEncoding() {
  550. const name = this.constructor.NAME;
  551. const value = pvtsutils__namespace.Convert.ToHex(this.valueBlock.valueBeforeDecodeView);
  552. return `${name} : ${value}`;
  553. }
  554. isEqual(other) {
  555. if (this === other) {
  556. return true;
  557. }
  558. if (!(other instanceof this.constructor)) {
  559. return false;
  560. }
  561. const thisRaw = this.toBER();
  562. const otherRaw = other.toBER();
  563. return pvutils__namespace.isEqualBuffer(thisRaw, otherRaw);
  564. }
  565. }
  566. BaseBlock.NAME = "BaseBlock";
  567. function prepareIndefiniteForm(baseBlock) {
  568. var _a;
  569. if (baseBlock instanceof typeStore.Constructed) {
  570. for (const value of baseBlock.valueBlock.value) {
  571. if (prepareIndefiniteForm(value)) {
  572. baseBlock.lenBlock.isIndefiniteForm = true;
  573. }
  574. }
  575. }
  576. return !!((_a = baseBlock.lenBlock) === null || _a === void 0 ? void 0 : _a.isIndefiniteForm);
  577. }
  578. class BaseStringBlock extends BaseBlock {
  579. getValue() {
  580. return this.valueBlock.value;
  581. }
  582. setValue(value) {
  583. this.valueBlock.value = value;
  584. }
  585. constructor({ value = EMPTY_STRING, ...parameters } = {}, stringValueBlockType) {
  586. super(parameters, stringValueBlockType);
  587. if (value) {
  588. this.fromString(value);
  589. }
  590. }
  591. fromBER(inputBuffer, inputOffset, inputLength) {
  592. const resultOffset = this.valueBlock.fromBER(inputBuffer, inputOffset, (this.lenBlock.isIndefiniteForm)
  593. ? inputLength
  594. : this.lenBlock.length);
  595. if (resultOffset === -1) {
  596. this.error = this.valueBlock.error;
  597. return resultOffset;
  598. }
  599. this.fromBuffer(this.valueBlock.valueHexView);
  600. if (!this.idBlock.error.length)
  601. this.blockLength += this.idBlock.blockLength;
  602. if (!this.lenBlock.error.length)
  603. this.blockLength += this.lenBlock.blockLength;
  604. if (!this.valueBlock.error.length)
  605. this.blockLength += this.valueBlock.blockLength;
  606. return resultOffset;
  607. }
  608. onAsciiEncoding() {
  609. return `${this.constructor.NAME} : '${this.valueBlock.value}'`;
  610. }
  611. }
  612. BaseStringBlock.NAME = "BaseStringBlock";
  613. class LocalPrimitiveValueBlock extends HexBlock(ValueBlock) {
  614. constructor({ isHexOnly = true, ...parameters } = {}) {
  615. super(parameters);
  616. this.isHexOnly = isHexOnly;
  617. }
  618. }
  619. LocalPrimitiveValueBlock.NAME = "PrimitiveValueBlock";
  620. var _a$w;
  621. class Primitive extends BaseBlock {
  622. constructor(parameters = {}) {
  623. super(parameters, LocalPrimitiveValueBlock);
  624. this.idBlock.isConstructed = false;
  625. }
  626. }
  627. _a$w = Primitive;
  628. (() => {
  629. typeStore.Primitive = _a$w;
  630. })();
  631. Primitive.NAME = "PRIMITIVE";
  632. const DEFAULT_MAX_DEPTH = 100;
  633. const DEFAULT_MAX_NODES = 10000;
  634. const DEFAULT_MAX_CONTENT_LENGTH = 16 * 1024 * 1024;
  635. const MAX_DEPTH_EXCEEDED_ERROR = "Maximum ASN.1 nesting depth exceeded";
  636. const MAX_NODES_EXCEEDED_ERROR = "Maximum ASN.1 node count exceeded";
  637. const MAX_CONTENT_LENGTH_EXCEEDED_ERROR = "Maximum ASN.1 content length exceeded";
  638. function createFromBerContext(options = {}) {
  639. var _a, _b, _c;
  640. return {
  641. depth: 0,
  642. maxDepth: (_a = options.maxDepth) !== null && _a !== void 0 ? _a : DEFAULT_MAX_DEPTH,
  643. nodesCount: 0,
  644. maxNodes: (_b = options.maxNodes) !== null && _b !== void 0 ? _b : DEFAULT_MAX_NODES,
  645. maxContentLength: (_c = options.maxContentLength) !== null && _c !== void 0 ? _c : DEFAULT_MAX_CONTENT_LENGTH,
  646. };
  647. }
  648. function createErrorResult(error) {
  649. const result = new BaseBlock({}, ValueBlock);
  650. result.error = error;
  651. return {
  652. offset: -1,
  653. result,
  654. };
  655. }
  656. function checkNodesLimit(context) {
  657. context.nodesCount += 1;
  658. if (context.nodesCount > context.maxNodes) {
  659. return MAX_NODES_EXCEEDED_ERROR;
  660. }
  661. return undefined;
  662. }
  663. function checkContentLengthLimit(inputLength, context) {
  664. if (inputLength > context.maxContentLength) {
  665. return MAX_CONTENT_LENGTH_EXCEEDED_ERROR;
  666. }
  667. return undefined;
  668. }
  669. function localFromBERWithChildContext(inputBuffer, inputOffset, inputLength, context) {
  670. const childDepth = context.depth + 1;
  671. if (childDepth > context.maxDepth) {
  672. return createErrorResult(MAX_DEPTH_EXCEEDED_ERROR);
  673. }
  674. context.depth = childDepth;
  675. try {
  676. return localFromBER(inputBuffer, inputOffset, inputLength, context);
  677. }
  678. finally {
  679. context.depth -= 1;
  680. }
  681. }
  682. function localChangeType(inputObject, newType) {
  683. if (inputObject instanceof newType) {
  684. return inputObject;
  685. }
  686. const newObject = new newType();
  687. newObject.idBlock = inputObject.idBlock;
  688. newObject.lenBlock = inputObject.lenBlock;
  689. newObject.warnings = inputObject.warnings;
  690. newObject.valueBeforeDecodeView = inputObject.valueBeforeDecodeView;
  691. return newObject;
  692. }
  693. function localFromBER(inputBuffer, inputOffset = 0, inputLength = inputBuffer.length, context = createFromBerContext()) {
  694. const incomingOffset = inputOffset;
  695. let returnObject = new BaseBlock({}, ValueBlock);
  696. const baseBlock = new LocalBaseBlock();
  697. if (!checkBufferParams(baseBlock, inputBuffer, inputOffset, inputLength)) {
  698. returnObject.error = baseBlock.error;
  699. return {
  700. offset: -1,
  701. result: returnObject,
  702. };
  703. }
  704. const intBuffer = inputBuffer.subarray(inputOffset, inputOffset + inputLength);
  705. if (!intBuffer.length) {
  706. returnObject.error = "Zero buffer length";
  707. return {
  708. offset: -1,
  709. result: returnObject,
  710. };
  711. }
  712. const nodesLimitError = checkNodesLimit(context);
  713. if (nodesLimitError) {
  714. returnObject.error = nodesLimitError;
  715. return {
  716. offset: -1,
  717. result: returnObject,
  718. };
  719. }
  720. let resultOffset = returnObject.idBlock.fromBER(inputBuffer, inputOffset, inputLength);
  721. if (returnObject.idBlock.warnings.length) {
  722. returnObject.warnings.concat(returnObject.idBlock.warnings);
  723. }
  724. if (resultOffset === -1) {
  725. returnObject.error = returnObject.idBlock.error;
  726. return {
  727. offset: -1,
  728. result: returnObject,
  729. };
  730. }
  731. inputOffset = resultOffset;
  732. inputLength -= returnObject.idBlock.blockLength;
  733. resultOffset = returnObject.lenBlock.fromBER(inputBuffer, inputOffset, inputLength);
  734. if (returnObject.lenBlock.warnings.length) {
  735. returnObject.warnings.concat(returnObject.lenBlock.warnings);
  736. }
  737. if (resultOffset === -1) {
  738. returnObject.error = returnObject.lenBlock.error;
  739. return {
  740. offset: -1,
  741. result: returnObject,
  742. };
  743. }
  744. inputOffset = resultOffset;
  745. inputLength -= returnObject.lenBlock.blockLength;
  746. const valueLength = returnObject.lenBlock.isIndefiniteForm ? inputLength : returnObject.lenBlock.length;
  747. const contentLengthError = checkContentLengthLimit(valueLength, context);
  748. if (contentLengthError) {
  749. returnObject.error = contentLengthError;
  750. return {
  751. offset: -1,
  752. result: returnObject,
  753. };
  754. }
  755. if (!returnObject.idBlock.isConstructed
  756. && returnObject.lenBlock.isIndefiniteForm) {
  757. returnObject.error = "Indefinite length form used for primitive encoding form";
  758. return {
  759. offset: -1,
  760. result: returnObject,
  761. };
  762. }
  763. let newASN1Type = BaseBlock;
  764. switch (returnObject.idBlock.tagClass) {
  765. case 1:
  766. if ((returnObject.idBlock.tagNumber >= 37)
  767. && (returnObject.idBlock.isHexOnly === false)) {
  768. returnObject.error = "UNIVERSAL 37 and upper tags are reserved by ASN.1 standard";
  769. return {
  770. offset: -1,
  771. result: returnObject,
  772. };
  773. }
  774. switch (returnObject.idBlock.tagNumber) {
  775. case 0:
  776. if ((returnObject.idBlock.isConstructed)
  777. && (returnObject.lenBlock.length > 0)) {
  778. returnObject.error = "Type [UNIVERSAL 0] is reserved";
  779. return {
  780. offset: -1,
  781. result: returnObject,
  782. };
  783. }
  784. newASN1Type = typeStore.EndOfContent;
  785. break;
  786. case 1:
  787. newASN1Type = typeStore.Boolean;
  788. break;
  789. case 2:
  790. newASN1Type = typeStore.Integer;
  791. break;
  792. case 3:
  793. newASN1Type = typeStore.BitString;
  794. break;
  795. case 4:
  796. newASN1Type = typeStore.OctetString;
  797. break;
  798. case 5:
  799. newASN1Type = typeStore.Null;
  800. break;
  801. case 6:
  802. newASN1Type = typeStore.ObjectIdentifier;
  803. break;
  804. case 10:
  805. newASN1Type = typeStore.Enumerated;
  806. break;
  807. case 12:
  808. newASN1Type = typeStore.Utf8String;
  809. break;
  810. case 13:
  811. newASN1Type = typeStore.RelativeObjectIdentifier;
  812. break;
  813. case 14:
  814. newASN1Type = typeStore.TIME;
  815. break;
  816. case 15:
  817. returnObject.error = "[UNIVERSAL 15] is reserved by ASN.1 standard";
  818. return {
  819. offset: -1,
  820. result: returnObject,
  821. };
  822. case 16:
  823. newASN1Type = typeStore.Sequence;
  824. break;
  825. case 17:
  826. newASN1Type = typeStore.Set;
  827. break;
  828. case 18:
  829. newASN1Type = typeStore.NumericString;
  830. break;
  831. case 19:
  832. newASN1Type = typeStore.PrintableString;
  833. break;
  834. case 20:
  835. newASN1Type = typeStore.TeletexString;
  836. break;
  837. case 21:
  838. newASN1Type = typeStore.VideotexString;
  839. break;
  840. case 22:
  841. newASN1Type = typeStore.IA5String;
  842. break;
  843. case 23:
  844. newASN1Type = typeStore.UTCTime;
  845. break;
  846. case 24:
  847. newASN1Type = typeStore.GeneralizedTime;
  848. break;
  849. case 25:
  850. newASN1Type = typeStore.GraphicString;
  851. break;
  852. case 26:
  853. newASN1Type = typeStore.VisibleString;
  854. break;
  855. case 27:
  856. newASN1Type = typeStore.GeneralString;
  857. break;
  858. case 28:
  859. newASN1Type = typeStore.UniversalString;
  860. break;
  861. case 29:
  862. newASN1Type = typeStore.CharacterString;
  863. break;
  864. case 30:
  865. newASN1Type = typeStore.BmpString;
  866. break;
  867. case 31:
  868. newASN1Type = typeStore.DATE;
  869. break;
  870. case 32:
  871. newASN1Type = typeStore.TimeOfDay;
  872. break;
  873. case 33:
  874. newASN1Type = typeStore.DateTime;
  875. break;
  876. case 34:
  877. newASN1Type = typeStore.Duration;
  878. break;
  879. default: {
  880. const newObject = returnObject.idBlock.isConstructed
  881. ? new typeStore.Constructed()
  882. : new typeStore.Primitive();
  883. newObject.idBlock = returnObject.idBlock;
  884. newObject.lenBlock = returnObject.lenBlock;
  885. newObject.warnings = returnObject.warnings;
  886. returnObject = newObject;
  887. }
  888. }
  889. break;
  890. case 2:
  891. case 3:
  892. case 4:
  893. default: {
  894. newASN1Type = returnObject.idBlock.isConstructed
  895. ? typeStore.Constructed
  896. : typeStore.Primitive;
  897. }
  898. }
  899. returnObject = localChangeType(returnObject, newASN1Type);
  900. resultOffset = returnObject.fromBER(inputBuffer, inputOffset, valueLength, context);
  901. returnObject.valueBeforeDecodeView = inputBuffer.subarray(incomingOffset, incomingOffset + returnObject.blockLength);
  902. return {
  903. offset: resultOffset,
  904. result: returnObject,
  905. };
  906. }
  907. function fromBER(inputBuffer, options = {}) {
  908. if (!inputBuffer.byteLength) {
  909. const result = new BaseBlock({}, ValueBlock);
  910. result.error = "Input buffer has zero length";
  911. return {
  912. offset: -1,
  913. result,
  914. };
  915. }
  916. return localFromBER(pvtsutils__namespace.BufferSourceConverter.toUint8Array(inputBuffer).slice(), 0, inputBuffer.byteLength, createFromBerContext(options));
  917. }
  918. function checkLen(indefiniteLength, length) {
  919. if (indefiniteLength) {
  920. return 1;
  921. }
  922. return length;
  923. }
  924. class LocalConstructedValueBlock extends ValueBlock {
  925. constructor({ value = [], isIndefiniteForm = false, ...parameters } = {}) {
  926. super(parameters);
  927. this.value = value;
  928. this.isIndefiniteForm = isIndefiniteForm;
  929. }
  930. fromBER(inputBuffer, inputOffset, inputLength, context) {
  931. const view = pvtsutils__namespace.BufferSourceConverter.toUint8Array(inputBuffer);
  932. const parseContext = context !== null && context !== void 0 ? context : createFromBerContext();
  933. if (!checkBufferParams(this, view, inputOffset, inputLength)) {
  934. return -1;
  935. }
  936. this.valueBeforeDecodeView = view.subarray(inputOffset, inputOffset + inputLength);
  937. if (this.valueBeforeDecodeView.length === 0) {
  938. this.warnings.push("Zero buffer length");
  939. return inputOffset;
  940. }
  941. let currentOffset = inputOffset;
  942. while (checkLen(this.isIndefiniteForm, inputLength) > 0) {
  943. const returnObject = localFromBERWithChildContext(view, currentOffset, inputLength, parseContext);
  944. if (returnObject.offset === -1) {
  945. this.error = returnObject.result.error;
  946. this.warnings.concat(returnObject.result.warnings);
  947. return -1;
  948. }
  949. currentOffset = returnObject.offset;
  950. this.blockLength += returnObject.result.blockLength;
  951. inputLength -= returnObject.result.blockLength;
  952. this.value.push(returnObject.result);
  953. if (this.isIndefiniteForm && returnObject.result.constructor.NAME === END_OF_CONTENT_NAME) {
  954. break;
  955. }
  956. }
  957. if (this.isIndefiniteForm) {
  958. if (this.value[this.value.length - 1].constructor.NAME === END_OF_CONTENT_NAME) {
  959. this.value.pop();
  960. }
  961. else {
  962. this.warnings.push("No EndOfContent block encoded");
  963. }
  964. }
  965. return currentOffset;
  966. }
  967. toBER(sizeOnly, writer) {
  968. const _writer = writer || new ViewWriter();
  969. for (let i = 0; i < this.value.length; i++) {
  970. this.value[i].toBER(sizeOnly, _writer);
  971. }
  972. if (!writer) {
  973. return _writer.final();
  974. }
  975. return EMPTY_BUFFER;
  976. }
  977. toJSON() {
  978. const object = {
  979. ...super.toJSON(),
  980. isIndefiniteForm: this.isIndefiniteForm,
  981. value: [],
  982. };
  983. for (const value of this.value) {
  984. object.value.push(value.toJSON());
  985. }
  986. return object;
  987. }
  988. }
  989. LocalConstructedValueBlock.NAME = "ConstructedValueBlock";
  990. var _a$v;
  991. class Constructed extends BaseBlock {
  992. constructor(parameters = {}) {
  993. super(parameters, LocalConstructedValueBlock);
  994. this.idBlock.isConstructed = true;
  995. }
  996. fromBER(inputBuffer, inputOffset, inputLength, context) {
  997. this.valueBlock.isIndefiniteForm = this.lenBlock.isIndefiniteForm;
  998. const resultOffset = this.valueBlock.fromBER(inputBuffer, inputOffset, (this.lenBlock.isIndefiniteForm) ? inputLength : this.lenBlock.length, context);
  999. if (resultOffset === -1) {
  1000. this.error = this.valueBlock.error;
  1001. return resultOffset;
  1002. }
  1003. if (!this.idBlock.error.length)
  1004. this.blockLength += this.idBlock.blockLength;
  1005. if (!this.lenBlock.error.length)
  1006. this.blockLength += this.lenBlock.blockLength;
  1007. if (!this.valueBlock.error.length)
  1008. this.blockLength += this.valueBlock.blockLength;
  1009. return resultOffset;
  1010. }
  1011. onAsciiEncoding() {
  1012. const values = [];
  1013. for (const value of this.valueBlock.value) {
  1014. values.push(value.toString("ascii").split("\n").map((o) => ` ${o}`).join("\n"));
  1015. }
  1016. const blockName = this.idBlock.tagClass === 3
  1017. ? `[${this.idBlock.tagNumber}]`
  1018. : this.constructor.NAME;
  1019. return values.length
  1020. ? `${blockName} :\n${values.join("\n")}`
  1021. : `${blockName} :`;
  1022. }
  1023. }
  1024. _a$v = Constructed;
  1025. (() => {
  1026. typeStore.Constructed = _a$v;
  1027. })();
  1028. Constructed.NAME = "CONSTRUCTED";
  1029. class LocalEndOfContentValueBlock extends ValueBlock {
  1030. fromBER(inputBuffer, inputOffset, _inputLength) {
  1031. return inputOffset;
  1032. }
  1033. toBER(_sizeOnly) {
  1034. return EMPTY_BUFFER;
  1035. }
  1036. }
  1037. LocalEndOfContentValueBlock.override = "EndOfContentValueBlock";
  1038. var _a$u;
  1039. class EndOfContent extends BaseBlock {
  1040. constructor(parameters = {}) {
  1041. super(parameters, LocalEndOfContentValueBlock);
  1042. this.idBlock.tagClass = 1;
  1043. this.idBlock.tagNumber = 0;
  1044. }
  1045. }
  1046. _a$u = EndOfContent;
  1047. (() => {
  1048. typeStore.EndOfContent = _a$u;
  1049. })();
  1050. EndOfContent.NAME = END_OF_CONTENT_NAME;
  1051. var _a$t;
  1052. class Null extends BaseBlock {
  1053. constructor(parameters = {}) {
  1054. super(parameters, ValueBlock);
  1055. this.idBlock.tagClass = 1;
  1056. this.idBlock.tagNumber = 5;
  1057. }
  1058. fromBER(inputBuffer, inputOffset, inputLength) {
  1059. if (this.lenBlock.length > 0)
  1060. this.warnings.push("Non-zero length of value block for Null type");
  1061. if (!this.idBlock.error.length)
  1062. this.blockLength += this.idBlock.blockLength;
  1063. if (!this.lenBlock.error.length)
  1064. this.blockLength += this.lenBlock.blockLength;
  1065. this.blockLength += inputLength;
  1066. if ((inputOffset + inputLength) > inputBuffer.byteLength) {
  1067. this.error = "End of input reached before message was fully decoded (inconsistent offset and length values)";
  1068. return -1;
  1069. }
  1070. return (inputOffset + inputLength);
  1071. }
  1072. toBER(sizeOnly, writer) {
  1073. const retBuf = new ArrayBuffer(2);
  1074. if (!sizeOnly) {
  1075. const retView = new Uint8Array(retBuf);
  1076. retView[0] = 0x05;
  1077. retView[1] = 0x00;
  1078. }
  1079. if (writer) {
  1080. writer.write(retBuf);
  1081. }
  1082. return retBuf;
  1083. }
  1084. onAsciiEncoding() {
  1085. return `${this.constructor.NAME}`;
  1086. }
  1087. }
  1088. _a$t = Null;
  1089. (() => {
  1090. typeStore.Null = _a$t;
  1091. })();
  1092. Null.NAME = "NULL";
  1093. class LocalBooleanValueBlock extends HexBlock(ValueBlock) {
  1094. get value() {
  1095. for (const octet of this.valueHexView) {
  1096. if (octet > 0) {
  1097. return true;
  1098. }
  1099. }
  1100. return false;
  1101. }
  1102. set value(value) {
  1103. this.valueHexView[0] = value ? 0xFF : 0x00;
  1104. }
  1105. constructor({ value, ...parameters } = {}) {
  1106. super(parameters);
  1107. if (parameters.valueHex) {
  1108. this.valueHexView = pvtsutils__namespace.BufferSourceConverter.toUint8Array(parameters.valueHex);
  1109. }
  1110. else {
  1111. this.valueHexView = new Uint8Array(1);
  1112. }
  1113. if (value) {
  1114. this.value = value;
  1115. }
  1116. }
  1117. fromBER(inputBuffer, inputOffset, inputLength) {
  1118. const inputView = pvtsutils__namespace.BufferSourceConverter.toUint8Array(inputBuffer);
  1119. if (!checkBufferParams(this, inputView, inputOffset, inputLength)) {
  1120. return -1;
  1121. }
  1122. this.valueHexView = inputView.subarray(inputOffset, inputOffset + inputLength);
  1123. if (inputLength > 1)
  1124. this.warnings.push("Boolean value encoded in more then 1 octet");
  1125. this.isHexOnly = true;
  1126. pvutils__namespace.utilDecodeTC.call(this);
  1127. this.blockLength = inputLength;
  1128. return (inputOffset + inputLength);
  1129. }
  1130. toBER() {
  1131. return this.valueHexView.slice();
  1132. }
  1133. toJSON() {
  1134. return {
  1135. ...super.toJSON(),
  1136. value: this.value,
  1137. };
  1138. }
  1139. }
  1140. LocalBooleanValueBlock.NAME = "BooleanValueBlock";
  1141. var _a$s;
  1142. class Boolean extends BaseBlock {
  1143. getValue() {
  1144. return this.valueBlock.value;
  1145. }
  1146. setValue(value) {
  1147. this.valueBlock.value = value;
  1148. }
  1149. constructor(parameters = {}) {
  1150. super(parameters, LocalBooleanValueBlock);
  1151. this.idBlock.tagClass = 1;
  1152. this.idBlock.tagNumber = 1;
  1153. }
  1154. onAsciiEncoding() {
  1155. return `${this.constructor.NAME} : ${this.getValue}`;
  1156. }
  1157. }
  1158. _a$s = Boolean;
  1159. (() => {
  1160. typeStore.Boolean = _a$s;
  1161. })();
  1162. Boolean.NAME = "BOOLEAN";
  1163. class LocalOctetStringValueBlock extends HexBlock(LocalConstructedValueBlock) {
  1164. constructor({ isConstructed = false, ...parameters } = {}) {
  1165. super(parameters);
  1166. this.isConstructed = isConstructed;
  1167. }
  1168. fromBER(inputBuffer, inputOffset, inputLength, context) {
  1169. let resultOffset = 0;
  1170. if (this.isConstructed) {
  1171. this.isHexOnly = false;
  1172. resultOffset = LocalConstructedValueBlock.prototype.fromBER.call(this, inputBuffer, inputOffset, inputLength, context);
  1173. if (resultOffset === -1)
  1174. return resultOffset;
  1175. for (let i = 0; i < this.value.length; i++) {
  1176. const currentBlockName = this.value[i].constructor.NAME;
  1177. if (currentBlockName === END_OF_CONTENT_NAME) {
  1178. if (this.isIndefiniteForm)
  1179. break;
  1180. else {
  1181. this.error = "EndOfContent is unexpected, OCTET STRING may consists of OCTET STRINGs only";
  1182. return -1;
  1183. }
  1184. }
  1185. if (currentBlockName !== OCTET_STRING_NAME) {
  1186. this.error = "OCTET STRING may consists of OCTET STRINGs only";
  1187. return -1;
  1188. }
  1189. }
  1190. }
  1191. else {
  1192. this.isHexOnly = true;
  1193. resultOffset = super.fromBER(inputBuffer, inputOffset, inputLength);
  1194. this.blockLength = inputLength;
  1195. }
  1196. return resultOffset;
  1197. }
  1198. toBER(sizeOnly, writer) {
  1199. if (this.isConstructed)
  1200. return LocalConstructedValueBlock.prototype.toBER.call(this, sizeOnly, writer);
  1201. return sizeOnly
  1202. ? new ArrayBuffer(this.valueHexView.byteLength)
  1203. : this.valueHexView.slice().buffer;
  1204. }
  1205. toJSON() {
  1206. return {
  1207. ...super.toJSON(),
  1208. isConstructed: this.isConstructed,
  1209. };
  1210. }
  1211. }
  1212. LocalOctetStringValueBlock.NAME = "OctetStringValueBlock";
  1213. var _a$r;
  1214. class OctetString extends BaseBlock {
  1215. constructor({ idBlock = {}, lenBlock = {}, ...parameters } = {}) {
  1216. var _b, _c;
  1217. (_b = parameters.isConstructed) !== null && _b !== void 0 ? _b : (parameters.isConstructed = !!((_c = parameters.value) === null || _c === void 0 ? void 0 : _c.length));
  1218. super({
  1219. idBlock: {
  1220. isConstructed: parameters.isConstructed,
  1221. ...idBlock,
  1222. },
  1223. lenBlock: {
  1224. ...lenBlock,
  1225. isIndefiniteForm: !!parameters.isIndefiniteForm,
  1226. },
  1227. ...parameters,
  1228. }, LocalOctetStringValueBlock);
  1229. this.idBlock.tagClass = 1;
  1230. this.idBlock.tagNumber = 4;
  1231. }
  1232. fromBER(inputBuffer, inputOffset, inputLength, context) {
  1233. this.valueBlock.isConstructed = this.idBlock.isConstructed;
  1234. this.valueBlock.isIndefiniteForm = this.lenBlock.isIndefiniteForm;
  1235. if (inputLength === 0) {
  1236. if (this.idBlock.error.length === 0)
  1237. this.blockLength += this.idBlock.blockLength;
  1238. if (this.lenBlock.error.length === 0)
  1239. this.blockLength += this.lenBlock.blockLength;
  1240. return inputOffset;
  1241. }
  1242. if (!this.valueBlock.isConstructed) {
  1243. const view = inputBuffer instanceof ArrayBuffer ? new Uint8Array(inputBuffer) : inputBuffer;
  1244. const buf = view.subarray(inputOffset, inputOffset + inputLength);
  1245. try {
  1246. if (buf.byteLength) {
  1247. const parseContext = context !== null && context !== void 0 ? context : createFromBerContext();
  1248. const asn = localFromBERWithChildContext(buf, 0, buf.byteLength, parseContext);
  1249. if (asn.offset !== -1 && asn.offset === inputLength) {
  1250. this.valueBlock.value = [asn.result];
  1251. }
  1252. }
  1253. }
  1254. catch {
  1255. }
  1256. }
  1257. return super.fromBER(inputBuffer, inputOffset, inputLength, context);
  1258. }
  1259. onAsciiEncoding() {
  1260. if (this.valueBlock.isConstructed || (this.valueBlock.value && this.valueBlock.value.length)) {
  1261. return Constructed.prototype.onAsciiEncoding.call(this);
  1262. }
  1263. const name = this.constructor.NAME;
  1264. const value = pvtsutils__namespace.Convert.ToHex(this.valueBlock.valueHexView);
  1265. return `${name} : ${value}`;
  1266. }
  1267. getValue() {
  1268. if (!this.idBlock.isConstructed) {
  1269. return this.valueBlock.valueHexView.slice().buffer;
  1270. }
  1271. const array = [];
  1272. for (const content of this.valueBlock.value) {
  1273. if (content instanceof _a$r) {
  1274. array.push(content.valueBlock.valueHexView);
  1275. }
  1276. }
  1277. return pvtsutils__namespace.BufferSourceConverter.concat(array);
  1278. }
  1279. }
  1280. _a$r = OctetString;
  1281. (() => {
  1282. typeStore.OctetString = _a$r;
  1283. })();
  1284. OctetString.NAME = OCTET_STRING_NAME;
  1285. class LocalBitStringValueBlock extends HexBlock(LocalConstructedValueBlock) {
  1286. constructor({ unusedBits = 0, isConstructed = false, ...parameters } = {}) {
  1287. super(parameters);
  1288. this.unusedBits = unusedBits;
  1289. this.isConstructed = isConstructed;
  1290. this.blockLength = this.valueHexView.byteLength;
  1291. }
  1292. fromBER(inputBuffer, inputOffset, inputLength, context) {
  1293. if (!inputLength) {
  1294. return inputOffset;
  1295. }
  1296. let resultOffset = -1;
  1297. if (this.isConstructed) {
  1298. resultOffset = LocalConstructedValueBlock.prototype.fromBER.call(this, inputBuffer, inputOffset, inputLength, context);
  1299. if (resultOffset === -1)
  1300. return resultOffset;
  1301. for (const value of this.value) {
  1302. const currentBlockName = value.constructor.NAME;
  1303. if (currentBlockName === END_OF_CONTENT_NAME) {
  1304. if (this.isIndefiniteForm)
  1305. break;
  1306. else {
  1307. this.error = "EndOfContent is unexpected, BIT STRING may consists of BIT STRINGs only";
  1308. return -1;
  1309. }
  1310. }
  1311. if (currentBlockName !== BIT_STRING_NAME) {
  1312. this.error = "BIT STRING may consists of BIT STRINGs only";
  1313. return -1;
  1314. }
  1315. const valueBlock = value.valueBlock;
  1316. if ((this.unusedBits > 0) && (valueBlock.unusedBits > 0)) {
  1317. this.error = "Using of \"unused bits\" inside constructive BIT STRING allowed for least one only";
  1318. return -1;
  1319. }
  1320. this.unusedBits = valueBlock.unusedBits;
  1321. }
  1322. return resultOffset;
  1323. }
  1324. const inputView = pvtsutils__namespace.BufferSourceConverter.toUint8Array(inputBuffer);
  1325. if (!checkBufferParams(this, inputView, inputOffset, inputLength)) {
  1326. return -1;
  1327. }
  1328. const intBuffer = inputView.subarray(inputOffset, inputOffset + inputLength);
  1329. this.unusedBits = intBuffer[0];
  1330. if (this.unusedBits > 7) {
  1331. this.error = "Unused bits for BitString must be in range 0-7";
  1332. return -1;
  1333. }
  1334. if (!this.unusedBits) {
  1335. const buf = intBuffer.subarray(1);
  1336. try {
  1337. if (buf.byteLength) {
  1338. const parseContext = context !== null && context !== void 0 ? context : createFromBerContext();
  1339. const asn = localFromBERWithChildContext(buf, 0, buf.byteLength, parseContext);
  1340. if (asn.offset !== -1 && asn.offset === (inputLength - 1)) {
  1341. this.value = [asn.result];
  1342. }
  1343. }
  1344. }
  1345. catch {
  1346. }
  1347. }
  1348. this.valueHexView = intBuffer.subarray(1);
  1349. this.blockLength = intBuffer.length;
  1350. return (inputOffset + inputLength);
  1351. }
  1352. toBER(sizeOnly, writer) {
  1353. if (this.isConstructed) {
  1354. return LocalConstructedValueBlock.prototype.toBER.call(this, sizeOnly, writer);
  1355. }
  1356. if (sizeOnly) {
  1357. return new ArrayBuffer(this.valueHexView.byteLength + 1);
  1358. }
  1359. if (!this.valueHexView.byteLength) {
  1360. const empty = new Uint8Array(1);
  1361. empty[0] = 0;
  1362. return empty.buffer;
  1363. }
  1364. const retView = new Uint8Array(this.valueHexView.length + 1);
  1365. retView[0] = this.unusedBits;
  1366. retView.set(this.valueHexView, 1);
  1367. return retView.buffer;
  1368. }
  1369. toJSON() {
  1370. return {
  1371. ...super.toJSON(),
  1372. unusedBits: this.unusedBits,
  1373. isConstructed: this.isConstructed,
  1374. };
  1375. }
  1376. }
  1377. LocalBitStringValueBlock.NAME = "BitStringValueBlock";
  1378. var _a$q;
  1379. class BitString extends BaseBlock {
  1380. constructor({ idBlock = {}, lenBlock = {}, ...parameters } = {}) {
  1381. var _b, _c;
  1382. (_b = parameters.isConstructed) !== null && _b !== void 0 ? _b : (parameters.isConstructed = !!((_c = parameters.value) === null || _c === void 0 ? void 0 : _c.length));
  1383. super({
  1384. idBlock: {
  1385. isConstructed: parameters.isConstructed,
  1386. ...idBlock,
  1387. },
  1388. lenBlock: {
  1389. ...lenBlock,
  1390. isIndefiniteForm: !!parameters.isIndefiniteForm,
  1391. },
  1392. ...parameters,
  1393. }, LocalBitStringValueBlock);
  1394. this.idBlock.tagClass = 1;
  1395. this.idBlock.tagNumber = 3;
  1396. }
  1397. fromBER(inputBuffer, inputOffset, inputLength, context) {
  1398. this.valueBlock.isConstructed = this.idBlock.isConstructed;
  1399. this.valueBlock.isIndefiniteForm = this.lenBlock.isIndefiniteForm;
  1400. return super.fromBER(inputBuffer, inputOffset, inputLength, context);
  1401. }
  1402. onAsciiEncoding() {
  1403. if (this.valueBlock.isConstructed || (this.valueBlock.value && this.valueBlock.value.length)) {
  1404. return Constructed.prototype.onAsciiEncoding.call(this);
  1405. }
  1406. else {
  1407. const bits = [];
  1408. const valueHex = this.valueBlock.valueHexView;
  1409. for (const byte of valueHex) {
  1410. bits.push(byte.toString(2).padStart(8, "0"));
  1411. }
  1412. const bitsStr = bits.join("");
  1413. const name = this.constructor.NAME;
  1414. const value = bitsStr.substring(0, bitsStr.length - this.valueBlock.unusedBits);
  1415. return `${name} : ${value}`;
  1416. }
  1417. }
  1418. }
  1419. _a$q = BitString;
  1420. (() => {
  1421. typeStore.BitString = _a$q;
  1422. })();
  1423. BitString.NAME = BIT_STRING_NAME;
  1424. var _a$p;
  1425. function viewAdd(first, second) {
  1426. const c = new Uint8Array([0]);
  1427. const firstView = new Uint8Array(first);
  1428. const secondView = new Uint8Array(second);
  1429. let firstViewCopy = firstView.slice(0);
  1430. const firstViewCopyLength = firstViewCopy.length - 1;
  1431. const secondViewCopy = secondView.slice(0);
  1432. const secondViewCopyLength = secondViewCopy.length - 1;
  1433. let value = 0;
  1434. const max = (secondViewCopyLength < firstViewCopyLength) ? firstViewCopyLength : secondViewCopyLength;
  1435. let counter = 0;
  1436. for (let i = max; i >= 0; i--, counter++) {
  1437. switch (true) {
  1438. case (counter < secondViewCopy.length):
  1439. value = firstViewCopy[firstViewCopyLength - counter] + secondViewCopy[secondViewCopyLength - counter] + c[0];
  1440. break;
  1441. default:
  1442. value = firstViewCopy[firstViewCopyLength - counter] + c[0];
  1443. }
  1444. c[0] = value / 10;
  1445. switch (true) {
  1446. case (counter >= firstViewCopy.length):
  1447. firstViewCopy = pvutils__namespace.utilConcatView(new Uint8Array([value % 10]), firstViewCopy);
  1448. break;
  1449. default:
  1450. firstViewCopy[firstViewCopyLength - counter] = value % 10;
  1451. }
  1452. }
  1453. if (c[0] > 0)
  1454. firstViewCopy = pvutils__namespace.utilConcatView(c, firstViewCopy);
  1455. return firstViewCopy;
  1456. }
  1457. function power2(n) {
  1458. if (n >= powers2.length) {
  1459. for (let p = powers2.length; p <= n; p++) {
  1460. const c = new Uint8Array([0]);
  1461. let digits = (powers2[p - 1]).slice(0);
  1462. for (let i = (digits.length - 1); i >= 0; i--) {
  1463. const newValue = new Uint8Array([(digits[i] << 1) + c[0]]);
  1464. c[0] = newValue[0] / 10;
  1465. digits[i] = newValue[0] % 10;
  1466. }
  1467. if (c[0] > 0)
  1468. digits = pvutils__namespace.utilConcatView(c, digits);
  1469. powers2.push(digits);
  1470. }
  1471. }
  1472. return powers2[n];
  1473. }
  1474. function viewSub(first, second) {
  1475. let b = 0;
  1476. const firstView = new Uint8Array(first);
  1477. const secondView = new Uint8Array(second);
  1478. const firstViewCopy = firstView.slice(0);
  1479. const firstViewCopyLength = firstViewCopy.length - 1;
  1480. const secondViewCopy = secondView.slice(0);
  1481. const secondViewCopyLength = secondViewCopy.length - 1;
  1482. let value;
  1483. let counter = 0;
  1484. for (let i = secondViewCopyLength; i >= 0; i--, counter++) {
  1485. value = firstViewCopy[firstViewCopyLength - counter] - secondViewCopy[secondViewCopyLength - counter] - b;
  1486. switch (true) {
  1487. case (value < 0):
  1488. b = 1;
  1489. firstViewCopy[firstViewCopyLength - counter] = value + 10;
  1490. break;
  1491. default:
  1492. b = 0;
  1493. firstViewCopy[firstViewCopyLength - counter] = value;
  1494. }
  1495. }
  1496. if (b > 0) {
  1497. for (let i = (firstViewCopyLength - secondViewCopyLength + 1); i >= 0; i--, counter++) {
  1498. value = firstViewCopy[firstViewCopyLength - counter] - b;
  1499. if (value < 0) {
  1500. b = 1;
  1501. firstViewCopy[firstViewCopyLength - counter] = value + 10;
  1502. }
  1503. else {
  1504. b = 0;
  1505. firstViewCopy[firstViewCopyLength - counter] = value;
  1506. break;
  1507. }
  1508. }
  1509. }
  1510. return firstViewCopy.slice();
  1511. }
  1512. class LocalIntegerValueBlock extends HexBlock(ValueBlock) {
  1513. setValueHex() {
  1514. if (this.valueHexView.length >= 4) {
  1515. this.warnings.push("Too big Integer for decoding, hex only");
  1516. this.isHexOnly = true;
  1517. this._valueDec = 0;
  1518. }
  1519. else {
  1520. this.isHexOnly = false;
  1521. if (this.valueHexView.length > 0) {
  1522. this._valueDec = pvutils__namespace.utilDecodeTC.call(this);
  1523. }
  1524. }
  1525. }
  1526. constructor({ value, ...parameters } = {}) {
  1527. super(parameters);
  1528. this._valueDec = 0;
  1529. if (parameters.valueHex) {
  1530. this.setValueHex();
  1531. }
  1532. if (value !== undefined) {
  1533. this.valueDec = value;
  1534. }
  1535. }
  1536. set valueDec(v) {
  1537. this._valueDec = v;
  1538. this.isHexOnly = false;
  1539. this.valueHexView = new Uint8Array(pvutils__namespace.utilEncodeTC(v));
  1540. }
  1541. get valueDec() {
  1542. return this._valueDec;
  1543. }
  1544. fromDER(inputBuffer, inputOffset, inputLength, expectedLength = 0) {
  1545. const offset = this.fromBER(inputBuffer, inputOffset, inputLength);
  1546. if (offset === -1)
  1547. return offset;
  1548. const view = this.valueHexView;
  1549. if ((view[0] === 0x00) && ((view[1] & 0x80) !== 0)) {
  1550. this.valueHexView = view.subarray(1);
  1551. }
  1552. else {
  1553. if (expectedLength !== 0) {
  1554. if (view.length < expectedLength) {
  1555. if ((expectedLength - view.length) > 1)
  1556. expectedLength = view.length + 1;
  1557. this.valueHexView = view.subarray(expectedLength - view.length);
  1558. }
  1559. }
  1560. }
  1561. return offset;
  1562. }
  1563. toDER(sizeOnly = false) {
  1564. const view = this.valueHexView;
  1565. switch (true) {
  1566. case ((view[0] & 0x80) !== 0):
  1567. {
  1568. const updatedView = new Uint8Array(this.valueHexView.length + 1);
  1569. updatedView[0] = 0x00;
  1570. updatedView.set(view, 1);
  1571. this.valueHexView = updatedView;
  1572. }
  1573. break;
  1574. case ((view[0] === 0x00) && ((view[1] & 0x80) === 0)):
  1575. {
  1576. this.valueHexView = this.valueHexView.subarray(1);
  1577. }
  1578. break;
  1579. }
  1580. return this.toBER(sizeOnly);
  1581. }
  1582. fromBER(inputBuffer, inputOffset, inputLength) {
  1583. const resultOffset = super.fromBER(inputBuffer, inputOffset, inputLength);
  1584. if (resultOffset === -1) {
  1585. return resultOffset;
  1586. }
  1587. this.setValueHex();
  1588. return resultOffset;
  1589. }
  1590. toBER(sizeOnly) {
  1591. return sizeOnly
  1592. ? new ArrayBuffer(this.valueHexView.length)
  1593. : this.valueHexView.slice().buffer;
  1594. }
  1595. toJSON() {
  1596. return {
  1597. ...super.toJSON(),
  1598. valueDec: this.valueDec,
  1599. };
  1600. }
  1601. toString() {
  1602. const firstBit = (this.valueHexView.length * 8) - 1;
  1603. let digits = new Uint8Array((this.valueHexView.length * 8) / 3);
  1604. let bitNumber = 0;
  1605. let currentByte;
  1606. const asn1View = this.valueHexView;
  1607. let result = "";
  1608. let flag = false;
  1609. for (let byteNumber = (asn1View.byteLength - 1); byteNumber >= 0; byteNumber--) {
  1610. currentByte = asn1View[byteNumber];
  1611. for (let i = 0; i < 8; i++) {
  1612. if ((currentByte & 1) === 1) {
  1613. switch (bitNumber) {
  1614. case firstBit:
  1615. digits = viewSub(power2(bitNumber), digits);
  1616. result = "-";
  1617. break;
  1618. default:
  1619. digits = viewAdd(digits, power2(bitNumber));
  1620. }
  1621. }
  1622. bitNumber++;
  1623. currentByte >>= 1;
  1624. }
  1625. }
  1626. for (let i = 0; i < digits.length; i++) {
  1627. if (digits[i])
  1628. flag = true;
  1629. if (flag)
  1630. result += digitsString.charAt(digits[i]);
  1631. }
  1632. if (flag === false)
  1633. result += digitsString.charAt(0);
  1634. return result;
  1635. }
  1636. }
  1637. _a$p = LocalIntegerValueBlock;
  1638. LocalIntegerValueBlock.NAME = "IntegerValueBlock";
  1639. (() => {
  1640. Object.defineProperty(_a$p.prototype, "valueHex", {
  1641. set: function (v) {
  1642. this.valueHexView = new Uint8Array(v);
  1643. this.setValueHex();
  1644. },
  1645. get: function () {
  1646. return this.valueHexView.slice().buffer;
  1647. },
  1648. });
  1649. })();
  1650. var _a$o;
  1651. class Integer extends BaseBlock {
  1652. constructor(parameters = {}) {
  1653. super(parameters, LocalIntegerValueBlock);
  1654. this.idBlock.tagClass = 1;
  1655. this.idBlock.tagNumber = 2;
  1656. }
  1657. toBigInt() {
  1658. assertBigInt();
  1659. return BigInt(this.valueBlock.toString());
  1660. }
  1661. static fromBigInt(value) {
  1662. assertBigInt();
  1663. const bigIntValue = BigInt(value);
  1664. const writer = new ViewWriter();
  1665. const hex = bigIntValue.toString(16).replace(/^-/, "");
  1666. const view = new Uint8Array(pvtsutils__namespace.Convert.FromHex(hex));
  1667. if (bigIntValue < 0) {
  1668. const first = new Uint8Array(view.length + (view[0] & 0x80 ? 1 : 0));
  1669. first[0] |= 0x80;
  1670. const firstInt = BigInt(`0x${pvtsutils__namespace.Convert.ToHex(first)}`);
  1671. const secondInt = firstInt + bigIntValue;
  1672. const second = pvtsutils__namespace.BufferSourceConverter.toUint8Array(pvtsutils__namespace.Convert.FromHex(secondInt.toString(16)));
  1673. second[0] |= 0x80;
  1674. writer.write(second);
  1675. }
  1676. else {
  1677. if (view[0] & 0x80) {
  1678. writer.write(new Uint8Array([0]));
  1679. }
  1680. writer.write(view);
  1681. }
  1682. const res = new _a$o({ valueHex: writer.final() });
  1683. return res;
  1684. }
  1685. convertToDER() {
  1686. const integer = new _a$o({ valueHex: this.valueBlock.valueHexView });
  1687. integer.valueBlock.toDER();
  1688. return integer;
  1689. }
  1690. convertFromDER() {
  1691. return new _a$o({
  1692. valueHex: this.valueBlock.valueHexView[0] === 0
  1693. ? this.valueBlock.valueHexView.subarray(1)
  1694. : this.valueBlock.valueHexView,
  1695. });
  1696. }
  1697. onAsciiEncoding() {
  1698. return `${this.constructor.NAME} : ${this.valueBlock.toString()}`;
  1699. }
  1700. }
  1701. _a$o = Integer;
  1702. (() => {
  1703. typeStore.Integer = _a$o;
  1704. })();
  1705. Integer.NAME = "INTEGER";
  1706. var _a$n;
  1707. class Enumerated extends Integer {
  1708. constructor(parameters = {}) {
  1709. super(parameters);
  1710. this.idBlock.tagClass = 1;
  1711. this.idBlock.tagNumber = 10;
  1712. }
  1713. }
  1714. _a$n = Enumerated;
  1715. (() => {
  1716. typeStore.Enumerated = _a$n;
  1717. })();
  1718. Enumerated.NAME = "ENUMERATED";
  1719. class LocalSidValueBlock extends HexBlock(ValueBlock) {
  1720. constructor({ valueDec = -1, isFirstSid = false, ...parameters } = {}) {
  1721. super(parameters);
  1722. this.valueDec = valueDec;
  1723. this.isFirstSid = isFirstSid;
  1724. }
  1725. fromBER(inputBuffer, inputOffset, inputLength) {
  1726. if (!inputLength) {
  1727. return inputOffset;
  1728. }
  1729. const inputView = pvtsutils__namespace.BufferSourceConverter.toUint8Array(inputBuffer);
  1730. if (!checkBufferParams(this, inputView, inputOffset, inputLength)) {
  1731. return -1;
  1732. }
  1733. const intBuffer = inputView.subarray(inputOffset, inputOffset + inputLength);
  1734. this.valueHexView = new Uint8Array(inputLength);
  1735. for (let i = 0; i < inputLength; i++) {
  1736. this.valueHexView[i] = intBuffer[i] & 0x7F;
  1737. this.blockLength++;
  1738. if ((intBuffer[i] & 0x80) === 0x00)
  1739. break;
  1740. }
  1741. const tempView = new Uint8Array(this.blockLength);
  1742. for (let i = 0; i < this.blockLength; i++) {
  1743. tempView[i] = this.valueHexView[i];
  1744. }
  1745. this.valueHexView = tempView;
  1746. if ((intBuffer[this.blockLength - 1] & 0x80) !== 0x00) {
  1747. this.error = "End of input reached before message was fully decoded";
  1748. return -1;
  1749. }
  1750. if (this.valueHexView[0] === 0x00)
  1751. this.warnings.push("Needlessly long format of SID encoding");
  1752. if (this.blockLength <= 8)
  1753. this.valueDec = pvutils__namespace.utilFromBase(this.valueHexView, 7);
  1754. else {
  1755. this.isHexOnly = true;
  1756. this.warnings.push("Too big SID for decoding, hex only");
  1757. }
  1758. return (inputOffset + this.blockLength);
  1759. }
  1760. set valueBigInt(value) {
  1761. assertBigInt();
  1762. let bits = BigInt(value).toString(2);
  1763. while (bits.length % 7) {
  1764. bits = "0" + bits;
  1765. }
  1766. const bytes = new Uint8Array(bits.length / 7);
  1767. for (let i = 0; i < bytes.length; i++) {
  1768. bytes[i] = parseInt(bits.slice(i * 7, i * 7 + 7), 2) + (i + 1 < bytes.length ? 0x80 : 0);
  1769. }
  1770. this.fromBER(bytes.buffer, 0, bytes.length);
  1771. }
  1772. toBER(sizeOnly) {
  1773. if (this.isHexOnly) {
  1774. if (sizeOnly)
  1775. return (new ArrayBuffer(this.valueHexView.byteLength));
  1776. const curView = this.valueHexView;
  1777. const retView = new Uint8Array(this.blockLength);
  1778. for (let i = 0; i < (this.blockLength - 1); i++)
  1779. retView[i] = curView[i] | 0x80;
  1780. retView[this.blockLength - 1] = curView[this.blockLength - 1];
  1781. return retView.buffer;
  1782. }
  1783. const encodedBuf = pvutils__namespace.utilToBase(this.valueDec, 7);
  1784. if (encodedBuf.byteLength === 0) {
  1785. this.error = "Error during encoding SID value";
  1786. return EMPTY_BUFFER;
  1787. }
  1788. const retView = new Uint8Array(encodedBuf.byteLength);
  1789. if (!sizeOnly) {
  1790. const encodedView = new Uint8Array(encodedBuf);
  1791. const len = encodedBuf.byteLength - 1;
  1792. for (let i = 0; i < len; i++)
  1793. retView[i] = encodedView[i] | 0x80;
  1794. retView[len] = encodedView[len];
  1795. }
  1796. return retView;
  1797. }
  1798. toString() {
  1799. let result = "";
  1800. if (this.isHexOnly)
  1801. result = pvtsutils__namespace.Convert.ToHex(this.valueHexView);
  1802. else {
  1803. if (this.isFirstSid) {
  1804. let sidValue = this.valueDec;
  1805. if (this.valueDec <= 39)
  1806. result = "0.";
  1807. else {
  1808. if (this.valueDec <= 79) {
  1809. result = "1.";
  1810. sidValue -= 40;
  1811. }
  1812. else {
  1813. result = "2.";
  1814. sidValue -= 80;
  1815. }
  1816. }
  1817. result += sidValue.toString();
  1818. }
  1819. else
  1820. result = this.valueDec.toString();
  1821. }
  1822. return result;
  1823. }
  1824. toJSON() {
  1825. return {
  1826. ...super.toJSON(),
  1827. valueDec: this.valueDec,
  1828. isFirstSid: this.isFirstSid,
  1829. };
  1830. }
  1831. }
  1832. LocalSidValueBlock.NAME = "sidBlock";
  1833. class LocalObjectIdentifierValueBlock extends ValueBlock {
  1834. constructor({ value = EMPTY_STRING, ...parameters } = {}) {
  1835. super(parameters);
  1836. this.value = [];
  1837. if (value) {
  1838. this.fromString(value);
  1839. }
  1840. }
  1841. fromBER(inputBuffer, inputOffset, inputLength) {
  1842. let resultOffset = inputOffset;
  1843. while (inputLength > 0) {
  1844. const sidBlock = new LocalSidValueBlock();
  1845. resultOffset = sidBlock.fromBER(inputBuffer, resultOffset, inputLength);
  1846. if (resultOffset === -1) {
  1847. this.blockLength = 0;
  1848. this.error = sidBlock.error;
  1849. return resultOffset;
  1850. }
  1851. if (this.value.length === 0)
  1852. sidBlock.isFirstSid = true;
  1853. this.blockLength += sidBlock.blockLength;
  1854. inputLength -= sidBlock.blockLength;
  1855. this.value.push(sidBlock);
  1856. }
  1857. return resultOffset;
  1858. }
  1859. toBER(sizeOnly) {
  1860. const retBuffers = [];
  1861. for (let i = 0; i < this.value.length; i++) {
  1862. const valueBuf = this.value[i].toBER(sizeOnly);
  1863. if (valueBuf.byteLength === 0) {
  1864. this.error = this.value[i].error;
  1865. return EMPTY_BUFFER;
  1866. }
  1867. retBuffers.push(valueBuf);
  1868. }
  1869. return concat(retBuffers);
  1870. }
  1871. fromString(string) {
  1872. this.value = [];
  1873. let pos1 = 0;
  1874. let pos2 = 0;
  1875. let sid = "";
  1876. let flag = false;
  1877. do {
  1878. pos2 = string.indexOf(".", pos1);
  1879. if (pos2 === -1)
  1880. sid = string.substring(pos1);
  1881. else
  1882. sid = string.substring(pos1, pos2);
  1883. pos1 = pos2 + 1;
  1884. if (flag) {
  1885. const sidBlock = this.value[0];
  1886. let plus = 0;
  1887. switch (sidBlock.valueDec) {
  1888. case 0:
  1889. break;
  1890. case 1:
  1891. plus = 40;
  1892. break;
  1893. case 2:
  1894. plus = 80;
  1895. break;
  1896. default:
  1897. this.value = [];
  1898. return;
  1899. }
  1900. const parsedSID = parseInt(sid, 10);
  1901. if (isNaN(parsedSID))
  1902. return;
  1903. sidBlock.valueDec = parsedSID + plus;
  1904. flag = false;
  1905. }
  1906. else {
  1907. const sidBlock = new LocalSidValueBlock();
  1908. if (sid > Number.MAX_SAFE_INTEGER) {
  1909. assertBigInt();
  1910. const sidValue = BigInt(sid);
  1911. sidBlock.valueBigInt = sidValue;
  1912. }
  1913. else {
  1914. sidBlock.valueDec = parseInt(sid, 10);
  1915. if (isNaN(sidBlock.valueDec))
  1916. return;
  1917. }
  1918. if (!this.value.length) {
  1919. sidBlock.isFirstSid = true;
  1920. flag = true;
  1921. }
  1922. this.value.push(sidBlock);
  1923. }
  1924. } while (pos2 !== -1);
  1925. }
  1926. toString() {
  1927. let result = "";
  1928. let isHexOnly = false;
  1929. for (let i = 0; i < this.value.length; i++) {
  1930. isHexOnly = this.value[i].isHexOnly;
  1931. let sidStr = this.value[i].toString();
  1932. if (i !== 0)
  1933. result = `${result}.`;
  1934. if (isHexOnly) {
  1935. sidStr = `{${sidStr}}`;
  1936. if (this.value[i].isFirstSid)
  1937. result = `2.{${sidStr} - 80}`;
  1938. else
  1939. result += sidStr;
  1940. }
  1941. else
  1942. result += sidStr;
  1943. }
  1944. return result;
  1945. }
  1946. toJSON() {
  1947. const object = {
  1948. ...super.toJSON(),
  1949. value: this.toString(),
  1950. sidArray: [],
  1951. };
  1952. for (let i = 0; i < this.value.length; i++) {
  1953. object.sidArray.push(this.value[i].toJSON());
  1954. }
  1955. return object;
  1956. }
  1957. }
  1958. LocalObjectIdentifierValueBlock.NAME = "ObjectIdentifierValueBlock";
  1959. var _a$m;
  1960. class ObjectIdentifier extends BaseBlock {
  1961. getValue() {
  1962. return this.valueBlock.toString();
  1963. }
  1964. setValue(value) {
  1965. this.valueBlock.fromString(value);
  1966. }
  1967. constructor(parameters = {}) {
  1968. super(parameters, LocalObjectIdentifierValueBlock);
  1969. this.idBlock.tagClass = 1;
  1970. this.idBlock.tagNumber = 6;
  1971. }
  1972. onAsciiEncoding() {
  1973. return `${this.constructor.NAME} : ${this.valueBlock.toString() || "empty"}`;
  1974. }
  1975. toJSON() {
  1976. return {
  1977. ...super.toJSON(),
  1978. value: this.getValue(),
  1979. };
  1980. }
  1981. }
  1982. _a$m = ObjectIdentifier;
  1983. (() => {
  1984. typeStore.ObjectIdentifier = _a$m;
  1985. })();
  1986. ObjectIdentifier.NAME = "OBJECT IDENTIFIER";
  1987. class LocalRelativeSidValueBlock extends HexBlock(LocalBaseBlock) {
  1988. constructor({ valueDec = 0, ...parameters } = {}) {
  1989. super(parameters);
  1990. this.valueDec = valueDec;
  1991. }
  1992. fromBER(inputBuffer, inputOffset, inputLength) {
  1993. if (inputLength === 0)
  1994. return inputOffset;
  1995. const inputView = pvtsutils__namespace.BufferSourceConverter.toUint8Array(inputBuffer);
  1996. if (!checkBufferParams(this, inputView, inputOffset, inputLength))
  1997. return -1;
  1998. const intBuffer = inputView.subarray(inputOffset, inputOffset + inputLength);
  1999. this.valueHexView = new Uint8Array(inputLength);
  2000. for (let i = 0; i < inputLength; i++) {
  2001. this.valueHexView[i] = intBuffer[i] & 0x7F;
  2002. this.blockLength++;
  2003. if ((intBuffer[i] & 0x80) === 0x00)
  2004. break;
  2005. }
  2006. const tempView = new Uint8Array(this.blockLength);
  2007. for (let i = 0; i < this.blockLength; i++)
  2008. tempView[i] = this.valueHexView[i];
  2009. this.valueHexView = tempView;
  2010. if ((intBuffer[this.blockLength - 1] & 0x80) !== 0x00) {
  2011. this.error = "End of input reached before message was fully decoded";
  2012. return -1;
  2013. }
  2014. if (this.valueHexView[0] === 0x00)
  2015. this.warnings.push("Needlessly long format of SID encoding");
  2016. if (this.blockLength <= 8)
  2017. this.valueDec = pvutils__namespace.utilFromBase(this.valueHexView, 7);
  2018. else {
  2019. this.isHexOnly = true;
  2020. this.warnings.push("Too big SID for decoding, hex only");
  2021. }
  2022. return (inputOffset + this.blockLength);
  2023. }
  2024. toBER(sizeOnly) {
  2025. if (this.isHexOnly) {
  2026. if (sizeOnly)
  2027. return (new ArrayBuffer(this.valueHexView.byteLength));
  2028. const curView = this.valueHexView;
  2029. const retView = new Uint8Array(this.blockLength);
  2030. for (let i = 0; i < (this.blockLength - 1); i++)
  2031. retView[i] = curView[i] | 0x80;
  2032. retView[this.blockLength - 1] = curView[this.blockLength - 1];
  2033. return retView.buffer;
  2034. }
  2035. const encodedBuf = pvutils__namespace.utilToBase(this.valueDec, 7);
  2036. if (encodedBuf.byteLength === 0) {
  2037. this.error = "Error during encoding SID value";
  2038. return EMPTY_BUFFER;
  2039. }
  2040. const retView = new Uint8Array(encodedBuf.byteLength);
  2041. if (!sizeOnly) {
  2042. const encodedView = new Uint8Array(encodedBuf);
  2043. const len = encodedBuf.byteLength - 1;
  2044. for (let i = 0; i < len; i++)
  2045. retView[i] = encodedView[i] | 0x80;
  2046. retView[len] = encodedView[len];
  2047. }
  2048. return retView.buffer;
  2049. }
  2050. toString() {
  2051. let result = "";
  2052. if (this.isHexOnly)
  2053. result = pvtsutils__namespace.Convert.ToHex(this.valueHexView);
  2054. else {
  2055. result = this.valueDec.toString();
  2056. }
  2057. return result;
  2058. }
  2059. toJSON() {
  2060. return {
  2061. ...super.toJSON(),
  2062. valueDec: this.valueDec,
  2063. };
  2064. }
  2065. }
  2066. LocalRelativeSidValueBlock.NAME = "relativeSidBlock";
  2067. class LocalRelativeObjectIdentifierValueBlock extends ValueBlock {
  2068. constructor({ value = EMPTY_STRING, ...parameters } = {}) {
  2069. super(parameters);
  2070. this.value = [];
  2071. if (value) {
  2072. this.fromString(value);
  2073. }
  2074. }
  2075. fromBER(inputBuffer, inputOffset, inputLength) {
  2076. let resultOffset = inputOffset;
  2077. while (inputLength > 0) {
  2078. const sidBlock = new LocalRelativeSidValueBlock();
  2079. resultOffset = sidBlock.fromBER(inputBuffer, resultOffset, inputLength);
  2080. if (resultOffset === -1) {
  2081. this.blockLength = 0;
  2082. this.error = sidBlock.error;
  2083. return resultOffset;
  2084. }
  2085. this.blockLength += sidBlock.blockLength;
  2086. inputLength -= sidBlock.blockLength;
  2087. this.value.push(sidBlock);
  2088. }
  2089. return resultOffset;
  2090. }
  2091. toBER(sizeOnly, _writer) {
  2092. const retBuffers = [];
  2093. for (let i = 0; i < this.value.length; i++) {
  2094. const valueBuf = this.value[i].toBER(sizeOnly);
  2095. if (valueBuf.byteLength === 0) {
  2096. this.error = this.value[i].error;
  2097. return EMPTY_BUFFER;
  2098. }
  2099. retBuffers.push(valueBuf);
  2100. }
  2101. return concat(retBuffers);
  2102. }
  2103. fromString(string) {
  2104. this.value = [];
  2105. let pos1 = 0;
  2106. let pos2 = 0;
  2107. let sid = "";
  2108. do {
  2109. pos2 = string.indexOf(".", pos1);
  2110. if (pos2 === -1)
  2111. sid = string.substring(pos1);
  2112. else
  2113. sid = string.substring(pos1, pos2);
  2114. pos1 = pos2 + 1;
  2115. const sidBlock = new LocalRelativeSidValueBlock();
  2116. sidBlock.valueDec = parseInt(sid, 10);
  2117. if (isNaN(sidBlock.valueDec))
  2118. return true;
  2119. this.value.push(sidBlock);
  2120. } while (pos2 !== -1);
  2121. return true;
  2122. }
  2123. toString() {
  2124. let result = "";
  2125. let isHexOnly = false;
  2126. for (let i = 0; i < this.value.length; i++) {
  2127. isHexOnly = this.value[i].isHexOnly;
  2128. let sidStr = this.value[i].toString();
  2129. if (i !== 0)
  2130. result = `${result}.`;
  2131. if (isHexOnly) {
  2132. sidStr = `{${sidStr}}`;
  2133. result += sidStr;
  2134. }
  2135. else
  2136. result += sidStr;
  2137. }
  2138. return result;
  2139. }
  2140. toJSON() {
  2141. const object = {
  2142. ...super.toJSON(),
  2143. value: this.toString(),
  2144. sidArray: [],
  2145. };
  2146. for (let i = 0; i < this.value.length; i++)
  2147. object.sidArray.push(this.value[i].toJSON());
  2148. return object;
  2149. }
  2150. }
  2151. LocalRelativeObjectIdentifierValueBlock.NAME = "RelativeObjectIdentifierValueBlock";
  2152. var _a$l;
  2153. class RelativeObjectIdentifier extends BaseBlock {
  2154. getValue() {
  2155. return this.valueBlock.toString();
  2156. }
  2157. setValue(value) {
  2158. this.valueBlock.fromString(value);
  2159. }
  2160. constructor(parameters = {}) {
  2161. super(parameters, LocalRelativeObjectIdentifierValueBlock);
  2162. this.idBlock.tagClass = 1;
  2163. this.idBlock.tagNumber = 13;
  2164. }
  2165. onAsciiEncoding() {
  2166. return `${this.constructor.NAME} : ${this.valueBlock.toString() || "empty"}`;
  2167. }
  2168. toJSON() {
  2169. return {
  2170. ...super.toJSON(),
  2171. value: this.getValue(),
  2172. };
  2173. }
  2174. }
  2175. _a$l = RelativeObjectIdentifier;
  2176. (() => {
  2177. typeStore.RelativeObjectIdentifier = _a$l;
  2178. })();
  2179. RelativeObjectIdentifier.NAME = "RelativeObjectIdentifier";
  2180. var _a$k;
  2181. class Sequence extends Constructed {
  2182. constructor(parameters = {}) {
  2183. super(parameters);
  2184. this.idBlock.tagClass = 1;
  2185. this.idBlock.tagNumber = 16;
  2186. }
  2187. }
  2188. _a$k = Sequence;
  2189. (() => {
  2190. typeStore.Sequence = _a$k;
  2191. })();
  2192. Sequence.NAME = "SEQUENCE";
  2193. var _a$j;
  2194. class Set extends Constructed {
  2195. constructor(parameters = {}) {
  2196. super(parameters);
  2197. this.idBlock.tagClass = 1;
  2198. this.idBlock.tagNumber = 17;
  2199. }
  2200. }
  2201. _a$j = Set;
  2202. (() => {
  2203. typeStore.Set = _a$j;
  2204. })();
  2205. Set.NAME = "SET";
  2206. class LocalStringValueBlock extends HexBlock(ValueBlock) {
  2207. constructor({ ...parameters } = {}) {
  2208. super(parameters);
  2209. this.isHexOnly = true;
  2210. this.value = EMPTY_STRING;
  2211. }
  2212. toJSON() {
  2213. return {
  2214. ...super.toJSON(),
  2215. value: this.value,
  2216. };
  2217. }
  2218. }
  2219. LocalStringValueBlock.NAME = "StringValueBlock";
  2220. class LocalSimpleStringValueBlock extends LocalStringValueBlock {
  2221. }
  2222. LocalSimpleStringValueBlock.NAME = "SimpleStringValueBlock";
  2223. class LocalSimpleStringBlock extends BaseStringBlock {
  2224. constructor({ ...parameters } = {}) {
  2225. super(parameters, LocalSimpleStringValueBlock);
  2226. }
  2227. fromBuffer(inputBuffer) {
  2228. this.valueBlock.value = String.fromCharCode.apply(null, pvtsutils__namespace.BufferSourceConverter.toUint8Array(inputBuffer));
  2229. }
  2230. fromString(inputString) {
  2231. const strLen = inputString.length;
  2232. const view = this.valueBlock.valueHexView = new Uint8Array(strLen);
  2233. for (let i = 0; i < strLen; i++)
  2234. view[i] = inputString.charCodeAt(i);
  2235. this.valueBlock.value = inputString;
  2236. }
  2237. }
  2238. LocalSimpleStringBlock.NAME = "SIMPLE STRING";
  2239. class LocalUtf8StringValueBlock extends LocalSimpleStringBlock {
  2240. fromBuffer(inputBuffer) {
  2241. this.valueBlock.valueHexView = pvtsutils__namespace.BufferSourceConverter.toUint8Array(inputBuffer);
  2242. try {
  2243. this.valueBlock.value = pvtsutils__namespace.Convert.ToUtf8String(inputBuffer);
  2244. }
  2245. catch (ex) {
  2246. this.warnings.push(`Error during "decodeURIComponent": ${ex}, using raw string`);
  2247. this.valueBlock.value = pvtsutils__namespace.Convert.ToBinary(inputBuffer);
  2248. }
  2249. }
  2250. fromString(inputString) {
  2251. this.valueBlock.valueHexView = new Uint8Array(pvtsutils__namespace.Convert.FromUtf8String(inputString));
  2252. this.valueBlock.value = inputString;
  2253. }
  2254. }
  2255. LocalUtf8StringValueBlock.NAME = "Utf8StringValueBlock";
  2256. var _a$i;
  2257. class Utf8String extends LocalUtf8StringValueBlock {
  2258. constructor(parameters = {}) {
  2259. super(parameters);
  2260. this.idBlock.tagClass = 1;
  2261. this.idBlock.tagNumber = 12;
  2262. }
  2263. }
  2264. _a$i = Utf8String;
  2265. (() => {
  2266. typeStore.Utf8String = _a$i;
  2267. })();
  2268. Utf8String.NAME = "UTF8String";
  2269. class LocalBmpStringValueBlock extends LocalSimpleStringBlock {
  2270. fromBuffer(inputBuffer) {
  2271. this.valueBlock.value = pvtsutils__namespace.Convert.ToUtf16String(inputBuffer);
  2272. this.valueBlock.valueHexView = pvtsutils__namespace.BufferSourceConverter.toUint8Array(inputBuffer);
  2273. }
  2274. fromString(inputString) {
  2275. this.valueBlock.value = inputString;
  2276. this.valueBlock.valueHexView = new Uint8Array(pvtsutils__namespace.Convert.FromUtf16String(inputString));
  2277. }
  2278. }
  2279. LocalBmpStringValueBlock.NAME = "BmpStringValueBlock";
  2280. var _a$h;
  2281. class BmpString extends LocalBmpStringValueBlock {
  2282. constructor({ ...parameters } = {}) {
  2283. super(parameters);
  2284. this.idBlock.tagClass = 1;
  2285. this.idBlock.tagNumber = 30;
  2286. }
  2287. }
  2288. _a$h = BmpString;
  2289. (() => {
  2290. typeStore.BmpString = _a$h;
  2291. })();
  2292. BmpString.NAME = "BMPString";
  2293. class LocalUniversalStringValueBlock extends LocalSimpleStringBlock {
  2294. fromBuffer(inputBuffer) {
  2295. const copyBuffer = ArrayBuffer.isView(inputBuffer) ? inputBuffer.slice().buffer : inputBuffer.slice(0);
  2296. const valueView = new Uint8Array(copyBuffer);
  2297. for (let i = 0; i < valueView.length; i += 4) {
  2298. valueView[i] = valueView[i + 3];
  2299. valueView[i + 1] = valueView[i + 2];
  2300. valueView[i + 2] = 0x00;
  2301. valueView[i + 3] = 0x00;
  2302. }
  2303. this.valueBlock.value = String.fromCharCode.apply(null, new Uint32Array(copyBuffer));
  2304. }
  2305. fromString(inputString) {
  2306. const strLength = inputString.length;
  2307. const valueHexView = this.valueBlock.valueHexView = new Uint8Array(strLength * 4);
  2308. for (let i = 0; i < strLength; i++) {
  2309. const codeBuf = pvutils__namespace.utilToBase(inputString.charCodeAt(i), 8);
  2310. const codeView = new Uint8Array(codeBuf);
  2311. if (codeView.length > 4)
  2312. continue;
  2313. const dif = 4 - codeView.length;
  2314. for (let j = (codeView.length - 1); j >= 0; j--)
  2315. valueHexView[i * 4 + j + dif] = codeView[j];
  2316. }
  2317. this.valueBlock.value = inputString;
  2318. }
  2319. }
  2320. LocalUniversalStringValueBlock.NAME = "UniversalStringValueBlock";
  2321. var _a$g;
  2322. class UniversalString extends LocalUniversalStringValueBlock {
  2323. constructor({ ...parameters } = {}) {
  2324. super(parameters);
  2325. this.idBlock.tagClass = 1;
  2326. this.idBlock.tagNumber = 28;
  2327. }
  2328. }
  2329. _a$g = UniversalString;
  2330. (() => {
  2331. typeStore.UniversalString = _a$g;
  2332. })();
  2333. UniversalString.NAME = "UniversalString";
  2334. var _a$f;
  2335. class NumericString extends LocalSimpleStringBlock {
  2336. constructor(parameters = {}) {
  2337. super(parameters);
  2338. this.idBlock.tagClass = 1;
  2339. this.idBlock.tagNumber = 18;
  2340. }
  2341. }
  2342. _a$f = NumericString;
  2343. (() => {
  2344. typeStore.NumericString = _a$f;
  2345. })();
  2346. NumericString.NAME = "NumericString";
  2347. var _a$e;
  2348. class PrintableString extends LocalSimpleStringBlock {
  2349. constructor(parameters = {}) {
  2350. super(parameters);
  2351. this.idBlock.tagClass = 1;
  2352. this.idBlock.tagNumber = 19;
  2353. }
  2354. }
  2355. _a$e = PrintableString;
  2356. (() => {
  2357. typeStore.PrintableString = _a$e;
  2358. })();
  2359. PrintableString.NAME = "PrintableString";
  2360. var _a$d;
  2361. class TeletexString extends LocalSimpleStringBlock {
  2362. constructor(parameters = {}) {
  2363. super(parameters);
  2364. this.idBlock.tagClass = 1;
  2365. this.idBlock.tagNumber = 20;
  2366. }
  2367. }
  2368. _a$d = TeletexString;
  2369. (() => {
  2370. typeStore.TeletexString = _a$d;
  2371. })();
  2372. TeletexString.NAME = "TeletexString";
  2373. var _a$c;
  2374. class VideotexString extends LocalSimpleStringBlock {
  2375. constructor(parameters = {}) {
  2376. super(parameters);
  2377. this.idBlock.tagClass = 1;
  2378. this.idBlock.tagNumber = 21;
  2379. }
  2380. }
  2381. _a$c = VideotexString;
  2382. (() => {
  2383. typeStore.VideotexString = _a$c;
  2384. })();
  2385. VideotexString.NAME = "VideotexString";
  2386. var _a$b;
  2387. class IA5String extends LocalSimpleStringBlock {
  2388. constructor(parameters = {}) {
  2389. super(parameters);
  2390. this.idBlock.tagClass = 1;
  2391. this.idBlock.tagNumber = 22;
  2392. }
  2393. }
  2394. _a$b = IA5String;
  2395. (() => {
  2396. typeStore.IA5String = _a$b;
  2397. })();
  2398. IA5String.NAME = "IA5String";
  2399. var _a$a;
  2400. class GraphicString extends LocalSimpleStringBlock {
  2401. constructor(parameters = {}) {
  2402. super(parameters);
  2403. this.idBlock.tagClass = 1;
  2404. this.idBlock.tagNumber = 25;
  2405. }
  2406. }
  2407. _a$a = GraphicString;
  2408. (() => {
  2409. typeStore.GraphicString = _a$a;
  2410. })();
  2411. GraphicString.NAME = "GraphicString";
  2412. var _a$9;
  2413. class VisibleString extends LocalSimpleStringBlock {
  2414. constructor(parameters = {}) {
  2415. super(parameters);
  2416. this.idBlock.tagClass = 1;
  2417. this.idBlock.tagNumber = 26;
  2418. }
  2419. }
  2420. _a$9 = VisibleString;
  2421. (() => {
  2422. typeStore.VisibleString = _a$9;
  2423. })();
  2424. VisibleString.NAME = "VisibleString";
  2425. var _a$8;
  2426. class GeneralString extends LocalSimpleStringBlock {
  2427. constructor(parameters = {}) {
  2428. super(parameters);
  2429. this.idBlock.tagClass = 1;
  2430. this.idBlock.tagNumber = 27;
  2431. }
  2432. }
  2433. _a$8 = GeneralString;
  2434. (() => {
  2435. typeStore.GeneralString = _a$8;
  2436. })();
  2437. GeneralString.NAME = "GeneralString";
  2438. var _a$7;
  2439. class CharacterString extends LocalSimpleStringBlock {
  2440. constructor(parameters = {}) {
  2441. super(parameters);
  2442. this.idBlock.tagClass = 1;
  2443. this.idBlock.tagNumber = 29;
  2444. }
  2445. }
  2446. _a$7 = CharacterString;
  2447. (() => {
  2448. typeStore.CharacterString = _a$7;
  2449. })();
  2450. CharacterString.NAME = "CharacterString";
  2451. var _a$6;
  2452. class UTCTime extends VisibleString {
  2453. constructor({ value, valueDate, ...parameters } = {}) {
  2454. super(parameters);
  2455. this.year = 0;
  2456. this.month = 0;
  2457. this.day = 0;
  2458. this.hour = 0;
  2459. this.minute = 0;
  2460. this.second = 0;
  2461. if (value) {
  2462. this.fromString(value);
  2463. this.valueBlock.valueHexView = new Uint8Array(value.length);
  2464. for (let i = 0; i < value.length; i++)
  2465. this.valueBlock.valueHexView[i] = value.charCodeAt(i);
  2466. }
  2467. if (valueDate) {
  2468. this.fromDate(valueDate);
  2469. this.valueBlock.valueHexView = new Uint8Array(this.toBuffer());
  2470. }
  2471. this.idBlock.tagClass = 1;
  2472. this.idBlock.tagNumber = 23;
  2473. }
  2474. fromBuffer(inputBuffer) {
  2475. this.fromString(String.fromCharCode.apply(null, pvtsutils__namespace.BufferSourceConverter.toUint8Array(inputBuffer)));
  2476. }
  2477. toBuffer() {
  2478. const str = this.toString();
  2479. const buffer = new ArrayBuffer(str.length);
  2480. const view = new Uint8Array(buffer);
  2481. for (let i = 0; i < str.length; i++)
  2482. view[i] = str.charCodeAt(i);
  2483. return buffer;
  2484. }
  2485. fromDate(inputDate) {
  2486. this.year = inputDate.getUTCFullYear();
  2487. this.month = inputDate.getUTCMonth() + 1;
  2488. this.day = inputDate.getUTCDate();
  2489. this.hour = inputDate.getUTCHours();
  2490. this.minute = inputDate.getUTCMinutes();
  2491. this.second = inputDate.getUTCSeconds();
  2492. }
  2493. toDate() {
  2494. return (new Date(Date.UTC(this.year, this.month - 1, this.day, this.hour, this.minute, this.second)));
  2495. }
  2496. fromString(inputString) {
  2497. const parser = /(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z/ig;
  2498. const parserArray = parser.exec(inputString);
  2499. if (parserArray === null) {
  2500. this.error = "Wrong input string for conversion";
  2501. return;
  2502. }
  2503. const year = parseInt(parserArray[1], 10);
  2504. if (year >= 50)
  2505. this.year = 1900 + year;
  2506. else
  2507. this.year = 2000 + year;
  2508. this.month = parseInt(parserArray[2], 10);
  2509. this.day = parseInt(parserArray[3], 10);
  2510. this.hour = parseInt(parserArray[4], 10);
  2511. this.minute = parseInt(parserArray[5], 10);
  2512. this.second = parseInt(parserArray[6], 10);
  2513. }
  2514. toString(encoding = "iso") {
  2515. if (encoding === "iso") {
  2516. const outputArray = new Array(7);
  2517. outputArray[0] = pvutils__namespace.padNumber(((this.year < 2000) ? (this.year - 1900) : (this.year - 2000)), 2);
  2518. outputArray[1] = pvutils__namespace.padNumber(this.month, 2);
  2519. outputArray[2] = pvutils__namespace.padNumber(this.day, 2);
  2520. outputArray[3] = pvutils__namespace.padNumber(this.hour, 2);
  2521. outputArray[4] = pvutils__namespace.padNumber(this.minute, 2);
  2522. outputArray[5] = pvutils__namespace.padNumber(this.second, 2);
  2523. outputArray[6] = "Z";
  2524. return outputArray.join("");
  2525. }
  2526. return super.toString(encoding);
  2527. }
  2528. onAsciiEncoding() {
  2529. return `${this.constructor.NAME} : ${this.toDate().toISOString()}`;
  2530. }
  2531. toJSON() {
  2532. return {
  2533. ...super.toJSON(),
  2534. year: this.year,
  2535. month: this.month,
  2536. day: this.day,
  2537. hour: this.hour,
  2538. minute: this.minute,
  2539. second: this.second,
  2540. };
  2541. }
  2542. }
  2543. _a$6 = UTCTime;
  2544. (() => {
  2545. typeStore.UTCTime = _a$6;
  2546. })();
  2547. UTCTime.NAME = "UTCTime";
  2548. var _a$5;
  2549. class GeneralizedTime extends UTCTime {
  2550. constructor(parameters = {}) {
  2551. var _b;
  2552. super(parameters);
  2553. (_b = this.millisecond) !== null && _b !== void 0 ? _b : (this.millisecond = 0);
  2554. this.idBlock.tagClass = 1;
  2555. this.idBlock.tagNumber = 24;
  2556. }
  2557. fromDate(inputDate) {
  2558. super.fromDate(inputDate);
  2559. this.millisecond = inputDate.getUTCMilliseconds();
  2560. }
  2561. toDate() {
  2562. const utcDate = Date.UTC(this.year, this.month - 1, this.day, this.hour, this.minute, this.second, this.millisecond);
  2563. return (new Date(utcDate));
  2564. }
  2565. fromString(inputString) {
  2566. let isUTC = false;
  2567. let timeString = "";
  2568. let dateTimeString = "";
  2569. let fractionPart = 0;
  2570. let parser;
  2571. let hourDifference = 0;
  2572. let minuteDifference = 0;
  2573. if (inputString[inputString.length - 1] === "Z") {
  2574. timeString = inputString.substring(0, inputString.length - 1);
  2575. isUTC = true;
  2576. }
  2577. else {
  2578. const number = new Number(inputString[inputString.length - 1]);
  2579. if (isNaN(number.valueOf()))
  2580. throw new Error("Wrong input string for conversion");
  2581. timeString = inputString;
  2582. }
  2583. if (isUTC) {
  2584. if (timeString.indexOf("+") !== -1)
  2585. throw new Error("Wrong input string for conversion");
  2586. if (timeString.indexOf("-") !== -1)
  2587. throw new Error("Wrong input string for conversion");
  2588. }
  2589. else {
  2590. let multiplier = 1;
  2591. let differencePosition = timeString.indexOf("+");
  2592. let differenceString = "";
  2593. if (differencePosition === -1) {
  2594. differencePosition = timeString.indexOf("-");
  2595. multiplier = -1;
  2596. }
  2597. if (differencePosition !== -1) {
  2598. differenceString = timeString.substring(differencePosition + 1);
  2599. timeString = timeString.substring(0, differencePosition);
  2600. if ((differenceString.length !== 2) && (differenceString.length !== 4))
  2601. throw new Error("Wrong input string for conversion");
  2602. let number = parseInt(differenceString.substring(0, 2), 10);
  2603. if (isNaN(number.valueOf()))
  2604. throw new Error("Wrong input string for conversion");
  2605. hourDifference = multiplier * number;
  2606. if (differenceString.length === 4) {
  2607. number = parseInt(differenceString.substring(2, 4), 10);
  2608. if (isNaN(number.valueOf()))
  2609. throw new Error("Wrong input string for conversion");
  2610. minuteDifference = multiplier * number;
  2611. }
  2612. }
  2613. }
  2614. let fractionPointPosition = timeString.indexOf(".");
  2615. if (fractionPointPosition === -1)
  2616. fractionPointPosition = timeString.indexOf(",");
  2617. if (fractionPointPosition !== -1) {
  2618. const fractionPartCheck = new Number(`0${timeString.substring(fractionPointPosition)}`);
  2619. if (isNaN(fractionPartCheck.valueOf()))
  2620. throw new Error("Wrong input string for conversion");
  2621. fractionPart = fractionPartCheck.valueOf();
  2622. dateTimeString = timeString.substring(0, fractionPointPosition);
  2623. }
  2624. else
  2625. dateTimeString = timeString;
  2626. switch (true) {
  2627. case (dateTimeString.length === 8):
  2628. parser = /(\d{4})(\d{2})(\d{2})/ig;
  2629. if (fractionPointPosition !== -1)
  2630. throw new Error("Wrong input string for conversion");
  2631. break;
  2632. case (dateTimeString.length === 10):
  2633. parser = /(\d{4})(\d{2})(\d{2})(\d{2})/ig;
  2634. if (fractionPointPosition !== -1) {
  2635. let fractionResult = 60 * fractionPart;
  2636. this.minute = Math.floor(fractionResult);
  2637. fractionResult = 60 * (fractionResult - this.minute);
  2638. this.second = Math.floor(fractionResult);
  2639. fractionResult = 1000 * (fractionResult - this.second);
  2640. this.millisecond = Math.floor(fractionResult);
  2641. }
  2642. break;
  2643. case (dateTimeString.length === 12):
  2644. parser = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/ig;
  2645. if (fractionPointPosition !== -1) {
  2646. let fractionResult = 60 * fractionPart;
  2647. this.second = Math.floor(fractionResult);
  2648. fractionResult = 1000 * (fractionResult - this.second);
  2649. this.millisecond = Math.floor(fractionResult);
  2650. }
  2651. break;
  2652. case (dateTimeString.length === 14):
  2653. parser = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/ig;
  2654. if (fractionPointPosition !== -1) {
  2655. const fractionResult = 1000 * fractionPart;
  2656. this.millisecond = Math.floor(fractionResult);
  2657. }
  2658. break;
  2659. default:
  2660. throw new Error("Wrong input string for conversion");
  2661. }
  2662. const parserArray = parser.exec(dateTimeString);
  2663. if (parserArray === null)
  2664. throw new Error("Wrong input string for conversion");
  2665. for (let j = 1; j < parserArray.length; j++) {
  2666. switch (j) {
  2667. case 1:
  2668. this.year = parseInt(parserArray[j], 10);
  2669. break;
  2670. case 2:
  2671. this.month = parseInt(parserArray[j], 10);
  2672. break;
  2673. case 3:
  2674. this.day = parseInt(parserArray[j], 10);
  2675. break;
  2676. case 4:
  2677. this.hour = parseInt(parserArray[j], 10) + hourDifference;
  2678. break;
  2679. case 5:
  2680. this.minute = parseInt(parserArray[j], 10) + minuteDifference;
  2681. break;
  2682. case 6:
  2683. this.second = parseInt(parserArray[j], 10);
  2684. break;
  2685. default:
  2686. throw new Error("Wrong input string for conversion");
  2687. }
  2688. }
  2689. if (isUTC === false) {
  2690. const tempDate = new Date(this.year, this.month, this.day, this.hour, this.minute, this.second, this.millisecond);
  2691. this.year = tempDate.getUTCFullYear();
  2692. this.month = tempDate.getUTCMonth();
  2693. this.day = tempDate.getUTCDay();
  2694. this.hour = tempDate.getUTCHours();
  2695. this.minute = tempDate.getUTCMinutes();
  2696. this.second = tempDate.getUTCSeconds();
  2697. this.millisecond = tempDate.getUTCMilliseconds();
  2698. }
  2699. }
  2700. toString(encoding = "iso") {
  2701. if (encoding === "iso") {
  2702. const outputArray = [];
  2703. outputArray.push(pvutils__namespace.padNumber(this.year, 4));
  2704. outputArray.push(pvutils__namespace.padNumber(this.month, 2));
  2705. outputArray.push(pvutils__namespace.padNumber(this.day, 2));
  2706. outputArray.push(pvutils__namespace.padNumber(this.hour, 2));
  2707. outputArray.push(pvutils__namespace.padNumber(this.minute, 2));
  2708. outputArray.push(pvutils__namespace.padNumber(this.second, 2));
  2709. if (this.millisecond !== 0) {
  2710. outputArray.push(".");
  2711. outputArray.push(pvutils__namespace.padNumber(this.millisecond, 3));
  2712. }
  2713. outputArray.push("Z");
  2714. return outputArray.join("");
  2715. }
  2716. return super.toString(encoding);
  2717. }
  2718. toJSON() {
  2719. return {
  2720. ...super.toJSON(),
  2721. millisecond: this.millisecond,
  2722. };
  2723. }
  2724. }
  2725. _a$5 = GeneralizedTime;
  2726. (() => {
  2727. typeStore.GeneralizedTime = _a$5;
  2728. })();
  2729. GeneralizedTime.NAME = "GeneralizedTime";
  2730. var _a$4;
  2731. class DATE extends Utf8String {
  2732. constructor(parameters = {}) {
  2733. super(parameters);
  2734. this.idBlock.tagClass = 1;
  2735. this.idBlock.tagNumber = 31;
  2736. }
  2737. }
  2738. _a$4 = DATE;
  2739. (() => {
  2740. typeStore.DATE = _a$4;
  2741. })();
  2742. DATE.NAME = "DATE";
  2743. var _a$3;
  2744. class TimeOfDay extends Utf8String {
  2745. constructor(parameters = {}) {
  2746. super(parameters);
  2747. this.idBlock.tagClass = 1;
  2748. this.idBlock.tagNumber = 32;
  2749. }
  2750. }
  2751. _a$3 = TimeOfDay;
  2752. (() => {
  2753. typeStore.TimeOfDay = _a$3;
  2754. })();
  2755. TimeOfDay.NAME = "TimeOfDay";
  2756. var _a$2;
  2757. class DateTime extends Utf8String {
  2758. constructor(parameters = {}) {
  2759. super(parameters);
  2760. this.idBlock.tagClass = 1;
  2761. this.idBlock.tagNumber = 33;
  2762. }
  2763. }
  2764. _a$2 = DateTime;
  2765. (() => {
  2766. typeStore.DateTime = _a$2;
  2767. })();
  2768. DateTime.NAME = "DateTime";
  2769. var _a$1;
  2770. class Duration extends Utf8String {
  2771. constructor(parameters = {}) {
  2772. super(parameters);
  2773. this.idBlock.tagClass = 1;
  2774. this.idBlock.tagNumber = 34;
  2775. }
  2776. }
  2777. _a$1 = Duration;
  2778. (() => {
  2779. typeStore.Duration = _a$1;
  2780. })();
  2781. Duration.NAME = "Duration";
  2782. var _a;
  2783. class TIME extends Utf8String {
  2784. constructor(parameters = {}) {
  2785. super(parameters);
  2786. this.idBlock.tagClass = 1;
  2787. this.idBlock.tagNumber = 14;
  2788. }
  2789. }
  2790. _a = TIME;
  2791. (() => {
  2792. typeStore.TIME = _a;
  2793. })();
  2794. TIME.NAME = "TIME";
  2795. class Any {
  2796. constructor({ name = EMPTY_STRING, optional = false } = {}) {
  2797. this.name = name;
  2798. this.optional = optional;
  2799. }
  2800. }
  2801. class Choice extends Any {
  2802. constructor({ value = [], ...parameters } = {}) {
  2803. super(parameters);
  2804. this.value = value;
  2805. }
  2806. }
  2807. class Repeated extends Any {
  2808. constructor({ value = new Any(), local = false, ...parameters } = {}) {
  2809. super(parameters);
  2810. this.value = value;
  2811. this.local = local;
  2812. }
  2813. }
  2814. class RawData {
  2815. get data() {
  2816. return this.dataView.slice().buffer;
  2817. }
  2818. set data(value) {
  2819. this.dataView = pvtsutils__namespace.BufferSourceConverter.toUint8Array(value);
  2820. }
  2821. constructor({ data = EMPTY_VIEW } = {}) {
  2822. this.dataView = pvtsutils__namespace.BufferSourceConverter.toUint8Array(data);
  2823. }
  2824. fromBER(inputBuffer, inputOffset, inputLength) {
  2825. const endLength = inputOffset + inputLength;
  2826. this.dataView = pvtsutils__namespace.BufferSourceConverter.toUint8Array(inputBuffer).subarray(inputOffset, endLength);
  2827. return endLength;
  2828. }
  2829. toBER(_sizeOnly) {
  2830. return this.dataView.slice().buffer;
  2831. }
  2832. }
  2833. function compareSchema(root, inputData, inputSchema) {
  2834. if (inputSchema instanceof Choice) {
  2835. for (const element of inputSchema.value) {
  2836. const result = compareSchema(root, inputData, element);
  2837. if (result.verified) {
  2838. return {
  2839. verified: true,
  2840. result: root,
  2841. };
  2842. }
  2843. }
  2844. {
  2845. const _result = {
  2846. verified: false,
  2847. result: { error: "Wrong values for Choice type" },
  2848. };
  2849. if (inputSchema.hasOwnProperty(NAME))
  2850. _result.name = inputSchema.name;
  2851. return _result;
  2852. }
  2853. }
  2854. if (inputSchema instanceof Any) {
  2855. if (inputSchema.hasOwnProperty(NAME))
  2856. root[inputSchema.name] = inputData;
  2857. return {
  2858. verified: true,
  2859. result: root,
  2860. };
  2861. }
  2862. if ((root instanceof Object) === false) {
  2863. return {
  2864. verified: false,
  2865. result: { error: "Wrong root object" },
  2866. };
  2867. }
  2868. if ((inputData instanceof Object) === false) {
  2869. return {
  2870. verified: false,
  2871. result: { error: "Wrong ASN.1 data" },
  2872. };
  2873. }
  2874. if ((inputSchema instanceof Object) === false) {
  2875. return {
  2876. verified: false,
  2877. result: { error: "Wrong ASN.1 schema" },
  2878. };
  2879. }
  2880. if ((ID_BLOCK in inputSchema) === false) {
  2881. return {
  2882. verified: false,
  2883. result: { error: "Wrong ASN.1 schema" },
  2884. };
  2885. }
  2886. if ((FROM_BER in inputSchema.idBlock) === false) {
  2887. return {
  2888. verified: false,
  2889. result: { error: "Wrong ASN.1 schema" },
  2890. };
  2891. }
  2892. if ((TO_BER in inputSchema.idBlock) === false) {
  2893. return {
  2894. verified: false,
  2895. result: { error: "Wrong ASN.1 schema" },
  2896. };
  2897. }
  2898. const encodedId = inputSchema.idBlock.toBER(false);
  2899. if (encodedId.byteLength === 0) {
  2900. return {
  2901. verified: false,
  2902. result: { error: "Error encoding idBlock for ASN.1 schema" },
  2903. };
  2904. }
  2905. const decodedOffset = inputSchema.idBlock.fromBER(encodedId, 0, encodedId.byteLength);
  2906. if (decodedOffset === -1) {
  2907. return {
  2908. verified: false,
  2909. result: { error: "Error decoding idBlock for ASN.1 schema" },
  2910. };
  2911. }
  2912. if (inputSchema.idBlock.hasOwnProperty(TAG_CLASS) === false) {
  2913. return {
  2914. verified: false,
  2915. result: { error: "Wrong ASN.1 schema" },
  2916. };
  2917. }
  2918. if (inputSchema.idBlock.tagClass !== inputData.idBlock.tagClass) {
  2919. return {
  2920. verified: false,
  2921. result: root,
  2922. };
  2923. }
  2924. if (inputSchema.idBlock.hasOwnProperty(TAG_NUMBER) === false) {
  2925. return {
  2926. verified: false,
  2927. result: { error: "Wrong ASN.1 schema" },
  2928. };
  2929. }
  2930. if (inputSchema.idBlock.tagNumber !== inputData.idBlock.tagNumber) {
  2931. return {
  2932. verified: false,
  2933. result: root,
  2934. };
  2935. }
  2936. if (inputSchema.idBlock.hasOwnProperty(IS_CONSTRUCTED) === false) {
  2937. return {
  2938. verified: false,
  2939. result: { error: "Wrong ASN.1 schema" },
  2940. };
  2941. }
  2942. if (inputSchema.idBlock.isConstructed !== inputData.idBlock.isConstructed) {
  2943. return {
  2944. verified: false,
  2945. result: root,
  2946. };
  2947. }
  2948. if (!(IS_HEX_ONLY in inputSchema.idBlock)) {
  2949. return {
  2950. verified: false,
  2951. result: { error: "Wrong ASN.1 schema" },
  2952. };
  2953. }
  2954. if (inputSchema.idBlock.isHexOnly !== inputData.idBlock.isHexOnly) {
  2955. return {
  2956. verified: false,
  2957. result: root,
  2958. };
  2959. }
  2960. if (inputSchema.idBlock.isHexOnly) {
  2961. if ((VALUE_HEX_VIEW in inputSchema.idBlock) === false) {
  2962. return {
  2963. verified: false,
  2964. result: { error: "Wrong ASN.1 schema" },
  2965. };
  2966. }
  2967. const schemaView = inputSchema.idBlock.valueHexView;
  2968. const asn1View = inputData.idBlock.valueHexView;
  2969. if (schemaView.length !== asn1View.length) {
  2970. return {
  2971. verified: false,
  2972. result: root,
  2973. };
  2974. }
  2975. for (let i = 0; i < schemaView.length; i++) {
  2976. if (schemaView[i] !== asn1View[1]) {
  2977. return {
  2978. verified: false,
  2979. result: root,
  2980. };
  2981. }
  2982. }
  2983. }
  2984. if (inputSchema.name) {
  2985. inputSchema.name = inputSchema.name.replace(/^\s+|\s+$/g, EMPTY_STRING);
  2986. if (inputSchema.name)
  2987. root[inputSchema.name] = inputData;
  2988. }
  2989. if (inputSchema instanceof typeStore.Constructed) {
  2990. let admission = 0;
  2991. let result = {
  2992. verified: false,
  2993. result: { error: "Unknown error" },
  2994. };
  2995. let maxLength = inputSchema.valueBlock.value.length;
  2996. if (maxLength > 0) {
  2997. if (inputSchema.valueBlock.value[0] instanceof Repeated) {
  2998. maxLength = inputData.valueBlock.value.length;
  2999. }
  3000. }
  3001. if (maxLength === 0) {
  3002. return {
  3003. verified: true,
  3004. result: root,
  3005. };
  3006. }
  3007. if ((inputData.valueBlock.value.length === 0)
  3008. && (inputSchema.valueBlock.value.length !== 0)) {
  3009. let _optional = true;
  3010. for (let i = 0; i < inputSchema.valueBlock.value.length; i++)
  3011. _optional = _optional && (inputSchema.valueBlock.value[i].optional || false);
  3012. if (_optional) {
  3013. return {
  3014. verified: true,
  3015. result: root,
  3016. };
  3017. }
  3018. if (inputSchema.name) {
  3019. inputSchema.name = inputSchema.name.replace(/^\s+|\s+$/g, EMPTY_STRING);
  3020. if (inputSchema.name)
  3021. delete root[inputSchema.name];
  3022. }
  3023. root.error = "Inconsistent object length";
  3024. return {
  3025. verified: false,
  3026. result: root,
  3027. };
  3028. }
  3029. for (let i = 0; i < maxLength; i++) {
  3030. if ((i - admission) >= inputData.valueBlock.value.length) {
  3031. if (inputSchema.valueBlock.value[i].optional === false) {
  3032. const _result = {
  3033. verified: false,
  3034. result: root,
  3035. };
  3036. root.error = "Inconsistent length between ASN.1 data and schema";
  3037. if (inputSchema.name) {
  3038. inputSchema.name = inputSchema.name.replace(/^\s+|\s+$/g, EMPTY_STRING);
  3039. if (inputSchema.name) {
  3040. delete root[inputSchema.name];
  3041. _result.name = inputSchema.name;
  3042. }
  3043. }
  3044. return _result;
  3045. }
  3046. }
  3047. else {
  3048. if (inputSchema.valueBlock.value[0] instanceof Repeated) {
  3049. result = compareSchema(root, inputData.valueBlock.value[i], inputSchema.valueBlock.value[0].value);
  3050. if (result.verified === false) {
  3051. if (inputSchema.valueBlock.value[0].optional)
  3052. admission++;
  3053. else {
  3054. if (inputSchema.name) {
  3055. inputSchema.name = inputSchema.name.replace(/^\s+|\s+$/g, EMPTY_STRING);
  3056. if (inputSchema.name)
  3057. delete root[inputSchema.name];
  3058. }
  3059. return result;
  3060. }
  3061. }
  3062. if ((NAME in inputSchema.valueBlock.value[0]) && (inputSchema.valueBlock.value[0].name.length > 0)) {
  3063. let arrayRoot = {};
  3064. if ((LOCAL in inputSchema.valueBlock.value[0]) && (inputSchema.valueBlock.value[0].local))
  3065. arrayRoot = inputData;
  3066. else
  3067. arrayRoot = root;
  3068. if (typeof arrayRoot[inputSchema.valueBlock.value[0].name] === "undefined")
  3069. arrayRoot[inputSchema.valueBlock.value[0].name] = [];
  3070. arrayRoot[inputSchema.valueBlock.value[0].name].push(inputData.valueBlock.value[i]);
  3071. }
  3072. }
  3073. else {
  3074. result = compareSchema(root, inputData.valueBlock.value[i - admission], inputSchema.valueBlock.value[i]);
  3075. if (result.verified === false) {
  3076. if (inputSchema.valueBlock.value[i].optional)
  3077. admission++;
  3078. else {
  3079. if (inputSchema.name) {
  3080. inputSchema.name = inputSchema.name.replace(/^\s+|\s+$/g, EMPTY_STRING);
  3081. if (inputSchema.name)
  3082. delete root[inputSchema.name];
  3083. }
  3084. return result;
  3085. }
  3086. }
  3087. }
  3088. }
  3089. }
  3090. if (result.verified === false) {
  3091. const _result = {
  3092. verified: false,
  3093. result: root,
  3094. };
  3095. if (inputSchema.name) {
  3096. inputSchema.name = inputSchema.name.replace(/^\s+|\s+$/g, EMPTY_STRING);
  3097. if (inputSchema.name) {
  3098. delete root[inputSchema.name];
  3099. _result.name = inputSchema.name;
  3100. }
  3101. }
  3102. return _result;
  3103. }
  3104. return {
  3105. verified: true,
  3106. result: root,
  3107. };
  3108. }
  3109. if (inputSchema.primitiveSchema
  3110. && (VALUE_HEX_VIEW in inputData.valueBlock)) {
  3111. const asn1 = localFromBER(inputData.valueBlock.valueHexView);
  3112. if (asn1.offset === -1) {
  3113. const _result = {
  3114. verified: false,
  3115. result: asn1.result,
  3116. };
  3117. if (inputSchema.name) {
  3118. inputSchema.name = inputSchema.name.replace(/^\s+|\s+$/g, EMPTY_STRING);
  3119. if (inputSchema.name) {
  3120. delete root[inputSchema.name];
  3121. _result.name = inputSchema.name;
  3122. }
  3123. }
  3124. return _result;
  3125. }
  3126. return compareSchema(root, asn1.result, inputSchema.primitiveSchema);
  3127. }
  3128. return {
  3129. verified: true,
  3130. result: root,
  3131. };
  3132. }
  3133. function verifySchema(inputBuffer, inputSchema) {
  3134. if ((inputSchema instanceof Object) === false) {
  3135. return {
  3136. verified: false,
  3137. result: { error: "Wrong ASN.1 schema type" },
  3138. };
  3139. }
  3140. const asn1 = localFromBER(pvtsutils__namespace.BufferSourceConverter.toUint8Array(inputBuffer));
  3141. if (asn1.offset === -1) {
  3142. return {
  3143. verified: false,
  3144. result: asn1.result,
  3145. };
  3146. }
  3147. return compareSchema(asn1.result, asn1.result, inputSchema);
  3148. }
  3149. exports.Any = Any;
  3150. exports.BaseBlock = BaseBlock;
  3151. exports.BaseStringBlock = BaseStringBlock;
  3152. exports.BitString = BitString;
  3153. exports.BmpString = BmpString;
  3154. exports.Boolean = Boolean;
  3155. exports.CharacterString = CharacterString;
  3156. exports.Choice = Choice;
  3157. exports.Constructed = Constructed;
  3158. exports.DATE = DATE;
  3159. exports.DEFAULT_MAX_CONTENT_LENGTH = DEFAULT_MAX_CONTENT_LENGTH;
  3160. exports.DEFAULT_MAX_DEPTH = DEFAULT_MAX_DEPTH;
  3161. exports.DEFAULT_MAX_NODES = DEFAULT_MAX_NODES;
  3162. exports.DateTime = DateTime;
  3163. exports.Duration = Duration;
  3164. exports.EndOfContent = EndOfContent;
  3165. exports.Enumerated = Enumerated;
  3166. exports.GeneralString = GeneralString;
  3167. exports.GeneralizedTime = GeneralizedTime;
  3168. exports.GraphicString = GraphicString;
  3169. exports.HexBlock = HexBlock;
  3170. exports.IA5String = IA5String;
  3171. exports.Integer = Integer;
  3172. exports.Null = Null;
  3173. exports.NumericString = NumericString;
  3174. exports.ObjectIdentifier = ObjectIdentifier;
  3175. exports.OctetString = OctetString;
  3176. exports.Primitive = Primitive;
  3177. exports.PrintableString = PrintableString;
  3178. exports.RawData = RawData;
  3179. exports.RelativeObjectIdentifier = RelativeObjectIdentifier;
  3180. exports.Repeated = Repeated;
  3181. exports.Sequence = Sequence;
  3182. exports.Set = Set;
  3183. exports.TIME = TIME;
  3184. exports.TeletexString = TeletexString;
  3185. exports.TimeOfDay = TimeOfDay;
  3186. exports.UTCTime = UTCTime;
  3187. exports.UniversalString = UniversalString;
  3188. exports.Utf8String = Utf8String;
  3189. exports.ValueBlock = ValueBlock;
  3190. exports.VideotexString = VideotexString;
  3191. exports.ViewWriter = ViewWriter;
  3192. exports.VisibleString = VisibleString;
  3193. exports.compareSchema = compareSchema;
  3194. exports.fromBER = fromBER;
  3195. exports.verifySchema = verifySchema;