tor-browser

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

KEKIdentifier.ts (6100B)


      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 { OtherKeyAttribute, OtherKeyAttributeJson, OtherKeyAttributeSchema } from "./OtherKeyAttribute";
      6 import { PkiObject, PkiObjectParameters } from "./PkiObject";
      7 import * as Schema from "./Schema";
      8 
      9 const KEY_IDENTIFIER = "keyIdentifier";
     10 const DATE = "date";
     11 const OTHER = "other";
     12 const CLEAR_PROPS = [
     13  KEY_IDENTIFIER,
     14  DATE,
     15  OTHER,
     16 ];
     17 
     18 export interface IKEKIdentifier {
     19  keyIdentifier: asn1js.OctetString;
     20  date?: asn1js.GeneralizedTime;
     21  other?: OtherKeyAttribute;
     22 }
     23 
     24 export interface KEKIdentifierJson {
     25  keyIdentifier: asn1js.OctetStringJson;
     26  date?: asn1js.GeneralizedTime;
     27  other?: OtherKeyAttributeJson;
     28 }
     29 
     30 export type KEKIdentifierParameters = PkiObjectParameters & Partial<IKEKIdentifier>;
     31 
     32 export type KEKIdentifierSchema = Schema.SchemaParameters<{
     33  keyIdentifier?: string;
     34  date?: string;
     35  other?: OtherKeyAttributeSchema;
     36 }>;
     37 
     38 /**
     39 * Represents the KEKIdentifier structure described in [RFC5652](https://datatracker.ietf.org/doc/html/rfc5652)
     40 */
     41 export class KEKIdentifier extends PkiObject implements IKEKIdentifier {
     42 
     43  public static override CLASS_NAME = "KEKIdentifier";
     44 
     45  public keyIdentifier!: asn1js.OctetString;
     46  public date?: asn1js.GeneralizedTime;
     47  public other?: OtherKeyAttribute;
     48 
     49  /**
     50   * Initializes a new instance of the {@link KEKIdentifier} class
     51   * @param parameters Initialization parameters
     52   */
     53  constructor(parameters: KEKIdentifierParameters = {}) {
     54    super();
     55 
     56    this.keyIdentifier = pvutils.getParametersValue(parameters, KEY_IDENTIFIER, KEKIdentifier.defaultValues(KEY_IDENTIFIER));
     57    if (DATE in parameters) {
     58      this.date = pvutils.getParametersValue(parameters, DATE, KEKIdentifier.defaultValues(DATE));
     59    }
     60    if (OTHER in parameters) {
     61      this.other = pvutils.getParametersValue(parameters, OTHER, KEKIdentifier.defaultValues(OTHER));
     62    }
     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 KEY_IDENTIFIER): asn1js.OctetString;
     75  public static override defaultValues(memberName: typeof DATE): asn1js.GeneralizedTime;
     76  public static override defaultValues(memberName: typeof OTHER): OtherKeyAttribute;
     77  public static override defaultValues(memberName: string): any {
     78    switch (memberName) {
     79      case KEY_IDENTIFIER:
     80        return new asn1js.OctetString();
     81      case DATE:
     82        return new asn1js.GeneralizedTime();
     83      case OTHER:
     84        return new OtherKeyAttribute();
     85      default:
     86        return super.defaultValues(memberName);
     87    }
     88  }
     89 
     90  /**
     91   * Compare values with default values for all class members
     92   * @param memberName String name for a class member
     93   * @param memberValue Value to compare with default value
     94   */
     95  public static compareWithDefault(memberName: string, memberValue: any): boolean {
     96    switch (memberName) {
     97      case KEY_IDENTIFIER:
     98        return (memberValue.isEqual(KEKIdentifier.defaultValues(KEY_IDENTIFIER)));
     99      case DATE:
    100        return ((memberValue.year === 0) &&
    101          (memberValue.month === 0) &&
    102          (memberValue.day === 0) &&
    103          (memberValue.hour === 0) &&
    104          (memberValue.minute === 0) &&
    105          (memberValue.second === 0) &&
    106          (memberValue.millisecond === 0));
    107      case OTHER:
    108        return ((memberValue.compareWithDefault("keyAttrId", memberValue.keyAttrId)) &&
    109          (("keyAttr" in memberValue) === false));
    110      default:
    111        return super.defaultValues(memberName);
    112    }
    113  }
    114 
    115  /**
    116   * @inheritdoc
    117   * @asn ASN.1 schema
    118   * ```asn
    119   * KEKIdentifier ::= SEQUENCE {
    120   *    keyIdentifier OCTET STRING,
    121   *    date GeneralizedTime OPTIONAL,
    122   *    other OtherKeyAttribute OPTIONAL }
    123   *```
    124   */
    125  public static override schema(parameters: KEKIdentifierSchema = {}): Schema.SchemaType {
    126    const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {});
    127 
    128    return (new asn1js.Sequence({
    129      name: (names.blockName || EMPTY_STRING),
    130      value: [
    131        new asn1js.OctetString({ name: (names.keyIdentifier || EMPTY_STRING) }),
    132        new asn1js.GeneralizedTime({
    133          optional: true,
    134          name: (names.date || EMPTY_STRING)
    135        }),
    136        OtherKeyAttribute.schema(names.other || {})
    137      ]
    138    }));
    139  }
    140 
    141  public fromSchema(schema: Schema.SchemaType): void {
    142    // Clear input data first
    143    pvutils.clearProps(schema, CLEAR_PROPS);
    144 
    145    // Check the schema is valid
    146    const asn1 = asn1js.compareSchema(schema,
    147      schema,
    148      KEKIdentifier.schema({
    149        names: {
    150          keyIdentifier: KEY_IDENTIFIER,
    151          date: DATE,
    152          other: {
    153            names: {
    154              blockName: OTHER
    155            }
    156          }
    157        }
    158      })
    159    );
    160    AsnError.assertSchema(asn1, this.className);
    161 
    162    // Get internal properties from parsed schema
    163    this.keyIdentifier = asn1.result.keyIdentifier;
    164    if (DATE in asn1.result)
    165      this.date = asn1.result.date;
    166    if (OTHER in asn1.result)
    167      this.other = new OtherKeyAttribute({ schema: asn1.result.other });
    168  }
    169 
    170  public toSchema(): asn1js.Sequence {
    171    //#region Create array for output sequence
    172    const outputArray = [];
    173 
    174    outputArray.push(this.keyIdentifier);
    175 
    176    if (this.date) {
    177      outputArray.push(this.date);
    178    }
    179    if (this.other) {
    180      outputArray.push(this.other.toSchema());
    181    }
    182    //#endregion
    183 
    184    //#region Construct and return new ASN.1 schema for this object
    185    return (new asn1js.Sequence({
    186      value: outputArray
    187    }));
    188    //#endregion
    189  }
    190 
    191  public toJSON(): KEKIdentifierJson {
    192    const res: KEKIdentifierJson = {
    193      keyIdentifier: this.keyIdentifier.toJSON()
    194    };
    195 
    196    if (this.date) {
    197      res.date = this.date;
    198    }
    199 
    200    if (this.other) {
    201      res.other = this.other.toJSON();
    202    }
    203 
    204    return res;
    205  }
    206 
    207 }