index.es.js 109 KB

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