Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.smartcardio/share/classes/sun/security/smartcardio/TerminalImpl.java
41159 views
1
/*
2
* Copyright (c) 2005, 2021, 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.util.*;
29
30
import javax.smartcardio.*;
31
32
import static sun.security.smartcardio.PCSC.*;
33
34
/**
35
* CardTerminal implementation.
36
*
37
* @since 1.6
38
* @author Andreas Sterbenz
39
*/
40
final class TerminalImpl extends CardTerminal {
41
42
// native SCARDCONTEXT
43
final long contextId;
44
45
// the name of this terminal (native PC/SC name)
46
final String name;
47
48
private CardImpl card;
49
50
TerminalImpl(long contextId, String name) {
51
this.contextId = contextId;
52
this.name = name;
53
}
54
55
public String getName() {
56
return name;
57
}
58
59
public synchronized Card connect(String protocol) throws CardException {
60
@SuppressWarnings("removal")
61
SecurityManager sm = System.getSecurityManager();
62
if (sm != null) {
63
sm.checkPermission(new CardPermission(name, "connect"));
64
}
65
if (card != null) {
66
if (card.isValid()) {
67
String cardProto = card.getProtocol();
68
if (protocol.equals("*") || protocol.equalsIgnoreCase(cardProto)) {
69
return card;
70
} else {
71
throw new CardException("Cannot connect using " + protocol
72
+ ", connection already established using " + cardProto);
73
}
74
} else {
75
card = null;
76
}
77
}
78
try {
79
card = new CardImpl(this, protocol);
80
return card;
81
} catch (PCSCException e) {
82
if (e.code == SCARD_W_REMOVED_CARD || e.code == SCARD_E_NO_SMARTCARD) {
83
throw new CardNotPresentException("No card present", e);
84
} else {
85
throw new CardException("connect() failed", e);
86
}
87
}
88
}
89
90
public boolean isCardPresent() throws CardException {
91
try {
92
int[] status = SCardGetStatusChange(contextId, 0,
93
new int[] {SCARD_STATE_UNAWARE}, new String[] {name});
94
return (status[0] & SCARD_STATE_PRESENT) != 0;
95
} catch (PCSCException e) {
96
throw new CardException("isCardPresent() failed", e);
97
}
98
}
99
100
private boolean waitForCard(boolean wantPresent, long timeout) throws CardException {
101
if (timeout < 0) {
102
throw new IllegalArgumentException("timeout must not be negative");
103
}
104
if (timeout == 0) {
105
timeout = TIMEOUT_INFINITE;
106
}
107
int[] status = new int[] {SCARD_STATE_UNAWARE};
108
String[] readers = new String[] {name};
109
try {
110
// check if card status already matches
111
status = SCardGetStatusChange(contextId, 0, status, readers);
112
boolean present = (status[0] & SCARD_STATE_PRESENT) != 0;
113
if (wantPresent == present) {
114
return true;
115
}
116
// no match, wait (until timeout expires)
117
long end = System.currentTimeMillis() + timeout;
118
while (wantPresent != present && timeout != 0) {
119
// set remaining timeout
120
if (timeout != TIMEOUT_INFINITE) {
121
timeout = Math.max(end - System.currentTimeMillis(), 0l);
122
}
123
status = SCardGetStatusChange(contextId, timeout, status, readers);
124
present = (status[0] & SCARD_STATE_PRESENT) != 0;
125
}
126
return wantPresent == present;
127
} catch (PCSCException e) {
128
if (e.code == SCARD_E_TIMEOUT) {
129
return false;
130
} else {
131
throw new CardException("waitForCard() failed", e);
132
}
133
}
134
}
135
136
public boolean waitForCardPresent(long timeout) throws CardException {
137
return waitForCard(true, timeout);
138
}
139
140
public boolean waitForCardAbsent(long timeout) throws CardException {
141
return waitForCard(false, timeout);
142
}
143
144
public String toString() {
145
return "PC/SC terminal " + name;
146
}
147
}
148
149