tor-browser

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

GeneralNames.ts (3432B)


      1 import * as asn1js from "asn1js";
      2 import * as pvutils from "pvutils";
      3 import * as Schema from "./Schema";
      4 import { GeneralName, GeneralNameJson } from "./GeneralName";
      5 import { PkiObject, PkiObjectParameters } from "./PkiObject";
      6 import { AsnError } from "./errors";
      7 import { EMPTY_STRING } from "./constants";
      8 
      9 const NAMES = "names";
     10 const GENERAL_NAMES = "generalNames";
     11 
     12 export interface IGeneralNames {
     13  names: GeneralName[];
     14 }
     15 
     16 export type GeneralNamesParameters = PkiObjectParameters & Partial<IGeneralNames>;
     17 
     18 export type GeneralNamesSchema = Schema.SchemaParameters<{
     19  generalNames?: string;
     20 }>;
     21 
     22 export interface GeneralNamesJson {
     23  names: GeneralNameJson[];
     24 }
     25 
     26 /**
     27 * Represents the GeneralNames structure described in [RFC5280](https://datatracker.ietf.org/doc/html/rfc5280)
     28 */
     29 export class GeneralNames extends PkiObject implements IGeneralNames {
     30 
     31  public static override CLASS_NAME = "GeneralNames";
     32 
     33  /**
     34   * Array of "general names"
     35   */
     36  public names!: GeneralName[];
     37 
     38  /**
     39   * Initializes a new instance of the {@link GeneralNames} class
     40   * @param parameters Initialization parameters
     41   */
     42  constructor(parameters: GeneralNamesParameters = {}) {
     43    super();
     44 
     45    this.names = pvutils.getParametersValue(parameters, NAMES, GeneralNames.defaultValues(NAMES));
     46 
     47    if (parameters.schema) {
     48      this.fromSchema(parameters.schema);
     49    }
     50  }
     51 
     52  /**
     53   * Returns default values for all class members
     54   * @param memberName String name for a class member
     55   * @returns Default value
     56   */
     57  public static override defaultValues(memberName: typeof NAMES): GeneralName[];
     58  public static override defaultValues(memberName: string): any {
     59    switch (memberName) {
     60      case "names":
     61        return [];
     62      default:
     63        return super.defaultValues(memberName);
     64    }
     65  }
     66 
     67  /**
     68   * @inheritdoc
     69   * @asn ASN.1 schema
     70   * ```asn
     71   * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
     72   * ```
     73   *
     74   * @param parameters Input parameters for the schema
     75   * @param optional Flag would be element optional or not
     76   * @returns ASN.1 schema object
     77   */
     78  public static override schema(parameters: GeneralNamesSchema = {}, optional = false): Schema.SchemaType {
     79    const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, NAMES, {});
     80 
     81    return (new asn1js.Sequence({
     82      optional,
     83      name: (names.blockName || EMPTY_STRING),
     84      value: [
     85        new asn1js.Repeated({
     86          name: (names.generalNames || EMPTY_STRING),
     87          value: GeneralName.schema()
     88        })
     89      ]
     90    }));
     91  }
     92 
     93  public fromSchema(schema: Schema.SchemaType): void {
     94    //#region Clear input data first
     95    pvutils.clearProps(schema, [
     96      NAMES,
     97      GENERAL_NAMES
     98    ]);
     99    //#endregion
    100 
    101    //#region Check the schema is valid
    102    const asn1 = asn1js.compareSchema(schema,
    103      schema,
    104      GeneralNames.schema({
    105        names: {
    106          blockName: NAMES,
    107          generalNames: GENERAL_NAMES
    108        }
    109      })
    110    );
    111 
    112    AsnError.assertSchema(asn1, this.className);
    113    //#endregion
    114 
    115    this.names = Array.from(asn1.result.generalNames, element => new GeneralName({ schema: element }));
    116  }
    117 
    118  public toSchema(): asn1js.Sequence {
    119    return (new asn1js.Sequence({
    120      value: Array.from(this.names, o => o.toSchema())
    121    }));
    122  }
    123 
    124  public toJSON(): GeneralNamesJson {
    125    return {
    126      names: Array.from(this.names, o => o.toJSON())
    127    };
    128  }
    129 
    130 }