Repository navigation
Expand file tree
/
Copy pathIsoATR.java
More file actions
145 lines (117 loc) · 3.48 KB
/
Copy pathIsoATR.java
File metadata and controls
145 lines (117 loc) · 3.48 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
/*
* 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.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import sasc.util.Util;
/**
*
* @author sasc
*/
public class IsoATR {
private byte[] atrBytes;
private Protocol protocol = Protocol.T_0;
private Convention convention;
private int numHistoricalBytes = 0;
//TODO
private byte TB1;
private byte TC1;
private byte TD1;
private byte TD2;
private byte TA3;
private byte TB3;
private byte TCK;
//TS
//T0
//TB1
//TC1
//TD1
//TD2
//TA3
//TB3
//TCK
public static class ParseException extends Exception{
public ParseException(String msg){
super(msg);
}
public ParseException(String msg, Throwable cause){
super(msg, cause);
}
}
public static enum Protocol{
T_0, T_1;
@Override
public String toString(){
switch(this){
case T_0:
return "T=0";
case T_1:
return "T=1";
}
return "";
}
}
public static enum Convention{
INVERSE, DIRECT
}
static IsoATR parse(byte[] atrBytes) throws IsoATR.ParseException{
return new IsoATR(atrBytes);
}
private IsoATR(byte[] atrBytes) throws IsoATR.ParseException{
//TODO throw ParseException if not ISO compliant
try{
this.atrBytes = atrBytes;
if(atrBytes[0] == (byte)0x3B){
convention = Convention.DIRECT;
}else if(atrBytes[0] == (byte)0x3F){
convention = Convention.INVERSE;
}
numHistoricalBytes = atrBytes[1] & 0x0F;
}catch(RuntimeException e){ //Catch all RE
throw new ParseException("Unable to parse ATR according to ISO", e);
}
}
public byte[] getATRBytes(){
return Arrays.copyOf(atrBytes, atrBytes.length);
}
public Convention getConvention(){
return convention;
}
public Protocol getProtocol(){
return protocol;
}
public byte[] getHistoricalBytes(){
byte[] tmp = new byte[numHistoricalBytes];
System.arraycopy(atrBytes, atrBytes.length-numHistoricalBytes, tmp, 0, numHistoricalBytes);
return tmp;
}
@Override
public String toString(){
StringWriter sw = new StringWriter();
dump(new PrintWriter(sw), 0);
return sw.toString();
}
public void dump(PrintWriter pw, int indent){
pw.println(Util.getSpaces(indent)+"ISO Compliant Answer To Reset (ATR)");
String indentStr = Util.getSpaces(indent+3);
pw.println(indentStr+"Convention - "+convention);
pw.println(indentStr+"Protocol - "+protocol);
if(numHistoricalBytes > 0){
pw.println(indentStr+"Historical bytes - "+Util.prettyPrintHex(Util.byteArrayToHexString(getHistoricalBytes())));
}
}
}