index.es.js 107 KB

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