tls: dtls initial implementation
[vpp.git] / src / vnet / tls / tls.h
1 /*
2  * Copyright (c) 2018-2019 Cisco and/or its affiliates.
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
16 #include <vnet/session/application_interface.h>
17 #include <vnet/session/application.h>
18 #include <vnet/session/session.h>
19 #include <vppinfra/lock.h>
20
21 #ifndef SRC_VNET_TLS_TLS_H_
22 #define SRC_VNET_TLS_TLS_H_
23
24 #define TLS_DEBUG               0
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
36 #define TLS_DBG(_lvl, _fmt, _args...)
37 #endif
38
39 /* *INDENT-OFF* */
40 typedef struct tls_cxt_id_
41 {
42   union {
43     session_handle_t app_session_handle;
44     u32 parent_app_api_ctx;
45   };
46   session_handle_t tls_session_handle;
47   u32 parent_app_wrk_index;
48   u32 ssl_ctx;
49   u32 listener_ctx_index;
50   u8 tcp_is_ip4;
51   u8 tls_engine_id;
52   void *migrate_ctx;
53 } tls_ctx_id_t;
54 /* *INDENT-ON* */
55
56 STATIC_ASSERT (sizeof (tls_ctx_id_t) <= TRANSPORT_CONN_ID_LEN,
57                "ctx id must be less than TRANSPORT_CONN_ID_LEN");
58
59 typedef struct tls_ctx_
60 {
61   union
62   {
63     transport_connection_t connection;
64     tls_ctx_id_t c_tls_ctx_id;
65   };
66 #define parent_app_wrk_index c_tls_ctx_id.parent_app_wrk_index
67 #define app_session_handle c_tls_ctx_id.app_session_handle
68 #define tls_session_handle c_tls_ctx_id.tls_session_handle
69 #define listener_ctx_index c_tls_ctx_id.listener_ctx_index
70 #define tcp_is_ip4 c_tls_ctx_id.tcp_is_ip4
71 #define tls_ctx_engine c_tls_ctx_id.tls_engine_id
72 #define tls_ssl_ctx c_tls_ctx_id.ssl_ctx
73 #define tls_ctx_handle c_c_index
74   /* Temporary storage for session open opaque. Overwritten once
75    * underlying tcp connection is established */
76 #define parent_app_api_context c_tls_ctx_id.parent_app_api_ctx
77 #define migration_ctx          c_tls_ctx_id.migrate_ctx
78
79   u8 is_passive_close;
80   u8 resume;
81   u8 app_closed;
82   u8 no_app_session;
83   u8 is_migrated;
84   u8 *srv_hostname;
85   u32 evt_index;
86   u32 ckpair_index;
87   transport_proto_t tls_type;
88 } tls_ctx_t;
89
90 typedef struct tls_main_
91 {
92   u32 app_index;
93   tls_ctx_t *listener_ctx_pool;
94   tls_ctx_t *half_open_ctx_pool;
95   clib_rwlock_t half_open_rwlock;
96   u8 **rx_bufs;
97   u8 **tx_bufs;
98
99   /*
100    * Config
101    */
102   u8 use_test_cert_in_ca;
103   char *ca_cert_path;
104   u64 first_seg_size;
105   u32 fifo_size;
106 } tls_main_t;
107
108 typedef struct tls_engine_vft_
109 {
110   u32 (*ctx_alloc) (void);
111   u32 (*ctx_alloc_w_thread) (u32 thread_index);
112   void (*ctx_free) (tls_ctx_t * ctx);
113   void *(*ctx_detach) (tls_ctx_t *ctx);
114   u32 (*ctx_attach) (u32 thread_index, void *ctx);
115   tls_ctx_t *(*ctx_get) (u32 ctx_index);
116   tls_ctx_t *(*ctx_get_w_thread) (u32 ctx_index, u8 thread_index);
117   int (*ctx_init_client) (tls_ctx_t * ctx);
118   int (*ctx_init_server) (tls_ctx_t * ctx);
119   int (*ctx_read) (tls_ctx_t * ctx, session_t * tls_session);
120   int (*ctx_write) (tls_ctx_t * ctx, session_t * app_session,
121                     transport_send_params_t * sp);
122     u8 (*ctx_handshake_is_over) (tls_ctx_t * ctx);
123   int (*ctx_start_listen) (tls_ctx_t * ctx);
124   int (*ctx_stop_listen) (tls_ctx_t * ctx);
125   int (*ctx_transport_close) (tls_ctx_t * ctx);
126   int (*ctx_app_close) (tls_ctx_t * ctx);
127 } tls_engine_vft_t;
128
129 tls_main_t *vnet_tls_get_main (void);
130 void tls_register_engine (const tls_engine_vft_t * vft,
131                           crypto_engine_type_t type);
132 int tls_add_vpp_q_rx_evt (session_t * s);
133 int tls_add_vpp_q_tx_evt (session_t * s);
134 int tls_add_vpp_q_builtin_tx_evt (session_t * s);
135 int tls_add_vpp_q_builtin_rx_evt (session_t * s);
136 int tls_notify_app_accept (tls_ctx_t * ctx);
137 int tls_notify_app_connected (tls_ctx_t * ctx, session_error_t err);
138 void tls_notify_app_enqueue (tls_ctx_t * ctx, session_t * app_session);
139 void tls_disconnect_transport (tls_ctx_t * ctx);
140 #endif /* SRC_VNET_TLS_TLS_H_ */
141
142 /*
143  * fd.io coding-style-patch-verification: ON
144  *
145  * Local Variables:
146  * eval: (c-set-style "gnu")
147  * End:
148  */