summaryrefslogtreecommitdiffstats
path: root/comm/third_party/asn1js/src/parser.ts
blob: 51ab7dfcd888ab48ec355045021501cd9d613a10 (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
import * as pvtsutils from "pvtsutils";
import { ValueBlock } from "./ValueBlock";
import { BaseBlock } from "./BaseBlock";
import { LocalBaseBlock } from "./internals/LocalBaseBlock";
import { AsnType, typeStore } from "./TypeStore";
import { checkBufferParams } from "./internals/utils";

export interface FromBerResult {
  offset: number;
  result: AsnType;
}

/**
 * Local function changing a type for ASN.1 classes
 * @param inputObject Incoming object
 * @param newType Target type to convert
 * @returns Converted object
 */
function localChangeType<T extends BaseBlock>(inputObject: BaseBlock, newType: new () => T): T {
  if (inputObject instanceof newType) {
    return inputObject;
  }

  const newObject = new newType();
  newObject.idBlock = inputObject.idBlock;
  newObject.lenBlock = inputObject.lenBlock;
  newObject.warnings = inputObject.warnings;
  newObject.valueBeforeDecodeView = inputObject.valueBeforeDecodeView;

  return newObject;
}

/**
 * Internal library function for decoding ASN.1 BER
 * @param inputBuffer ASN.1 BER encoded array
 * @param inputOffset Offset in ASN.1 BER encoded array where decoding should be started
 * @param inputLength Maximum length of array of bytes which can be using in this function
 * @returns
 */
export function localFromBER(inputBuffer: Uint8Array, inputOffset = 0, inputLength = inputBuffer.length): FromBerResult {
  const incomingOffset = inputOffset; // Need to store initial offset since "inputOffset" is changing in the function

  // Create a basic ASN.1 type since we need to return errors and warnings from the function
  let returnObject = new BaseBlock({}, ValueBlock);

  // Basic check for parameters
  const baseBlock = new LocalBaseBlock();
  if (!checkBufferParams(baseBlock, inputBuffer, inputOffset, inputLength)) {
    returnObject.error = baseBlock.error;

    return {
      offset: -1,
      result: returnObject
    };
  }

  // Getting Uint8Array subarray
  const intBuffer = inputBuffer.subarray(inputOffset, inputOffset + inputLength);

  // Initial checks
  if (!intBuffer.length) {
    returnObject.error = "Zero buffer length";

    return {
      offset: -1,
      result: returnObject
    };
  }

  // Decode identification block of ASN.1 BER structure
  // console.time("idBlock");
  let resultOffset = returnObject.idBlock.fromBER(inputBuffer, inputOffset, inputLength);
  if (returnObject.idBlock.warnings.length) {
    returnObject.warnings.concat(returnObject.idBlock.warnings);
  }
  if (resultOffset === -1) {
    returnObject.error = returnObject.idBlock.error;

    return {
      offset: -1,
      result: returnObject
    };
  }
  // console.timeEnd("idBlock");

  inputOffset = resultOffset;
  inputLength -= returnObject.idBlock.blockLength;

  // Decode length block of ASN.1 BER structure
  // console.time("lengthBlock");
  resultOffset = returnObject.lenBlock.fromBER(inputBuffer, inputOffset, inputLength);
  if (returnObject.lenBlock.warnings.length) {
    returnObject.warnings.concat(returnObject.lenBlock.warnings);
  }
  if (resultOffset === -1) {
    returnObject.error = returnObject.lenBlock.error;

    return {
      offset: -1,
      result: returnObject
    };
  }
  // console.timeEnd("lengthBlock");

  inputOffset = resultOffset;
  inputLength -= returnObject.lenBlock.blockLength;

  // Check for using indefinite length form in encoding for primitive types
  if (!returnObject.idBlock.isConstructed &&
    returnObject.lenBlock.isIndefiniteForm) {
    returnObject.error = "Indefinite length form used for primitive encoding form";

    return {
      offset: -1,
      result: returnObject
    };
  }

  // Switch ASN.1 block type
  let newASN1Type: new () => AsnType = BaseBlock as any;

  switch (returnObject.idBlock.tagClass) {
    // UNIVERSAL
    case 1:
      // Check for reserved tag numbers
      if ((returnObject.idBlock.tagNumber >= 37) &&
        (returnObject.idBlock.isHexOnly === false)) {
        returnObject.error = "UNIVERSAL 37 and upper tags are reserved by ASN.1 standard";

        return {
          offset: -1,
          result: returnObject
        };
      }
      switch (returnObject.idBlock.tagNumber) {
        case 0: // EndOfContent
          // Check for EndOfContent type
          if ((returnObject.idBlock.isConstructed) &&
            (returnObject.lenBlock.length > 0)) {
            returnObject.error = "Type [UNIVERSAL 0] is reserved";

            return {
              offset: -1,
              result: returnObject
            };
          }

          newASN1Type = typeStore.EndOfContent;

          break;
        case 1: // Boolean
          newASN1Type = typeStore.Boolean;
          break;
        case 2: // Integer
          newASN1Type = typeStore.Integer;
          break;
        case 3: // BitString
          newASN1Type = typeStore.BitString;
          break;
        case 4: // OctetString
          newASN1Type = typeStore.OctetString;
          break;
        case 5: // Null
          newASN1Type = typeStore.Null;
          break;
        case 6: // ObjectIdentifier
          newASN1Type = typeStore.ObjectIdentifier;
          break;
        case 10: // Enumerated
          newASN1Type = typeStore.Enumerated;
          break;
        case 12: // Utf8String
          newASN1Type = typeStore.Utf8String;
          break;
        case 13: // RelativeObjectIdentifier
          newASN1Type = typeStore.RelativeObjectIdentifier;
          break;
        case 14: // TIME
          newASN1Type = typeStore.TIME;
          break;
        case 15:
          returnObject.error = "[UNIVERSAL 15] is reserved by ASN.1 standard";

          return {
            offset: -1,
            result: returnObject
          };
        case 16: // Sequence
          newASN1Type = typeStore.Sequence;
          break;
        case 17: // Set
          newASN1Type = typeStore.Set;
          break;
        case 18: // NumericString
          newASN1Type = typeStore.NumericString;
          break;
        case 19: // PrintableString
          newASN1Type = typeStore.PrintableString;
          break;
        case 20: // TeletexString
          newASN1Type = typeStore.TeletexString;
          break;
        case 21: // VideotexString
          newASN1Type = typeStore.VideotexString;
          break;
        case 22: // IA5String
          newASN1Type = typeStore.IA5String;
          break;
        case 23: // UTCTime
          newASN1Type = typeStore.UTCTime;
          break;
        case 24: // GeneralizedTime
          newASN1Type = typeStore.GeneralizedTime;
          break;
        case 25: // GraphicString
          newASN1Type = typeStore.GraphicString;
          break;
        case 26: // VisibleString
          newASN1Type = typeStore.VisibleString;
          break;
        case 27: // GeneralString
          newASN1Type = typeStore.GeneralString;
          break;
        case 28: // UniversalString
          newASN1Type = typeStore.UniversalString;
          break;
        case 29: // CharacterString
          newASN1Type = typeStore.CharacterString;
          break;
        case 30: // BmpString
          newASN1Type = typeStore.BmpString;
          break;
        case 31: // DATE
          newASN1Type = typeStore.DATE;
          break;
        case 32: // TimeOfDay
          newASN1Type = typeStore.TimeOfDay;
          break;
        case 33: // DateTime
          newASN1Type = typeStore.DateTime;
          break;
        case 34: // Duration
          newASN1Type = typeStore.Duration;
          break;
        default: {
          const newObject = returnObject.idBlock.isConstructed
            ? new typeStore.Constructed()
            : new typeStore.Primitive();

          newObject.idBlock = returnObject.idBlock;
          newObject.lenBlock = returnObject.lenBlock;
          newObject.warnings = returnObject.warnings;

          returnObject = newObject;
        }
      }
      break;
    // All other tag classes
    case 2: // APPLICATION
    case 3: // CONTEXT-SPECIFIC
    case 4: // PRIVATE
    default: {
      newASN1Type = returnObject.idBlock.isConstructed
        ? typeStore.Constructed
        : typeStore.Primitive;
    }
  }


  // Change type and perform BER decoding
  returnObject = localChangeType(returnObject, newASN1Type);
  // console.time("valueBlock");
  resultOffset = returnObject.fromBER(inputBuffer, inputOffset, returnObject.lenBlock.isIndefiniteForm ? inputLength : returnObject.lenBlock.length);

  // Coping incoming buffer for entire ASN.1 block
  returnObject.valueBeforeDecodeView = inputBuffer.subarray(incomingOffset, incomingOffset + returnObject.blockLength);
  // console.timeEnd("valueBlock");

  return {
    offset: resultOffset,
    result: returnObject
  };
}

/**
 * Major function for decoding ASN.1 BER array into internal library structures
 * @param inputBuffer ASN.1 BER encoded array of bytes
 */
export function fromBER(inputBuffer: pvtsutils.BufferSource): FromBerResult {
  if (!inputBuffer.byteLength) {
    const result = new BaseBlock({}, ValueBlock);
    result.error = "Input buffer has zero length";

    return {
      offset: -1,
      result
    };
  }

  return localFromBER(pvtsutils.BufferSourceConverter.toUint8Array(inputBuffer).slice(), 0, inputBuffer.byteLength);
}