| 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 <stdbool.h> |
| 16 |
#include <stdio.h> |
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
#include <gmock/gmock.h> |
| 20 |
#include <gtest/gtest.h> |
| 21 |
|
| •22 |
#include "absl/strings/str_cat.h" |
| 23 |
|
| 24 |
#include <grpc/grpc.h> |
| 25 |
#include <grpc/support/alloc.h> |
| 26 |
#include <grpc/support/log.h> |
| 27 |
#include <grpc/support/string_util.h> |
| 28 |
|
| 29 |
#include "src/core/lib/iomgr/load_file.h" |
| 30 |
#include "src/core/lib/security/security_connector/security_connector.h" |
| 31 |
#include "src/core/tsi/ssl_transport_security.h" |
| 32 |
#include "src/core/tsi/transport_security.h" |
| 33 |
#include "src/core/tsi/transport_security_interface.h" |
| 34 |
#include "test/core/tsi/transport_security_test_lib.h" |
| 35 |
#include "test/core/util/test_config.h" |
| 36 |
|
| 37 |
extern "C" { |
| 38 |
#include <openssl/crypto.h> |
| 39 |
#include <openssl/pem.h> |
| 40 |
} |
| 41 |
|
| 42 |
namespace { |
| 43 |
|
| 44 |
const int kSslTsiTestRevokedKeyCertPairsNum = 1; |
| 45 |
const int kSslTsiTestValidKeyCertPairsNum = 1; |
| 46 |
const char* kSslTsiTestCrlSupportedCredentialsDir = |
| 47 |
"test/core/tsi/test_creds/crl_data/"; |
| 48 |
const char* kSslTsiTestFaultyCrlsDir = "bad_path/"; |
| 49 |
|
| 50 |
class CrlSslTransportSecurityTest |
| 51 |
: public testing::TestWithParam<tsi_tls_version> { |
| 52 |
protected: |
| 53 |
// A tsi_test_fixture implementation. |
| 54 |
class SslTsiTestFixture { |
| 55 |
public: |
| 56 |
// When use_faulty_crl_directory is set, the crl_directory of the |
| 57 |
// client is set to a non-existant path. |
| 58 |
static SslTsiTestFixture* Create(bool use_revoked_server_cert, |
| 59 |
bool use_revoked_client_cert, |
| 60 |
bool use_faulty_crl_directory) { |
| 61 |
return new SslTsiTestFixture(use_revoked_server_cert, |
| 62 |
use_revoked_client_cert, |
| 63 |
use_faulty_crl_directory); |
| 64 |
} |
| 65 |
|
| 66 |
void Run() { |
| 67 |
tsi_test_do_handshake(&base_); |
| 68 |
tsi_test_fixture_destroy(&base_); |
| 69 |
} |
| 70 |
|
| 71 |
private: |
| 72 |
SslTsiTestFixture(bool use_revoked_server_cert, |
| 73 |
bool use_revoked_client_cert, |
| 74 |
bool use_faulty_crl_directory) |
| 75 |
: use_revoked_server_cert_(use_revoked_server_cert), |
| 76 |
use_revoked_client_cert_(use_revoked_client_cert), |
| 77 |
use_faulty_crl_directory_(use_faulty_crl_directory) { |
| 78 |
tsi_test_fixture_init(&base_); |
| 79 |
base_.test_unused_bytes = true; |
| 80 |
base_.vtable = &kVtable; |
| 81 |
// Load cert data. |
| 82 |
revoked_pem_key_cert_pairs_ = static_cast<tsi_ssl_pem_key_cert_pair*>( |
| 83 |
gpr_malloc(sizeof(tsi_ssl_pem_key_cert_pair) * |
| 84 |
kSslTsiTestRevokedKeyCertPairsNum)); |
| 85 |
revoked_pem_key_cert_pairs_[0].private_key = LoadFile( |
| 86 |
absl::StrCat(kSslTsiTestCrlSupportedCredentialsDir, "revoked.key")); |
| 87 |
revoked_pem_key_cert_pairs_[0].cert_chain = LoadFile( |
| 88 |
absl::StrCat(kSslTsiTestCrlSupportedCredentialsDir, "revoked.pem")); |
| 89 |
valid_pem_key_cert_pairs_ = static_cast<tsi_ssl_pem_key_cert_pair*>( |
| 90 |
gpr_malloc(sizeof(tsi_ssl_pem_key_cert_pair) * |
| 91 |
kSslTsiTestValidKeyCertPairsNum)); |
| 92 |
valid_pem_key_cert_pairs_[0].private_key = LoadFile( |
| 93 |
absl::StrCat(kSslTsiTestCrlSupportedCredentialsDir, "valid.key")); |
| 94 |
valid_pem_key_cert_pairs_[0].cert_chain = LoadFile( |
| 95 |
absl::StrCat(kSslTsiTestCrlSupportedCredentialsDir, "valid.pem")); |
| 96 |
root_cert_ = LoadFile( |
| 97 |
absl::StrCat(kSslTsiTestCrlSupportedCredentialsDir, "ca.pem")); |
| 98 |
root_store_ = tsi_ssl_root_certs_store_create(root_cert_); |
| 99 |
GPR_ASSERT(root_store_ != nullptr); |
| 100 |
} |
| 101 |
|
| 102 |
~SslTsiTestFixture() { |
| 103 |
for (size_t i = 0; i < kSslTsiTestValidKeyCertPairsNum; i++) { |
| 104 |
PemKeyCertPairDestroy(valid_pem_key_cert_pairs_[i]); |
| 105 |
} |
| 106 |
gpr_free(valid_pem_key_cert_pairs_); |
| 107 |
for (size_t i = 0; i < kSslTsiTestRevokedKeyCertPairsNum; i++) { |
| 108 |
PemKeyCertPairDestroy(revoked_pem_key_cert_pairs_[i]); |
| 109 |
} |
| 110 |
gpr_free(revoked_pem_key_cert_pairs_); |
| 111 |
gpr_free(root_cert_); |
| 112 |
tsi_ssl_root_certs_store_destroy(root_store_); |
| 113 |
tsi_ssl_server_handshaker_factory_unref(server_handshaker_factory_); |
| 114 |
tsi_ssl_client_handshaker_factory_unref(client_handshaker_factory_); |
| 115 |
} |
| 116 |
|
| 117 |
static void SetupHandshakers(tsi_test_fixture* fixture) { |
| 118 |
GPR_ASSERT(fixture != nullptr); |
| 119 |
auto* self = reinterpret_cast<SslTsiTestFixture*>(fixture); |
| 120 |
self->SetupHandshakers(); |
| 121 |
} |
| 122 |
|
| 123 |
void SetupHandshakers() { |
| 124 |
// Create client handshaker factory. |
| 125 |
tsi_ssl_client_handshaker_options client_options; |
| 126 |
client_options.pem_root_certs = root_cert_; |
| 127 |
if (use_revoked_client_cert_) { |
| 128 |
client_options.pem_key_cert_pair = revoked_pem_key_cert_pairs_; |
| 129 |
} else { |
| 130 |
client_options.pem_key_cert_pair = valid_pem_key_cert_pairs_; |
| 131 |
} |
| 132 |
if (use_faulty_crl_directory_) { |
| 133 |
client_options.crl_directory = kSslTsiTestFaultyCrlsDir; |
| 134 |
} else { |
| 135 |
client_options.crl_directory = kSslTsiTestCrlSupportedCredentialsDir; |
| 136 |
} |
| 137 |
client_options.root_store = root_store_; |
| 138 |
client_options.min_tls_version = GetParam(); |
| 139 |
client_options.max_tls_version = GetParam(); |
| 140 |
EXPECT_EQ(tsi_create_ssl_client_handshaker_factory_with_options( |
| 141 |
&client_options, &client_handshaker_factory_), |
| 142 |
TSI_OK); |
| 143 |
// Create server handshaker factory. |
| 144 |
tsi_ssl_server_handshaker_options server_options; |
| 145 |
if (use_revoked_server_cert_) { |
| 146 |
server_options.pem_key_cert_pairs = revoked_pem_key_cert_pairs_; |
| 147 |
server_options.num_key_cert_pairs = kSslTsiTestRevokedKeyCertPairsNum; |
| 148 |
} else { |
| 149 |
server_options.pem_key_cert_pairs = valid_pem_key_cert_pairs_; |
| 150 |
server_options.num_key_cert_pairs = kSslTsiTestValidKeyCertPairsNum; |
| 151 |
} |
| 152 |
server_options.pem_client_root_certs = root_cert_; |
| 153 |
server_options.crl_directory = kSslTsiTestCrlSupportedCredentialsDir; |
| 154 |
server_options.client_certificate_request = |
| 155 |
TSI_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY; |
| 156 |
server_options.session_ticket_key = nullptr; |
| 157 |
server_options.session_ticket_key_size = 0; |
| 158 |
server_options.min_tls_version = GetParam(); |
| 159 |
server_options.max_tls_version = GetParam(); |
| 160 |
EXPECT_EQ(tsi_create_ssl_server_handshaker_factory_with_options( |
| 161 |
&server_options, &server_handshaker_factory_), |
| 162 |
TSI_OK); |
| 163 |
// Create server and client handshakers. |
| 164 |
EXPECT_EQ(tsi_ssl_client_handshaker_factory_create_handshaker( |
| 165 |
client_handshaker_factory_, nullptr, 0, 0, |
| 166 |
&base_.client_handshaker), |
| 167 |
TSI_OK); |
| 168 |
EXPECT_EQ(tsi_ssl_server_handshaker_factory_create_handshaker( |
| 169 |
server_handshaker_factory_, 0, 0, &base_.server_handshaker), |
| 170 |
TSI_OK); |
| 171 |
} |
| 172 |
|
| 173 |
static void CheckHandshakerPeers(tsi_test_fixture* fixture) { |
| 174 |
GPR_ASSERT(fixture != nullptr); |
| 175 |
auto* self = reinterpret_cast<SslTsiTestFixture*>(fixture); |
| 176 |
self->CheckHandshakerPeers(); |
| 177 |
} |
| 178 |
|
| 179 |
void CheckHandshakerPeers() { |
| 180 |
// In TLS 1.3, the client-side handshake succeeds even if the client |
| 181 |
// sends a revoked certificate. In such a case, the server would fail |
| 182 |
// the TLS handshake and send an alert to the client as the first |
| 183 |
// application data message. In TLS 1.2, the client-side handshake will |
| 184 |
// fail if the client sends a revoked certificate. |
| 185 |
// |
| 186 |
// For OpenSSL versions < 1.1, TLS 1.3 is not supported, so the |
| 187 |
// client-side handshake should succeed precisely when the server-side |
| 188 |
// handshake succeeds. |
| 189 |
bool expect_server_success = |
| 190 |
!(use_revoked_server_cert_ || use_revoked_client_cert_); |
| 191 |
#if OPENSSL_VERSION_NUMBER >= 0x10100000 |
| 192 |
bool expect_client_success = GetParam() == tsi_tls_version::TSI_TLS1_2 |
| 193 |
? expect_server_success |
| 194 |
: !use_revoked_server_cert_; |
| 195 |
#else |
| 196 |
// If using OpenSSL version < 1.1, the CRL revocation won't be enabled |
| 197 |
// anyways, so we always expect the connection to be successful. |
| 198 |
expect_server_success = true; |
| 199 |
bool expect_client_success = expect_server_success; |
| 200 |
#endif |
| 201 |
tsi_peer peer; |
| 202 |
if (expect_client_success) { |
| 203 |
EXPECT_EQ( |
| 204 |
tsi_handshaker_result_extract_peer(base_.client_result, &peer), |
| 205 |
TSI_OK); |
| 206 |
tsi_peer_destruct(&peer); |
| 207 |
} else { |
| 208 |
EXPECT_EQ(base_.client_result, nullptr); |
| 209 |
} |
| 210 |
if (expect_server_success) { |
| 211 |
EXPECT_EQ( |
| 212 |
tsi_handshaker_result_extract_peer(base_.server_result, &peer), |
| 213 |
TSI_OK); |
| 214 |
tsi_peer_destruct(&peer); |
| 215 |
} else { |
| 216 |
EXPECT_EQ(base_.server_result, nullptr); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
static void PemKeyCertPairDestroy(tsi_ssl_pem_key_cert_pair kp) { |
| 221 |
gpr_free(const_cast<char*>(kp.private_key)); |
| 222 |
gpr_free(const_cast<char*>(kp.cert_chain)); |
| 223 |
} |
| 224 |
|
| 225 |
static void Destruct(tsi_test_fixture* fixture) { |
| 226 |
auto* self = reinterpret_cast<SslTsiTestFixture*>(fixture); |
| 227 |
delete self; |
| 228 |
} |
| 229 |
|
| 230 |
static char* LoadFile(absl::string_view file_path) { |
| 231 |
grpc_slice slice; |
| 232 |
GPR_ASSERT(grpc_load_file(file_path.data(), 1, &slice) == |
| 233 |
absl::OkStatus()); |
| 234 |
char* data = grpc_slice_to_c_string(slice); |
| 235 |
grpc_slice_unref(slice); |
| 236 |
return data; |
| 237 |
} |
| 238 |
|
| 239 |
static struct tsi_test_fixture_vtable kVtable; |
| 240 |
|
| 241 |
tsi_test_fixture base_; |
| 242 |
bool use_revoked_server_cert_; |
| 243 |
bool use_revoked_client_cert_; |
| 244 |
bool use_faulty_crl_directory_; |
| 245 |
char* root_cert_; |
| 246 |
tsi_ssl_root_certs_store* root_store_; |
| 247 |
tsi_ssl_pem_key_cert_pair* revoked_pem_key_cert_pairs_; |
| 248 |
tsi_ssl_pem_key_cert_pair* valid_pem_key_cert_pairs_; |
| 249 |
tsi_ssl_server_handshaker_factory* server_handshaker_factory_; |
| 250 |
tsi_ssl_client_handshaker_factory* client_handshaker_factory_; |
| 251 |
}; |
| 252 |
}; |
| 253 |
|
| 254 |
struct tsi_test_fixture_vtable |
| 255 |
CrlSslTransportSecurityTest::SslTsiTestFixture::kVtable = { |
| 256 |
&CrlSslTransportSecurityTest::SslTsiTestFixture::SetupHandshakers, |
| 257 |
&CrlSslTransportSecurityTest::SslTsiTestFixture::CheckHandshakerPeers, |
| 258 |
&CrlSslTransportSecurityTest::SslTsiTestFixture::Destruct}; |
| 259 |
|
| 260 |
TEST_P(CrlSslTransportSecurityTest, RevokedServerCert) { |
| 261 |
auto* fixture = SslTsiTestFixture::Create(/*use_revoked_server_cert=*/true, |
| 262 |
/*use_revoked_client_cert=*/false, |
| 263 |
/*use_faulty_crl_directory=*/false); |
| 264 |
fixture->Run(); |
| 265 |
} |
| 266 |
|
| 267 |
TEST_P(CrlSslTransportSecurityTest, RevokedClientCert) { |
| 268 |
auto* fixture = SslTsiTestFixture::Create(/*use_revoked_server_cert=*/false, |
| 269 |
/*use_revoked_client_cert=*/true, |
| 270 |
/*use_faulty_crl_directory=*/false); |
| 271 |
fixture->Run(); |
| 272 |
} |
| 273 |
|
| 274 |
TEST_P(CrlSslTransportSecurityTest, ValidCerts) { |
| 275 |
auto* fixture = SslTsiTestFixture::Create(/*use_revoked_server_cert=*/false, |
| 276 |
/*use_revoked_client_cert=*/false, |
| 277 |
/*use_faulty_crl_directory=*/false); |
| 278 |
fixture->Run(); |
| 279 |
} |
| 280 |
|
| 281 |
TEST_P(CrlSslTransportSecurityTest, UseFaultyCrlDirectory) { |
| 282 |
auto* fixture = SslTsiTestFixture::Create(/*use_revoked_server_cert=*/false, |
| 283 |
/*use_revoked_client_cert=*/false, |
| 284 |
/*use_faulty_crl_directory=*/true); |
| 285 |
fixture->Run(); |
| 286 |
} |
| 287 |
|
| 288 |
std::string TestNameSuffix( |
| 289 |
const ::testing::TestParamInfo<tsi_tls_version>& version) { |
| 290 |
if (version.param == tsi_tls_version::TSI_TLS1_2) return "TLS_1_2"; |
| 291 |
GPR_ASSERT(version.param == tsi_tls_version::TSI_TLS1_3); |
| 292 |
return "TLS_1_3"; |
| 293 |
} |
| 294 |
|
| 295 |
INSTANTIATE_TEST_SUITE_P(TLSVersionsTest, CrlSslTransportSecurityTest, |
| 296 |
testing::Values(tsi_tls_version::TSI_TLS1_2, |
| 297 |
tsi_tls_version::TSI_TLS1_3), |
| 298 |
&TestNameSuffix); |
| 299 |
|
| 300 |
} // namespace |
| 301 |
|
| 302 |
int main(int argc, char** argv) { |
| 303 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 304 |
::testing::InitGoogleTest(&argc, argv); |
| 305 |
return RUN_ALL_TESTS(); |
| 306 |
} |