tor-browser

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

CertificatePolicies.ts (3470B)


      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 { PolicyInformation, PolicyInformationJson } from "./PolicyInformation";
      7 import * as Schema from "./Schema";
      8 
      9 const CERTIFICATE_POLICIES = "certificatePolicies";
     10 const CLEAR_PROPS = [
     11  CERTIFICATE_POLICIES,
     12 ];
     13 
     14 export interface ICertificatePolicies {
     15  certificatePolicies: PolicyInformation[];
     16 }
     17 
     18 export type CertificatePoliciesParameters = PkiObjectParameters & Partial<ICertificatePolicies>;
     19 
     20 export interface CertificatePoliciesJson {
     21  certificatePolicies: PolicyInformationJson[];
     22 }
     23 
     24 /**
     25 * Represents the CertificatePolicies structure described in [RFC5280](https://datatracker.ietf.org/doc/html/rfc5280)
     26 */
     27 export class CertificatePolicies extends PkiObject implements ICertificatePolicies {
     28 
     29  public static override CLASS_NAME = "CertificatePolicies";
     30 
     31  public certificatePolicies!: PolicyInformation[];
     32 
     33  /**
     34   * Initializes a new instance of the {@link CertificatePolicies} class
     35   * @param parameters Initialization parameters
     36   */
     37  constructor(parameters: CertificatePoliciesParameters = {}) {
     38    super();
     39 
     40    this.certificatePolicies = pvutils.getParametersValue(parameters, CERTIFICATE_POLICIES, CertificatePolicies.defaultValues(CERTIFICATE_POLICIES));
     41 
     42    if (parameters.schema) {
     43      this.fromSchema(parameters.schema);
     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 override defaultValues(memberName: typeof CERTIFICATE_POLICIES): PolicyInformation[];
     53  public static override defaultValues(memberName: string): any {
     54    switch (memberName) {
     55      case CERTIFICATE_POLICIES:
     56        return [];
     57      default:
     58        return super.defaultValues(memberName);
     59    }
     60  }
     61 
     62  /**
     63   * @inheritdoc
     64   * @asn ASN.1 schema
     65   * ```asn
     66   * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
     67   *```
     68   */
     69  static override schema(parameters: Schema.SchemaParameters<{ certificatePolicies?: string; }> = {}) {
     70    const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {});
     71 
     72    return (new asn1js.Sequence({
     73      name: (names.blockName || EMPTY_STRING),
     74      value: [
     75        new asn1js.Repeated({
     76          name: (names.certificatePolicies || EMPTY_STRING),
     77          value: PolicyInformation.schema()
     78        })
     79      ]
     80    }));
     81  }
     82 
     83  public fromSchema(schema: Schema.SchemaType): void {
     84    // Clear input data first
     85    pvutils.clearProps(schema, CLEAR_PROPS);
     86 
     87    // Check the schema is valid
     88    const asn1 = asn1js.compareSchema(schema,
     89      schema,
     90      CertificatePolicies.schema({
     91        names: {
     92          certificatePolicies: CERTIFICATE_POLICIES
     93        }
     94      })
     95    );
     96    AsnError.assertSchema(asn1, this.className);
     97 
     98    // Get internal properties from parsed schema
     99    this.certificatePolicies = Array.from(asn1.result.certificatePolicies, element => new PolicyInformation({ schema: element }));
    100  }
    101 
    102  public toSchema(): asn1js.Sequence {
    103    return (new asn1js.Sequence({
    104      value: Array.from(this.certificatePolicies, o => o.toSchema())
    105    }));
    106  }
    107 
    108  public toJSON(): CertificatePoliciesJson {
    109    return {
    110      certificatePolicies: Array.from(this.certificatePolicies, o => o.toJSON())
    111    };
    112  }
    113 
    114 }