tor-browser

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

ExtKeyUsage.ts (3330B)


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