summaryrefslogtreecommitdiffstats
path: root/third_party/js/PKI.js/src/AuthenticatedSafe.ts
blob: ea85afbd38d82a401b21f97520a95278bb189b0e (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import * as asn1js from "asn1js";
import * as pvutils from "pvutils";
import { ContentInfo, ContentInfoJson } from "./ContentInfo";
import { SafeContents } from "./SafeContents";
import { EnvelopedData } from "./EnvelopedData";
import { EncryptedData } from "./EncryptedData";
import * as Schema from "./Schema";
import { id_ContentType_Data, id_ContentType_EncryptedData, id_ContentType_EnvelopedData } from "./ObjectIdentifiers";
import { ArgumentError, AsnError, ParameterError } from "./errors";
import { PkiObject, PkiObjectParameters } from "./PkiObject";
import { EMPTY_STRING } from "./constants";
import * as common from "./common";

const SAFE_CONTENTS = "safeContents";
const PARSED_VALUE = "parsedValue";
const CONTENT_INFOS = "contentInfos";

export interface IAuthenticatedSafe {
  safeContents: ContentInfo[];
  parsedValue: any;
}

export type AuthenticatedSafeParameters = PkiObjectParameters & Partial<IAuthenticatedSafe>;

export interface AuthenticatedSafeJson {
  safeContents: ContentInfoJson[];
}

export type SafeContent = ContentInfo | EncryptedData | EnvelopedData | object;

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

  public static override CLASS_NAME = "AuthenticatedSafe";

  public safeContents!: ContentInfo[];
  public parsedValue: any;

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

    this.safeContents = pvutils.getParametersValue(parameters, SAFE_CONTENTS, AuthenticatedSafe.defaultValues(SAFE_CONTENTS));
    if (PARSED_VALUE in parameters) {
      this.parsedValue = pvutils.getParametersValue(parameters, PARSED_VALUE, AuthenticatedSafe.defaultValues(PARSED_VALUE));
    }

    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 SAFE_CONTENTS): ContentInfo[];
  public static override defaultValues(memberName: typeof PARSED_VALUE): any;
  public static override defaultValues(memberName: string): any {
    switch (memberName) {
      case SAFE_CONTENTS:
        return [];
      case PARSED_VALUE:
        return {};
      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 SAFE_CONTENTS:
        return (memberValue.length === 0);
      case PARSED_VALUE:
        return ((memberValue instanceof Object) && (Object.keys(memberValue).length === 0));
      default:
        return super.defaultValues(memberName);
    }
  }

  /**
   * @inheritdoc
   * @asn ASN.1 schema
   * ```asn
   * AuthenticatedSafe ::= SEQUENCE OF ContentInfo
   * -- Data if unencrypted
   * -- EncryptedData if password-encrypted
   * -- EnvelopedData if public key-encrypted
   *```
   */
  public static override schema(parameters: Schema.SchemaParameters<{
    contentInfos?: string;
  }> = {}): Schema.SchemaType {
    const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {});

    return (new asn1js.Sequence({
      name: (names.blockName || EMPTY_STRING),
      value: [
        new asn1js.Repeated({
          name: (names.contentInfos || EMPTY_STRING),
          value: ContentInfo.schema()
        })
      ]
    }));
  }

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

    // Check the schema is valid
    const asn1 = asn1js.compareSchema(schema,
      schema,
      AuthenticatedSafe.schema({
        names: {
          contentInfos: CONTENT_INFOS
        }
      })
    );
    AsnError.assertSchema(asn1, this.className);

    // Get internal properties from parsed schema
    this.safeContents = Array.from(asn1.result.contentInfos, element => new ContentInfo({ schema: element }));
  }

  public toSchema(): asn1js.Sequence {
    return (new asn1js.Sequence({
      value: Array.from(this.safeContents, o => o.toSchema())
    }));
  }

  public toJSON(): AuthenticatedSafeJson {
    return {
      safeContents: Array.from(this.safeContents, o => o.toJSON())
    };
  }

  public async parseInternalValues(parameters: { safeContents: SafeContent[]; }, crypto = common.getCrypto(true)): Promise<void> {
    //#region Check input data from "parameters"
    ParameterError.assert(parameters, SAFE_CONTENTS);
    ArgumentError.assert(parameters.safeContents, SAFE_CONTENTS, "Array");
    if (parameters.safeContents.length !== this.safeContents.length) {
      throw new ArgumentError("Length of \"parameters.safeContents\" must be equal to \"this.safeContents.length\"");
    }
    //#endregion

    //#region Create value for "this.parsedValue.authenticatedSafe"
    this.parsedValue = {
      safeContents: [] as any[],
    };

    for (const [index, content] of this.safeContents.entries()) {
      const safeContent = parameters.safeContents[index];
      const errorTarget = `parameters.safeContents[${index}]`;
      switch (content.contentType) {
        //#region data
        case id_ContentType_Data:
          {
            // Check that we do have OCTET STRING as "content"
            ArgumentError.assert(content.content, "this.safeContents[j].content", asn1js.OctetString);

            //#region Check we have "constructive encoding" for AuthSafe content
            const authSafeContent = content.content.getValue();
            //#endregion

            //#region Finally initialize initial values of SAFE_CONTENTS type
            this.parsedValue.safeContents.push({
              privacyMode: 0, // No privacy, clear data
              value: SafeContents.fromBER(authSafeContent)
            });
            //#endregion
          }
          break;
        //#endregion
        //#region envelopedData
        case id_ContentType_EnvelopedData:
          {
            //#region Initial variables
            const cmsEnveloped = new EnvelopedData({ schema: content.content });
            //#endregion

            //#region Check mandatory parameters
            ParameterError.assert(errorTarget, safeContent, "recipientCertificate", "recipientKey");
            const envelopedData = safeContent as any;
            const recipientCertificate = envelopedData.recipientCertificate;
            const recipientKey = envelopedData.recipientKey;
            //#endregion

            //#region Decrypt CMS EnvelopedData using first recipient information
            const decrypted = await cmsEnveloped.decrypt(0, {
              recipientCertificate,
              recipientPrivateKey: recipientKey
            }, crypto);

            this.parsedValue.safeContents.push({
              privacyMode: 2, // Public-key privacy mode
              value: SafeContents.fromBER(decrypted),
            });
            //#endregion
          }
          break;
        //#endregion
        //#region encryptedData
        case id_ContentType_EncryptedData:
          {
            //#region Initial variables
            const cmsEncrypted = new EncryptedData({ schema: content.content });
            //#endregion

            //#region Check mandatory parameters
            ParameterError.assert(errorTarget, safeContent, "password");

            const password = (safeContent as any).password;
            //#endregion

            //#region Decrypt CMS EncryptedData using password
            const decrypted = await cmsEncrypted.decrypt({
              password
            }, crypto);
            //#endregion

            //#region Initialize internal data
            this.parsedValue.safeContents.push({
              privacyMode: 1, // Password-based privacy mode
              value: SafeContents.fromBER(decrypted),
            });
            //#endregion
          }
          break;
        //#endregion
        //#region default
        default:
          throw new Error(`Unknown "contentType" for AuthenticatedSafe: " ${content.contentType}`);
        //#endregion
      }
    }
    //#endregion
  }
  public async makeInternalValues(parameters: {
    safeContents: any[];
  }, crypto = common.getCrypto(true)): Promise<this> {
    //#region Check data in PARSED_VALUE
    if (!(this.parsedValue)) {
      throw new Error("Please run \"parseValues\" first or add \"parsedValue\" manually");
    }
    ArgumentError.assert(this.parsedValue, "this.parsedValue", "object");
    ArgumentError.assert(this.parsedValue.safeContents, "this.parsedValue.safeContents", "Array");

    //#region Check input data from "parameters"
    ArgumentError.assert(parameters, "parameters", "object");
    ParameterError.assert(parameters, "safeContents");
    ArgumentError.assert(parameters.safeContents, "parameters.safeContents", "Array");
    if (parameters.safeContents.length !== this.parsedValue.safeContents.length) {
      throw new ArgumentError("Length of \"parameters.safeContents\" must be equal to \"this.parsedValue.safeContents\"");
    }
    //#endregion

    //#region Create internal values from already parsed values
    this.safeContents = [];

    for (const [index, content] of this.parsedValue.safeContents.entries()) {
      //#region Check current "content" value
      ParameterError.assert("content", content, "privacyMode", "value");
      ArgumentError.assert(content.value, "content.value", SafeContents);
      //#endregion

      switch (content.privacyMode) {
        //#region No privacy
        case 0:
          {
            const contentBuffer = content.value.toSchema().toBER(false);

            this.safeContents.push(new ContentInfo({
              contentType: "1.2.840.113549.1.7.1",
              content: new asn1js.OctetString({ valueHex: contentBuffer })
            }));
          }
          break;
        //#endregion
        //#region Privacy with password
        case 1:
          {
            //#region Initial variables
            const cmsEncrypted = new EncryptedData();

            const currentParameters = parameters.safeContents[index];
            currentParameters.contentToEncrypt = content.value.toSchema().toBER(false);
            //#endregion

            //#region Encrypt CMS EncryptedData using password
            await cmsEncrypted.encrypt(currentParameters);
            //#endregion

            //#region Store result content in CMS_CONTENT_INFO type
            this.safeContents.push(new ContentInfo({
              contentType: "1.2.840.113549.1.7.6",
              content: cmsEncrypted.toSchema()
            }));
            //#endregion
          }
          break;
        //#endregion
        //#region Privacy with public key
        case 2:
          {
            //#region Initial variables
            const cmsEnveloped = new EnvelopedData();
            const contentToEncrypt = content.value.toSchema().toBER(false);
            const safeContent = parameters.safeContents[index];
            //#endregion

            //#region Check mandatory parameters
            ParameterError.assert(`parameters.safeContents[${index}]`, safeContent, "encryptingCertificate", "encryptionAlgorithm");

            switch (true) {
              case (safeContent.encryptionAlgorithm.name.toLowerCase() === "aes-cbc"):
              case (safeContent.encryptionAlgorithm.name.toLowerCase() === "aes-gcm"):
                break;
              default:
                throw new Error(`Incorrect parameter "encryptionAlgorithm" in "parameters.safeContents[i]": ${safeContent.encryptionAlgorithm}`);
            }

            switch (true) {
              case (safeContent.encryptionAlgorithm.length === 128):
              case (safeContent.encryptionAlgorithm.length === 192):
              case (safeContent.encryptionAlgorithm.length === 256):
                break;
              default:
                throw new Error(`Incorrect parameter "encryptionAlgorithm.length" in "parameters.safeContents[i]": ${safeContent.encryptionAlgorithm.length}`);
            }
            //#endregion

            //#region Making correct "encryptionAlgorithm" variable
            const encryptionAlgorithm = safeContent.encryptionAlgorithm;
            //#endregion

            //#region Append recipient for enveloped data
            cmsEnveloped.addRecipientByCertificate(safeContent.encryptingCertificate, {}, undefined, crypto);
            //#endregion

            //#region Making encryption
            await cmsEnveloped.encrypt(encryptionAlgorithm, contentToEncrypt, crypto);

            this.safeContents.push(new ContentInfo({
              contentType: "1.2.840.113549.1.7.3",
              content: cmsEnveloped.toSchema()
            }));
            //#endregion
          }
          break;
        //#endregion
        //#region default
        default:
          throw new Error(`Incorrect value for "content.privacyMode": ${content.privacyMode}`);
        //#endregion
      }
    }
    //#endregion

    //#region Return result of the function
    return this;
    //#endregion
  }

}