| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 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 |
#include <string.h> |
| 20 |
|
| 21 |
#include <string> |
| 22 |
|
| 23 |
#include <grpc/grpc.h> |
| 24 |
#include <grpc/grpc_security.h> |
| 25 |
#include <grpc/grpc_security_constants.h> |
| 26 |
#include <grpc/slice.h> |
| 27 |
#include <grpc/status.h> |
| 28 |
#include <grpc/support/log.h> |
| 29 |
|
| 30 |
#include "src/core/lib/channel/channel_args.h" |
| 31 |
#include "src/core/lib/gprpp/global_config_generic.h" |
| 32 |
#include "src/core/lib/gprpp/host_port.h" |
| 33 |
#include "src/core/lib/iomgr/error.h" |
| 34 |
#include "src/core/lib/iomgr/load_file.h" |
| 35 |
#include "src/core/lib/security/credentials/ssl/ssl_credentials.h" |
| 36 |
#include "src/core/lib/security/security_connector/ssl_utils_config.h" |
| 37 |
#include "test/core/end2end/end2end_tests.h" |
| 38 |
#include "test/core/util/port.h" |
| 39 |
#include "test/core/util/test_config.h" |
| 40 |
|
| 41 |
#define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem" |
| 42 |
#define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem" |
| 43 |
#define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key" |
| 44 |
|
| 45 |
struct fullstack_secure_fixture_data { |
| 46 |
std::string localaddr; |
| 47 |
grpc_tls_version tls_version; |
| 48 |
}; |
| 49 |
|
| 50 |
static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack( |
| 51 |
const grpc_channel_args* /*client_args*/, |
| 52 |
const grpc_channel_args* /*server_args*/, grpc_tls_version tls_version) { |
| 53 |
grpc_end2end_test_fixture f; |
| 54 |
int port = grpc_pick_unused_port_or_die(); |
| 55 |
fullstack_secure_fixture_data* ffd = new fullstack_secure_fixture_data(); |
| 56 |
memset(&f, 0, sizeof(f)); |
| 57 |
|
| 58 |
ffd->localaddr = grpc_core::JoinHostPort("localhost", port); |
| 59 |
ffd->tls_version = tls_version; |
| 60 |
|
| 61 |
f.fixture_data = ffd; |
| 62 |
f.cq = grpc_completion_queue_create_for_next(nullptr); |
| 63 |
|
| 64 |
return f; |
| 65 |
} |
| 66 |
|
| 67 |
static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack_tls1_3( |
| 68 |
const grpc_channel_args* client_args, |
| 69 |
const grpc_channel_args* server_args) { |
| 70 |
return chttp2_create_fixture_secure_fullstack(client_args, server_args, |
| 71 |
grpc_tls_version::TLS1_3); |
| 72 |
} |
| 73 |
|
| 74 |
static void process_auth_failure(void* state, grpc_auth_context* /*ctx*/, |
| 75 |
const grpc_metadata* /*md*/, |
| 76 |
size_t /*md_count*/, |
| 77 |
grpc_process_auth_metadata_done_cb cb, |
| 78 |
void* user_data) { |
| 79 |
GPR_ASSERT(state == nullptr); |
| 80 |
cb(user_data, nullptr, 0, nullptr, 0, GRPC_STATUS_UNAUTHENTICATED, nullptr); |
| 81 |
} |
| 82 |
|
| 83 |
static void chttp2_init_client_secure_fullstack( |
| 84 |
grpc_end2end_test_fixture* f, const grpc_channel_args* client_args, |
| 85 |
grpc_channel_credentials* creds) { |
| 86 |
fullstack_secure_fixture_data* ffd = |
| 87 |
static_cast<fullstack_secure_fixture_data*>(f->fixture_data); |
| 88 |
f->client = grpc_channel_create(ffd->localaddr.c_str(), creds, client_args); |
| 89 |
GPR_ASSERT(f->client != nullptr); |
| 90 |
grpc_channel_credentials_release(creds); |
| 91 |
} |
| 92 |
|
| 93 |
static void chttp2_init_server_secure_fullstack( |
| 94 |
grpc_end2end_test_fixture* f, const grpc_channel_args* server_args, |
| 95 |
grpc_server_credentials* server_creds) { |
| 96 |
fullstack_secure_fixture_data* ffd = |
| 97 |
static_cast<fullstack_secure_fixture_data*>(f->fixture_data); |
| 98 |
if (f->server) { |
| 99 |
grpc_server_destroy(f->server); |
| 100 |
} |
| 101 |
f->server = grpc_server_create(server_args, nullptr); |
| 102 |
grpc_server_register_completion_queue(f->server, f->cq, nullptr); |
| 103 |
GPR_ASSERT(grpc_server_add_http2_port(f->server, ffd->localaddr.c_str(), |
| 104 |
server_creds)); |
| 105 |
grpc_server_credentials_release(server_creds); |
| 106 |
grpc_server_start(f->server); |
| 107 |
} |
| 108 |
|
| 109 |
void chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture* f) { |
| 110 |
fullstack_secure_fixture_data* ffd = |
| 111 |
static_cast<fullstack_secure_fixture_data*>(f->fixture_data); |
| 112 |
delete ffd; |
| 113 |
} |
| 114 |
|
| 115 |
static void chttp2_init_client_simple_ssl_secure_fullstack( |
| 116 |
grpc_end2end_test_fixture* f, const grpc_channel_args* client_args) { |
| 117 |
grpc_channel_credentials* ssl_creds = |
| 118 |
grpc_ssl_credentials_create(nullptr, nullptr, nullptr, nullptr); |
| 119 |
if (f != nullptr && ssl_creds != nullptr) { |
| 120 |
// Set the min and max TLS version. |
| 121 |
grpc_ssl_credentials* creds = |
| 122 |
reinterpret_cast<grpc_ssl_credentials*>(ssl_creds); |
| 123 |
fullstack_secure_fixture_data* ffd = |
| 124 |
static_cast<fullstack_secure_fixture_data*>(f->fixture_data); |
| 125 |
creds->set_min_tls_version(ffd->tls_version); |
| 126 |
creds->set_max_tls_version(ffd->tls_version); |
| 127 |
} |
| 128 |
grpc_arg ssl_name_override = { |
| 129 |
GRPC_ARG_STRING, |
| 130 |
const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG), |
| 131 |
{const_cast<char*>("foo.test.google.fr")}}; |
| 132 |
const grpc_channel_args* new_client_args = |
| 133 |
grpc_channel_args_copy_and_add(client_args, &ssl_name_override, 1); |
| 134 |
chttp2_init_client_secure_fullstack(f, new_client_args, ssl_creds); |
| 135 |
grpc_channel_args_destroy(new_client_args); |
| 136 |
} |
| 137 |
|
| 138 |
static int fail_server_auth_check(const grpc_channel_args* server_args) { |
| 139 |
size_t i; |
| 140 |
if (server_args == nullptr) return 0; |
| 141 |
for (i = 0; i < server_args->num_args; i++) { |
| 142 |
if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) == |
| 143 |
0) { |
| 144 |
return 1; |
| 145 |
} |
| 146 |
} |
| 147 |
return 0; |
| 148 |
} |
| 149 |
|
| 150 |
static void chttp2_init_server_simple_ssl_secure_fullstack( |
| 151 |
grpc_end2end_test_fixture* f, const grpc_channel_args* server_args) { |
| 152 |
grpc_slice cert_slice, key_slice; |
| 153 |
GPR_ASSERT(GRPC_LOG_IF_ERROR( |
| 154 |
"load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice))); |
| 155 |
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file", |
| 156 |
grpc_load_file(SERVER_KEY_PATH, 1, &key_slice))); |
| 157 |
const char* server_cert = |
| 158 |
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice); |
| 159 |
const char* server_key = |
| 160 |
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice); |
| 161 |
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key, server_cert}; |
| 162 |
grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create( |
| 163 |
nullptr, &pem_key_cert_pair, 1, 0, nullptr); |
| 164 |
if (f != nullptr && ssl_creds != nullptr) { |
| 165 |
// Set the min and max TLS version. |
| 166 |
grpc_ssl_server_credentials* creds = |
| 167 |
reinterpret_cast<grpc_ssl_server_credentials*>(ssl_creds); |
| 168 |
fullstack_secure_fixture_data* ffd = |
| 169 |
static_cast<fullstack_secure_fixture_data*>(f->fixture_data); |
| 170 |
creds->set_min_tls_version(ffd->tls_version); |
| 171 |
creds->set_max_tls_version(ffd->tls_version); |
| 172 |
} |
| 173 |
grpc_slice_unref(cert_slice); |
| 174 |
grpc_slice_unref(key_slice); |
| 175 |
if (fail_server_auth_check(server_args)) { |
| 176 |
grpc_auth_metadata_processor processor = {process_auth_failure, nullptr, |
| 177 |
nullptr}; |
| 178 |
grpc_server_credentials_set_auth_metadata_processor(ssl_creds, processor); |
| 179 |
} |
| 180 |
chttp2_init_server_secure_fullstack(f, server_args, ssl_creds); |
| 181 |
} |
| 182 |
|
| 183 |
/* All test configurations */ |
| 184 |
|
| 185 |
static grpc_end2end_test_config configs[] = { |
| 186 |
{"chttp2/simple_ssl_fullstack_tls1_3", |
| 187 |
FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION | |
| 188 |
FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS | |
| 189 |
FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL | |
| 190 |
FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER | |
| 191 |
FEATURE_MASK_DOES_NOT_SUPPORT_CLIENT_HANDSHAKE_COMPLETE_FIRST, |
| 192 |
"foo.test.google.fr", chttp2_create_fixture_secure_fullstack_tls1_3, |
| 193 |
chttp2_init_client_simple_ssl_secure_fullstack, |
| 194 |
chttp2_init_server_simple_ssl_secure_fullstack, |
| 195 |
chttp2_tear_down_secure_fullstack}, |
| 196 |
}; |
| 197 |
|
| 198 |
int main(int argc, char** argv) { |
| 199 |
size_t i; |
| 200 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 201 |
grpc_end2end_tests_pre_init(); |
| 202 |
GPR_GLOBAL_CONFIG_SET(grpc_default_ssl_roots_file_path, CA_CERT_PATH); |
| 203 |
|
| 204 |
grpc_init(); |
| 205 |
|
| 206 |
for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) { |
| 207 |
grpc_end2end_tests(argc, argv, configs[i]); |
| 208 |
} |
| 209 |
|
| 210 |
grpc_shutdown(); |
| 211 |
return 0; |
| 212 |
} |