tor-browser

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

CertificateTemplate.ts (5585B)


      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 TEMPLATE_ID = "templateID";
      9 const TEMPLATE_MAJOR_VERSION = "templateMajorVersion";
     10 const TEMPLATE_MINOR_VERSION = "templateMinorVersion";
     11 const CLEAR_PROPS = [
     12  TEMPLATE_ID,
     13  TEMPLATE_MAJOR_VERSION,
     14  TEMPLATE_MINOR_VERSION
     15 ];
     16 
     17 export interface ICertificateTemplate {
     18  templateID: string;
     19  templateMajorVersion?: number;
     20  templateMinorVersion?: number;
     21 }
     22 
     23 export interface CertificateTemplateJson {
     24  templateID: string;
     25  templateMajorVersion?: number;
     26  templateMinorVersion?: number;
     27 }
     28 
     29 export type CertificateTemplateParameters = PkiObjectParameters & Partial<ICertificateTemplate>;
     30 
     31 /**
     32 * Class from "[MS-WCCE]: Windows Client Certificate Enrollment Protocol"
     33 */
     34 export class CertificateTemplate extends PkiObject implements ICertificateTemplate {
     35 
     36  public templateID!: string;
     37  public templateMajorVersion?: number;
     38  public templateMinorVersion?: number;
     39 
     40  /**
     41   * Initializes a new instance of the {@link CertificateTemplate} class
     42   * @param parameters Initialization parameters
     43   */
     44  constructor(parameters: CertificateTemplateParameters = {}) {
     45    super();
     46 
     47    this.templateID = pvutils.getParametersValue(parameters, TEMPLATE_ID, CertificateTemplate.defaultValues(TEMPLATE_ID));
     48    if (TEMPLATE_MAJOR_VERSION in parameters) {
     49      this.templateMajorVersion = pvutils.getParametersValue(parameters, TEMPLATE_MAJOR_VERSION, CertificateTemplate.defaultValues(TEMPLATE_MAJOR_VERSION));
     50    }
     51    if (TEMPLATE_MINOR_VERSION in parameters) {
     52      this.templateMinorVersion = pvutils.getParametersValue(parameters, TEMPLATE_MINOR_VERSION, CertificateTemplate.defaultValues(TEMPLATE_MINOR_VERSION));
     53    }
     54 
     55    if (parameters.schema) {
     56      this.fromSchema(parameters.schema);
     57    }
     58  }
     59 
     60  /**
     61   * Returns default values for all class members
     62   * @param memberName String name for a class member
     63   * @returns Default value
     64   */
     65  public static override defaultValues(memberName: typeof TEMPLATE_MINOR_VERSION): number;
     66  public static override defaultValues(memberName: typeof TEMPLATE_MAJOR_VERSION): number;
     67  public static override defaultValues(memberName: typeof TEMPLATE_ID): string;
     68  public static override defaultValues(memberName: string): any {
     69    switch (memberName) {
     70      case TEMPLATE_ID:
     71        return EMPTY_STRING;
     72      case TEMPLATE_MAJOR_VERSION:
     73      case TEMPLATE_MINOR_VERSION:
     74        return 0;
     75      default:
     76        return super.defaultValues(memberName);
     77    }
     78  }
     79 
     80  /**
     81   * @inheritdoc
     82   * @asn ASN.1 schema
     83   * ```asn
     84   * CertificateTemplateOID ::= SEQUENCE {
     85   *    templateID              OBJECT IDENTIFIER,
     86   *    templateMajorVersion    INTEGER (0..4294967295) OPTIONAL,
     87   *    templateMinorVersion    INTEGER (0..4294967295) OPTIONAL
     88   * }
     89   *```
     90   */
     91  static override schema(parameters: Schema.SchemaParameters<{
     92    templateID?: string,
     93    templateMajorVersion?: string,
     94    templateMinorVersion?: string,
     95  }> = {}): Schema.SchemaType {
     96    const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {});
     97 
     98    return (new asn1js.Sequence({
     99      name: (names.blockName || EMPTY_STRING),
    100      value: [
    101        new asn1js.ObjectIdentifier({ name: (names.templateID || EMPTY_STRING) }),
    102        new asn1js.Integer({
    103          name: (names.templateMajorVersion || EMPTY_STRING),
    104          optional: true
    105        }),
    106        new asn1js.Integer({
    107          name: (names.templateMinorVersion || EMPTY_STRING),
    108          optional: true
    109        }),
    110      ]
    111    }));
    112  }
    113 
    114  public fromSchema(schema: Schema.SchemaType): void {
    115    // Clear input data first
    116    pvutils.clearProps(schema, CLEAR_PROPS);
    117 
    118    // Check the schema is valid
    119    const asn1 = asn1js.compareSchema(schema,
    120      schema,
    121      CertificateTemplate.schema({
    122        names: {
    123          templateID: TEMPLATE_ID,
    124          templateMajorVersion: TEMPLATE_MAJOR_VERSION,
    125          templateMinorVersion: TEMPLATE_MINOR_VERSION
    126        }
    127      })
    128    );
    129    AsnError.assertSchema(asn1, this.className);
    130 
    131    // Get internal properties from parsed schema
    132    this.templateID = asn1.result.templateID.valueBlock.toString();
    133    if (TEMPLATE_MAJOR_VERSION in asn1.result) {
    134      this.templateMajorVersion = asn1.result.templateMajorVersion.valueBlock.valueDec;
    135    }
    136    if (TEMPLATE_MINOR_VERSION in asn1.result) {
    137      this.templateMinorVersion = asn1.result.templateMinorVersion.valueBlock.valueDec;
    138    }
    139  }
    140 
    141  public toSchema(): asn1js.Sequence {
    142    // Create array for output sequence
    143    const outputArray = [];
    144    outputArray.push(new asn1js.ObjectIdentifier({ value: this.templateID }));
    145    if (TEMPLATE_MAJOR_VERSION in this) {
    146      outputArray.push(new asn1js.Integer({ value: this.templateMajorVersion }));
    147    }
    148    if (TEMPLATE_MINOR_VERSION in this) {
    149      outputArray.push(new asn1js.Integer({ value: this.templateMinorVersion }));
    150    }
    151 
    152    // Construct and return new ASN.1 schema for this object
    153    return (new asn1js.Sequence({
    154      value: outputArray
    155    }));
    156  }
    157 
    158  public toJSON(): CertificateTemplateJson {
    159    const res: CertificateTemplateJson = {
    160      templateID: this.templateID
    161    };
    162 
    163    if (TEMPLATE_MAJOR_VERSION in this)
    164      res.templateMajorVersion = this.templateMajorVersion;
    165 
    166    if (TEMPLATE_MINOR_VERSION in this)
    167      res.templateMinorVersion = this.templateMinorVersion;
    168 
    169    return res;
    170  }
    171 
    172 }