blob: 944bc549acca9e124e5499f5f05420eea8ad1e5c [file] [log] [blame]
Dan Egnor60586f22010-02-08 21:56:38 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package android.net;
18
Artur Satayev26958002019-12-10 17:47:52 +000019import android.compat.annotation.UnsupportedAppUsage;
Dan Egnor60586f22010-02-08 21:56:38 -080020import android.content.Context;
21import android.util.Log;
22
Narayan Kamathd6b37fd2013-09-09 18:23:27 +010023import com.android.org.conscrypt.ClientSessionContext;
Kenny Root12e75222013-04-23 22:34:24 -070024import com.android.org.conscrypt.FileClientSessionCache;
25import com.android.org.conscrypt.SSLClientSessionCache;
26
Dan Egnor60586f22010-02-08 21:56:38 -080027import java.io.File;
28import java.io.IOException;
29
Narayan Kamathd6b37fd2013-09-09 18:23:27 +010030import javax.net.ssl.SSLContext;
31import javax.net.ssl.SSLSessionContext;
32
Dan Egnor60586f22010-02-08 21:56:38 -080033/**
34 * File-based cache of established SSL sessions. When re-establishing a
35 * connection to the same server, using an SSL session cache can save some time,
36 * power, and bandwidth by skipping directly to an encrypted stream.
37 * This is a persistent cache which can span executions of the application.
38 *
39 * @see SSLCertificateSocketFactory
40 */
41public final class SSLSessionCache {
42 private static final String TAG = "SSLSessionCache";
Mathew Inwood53f089f2018-08-08 14:44:44 +010043 @UnsupportedAppUsage
Dan Egnor60586f22010-02-08 21:56:38 -080044 /* package */ final SSLClientSessionCache mSessionCache;
45
46 /**
Narayan Kamathd6b37fd2013-09-09 18:23:27 +010047 * Installs a {@link SSLSessionCache} on a {@link SSLContext}. The cache will
48 * be used on all socket factories created by this context (including factories
49 * created before this call).
50 *
51 * @param cache the cache instance to install, or {@code null} to uninstall any
52 * existing cache.
53 * @param context the context to install it on.
54 * @throws IllegalArgumentException if the context does not support a session
55 * cache.
56 *
57 * @hide candidate for public API
58 */
59 public static void install(SSLSessionCache cache, SSLContext context) {
60 SSLSessionContext clientContext = context.getClientSessionContext();
61 if (clientContext instanceof ClientSessionContext) {
62 ((ClientSessionContext) clientContext).setPersistentCache(
63 cache == null ? null : cache.mSessionCache);
64 } else {
65 throw new IllegalArgumentException("Incompatible SSLContext: " + context);
66 }
67 }
68
69 /**
70 * NOTE: This needs to be Object (and not SSLClientSessionCache) because apps
71 * that build directly against the framework (and not the SDK) might not declare
72 * a dependency on conscrypt. Javac will then has fail while resolving constructors.
73 *
74 * @hide For unit test use only
75 */
76 public SSLSessionCache(Object cache) {
77 mSessionCache = (SSLClientSessionCache) cache;
78 }
79
80 /**
Dan Egnor60586f22010-02-08 21:56:38 -080081 * Create a session cache using the specified directory.
82 * Individual session entries will be files within the directory.
83 * Multiple instances for the same directory share data internally.
84 *
85 * @param dir to store session files in (created if necessary)
86 * @throws IOException if the cache can't be opened
87 */
88 public SSLSessionCache(File dir) throws IOException {
Narayan Kamathfdf4bbf2013-09-09 16:46:16 +000089 mSessionCache = FileClientSessionCache.usingDirectory(dir);
Dan Egnor60586f22010-02-08 21:56:38 -080090 }
91
92 /**
93 * Create a session cache at the default location for this app.
94 * Multiple instances share data internally.
95 *
96 * @param context for the application
97 */
98 public SSLSessionCache(Context context) {
99 File dir = context.getDir("sslcache", Context.MODE_PRIVATE);
100 SSLClientSessionCache cache = null;
101 try {
102 cache = FileClientSessionCache.usingDirectory(dir);
103 } catch (IOException e) {
104 Log.w(TAG, "Unable to create SSL session cache in " + dir, e);
105 }
106 mSessionCache = cache;
107 }
108}