tor-browser

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

RevokedCertificate.ts (5542B)


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