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
17package android.net;
18
19import android.content.Context;
20import android.util.Log;
21
22import com.android.org.conscrypt.ClientSessionContext;
23import com.android.org.conscrypt.FileClientSessionCache;
24import com.android.org.conscrypt.SSLClientSessionCache;
25
26import java.io.File;
27import java.io.IOException;
28
29import javax.net.ssl.SSLContext;
30import javax.net.ssl.SSLSessionContext;
31
32/**
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    /**
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).
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    /**
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.
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 {
87        mSessionCache = FileClientSessionCache.usingDirectory(dir);
88    }
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}
107