Path: blob/master/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/KeyCache.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.util.*;28import java.lang.ref.*;2930import java.security.Key;3132import sun.security.util.Cache;3334/**35* Key to P11Key translation cache. The PKCS#11 token can only perform36* operations on keys stored on the token (permanently or temporarily). That37* means that in order to allow the PKCS#11 provider to use keys from other38* providers, we need to transparently convert them to P11Keys. The engines39* do that using (Secret)KeyFactories, which in turn use this class as a40* cache.41*42* There are two KeyCache instances per provider, one for secret keys and43* one for public and private keys.44*45* @author Andreas Sterbenz46* @since 1.547*/48final class KeyCache {4950private final Cache<IdentityWrapper, P11Key> strongCache;5152private WeakReference<Map<Key,P11Key>> cacheReference;5354KeyCache() {55strongCache = Cache.newHardMemoryCache(16);56}5758private static final class IdentityWrapper {59final Object obj;60IdentityWrapper(Object obj) {61this.obj = obj;62}63public boolean equals(Object o) {64if (this == o) {65return true;66}67if (o instanceof IdentityWrapper == false) {68return false;69}70IdentityWrapper other = (IdentityWrapper)o;71return this.obj == other.obj;72}73public int hashCode() {74return System.identityHashCode(obj);75}76}7778synchronized P11Key get(Key key) {79P11Key p11Key = strongCache.get(new IdentityWrapper(key));80if (p11Key != null) {81return p11Key;82}83Map<Key,P11Key> map =84(cacheReference == null) ? null : cacheReference.get();85if (map == null) {86return null;87}88return map.get(key);89}9091synchronized void put(Key key, P11Key p11Key) {92strongCache.put(new IdentityWrapper(key), p11Key);93Map<Key,P11Key> map =94(cacheReference == null) ? null : cacheReference.get();95if (map == null) {96map = new IdentityHashMap<>();97cacheReference = new WeakReference<>(map);98}99map.put(key, p11Key);100}101102synchronized void clear() {103strongCache.clear();104cacheReference = null;105}106}107108109