tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

PkiObject.ts (2805B)


      1 /* eslint-disable @typescript-eslint/no-unused-vars */
      2 import * as asn1js from "asn1js";
      3 import * as pvtsutils from "pvtsutils";
      4 import { AsnError } from "./errors";
      5 import * as Schema from "./Schema";
      6 
      7 export interface PkiObjectParameters {
      8  schema?: Schema.SchemaType;
      9 }
     10 
     11 interface PkiObjectConstructor<T extends PkiObject = PkiObject> {
     12  new(params: PkiObjectParameters): T;
     13  CLASS_NAME: string;
     14 }
     15 
     16 export abstract class PkiObject {
     17 
     18  /**
     19   * Name of the class
     20   */
     21  public static CLASS_NAME = "PkiObject";
     22 
     23  /**
     24   * Returns block name
     25   * @returns Returns string block name
     26   */
     27  public static blockName(): string {
     28    return this.CLASS_NAME;
     29  }
     30 
     31  /**
     32   * Creates PKI object from the raw data
     33   * @param raw ASN.1 encoded raw data
     34   * @returns Initialized and filled current class object
     35   */
     36  public static fromBER<T extends PkiObject>(this: PkiObjectConstructor<T>, raw: BufferSource): T {
     37    const asn1 = asn1js.fromBER(raw);
     38    AsnError.assert(asn1, this.name);
     39 
     40    try {
     41      return new this({ schema: asn1.result });
     42    } catch (e) {
     43      throw new AsnError(`Cannot create '${this.CLASS_NAME}' from ASN.1 object`);
     44    }
     45  }
     46 
     47  /**
     48   * Returns default values for all class members
     49   * @param memberName String name for a class member
     50   * @returns Default value
     51   */
     52  public static defaultValues(memberName: string): any {
     53    throw new Error(`Invalid member name for ${this.CLASS_NAME} class: ${memberName}`);
     54  }
     55 
     56  /**
     57   * Returns value of pre-defined ASN.1 schema for current class
     58   * @param parameters Input parameters for the schema
     59   * @returns ASN.1 schema object
     60   */
     61  public static schema(parameters: Schema.SchemaParameters = {}): Schema.SchemaType {
     62    throw new Error(`Method '${this.CLASS_NAME}.schema' should be overridden`);
     63  }
     64 
     65  public get className(): string {
     66    return (this.constructor as unknown as { CLASS_NAME: string; }).CLASS_NAME;
     67  }
     68 
     69  /**
     70   * Converts parsed ASN.1 object into current class
     71   * @param schema ASN.1 schema
     72   */
     73  public abstract fromSchema(schema: Schema.SchemaType): void;
     74 
     75  /**
     76   * Converts current object to ASN.1 object and sets correct values
     77   * @param encodeFlag If param equal to `false` then creates schema via decoding stored value. In other case creates schema via assembling from cached parts
     78   * @returns ASN.1 object
     79   */
     80  public abstract toSchema(encodeFlag?: boolean): Schema.SchemaType;
     81 
     82  /**
     83   * Converts the class to JSON object
     84   * @returns JSON object
     85   */
     86  public abstract toJSON(): any;
     87 
     88  public toString(encoding: "hex" | "base64" | "base64url" = "hex"): string {
     89    let schema: Schema.SchemaType;
     90 
     91    try {
     92      schema = this.toSchema();
     93    } catch {
     94      schema = this.toSchema(true);
     95    }
     96 
     97    return pvtsutils.Convert.ToString(schema.toBER(), encoding);
     98  }
     99 
    100 }