Debian Codegraph @

crt/aws-c-io/source/tls_channel_handler.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/io/channel.h>
7 #include <aws/io/file_utils.h>
8 #include <aws/io/logging.h>
9 #include <aws/io/pkcs11.h>
10 #include <aws/io/private/pem_utils.h>
11 #include <aws/io/private/tls_channel_handler_private.h>
12 #include <aws/io/private/tls_channel_handler_shared.h>
13 #include <aws/io/tls_channel_handler.h>
14
15 #define AWS_DEFAULT_TLS_TIMEOUT_MS 10000
16
17 #include "./pkcs11_private.h"
18
19 #include <aws/common/environment.h>
20 #include <aws/common/string.h>
21
22 /* Global TLS v-table, populated at init time with the platform-specific TLS implementation. */
23 static struct aws_tls_vtable s_tls_channel_handler_vtable;
24
25 void aws_tls_ctx_options_init_default_client(struct aws_tls_ctx_options *options, struct aws_allocator *allocator) {
26 AWS_ZERO_STRUCT(*options);
27 options->allocator = allocator;
28 options->minimum_tls_version = AWS_IO_TLS_VER_SYS_DEFAULTS;
29 options->cipher_pref = AWS_IO_TLS_CIPHER_PREF_SYSTEM_DEFAULT;
30 options->verify_peer = true;
31 options->max_fragment_size = g_aws_channel_max_fragment_size;
32
33 #ifdef __APPLE__
34
35 if (aws_is_using_secitem()) {
36 options->secitem_options.cert_label = aws_string_new_from_c_str(allocator, "aws-crt-default-certificate-label");
37 options->secitem_options.key_label = aws_string_new_from_c_str(allocator, "aws-crt-default-key-label");
38 }
39
40 #endif /* __APPLE__ */
41 }
42
43 void aws_tls_ctx_options_clean_up(struct aws_tls_ctx_options *options) {
44 aws_byte_buf_clean_up(&options->ca_file);
45 aws_string_destroy(options->ca_path);
46 aws_byte_buf_clean_up(&options->certificate);
47 aws_byte_buf_clean_up_secure(&options->private_key);
48
49 #ifdef __APPLE__
50
51 aws_byte_buf_clean_up_secure(&options->pkcs12);
52 aws_byte_buf_clean_up_secure(&options->pkcs12_password);
53 aws_tls_secitem_options_clean_up(&options->secitem_options);
54 aws_string_destroy(options->keychain_path);
55
56 #endif /* __APPLE__ */
57
58 aws_string_destroy(options->alpn_list);
59 aws_custom_key_op_handler_release(options->custom_key_op_handler);
60
61 AWS_ZERO_STRUCT(*options);
62 }
63
64 int aws_tls_ctx_options_init_client_mtls(
65 struct aws_tls_ctx_options *options,
66 struct aws_allocator *allocator,
67 const struct aws_byte_cursor *cert,
68 const struct aws_byte_cursor *pkey) {
69
70 aws_tls_ctx_options_init_default_client(options, allocator);
71
72 if (aws_byte_buf_init_copy_from_cursor(&options->certificate, allocator, *cert)) {
73 goto error;
74 }
75
76 if (aws_sanitize_pem(&options->certificate, allocator)) {
77 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: Invalid certificate. File must contain PEM encoded data");
78 goto error;
79 }
80
81 if (aws_byte_buf_init_copy_from_cursor(&options->private_key, allocator, *pkey)) {
82 goto error;
83 }
84
85 if (aws_sanitize_pem(&options->private_key, allocator)) {
86 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: Invalid private key. File must contain PEM encoded data");
87 goto error;
88 }
89
90 return AWS_OP_SUCCESS;
91 error:
92 aws_tls_ctx_options_clean_up(options);
93 return AWS_OP_ERR;
94 }
95
96 int aws_tls_ctx_options_init_client_mtls_from_path(
97 struct aws_tls_ctx_options *options,
98 struct aws_allocator *allocator,
99 const char *cert_path,
100 const char *pkey_path) {
101 aws_tls_ctx_options_init_default_client(options, allocator);
102
103 if (aws_byte_buf_init_from_file(&options->certificate, allocator, cert_path)) {
104 goto error;
105 }
106
107 if (aws_sanitize_pem(&options->certificate, allocator)) {
108 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: Invalid certificate. File must contain PEM encoded data");
109 goto error;
110 }
111
112 if (aws_byte_buf_init_from_file(&options->private_key, allocator, pkey_path)) {
113 goto error;
114 }
115
116 if (aws_sanitize_pem(&options->private_key, allocator)) {
117 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: Invalid private key. File must contain PEM encoded data");
118 goto error;
119 }
120
121 return AWS_OP_SUCCESS;
122 error:
123 aws_tls_ctx_options_clean_up(options);
124 return AWS_OP_ERR;
125 }
126
127 int aws_tls_ctx_options_init_client_mtls_with_custom_key_operations(
128 struct aws_tls_ctx_options *options,
129 struct aws_allocator *allocator,
130 struct aws_custom_key_op_handler *custom,
131 const struct aws_byte_cursor *cert_file_contents) {
132
133 #if !USE_S2N
134 (void)options;
135 (void)allocator;
136 (void)custom;
137 (void)cert_file_contents;
138 AWS_ZERO_STRUCT(*options);
139 AWS_LOGF_ERROR(
140 AWS_LS_IO_TLS, "static: This platform does not currently support TLS with custom private key operations.");
141 return aws_raise_error(AWS_ERROR_UNIMPLEMENTED);
142 #else
143
144 aws_tls_ctx_options_init_default_client(options, allocator);
145
146 /* on_key_operation is required */
147 AWS_ASSERT(custom != NULL);
148 AWS_ASSERT(custom->vtable != NULL);
149 AWS_ASSERT(custom->vtable->on_key_operation != NULL);
150
151 /* Hold a reference to the custom key operation handler so it cannot be destroyed */
152 options->custom_key_op_handler = aws_custom_key_op_handler_acquire((struct aws_custom_key_op_handler *)custom);
153
154 /* Copy the certificate data from the cursor */
155 AWS_ASSERT(cert_file_contents != NULL);
156 aws_byte_buf_init_copy_from_cursor(&options->certificate, allocator, *cert_file_contents);
157
158 /* Make sure the certificate is set and valid */
159 if (aws_sanitize_pem(&options->certificate, allocator)) {
160 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: Invalid certificate. File must contain PEM encoded data");
161 goto error;
162 }
163
164 return AWS_OP_SUCCESS;
165
166 error:
167 aws_tls_ctx_options_clean_up(options);
168 return AWS_OP_ERR;
169
170 #endif /* PLATFORM-SUPPORTS-CUSTOM-KEY-OPERATIONS */
171 }
172
173 int aws_tls_ctx_options_init_client_mtls_with_pkcs11(
174 struct aws_tls_ctx_options *options,
175 struct aws_allocator *allocator,
176 const struct aws_tls_ctx_pkcs11_options *pkcs11_options) {
177
178 #if defined(USE_S2N)
179
180 struct aws_custom_key_op_handler *pkcs11_handler = aws_pkcs11_tls_op_handler_new(
181 allocator,
182 pkcs11_options->pkcs11_lib,
183 &pkcs11_options->user_pin,
184 &pkcs11_options->token_label,
185 &pkcs11_options->private_key_object_label,
186 pkcs11_options->slot_id);
187
188 struct aws_byte_buf tmp_cert_buf;
189 AWS_ZERO_STRUCT(tmp_cert_buf);
190 bool success = false;
191 int custom_key_result = AWS_OP_ERR;
192
193 if (pkcs11_handler == NULL) {
194 goto finish;
195 }
196
197 if ((pkcs11_options->cert_file_contents.ptr != NULL) && (pkcs11_options->cert_file_path.ptr != NULL)) {
198 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: Cannot use certificate AND certificate file path, only one can be set");
199 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
200 goto finish;
201 } else if (pkcs11_options->cert_file_contents.ptr != NULL) {
202 custom_key_result = aws_tls_ctx_options_init_client_mtls_with_custom_key_operations(
203 options, allocator, pkcs11_handler, &pkcs11_options->cert_file_contents);
204 success = true;
205 } else {
206 struct aws_string *tmp_string = aws_string_new_from_cursor(allocator, &pkcs11_options->cert_file_path);
207 int op = aws_byte_buf_init_from_file(&tmp_cert_buf, allocator, aws_string_c_str(tmp_string));
208 aws_string_destroy(tmp_string);
209
210 if (op != AWS_OP_SUCCESS) {
211 goto finish;
212 }
213
214 struct aws_byte_cursor tmp_cursor = aws_byte_cursor_from_buf(&tmp_cert_buf);
215 custom_key_result = aws_tls_ctx_options_init_client_mtls_with_custom_key_operations(
216 options, allocator, pkcs11_handler, &tmp_cursor);
217 success = true;
218 }
219
220 finish:
221
222 if (pkcs11_handler != NULL) {
223 /**
224 * Calling aws_tls_ctx_options_init_client_mtls_with_custom_key_operations will have this options
225 * hold a reference to the custom key operations, but creating the TLS operations handler using
226 * aws_pkcs11_tls_op_handler_set_certificate_data adds a reference too, so we need to release
227 * this reference so the only thing (currently) holding a reference is the TLS options itself and
228 * not this function.
229 */
230 aws_custom_key_op_handler_release(pkcs11_handler);
231 }
232 if (success == false) {
233 aws_tls_ctx_options_clean_up(options);
234 }
235 aws_byte_buf_clean_up(&tmp_cert_buf);
236
237 if (success) {
238 return custom_key_result;
239 } else {
240 return AWS_OP_ERR;
241 }
242
243 #else /* Platform does not support S2N */
244
245 (void)allocator;
246 (void)pkcs11_options;
247 AWS_ZERO_STRUCT(*options);
248 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: This platform does not currently support TLS with PKCS#11.");
249 return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED);
250
251 #endif /* PLATFORM-SUPPORTS-PKCS11-TLS */
252 }
253
254 int aws_tls_ctx_options_set_keychain_path(
255 struct aws_tls_ctx_options *options,
256 struct aws_byte_cursor *keychain_path_cursor) {
257
258 #if defined(__APPLE__)
259
260 if (aws_is_using_secitem()) {
261 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: Keychain path cannot be set when using Secitem.");
262 return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED);
263 }
264
265 AWS_LOGF_WARN(AWS_LS_IO_TLS, "static: Keychain path is deprecated.");
266
267 options->keychain_path = aws_string_new_from_cursor(options->allocator, keychain_path_cursor);
268 if (!options->keychain_path) {
269 return AWS_OP_ERR;
270 }
271 return AWS_OP_SUCCESS;
272
273 #endif /* __APPLE__*/
274
275 (void)options;
276 (void)keychain_path_cursor;
277 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: Keychain path can only be set on MacOS.");
278 return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED);
279 }
280
281 #ifdef __APPLE__
282
283 int aws_tls_ctx_options_set_secitem_options(
284 struct aws_tls_ctx_options *tls_ctx_options,
285 const struct aws_secitem_options *secitem_options) {
286 if (aws_is_using_secitem()) {
287
288 if (secitem_options->cert_label != NULL) {
289 aws_string_destroy(tls_ctx_options->secitem_options.cert_label);
290 tls_ctx_options->secitem_options.cert_label = NULL;
291 tls_ctx_options->secitem_options.cert_label =
292 aws_string_new_from_string(tls_ctx_options->allocator, secitem_options->cert_label);
293 if (tls_ctx_options->secitem_options.cert_label == NULL) {
294 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: Secitem option certificate label is invalid.");
295 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
296 }
297 AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "static: Secitem option certificate label set.");
298 }
299
300 if (secitem_options->key_label != NULL) {
301 aws_string_destroy(tls_ctx_options->secitem_options.key_label);
302 tls_ctx_options->secitem_options.key_label = NULL;
303 tls_ctx_options->secitem_options.key_label =
304 aws_string_new_from_string(tls_ctx_options->allocator, secitem_options->key_label);
305 if (tls_ctx_options->secitem_options.key_label == NULL) {
306 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: Secitem option key label is invalid.");
307 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
308 }
309 AWS_LOGF_DEBUG(AWS_LS_IO_TLS, "static: Secitem option key label set.");
310 }
311 return AWS_OP_SUCCESS;
312 }
313
314 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "Secitem options can only be set when using Secitem.");
315 return AWS_OP_ERR;
316 }
317
318 #else /* __APPLE__ */
319
320 int aws_tls_ctx_options_set_secitem_options(
321 struct aws_tls_ctx_options *tls_ctx_options,
322 const struct aws_secitem_options *secitem_options) {
323 (void)tls_ctx_options;
324 (void)secitem_options;
325 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: Setting of secitem options only supported on Apple.");
326 return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED);
327 }
328
329 #endif
330
331 void aws_tls_secitem_options_clean_up(struct aws_secitem_options *secitem_options) {
332 if (secitem_options == NULL) {
333 return;
334 }
335
336 aws_string_destroy(secitem_options->cert_label);
337 aws_string_destroy(secitem_options->key_label);
338 }
339
340 int aws_tls_ctx_options_init_client_mtls_from_system_path(
341 struct aws_tls_ctx_options *options,
342 struct aws_allocator *allocator,
343 const char *cert_reg_path) {
344
345 #ifdef _WIN32
346 aws_tls_ctx_options_init_default_client(options, allocator);
347 options->system_certificate_path = cert_reg_path;
348 return AWS_OP_SUCCESS;
349 #else
350 (void)allocator;
351 (void)cert_reg_path;
352 AWS_ZERO_STRUCT(*options);
353 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: System certificate path can only be set on Windows.");
354 return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED);
355 #endif
356 }
357
358 int aws_tls_ctx_options_init_default_server_from_system_path(
359 struct aws_tls_ctx_options *options,
360 struct aws_allocator *allocator,
361 const char *cert_reg_path) {
362 if (aws_tls_ctx_options_init_client_mtls_from_system_path(options, allocator, cert_reg_path)) {
363 return AWS_OP_ERR;
364 }
365 options->verify_peer = false;
366 return AWS_OP_SUCCESS;
367 }
368
369 int aws_tls_ctx_options_init_client_mtls_pkcs12_from_path(
370 struct aws_tls_ctx_options *options,
371 struct aws_allocator *allocator,
372 const char *pkcs12_path,
373 const struct aws_byte_cursor *pkcs_pwd) {
374
375 #ifdef __APPLE__
376 aws_tls_ctx_options_init_default_client(options, allocator);
377
378 if (aws_byte_buf_init_from_file(&options->pkcs12, allocator, pkcs12_path)) {
379 return AWS_OP_ERR;
380 }
381
382 if (aws_byte_buf_init_copy_from_cursor(&options->pkcs12_password, allocator, *pkcs_pwd)) {
383 aws_byte_buf_clean_up_secure(&options->pkcs12);
384 return AWS_OP_ERR;
385 }
386
387 return AWS_OP_SUCCESS;
388 #else
389 (void)allocator;
390 (void)pkcs12_path;
391 (void)pkcs_pwd;
392 AWS_ZERO_STRUCT(*options);
393 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: This platform does not support PKCS#12 files.");
394 return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED);
395 #endif
396 }
397
398 int aws_tls_ctx_options_init_client_mtls_pkcs12(
399 struct aws_tls_ctx_options *options,
400 struct aws_allocator *allocator,
401 struct aws_byte_cursor *pkcs12,
402 struct aws_byte_cursor *pkcs_pwd) {
403
404 #ifdef __APPLE__
405 aws_tls_ctx_options_init_default_client(options, allocator);
406
407 if (aws_byte_buf_init_copy_from_cursor(&options->pkcs12, allocator, *pkcs12)) {
408 return AWS_OP_ERR;
409 }
410
411 if (aws_byte_buf_init_copy_from_cursor(&options->pkcs12_password, allocator, *pkcs_pwd)) {
412 aws_byte_buf_clean_up_secure(&options->pkcs12);
413 return AWS_OP_ERR;
414 }
415
416 return AWS_OP_SUCCESS;
417 #else
418 (void)allocator;
419 (void)pkcs12;
420 (void)pkcs_pwd;
421 AWS_ZERO_STRUCT(*options);
422 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: This platform does not support PKCS#12 files.");
423 return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED);
424 #endif
425 }
426
427 int aws_tls_ctx_options_init_server_pkcs12_from_path(
428 struct aws_tls_ctx_options *options,
429 struct aws_allocator *allocator,
430 const char *pkcs12_path,
431 struct aws_byte_cursor *pkcs_password) {
432 if (aws_tls_ctx_options_init_client_mtls_pkcs12_from_path(options, allocator, pkcs12_path, pkcs_password)) {
433 return AWS_OP_ERR;
434 }
435
436 options->verify_peer = false;
437 return AWS_OP_SUCCESS;
438 }
439
440 int aws_tls_ctx_options_init_server_pkcs12(
441 struct aws_tls_ctx_options *options,
442 struct aws_allocator *allocator,
443 struct aws_byte_cursor *pkcs12,
444 struct aws_byte_cursor *pkcs_password) {
445 if (aws_tls_ctx_options_init_client_mtls_pkcs12(options, allocator, pkcs12, pkcs_password)) {
446 return AWS_OP_ERR;
447 }
448
449 options->verify_peer = false;
450 return AWS_OP_SUCCESS;
451 }
452
453 int aws_tls_ctx_options_init_default_server_from_path(
454 struct aws_tls_ctx_options *options,
455 struct aws_allocator *allocator,
456 const char *cert_path,
457 const char *pkey_path) {
458
459 #if !defined(AWS_OS_IOS)
460 if (aws_tls_ctx_options_init_client_mtls_from_path(options, allocator, cert_path, pkey_path)) {
461 return AWS_OP_ERR;
462 }
463
464 options->verify_peer = false;
465 return AWS_OP_SUCCESS;
466 #else
467 (void)allocator;
468 (void)cert_path;
469 (void)pkey_path;
470 AWS_ZERO_STRUCT(*options);
471 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: Cannot create a server on this platform.");
472 return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED);
473 #endif
474 }
475
476 int aws_tls_ctx_options_init_default_server(
477 struct aws_tls_ctx_options *options,
478 struct aws_allocator *allocator,
479 struct aws_byte_cursor *cert,
480 struct aws_byte_cursor *pkey) {
481
482 #if !defined(AWS_OS_IOS)
483 if (aws_tls_ctx_options_init_client_mtls(options, allocator, cert, pkey)) {
484 return AWS_OP_ERR;
485 }
486
487 options->verify_peer = false;
488 return AWS_OP_SUCCESS;
489 #else
490 (void)allocator;
491 (void)cert;
492 (void)pkey;
493 AWS_ZERO_STRUCT(*options);
494 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: Cannot create a server on this platform.");
495 return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED);
496 #endif
497 }
498
499 int aws_tls_ctx_options_set_alpn_list(struct aws_tls_ctx_options *options, const char *alpn_list) {
500 aws_string_destroy(options->alpn_list);
501
502 options->alpn_list = aws_string_new_from_c_str(options->allocator, alpn_list);
503 if (!options->alpn_list) {
504 return AWS_OP_ERR;
505 }
506
507 return AWS_OP_SUCCESS;
508 }
509
510 void aws_tls_ctx_options_set_verify_peer(struct aws_tls_ctx_options *options, bool verify_peer) {
511 options->verify_peer = verify_peer;
512 }
513
514 void aws_tls_ctx_options_set_no_certificate_revocation(struct aws_tls_ctx_options *options, bool no_revocation) {
515 options->no_certificate_revocation = no_revocation;
516 }
517
518 void aws_tls_ctx_options_set_minimum_tls_version(
519 struct aws_tls_ctx_options *options,
520 enum aws_tls_versions minimum_tls_version) {
521 options->minimum_tls_version = minimum_tls_version;
522 }
523
524 void aws_tls_ctx_options_set_tls_cipher_preference(
525 struct aws_tls_ctx_options *options,
526 enum aws_tls_cipher_pref cipher_pref) {
527 options->cipher_pref = cipher_pref;
528 }
529
530 int aws_tls_ctx_options_override_default_trust_store_from_path(
531 struct aws_tls_ctx_options *options,
532 const char *ca_path,
533 const char *ca_file) {
534
535 /* Note: on success these are not cleaned up, their data is "moved" into the options struct */
536 struct aws_string *ca_path_tmp = NULL;
537 struct aws_byte_buf ca_file_tmp;
538 AWS_ZERO_STRUCT(ca_file_tmp);
539
540 if (ca_path) {
541 if (options->ca_path) {
542 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: cannot override trust store multiple times");
543 aws_raise_error(AWS_ERROR_INVALID_STATE);
544 goto error;
545 }
546
547 ca_path_tmp = aws_string_new_from_c_str(options->allocator, ca_path);
548 if (!ca_path_tmp) {
549 goto error;
550 }
551 }
552
553 if (ca_file) {
554 if (aws_tls_options_buf_is_set(&options->ca_file)) {
555 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: cannot override trust store multiple times");
556 aws_raise_error(AWS_ERROR_INVALID_STATE);
557 goto error;
558 }
559
560 if (aws_byte_buf_init_from_file(&ca_file_tmp, options->allocator, ca_file)) {
561 goto error;
562 }
563
564 if (aws_sanitize_pem(&ca_file_tmp, options->allocator)) {
565 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: Invalid CA file. File must contain PEM encoded data");
566 goto error;
567 }
568 }
569
570 /* Success, set new values. (no need to clean up old values, we checked earlier that they were unallocated) */
571 if (ca_path) {
572 options->ca_path = ca_path_tmp;
573 }
574 if (ca_file) {
575 options->ca_file = ca_file_tmp;
576 }
577 return AWS_OP_SUCCESS;
578
579 error:
580 aws_string_destroy_secure(ca_path_tmp);
581 aws_byte_buf_clean_up_secure(&ca_file_tmp);
582 return AWS_OP_ERR;
583 }
584
585 void aws_tls_ctx_options_set_extension_data(struct aws_tls_ctx_options *options, void *extension_data) {
586 options->ctx_options_extension = extension_data;
587 }
588
589 int aws_tls_ctx_options_override_default_trust_store(
590 struct aws_tls_ctx_options *options,
591 const struct aws_byte_cursor *ca_file) {
592
593 if (aws_tls_options_buf_is_set(&options->ca_file)) {
594 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: cannot override trust store multiple times");
595 return aws_raise_error(AWS_ERROR_INVALID_STATE);
596 }
597
598 if (aws_byte_buf_init_copy_from_cursor(&options->ca_file, options->allocator, *ca_file)) {
599 goto error;
600 }
601
602 if (aws_sanitize_pem(&options->ca_file, options->allocator)) {
603 AWS_LOGF_ERROR(AWS_LS_IO_TLS, "static: Invalid CA file. File must contain PEM encoded data");
604 goto error;
605 }
606
607 return AWS_OP_SUCCESS;
608
609 error:
610 aws_byte_buf_clean_up_secure(&options->ca_file);
611 return AWS_OP_ERR;
612 }
613
614 void aws_tls_connection_options_init_from_ctx(
615 struct aws_tls_connection_options *conn_options,
616 struct aws_tls_ctx *ctx) {
617 AWS_ZERO_STRUCT(*conn_options);
618 /* the assumption here, is that if it was set in the context, we WANT it to be NULL here unless it's different.
619 * so only set verify peer at this point. */
620 conn_options->ctx = aws_tls_ctx_acquire(ctx);
621
622 conn_options->timeout_ms = AWS_DEFAULT_TLS_TIMEOUT_MS;
623 }
624
625 int aws_tls_connection_options_copy(
626 struct aws_tls_connection_options *to,
627 const struct aws_tls_connection_options *from) {
628
629 /* clean up the options before copy. */
630 aws_tls_connection_options_clean_up(to);
631
632 /* copy everything copyable over, then override the rest with deep copies. */
633 *to = *from;
634
635 to->ctx = aws_tls_ctx_acquire(from->ctx);
636
637 if (from->alpn_list) {
638 to->alpn_list = aws_string_new_from_string(from->alpn_list->allocator, from->alpn_list);
639
640 if (!to->alpn_list) {
641 return AWS_OP_ERR;
642 }
643 }
644
645 if (from->server_name) {
646 to->server_name = aws_string_new_from_string(from->server_name->allocator, from->server_name);
647
648 if (!to->server_name) {
649 aws_string_destroy(to->server_name);
650 return AWS_OP_ERR;
651 }
652 }
653
654 return AWS_OP_SUCCESS;
655 }
656
657 void aws_tls_connection_options_clean_up(struct aws_tls_connection_options *connection_options) {
658 aws_tls_ctx_release(connection_options->ctx);
659
660 if (connection_options->alpn_list) {
661 aws_string_destroy(connection_options->alpn_list);
662 }
663
664 if (connection_options->server_name) {
665 aws_string_destroy(connection_options->server_name);
666 }
667
668 AWS_ZERO_STRUCT(*connection_options);
669 }
670
671 void aws_tls_connection_options_set_callbacks(
672 struct aws_tls_connection_options *conn_options,
673 aws_tls_on_negotiation_result_fn *on_negotiation_result,
674 aws_tls_on_data_read_fn *on_data_read,
675 aws_tls_on_error_fn *on_error,
676 void *user_data) {
677 conn_options->on_negotiation_result = on_negotiation_result;
678 conn_options->on_data_read = on_data_read;
679 conn_options->on_error = on_error;
680 conn_options->user_data = user_data;
681 }
682
683 int aws_tls_connection_options_set_server_name(
684 struct aws_tls_connection_options *conn_options,
685 struct aws_allocator *allocator,
686 const struct aws_byte_cursor *server_name) {
687
688 if (conn_options->server_name != NULL) {
689 aws_string_destroy(conn_options->server_name);
690 conn_options->server_name = NULL;
691 }
692
693 conn_options->server_name = aws_string_new_from_cursor(allocator, server_name);
694 if (!conn_options->server_name) {
695 return AWS_OP_ERR;
696 }
697
698 return AWS_OP_SUCCESS;
699 }
700
701 int aws_tls_connection_options_set_alpn_list(
702 struct aws_tls_connection_options *conn_options,
703 struct aws_allocator *allocator,
704 const char *alpn_list) {
705
706 if (conn_options->alpn_list != NULL) {
707 aws_string_destroy(conn_options->alpn_list);
708 conn_options->alpn_list = NULL;
709 }
710
711 conn_options->alpn_list = aws_string_new_from_c_str(allocator, alpn_list);
712 if (!conn_options->alpn_list) {
713 return AWS_OP_ERR;
714 }
715
716 return AWS_OP_SUCCESS;
717 }
718
719 #ifdef BYO_CRYPTO
720
721 struct aws_tls_ctx *aws_tls_server_ctx_new(struct aws_allocator *alloc, const struct aws_tls_ctx_options *options) {
722 (void)alloc;
723 (void)options;
724 AWS_FATAL_ASSERT(
725 false &&
726 "When using BYO_CRYPTO, user is responsible for creating aws_tls_ctx manually. You cannot call this function.");
727 }
728
729 struct aws_tls_ctx *aws_tls_client_ctx_new(struct aws_allocator *alloc, const struct aws_tls_ctx_options *options) {
730 (void)alloc;
731 (void)options;
732 AWS_FATAL_ASSERT(
733 false &&
734 "When using BYO_CRYPTO, user is responsible for creating aws_tls_ctx manually. You cannot call this function.");
735 }
736
737 static aws_tls_handler_new_fn *s_client_handler_new = NULL;
738 static aws_tls_client_handler_start_negotiation_fn *s_start_negotiation_fn = NULL;
739 static void *s_client_user_data = NULL;
740
741 static aws_tls_handler_new_fn *s_server_handler_new = NULL;
742 static void *s_server_user_data = NULL;
743
744 struct aws_channel_handler *aws_tls_client_handler_new(
745 struct aws_allocator *allocator,
746 struct aws_tls_connection_options *options,
747 struct aws_channel_slot *slot) {
748 AWS_FATAL_ASSERT(
749 s_client_handler_new &&
750 "For BYO_CRYPTO, you must call aws_tls_client_handler_new_set_callback() with a non-null value.");
751 return s_client_handler_new(allocator, options, slot, s_client_user_data);
752 }
753
754 struct aws_channel_handler *aws_tls_server_handler_new(
755 struct aws_allocator *allocator,
756 struct aws_tls_connection_options *options,
757 struct aws_channel_slot *slot) {
758 AWS_FATAL_ASSERT(
759 s_client_handler_new &&
760 "For BYO_CRYPTO, you must call aws_tls_server_handler_new_set_callback() with a non-null value.");
761 return s_server_handler_new(allocator, options, slot, s_server_user_data);
762 }
763
764 void aws_tls_byo_crypto_set_client_setup_options(const struct aws_tls_byo_crypto_setup_options *options) {
765 AWS_FATAL_ASSERT(options);
766 AWS_FATAL_ASSERT(options->new_handler_fn);
767 AWS_FATAL_ASSERT(options->start_negotiation_fn);
768
769 s_client_handler_new = options->new_handler_fn;
770 s_start_negotiation_fn = options->start_negotiation_fn;
771 s_client_user_data = options->user_data;
772 }
773
774 void aws_tls_byo_crypto_set_server_setup_options(const struct aws_tls_byo_crypto_setup_options *options) {
775 AWS_FATAL_ASSERT(options);
776 AWS_FATAL_ASSERT(options->new_handler_fn);
777
778 s_server_handler_new = options->new_handler_fn;
779 s_server_user_data = options->user_data;
780 }
781
782 int aws_tls_client_handler_start_negotiation(struct aws_channel_handler *handler) {
783 AWS_FATAL_ASSERT(
784 s_start_negotiation_fn &&
785 "For BYO_CRYPTO, you must call aws_tls_client_handler_set_start_negotiation_callback() with a non-null value.");
786 return s_start_negotiation_fn(handler, s_client_user_data);
787 }
788
789 void aws_tls_init_static_state(struct aws_allocator *alloc) {
790 (void)alloc;
791 }
792
793 void aws_tls_clean_up_static_state(void) {}
794
795 void aws_tls_init_vtable(struct aws_allocator *allocator) {
796 (void)allocator;
797 }
798
799 #else /* BYO_CRYPTO */
800
801 void s2n_init_tls_vtable(struct aws_tls_vtable *vtable);
802 void secure_transport_init_tls_vtable(struct aws_tls_vtable *vtable);
803 void secure_channel_init_tls_vtable(struct aws_tls_vtable *vtable);
804
805 void aws_tls_init_vtable(struct aws_allocator *allocator) {
806 (void)allocator;
807
808 /* Allow the vtable to be set up only once on library initialization. */
809 static bool s_vtable_initialized = false;
810 if (s_vtable_initialized) {
811 return;
812 }
813 s_vtable_initialized = true;
814
815 # if defined(__APPLE__) && defined(AWS_USE_SECITEM)
816 secure_transport_init_tls_vtable(&s_tls_channel_handler_vtable);
817
818 # elif defined(__APPLE__) && defined(USE_S2N)
819 struct aws_string *use_non_fips_13 = aws_get_env_nonempty(allocator, "AWS_CRT_USE_NON_FIPS_TLS_13");
820 if (use_non_fips_13) {
821 s2n_init_tls_vtable(&s_tls_channel_handler_vtable);
822 aws_string_destroy(use_non_fips_13);
823 } else {
824 secure_transport_init_tls_vtable(&s_tls_channel_handler_vtable);
825 }
826
827 # elif defined(__APPLE__)
828 secure_transport_init_tls_vtable(&s_tls_channel_handler_vtable);
829
830 # elif defined(_WIN32)
831 secure_channel_init_tls_vtable(&s_tls_channel_handler_vtable);
832
833 # elif defined(USE_S2N)
834 s2n_init_tls_vtable(&s_tls_channel_handler_vtable);
835
836 # else
837 AWS_FATAL_ASSERT(false && "No TLS implementation available.");
838 # endif
839 }
840
841 void aws_tls_init_static_state(struct aws_allocator *allocator) {
842 AWS_PRECONDITION(s_tls_channel_handler_vtable.init_static_state);
843 s_tls_channel_handler_vtable.init_static_state(allocator);
844 }
845
846 void aws_tls_clean_up_static_state(void) {
847 AWS_PRECONDITION(s_tls_channel_handler_vtable.clean_up_static_state);
848 s_tls_channel_handler_vtable.clean_up_static_state();
849 }
850
851 struct aws_tls_ctx *aws_tls_server_ctx_new(struct aws_allocator *alloc, const struct aws_tls_ctx_options *options) {
852 AWS_PRECONDITION(s_tls_channel_handler_vtable.server_ctx_new);
853 return s_tls_channel_handler_vtable.server_ctx_new(alloc, options);
854 }
855
856 struct aws_tls_ctx *aws_tls_client_ctx_new(struct aws_allocator *alloc, const struct aws_tls_ctx_options *options) {
857 AWS_PRECONDITION(s_tls_channel_handler_vtable.client_ctx_new);
858 return s_tls_channel_handler_vtable.client_ctx_new(alloc, options);
859 }
860
861 struct aws_channel_handler *aws_tls_client_handler_new(
862 struct aws_allocator *allocator,
863 struct aws_tls_connection_options *options,
864 struct aws_channel_slot *slot) {
865 AWS_PRECONDITION(s_tls_channel_handler_vtable.client_handler_new);
866 return s_tls_channel_handler_vtable.client_handler_new(allocator, options, slot);
867 }
868
869 struct aws_channel_handler *aws_tls_server_handler_new(
870 struct aws_allocator *allocator,
871 struct aws_tls_connection_options *options,
872 struct aws_channel_slot *slot) {
873 AWS_PRECONDITION(s_tls_channel_handler_vtable.server_handler_new);
874 return s_tls_channel_handler_vtable.server_handler_new(allocator, options, slot);
875 }
876
877 int aws_tls_client_handler_start_negotiation(struct aws_channel_handler *handler) {
878 AWS_PRECONDITION(s_tls_channel_handler_vtable.client_handler_start_negotiation);
879 return s_tls_channel_handler_vtable.client_handler_start_negotiation(handler);
880 }
881
882 struct aws_byte_buf aws_tls_handler_protocol(struct aws_channel_handler *handler) {
883 AWS_PRECONDITION(s_tls_channel_handler_vtable.handler_protocol);
884 return s_tls_channel_handler_vtable.handler_protocol(handler);
885 }
886
887 struct aws_byte_buf aws_tls_handler_server_name(struct aws_channel_handler *handler) {
888 AWS_PRECONDITION(s_tls_channel_handler_vtable.handler_server_name);
889 return s_tls_channel_handler_vtable.handler_server_name(handler);
890 }
891
892 bool aws_tls_is_alpn_available(void) {
893 AWS_PRECONDITION(s_tls_channel_handler_vtable.is_alpn_available);
894 return s_tls_channel_handler_vtable.is_alpn_available();
895 }
896
897 bool aws_tls_is_cipher_pref_supported(enum aws_tls_cipher_pref cipher_pref) {
898 AWS_PRECONDITION(s_tls_channel_handler_vtable.is_cipher_pref_supported);
899 return s_tls_channel_handler_vtable.is_cipher_pref_supported(cipher_pref);
900 }
901
902 #endif /* BYO_CRYPTO */
903
904 int aws_channel_setup_client_tls(
905 struct aws_channel_slot *right_of_slot,
906 struct aws_tls_connection_options *tls_options) {
907
908 AWS_FATAL_ASSERT(right_of_slot != NULL);
909 struct aws_channel *channel = right_of_slot->channel;
910 struct aws_allocator *allocator = right_of_slot->alloc;
911
912 struct aws_channel_slot *tls_slot = aws_channel_slot_new(channel);
913
914 /* as far as cleanup goes, since this stuff is being added to a channel, the caller will free this memory
915 when they clean up the channel. */
916 if (!tls_slot) {
917 return AWS_OP_ERR;
918 }
919
920 struct aws_channel_handler *tls_handler = aws_tls_client_handler_new(allocator, tls_options, tls_slot);
921 if (!tls_handler) {
922 aws_mem_release(allocator, tls_slot);
923 return AWS_OP_ERR;
924 }
925
926 /*
927 * From here on out, channel shutdown will handle slot/handler cleanup
928 */
929 aws_channel_slot_insert_right(right_of_slot, tls_slot);
930 AWS_LOGF_TRACE(
931 AWS_LS_IO_CHANNEL,
932 "id=%p: Setting up client TLS with handler %p on slot %p",
933 (void *)channel,
934 (void *)tls_handler,
935 (void *)tls_slot);
936
937 if (aws_channel_slot_set_handler(tls_slot, tls_handler) != AWS_OP_SUCCESS) {
938 return AWS_OP_ERR;
939 }
940
941 if (aws_tls_client_handler_start_negotiation(tls_handler) != AWS_OP_SUCCESS) {
942 return AWS_OP_ERR;
943 }
944
945 return AWS_OP_SUCCESS;
946 }
947
948 struct aws_tls_ctx *aws_tls_ctx_acquire(struct aws_tls_ctx *ctx) {
949 if (ctx != NULL) {
950 aws_ref_count_acquire(&ctx->ref_count);
951 }
952
953 return ctx;
954 }
955
956 void aws_tls_ctx_release(struct aws_tls_ctx *ctx) {
957 if (ctx != NULL) {
958 aws_ref_count_release(&ctx->ref_count);
959 }
960 }
961
962 const char *aws_tls_hash_algorithm_str(enum aws_tls_hash_algorithm hash) {
963 /* clang-format off */
964 switch (hash) {
965 case (AWS_TLS_HASH_SHA1): return "SHA1";
966 case (AWS_TLS_HASH_SHA224): return "SHA224";
967 case (AWS_TLS_HASH_SHA256): return "SHA256";
968 case (AWS_TLS_HASH_SHA384): return "SHA384";
969 case (AWS_TLS_HASH_SHA512): return "SHA512";
970 default: return "<UNKNOWN HASH ALGORITHM>";
971 }
972 /* clang-format on */
973 }
974
975 const char *aws_tls_signature_algorithm_str(enum aws_tls_signature_algorithm signature) {
976 /* clang-format off */
977 switch (signature) {
978 case (AWS_TLS_SIGNATURE_RSA): return "RSA";
979 case (AWS_TLS_SIGNATURE_ECDSA): return "ECDSA";
980 default: return "<UNKNOWN SIGNATURE ALGORITHM>";
981 }
982 /* clang-format on */
983 }
984
985 const char *aws_tls_key_operation_type_str(enum aws_tls_key_operation_type operation_type) {
986 /* clang-format off */
987 switch (operation_type) {
988 case (AWS_TLS_KEY_OPERATION_SIGN): return "SIGN";
989 case (AWS_TLS_KEY_OPERATION_DECRYPT): return "DECRYPT";
990 default: return "<UNKNOWN OPERATION TYPE>";
991 }
992 /* clang-format on */
993 }
994
995 void aws_tls_key_operation_complete(struct aws_tls_key_operation *operation, struct aws_byte_cursor output) {
996 if (s_tls_channel_handler_vtable.key_operation_complete != NULL) {
997 s_tls_channel_handler_vtable.key_operation_complete(operation, output);
998 }
999 }
1000
1001 void aws_tls_key_operation_complete_with_error(struct aws_tls_key_operation *operation, int error_code) {
1002 if (s_tls_channel_handler_vtable.key_operation_complete_with_error) {
1003 s_tls_channel_handler_vtable.key_operation_complete_with_error(operation, error_code);
1004 }
1005 }
1006
1007 struct aws_byte_cursor aws_tls_key_operation_get_input(const struct aws_tls_key_operation *operation) {
1008 if (s_tls_channel_handler_vtable.key_operation_get_input) {
1009 return s_tls_channel_handler_vtable.key_operation_get_input(operation);
1010 }
1011 return aws_byte_cursor_from_array(NULL, 0);
1012 }
1013
1014 enum aws_tls_key_operation_type aws_tls_key_operation_get_type(const struct aws_tls_key_operation *operation) {
1015 if (s_tls_channel_handler_vtable.key_operation_get_type != NULL) {
1016 return s_tls_channel_handler_vtable.key_operation_get_type(operation);
1017 }
1018 return AWS_TLS_KEY_OPERATION_UNKNOWN;
1019 }
1020
1021 enum aws_tls_signature_algorithm aws_tls_key_operation_get_signature_algorithm(
1022 const struct aws_tls_key_operation *operation) {
1023 if (s_tls_channel_handler_vtable.key_operation_get_signature_algorithm != NULL) {
1024 return s_tls_channel_handler_vtable.key_operation_get_signature_algorithm(operation);
1025 }
1026 return AWS_TLS_SIGNATURE_UNKNOWN;
1027 }
1028
1029 enum aws_tls_hash_algorithm aws_tls_key_operation_get_digest_algorithm(const struct aws_tls_key_operation *operation) {
1030 if (s_tls_channel_handler_vtable.key_operation_get_digest_algorithm != NULL) {
1031 return s_tls_channel_handler_vtable.key_operation_get_digest_algorithm(operation);
1032 }
1033 return AWS_TLS_HASH_UNKNOWN;
1034 }
1035
1036 struct aws_custom_key_op_handler *aws_custom_key_op_handler_acquire(struct aws_custom_key_op_handler *key_op_handler) {
1037 if (key_op_handler != NULL) {
1038 aws_ref_count_acquire(&key_op_handler->ref_count);
1039 }
1040 return key_op_handler;
1041 }
1042
1043 struct aws_custom_key_op_handler *aws_custom_key_op_handler_release(struct aws_custom_key_op_handler *key_op_handler) {
1044 if (key_op_handler != NULL) {
1045 aws_ref_count_release(&key_op_handler->ref_count);
1046 }
1047 return NULL;
1048 }
1049
1050 void aws_custom_key_op_handler_perform_operation(
1051 struct aws_custom_key_op_handler *key_op_handler,
1052 struct aws_tls_key_operation *operation) {
1053 key_op_handler->vtable->on_key_operation(key_op_handler, operation);
1054 }
1055
1056 bool aws_error_code_is_tls(int error_code) {
1057 switch (error_code) {
1058 case AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE:
1059 case AWS_IO_TLS_ERROR_NOT_NEGOTIATED:
1060 case AWS_IO_TLS_ERROR_WRITE_FAILURE:
1061 case AWS_IO_TLS_ERROR_ALERT_RECEIVED:
1062 case AWS_IO_TLS_CTX_ERROR:
1063 case AWS_IO_TLS_VERSION_UNSUPPORTED:
1064 case AWS_IO_TLS_CIPHER_PREF_UNSUPPORTED:
1065 case AWS_IO_TLS_NEGOTIATION_TIMEOUT:
1066 case AWS_IO_TLS_ALERT_NOT_GRACEFUL:
1067 case AWS_IO_TLS_DIGEST_ALGORITHM_UNSUPPORTED:
1068 case AWS_IO_TLS_SIGNATURE_ALGORITHM_UNSUPPORTED:
1069 case AWS_IO_TLS_ERROR_READ_FAILURE:
1070 case AWS_IO_TLS_UNKNOWN_ROOT_CERTIFICATE:
1071 case AWS_IO_TLS_NO_ROOT_CERTIFICATE_FOUND:
1072 case AWS_IO_TLS_CERTIFICATE_EXPIRED:
1073 case AWS_IO_TLS_CERTIFICATE_NOT_YET_VALID:
1074 case AWS_IO_TLS_BAD_CERTIFICATE:
1075 case AWS_IO_TLS_PEER_CERTIFICATE_EXPIRED:
1076 case AWS_IO_TLS_BAD_PEER_CERTIFICATE:
1077 case AWS_IO_TLS_PEER_CERTIFICATE_REVOKED:
1078 case AWS_IO_TLS_PEER_CERTIFICATE_UNKNOWN:
1079 case AWS_IO_TLS_INTERNAL_ERROR:
1080 case AWS_IO_TLS_CLOSED_GRACEFUL:
1081 case AWS_IO_TLS_CLOSED_ABORT:
1082 case AWS_IO_TLS_INVALID_CERTIFICATE_CHAIN:
1083 case AWS_IO_TLS_HOST_NAME_MISMATCH:
1084 return true;
1085 default:
1086 return false;
1087 }
1088 }