Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/smartcardio/ChannelImpl.java
38829 views
1
/*
2
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.security.smartcardio;
27
28
import java.nio.*;
29
import java.security.AccessController;
30
31
import javax.smartcardio.*;
32
33
import static sun.security.smartcardio.PCSC.*;
34
35
import sun.security.action.GetPropertyAction;
36
37
/**
38
* CardChannel implementation.
39
*
40
* @since 1.6
41
* @author Andreas Sterbenz
42
*/
43
final class ChannelImpl extends CardChannel {
44
45
// the card this channel is associated with
46
private final CardImpl card;
47
48
// the channel number, 0 for the basic logical channel
49
private final int channel;
50
51
// whether this channel has been closed. only logical channels can be closed
52
private volatile boolean isClosed;
53
54
ChannelImpl(CardImpl card, int channel) {
55
this.card = card;
56
this.channel = channel;
57
}
58
59
void checkClosed() {
60
card.checkState();
61
if (isClosed) {
62
throw new IllegalStateException("Logical channel has been closed");
63
}
64
}
65
66
public Card getCard() {
67
return card;
68
}
69
70
public int getChannelNumber() {
71
checkClosed();
72
return channel;
73
}
74
75
private static void checkManageChannel(byte[] b) {
76
if (b.length < 4) {
77
throw new IllegalArgumentException
78
("Command APDU must be at least 4 bytes long");
79
}
80
if ((b[0] >= 0) && (b[1] == 0x70)) {
81
throw new IllegalArgumentException
82
("Manage channel command not allowed, use openLogicalChannel()");
83
}
84
}
85
86
public ResponseAPDU transmit(CommandAPDU command) throws CardException {
87
checkClosed();
88
card.checkExclusive();
89
byte[] commandBytes = command.getBytes();
90
byte[] responseBytes = doTransmit(commandBytes);
91
return new ResponseAPDU(responseBytes);
92
}
93
94
public int transmit(ByteBuffer command, ByteBuffer response) throws CardException {
95
checkClosed();
96
card.checkExclusive();
97
if ((command == null) || (response == null)) {
98
throw new NullPointerException();
99
}
100
if (response.isReadOnly()) {
101
throw new ReadOnlyBufferException();
102
}
103
if (command == response) {
104
throw new IllegalArgumentException
105
("command and response must not be the same object");
106
}
107
if (response.remaining() < 258) {
108
throw new IllegalArgumentException
109
("Insufficient space in response buffer");
110
}
111
byte[] commandBytes = new byte[command.remaining()];
112
command.get(commandBytes);
113
byte[] responseBytes = doTransmit(commandBytes);
114
response.put(responseBytes);
115
return responseBytes.length;
116
}
117
118
private final static boolean t0GetResponse =
119
getBooleanProperty("sun.security.smartcardio.t0GetResponse", true);
120
121
private final static boolean t1GetResponse =
122
getBooleanProperty("sun.security.smartcardio.t1GetResponse", true);
123
124
private final static boolean t1StripLe =
125
getBooleanProperty("sun.security.smartcardio.t1StripLe", false);
126
127
private static boolean getBooleanProperty(String name, boolean def) {
128
String val = AccessController.doPrivileged(new GetPropertyAction(name));
129
if (val == null) {
130
return def;
131
}
132
if (val.equalsIgnoreCase("true")) {
133
return true;
134
} else if (val.equalsIgnoreCase("false")) {
135
return false;
136
} else {
137
throw new IllegalArgumentException
138
(name + " must be either 'true' or 'false'");
139
}
140
}
141
142
private byte[] concat(byte[] b1, byte[] b2, int n2) {
143
int n1 = b1.length;
144
if ((n1 == 0) && (n2 == b2.length)) {
145
return b2;
146
}
147
byte[] res = new byte[n1 + n2];
148
System.arraycopy(b1, 0, res, 0, n1);
149
System.arraycopy(b2, 0, res, n1, n2);
150
return res;
151
}
152
153
private final static int RESPONSE_ITERATIONS = 256;
154
private final static byte[] B0 = new byte[0];
155
156
private byte[] doTransmit(byte[] command) throws CardException {
157
// note that we modify the 'command' array in some cases, so it must
158
// be a copy of the application provided data.
159
try {
160
checkManageChannel(command);
161
setChannel(command);
162
int n = command.length;
163
boolean t0 = card.protocol == SCARD_PROTOCOL_T0;
164
boolean t1 = card.protocol == SCARD_PROTOCOL_T1;
165
if (t0 && (n >= 7) && (command[4] == 0)) {
166
throw new CardException
167
("Extended length forms not supported for T=0");
168
}
169
if ((t0 || (t1 && t1StripLe)) && (n >= 7)) {
170
int lc = command[4] & 0xff;
171
if (lc != 0) {
172
if (n == lc + 6) {
173
n--;
174
}
175
} else {
176
lc = ((command[5] & 0xff) << 8) | (command[6] & 0xff);
177
if (n == lc + 9) {
178
n -= 2;
179
}
180
}
181
}
182
boolean getresponse = (t0 && t0GetResponse) || (t1 && t1GetResponse);
183
int k = 0;
184
byte[] result = B0;
185
while (true) {
186
if (++k > RESPONSE_ITERATIONS) {
187
throw new CardException("Number of response iterations" +
188
" exceeded maximum " + RESPONSE_ITERATIONS);
189
}
190
byte[] response = SCardTransmit
191
(card.cardId, card.protocol, command, 0, n);
192
int rn = response.length;
193
if (getresponse && (rn >= 2)) {
194
// see ISO 7816/2005, 5.1.3
195
if ((rn == 2) && (response[0] == 0x6c)) {
196
// Resend command using SW2 as short Le field
197
command[n - 1] = response[1];
198
continue;
199
}
200
if (response[rn - 2] == 0x61) {
201
// Issue a GET RESPONSE command with the same CLA
202
// using SW2 as short Le field
203
if (rn > 2) {
204
result = concat(result, response, rn - 2);
205
}
206
command[1] = (byte)0xC0;
207
command[2] = 0;
208
command[3] = 0;
209
command[4] = response[rn - 1];
210
n = 5;
211
continue;
212
}
213
214
}
215
result = concat(result, response, rn);
216
break;
217
}
218
return result;
219
} catch (PCSCException e) {
220
card.handleError(e);
221
throw new CardException(e);
222
}
223
}
224
225
private static int getSW(byte[] res) throws CardException {
226
if (res.length < 2) {
227
throw new CardException("Invalid response length: " + res.length);
228
}
229
int sw1 = res[res.length - 2] & 0xff;
230
int sw2 = res[res.length - 1] & 0xff;
231
return (sw1 << 8) | sw2;
232
}
233
234
private static boolean isOK(byte[] res) throws CardException {
235
return (res.length == 2) && (getSW(res) == 0x9000);
236
}
237
238
private void setChannel(byte[] com) {
239
int cla = com[0];
240
if (cla < 0) {
241
// proprietary class format, cannot set or check logical channel
242
// for now, just return
243
return;
244
}
245
// classes 001x xxxx is reserved for future use in ISO, ignore
246
if ((cla & 0xe0) == 0x20) {
247
return;
248
}
249
// see ISO 7816/2005, table 2 and 3
250
if (channel <= 3) {
251
// mask of bits 7, 1, 0 (channel number)
252
// 0xbc == 1011 1100
253
com[0] &= 0xbc;
254
com[0] |= channel;
255
} else if (channel <= 19) {
256
// mask of bits 7, 3, 2, 1, 0 (channel number)
257
// 0xbc == 1011 0000
258
com[0] &= 0xb0;
259
com[0] |= 0x40;
260
com[0] |= (channel - 4);
261
} else {
262
throw new RuntimeException("Unsupported channel number: " + channel);
263
}
264
}
265
266
public void close() throws CardException {
267
if (getChannelNumber() == 0) {
268
throw new IllegalStateException("Cannot close basic logical channel");
269
}
270
if (isClosed) {
271
return;
272
}
273
card.checkExclusive();
274
try {
275
byte[] com = new byte[] {0x00, 0x70, (byte)0x80, 0};
276
com[3] = (byte)getChannelNumber();
277
setChannel(com);
278
byte[] res = SCardTransmit(card.cardId, card.protocol, com, 0, com.length);
279
if (isOK(res) == false) {
280
throw new CardException("close() failed: " + PCSC.toString(res));
281
}
282
} catch (PCSCException e) {
283
card.handleError(e);
284
throw new CardException("Could not close channel", e);
285
} finally {
286
isClosed = true;
287
}
288
}
289
290
public String toString() {
291
return "PC/SC channel " + channel;
292
}
293
294
}
295
296