tls: dtls initial implementation

Type: feature

Basic dtls transport protocol implementation that relies on openssl
wire protocol implementation. Retries/timeouts not yet supported.

To test using vcl test apps, first ensure all arp entries are properly
resolved and subsequently:

server: vcl_server -p dtls 1234
client: vcl_client -p dtls <server-ip> 1234 -U -N 2000000 -T 1460 -X

Signed-off-by: Florin Coras <fcoras@cisco.com>
Change-Id: I04b4516a8fe9ce85ba230bcdd891f33a900046ed
This commit is contained in:
Florin Coras
2020-11-19 13:38:26 -08:00
committed by Dave Barach
parent da2305fb87
commit 4b47ee26cb
15 changed files with 749 additions and 75 deletions

View File

@@ -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

View File

@@ -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:
*/

View File

@@ -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_ */

File diff suppressed because it is too large Load Diff

View File

@@ -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
*