UNPKG

@wildboar/pkcs

Version:
255 lines (254 loc) 8.55 kB
/* eslint-disable */ import { ASN1TagClass as _TagClass, } from "@wildboar/asn1"; import * as $ from "@wildboar/asn1/functional"; import { _decode_OtherPrimeInfos, _encode_OtherPrimeInfos, } from "../PKCS-1/OtherPrimeInfos.ta.mjs"; import { _decode_Version, _encode_Version, } from "../PKCS-1/Version.ta.mjs"; /** * @summary RSAPrivateKey * @description * * ### ASN.1 Definition: * * ```asn1 * RSAPrivateKey ::= SEQUENCE { * version Version, * modulus INTEGER, -- n * publicExponent INTEGER, -- e * privateExponent INTEGER, -- d * prime1 INTEGER, -- p * prime2 INTEGER, -- q * exponent1 INTEGER, -- d mod (p-1) * exponent2 INTEGER, -- d mod (q-1) * coefficient INTEGER, -- (inverse of q) mod p * otherPrimeInfos OtherPrimeInfos OPTIONAL * } * ``` * */ export class RSAPrivateKey { version; modulus; publicExponent; privateExponent; prime1; prime2; exponent1; exponent2; coefficient; otherPrimeInfos; constructor( /** * @summary `version`. * @public * @readonly */ version, /** * @summary `modulus`. * @public * @readonly */ modulus, /** * @summary `publicExponent`. * @public * @readonly */ publicExponent, /** * @summary `privateExponent`. * @public * @readonly */ privateExponent, /** * @summary `prime1`. * @public * @readonly */ prime1, /** * @summary `prime2`. * @public * @readonly */ prime2, /** * @summary `exponent1`. * @public * @readonly */ exponent1, /** * @summary `exponent2`. * @public * @readonly */ exponent2, /** * @summary `coefficient`. * @public * @readonly */ coefficient, /** * @summary `otherPrimeInfos`. * @public * @readonly */ otherPrimeInfos) { this.version = version; this.modulus = modulus; this.publicExponent = publicExponent; this.privateExponent = privateExponent; this.prime1 = prime1; this.prime2 = prime2; this.exponent1 = exponent1; this.exponent2 = exponent2; this.coefficient = coefficient; this.otherPrimeInfos = otherPrimeInfos; } /** * @summary Restructures an object into a RSAPrivateKey * @description * * This takes an `object` and converts it to a `RSAPrivateKey`. * * @public * @static * @method * @param {Object} _o An object having all of the keys and values of a `RSAPrivateKey`. * @returns {RSAPrivateKey} */ static _from_object(_o) { return new RSAPrivateKey(_o.version, _o.modulus, _o.publicExponent, _o.privateExponent, _o.prime1, _o.prime2, _o.exponent1, _o.exponent2, _o.coefficient, _o.otherPrimeInfos); } } /** * @summary The Leading Root Component Types of RSAPrivateKey * @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_RSAPrivateKey = [ new $.ComponentSpec("version", false, $.hasTag(_TagClass.universal, 2)), new $.ComponentSpec("modulus", false, $.hasTag(_TagClass.universal, 2)), new $.ComponentSpec("publicExponent", false, $.hasTag(_TagClass.universal, 2)), new $.ComponentSpec("privateExponent", false, $.hasTag(_TagClass.universal, 2)), new $.ComponentSpec("prime1", false, $.hasTag(_TagClass.universal, 2)), new $.ComponentSpec("prime2", false, $.hasTag(_TagClass.universal, 2)), new $.ComponentSpec("exponent1", false, $.hasTag(_TagClass.universal, 2)), new $.ComponentSpec("exponent2", false, $.hasTag(_TagClass.universal, 2)), new $.ComponentSpec("coefficient", false, $.hasTag(_TagClass.universal, 2)), new $.ComponentSpec("otherPrimeInfos", true, $.hasTag(_TagClass.universal, 16)), ]; /** * @summary The Trailing Root Component Types of RSAPrivateKey * @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_RSAPrivateKey = []; /** * @summary The Extension Addition Component Types of RSAPrivateKey * @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_RSAPrivateKey = []; let _cached_decoder_for_RSAPrivateKey = null; /** * @summary Decodes an ASN.1 element into a(n) RSAPrivateKey * @function * @param {_Element} el The element being decoded. * @returns {RSAPrivateKey} The decoded data structure. */ export function _decode_RSAPrivateKey(el) { if (!_cached_decoder_for_RSAPrivateKey) { _cached_decoder_for_RSAPrivateKey = function (el) { let version; let modulus; let publicExponent; let privateExponent; let prime1; let prime2; let exponent1; let exponent2; let coefficient; let otherPrimeInfos; const callbacks = { version: (_el) => { version = _decode_Version(_el); }, modulus: (_el) => { modulus = $._decodeBigInt(_el); }, publicExponent: (_el) => { publicExponent = $._decodeBigInt(_el); }, privateExponent: (_el) => { privateExponent = $._decodeBigInt(_el); }, prime1: (_el) => { prime1 = $._decodeBigInt(_el); }, prime2: (_el) => { prime2 = $._decodeBigInt(_el); }, exponent1: (_el) => { exponent1 = $._decodeBigInt(_el); }, exponent2: (_el) => { exponent2 = $._decodeBigInt(_el); }, coefficient: (_el) => { coefficient = $._decodeBigInt(_el); }, otherPrimeInfos: (_el) => { otherPrimeInfos = _decode_OtherPrimeInfos(_el); }, }; $._parse_sequence(el, callbacks, _root_component_type_list_1_spec_for_RSAPrivateKey, _extension_additions_list_spec_for_RSAPrivateKey, _root_component_type_list_2_spec_for_RSAPrivateKey, undefined); return new RSAPrivateKey(version, modulus, publicExponent, privateExponent, prime1, prime2, exponent1, exponent2, coefficient, otherPrimeInfos); }; } return _cached_decoder_for_RSAPrivateKey(el); } let _cached_encoder_for_RSAPrivateKey = null; /** * @summary Encodes a(n) RSAPrivateKey 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 RSAPrivateKey, encoded as an ASN.1 Element. */ export function _encode_RSAPrivateKey(value, elGetter) { if (!_cached_encoder_for_RSAPrivateKey) { _cached_encoder_for_RSAPrivateKey = function (value) { return $._encodeSequence([] .concat([ /* REQUIRED */ _encode_Version(value.version, $.BER), /* REQUIRED */ $._encodeBigInt(value.modulus, $.BER), /* REQUIRED */ $._encodeBigInt(value.publicExponent, $.BER), /* REQUIRED */ $._encodeBigInt(value.privateExponent, $.BER), /* REQUIRED */ $._encodeBigInt(value.prime1, $.BER), /* REQUIRED */ $._encodeBigInt(value.prime2, $.BER), /* REQUIRED */ $._encodeBigInt(value.exponent1, $.BER), /* REQUIRED */ $._encodeBigInt(value.exponent2, $.BER), /* REQUIRED */ $._encodeBigInt(value.coefficient, $.BER), /* IF_ABSENT */ value.otherPrimeInfos === undefined ? undefined : _encode_OtherPrimeInfos(value.otherPrimeInfos, $.BER), ]) .filter((c) => !!c), $.BER); }; } return _cached_encoder_for_RSAPrivateKey(value, elGetter); } /* eslint-enable */