#ifndef AWS_IO_TLS_CHANNEL_HANDLER_H
#define AWS_IO_TLS_CHANNEL_HANDLER_H
#include <aws/common/byte_buf.h>
#include <aws/common/ref_count.h>
#include <aws/io/io.h>
struct aws_channel_slot;
struct aws_channel_handler;
struct aws_pkcs11_session;
struct aws_string;
enum aws_tls_versions {
AWS_IO_SSLv3,
AWS_IO_TLSv1,
AWS_IO_TLSv1_1,
AWS_IO_TLSv1_2,
AWS_IO_TLSv1_3,
AWS_IO_TLS_VER_SYS_DEFAULTS = 128,
};
enum aws_tls_cipher_pref {
AWS_IO_TLS_CIPHER_PREF_SYSTEM_DEFAULT = 0,
AWS_IO_TLS_CIPHER_PREF_KMS_PQ_TLSv1_0_2019_06 = 1,
AWS_IO_TLS_CIPHER_PREF_KMS_PQ_SIKE_TLSv1_0_2019_11 = 2,
AWS_IO_TLS_CIPHER_PREF_KMS_PQ_TLSv1_0_2020_02 = 3,
AWS_IO_TLS_CIPHER_PREF_KMS_PQ_SIKE_TLSv1_0_2020_02 = 4,
AWS_IO_TLS_CIPHER_PREF_KMS_PQ_TLSv1_0_2020_07 = 5,
AWS_IO_TLS_CIPHER_PREF_PQ_TLSv1_0_2021_05 = 6,
AWS_IO_TLS_CIPHER_PREF_END_RANGE = 0xFFFF
};
enum aws_tls_hash_algorithm {
AWS_TLS_HASH_UNKNOWN,
AWS_TLS_HASH_SHA1,
AWS_TLS_HASH_SHA224,
AWS_TLS_HASH_SHA256,
AWS_TLS_HASH_SHA384,
AWS_TLS_HASH_SHA512,
};
enum aws_tls_signature_algorithm {
AWS_TLS_SIGNATURE_UNKNOWN,
AWS_TLS_SIGNATURE_RSA,
AWS_TLS_SIGNATURE_ECDSA,
};
enum aws_tls_key_operation_type {
AWS_TLS_KEY_OPERATION_UNKNOWN,
AWS_TLS_KEY_OPERATION_SIGN,
AWS_TLS_KEY_OPERATION_DECRYPT,
};
struct aws_tls_ctx {
struct aws_allocator *alloc;
void *impl;
struct aws_ref_count ref_count;
};
typedef void(aws_tls_on_negotiation_result_fn)(
struct aws_channel_handler *handler,
struct aws_channel_slot *slot,
int error_code,
void *user_data);
typedef void(aws_tls_on_data_read_fn)(
struct aws_channel_handler *handler,
struct aws_channel_slot *slot,
struct aws_byte_buf *buffer,
void *user_data);
typedef void(aws_tls_on_error_fn)(
struct aws_channel_handler *handler,
struct aws_channel_slot *slot,
int err,
const char *message,
void *user_data);
struct aws_tls_connection_options {
struct aws_string *alpn_list;
struct aws_string *server_name;
aws_tls_on_negotiation_result_fn *on_negotiation_result;
aws_tls_on_data_read_fn *on_data_read;
aws_tls_on_error_fn *on_error;
void *user_data;
struct aws_tls_ctx *ctx;
bool advertise_alpn_message;
uint32_t timeout_ms;
};
struct aws_tls_key_operation;
struct aws_tls_ctx_options {
struct aws_allocator *allocator;
enum aws_tls_versions minimum_tls_version;
enum aws_tls_cipher_pref cipher_pref;
struct aws_byte_buf ca_file;
struct aws_string *ca_path;
struct aws_string *alpn_list;
struct aws_byte_buf certificate;
#ifdef _WIN32
const char *system_certificate_path;
#endif
struct aws_byte_buf private_key;
#ifdef __APPLE__
struct aws_byte_buf pkcs12;
struct aws_byte_buf pkcs12_password;
# if !defined(AWS_OS_IOS)
struct aws_string *keychain_path;
# endif
#endif
size_t max_fragment_size;
bool verify_peer;
void *ctx_options_extension;
struct aws_custom_key_op_handler *custom_key_op_handler;
};
struct aws_tls_negotiated_protocol_message {
struct aws_byte_buf protocol;
};
static const int AWS_TLS_NEGOTIATED_PROTOCOL_MESSAGE = 0x01;
typedef struct aws_channel_handler *(
*aws_tls_on_protocol_negotiated)(struct aws_channel_slot *new_slot, struct aws_byte_buf *protocol, void *user_data);
enum aws_tls_negotiation_status {
AWS_TLS_NEGOTIATION_STATUS_NONE,
AWS_TLS_NEGOTIATION_STATUS_ONGOING,
AWS_TLS_NEGOTIATION_STATUS_SUCCESS,
AWS_TLS_NEGOTIATION_STATUS_FAILURE
};
#ifdef BYO_CRYPTO
typedef struct aws_channel_handler *(aws_tls_handler_new_fn)(
struct aws_allocator *allocator,
struct aws_tls_connection_options *options,
struct aws_channel_slot *slot,
void *user_data);
typedef int(aws_tls_client_handler_start_negotiation_fn)(struct aws_channel_handler *handler, void *user_data);
struct aws_tls_byo_crypto_setup_options {
aws_tls_handler_new_fn *new_handler_fn;
aws_tls_client_handler_start_negotiation_fn *start_negotiation_fn;
void *user_data;
};
#endif
AWS_EXTERN_C_BEGIN
AWS_IO_API void aws_tls_ctx_options_init_default_client(
struct aws_tls_ctx_options *options,
struct aws_allocator *allocator);
AWS_IO_API void aws_tls_ctx_options_clean_up(struct aws_tls_ctx_options *options);
AWS_IO_API int aws_tls_ctx_options_init_client_mtls_from_path(
struct aws_tls_ctx_options *options,
struct aws_allocator *allocator,
const char *cert_path,
const char *pkey_path);
AWS_IO_API int aws_tls_ctx_options_init_client_mtls(
struct aws_tls_ctx_options *options,
struct aws_allocator *allocator,
const struct aws_byte_cursor *cert,
const struct aws_byte_cursor *pkey);
struct aws_custom_key_op_handler_vtable {
void (*on_key_operation)(struct aws_custom_key_op_handler *key_op_handler, struct aws_tls_key_operation *operation);
};
struct aws_custom_key_op_handler {
void *impl;
const struct aws_custom_key_op_handler_vtable *vtable;
struct aws_ref_count ref_count;
};
AWS_IO_API struct aws_custom_key_op_handler *aws_custom_key_op_handler_acquire(
struct aws_custom_key_op_handler *key_op_handler);
AWS_IO_API struct aws_custom_key_op_handler *aws_custom_key_op_handler_release(
struct aws_custom_key_op_handler *key_op_handler);
AWS_IO_API void aws_custom_key_op_handler_perform_operation(
struct aws_custom_key_op_handler *key_op_handler,
struct aws_tls_key_operation *operation);
AWS_IO_API int aws_tls_ctx_options_init_client_mtls_with_custom_key_operations(
struct aws_tls_ctx_options *options,
struct aws_allocator *allocator,
struct aws_custom_key_op_handler *custom,
const struct aws_byte_cursor *cert_file_contents);
struct aws_tls_ctx_pkcs11_options {
struct aws_pkcs11_lib *pkcs11_lib;
struct aws_byte_cursor user_pin;
const uint64_t *slot_id;
struct aws_byte_cursor token_label;
struct aws_byte_cursor private_key_object_label;
struct aws_byte_cursor cert_file_path;
struct aws_byte_cursor cert_file_contents;
};
AWS_IO_API int aws_tls_ctx_options_init_client_mtls_with_pkcs11(
struct aws_tls_ctx_options *options,
struct aws_allocator *allocator,
const struct aws_tls_ctx_pkcs11_options *pkcs11_options);
AWS_IO_API int aws_tls_ctx_options_set_keychain_path(
struct aws_tls_ctx_options *options,
struct aws_byte_cursor *keychain_path_cursor);
AWS_IO_API int aws_tls_ctx_options_init_default_server_from_path(
struct aws_tls_ctx_options *options,
struct aws_allocator *allocator,
const char *cert_path,
const char *pkey_path);
AWS_IO_API int aws_tls_ctx_options_init_default_server(
struct aws_tls_ctx_options *options,
struct aws_allocator *allocator,
struct aws_byte_cursor *cert,
struct aws_byte_cursor *pkey);
AWS_IO_API int aws_tls_ctx_options_init_client_mtls_from_system_path(
struct aws_tls_ctx_options *options,
struct aws_allocator *allocator,
const char *cert_reg_path);
AWS_IO_API int aws_tls_ctx_options_init_default_server_from_system_path(
struct aws_tls_ctx_options *options,
struct aws_allocator *allocator,
const char *cert_reg_path);
AWS_IO_API int aws_tls_ctx_options_init_client_mtls_pkcs12_from_path(
struct aws_tls_ctx_options *options,
struct aws_allocator *allocator,
const char *pkcs12_path,
const struct aws_byte_cursor *pkcs_pwd);
AWS_IO_API int aws_tls_ctx_options_init_client_mtls_pkcs12(
struct aws_tls_ctx_options *options,
struct aws_allocator *allocator,
struct aws_byte_cursor *pkcs12,
struct aws_byte_cursor *pkcs_pwd);
AWS_IO_API int aws_tls_ctx_options_init_server_pkcs12_from_path(
struct aws_tls_ctx_options *options,
struct aws_allocator *allocator,
const char *pkcs12_path,
struct aws_byte_cursor *pkcs_password);
AWS_IO_API int aws_tls_ctx_options_init_server_pkcs12(
struct aws_tls_ctx_options *options,
struct aws_allocator *allocator,
struct aws_byte_cursor *pkcs12,
struct aws_byte_cursor *pkcs_password);
AWS_IO_API int aws_tls_ctx_options_set_alpn_list(struct aws_tls_ctx_options *options, const char *alpn_list);
AWS_IO_API void aws_tls_ctx_options_set_verify_peer(struct aws_tls_ctx_options *options, bool verify_peer);
AWS_IO_API void aws_tls_ctx_options_set_minimum_tls_version(
struct aws_tls_ctx_options *options,
enum aws_tls_versions minimum_tls_version);
AWS_IO_API int aws_tls_ctx_options_override_default_trust_store(
struct aws_tls_ctx_options *options,
const struct aws_byte_cursor *ca_file);
AWS_IO_API int aws_tls_ctx_options_override_default_trust_store_from_path(
struct aws_tls_ctx_options *options,
const char *ca_path,
const char *ca_file);
AWS_IO_API void aws_tls_ctx_options_set_extension_data(struct aws_tls_ctx_options *options, void *extension_data);
AWS_IO_API void aws_tls_connection_options_init_from_ctx(
struct aws_tls_connection_options *conn_options,
struct aws_tls_ctx *ctx);
AWS_IO_API void aws_tls_connection_options_clean_up(struct aws_tls_connection_options *connection_options);
AWS_IO_API int aws_tls_connection_options_copy(
struct aws_tls_connection_options *to,
const struct aws_tls_connection_options *from);
AWS_IO_API void aws_tls_connection_options_set_callbacks(
struct aws_tls_connection_options *conn_options,
aws_tls_on_negotiation_result_fn *on_negotiation_result,
aws_tls_on_data_read_fn *on_data_read,
aws_tls_on_error_fn *on_error,
void *user_data);
AWS_IO_API int aws_tls_connection_options_set_server_name(
struct aws_tls_connection_options *conn_options,
struct aws_allocator *allocator,
const struct aws_byte_cursor *server_name);
AWS_IO_API int aws_tls_connection_options_set_alpn_list(
struct aws_tls_connection_options *conn_options,
struct aws_allocator *allocator,
const char *alpn_list);
AWS_IO_API bool aws_tls_is_alpn_available(void);
AWS_IO_API bool aws_tls_is_cipher_pref_supported(enum aws_tls_cipher_pref cipher_pref);
AWS_IO_API struct aws_channel_handler *aws_tls_client_handler_new(
struct aws_allocator *allocator,
struct aws_tls_connection_options *options,
struct aws_channel_slot *slot);
AWS_IO_API struct aws_channel_handler *aws_tls_server_handler_new(
struct aws_allocator *allocator,
struct aws_tls_connection_options *options,
struct aws_channel_slot *slot);
#ifdef BYO_CRYPTO
AWS_IO_API void aws_tls_byo_crypto_set_client_setup_options(const struct aws_tls_byo_crypto_setup_options *options);
AWS_IO_API void aws_tls_byo_crypto_set_server_setup_options(const struct aws_tls_byo_crypto_setup_options *options);
#endif
AWS_IO_API struct aws_channel_handler *aws_tls_alpn_handler_new(
struct aws_allocator *allocator,
aws_tls_on_protocol_negotiated on_protocol_negotiated,
void *user_data);
AWS_IO_API int aws_tls_client_handler_start_negotiation(struct aws_channel_handler *handler);
#ifndef BYO_CRYPTO
AWS_IO_API struct aws_tls_ctx *aws_tls_server_ctx_new(
struct aws_allocator *alloc,
const struct aws_tls_ctx_options *options);
AWS_IO_API struct aws_tls_ctx *aws_tls_client_ctx_new(
struct aws_allocator *alloc,
const struct aws_tls_ctx_options *options);
#endif
AWS_IO_API struct aws_tls_ctx *aws_tls_ctx_acquire(struct aws_tls_ctx *ctx);
AWS_IO_API void aws_tls_ctx_release(struct aws_tls_ctx *ctx);
AWS_IO_API int aws_tls_handler_write(
struct aws_channel_handler *handler,
struct aws_channel_slot *slot,
struct aws_byte_buf *buf,
aws_channel_on_message_write_completed_fn *on_write_completed,
void *completion_user_data);
AWS_IO_API struct aws_byte_buf aws_tls_handler_protocol(struct aws_channel_handler *handler);
AWS_IO_API struct aws_byte_buf aws_tls_handler_server_name(struct aws_channel_handler *handler);
AWS_IO_API
void aws_tls_key_operation_complete(struct aws_tls_key_operation *operation, struct aws_byte_cursor output);
AWS_IO_API
void aws_tls_key_operation_complete_with_error(struct aws_tls_key_operation *operation, int error_code);
AWS_IO_API
struct aws_byte_cursor aws_tls_key_operation_get_input(const struct aws_tls_key_operation *operation);
AWS_IO_API
enum aws_tls_key_operation_type aws_tls_key_operation_get_type(const struct aws_tls_key_operation *operation);
AWS_IO_API
enum aws_tls_signature_algorithm aws_tls_key_operation_get_signature_algorithm(
const struct aws_tls_key_operation *operation);
AWS_IO_API
enum aws_tls_hash_algorithm aws_tls_key_operation_get_digest_algorithm(const struct aws_tls_key_operation *operation);
AWS_IO_API int aws_channel_setup_client_tls(
struct aws_channel_slot *right_of_slot,
struct aws_tls_connection_options *tls_options);
AWS_IO_API
const char *aws_tls_hash_algorithm_str(enum aws_tls_hash_algorithm hash);
AWS_IO_API
const char *aws_tls_signature_algorithm_str(enum aws_tls_signature_algorithm signature);
AWS_IO_API
const char *aws_tls_key_operation_type_str(enum aws_tls_key_operation_type operation_type);
AWS_EXTERN_C_END
#endif