blob: 54c1296bef60345eadab9f87aa96a2b146013d70 [file] [log] [blame]
Mike Yubab3daa2018-10-19 22:11:43 +08001/*
2 * Copyright (C) 2018 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#include "netd_resolv/DnsTlsSessionCache.h"
18
19#define LOG_TAG "DnsTlsSessionCache"
20//#define LOG_NDEBUG 0
21
22#include "log/log.h"
23
24namespace android {
25namespace net {
26
27bool DnsTlsSessionCache::prepareSsl(SSL* ssl) {
28 // Add this cache as the 0-index extra data for the socket.
29 // This is used by newSessionCallback.
30 int ret = SSL_set_ex_data(ssl, 0, this);
31 return ret == 1;
32}
33
34void DnsTlsSessionCache::prepareSslContext(SSL_CTX* ssl_ctx) {
35 SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_CLIENT);
36 SSL_CTX_sess_set_new_cb(ssl_ctx, &DnsTlsSessionCache::newSessionCallback);
37}
38
39// static
40int DnsTlsSessionCache::newSessionCallback(SSL* ssl, SSL_SESSION* session) {
41 if (!ssl || !session) {
42 ALOGE("Null SSL object in new session callback");
43 return 0;
44 }
45 DnsTlsSessionCache* cache = reinterpret_cast<DnsTlsSessionCache*>(
46 SSL_get_ex_data(ssl, 0));
47 if (!cache) {
48 ALOGE("null transport in new session callback");
49 return 0;
50 }
51 ALOGV("Recording session");
52 cache->recordSession(session);
53 return 1; // Increment the refcount of session.
54}
55
56void DnsTlsSessionCache::recordSession(SSL_SESSION* session) {
57 std::lock_guard guard(mLock);
58 mSessions.emplace_front(session);
59 if (mSessions.size() > kMaxSize) {
60 ALOGV("Too many sessions; trimming");
61 mSessions.pop_back();
62 }
63}
64
65bssl::UniquePtr<SSL_SESSION> DnsTlsSessionCache::getSession() {
66 std::lock_guard guard(mLock);
67 if (mSessions.size() == 0) {
68 ALOGV("No known sessions");
69 return nullptr;
70 }
71 bssl::UniquePtr<SSL_SESSION> ret = std::move(mSessions.front());
72 mSessions.pop_front();
73 return ret;
74}
75
76} // end of namespace net
77} // end of namespace android