Repository navigation
Expand file tree
/
Copy pathATR_DB.java
More file actions
180 lines (162 loc) · 6.8 KB
/
Copy pathATR_DB.java
File metadata and controls
180 lines (162 loc) · 6.8 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
/*
* 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.lookup;
import java.io.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import sasc.util.Log;
import sasc.util.Util;
/**
* Ludovic Rousseau's smartcard list
* http://ludovic.rousseau.free.fr/softwares/pcsc-tools/smartcard_list.txt
*
* + some additional ATRs
*
*
* @author sasc
*/
public class ATR_DB {
private static final CountDownLatch initLatch = new CountDownLatch(1);
private static final Map<String, PublicATR> atrMap = new ConcurrentHashMap<String, PublicATR>();
private static final AtomicBoolean initCalled = new AtomicBoolean(false);
public synchronized static void initialize() {
if(initCalled.getAndSet(true)){
return;
}
new Thread(new Runnable(){
@Override
public void run() {
InputStream is1 = null;
InputStream is2 = null;
BufferedReader br = null;
try{
is1 = Util.loadResource(ATR_DB.class, "/smartcard_list.txt");
is2 = Util.loadResource(ATR_DB.class, "/smartcard_list_additional_atrs.txt");
br = new BufferedReader(new InputStreamReader(new SequenceInputStream(is1, is2), "UTF-8"));
int lineNumber = 0;
String line;
String currentATR = null;
while((line = br.readLine()) != null){
++lineNumber;
if(line.startsWith("#") || line.trim().length() == 0){ //comment ^#/ empty line ^$/
continue;
}else if(line.startsWith("\t") && currentATR != null){
atrMap.get(currentATR).addDescriptiveText(line.replace("\t", "").trim());
// Log.debug("Adding descriptive text for ATR="+currentATR+" "+line.replace("\t", ""));
}else if(line.startsWith("3")){ // ATR hex
currentATR = line.toUpperCase().trim();
if(!atrMap.containsKey(currentATR)){
atrMap.put(currentATR, new PublicATR(line));
}else{
// Log.debug("Found existing ATR: "+currentATR);
}
}else{
Log.debug("Encountered unexpected line in atr list: currentATR="+currentATR+" Line("+lineNumber+")="+line);
//Just skip
//throw new RuntimeException("Encountered unexpected line in atr list: currentATR="+currentATR+" Line="+line);
}
}
}catch(IOException e){
throw new RuntimeException(e);
}finally{
if(is1 != null){
try {
is1.close();
} catch (IOException ex) {
//Ignore
}
}
if(is2 != null){
try {
is2.close();
} catch (IOException ex) {
//Ignore
}
}
if(br != null){
try {
br.close();
} catch (IOException ex) {
//Ignore
}
}
initLatch.countDown();
}
}
}).start();
}
/**
* Disable this database
* (for example on memory restricted devices)
*/
public synchronized static void disable() {
initCalled.set(true);
initLatch.countDown();
}
public static Map<String, PublicATR> getAll() {
awaitInit();
return Collections.unmodifiableMap(atrMap);
}
public static boolean awaitInit(){
if(!initCalled.get()){
throw new IllegalStateException("Not initalized. Call initialize() first");
}
try{
initLatch.await();
return true;
}catch(InterruptedException ex){
Thread.currentThread().interrupt();
}
return false;
}
public static class PublicATR{
String atr;
List<String> descriptiveText = new ArrayList<String>();
private PublicATR(String atr){
this.atr = atr; //With spaces between bytes
}
private void addDescriptiveText(String text){
descriptiveText.add(text);
}
public List<String> getDescriptiveText(){
return Collections.unmodifiableList(descriptiveText);
}
}
public static List<String> searchATR(byte[] atr){
awaitInit();
String atrStr = Util.prettyPrintHexNoWrap(atr).toUpperCase();
for(String atrPatternStr : atrMap.keySet()){
if(atrStr.matches("^"+atrPatternStr+"$")){
PublicATR publicATR = atrMap.get(atrPatternStr);
if(publicATR != null){
return publicATR.getDescriptiveText();
}
}
}
return null;
}
public static void main(String[] args){
initialize();
System.out.println(ATR_DB.searchATR(new byte[]{(byte)0x3B, (byte)0x90, (byte)0x95, (byte)0x80, (byte)0x1F, (byte)0xC3, (byte)0x59}));
System.out.println(ATR_DB.searchATR(new byte[]{(byte)0x3B, (byte)0x04, (byte)0xA2, (byte)0x13, (byte)0x10, (byte)0x91}));
System.out.println(ATR_DB.searchATR(new byte[]{(byte)0x3B, (byte)0x67, (byte)0x00, (byte)0x00, (byte)0xa6, (byte)0x40, (byte)0x40, (byte)0x00, (byte)0x09, (byte)0x90, (byte)0x00}));
System.out.println(ATR_DB.searchATR(new byte[]{(byte)0x3B, (byte)0x24, (byte)0x00, (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x45}));
System.out.println(ATR_DB.searchATR(Util.fromHexString("3F 6A 00 00 00 64 01 50 01 0C 82 01 01 A9")));
// 3b 67 00 00 a6 40 40 00 09 90 00
}
}