Path: blob/master/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/Session.java
41154 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.pkcs11;2627import java.lang.ref.*;28import java.util.*;29import java.util.concurrent.atomic.AtomicInteger;3031import java.security.*;3233import sun.security.pkcs11.wrapper.*;3435/**36* A session object. Sessions are obtained via the SessionManager,37* see there for details. Most code will only ever need one method in38* this class, the id() method to obtain the session id.39*40* @author Andreas Sterbenz41* @since 1.542*/43final class Session implements Comparable<Session> {4445// time after which to close idle sessions, in milliseconds (3 minutes)46private static final long MAX_IDLE_TIME = 3 * 60 * 1000;4748// token instance49final Token token;5051// session id52private final long id;5354// number of objects created within this session55private final AtomicInteger createdObjects;5657// time this session was last used58// not synchronized/volatile for performance, so may be unreliable59// this could lead to idle sessions being closed early, but that is harmless60private long lastAccess;6162private final SessionRef sessionRef;6364Session(Token token, long id) {65this.token = token;66this.id = id;67createdObjects = new AtomicInteger();68id();69sessionRef = new SessionRef(this, id, token);70}7172public int compareTo(Session other) {73if (this.lastAccess == other.lastAccess) {74return 0;75} else {76return (this.lastAccess < other.lastAccess) ? -1 : 1;77}78}7980boolean isLive(long currentTime) {81return currentTime - lastAccess < MAX_IDLE_TIME;82}8384long id() {85if (token.isPresent(this.id) == false) {86throw new ProviderException("Token has been removed");87}88lastAccess = System.currentTimeMillis();89return id;90}9192void addObject() {93int n = createdObjects.incrementAndGet();94// XXX update statistics in session manager if n == 195}9697void removeObject() {98int n = createdObjects.decrementAndGet();99if (n == 0) {100token.sessionManager.demoteObjSession(this);101} else if (n < 0) {102throw new ProviderException("Internal error: objects created " + n);103}104}105106boolean hasObjects() {107return createdObjects.get() != 0;108}109110// regular close which will not close sessions when there are objects(keys)111// still associated with them112void close() {113close(true);114}115116// forced close which will close sessions regardless if there are objects117// associated with them. Note that closing the sessions this way may118// lead to those associated objects(keys) un-usable. Thus should only be119// used for scenarios such as the token is about to be removed, etc.120void kill() {121close(false);122}123124private void close(boolean checkObjCtr) {125if (hasObjects() && checkObjCtr) {126throw new ProviderException(127"Internal error: close session with active objects");128}129sessionRef.dispose();130}131132// Called by the NativeResourceCleaner at specified intervals133// See NativeResourceCleaner for more information134static boolean drainRefQueue() {135boolean found = false;136SessionRef next;137while ((next = (SessionRef) SessionRef.refQueue.poll())!= null) {138found = true;139next.dispose();140}141return found;142}143}144/*145* NOTE: Use PhantomReference here and not WeakReference146* otherwise the sessions maybe closed before other objects147* which are still being finalized.148*/149final class SessionRef extends PhantomReference<Session>150implements Comparable<SessionRef> {151152static ReferenceQueue<Session> refQueue = new ReferenceQueue<>();153154private static Set<SessionRef> refList =155Collections.synchronizedSortedSet(new TreeSet<>());156157// handle to the native session158private long id;159private Token token;160161SessionRef(Session session, long id, Token token) {162super(session, refQueue);163this.id = id;164this.token = token;165refList.add(this);166}167168void dispose() {169refList.remove(this);170try {171if (token.isPresent(id)) {172token.p11.C_CloseSession(id);173}174} catch (PKCS11Exception | ProviderException e1) {175// ignore176} finally {177this.clear();178}179}180181public int compareTo(SessionRef other) {182if (this.id == other.id) {183return 0;184} else {185return (this.id < other.id) ? -1 : 1;186}187}188}189190191