summaryrefslogtreecommitdiffstats
path: root/src/plugins/tlsopenssl
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/tlsopenssl')
-rw-r--r--src/plugins/tlsopenssl/CMakeLists.txt1
-rw-r--r--src/plugins/tlsopenssl/dtls_bio.c225
-rw-r--r--src/plugins/tlsopenssl/tls_bios.h (renamed from src/plugins/tlsopenssl/tls_bio.h)1
-rw-r--r--src/plugins/tlsopenssl/tls_openssl.c244
-rw-r--r--src/plugins/tlsopenssl/tls_openssl.h10
5 files changed, 462 insertions, 19 deletions
diff --git a/src/plugins/tlsopenssl/CMakeLists.txt b/src/plugins/tlsopenssl/CMakeLists.txt
index 61d3638a39a..eb67e4cceaf 100644
--- a/src/plugins/tlsopenssl/CMakeLists.txt
+++ b/src/plugins/tlsopenssl/CMakeLists.txt
@@ -20,6 +20,7 @@ if(OPENSSL_FOUND AND OPENSSL_VERSION VERSION_GREATER_EQUAL "1.1.0")
tls_openssl.c
tls_openssl_api.c
tls_async.c
+ dtls_bio.c
API_FILES
tls_openssl.api
diff --git a/src/plugins/tlsopenssl/dtls_bio.c b/src/plugins/tlsopenssl/dtls_bio.c
new file mode 100644
index 00000000000..7cd2abd42e2
--- /dev/null
+++ b/src/plugins/tlsopenssl/dtls_bio.c
@@ -0,0 +1,225 @@
+/*
+ * Copyright (c) 2021 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <openssl/bio.h>
+#include <openssl/err.h>
+
+#include <vnet/session/session.h>
+#include <vnet/session/application_interface.h>
+
+static inline session_t *
+bio_session (BIO *bio)
+{
+ return session_get_from_handle (pointer_to_uword (BIO_get_data (bio)));
+}
+
+static int
+bio_dtls_alloc (BIO *bio)
+{
+ BIO_set_init (bio, 0);
+ BIO_set_data (bio, 0);
+ BIO_set_flags (bio, 0);
+ BIO_set_shutdown (bio, 0);
+ return 1;
+}
+
+static int
+bio_dtls_free (BIO *bio)
+{
+ if (!bio)
+ return 0;
+
+ if (BIO_get_shutdown (bio))
+ {
+ if (BIO_get_init (bio))
+ session_close (bio_session (bio));
+ BIO_set_init (bio, 0);
+ BIO_set_flags (bio, 0);
+ }
+ return 1;
+}
+
+static int
+bio_dtls_read (BIO *b, char *out, int outl)
+{
+ app_session_transport_t at;
+ session_t *s;
+ int rv;
+
+ if (PREDICT_FALSE (!out))
+ return 0;
+
+ s = bio_session (b);
+ if (!s)
+ {
+ clib_warning ("no session");
+ errno = EBADFD;
+ return -1;
+ }
+
+ rv = app_recv_dgram_raw (s->rx_fifo, (u8 *) out, outl, &at,
+ 0 /* clear evt */, 0 /* peek */);
+
+ if (rv < 0)
+ {
+ BIO_set_retry_read (b);
+ errno = EAGAIN;
+ return -1;
+ }
+
+ if (svm_fifo_is_empty_cons (s->rx_fifo))
+ svm_fifo_unset_event (s->rx_fifo);
+
+ BIO_clear_retry_flags (b);
+
+ return rv;
+}
+
+static int
+bio_dtls_write (BIO *b, const char *in, int inl)
+{
+ app_session_transport_t at = { 0 };
+ svm_msg_q_t *mq;
+ session_t *s;
+ int rv;
+
+ if (PREDICT_FALSE (!in))
+ return 0;
+
+ s = bio_session (b);
+ if (!s)
+ {
+ clib_warning ("no session");
+ errno = EBADFD;
+ return -1;
+ }
+
+ mq = session_main_get_vpp_event_queue (s->thread_index);
+ rv = app_send_dgram_raw (s->tx_fifo, &at, mq, (u8 *) in, inl,
+ SESSION_IO_EVT_TX, 1 /* do_evt */, 0 /* noblock */);
+
+ if (rv < 0)
+ {
+ BIO_set_retry_read (b);
+ errno = EAGAIN;
+ return -1;
+ }
+
+ BIO_clear_retry_flags (b);
+
+ return rv;
+}
+
+static int
+dtls_dgram_overhead (BIO *b)
+{
+ session_t *s = bio_session (b);
+ if (session_type_is_ip4 (s->session_type))
+ /* 20B ip 8B udp */
+ return 28;
+ else
+ /* 40B ip 8B udp */
+ return 48;
+}
+
+static u16
+dtls_dgram_mss (BIO *b)
+{
+ session_t *s = bio_session (b);
+ transport_send_params_t sp;
+
+ transport_connection_snd_params (session_get_transport (s), &sp);
+
+ return sp.snd_mss;
+}
+
+long
+bio_dtls_ctrl (BIO *b, int cmd, long larg, void *parg)
+{
+ long ret = 1;
+
+ switch (cmd)
+ {
+ case BIO_C_SET_FD:
+ os_panic ();
+ break;
+ case BIO_C_GET_FD:
+ os_panic ();
+ break;
+ case BIO_CTRL_GET_CLOSE:
+ ret = BIO_get_shutdown (b);
+ break;
+ case BIO_CTRL_SET_CLOSE:
+ BIO_set_shutdown (b, (int) larg);
+ break;
+ case BIO_CTRL_PENDING:
+ case BIO_CTRL_WPENDING:
+ ret = 0;
+ break;
+ case BIO_CTRL_DUP:
+ case BIO_CTRL_FLUSH:
+ ret = 1;
+ break;
+ case BIO_CTRL_DGRAM_QUERY_MTU:
+ ret = dtls_dgram_mss (b);
+ break;
+ case BIO_CTRL_DGRAM_SET_MTU:
+ ret = 0;
+ break;
+ case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
+ ret = 0;
+ break;
+ case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
+ ret = dtls_dgram_overhead (b);
+ break;
+ default:
+ ret = 0;
+ break;
+ }
+ return ret;
+}
+
+BIO *
+BIO_new_dtls (session_handle_t sh)
+{
+ static BIO_METHOD *dtls_bio_method;
+ BIO *b;
+
+ if (!dtls_bio_method)
+ {
+ dtls_bio_method = BIO_meth_new (BIO_TYPE_SOCKET, "dtls_bio");
+ BIO_meth_set_write (dtls_bio_method, bio_dtls_write);
+ BIO_meth_set_read (dtls_bio_method, bio_dtls_read);
+ BIO_meth_set_create (dtls_bio_method, bio_dtls_alloc);
+ BIO_meth_set_destroy (dtls_bio_method, bio_dtls_free);
+ BIO_meth_set_ctrl (dtls_bio_method, bio_dtls_ctrl);
+ }
+
+ b = BIO_new (dtls_bio_method);
+
+ /* Initialize the BIO */
+ BIO_set_data (b, uword_to_pointer (sh, void *));
+ BIO_set_init (b, 1);
+
+ return b;
+}
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/plugins/tlsopenssl/tls_bio.h b/src/plugins/tlsopenssl/tls_bios.h
index b9f3c5384b9..259900ef754 100644
--- a/src/plugins/tlsopenssl/tls_bio.h
+++ b/src/plugins/tlsopenssl/tls_bios.h
@@ -19,6 +19,7 @@
#include <vnet/session/session_types.h>
BIO *BIO_new_tls (session_handle_t sh);
+BIO *BIO_new_dtls (session_handle_t sh);
#endif /* SRC_PLUGINS_TLSOPENSSL_TLS_BIO_H_ */
diff --git a/src/plugins/tlsopenssl/tls_openssl.c b/src/plugins/tlsopenssl/tls_openssl.c
index 789c64d88d3..00cd4621897 100644
--- a/src/plugins/tlsopenssl/tls_openssl.c
+++ b/src/plugins/tlsopenssl/tls_openssl.c
@@ -26,19 +26,19 @@
#include <vnet/tls/tls.h>
#include <ctype.h>
#include <tlsopenssl/tls_openssl.h>
-#include <tlsopenssl/tls_bio.h>
+#include <tlsopenssl/tls_bios.h>
#define MAX_CRYPTO_LEN 64
openssl_main_t openssl_main;
+
static u32
-openssl_ctx_alloc (void)
+openssl_ctx_alloc_w_thread (u32 thread_index)
{
- u8 thread_index = vlib_get_thread_index ();
- openssl_main_t *tm = &openssl_main;
+ openssl_main_t *om = &openssl_main;
openssl_ctx_t **ctx;
- pool_get (tm->ctx_pool[thread_index], ctx);
+ pool_get (om->ctx_pool[thread_index], ctx);
if (!(*ctx))
*ctx = clib_mem_alloc (sizeof (openssl_ctx_t));
@@ -46,10 +46,16 @@ openssl_ctx_alloc (void)
(*ctx)->ctx.c_thread_index = thread_index;
(*ctx)->ctx.tls_ctx_engine = CRYPTO_ENGINE_OPENSSL;
(*ctx)->ctx.app_session_handle = SESSION_INVALID_HANDLE;
- (*ctx)->openssl_ctx_index = ctx - tm->ctx_pool[thread_index];
+ (*ctx)->openssl_ctx_index = ctx - om->ctx_pool[thread_index];
return ((*ctx)->openssl_ctx_index);
}
+static u32
+openssl_ctx_alloc (void)
+{
+ return openssl_ctx_alloc_w_thread (vlib_get_thread_index ());
+}
+
static void
openssl_ctx_free (tls_ctx_t * ctx)
{
@@ -68,6 +74,40 @@ openssl_ctx_free (tls_ctx_t * ctx)
oc->openssl_ctx_index);
}
+static void *
+openssl_ctx_detach (tls_ctx_t *ctx)
+{
+ openssl_ctx_t *oc = (openssl_ctx_t *) ctx, *oc_copy;
+
+ oc_copy = clib_mem_alloc (sizeof (*oc_copy));
+ clib_memcpy (oc_copy, oc, sizeof (*oc));
+
+ return oc_copy;
+}
+
+static u32
+openssl_ctx_attach (u32 thread_index, void *ctx_ptr)
+{
+ openssl_main_t *om = &openssl_main;
+ session_handle_t sh;
+ openssl_ctx_t **oc;
+
+ pool_get (om->ctx_pool[thread_index], oc);
+ /* Free the old instance instead of looking for an empty spot */
+ if (*oc)
+ clib_mem_free (*oc);
+
+ *oc = ctx_ptr;
+ (*oc)->openssl_ctx_index = oc - om->ctx_pool[thread_index];
+ (*oc)->ctx.c_thread_index = thread_index;
+
+ sh = (*oc)->ctx.tls_session_handle;
+ BIO_set_data ((*oc)->rbio, uword_to_pointer (sh, void *));
+ BIO_set_data ((*oc)->wbio, uword_to_pointer (sh, void *));
+
+ return ((*oc)->openssl_ctx_index);
+}
+
tls_ctx_t *
openssl_ctx_get (u32 ctx_index)
{
@@ -310,9 +350,9 @@ openssl_confirm_app_close (tls_ctx_t * ctx)
session_transport_closed_notify (&ctx->connection);
}
-static inline int
-openssl_ctx_write (tls_ctx_t * ctx, session_t * app_session,
- transport_send_params_t * sp)
+static int
+openssl_ctx_write_tls (tls_ctx_t *ctx, session_t *app_session,
+ transport_send_params_t *sp)
{
openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
u32 deq_max, space, enq_buf;
@@ -358,8 +398,77 @@ check_tls_fifo:
return wrote;
}
+static int
+openssl_ctx_write_dtls (tls_ctx_t *ctx, session_t *app_session,
+ transport_send_params_t *sp)
+{
+ openssl_main_t *om = &openssl_main;
+ openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
+ session_dgram_pre_hdr_t hdr;
+ session_t *us;
+ int wrote, rv;
+ u32 read = 0, to_deq, dgram_sz;
+ u8 *buf;
+
+ us = session_get_from_handle (ctx->tls_session_handle);
+ to_deq = svm_fifo_max_dequeue_cons (app_session->tx_fifo);
+ buf = om->tx_bufs[ctx->c_thread_index];
+
+ while (to_deq > 0)
+ {
+ /* Peeking only pre-header dgram because the session is connected */
+ rv = svm_fifo_peek (app_session->tx_fifo, 0, sizeof (hdr), (u8 *) &hdr);
+ ASSERT (rv == sizeof (hdr) && hdr.data_length < vec_len (buf));
+ ASSERT (to_deq >= hdr.data_length + SESSION_CONN_HDR_LEN);
+
+ dgram_sz = hdr.data_length + SESSION_CONN_HDR_LEN;
+ if (svm_fifo_max_enqueue_prod (us->tx_fifo) < dgram_sz + TLSO_CTRL_BYTES)
+ {
+ svm_fifo_add_want_deq_ntf (us->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
+ transport_connection_deschedule (&ctx->connection);
+ sp->flags |= TRANSPORT_SND_F_DESCHED;
+ goto done;
+ }
+
+ rv = svm_fifo_peek (app_session->tx_fifo, SESSION_CONN_HDR_LEN,
+ hdr.data_length, buf);
+ ASSERT (rv == hdr.data_length);
+ svm_fifo_dequeue_drop (app_session->tx_fifo, dgram_sz);
+
+ wrote = SSL_write (oc->ssl, buf, rv);
+ ASSERT (wrote > 0);
+
+ read += rv;
+ to_deq -= dgram_sz;
+ }
+
+done:
+
+ if (svm_fifo_needs_deq_ntf (app_session->tx_fifo, read))
+ session_dequeue_notify (app_session);
+
+ if (read)
+ tls_add_vpp_q_tx_evt (us);
+
+ if (PREDICT_FALSE (ctx->app_closed &&
+ !svm_fifo_max_enqueue_prod (us->rx_fifo)))
+ openssl_confirm_app_close (ctx);
+
+ return read;
+}
+
+static inline int
+openssl_ctx_write (tls_ctx_t *ctx, session_t *app_session,
+ transport_send_params_t *sp)
+{
+ if (ctx->tls_type == TRANSPORT_PROTO_TLS)
+ return openssl_ctx_write_tls (ctx, app_session, sp);
+ else
+ return openssl_ctx_write_dtls (ctx, app_session, sp);
+}
+
static inline int
-openssl_ctx_read (tls_ctx_t * ctx, session_t * tls_session)
+openssl_ctx_read_tls (tls_ctx_t *ctx, session_t *tls_session)
{
openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
session_t *app_session;
@@ -387,6 +496,76 @@ openssl_ctx_read (tls_ctx_t * ctx, session_t * tls_session)
return wrote;
}
+static inline int
+openssl_ctx_read_dtls (tls_ctx_t *ctx, session_t *us)
+{
+ openssl_main_t *om = &openssl_main;
+ openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
+ session_dgram_hdr_t hdr;
+ session_t *app_session;
+ u32 wrote = 0;
+ int read, rv;
+ u8 *buf;
+
+ if (PREDICT_FALSE (SSL_in_init (oc->ssl)))
+ {
+ u32 us_index = us->session_index;
+ if (openssl_ctx_handshake_rx (ctx, us) < 0)
+ return 0;
+ /* Session pool might grow when allocating the app's session */
+ us = session_get (us_index, ctx->c_thread_index);
+ }
+
+ buf = om->rx_bufs[ctx->c_thread_index];
+ app_session = session_get_from_handle (ctx->app_session_handle);
+ svm_fifo_fill_chunk_list (app_session->rx_fifo);
+
+ while (svm_fifo_max_dequeue_cons (us->rx_fifo) > 0)
+ {
+ if (svm_fifo_max_enqueue_prod (app_session->rx_fifo) < DTLSO_MAX_DGRAM)
+ {
+ tls_add_vpp_q_builtin_rx_evt (us);
+ goto done;
+ }
+
+ read = SSL_read (oc->ssl, buf, vec_len (buf));
+ if (PREDICT_FALSE (read <= 0))
+ {
+ if (read < 0)
+ tls_add_vpp_q_builtin_rx_evt (us);
+ goto done;
+ }
+ wrote += read;
+
+ hdr.data_length = read;
+ hdr.data_offset = 0;
+
+ svm_fifo_seg_t segs[2] = { { (u8 *) &hdr, sizeof (hdr) },
+ { buf, read } };
+
+ rv = svm_fifo_enqueue_segments (app_session->rx_fifo, segs, 2,
+ 0 /* allow partial */);
+ ASSERT (rv > 0);
+ }
+
+done:
+
+ /* If handshake just completed, session may still be in accepting state */
+ if (app_session->session_state >= SESSION_STATE_READY)
+ tls_notify_app_enqueue (ctx, app_session);
+
+ return wrote;
+}
+
+static inline int
+openssl_ctx_read (tls_ctx_t *ctx, session_t *ts)
+{
+ if (ctx->tls_type == TRANSPORT_PROTO_TLS)
+ return openssl_ctx_read_tls (ctx, ts);
+ else
+ return openssl_ctx_read_dtls (ctx, ts);
+}
+
static int
openssl_ctx_init_client (tls_ctx_t * ctx)
{
@@ -396,10 +575,11 @@ openssl_ctx_init_client (tls_ctx_t * ctx)
const SSL_METHOD *method;
int rv, err;
- method = SSLv23_client_method ();
+ method = ctx->tls_type == TRANSPORT_PROTO_TLS ? SSLv23_client_method () :
+ DTLS_client_method ();
if (method == NULL)
{
- TLS_DBG (1, "SSLv23_method returned null");
+ TLS_DBG (1, "(D)TLS_method returned null");
return -1;
}
@@ -433,8 +613,16 @@ openssl_ctx_init_client (tls_ctx_t * ctx)
return -1;
}
- oc->rbio = BIO_new_tls (ctx->tls_session_handle);
- oc->wbio = BIO_new_tls (ctx->tls_session_handle);
+ if (ctx->tls_type == TRANSPORT_PROTO_TLS)
+ {
+ oc->rbio = BIO_new_tls (ctx->tls_session_handle);
+ oc->wbio = BIO_new_tls (ctx->tls_session_handle);
+ }
+ else
+ {
+ oc->rbio = BIO_new_dtls (ctx->tls_session_handle);
+ oc->wbio = BIO_new_dtls (ctx->tls_session_handle);
+ }
SSL_set_bio (oc->ssl, oc->wbio, oc->rbio);
SSL_set_connect_state (oc->ssl);
@@ -504,7 +692,8 @@ openssl_start_listen (tls_ctx_t * lctx)
return -1;
}
- method = SSLv23_method ();
+ method = lctx->tls_type == TRANSPORT_PROTO_TLS ? SSLv23_server_method () :
+ DTLS_server_method ();
ssl_ctx = SSL_CTX_new (method);
if (!ssl_ctx)
{
@@ -604,8 +793,16 @@ openssl_ctx_init_server (tls_ctx_t * ctx)
return -1;
}
- oc->rbio = BIO_new_tls (ctx->tls_session_handle);
- oc->wbio = BIO_new_tls (ctx->tls_session_handle);
+ if (ctx->tls_type == TRANSPORT_PROTO_TLS)
+ {
+ oc->rbio = BIO_new_tls (ctx->tls_session_handle);
+ oc->wbio = BIO_new_tls (ctx->tls_session_handle);
+ }
+ else
+ {
+ oc->rbio = BIO_new_dtls (ctx->tls_session_handle);
+ oc->wbio = BIO_new_dtls (ctx->tls_session_handle);
+ }
SSL_set_bio (oc->ssl, oc->wbio, oc->rbio);
SSL_set_accept_state (oc->ssl);
@@ -682,7 +879,10 @@ openssl_app_close (tls_ctx_t * ctx)
const static tls_engine_vft_t openssl_engine = {
.ctx_alloc = openssl_ctx_alloc,
+ .ctx_alloc_w_thread = openssl_ctx_alloc_w_thread,
.ctx_free = openssl_ctx_free,
+ .ctx_attach = openssl_ctx_attach,
+ .ctx_detach = openssl_ctx_detach,
.ctx_get = openssl_ctx_get,
.ctx_get_w_thread = openssl_ctx_get_w_thread,
.ctx_init_server = openssl_ctx_init_server,
@@ -771,7 +971,7 @@ tls_openssl_init (vlib_main_t * vm)
vlib_thread_main_t *vtm = vlib_get_thread_main ();
openssl_main_t *om = &openssl_main;
clib_error_t *error = 0;
- u32 num_threads;
+ u32 num_threads, i;
error = tls_openssl_api_init (vm);
num_threads = 1 /* main thread */ + vtm->n_threads;
@@ -786,7 +986,13 @@ tls_openssl_init (vlib_main_t * vm)
}
vec_validate (om->ctx_pool, num_threads - 1);
-
+ vec_validate (om->rx_bufs, num_threads - 1);
+ vec_validate (om->tx_bufs, num_threads - 1);
+ for (i = 0; i < num_threads; i++)
+ {
+ vec_validate (om->rx_bufs[i], DTLSO_MAX_DGRAM);
+ vec_validate (om->tx_bufs[i], DTLSO_MAX_DGRAM);
+ }
tls_register_engine (&openssl_engine, CRYPTO_ENGINE_OPENSSL);
om->engine_init = 0;
diff --git a/src/plugins/tlsopenssl/tls_openssl.h b/src/plugins/tlsopenssl/tls_openssl.h
index 16b256d0ceb..a4beecc8ec1 100644
--- a/src/plugins/tlsopenssl/tls_openssl.h
+++ b/src/plugins/tlsopenssl/tls_openssl.h
@@ -13,6 +13,9 @@
* limitations under the License.
*/
+#ifndef SRC_PLUGINS_TLSOPENSSL_TLS_OPENSSL_H_
+#define SRC_PLUGINS_TLSOPENSSL_TLS_OPENSSL_H_
+
#include <openssl/ssl.h>
#include <openssl/conf.h>
#include <openssl/err.h>
@@ -24,6 +27,8 @@
#define TLSO_CTRL_BYTES 1000
#define TLSO_MIN_ENQ_SPACE (1 << 16)
+#define DTLSO_MAX_DGRAM 2000
+
typedef struct tls_ctx_openssl_
{
tls_ctx_t ctx; /**< First */
@@ -48,6 +53,9 @@ typedef struct openssl_main_
openssl_ctx_t ***ctx_pool;
openssl_listen_ctx_t *lctx_pool;
+ u8 **rx_bufs;
+ u8 **tx_bufs;
+
/* API message ID base */
u16 msg_id_base;
@@ -73,6 +81,8 @@ clib_error_t *tls_openssl_api_init (vlib_main_t * vm);
int tls_openssl_set_ciphers (char *ciphers);
int vpp_openssl_is_inflight (tls_ctx_t * ctx);
+#endif /* SRC_PLUGINS_TLSOPENSSL_TLS_OPENSSL_H_ */
+
/*
* fd.io coding-style-patch-verification: ON
*