2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 import android.content.Context;
20 import android.util.Log;
22 import com.android.org.conscrypt.ClientSessionContext;
23 import com.android.org.conscrypt.FileClientSessionCache;
24 import com.android.org.conscrypt.SSLClientSessionCache;
27 import java.io.IOException;
29 import javax.net.ssl.SSLContext;
30 import javax.net.ssl.SSLSessionContext;
33 * File-based cache of established SSL sessions. When re-establishing a
34 * connection to the same server, using an SSL session cache can save some time,
35 * power, and bandwidth by skipping directly to an encrypted stream.
36 * This is a persistent cache which can span executions of the application.
38 * @see SSLCertificateSocketFactory
40 public final class SSLSessionCache {
41 private static final String TAG = "SSLSessionCache";
42 /* package */ final SSLClientSessionCache mSessionCache;
45 * Installs a {@link SSLSessionCache} on a {@link SSLContext}. The cache will
46 * be used on all socket factories created by this context (including factories
47 * created before this call).
49 * @param cache the cache instance to install, or {@code null} to uninstall any
51 * @param context the context to install it on.
52 * @throws IllegalArgumentException if the context does not support a session
55 * @hide candidate for public API
57 public static void install(SSLSessionCache cache, SSLContext context) {
58 SSLSessionContext clientContext = context.getClientSessionContext();
59 if (clientContext instanceof ClientSessionContext) {
60 ((ClientSessionContext) clientContext).setPersistentCache(
61 cache == null ? null : cache.mSessionCache);
63 throw new IllegalArgumentException("Incompatible SSLContext: " + context);
68 * NOTE: This needs to be Object (and not SSLClientSessionCache) because apps
69 * that build directly against the framework (and not the SDK) might not declare
70 * a dependency on conscrypt. Javac will then has fail while resolving constructors.
72 * @hide For unit test use only
74 public SSLSessionCache(Object cache) {
75 mSessionCache = (SSLClientSessionCache) cache;
79 * Create a session cache using the specified directory.
80 * Individual session entries will be files within the directory.
81 * Multiple instances for the same directory share data internally.
83 * @param dir to store session files in (created if necessary)
84 * @throws IOException if the cache can't be opened
86 public SSLSessionCache(File dir) throws IOException {
87 mSessionCache = FileClientSessionCache.usingDirectory(dir);
91 * Create a session cache at the default location for this app.
92 * Multiple instances share data internally.
94 * @param context for the application
96 public SSLSessionCache(Context context) {
97 File dir = context.getDir("sslcache", Context.MODE_PRIVATE);
98 SSLClientSessionCache cache = null;
100 cache = FileClientSessionCache.usingDirectory(dir);
101 } catch (IOException e) {
102 Log.w(TAG, "Unable to create SSL session cache in " + dir, e);
104 mSessionCache = cache;