@trustchex/react-native-sdk 1.163.8 → 1.163.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/lib/module/Shared/EIDReader/constants/eidConstants.js +65 -0
  2. package/lib/module/Shared/EIDReader/data/country.js +21 -44
  3. package/lib/module/Shared/EIDReader/data/countryInterface.js +1 -0
  4. package/lib/module/Shared/EIDReader/data/countryRegistry.js +65 -0
  5. package/lib/module/Shared/EIDReader/data/isoCountry.js +5 -1
  6. package/lib/module/Shared/EIDReader/data/unicodeCountry.js +5 -1
  7. package/lib/module/Shared/EIDReader/defaultFileSystem.js +4 -4
  8. package/lib/module/Shared/EIDReader/eidService.js +52 -51
  9. package/lib/module/Shared/EIDReader/lds/cvcaFile.js +2 -2
  10. package/lib/module/Shared/EIDReader/tlv/tlvCore.js +161 -0
  11. package/lib/module/Shared/EIDReader/tlv/tlvInputStream.js +4 -4
  12. package/lib/module/Shared/EIDReader/tlv/tlvOutputState.js +4 -4
  13. package/lib/module/Shared/EIDReader/tlv/tlvOutputStream.js +13 -11
  14. package/lib/typescript/src/Shared/EIDReader/constants/eidConstants.d.ts +58 -0
  15. package/lib/typescript/src/Shared/EIDReader/constants/eidConstants.d.ts.map +1 -0
  16. package/lib/typescript/src/Shared/EIDReader/data/country.d.ts +4 -5
  17. package/lib/typescript/src/Shared/EIDReader/data/country.d.ts.map +1 -1
  18. package/lib/typescript/src/Shared/EIDReader/data/countryInterface.d.ts +21 -0
  19. package/lib/typescript/src/Shared/EIDReader/data/countryInterface.d.ts.map +1 -0
  20. package/lib/typescript/src/Shared/EIDReader/data/countryRegistry.d.ts +14 -0
  21. package/lib/typescript/src/Shared/EIDReader/data/countryRegistry.d.ts.map +1 -0
  22. package/lib/typescript/src/Shared/EIDReader/data/isoCountry.d.ts.map +1 -1
  23. package/lib/typescript/src/Shared/EIDReader/data/unicodeCountry.d.ts.map +1 -1
  24. package/lib/typescript/src/Shared/EIDReader/eidService.d.ts.map +1 -1
  25. package/lib/typescript/src/Shared/EIDReader/tlv/tlvCore.d.ts +22 -0
  26. package/lib/typescript/src/Shared/EIDReader/tlv/tlvCore.d.ts.map +1 -0
  27. package/lib/typescript/src/Shared/EIDReader/tlv/tlvOutputStream.d.ts +2 -2
  28. package/lib/typescript/src/Shared/EIDReader/tlv/tlvOutputStream.d.ts.map +1 -1
  29. package/package.json +1 -1
  30. package/src/Shared/EIDReader/constants/eidConstants.ts +67 -0
  31. package/src/Shared/EIDReader/data/country.ts +16 -47
  32. package/src/Shared/EIDReader/data/countryInterface.ts +21 -0
  33. package/src/Shared/EIDReader/data/countryRegistry.ts +73 -0
  34. package/src/Shared/EIDReader/data/isoCountry.ts +4 -0
  35. package/src/Shared/EIDReader/data/unicodeCountry.ts +4 -0
  36. package/src/Shared/EIDReader/defaultFileSystem.ts +4 -4
  37. package/src/Shared/EIDReader/eidService.ts +58 -53
  38. package/src/Shared/EIDReader/lds/cvcaFile.ts +2 -2
  39. package/src/Shared/EIDReader/tlv/tlvCore.ts +170 -0
  40. package/src/Shared/EIDReader/tlv/tlvInputStream.ts +4 -4
  41. package/src/Shared/EIDReader/tlv/tlvOutputState.ts +4 -4
  42. package/src/Shared/EIDReader/tlv/tlvOutputStream.ts +13 -12
@@ -1,4 +1,5 @@
1
1
  import { Country } from './country';
2
+ import { CountryRegistry } from './countryRegistry';
2
3
 
3
4
  export class ISOCountry extends Country {
4
5
  public static AD = new ISOCountry(32, 'AD', 'AND', 'Andorra', 'Andorran');
@@ -1249,3 +1250,6 @@ export class ISOCountry extends Country {
1249
1250
  return this.alpha3Code;
1250
1251
  }
1251
1252
  }
1253
+
1254
+ // Register this country type with the registry
1255
+ CountryRegistry.register(ISOCountry);
@@ -1,4 +1,5 @@
1
1
  import { Country } from './country';
2
+ import { CountryRegistry } from './countryRegistry';
2
3
 
3
4
  export class UnicodeCountry extends Country {
4
5
  public static AD = new UnicodeCountry(32, 'AD', 'AND', 'Andorra', 'Andorran');
@@ -1514,3 +1515,6 @@ export class UnicodeCountry extends Country {
1514
1515
  return this.alpha3Code;
1515
1516
  }
1516
1517
  }
1518
+
1519
+ // Register this country type with the registry
1520
+ CountryRegistry.register(UnicodeCountry);
@@ -1,4 +1,4 @@
1
- import { EIDService } from './eidService';
1
+ import { EID_CONSTANTS } from './constants/eidConstants';
2
2
  import type { APDULevelReadBinaryCapable } from './apduLevelReadBinaryCapable';
3
3
  import { ByteArrayInputStream } from './java/byteArrayInputStream';
4
4
  import { DefaultFileInfo } from './defaultFileInfo';
@@ -37,7 +37,7 @@ export class DefaultFileSystem implements FileSystemStructured {
37
37
  this.isSelected = false;
38
38
  this.isSFIEnabled = isSFIEnabled;
39
39
  this.fidToSFI = fidToSFI;
40
- this.maxReadBinaryLength = EIDService.EXTENDED_MAX_TRANSCEIVE_LENGTH;
40
+ this.maxReadBinaryLength = EID_CONSTANTS.EXTENDED_MAX_TRANSCEIVE_LENGTH;
41
41
  }
42
42
 
43
43
  public setWrapper(wrapper: APDUWrapper): void {
@@ -134,10 +134,10 @@ export class DefaultFileSystem implements FileSystemStructured {
134
134
  const sw = (error as any).getSW();
135
135
  if (
136
136
  (sw & ISO7816_SW.WRONG_LENGTH) === ISO7816_SW.WRONG_LENGTH &&
137
- this.maxReadBinaryLength > EIDService.DEFAULT_MAX_BLOCKSIZE
137
+ this.maxReadBinaryLength > EID_CONSTANTS.DEFAULT_MAX_BLOCKSIZE
138
138
  ) {
139
139
  this.wrapper = this.oldWrapper;
140
- this.maxReadBinaryLength = EIDService.DEFAULT_MAX_BLOCKSIZE;
140
+ this.maxReadBinaryLength = EID_CONSTANTS.DEFAULT_MAX_BLOCKSIZE;
141
141
  return new Uint8Array(0);
142
142
  }
143
143
 
@@ -12,166 +12,171 @@ import { BACProtocol } from './protocol/bacProtocol';
12
12
  import { CommandAPDU } from './smartcards/commandAPDU';
13
13
  import type { APDUListener } from './smartcards/apduListener';
14
14
  import { APDUEvent } from './smartcards/apduEvent';
15
+ import { EID_CONSTANTS } from './constants/eidConstants';
15
16
 
16
17
  export class EIDService extends AbstractMRTDCardService {
17
18
  /** Shared secret type for non-PACE key. */
18
- public static NO_PACE_KEY_REFERENCE: number = 0x00;
19
+ public static NO_PACE_KEY_REFERENCE: number =
20
+ EID_CONSTANTS.NO_PACE_KEY_REFERENCE;
19
21
 
20
22
  /** Shared secret type for PACE according to BSI TR-03110 v2.03 B.11.1. */
21
- public static MRZ_PACE_KEY_REFERENCE: number = 0x01;
23
+ public static MRZ_PACE_KEY_REFERENCE: number =
24
+ EID_CONSTANTS.MRZ_PACE_KEY_REFERENCE;
22
25
 
23
26
  /** Shared secret type for PACE according to BSI TR-03110 v2.03 B.11.1. */
24
- public static CAN_PACE_KEY_REFERENCE: number = 0x02;
27
+ public static CAN_PACE_KEY_REFERENCE: number =
28
+ EID_CONSTANTS.CAN_PACE_KEY_REFERENCE;
25
29
 
26
30
  /** Shared secret type for PACE according to BSI TR-03110 v2.03 B.11.1. */
27
- public static PIN_PACE_KEY_REFERENCE: number = 0x03;
31
+ public static PIN_PACE_KEY_REFERENCE: number =
32
+ EID_CONSTANTS.PIN_PACE_KEY_REFERENCE;
28
33
 
29
34
  /** Shared secret type for PACE according to BSI TR-03110 v2.03 B.11.1. */
30
- public static PUK_PACE_KEY_REFERENCE: number = 0x04;
35
+ public static PUK_PACE_KEY_REFERENCE: number =
36
+ EID_CONSTANTS.PUK_PACE_KEY_REFERENCE;
31
37
 
32
38
  /** Card Access. */
33
- public static EF_CARD_ACCESS: number = 0x011c;
39
+ public static EF_CARD_ACCESS: number = EID_CONSTANTS.EF_CARD_ACCESS;
34
40
 
35
41
  /** Card Security. */
36
- public static EF_CARD_SECURITY: number = 0x011d;
42
+ public static EF_CARD_SECURITY: number = EID_CONSTANTS.EF_CARD_SECURITY;
37
43
 
38
44
  /** File identifier for data group 1. Data group 1 contains the MRZ. */
39
- public static EF_DG1: number = 0x0101;
45
+ public static EF_DG1: number = EID_CONSTANTS.EF_DG1;
40
46
 
41
47
  /** File identifier for data group 2. Data group 2 contains face image data. */
42
- public static EF_DG2: number = 0x0102;
48
+ public static EF_DG2: number = EID_CONSTANTS.EF_DG2;
43
49
 
44
50
  /** File identifier for data group 3. Data group 3 contains finger print data. */
45
- public static EF_DG3: number = 0x0103;
51
+ public static EF_DG3: number = EID_CONSTANTS.EF_DG3;
46
52
 
47
53
  /** File identifier for data group 4. Data group 4 contains iris data. */
48
- public static EF_DG4: number = 0x0104;
54
+ public static EF_DG4: number = EID_CONSTANTS.EF_DG4;
49
55
 
50
56
  /** File identifier for data group 5. Data group 5 contains displayed portrait. */
51
- public static EF_DG5: number = 0x0105;
57
+ public static EF_DG5: number = EID_CONSTANTS.EF_DG5;
52
58
 
53
59
  /** File identifier for data group 6. Data group 6 is RFU. */
54
- public static EF_DG6: number = 0x0106;
60
+ public static EF_DG6: number = EID_CONSTANTS.EF_DG6;
55
61
 
56
62
  /** File identifier for data group 7. Data group 7 contains displayed signature. */
57
- public static EF_DG7: number = 0x0107;
63
+ public static EF_DG7: number = EID_CONSTANTS.EF_DG7;
58
64
 
59
65
  /** File identifier for data group 8. Data group 8 contains data features. */
60
- public static EF_DG8: number = 0x0108;
66
+ public static EF_DG8: number = EID_CONSTANTS.EF_DG8;
61
67
 
62
68
  /** File identifier for data group 9. Data group 9 contains structure features. */
63
- public static EF_DG9: number = 0x0109;
69
+ public static EF_DG9: number = EID_CONSTANTS.EF_DG9;
64
70
 
65
71
  /** File identifier for data group 10. Data group 10 contains substance features. */
66
- public static EF_DG10: number = 0x010a;
72
+ public static EF_DG10: number = EID_CONSTANTS.EF_DG10;
67
73
 
68
74
  /** File identifier for data group 11. Data group 11 contains additional personal details. */
69
- public static EF_DG11: number = 0x010b;
75
+ public static EF_DG11: number = EID_CONSTANTS.EF_DG11;
70
76
 
71
77
  /** File identifier for data group 12. Data group 12 contains additional document details. */
72
- public static EF_DG12: number = 0x010c;
78
+ public static EF_DG12: number = EID_CONSTANTS.EF_DG12;
73
79
 
74
80
  /** File identifier for data group 13. Data group 13 contains optional details. */
75
- public static EF_DG13: number = 0x010d;
81
+ public static EF_DG13: number = EID_CONSTANTS.EF_DG13;
76
82
 
77
83
  /** File identifier for data group 14. Data group 14 contains security infos. */
78
- public static EF_DG14: number = 0x010e;
84
+ public static EF_DG14: number = EID_CONSTANTS.EF_DG14;
79
85
 
80
86
  /** File identifier for data group 15. Data group 15 contains the public key used for Active Authentication. */
81
- public static EF_DG15: number = 0x010f;
87
+ public static EF_DG15: number = EID_CONSTANTS.EF_DG15;
82
88
 
83
89
  /** File identifier for data group 16. Data group 16 contains person(s) to notify. */
84
- public static EF_DG16: number = 0x0110;
90
+ public static EF_DG16: number = EID_CONSTANTS.EF_DG16;
85
91
 
86
92
  /** The security document. */
87
- public static EF_SOD: number = 0x011d;
93
+ public static EF_SOD: number = EID_CONSTANTS.EF_SOD;
88
94
 
89
95
  /** The data group presence list. */
90
- public static EF_COM: number = 0x011e;
96
+ public static EF_COM: number = EID_CONSTANTS.EF_COM;
91
97
 
92
98
  /**
93
99
  * Contains EAC CVA references. Note: this can be overridden by a file
94
100
  * identifier in the DG14 file (in a TerminalAuthenticationInfo). Check DG14
95
101
  * first. Also, this file does not have a header tag, like the others.
96
102
  */
97
- public static EF_CVCA: number = 0x011c;
103
+ public static EF_CVCA: number = EID_CONSTANTS.EF_CVCA;
98
104
 
99
105
  /** Short file identifier for card access file. */
100
- public static SFI_CARD_ACCESS: number = 0x1c;
106
+ public static SFI_CARD_ACCESS: number = EID_CONSTANTS.SFI_CARD_ACCESS;
101
107
 
102
108
  /** Short file identifier for card security file. */
103
- public static SFI_CARD_SECURITY: number = 0x1d;
109
+ public static SFI_CARD_SECURITY: number = EID_CONSTANTS.SFI_CARD_SECURITY;
104
110
 
105
111
  /** Short file identifier for file. */
106
- public static SFI_DG1: number = 0x01;
112
+ public static SFI_DG1: number = EID_CONSTANTS.SFI_DG1;
107
113
 
108
114
  /** Short file identifier for file. */
109
- public static SFI_DG2: number = 0x02;
115
+ public static SFI_DG2: number = EID_CONSTANTS.SFI_DG2;
110
116
 
111
117
  /** Short file identifier for file. */
112
- public static SFI_DG3: number = 0x03;
118
+ public static SFI_DG3: number = EID_CONSTANTS.SFI_DG3;
113
119
 
114
120
  /** Short file identifier for file. */
115
- public static SFI_DG4: number = 0x04;
121
+ public static SFI_DG4: number = EID_CONSTANTS.SFI_DG4;
116
122
 
117
123
  /** Short file identifier for file. */
118
- public static SFI_DG5: number = 0x05;
124
+ public static SFI_DG5: number = EID_CONSTANTS.SFI_DG5;
119
125
 
120
126
  /** Short file identifier for file. */
121
- public static SFI_DG6: number = 0x06;
127
+ public static SFI_DG6: number = EID_CONSTANTS.SFI_DG6;
122
128
 
123
129
  /** Short file identifier for file. */
124
- public static SFI_DG7: number = 0x07;
130
+ public static SFI_DG7: number = EID_CONSTANTS.SFI_DG7;
125
131
 
126
132
  /** Short file identifier for file. */
127
- public static SFI_DG8: number = 0x08;
133
+ public static SFI_DG8: number = EID_CONSTANTS.SFI_DG8;
128
134
 
129
135
  /** Short file identifier for file. */
130
- public static SFI_DG9: number = 0x09;
136
+ public static SFI_DG9: number = EID_CONSTANTS.SFI_DG9;
131
137
 
132
138
  /** Short file identifier for file. */
133
- public static SFI_DG10: number = 0x0a;
139
+ public static SFI_DG10: number = EID_CONSTANTS.SFI_DG10;
134
140
 
135
141
  /** Short file identifier for file. */
136
- public static SFI_DG11: number = 0x0b;
142
+ public static SFI_DG11: number = EID_CONSTANTS.SFI_DG11;
137
143
 
138
144
  /** Short file identifier for file. */
139
- public static SFI_DG12: number = 0x0c;
145
+ public static SFI_DG12: number = EID_CONSTANTS.SFI_DG12;
140
146
 
141
147
  /** Short file identifier for file. */
142
- public static SFI_DG13: number = 0x0d;
148
+ public static SFI_DG13: number = EID_CONSTANTS.SFI_DG13;
143
149
 
144
150
  /** Short file identifier for file. */
145
- public static SFI_DG14: number = 0x0e;
151
+ public static SFI_DG14: number = EID_CONSTANTS.SFI_DG14;
146
152
 
147
153
  /** Short file identifier for file. */
148
- public static SFI_DG15: number = 0x0f;
154
+ public static SFI_DG15: number = EID_CONSTANTS.SFI_DG15;
149
155
 
150
156
  /** Short file identifier for file. */
151
- public static SFI_DG16: number = 0x10;
157
+ public static SFI_DG16: number = EID_CONSTANTS.SFI_DG16;
152
158
 
153
159
  /** Short file identifier for file. */
154
- public static SFI_COM: number = 0x1e;
160
+ public static SFI_COM: number = EID_CONSTANTS.SFI_COM;
155
161
 
156
162
  /** Short file identifier for file. */
157
- public static SFI_SOD: number = 0x1d;
163
+ public static SFI_SOD: number = EID_CONSTANTS.SFI_SOD;
158
164
 
159
165
  /** Short file identifier for file. */
160
- public static SFI_CVCA: number = 0x1c;
166
+ public static SFI_CVCA: number = EID_CONSTANTS.SFI_CVCA;
161
167
 
162
168
  /** The default maximal blocksize used for unencrypted APDUs. */
163
- public static DEFAULT_MAX_BLOCKSIZE: number = 223;
169
+ public static DEFAULT_MAX_BLOCKSIZE: number =
170
+ EID_CONSTANTS.DEFAULT_MAX_BLOCKSIZE;
164
171
 
165
172
  /** The normal maximal transceive length of APDUs. */
166
- public static NORMAL_MAX_TRANSCEIVE_LENGTH: number = 256;
173
+ public static NORMAL_MAX_TRANSCEIVE_LENGTH: number = EID_CONSTANTS.NORMAL_MAX_TRANSCEIVE_LENGTH;
167
174
 
168
175
  /** The extended maximal transceive length of APDUs. */
169
- public static EXTENDED_MAX_TRANSCEIVE_LENGTH: number = 65536;
176
+ public static EXTENDED_MAX_TRANSCEIVE_LENGTH: number = EID_CONSTANTS.EXTENDED_MAX_TRANSCEIVE_LENGTH;
170
177
 
171
178
  /** The applet we select when we start a session. */
172
- public static APPLET_AID: Uint8Array = Uint8Array.from([
173
- 0xa0, 0x00, 0x00, 0x02, 0x47, 0x10, 0x01,
174
- ]);
179
+ public static APPLET_AID: Uint8Array = EID_CONSTANTS.APPLET_AID;
175
180
 
176
181
  private maxBlockSize: number;
177
182
  private isOpen: boolean;
@@ -1,4 +1,4 @@
1
- import { EIDService } from '../eidService';
1
+ import { EID_CONSTANTS } from '../constants/eidConstants';
2
2
  import { CVCPrincipal } from '../cert/cvcPrincipal';
3
3
  import { InputStream } from '../java/inputStream';
4
4
  import { OutputStream } from '../java/outputStream';
@@ -31,7 +31,7 @@ export class CVCAFile {
31
31
  this.caReference = caReference;
32
32
  this.altCAReference = altCAReference || null;
33
33
  } else {
34
- this.fid = EIDService.EF_CVCA;
34
+ this.fid = EID_CONSTANTS.EF_CVCA;
35
35
  this.readObject(fidOrInputStream);
36
36
  }
37
37
  }
@@ -0,0 +1,170 @@
1
+ import { ASN1Constants } from './asn1Constants';
2
+
3
+ /**
4
+ * Core TLV utility functions that don't depend on TLVInputStream/TLVOutputStream
5
+ * to avoid circular dependencies.
6
+ */
7
+ export class TLVCore extends ASN1Constants {
8
+ // Tag class constants
9
+ public static readonly UNIVERSAL_CLASS = 0;
10
+ public static readonly APPLICATION_CLASS = 1;
11
+ public static readonly CONTEXT_SPECIFIC_CLASS = 2;
12
+ public static readonly PRIVATE_CLASS = 3;
13
+
14
+ public static isPrimitive(tag: number): boolean {
15
+ let i = 3;
16
+ for (; i >= 0; i--) {
17
+ const mask = 0xff << (8 * i);
18
+ if ((tag & mask) !== 0x00) {
19
+ break;
20
+ }
21
+ }
22
+ const msByte = ((tag & (0xff << (8 * i))) >> (8 * i)) & 0xff;
23
+ return (msByte & 0x20) === 0x00;
24
+ }
25
+
26
+ public static getTagLength(tag: number): number {
27
+ if (tag < 0x1f) {
28
+ return 1;
29
+ }
30
+ if (tag < 0x80) {
31
+ return 2;
32
+ }
33
+ if (tag < 0x8000) {
34
+ return 3;
35
+ }
36
+ if (tag < 0x800000) {
37
+ return 4;
38
+ }
39
+ return 5;
40
+ }
41
+
42
+ public static isValidTag(tag: number): boolean {
43
+ return tag >= 0x00 && tag <= 0xffffffff;
44
+ }
45
+
46
+ public static getLengthLength(length: number): number {
47
+ if (length < 0x80) {
48
+ return 1;
49
+ }
50
+ if (length < 0x100) {
51
+ return 2;
52
+ }
53
+ if (length < 0x10000) {
54
+ return 3;
55
+ }
56
+ if (length < 0x1000000) {
57
+ return 4;
58
+ }
59
+ return 5;
60
+ }
61
+
62
+ public static getTagAsBytes(tag: number): number[] {
63
+ const out: number[] = [];
64
+ const byteCount = Math.floor(Math.log(tag) / Math.log(256)) + 1;
65
+ for (let i = 0; i < byteCount; i++) {
66
+ const pos = 8 * (byteCount - i - 1);
67
+ out.push((tag & (0xff << pos)) >> pos);
68
+ }
69
+ const tagBytes = out;
70
+ switch (TLVCore.getTagClass(tag)) {
71
+ case TLVCore.APPLICATION_CLASS:
72
+ tagBytes[0] |= 0x40;
73
+ break;
74
+ case TLVCore.CONTEXT_SPECIFIC_CLASS:
75
+ tagBytes[0] |= 0x80;
76
+ break;
77
+ case TLVCore.PRIVATE_CLASS:
78
+ tagBytes[0] |= 0xc0;
79
+ break;
80
+ default:
81
+ /* NOTE: Unsupported tag class. Now what? */
82
+ break;
83
+ }
84
+ if (!TLVCore.isPrimitive(tag)) {
85
+ tagBytes[0] |= 0x20;
86
+ }
87
+ return tagBytes;
88
+ }
89
+
90
+ public static getLengthAsBytes(length: number): number[] {
91
+ const out: number[] = [];
92
+ if (length < 0x80) {
93
+ /* short form */
94
+ out.push(length);
95
+ } else {
96
+ const byteCount = TLVCore.log(length, 256);
97
+ out.push(0x80 | byteCount);
98
+ for (let i = 0; i < byteCount; i++) {
99
+ const pos = 8 * (byteCount - i - 1);
100
+ out.push((length & (0xff << pos)) >> pos);
101
+ }
102
+ }
103
+ return out;
104
+ }
105
+
106
+ public static getTagClass(tag: number): number {
107
+ let i = 3;
108
+ for (; i >= 0; i--) {
109
+ const mask = 0xff << (8 * i);
110
+ if ((tag & mask) !== 0x00) {
111
+ break;
112
+ }
113
+ }
114
+ const msByte = ((tag & (0xff << (8 * i))) >> (8 * i)) & 0xff;
115
+ switch (msByte & 0xc0) {
116
+ case 0x00:
117
+ return TLVCore.UNIVERSAL_CLASS;
118
+ case 0x40:
119
+ return TLVCore.APPLICATION_CLASS;
120
+ case 0x80:
121
+ return TLVCore.CONTEXT_SPECIFIC_CLASS;
122
+ case 0xc0:
123
+ default:
124
+ return TLVCore.PRIVATE_CLASS;
125
+ }
126
+ }
127
+
128
+ private static log(n: number, base: number): number {
129
+ let result = 0;
130
+ while (n > 0) {
131
+ n = n / base;
132
+ result++;
133
+ }
134
+ return result;
135
+ }
136
+
137
+ public static writeTagLength(tag: number, length: number): Uint8Array {
138
+ const tagLength = TLVCore.getTagLength(tag);
139
+ const lengthLength = TLVCore.getLengthLength(length);
140
+ const result = new Uint8Array(tagLength + lengthLength);
141
+ let offset = 0;
142
+
143
+ // Write tag
144
+ if (tagLength === 1) {
145
+ result[offset++] = tag & 0xff;
146
+ } else if (tagLength === 2) {
147
+ result[offset++] = 0x1f;
148
+ result[offset++] = tag & 0xff;
149
+ } else {
150
+ result[offset++] = 0x1f;
151
+ for (let i = tagLength - 2; i >= 0; i--) {
152
+ result[offset++] = (tag >> (i * 8)) & 0xff;
153
+ }
154
+ }
155
+
156
+ // Write length
157
+ if (lengthLength === 1) {
158
+ result[offset++] = length & 0xff;
159
+ } else {
160
+ result[offset++] = 0x80 | (lengthLength - 1);
161
+ for (let i = lengthLength - 2; i >= 0; i--) {
162
+ result[offset++] = (length >> (i * 8)) & 0xff;
163
+ }
164
+ }
165
+
166
+ return result;
167
+ }
168
+ }
169
+
170
+ export default TLVCore;
@@ -1,7 +1,7 @@
1
1
  import { ByteArrayInputStream } from '../java/byteArrayInputStream';
2
2
  import { DataInputStream } from '../java/dataInputStream';
3
3
  import { InputStream } from '../java/inputStream';
4
- import TLVUtil from './tlv.utils';
4
+ import TLVCore from './tlvCore';
5
5
  import { TLVInputState } from './tlvInputState';
6
6
 
7
7
  export class TLVInputStream extends InputStream {
@@ -132,11 +132,11 @@ export class TLVInputStream extends InputStream {
132
132
  if (this.state.getIsAtStartOfTag()) {
133
133
  } else if (this.state.getIsAtStartOfLength()) {
134
134
  await this.readLength();
135
- if (TLVUtil.isPrimitive(this.state.getTag())) {
135
+ if (TLVCore.isPrimitive(this.state.getTag())) {
136
136
  await this.skipValue();
137
137
  }
138
138
  } else {
139
- if (TLVUtil.isPrimitive(this.state.getTag())) {
139
+ if (TLVCore.isPrimitive(this.state.getTag())) {
140
140
  await this.skipValue();
141
141
  }
142
142
  }
@@ -144,7 +144,7 @@ export class TLVInputStream extends InputStream {
144
144
  if (tag === searchTag) {
145
145
  return;
146
146
  }
147
- if (TLVUtil.isPrimitive(tag)) {
147
+ if (TLVCore.isPrimitive(tag)) {
148
148
  const length = await this.readLength();
149
149
  const skippedBytes = await this.skipValue();
150
150
  if (skippedBytes >= length) {
@@ -1,4 +1,4 @@
1
- import tlvUtils from './tlv.utils';
1
+ import TLVCore from './tlvCore';
2
2
 
3
3
  export class TLVOutputState {
4
4
  private state: TLVStruct[];
@@ -68,7 +68,7 @@ export class TLVOutputState {
68
68
  const obj = new TLVStruct(tag);
69
69
  if (this.state.length !== 0) {
70
70
  const parent = this.state[this.state.length - 1];
71
- const tagBytes = tlvUtils.getTagAsBytes(tag);
71
+ const tagBytes = TLVCore.getTagAsBytes(tag);
72
72
  parent.write(tagBytes, 0, tagBytes.length);
73
73
  }
74
74
 
@@ -99,7 +99,7 @@ export class TLVOutputState {
99
99
  const obj = this.state.pop();
100
100
  if (this.state.length !== 0) {
101
101
  const parent = this.state[this.state.length - 1];
102
- const lengthBytes = tlvUtils.getLengthAsBytes(length);
102
+ const lengthBytes = TLVCore.getLengthAsBytes(length);
103
103
  parent.write(lengthBytes, 0, lengthBytes.length);
104
104
  }
105
105
 
@@ -125,7 +125,7 @@ export class TLVOutputState {
125
125
  currentObject.getValueBytesProcessed() === currentObject.getLength()
126
126
  ) {
127
127
  this.state.pop();
128
- const lengthBytes = tlvUtils.getLengthAsBytes(byteCount);
128
+ const lengthBytes = TLVCore.getLengthAsBytes(byteCount);
129
129
  const value = currentObject.getValue();
130
130
  this.updateValueBytesProcessed(lengthBytes, 0, lengthBytes.length);
131
131
  this.updateValueBytesProcessed(value, 0, value.length);
@@ -1,6 +1,6 @@
1
1
  import { DataOutputStream } from '../java/dataOutputStream';
2
2
  import { OutputStream } from '../java/outputStream';
3
- import TLVUtil from './tlv.utils';
3
+ import TLVCore from './tlvCore';
4
4
  import { TLVOutputState } from './tlvOutputState';
5
5
 
6
6
  export class TLVOutputStream extends OutputStream {
@@ -16,21 +16,22 @@ export class TLVOutputStream extends OutputStream {
16
16
  this.state = new TLVOutputState();
17
17
  }
18
18
 
19
- writeTag(tag: number): void {
20
- const tagAsBytes = TLVUtil.getTagAsBytes(tag);
21
- if (this.state.canBeWritten()) {
22
- this.outputStream.writeBytes(Uint8Array.from(tagAsBytes));
19
+ public async writeTag(tag: number): Promise<void> {
20
+ if (!this.state.getIsAtStartOfTag()) {
21
+ throw new Error('Cannot write tag, still need length');
23
22
  }
24
-
23
+ const tagAsBytes = TLVCore.getTagAsBytes(tag);
24
+ await this.outputStream.writeBytes(Uint8Array.from(tagAsBytes));
25
25
  this.state.setTagProcessed(tag);
26
26
  }
27
27
 
28
- writeLength(length: number): void {
29
- const lengthAsBytes = TLVUtil.getLengthAsBytes(length);
30
- this.state.setLengthProcessed(length);
31
- if (this.state.canBeWritten()) {
32
- this.outputStream.writeBytes(Uint8Array.from(lengthAsBytes));
28
+ public async writeLength(length: number): Promise<void> {
29
+ if (!this.state.getIsAtStartOfLength()) {
30
+ throw new Error('Cannot write length');
33
31
  }
32
+ const lengthAsBytes = TLVCore.getLengthAsBytes(length);
33
+ await this.outputStream.writeBytes(Uint8Array.from(lengthAsBytes));
34
+ this.state.setLengthProcessed(length);
34
35
  }
35
36
 
36
37
  writeValue(value: Uint8Array): void {
@@ -91,7 +92,7 @@ export class TLVOutputStream extends OutputStream {
91
92
  const length = bufferedValueBytes.length;
92
93
  this.state.updatePreviousLength(length);
93
94
  if (this.state.canBeWritten()) {
94
- const lengthAsBytes = TLVUtil.getLengthAsBytes(length);
95
+ const lengthAsBytes = TLVCore.getLengthAsBytes(length);
95
96
  this.outputStream.writeBytes(Uint8Array.from(lengthAsBytes));
96
97
  this.outputStream.writeBytes(Uint8Array.from(bufferedValueBytes));
97
98
  }