1 /*
2 * Copyright (c) 2005, 2006, 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
171 if (n == lc + 6) {
172 n--;
173 }
174 } else {
175 lc = ((command[5] & 0xff) << 8) | (command[6] & 0xff);
176 if (n == lc + 9) {
177 n -= 2;
178 }
179 }
180 }
181 boolean getresponse = (t0 && t0GetResponse) || (t1 && t1GetResponse);
182 int k = 0;
183 byte[] result = B0;
184 while (true) {
185 if (++k >= 32) {
186 throw new CardException("Could not obtain response");
187 }
188 byte[] response = SCardTransmit
189 (card.cardId, card.protocol, command, 0, n);
190 int rn = response.length;
191 if (getresponse && (rn >= 2)) {
192 // see ISO 7816/2005, 5.1.3
193 if ((rn == 2) && (response[0] == 0x6c)) {
194 // Resend command using SW2 as short Le field
195 command[n - 1] = response[1];
196 continue;
197 }
198 if (response[rn - 2] == 0x61) {
199 // Issue a GET RESPONSE command with the same CLA
200 // using SW2 as short Le field
201 if (rn > 2) {
202 result = concat(result, response, rn - 2);
203 }
204 command[1] = (byte)0xC0;
205 command[2] = 0;
206 command[3] = 0;
207 command[4] = response[rn - 1];
208 n = 5;
209 continue;
210 }
211
212 }
213 result = concat(result, response, rn);
214 break;
215 }
216 return result;
217 } catch (PCSCException e) {
218 card.handleError(e);
219 throw new CardException(e);
220 }
221 }
222
223 private static int getSW(byte[] res) throws CardException {
224 if (res.length < 2) {
225 throw new CardException("Invalid response length: " + res.length);
226 }
227 int sw1 = res[res.length - 2] & 0xff;
228 int sw2 = res[res.length - 1] & 0xff;
229 return (sw1 << 8) | sw2;
230 }
231
|
1 /*
2 * Copyright (c) 2005, 2018, 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
171 if (n == lc + 6) {
172 n--;
173 }
174 } else {
175 lc = ((command[5] & 0xff) << 8) | (command[6] & 0xff);
176 if (n == lc + 9) {
177 n -= 2;
178 }
179 }
180 }
181 boolean getresponse = (t0 && t0GetResponse) || (t1 && t1GetResponse);
182 int k = 0;
183 byte[] result = B0;
184 while (true) {
185 if (++k >= 32) {
186 throw new CardException("Could not obtain response");
187 }
188 byte[] response = SCardTransmit
189 (card.cardId, card.protocol, command, 0, n);
190 int rn = response.length;
191 if (getresponse && (rn >= 2) && (n >= 1)) {
192 // see ISO 7816/2005, 5.1.3
193 if ((rn == 2) && (response[0] == 0x6c)) {
194 // Resend command using SW2 as short Le field
195 command[n - 1] = response[1];
196 continue;
197 }
198 if (response[rn - 2] == 0x61) {
199 // Issue a GET RESPONSE command with the same CLA
200 // using SW2 as short Le field
201 if (rn > 2) {
202 result = concat(result, response, rn - 2);
203 }
204 if (command.length < 5) {
205 byte cla = command[0];
206 command = new byte[5];
207 command[0] = cla;
208 }
209 command[1] = (byte)0xC0;
210 command[2] = 0;
211 command[3] = 0;
212 command[4] = response[rn - 1];
213 n = 5;
214 continue;
215 }
216 }
217 result = concat(result, response, rn);
218 break;
219 }
220 return result;
221 } catch (PCSCException e) {
222 card.handleError(e);
223 throw new CardException(e);
224 }
225 }
226
227 private static int getSW(byte[] res) throws CardException {
228 if (res.length < 2) {
229 throw new CardException("Invalid response length: " + res.length);
230 }
231 int sw1 = res[res.length - 2] & 0xff;
232 int sw2 = res[res.length - 1] & 0xff;
233 return (sw1 << 8) | sw2;
234 }
235
|