| 1 |
// Copyright 2021 gRPC authors. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include <grpc/support/port_platform.h> |
| 16 |
|
| 17 |
#include "src/core/tsi/ssl/key_logging/ssl_key_logging.h" |
| 18 |
|
| 19 |
#include <map> |
| 20 |
|
| 21 |
#include <grpc/support/log.h> |
| 22 |
|
| 23 |
#include "src/core/lib/gprpp/sync.h" |
| 24 |
#include "src/core/lib/iomgr/error.h" |
| 25 |
#include "src/core/lib/slice/slice_internal.h" |
| 26 |
|
| 27 |
using TlsSessionKeyLogger = tsi::TlsSessionKeyLoggerCache::TlsSessionKeyLogger; |
| 28 |
|
| 29 |
namespace tsi { |
| 30 |
|
| 31 |
namespace { |
| 32 |
|
| 33 |
gpr_once g_cache_mutex_init = GPR_ONCE_INIT; |
| 34 |
grpc_core::Mutex* g_tls_session_key_log_cache_mu = nullptr; |
| 35 |
// A pointer to a global singleton instance. |
| 36 |
TlsSessionKeyLoggerCache* g_cache_instance |
| 37 |
ABSL_GUARDED_BY(g_tls_session_key_log_cache_mu) = nullptr; |
| 38 |
|
| 39 |
void do_cache_mutex_init(void) { |
| 40 |
g_tls_session_key_log_cache_mu = new grpc_core::Mutex(); |
| 41 |
} |
| 42 |
|
| 43 |
} // namespace |
| 44 |
|
| 45 |
TlsSessionKeyLoggerCache::TlsSessionKeyLogger::TlsSessionKeyLogger( |
| 46 |
std::string tls_session_key_log_file_path, |
| 47 |
grpc_core::RefCountedPtr<TlsSessionKeyLoggerCache> cache) |
| 48 |
: tls_session_key_log_file_path_(std::move(tls_session_key_log_file_path)), |
| 49 |
cache_(std::move(cache)) { |
| 50 |
GPR_ASSERT(!tls_session_key_log_file_path_.empty()); |
| 51 |
GPR_ASSERT(cache_ != nullptr); |
| 52 |
fd_ = fopen(tls_session_key_log_file_path_.c_str(), "w+"); |
| 53 |
if (fd_ == nullptr) { |
| 54 |
grpc_error_handle error = GRPC_OS_ERROR(errno, "fopen"); |
| 55 |
gpr_log(GPR_ERROR, |
| 56 |
"Ignoring TLS Key logging. ERROR Opening TLS Keylog " |
| 57 |
"file: %s", |
| 58 |
grpc_core::StatusToString(error).c_str()); |
| 59 |
} |
| 60 |
cache_->tls_session_key_logger_map_.emplace(tls_session_key_log_file_path_, |
| 61 |
this); |
| 62 |
}; |
| 63 |
|
| 64 |
TlsSessionKeyLoggerCache::TlsSessionKeyLogger::~TlsSessionKeyLogger() { |
| 65 |
{ |
| 66 |
grpc_core::MutexLock lock(&lock_); |
| 67 |
if (fd_ != nullptr) fclose(fd_); |
| 68 |
} |
| 69 |
{ |
| 70 |
grpc_core::MutexLock lock(g_tls_session_key_log_cache_mu); |
| 71 |
auto it = cache_->tls_session_key_logger_map_.find( |
| 72 |
tls_session_key_log_file_path_); |
| 73 |
if (it != cache_->tls_session_key_logger_map_.end() && it->second == this) { |
| 74 |
cache_->tls_session_key_logger_map_.erase(it); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
void TlsSessionKeyLoggerCache::TlsSessionKeyLogger::LogSessionKeys( |
| 80 |
SSL_CTX* /* ssl_context */, const std::string& session_keys_info) { |
| 81 |
grpc_core::MutexLock lock(&lock_); |
| 82 |
if (fd_ == nullptr || session_keys_info.empty()) return; |
| 83 |
// Append to key log file under lock |
| 84 |
bool err = |
| 85 |
fwrite((session_keys_info + "\r\n").c_str(), sizeof(char), |
| 86 |
session_keys_info.length() + 1, fd_) < session_keys_info.length(); |
| 87 |
|
| 88 |
if (err) { |
| 89 |
grpc_error_handle error = GRPC_OS_ERROR(errno, "fwrite"); |
| 90 |
gpr_log(GPR_ERROR, "Error Appending to TLS session key log file: %s", |
| 91 |
grpc_core::StatusToString(error).c_str()); |
| 92 |
fclose(fd_); |
| 93 |
fd_ = nullptr; // disable future attempts to write to this file |
| 94 |
} else { |
| 95 |
fflush(fd_); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
TlsSessionKeyLoggerCache::TlsSessionKeyLoggerCache() |
| 100 |
ABSL_EXCLUSIVE_LOCKS_REQUIRED(g_tls_session_key_log_cache_mu) { |
| 101 |
g_cache_instance = this; |
| 102 |
} |
| 103 |
|
| 104 |
TlsSessionKeyLoggerCache::~TlsSessionKeyLoggerCache() { |
| 105 |
grpc_core::MutexLock lock(g_tls_session_key_log_cache_mu); |
| 106 |
g_cache_instance = nullptr; |
| 107 |
} |
| 108 |
|
| 109 |
grpc_core::RefCountedPtr<TlsSessionKeyLogger> TlsSessionKeyLoggerCache::Get( |
| 110 |
std::string tls_session_key_log_file_path) { |
| 111 |
gpr_once_init(&g_cache_mutex_init, do_cache_mutex_init); |
| 112 |
GPR_DEBUG_ASSERT(g_tls_session_key_log_cache_mu != nullptr); |
| 113 |
if (tls_session_key_log_file_path.empty()) { |
| 114 |
return nullptr; |
| 115 |
} |
| 116 |
{ |
| 117 |
grpc_core::MutexLock lock(g_tls_session_key_log_cache_mu); |
| 118 |
grpc_core::RefCountedPtr<TlsSessionKeyLoggerCache> cache; |
| 119 |
if (g_cache_instance == nullptr) { |
| 120 |
// This will automatically set g_cache_instance. |
| 121 |
// Not using MakeRefCounted because to the thread safety analysis, which |
| 122 |
// cannot see through calls, it would look like MakeRefCounted was |
| 123 |
// calling TlsSessionKeyLoggerCache without holding a lock on |
| 124 |
// g_tls_session_key_log_cache_mu. |
| 125 |
cache.reset(new TlsSessionKeyLoggerCache()); |
| 126 |
} else { |
| 127 |
cache = g_cache_instance->Ref(); |
| 128 |
} |
| 129 |
// Check cache for entry. |
| 130 |
auto it = |
| 131 |
cache->tls_session_key_logger_map_.find(tls_session_key_log_file_path); |
| 132 |
if (it != cache->tls_session_key_logger_map_.end()) { |
| 133 |
// Avoid a race condition if the destructor of the tls key logger |
| 134 |
// of interest is currently executing. |
| 135 |
auto key_logger = it->second->RefIfNonZero(); |
| 136 |
if (key_logger != nullptr) return key_logger; |
| 137 |
} |
| 138 |
// Not found in cache, so create new entry. |
| 139 |
// This will automatically add itself to tls_session_key_logger_map_. |
| 140 |
return grpc_core::MakeRefCounted<TlsSessionKeyLogger>( |
| 141 |
std::move(tls_session_key_log_file_path), std::move(cache)); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
}; // namespace tsi |