You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by al...@apache.org on 2021/03/18 16:25:07 UTC
[kudu] branch master updated (f69a054 -> 6f4c534)
This is an automated email from the ASF dual-hosted git repository.
alexey pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git.
from f69a054 [TlsSocket] clear error queue in Recv on SSL_RECEIVED_SHUTDOWN
new de0e0ef [security] move SslTypeTraits<BIO>::kFreeFunc to openssl_util.h
new bdeaa12 [security] small re-factoring on TlsHandshake
new 5966364 [security] turn off TLS session cache
new 6f4c534 [security] enhance tls_handshake test
The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
src/kudu/rpc/client_negotiation.cc | 20 +++----
src/kudu/rpc/server_negotiation.cc | 19 ++++---
src/kudu/security/openssl_util.h | 3 ++
src/kudu/security/openssl_util_bio.h | 3 --
src/kudu/security/tls_context.cc | 44 +++++++--------
src/kudu/security/tls_context.h | 9 ++--
src/kudu/security/tls_handshake-test.cc | 95 +++++++++++++++++++++++++++++----
src/kudu/security/tls_handshake.cc | 48 +++++++++++++++--
src/kudu/security/tls_handshake.h | 29 +++++-----
src/kudu/security/tls_socket-test.cc | 8 +--
10 files changed, 193 insertions(+), 85 deletions(-)
[kudu] 04/04: [security] enhance tls_handshake test
Posted by al...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
alexey pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git
commit 6f4c534a08c78184330f8b44416e2b4d8b6c6ccc
Author: Alexey Serbin <al...@apache.org>
AuthorDate: Wed Mar 17 20:03:03 2021 -0700
[security] enhance tls_handshake test
This patch adds extra verification step into the TestHandshakeSequence
scenario of TestTlsHandshake. That's to assert that both parties can
send and receive application data using OpenSSL API (i.e. SSL_Read() and
SSL_Write() function) once TlsHandshake::Continue() returns Status::OK.
In addition, the newly introduced utility methods in TestTlsHandshake
are useful for a follow-up changelist as well.
In addition, I cleaned up code in {client,server}_negotiation.cc
and in tls_context.cc.
Change-Id: Ie77d1f35e12fe74be9f4022e8cd7087500052743
Reviewed-on: http://gerrit.cloudera.org:8080/17198
Tested-by: Kudu Jenkins
Reviewed-by: Grant Henke <gr...@apache.org>
Reviewed-by: Attila Bukor <ab...@apache.org>
---
src/kudu/rpc/client_negotiation.cc | 16 +++----
src/kudu/rpc/server_negotiation.cc | 15 +++----
src/kudu/security/tls_context.cc | 7 ++-
src/kudu/security/tls_handshake-test.cc | 77 +++++++++++++++++++++++++++++++++
src/kudu/security/tls_handshake.h | 3 ++
5 files changed, 98 insertions(+), 20 deletions(-)
diff --git a/src/kudu/rpc/client_negotiation.cc b/src/kudu/rpc/client_negotiation.cc
index e5696b9..d876e35 100644
--- a/src/kudu/rpc/client_negotiation.cc
+++ b/src/kudu/rpc/client_negotiation.cc
@@ -247,7 +247,7 @@ Status ClientNegotiation::SendNegotiatePB(const NegotiatePB& msg) {
DCHECK(msg.has_step()) << "message must have a step";
TRACE("Sending $0 NegotiatePB request", NegotiatePB::NegotiateStep_Name(msg.step()));
- return SendFramedMessageBlocking(socket(), header, msg, deadline_);
+ return SendFramedMessageBlocking(socket_.get(), header, msg, deadline_);
}
Status ClientNegotiation::RecvNegotiatePB(NegotiatePB* msg,
@@ -255,7 +255,8 @@ Status ClientNegotiation::RecvNegotiatePB(NegotiatePB* msg,
unique_ptr<ErrorStatusPB>* rpc_error) {
ResponseHeader header;
Slice param_buf;
- RETURN_NOT_OK(ReceiveFramedMessageBlocking(socket(), buffer, &header, ¶m_buf, deadline_));
+ RETURN_NOT_OK(ReceiveFramedMessageBlocking(
+ socket_.get(), buffer, &header, ¶m_buf, deadline_));
RETURN_NOT_OK(helper_.CheckNegotiateCallId(header.call_id()));
if (header.is_error()) {
@@ -263,7 +264,8 @@ Status ClientNegotiation::RecvNegotiatePB(NegotiatePB* msg,
}
RETURN_NOT_OK(helper_.ParseNegotiatePB(param_buf, msg));
- TRACE("Received $0 NegotiatePB response", NegotiatePB::NegotiateStep_Name(msg->step()));
+ TRACE("Received $0 NegotiatePB response",
+ NegotiatePB::NegotiateStep_Name(msg->step()));
return Status::OK();
}
@@ -288,7 +290,7 @@ Status ClientNegotiation::SendConnectionHeader() {
uint8_t buf[buflen];
serialization::SerializeConnHeader(buf);
size_t nsent;
- return socket()->BlockingWrite(buf, buflen, &nsent, deadline_);
+ return socket_->BlockingWrite(buf, buflen, &nsent, deadline_);
}
Status ClientNegotiation::InitSaslClient() {
@@ -352,8 +354,7 @@ Status ClientNegotiation::SendNegotiate() {
return Status::NotAuthorized("client is not configured with an authentication type");
}
- RETURN_NOT_OK(SendNegotiatePB(msg));
- return Status::OK();
+ return SendNegotiatePB(msg);
}
Status ClientNegotiation::HandleNegotiate(const NegotiatePB& response) {
@@ -434,7 +435,6 @@ Status ClientNegotiation::HandleNegotiate(const NegotiatePB& response) {
// TODO(KUDU-1921): allow the client to require authentication.
if (ContainsKey(client_mechs, SaslMechanism::GSSAPI) &&
ContainsKey(server_mechs, SaslMechanism::GSSAPI)) {
-
// Check that the client has local Kerberos credentials, and if not fall
// back to an alternate mechanism.
Status s = CheckGSSAPI();
@@ -730,7 +730,7 @@ Status ClientNegotiation::SendConnectionContext() {
*conn_context.mutable_encoded_nonce() = ciphertext.ToString();
}
- return SendFramedMessageBlocking(socket(), header, conn_context, deadline_);
+ return SendFramedMessageBlocking(socket_.get(), header, conn_context, deadline_);
}
int ClientNegotiation::GetOptionCb(const char* plugin_name, const char* option,
diff --git a/src/kudu/rpc/server_negotiation.cc b/src/kudu/rpc/server_negotiation.cc
index c636856..e7936ba 100644
--- a/src/kudu/rpc/server_negotiation.cc
+++ b/src/kudu/rpc/server_negotiation.cc
@@ -335,7 +335,8 @@ Status ServerNegotiation::PreflightCheckGSSAPI(const std::string& sasl_proto_nam
Status ServerNegotiation::RecvNegotiatePB(NegotiatePB* msg, faststring* recv_buf) {
RequestHeader header;
Slice param_buf;
- RETURN_NOT_OK(ReceiveFramedMessageBlocking(socket(), recv_buf, &header, ¶m_buf, deadline_));
+ RETURN_NOT_OK(ReceiveFramedMessageBlocking(
+ socket_.get(), recv_buf, &header, ¶m_buf, deadline_));
Status s = helper_.CheckNegotiateCallId(header.call_id());
if (!s.ok()) {
RETURN_NOT_OK(SendError(ErrorStatusPB::FATAL_INVALID_RPC_HEADER, s));
@@ -361,7 +362,7 @@ Status ServerNegotiation::SendNegotiatePB(const NegotiatePB& msg) {
DCHECK(msg.has_step()) << "message must have a step";
TRACE("Sending $0 NegotiatePB response", NegotiatePB::NegotiateStep_Name(msg.step()));
- return SendFramedMessageBlocking(socket(), header, msg, deadline_);
+ return SendFramedMessageBlocking(socket_.get(), header, msg, deadline_);
}
Status ServerNegotiation::SendError(ErrorStatusPB::RpcErrorCodePB code, const Status& err) {
@@ -378,9 +379,7 @@ Status ServerNegotiation::SendError(ErrorStatusPB::RpcErrorCodePB code, const St
msg.set_message(err.ToString());
TRACE("Sending RPC error: $0: $1", ErrorStatusPB::RpcErrorCodePB_Name(code), err.ToString());
- RETURN_NOT_OK(SendFramedMessageBlocking(socket(), header, msg, deadline_));
-
- return Status::OK();
+ return SendFramedMessageBlocking(socket_.get(), header, msg, deadline_);
}
Status ServerNegotiation::ValidateConnectionHeader(faststring* recv_buf) {
@@ -919,15 +918,15 @@ Status ServerNegotiation::SendSaslSuccess() {
}
}
- RETURN_NOT_OK(SendNegotiatePB(response));
- return Status::OK();
+ return SendNegotiatePB(response);
}
Status ServerNegotiation::RecvConnectionContext(faststring* recv_buf) {
TRACE("Waiting for connection context");
RequestHeader header;
Slice param_buf;
- RETURN_NOT_OK(ReceiveFramedMessageBlocking(socket(), recv_buf, &header, ¶m_buf, deadline_));
+ RETURN_NOT_OK(ReceiveFramedMessageBlocking(
+ socket_.get(), recv_buf, &header, ¶m_buf, deadline_));
DCHECK(header.IsInitialized());
if (header.call_id() != kConnectionContextCallId) {
diff --git a/src/kudu/security/tls_context.cc b/src/kudu/security/tls_context.cc
index 0ba7053..52d8f0a 100644
--- a/src/kudu/security/tls_context.cc
+++ b/src/kudu/security/tls_context.cc
@@ -78,6 +78,7 @@
#define TLS1_2_VERSION 0x0303
#endif
+using kudu::security::ca::CertRequestGenerator;
using strings::Substitute;
using std::string;
using std::unique_lock;
@@ -97,8 +98,6 @@ TAG_FLAG(openssl_security_level_override, unsafe);
namespace kudu {
namespace security {
-using ca::CertRequestGenerator;
-
template<> struct SslTypeTraits<SSL> {
static constexpr auto kFreeFunc = &SSL_free;
};
@@ -127,8 +126,8 @@ Status CheckMaxSupportedTlsVersion(int tls_version, const char* tls_version_str)
} // anonymous namespace
TlsContext::TlsContext()
- : tls_ciphers_(kudu::security::SecurityDefaults::kDefaultTlsCiphers),
- tls_min_protocol_(kudu::security::SecurityDefaults::kDefaultTlsMinVersion),
+ : tls_ciphers_(SecurityDefaults::kDefaultTlsCiphers),
+ tls_min_protocol_(SecurityDefaults::kDefaultTlsMinVersion),
lock_(RWMutex::Priority::PREFER_READING),
trusted_cert_count_(0),
has_cert_(false),
diff --git a/src/kudu/security/tls_handshake-test.cc b/src/kudu/security/tls_handshake-test.cc
index 698af49..c3dc94c 100644
--- a/src/kudu/security/tls_handshake-test.cc
+++ b/src/kudu/security/tls_handshake-test.cc
@@ -17,6 +17,8 @@
#include "kudu/security/tls_handshake.h"
+#include <openssl/ssl.h>
+
#include <atomic>
#include <iostream>
#include <string>
@@ -31,6 +33,7 @@
#include "kudu/security/ca/cert_management.h"
#include "kudu/security/cert.h"
#include "kudu/security/crypto.h"
+#include "kudu/security/openssl_util.h"
#include "kudu/security/security-test-util.h"
#include "kudu/security/tls_context.h"
#include "kudu/util/monotime.h"
@@ -127,6 +130,67 @@ class TestTlsHandshakeBase : public KuduTest {
return Status::OK();
}
+ // Read data through the specified SSL handle using OpenSSL API.
+ static void ReadAndCompare(SSL* ssl, const string& expected_data) {
+ ASSERT_GT(expected_data.size(), 0);
+ string result;
+ string buf;
+ buf.resize(expected_data.size());
+ int yet_to_read = expected_data.size();
+ while (yet_to_read > 0) {
+ auto bytes_read = SSL_read(ssl, buf.data(), yet_to_read);
+ if (bytes_read <= 0) {
+ auto error_code = SSL_get_error(ssl, bytes_read);
+ EXPECT_EQ(SSL_ERROR_WANT_READ, error_code);
+ if (error_code != SSL_ERROR_WANT_READ) {
+ FAIL() << "OpenSSL error: " << GetSSLErrorDescription(error_code);
+ }
+ continue;
+ }
+ yet_to_read -= bytes_read;
+ result += buf.substr(0, bytes_read);
+ }
+ ASSERT_EQ(expected_data, result);
+ }
+
+ // Write data through the specified SSL handle using OpenSSL API.
+ static void Write(SSL* ssl, const string& data) {
+ // TODO(aserbin): handle SSL_ERROR_WANT_WRITE if transferring more data
+ auto bytes_written = SSL_write(ssl, data.data(), data.size());
+ EXPECT_EQ(data.size(), bytes_written);
+ if (bytes_written != data.size()) {
+ auto error_code = SSL_get_error(ssl, bytes_written);
+ FAIL() << "OpenSSL error: " << GetSSLErrorDescription(error_code);
+ }
+ }
+
+ // Transfer data between the source and the destination SSL handles.
+ //
+ // Since the TlsHandshake class uses memory BIO for running the TLS handshake,
+ // data between the client and the server TlsHandshake objects is transferred
+ // using the BIO read/write API on the encrypted side of the TLS engine.
+ // This diagram illustrates the approach used by the data transfer method
+ // below:
+ // +-----+
+ // +--> BIO_write(rbio) -->| |--> SSL_read(ssl) --> application data IN
+ // | | SSL |
+ // +---- BIO_read(wbio) <--| |<-- SSL_write(ssl) <-- application data OUT
+ // +-----+
+ static void Transfer(SSL* src, SSL* dst) {
+ BIO* wbio = SSL_get_wbio(src);
+ int pending_wr = BIO_ctrl_pending(wbio);
+
+ string data;
+ data.resize(pending_wr);
+ auto bytes_read = BIO_read(wbio, &data[0], data.size());
+ ASSERT_EQ(data.size(), bytes_read);
+ ASSERT_EQ(0, BIO_ctrl_pending(wbio));
+
+ BIO* rbio = SSL_get_rbio(dst);
+ auto bytes_written = BIO_write(rbio, &data[0], data.size());
+ ASSERT_EQ(data.size(), bytes_written);
+ }
+
TlsContext client_tls_;
TlsContext server_tls_;
@@ -213,6 +277,19 @@ TEST_F(TestTlsHandshake, TestHandshakeSequence) {
// Client receives server Finished
ASSERT_OK(client.Continue(buf1, &buf2));
ASSERT_EQ(buf2.size(), 0);
+
+ auto* client_ssl = client.ssl();
+ auto* server_ssl = server.ssl();
+
+ // Make sure it's possible to exchange data between the client and the server
+ // once the TLS handshake is complete.
+ NO_FATALS(Write(client_ssl, "hello"));
+ NO_FATALS(Transfer(client_ssl, server_ssl));
+ NO_FATALS(ReadAndCompare(server_ssl, "hello"));
+
+ NO_FATALS(Write(client_ssl, "bye"));
+ NO_FATALS(Transfer(client_ssl, server_ssl));
+ NO_FATALS(ReadAndCompare(server_ssl, "bye"));
}
// Tests that the TlsContext can transition from self signed cert to signed
diff --git a/src/kudu/security/tls_handshake.h b/src/kudu/security/tls_handshake.h
index 41a7627..70cdc94 100644
--- a/src/kudu/security/tls_handshake.h
+++ b/src/kudu/security/tls_handshake.h
@@ -22,6 +22,7 @@
#include <string>
#include <glog/logging.h>
+#include <gtest/gtest_prod.h>
#include "kudu/gutil/port.h"
#include "kudu/security/cert.h"
@@ -138,6 +139,8 @@ class TlsHandshake {
std::string GetCipherDescription() const;
private:
+ FRIEND_TEST(TestTlsHandshake, TestHandshakeSequence);
+
// Set the verification mode on the underlying SSL object.
void SetSSLVerify();
[kudu] 02/04: [security] small re-factoring on TlsHandshake
Posted by al...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
alexey pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git
commit bdeaa122f6b14c41b3640ab5d575b11d4c5ffe1c
Author: Alexey Serbin <al...@apache.org>
AuthorDate: Wed Mar 17 13:21:00 2021 -0700
[security] small re-factoring on TlsHandshake
A follow-up patch needs to know what side the TlsHandshake is running
at. This changelist introduces that piece of information into the
TlsHandshake class and also does other related re-factoring in the
related code.
Change-Id: Id8dfb16d0f59dc467d1d4dcdf5c1bc568062087e
Reviewed-on: http://gerrit.cloudera.org:8080/17193
Tested-by: Kudu Jenkins
Reviewed-by: Grant Henke <gr...@apache.org>
Reviewed-by: Attila Bukor <ab...@apache.org>
---
src/kudu/rpc/client_negotiation.cc | 4 +--
src/kudu/rpc/server_negotiation.cc | 4 +--
src/kudu/security/tls_context.cc | 28 ++++++-------------
src/kudu/security/tls_context.h | 9 +++----
src/kudu/security/tls_handshake-test.cc | 18 ++++++-------
src/kudu/security/tls_handshake.cc | 48 ++++++++++++++++++++++++++++++---
src/kudu/security/tls_handshake.h | 28 +++++++++----------
src/kudu/security/tls_socket-test.cc | 8 +++---
8 files changed, 84 insertions(+), 63 deletions(-)
diff --git a/src/kudu/rpc/client_negotiation.cc b/src/kudu/rpc/client_negotiation.cc
index efa69a1..e5696b9 100644
--- a/src/kudu/rpc/client_negotiation.cc
+++ b/src/kudu/rpc/client_negotiation.cc
@@ -126,6 +126,7 @@ ClientNegotiation::ClientNegotiation(unique_ptr<Socket> socket,
: socket_(std::move(socket)),
helper_(SaslHelper::CLIENT),
tls_context_(tls_context),
+ tls_handshake_(security::TlsHandshakeType::CLIENT),
encryption_(encryption),
tls_negotiated_(false),
authn_token_(std::move(authn_token)),
@@ -191,8 +192,7 @@ Status ClientNegotiation::Negotiate(unique_ptr<ErrorStatusPB>* rpc_error) {
// TODO(KUDU-1921): allow the client to require TLS.
if (encryption_ != RpcEncryption::DISABLED &&
ContainsKey(server_features_, TLS)) {
- RETURN_NOT_OK(tls_context_->InitiateHandshake(security::TlsHandshakeType::CLIENT,
- &tls_handshake_));
+ RETURN_NOT_OK(tls_context_->InitiateHandshake(&tls_handshake_));
if (negotiated_authn_ == AuthenticationType::SASL) {
// When using SASL authentication, verifying the server's certificate is
diff --git a/src/kudu/rpc/server_negotiation.cc b/src/kudu/rpc/server_negotiation.cc
index ac55399..c636856 100644
--- a/src/kudu/rpc/server_negotiation.cc
+++ b/src/kudu/rpc/server_negotiation.cc
@@ -160,6 +160,7 @@ ServerNegotiation::ServerNegotiation(unique_ptr<Socket> socket,
: socket_(std::move(socket)),
helper_(SaslHelper::SERVER),
tls_context_(tls_context),
+ tls_handshake_(security::TlsHandshakeType::SERVER),
encryption_(encryption),
tls_negotiated_(false),
token_verifier_(token_verifier),
@@ -223,8 +224,7 @@ Status ServerNegotiation::Negotiate() {
if (encryption_ != RpcEncryption::DISABLED &&
tls_context_->has_cert() &&
ContainsKey(client_features_, TLS)) {
- RETURN_NOT_OK(tls_context_->InitiateHandshake(security::TlsHandshakeType::SERVER,
- &tls_handshake_));
+ RETURN_NOT_OK(tls_context_->InitiateHandshake(&tls_handshake_));
if (negotiated_authn_ != AuthenticationType::CERTIFICATE) {
// The server does not need to verify the client's certificate unless it's
diff --git a/src/kudu/security/tls_context.cc b/src/kudu/security/tls_context.cc
index dcc1ee4..e32e78a 100644
--- a/src/kudu/security/tls_context.cc
+++ b/src/kudu/security/tls_context.cc
@@ -531,33 +531,21 @@ Status TlsContext::LoadCertificateAuthority(const string& certificate_path) {
return AddTrustedCertificate(c);
}
-Status TlsContext::InitiateHandshake(TlsHandshakeType handshake_type,
- TlsHandshake* handshake) const {
+Status TlsContext::InitiateHandshake(TlsHandshake* handshake) const {
SCOPED_OPENSSL_NO_PENDING_ERRORS;
+ DCHECK(handshake);
CHECK(ctx_);
- CHECK(!handshake->ssl_);
+ c_unique_ptr<SSL> ssl;
{
+ // This lock is to protect against concurrent change of certificates
+ // while calling SSL_new() here.
shared_lock<RWMutex> lock(lock_);
- handshake->adopt_ssl(ssl_make_unique(SSL_new(ctx_.get())));
+ ssl = ssl_make_unique(SSL_new(ctx_.get()));
}
- if (!handshake->ssl_) {
+ if (!ssl) {
return Status::RuntimeError("failed to create SSL handle", GetOpenSSLErrors());
}
-
- SSL_set_bio(handshake->ssl(),
- BIO_new(BIO_s_mem()),
- BIO_new(BIO_s_mem()));
-
- switch (handshake_type) {
- case TlsHandshakeType::SERVER:
- SSL_set_accept_state(handshake->ssl());
- break;
- case TlsHandshakeType::CLIENT:
- SSL_set_connect_state(handshake->ssl());
- break;
- }
-
- return Status::OK();
+ return handshake->Init(std::move(ssl));
}
} // namespace security
diff --git a/src/kudu/security/tls_context.h b/src/kudu/security/tls_context.h
index edf37f3..a13e838 100644
--- a/src/kudu/security/tls_context.h
+++ b/src/kudu/security/tls_context.h
@@ -26,19 +26,17 @@
#include <boost/optional/optional.hpp>
#include "kudu/gutil/port.h"
+#include "kudu/security/cert.h" // IWYU pragma: keep
#include "kudu/security/openssl_util.h"
-#include "kudu/security/tls_handshake.h"
#include "kudu/util/locks.h"
#include "kudu/util/rw_mutex.h"
#include "kudu/util/status.h"
-// IWYU pragma: no_include "kudu/security/cert.h"
namespace kudu {
namespace security {
-class Cert; // IWYU pragma: keep
-class CertSignRequest;// IWYU pragma: keep
class PrivateKey;
+class TlsHandshake;
// TlsContext wraps data required by the OpenSSL library for creating and
// accepting TLS protected channels. A single TlsContext instance should be used
@@ -162,8 +160,7 @@ class TlsContext {
Status LoadCertificateAuthority(const std::string& certificate_path) WARN_UNUSED_RESULT;
// Initiates a new TlsHandshake instance.
- Status InitiateHandshake(TlsHandshakeType handshake_type,
- TlsHandshake* handshake) const WARN_UNUSED_RESULT;
+ Status InitiateHandshake(TlsHandshake* handshake) const WARN_UNUSED_RESULT;
// Return the number of certs that have been marked as trusted.
// Used by tests.
diff --git a/src/kudu/security/tls_handshake-test.cc b/src/kudu/security/tls_handshake-test.cc
index 5e6a71d..698af49 100644
--- a/src/kudu/security/tls_handshake-test.cc
+++ b/src/kudu/security/tls_handshake-test.cc
@@ -40,6 +40,7 @@
#include "kudu/util/test_macros.h"
#include "kudu/util/test_util.h"
+using kudu::security::ca::CertSigner;
using std::string;
using std::vector;
@@ -48,8 +49,6 @@ DECLARE_int32(ipki_server_key_size);
namespace kudu {
namespace security {
-using ca::CertSigner;
-
struct Case {
PkiConfig client_pki;
TlsVerificationMode client_verification;
@@ -91,9 +90,10 @@ class TestTlsHandshakeBase : public KuduTest {
// verification modes are set to 'client_verify' and 'server_verify' respectively.
Status RunHandshake(TlsVerificationMode client_verify,
TlsVerificationMode server_verify) {
- TlsHandshake client, server;
- RETURN_NOT_OK(client_tls_.InitiateHandshake(TlsHandshakeType::CLIENT, &client));
- RETURN_NOT_OK(server_tls_.InitiateHandshake(TlsHandshakeType::SERVER, &server));
+ TlsHandshake client(TlsHandshakeType::CLIENT);
+ RETURN_NOT_OK(client_tls_.InitiateHandshake(&client));
+ TlsHandshake server(TlsHandshakeType::SERVER);
+ RETURN_NOT_OK(server_tls_.InitiateHandshake(&server));
client.set_verification_mode(client_verify);
server.set_verification_mode(server_verify);
@@ -186,10 +186,10 @@ TEST_F(TestTlsHandshake, TestHandshakeSequence) {
ASSERT_OK(ConfigureTlsContext(PkiConfig::SIGNED, ca_cert, ca_key, &client_tls_));
ASSERT_OK(ConfigureTlsContext(PkiConfig::SIGNED, ca_cert, ca_key, &server_tls_));
- TlsHandshake server;
- TlsHandshake client;
- ASSERT_OK(client_tls_.InitiateHandshake(TlsHandshakeType::SERVER, &server));
- ASSERT_OK(server_tls_.InitiateHandshake(TlsHandshakeType::CLIENT, &client));
+ TlsHandshake server(TlsHandshakeType::SERVER);
+ ASSERT_OK(client_tls_.InitiateHandshake(&server));
+ TlsHandshake client(TlsHandshakeType::CLIENT);
+ ASSERT_OK(server_tls_.InitiateHandshake(&client));
string buf1;
string buf2;
diff --git a/src/kudu/security/tls_handshake.cc b/src/kudu/security/tls_handshake.cc
index 52162c1..96e3b88 100644
--- a/src/kudu/security/tls_handshake.cc
+++ b/src/kudu/security/tls_handshake.cc
@@ -23,10 +23,12 @@
#include <memory>
#include <string>
+#include <utility>
#include "kudu/gutil/strings/strip.h"
#include "kudu/gutil/strings/substitute.h"
#include "kudu/security/cert.h"
+#include "kudu/security/openssl_util.h"
#include "kudu/security/tls_socket.h"
#include "kudu/util/net/socket.h"
#include "kudu/util/status.h"
@@ -79,22 +81,60 @@ void TlsHandshake::SetSSLVerify() {
SSL_set_verify(ssl_.get(), ssl_mode, /* callback = */nullptr);
}
+TlsHandshake::TlsHandshake(TlsHandshakeType type)
+ : type_(type) {
+}
+
+Status TlsHandshake::Init(c_unique_ptr<SSL> s) {
+ SCOPED_OPENSSL_NO_PENDING_ERRORS;
+ DCHECK(s);
+
+ if (ssl_) {
+ return Status::IllegalState("TlsHandshake is already initialized");
+ }
+
+ auto rbio = ssl_make_unique(BIO_new(BIO_s_mem()));
+ if (!rbio) {
+ return Status::RuntimeError(
+ "failed to create memory-based read BIO", GetOpenSSLErrors());
+ }
+ auto wbio = ssl_make_unique(BIO_new(BIO_s_mem()));
+ if (!wbio) {
+ return Status::RuntimeError(
+ "failed to create memory-based write BIO", GetOpenSSLErrors());
+ }
+ ssl_ = std::move(s);
+ auto* ssl = ssl_.get();
+ SSL_set_bio(ssl, rbio.release(), wbio.release());
+
+ switch (type_) {
+ case TlsHandshakeType::SERVER:
+ SSL_set_accept_state(ssl);
+ break;
+ case TlsHandshakeType::CLIENT:
+ SSL_set_connect_state(ssl);
+ break;
+ }
+ return Status::OK();
+}
+
Status TlsHandshake::Continue(const string& recv, string* send) {
SCOPED_OPENSSL_NO_PENDING_ERRORS;
if (!has_started_) {
SetSSLVerify();
has_started_ = true;
}
- CHECK(ssl_);
+ DCHECK(ssl_);
+ auto* ssl = ssl_.get();
- BIO* rbio = SSL_get_rbio(ssl_.get());
+ BIO* rbio = SSL_get_rbio(ssl);
int n = BIO_write(rbio, recv.data(), recv.size());
DCHECK(n == recv.size() || (n == -1 && recv.empty()));
DCHECK_EQ(BIO_ctrl_pending(rbio), recv.size());
- int rc = SSL_do_handshake(ssl_.get());
+ int rc = SSL_do_handshake(ssl);
if (rc != 1) {
- int ssl_err = SSL_get_error(ssl_.get(), rc);
+ int ssl_err = SSL_get_error(ssl, rc);
// WANT_READ and WANT_WRITE indicate that the handshake is not yet complete.
if (ssl_err != SSL_ERROR_WANT_READ && ssl_err != SSL_ERROR_WANT_WRITE) {
return Status::RuntimeError("TLS Handshake error", GetSSLErrorDescription(ssl_err));
diff --git a/src/kudu/security/tls_handshake.h b/src/kudu/security/tls_handshake.h
index da70331..41a7627 100644
--- a/src/kudu/security/tls_handshake.h
+++ b/src/kudu/security/tls_handshake.h
@@ -20,7 +20,6 @@
#include <functional>
#include <memory>
#include <string>
-#include <utility>
#include <glog/logging.h>
@@ -65,13 +64,16 @@ enum class TlsVerificationMode {
// TlsHandshake manages an ongoing TLS handshake between a client and server.
//
// TlsHandshake instances are default constructed, but must be initialized
-// before use using TlsContext::InitiateHandshake.
+// before using: call the Init() method to initialize an instance.
class TlsHandshake {
public:
-
- TlsHandshake() = default;
+ explicit TlsHandshake(TlsHandshakeType type);
~TlsHandshake() = default;
+ // Initialize the instance for the specified type of handshake
+ // using the given SSL handle.
+ Status Init(c_unique_ptr<SSL> s) WARN_UNUSED_RESULT;
+
// Set the verification mode for this handshake. The default verification mode
// is VERIFY_REMOTE_CERT_AND_HOST.
//
@@ -136,21 +138,9 @@ class TlsHandshake {
std::string GetCipherDescription() const;
private:
- friend class TlsContext;
-
- bool has_started_ = false;
- TlsVerificationMode verification_mode_ = TlsVerificationMode::VERIFY_REMOTE_CERT_AND_HOST;
-
// Set the verification mode on the underlying SSL object.
void SetSSLVerify();
- // Set the SSL to use during the handshake. Called once by
- // TlsContext::InitiateHandshake before starting the handshake processes.
- void adopt_ssl(c_unique_ptr<SSL> ssl) {
- CHECK(!ssl_);
- ssl_ = std::move(ssl);
- }
-
SSL* ssl() {
return ssl_.get();
}
@@ -161,9 +151,15 @@ class TlsHandshake {
// Verifies that the handshake is valid for the provided socket.
Status Verify(const Socket& socket) const WARN_UNUSED_RESULT;
+ // The type of TLS handshake this wrapper represents: client or server.
+ const TlsHandshakeType type_;
+
// Owned SSL handle.
c_unique_ptr<SSL> ssl_;
+ bool has_started_ = false;
+ TlsVerificationMode verification_mode_ = TlsVerificationMode::VERIFY_REMOTE_CERT_AND_HOST;
+
Cert local_cert_;
Cert remote_cert_;
};
diff --git a/src/kudu/security/tls_socket-test.cc b/src/kudu/security/tls_socket-test.cc
index 2db41d8..53b1185 100644
--- a/src/kudu/security/tls_socket-test.cc
+++ b/src/kudu/security/tls_socket-test.cc
@@ -114,8 +114,8 @@ void TlsSocketTest::ConnectClient(const Sockaddr& addr, unique_ptr<Socket>* sock
ASSERT_OK(client_sock->Init(addr.family(), 0));
ASSERT_OK(client_sock->Connect(addr));
- TlsHandshake client;
- ASSERT_OK(client_tls_.InitiateHandshake(TlsHandshakeType::CLIENT, &client));
+ TlsHandshake client(TlsHandshakeType::CLIENT);
+ ASSERT_OK(client_tls_.InitiateHandshake(&client));
ASSERT_OK(DoNegotiationSide(client_sock.get(), &client, "client"));
ASSERT_OK(client.Finish(&client_sock));
*sock = std::move(client_sock);
@@ -146,8 +146,8 @@ class EchoServer {
Sockaddr remote;
CHECK_OK(listener_.Accept(sock.get(), &remote, /*flags=*/0));
- TlsHandshake server;
- CHECK_OK(server_tls_.InitiateHandshake(TlsHandshakeType::SERVER, &server));
+ TlsHandshake server(TlsHandshakeType::SERVER);
+ CHECK_OK(server_tls_.InitiateHandshake(&server));
CHECK_OK(DoNegotiationSide(sock.get(), &server, "server"));
CHECK_OK(server.Finish(&sock));
[kudu] 03/04: [security] turn off TLS session cache
Posted by al...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
alexey pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git
commit 5966364d4c022507faa7f51e2ffed154ed3bb167
Author: Alexey Serbin <al...@apache.org>
AuthorDate: Wed Mar 17 18:53:03 2021 -0700
[security] turn off TLS session cache
As of now, a Kudu RPC connection cannot be re-established based on TLS
session. Every connection attempt leads to negotiating a new connection
from scratch since the client should have been calling SSL_set_session()
explicitly to use a TLS session to re-establish previously used
connection. Disabling the TLS session cache on both sides helps to
spare a bit of memory and CPU needed to maintain the cache otherwise.
Change-Id: I471b2c9dd3a406bb3604d86d28b5977289af2b09
Reviewed-on: http://gerrit.cloudera.org:8080/17197
Tested-by: Alexey Serbin <as...@cloudera.com>
Reviewed-by: Grant Henke <gr...@apache.org>
Reviewed-by: Attila Bukor <ab...@apache.org>
---
src/kudu/security/tls_context.cc | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/src/kudu/security/tls_context.cc b/src/kudu/security/tls_context.cc
index e32e78a..0ba7053 100644
--- a/src/kudu/security/tls_context.cc
+++ b/src/kudu/security/tls_context.cc
@@ -189,6 +189,15 @@ Status TlsContext::Init() {
SSL_CTX_set_options(ctx_.get(), options);
+ // Disable the TLS session cache on both the client and server sides. In Kudu
+ // RPC, connections are not re-established based on TLS sessions anyway. Every
+ // connection attempt from a client to a server results in a new connection
+ // negotiation. Disabling the TLS session cache helps to avoid using extra
+ // resources to store TLS session information and running the automatic check
+ // for expired sessions every 255 connections, as mentioned at
+ // https://www.openssl.org/docs/manmaster/man3/SSL_CTX_set_session_cache_mode.html
+ SSL_CTX_set_session_cache_mode(ctx_.get(), SSL_SESS_CACHE_OFF);
+
OPENSSL_RET_NOT_OK(
SSL_CTX_set_cipher_list(ctx_.get(), tls_ciphers_.c_str()),
"failed to set TLS ciphers");
[kudu] 01/04: [security] move SslTypeTraits::kFreeFunc to
openssl_util.h
Posted by al...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
alexey pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git
commit de0e0ef1b35fe666f46139510d11721ea9183dc4
Author: Alexey Serbin <al...@apache.org>
AuthorDate: Wed Mar 17 17:58:56 2021 -0700
[security] move SslTypeTraits<BIO>::kFreeFunc to openssl_util.h
This patch moves SslTypeTraits<BIO>::kFreeFunc definition from the
openssl_util_bio.h to openssl_util.h header file. The rationale for
this update is that ssl_make_unique(BIO_new(...)) construct can be
used separately from utility functions defined in openssl_util_bio.h,
exactly like it's done in a follow-up patch. With that, inclusion of
openssl_util_bio.h isn't necessary once openssl_util.h is already
included.
Change-Id: I04ac09fbd79abda8c07491f4fe3bdbcac0af07ca
Reviewed-on: http://gerrit.cloudera.org:8080/17196
Tested-by: Kudu Jenkins
Reviewed-by: Grant Henke <gr...@apache.org>
---
src/kudu/security/openssl_util.h | 3 +++
src/kudu/security/openssl_util_bio.h | 3 ---
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/src/kudu/security/openssl_util.h b/src/kudu/security/openssl_util.h
index e5b522e..b2f936d 100644
--- a/src/kudu/security/openssl_util.h
+++ b/src/kudu/security/openssl_util.h
@@ -164,6 +164,9 @@ template<> struct SslTypeTraits<EVP_PKEY> {
template<> struct SslTypeTraits<SSL_CTX> {
static constexpr auto kFreeFunc = &SSL_CTX_free;
};
+template<> struct SslTypeTraits<BIO> {
+ static constexpr auto kFreeFunc = &BIO_free;
+};
template<typename SSL_TYPE, typename Traits = SslTypeTraits<SSL_TYPE>>
c_unique_ptr<SSL_TYPE> ssl_make_unique(SSL_TYPE* d) {
diff --git a/src/kudu/security/openssl_util_bio.h b/src/kudu/security/openssl_util_bio.h
index c935b0b..3c41adb 100644
--- a/src/kudu/security/openssl_util_bio.h
+++ b/src/kudu/security/openssl_util_bio.h
@@ -31,9 +31,6 @@
namespace kudu {
namespace security {
-template<> struct SslTypeTraits<BIO> {
- static constexpr auto kFreeFunc = &BIO_free;
-};
template<typename TYPE, typename Traits = SslTypeTraits<TYPE>>
Status ToBIO(BIO* bio, DataFormat format, TYPE* obj) {