16 #include <openssl/ssl.h> 17 #include <openssl/conf.h> 18 #include <openssl/err.h> 19 #ifdef HAVE_OPENSSL_ASYNC 20 #include <openssl/async.h> 24 #include <vpp/app/version.h> 29 #define MAX_CRYPTO_LEN 16 44 (*ctx)->ctx.c_thread_index = thread_index;
47 (*ctx)->openssl_ctx_index = ctx - tm->
ctx_pool[thread_index];
48 return ((*ctx)->openssl_ctx_index);
57 SSL_shutdown (oc->
ssl);
111 u32 deq_max, deq_now;
142 u32 enq_max, deq_now;
146 if (BIO_ctrl_pending (oc->
rbio) <= 0)
176 #ifdef HAVE_OPENSSL_ASYNC 178 vpp_ssl_async_process_event (
tls_ctx_t * ctx,
187 SSL_set_async_callback_arg (oc->
ssl, (
void *) engine_cb->
arg);
217 ctx->no_app_session = 1;
235 #ifdef HAVE_OPENSSL_ASYNC 240 while (SSL_in_init (oc->
ssl))
251 #ifdef HAVE_OPENSSL_ASYNC 253 vpp_ssl_async_process_event (ctx, myself);
256 rv = SSL_do_handshake (oc->
ssl);
257 err = SSL_get_error (oc->
ssl, rv);
259 if (err == SSL_ERROR_SSL)
262 ERR_error_string (ERR_get_error (), buf);
270 #ifdef HAVE_OPENSSL_ASYNC 271 if (err == SSL_ERROR_WANT_ASYNC)
273 SSL_get_async_status (oc->
ssl, &estatus);
275 if (estatus == ASYNC_STATUS_EAGAIN)
277 vpp_ssl_async_retry_func (ctx, myself);
282 if (err != SSL_ERROR_WANT_WRITE)
286 SSL_state_string_long (oc->
ssl));
288 if (SSL_in_init (oc->
ssl))
294 if (!SSL_is_server (oc->
ssl))
299 if ((rv = SSL_get_verify_result (oc->
ssl)) != X509_V_OK)
301 TLS_DBG (1,
" failed verify: %s\n",
302 X509_verify_cert_error_string (rv));
320 TLS_DBG (1,
"Handshake for %u complete. TLS cipher is %s",
336 int wrote = 0, rv, read, max_buf = 100 *
TLS_CHUNK_SIZE, max_space;
337 u32 enq_max, deq_max, deq_now, to_write;
346 max_space = max_buf - BIO_ctrl_pending (oc->
rbio);
347 max_space = (max_space < 0) ? 0 : max_space;
376 if (BIO_ctrl_pending (oc->
rbio) <= 0)
399 if (read < enq_max && BIO_ctrl_pending (oc->
rbio) > 0)
407 if (BIO_ctrl_pending (oc->
rbio) > 0)
418 int read, wrote = 0, max_space, max_buf = 100 *
TLS_CHUNK_SIZE, rv;
420 u32 deq_max, enq_max, deq_now, to_read;
434 max_space = max_buf - BIO_ctrl_pending (oc->
wbio);
435 max_space = max_space < 0 ? 0 : max_space;
436 deq_now =
clib_min (deq_max, max_space);
463 if (BIO_ctrl_pending (oc->
wbio) <= 0)
483 if (read < enq_max && SSL_pending (oc->
ssl) > 0)
494 if (SSL_pending (oc->
ssl) > 0)
503 long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
507 const SSL_METHOD *method;
509 #ifdef HAVE_OPENSSL_ASYNC 513 method = SSLv23_client_method ();
516 TLS_DBG (1,
"SSLv23_method returned null");
520 oc->
ssl_ctx = SSL_CTX_new (method);
523 TLS_DBG (1,
"SSL_CTX_new returned null");
527 SSL_CTX_set_ecdh_auto (oc->
ssl_ctx, 1);
528 SSL_CTX_set_mode (oc->
ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
529 #ifdef HAVE_OPENSSL_ASYNC 531 SSL_CTX_set_mode (oc->
ssl_ctx, SSL_MODE_ASYNC);
533 rv = SSL_CTX_set_cipher_list (oc->
ssl_ctx, (
const char *) om->
ciphers);
536 TLS_DBG (1,
"Couldn't set cipher");
540 SSL_CTX_set_options (oc->
ssl_ctx, flags);
546 TLS_DBG (1,
"Couldn't initialize ssl struct");
550 oc->
rbio = BIO_new (BIO_s_mem ());
551 oc->
wbio = BIO_new (BIO_s_mem ());
553 BIO_set_mem_eof_return (oc->
rbio, -1);
554 BIO_set_mem_eof_return (oc->
wbio, -1);
557 SSL_set_connect_state (oc->
ssl);
562 TLS_DBG (1,
"Couldn't set hostname");
569 TLS_DBG (1,
"Initiating handshake for [%u]%u", ctx->c_thread_index,
575 rv = SSL_do_handshake (oc->
ssl);
576 err = SSL_get_error (oc->
ssl, rv);
578 #ifdef HAVE_OPENSSL_ASYNC 579 if (err == SSL_ERROR_WANT_ASYNC)
582 vpp_ssl_async_process_event (ctx, handler);
586 if (err != SSL_ERROR_WANT_WRITE)
590 TLS_DBG (2,
"tls state for [%u]%u is su", ctx->c_thread_index,
599 const SSL_METHOD *method;
609 long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
619 TLS_DBG (1,
"tls cert and/or key not configured %d",
620 lctx->parent_app_wrk_index);
624 method = SSLv23_method ();
625 ssl_ctx = SSL_CTX_new (method);
632 SSL_CTX_set_mode (ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
633 #ifdef HAVE_OPENSSL_ASYNC 635 SSL_CTX_set_mode (ssl_ctx, SSL_MODE_ASYNC);
638 SSL_CTX_set_options (ssl_ctx, flags);
639 SSL_CTX_set_ecdh_auto (ssl_ctx, 1);
641 rv = SSL_CTX_set_cipher_list (ssl_ctx, (
const char *) om->
ciphers);
644 TLS_DBG (1,
"Couldn't set cipher");
651 cert_bio = BIO_new (BIO_s_mem ());
653 srvcert = PEM_read_bio_X509 (cert_bio, NULL, NULL, NULL);
659 SSL_CTX_use_certificate (ssl_ctx, srvcert);
662 cert_bio = BIO_new (BIO_s_mem ());
664 pkey = PEM_read_bio_PrivateKey (cert_bio, NULL, NULL, NULL);
670 SSL_CTX_use_PrivateKey (ssl_ctx, pkey);
680 lctx->tls_ssl_ctx = olc_index;
692 olc_index = lctx->tls_ssl_ctx;
696 EVP_PKEY_free (olc->
pkey);
708 u32 olc_index = ctx->tls_ssl_ctx;
712 #ifdef HAVE_OPENSSL_ASYNC 722 TLS_DBG (1,
"Couldn't initialize ssl struct");
726 oc->
rbio = BIO_new (BIO_s_mem ());
727 oc->
wbio = BIO_new (BIO_s_mem ());
729 BIO_set_mem_eof_return (oc->
rbio, -1);
730 BIO_set_mem_eof_return (oc->
wbio, -1);
733 SSL_set_accept_state (oc->
ssl);
735 TLS_DBG (1,
"Initiating handshake for [%u]%u", ctx->c_thread_index,
741 rv = SSL_do_handshake (oc->
ssl);
742 err = SSL_get_error (oc->
ssl, rv);
744 #ifdef HAVE_OPENSSL_ASYNC 745 if (err == SSL_ERROR_WANT_ASYNC)
748 vpp_ssl_async_process_event (ctx, handler);
752 if (err != SSL_ERROR_WANT_WRITE)
756 TLS_DBG (2,
"tls state for [%u]%u is su", ctx->c_thread_index,
767 return SSL_is_init_finished (mc->
ssl);
790 if (BIO_ctrl_pending (oc->
rbio) <= 0
825 clib_warning (
"Could not initialize TLS CA certificates");
835 #if OPENSSL_VERSION_NUMBER >= 0x30000000L 848 cert_bio = BIO_new (BIO_s_mem ());
850 testcert = PEM_read_bio_X509 (cert_bio, NULL, NULL, NULL);
856 X509_STORE_add_cert (om->
cert_store, testcert);
859 return (rv < 0 ? -1 : 0);
876 om->
ciphers[
i] = toupper (ciphers[i]);
893 SSL_load_error_strings ();
909 (
"ALL:!ADH:!LOW:!EXP:!MD5:!RC4-SHA:!DES-CBC3-SHA:@STRENGTH");
920 #ifdef HAVE_OPENSSL_ASYNC 926 char *engine_name = NULL;
927 char *engine_alg = NULL;
928 char *ciphers = NULL;
929 u8 engine_name_set = 0;
937 (0,
"engine has started, and no config is accepted");
942 if (
unformat (input,
"engine %s", &engine_name))
951 else if (
unformat (input,
"alg %s", &engine_alg))
954 engine_alg[i] = toupper (engine_alg[i]);
956 else if (
unformat (input,
"ciphers %s", &ciphers))
966 if (!engine_name_set)
986 .path =
"tls openssl set",
987 .short_help =
"tls openssl set [engine <engine name>] [alg [algorithm] [async]",
988 .function = tls_openssl_set_command_fn,
995 .version = VPP_BUILD_VER,
996 .description =
"Transport Layer Security (TLS) Engine, OpenSSL Based",
tls_main_t * vnet_tls_get_main(void)
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
openssl_listen_ctx_t * lctx_pool
int(* callback)(SSL *ssl, void *arg)
static int openssl_transport_close(tls_ctx_t *ctx)
static int tls_openssl_set_ciphers(char *ciphers)
static u8 openssl_handshake_is_over(tls_ctx_t *ctx)
static u32 svm_fifo_max_enqueue_prod(svm_fifo_t *f)
Maximum number of bytes that can be enqueued into fifo.
void tls_disconnect_transport(tls_ctx_t *ctx)
static void openssl_handle_handshake_failure(tls_ctx_t *ctx)
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
svm_fifo_t * rx_fifo
Pointers to rx/tx buffers.
void openssl_async_node_enable_disable(u8 is_en)
int tls_add_vpp_q_builtin_rx_evt(session_t *s)
static u32 openssl_ctx_alloc(void)
static void openssl_listen_ctx_free(openssl_listen_ctx_t *lctx)
int tls_async_openssl_callback(SSL *s, void *evt)
void svm_fifo_enqueue_nocopy(svm_fifo_t *f, u32 len)
Advance tail.
static void openssl_ctx_free(tls_ctx_t *ctx)
static session_t * session_get(u32 si, u32 thread_index)
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
static u8 * svm_fifo_tail(svm_fifo_t *f)
struct _svm_fifo svm_fifo_t
void session_transport_closing_notify(transport_connection_t *tc)
Notification from transport that connection is being closed.
#define VLIB_INIT_FUNCTION(x)
static u32 svm_fifo_max_dequeue_cons(svm_fifo_t *f)
Fifo max bytes to dequeue optimized for consumer.
int openssl_engine_register(char *engine_name, char *algorithm)
#define clib_error_return(e, args...)
static int openssl_stop_listen(tls_ctx_t *lctx)
static u32 svm_fifo_max_write_chunk(svm_fifo_t *f)
Max contiguous chunk of data that can be written.
#define SESSION_INVALID_HANDLE
static u8 * svm_fifo_head(svm_fifo_t *f)
int tls_add_vpp_q_builtin_tx_evt(session_t *s)
static u32 svm_fifo_max_read_chunk(svm_fifo_t *f)
Max contiguous chunk of data that can be read.
int tls_init_ca_chain(void)
int session_dequeue_notify(session_t *s)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
int vpp_add_async_run_event(tls_ctx_t *ctx, openssl_resume_handler *handler)
static session_t * session_get_from_handle(session_handle_t handle)
int tls_notify_app_connected(tls_ctx_t *ctx, u8 is_failed)
tls_ctx_t * openssl_ctx_get(u32 ctx_index)
#define SESSION_INVALID_INDEX
static const char test_srv_crt_rsa[]
static int openssl_ctx_init_client(tls_ctx_t *ctx)
static u8 svm_fifo_needs_deq_ntf(svm_fifo_t *f, u32 n_last_deq)
Check if fifo needs dequeue notification.
static_always_inline uword vlib_get_thread_index(void)
static int openssl_app_close(tls_ctx_t *ctx)
#define vec_free(V)
Free vector's memory (no header).
void session_free(session_t *s)
openssl_ctx_t *** ctx_pool
#define clib_warning(format, args...)
static int openssl_ctx_write(tls_ctx_t *ctx, session_t *app_session)
static int openssl_try_handshake_write(openssl_ctx_t *oc, session_t *tls_session)
static int openssl_ctx_read(tls_ctx_t *ctx, session_t *tls_session)
static openssl_main_t openssl_main
int tls_add_vpp_q_tx_evt(session_t *s)
transport_connection_t connection
void tls_register_engine(const tls_engine_vft_t *vft, tls_engine_type_t type)
application_t * application_get(u32 app_index)
#define VLIB_CLI_COMMAND(x,...)
#define pool_put_index(p, i)
Free pool element with given index.
static void openssl_confirm_app_close(tls_ctx_t *ctx)
int openssl_resume_handler(tls_ctx_t *ctx, session_t *tls_session)
static clib_error_t * tls_openssl_init(vlib_main_t *vm)
u8 * tls_key
PEM encoded key.
static void * clib_mem_alloc(uword size)
tls_ctx_t * openssl_ctx_get_w_thread(u32 ctx_index, u8 thread_index)
static int openssl_ctx_init_server(tls_ctx_t *ctx)
app_worker_t * app_worker_get(u32 wrk_index)
openssl_tls_callback_t * vpp_add_async_pending_event(tls_ctx_t *ctx, openssl_resume_handler *handler)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
volatile u8 session_state
State in session layer state machine.
void session_transport_closed_notify(transport_connection_t *tc)
Notification from transport that it is closed.
static int openssl_start_listen(tls_ctx_t *lctx)
int openssl_ctx_handshake_rx(tls_ctx_t *ctx, session_t *tls_session)
int tls_notify_app_accept(tls_ctx_t *ctx)
u32 app_index
Index of owning app.
void tls_notify_app_enqueue(tls_ctx_t *ctx, session_t *app_session)
static int openssl_try_handshake_read(openssl_ctx_t *oc, session_t *tls_session)
static vlib_thread_main_t * vlib_get_thread_main()
u8 * tls_cert
Certificate to be used for listen sessions.
openssl_listen_ctx_t * openssl_lctx_get(u32 lctx_index)
static u32 openssl_listen_ctx_alloc(void)
int svm_fifo_dequeue_drop(svm_fifo_t *f, u32 len)
Dequeue and drop bytes from fifo.
#define TLS_DBG(_lvl, _fmt, _args...)
static const u32 test_srv_crt_rsa_len