tor-browser

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

ResponseBytes.ts (4299B)


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