Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/smartcardio/TestTransmit.java
38840 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
* @test
26
* @bug 6293769 6294527
27
* @summary test transmit() works
28
* @author Andreas Sterbenz
29
* @ignore requires special hardware
30
* @run main/manual TestTransmit
31
*/
32
33
import java.io.BufferedReader;
34
import java.io.ByteArrayOutputStream;
35
import java.io.FileReader;
36
import java.io.IOException;
37
import java.io.StringReader;
38
import javax.smartcardio.Card;
39
import javax.smartcardio.CardChannel;
40
import javax.smartcardio.CardTerminal;
41
import javax.smartcardio.CommandAPDU;
42
import javax.smartcardio.ResponseAPDU;
43
44
public class TestTransmit extends Utils {
45
46
private final static String CMD_MARKER = "C-APDU: ";
47
private final static String RES_MARKER = "R-APDU: ";
48
49
public static void main(String[] args) throws Exception {
50
CardTerminal terminal = getTerminal(args);
51
if (terminal == null) {
52
System.out.println("Skipping the test: " +
53
"no card terminals available");
54
return;
55
}
56
57
Card card = terminal.connect("T=0");
58
CardChannel channel = card.getBasicChannel();
59
60
BufferedReader reader = new BufferedReader(new FileReader("apdu.log"));
61
62
byte[] command = null;
63
while (true) {
64
String line = reader.readLine();
65
if (line == null) {
66
break;
67
}
68
if (line.startsWith(CMD_MARKER)) {
69
System.out.println(line);
70
line = line.substring(CMD_MARKER.length());
71
command = parse(line);
72
} else if (line.startsWith(RES_MARKER)) {
73
System.out.println(line);
74
line = line.substring(RES_MARKER.length());
75
Bytes response = parseWildcard(line);
76
CommandAPDU capdu = new CommandAPDU(command);
77
ResponseAPDU rapdu = channel.transmit(capdu);
78
byte[] received = rapdu.getBytes();
79
if (received.length != response.bytes.length) {
80
throw new Exception("Length mismatch: " + toString(received));
81
}
82
for (int i = 0; i < received.length; i++) {
83
byte mask = response.mask[i];
84
if ((received[i] & response.mask[i]) != response.bytes[i]) {
85
throw new Exception("Mismatch: " + toString(received));
86
}
87
}
88
} // else ignore
89
}
90
91
// disconnect
92
card.disconnect(true);
93
94
System.out.println("OK.");
95
}
96
97
private static class Bytes {
98
final byte[] bytes;
99
final byte[] mask;
100
Bytes(byte[] bytes, byte[] mask) {
101
this.bytes = bytes;
102
this.mask = mask;
103
}
104
}
105
106
private static Bytes parseWildcard(String s) {
107
try {
108
int n = s.length();
109
ByteArrayOutputStream out = new ByteArrayOutputStream(n >> 1);
110
ByteArrayOutputStream mask = new ByteArrayOutputStream(n >> 1);
111
StringReader r = new StringReader(s);
112
while (true) {
113
int b1 = nextNibble(r);
114
if (b1 < 0) {
115
if (b1 == -1) {
116
break;
117
}
118
int b2 = nextNibble(r);
119
if (b2 != -2) {
120
throw new RuntimeException("Invalid wildcard: " + s);
121
}
122
out.write(0);
123
mask.write(0);
124
continue;
125
}
126
int b2 = nextNibble(r);
127
if (b2 < 0) {
128
throw new RuntimeException("Invalid string " + s);
129
}
130
int b = (b1 << 4) | b2;
131
out.write(b);
132
mask.write(0xff);
133
}
134
byte[] b = out.toByteArray();
135
byte[] m = mask.toByteArray();
136
return new Bytes(b, m);
137
} catch (IOException e) {
138
throw new RuntimeException(e);
139
}
140
}
141
142
private static int nextNibble(StringReader r) throws IOException {
143
while (true) {
144
int ch = r.read();
145
if (ch == -1) {
146
return -1;
147
} else if ((ch >= '0') && (ch <= '9')) {
148
return ch - '0';
149
} else if ((ch >= 'a') && (ch <= 'f')) {
150
return ch - 'a' + 10;
151
} else if ((ch >= 'A') && (ch <= 'F')) {
152
return ch - 'A' + 10;
153
} else if (ch == 'X') {
154
return -2;
155
}
156
}
157
}
158
159
}
160
161