summaryrefslogtreecommitdiffstats
path: root/third_party/js/PKI.js/src/MacData.ts
blob: ec9ccc747db11f509f9b1dbf7c30a4f17923756e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import * as asn1js from "asn1js";
import * as pvutils from "pvutils";
import { EMPTY_STRING } from "./constants";
import { DigestInfo, DigestInfoJson, DigestInfoSchema } from "./DigestInfo";
import { AsnError } from "./errors";
import { PkiObject, PkiObjectParameters } from "./PkiObject";
import * as Schema from "./Schema";

const MAC = "mac";
const MAC_SALT = "macSalt";
const ITERATIONS = "iterations";
const CLEAR_PROPS = [
  MAC,
  MAC_SALT,
  ITERATIONS
];

export interface IMacData {
  mac: DigestInfo;
  macSalt: asn1js.OctetString;
  iterations?: number;
}

export interface MacDataJson {
  mac: DigestInfoJson;
  macSalt: asn1js.OctetStringJson;
  iterations?: number;
}

export type MacDataParameters = PkiObjectParameters & Partial<IMacData>;

export type MacDataSchema = Schema.SchemaParameters<{
  mac?: DigestInfoSchema;
  macSalt?: string;
  iterations?: string;
}>;

/**
 * Represents the MacData structure described in [RFC7292](https://datatracker.ietf.org/doc/html/rfc7292)
 */
export class MacData extends PkiObject implements IMacData {

  public static override CLASS_NAME = "MacData";

  public mac!: DigestInfo;
  public macSalt!: asn1js.OctetString;
  public iterations?: number;

  /**
   * Initializes a new instance of the {@link MacData} class
   * @param parameters Initialization parameters
   */
  constructor(parameters: MacDataParameters = {}) {
    super();

    this.mac = pvutils.getParametersValue(parameters, MAC, MacData.defaultValues(MAC));
    this.macSalt = pvutils.getParametersValue(parameters, MAC_SALT, MacData.defaultValues(MAC_SALT));
    if (ITERATIONS in parameters) {
      this.iterations = pvutils.getParametersValue(parameters, ITERATIONS, MacData.defaultValues(ITERATIONS));
    }

    if (parameters.schema) {
      this.fromSchema(parameters.schema);
    }
  }

  /**
   * Returns default values for all class members
   * @param memberName String name for a class member
   * @returns Default value
   */
  public static override defaultValues(memberName: typeof MAC): DigestInfo;
  public static override defaultValues(memberName: typeof MAC_SALT): asn1js.OctetString;
  public static override defaultValues(memberName: typeof ITERATIONS): number;
  public static override defaultValues(memberName: string): any {
    switch (memberName) {
      case MAC:
        return new DigestInfo();
      case MAC_SALT:
        return new asn1js.OctetString();
      case ITERATIONS:
        return 1;
      default:
        return super.defaultValues(memberName);
    }
  }

  /**
   * Compare values with default values for all class members
   * @param memberName String name for a class member
   * @param memberValue Value to compare with default value
   */
  public static compareWithDefault(memberName: string, memberValue: any): boolean {
    switch (memberName) {
      case MAC:
        return ((DigestInfo.compareWithDefault("digestAlgorithm", memberValue.digestAlgorithm)) &&
          (DigestInfo.compareWithDefault("digest", memberValue.digest)));
      case MAC_SALT:
        return (memberValue.isEqual(MacData.defaultValues(memberName)));
      case ITERATIONS:
        return (memberValue === MacData.defaultValues(memberName));
      default:
        return super.defaultValues(memberName);
    }
  }

  /**
   * @inheritdoc
   * @asn ASN.1 schema
   * ```asn
   * MacData ::= SEQUENCE {
   *    mac           DigestInfo,
   *    macSalt       OCTET STRING,
   *    iterations    INTEGER DEFAULT 1
   *    -- Note: The default is for historical reasons and its use is
   *    -- deprecated. A higher value, like 1024 is recommended.
   *    }
   *```
   */
  public static override schema(parameters: MacDataSchema = {}): Schema.SchemaType {
    const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {});

    return (new asn1js.Sequence({
      name: (names.blockName || EMPTY_STRING),
      optional: (names.optional || true),
      value: [
        DigestInfo.schema(names.mac || {
          names: {
            blockName: MAC
          }
        }),
        new asn1js.OctetString({ name: (names.macSalt || MAC_SALT) }),
        new asn1js.Integer({
          optional: true,
          name: (names.iterations || ITERATIONS)
        })
      ]
    }));
  }

  public fromSchema(schema: Schema.SchemaType): void {
    // Clear input data first
    pvutils.clearProps(schema, CLEAR_PROPS);

    // Check the schema is valid
    const asn1 = asn1js.compareSchema(schema,
      schema,
      MacData.schema({
        names: {
          mac: {
            names: {
              blockName: MAC
            }
          },
          macSalt: MAC_SALT,
          iterations: ITERATIONS
        }
      })
    );
    AsnError.assertSchema(asn1, this.className);

    // Get internal properties from parsed schema
    this.mac = new DigestInfo({ schema: asn1.result.mac });
    this.macSalt = asn1.result.macSalt;
    if (ITERATIONS in asn1.result)
      this.iterations = asn1.result.iterations.valueBlock.valueDec;
  }

  public toSchema(): asn1js.Sequence {
    //#region Construct and return new ASN.1 schema for this object
    const outputArray: any[] = [
      this.mac.toSchema(),
      this.macSalt
    ];

    if (this.iterations !== undefined) {
      outputArray.push(new asn1js.Integer({ value: this.iterations }));
    }

    return (new asn1js.Sequence({
      value: outputArray
    }));
    //#endregion
  }

  public toJSON(): MacDataJson {
    const res: MacDataJson = {
      mac: this.mac.toJSON(),
      macSalt: this.macSalt.toJSON(),
    };

    if (this.iterations !== undefined) {
      res.iterations = this.iterations;
    }

    return res;
  }

}