Debian Codegraph @

crt/aws-c-io/include/aws/io/tls_channel_handler.h

raw symbols SCIP
1 #ifndef AWS_IO_TLS_CHANNEL_HANDLER_H
2 #define AWS_IO_TLS_CHANNEL_HANDLER_H
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7 #include <aws/common/byte_buf.h>
8 #include <aws/common/ref_count.h>
9 #include <aws/io/io.h>
10
11 AWS_PUSH_SANE_WARNING_LEVEL
12
13 #define AWS_TLS_NEGOTIATED_PROTOCOL_MESSAGE 0x01
14
15 struct aws_channel_slot;
16 struct aws_channel_handler;
17 struct aws_pkcs11_session;
18 struct aws_string;
19
20 enum aws_tls_versions {
21 AWS_IO_SSLv3,
22 AWS_IO_TLSv1,
23 AWS_IO_TLSv1_1,
24 AWS_IO_TLSv1_2,
25 AWS_IO_TLSv1_3,
26 AWS_IO_TLS_VER_SYS_DEFAULTS = 128,
27 };
28
29 enum aws_tls_cipher_pref {
30 AWS_IO_TLS_CIPHER_PREF_SYSTEM_DEFAULT = 0,
31
32 /* Deprecated */ AWS_IO_TLS_CIPHER_PREF_KMS_PQ_TLSv1_0_2019_06 = 1,
33 /* Deprecated */ AWS_IO_TLS_CIPHER_PREF_KMS_PQ_SIKE_TLSv1_0_2019_11 = 2,
34 /* Deprecated */ AWS_IO_TLS_CIPHER_PREF_KMS_PQ_TLSv1_0_2020_02 = 3,
35 /* Deprecated */ AWS_IO_TLS_CIPHER_PREF_KMS_PQ_SIKE_TLSv1_0_2020_02 = 4,
36 /* Deprecated */ AWS_IO_TLS_CIPHER_PREF_KMS_PQ_TLSv1_0_2020_07 = 5,
37 /* Deprecated */ AWS_IO_TLS_CIPHER_PREF_PQ_TLSv1_0_2021_05 = 6,
38
39 /*
40 * This TLS cipher preference list contains post-quantum key exchange algorithms that have been standardized by
41 * NIST. PQ algorithms in this preference list will be used in hybrid mode, and always combined with a classical
42 * ECDHE key exchange.
43 */
44 AWS_IO_TLS_CIPHER_PREF_PQ_TLSV1_2_2024_10 = 7,
45
46 /* Recommended default policy with post-quantum algorithm support. This policy may change over time. */
47 AWS_IO_TLS_CIPHER_PREF_PQ_DEFAULT = 8,
48
49 /* This security policy is based on AWS-CRT-SDK-TLSv1.2-2023 (the default when a minimum TLS version is TLS 1.2),
50 * with tightened security. This security policy is FIPS-complaint.
51 */
52 AWS_IO_TLS_CIPHER_PREF_TLSV1_2_2025_07 = 9,
53
54 /* This security policy was the system default before PQ was enabled by default. */
55 AWS_IO_TLS_CIPHER_PREF_TLSV1_0_2023_06 = 10,
56
57 /* Recommended default policy without post-quantum algorithm support. This policy may change over time. */
58 AWS_IO_TLS_CIPHER_PREF_NON_PQ_DEFAULT = 11,
59
60 AWS_IO_TLS_CIPHER_PREF_END_RANGE = 0xFFFF
61 };
62
63 /**
64 * The hash algorithm of a TLS private key operation. Any custom private key operation handlers are expected to perform
65 * operations on the input TLS data using the correct hash algorithm or fail the operation.
66 */
67 enum aws_tls_hash_algorithm {
68 AWS_TLS_HASH_UNKNOWN,
69 AWS_TLS_HASH_SHA1,
70 AWS_TLS_HASH_SHA224,
71 AWS_TLS_HASH_SHA256,
72 AWS_TLS_HASH_SHA384,
73 AWS_TLS_HASH_SHA512,
74 };
75
76 /**
77 * The signature of a TLS private key operation. Any custom private key operation handlers are expected to perform
78 * operations on the input TLS data using the correct signature algorithm or fail the operation.
79 */
80 enum aws_tls_signature_algorithm {
81 AWS_TLS_SIGNATURE_UNKNOWN,
82 AWS_TLS_SIGNATURE_RSA,
83 AWS_TLS_SIGNATURE_ECDSA,
84 };
85
86 /**
87 * The TLS private key operation that needs to be performed by a custom private key operation handler when making
88 * a connection using mutual TLS.
89 */
90 enum aws_tls_key_operation_type {
91 AWS_TLS_KEY_OPERATION_UNKNOWN,
92 AWS_TLS_KEY_OPERATION_SIGN,
93 AWS_TLS_KEY_OPERATION_DECRYPT,
94 };
95
96 struct aws_tls_ctx {
97 struct aws_allocator *alloc;
98 void *impl;
99 struct aws_ref_count ref_count;
100 };
101
102 /**
103 * Invoked upon completion of the TLS handshake. If successful error_code will be AWS_OP_SUCCESS, otherwise
104 * the negotiation failed and immediately after this function is invoked, the channel will be shutting down.
105 *
106 * NOTE: When using SecItem the handler and slot arguments will be pointers to the socket slot and socket handler. This
107 * is due to TLS negotiaion being handled by the Apple Network Framework connection in the socket slot/handler.
108 */
109 typedef void(aws_tls_on_negotiation_result_fn)(
110 struct aws_channel_handler *handler,
111 struct aws_channel_slot *slot,
112 int error_code,
113 void *user_data);
114
115 /**
116 * Only used if the TLS handler is the last handler in the channel. This allows you to read any data that
117 * was read and decrypted by the handler. If you have application protocol channel handlers, this function
118 * is not necessary and certainly not recommended.
119 */
120 typedef void(aws_tls_on_data_read_fn)(
121 struct aws_channel_handler *handler,
122 struct aws_channel_slot *slot,
123 struct aws_byte_buf *buffer,
124 void *user_data);
125
126 /**
127 * Invoked when an error occurs in the TLS state machine AFTER the handshake has completed. This function should only
128 * be used in conjunction with the rules of aws_tls_on_data_read_fn.
129 */
130 typedef void(aws_tls_on_error_fn)(
131 struct aws_channel_handler *handler,
132 struct aws_channel_slot *slot,
133 int err,
134 const char *message,
135 void *user_data);
136
137 struct aws_tls_connection_options {
138 /** semi-colon delimited list of protocols. Example:
139 * h2;http/1.1
140 */
141 struct aws_string *alpn_list;
142 /**
143 * Serves two purposes. If SNI is supported (hint... it is),
144 * this sets the SNI extension.
145 *
146 * For X.509 validation this also sets the name that will be used
147 * for verifying the subj alt name and common name of the peer's certificate.
148 */
149 struct aws_string *server_name;
150 aws_tls_on_negotiation_result_fn *on_negotiation_result;
151 aws_tls_on_data_read_fn *on_data_read;
152 aws_tls_on_error_fn *on_error;
153 void *user_data;
154 struct aws_tls_ctx *ctx;
155 bool advertise_alpn_message;
156 uint32_t timeout_ms;
157 };
158
159 /**
160 * A struct containing all of the data needed for a private key operation when
161 * making a mutual TLS connection. This struct contains the data that needs
162 * to be operated on, like performing a sign operation or a decrypt operation.
163 */
164 struct aws_tls_key_operation;
165
166 /**
167 * A struct containing parameters used during import of Certificate and Private Key into a
168 * data protection keychain using Apple's SecItem API.
169 */
170 struct aws_secitem_options {
171 /**
172 * Human-Readable identifier tag for certificate being used in keychain.
173 * Value will be used with kSecAttrLabel Key in SecItem functions.
174 * If one is not provided, we generate it ourselves.
175 */
176 struct aws_string *cert_label;
177
178 /**
179 * Human-Readable identifier tag for private key being used in keychain.
180 * Value will be used with kSecAttrLabel Key in SecItem functions.
181 * If one is not provided, we generate it ourselves.
182 */
183 struct aws_string *key_label;
184 };
185
186 struct aws_tls_ctx_options {
187 struct aws_allocator *allocator;
188
189 /**
190 * minimum tls version to use. If you just want us to use the
191 * system defaults, you can set: AWS_IO_TLS_VER_SYS_DEFAULTS. This
192 * has the added benefit of automatically picking up new TLS versions
193 * as your OS or distribution adds support.
194 */
195 enum aws_tls_versions minimum_tls_version;
196
197 /**
198 * The Cipher Preference List to use
199 */
200 enum aws_tls_cipher_pref cipher_pref;
201
202 /**
203 * A PEM armored PKCS#7 collection of CAs you want to trust as a string.
204 * Only use this if it's a CA not currently installed on your system.
205 */
206 struct aws_byte_buf ca_file;
207 /**
208 * Only used on Unix systems using an openssl style trust API.
209 * this is typically something like /etc/pki/tls/certs/"
210 */
211 struct aws_string *ca_path;
212 /**
213 * Sets ctx wide alpn string. This is most useful for servers.
214 * This is a semi-colon delimited list. example:
215 * h2;http/1.1
216 */
217 struct aws_string *alpn_list;
218 /**
219 * A PEM armored PKCS#7 certificate as a string.
220 * It is supported on every operating system.
221 */
222 struct aws_byte_buf certificate;
223
224 #ifdef _WIN32
225 /** The path to a system
226 * installed certficate/private key pair. Example:
227 * CurrentUser\\MY\\<thumprint>
228 */
229 const char *system_certificate_path;
230 #endif
231
232 /**
233 * A PEM armored PKCS#7 private key as a string.
234 *
235 * On windows, this field should be NULL only if you are
236 * using a system installed certficate.
237 */
238 struct aws_byte_buf private_key;
239
240 #ifdef __APPLE__
241 /**
242 * Apple Only!
243 *
244 * On Apple OS you can also use a pkcs#12 for your certificate
245 * and private key. This is the contents the certificate.
246 */
247 struct aws_byte_buf pkcs12;
248
249 /**
250 * Password for the pkcs12 data in pkcs12.
251 */
252 struct aws_byte_buf pkcs12_password;
253
254 /**
255 * On iOS/tvOS the available settings when adding items to the keychain using
256 * SecItem are contained within this struct. This is NOT supported on MacOS.
257 */
258 struct aws_secitem_options secitem_options;
259
260 /**
261 * On MacOS you can also use a custom keychain instead of
262 * the default keychain of the account. This is NOT supported on iOS.
263 */
264 struct aws_string *keychain_path;
265
266 #endif /* __APPLE__ */
267
268 /** max tls fragment size. Default is the value of g_aws_channel_max_fragment_size. */
269 size_t max_fragment_size;
270
271 /**
272 * default is true for clients and false for servers.
273 * You should not change this default for clients unless
274 * you're testing and don't want to fool around with CA trust stores.
275 * Before you release to production, you'll want to turn this back on
276 * and add your custom CA to the aws_tls_ctx_options.
277 *
278 * If you set this in server mode, it enforces client authentication.
279 */
280 bool verify_peer;
281
282 /**
283 * For use when adding BYO_CRYPTO implementations. You can set extra data in here for use with your TLS
284 * implementation.
285 */
286 void *ctx_options_extension;
287
288 /**
289 * Set if using custom private key operations.
290 * See aws_custom_key_op_handler for more details
291 *
292 * Note: Custom key operations (and PKCS#11 integration) hasn't been tested with TLS 1.3, so don't use
293 * cipher preferences that allow TLS 1.3. If this is set, we will always use non TLS 1.3 preferences.
294 */
295 struct aws_custom_key_op_handler *custom_key_op_handler;
296
297 /**
298 * Set to true to disable certificate revocation checking during TLS negotiation.
299 *
300 * On Windows (SChannel), this prevents the TLS handshake from making outbound network calls
301 * to CRL/OCSP revocation endpoints, which can block for minutes when the endpoints are unreachable
302 * (e.g., in private subnets without internet access).
303 *
304 * On Linux (s2n), this disables validation of OCSP stapled responses provided by the server.
305 *
306 * On Apple platforms, this is a no-op as revocation checking is not enabled by default.
307 *
308 * Default is false (Windows makes a best-effort certificate revocation check to CRL/OCSP and Linux (s2n)
309 * checks against stapled responses).
310 */
311 bool no_certificate_revocation;
312 };
313
314 struct aws_tls_negotiated_protocol_message {
315 struct aws_byte_buf protocol;
316 };
317
318 typedef struct aws_channel_handler *(
319 *aws_tls_on_protocol_negotiated)(struct aws_channel_slot *new_slot, struct aws_byte_buf *protocol, void *user_data);
320
321 /**
322 * An enum for the current state of tls negotiation within a tls channel handler
323 */
324 enum aws_tls_negotiation_status {
325 AWS_TLS_NEGOTIATION_STATUS_NONE,
326 AWS_TLS_NEGOTIATION_STATUS_ONGOING,
327 AWS_TLS_NEGOTIATION_STATUS_SUCCESS,
328 AWS_TLS_NEGOTIATION_STATUS_FAILURE
329 };
330
331 #ifdef BYO_CRYPTO
332 /**
333 * Callback for creating a TLS handler. If you're using this you're using BYO_CRYPTO. This function should return
334 * a fully implemented aws_channel_handler instance for TLS. Note: the aws_tls_options passed to your
335 * aws_tls_handler_new_fn contains multiple callbacks. Namely: aws_tls_on_negotiation_result_fn. You are responsible for
336 * invoking this function when TLs session negotiation has completed.
337 */
338 typedef struct aws_channel_handler *(aws_tls_handler_new_fn)(struct aws_allocator *allocator,
339 struct aws_tls_connection_options *options,
340 struct aws_channel_slot *slot,
341 void *user_data);
342
343 /**
344 * Invoked when it's time to start TLS negotiation. Note: the aws_tls_options passed to your aws_tls_handler_new_fn
345 * contains multiple callbacks. Namely: aws_tls_on_negotiation_result_fn. You are responsible for invoking this function
346 * when TLS session negotiation has completed.
347 */
348 typedef int(aws_tls_client_handler_start_negotiation_fn)(struct aws_channel_handler *handler, void *user_data);
349
350 struct aws_tls_byo_crypto_setup_options {
351 aws_tls_handler_new_fn *new_handler_fn;
352 /* ignored for server implementations, required for clients. */
353 aws_tls_client_handler_start_negotiation_fn *start_negotiation_fn;
354 void *user_data;
355 };
356
357 #endif /* BYO_CRYPTO */
358
359 AWS_EXTERN_C_BEGIN
360
361 /******************************** tls options init stuff ***********************/
362 /**
363 * Initializes options with default client options
364 */
365 AWS_IO_API void aws_tls_ctx_options_init_default_client(
366 struct aws_tls_ctx_options *options,
367 struct aws_allocator *allocator);
368 /**
369 * Cleans up resources allocated by init_* functions
370 */
371 AWS_IO_API void aws_tls_ctx_options_clean_up(struct aws_tls_ctx_options *options);
372
373 /**
374 * Initializes options for use with mutual tls in client mode.
375 * cert_path and pkey_path are paths to files on disk. cert_path
376 * and pkey_path are treated as PKCS#7 PEM armored. They are loaded
377 * from disk and stored in buffers internally.
378 */
379 AWS_IO_API int aws_tls_ctx_options_init_client_mtls_from_path(
380 struct aws_tls_ctx_options *options,
381 struct aws_allocator *allocator,
382 const char *cert_path,
383 const char *pkey_path);
384
385 /**
386 * Initializes options for use with mutual tls in client mode.
387 * cert and pkey are copied. cert and pkey are treated as PKCS#7 PEM
388 * armored.
389 */
390 AWS_IO_API int aws_tls_ctx_options_init_client_mtls(
391 struct aws_tls_ctx_options *options,
392 struct aws_allocator *allocator,
393 const struct aws_byte_cursor *cert,
394 const struct aws_byte_cursor *pkey);
395
396 /**
397 * vtable for aws_custom_key_op_handler.
398 */
399 struct aws_custom_key_op_handler_vtable {
400 /**
401 * Called when the a TLS handshake has an operation it needs the custom key operation handler to perform.
402 * NOTE: You must call aws_tls_key_operation_complete() or aws_tls_key_operation_complete_with_error()
403 * otherwise the TLS handshake will stall the TLS connection indefinitely and leak memory.
404 */
405 void (*on_key_operation)(struct aws_custom_key_op_handler *key_op_handler, struct aws_tls_key_operation *operation);
406 };
407
408 /**
409 * The custom key operation that is used when performing a mutual TLS handshake. This can
410 * be extended to provide custom private key operations, like PKCS11 or similar.
411 */
412 struct aws_custom_key_op_handler {
413 /**
414 * A void* intended to be populated with a reference to whatever class is extending this class. For example,
415 * if you have extended aws_custom_key_op_handler with a custom struct, you would put a pointer to this struct
416 * to *impl so you can retrieve it back in the vtable functions.
417 */
418 void *impl;
419
420 /**
421 * A vtable containing all of the functions the aws_custom_key_op_handler implements. Is intended to be extended.
422 * NOTE: Use "aws_custom_key_op_handler_<func>" to access vtable functions.
423 */
424 const struct aws_custom_key_op_handler_vtable *vtable;
425
426 /**
427 * A reference count for handling memory usage.
428 * Use aws_custom_key_op_handler_acquire and aws_custom_key_op_handler_release to increase/decrease count.
429 */
430 struct aws_ref_count ref_count;
431 };
432
433 /**
434 * Increases the reference count for the passed-in aws_custom_key_op_handler and returns it.
435 */
436 AWS_IO_API struct aws_custom_key_op_handler *aws_custom_key_op_handler_acquire(
437 struct aws_custom_key_op_handler *key_op_handler);
438
439 /**
440 * Decreases the reference count for the passed-in aws_custom_key_op_handler and returns NULL.
441 */
442 AWS_IO_API struct aws_custom_key_op_handler *aws_custom_key_op_handler_release(
443 struct aws_custom_key_op_handler *key_op_handler);
444
445 /**
446 * Calls the on_key_operation vtable function. See aws_custom_key_op_handler_vtable for function details.
447 */
448 AWS_IO_API void aws_custom_key_op_handler_perform_operation(
449 struct aws_custom_key_op_handler *key_op_handler,
450 struct aws_tls_key_operation *operation);
451
452 /**
453 * Initializes options for use with mutual TLS in client mode,
454 * where private key operations are handled by custom code.
455 *
456 * Note: cert_file_contents will be copied into a new buffer after this
457 * function is called, so you do not need to keep that data alive
458 * after calling this function.
459 *
460 * @param options aws_tls_ctx_options to be initialized.
461 * @param allocator Allocator to use.
462 * @param custom Options for custom key operations.
463 * @param cert_file_contents The contents of a certificate file.
464 */
465 AWS_IO_API int aws_tls_ctx_options_init_client_mtls_with_custom_key_operations(
466 struct aws_tls_ctx_options *options,
467 struct aws_allocator *allocator,
468 struct aws_custom_key_op_handler *custom,
469 const struct aws_byte_cursor *cert_file_contents);
470
471 /**
472 * This struct exists as a graceful way to pass many arguments when
473 * calling init-with-pkcs11 functions on aws_tls_ctx_options (this also makes
474 * it easy to introduce optional arguments in the future).
475 * Instances of this struct should only exist briefly on the stack.
476 *
477 * Instructions for binding this to high-level languages:
478 * - Python: The members of this struct should be the keyword args to the init-with-pkcs11 functions.
479 * - JavaScript: This should be an options map passed to init-with-pkcs11 functions.
480 * - Java: This should be an options class passed to init-with-pkcs11 functions.
481 * - C++: Same as Java
482 *
483 * Notes on integer types:
484 * PKCS#11 uses `unsigned long` for IDs, handles, etc but we expose them as `uint64_t` in public APIs.
485 * We do this because sizeof(long) is inconsistent across platform/arch/language
486 * (ex: always 64bit in Java, always 32bit in C on Windows, matches CPU in C on Linux and Apple).
487 * By using uint64_t in our public API, we can keep the careful bounds-checking all in one
488 * place, instead of expecting each high-level language binding to get it just right.
489 */
490 struct aws_tls_ctx_pkcs11_options {
491 /**
492 * The PKCS#11 library to use.
493 * This field is required.
494 */
495 struct aws_pkcs11_lib *pkcs11_lib;
496
497 /**
498 * User PIN, for logging into the PKCS#11 token (UTF-8).
499 * Zero out to log into a token with a "protected authentication path".
500 */
501 struct aws_byte_cursor user_pin;
502
503 /**
504 * ID of slot containing PKCS#11 token.
505 * If set to NULL, the token will be chosen based on other criteria
506 * (such as token label).
507 */
508 const uint64_t *slot_id;
509
510 /**
511 * Label of PKCS#11 token to use.
512 * If zeroed out, the token will be chosen based on other criteria
513 * (such as slot ID).
514 */
515 struct aws_byte_cursor token_label;
516
517 /**
518 * Label of private key object on PKCS#11 token (UTF-8).
519 * If zeroed out, the private key will be chosen based on other criteria
520 * (such as being the only available private key on the token).
521 */
522 struct aws_byte_cursor private_key_object_label;
523
524 /**
525 * Certificate's file path on disk (UTF-8).
526 * The certificate must be PEM formatted and UTF-8 encoded.
527 * Zero out if passing in certificate by some other means (such as file contents).
528 */
529 struct aws_byte_cursor cert_file_path;
530
531 /**
532 * Certificate's file contents (UTF-8).
533 * The certificate must be PEM formatted and UTF-8 encoded.
534 * Zero out if passing in certificate by some other means (such as file path).
535 */
536 struct aws_byte_cursor cert_file_contents;
537 };
538
539 /**
540 * Initializes options for use with mutual TLS in client mode,
541 * where a PKCS#11 library provides access to the private key.
542 *
543 * NOTE: This only works on Unix devices.
544 *
545 * @param options aws_tls_ctx_options to be initialized.
546 * @param allocator Allocator to use.
547 * @param pkcs11_options Options for using PKCS#11 (contents are copied)
548 */
549 AWS_IO_API int aws_tls_ctx_options_init_client_mtls_with_pkcs11(
550 struct aws_tls_ctx_options *options,
551 struct aws_allocator *allocator,
552 const struct aws_tls_ctx_pkcs11_options *pkcs11_options);
553
554 /**
555 * @Deprecated
556 *
557 * Sets a custom keychain path for storing the cert and pkey with mutual tls in client mode.
558 *
559 * NOTE: This only works on MacOS.
560 */
561 AWS_IO_API int aws_tls_ctx_options_set_keychain_path(
562 struct aws_tls_ctx_options *options,
563 struct aws_byte_cursor *keychain_path_cursor);
564
565 /**
566 * Applies provided SecItem options to certificate and private key being
567 * added to the iOS/tvOS KeyChain.
568 *
569 * NOTE: Currently only supported on iOS and tvOS using SecItem.
570 *
571 * @param options aws_tls_ctx_options to be modified.
572 * @param secitem_options Options for SecItems
573 */
574 AWS_IO_API int aws_tls_ctx_options_set_secitem_options(
575 struct aws_tls_ctx_options *tls_ctx_options,
576 const struct aws_secitem_options *secitem_options);
577
578 /**
579 * Cleans up resources in secitem_options.
580 */
581 AWS_IO_API void aws_tls_secitem_options_clean_up(struct aws_secitem_options *secitem_options);
582
583 /**
584 * Initializes options for use with in server mode.
585 * cert_path and pkey_path are paths to files on disk. cert_path
586 * and pkey_path are treated as PKCS#7 PEM armored. They are loaded
587 * from disk and stored in buffers internally.
588 */
589 AWS_IO_API int aws_tls_ctx_options_init_default_server_from_path(
590 struct aws_tls_ctx_options *options,
591 struct aws_allocator *allocator,
592 const char *cert_path,
593 const char *pkey_path);
594
595 /**
596 * Initializes options for use with in server mode.
597 * cert and pkey are copied. cert and pkey are treated as PKCS#7 PEM
598 * armored.
599 */
600 AWS_IO_API int aws_tls_ctx_options_init_default_server(
601 struct aws_tls_ctx_options *options,
602 struct aws_allocator *allocator,
603 struct aws_byte_cursor *cert,
604 struct aws_byte_cursor *pkey);
605
606 /**
607 * Initializes options for use with mutual tls in client mode.
608 * cert_reg_path is the path to a system
609 * installed certficate/private key pair. Example:
610 * CurrentUser\\MY\\<thumprint>
611 *
612 * NOTE: This only works on Windows.
613 */
614 AWS_IO_API int aws_tls_ctx_options_init_client_mtls_from_system_path(
615 struct aws_tls_ctx_options *options,
616 struct aws_allocator *allocator,
617 const char *cert_reg_path);
618
619 /**
620 * Initializes options for use with server mode.
621 * cert_reg_path is the path to a system
622 * installed certficate/private key pair. Example:
623 * CurrentUser\\MY\\<thumprint>
624 *
625 * NOTE: This only works on Windows.
626 */
627 AWS_IO_API int aws_tls_ctx_options_init_default_server_from_system_path(
628 struct aws_tls_ctx_options *options,
629 struct aws_allocator *allocator,
630 const char *cert_reg_path);
631
632 /**
633 * Initializes options for use with mutual tls in client mode.
634 * pkcs12_path is a path to a file on disk containing a pkcs#12 file. The file is loaded
635 * into an internal buffer. pkcs_pwd is the corresponding password for the pkcs#12 file; it is copied.
636 *
637 * NOTE: This only works on Apple devices.
638 */
639 AWS_IO_API int aws_tls_ctx_options_init_client_mtls_pkcs12_from_path(
640 struct aws_tls_ctx_options *options,
641 struct aws_allocator *allocator,
642 const char *pkcs12_path,
643 const struct aws_byte_cursor *pkcs_pwd);
644
645 /**
646 * Initializes options for use with mutual tls in client mode.
647 * pkcs12 is a buffer containing a pkcs#12 certificate and private key; it is copied.
648 * pkcs_pwd is the corresponding password for the pkcs#12 buffer; it is copied.
649 *
650 * NOTE: This only works on Apple devices.
651 */
652 AWS_IO_API int aws_tls_ctx_options_init_client_mtls_pkcs12(
653 struct aws_tls_ctx_options *options,
654 struct aws_allocator *allocator,
655 struct aws_byte_cursor *pkcs12,
656 struct aws_byte_cursor *pkcs_pwd);
657
658 /**
659 * Initializes options for use in server mode.
660 * pkcs12_path is a path to a file on disk containing a pkcs#12 file. The file is loaded
661 * into an internal buffer. pkcs_pwd is the corresponding password for the pkcs#12 file; it is copied.
662 *
663 * NOTE: This only works on Apple devices.
664 */
665 AWS_IO_API int aws_tls_ctx_options_init_server_pkcs12_from_path(
666 struct aws_tls_ctx_options *options,
667 struct aws_allocator *allocator,
668 const char *pkcs12_path,
669 struct aws_byte_cursor *pkcs_password);
670
671 /**
672 * Initializes options for use in server mode.
673 * pkcs12 is a buffer containing a pkcs#12 certificate and private key; it is copied.
674 * pkcs_pwd is the corresponding password for the pkcs#12 buffer; it is copied.
675 *
676 * NOTE: This only works on Apple devices.
677 */
678 AWS_IO_API int aws_tls_ctx_options_init_server_pkcs12(
679 struct aws_tls_ctx_options *options,
680 struct aws_allocator *allocator,
681 struct aws_byte_cursor *pkcs12,
682 struct aws_byte_cursor *pkcs_password);
683
684 /**
685 * Sets alpn list in the form <protocol1;protocol2;...>. A maximum of 4 protocols are supported.
686 * alpn_list is copied.
687 */
688 AWS_IO_API int aws_tls_ctx_options_set_alpn_list(struct aws_tls_ctx_options *options, const char *alpn_list);
689
690 /**
691 * Enables or disables x.509 validation. Disable this only for testing. To enable mutual TLS in server mode,
692 * set verify_peer to true.
693 */
694 AWS_IO_API void aws_tls_ctx_options_set_verify_peer(struct aws_tls_ctx_options *options, bool verify_peer);
695
696 /**
697 * Enables or disables certificate revocation checking during TLS negotiation.
698 *
699 * On Windows (SChannel), the TLS handshake will make outbound network calls to CRL/OCSP revocation endpoints and makes
700 * a best-effort check. In environments without internet access, this can cause the handshake to block for minutes while
701 * waiting for network timeouts.
702 *
703 * On Linux (s2n) checks against stapled responses
704 *
705 * Set this to true to skip revocation checking entirely.
706 */
707 AWS_IO_API void aws_tls_ctx_options_set_no_certificate_revocation(
708 struct aws_tls_ctx_options *options,
709 bool no_revocation);
710
711 /**
712 * Sets preferred TLS Cipher List
713 */
714 AWS_IO_API void aws_tls_ctx_options_set_tls_cipher_preference(
715 struct aws_tls_ctx_options *options,
716 enum aws_tls_cipher_pref cipher_pref);
717
718 /**
719 * Sets the minimum TLS version to allow.
720 */
721 AWS_IO_API void aws_tls_ctx_options_set_minimum_tls_version(
722 struct aws_tls_ctx_options *options,
723 enum aws_tls_versions minimum_tls_version);
724
725 /**
726 * Override the default trust store. ca_file is a buffer containing a PEM armored chain of trusted CA certificates.
727 * ca_file is copied.
728 */
729 AWS_IO_API int aws_tls_ctx_options_override_default_trust_store(
730 struct aws_tls_ctx_options *options,
731 const struct aws_byte_cursor *ca_file);
732
733 /**
734 * Override the default trust store. ca_path is a path to a directory on disk containing trusted certificates. This is
735 * only supported on Unix systems (otherwise this parameter is ignored). ca_file is a path to a file on disk containing
736 * trusted certificates. ca_file is loaded from disk and stored in an internal buffer.
737 */
738 AWS_IO_API int aws_tls_ctx_options_override_default_trust_store_from_path(
739 struct aws_tls_ctx_options *options,
740 const char *ca_path,
741 const char *ca_file);
742
743 /**
744 * When implementing BYO_CRYPTO, if you need extra data to pass to your tls implementation, set it here. The lifetime of
745 * extension_data must outlive the options object and be cleaned up after options is cleaned up.
746 */
747 AWS_IO_API void aws_tls_ctx_options_set_extension_data(struct aws_tls_ctx_options *options, void *extension_data);
748
749 /**
750 * Initializes default connection options from an instance ot aws_tls_ctx.
751 */
752 AWS_IO_API void aws_tls_connection_options_init_from_ctx(
753 struct aws_tls_connection_options *conn_options,
754 struct aws_tls_ctx *ctx);
755
756 /**
757 * Cleans up resources in aws_tls_connection_options. This can be called immediately after initializing
758 * a tls handler, or if using the bootstrap api, immediately after asking for a channel.
759 */
760 AWS_IO_API void aws_tls_connection_options_clean_up(struct aws_tls_connection_options *connection_options);
761
762 /**
763 * Cleans up 'to' and copies 'from' to 'to'.
764 * 'to' must be initialized.
765 */
766 AWS_IO_API int aws_tls_connection_options_copy(
767 struct aws_tls_connection_options *to,
768 const struct aws_tls_connection_options *from);
769
770 /**
771 * Sets callbacks for use with a tls connection.
772 */
773 AWS_IO_API void aws_tls_connection_options_set_callbacks(
774 struct aws_tls_connection_options *conn_options,
775 aws_tls_on_negotiation_result_fn *on_negotiation_result,
776 aws_tls_on_data_read_fn *on_data_read,
777 aws_tls_on_error_fn *on_error,
778 void *user_data);
779
780 /**
781 * Sets server name to use for the SNI extension (supported everywhere), as well as x.509 validation. If you don't
782 * set this, your x.509 validation will likely fail.
783 */
784 AWS_IO_API int aws_tls_connection_options_set_server_name(
785 struct aws_tls_connection_options *conn_options,
786 struct aws_allocator *allocator,
787 const struct aws_byte_cursor *server_name);
788
789 /**
790 * Sets alpn list in the form <protocol1;protocol2;...>. A maximum of 4 protocols are supported.
791 * alpn_list is copied. This value is already inherited from aws_tls_ctx, but the aws_tls_ctx is expensive,
792 * and should be used across as many connections as possible. If you want to set this per connection, set it here.
793 */
794 AWS_IO_API int aws_tls_connection_options_set_alpn_list(
795 struct aws_tls_connection_options *conn_options,
796 struct aws_allocator *allocator,
797 const char *alpn_list);
798
799 /********************************* TLS context and state management *********************************/
800
801 /**
802 * Returns true if alpn is available in the underlying tls implementation.
803 * This function should always be called before setting an alpn list.
804 */
805 AWS_IO_API bool aws_tls_is_alpn_available(void);
806
807 /**
808 * Returns true if this Cipher Preference is available in the underlying TLS implementation.
809 * This function should always be called before setting a Cipher Preference
810 */
811 AWS_IO_API bool aws_tls_is_cipher_pref_supported(enum aws_tls_cipher_pref cipher_pref);
812
813 /**
814 * Creates a new tls channel handler in client mode. Options will be copied.
815 * You must call aws_tls_client_handler_start_negotiation and wait on the
816 * aws_tls_on_negotiation_result_fn callback before the handler can begin processing
817 * application data.
818 */
819 AWS_IO_API struct aws_channel_handler *aws_tls_client_handler_new(
820 struct aws_allocator *allocator,
821 struct aws_tls_connection_options *options,
822 struct aws_channel_slot *slot);
823
824 /**
825 * Creates a new tls channel handler in server mode. Options will be copied.
826 * You must wait on the aws_tls_on_negotiation_result_fn callback before the handler can begin processing
827 * application data.
828 */
829 AWS_IO_API struct aws_channel_handler *aws_tls_server_handler_new(
830 struct aws_allocator *allocator,
831 struct aws_tls_connection_options *options,
832 struct aws_channel_slot *slot);
833
834 #ifdef BYO_CRYPTO
835 /**
836 * If using BYO_CRYPTO, you need to call this function prior to creating any client channels in the application.
837 */
838 AWS_IO_API void aws_tls_byo_crypto_set_client_setup_options(const struct aws_tls_byo_crypto_setup_options *options);
839 /**
840 * If using BYO_CRYPTO, you need to call this function prior to creating any server channels in the application.
841 */
842 AWS_IO_API void aws_tls_byo_crypto_set_server_setup_options(const struct aws_tls_byo_crypto_setup_options *options);
843
844 #endif /* BYO_CRYPTO */
845
846 /**
847 * Creates a channel handler, for client or server mode, that handles alpn. This isn't necessarily required
848 * since you can always call aws_tls_handler_protocol in the aws_tls_on_negotiation_result_fn callback, but
849 * this makes channel bootstrap easier to handle.
850 */
851 AWS_IO_API struct aws_channel_handler *aws_tls_alpn_handler_new(
852 struct aws_allocator *allocator,
853 aws_tls_on_protocol_negotiated on_protocol_negotiated,
854 void *user_data);
855
856 /**
857 * Kicks off the negotiation process. This function must be called when in client mode to initiate the
858 * TLS handshake. Once the handshake has completed the aws_tls_on_negotiation_result_fn will be invoked.
859 */
860 AWS_IO_API int aws_tls_client_handler_start_negotiation(struct aws_channel_handler *handler);
861
862 #ifndef BYO_CRYPTO
863 /**
864 * Creates a new server ctx. This ctx can be used for the lifetime of the application assuming you want the same
865 * options for every incoming connection. Options will be copied.
866 */
867 AWS_IO_API struct aws_tls_ctx *aws_tls_server_ctx_new(
868 struct aws_allocator *alloc,
869 const struct aws_tls_ctx_options *options);
870
871 /**
872 * Creates a new client ctx. This ctx can be used for the lifetime of the application assuming you want the same
873 * options for every outgoing connection. Options will be copied.
874 */
875 AWS_IO_API struct aws_tls_ctx *aws_tls_client_ctx_new(
876 struct aws_allocator *alloc,
877 const struct aws_tls_ctx_options *options);
878 #endif /* BYO_CRYPTO */
879
880 /**
881 * Increments the reference count on the tls context, allowing the caller to take a reference to it.
882 *
883 * Returns the same tls context passed in.
884 */
885 AWS_IO_API struct aws_tls_ctx *aws_tls_ctx_acquire(struct aws_tls_ctx *ctx);
886
887 /**
888 * Decrements a tls context's ref count. When the ref count drops to zero, the object will be destroyed.
889 */
890 AWS_IO_API void aws_tls_ctx_release(struct aws_tls_ctx *ctx);
891
892 /**
893 * Returns a byte buffer by copy of the negotiated protocols. If there is no agreed upon protocol, len will be 0 and
894 * buffer will be NULL.
895 */
896 AWS_IO_API struct aws_byte_buf aws_tls_handler_protocol(struct aws_channel_handler *handler);
897
898 /**
899 * Client mode only. This is the server name that was used for SNI and host name validation.
900 */
901 AWS_IO_API struct aws_byte_buf aws_tls_handler_server_name(struct aws_channel_handler *handler);
902
903 /**************************** TLS KEY OPERATION *******************************/
904
905 /* Note: Currently this assumes the user knows what key is being used for key/cert pairs
906 but s2n supports multiple cert/key pairs. This functionality is not used in the
907 CRT currently, but in the future, we may need to implement this */
908
909 /**
910 * Complete a successful TLS private key operation by providing its output.
911 * The output is copied into the TLS connection.
912 * The operation is freed by this call.
913 *
914 * You MUST call this or aws_tls_key_operation_complete_with_error().
915 * Failure to do so will stall the TLS connection indefinitely and leak memory.
916 */
917 AWS_IO_API
918 void aws_tls_key_operation_complete(struct aws_tls_key_operation *operation, struct aws_byte_cursor output);
919
920 /**
921 * Complete an failed TLS private key operation.
922 * The TLS connection will fail.
923 * The operation is freed by this call.
924 *
925 * You MUST call this or aws_tls_key_operation_complete().
926 * Failure to do so will stall the TLS connection indefinitely and leak memory.
927 */
928 AWS_IO_API
929 void aws_tls_key_operation_complete_with_error(struct aws_tls_key_operation *operation, int error_code);
930
931 /**
932 * Returns the input data that needs to be operated on by the custom key operation.
933 */
934 AWS_IO_API
935 struct aws_byte_cursor aws_tls_key_operation_get_input(const struct aws_tls_key_operation *operation);
936
937 /**
938 * Returns the type of operation that needs to be performed by the custom key operation.
939 * If the implementation cannot perform the operation,
940 * use aws_tls_key_operation_complete_with_error() to preventing stalling the TLS connection.
941 */
942 AWS_IO_API
943 enum aws_tls_key_operation_type aws_tls_key_operation_get_type(const struct aws_tls_key_operation *operation);
944
945 /**
946 * Returns the algorithm the operation is expected to be operated with.
947 * If the implementation does not support the signature algorithm,
948 * use aws_tls_key_operation_complete_with_error() to preventing stalling the TLS connection.
949 */
950 AWS_IO_API
951 enum aws_tls_signature_algorithm aws_tls_key_operation_get_signature_algorithm(
952 const struct aws_tls_key_operation *operation);
953
954 /**
955 * Returns the algorithm the operation digest is signed with.
956 * If the implementation does not support the digest algorithm,
957 * use aws_tls_key_operation_complete_with_error() to preventing stalling the TLS connection.
958 */
959 AWS_IO_API
960 enum aws_tls_hash_algorithm aws_tls_key_operation_get_digest_algorithm(const struct aws_tls_key_operation *operation);
961
962 /********************************* Misc TLS related *********************************/
963
964 /*
965 * Injects a tls handler/slot into a channel and begins tls negotiation.
966 * If desired, ALPN must be handled separately
967 *
968 * right_of_slot must be an existing slot in a channel
969 */
970 AWS_IO_API int aws_channel_setup_client_tls(
971 struct aws_channel_slot *right_of_slot,
972 struct aws_tls_connection_options *tls_options);
973
974 /**
975 * Given enum, return string like: AWS_TLS_HASH_SHA256 -> "SHA256"
976 */
977 AWS_IO_API
978 const char *aws_tls_hash_algorithm_str(enum aws_tls_hash_algorithm hash);
979
980 /**
981 * Given enum, return string like: AWS_TLS_SIGNATURE_RSA -> "RSA"
982 */
983 AWS_IO_API
984 const char *aws_tls_signature_algorithm_str(enum aws_tls_signature_algorithm signature);
985
986 /**
987 * Given enum, return string like: AWS_TLS_SIGNATURE_RSA -> "RSA"
988 */
989 AWS_IO_API
990 const char *aws_tls_key_operation_type_str(enum aws_tls_key_operation_type operation_type);
991
992 /**
993 * Returns true if error_code is a TLS Negotiation related error.
994 */
995 AWS_IO_API bool aws_error_code_is_tls(int error_code);
996
997 AWS_EXTERN_C_END
998 AWS_POP_SANE_WARNING_LEVEL
999
1000 #endif /* AWS_IO_TLS_CHANNEL_HANDLER_H */