UNPKG

@wildboar/pki-stub

Version:
209 lines (208 loc) 7.16 kB
/* eslint-disable */ import { ASN1TagClass as _TagClass, } from "@wildboar/asn1"; import * as $ from "@wildboar/asn1/functional"; import { _decode_AlgorithmIdentifier, _encode_AlgorithmIdentifier, } from "../PKI-Stub/AlgorithmIdentifier.ta.mjs"; /** * @summary SIGNED * @description * * ### ASN.1 Definition: * * ```asn1 * SIGNED{ToBeSigned} ::= SEQUENCE { * toBeSigned ToBeSigned, * algorithmIdentifier AlgorithmIdentifier{{SupportedAlgorithms}}, * signature BIT STRING, * ..., * altAlgorithmIdentifier AlgorithmIdentifier{{SupportedAlgorithms}} OPTIONAL, * altSignature BIT STRING OPTIONAL * } (WITH COMPONENTS {..., altAlgorithmIdentifier PRESENT, altSignature PRESENT } | * WITH COMPONENTS {..., altAlgorithmIdentifier ABSENT, altSignature ABSENT } ) * ``` * */ export class SIGNED { toBeSigned; algorithmIdentifier; signature; altAlgorithmIdentifier; altSignature; _unrecognizedExtensionsList; /** * @summary The orignal DER encoding of the signed thing (not just the toBeSigned) * @description * * This is the original byte encoding of the signed thing, not just the * `toBeSigned` part alone. (Instead of preserving just the bytes of * `toBeSigned`, we preserve all bytes so that the entire certificate can * be hashed reliably, which is important for some applications.) * * This exists so that the original encoding element can be preserved for * the sake of validating the signature correctly. * * @public */ originalDER; constructor( /** * @summary `toBeSigned`. * @public * @readonly */ toBeSigned, /** * @summary `algorithmIdentifier`. * @public * @readonly */ algorithmIdentifier, /** * @summary `signature`. * @public * @readonly */ signature, /** * @summary `altAlgorithmIdentifier`. * @public * @readonly */ altAlgorithmIdentifier, /** * @summary `altSignature`. * @public * @readonly */ altSignature, /** * @summary Extensions that are not recognized. * @public * @readonly */ _unrecognizedExtensionsList = [], /** * @summary */ originalDER) { this.toBeSigned = toBeSigned; this.algorithmIdentifier = algorithmIdentifier; this.signature = signature; this.altAlgorithmIdentifier = altAlgorithmIdentifier; this.altSignature = altSignature; this._unrecognizedExtensionsList = _unrecognizedExtensionsList; this.originalDER = originalDER?.toBytes(); } /** * @summary Restructures an object into a SIGNED * @description * * This takes an `object` and converts it to a `SIGNED`. * * @public * @static * @method * @param {Object} _o An object having all of the keys and values of a `SIGNED`. * @returns {SIGNED} */ static _from_object(_o) { return new SIGNED(_o.toBeSigned, _o.algorithmIdentifier, _o.signature, _o.altAlgorithmIdentifier, _o.altSignature, _o._unrecognizedExtensionsList); } } /** * @summary The Leading Root Component Types of SIGNED * @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_SIGNED = [ new $.ComponentSpec("toBeSigned", false, $.hasAnyTag), new $.ComponentSpec("algorithmIdentifier", false, $.hasTag(_TagClass.universal, 16)), new $.ComponentSpec("signature", false, $.hasTag(_TagClass.universal, 3)), ]; /** * @summary The Trailing Root Component Types of SIGNED * @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_SIGNED = []; /** * @summary The Extension Addition Component Types of SIGNED * @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_SIGNED = [ new $.ComponentSpec("altAlgorithmIdentifier", true, $.hasTag(_TagClass.universal, 16)), new $.ComponentSpec("altSignature", true, $.hasTag(_TagClass.universal, 3)), ]; /** * @summary Returns a function that will decode an ASN.1 element into a(n) SIGNED * @function * @param {_Element} el The element being decoded. * @returns A function that will decode an ASN.1 element. */ export function _get_decoder_for_SIGNED(_decode_ToBeSigned) { return function (el) { let toBeSigned; let algorithmIdentifier; let signature; let altAlgorithmIdentifier; let altSignature; let _unrecognizedExtensionsList = []; const originalDER = el.toBytes(); const callbacks = { toBeSigned: (_el) => { toBeSigned = _decode_ToBeSigned(_el); }, algorithmIdentifier: (_el) => { algorithmIdentifier = _decode_AlgorithmIdentifier(_el); }, signature: (_el) => { signature = $._decodeBitString(_el); }, altAlgorithmIdentifier: (_el) => { altAlgorithmIdentifier = _decode_AlgorithmIdentifier(_el); }, altSignature: (_el) => { altSignature = $._decodeBitString(_el); }, }; $._parse_sequence(el, callbacks, _root_component_type_list_1_spec_for_SIGNED, _extension_additions_list_spec_for_SIGNED, _root_component_type_list_2_spec_for_SIGNED, (ext) => { _unrecognizedExtensionsList.push(ext); }); const ret = new SIGNED(toBeSigned, algorithmIdentifier, signature, altAlgorithmIdentifier, altSignature, _unrecognizedExtensionsList, el); ret.originalDER = originalDER; return ret; }; } /** * @summary Returns a function that will encode a(n) SIGNED into an ASN.1 Element. * @function * @returns A function that will encode a(n) SIGNED as an ASN.1 element. */ export function _get_encoder_for_SIGNED(_encode_ToBeSigned) { return function (value) { const components = [ /* REQUIRED */ _encode_ToBeSigned(value.toBeSigned, $.DER), /* REQUIRED */ _encode_AlgorithmIdentifier(value.algorithmIdentifier, $.DER), /* REQUIRED */ $._encodeBitString(value.signature, $.DER), /* IF_ABSENT */ value.altAlgorithmIdentifier === undefined ? undefined : _encode_AlgorithmIdentifier(value.altAlgorithmIdentifier, $.DER), /* IF_ABSENT */ value.altSignature === undefined ? undefined : $._encodeBitString(value.altSignature, $.BER), ...value._unrecognizedExtensionsList ?? [], ].filter((c) => !!c); return $._encodeSequence(components, $.DER); }; } /* eslint-enable */