tor-browser

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

AccessDescription.ts (4836B)


      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 { GeneralName, GeneralNameJson, GeneralNameSchema } from "./GeneralName";
      6 import { PkiObject, PkiObjectParameters } from "./PkiObject";
      7 import * as Schema from "./Schema";
      8 
      9 const ACCESS_METHOD = "accessMethod";
     10 const ACCESS_LOCATION = "accessLocation";
     11 const CLEAR_PROPS = [
     12  ACCESS_METHOD,
     13  ACCESS_LOCATION,
     14 ];
     15 
     16 export interface IAccessDescription {
     17  /**
     18   * The type and format of the information are specified by the accessMethod field. This profile defines two accessMethod OIDs: id-ad-caIssuers and id-ad-ocsp
     19   */
     20  accessMethod: string;
     21  /**
     22   * The accessLocation field specifies the location of the information
     23   */
     24  accessLocation: GeneralName;
     25 }
     26 
     27 export type AccessDescriptionParameters = PkiObjectParameters & Partial<IAccessDescription>;
     28 
     29 /**
     30 * JSON representation of {@link AccessDescription}
     31 */
     32 export interface AccessDescriptionJson {
     33  accessMethod: string;
     34  accessLocation: GeneralNameJson;
     35 }
     36 
     37 /**
     38 * Represents the AccessDescription structure described in [RFC5280](https://datatracker.ietf.org/doc/html/rfc5280)
     39 *
     40 * The authority information access extension indicates how to access
     41 * information and services for the issuer of the certificate in which
     42 * the extension appears. Information and services may include on-line
     43 * validation services and CA policy data. This extension may be included in
     44 * end entity or CA certificates. Conforming CAs MUST mark this
     45 * extension as non-critical.
     46 */
     47 export class AccessDescription extends PkiObject implements IAccessDescription {
     48 
     49  public static override CLASS_NAME = "AccessDescription";
     50 
     51  public accessMethod!: string;
     52  public accessLocation!: GeneralName;
     53 
     54  /**
     55   * Initializes a new instance of the {@link AccessDescription} class
     56   * @param parameters Initialization parameters
     57   */
     58  constructor(parameters: AccessDescriptionParameters = {}) {
     59    super();
     60 
     61    this.accessMethod = pvutils.getParametersValue(parameters, ACCESS_METHOD, AccessDescription.defaultValues(ACCESS_METHOD));
     62    this.accessLocation = pvutils.getParametersValue(parameters, ACCESS_LOCATION, AccessDescription.defaultValues(ACCESS_LOCATION));
     63 
     64    if (parameters.schema) {
     65      this.fromSchema(parameters.schema);
     66    }
     67  }
     68 
     69  /**
     70   * Returns default values for all class members
     71   * @param memberName String name for a class member
     72   * @returns Default value
     73   */
     74  public static override defaultValues(memberName: typeof ACCESS_METHOD): string;
     75  public static override defaultValues(memberName: typeof ACCESS_LOCATION): GeneralName;
     76  public static override defaultValues(memberName: string): any {
     77    switch (memberName) {
     78      case ACCESS_METHOD:
     79        return EMPTY_STRING;
     80      case ACCESS_LOCATION:
     81        return new GeneralName();
     82      default:
     83        return super.defaultValues(memberName);
     84    }
     85  }
     86 
     87  /**
     88   * @inheritdoc
     89   * @asn ASN.1 schema
     90   * ```asn
     91   * AccessDescription ::= SEQUENCE {
     92   *    accessMethod          OBJECT IDENTIFIER,
     93   *    accessLocation        GeneralName  }
     94   *```
     95   */
     96  static override schema(parameters: Schema.SchemaParameters<{ accessMethod?: string; accessLocation?: GeneralNameSchema; }> = {}) {
     97    const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {});
     98 
     99    return (new asn1js.Sequence({
    100      name: (names.blockName || EMPTY_STRING),
    101      value: [
    102        new asn1js.ObjectIdentifier({ name: (names.accessMethod || EMPTY_STRING) }),
    103        GeneralName.schema(names.accessLocation || {})
    104      ]
    105    }));
    106  }
    107 
    108  public 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      AccessDescription.schema({
    116        names: {
    117          accessMethod: ACCESS_METHOD,
    118          accessLocation: {
    119            names: {
    120              blockName: ACCESS_LOCATION
    121            }
    122          }
    123        }
    124      })
    125    );
    126    AsnError.assertSchema(asn1, this.className);
    127 
    128    // Get internal properties from parsed schema
    129    this.accessMethod = asn1.result.accessMethod.valueBlock.toString();
    130    this.accessLocation = new GeneralName({ schema: asn1.result.accessLocation });
    131  }
    132 
    133  public toSchema(): asn1js.Sequence {
    134    //#region Construct and return new ASN.1 schema for this object
    135    return (new asn1js.Sequence({
    136      value: [
    137        new asn1js.ObjectIdentifier({ value: this.accessMethod }),
    138        this.accessLocation.toSchema()
    139      ]
    140    }));
    141    //#endregion
    142  }
    143 
    144  public toJSON(): AccessDescriptionJson {
    145    return {
    146      accessMethod: this.accessMethod,
    147      accessLocation: this.accessLocation.toJSON()
    148    };
    149  }
    150 
    151 }