2
|
1 |
/*
|
5301
|
2 |
* Copyright 2005-2010 Sun Microsystems, Inc. All Rights Reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.security.smartcardio;
|
|
27 |
|
|
28 |
import java.nio.ByteBuffer;
|
|
29 |
|
|
30 |
import javax.smartcardio.*;
|
|
31 |
|
|
32 |
import static sun.security.smartcardio.PCSC.*;
|
|
33 |
|
|
34 |
/**
|
|
35 |
* Card implementation.
|
|
36 |
*
|
|
37 |
* @since 1.6
|
|
38 |
* @author Andreas Sterbenz
|
|
39 |
*/
|
|
40 |
final class CardImpl extends Card {
|
|
41 |
|
|
42 |
private static enum State { OK, REMOVED, DISCONNECTED };
|
|
43 |
|
|
44 |
// the terminal that created this card
|
|
45 |
private final TerminalImpl terminal;
|
|
46 |
|
|
47 |
// the native SCARDHANDLE
|
|
48 |
final long cardId;
|
|
49 |
|
|
50 |
// atr of this card
|
|
51 |
private final ATR atr;
|
|
52 |
|
|
53 |
// protocol in use, one of SCARD_PROTOCOL_T0 and SCARD_PROTOCOL_T1
|
|
54 |
final int protocol;
|
|
55 |
|
|
56 |
// the basic logical channel (channel 0)
|
|
57 |
private final ChannelImpl basicChannel;
|
|
58 |
|
|
59 |
// state of this card connection
|
|
60 |
private volatile State state;
|
|
61 |
|
|
62 |
// thread holding exclusive access to the card, or null
|
|
63 |
private volatile Thread exclusiveThread;
|
|
64 |
|
|
65 |
CardImpl(TerminalImpl terminal, String protocol) throws PCSCException {
|
|
66 |
this.terminal = terminal;
|
|
67 |
int sharingMode = SCARD_SHARE_SHARED;
|
|
68 |
int connectProtocol;
|
|
69 |
if (protocol.equals("*")) {
|
|
70 |
connectProtocol = SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1;
|
|
71 |
} else if (protocol.equalsIgnoreCase("T=0")) {
|
|
72 |
connectProtocol = SCARD_PROTOCOL_T0;
|
|
73 |
} else if (protocol.equalsIgnoreCase("T=1")) {
|
|
74 |
connectProtocol = SCARD_PROTOCOL_T1;
|
|
75 |
} else if (protocol.equalsIgnoreCase("direct")) {
|
|
76 |
// testing
|
|
77 |
connectProtocol = 0;
|
|
78 |
sharingMode = SCARD_SHARE_DIRECT;
|
|
79 |
} else {
|
|
80 |
throw new IllegalArgumentException("Unsupported protocol " + protocol);
|
|
81 |
}
|
|
82 |
cardId = SCardConnect(terminal.contextId, terminal.name,
|
|
83 |
sharingMode, connectProtocol);
|
|
84 |
byte[] status = new byte[2];
|
|
85 |
byte[] atrBytes = SCardStatus(cardId, status);
|
|
86 |
atr = new ATR(atrBytes);
|
|
87 |
this.protocol = status[1] & 0xff;
|
|
88 |
basicChannel = new ChannelImpl(this, 0);
|
|
89 |
state = State.OK;
|
|
90 |
}
|
|
91 |
|
|
92 |
void checkState() {
|
|
93 |
State s = state;
|
|
94 |
if (s == State.DISCONNECTED) {
|
|
95 |
throw new IllegalStateException("Card has been disconnected");
|
|
96 |
} else if (s == State.REMOVED) {
|
|
97 |
throw new IllegalStateException("Card has been removed");
|
|
98 |
}
|
|
99 |
}
|
|
100 |
|
|
101 |
boolean isValid() {
|
|
102 |
if (state != State.OK) {
|
|
103 |
return false;
|
|
104 |
}
|
|
105 |
// ping card via SCardStatus
|
|
106 |
try {
|
|
107 |
SCardStatus(cardId, new byte[2]);
|
|
108 |
return true;
|
|
109 |
} catch (PCSCException e) {
|
|
110 |
state = State.REMOVED;
|
|
111 |
return false;
|
|
112 |
}
|
|
113 |
}
|
|
114 |
|
|
115 |
private void checkSecurity(String action) {
|
|
116 |
SecurityManager sm = System.getSecurityManager();
|
|
117 |
if (sm != null) {
|
|
118 |
sm.checkPermission(new CardPermission(terminal.name, action));
|
|
119 |
}
|
|
120 |
}
|
|
121 |
|
|
122 |
void handleError(PCSCException e) {
|
|
123 |
if (e.code == SCARD_W_REMOVED_CARD) {
|
|
124 |
state = State.REMOVED;
|
|
125 |
}
|
|
126 |
}
|
|
127 |
|
|
128 |
public ATR getATR() {
|
|
129 |
return atr;
|
|
130 |
}
|
|
131 |
|
|
132 |
public String getProtocol() {
|
|
133 |
switch (protocol) {
|
|
134 |
case SCARD_PROTOCOL_T0:
|
|
135 |
return "T=0";
|
|
136 |
case SCARD_PROTOCOL_T1:
|
|
137 |
return "T=1";
|
|
138 |
default:
|
|
139 |
// should never occur
|
|
140 |
return "Unknown protocol " + protocol;
|
|
141 |
}
|
|
142 |
}
|
|
143 |
|
|
144 |
public CardChannel getBasicChannel() {
|
|
145 |
checkSecurity("getBasicChannel");
|
|
146 |
checkState();
|
|
147 |
return basicChannel;
|
|
148 |
}
|
|
149 |
|
|
150 |
private static int getSW(byte[] b) {
|
|
151 |
if (b.length < 2) {
|
|
152 |
return -1;
|
|
153 |
}
|
|
154 |
int sw1 = b[b.length - 2] & 0xff;
|
|
155 |
int sw2 = b[b.length - 1] & 0xff;
|
|
156 |
return (sw1 << 8) | sw2;
|
|
157 |
}
|
|
158 |
|
|
159 |
private static byte[] commandOpenChannel = new byte[] {0, 0x70, 0, 0, 1};
|
|
160 |
|
|
161 |
public CardChannel openLogicalChannel() throws CardException {
|
|
162 |
checkSecurity("openLogicalChannel");
|
|
163 |
checkState();
|
|
164 |
checkExclusive();
|
|
165 |
try {
|
|
166 |
byte[] response = SCardTransmit
|
|
167 |
(cardId, protocol, commandOpenChannel, 0, commandOpenChannel.length);
|
|
168 |
if ((response.length != 3) || (getSW(response) != 0x9000)) {
|
|
169 |
throw new CardException
|
|
170 |
("openLogicalChannel() failed, card response: "
|
|
171 |
+ PCSC.toString(response));
|
|
172 |
}
|
|
173 |
return new ChannelImpl(this, response[0]);
|
|
174 |
} catch (PCSCException e) {
|
|
175 |
handleError(e);
|
|
176 |
throw new CardException("openLogicalChannel() failed", e);
|
|
177 |
}
|
|
178 |
}
|
|
179 |
|
|
180 |
void checkExclusive() throws CardException {
|
|
181 |
Thread t = exclusiveThread;
|
|
182 |
if (t == null) {
|
|
183 |
return;
|
|
184 |
}
|
|
185 |
if (t != Thread.currentThread()) {
|
|
186 |
throw new CardException("Exclusive access established by another Thread");
|
|
187 |
}
|
|
188 |
}
|
|
189 |
|
|
190 |
public synchronized void beginExclusive() throws CardException {
|
|
191 |
checkSecurity("exclusive");
|
|
192 |
checkState();
|
|
193 |
if (exclusiveThread != null) {
|
|
194 |
throw new CardException
|
|
195 |
("Exclusive access has already been assigned to Thread "
|
|
196 |
+ exclusiveThread.getName());
|
|
197 |
}
|
|
198 |
try {
|
|
199 |
SCardBeginTransaction(cardId);
|
|
200 |
} catch (PCSCException e) {
|
|
201 |
handleError(e);
|
|
202 |
throw new CardException("beginExclusive() failed", e);
|
|
203 |
}
|
|
204 |
exclusiveThread = Thread.currentThread();
|
|
205 |
}
|
|
206 |
|
|
207 |
public synchronized void endExclusive() throws CardException {
|
|
208 |
checkState();
|
|
209 |
if (exclusiveThread != Thread.currentThread()) {
|
|
210 |
throw new IllegalStateException
|
|
211 |
("Exclusive access not assigned to current Thread");
|
|
212 |
}
|
|
213 |
try {
|
|
214 |
SCardEndTransaction(cardId, SCARD_LEAVE_CARD);
|
|
215 |
} catch (PCSCException e) {
|
|
216 |
handleError(e);
|
5301
|
217 |
throw new CardException("endExclusive() failed", e);
|
2
|
218 |
} finally {
|
|
219 |
exclusiveThread = null;
|
|
220 |
}
|
|
221 |
}
|
|
222 |
|
|
223 |
public byte[] transmitControlCommand(int controlCode, byte[] command)
|
|
224 |
throws CardException {
|
|
225 |
checkSecurity("transmitControl");
|
|
226 |
checkState();
|
|
227 |
checkExclusive();
|
|
228 |
if (command == null) {
|
|
229 |
throw new NullPointerException();
|
|
230 |
}
|
|
231 |
try {
|
|
232 |
byte[] r = SCardControl(cardId, controlCode, command);
|
|
233 |
return r;
|
|
234 |
} catch (PCSCException e) {
|
|
235 |
handleError(e);
|
|
236 |
throw new CardException("transmitControlCommand() failed", e);
|
|
237 |
}
|
|
238 |
}
|
|
239 |
|
|
240 |
public void disconnect(boolean reset) throws CardException {
|
|
241 |
if (reset) {
|
|
242 |
checkSecurity("reset");
|
|
243 |
}
|
|
244 |
if (state != State.OK) {
|
|
245 |
return;
|
|
246 |
}
|
|
247 |
checkExclusive();
|
|
248 |
try {
|
|
249 |
SCardDisconnect(cardId, (reset ? SCARD_LEAVE_CARD : SCARD_RESET_CARD));
|
|
250 |
} catch (PCSCException e) {
|
|
251 |
throw new CardException("disconnect() failed", e);
|
|
252 |
} finally {
|
|
253 |
state = State.DISCONNECTED;
|
|
254 |
exclusiveThread = null;
|
|
255 |
}
|
|
256 |
}
|
|
257 |
|
|
258 |
public String toString() {
|
|
259 |
return "PC/SC card in " + terminal.getName()
|
|
260 |
+ ", protocol " + getProtocol() + ", state " + state;
|
|
261 |
}
|
|
262 |
|
|
263 |
protected void finalize() throws Throwable {
|
|
264 |
try {
|
|
265 |
if (state == State.OK) {
|
|
266 |
SCardDisconnect(cardId, SCARD_LEAVE_CARD);
|
|
267 |
}
|
|
268 |
} finally {
|
|
269 |
super.finalize();
|
|
270 |
}
|
|
271 |
}
|
|
272 |
|
|
273 |
}
|