Repository navigation
Expand file tree
/
Copy pathTLVUtil.java
More file actions
335 lines (284 loc) · 11.7 KB
/
Copy pathTLVUtil.java
File metadata and controls
335 lines (284 loc) · 11.7 KB
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
/*
* Copyright 2010 sasc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sasc.iso7816;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import sasc.emv.EMVApplication;
import sasc.emv.EMVTags;
import static sasc.iso7816.TagValueType.BINARY;
import static sasc.iso7816.TagValueType.DOL;
import static sasc.iso7816.TagValueType.MIXED;
import static sasc.iso7816.TagValueType.NUMERIC;
import static sasc.iso7816.TagValueType.TEXT;
import sasc.util.Log;
import sasc.util.Util;
/**
*
* @author sasc
*/
public class TLVUtil {
private static Tag searchTagById(byte[] tagIdBytes) {
return EMVTags.getNotNull(tagIdBytes); //TODO take app (IIN or RID) into consideration
}
private static Tag searchTagById(ByteArrayInputStream stream){
return searchTagById(TLVUtil.readTagIdBytes(stream));
}
//This is just a list of Tag And Lengths (eg DOLs)
public static String getFormattedTagAndLength(byte[] data, int indentLength) {
StringBuilder buf = new StringBuilder();
String indent = Util.getSpaces(indentLength);
ByteArrayInputStream stream = new ByteArrayInputStream(data);
boolean firstLine = true;
while (stream.available() > 0) {
if (firstLine) {
firstLine = false;
} else {
buf.append("\n");
}
buf.append(indent);
Tag tag = searchTagById(stream);
int length = TLVUtil.readTagLength(stream);
buf.append(Util.prettyPrintHex(tag.getTagBytes()));
buf.append(" ");
buf.append(Util.byteArrayToHexString(Util.intToByteArray(length)));
buf.append(" -- ");
buf.append(tag.getName());
}
return buf.toString();
}
public static byte[] readTagIdBytes(ByteArrayInputStream stream) {
ByteArrayOutputStream tagBAOS = new ByteArrayOutputStream();
byte tagFirstOctet = (byte) stream.read();
tagBAOS.write(tagFirstOctet);
//Find TAG bytes
byte MASK = (byte) 0x1F;
if ((tagFirstOctet & MASK) == MASK) { // EMV book 3, Page 178 or Annex B1 (EMV4.3)
//Tag field is longer than 1 byte
do {
int nextOctet = stream.read();
if(nextOctet < 0){
break;
}
byte tlvIdNextOctet = (byte) nextOctet;
tagBAOS.write(tlvIdNextOctet);
if (!Util.isBitSet(tlvIdNextOctet, 8) || (Util.isBitSet(tlvIdNextOctet, 8) && (tlvIdNextOctet & 0x7f) == 0) ) {
break;
}
} while (true);
}
return tagBAOS.toByteArray();
}
public static int readTagLength(ByteArrayInputStream stream) {
//Find LENGTH bytes
int length;
int tmpLength = stream.read();
if(tmpLength < 0) {
throw new TLVException("Negative length: "+tmpLength);
}
if (tmpLength <= 127) { // 0111 1111
// short length form
length = tmpLength;
} else if (tmpLength == 128) { // 1000 0000
// length identifies indefinite form, will be set later
// indefinite form is not specified in ISO7816-4, but we include it here for completeness
length = tmpLength;
} else {
// long length form
int numberOfLengthOctets = tmpLength & 127; // turn off 8th bit
tmpLength = 0;
for (int i = 0; i < numberOfLengthOctets; i++) {
int nextLengthOctet = stream.read();
if(nextLengthOctet < 0){
throw new TLVException("EOS when reading length bytes");
}
tmpLength <<= 8;
tmpLength |= nextLengthOctet;
}
length = tmpLength;
}
return length;
}
public static BERTLV getNextTLV(ByteArrayInputStream stream) {
if (stream.available() < 2) {
throw new TLVException("Error parsing data. Available bytes < 2 . Length=" + stream.available());
}
//ISO/IEC 7816 uses neither '00' nor 'FF' as tag value.
//Before, between, or after TLV-coded data objects,
//'00' or 'FF' bytes without any meaning may occur
//(for example, due to erased or modified TLV-coded data objects).
stream.mark(0);
int peekInt = stream.read();
byte peekByte = (byte) peekInt;
//peekInt == 0xffffffff indicates EOS
while (peekInt != -1 && (peekByte == (byte) 0xFF || peekByte == (byte) 0x00)) {
stream.mark(0); //Current position
peekInt = stream.read();
peekByte = (byte) peekInt;
}
stream.reset(); //Reset back to the last known position without 0x00 or 0xFF
if (stream.available() < 2) {
throw new TLVException("Error parsing data. Available bytes < 2 . Length=" + stream.available());
}
byte[] tagIdBytes = TLVUtil.readTagIdBytes(stream);
//We need to get the raw length bytes.
//Use quick and dirty workaround
stream.mark(0);
int posBefore = stream.available();
//Now parse the lengthbyte(s)
//This method will read all length bytes. We can then find out how many bytes was read.
int length = TLVUtil.readTagLength(stream); //Decoded
//Now find the raw (encoded) length bytes
int posAfter = stream.available();
stream.reset();
byte[] lengthBytes = new byte[posBefore - posAfter];
if(lengthBytes.length < 1 || lengthBytes.length > 4){
throw new TLVException("Number of length bytes must be from 1 to 4. Found "+lengthBytes.length);
}
stream.read(lengthBytes, 0, lengthBytes.length);
int rawLength = Util.byteArrayToInt(lengthBytes);
byte[] valueBytes;
Tag tag = searchTagById(tagIdBytes);
// Find VALUE bytes
if (rawLength == 128) { // 1000 0000
// indefinite form
stream.mark(0);
int prevOctet = 1;
int curOctet;
int len = 0;
while (true) {
len++;
curOctet = stream.read();
if (curOctet < 0) {
throw new TLVException("Error parsing data. TLV "
+ "length byte indicated indefinite length, but EOS "
+ "was reached before 0x0000 was found" + stream.available());
}
if (prevOctet == 0 && curOctet == 0) {
break;
}
prevOctet = curOctet;
}
len -= 2;
valueBytes = new byte[len];
stream.reset();
stream.read(valueBytes, 0, len);
length = len;
} else {
if(stream.available() < length){
throw new TLVException("Length byte(s) indicated "+length+" value bytes, but only "+stream.available()+ " " +(stream.available()>1?"are":"is")+" available");
}
// definite form
valueBytes = new byte[length];
stream.read(valueBytes, 0, length);
}
//Remove any trailing 0x00 and 0xFF
stream.mark(0);
peekInt = stream.read();
peekByte = (byte) peekInt;
while (peekInt != -1 && (peekByte == (byte) 0xFF || peekByte == (byte) 0x00)) {
stream.mark(0);
peekInt = stream.read();
peekByte = (byte) peekInt;
}
stream.reset(); //Reset back to the last known position without 0x00 or 0xFF
BERTLV tlv = new BERTLV(tag, length, lengthBytes, valueBytes);
return tlv;
}
private static String getTagValueAsString(Tag tag, byte[] value) {
StringBuilder buf = new StringBuilder();
switch (tag.getTagValueType()) {
case TEXT:
buf.append("=");
buf.append(new String(value));
break;
case NUMERIC:
buf.append("NUMERIC");
break;
case BINARY:
buf.append("BINARY");
break;
case MIXED:
buf.append("=");
buf.append(Util.getSafePrintChars(value));
break;
case DOL:
buf.append("");
break;
}
return buf.toString();
}
public static List<TagAndLength> parseTagAndLength(byte[] data) {
ByteArrayInputStream stream = new ByteArrayInputStream(data);
List<TagAndLength> tagAndLengthList = new ArrayList<TagAndLength>();
while (stream.available() > 0) {
if (stream.available() < 2) {
throw new SmartCardException("Data length < 2 : " + stream.available());
}
byte[] tagIdBytes = TLVUtil.readTagIdBytes(stream);
int tagValueLength = TLVUtil.readTagLength(stream);
Tag tag = searchTagById(tagIdBytes);
tagAndLengthList.add(new TagAndLength(tag, tagValueLength));
}
return tagAndLengthList;
}
public static String prettyPrintAPDUResponse(byte[] data) {
return prettyPrintAPDUResponse(data, 0);
}
public static String prettyPrintAPDUResponse(byte[] data, int startPos, int length) {
byte[] tmp = new byte[length-startPos];
System.arraycopy(data, startPos, tmp, 0, length);
return prettyPrintAPDUResponse(tmp, 0);
}
public static String prettyPrintAPDUResponse(byte[] data, int indentLength) {
StringBuilder buf = new StringBuilder();
ByteArrayInputStream stream = new ByteArrayInputStream(data);
while (stream.available() > 0) {
buf.append("\n");
buf.append(Util.getSpaces(indentLength));
BERTLV tlv = TLVUtil.getNextTLV(stream);
Log.debug(tlv.toString());
byte[] tagBytes = tlv.getTagBytes();
byte[] lengthBytes = tlv.getRawEncodedLengthBytes();
byte[] valueBytes = tlv.getValueBytes();
Tag tag = tlv.getTag();
buf.append(Util.prettyPrintHex(tagBytes));
buf.append(" ");
buf.append(Util.prettyPrintHex(lengthBytes));
buf.append(" -- ");
buf.append(tag.getName());
int extraIndent = (lengthBytes.length * 3) + (tagBytes.length * 3);
if (tag.isConstructed()) {
//indentLength += extraIndent; //TODO check this
//Recursion
buf.append(prettyPrintAPDUResponse(valueBytes, indentLength + extraIndent));
} else {
buf.append("\n");
if (tag.getTagValueType() == TagValueType.DOL) {
buf.append(TLVUtil.getFormattedTagAndLength(valueBytes, indentLength + extraIndent));
} else {
buf.append(Util.getSpaces(indentLength + extraIndent));
buf.append(Util.prettyPrintHex(Util.byteArrayToHexString(valueBytes), indentLength + extraIndent));
buf.append(" (");
buf.append(TLVUtil.getTagValueAsString(tag, valueBytes));
buf.append(")");
}
}
}
return buf.toString();
}
}