tor-browser

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

SafeContents.ts (3566B)


      1 import * as asn1js from "asn1js";
      2 import * as pvutils from "pvutils";
      3 import * as Schema from "./Schema";
      4 import { PkiObject, PkiObjectParameters } from "./PkiObject";
      5 import { AsnError } from "./errors";
      6 
      7 const SAFE_BUGS = "safeBags";
      8 
      9 export interface ISafeContents {
     10  safeBags: SafeBag[];
     11 }
     12 
     13 export type SafeContentsParameters = PkiObjectParameters & Partial<ISafeContents>;
     14 
     15 export interface SafeContentsJson {
     16  safeBags: SafeBagJson[];
     17 }
     18 
     19 /**
     20 * Represents the SafeContents structure described in [RFC7292](https://datatracker.ietf.org/doc/html/rfc7292)
     21 */
     22 export class SafeContents extends PkiObject implements ISafeContents {
     23 
     24  public static override CLASS_NAME = "SafeContents";
     25 
     26  public safeBags!: SafeBag[];
     27 
     28  /**
     29   * Initializes a new instance of the {@link SafeContents} class
     30   * @param parameters Initialization parameters
     31   */
     32  constructor(parameters: SafeContentsParameters = {}) {
     33    super();
     34 
     35    this.safeBags = pvutils.getParametersValue(parameters, SAFE_BUGS, SafeContents.defaultValues(SAFE_BUGS));
     36 
     37    if (parameters.schema) {
     38      this.fromSchema(parameters.schema);
     39    }
     40  }
     41 
     42  /**
     43   * Returns default values for all class members
     44   * @param memberName String name for a class member
     45   * @returns Default value
     46   */
     47  public static override defaultValues(memberName: typeof SAFE_BUGS): SafeBag[];
     48  public static override defaultValues(memberName: string): any {
     49    switch (memberName) {
     50      case SAFE_BUGS:
     51        return [];
     52      default:
     53        return super.defaultValues(memberName);
     54    }
     55  }
     56 
     57  /**
     58   * Compare values with default values for all class members
     59   * @param memberName String name for a class member
     60   * @param memberValue Value to compare with default value
     61   */
     62  public static compareWithDefault(memberName: string, memberValue: any): boolean {
     63    switch (memberName) {
     64      case SAFE_BUGS:
     65        return (memberValue.length === 0);
     66      default:
     67        return super.defaultValues(memberName);
     68    }
     69  }
     70 
     71  /**
     72   * @inheritdoc
     73   * @asn ASN.1 schema
     74   * ```asn
     75   * SafeContents ::= SEQUENCE OF SafeBag
     76   *```
     77   */
     78  public static override schema(parameters: Schema.SchemaParameters<{
     79    safeBags?: string;
     80  }> = {}): Schema.SchemaType {
     81    const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {});
     82 
     83    return (new asn1js.Sequence({
     84      name: (names.blockName || EMPTY_STRING),
     85      value: [
     86        new asn1js.Repeated({
     87          name: (names.safeBags || EMPTY_STRING),
     88          value: SafeBag.schema()
     89        })
     90      ]
     91    }));
     92  }
     93 
     94  public fromSchema(schema: Schema.SchemaType): void {
     95    // Clear input data first
     96    pvutils.clearProps(schema, [
     97      SAFE_BUGS
     98    ]);
     99 
    100    // Check the schema is valid
    101    const asn1 = asn1js.compareSchema(schema,
    102      schema,
    103      SafeContents.schema({
    104        names: {
    105          safeBags: SAFE_BUGS
    106        }
    107      })
    108    );
    109    AsnError.assertSchema(asn1, this.className);
    110 
    111    // Get internal properties from parsed schema
    112    this.safeBags = Array.from(asn1.result.safeBags, element => new SafeBag({ schema: element }));
    113  }
    114 
    115  public toSchema(): asn1js.Sequence {
    116    //#region Construct and return new ASN.1 schema for this object
    117    return (new asn1js.Sequence({
    118      value: Array.from(this.safeBags, o => o.toSchema())
    119    }));
    120    //#endregion
    121  }
    122 
    123  public toJSON(): SafeContentsJson {
    124    return {
    125      safeBags: Array.from(this.safeBags, o => o.toJSON())
    126    };
    127  }
    128 
    129 }
    130 
    131 import { SafeBag, SafeBagJson } from "./SafeBag";import { EMPTY_STRING } from "./constants";