tor-browser

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

OriginatorInfo.ts (6047B)


      1 import * as asn1js from "asn1js";
      2 import * as pvutils from "pvutils";
      3 import { CertificateSet, CertificateSetJson } from "./CertificateSet";
      4 import { EMPTY_STRING } from "./constants";
      5 import { AsnError } from "./errors";
      6 import { PkiObject, PkiObjectParameters } from "./PkiObject";
      7 import { RevocationInfoChoices, RevocationInfoChoicesJson } from "./RevocationInfoChoices";
      8 import * as Schema from "./Schema";
      9 
     10 const CERTS = "certs";
     11 const CRLS = "crls";
     12 const CLEAR_PROPS = [
     13  CERTS,
     14  CRLS,
     15 ];
     16 
     17 export interface IOriginatorInfo {
     18  /**
     19   * Collection of certificates. In may contain originator certificates associated with several different
     20   * key management algorithms. It may also contain attribute certificates associated with the originator.
     21   */
     22  certs?: CertificateSet;
     23  /**
     24   * Collection of CRLs. It is intended that the set contain information sufficient to determine whether
     25   * or not the certificates in the certs field are valid, but such correspondence is not necessary
     26   */
     27  crls?: RevocationInfoChoices;
     28 }
     29 
     30 export interface OriginatorInfoJson {
     31  certs?: CertificateSetJson;
     32  crls?: RevocationInfoChoicesJson;
     33 }
     34 
     35 export type OriginatorInfoParameters = PkiObjectParameters & Partial<IOriginatorInfo>;
     36 
     37 /**
     38 * Represents the OriginatorInfo structure described in [RFC5652](https://datatracker.ietf.org/doc/html/rfc5652)
     39 */
     40 export class OriginatorInfo extends PkiObject implements IOriginatorInfo {
     41 
     42  public static override CLASS_NAME = "OriginatorInfo";
     43 
     44  public certs?: CertificateSet;
     45  public crls?: RevocationInfoChoices;
     46 
     47  /**
     48   * Initializes a new instance of the {@link CertificateSet} class
     49   * @param parameters Initialization parameters
     50   */
     51  constructor(parameters: OriginatorInfoParameters = {}) {
     52    super();
     53 
     54    this.crls = pvutils.getParametersValue(parameters, CRLS, OriginatorInfo.defaultValues(CRLS));
     55 
     56    if (parameters.schema) {
     57      this.fromSchema(parameters.schema);
     58    }
     59  }
     60 
     61  /**
     62   * Returns default values for all class members
     63   * @param memberName String name for a class member
     64   * @returns Default value
     65   */
     66  public static override defaultValues(memberName: typeof CERTS): CertificateSet;
     67  public static override defaultValues(memberName: typeof CRLS): RevocationInfoChoices;
     68  public static override defaultValues(memberName: string): any {
     69    switch (memberName) {
     70      case CERTS:
     71        return new CertificateSet();
     72      case CRLS:
     73        return new RevocationInfoChoices();
     74      default:
     75        return super.defaultValues(memberName);
     76    }
     77  }
     78 
     79  /**
     80   * Compare values with default values for all class members
     81   * @param memberName String name for a class member
     82   * @param memberValue Value to compare with default value
     83   */
     84  public static compareWithDefault(memberName: string, memberValue: any): boolean {
     85    switch (memberName) {
     86      case CERTS:
     87        return (memberValue.certificates.length === 0);
     88      case CRLS:
     89        return ((memberValue.crls.length === 0) && (memberValue.otherRevocationInfos.length === 0));
     90      default:
     91        return super.defaultValues(memberName);
     92    }
     93  }
     94 
     95  /**
     96   * @inheritdoc
     97   * @asn ASN.1 schema
     98   * ```asn
     99   * OriginatorInfo ::= SEQUENCE {
    100   *    certs [0] IMPLICIT CertificateSet OPTIONAL,
    101   *    crls [1] IMPLICIT RevocationInfoChoices OPTIONAL }
    102   *```
    103   */
    104  public static override schema(parameters: Schema.SchemaParameters<{
    105    certs?: string;
    106    crls?: string;
    107  }> = {}): Schema.SchemaType {
    108    const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {});
    109 
    110    return (new asn1js.Sequence({
    111      name: (names.blockName || EMPTY_STRING),
    112      value: [
    113        new asn1js.Constructed({
    114          name: (names.certs || EMPTY_STRING),
    115          optional: true,
    116          idBlock: {
    117            tagClass: 3, // CONTEXT-SPECIFIC
    118            tagNumber: 0 // [0]
    119          },
    120          value: CertificateSet.schema().valueBlock.value
    121        }),
    122        new asn1js.Constructed({
    123          name: (names.crls || EMPTY_STRING),
    124          optional: true,
    125          idBlock: {
    126            tagClass: 3, // CONTEXT-SPECIFIC
    127            tagNumber: 1 // [1]
    128          },
    129          value: RevocationInfoChoices.schema().valueBlock.value
    130        })
    131      ]
    132    }));
    133  }
    134 
    135  public fromSchema(schema: Schema.SchemaType): void {
    136    // Clear input data first
    137    pvutils.clearProps(schema, CLEAR_PROPS);
    138 
    139    // Check the schema is valid
    140    const asn1 = asn1js.compareSchema(schema,
    141      schema,
    142      OriginatorInfo.schema({
    143        names: {
    144          certs: CERTS,
    145          crls: CRLS
    146        }
    147      })
    148    );
    149    AsnError.assertSchema(asn1, this.className);
    150 
    151    // Get internal properties from parsed schema
    152    if (CERTS in asn1.result) {
    153      this.certs = new CertificateSet({
    154        schema: new asn1js.Set({
    155          value: asn1.result.certs.valueBlock.value
    156        })
    157      });
    158    }
    159    if (CRLS in asn1.result) {
    160      this.crls = new RevocationInfoChoices({
    161        schema: new asn1js.Set({
    162          value: asn1.result.crls.valueBlock.value
    163        })
    164      });
    165    }
    166  }
    167 
    168  public toSchema(): asn1js.Sequence {
    169    const sequenceValue = [];
    170 
    171    if (this.certs) {
    172      sequenceValue.push(new asn1js.Constructed({
    173        idBlock: {
    174          tagClass: 3, // CONTEXT-SPECIFIC
    175          tagNumber: 0 // [0]
    176        },
    177        value: this.certs.toSchema().valueBlock.value
    178      }));
    179    }
    180 
    181    if (this.crls) {
    182      sequenceValue.push(new asn1js.Constructed({
    183        idBlock: {
    184          tagClass: 3, // CONTEXT-SPECIFIC
    185          tagNumber: 1 // [1]
    186        },
    187        value: this.crls.toSchema().valueBlock.value
    188      }));
    189    }
    190 
    191    //#region Construct and return new ASN.1 schema for this object
    192    return (new asn1js.Sequence({
    193      value: sequenceValue
    194    }));
    195    //#endregion
    196  }
    197 
    198  public toJSON(): OriginatorInfoJson {
    199    const res: OriginatorInfoJson = {};
    200 
    201    if (this.certs) {
    202      res.certs = this.certs.toJSON();
    203    }
    204 
    205    if (this.crls) {
    206      res.crls = this.crls.toJSON();
    207    }
    208 
    209    return res;
    210  }
    211 
    212 }