Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/smartcardio/Utils.java
41149 views
1
/*
2
* Copyright (c) 2005, 2016, 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.
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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
25
// common utility functions for the PC/SC tests
26
27
import java.io.StringReader;
28
import java.io.ByteArrayOutputStream;
29
import java.io.IOException;
30
import java.security.NoSuchAlgorithmException;
31
import java.util.Arrays;
32
import java.util.List;
33
import javax.smartcardio.CardTerminal;
34
import javax.smartcardio.CardChannel;
35
import javax.smartcardio.ResponseAPDU;
36
import javax.smartcardio.CommandAPDU;
37
import javax.smartcardio.TerminalFactory;
38
39
public class Utils {
40
41
static void setLibrary(String[] args) {
42
if ((args.length > 0) && args[0].equalsIgnoreCase("MUSCLE")) {
43
System.setProperty("sun.security.smartcardio.library", "/usr/local/$LIBISA/libpcsclite.so");
44
}
45
}
46
47
static TerminalFactory getTerminalFactory(String provName) throws Exception {
48
try {
49
TerminalFactory factory = (provName == null)
50
? TerminalFactory.getInstance("PC/SC", null)
51
: TerminalFactory.getInstance("PC/SC", null, provName);
52
System.out.println(factory);
53
return factory;
54
} catch (NoSuchAlgorithmException e) {
55
Throwable cause = e.getCause();
56
if (cause != null && cause.getMessage().startsWith("PC/SC not available")) {
57
return null;
58
}
59
throw e;
60
}
61
}
62
63
static CardTerminal getTerminal(String[] args) throws Exception {
64
return getTerminal(args, null);
65
}
66
67
static CardTerminal getTerminal(String[] args, String provider) throws Exception {
68
setLibrary(args);
69
70
try {
71
TerminalFactory factory = (provider == null)
72
? TerminalFactory.getInstance("PC/SC", null)
73
: TerminalFactory.getInstance("PC/SC", null, provider);
74
System.out.println(factory);
75
76
List<CardTerminal> terminals = factory.terminals().list();
77
System.out.println("Terminals: " + terminals);
78
if (terminals.isEmpty()) {
79
return null;
80
}
81
CardTerminal terminal = terminals.get(0);
82
83
if (terminal.isCardPresent() == false) {
84
System.out.println("*** Insert card");
85
if (terminal.waitForCardPresent(20 * 1000) == false) {
86
throw new Exception("no card available");
87
}
88
}
89
System.out.println("card present: " + terminal.isCardPresent());
90
91
return terminal;
92
93
} catch (NoSuchAlgorithmException e) {
94
Throwable cause = e.getCause();
95
if (cause != null && cause.getMessage().startsWith("PC/SC not available")) {
96
return null;
97
}
98
throw e;
99
}
100
}
101
102
static final byte[] C1 = parse("00 A4 04 00 07 A0 00 00 00 62 81 01 00");
103
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");
104
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");
105
106
static void transmitTestCommand(CardChannel channel) throws Exception {
107
ResponseAPDU r = channel.transmit(new CommandAPDU(C1));
108
byte[] rb = r.getBytes();
109
if ((Arrays.equals(rb, R1a) == false) && (Arrays.equals(rb, R1b) == false)) {
110
System.out.println("expected: " + toString(R1a));
111
System.out.println("received: " + toString(rb));
112
throw new Exception("Response does not match");
113
}
114
}
115
116
private final static char[] hexDigits = "0123456789abcdef".toCharArray();
117
118
public static String toString(byte[] b) {
119
StringBuffer sb = new StringBuffer(b.length * 3);
120
for (int i = 0; i < b.length; i++) {
121
int k = b[i] & 0xff;
122
if (i != 0) {
123
sb.append(':');
124
}
125
sb.append(hexDigits[k >>> 4]);
126
sb.append(hexDigits[k & 0xf]);
127
}
128
return sb.toString();
129
}
130
131
public static byte[] parse(String s) {
132
try {
133
int n = s.length();
134
ByteArrayOutputStream out = new ByteArrayOutputStream(n >> 1);
135
StringReader r = new StringReader(s);
136
while (true) {
137
int b1 = nextNibble(r);
138
if (b1 < 0) {
139
break;
140
}
141
int b2 = nextNibble(r);
142
if (b2 < 0) {
143
throw new RuntimeException("Invalid string " + s);
144
}
145
int b = (b1 << 4) | b2;
146
out.write(b);
147
}
148
return out.toByteArray();
149
} catch (IOException e) {
150
throw new RuntimeException(e);
151
}
152
}
153
154
private static int nextNibble(StringReader r) throws IOException {
155
while (true) {
156
int ch = r.read();
157
if (ch == -1) {
158
return -1;
159
} else if ((ch >= '0') && (ch <= '9')) {
160
return ch - '0';
161
} else if ((ch >= 'a') && (ch <= 'f')) {
162
return ch - 'a' + 10;
163
} else if ((ch >= 'A') && (ch <= 'F')) {
164
return ch - 'A' + 10;
165
}
166
}
167
}
168
169
}
170
171