blob: 65db2c381e1dcfc5f337bd2c68059acba70f9df3 [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
Dan Egnor60586f22010-02-08 21:56:38 -080019import android.content.Context;
20import android.util.Log;
21
Narayan Kamathd6b37fd2013-09-09 18:23:27 +010022import com.android.org.conscrypt.ClientSessionContext;
Kenny Root12e75222013-04-23 22:34:24 -070023import com.android.org.conscrypt.FileClientSessionCache;
24import com.android.org.conscrypt.SSLClientSessionCache;
25
Dan Egnor60586f22010-02-08 21:56:38 -080026import java.io.File;
27import java.io.IOException;
28
Narayan Kamathd6b37fd2013-09-09 18:23:27 +010029import javax.net.ssl.SSLContext;
30import javax.net.ssl.SSLSessionContext;
31
Dan Egnor60586f22010-02-08 21:56:38 -080032/**
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.
37 *
38 * @see SSLCertificateSocketFactory
39 */
40public final class SSLSessionCache {
41 private static final String TAG = "SSLSessionCache";
42 /* package */ final SSLClientSessionCache mSessionCache;
43
44 /**
Narayan Kamathd6b37fd2013-09-09 18:23:27 +010045 * 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).
48 *
49 * @param cache the cache instance to install, or {@code null} to uninstall any
50 * existing cache.
51 * @param context the context to install it on.
52 * @throws IllegalArgumentException if the context does not support a session
53 * cache.
54 *
55 * @hide candidate for public API
56 */
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);
62 } else {
63 throw new IllegalArgumentException("Incompatible SSLContext: " + context);
64 }
65 }
66
67 /**
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.
71 *
72 * @hide For unit test use only
73 */
74 public SSLSessionCache(Object cache) {
75 mSessionCache = (SSLClientSessionCache) cache;
76 }
77
78 /**
Dan Egnor60586f22010-02-08 21:56:38 -080079 * 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.
82 *
83 * @param dir to store session files in (created if necessary)
84 * @throws IOException if the cache can't be opened
85 */
86 public SSLSessionCache(File dir) throws IOException {
Narayan Kamathfdf4bbf2013-09-09 16:46:16 +000087 mSessionCache = FileClientSessionCache.usingDirectory(dir);
Dan Egnor60586f22010-02-08 21:56:38 -080088 }
89
90 /**
91 * Create a session cache at the default location for this app.
92 * Multiple instances share data internally.
93 *
94 * @param context for the application
95 */
96 public SSLSessionCache(Context context) {
97 File dir = context.getDir("sslcache", Context.MODE_PRIVATE);
98 SSLClientSessionCache cache = null;
99 try {
100 cache = FileClientSessionCache.usingDirectory(dir);
101 } catch (IOException e) {
102 Log.w(TAG, "Unable to create SSL session cache in " + dir, e);
103 }
104 mSessionCache = cache;
105 }
106}