UNPKG

@wildboar/pki-stub

Version:
163 lines (162 loc) 4.8 kB
/* eslint-disable */ import { ASN1TagClass as _TagClass, DERElement, } from "@wildboar/asn1"; import * as $ from "@wildboar/asn1/functional"; /** * @summary Extension * @description * * ### ASN.1 Definition: * * ```asn1 * Extension ::= SEQUENCE { * extnId EXTENSION.&id({ExtensionSet}), * critical BOOLEAN DEFAULT FALSE, * extnValue OCTET STRING * (CONTAINING EXTENSION.&ExtnType({ExtensionSet}{@extnId}) * ENCODED BY der), * ... } * ``` * */ export class Extension { extnId; critical; extnValue; _unrecognizedExtensionsList; constructor( /** * @summary `extnId`. * @public * @readonly */ extnId, /** * @summary `critical`. * @public * @readonly */ critical, /** * @summary `extnValue`. * @public * @readonly */ extnValue, /** * @summary Extensions that are not recognized. * @public * @readonly */ _unrecognizedExtensionsList = []) { this.extnId = extnId; this.critical = critical; this.extnValue = extnValue; this._unrecognizedExtensionsList = _unrecognizedExtensionsList; } /** * @summary Restructures an object into a Extension * @description * * This takes an `object` and converts it to a `Extension`. * * @public * @static * @method * @param {Object} _o An object having all of the keys and values of a `Extension`. * @returns {Extension} */ static _from_object(_o) { return new Extension(_o.extnId, _o.critical, _o.extnValue, _o._unrecognizedExtensionsList); } /** * @summary Getter that returns the default value for `critical`. * @public * @static * @method */ static get _default_value_for_critical() { return false; } valueElement() { const el = new DERElement(); el.fromBytes(this.extnValue); return el; } } /** * @summary The Leading Root Component Types of Extension * @description * * This is an array of `ComponentSpec`s that define how to decode the leading root component type list of a SET or SEQUENCE. * * @constant */ export const _root_component_type_list_1_spec_for_Extension = [ new $.ComponentSpec("extnId", false, $.hasTag(_TagClass.universal, 6)), new $.ComponentSpec("critical", true, $.hasTag(_TagClass.universal, 1)), new $.ComponentSpec("extnValue", false, $.hasTag(_TagClass.universal, 4)), ]; /** * @summary The Trailing Root Component Types of Extension * @description * * This is an array of `ComponentSpec`s that define how to decode the trailing root component type list of a SET or SEQUENCE. * * @constant */ export const _root_component_type_list_2_spec_for_Extension = []; /** * @summary The Extension Addition Component Types of Extension * @description * * This is an array of `ComponentSpec`s that define how to decode the extension addition component type list of a SET or SEQUENCE. * * @constant */ export const _extension_additions_list_spec_for_Extension = []; /** * @summary Decodes an ASN.1 element into a(n) Extension * @function * @param {_Element} el The element being decoded. * @returns {Extension} The decoded data structure. */ export function _decode_Extension(el) { let extnId; let critical = Extension._default_value_for_critical; let extnValue; let _unrecognizedExtensionsList = []; const callbacks = { extnId: (_el) => { extnId = $._decodeObjectIdentifier(_el); }, critical: (_el) => { critical = $._decodeBoolean(_el); }, extnValue: (_el) => { extnValue = $._decodeOctetString(_el); }, }; $._parse_sequence(el, callbacks, _root_component_type_list_1_spec_for_Extension, _extension_additions_list_spec_for_Extension, _root_component_type_list_2_spec_for_Extension, (ext) => { _unrecognizedExtensionsList.push(ext); }); return new Extension(extnId, critical, extnValue, _unrecognizedExtensionsList); } /** * @summary Encodes a(n) Extension into an ASN.1 Element. * @function * @param value The element being encoded. * @param elGetter A function that can be used to get new ASN.1 elements. * @returns {_Element} The Extension, encoded as an ASN.1 Element. */ export function _encode_Extension(value) { const components = [ /* REQUIRED */ $._encodeObjectIdentifier(value.extnId, $.BER), ]; if (value.critical) { components.push($._encodeBoolean(value.critical, $.BER)); } components.push($._encodeOctetString(value.extnValue, $.BER)); components.push(...value._unrecognizedExtensionsList); return $._encodeSequence(components, $.BER); } /* eslint-enable */