index.js 109 KB

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