Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 1 | /* |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 Cisco and/or its affiliates. |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 16 | #include <vnet/session/application_interface.h> |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 17 | #include <vnet/session/application.h> |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 18 | #include <vnet/session/session.h> |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 19 | #include <vppinfra/lock.h> |
| 20 | |
| 21 | #ifndef SRC_VNET_TLS_TLS_H_ |
| 22 | #define SRC_VNET_TLS_TLS_H_ |
| 23 | |
Florin Coras | 4b47ee2 | 2020-11-19 13:38:26 -0800 | [diff] [blame] | 24 | #define TLS_DEBUG 0 |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 25 | #define TLS_DEBUG_LEVEL_CLIENT 0 |
| 26 | #define TLS_DEBUG_LEVEL_SERVER 0 |
| 27 | |
| 28 | #define TLS_CHUNK_SIZE (1 << 14) |
| 29 | #define TLS_CA_CERT_PATH "/etc/ssl/certs/ca-certificates.crt" |
| 30 | |
| 31 | #if TLS_DEBUG |
| 32 | #define TLS_DBG(_lvl, _fmt, _args...) \ |
| 33 | if (_lvl <= TLS_DEBUG) \ |
| 34 | clib_warning (_fmt, ##_args) |
| 35 | #else |
Ping Yu | 970a0b8 | 2018-07-19 10:51:09 -0400 | [diff] [blame] | 36 | #define TLS_DBG(_lvl, _fmt, _args...) |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 37 | #endif |
| 38 | |
| 39 | /* *INDENT-OFF* */ |
Florin Coras | 854e0a2 | 2019-07-26 14:46:12 -0700 | [diff] [blame] | 40 | typedef struct tls_cxt_id_ |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 41 | { |
Florin Coras | e6d05dc | 2024-01-22 18:38:08 -0800 | [diff] [blame] | 42 | session_handle_t app_session_handle; |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 43 | session_handle_t tls_session_handle; |
Florin Coras | 2c876f9 | 2021-05-10 21:12:27 -0700 | [diff] [blame] | 44 | void *migrate_ctx; |
Florin Coras | 854e0a2 | 2019-07-26 14:46:12 -0700 | [diff] [blame] | 45 | u32 parent_app_wrk_index; |
Ping Yu | decda5b | 2018-08-13 06:20:00 -0400 | [diff] [blame] | 46 | u32 ssl_ctx; |
Florin Coras | e6d05dc | 2024-01-22 18:38:08 -0800 | [diff] [blame] | 47 | union |
| 48 | { |
| 49 | u32 listener_ctx_index; |
| 50 | u32 parent_app_api_ctx; |
| 51 | }; |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 52 | u8 tcp_is_ip4; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 53 | u8 tls_engine_id; |
Florin Coras | 854e0a2 | 2019-07-26 14:46:12 -0700 | [diff] [blame] | 54 | } tls_ctx_id_t; |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 55 | /* *INDENT-ON* */ |
| 56 | |
Florin Coras | 854e0a2 | 2019-07-26 14:46:12 -0700 | [diff] [blame] | 57 | STATIC_ASSERT (sizeof (tls_ctx_id_t) <= TRANSPORT_CONN_ID_LEN, |
| 58 | "ctx id must be less than TRANSPORT_CONN_ID_LEN"); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 59 | |
Florin Coras | 4a98b93 | 2024-01-31 13:45:39 -0800 | [diff] [blame^] | 60 | #define foreach_tls_conn_flags \ |
| 61 | _ (HO_DONE, "ho-done") \ |
| 62 | _ (PASSIVE_CLOSE, "passive-close") \ |
| 63 | _ (APP_CLOSED, "app-closed") \ |
| 64 | _ (MIGRATED, "migrated") \ |
| 65 | _ (NO_APP_SESSION, "no-app-session") \ |
| 66 | _ (RESUME, "resume") |
Florin Coras | a474bc8 | 2023-12-04 20:29:52 -0800 | [diff] [blame] | 67 | |
| 68 | typedef enum tls_conn_flags_bit_ |
| 69 | { |
| 70 | #define _(sym, str) TLS_CONN_F_BIT_##sym, |
| 71 | foreach_tls_conn_flags |
| 72 | #undef _ |
| 73 | } tls_conn_flags_bit_t; |
| 74 | |
| 75 | typedef enum tls_conn_flags_ |
| 76 | { |
| 77 | #define _(sym, str) TLS_CONN_F_##sym = 1 << TLS_CONN_F_BIT_##sym, |
| 78 | foreach_tls_conn_flags |
| 79 | #undef _ |
| 80 | } __clib_packed tls_conn_flags_t; |
| 81 | |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 82 | typedef struct tls_ctx_ |
| 83 | { |
| 84 | union |
| 85 | { |
| 86 | transport_connection_t connection; |
| 87 | tls_ctx_id_t c_tls_ctx_id; |
| 88 | }; |
Florin Coras | df57ea0 | 2019-02-18 20:14:20 -0800 | [diff] [blame] | 89 | #define parent_app_wrk_index c_tls_ctx_id.parent_app_wrk_index |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 90 | #define app_session_handle c_tls_ctx_id.app_session_handle |
| 91 | #define tls_session_handle c_tls_ctx_id.tls_session_handle |
| 92 | #define listener_ctx_index c_tls_ctx_id.listener_ctx_index |
| 93 | #define tcp_is_ip4 c_tls_ctx_id.tcp_is_ip4 |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 94 | #define tls_ctx_engine c_tls_ctx_id.tls_engine_id |
Ping Yu | decda5b | 2018-08-13 06:20:00 -0400 | [diff] [blame] | 95 | #define tls_ssl_ctx c_tls_ctx_id.ssl_ctx |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 96 | #define tls_ctx_handle c_c_index |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 97 | /* Temporary storage for session open opaque. Overwritten once |
| 98 | * underlying tcp connection is established */ |
Florin Coras | 58a93e8 | 2019-01-14 23:33:46 -0800 | [diff] [blame] | 99 | #define parent_app_api_context c_tls_ctx_id.parent_app_api_ctx |
Florin Coras | 4b47ee2 | 2020-11-19 13:38:26 -0800 | [diff] [blame] | 100 | #define migration_ctx c_tls_ctx_id.migrate_ctx |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 101 | |
Florin Coras | a474bc8 | 2023-12-04 20:29:52 -0800 | [diff] [blame] | 102 | tls_conn_flags_t flags; |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 103 | u8 *srv_hostname; |
Yu Ping | f4a92f6 | 2020-01-21 05:07:30 +0800 | [diff] [blame] | 104 | u32 evt_index; |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 105 | u32 ckpair_index; |
Florin Coras | 4b47ee2 | 2020-11-19 13:38:26 -0800 | [diff] [blame] | 106 | transport_proto_t tls_type; |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 107 | } tls_ctx_t; |
| 108 | |
| 109 | typedef struct tls_main_ |
| 110 | { |
| 111 | u32 app_index; |
| 112 | tls_ctx_t *listener_ctx_pool; |
| 113 | tls_ctx_t *half_open_ctx_pool; |
Florin Coras | a474bc8 | 2023-12-04 20:29:52 -0800 | [diff] [blame] | 114 | u32 *postponed_ho_free; |
| 115 | u32 *ho_free_list; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 116 | u8 **rx_bufs; |
| 117 | u8 **tx_bufs; |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * Config |
| 121 | */ |
| 122 | u8 use_test_cert_in_ca; |
| 123 | char *ca_cert_path; |
Florin Coras | ddd98f3 | 2019-03-25 08:30:53 -0700 | [diff] [blame] | 124 | u64 first_seg_size; |
Florin Coras | a3767bd | 2021-11-18 15:25:31 -0800 | [diff] [blame] | 125 | u64 add_seg_size; |
Florin Coras | ddd98f3 | 2019-03-25 08:30:53 -0700 | [diff] [blame] | 126 | u32 fifo_size; |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 127 | } tls_main_t; |
| 128 | |
| 129 | typedef struct tls_engine_vft_ |
| 130 | { |
| 131 | u32 (*ctx_alloc) (void); |
Florin Coras | 4b47ee2 | 2020-11-19 13:38:26 -0800 | [diff] [blame] | 132 | u32 (*ctx_alloc_w_thread) (u32 thread_index); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 133 | void (*ctx_free) (tls_ctx_t * ctx); |
Florin Coras | 4b47ee2 | 2020-11-19 13:38:26 -0800 | [diff] [blame] | 134 | void *(*ctx_detach) (tls_ctx_t *ctx); |
| 135 | u32 (*ctx_attach) (u32 thread_index, void *ctx); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 136 | tls_ctx_t *(*ctx_get) (u32 ctx_index); |
| 137 | tls_ctx_t *(*ctx_get_w_thread) (u32 ctx_index, u8 thread_index); |
| 138 | int (*ctx_init_client) (tls_ctx_t * ctx); |
| 139 | int (*ctx_init_server) (tls_ctx_t * ctx); |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 140 | int (*ctx_read) (tls_ctx_t * ctx, session_t * tls_session); |
Florin Coras | 9f86d22 | 2020-03-23 15:34:22 +0000 | [diff] [blame] | 141 | int (*ctx_write) (tls_ctx_t * ctx, session_t * app_session, |
| 142 | transport_send_params_t * sp); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 143 | u8 (*ctx_handshake_is_over) (tls_ctx_t * ctx); |
Ping Yu | decda5b | 2018-08-13 06:20:00 -0400 | [diff] [blame] | 144 | int (*ctx_start_listen) (tls_ctx_t * ctx); |
| 145 | int (*ctx_stop_listen) (tls_ctx_t * ctx); |
Florin Coras | 06a6a30 | 2019-04-17 14:19:12 -0700 | [diff] [blame] | 146 | int (*ctx_transport_close) (tls_ctx_t * ctx); |
| 147 | int (*ctx_app_close) (tls_ctx_t * ctx); |
Saravanan Murugesan | d918cc5 | 2022-02-28 19:54:11 +0530 | [diff] [blame] | 148 | int (*ctx_reinit_cachain) (void); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 149 | } tls_engine_vft_t; |
| 150 | |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 151 | tls_main_t *vnet_tls_get_main (void); |
| 152 | void tls_register_engine (const tls_engine_vft_t * vft, |
Nathan Skrzypczak | 82fc5fd | 2019-09-13 10:20:15 +0200 | [diff] [blame] | 153 | crypto_engine_type_t type); |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 154 | int tls_add_vpp_q_rx_evt (session_t * s); |
| 155 | int tls_add_vpp_q_tx_evt (session_t * s); |
| 156 | int tls_add_vpp_q_builtin_tx_evt (session_t * s); |
| 157 | int tls_add_vpp_q_builtin_rx_evt (session_t * s); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 158 | int tls_notify_app_accept (tls_ctx_t * ctx); |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 159 | int tls_notify_app_connected (tls_ctx_t * ctx, session_error_t err); |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 160 | void tls_notify_app_enqueue (tls_ctx_t * ctx, session_t * app_session); |
Florin Coras | 8c5e5f6 | 2022-02-24 16:35:26 -0800 | [diff] [blame] | 161 | void tls_notify_app_io_error (tls_ctx_t *ctx); |
Florin Coras | 06a6a30 | 2019-04-17 14:19:12 -0700 | [diff] [blame] | 162 | void tls_disconnect_transport (tls_ctx_t * ctx); |
Saravanan Murugesan | d918cc5 | 2022-02-28 19:54:11 +0530 | [diff] [blame] | 163 | int tls_reinit_ca_chain (crypto_engine_type_t tls_engine_id); |
Florin Coras | a474bc8 | 2023-12-04 20:29:52 -0800 | [diff] [blame] | 164 | |
| 165 | void tls_add_postponed_ho_cleanups (u32 ho_index); |
| 166 | void tls_flush_postponed_ho_cleanups (); |
| 167 | |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 168 | #endif /* SRC_VNET_TLS_TLS_H_ */ |
Florin Coras | 54a51fd | 2019-02-07 15:34:52 -0800 | [diff] [blame] | 169 | |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 170 | /* |
| 171 | * fd.io coding-style-patch-verification: ON |
| 172 | * |
| 173 | * Local Variables: |
| 174 | * eval: (c-set-style "gnu") |
| 175 | * End: |
| 176 | */ |