tor-browser

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

OtherRecipientInfo.ts (4320B)


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