diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:44:51 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:44:51 +0000 |
commit | 9e3c08db40b8916968b9f30096c7be3f00ce9647 (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /third_party/js/PKI.js/src/PkiObject.ts | |
parent | Initial commit. (diff) | |
download | thunderbird-9e3c08db40b8916968b9f30096c7be3f00ce9647.tar.xz thunderbird-9e3c08db40b8916968b9f30096c7be3f00ce9647.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/js/PKI.js/src/PkiObject.ts')
-rw-r--r-- | third_party/js/PKI.js/src/PkiObject.ts | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/third_party/js/PKI.js/src/PkiObject.ts b/third_party/js/PKI.js/src/PkiObject.ts new file mode 100644 index 0000000000..780d6fc328 --- /dev/null +++ b/third_party/js/PKI.js/src/PkiObject.ts @@ -0,0 +1,100 @@ +/* eslint-disable @typescript-eslint/no-unused-vars */ +import * as asn1js from "asn1js"; +import * as pvtsutils from "pvtsutils"; +import { AsnError } from "./errors"; +import * as Schema from "./Schema"; + +export interface PkiObjectParameters { + schema?: Schema.SchemaType; +} + +interface PkiObjectConstructor<T extends PkiObject = PkiObject> { + new(params: PkiObjectParameters): T; + CLASS_NAME: string; +} + +export abstract class PkiObject { + + /** + * Name of the class + */ + public static CLASS_NAME = "PkiObject"; + + /** + * Returns block name + * @returns Returns string block name + */ + public static blockName(): string { + return this.CLASS_NAME; + } + + /** + * Creates PKI object from the raw data + * @param raw ASN.1 encoded raw data + * @returns Initialized and filled current class object + */ + public static fromBER<T extends PkiObject>(this: PkiObjectConstructor<T>, raw: BufferSource): T { + const asn1 = asn1js.fromBER(raw); + AsnError.assert(asn1, this.name); + + try { + return new this({ schema: asn1.result }); + } catch (e) { + throw new AsnError(`Cannot create '${this.CLASS_NAME}' from ASN.1 object`); + } + } + + /** + * Returns default values for all class members + * @param memberName String name for a class member + * @returns Default value + */ + public static defaultValues(memberName: string): any { + throw new Error(`Invalid member name for ${this.CLASS_NAME} class: ${memberName}`); + } + + /** + * Returns value of pre-defined ASN.1 schema for current class + * @param parameters Input parameters for the schema + * @returns ASN.1 schema object + */ + public static schema(parameters: Schema.SchemaParameters = {}): Schema.SchemaType { + throw new Error(`Method '${this.CLASS_NAME}.schema' should be overridden`); + } + + public get className(): string { + return (this.constructor as unknown as { CLASS_NAME: string; }).CLASS_NAME; + } + + /** + * Converts parsed ASN.1 object into current class + * @param schema ASN.1 schema + */ + public abstract fromSchema(schema: Schema.SchemaType): void; + + /** + * Converts current object to ASN.1 object and sets correct values + * @param encodeFlag If param equal to `false` then creates schema via decoding stored value. In other case creates schema via assembling from cached parts + * @returns ASN.1 object + */ + public abstract toSchema(encodeFlag?: boolean): Schema.SchemaType; + + /** + * Converts the class to JSON object + * @returns JSON object + */ + public abstract toJSON(): any; + + public toString(encoding: "hex" | "base64" | "base64url" = "hex"): string { + let schema: Schema.SchemaType; + + try { + schema = this.toSchema(); + } catch { + schema = this.toSchema(true); + } + + return pvtsutils.Convert.ToString(schema.toBER(), encoding); + } + +}
\ No newline at end of file |