• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package android.net;
18 
19 import android.annotation.UnsupportedAppUsage;
20 import android.content.Context;
21 import android.util.Log;
22 
23 import com.android.org.conscrypt.ClientSessionContext;
24 import com.android.org.conscrypt.FileClientSessionCache;
25 import com.android.org.conscrypt.SSLClientSessionCache;
26 
27 import java.io.File;
28 import java.io.IOException;
29 
30 import javax.net.ssl.SSLContext;
31 import javax.net.ssl.SSLSessionContext;
32 
33 /**
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  */
41 public final class SSLSessionCache {
42     private static final String TAG = "SSLSessionCache";
43     @UnsupportedAppUsage
44     /* package */ final SSLClientSessionCache mSessionCache;
45 
46     /**
47      * 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      */
install(SSLSessionCache cache, SSLContext context)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      */
SSLSessionCache(Object cache)76     public SSLSessionCache(Object cache) {
77         mSessionCache = (SSLClientSessionCache) cache;
78     }
79 
80     /**
81      * 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      */
SSLSessionCache(File dir)88     public SSLSessionCache(File dir) throws IOException {
89         mSessionCache = FileClientSessionCache.usingDirectory(dir);
90     }
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      */
SSLSessionCache(Context context)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 }
109