summaryrefslogtreecommitdiffstats
path: root/comm/third_party/asn1js/src/schema.ts
blob: 4f07e92a13b349a2635c48b989048b64f924bf76 (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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/* eslint-disable @typescript-eslint/ban-ts-comment */
import * as pvtsutils from "pvtsutils";
import { IS_CONSTRUCTED, EMPTY_STRING, NAME, ID_BLOCK, FROM_BER, TO_BER, TAG_CLASS, TAG_NUMBER, IS_HEX_ONLY, LOCAL, VALUE_HEX_VIEW } from "./internals/constants";
import { Any } from "./Any";
import { Choice } from "./Choice";
import { Repeated } from "./Repeated";
import { localFromBER } from "./parser";
import { AsnType, typeStore } from "./TypeStore";

export type AsnSchemaType = AsnType | Any | Choice | Repeated;

export interface CompareSchemaSuccess {
  verified: true;
  result: AsnType & { [key: string]: any; };
}

export interface CompareSchemaFail {
  verified: false;
  name?: string;
  result: AsnType | { error: string; };
}

export type CompareSchemaResult = CompareSchemaSuccess | CompareSchemaFail;

/**
 * Compare of two ASN.1 object trees
 * @param root Root of input ASN.1 object tree
 * @param inputData Input ASN.1 object tree
 * @param inputSchema Input ASN.1 schema to compare with
 * @return Returns result of comparison
 */
export function compareSchema(root: AsnType, inputData: AsnType, inputSchema: AsnSchemaType): CompareSchemaResult {
  //#region Special case for Choice schema element type
  if (inputSchema instanceof Choice) {
    const choiceResult = false;

    for (let j = 0; j < inputSchema.value.length; j++) {
      const result = compareSchema(root, inputData, inputSchema.value[j]);
      if (result.verified) {
        return {
          verified: true,
          result: root
        };
      }
    }

    if (choiceResult === false) {
      const _result: CompareSchemaResult = {
        verified: false,
        result: {
          error: "Wrong values for Choice type"
        },
      };

      if (inputSchema.hasOwnProperty(NAME))
        _result.name = inputSchema.name;

      return _result;
    }
  }
  //#endregion
  //#region Special case for Any schema element type
  if (inputSchema instanceof Any) {
    //#region Add named component of ASN.1 schema
    if (inputSchema.hasOwnProperty(NAME))
      (root as any)[inputSchema.name] = inputData; // TODO Such call may replace original field of the object (eg idBlock)


    //#endregion
    return {
      verified: true,
      result: root
    };
  }
  //#endregion
  //#region Initial check
  if ((root instanceof Object) === false) {
    return {
      verified: false,
      result: { error: "Wrong root object" }
    };
  }

  if ((inputData instanceof Object) === false) {
    return {
      verified: false,
      result: { error: "Wrong ASN.1 data" }
    };
  }

  if ((inputSchema instanceof Object) === false) {
    return {
      verified: false,
      result: { error: "Wrong ASN.1 schema" }
    };
  }

  if ((ID_BLOCK in inputSchema) === false) {
    return {
      verified: false,
      result: { error: "Wrong ASN.1 schema" }
    };
  }
  //#endregion
  //#region Comparing idBlock properties in ASN.1 data and ASN.1 schema
  //#region Encode and decode ASN.1 schema idBlock
  /// <remarks>This encoding/decoding is necessary because could be an errors in schema definition</remarks>
  if ((FROM_BER in inputSchema.idBlock) === false) {
    return {
      verified: false,
      result: { error: "Wrong ASN.1 schema" }
    };
  }

  if ((TO_BER in inputSchema.idBlock) === false) {
    return {
      verified: false,
      result: { error: "Wrong ASN.1 schema" }
    };
  }

  const encodedId = inputSchema.idBlock.toBER(false);
  if (encodedId.byteLength === 0) {
    return {
      verified: false,
      result: { error: "Error encoding idBlock for ASN.1 schema" }
    };
  }

  const decodedOffset = inputSchema.idBlock.fromBER(encodedId, 0, encodedId.byteLength);
  if (decodedOffset === -1) {
    return {
      verified: false,
      result: { error: "Error decoding idBlock for ASN.1 schema" }
    };
  }
  //#endregion
  //#region tagClass
  if (inputSchema.idBlock.hasOwnProperty(TAG_CLASS) === false) {
    return {
      verified: false,
      result: { error: "Wrong ASN.1 schema" }
    };
  }

  if (inputSchema.idBlock.tagClass !== inputData.idBlock.tagClass) {
    return {
      verified: false,
      result: root
    };
  }
  //#endregion
  //#region tagNumber
  if (inputSchema.idBlock.hasOwnProperty(TAG_NUMBER) === false) {
    return {
      verified: false,
      result: { error: "Wrong ASN.1 schema" }
    };
  }

  if (inputSchema.idBlock.tagNumber !== inputData.idBlock.tagNumber) {
    return {
      verified: false,
      result: root
    };
  }
  //#endregion
  //#region isConstructed
  if (inputSchema.idBlock.hasOwnProperty(IS_CONSTRUCTED) === false) {
    return {
      verified: false,
      result: { error: "Wrong ASN.1 schema" }
    };
  }

  if (inputSchema.idBlock.isConstructed !== inputData.idBlock.isConstructed) {
    return {
      verified: false,
      result: root
    };
  }
  //#endregion
  //#region isHexOnly
  if (!(IS_HEX_ONLY in inputSchema.idBlock)) // Since 'isHexOnly' is an inherited property
  {
    return {
      verified: false,
      result: { error: "Wrong ASN.1 schema" }
    };
  }

  if (inputSchema.idBlock.isHexOnly !== inputData.idBlock.isHexOnly) {
    return {
      verified: false,
      result: root
    };
  }
  //#endregion
  //#region valueHex
  if (inputSchema.idBlock.isHexOnly) {
    if ((VALUE_HEX_VIEW in inputSchema.idBlock) === false) // Since 'valueHex' is an inherited property
    {
      return {
        verified: false,
        result: { error: "Wrong ASN.1 schema" }
      };
    }

    const schemaView = inputSchema.idBlock.valueHexView;
    const asn1View = inputData.idBlock.valueHexView;

    if (schemaView.length !== asn1View.length) {
      return {
        verified: false,
        result: root
      };
    }

    for (let i = 0; i < schemaView.length; i++) {
      if (schemaView[i] !== asn1View[1]) {
        return {
          verified: false,
          result: root
        };
      }
    }
  }
  //#endregion
  //#endregion
  //#region Add named component of ASN.1 schema
  if (inputSchema.name) {
    inputSchema.name = inputSchema.name.replace(/^\s+|\s+$/g, EMPTY_STRING);
    if (inputSchema.name)
      (root as any)[inputSchema.name] = inputData; // TODO check field existence. If exists throw an error
  }
  //#endregion
  //#region Getting next ASN.1 block for comparison
  if (inputSchema instanceof typeStore.Constructed) {
    // TODO not clear how it works for OctetString and BitString
    //* if (inputSchema.idBlock.isConstructed) {
    let admission = 0;
    let result: CompareSchemaResult = {
      verified: false,
      result: {
        error: "Unknown error",
      }
    };

    let maxLength = inputSchema.valueBlock.value.length;

    if (maxLength > 0) {
      if (inputSchema.valueBlock.value[0] instanceof Repeated) {
        // @ts-ignore
        maxLength = inputData.valueBlock.value.length; // TODO debug it
      }
    }

    //#region Special case when constructive value has no elements
    if (maxLength === 0) {
      return {
        verified: true,
        result: root
      };
    }
    //#endregion
    //#region Special case when "inputData" has no values and "inputSchema" has all optional values
    // @ts-ignore
    // TODO debug it
    if ((inputData.valueBlock.value.length === 0) &&
      (inputSchema.valueBlock.value.length !== 0)) {
      let _optional = true;

      for (let i = 0; i < inputSchema.valueBlock.value.length; i++)
        _optional = _optional && (inputSchema.valueBlock.value[i].optional || false);

      if (_optional) {
        return {
          verified: true,
          result: root
        };
      }

      //#region Delete early added name of block
      if (inputSchema.name) {
        inputSchema.name = inputSchema.name.replace(/^\s+|\s+$/g, EMPTY_STRING);
        if (inputSchema.name)
          delete (root as any)[inputSchema.name];
      }
      //#endregion
      root.error = "Inconsistent object length";

      return {
        verified: false,
        result: root
      };
    }
    //#endregion
    for (let i = 0; i < maxLength; i++) {
      //#region Special case when there is an OPTIONAL element of ASN.1 schema at the end
      // @ts-ignore
      if ((i - admission) >= inputData.valueBlock.value.length) {
        if (inputSchema.valueBlock.value[i].optional === false) {
          const _result: CompareSchemaResult = {
            verified: false,
            result: root
          };

          root.error = "Inconsistent length between ASN.1 data and schema";

          //#region Delete early added name of block
          if (inputSchema.name) {
            inputSchema.name = inputSchema.name.replace(/^\s+|\s+$/g, EMPTY_STRING);
            if (inputSchema.name) {
              delete (root as any)[inputSchema.name];
              _result.name = inputSchema.name;
            }
          }
          //#endregion

          return _result;
        }
      }

      //#endregion
      else {
        //#region Special case for Repeated type of ASN.1 schema element
        if (inputSchema.valueBlock.value[0] instanceof Repeated) {
          // @ts-ignore
          result = compareSchema(root, inputData.valueBlock.value[i], inputSchema.valueBlock.value[0].value);
          if (result.verified === false) {
            if (inputSchema.valueBlock.value[0].optional)
              admission++;
            else {
              //#region Delete early added name of block
              if (inputSchema.name) {
                inputSchema.name = inputSchema.name.replace(/^\s+|\s+$/g, EMPTY_STRING);
                if (inputSchema.name)
                  delete (root as any)[inputSchema.name];
              }
              //#endregion

              return result;
            }
          }

          if ((NAME in inputSchema.valueBlock.value[0]) && (inputSchema.valueBlock.value[0].name.length > 0)) {
            let arrayRoot: Record<string, any> = {};

            if ((LOCAL in inputSchema.valueBlock.value[0]) && (inputSchema.valueBlock.value[0].local))
              arrayRoot = inputData;

            else
              arrayRoot = root;

            if (typeof arrayRoot[inputSchema.valueBlock.value[0].name] === "undefined")
              arrayRoot[inputSchema.valueBlock.value[0].name] = [];

            // @ts-ignore
            arrayRoot[inputSchema.valueBlock.value[0].name].push(inputData.valueBlock.value[i]);
          }
        }

        //#endregion
        else {
          // @ts-ignore
          result = compareSchema(root, inputData.valueBlock.value[i - admission], inputSchema.valueBlock.value[i]);
          if (result.verified === false) {
            if (inputSchema.valueBlock.value[i].optional)
              admission++;
            else {
              //#region Delete early added name of block
              if (inputSchema.name) {
                inputSchema.name = inputSchema.name.replace(/^\s+|\s+$/g, EMPTY_STRING);
                if (inputSchema.name)
                  delete (root as any)[inputSchema.name];
              }
              //#endregion

              return result;
            }
          }
        }
      }
    }

    if (result.verified === false) // The situation may take place if last element is OPTIONAL and verification failed
    {
      const _result: CompareSchemaResult = {
        verified: false,
        result: root
      };

      //#region Delete early added name of block
      if (inputSchema.name) {
        inputSchema.name = inputSchema.name.replace(/^\s+|\s+$/g, EMPTY_STRING);
        if (inputSchema.name) {
          delete (root as any)[inputSchema.name];
          _result.name = inputSchema.name;
        }
      }
      //#endregion

      return _result;
    }

    return {
      verified: true,
      result: root
    };
  }
  //#endregion
  //#region Ability to parse internal value for primitive-encoded value (value of OctetString, for example)
  if (inputSchema.primitiveSchema &&
    (VALUE_HEX_VIEW in inputData.valueBlock)) {
    //#region Decoding of raw ASN.1 data
    const asn1 = localFromBER(inputData.valueBlock.valueHexView);
    if (asn1.offset === -1) {
      const _result: CompareSchemaResult = {
        verified: false,
        result: asn1.result
      };

      //#region Delete early added name of block
      if (inputSchema.name) {
        inputSchema.name = inputSchema.name.replace(/^\s+|\s+$/g, EMPTY_STRING);
        if (inputSchema.name) {
          delete (root as any)[inputSchema.name];
          _result.name = inputSchema.name;
        }
      }
      //#endregion

      return _result;
    }
    //#endregion

    return compareSchema(root, asn1.result, inputSchema.primitiveSchema);
  }

  return {
    verified: true,
    result: root
  };
  //#endregion
}
/**
 * ASN.1 schema verification for ArrayBuffer data
 * @param  inputBuffer Input BER-encoded ASN.1 data
 * @param  inputSchema Input ASN.1 schema to verify against to
 * @return
 */

export function verifySchema(inputBuffer: pvtsutils.BufferSource, inputSchema: AsnSchemaType): CompareSchemaResult {
  //#region Initial check
  if ((inputSchema instanceof Object) === false) {
    return {
      verified: false,
      result: { error: "Wrong ASN.1 schema type" }
    };
  }
  //#endregion
  //#region Decoding of raw ASN.1 data
  const asn1 = localFromBER(pvtsutils.BufferSourceConverter.toUint8Array(inputBuffer));
  if (asn1.offset === -1) {
    return {
      verified: false,
      result: asn1.result
    };
  }
  //#endregion
  //#region Compare ASN.1 struct with input schema

  return compareSchema(asn1.result, asn1.result, inputSchema);
  //#endregion
}