summaryrefslogtreecommitdiffstats
path: root/third_party/js/PKI.js/src/common.ts
blob: e95772f4e755e763848c3c23d33d483d9209454a (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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import * as asn1js from "asn1js";
import * as pvutils from "pvutils";
import { AlgorithmIdentifier } from "./AlgorithmIdentifier";
import { EMPTY_BUFFER } from "./constants";
import type { CryptoEngineAlgorithmOperation, CryptoEngineAlgorithmParams, ICryptoEngine } from "./CryptoEngine/CryptoEngineInterface";
import { ArgumentError } from "./errors";

//#region Crypto engine related function
export { ICryptoEngine } from "./CryptoEngine/CryptoEngineInterface";

export interface GlobalCryptoEngine {
  name: string;
  crypto: ICryptoEngine | null;
}
export let engine: GlobalCryptoEngine = {
  name: "none",
  crypto: null,
};

function isCryptoEngine(engine: unknown): engine is ICryptoEngine {
  return engine
    && typeof engine === "object"
    && "crypto" in engine
    ? true
    : false;
}

/**
 * Sets global crypto engine
 * @param name Name of the crypto engine
 * @param crypto
 * @param subtle
 * @deprecated Since version 3.0.0
 */
export function setEngine(name: string, crypto: ICryptoEngine | Crypto, subtle: ICryptoEngine | SubtleCrypto): void;
/**
 * Sets global crypto engine
 * @param name Name of the crypto engine
 * @param crypto Crypto engine. If the parameter is omitted, `CryptoEngine` with `self.crypto` are used
 * @since 3.0.0
 */
export function setEngine(name: string, crypto?: ICryptoEngine): void;
export function setEngine(name: string, ...args: any[]): void {
  let crypto: ICryptoEngine | null = null;
  if (args.length < 2) {
    // v3.0.0 implementation
    if (args.length) {
      crypto = args[0];
    } else {
      // crypto param is omitted, use CryptoEngine(self.crypto)
      crypto = typeof self !== "undefined" && self.crypto ? new CryptoEngine({ name: "browser", crypto: self.crypto }) : null;
    }
  } else {
    // prev implementation
    const cryptoArg = args[0];
    const subtleArg = args[1];
    if (isCryptoEngine(subtleArg)) {
      crypto = subtleArg;
    } else if (isCryptoEngine(cryptoArg)) {
      crypto = cryptoArg;
    } else if ("subtle" in cryptoArg && "getRandomValues" in cryptoArg) {
      crypto = new CryptoEngine({
        crypto: cryptoArg,
      });
    }
  }

  if ((typeof process !== "undefined") && ("pid" in process) && (typeof global !== "undefined") && (typeof window === "undefined")) {
    // We are in Node
    if (typeof (global as any)[process.pid] === "undefined") {
      (global as any)[process.pid] = {};
    }
    else {
      if (typeof (global as any)[process.pid] !== "object") {
        throw new Error(`Name global.${process.pid} already exists and it is not an object`);
      }
    }

    if (typeof (global as any)[process.pid].pkijs === "undefined") {
      (global as any)[process.pid].pkijs = {};
    }
    else {
      if (typeof (global as any)[process.pid].pkijs !== "object") {
        throw new Error(`Name global.${process.pid}.pkijs already exists and it is not an object`);
      }
    }

    (global as any)[process.pid].pkijs.engine = {
      name: name,
      crypto,
    };
  } else {
    // We are in browser
    engine = {
      name: name,
      crypto,
    };
  }
}

export function getEngine(): GlobalCryptoEngine {
  //#region We are in Node
  if ((typeof process !== "undefined") && ("pid" in process) && (typeof global !== "undefined") && (typeof window === "undefined")) {
    let _engine;

    try {
      _engine = (global as any)[process.pid].pkijs.engine;
    }
    catch (ex) {
      throw new Error("Please call 'setEngine' before call to 'getEngine'");
    }

    return _engine;
  }
  //#endregion

  return engine;
}
//#endregion

//#region Declaration of common functions

/**
 * Gets crypto subtle from the current "crypto engine"
 * @param safety
 * @returns Reruns {@link ICryptoEngine} or `null`
 */
export function getCrypto(safety?: boolean): ICryptoEngine | null;
/**
 * Gets crypto subtle from the current "crypto engine"
 * @param safety
 * @returns Reruns {@link ICryptoEngine} or throws en exception
 * @throws Throws {@link Error} if `subtle` is empty
 */
export function getCrypto(safety: true): ICryptoEngine;
export function getCrypto(safety = false): ICryptoEngine | null {
  const _engine = getEngine();

  if (!_engine.crypto && safety) {
    throw new Error("Unable to create WebCrypto object");
  }

  return _engine.crypto;
}

/**
 * Initialize input Uint8Array by random values (with help from current "crypto engine")
 * @param view
 */
export function getRandomValues(view: Uint8Array) {
  return getCrypto(true).getRandomValues(view);
}

/**
 * Get OID for each specific algorithm
 * @param algorithm WebCrypto Algorithm
 * @param safety if `true` throws exception on unknown algorithm
 * @param target name of the target
 * @throws Throws {@link Error} exception if unknown WebCrypto algorithm
 */
export function getOIDByAlgorithm(algorithm: Algorithm, safety?: boolean, target?: string) {
  return getCrypto(true).getOIDByAlgorithm(algorithm, safety, target);
}

/**
 * Get default algorithm parameters for each kind of operation
 * @param algorithmName Algorithm name to get common parameters for
 * @param operation Kind of operation: "sign", "encrypt", "generateKey", "importKey", "exportKey", "verify"
 */
// TODO Add safety
export function getAlgorithmParameters(algorithmName: string, operation: CryptoEngineAlgorithmOperation): CryptoEngineAlgorithmParams {
  return getCrypto(true).getAlgorithmParameters(algorithmName, operation);
}

/**
 * Create CMS ECDSA signature from WebCrypto ECDSA signature
 * @param signatureBuffer WebCrypto result of "sign" function
 */
export function createCMSECDSASignature(signatureBuffer: ArrayBuffer): ArrayBuffer {
  //#region Initial check for correct length
  if ((signatureBuffer.byteLength % 2) !== 0)
    return EMPTY_BUFFER;
  //#endregion

  //#region Initial variables
  const length = signatureBuffer.byteLength / 2; // There are two equal parts inside incoming ArrayBuffer

  const rBuffer = new ArrayBuffer(length);
  const rView = new Uint8Array(rBuffer);
  rView.set(new Uint8Array(signatureBuffer, 0, length));

  const rInteger = new asn1js.Integer({ valueHex: rBuffer });

  const sBuffer = new ArrayBuffer(length);
  const sView = new Uint8Array(sBuffer);
  sView.set(new Uint8Array(signatureBuffer, length, length));

  const sInteger = new asn1js.Integer({ valueHex: sBuffer });
  //#endregion

  return (new asn1js.Sequence({
    value: [
      rInteger.convertToDER(),
      sInteger.convertToDER()
    ]
  })).toBER(false);
}

/**
 * Create a single ArrayBuffer from CMS ECDSA signature
 * @param cmsSignature ASN.1 SEQUENCE contains CMS ECDSA signature
 * @param pointSize Size of EC point. Use {@link ECNamedCurves.find} to get correct point size
 * @returns WebCrypto signature
 */
export function createECDSASignatureFromCMS(cmsSignature: asn1js.AsnType, pointSize: number): ArrayBuffer {
  // Check input variables
  if (!(cmsSignature instanceof asn1js.Sequence
    && cmsSignature.valueBlock.value.length === 2
    && cmsSignature.valueBlock.value[0] instanceof asn1js.Integer
    && cmsSignature.valueBlock.value[1] instanceof asn1js.Integer))
    return EMPTY_BUFFER;

  const rValueView = cmsSignature.valueBlock.value[0].convertFromDER().valueBlock.valueHexView;
  const sValueView = cmsSignature.valueBlock.value[1].convertFromDER().valueBlock.valueHexView;

  const res = new Uint8Array(pointSize * 2);

  res.set(rValueView, pointSize - rValueView.byteLength);
  res.set(sValueView, (2 * pointSize) - sValueView.byteLength);

  return res.buffer;
}

/**
 * Gets WebCrypto algorithm by well-known OID
 * @param oid algorithm identifier
 * @param safety if true throws exception on unknown algorithm identifier
 * @param target name of the target
 * @returns WebCrypto algorithm or an empty object
 */
export function getAlgorithmByOID<T extends Algorithm = Algorithm>(oid: string, safety?: boolean, target?: string): T | object;
export function getAlgorithmByOID<T extends Algorithm = Algorithm>(oid: string, safety: true, target?: string): T;
export function getAlgorithmByOID(oid: string, safety = false, target?: string): any {
  return getCrypto(true).getAlgorithmByOID(oid, safety, target);
}

/**
 * Getting hash algorithm by signature algorithm
 * @param signatureAlgorithm Signature algorithm
 */
export function getHashAlgorithm(signatureAlgorithm: AlgorithmIdentifier): string {
  return getCrypto(true).getHashAlgorithm(signatureAlgorithm);
}

/**
 * ANS X9.63 Key Derivation Function having a "Counter" as a parameter
 * @param hashFunction Used hash function
 * @param zBuffer ArrayBuffer containing ECDH shared secret to derive from
 * @param Counter
 * @param SharedInfo Usually DER encoded "ECC_CMS_SharedInfo" structure
 * @param crypto Crypto engine
 */
async function kdfWithCounter(hashFunction: string, zBuffer: ArrayBuffer, Counter: number, SharedInfo: ArrayBuffer, crypto: ICryptoEngine): Promise<{ counter: number; result: ArrayBuffer; }> {
  //#region Check of input parameters
  switch (hashFunction.toUpperCase()) {
    case "SHA-1":
    case "SHA-256":
    case "SHA-384":
    case "SHA-512":
      break;
    default:
      throw new ArgumentError(`Unknown hash function: ${hashFunction}`);
  }

  ArgumentError.assert(zBuffer, "zBuffer", "ArrayBuffer");
  if (zBuffer.byteLength === 0)
    throw new ArgumentError("'zBuffer' has zero length, error");

  ArgumentError.assert(SharedInfo, "SharedInfo", "ArrayBuffer");
  if (Counter > 255)
    throw new ArgumentError("Please set 'Counter' argument to value less or equal to 255");
  //#endregion

  //#region Initial variables
  const counterBuffer = new ArrayBuffer(4);
  const counterView = new Uint8Array(counterBuffer);
  counterView[0] = 0x00;
  counterView[1] = 0x00;
  counterView[2] = 0x00;
  counterView[3] = Counter;

  let combinedBuffer = EMPTY_BUFFER;
  //#endregion

  //#region Create a combined ArrayBuffer for digesting
  combinedBuffer = pvutils.utilConcatBuf(combinedBuffer, zBuffer);
  combinedBuffer = pvutils.utilConcatBuf(combinedBuffer, counterBuffer);
  combinedBuffer = pvutils.utilConcatBuf(combinedBuffer, SharedInfo);
  //#endregion

  //#region Return digest of combined ArrayBuffer and information about current counter
  const result = await crypto.digest(
    { name: hashFunction },
    combinedBuffer);

  return {
    counter: Counter,
    result
  };
  //#endregion
}

/**
 * ANS X9.63 Key Derivation Function
 * @param hashFunction Used hash function
 * @param Zbuffer ArrayBuffer containing ECDH shared secret to derive from
 * @param keydatalen Length (!!! in BITS !!!) of used kew derivation function
 * @param SharedInfo Usually DER encoded "ECC_CMS_SharedInfo" structure
 * @param crypto Crypto engine
 */
export async function kdf(hashFunction: string, Zbuffer: ArrayBuffer, keydatalen: number, SharedInfo: ArrayBuffer, crypto = getCrypto(true)) {
  //#region Initial variables
  let hashLength = 0;
  let maxCounter = 1;
  //#endregion

  //#region Check of input parameters
  switch (hashFunction.toUpperCase()) {
    case "SHA-1":
      hashLength = 160; // In bits
      break;
    case "SHA-256":
      hashLength = 256; // In bits
      break;
    case "SHA-384":
      hashLength = 384; // In bits
      break;
    case "SHA-512":
      hashLength = 512; // In bits
      break;
    default:
      throw new ArgumentError(`Unknown hash function: ${hashFunction}`);
  }

  ArgumentError.assert(Zbuffer, "Zbuffer", "ArrayBuffer");
  if (Zbuffer.byteLength === 0)
    throw new ArgumentError("'Zbuffer' has zero length, error");
  ArgumentError.assert(SharedInfo, "SharedInfo", "ArrayBuffer");
  //#endregion

  //#region Calculated maximum value of "Counter" variable
  const quotient = keydatalen / hashLength;

  if (Math.floor(quotient) > 0) {
    maxCounter = Math.floor(quotient);

    if ((quotient - maxCounter) > 0)
      maxCounter++;
  }
  //#endregion

  //#region Create an array of "kdfWithCounter"
  const incomingResult = [];
  for (let i = 1; i <= maxCounter; i++)
    incomingResult.push(await kdfWithCounter(hashFunction, Zbuffer, i, SharedInfo, crypto));
  //#endregion

  //#region Return combined digest with specified length
  //#region Initial variables
  let combinedBuffer = EMPTY_BUFFER;
  let currentCounter = 1;
  let found = true;
  //#endregion

  //#region Combine all buffer together
  while (found) {
    found = false;

    for (const result of incomingResult) {
      if (result.counter === currentCounter) {
        combinedBuffer = pvutils.utilConcatBuf(combinedBuffer, result.result);
        found = true;
        break;
      }
    }

    currentCounter++;
  }
  //#endregion

  //#region Create output buffer with specified length
  keydatalen >>= 3; // Divide by 8 since "keydatalen" is in bits

  if (combinedBuffer.byteLength > keydatalen) {
    const newBuffer = new ArrayBuffer(keydatalen);
    const newView = new Uint8Array(newBuffer);
    const combinedView = new Uint8Array(combinedBuffer);

    for (let i = 0; i < keydatalen; i++)
      newView[i] = combinedView[i];

    return newBuffer;
  }

  return combinedBuffer; // Since the situation when "combinedBuffer.byteLength < keydatalen" here we have only "combinedBuffer.byteLength === keydatalen"
  //#endregion
  //#endregion
}
//#endregion

import { CryptoEngine } from "./CryptoEngine/CryptoEngine";