@wildboar/pkcs
Version:
Public Key Cryptography Standard PDUs in TypeScript
136 lines (135 loc) • 4.33 kB
JavaScript
/* eslint-disable */
import { ASN1TagClass as _TagClass, } from "@wildboar/asn1";
import * as $ from "@wildboar/asn1/functional";
import { _decode_KeyUsage, _encode_KeyUsage, } from "@wildboar/x500/CertificateExtensions";
/**
* @summary Usage
* @description
*
* ### ASN.1 Definition:
*
* ```asn1
* Usage ::= SEQUENCE {
* keyUsage KeyUsage OPTIONAL,
* extKeyUsage SEQUENCE SIZE (1..MAX) OF OBJECT IDENTIFIER OPTIONAL
* }(WITH COMPONENTS {..., keyUsage PRESENT} | WITH COMPONENTS {..., extKeyUsage PRESENT})
* ```
*
*/
export class Usage {
keyUsage;
extKeyUsage;
constructor(
/**
* @summary `keyUsage`.
* @public
* @readonly
*/
keyUsage,
/**
* @summary `extKeyUsage`.
* @public
* @readonly
*/
extKeyUsage) {
this.keyUsage = keyUsage;
this.extKeyUsage = extKeyUsage;
}
/**
* @summary Restructures an object into a Usage
* @description
*
* This takes an `object` and converts it to a `Usage`.
*
* @public
* @static
* @method
* @param {Object} _o An object having all of the keys and values of a `Usage`.
* @returns {Usage}
*/
static _from_object(_o) {
return new Usage(_o.keyUsage, _o.extKeyUsage);
}
}
/**
* @summary The Leading Root Component Types of Usage
* @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_Usage = [
new $.ComponentSpec("keyUsage", true, $.hasTag(_TagClass.universal, 3)),
new $.ComponentSpec("extKeyUsage", true, $.hasTag(_TagClass.universal, 16)),
];
/**
* @summary The Trailing Root Component Types of Usage
* @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_Usage = [];
/**
* @summary The Extension Addition Component Types of Usage
* @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_Usage = [];
let _cached_decoder_for_Usage = null;
/**
* @summary Decodes an ASN.1 element into a(n) Usage
* @function
* @param {_Element} el The element being decoded.
* @returns {Usage} The decoded data structure.
*/
export function _decode_Usage(el) {
if (!_cached_decoder_for_Usage) {
_cached_decoder_for_Usage = function (el) {
let keyUsage;
let extKeyUsage;
const callbacks = {
keyUsage: (_el) => {
keyUsage = _decode_KeyUsage(_el);
},
extKeyUsage: (_el) => {
extKeyUsage = $._decodeSequenceOf(() => $._decodeObjectIdentifier)(_el);
},
};
$._parse_sequence(el, callbacks, _root_component_type_list_1_spec_for_Usage, _extension_additions_list_spec_for_Usage, _root_component_type_list_2_spec_for_Usage, undefined);
return new Usage(keyUsage, extKeyUsage);
};
}
return _cached_decoder_for_Usage(el);
}
let _cached_encoder_for_Usage = null;
/**
* @summary Encodes a(n) Usage 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 Usage, encoded as an ASN.1 Element.
*/
export function _encode_Usage(value, elGetter) {
if (!_cached_encoder_for_Usage) {
_cached_encoder_for_Usage = function (value) {
return $._encodeSequence([]
.concat([
/* IF_ABSENT */ value.keyUsage === undefined
? undefined
: _encode_KeyUsage(value.keyUsage, $.BER),
/* IF_ABSENT */ value.extKeyUsage === undefined
? undefined
: $._encodeSequenceOf(() => $._encodeObjectIdentifier, $.BER)(value.extKeyUsage, $.BER),
])
.filter((c) => !!c), $.BER);
};
}
return _cached_encoder_for_Usage(value, elGetter);
}
/* eslint-enable */