tor-browser

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

IssuerAndSerialNumber.ts (4524B)


      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 { RelativeDistinguishedNames, RelativeDistinguishedNamesJson, RelativeDistinguishedNamesSchema } from "./RelativeDistinguishedNames";
      7 import * as Schema from "./Schema";
      8 
      9 const ISSUER = "issuer";
     10 const SERIAL_NUMBER = "serialNumber";
     11 const CLEAR_PROPS = [
     12  ISSUER,
     13  SERIAL_NUMBER,
     14 ];
     15 
     16 export interface IIssuerAndSerialNumber {
     17  /**
     18   * Certificate issuer name
     19   */
     20  issuer: RelativeDistinguishedNames;
     21  /**
     22   * Certificate serial number
     23   */
     24  serialNumber: asn1js.Integer;
     25 }
     26 
     27 export interface IssuerAndSerialNumberJson {
     28  issuer: RelativeDistinguishedNamesJson;
     29  serialNumber: asn1js.IntegerJson;
     30 }
     31 
     32 export type IssuerAndSerialNumberParameters = PkiObjectParameters & Partial<IIssuerAndSerialNumber>;
     33 
     34 export type IssuerAndSerialNumberSchema = Schema.SchemaParameters<{
     35  issuer?: RelativeDistinguishedNamesSchema;
     36  serialNumber?: string;
     37 }>;
     38 
     39 /**
     40 * Represents the IssuerAndSerialNumber structure described in [RFC5652](https://datatracker.ietf.org/doc/html/rfc5652)
     41 */
     42 export class IssuerAndSerialNumber extends PkiObject implements IIssuerAndSerialNumber {
     43 
     44  public static override CLASS_NAME = "IssuerAndSerialNumber";
     45 
     46  public issuer!: RelativeDistinguishedNames;
     47  public serialNumber!: asn1js.Integer;
     48 
     49  /**
     50   * Initializes a new instance of the {@link IssuerAndSerialNumber} class
     51   * @param parameters Initialization parameters
     52   */
     53  constructor(parameters: IssuerAndSerialNumberParameters = {}) {
     54    super();
     55 
     56    this.issuer = pvutils.getParametersValue(parameters, ISSUER, IssuerAndSerialNumber.defaultValues(ISSUER));
     57    this.serialNumber = pvutils.getParametersValue(parameters, SERIAL_NUMBER, IssuerAndSerialNumber.defaultValues(SERIAL_NUMBER));
     58 
     59    if (parameters.schema) {
     60      this.fromSchema(parameters.schema);
     61    }
     62  }
     63 
     64  /**
     65   * Returns default values for all class members
     66   * @param memberName String name for a class member
     67   * @returns Default value
     68   */
     69  public static override defaultValues(memberName: typeof ISSUER): RelativeDistinguishedNames;
     70  public static override defaultValues(memberName: typeof SERIAL_NUMBER): asn1js.Integer;
     71  public static override defaultValues(memberName: string): any {
     72    switch (memberName) {
     73      case ISSUER:
     74        return new RelativeDistinguishedNames();
     75      case SERIAL_NUMBER:
     76        return new asn1js.Integer();
     77      default:
     78        return super.defaultValues(memberName);
     79    }
     80  }
     81 
     82  /**
     83   * @inheritdoc
     84   * @asn ASN.1 schema
     85   * ```asn
     86   * IssuerAndSerialNumber ::= SEQUENCE {
     87   *    issuer Name,
     88   *    serialNumber CertificateSerialNumber }
     89   *
     90   * CertificateSerialNumber ::= INTEGER
     91   *```
     92   */
     93  public static override schema(parameters: IssuerAndSerialNumberSchema = {}): Schema.SchemaType {
     94    /**
     95     * @type {Object}
     96     * @property {string} [blockName]
     97     * @property {string} [issuer]
     98     * @property {string} [serialNumber]
     99     */
    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        RelativeDistinguishedNames.schema(names.issuer || {}),
    106        new asn1js.Integer({ name: (names.serialNumber || EMPTY_STRING) })
    107      ]
    108    }));
    109  }
    110 
    111  public fromSchema(schema: Schema.SchemaType): void {
    112    // Clear input data first
    113    pvutils.clearProps(schema, CLEAR_PROPS);
    114 
    115    // Check the schema is valid
    116    const asn1 = asn1js.compareSchema(schema,
    117      schema,
    118      IssuerAndSerialNumber.schema({
    119        names: {
    120          issuer: {
    121            names: {
    122              blockName: ISSUER
    123            }
    124          },
    125          serialNumber: SERIAL_NUMBER
    126        }
    127      })
    128    );
    129    AsnError.assertSchema(asn1, this.className);
    130 
    131    // Get internal properties from parsed schema
    132    this.issuer = new RelativeDistinguishedNames({ schema: asn1.result.issuer });
    133    this.serialNumber = asn1.result.serialNumber;
    134  }
    135 
    136  public toSchema(): asn1js.Sequence {
    137    // Construct and return new ASN.1 schema for this object
    138    return (new asn1js.Sequence({
    139      value: [
    140        this.issuer.toSchema(),
    141        this.serialNumber
    142      ]
    143    }));
    144  }
    145 
    146  public toJSON(): IssuerAndSerialNumberJson {
    147    return {
    148      issuer: this.issuer.toJSON(),
    149      serialNumber: this.serialNumber.toJSON(),
    150    };
    151  }
    152 
    153 }