Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/Session.java
41154 views
1
/*
2
* Copyright (c) 2003, 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.pkcs11;
27
28
import java.lang.ref.*;
29
import java.util.*;
30
import java.util.concurrent.atomic.AtomicInteger;
31
32
import java.security.*;
33
34
import sun.security.pkcs11.wrapper.*;
35
36
/**
37
* A session object. Sessions are obtained via the SessionManager,
38
* see there for details. Most code will only ever need one method in
39
* this class, the id() method to obtain the session id.
40
*
41
* @author Andreas Sterbenz
42
* @since 1.5
43
*/
44
final class Session implements Comparable<Session> {
45
46
// time after which to close idle sessions, in milliseconds (3 minutes)
47
private static final long MAX_IDLE_TIME = 3 * 60 * 1000;
48
49
// token instance
50
final Token token;
51
52
// session id
53
private final long id;
54
55
// number of objects created within this session
56
private final AtomicInteger createdObjects;
57
58
// time this session was last used
59
// not synchronized/volatile for performance, so may be unreliable
60
// this could lead to idle sessions being closed early, but that is harmless
61
private long lastAccess;
62
63
private final SessionRef sessionRef;
64
65
Session(Token token, long id) {
66
this.token = token;
67
this.id = id;
68
createdObjects = new AtomicInteger();
69
id();
70
sessionRef = new SessionRef(this, id, token);
71
}
72
73
public int compareTo(Session other) {
74
if (this.lastAccess == other.lastAccess) {
75
return 0;
76
} else {
77
return (this.lastAccess < other.lastAccess) ? -1 : 1;
78
}
79
}
80
81
boolean isLive(long currentTime) {
82
return currentTime - lastAccess < MAX_IDLE_TIME;
83
}
84
85
long id() {
86
if (token.isPresent(this.id) == false) {
87
throw new ProviderException("Token has been removed");
88
}
89
lastAccess = System.currentTimeMillis();
90
return id;
91
}
92
93
void addObject() {
94
int n = createdObjects.incrementAndGet();
95
// XXX update statistics in session manager if n == 1
96
}
97
98
void removeObject() {
99
int n = createdObjects.decrementAndGet();
100
if (n == 0) {
101
token.sessionManager.demoteObjSession(this);
102
} else if (n < 0) {
103
throw new ProviderException("Internal error: objects created " + n);
104
}
105
}
106
107
boolean hasObjects() {
108
return createdObjects.get() != 0;
109
}
110
111
// regular close which will not close sessions when there are objects(keys)
112
// still associated with them
113
void close() {
114
close(true);
115
}
116
117
// forced close which will close sessions regardless if there are objects
118
// associated with them. Note that closing the sessions this way may
119
// lead to those associated objects(keys) un-usable. Thus should only be
120
// used for scenarios such as the token is about to be removed, etc.
121
void kill() {
122
close(false);
123
}
124
125
private void close(boolean checkObjCtr) {
126
if (hasObjects() && checkObjCtr) {
127
throw new ProviderException(
128
"Internal error: close session with active objects");
129
}
130
sessionRef.dispose();
131
}
132
133
// Called by the NativeResourceCleaner at specified intervals
134
// See NativeResourceCleaner for more information
135
static boolean drainRefQueue() {
136
boolean found = false;
137
SessionRef next;
138
while ((next = (SessionRef) SessionRef.refQueue.poll())!= null) {
139
found = true;
140
next.dispose();
141
}
142
return found;
143
}
144
}
145
/*
146
* NOTE: Use PhantomReference here and not WeakReference
147
* otherwise the sessions maybe closed before other objects
148
* which are still being finalized.
149
*/
150
final class SessionRef extends PhantomReference<Session>
151
implements Comparable<SessionRef> {
152
153
static ReferenceQueue<Session> refQueue = new ReferenceQueue<>();
154
155
private static Set<SessionRef> refList =
156
Collections.synchronizedSortedSet(new TreeSet<>());
157
158
// handle to the native session
159
private long id;
160
private Token token;
161
162
SessionRef(Session session, long id, Token token) {
163
super(session, refQueue);
164
this.id = id;
165
this.token = token;
166
refList.add(this);
167
}
168
169
void dispose() {
170
refList.remove(this);
171
try {
172
if (token.isPresent(id)) {
173
token.p11.C_CloseSession(id);
174
}
175
} catch (PKCS11Exception | ProviderException e1) {
176
// ignore
177
} finally {
178
this.clear();
179
}
180
}
181
182
public int compareTo(SessionRef other) {
183
if (this.id == other.id) {
184
return 0;
185
} else {
186
return (this.id < other.id) ? -1 : 1;
187
}
188
}
189
}
190
191