Debian Codegraph @

test/core/end2end/fixtures/h2_oauth2_tls12.cc

raw
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 <stdio.h>
20 #include <string.h>
21
22 #include <string>
23
24 #include <grpc/grpc.h>
25 #include <grpc/grpc_security.h>
26 #include <grpc/grpc_security_constants.h>
27 #include <grpc/slice.h>
28 #include <grpc/status.h>
29 #include <grpc/support/alloc.h>
30 #include <grpc/support/log.h>
31
32 #include "src/core/lib/channel/channel_args.h"
33 #include "src/core/lib/gprpp/host_port.h"
34 #include "src/core/lib/iomgr/error.h"
35 #include "src/core/lib/iomgr/exec_ctx.h"
36 #include "src/core/lib/iomgr/load_file.h"
37 #include "src/core/lib/security/credentials/credentials.h"
38 #include "src/core/lib/security/credentials/ssl/ssl_credentials.h"
39 #include "test/core/end2end/end2end_tests.h"
40 #include "test/core/util/port.h"
41 #include "test/core/util/test_config.h"
42
43 #define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
44 #define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem"
45 #define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key"
46
47 static const char oauth2_md[] = "Bearer aaslkfjs424535asdf";
48 static const char* client_identity_property_name = "smurf_name";
49 static const char* client_identity = "Brainy Smurf";
50
51 struct fullstack_secure_fixture_data {
52 std::string localaddr;
53 grpc_tls_version tls_version;
54 };
55
56 static const grpc_metadata* find_metadata(const grpc_metadata* md,
57 size_t md_count, const char* key,
58 const char* value) {
59 size_t i;
60 for (i = 0; i < md_count; i++) {
61 if (grpc_slice_str_cmp(md[i].key, key) == 0 &&
62 grpc_slice_str_cmp(md[i].value, value) == 0) {
63 return &md[i];
64 }
65 }
66 return nullptr;
67 }
68
69 typedef struct {
70 size_t pseudo_refcount;
71 } test_processor_state;
72
73 static void process_oauth2_success(void* state, grpc_auth_context* ctx,
74 const grpc_metadata* md, size_t md_count,
75 grpc_process_auth_metadata_done_cb cb,
76 void* user_data) {
77 const grpc_metadata* oauth2 =
78 find_metadata(md, md_count, "authorization", oauth2_md);
79 test_processor_state* s;
80
81 GPR_ASSERT(state != nullptr);
82 s = static_cast<test_processor_state*>(state);
83 GPR_ASSERT(s->pseudo_refcount == 1);
84 GPR_ASSERT(oauth2 != nullptr);
85 grpc_auth_context_add_cstring_property(ctx, client_identity_property_name,
86 client_identity);
87 GPR_ASSERT(grpc_auth_context_set_peer_identity_property_name(
88 ctx, client_identity_property_name) == 1);
89 cb(user_data, oauth2, 1, nullptr, 0, GRPC_STATUS_OK, nullptr);
90 }
91
92 static void process_oauth2_failure(void* state, grpc_auth_context* /*ctx*/,
93 const grpc_metadata* md, size_t md_count,
94 grpc_process_auth_metadata_done_cb cb,
95 void* user_data) {
96 const grpc_metadata* oauth2 =
97 find_metadata(md, md_count, "authorization", oauth2_md);
98 test_processor_state* s;
99 GPR_ASSERT(state != nullptr);
100 s = static_cast<test_processor_state*>(state);
101 GPR_ASSERT(s->pseudo_refcount == 1);
102 GPR_ASSERT(oauth2 != nullptr);
103 cb(user_data, oauth2, 1, nullptr, 0, GRPC_STATUS_UNAUTHENTICATED, nullptr);
104 }
105
106 static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack(
107 const grpc_channel_args* /*client_args*/,
108 const grpc_channel_args* /*server_args*/, grpc_tls_version tls_version) {
109 grpc_end2end_test_fixture f;
110 int port = grpc_pick_unused_port_or_die();
111 fullstack_secure_fixture_data* ffd = new fullstack_secure_fixture_data();
112 memset(&f, 0, sizeof(f));
113 ffd->localaddr = grpc_core::JoinHostPort("localhost", port);
114 ffd->tls_version = tls_version;
115 f.fixture_data = ffd;
116 f.cq = grpc_completion_queue_create_for_next(nullptr);
117 return f;
118 }
119
120 static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack_tls1_2(
121 const grpc_channel_args* client_args,
122 const grpc_channel_args* server_args) {
123 return chttp2_create_fixture_secure_fullstack(client_args, server_args,
124 grpc_tls_version::TLS1_2);
125 }
126
127 static void chttp2_init_client_secure_fullstack(
128 grpc_end2end_test_fixture* f, const grpc_channel_args* client_args,
129 grpc_channel_credentials* creds) {
130 fullstack_secure_fixture_data* ffd =
131 static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
132 f->client = grpc_channel_create(ffd->localaddr.c_str(), creds, client_args);
133 GPR_ASSERT(f->client != nullptr);
134 grpc_channel_credentials_release(creds);
135 }
136
137 static void chttp2_init_server_secure_fullstack(
138 grpc_end2end_test_fixture* f, const grpc_channel_args* server_args,
139 grpc_server_credentials* server_creds) {
140 fullstack_secure_fixture_data* ffd =
141 static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
142 if (f->server) {
143 grpc_server_destroy(f->server);
144 }
145 f->server = grpc_server_create(server_args, nullptr);
146 grpc_server_register_completion_queue(f->server, f->cq, nullptr);
147 GPR_ASSERT(grpc_server_add_http2_port(f->server, ffd->localaddr.c_str(),
148 server_creds));
149 grpc_server_credentials_release(server_creds);
150 grpc_server_start(f->server);
151 }
152
153 void chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture* f) {
154 fullstack_secure_fixture_data* ffd =
155 static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
156 delete ffd;
157 }
158
159 static void chttp2_init_client_simple_ssl_with_oauth2_secure_fullstack(
160 grpc_end2end_test_fixture* f, const grpc_channel_args* client_args) {
161 grpc_core::ExecCtx exec_ctx;
162 grpc_slice ca_slice;
163 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
164 grpc_load_file(CA_CERT_PATH, 1, &ca_slice)));
165 const char* test_root_cert =
166 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
167 grpc_channel_credentials* ssl_creds =
168 grpc_ssl_credentials_create(test_root_cert, nullptr, nullptr, nullptr);
169 if (f != nullptr && ssl_creds != nullptr) {
170 // Set the min and max TLS version.
171 grpc_ssl_credentials* creds =
172 reinterpret_cast<grpc_ssl_credentials*>(ssl_creds);
173 fullstack_secure_fixture_data* ffd =
174 static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
175 creds->set_min_tls_version(ffd->tls_version);
176 creds->set_max_tls_version(ffd->tls_version);
177 }
178 grpc_call_credentials* oauth2_creds =
179 grpc_md_only_test_credentials_create("authorization", oauth2_md);
180 grpc_channel_credentials* ssl_oauth2_creds =
181 grpc_composite_channel_credentials_create(ssl_creds, oauth2_creds,
182 nullptr);
183 grpc_arg ssl_name_override = {
184 GRPC_ARG_STRING,
185 const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
186 {const_cast<char*>("foo.test.google.fr")}};
187 const grpc_channel_args* new_client_args =
188 grpc_channel_args_copy_and_add(client_args, &ssl_name_override, 1);
189 chttp2_init_client_secure_fullstack(f, new_client_args, ssl_oauth2_creds);
190 grpc_channel_args_destroy(new_client_args);
191 grpc_channel_credentials_release(ssl_creds);
192 grpc_call_credentials_release(oauth2_creds);
193 grpc_slice_unref(ca_slice);
194 }
195
196 static int fail_server_auth_check(const grpc_channel_args* server_args) {
197 size_t i;
198 if (server_args == nullptr) return 0;
199 for (i = 0; i < server_args->num_args; i++) {
200 if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) ==
201 0) {
202 return 1;
203 }
204 }
205 return 0;
206 }
207
208 static void processor_destroy(void* state) {
209 test_processor_state* s = static_cast<test_processor_state*>(state);
210 GPR_ASSERT((s->pseudo_refcount--) == 1);
211 gpr_free(s);
212 }
213
214 static grpc_auth_metadata_processor test_processor_create(int failing) {
215 test_processor_state* s =
216 static_cast<test_processor_state*>(gpr_malloc(sizeof(*s)));
217 grpc_auth_metadata_processor result;
218 s->pseudo_refcount = 1;
219 result.state = s;
220 result.destroy = processor_destroy;
221 if (failing) {
222 result.process = process_oauth2_failure;
223 } else {
224 result.process = process_oauth2_success;
225 }
226 return result;
227 }
228
229 static void chttp2_init_server_simple_ssl_secure_fullstack(
230 grpc_end2end_test_fixture* f, const grpc_channel_args* server_args) {
231 grpc_slice cert_slice, key_slice;
232 GPR_ASSERT(GRPC_LOG_IF_ERROR(
233 "load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice)));
234 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
235 grpc_load_file(SERVER_KEY_PATH, 1, &key_slice)));
236 const char* server_cert =
237 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
238 const char* server_key =
239 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
240 grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key, server_cert};
241 grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create(
242 nullptr, &pem_key_cert_pair, 1, 0, nullptr);
243 if (f != nullptr && ssl_creds != nullptr) {
244 // Set the min and max TLS version.
245 grpc_ssl_server_credentials* creds =
246 reinterpret_cast<grpc_ssl_server_credentials*>(ssl_creds);
247 fullstack_secure_fixture_data* ffd =
248 static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
249 creds->set_min_tls_version(ffd->tls_version);
250 creds->set_max_tls_version(ffd->tls_version);
251 }
252 grpc_server_credentials_set_auth_metadata_processor(
253 ssl_creds, test_processor_create(fail_server_auth_check(server_args)));
254 chttp2_init_server_secure_fullstack(f, server_args, ssl_creds);
255 grpc_slice_unref(cert_slice);
256 grpc_slice_unref(key_slice);
257 }
258
259 /* All test configurations */
260
261 static grpc_end2end_test_config configs[] = {
262 {"chttp2/simple_ssl_with_oauth2_fullstack_tls1_2",
263 FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION |
264 FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS |
265 FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL |
266 FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER,
267 "foo.test.google.fr", chttp2_create_fixture_secure_fullstack_tls1_2,
268 chttp2_init_client_simple_ssl_with_oauth2_secure_fullstack,
269 chttp2_init_server_simple_ssl_secure_fullstack,
270 chttp2_tear_down_secure_fullstack},
271 };
272
273 int main(int argc, char** argv) {
274 size_t i;
275 grpc::testing::TestEnvironment env(&argc, argv);
276 grpc_end2end_tests_pre_init();
277 grpc_init();
278
279 for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
280 grpc_end2end_tests(argc, argv, configs[i]);
281 }
282
283 grpc_shutdown();
284
285 return 0;
286 }