| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2018 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#ifndef H2_TLS_COMMON_H |
| 20 |
#define H2_TLS_COMMON_H |
| 21 |
|
| 22 |
#include <stdint.h> |
| 23 |
#include <string.h> |
| 24 |
|
| 25 |
#include <string> |
| 26 |
|
| 27 |
#include "absl/strings/string_view.h" |
| 28 |
|
| 29 |
#include <grpc/grpc.h> |
| 30 |
#include <grpc/grpc_security.h> |
| 31 |
#include <grpc/grpc_security_constants.h> |
| 32 |
#include <grpc/slice.h> |
| 33 |
#include <grpc/status.h> |
| 34 |
#include <grpc/support/log.h> |
| 35 |
|
| 36 |
#include "src/core/lib/channel/channel_args.h" |
| 37 |
#include "src/core/lib/iomgr/error.h" |
| 38 |
#include "src/core/lib/iomgr/load_file.h" |
| 39 |
#include "src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h" |
| 40 |
#include "src/core/lib/slice/slice_internal.h" |
| 41 |
#include "test/core/end2end/end2end_tests.h" |
| 42 |
#include "test/core/util/tls_utils.h" |
| 43 |
|
| 44 |
// For normal TLS connections. |
| 45 |
#define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem" |
| 46 |
#define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem" |
| 47 |
#define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key" |
| 48 |
|
| 49 |
struct SecurityPrimitives { |
| 50 |
enum ProviderType { STATIC_PROVIDER = 0, FILE_PROVIDER = 1 } provider_type; |
| 51 |
enum VerifierType { |
| 52 |
EXTERNAL_SYNC_VERIFIER = 0, |
| 53 |
EXTERNAL_ASYNC_VERIFIER = 1, |
| 54 |
HOSTNAME_VERIFIER = 2 |
| 55 |
} verifier_type; |
| 56 |
enum TlsVersion { V_12 = 0, V_13 = 1 } tls_version; |
| 57 |
}; |
| 58 |
|
| 59 |
struct fullstack_secure_fixture_data { |
| 60 |
~fullstack_secure_fixture_data() { |
| 61 |
grpc_tls_certificate_provider_release(client_provider); |
| 62 |
grpc_tls_certificate_provider_release(server_provider); |
| 63 |
grpc_tls_certificate_verifier_release(client_verifier); |
| 64 |
grpc_tls_certificate_verifier_release(server_verifier); |
| 65 |
} |
| 66 |
std::string localaddr; |
| 67 |
grpc_tls_version tls_version; |
| 68 |
grpc_tls_certificate_provider* client_provider = nullptr; |
| 69 |
grpc_tls_certificate_provider* server_provider = nullptr; |
| 70 |
grpc_tls_certificate_verifier* client_verifier = nullptr; |
| 71 |
grpc_tls_certificate_verifier* server_verifier = nullptr; |
| 72 |
bool check_call_host = true; |
| 73 |
}; |
| 74 |
|
| 75 |
inline void SetTlsVersion(fullstack_secure_fixture_data* ffd, |
| 76 |
SecurityPrimitives::TlsVersion tls_version) { |
| 77 |
switch (tls_version) { |
| 78 |
case SecurityPrimitives::TlsVersion::V_12: { |
| 79 |
ffd->tls_version = grpc_tls_version::TLS1_2; |
| 80 |
break; |
| 81 |
} |
| 82 |
case SecurityPrimitives::TlsVersion::V_13: { |
| 83 |
ffd->tls_version = grpc_tls_version::TLS1_3; |
| 84 |
break; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
inline void SetCertificateProvider( |
| 90 |
fullstack_secure_fixture_data* ffd, |
| 91 |
SecurityPrimitives::ProviderType provider_type) { |
| 92 |
switch (provider_type) { |
| 93 |
case SecurityPrimitives::ProviderType::STATIC_PROVIDER: { |
| 94 |
grpc_slice root_slice, cert_slice, key_slice; |
| 95 |
GPR_ASSERT(GRPC_LOG_IF_ERROR( |
| 96 |
"load_file", grpc_load_file(CA_CERT_PATH, 1, &root_slice))); |
| 97 |
std::string root_cert = |
| 98 |
std::string(grpc_core::StringViewFromSlice(root_slice)); |
| 99 |
GPR_ASSERT(GRPC_LOG_IF_ERROR( |
| 100 |
"load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice))); |
| 101 |
std::string identity_cert = |
| 102 |
std::string(grpc_core::StringViewFromSlice(cert_slice)); |
| 103 |
GPR_ASSERT(GRPC_LOG_IF_ERROR( |
| 104 |
"load_file", grpc_load_file(SERVER_KEY_PATH, 1, &key_slice))); |
| 105 |
std::string private_key = |
| 106 |
std::string(grpc_core::StringViewFromSlice(key_slice)); |
| 107 |
grpc_tls_identity_pairs* client_pairs = grpc_tls_identity_pairs_create(); |
| 108 |
grpc_tls_identity_pairs_add_pair(client_pairs, private_key.c_str(), |
| 109 |
identity_cert.c_str()); |
| 110 |
ffd->client_provider = grpc_tls_certificate_provider_static_data_create( |
| 111 |
root_cert.c_str(), client_pairs); |
| 112 |
grpc_tls_identity_pairs* server_pairs = grpc_tls_identity_pairs_create(); |
| 113 |
grpc_tls_identity_pairs_add_pair(server_pairs, private_key.c_str(), |
| 114 |
identity_cert.c_str()); |
| 115 |
ffd->server_provider = grpc_tls_certificate_provider_static_data_create( |
| 116 |
root_cert.c_str(), server_pairs); |
| 117 |
grpc_slice_unref(root_slice); |
| 118 |
grpc_slice_unref(cert_slice); |
| 119 |
grpc_slice_unref(key_slice); |
| 120 |
break; |
| 121 |
} |
| 122 |
case SecurityPrimitives::ProviderType::FILE_PROVIDER: { |
| 123 |
ffd->client_provider = grpc_tls_certificate_provider_file_watcher_create( |
| 124 |
SERVER_KEY_PATH, SERVER_CERT_PATH, CA_CERT_PATH, 1); |
| 125 |
ffd->server_provider = grpc_tls_certificate_provider_file_watcher_create( |
| 126 |
SERVER_KEY_PATH, SERVER_CERT_PATH, CA_CERT_PATH, 1); |
| 127 |
break; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
inline void SetCertificateVerifier( |
| 133 |
fullstack_secure_fixture_data* ffd, |
| 134 |
SecurityPrimitives::VerifierType verifier_type) { |
| 135 |
switch (verifier_type) { |
| 136 |
case SecurityPrimitives::VerifierType::EXTERNAL_SYNC_VERIFIER: { |
| 137 |
auto* client_sync_verifier = |
| 138 |
new grpc_core::testing::SyncExternalVerifier(true); |
| 139 |
ffd->client_verifier = grpc_tls_certificate_verifier_external_create( |
| 140 |
client_sync_verifier->base()); |
| 141 |
auto* server_sync_verifier = |
| 142 |
new grpc_core::testing::SyncExternalVerifier(true); |
| 143 |
ffd->server_verifier = grpc_tls_certificate_verifier_external_create( |
| 144 |
server_sync_verifier->base()); |
| 145 |
ffd->check_call_host = false; |
| 146 |
break; |
| 147 |
} |
| 148 |
case SecurityPrimitives::VerifierType::EXTERNAL_ASYNC_VERIFIER: { |
| 149 |
auto* client_async_verifier = |
| 150 |
new grpc_core::testing::AsyncExternalVerifier(true); |
| 151 |
ffd->client_verifier = grpc_tls_certificate_verifier_external_create( |
| 152 |
client_async_verifier->base()); |
| 153 |
auto* server_async_verifier = |
| 154 |
new grpc_core::testing::AsyncExternalVerifier(true); |
| 155 |
ffd->server_verifier = grpc_tls_certificate_verifier_external_create( |
| 156 |
server_async_verifier->base()); |
| 157 |
ffd->check_call_host = false; |
| 158 |
break; |
| 159 |
} |
| 160 |
case SecurityPrimitives::VerifierType::HOSTNAME_VERIFIER: { |
| 161 |
ffd->client_verifier = grpc_tls_certificate_verifier_host_name_create(); |
| 162 |
// Hostname verifier couldn't be applied to the server side, so we will |
| 163 |
// use sync external verifier here. |
| 164 |
auto* server_async_verifier = |
| 165 |
new grpc_core::testing::AsyncExternalVerifier(true); |
| 166 |
ffd->server_verifier = grpc_tls_certificate_verifier_external_create( |
| 167 |
server_async_verifier->base()); |
| 168 |
break; |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
inline void process_auth_failure(void* state, grpc_auth_context* /*ctx*/, |
| 174 |
const grpc_metadata* /*md*/, |
| 175 |
size_t /*md_count*/, |
| 176 |
grpc_process_auth_metadata_done_cb cb, |
| 177 |
void* user_data) { |
| 178 |
GPR_ASSERT(state == nullptr); |
| 179 |
cb(user_data, nullptr, 0, nullptr, 0, GRPC_STATUS_UNAUTHENTICATED, nullptr); |
| 180 |
} |
| 181 |
|
| 182 |
inline void chttp2_init_client_secure_fullstack( |
| 183 |
grpc_end2end_test_fixture* f, const grpc_channel_args* client_args, |
| 184 |
grpc_channel_credentials* creds) { |
| 185 |
fullstack_secure_fixture_data* ffd = |
| 186 |
static_cast<fullstack_secure_fixture_data*>(f->fixture_data); |
| 187 |
f->client = grpc_channel_create(ffd->localaddr.c_str(), creds, client_args); |
| 188 |
GPR_ASSERT(f->client != nullptr); |
| 189 |
grpc_channel_credentials_release(creds); |
| 190 |
} |
| 191 |
|
| 192 |
inline void chttp2_init_server_secure_fullstack( |
| 193 |
grpc_end2end_test_fixture* f, const grpc_channel_args* server_args, |
| 194 |
grpc_server_credentials* server_creds) { |
| 195 |
fullstack_secure_fixture_data* ffd = |
| 196 |
static_cast<fullstack_secure_fixture_data*>(f->fixture_data); |
| 197 |
if (f->server) { |
| 198 |
grpc_server_destroy(f->server); |
| 199 |
} |
| 200 |
f->server = grpc_server_create(server_args, nullptr); |
| 201 |
grpc_server_register_completion_queue(f->server, f->cq, nullptr); |
| 202 |
GPR_ASSERT(grpc_server_add_http2_port(f->server, ffd->localaddr.c_str(), |
| 203 |
server_creds)); |
| 204 |
grpc_server_credentials_release(server_creds); |
| 205 |
grpc_server_start(f->server); |
| 206 |
} |
| 207 |
|
| 208 |
inline void chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture* f) { |
| 209 |
fullstack_secure_fixture_data* ffd = |
| 210 |
static_cast<fullstack_secure_fixture_data*>(f->fixture_data); |
| 211 |
delete ffd; |
| 212 |
} |
| 213 |
|
| 214 |
// Create a TLS channel credential. |
| 215 |
inline grpc_channel_credentials* create_tls_channel_credentials( |
| 216 |
fullstack_secure_fixture_data* ffd) { |
| 217 |
grpc_tls_credentials_options* options = grpc_tls_credentials_options_create(); |
| 218 |
grpc_tls_credentials_options_set_verify_server_cert( |
| 219 |
options, 1 /* = verify server certs */); |
| 220 |
options->set_min_tls_version(ffd->tls_version); |
| 221 |
options->set_max_tls_version(ffd->tls_version); |
| 222 |
// Set credential provider. |
| 223 |
grpc_tls_credentials_options_set_certificate_provider(options, |
| 224 |
ffd->client_provider); |
| 225 |
grpc_tls_credentials_options_watch_root_certs(options); |
| 226 |
grpc_tls_credentials_options_watch_identity_key_cert_pairs(options); |
| 227 |
// Set credential verifier. |
| 228 |
grpc_tls_credentials_options_set_certificate_verifier(options, |
| 229 |
ffd->client_verifier); |
| 230 |
grpc_tls_credentials_options_set_check_call_host(options, |
| 231 |
ffd->check_call_host); |
| 232 |
/* Create TLS channel credentials. */ |
| 233 |
grpc_channel_credentials* creds = grpc_tls_credentials_create(options); |
| 234 |
return creds; |
| 235 |
} |
| 236 |
|
| 237 |
// Create a TLS server credential. |
| 238 |
inline grpc_server_credentials* create_tls_server_credentials( |
| 239 |
fullstack_secure_fixture_data* ffd) { |
| 240 |
grpc_tls_credentials_options* options = grpc_tls_credentials_options_create(); |
| 241 |
options->set_min_tls_version(ffd->tls_version); |
| 242 |
options->set_max_tls_version(ffd->tls_version); |
| 243 |
// Set credential provider. |
| 244 |
grpc_tls_credentials_options_set_certificate_provider(options, |
| 245 |
ffd->server_provider); |
| 246 |
grpc_tls_credentials_options_watch_root_certs(options); |
| 247 |
grpc_tls_credentials_options_watch_identity_key_cert_pairs(options); |
| 248 |
// Set client certificate request type. |
| 249 |
grpc_tls_credentials_options_set_cert_request_type( |
| 250 |
options, GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY); |
| 251 |
// Set credential verifier. |
| 252 |
grpc_tls_credentials_options_set_certificate_verifier(options, |
| 253 |
ffd->server_verifier); |
| 254 |
grpc_server_credentials* creds = grpc_tls_server_credentials_create(options); |
| 255 |
return creds; |
| 256 |
} |
| 257 |
|
| 258 |
inline void chttp2_init_client(grpc_end2end_test_fixture* f, |
| 259 |
const grpc_channel_args* client_args) { |
| 260 |
grpc_channel_credentials* ssl_creds = create_tls_channel_credentials( |
| 261 |
static_cast<fullstack_secure_fixture_data*>(f->fixture_data)); |
| 262 |
grpc_arg ssl_name_override = { |
| 263 |
GRPC_ARG_STRING, |
| 264 |
const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG), |
| 265 |
{const_cast<char*>("foo.test.google.fr")}}; |
| 266 |
const grpc_channel_args* new_client_args = |
| 267 |
grpc_channel_args_copy_and_add(client_args, &ssl_name_override, 1); |
| 268 |
chttp2_init_client_secure_fullstack(f, new_client_args, ssl_creds); |
| 269 |
grpc_channel_args_destroy(new_client_args); |
| 270 |
} |
| 271 |
|
| 272 |
inline int fail_server_auth_check(const grpc_channel_args* server_args) { |
| 273 |
size_t i; |
| 274 |
if (server_args == nullptr) return 0; |
| 275 |
for (i = 0; i < server_args->num_args; i++) { |
| 276 |
if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) == |
| 277 |
0) { |
| 278 |
return 1; |
| 279 |
} |
| 280 |
} |
| 281 |
return 0; |
| 282 |
} |
| 283 |
|
| 284 |
inline void chttp2_init_server(grpc_end2end_test_fixture* f, |
| 285 |
const grpc_channel_args* server_args) { |
| 286 |
grpc_server_credentials* ssl_creds = create_tls_server_credentials( |
| 287 |
static_cast<fullstack_secure_fixture_data*>(f->fixture_data)); |
| 288 |
if (fail_server_auth_check(server_args)) { |
| 289 |
grpc_auth_metadata_processor processor = {process_auth_failure, nullptr, |
| 290 |
nullptr}; |
| 291 |
grpc_server_credentials_set_auth_metadata_processor(ssl_creds, processor); |
| 292 |
} |
| 293 |
chttp2_init_server_secure_fullstack(f, server_args, ssl_creds); |
| 294 |
} |
| 295 |
|
| 296 |
static const uint32_t kH2TLSFeatureMask = |
| 297 |
FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION | |
| 298 |
FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS | |
| 299 |
FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL | |
| 300 |
FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER; |
| 301 |
|
| 302 |
#endif |