tor-browser

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

RevocationInfoChoices.ts (5243B)


      1 import * as asn1js from "asn1js";
      2 import * as pvutils from "pvutils";
      3 import { CertificateRevocationList, CertificateRevocationListJson } from "./CertificateRevocationList";
      4 import { EMPTY_STRING } from "./constants";
      5 import { AsnError } from "./errors";
      6 import { OtherRevocationInfoFormat, OtherRevocationInfoFormatJson } from "./OtherRevocationInfoFormat";
      7 import { PkiObject, PkiObjectParameters } from "./PkiObject";
      8 import * as Schema from "./Schema";
      9 
     10 const CRLS = "crls";
     11 const OTHER_REVOCATION_INFOS = "otherRevocationInfos";
     12 const CLEAR_PROPS = [
     13  CRLS
     14 ];
     15 
     16 export interface IRevocationInfoChoices {
     17  crls: CertificateRevocationList[];
     18  otherRevocationInfos: OtherRevocationInfoFormat[];
     19 }
     20 
     21 export interface RevocationInfoChoicesJson {
     22  crls: CertificateRevocationListJson[];
     23  otherRevocationInfos: OtherRevocationInfoFormatJson[];
     24 }
     25 
     26 export type RevocationInfoChoicesParameters = PkiObjectParameters & Partial<IRevocationInfoChoices>;
     27 
     28 export type RevocationInfoChoicesSchema = Schema.SchemaParameters<{
     29  crls?: string;
     30 }>;
     31 
     32 /**
     33 * Represents the RevocationInfoChoices structure described in [RFC5652](https://datatracker.ietf.org/doc/html/rfc5652)
     34 */
     35 export class RevocationInfoChoices extends PkiObject implements IRevocationInfoChoices {
     36 
     37  public static override CLASS_NAME = "RevocationInfoChoices";
     38 
     39  public crls!: CertificateRevocationList[];
     40  public otherRevocationInfos!: OtherRevocationInfoFormat[];
     41 
     42  /**
     43   * Initializes a new instance of the {@link RevocationInfoChoices} class
     44   * @param parameters Initialization parameters
     45   */
     46  constructor(parameters: RevocationInfoChoicesParameters = {}) {
     47    super();
     48 
     49    this.crls = pvutils.getParametersValue(parameters, CRLS, RevocationInfoChoices.defaultValues(CRLS));
     50    this.otherRevocationInfos = pvutils.getParametersValue(parameters, OTHER_REVOCATION_INFOS, RevocationInfoChoices.defaultValues(OTHER_REVOCATION_INFOS));
     51 
     52    if (parameters.schema) {
     53      this.fromSchema(parameters.schema);
     54    }
     55  }
     56 
     57  /**
     58   * Returns default values for all class members
     59   * @param memberName String name for a class member
     60   * @returns Default value
     61   */
     62  public static override defaultValues(memberName: typeof CRLS): CertificateRevocationList[];
     63  public static override defaultValues(memberName: typeof OTHER_REVOCATION_INFOS): OtherRevocationInfoFormat[];
     64  public static override defaultValues(memberName: string): any {
     65    switch (memberName) {
     66      case CRLS:
     67        return [];
     68      case OTHER_REVOCATION_INFOS:
     69        return [];
     70      default:
     71        return super.defaultValues(memberName);
     72    }
     73  }
     74 
     75  /**
     76   * @inheritdoc
     77   * @asn ASN.1 schema
     78   * ```asn
     79   * RevocationInfoChoices ::= SET OF RevocationInfoChoice
     80   *
     81   * RevocationInfoChoice ::= CHOICE {
     82   *    crl CertificateList,
     83   *    other [1] IMPLICIT OtherRevocationInfoFormat }
     84   *```
     85   */
     86  public static override schema(parameters: RevocationInfoChoicesSchema = {}): Schema.SchemaType {
     87    const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {});
     88 
     89    return (new asn1js.Set({
     90      name: (names.blockName || EMPTY_STRING),
     91      value: [
     92        new asn1js.Repeated({
     93          name: (names.crls || EMPTY_STRING),
     94          value: new asn1js.Choice({
     95            value: [
     96              CertificateRevocationList.schema(),
     97              new asn1js.Constructed({
     98                idBlock: {
     99                  tagClass: 3, // CONTEXT-SPECIFIC
    100                  tagNumber: 1 // [1]
    101                },
    102                value: [
    103                  new asn1js.ObjectIdentifier(),
    104                  new asn1js.Any()
    105                ]
    106              })
    107            ]
    108          })
    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      RevocationInfoChoices.schema({
    122        names: {
    123          crls: CRLS
    124        }
    125      })
    126    );
    127    AsnError.assertSchema(asn1, this.className);
    128 
    129    // Get internal properties from parsed schema
    130    if (asn1.result.crls) {
    131      for (const element of asn1.result.crls) {
    132        if (element.idBlock.tagClass === 1)
    133          this.crls.push(new CertificateRevocationList({ schema: element }));
    134        else
    135          this.otherRevocationInfos.push(new OtherRevocationInfoFormat({ schema: element }));
    136      }
    137    }
    138  }
    139 
    140  public toSchema(): asn1js.Sequence {
    141    //#region Create array for output set
    142    const outputArray = [];
    143 
    144    outputArray.push(...Array.from(this.crls, o => o.toSchema()));
    145 
    146    outputArray.push(...Array.from(this.otherRevocationInfos, element => {
    147      const schema = element.toSchema();
    148 
    149      schema.idBlock.tagClass = 3;
    150      schema.idBlock.tagNumber = 1;
    151 
    152      return schema;
    153    }));
    154    //#endregion
    155 
    156    //#region Construct and return new ASN.1 schema for this object
    157    return (new asn1js.Set({
    158      value: outputArray
    159    }));
    160    //#endregion
    161  }
    162 
    163  public toJSON(): RevocationInfoChoicesJson {
    164    return {
    165      crls: Array.from(this.crls, o => o.toJSON()),
    166      otherRevocationInfos: Array.from(this.otherRevocationInfos, o => o.toJSON())
    167    };
    168  }
    169 
    170 }