UNPKG

@peculiar/x509

Version:

@peculiar/x509 is an easy to use TypeScript/Javascript library based on @peculiar/asn1-schema that makes generating X.509 Certificates and Certificate Requests as well as validating certificate chains easy

61 lines 197 kB
/*! * MIT License * * Copyright (c) Peculiar Ventures. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * */ var x509=function(e){"use strict";var t,r="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},i={};!function(){return t||(t=1,function(e){!function(){var t="object"==typeof globalThis?globalThis:"object"==typeof r?r:"object"==typeof self?self:"object"==typeof this?this:function(){try{return Function("return this;")()}catch(e){}}()||function(){try{return(0,eval)("(function() { return this; })()")}catch(e){}}(),i=n(e);function n(e,t){return function(r,i){Object.defineProperty(e,r,{configurable:!0,writable:!0,value:i}),t&&t(r,i)}}void 0!==t.Reflect&&(i=n(t.Reflect,i)),function(e,t){var r=Object.prototype.hasOwnProperty,i="function"==typeof Symbol,n=i&&void 0!==Symbol.toPrimitive?Symbol.toPrimitive:"@@toPrimitive",s=i&&void 0!==Symbol.iterator?Symbol.iterator:"@@iterator",o="function"==typeof Object.create,a={__proto__:[]}instanceof Array,c=!o&&!a,l={create:o?function(){return pe(Object.create(null))}:a?function(){return pe({__proto__:null})}:function(){return pe({})},has:c?function(e,t){return r.call(e,t)}:function(e,t){return t in e},get:c?function(e,t){return r.call(e,t)?e[t]:void 0}:function(e,t){return e[t]}},u=Object.getPrototypeOf(Function),p="function"==typeof Map&&"function"==typeof Map.prototype.entries?Map:ce(),h="function"==typeof Set&&"function"==typeof Set.prototype.entries?Set:le(),f="function"==typeof WeakMap?WeakMap:ue(),y=i?Symbol.for("@reflect-metadata:registry"):void 0,d=ne(),g=se(d);function v(e,t,r,i){if(D(r)){if(!q(e))throw new TypeError;if(!G(t))throw new TypeError;return C(e,t)}if(!q(e))throw new TypeError;if(!M(t))throw new TypeError;if(!M(i)&&!D(i)&&!L(i))throw new TypeError;return L(i)&&(i=void 0),O(e,t,r=z(r),i)}function m(e,t){function r(r,i){if(!M(r))throw new TypeError;if(!D(i)&&!J(i))throw new TypeError;U(e,t,r,i)}return r}function b(e,t,r,i){if(!M(r))throw new TypeError;return D(i)||(i=z(i)),U(e,t,r,i)}function w(e,t,r){if(!M(t))throw new TypeError;return D(r)||(r=z(r)),N(e,t,r)}function A(e,t,r){if(!M(t))throw new TypeError;return D(r)||(r=z(r)),I(e,t,r)}function S(e,t,r){if(!M(t))throw new TypeError;return D(r)||(r=z(r)),T(e,t,r)}function x(e,t,r){if(!M(t))throw new TypeError;return D(r)||(r=z(r)),j(e,t,r)}function B(e,t){if(!M(e))throw new TypeError;return D(t)||(t=z(t)),P(e,t)}function k(e,t){if(!M(e))throw new TypeError;return D(t)||(t=z(t)),R(e,t)}function E(e,t,r){if(!M(t))throw new TypeError;if(D(r)||(r=z(r)),!M(t))throw new TypeError;D(r)||(r=z(r));var i=ae(t,r,!1);return!D(i)&&i.OrdinaryDeleteMetadata(e,t,r)}function C(e,t){for(var r=e.length-1;r>=0;--r){var i=(0,e[r])(t);if(!D(i)&&!L(i)){if(!G(i))throw new TypeError;t=i}}return t}function O(e,t,r,i){for(var n=e.length-1;n>=0;--n){var s=(0,e[n])(t,r,i);if(!D(s)&&!L(s)){if(!M(s))throw new TypeError;i=s}}return i}function N(e,t,r){if(I(e,t,r))return!0;var i=re(t);return!L(i)&&N(e,i,r)}function I(e,t,r){var i=ae(t,r,!1);return!D(i)&&F(i.OrdinaryHasOwnMetadata(e,t,r))}function T(e,t,r){if(I(e,t,r))return j(e,t,r);var i=re(t);return L(i)?void 0:T(e,i,r)}function j(e,t,r){var i=ae(t,r,!1);if(!D(i))return i.OrdinaryGetOwnMetadata(e,t,r)}function U(e,t,r,i){ae(r,i,!0).OrdinaryDefineOwnMetadata(e,t,r,i)}function P(e,t){var r=R(e,t),i=re(e);if(null===i)return r;var n=P(i,t);if(n.length<=0)return r;if(r.length<=0)return n;for(var s=new h,o=[],a=0,c=r;a<c.length;a++){var l=c[a];s.has(l)||(s.add(l),o.push(l))}for(var u=0,p=n;u<p.length;u++){l=p[u];s.has(l)||(s.add(l),o.push(l))}return o}function R(e,t){var r=ae(e,t,!1);return r?r.OrdinaryOwnMetadataKeys(e,t):[]}function V(e){if(null===e)return 1;switch(typeof e){case"undefined":return 0;case"boolean":return 2;case"string":return 3;case"symbol":return 4;case"number":return 5;case"object":return null===e?1:6;default:return 6}}function D(e){return void 0===e}function L(e){return null===e}function H(e){return"symbol"==typeof e}function M(e){return"object"==typeof e?null!==e:"function"==typeof e}function $(e,t){switch(V(e)){case 0:case 1:case 2:case 3:case 4:case 5:return e}var r="string",i=Z(e,n);if(void 0!==i){var s=i.call(e,r);if(M(s))throw new TypeError;return s}return K(e)}function K(e,t){var r,i,n=e.toString;if(W(n)&&!M(i=n.call(e)))return i;if(W(r=e.valueOf)&&!M(i=r.call(e)))return i;throw new TypeError}function F(e){return!!e}function _(e){return""+e}function z(e){var t=$(e);return H(t)?t:_(t)}function q(e){return Array.isArray?Array.isArray(e):e instanceof Object?e instanceof Array:"[object Array]"===Object.prototype.toString.call(e)}function W(e){return"function"==typeof e}function G(e){return"function"==typeof e}function J(e){switch(V(e)){case 3:case 4:return!0;default:return!1}}function X(e,t){return e===t||e!=e&&t!=t}function Z(e,t){var r=e[t];if(null!=r){if(!W(r))throw new TypeError;return r}}function Y(e){var t=Z(e,s);if(!W(t))throw new TypeError;var r=t.call(e);if(!M(r))throw new TypeError;return r}function Q(e){return e.value}function ee(e){var t=e.next();return!t.done&&t}function te(e){var t=e.return;t&&t.call(e)}function re(e){var t=Object.getPrototypeOf(e);if("function"!=typeof e||e===u)return t;if(t!==u)return t;var r=e.prototype,i=r&&Object.getPrototypeOf(r);if(null==i||i===Object.prototype)return t;var n=i.constructor;return"function"!=typeof n||n===e?t:n}function ie(){var e,r,i,n;D(y)||void 0===t.Reflect||y in t.Reflect||"function"!=typeof t.Reflect.defineMetadata||(e=oe(t.Reflect));var s=new f,o={registerProvider:a,getProvider:l,setProvider:d};return o;function a(t){if(!Object.isExtensible(o))throw new Error("Cannot add provider to a frozen registry.");switch(!0){case e===t:break;case D(r):r=t;break;case r===t:break;case D(i):i=t;break;case i===t:break;default:void 0===n&&(n=new h),n.add(t)}}function c(t,s){if(!D(r)){if(r.isProviderFor(t,s))return r;if(!D(i)){if(i.isProviderFor(t,s))return r;if(!D(n))for(var o=Y(n);;){var a=ee(o);if(!a)return;var c=Q(a);if(c.isProviderFor(t,s))return te(o),c}}}if(!D(e)&&e.isProviderFor(t,s))return e}function l(e,t){var r,i=s.get(e);return D(i)||(r=i.get(t)),D(r)?(D(r=c(e,t))||(D(i)&&(i=new p,s.set(e,i)),i.set(t,r)),r):r}function u(e){if(D(e))throw new TypeError;return r===e||i===e||!D(n)&&n.has(e)}function d(e,t,r){if(!u(r))throw new Error("Metadata provider not registered.");var i=l(e,t);if(i!==r){if(!D(i))return!1;var n=s.get(e);D(n)&&(n=new p,s.set(e,n)),n.set(t,r)}return!0}}function ne(){var e;return!D(y)&&M(t.Reflect)&&Object.isExtensible(t.Reflect)&&(e=t.Reflect[y]),D(e)&&(e=ie()),!D(y)&&M(t.Reflect)&&Object.isExtensible(t.Reflect)&&Object.defineProperty(t.Reflect,y,{enumerable:!1,configurable:!1,writable:!1,value:e}),e}function se(e){var t=new f,r={isProviderFor:function(e,r){var i=t.get(e);return!D(i)&&i.has(r)},OrdinaryDefineOwnMetadata:o,OrdinaryHasOwnMetadata:n,OrdinaryGetOwnMetadata:s,OrdinaryOwnMetadataKeys:a,OrdinaryDeleteMetadata:c};return d.registerProvider(r),r;function i(i,n,s){var o=t.get(i),a=!1;if(D(o)){if(!s)return;o=new p,t.set(i,o),a=!0}var c=o.get(n);if(D(c)){if(!s)return;if(c=new p,o.set(n,c),!e.setProvider(i,n,r))throw o.delete(n),a&&t.delete(i),new Error("Wrong provider for target.")}return c}function n(e,t,r){var n=i(t,r,!1);return!D(n)&&F(n.has(e))}function s(e,t,r){var n=i(t,r,!1);if(!D(n))return n.get(e)}function o(e,t,r,n){i(r,n,!0).set(e,t)}function a(e,t){var r=[],n=i(e,t,!1);if(D(n))return r;for(var s=Y(n.keys()),o=0;;){var a=ee(s);if(!a)return r.length=o,r;var c=Q(a);try{r[o]=c}catch(e){try{te(s)}finally{throw e}}o++}}function c(e,r,n){var s=i(r,n,!1);if(D(s))return!1;if(!s.delete(e))return!1;if(0===s.size){var o=t.get(r);D(o)||(o.delete(n),0===o.size&&t.delete(o))}return!0}}function oe(e){var t=e.defineMetadata,r=e.hasOwnMetadata,i=e.getOwnMetadata,n=e.getOwnMetadataKeys,s=e.deleteMetadata,o=new f;return{isProviderFor:function(e,t){var r=o.get(e);return!(D(r)||!r.has(t))||!!n(e,t).length&&(D(r)&&(r=new h,o.set(e,r)),r.add(t),!0)},OrdinaryDefineOwnMetadata:t,OrdinaryHasOwnMetadata:r,OrdinaryGetOwnMetadata:i,OrdinaryOwnMetadataKeys:n,OrdinaryDeleteMetadata:s}}function ae(e,t,r){var i=d.getProvider(e,t);if(!D(i))return i;if(r){if(d.setProvider(e,t,g))return g;throw new Error("Illegal state.")}}function ce(){var e={},t=[],r=function(){function e(e,t,r){this._index=0,this._keys=e,this._values=t,this._selector=r}return e.prototype["@@iterator"]=function(){return this},e.prototype[s]=function(){return this},e.prototype.next=function(){var e=this._index;if(e>=0&&e<this._keys.length){var r=this._selector(this._keys[e],this._values[e]);return e+1>=this._keys.length?(this._index=-1,this._keys=t,this._values=t):this._index++,{value:r,done:!1}}return{value:void 0,done:!0}},e.prototype.throw=function(e){throw this._index>=0&&(this._index=-1,this._keys=t,this._values=t),e},e.prototype.return=function(e){return this._index>=0&&(this._index=-1,this._keys=t,this._values=t),{value:e,done:!0}},e}();return function(){function t(){this._keys=[],this._values=[],this._cacheKey=e,this._cacheIndex=-2}return Object.defineProperty(t.prototype,"size",{get:function(){return this._keys.length},enumerable:!0,configurable:!0}),t.prototype.has=function(e){return this._find(e,!1)>=0},t.prototype.get=function(e){var t=this._find(e,!1);return t>=0?this._values[t]:void 0},t.prototype.set=function(e,t){var r=this._find(e,!0);return this._values[r]=t,this},t.prototype.delete=function(t){var r=this._find(t,!1);if(r>=0){for(var i=this._keys.length,n=r+1;n<i;n++)this._keys[n-1]=this._keys[n],this._values[n-1]=this._values[n];return this._keys.length--,this._values.length--,X(t,this._cacheKey)&&(this._cacheKey=e,this._cacheIndex=-2),!0}return!1},t.prototype.clear=function(){this._keys.length=0,this._values.length=0,this._cacheKey=e,this._cacheIndex=-2},t.prototype.keys=function(){return new r(this._keys,this._values,i)},t.prototype.values=function(){return new r(this._keys,this._values,n)},t.prototype.entries=function(){return new r(this._keys,this._values,o)},t.prototype["@@iterator"]=function(){return this.entries()},t.prototype[s]=function(){return this.entries()},t.prototype._find=function(e,t){if(!X(this._cacheKey,e)){this._cacheIndex=-1;for(var r=0;r<this._keys.length;r++)if(X(this._keys[r],e)){this._cacheIndex=r;break}}return this._cacheIndex<0&&t&&(this._cacheIndex=this._keys.length,this._keys.push(e),this._values.push(void 0)),this._cacheIndex},t}();function i(e,t){return e}function n(e,t){return t}function o(e,t){return[e,t]}}function le(){return function(){function e(){this._map=new p}return Object.defineProperty(e.prototype,"size",{get:function(){return this._map.size},enumerable:!0,configurable:!0}),e.prototype.has=function(e){return this._map.has(e)},e.prototype.add=function(e){return this._map.set(e,e),this},e.prototype.delete=function(e){return this._map.delete(e)},e.prototype.clear=function(){this._map.clear()},e.prototype.keys=function(){return this._map.keys()},e.prototype.values=function(){return this._map.keys()},e.prototype.entries=function(){return this._map.entries()},e.prototype["@@iterator"]=function(){return this.keys()},e.prototype[s]=function(){return this.keys()},e}()}function ue(){var e=16,t=l.create(),i=n();return function(){function e(){this._key=n()}return e.prototype.has=function(e){var t=s(e,!1);return void 0!==t&&l.has(t,this._key)},e.prototype.get=function(e){var t=s(e,!1);return void 0!==t?l.get(t,this._key):void 0},e.prototype.set=function(e,t){return s(e,!0)[this._key]=t,this},e.prototype.delete=function(e){var t=s(e,!1);return void 0!==t&&delete t[this._key]},e.prototype.clear=function(){this._key=n()},e}();function n(){var e;do{e="@@WeakMap@@"+c()}while(l.has(t,e));return t[e]=!0,e}function s(e,t){if(!r.call(e,i)){if(!t)return;Object.defineProperty(e,i,{value:l.create()})}return e[i]}function o(e,t){for(var r=0;r<t;++r)e[r]=255*Math.random()|0;return e}function a(e){if("function"==typeof Uint8Array){var t=new Uint8Array(e);return"undefined"!=typeof crypto?crypto.getRandomValues(t):"undefined"!=typeof msCrypto?msCrypto.getRandomValues(t):o(t,e),t}return o(new Array(e),e)}function c(){var t=a(e);t[6]=79&t[6]|64,t[8]=191&t[8]|128;for(var r="",i=0;i<e;++i){var n=t[i];4!==i&&6!==i&&8!==i||(r+="-"),n<16&&(r+="0"),r+=n.toString(16).toLowerCase()}return r}}function pe(e){return e.__=void 0,delete e.__,e}e("decorate",v),e("metadata",m),e("defineMetadata",b),e("hasMetadata",w),e("hasOwnMetadata",A),e("getMetadata",S),e("getOwnMetadata",x),e("getMetadataKeys",B),e("getOwnMetadataKeys",k),e("deleteMetadata",E)}(i,t),void 0===t.Reflect&&(t.Reflect=e)}()}(e||(e={}))),i;var e}();class n{static isArrayBuffer(e){return"[object ArrayBuffer]"===Object.prototype.toString.call(e)}static toArrayBuffer(e){return this.isArrayBuffer(e)?e:e.byteLength===e.buffer.byteLength||0===e.byteOffset&&e.byteLength===e.buffer.byteLength?e.buffer:this.toUint8Array(e.buffer).slice(e.byteOffset,e.byteOffset+e.byteLength).buffer}static toUint8Array(e){return this.toView(e,Uint8Array)}static toView(e,t){if(e.constructor===t)return e;if(this.isArrayBuffer(e))return new t(e);if(this.isArrayBufferView(e))return new t(e.buffer,e.byteOffset,e.byteLength);throw new TypeError("The provided value is not of type '(ArrayBuffer or ArrayBufferView)'")}static isBufferSource(e){return this.isArrayBufferView(e)||this.isArrayBuffer(e)}static isArrayBufferView(e){return ArrayBuffer.isView(e)||e&&this.isArrayBuffer(e.buffer)}static isEqual(e,t){const r=n.toUint8Array(e),i=n.toUint8Array(t);if(r.length!==i.byteLength)return!1;for(let e=0;e<r.length;e++)if(r[e]!==i[e])return!1;return!0}static concat(...e){let t;t=!Array.isArray(e[0])||e[1]instanceof Function?Array.isArray(e[0])&&e[1]instanceof Function?e[0]:e[e.length-1]instanceof Function?e.slice(0,e.length-1):e:e[0];let r=0;for(const e of t)r+=e.byteLength;const i=new Uint8Array(r);let n=0;for(const e of t){const t=this.toUint8Array(e);i.set(t,n),n+=t.length}return e[e.length-1]instanceof Function?this.toView(i,e[e.length-1]):i.buffer}}const s="string",o=/^[0-9a-f\s]+$/i,a=/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/,c=/^[a-zA-Z0-9-_]+$/;class l{static fromString(e){const t=unescape(encodeURIComponent(e)),r=new Uint8Array(t.length);for(let e=0;e<t.length;e++)r[e]=t.charCodeAt(e);return r.buffer}static toString(e){const t=n.toUint8Array(e);let r="";for(let e=0;e<t.length;e++)r+=String.fromCharCode(t[e]);return decodeURIComponent(escape(r))}}class u{static toString(e,t=!1){const r=n.toArrayBuffer(e),i=new DataView(r);let s="";for(let e=0;e<r.byteLength;e+=2){const r=i.getUint16(e,t);s+=String.fromCharCode(r)}return s}static fromString(e,t=!1){const r=new ArrayBuffer(2*e.length),i=new DataView(r);for(let r=0;r<e.length;r++)i.setUint16(2*r,e.charCodeAt(r),t);return r}}class p{static isHex(e){return typeof e===s&&o.test(e)}static isBase64(e){return typeof e===s&&a.test(e)}static isBase64Url(e){return typeof e===s&&c.test(e)}static ToString(e,t="utf8"){const r=n.toUint8Array(e);switch(t.toLowerCase()){case"utf8":return this.ToUtf8String(r);case"binary":return this.ToBinary(r);case"hex":return this.ToHex(r);case"base64":return this.ToBase64(r);case"base64url":return this.ToBase64Url(r);case"utf16le":return u.toString(r,!0);case"utf16":case"utf16be":return u.toString(r);default:throw new Error(`Unknown type of encoding '${t}'`)}}static FromString(e,t="utf8"){if(!e)return new ArrayBuffer(0);switch(t.toLowerCase()){case"utf8":return this.FromUtf8String(e);case"binary":return this.FromBinary(e);case"hex":return this.FromHex(e);case"base64":return this.FromBase64(e);case"base64url":return this.FromBase64Url(e);case"utf16le":return u.fromString(e,!0);case"utf16":case"utf16be":return u.fromString(e);default:throw new Error(`Unknown type of encoding '${t}'`)}}static ToBase64(e){const t=n.toUint8Array(e);if("undefined"!=typeof btoa){const e=this.ToString(t,"binary");return btoa(e)}return Buffer.from(t).toString("base64")}static FromBase64(e){const t=this.formatString(e);if(!t)return new ArrayBuffer(0);if(!p.isBase64(t))throw new TypeError("Argument 'base64Text' is not Base64 encoded");return"undefined"!=typeof atob?this.FromBinary(atob(t)):new Uint8Array(Buffer.from(t,"base64")).buffer}static FromBase64Url(e){const t=this.formatString(e);if(!t)return new ArrayBuffer(0);if(!p.isBase64Url(t))throw new TypeError("Argument 'base64url' is not Base64Url encoded");return this.FromBase64(this.Base64Padding(t.replace(/\-/g,"+").replace(/\_/g,"/")))}static ToBase64Url(e){return this.ToBase64(e).replace(/\+/g,"-").replace(/\//g,"_").replace(/\=/g,"")}static FromUtf8String(e,t=p.DEFAULT_UTF8_ENCODING){switch(t){case"ascii":return this.FromBinary(e);case"utf8":return l.fromString(e);case"utf16":case"utf16be":return u.fromString(e);case"utf16le":case"usc2":return u.fromString(e,!0);default:throw new Error(`Unknown type of encoding '${t}'`)}}static ToUtf8String(e,t=p.DEFAULT_UTF8_ENCODING){switch(t){case"ascii":return this.ToBinary(e);case"utf8":return l.toString(e);case"utf16":case"utf16be":return u.toString(e);case"utf16le":case"usc2":return u.toString(e,!0);default:throw new Error(`Unknown type of encoding '${t}'`)}}static FromBinary(e){const t=e.length,r=new Uint8Array(t);for(let i=0;i<t;i++)r[i]=e.charCodeAt(i);return r.buffer}static ToBinary(e){const t=n.toUint8Array(e);let r="";for(let e=0;e<t.length;e++)r+=String.fromCharCode(t[e]);return r}static ToHex(e){const t=n.toUint8Array(e);let r="";const i=t.length;for(let e=0;e<i;e++){const i=t[e];i<16&&(r+="0"),r+=i.toString(16)}return r}static FromHex(e){let t=this.formatString(e);if(!t)return new ArrayBuffer(0);if(!p.isHex(t))throw new TypeError("Argument 'hexString' is not HEX encoded");t.length%2&&(t=`0${t}`);const r=new Uint8Array(t.length/2);for(let e=0;e<t.length;e+=2){const i=t.slice(e,e+2);r[e/2]=parseInt(i,16)}return r.buffer}static ToUtf16String(e,t=!1){return u.toString(e,t)}static FromUtf16String(e,t=!1){return u.fromString(e,t)}static Base64Padding(e){const t=4-e.length%4;if(t<4)for(let r=0;r<t;r++)e+="=";return e}static formatString(e){return(null==e?void 0:e.replace(/[\n\r\t ]/g,""))||""}}function h(e,t){if(!e||!t)return!1;if(e.byteLength!==t.byteLength)return!1;const r=new Uint8Array(e),i=new Uint8Array(t);for(let t=0;t<e.byteLength;t++)if(r[t]!==i[t])return!1;return!0} /*! Copyright (c) Peculiar Ventures, LLC */function f(e,t){let r=0;if(1===e.length)return e[0];for(let i=e.length-1;i>=0;i--)r+=e[e.length-1-i]*Math.pow(2,t*i);return r}function y(e,t,r=-1){const i=r;let n=e,s=0,o=Math.pow(2,t);for(let r=1;r<8;r++){if(e<o){let e;if(i<0)e=new ArrayBuffer(r),s=r;else{if(i<r)return new ArrayBuffer(0);e=new ArrayBuffer(i),s=i}const o=new Uint8Array(e);for(let e=r-1;e>=0;e--){const r=Math.pow(2,e*t);o[s-e-1]=Math.floor(n/r),n-=o[s-e-1]*r}return e}o*=Math.pow(2,t)}return new ArrayBuffer(0)}function d(...e){let t=0,r=0;for(const r of e)t+=r.length;const i=new ArrayBuffer(t),n=new Uint8Array(i);for(const t of e)n.set(t,r),r+=t.length;return n}function g(){const e=new Uint8Array(this.valueHex);if(this.valueHex.byteLength>=2){const t=255===e[0]&&128&e[1],r=0===e[0]&&!(128&e[1]);(t||r)&&this.warnings.push("Needlessly long format")}const t=new ArrayBuffer(this.valueHex.byteLength),r=new Uint8Array(t);for(let e=0;e<this.valueHex.byteLength;e++)r[e]=0;r[0]=128&e[0];const i=f(r,8),n=new ArrayBuffer(this.valueHex.byteLength),s=new Uint8Array(n);for(let t=0;t<this.valueHex.byteLength;t++)s[t]=e[t];s[0]&=127;return f(s,8)-i}function v(e,t){const r=e.toString(10);if(t<r.length)return"";const i=t-r.length,n=new Array(i);for(let e=0;e<i;e++)n[e]="0";return n.join("").concat(r)} /*! * Copyright (c) 2014, GMO GlobalSign * Copyright (c) 2015-2022, Peculiar Ventures * All rights reserved. * * Author 2014-2019, Yury Strozhevsky * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, this * list of conditions and the following disclaimer in the documentation and/or * other materials provided with the distribution. * * * Neither the name of the copyright holder nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */function m(){if("undefined"==typeof BigInt)throw new Error("BigInt is not defined. Your environment doesn't implement BigInt.")}function b(e){let t=0,r=0;for(let r=0;r<e.length;r++){t+=e[r].byteLength}const i=new Uint8Array(t);for(let t=0;t<e.length;t++){const n=e[t];i.set(new Uint8Array(n),r),r+=n.byteLength}return i.buffer}function w(e,t,r,i){return t instanceof Uint8Array?t.byteLength?r<0?(e.error="Wrong parameter: inputOffset less than zero",!1):i<0?(e.error="Wrong parameter: inputLength less than zero",!1):!(t.byteLength-r-i<0)||(e.error="End of input reached before message was fully decoded (inconsistent offset and length values)",!1):(e.error="Wrong parameter: inputBuffer has zero length",!1):(e.error="Wrong parameter: inputBuffer must be 'Uint8Array'",!1)}p.DEFAULT_UTF8_ENCODING="utf8";class A{constructor(){this.items=[]}write(e){this.items.push(e)}final(){return b(this.items)}}const S=[new Uint8Array([1])],x="0123456789",B="name",k="valueHexView",E="",C=new ArrayBuffer(0),O=new Uint8Array(0),N="EndOfContent",I="OCTET STRING",T="BIT STRING";function j(e){var t;return(t=class extends e{constructor(...e){var t;super(...e);const r=e[0]||{};this.isHexOnly=null!==(t=r.isHexOnly)&&void 0!==t&&t,this.valueHexView=r.valueHex?n.toUint8Array(r.valueHex):O}get valueHex(){return this.valueHexView.slice().buffer}set valueHex(e){this.valueHexView=new Uint8Array(e)}fromBER(e,t,r){const i=e instanceof ArrayBuffer?new Uint8Array(e):e;if(!w(this,i,t,r))return-1;const n=t+r;return this.valueHexView=i.subarray(t,n),this.valueHexView.length?(this.blockLength=r,n):(this.warnings.push("Zero buffer length"),t)}toBER(e=!1){return this.isHexOnly?e?new ArrayBuffer(this.valueHexView.byteLength):this.valueHexView.byteLength===this.valueHexView.buffer.byteLength?this.valueHexView.buffer:this.valueHexView.slice().buffer:(this.error="Flag 'isHexOnly' is not set, abort",C)}toJSON(){return{...super.toJSON(),isHexOnly:this.isHexOnly,valueHex:p.ToHex(this.valueHexView)}}}).NAME="hexBlock",t}class U{constructor({blockLength:e=0,error:t="",warnings:r=[],valueBeforeDecode:i=O}={}){this.blockLength=e,this.error=t,this.warnings=r,this.valueBeforeDecodeView=n.toUint8Array(i)}static blockName(){return this.NAME}get valueBeforeDecode(){return this.valueBeforeDecodeView.slice().buffer}set valueBeforeDecode(e){this.valueBeforeDecodeView=new Uint8Array(e)}toJSON(){return{blockName:this.constructor.NAME,blockLength:this.blockLength,error:this.error,warnings:this.warnings,valueBeforeDecode:p.ToHex(this.valueBeforeDecodeView)}}}U.NAME="baseBlock";class P extends U{fromBER(e,t,r){throw TypeError("User need to make a specific function in a class which extends 'ValueBlock'")}toBER(e,t){throw TypeError("User need to make a specific function in a class which extends 'ValueBlock'")}}P.NAME="valueBlock";class R extends(j(U)){constructor({idBlock:e={}}={}){var t,r,i,s;super(),e?(this.isHexOnly=null!==(t=e.isHexOnly)&&void 0!==t&&t,this.valueHexView=e.valueHex?n.toUint8Array(e.valueHex):O,this.tagClass=null!==(r=e.tagClass)&&void 0!==r?r:-1,this.tagNumber=null!==(i=e.tagNumber)&&void 0!==i?i:-1,this.isConstructed=null!==(s=e.isConstructed)&&void 0!==s&&s):(this.tagClass=-1,this.tagNumber=-1,this.isConstructed=!1)}toBER(e=!1){let t=0;switch(this.tagClass){case 1:t|=0;break;case 2:t|=64;break;case 3:t|=128;break;case 4:t|=192;break;default:return this.error="Unknown tag class",C}if(this.isConstructed&&(t|=32),this.tagNumber<31&&!this.isHexOnly){const r=new Uint8Array(1);if(!e){let e=this.tagNumber;e&=31,t|=e,r[0]=t}return r.buffer}if(!this.isHexOnly){const r=y(this.tagNumber,7),i=new Uint8Array(r),n=r.byteLength,s=new Uint8Array(n+1);if(s[0]=31|t,!e){for(let e=0;e<n-1;e++)s[e+1]=128|i[e];s[n]=i[n-1]}return s.buffer}const r=new Uint8Array(this.valueHexView.byteLength+1);if(r[0]=31|t,!e){const e=this.valueHexView;for(let t=0;t<e.length-1;t++)r[t+1]=128|e[t];r[this.valueHexView.byteLength]=e[e.length-1]}return r.buffer}fromBER(e,t,r){const i=n.toUint8Array(e);if(!w(this,i,t,r))return-1;const s=i.subarray(t,t+r);if(0===s.length)return this.error="Zero buffer length",-1;switch(192&s[0]){case 0:this.tagClass=1;break;case 64:this.tagClass=2;break;case 128:this.tagClass=3;break;case 192:this.tagClass=4;break;default:return this.error="Unknown tag class",-1}this.isConstructed=!(32&~s[0]),this.isHexOnly=!1;const o=31&s[0];if(31!==o)this.tagNumber=o,this.blockLength=1;else{let e=1,t=this.valueHexView=new Uint8Array(255),r=255;for(;128&s[e];){if(t[e-1]=127&s[e],e++,e>=s.length)return this.error="End of input reached before message was fully decoded",-1;if(e===r){r+=255;const e=new Uint8Array(r);for(let r=0;r<t.length;r++)e[r]=t[r];t=this.valueHexView=new Uint8Array(r)}}this.blockLength=e+1,t[e-1]=127&s[e];const i=new Uint8Array(e);for(let r=0;r<e;r++)i[r]=t[r];t=this.valueHexView=new Uint8Array(e),t.set(i),this.blockLength<=9?this.tagNumber=f(t,7):(this.isHexOnly=!0,this.warnings.push("Tag too long, represented as hex-coded"))}if(1===this.tagClass&&this.isConstructed)switch(this.tagNumber){case 1:case 2:case 5:case 6:case 9:case 13:case 14:case 23:case 24:case 31:case 32:case 33:case 34:return this.error="Constructed encoding used for primitive type",-1}return t+this.blockLength}toJSON(){return{...super.toJSON(),tagClass:this.tagClass,tagNumber:this.tagNumber,isConstructed:this.isConstructed}}}R.NAME="identificationBlock";class V extends U{constructor({lenBlock:e={}}={}){var t,r,i;super(),this.isIndefiniteForm=null!==(t=e.isIndefiniteForm)&&void 0!==t&&t,this.longFormUsed=null!==(r=e.longFormUsed)&&void 0!==r&&r,this.length=null!==(i=e.length)&&void 0!==i?i:0}fromBER(e,t,r){const i=n.toUint8Array(e);if(!w(this,i,t,r))return-1;const s=i.subarray(t,t+r);if(0===s.length)return this.error="Zero buffer length",-1;if(255===s[0])return this.error="Length block 0xFF is reserved by standard",-1;if(this.isIndefiniteForm=128===s[0],this.isIndefiniteForm)return this.blockLength=1,t+this.blockLength;if(this.longFormUsed=!!(128&s[0]),!1===this.longFormUsed)return this.length=s[0],this.blockLength=1,t+this.blockLength;const o=127&s[0];if(o>8)return this.error="Too big integer",-1;if(o+1>s.length)return this.error="End of input reached before message was fully decoded",-1;const a=t+1,c=i.subarray(a,a+o);return 0===c[o-1]&&this.warnings.push("Needlessly long encoded length"),this.length=f(c,8),this.longFormUsed&&this.length<=127&&this.warnings.push("Unnecessary usage of long length form"),this.blockLength=o+1,t+this.blockLength}toBER(e=!1){let t,r;if(this.length>127&&(this.longFormUsed=!0),this.isIndefiniteForm)return t=new ArrayBuffer(1),!1===e&&(r=new Uint8Array(t),r[0]=128),t;if(this.longFormUsed){const i=y(this.length,8);if(i.byteLength>127)return this.error="Too big length",C;if(t=new ArrayBuffer(i.byteLength+1),e)return t;const n=new Uint8Array(i);r=new Uint8Array(t),r[0]=128|i.byteLength;for(let e=0;e<i.byteLength;e++)r[e+1]=n[e];return t}return t=new ArrayBuffer(1),!1===e&&(r=new Uint8Array(t),r[0]=this.length),t}toJSON(){return{...super.toJSON(),isIndefiniteForm:this.isIndefiniteForm,longFormUsed:this.longFormUsed,length:this.length}}}V.NAME="lengthBlock";const D={};class L extends U{constructor({name:e="",optional:t=!1,primitiveSchema:r,...i}={},n){super(i),this.name=e,this.optional=t,r&&(this.primitiveSchema=r),this.idBlock=new R(i),this.lenBlock=new V(i),this.valueBlock=n?new n(i):new P(i)}fromBER(e,t,r){const i=this.valueBlock.fromBER(e,t,this.lenBlock.isIndefiniteForm?r:this.lenBlock.length);return-1===i?(this.error=this.valueBlock.error,i):(this.idBlock.error.length||(this.blockLength+=this.idBlock.blockLength),this.lenBlock.error.length||(this.blockLength+=this.lenBlock.blockLength),this.valueBlock.error.length||(this.blockLength+=this.valueBlock.blockLength),i)}toBER(e,t){const r=t||new A;t||H(this);const i=this.idBlock.toBER(e);if(r.write(i),this.lenBlock.isIndefiniteForm)r.write(new Uint8Array([128]).buffer),this.valueBlock.toBER(e,r),r.write(new ArrayBuffer(2));else{const t=this.valueBlock.toBER(e);this.lenBlock.length=t.byteLength;const i=this.lenBlock.toBER(e);r.write(i),r.write(t)}return t?C:r.final()}toJSON(){const e={...super.toJSON(),idBlock:this.idBlock.toJSON(),lenBlock:this.lenBlock.toJSON(),valueBlock:this.valueBlock.toJSON(),name:this.name,optional:this.optional};return this.primitiveSchema&&(e.primitiveSchema=this.primitiveSchema.toJSON()),e}toString(e="ascii"){return"ascii"===e?this.onAsciiEncoding():p.ToHex(this.toBER())}onAsciiEncoding(){return`${this.constructor.NAME} : ${p.ToHex(this.valueBlock.valueBeforeDecodeView)}`}isEqual(e){if(this===e)return!0;if(!(e instanceof this.constructor))return!1;return function(e,t){if(e.byteLength!==t.byteLength)return!1;const r=new Uint8Array(e),i=new Uint8Array(t);for(let e=0;e<r.length;e++)if(r[e]!==i[e])return!1;return!0}(this.toBER(),e.toBER())}}function H(e){if(e instanceof D.Constructed)for(const t of e.valueBlock.value)H(t)&&(e.lenBlock.isIndefiniteForm=!0);return!!e.lenBlock.isIndefiniteForm}L.NAME="BaseBlock";class M extends L{constructor({value:e="",...t}={},r){super(t,r),e&&this.fromString(e)}getValue(){return this.valueBlock.value}setValue(e){this.valueBlock.value=e}fromBER(e,t,r){const i=this.valueBlock.fromBER(e,t,this.lenBlock.isIndefiniteForm?r:this.lenBlock.length);return-1===i?(this.error=this.valueBlock.error,i):(this.fromBuffer(this.valueBlock.valueHexView),this.idBlock.error.length||(this.blockLength+=this.idBlock.blockLength),this.lenBlock.error.length||(this.blockLength+=this.lenBlock.blockLength),this.valueBlock.error.length||(this.blockLength+=this.valueBlock.blockLength),i)}onAsciiEncoding(){return`${this.constructor.NAME} : '${this.valueBlock.value}'`}}M.NAME="BaseStringBlock";class $ extends(j(P)){constructor({isHexOnly:e=!0,...t}={}){super(t),this.isHexOnly=e}}var K,F,_,z,q,W;$.NAME="PrimitiveValueBlock";class G extends L{constructor(e={}){super(e,$),this.idBlock.isConstructed=!1}}function J(e,t=0,r=e.length){const i=t;let n=new L({},P);const s=new U;if(!w(s,e,t,r))return n.error=s.error,{offset:-1,result:n};if(!e.subarray(t,t+r).length)return n.error="Zero buffer length",{offset:-1,result:n};let o=n.idBlock.fromBER(e,t,r);if(n.idBlock.warnings.length&&n.warnings.concat(n.idBlock.warnings),-1===o)return n.error=n.idBlock.error,{offset:-1,result:n};if(t=o,r-=n.idBlock.blockLength,o=n.lenBlock.fromBER(e,t,r),n.lenBlock.warnings.length&&n.warnings.concat(n.lenBlock.warnings),-1===o)return n.error=n.lenBlock.error,{offset:-1,result:n};if(t=o,r-=n.lenBlock.blockLength,!n.idBlock.isConstructed&&n.lenBlock.isIndefiniteForm)return n.error="Indefinite length form used for primitive encoding form",{offset:-1,result:n};let a=L;if(1===n.idBlock.tagClass){if(n.idBlock.tagNumber>=37&&!1===n.idBlock.isHexOnly)return n.error="UNIVERSAL 37 and upper tags are reserved by ASN.1 standard",{offset:-1,result:n};switch(n.idBlock.tagNumber){case 0:if(n.idBlock.isConstructed&&n.lenBlock.length>0)return n.error="Type [UNIVERSAL 0] is reserved",{offset:-1,result:n};a=D.EndOfContent;break;case 1:a=D.Boolean;break;case 2:a=D.Integer;break;case 3:a=D.BitString;break;case 4:a=D.OctetString;break;case 5:a=D.Null;break;case 6:a=D.ObjectIdentifier;break;case 10:a=D.Enumerated;break;case 12:a=D.Utf8String;break;case 13:a=D.RelativeObjectIdentifier;break;case 14:a=D.TIME;break;case 15:return n.error="[UNIVERSAL 15] is reserved by ASN.1 standard",{offset:-1,result:n};case 16:a=D.Sequence;break;case 17:a=D.Set;break;case 18:a=D.NumericString;break;case 19:a=D.PrintableString;break;case 20:a=D.TeletexString;break;case 21:a=D.VideotexString;break;case 22:a=D.IA5String;break;case 23:a=D.UTCTime;break;case 24:a=D.GeneralizedTime;break;case 25:a=D.GraphicString;break;case 26:a=D.VisibleString;break;case 27:a=D.GeneralString;break;case 28:a=D.UniversalString;break;case 29:a=D.CharacterString;break;case 30:a=D.BmpString;break;case 31:a=D.DATE;break;case 32:a=D.TimeOfDay;break;case 33:a=D.DateTime;break;case 34:a=D.Duration;break;default:{const e=n.idBlock.isConstructed?new D.Constructed:new D.Primitive;e.idBlock=n.idBlock,e.lenBlock=n.lenBlock,e.warnings=n.warnings,n=e}}}else a=n.idBlock.isConstructed?D.Constructed:D.Primitive;return n=function(e,t){if(e instanceof t)return e;const r=new t;return r.idBlock=e.idBlock,r.lenBlock=e.lenBlock,r.warnings=e.warnings,r.valueBeforeDecodeView=e.valueBeforeDecodeView,r}(n,a),o=n.fromBER(e,t,n.lenBlock.isIndefiniteForm?r:n.lenBlock.length),n.valueBeforeDecodeView=e.subarray(i,i+n.blockLength),{offset:o,result:n}}function X(e){if(!e.byteLength){const e=new L({},P);return e.error="Input buffer has zero length",{offset:-1,result:e}}return J(n.toUint8Array(e).slice(),0,e.byteLength)}K=G,D.Primitive=K,G.NAME="PRIMITIVE";class Z extends P{constructor({value:e=[],isIndefiniteForm:t=!1,...r}={}){super(r),this.value=e,this.isIndefiniteForm=t}fromBER(e,t,r){const i=n.toUint8Array(e);if(!w(this,i,t,r))return-1;if(this.valueBeforeDecodeView=i.subarray(t,t+r),0===this.valueBeforeDecodeView.length)return this.warnings.push("Zero buffer length"),t;let s=t;for(;o=this.isIndefiniteForm,a=r,(o?1:a)>0;){const e=J(i,s,r);if(-1===e.offset)return this.error=e.result.error,this.warnings.concat(e.result.warnings),-1;if(s=e.offset,this.blockLength+=e.result.blockLength,r-=e.result.blockLength,this.value.push(e.result),this.isIndefiniteForm&&e.result.constructor.NAME===N)break}var o,a;return this.isIndefiniteForm&&(this.value[this.value.length-1].constructor.NAME===N?this.value.pop():this.warnings.push("No EndOfContent block encoded")),s}toBER(e,t){const r=t||new A;for(let t=0;t<this.value.length;t++)this.value[t].toBER(e,r);return t?C:r.final()}toJSON(){const e={...super.toJSON(),isIndefiniteForm:this.isIndefiniteForm,value:[]};for(const t of this.value)e.value.push(t.toJSON());return e}}Z.NAME="ConstructedValueBlock";class Y extends L{constructor(e={}){super(e,Z),this.idBlock.isConstructed=!0}fromBER(e,t,r){this.valueBlock.isIndefiniteForm=this.lenBlock.isIndefiniteForm;const i=this.valueBlock.fromBER(e,t,this.lenBlock.isIndefiniteForm?r:this.lenBlock.length);return-1===i?(this.error=this.valueBlock.error,i):(this.idBlock.error.length||(this.blockLength+=this.idBlock.blockLength),this.lenBlock.error.length||(this.blockLength+=this.lenBlock.blockLength),this.valueBlock.error.length||(this.blockLength+=this.valueBlock.blockLength),i)}onAsciiEncoding(){const e=[];for(const t of this.valueBlock.value)e.push(t.toString("ascii").split("\n").map((e=>` ${e}`)).join("\n"));const t=3===this.idBlock.tagClass?`[${this.idBlock.tagNumber}]`:this.constructor.NAME;return e.length?`${t} :\n${e.join("\n")}`:`${t} :`}}F=Y,D.Constructed=F,Y.NAME="CONSTRUCTED";class Q extends P{fromBER(e,t,r){return t}toBER(e){return C}}Q.override="EndOfContentValueBlock";class ee extends L{constructor(e={}){super(e,Q),this.idBlock.tagClass=1,this.idBlock.tagNumber=0}}_=ee,D.EndOfContent=_,ee.NAME=N;class te extends L{constructor(e={}){super(e,P),this.idBlock.tagClass=1,this.idBlock.tagNumber=5}fromBER(e,t,r){return this.lenBlock.length>0&&this.warnings.push("Non-zero length of value block for Null type"),this.idBlock.error.length||(this.blockLength+=this.idBlock.blockLength),this.lenBlock.error.length||(this.blockLength+=this.lenBlock.blockLength),this.blockLength+=r,t+r>e.byteLength?(this.error="End of input reached before message was fully decoded (inconsistent offset and length values)",-1):t+r}toBER(e,t){const r=new ArrayBuffer(2);if(!e){const e=new Uint8Array(r);e[0]=5,e[1]=0}return t&&t.write(r),r}onAsciiEncoding(){return`${this.constructor.NAME}`}}z=te,D.Null=z,te.NAME="NULL";class re extends(j(P)){constructor({value:e,...t}={}){super(t),t.valueHex?this.valueHexView=n.toUint8Array(t.valueHex):this.valueHexView=new Uint8Array(1),e&&(this.value=e)}get value(){for(const e of this.valueHexView)if(e>0)return!0;return!1}set value(e){this.valueHexView[0]=e?255:0}fromBER(e,t,r){const i=n.toUint8Array(e);return w(this,i,t,r)?(this.valueHexView=i.subarray(t,t+r),r>1&&this.warnings.push("Boolean value encoded in more then 1 octet"),this.isHexOnly=!0,g.call(this),this.blockLength=r,t+r):-1}toBER(){return this.valueHexView.slice()}toJSON(){return{...super.toJSON(),value:this.value}}}re.NAME="BooleanValueBlock";class ie extends L{constructor(e={}){super(e,re),this.idBlock.tagClass=1,this.idBlock.tagNumber=1}getValue(){return this.valueBlock.value}setValue(e){this.valueBlock.value=e}onAsciiEncoding(){return`${this.constructor.NAME} : ${this.getValue}`}}q=ie,D.Boolean=q,ie.NAME="BOOLEAN";class ne extends(j(Z)){constructor({isConstructed:e=!1,...t}={}){super(t),this.isConstructed=e}fromBER(e,t,r){let i=0;if(this.isConstructed){if(this.isHexOnly=!1,i=Z.prototype.fromBER.call(this,e,t,r),-1===i)return i;for(let e=0;e<this.value.length;e++){const t=this.value[e].constructor.NAME;if(t===N){if(this.isIndefiniteForm)break;return this.error="EndOfContent is unexpected, OCTET STRING may consists of OCTET STRINGs only",-1}if(t!==I)return this.error="OCTET STRING may consists of OCTET STRINGs only",-1}}else this.isHexOnly=!0,i=super.fromBER(e,t,r),this.blockLength=r;return i}toBER(e,t){return this.isConstructed?Z.prototype.toBER.call(this,e,t):e?new ArrayBuffer(this.valueHexView.byteLength):this.valueHexView.slice().buffer}toJSON(){return{...super.toJSON(),isConstructed:this.isConstructed}}}ne.NAME="OctetStringValueBlock";let se=class e extends L{constructor({idBlock:e={},lenBlock:t={},...r}={}){var i,n;null!==(i=r.isConstructed)&&void 0!==i||(r.isConstructed=!!(null===(n=r.value)||void 0===n?void 0:n.length)),super({idBlock:{isConstructed:r.isConstructed,...e},lenBlock:{...t,isIndefiniteForm:!!r.isIndefiniteForm},...r},ne),this.idBlock.tagClass=1,this.idBlock.tagNumber=4}fromBER(e,t,r){if(this.valueBlock.isConstructed=this.idBlock.isConstructed,this.valueBlock.isIndefiniteForm=this.lenBlock.isIndefiniteForm,0===r)return 0===this.idBlock.error.length&&(this.blockLength+=this.idBlock.blockLength),0===this.lenBlock.error.length&&(this.blockLength+=this.lenBlock.blockLength),t;if(!this.valueBlock.isConstructed){const i=(e instanceof ArrayBuffer?new Uint8Array(e):e).subarray(t,t+r);try{if(i.byteLength){const e=J(i,0,i.byteLength);-1!==e.offset&&e.offset===r&&(this.valueBlock.value=[e.result])}}catch(e){}}return super.fromBER(e,t,r)}onAsciiEncoding(){return this.valueBlock.isConstructed||this.valueBlock.value&&this.valueBlock.value.length?Y.prototype.onAsciiEncoding.call(this):`${this.constructor.NAME} : ${p.ToHex(this.valueBlock.valueHexView)}`}getValue(){if(!this.idBlock.isConstructed)return this.valueBlock.valueHexView.slice().buffer;const t=[];for(const r of this.valueBlock.value)r instanceof e&&t.push(r.valueBlock.valueHexView);return n.concat(t)}};W=se,D.OctetString=W,se.NAME=I;class oe extends(j(Z)){constructor({unusedBits:e=0,isConstructed:t=!1,...r}={}){super(r),this.unusedBits=e,this.isConstructed=t,this.blockLength=this.valueHexView.byteLength}fromBER(e,t,r){if(!r)return t;let i=-1;if(this.isConstructed){if(i=Z.prototype.fromBER.call(this,e,t,r),-1===i)return i;for(const e of this.value){const t=e.constructor.NAME;if(t===N){if(this.isIndefiniteForm)break;return this.error="EndOfContent is unexpected, BIT STRING may consists of BIT STRINGs only",-1}if(t!==T)return this.error="BIT STRING may consists of BIT STRINGs only",-1;const r=e.valueBlock;if(this.unusedBits>0&&r.unusedBits>0)return this.error='Using of "unused bits" inside constructive BIT STRING allowed for least one only',-1;this.unusedBits=r.unusedBits}return i}const s=n.toUint8Array(e);if(!w(this,s,t,r))return-1;const o=s.subarray(t,t+r);if(this.unusedBits=o[0],this.unusedBits>7)return this.error="Unused bits for BitString must be in range 0-7",-1;if(!this.unusedBits){const e=o.subarray(1);try{if(e.byteLength){const t=J(e,0,e.byteLength);-1!==t.offset&&t.offset===r-1&&(this.value=[t.result])}}catch(e){}}return this.valueHexView=o.subarray(1),this.blockLength=o.length,t+r}toBER(e,t){if(this.isConstructed)return Z.prototype.toBER.call(this,e,t);if(e)return new ArrayBuffer(this.valueHexView.byteLength+1);if(!this.valueHexView.byteLength)return C;const r=new Uint8Array(this.valueHexView.length+1);return r[0]=this.unusedBits,r.set(this.valueHexView,1),r.buffer}toJSON(){return{...super.toJSON(),unusedBits:this.unusedBits,isConstructed:this.isConstructed}}}var ae;oe.NAME="BitStringValueBlock";let ce=class extends L{constructor({idBlock:e={},lenBlock:t={},...r}={}){var i,n;null!==(i=r.isConstructed)&&void 0!==i||(r.isConstructed=!!(null===(n=r.value)||void 0===n?void 0:n.length)),super({idBlock:{isConstructed:r.isConstructed,...e},lenBlock:{...t,isIndefiniteForm:!!r.isIndefiniteForm},...r},oe),this.idBlock.tagClass=1,this.idBlock.tagNumber=3}fromBER(e,t,r){return this.valueBlock.isConstructed=this.idBlock.isConstructed,this.valueBlock.isIndefiniteForm=this.lenBlock.isIndefiniteForm,super.fromBER(e,t,r)}onAsciiEncoding(){if(this.valueBlock.isConstructed||this.valueBlock.value&&this.valueBlock.value.length)return Y.prototype.onAsciiEncoding.call(this);{const e=[],t=this.valueBlock.valueHexView;for(const r of t)e.push(r.toString(2).padStart(8,"0"));const r=e.join("");return`${this.constructor.NAME} : ${r.substring(0,r.length-this.valueBlock.unusedBits)}`}}};var le,ue,pe,he,fe,ye,de;function ge(e,t){const r=new Uint8Array([0]),i=new Uint8Array(e),n=new Uint8Array(t);let s=i.slice(0);const o=s.length-1,a=n.slice(0),c=a.length-1;let l=0;let u=0;for(let e=c<o?o:c;e>=0;e--,u++){if(!0==u<a.length)l=s[o-u]+a[c-u]+r[0];else l=s[o-u]+r[0];if(r[0]=l/10,!0==u>=s.length)s=d(new Uint8Array([l%10]),s);else s[o-u]=l%10}return r[0]>0&&(s=d(r,s)),s}function ve(e){if(e>=S.length)for(let t=S.length;t<=e;t++){const e=new Uint8Array([0]);let r=S[t-1].slice(0);for(let t=r.length-1;t>=0;t--){const i=new Uint8Array([(r[t]<<1)+e[0]]);e[0]=i[0]/10,r[t]=i[0]%10}e[0]>0&&(r=d(e,r)),S.push(r)}return S[e]}function me(e,t){let r=0;const i=new Uint8Array(e),n=new Uint8Array(t),s=i.slice(0),o=s.length-1,a=n.slice(0),c=a.length-1;let l,u=0;for(let e=c;e>=0;e--,u++)if(l=s[o-u]-a[c-u]-r,!0==l<0)r=1,s[o-u]=l+10;else r=0,s[o-u]=l;if(r>0)for(let e=o-c+1;e>=0;e--,u++){if(l=s[o-u]-r,!(l<0)){r=0,s[o-u]=l;break}r=1,s[o-u]=l+10}return s.slice()}ae=ce,D.BitString=ae,ce.NAME=T;class be extends(j(P)){constructor({value:e,...t}={}){super(t),this._valueDec=0,t.valueHex&&this.setValueHex(),void 0!==e&&(this.valueDec=e)}setValueHex(){this.valueHexView.length>=4?(this.warnings.push("Too big Integer for decoding, hex only"),this.isHexOnly=!0,this._valueDec=0):(this.isHexOnly=!1,this.valueHexView.length>0&&(this._valueDec=g.call(this)))}set valueDec(e){this._valueDec=e,this.isHexOnly=!1,this.valueHexView=new Uint8Array(function(e){const t=e<0?-1*e:e;let r=128;for(let i=1;i<8;i++){if(t<=r){if(e<0){const e=y(r-t,8,i);return new Uint8Array(e)[0]|=128,e}let n=y(t,8,i),s=new Uint8Array(n);if(128&s[0]){const e=n.slice(0),t=new Uint8Array(e);n=new ArrayBuffer(n.byteLength+1),s=new Uint8Array(n);for(let r=0;r<e.byteLength;r++)s[r+1]=t[r];s[0]=0}return n}r*=Math.pow(2,8)}return new ArrayBuffer(0)}(e))}get valueDec(){return this._valueDec}fromDER(e,t,r,i=0){const n=this.fromBER(e,t,r);if(-1===n)return n;const s=this.valueHexView;return 0===s[0]&&128&s[1]?this.valueHexView=s.subarray(1):0!==i&&s.length<i&&(i-s.length>1&&(i=s.length+1),this.valueHexView=s.subarray(i-s.length)),n}toDER(e=!1){const t=this.valueHexView;switch(!0){case!!(128&t[0]):{const e=new Uint8Array(this.valueHexView.length+1);e[0]=0,e.set(t,1),this.valueHexView=e}break;case 0===t[0]&&!(128&t[1]):this.valueHexView=this.valueHexView.subarray(1)}return this.toBER(e)}fromBER(e,t,r){const i=super.fromBER(e,t,r);return-1===i||this.setValueHex(),i}toBER(e){return e?new ArrayBuffer(this.valueHexView.length):this.valueHexView.slice().buffer}toJSON(){return{...super.toJSON(),valueDec:this.valueDec}}toString(){const e=8*this.valueHexView.length-1;let t,r=new Uint8Array(8*this.valueHexView.length/3),i=0;const n=this.valueHexView;let s="",o=!1;for(let o=n.byteLength-1;o>=0;o--){t=n[o];for(let n=0;n<8;n++){if(!(1&~t))if(i===e)r=me(ve(i),r),s="-";else r=ge(r,ve(i));i++,t>>=1}}for(let e=0;e<r.length;e++)r[e]&&(o=!0),o&&(s+=x.charAt(r[e]));return!1===o&&(s+=x.charAt(0)),s}}le=be,be.NAME="IntegerValueBlock",Object.defineProperty(le.prototype,"valueHex",{set:function(e){this.valueHexView=new Uint8Array(e),this.setValueHex()},get:function(){return this.valueHexView.slice().buffer}});class we extends L{constructor(e={}){super(e,be),this.idBlock.tagClass=1,this.idBlock.tagNumber=2}toBigInt(){return m(),BigInt(this.valueBlock.toString())}static fromBigInt(e){m();const t=BigInt(e),r=new A,i=t.toString(16).replace(/^-/,""),s=new Uint8Array(p.FromHex(i));if(t<0){const e=new Uint8Array(s.length+(128&s[0]?1:0));e[0]|=128;const i=BigInt(`0x${p.ToHex(e)}`)+t,o=n.toUint8Array(p.FromHex(i.toString(16)));o[0]|=128,r.write(o)}else 128&s[0]&&r.write(new Uint8Array([0])),r.write(s);return new we({valueHex:r.final()})}convertToDER(){const e=new we({valueHex:this.valueBlock.valueHexView});return e.valueBlock.toDER(),e}convertFromDER(){return new we({valueHex:0===this.valueBlock.valueHexView[0]?this.valueBlock.valueHexView.subarray(1):this.valueBlock.valueHexView})}onAsciiEncoding(){return`${this.constructor.NAME} : ${this.valueBlock.toString()}`}}ue=we,D.Integer=ue,we.NAME="INTEGER";class Ae extends we{constructor(e={}){super(e),this.idBlock.tagClass=1,this.idBlock.tagNumber=10}}pe=Ae,D.Enumerated=pe,Ae.NAME="ENUMERATED";class Se extends(j(P)){constructor({valueDec:e=-1,isFirstSid:t=!1,...r}={}){super(r),this.valueDec=e,this.isFirstSid=t}fromBER(e,t,r){if(!r)return t;const i=n.toUint8Array(e);if(!w(this,i,t,r))return-1;const s=i.subarray(t,t+r);this.valueHexView=new Uint8Array(r);for(let e=0;e<r&&(this.valueHexView[e]=127&s[e],this.blockLength++,128&s[e]);e++);const o=new Uint8Array(this.blockLength);for(let e=0;e<this.blockLength;e++)o[e]=this.valueHexView[e];return this.valueHexView=o,128&s[this.blockLength-1]?(this.error="End of input reached before message was fully decoded",-1):(0===this.valueHexView[0]&&this.warnings.push("Needlessly long format of SID encoding"),this.blockLength<=8?this.valueDec=f(this.valueHexView,7):(this.isHexOnly=!0,this.warnings.push("Too big SID for decoding, hex only")),t+this.blockLength)}set valueBigInt(e){m();let t=BigInt(e).toString(2);for(;t.length%7;)t="0"+t;const r=new Uint8Array(t.length/7);for(let e=0;e<r.length;e++)r[e]=parseInt(t.slice(7*e,7*e+7),2)+(e+1<r.length?128:0);this.fromBER(r.buffer,0,r.length)}toBER(e){if(this.isHexOnly){if(e)return new ArrayBuffer(this.valueHexView.byteLength);const t=this.valueHexView,r=new Uint8Array(this.blockLength);for(let e=0;e<this.blockLength-1;e++)r[e]=128|t[e];return r[this.blockLength-1]=t[this.blockLength-1],r.buffer}const t=y(this.valueDec,7);if(0===t.byteLength)return this.error="Error during encoding SID value",C;const r=new Uint8Array(t.byteLength);if(!e){const e=new Uint8Array(t),i=t.byteLength-1;for(let t=0;t<i;t++)r[t]=128|e[t];r[i]=e[i]}return r}toString(){let e="";if(this.isHexOnly)e=p.ToHex(this.valueHexView);else if(this.isFirstSid){let t=this.valueDec;this.valueDec<=39?e="0.":this.valueDec<=79?(e="1.",t-=40):(e="2.",t-=80),e+=t.toString()}else e=this.valueDec.toString();return e}toJSON(){return{...super.toJSON(),valueDec:this.valueDec,isFirstSid:this.isFirstSid}}}Se.NAME="sidBlock";class xe extends P{constructor({value:e="",...t}={}){super(t),this.value=[],e&&this.fromString(e)}fromBER(e,t,r){let i=t;for(;r>0;){const t=new Se;if(i=t.fromBER(e,i,r),-1===i)return this.blockLength=0,this.error=t.error,i;0===this.value.length&&(t.isFirstSid=!0),this.blockLength+=t.blockLength,r-=t.blockLength,this.value.push(t)}return i}toBER