import { __commonJS } from "../../../_virtual/rolldown_runtime.mjs";
import { require_forge } from "./forge.mjs";
import { require_util } from "./util.mjs";
import { require_oids } from "./oids.mjs";

//#region ../../node_modules/node-forge/lib/asn1.js
var require_asn1 = /* @__PURE__ */ __commonJS({ "../../node_modules/node-forge/lib/asn1.js": ((exports, module) => {
	/**
	* Javascript implementation of Abstract Syntax Notation Number One.
	*
	* @author Dave Longley
	*
	* Copyright (c) 2010-2015 Digital Bazaar, Inc.
	*
	* An API for storing data using the Abstract Syntax Notation Number One
	* format using DER (Distinguished Encoding Rules) encoding. This encoding is
	* commonly used to store data for PKI, i.e. X.509 Certificates, and this
	* implementation exists for that purpose.
	*
	* Abstract Syntax Notation Number One (ASN.1) is used to define the abstract
	* syntax of information without restricting the way the information is encoded
	* for transmission. It provides a standard that allows for open systems
	* communication. ASN.1 defines the syntax of information data and a number of
	* simple data types as well as a notation for describing them and specifying
	* values for them.
	*
	* The RSA algorithm creates public and private keys that are often stored in
	* X.509 or PKCS#X formats -- which use ASN.1 (encoded in DER format). This
	* class provides the most basic functionality required to store and load DSA
	* keys that are encoded according to ASN.1.
	*
	* The most common binary encodings for ASN.1 are BER (Basic Encoding Rules)
	* and DER (Distinguished Encoding Rules). DER is just a subset of BER that
	* has stricter requirements for how data must be encoded.
	*
	* Each ASN.1 structure has a tag (a byte identifying the ASN.1 structure type)
	* and a byte array for the value of this ASN1 structure which may be data or a
	* list of ASN.1 structures.
	*
	* Each ASN.1 structure using BER is (Tag-Length-Value):
	*
	* | byte 0 | bytes X | bytes Y |
	* |--------|---------|----------
	* |  tag   | length  |  value  |
	*
	* ASN.1 allows for tags to be of "High-tag-number form" which allows a tag to
	* be two or more octets, but that is not supported by this class. A tag is
	* only 1 byte. Bits 1-5 give the tag number (ie the data type within a
	* particular 'class'), 6 indicates whether or not the ASN.1 value is
	* constructed from other ASN.1 values, and bits 7 and 8 give the 'class'. If
	* bits 7 and 8 are both zero, the class is UNIVERSAL. If only bit 7 is set,
	* then the class is APPLICATION. If only bit 8 is set, then the class is
	* CONTEXT_SPECIFIC. If both bits 7 and 8 are set, then the class is PRIVATE.
	* The tag numbers for the data types for the class UNIVERSAL are listed below:
	*
	* UNIVERSAL 0 Reserved for use by the encoding rules
	* UNIVERSAL 1 Boolean type
	* UNIVERSAL 2 Integer type
	* UNIVERSAL 3 Bitstring type
	* UNIVERSAL 4 Octetstring type
	* UNIVERSAL 5 Null type
	* UNIVERSAL 6 Object identifier type
	* UNIVERSAL 7 Object descriptor type
	* UNIVERSAL 8 External type and Instance-of type
	* UNIVERSAL 9 Real type
	* UNIVERSAL 10 Enumerated type
	* UNIVERSAL 11 Embedded-pdv type
	* UNIVERSAL 12 UTF8String type
	* UNIVERSAL 13 Relative object identifier type
	* UNIVERSAL 14-15 Reserved for future editions
	* UNIVERSAL 16 Sequence and Sequence-of types
	* UNIVERSAL 17 Set and Set-of types
	* UNIVERSAL 18-22, 25-30 Character string types
	* UNIVERSAL 23-24 Time types
	*
	* The length of an ASN.1 structure is specified after the tag identifier.
	* There is a definite form and an indefinite form. The indefinite form may
	* be used if the encoding is constructed and not all immediately available.
	* The indefinite form is encoded using a length byte with only the 8th bit
	* set. The end of the constructed object is marked using end-of-contents
	* octets (two zero bytes).
	*
	* The definite form looks like this:
	*
	* The length may take up 1 or more bytes, it depends on the length of the
	* value of the ASN.1 structure. DER encoding requires that if the ASN.1
	* structure has a value that has a length greater than 127, more than 1 byte
	* will be used to store its length, otherwise just one byte will be used.
	* This is strict.
	*
	* In the case that the length of the ASN.1 value is less than 127, 1 octet
	* (byte) is used to store the "short form" length. The 8th bit has a value of
	* 0 indicating the length is "short form" and not "long form" and bits 7-1
	* give the length of the data. (The 8th bit is the left-most, most significant
	* bit: also known as big endian or network format).
	*
	* In the case that the length of the ASN.1 value is greater than 127, 2 to
	* 127 octets (bytes) are used to store the "long form" length. The first
	* byte's 8th bit is set to 1 to indicate the length is "long form." Bits 7-1
	* give the number of additional octets. All following octets are in base 256
	* with the most significant digit first (typical big-endian binary unsigned
	* integer storage). So, for instance, if the length of a value was 257, the
	* first byte would be set to:
	*
	* 10000010 = 130 = 0x82.
	*
	* This indicates there are 2 octets (base 256) for the length. The second and
	* third bytes (the octets just mentioned) would store the length in base 256:
	*
	* octet 2: 00000001 = 1 * 256^1 = 256
	* octet 3: 00000001 = 1 * 256^0 = 1
	* total = 257
	*
	* The algorithm for converting a js integer value of 257 to base-256 is:
	*
	* var value = 257;
	* var bytes = [];
	* bytes[0] = (value >>> 8) & 0xFF; // most significant byte first
	* bytes[1] = value & 0xFF;        // least significant byte last
	*
	* On the ASN.1 UNIVERSAL Object Identifier (OID) type:
	*
	* An OID can be written like: "value1.value2.value3...valueN"
	*
	* The DER encoding rules:
	*
	* The first byte has the value 40 * value1 + value2.
	* The following bytes, if any, encode the remaining values. Each value is
	* encoded in base 128, most significant digit first (big endian), with as
	* few digits as possible, and the most significant bit of each byte set
	* to 1 except the last in each value's encoding. For example: Given the
	* OID "1.2.840.113549", its DER encoding is (remember each byte except the
	* last one in each encoding is OR'd with 0x80):
	*
	* byte 1: 40 * 1 + 2 = 42 = 0x2A.
	* bytes 2-3: 128 * 6 + 72 = 840 = 6 72 = 6 72 = 0x0648 = 0x8648
	* bytes 4-6: 16384 * 6 + 128 * 119 + 13 = 6 119 13 = 0x06770D = 0x86F70D
	*
	* The final value is: 0x2A864886F70D.
	* The full OID (including ASN.1 tag and length of 6 bytes) is:
	* 0x06062A864886F70D
	*/
	var forge = require_forge();
	require_util();
	require_oids();
	var asn1 = module.exports = forge.asn1 = forge.asn1 || {};
	/**
	* ASN.1 classes.
	*/
	asn1.Class = {
		UNIVERSAL: 0,
		APPLICATION: 64,
		CONTEXT_SPECIFIC: 128,
		PRIVATE: 192
	};
	/**
	* ASN.1 types. Not all types are supported by this implementation, only
	* those necessary to implement a simple PKI are implemented.
	*/
	asn1.Type = {
		NONE: 0,
		BOOLEAN: 1,
		INTEGER: 2,
		BITSTRING: 3,
		OCTETSTRING: 4,
		NULL: 5,
		OID: 6,
		ODESC: 7,
		EXTERNAL: 8,
		REAL: 9,
		ENUMERATED: 10,
		EMBEDDED: 11,
		UTF8: 12,
		ROID: 13,
		SEQUENCE: 16,
		SET: 17,
		PRINTABLESTRING: 19,
		IA5STRING: 22,
		UTCTIME: 23,
		GENERALIZEDTIME: 24,
		BMPSTRING: 30
	};
	/**
	* Creates a new asn1 object.
	*
	* @param tagClass the tag class for the object.
	* @param type the data type (tag number) for the object.
	* @param constructed true if the asn1 object is in constructed form.
	* @param value the value for the object, if it is not constructed.
	* @param [options] the options to use:
	*          [bitStringContents] the plain BIT STRING content including padding
	*            byte.
	*
	* @return the asn1 object.
	*/
	asn1.create = function(tagClass, type, constructed, value, options) {
		if (forge.util.isArray(value)) {
			var tmp = [];
			for (var i = 0; i < value.length; ++i) if (value[i] !== void 0) tmp.push(value[i]);
			value = tmp;
		}
		var obj = {
			tagClass,
			type,
			constructed,
			composed: constructed || forge.util.isArray(value),
			value
		};
		if (options && "bitStringContents" in options) {
			obj.bitStringContents = options.bitStringContents;
			obj.original = asn1.copy(obj);
		}
		return obj;
	};
	/**
	* Copies an asn1 object.
	*
	* @param obj the asn1 object.
	* @param [options] copy options:
	*          [excludeBitStringContents] true to not copy bitStringContents
	*
	* @return the a copy of the asn1 object.
	*/
	asn1.copy = function(obj, options) {
		var copy;
		if (forge.util.isArray(obj)) {
			copy = [];
			for (var i = 0; i < obj.length; ++i) copy.push(asn1.copy(obj[i], options));
			return copy;
		}
		if (typeof obj === "string") return obj;
		copy = {
			tagClass: obj.tagClass,
			type: obj.type,
			constructed: obj.constructed,
			composed: obj.composed,
			value: asn1.copy(obj.value, options)
		};
		if (options && !options.excludeBitStringContents) copy.bitStringContents = obj.bitStringContents;
		return copy;
	};
	/**
	* Compares asn1 objects for equality.
	*
	* Note this function does not run in constant time.
	*
	* @param obj1 the first asn1 object.
	* @param obj2 the second asn1 object.
	* @param [options] compare options:
	*          [includeBitStringContents] true to compare bitStringContents
	*
	* @return true if the asn1 objects are equal.
	*/
	asn1.equals = function(obj1, obj2, options) {
		if (forge.util.isArray(obj1)) {
			if (!forge.util.isArray(obj2)) return false;
			if (obj1.length !== obj2.length) return false;
			for (var i = 0; i < obj1.length; ++i) if (!asn1.equals(obj1[i], obj2[i])) return false;
			return true;
		}
		if (typeof obj1 !== typeof obj2) return false;
		if (typeof obj1 === "string") return obj1 === obj2;
		var equal = obj1.tagClass === obj2.tagClass && obj1.type === obj2.type && obj1.constructed === obj2.constructed && obj1.composed === obj2.composed && asn1.equals(obj1.value, obj2.value);
		if (options && options.includeBitStringContents) equal = equal && obj1.bitStringContents === obj2.bitStringContents;
		return equal;
	};
	/**
	* Gets the length of a BER-encoded ASN.1 value.
	*
	* In case the length is not specified, undefined is returned.
	*
	* @param b the BER-encoded ASN.1 byte buffer, starting with the first
	*          length byte.
	*
	* @return the length of the BER-encoded ASN.1 value or undefined.
	*/
	asn1.getBerValueLength = function(b) {
		var b2 = b.getByte();
		if (b2 === 128) return;
		var length;
		if (!(b2 & 128)) length = b2;
		else length = b.getInt((b2 & 127) << 3);
		return length;
	};
	/**
	* Check if the byte buffer has enough bytes. Throws an Error if not.
	*
	* @param bytes the byte buffer to parse from.
	* @param remaining the bytes remaining in the current parsing state.
	* @param n the number of bytes the buffer must have.
	*/
	function _checkBufferLength(bytes, remaining, n) {
		if (n > remaining) {
			var error = /* @__PURE__ */ new Error("Too few bytes to parse DER.");
			error.available = bytes.length();
			error.remaining = remaining;
			error.requested = n;
			throw error;
		}
	}
	/**
	* Gets the length of a BER-encoded ASN.1 value.
	*
	* In case the length is not specified, undefined is returned.
	*
	* @param bytes the byte buffer to parse from.
	* @param remaining the bytes remaining in the current parsing state.
	*
	* @return the length of the BER-encoded ASN.1 value or undefined.
	*/
	var _getValueLength = function(bytes, remaining) {
		var b2 = bytes.getByte();
		remaining--;
		if (b2 === 128) return;
		var length;
		if (!(b2 & 128)) length = b2;
		else {
			var longFormBytes = b2 & 127;
			_checkBufferLength(bytes, remaining, longFormBytes);
			length = bytes.getInt(longFormBytes << 3);
		}
		if (length < 0) throw new Error("Negative length: " + length);
		return length;
	};
	/**
	* Parses an asn1 object from a byte buffer in DER format.
	*
	* @param bytes the byte buffer to parse from.
	* @param [strict] true to be strict when checking value lengths, false to
	*          allow truncated values (default: true).
	* @param [options] object with options or boolean strict flag
	*          [strict] true to be strict when checking value lengths, false to
	*            allow truncated values (default: true).
	*          [parseAllBytes] true to ensure all bytes are parsed
	*            (default: true)
	*          [decodeBitStrings] true to attempt to decode the content of
	*            BIT STRINGs (not OCTET STRINGs) using strict mode. Note that
	*            without schema support to understand the data context this can
	*            erroneously decode values that happen to be valid ASN.1. This
	*            flag will be deprecated or removed as soon as schema support is
	*            available. (default: true)
	*
	* @throws Will throw an error for various malformed input conditions.
	*
	* @return the parsed asn1 object.
	*/
	asn1.fromDer = function(bytes, options) {
		if (options === void 0) options = {
			strict: true,
			parseAllBytes: true,
			decodeBitStrings: true
		};
		if (typeof options === "boolean") options = {
			strict: options,
			parseAllBytes: true,
			decodeBitStrings: true
		};
		if (!("strict" in options)) options.strict = true;
		if (!("parseAllBytes" in options)) options.parseAllBytes = true;
		if (!("decodeBitStrings" in options)) options.decodeBitStrings = true;
		if (typeof bytes === "string") bytes = forge.util.createBuffer(bytes);
		var byteCount = bytes.length();
		var value = _fromDer(bytes, bytes.length(), 0, options);
		if (options.parseAllBytes && bytes.length() !== 0) {
			var error = /* @__PURE__ */ new Error("Unparsed DER bytes remain after ASN.1 parsing.");
			error.byteCount = byteCount;
			error.remaining = bytes.length();
			throw error;
		}
		return value;
	};
	/**
	* Internal function to parse an asn1 object from a byte buffer in DER format.
	*
	* @param bytes the byte buffer to parse from.
	* @param remaining the number of bytes remaining for this chunk.
	* @param depth the current parsing depth.
	* @param options object with same options as fromDer().
	*
	* @return the parsed asn1 object.
	*/
	function _fromDer(bytes, remaining, depth, options) {
		var start;
		_checkBufferLength(bytes, remaining, 2);
		var b1 = bytes.getByte();
		remaining--;
		var tagClass = b1 & 192;
		var type = b1 & 31;
		start = bytes.length();
		var length = _getValueLength(bytes, remaining);
		remaining -= start - bytes.length();
		if (length !== void 0 && length > remaining) {
			if (options.strict) {
				var error = /* @__PURE__ */ new Error("Too few bytes to read ASN.1 value.");
				error.available = bytes.length();
				error.remaining = remaining;
				error.requested = length;
				throw error;
			}
			length = remaining;
		}
		var value;
		var bitStringContents;
		var constructed = (b1 & 32) === 32;
		if (constructed) {
			value = [];
			if (length === void 0) for (;;) {
				_checkBufferLength(bytes, remaining, 2);
				if (bytes.bytes(2) === String.fromCharCode(0, 0)) {
					bytes.getBytes(2);
					remaining -= 2;
					break;
				}
				start = bytes.length();
				value.push(_fromDer(bytes, remaining, depth + 1, options));
				remaining -= start - bytes.length();
			}
			else while (length > 0) {
				start = bytes.length();
				value.push(_fromDer(bytes, length, depth + 1, options));
				remaining -= start - bytes.length();
				length -= start - bytes.length();
			}
		}
		if (value === void 0 && tagClass === asn1.Class.UNIVERSAL && type === asn1.Type.BITSTRING) bitStringContents = bytes.bytes(length);
		if (value === void 0 && options.decodeBitStrings && tagClass === asn1.Class.UNIVERSAL && type === asn1.Type.BITSTRING && length > 1) {
			var savedRead = bytes.read;
			var savedRemaining = remaining;
			var unused = 0;
			if (type === asn1.Type.BITSTRING) {
				_checkBufferLength(bytes, remaining, 1);
				unused = bytes.getByte();
				remaining--;
			}
			if (unused === 0) try {
				start = bytes.length();
				var composed = _fromDer(bytes, remaining, depth + 1, {
					strict: true,
					decodeBitStrings: true
				});
				var used = start - bytes.length();
				remaining -= used;
				if (type == asn1.Type.BITSTRING) used++;
				var tc = composed.tagClass;
				if (used === length && (tc === asn1.Class.UNIVERSAL || tc === asn1.Class.CONTEXT_SPECIFIC)) value = [composed];
			} catch (ex) {}
			if (value === void 0) {
				bytes.read = savedRead;
				remaining = savedRemaining;
			}
		}
		if (value === void 0) {
			if (length === void 0) {
				if (options.strict) throw new Error("Non-constructed ASN.1 object of indefinite length.");
				length = remaining;
			}
			if (type === asn1.Type.BMPSTRING) {
				value = "";
				for (; length > 0; length -= 2) {
					_checkBufferLength(bytes, remaining, 2);
					value += String.fromCharCode(bytes.getInt16());
					remaining -= 2;
				}
			} else {
				value = bytes.getBytes(length);
				remaining -= length;
			}
		}
		var asn1Options = bitStringContents === void 0 ? null : { bitStringContents };
		return asn1.create(tagClass, type, constructed, value, asn1Options);
	}
	/**
	* Converts the given asn1 object to a buffer of bytes in DER format.
	*
	* @param asn1 the asn1 object to convert to bytes.
	*
	* @return the buffer of bytes.
	*/
	asn1.toDer = function(obj) {
		var bytes = forge.util.createBuffer();
		var b1 = obj.tagClass | obj.type;
		var value = forge.util.createBuffer();
		var useBitStringContents = false;
		if ("bitStringContents" in obj) {
			useBitStringContents = true;
			if (obj.original) useBitStringContents = asn1.equals(obj, obj.original);
		}
		if (useBitStringContents) value.putBytes(obj.bitStringContents);
		else if (obj.composed) {
			if (obj.constructed) b1 |= 32;
			else value.putByte(0);
			for (var i = 0; i < obj.value.length; ++i) if (obj.value[i] !== void 0) value.putBuffer(asn1.toDer(obj.value[i]));
		} else if (obj.type === asn1.Type.BMPSTRING) for (var i = 0; i < obj.value.length; ++i) value.putInt16(obj.value.charCodeAt(i));
		else if (obj.type === asn1.Type.INTEGER && obj.value.length > 1 && (obj.value.charCodeAt(0) === 0 && (obj.value.charCodeAt(1) & 128) === 0 || obj.value.charCodeAt(0) === 255 && (obj.value.charCodeAt(1) & 128) === 128)) value.putBytes(obj.value.substr(1));
		else value.putBytes(obj.value);
		bytes.putByte(b1);
		if (value.length() <= 127) bytes.putByte(value.length() & 127);
		else {
			var len = value.length();
			var lenBytes = "";
			do {
				lenBytes += String.fromCharCode(len & 255);
				len = len >>> 8;
			} while (len > 0);
			bytes.putByte(lenBytes.length | 128);
			for (var i = lenBytes.length - 1; i >= 0; --i) bytes.putByte(lenBytes.charCodeAt(i));
		}
		bytes.putBuffer(value);
		return bytes;
	};
	/**
	* Converts an OID dot-separated string to a byte buffer. The byte buffer
	* contains only the DER-encoded value, not any tag or length bytes.
	*
	* @param oid the OID dot-separated string.
	*
	* @return the byte buffer.
	*/
	asn1.oidToDer = function(oid) {
		var values = oid.split(".");
		var bytes = forge.util.createBuffer();
		bytes.putByte(40 * parseInt(values[0], 10) + parseInt(values[1], 10));
		var last, valueBytes, value, b;
		for (var i = 2; i < values.length; ++i) {
			last = true;
			valueBytes = [];
			value = parseInt(values[i], 10);
			do {
				b = value & 127;
				value = value >>> 7;
				if (!last) b |= 128;
				valueBytes.push(b);
				last = false;
			} while (value > 0);
			for (var n = valueBytes.length - 1; n >= 0; --n) bytes.putByte(valueBytes[n]);
		}
		return bytes;
	};
	/**
	* Converts a DER-encoded byte buffer to an OID dot-separated string. The
	* byte buffer should contain only the DER-encoded value, not any tag or
	* length bytes.
	*
	* @param bytes the byte buffer.
	*
	* @return the OID dot-separated string.
	*/
	asn1.derToOid = function(bytes) {
		var oid;
		if (typeof bytes === "string") bytes = forge.util.createBuffer(bytes);
		var b = bytes.getByte();
		oid = Math.floor(b / 40) + "." + b % 40;
		var value = 0;
		while (bytes.length() > 0) {
			b = bytes.getByte();
			value = value << 7;
			if (b & 128) value += b & 127;
			else {
				oid += "." + (value + b);
				value = 0;
			}
		}
		return oid;
	};
	/**
	* Converts a UTCTime value to a date.
	*
	* Note: GeneralizedTime has 4 digits for the year and is used for X.509
	* dates past 2049. Parsing that structure hasn't been implemented yet.
	*
	* @param utc the UTCTime value to convert.
	*
	* @return the date.
	*/
	asn1.utcTimeToDate = function(utc) {
		var date = /* @__PURE__ */ new Date();
		var year = parseInt(utc.substr(0, 2), 10);
		year = year >= 50 ? 1900 + year : 2e3 + year;
		var MM = parseInt(utc.substr(2, 2), 10) - 1;
		var DD = parseInt(utc.substr(4, 2), 10);
		var hh = parseInt(utc.substr(6, 2), 10);
		var mm = parseInt(utc.substr(8, 2), 10);
		var ss = 0;
		if (utc.length > 11) {
			var c = utc.charAt(10);
			var end = 10;
			if (c !== "+" && c !== "-") {
				ss = parseInt(utc.substr(10, 2), 10);
				end += 2;
			}
		}
		date.setUTCFullYear(year, MM, DD);
		date.setUTCHours(hh, mm, ss, 0);
		if (end) {
			c = utc.charAt(end);
			if (c === "+" || c === "-") {
				var hhoffset = parseInt(utc.substr(end + 1, 2), 10);
				var mmoffset = parseInt(utc.substr(end + 4, 2), 10);
				var offset = hhoffset * 60 + mmoffset;
				offset *= 6e4;
				if (c === "+") date.setTime(+date - offset);
				else date.setTime(+date + offset);
			}
		}
		return date;
	};
	/**
	* Converts a GeneralizedTime value to a date.
	*
	* @param gentime the GeneralizedTime value to convert.
	*
	* @return the date.
	*/
	asn1.generalizedTimeToDate = function(gentime) {
		var date = /* @__PURE__ */ new Date();
		var YYYY = parseInt(gentime.substr(0, 4), 10);
		var MM = parseInt(gentime.substr(4, 2), 10) - 1;
		var DD = parseInt(gentime.substr(6, 2), 10);
		var hh = parseInt(gentime.substr(8, 2), 10);
		var mm = parseInt(gentime.substr(10, 2), 10);
		var ss = parseInt(gentime.substr(12, 2), 10);
		var fff = 0;
		var offset = 0;
		var isUTC = false;
		if (gentime.charAt(gentime.length - 1) === "Z") isUTC = true;
		var end = gentime.length - 5, c = gentime.charAt(end);
		if (c === "+" || c === "-") {
			var hhoffset = parseInt(gentime.substr(end + 1, 2), 10);
			var mmoffset = parseInt(gentime.substr(end + 4, 2), 10);
			offset = hhoffset * 60 + mmoffset;
			offset *= 6e4;
			if (c === "+") offset *= -1;
			isUTC = true;
		}
		if (gentime.charAt(14) === ".") fff = parseFloat(gentime.substr(14), 10) * 1e3;
		if (isUTC) {
			date.setUTCFullYear(YYYY, MM, DD);
			date.setUTCHours(hh, mm, ss, fff);
			date.setTime(+date + offset);
		} else {
			date.setFullYear(YYYY, MM, DD);
			date.setHours(hh, mm, ss, fff);
		}
		return date;
	};
	/**
	* Converts a date to a UTCTime value.
	*
	* Note: GeneralizedTime has 4 digits for the year and is used for X.509
	* dates past 2049. Converting to a GeneralizedTime hasn't been
	* implemented yet.
	*
	* @param date the date to convert.
	*
	* @return the UTCTime value.
	*/
	asn1.dateToUtcTime = function(date) {
		if (typeof date === "string") return date;
		var rval = "";
		var format = [];
		format.push(("" + date.getUTCFullYear()).substr(2));
		format.push("" + (date.getUTCMonth() + 1));
		format.push("" + date.getUTCDate());
		format.push("" + date.getUTCHours());
		format.push("" + date.getUTCMinutes());
		format.push("" + date.getUTCSeconds());
		for (var i = 0; i < format.length; ++i) {
			if (format[i].length < 2) rval += "0";
			rval += format[i];
		}
		rval += "Z";
		return rval;
	};
	/**
	* Converts a date to a GeneralizedTime value.
	*
	* @param date the date to convert.
	*
	* @return the GeneralizedTime value as a string.
	*/
	asn1.dateToGeneralizedTime = function(date) {
		if (typeof date === "string") return date;
		var rval = "";
		var format = [];
		format.push("" + date.getUTCFullYear());
		format.push("" + (date.getUTCMonth() + 1));
		format.push("" + date.getUTCDate());
		format.push("" + date.getUTCHours());
		format.push("" + date.getUTCMinutes());
		format.push("" + date.getUTCSeconds());
		for (var i = 0; i < format.length; ++i) {
			if (format[i].length < 2) rval += "0";
			rval += format[i];
		}
		rval += "Z";
		return rval;
	};
	/**
	* Converts a javascript integer to a DER-encoded byte buffer to be used
	* as the value for an INTEGER type.
	*
	* @param x the integer.
	*
	* @return the byte buffer.
	*/
	asn1.integerToDer = function(x) {
		var rval = forge.util.createBuffer();
		if (x >= -128 && x < 128) return rval.putSignedInt(x, 8);
		if (x >= -32768 && x < 32768) return rval.putSignedInt(x, 16);
		if (x >= -8388608 && x < 8388608) return rval.putSignedInt(x, 24);
		if (x >= -2147483648 && x < 2147483648) return rval.putSignedInt(x, 32);
		var error = /* @__PURE__ */ new Error("Integer too large; max is 32-bits.");
		error.integer = x;
		throw error;
	};
	/**
	* Converts a DER-encoded byte buffer to a javascript integer. This is
	* typically used to decode the value of an INTEGER type.
	*
	* @param bytes the byte buffer.
	*
	* @return the integer.
	*/
	asn1.derToInteger = function(bytes) {
		if (typeof bytes === "string") bytes = forge.util.createBuffer(bytes);
		var n = bytes.length() * 8;
		if (n > 32) throw new Error("Integer too large; max is 32-bits.");
		return bytes.getSignedInt(n);
	};
	/**
	* Validates that the given ASN.1 object is at least a super set of the
	* given ASN.1 structure. Only tag classes and types are checked. An
	* optional map may also be provided to capture ASN.1 values while the
	* structure is checked.
	*
	* To capture an ASN.1 value, set an object in the validator's 'capture'
	* parameter to the key to use in the capture map. To capture the full
	* ASN.1 object, specify 'captureAsn1'. To capture BIT STRING bytes, including
	* the leading unused bits counter byte, specify 'captureBitStringContents'.
	* To capture BIT STRING bytes, without the leading unused bits counter byte,
	* specify 'captureBitStringValue'.
	*
	* Objects in the validator may set a field 'optional' to true to indicate
	* that it isn't necessary to pass validation.
	*
	* @param obj the ASN.1 object to validate.
	* @param v the ASN.1 structure validator.
	* @param capture an optional map to capture values in.
	* @param errors an optional array for storing validation errors.
	*
	* @return true on success, false on failure.
	*/
	asn1.validate = function(obj, v, capture, errors) {
		var rval = false;
		if ((obj.tagClass === v.tagClass || typeof v.tagClass === "undefined") && (obj.type === v.type || typeof v.type === "undefined")) {
			if (obj.constructed === v.constructed || typeof v.constructed === "undefined") {
				rval = true;
				if (v.value && forge.util.isArray(v.value)) {
					var j = 0;
					for (var i = 0; rval && i < v.value.length; ++i) {
						rval = v.value[i].optional || false;
						if (obj.value[j]) {
							rval = asn1.validate(obj.value[j], v.value[i], capture, errors);
							if (rval) ++j;
							else if (v.value[i].optional) rval = true;
						}
						if (!rval && errors) errors.push("[" + v.name + "] Tag class \"" + v.tagClass + "\", type \"" + v.type + "\" expected value length \"" + v.value.length + "\", got \"" + obj.value.length + "\"");
					}
				}
				if (rval && capture) {
					if (v.capture) capture[v.capture] = obj.value;
					if (v.captureAsn1) capture[v.captureAsn1] = obj;
					if (v.captureBitStringContents && "bitStringContents" in obj) capture[v.captureBitStringContents] = obj.bitStringContents;
					if (v.captureBitStringValue && "bitStringContents" in obj) if (obj.bitStringContents.length < 2) capture[v.captureBitStringValue] = "";
					else {
						if (obj.bitStringContents.charCodeAt(0) !== 0) throw new Error("captureBitStringValue only supported for zero unused bits");
						capture[v.captureBitStringValue] = obj.bitStringContents.slice(1);
					}
				}
			} else if (errors) errors.push("[" + v.name + "] Expected constructed \"" + v.constructed + "\", got \"" + obj.constructed + "\"");
		} else if (errors) {
			if (obj.tagClass !== v.tagClass) errors.push("[" + v.name + "] Expected tag class \"" + v.tagClass + "\", got \"" + obj.tagClass + "\"");
			if (obj.type !== v.type) errors.push("[" + v.name + "] Expected type \"" + v.type + "\", got \"" + obj.type + "\"");
		}
		return rval;
	};
	var _nonLatinRegex = /[^\\u0000-\\u00ff]/;
	/**
	* Pretty prints an ASN.1 object to a string.
	*
	* @param obj the object to write out.
	* @param level the level in the tree.
	* @param indentation the indentation to use.
	*
	* @return the string.
	*/
	asn1.prettyPrint = function(obj, level, indentation) {
		var rval = "";
		level = level || 0;
		indentation = indentation || 2;
		if (level > 0) rval += "\n";
		var indent = "";
		for (var i = 0; i < level * indentation; ++i) indent += " ";
		rval += indent + "Tag: ";
		switch (obj.tagClass) {
			case asn1.Class.UNIVERSAL:
				rval += "Universal:";
				break;
			case asn1.Class.APPLICATION:
				rval += "Application:";
				break;
			case asn1.Class.CONTEXT_SPECIFIC:
				rval += "Context-Specific:";
				break;
			case asn1.Class.PRIVATE:
				rval += "Private:";
				break;
		}
		if (obj.tagClass === asn1.Class.UNIVERSAL) {
			rval += obj.type;
			switch (obj.type) {
				case asn1.Type.NONE:
					rval += " (None)";
					break;
				case asn1.Type.BOOLEAN:
					rval += " (Boolean)";
					break;
				case asn1.Type.INTEGER:
					rval += " (Integer)";
					break;
				case asn1.Type.BITSTRING:
					rval += " (Bit string)";
					break;
				case asn1.Type.OCTETSTRING:
					rval += " (Octet string)";
					break;
				case asn1.Type.NULL:
					rval += " (Null)";
					break;
				case asn1.Type.OID:
					rval += " (Object Identifier)";
					break;
				case asn1.Type.ODESC:
					rval += " (Object Descriptor)";
					break;
				case asn1.Type.EXTERNAL:
					rval += " (External or Instance of)";
					break;
				case asn1.Type.REAL:
					rval += " (Real)";
					break;
				case asn1.Type.ENUMERATED:
					rval += " (Enumerated)";
					break;
				case asn1.Type.EMBEDDED:
					rval += " (Embedded PDV)";
					break;
				case asn1.Type.UTF8:
					rval += " (UTF8)";
					break;
				case asn1.Type.ROID:
					rval += " (Relative Object Identifier)";
					break;
				case asn1.Type.SEQUENCE:
					rval += " (Sequence)";
					break;
				case asn1.Type.SET:
					rval += " (Set)";
					break;
				case asn1.Type.PRINTABLESTRING:
					rval += " (Printable String)";
					break;
				case asn1.Type.IA5String:
					rval += " (IA5String (ASCII))";
					break;
				case asn1.Type.UTCTIME:
					rval += " (UTC time)";
					break;
				case asn1.Type.GENERALIZEDTIME:
					rval += " (Generalized time)";
					break;
				case asn1.Type.BMPSTRING:
					rval += " (BMP String)";
					break;
			}
		} else rval += obj.type;
		rval += "\n";
		rval += indent + "Constructed: " + obj.constructed + "\n";
		if (obj.composed) {
			var subvalues = 0;
			var sub = "";
			for (var i = 0; i < obj.value.length; ++i) if (obj.value[i] !== void 0) {
				subvalues += 1;
				sub += asn1.prettyPrint(obj.value[i], level + 1, indentation);
				if (i + 1 < obj.value.length) sub += ",";
			}
			rval += indent + "Sub values: " + subvalues + sub;
		} else {
			rval += indent + "Value: ";
			if (obj.type === asn1.Type.OID) {
				var oid = asn1.derToOid(obj.value);
				rval += oid;
				if (forge.pki && forge.pki.oids) {
					if (oid in forge.pki.oids) rval += " (" + forge.pki.oids[oid] + ") ";
				}
			}
			if (obj.type === asn1.Type.INTEGER) try {
				rval += asn1.derToInteger(obj.value);
			} catch (ex) {
				rval += "0x" + forge.util.bytesToHex(obj.value);
			}
			else if (obj.type === asn1.Type.BITSTRING) {
				if (obj.value.length > 1) rval += "0x" + forge.util.bytesToHex(obj.value.slice(1));
				else rval += "(none)";
				if (obj.value.length > 0) {
					var unused = obj.value.charCodeAt(0);
					if (unused == 1) rval += " (1 unused bit shown)";
					else if (unused > 1) rval += " (" + unused + " unused bits shown)";
				}
			} else if (obj.type === asn1.Type.OCTETSTRING) {
				if (!_nonLatinRegex.test(obj.value)) rval += "(" + obj.value + ") ";
				rval += "0x" + forge.util.bytesToHex(obj.value);
			} else if (obj.type === asn1.Type.UTF8) try {
				rval += forge.util.decodeUtf8(obj.value);
			} catch (e) {
				if (e.message === "URI malformed") rval += "0x" + forge.util.bytesToHex(obj.value) + " (malformed UTF8)";
				else throw e;
			}
			else if (obj.type === asn1.Type.PRINTABLESTRING || obj.type === asn1.Type.IA5String) rval += obj.value;
			else if (_nonLatinRegex.test(obj.value)) rval += "0x" + forge.util.bytesToHex(obj.value);
			else if (obj.value.length === 0) rval += "[null]";
			else rval += obj.value;
		}
		return rval;
	};
}) });

//#endregion
export default require_asn1();

export { require_asn1 };
//# sourceMappingURL=asn1.mjs.map