blob: 34e6d60388d8539d62b1c2161cd8463e8ad41505 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005-2006 Sun Microsystems, Inc. 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24
25// common utility functions for the PC/SC tests
26
27import javax.smartcardio.*;
28
29import java.io.*;
30import java.util.*;
31
32public class Utils {
33
34 static void setLibrary(String[] args) {
35 if ((args.length > 0) && args[0].equalsIgnoreCase("MUSCLE")) {
36 System.setProperty("sun.security.smartcardio.library", "/usr/local/$LIBISA/libpcsclite.so");
37 }
38 }
39
40 static CardTerminal getTerminal(String[] args) throws Exception {
41 setLibrary(args);
42
43 TerminalFactory factory = TerminalFactory.getInstance("PC/SC", null);
44 System.out.println(factory);
45
46 List<CardTerminal> terminals = factory.terminals().list();
47 System.out.println("Terminals: " + terminals);
48 if (terminals.isEmpty()) {
49 throw new Exception("No card terminals available");
50 }
51 CardTerminal terminal = terminals.get(0);
52
53 if (terminal.isCardPresent() == false) {
54 System.out.println("*** Insert card");
55 if (terminal.waitForCardPresent(20 * 1000) == false) {
56 throw new Exception("no card available");
57 }
58 }
59 return terminal;
60 }
61
62 static final byte[] C1 = parse("00 A4 04 00 07 A0 00 00 00 62 81 01 00");
63 static final byte[] R1a = parse("07 A0 00 00 00 62 81 01 04 01 00 00 24 05 00 0B 04 B0 25 90 00");
64 static final byte[] R1b = parse("07 A0 00 00 00 62 81 01 04 01 00 00 24 05 00 0B 04 B0 55 90 00");
65
66 static void transmitTestCommand(CardChannel channel) throws Exception {
67 ResponseAPDU r = channel.transmit(new CommandAPDU(C1));
68 byte[] rb = r.getBytes();
69 if ((Arrays.equals(rb, R1a) == false) && (Arrays.equals(rb, R1b) == false)) {
70 System.out.println("expected: " + toString(R1a));
71 System.out.println("received: " + toString(rb));
72 throw new Exception("Response does not match");
73 }
74 }
75
76 private final static char[] hexDigits = "0123456789abcdef".toCharArray();
77
78 public static String toString(byte[] b) {
79 StringBuffer sb = new StringBuffer(b.length * 3);
80 for (int i = 0; i < b.length; i++) {
81 int k = b[i] & 0xff;
82 if (i != 0) {
83 sb.append(':');
84 }
85 sb.append(hexDigits[k >>> 4]);
86 sb.append(hexDigits[k & 0xf]);
87 }
88 return sb.toString();
89 }
90
91 public static byte[] parse(String s) {
92 try {
93 int n = s.length();
94 ByteArrayOutputStream out = new ByteArrayOutputStream(n >> 1);
95 StringReader r = new StringReader(s);
96 while (true) {
97 int b1 = nextNibble(r);
98 if (b1 < 0) {
99 break;
100 }
101 int b2 = nextNibble(r);
102 if (b2 < 0) {
103 throw new RuntimeException("Invalid string " + s);
104 }
105 int b = (b1 << 4) | b2;
106 out.write(b);
107 }
108 return out.toByteArray();
109 } catch (IOException e) {
110 throw new RuntimeException(e);
111 }
112 }
113
114 private static int nextNibble(StringReader r) throws IOException {
115 while (true) {
116 int ch = r.read();
117 if (ch == -1) {
118 return -1;
119 } else if ((ch >= '0') && (ch <= '9')) {
120 return ch - '0';
121 } else if ((ch >= 'a') && (ch <= 'f')) {
122 return ch - 'a' + 10;
123 } else if ((ch >= 'A') && (ch <= 'F')) {
124 return ch - 'A' + 10;
125 }
126 }
127 }
128
129}