tor-browser

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

PolicyInformation.ts (5031B)


      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 { PolicyQualifierInfo, PolicyQualifierInfoJson } from "./PolicyQualifierInfo";
      7 import * as Schema from "./Schema";
      8 
      9 export const POLICY_IDENTIFIER = "policyIdentifier";
     10 export const POLICY_QUALIFIERS = "policyQualifiers";
     11 const CLEAR_PROPS = [
     12  POLICY_IDENTIFIER,
     13  POLICY_QUALIFIERS
     14 ];
     15 
     16 export interface IPolicyInformation {
     17  policyIdentifier: string;
     18  policyQualifiers?: PolicyQualifierInfo[];
     19 }
     20 
     21 export type PolicyInformationParameters = PkiObjectParameters & Partial<IPolicyInformation>;
     22 
     23 export interface PolicyInformationJson {
     24  policyIdentifier: string;
     25  policyQualifiers?: PolicyQualifierInfoJson[];
     26 }
     27 
     28 /**
     29 * Represents the PolicyInformation structure described in [RFC5280](https://datatracker.ietf.org/doc/html/rfc5280)
     30 */
     31 export class PolicyInformation extends PkiObject implements IPolicyInformation {
     32 
     33  public static override CLASS_NAME = "PolicyInformation";
     34 
     35  public policyIdentifier!: string;
     36  public policyQualifiers?: PolicyQualifierInfo[];
     37 
     38  /**
     39   * Initializes a new instance of the {@link PolicyInformation} class
     40   * @param parameters Initialization parameters
     41   */
     42  constructor(parameters: PolicyInformationParameters = {}) {
     43    super();
     44 
     45    this.policyIdentifier = pvutils.getParametersValue(parameters, POLICY_IDENTIFIER, PolicyInformation.defaultValues(POLICY_IDENTIFIER));
     46    if (POLICY_QUALIFIERS in parameters) {
     47      this.policyQualifiers = pvutils.getParametersValue(parameters, POLICY_QUALIFIERS, PolicyInformation.defaultValues(POLICY_QUALIFIERS));
     48    }
     49    if (parameters.schema) {
     50      this.fromSchema(parameters.schema);
     51    }
     52  }
     53 
     54  /**
     55   * Returns default values for all class members
     56   * @param memberName String name for a class member
     57   * @returns Default value
     58   */
     59  public static override defaultValues(memberName: typeof POLICY_IDENTIFIER): string;
     60  public static override defaultValues(memberName: typeof POLICY_QUALIFIERS): PolicyQualifierInfo[];
     61  public static override defaultValues(memberName: string): any {
     62    switch (memberName) {
     63      case POLICY_IDENTIFIER:
     64        return EMPTY_STRING;
     65      case POLICY_QUALIFIERS:
     66        return [];
     67      default:
     68        return super.defaultValues(memberName);
     69    }
     70  }
     71 
     72  /**
     73   * @inheritdoc
     74   * @asn ASN.1 schema
     75   * ```asn
     76   * PolicyInformation ::= SEQUENCE {
     77   *    policyIdentifier   CertPolicyId,
     78   *    policyQualifiers   SEQUENCE SIZE (1..MAX) OF
     79   *    PolicyQualifierInfo OPTIONAL }
     80   *
     81   * CertPolicyId ::= OBJECT IDENTIFIER
     82   *```
     83   */
     84  public static override schema(parameters: Schema.SchemaParameters<{ policyIdentifier?: string; policyQualifiers?: string; }> = {}): Schema.SchemaType {
     85    const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {});
     86 
     87    return (new asn1js.Sequence({
     88      name: (names.blockName || EMPTY_STRING),
     89      value: [
     90        new asn1js.ObjectIdentifier({ name: (names.policyIdentifier || EMPTY_STRING) }),
     91        new asn1js.Sequence({
     92          optional: true,
     93          value: [
     94            new asn1js.Repeated({
     95              name: (names.policyQualifiers || EMPTY_STRING),
     96              value: PolicyQualifierInfo.schema()
     97            })
     98          ]
     99        })
    100      ]
    101    }));
    102  }
    103 
    104  /**
    105   * Converts parsed ASN.1 object into current class
    106   * @param schema
    107   */
    108  fromSchema(schema: Schema.SchemaType): void {
    109    // Clear input data first
    110    pvutils.clearProps(schema, CLEAR_PROPS);
    111 
    112    // Check the schema is valid
    113    const asn1 = asn1js.compareSchema(schema,
    114      schema,
    115      PolicyInformation.schema({
    116        names: {
    117          policyIdentifier: POLICY_IDENTIFIER,
    118          policyQualifiers: POLICY_QUALIFIERS
    119        }
    120      })
    121    );
    122    AsnError.assertSchema(asn1, this.className);
    123 
    124    // Get internal properties from parsed schema
    125    this.policyIdentifier = asn1.result.policyIdentifier.valueBlock.toString();
    126    if (POLICY_QUALIFIERS in asn1.result) {
    127      this.policyQualifiers = Array.from(asn1.result.policyQualifiers, element => new PolicyQualifierInfo({ schema: element }));
    128    }
    129  }
    130 
    131  public toSchema(): asn1js.Sequence {
    132    //Create array for output sequence
    133    const outputArray = [];
    134 
    135    outputArray.push(new asn1js.ObjectIdentifier({ value: this.policyIdentifier }));
    136 
    137    if (this.policyQualifiers) {
    138      outputArray.push(new asn1js.Sequence({
    139        value: Array.from(this.policyQualifiers, o => o.toSchema())
    140      }));
    141    }
    142 
    143    // Construct and return new ASN.1 schema for this object
    144    return (new asn1js.Sequence({
    145      value: outputArray
    146    }));
    147  }
    148 
    149  public toJSON(): PolicyInformationJson {
    150    const res: PolicyInformationJson = {
    151      policyIdentifier: this.policyIdentifier
    152    };
    153 
    154    if (this.policyQualifiers)
    155      res.policyQualifiers = Array.from(this.policyQualifiers, o => o.toJSON());
    156 
    157    return res;
    158  }
    159 
    160 }