2 * Copyright (c) 2018 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
23 #include <vnet/plugin/plugin.h>
24 #include <vpp/app/version.h>
25 #include <vnet/tls/tls.h>
27 #include <tlsopenssl/tls_openssl.h>
29 #define MAX_CRYPTO_LEN 16
31 static openssl_main_t openssl_main;
33 openssl_ctx_alloc (void)
35 u8 thread_index = vlib_get_thread_index ();
36 openssl_main_t *tm = &openssl_main;
39 pool_get (tm->ctx_pool[thread_index], ctx);
41 *ctx = clib_mem_alloc (sizeof (openssl_ctx_t));
43 clib_memset (*ctx, 0, sizeof (openssl_ctx_t));
44 (*ctx)->ctx.c_thread_index = thread_index;
45 (*ctx)->ctx.tls_ctx_engine = TLS_ENGINE_OPENSSL;
46 (*ctx)->ctx.app_session_handle = SESSION_INVALID_HANDLE;
47 (*ctx)->openssl_ctx_index = ctx - tm->ctx_pool[thread_index];
48 return ((*ctx)->openssl_ctx_index);
52 openssl_ctx_free (tls_ctx_t * ctx)
54 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
56 if (SSL_is_init_finished (oc->ssl) && !ctx->is_passive_close)
57 SSL_shutdown (oc->ssl);
61 vec_free (ctx->srv_hostname);
62 pool_put_index (openssl_main.ctx_pool[ctx->c_thread_index],
63 oc->openssl_ctx_index);
67 openssl_ctx_get (u32 ctx_index)
70 ctx = pool_elt_at_index (openssl_main.ctx_pool[vlib_get_thread_index ()],
76 openssl_ctx_get_w_thread (u32 ctx_index, u8 thread_index)
79 ctx = pool_elt_at_index (openssl_main.ctx_pool[thread_index], ctx_index);
84 openssl_listen_ctx_alloc (void)
86 openssl_main_t *om = &openssl_main;
87 openssl_listen_ctx_t *lctx;
89 pool_get (om->lctx_pool, lctx);
91 clib_memset (lctx, 0, sizeof (openssl_listen_ctx_t));
92 lctx->openssl_lctx_index = lctx - om->lctx_pool;
93 return lctx->openssl_lctx_index;
97 openssl_listen_ctx_free (openssl_listen_ctx_t * lctx)
99 pool_put_index (openssl_main.lctx_pool, lctx->openssl_lctx_index);
102 openssl_listen_ctx_t *
103 openssl_lctx_get (u32 lctx_index)
105 return pool_elt_at_index (openssl_main.lctx_pool, lctx_index);
109 openssl_try_handshake_read (openssl_ctx_t * oc, session_t * tls_session)
111 u32 deq_max, deq_now;
115 f = tls_session->rx_fifo;
116 deq_max = svm_fifo_max_dequeue_cons (f);
120 deq_now = clib_min (svm_fifo_max_read_chunk (f), deq_max);
121 wrote = BIO_write (oc->wbio, svm_fifo_head (f), deq_now);
125 svm_fifo_dequeue_drop (f, wrote);
128 deq_now = clib_min (svm_fifo_max_read_chunk (f), deq_max - wrote);
129 rv = BIO_write (oc->wbio, svm_fifo_head (f), deq_now);
132 svm_fifo_dequeue_drop (f, rv);
140 openssl_try_handshake_write (openssl_ctx_t * oc, session_t * tls_session)
142 u32 enq_max, deq_now;
146 if (BIO_ctrl_pending (oc->rbio) <= 0)
149 f = tls_session->tx_fifo;
150 enq_max = svm_fifo_max_enqueue_prod (f);
154 deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max);
155 read = BIO_read (oc->rbio, svm_fifo_tail (f), deq_now);
159 svm_fifo_enqueue_nocopy (f, read);
160 tls_add_vpp_q_tx_evt (tls_session);
164 deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max - read);
165 rv = BIO_read (oc->rbio, svm_fifo_tail (f), deq_now);
168 svm_fifo_enqueue_nocopy (f, rv);
176 #ifdef HAVE_OPENSSL_ASYNC
178 vpp_ssl_async_process_event (tls_ctx_t * ctx,
179 openssl_resume_handler * handler)
181 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
182 openssl_tls_callback_t *engine_cb;
184 engine_cb = vpp_add_async_pending_event (ctx, handler);
187 SSL_set_async_callback_arg (oc->ssl, (void *) engine_cb->arg);
188 TLS_DBG (2, "set callback to engine %p\n", engine_cb->callback);
194 /* Due to engine busy stat, VPP need to retry later */
196 vpp_ssl_async_retry_func (tls_ctx_t * ctx, openssl_resume_handler * handler)
199 if (vpp_add_async_run_event (ctx, handler))
209 openssl_handle_handshake_failure (tls_ctx_t * ctx)
211 if (SSL_is_server (((openssl_ctx_t *) ctx)->ssl))
214 * Cleanup pre-allocated app session and close transport
216 session_free (session_get (ctx->c_s_index, ctx->c_thread_index));
217 ctx->no_app_session = 1;
218 ctx->c_s_index = SESSION_INVALID_INDEX;
219 tls_disconnect_transport (ctx);
224 * Also handles cleanup of the pre-allocated session
226 tls_notify_app_connected (ctx, /* is failed */ 1);
231 openssl_ctx_handshake_rx (tls_ctx_t * ctx, session_t * tls_session)
233 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
235 #ifdef HAVE_OPENSSL_ASYNC
237 openssl_resume_handler *myself;
240 while (SSL_in_init (oc->ssl))
246 else if (!openssl_try_handshake_read (oc, tls_session))
251 #ifdef HAVE_OPENSSL_ASYNC
252 myself = openssl_ctx_handshake_rx;
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);
263 clib_warning ("Err: %s", buf);
265 openssl_handle_handshake_failure (ctx);
269 openssl_try_handshake_write (oc, tls_session);
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)
285 TLS_DBG (2, "tls state for %u is %s", oc->openssl_ctx_index,
286 SSL_state_string_long (oc->ssl));
288 if (SSL_in_init (oc->ssl))
294 if (!SSL_is_server (oc->ssl))
297 * Verify server certificate
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));
305 * Presence of hostname enforces strict certificate verification
307 if (ctx->srv_hostname)
309 tls_notify_app_connected (ctx, /* is failed */ 0);
313 tls_notify_app_connected (ctx, /* is failed */ 0);
317 tls_notify_app_accept (ctx);
320 TLS_DBG (1, "Handshake for %u complete. TLS cipher is %s",
321 oc->openssl_ctx_index, SSL_get_cipher (oc->ssl));
326 openssl_confirm_app_close (tls_ctx_t * ctx)
328 tls_disconnect_transport (ctx);
329 session_transport_closed_notify (&ctx->connection);
333 openssl_ctx_write (tls_ctx_t * ctx, session_t * app_session)
335 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
336 int wrote = 0, rv, read, max_buf = 100 * TLS_CHUNK_SIZE, max_space;
337 u32 enq_max, deq_max, deq_now, to_write;
338 session_t *tls_session;
341 f = app_session->tx_fifo;
342 deq_max = svm_fifo_max_dequeue_cons (f);
346 max_space = max_buf - BIO_ctrl_pending (oc->rbio);
347 max_space = (max_space < 0) ? 0 : max_space;
348 deq_now = clib_min (deq_max, (u32) max_space);
349 to_write = clib_min (svm_fifo_max_read_chunk (f), deq_now);
350 wrote = SSL_write (oc->ssl, svm_fifo_head (f), to_write);
353 tls_add_vpp_q_builtin_tx_evt (app_session);
356 svm_fifo_dequeue_drop (app_session->tx_fifo, wrote);
359 to_write = clib_min (svm_fifo_max_read_chunk (f), deq_now - wrote);
360 rv = SSL_write (oc->ssl, svm_fifo_head (f), to_write);
363 svm_fifo_dequeue_drop (app_session->tx_fifo, rv);
368 if (svm_fifo_needs_deq_ntf (app_session->tx_fifo, wrote))
369 session_dequeue_notify (app_session);
372 tls_add_vpp_q_builtin_tx_evt (app_session);
376 if (BIO_ctrl_pending (oc->rbio) <= 0)
379 tls_session = session_get_from_handle (ctx->tls_session_handle);
380 f = tls_session->tx_fifo;
381 enq_max = svm_fifo_max_enqueue_prod (f);
384 tls_add_vpp_q_builtin_tx_evt (app_session);
388 deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max);
389 read = BIO_read (oc->rbio, svm_fifo_tail (f), deq_now);
392 tls_add_vpp_q_builtin_tx_evt (app_session);
396 svm_fifo_enqueue_nocopy (f, read);
397 tls_add_vpp_q_tx_evt (tls_session);
399 if (read < enq_max && BIO_ctrl_pending (oc->rbio) > 0)
401 deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max - read);
402 read = BIO_read (oc->rbio, svm_fifo_tail (f), deq_now);
404 svm_fifo_enqueue_nocopy (f, read);
407 if (BIO_ctrl_pending (oc->rbio) > 0)
408 tls_add_vpp_q_builtin_tx_evt (app_session);
409 else if (ctx->app_closed)
410 openssl_confirm_app_close (ctx);
416 openssl_ctx_read (tls_ctx_t * ctx, session_t * tls_session)
418 int read, wrote = 0, max_space, max_buf = 100 * TLS_CHUNK_SIZE, rv;
419 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
420 u32 deq_max, enq_max, deq_now, to_read;
421 session_t *app_session;
424 if (PREDICT_FALSE (SSL_in_init (oc->ssl)))
426 openssl_ctx_handshake_rx (ctx, tls_session);
430 f = tls_session->rx_fifo;
431 deq_max = svm_fifo_max_dequeue_cons (f);
432 max_space = max_buf - BIO_ctrl_pending (oc->wbio);
433 max_space = max_space < 0 ? 0 : max_space;
434 deq_now = clib_min (deq_max, max_space);
438 to_read = clib_min (svm_fifo_max_read_chunk (f), deq_now);
439 wrote = BIO_write (oc->wbio, svm_fifo_head (f), to_read);
442 tls_add_vpp_q_builtin_rx_evt (tls_session);
445 svm_fifo_dequeue_drop (f, wrote);
448 to_read = clib_min (svm_fifo_max_read_chunk (f), deq_now - wrote);
449 rv = BIO_write (oc->wbio, svm_fifo_head (f), to_read);
452 svm_fifo_dequeue_drop (f, rv);
456 if (svm_fifo_max_dequeue_cons (f))
457 tls_add_vpp_q_builtin_rx_evt (tls_session);
461 if (BIO_ctrl_pending (oc->wbio) <= 0)
464 app_session = session_get_from_handle (ctx->app_session_handle);
465 f = app_session->rx_fifo;
466 enq_max = svm_fifo_max_enqueue_prod (f);
469 tls_add_vpp_q_builtin_rx_evt (tls_session);
473 deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max);
474 read = SSL_read (oc->ssl, svm_fifo_tail (f), deq_now);
477 tls_add_vpp_q_builtin_rx_evt (tls_session);
480 svm_fifo_enqueue_nocopy (f, read);
481 if (read < enq_max && BIO_ctrl_pending (oc->wbio) > 0)
483 deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max - read);
484 read = SSL_read (oc->ssl, svm_fifo_tail (f), deq_now);
486 svm_fifo_enqueue_nocopy (f, read);
489 tls_notify_app_enqueue (ctx, app_session);
490 if (BIO_ctrl_pending (oc->wbio) > 0)
491 tls_add_vpp_q_builtin_rx_evt (tls_session);
497 openssl_ctx_init_client (tls_ctx_t * ctx)
499 long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
500 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
501 openssl_main_t *om = &openssl_main;
502 session_t *tls_session;
503 const SSL_METHOD *method;
505 #ifdef HAVE_OPENSSL_ASYNC
506 openssl_resume_handler *handler;
509 method = SSLv23_client_method ();
512 TLS_DBG (1, "SSLv23_method returned null");
516 oc->ssl_ctx = SSL_CTX_new (method);
517 if (oc->ssl_ctx == NULL)
519 TLS_DBG (1, "SSL_CTX_new returned null");
523 SSL_CTX_set_ecdh_auto (oc->ssl_ctx, 1);
524 SSL_CTX_set_mode (oc->ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
525 #ifdef HAVE_OPENSSL_ASYNC
527 SSL_CTX_set_mode (oc->ssl_ctx, SSL_MODE_ASYNC);
529 rv = SSL_CTX_set_cipher_list (oc->ssl_ctx, (const char *) om->ciphers);
532 TLS_DBG (1, "Couldn't set cipher");
536 SSL_CTX_set_options (oc->ssl_ctx, flags);
537 SSL_CTX_set_cert_store (oc->ssl_ctx, om->cert_store);
539 oc->ssl = SSL_new (oc->ssl_ctx);
542 TLS_DBG (1, "Couldn't initialize ssl struct");
546 oc->rbio = BIO_new (BIO_s_mem ());
547 oc->wbio = BIO_new (BIO_s_mem ());
549 BIO_set_mem_eof_return (oc->rbio, -1);
550 BIO_set_mem_eof_return (oc->wbio, -1);
552 SSL_set_bio (oc->ssl, oc->wbio, oc->rbio);
553 SSL_set_connect_state (oc->ssl);
555 rv = SSL_set_tlsext_host_name (oc->ssl, ctx->srv_hostname);
558 TLS_DBG (1, "Couldn't set hostname");
563 * 2. Do the first steps in the handshake.
565 TLS_DBG (1, "Initiating handshake for [%u]%u", ctx->c_thread_index,
566 oc->openssl_ctx_index);
568 tls_session = session_get_from_handle (ctx->tls_session_handle);
571 rv = SSL_do_handshake (oc->ssl);
572 err = SSL_get_error (oc->ssl, rv);
573 openssl_try_handshake_write (oc, tls_session);
574 #ifdef HAVE_OPENSSL_ASYNC
575 if (err == SSL_ERROR_WANT_ASYNC)
577 handler = (openssl_resume_handler *) openssl_ctx_handshake_rx;
578 vpp_ssl_async_process_event (ctx, handler);
582 if (err != SSL_ERROR_WANT_WRITE)
586 TLS_DBG (2, "tls state for [%u]%u is su", ctx->c_thread_index,
587 oc->openssl_ctx_index, SSL_state_string_long (oc->ssl));
592 openssl_start_listen (tls_ctx_t * lctx)
595 const SSL_METHOD *method;
602 openssl_listen_ctx_t *olc;
603 app_worker_t *app_wrk;
605 long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
606 openssl_main_t *om = &openssl_main;
608 app_wrk = app_worker_get (lctx->parent_app_wrk_index);
612 app = application_get (app_wrk->app_index);
613 if (!app->tls_cert || !app->tls_key)
615 TLS_DBG (1, "tls cert and/or key not configured %d",
616 lctx->parent_app_wrk_index);
620 method = SSLv23_method ();
621 ssl_ctx = SSL_CTX_new (method);
624 clib_warning ("Unable to create SSL context");
628 SSL_CTX_set_mode (ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
629 #ifdef HAVE_OPENSSL_ASYNC
631 SSL_CTX_set_mode (ssl_ctx, SSL_MODE_ASYNC);
632 SSL_CTX_set_async_callback (ssl_ctx, tls_async_openssl_callback);
634 SSL_CTX_set_options (ssl_ctx, flags);
635 SSL_CTX_set_ecdh_auto (ssl_ctx, 1);
637 rv = SSL_CTX_set_cipher_list (ssl_ctx, (const char *) om->ciphers);
640 TLS_DBG (1, "Couldn't set cipher");
645 * Set the key and cert
647 cert_bio = BIO_new (BIO_s_mem ());
648 BIO_write (cert_bio, app->tls_cert, vec_len (app->tls_cert));
649 srvcert = PEM_read_bio_X509 (cert_bio, NULL, NULL, NULL);
652 clib_warning ("unable to parse certificate");
655 SSL_CTX_use_certificate (ssl_ctx, srvcert);
658 cert_bio = BIO_new (BIO_s_mem ());
659 BIO_write (cert_bio, app->tls_key, vec_len (app->tls_key));
660 pkey = PEM_read_bio_PrivateKey (cert_bio, NULL, NULL, NULL);
663 clib_warning ("unable to parse pkey");
666 SSL_CTX_use_PrivateKey (ssl_ctx, pkey);
669 olc_index = openssl_listen_ctx_alloc ();
670 olc = openssl_lctx_get (olc_index);
671 olc->ssl_ctx = ssl_ctx;
672 olc->srvcert = srvcert;
675 /* store SSL_CTX into TLS level structure */
676 lctx->tls_ssl_ctx = olc_index;
683 openssl_stop_listen (tls_ctx_t * lctx)
686 openssl_listen_ctx_t *olc;
688 olc_index = lctx->tls_ssl_ctx;
689 olc = openssl_lctx_get (olc_index);
691 X509_free (olc->srvcert);
692 EVP_PKEY_free (olc->pkey);
694 SSL_CTX_free (olc->ssl_ctx);
695 openssl_listen_ctx_free (olc);
701 openssl_ctx_init_server (tls_ctx_t * ctx)
703 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
704 u32 olc_index = ctx->tls_ssl_ctx;
705 openssl_listen_ctx_t *olc;
706 session_t *tls_session;
708 #ifdef HAVE_OPENSSL_ASYNC
709 openssl_resume_handler *handler;
712 /* Start a new connection */
714 olc = openssl_lctx_get (olc_index);
715 oc->ssl = SSL_new (olc->ssl_ctx);
718 TLS_DBG (1, "Couldn't initialize ssl struct");
722 oc->rbio = BIO_new (BIO_s_mem ());
723 oc->wbio = BIO_new (BIO_s_mem ());
725 BIO_set_mem_eof_return (oc->rbio, -1);
726 BIO_set_mem_eof_return (oc->wbio, -1);
728 SSL_set_bio (oc->ssl, oc->wbio, oc->rbio);
729 SSL_set_accept_state (oc->ssl);
731 TLS_DBG (1, "Initiating handshake for [%u]%u", ctx->c_thread_index,
732 oc->openssl_ctx_index);
734 tls_session = session_get_from_handle (ctx->tls_session_handle);
737 rv = SSL_do_handshake (oc->ssl);
738 err = SSL_get_error (oc->ssl, rv);
739 openssl_try_handshake_write (oc, tls_session);
740 #ifdef HAVE_OPENSSL_ASYNC
741 if (err == SSL_ERROR_WANT_ASYNC)
743 handler = (openssl_resume_handler *) openssl_ctx_handshake_rx;
744 vpp_ssl_async_process_event (ctx, handler);
748 if (err != SSL_ERROR_WANT_WRITE)
752 TLS_DBG (2, "tls state for [%u]%u is su", ctx->c_thread_index,
753 oc->openssl_ctx_index, SSL_state_string_long (oc->ssl));
758 openssl_handshake_is_over (tls_ctx_t * ctx)
760 openssl_ctx_t *mc = (openssl_ctx_t *) ctx;
763 return SSL_is_init_finished (mc->ssl);
767 openssl_transport_close (tls_ctx_t * ctx)
769 if (!openssl_handshake_is_over (ctx))
771 openssl_handle_handshake_failure (ctx);
774 session_transport_closing_notify (&ctx->connection);
779 openssl_app_close (tls_ctx_t * ctx)
781 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
782 session_t *app_session;
784 /* Wait for all data to be written to tcp */
785 app_session = session_get_from_handle (ctx->app_session_handle);
786 if (BIO_ctrl_pending (oc->rbio) <= 0
787 && !svm_fifo_max_dequeue_cons (app_session->tx_fifo))
788 openssl_confirm_app_close (ctx);
794 const static tls_engine_vft_t openssl_engine = {
795 .ctx_alloc = openssl_ctx_alloc,
796 .ctx_free = openssl_ctx_free,
797 .ctx_get = openssl_ctx_get,
798 .ctx_get_w_thread = openssl_ctx_get_w_thread,
799 .ctx_init_server = openssl_ctx_init_server,
800 .ctx_init_client = openssl_ctx_init_client,
801 .ctx_write = openssl_ctx_write,
802 .ctx_read = openssl_ctx_read,
803 .ctx_handshake_is_over = openssl_handshake_is_over,
804 .ctx_start_listen = openssl_start_listen,
805 .ctx_stop_listen = openssl_stop_listen,
806 .ctx_transport_close = openssl_transport_close,
807 .ctx_app_close = openssl_app_close,
811 tls_init_ca_chain (void)
813 openssl_main_t *om = &openssl_main;
814 tls_main_t *tm = vnet_tls_get_main ();
819 if (access (tm->ca_cert_path, F_OK | R_OK) == -1)
821 clib_warning ("Could not initialize TLS CA certificates");
825 if (!(om->cert_store = X509_STORE_new ()))
827 clib_warning ("failed to create cert store");
831 rv = X509_STORE_load_locations (om->cert_store, tm->ca_cert_path, 0);
834 clib_warning ("failed to load ca certificate");
837 if (tm->use_test_cert_in_ca)
839 cert_bio = BIO_new (BIO_s_mem ());
840 BIO_write (cert_bio, test_srv_crt_rsa, test_srv_crt_rsa_len);
841 testcert = PEM_read_bio_X509 (cert_bio, NULL, NULL, NULL);
844 clib_warning ("unable to parse certificate");
847 X509_STORE_add_cert (om->cert_store, testcert);
850 return (rv < 0 ? -1 : 0);
854 tls_openssl_set_ciphers (char *ciphers)
856 openssl_main_t *om = &openssl_main;
864 vec_validate (om->ciphers, strlen (ciphers) - 1);
865 for (i = 0; i < vec_len (om->ciphers); i++)
867 om->ciphers[i] = toupper (ciphers[i]);
874 static clib_error_t *
875 tls_openssl_init (vlib_main_t * vm)
877 vlib_thread_main_t *vtm = vlib_get_thread_main ();
878 openssl_main_t *om = &openssl_main;
881 num_threads = 1 /* main thread */ + vtm->n_threads;
884 SSL_load_error_strings ();
886 if (tls_init_ca_chain ())
888 clib_warning ("failed to initialize TLS CA chain");
892 vec_validate (om->ctx_pool, num_threads - 1);
894 tls_register_engine (&openssl_engine, TLS_ENGINE_OPENSSL);
898 /* default ciphers */
899 tls_openssl_set_ciphers
900 ("ALL:!ADH:!LOW:!EXP:!MD5:!RC4-SHA:!DES-CBC3-SHA:@STRENGTH");
905 VLIB_INIT_FUNCTION (tls_openssl_init) =
907 .runs_after = VLIB_INITS("tls_init"),
911 #ifdef HAVE_OPENSSL_ASYNC
912 static clib_error_t *
913 tls_openssl_set_command_fn (vlib_main_t * vm, unformat_input_t * input,
914 vlib_cli_command_t * cmd)
916 openssl_main_t *om = &openssl_main;
917 char *engine_name = NULL;
918 char *engine_alg = NULL;
919 char *ciphers = NULL;
920 u8 engine_name_set = 0;
923 /* By present, it is not allowed to configure engine again after running */
926 clib_warning ("engine has started!\n");
927 return clib_error_return
928 (0, "engine has started, and no config is accepted");
931 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
933 if (unformat (input, "engine %s", &engine_name))
937 else if (unformat (input, "async"))
940 openssl_async_node_enable_disable (1);
942 else if (unformat (input, "alg %s", &engine_alg))
944 for (i = 0; i < strnlen (engine_alg, MAX_CRYPTO_LEN); i++)
945 engine_alg[i] = toupper (engine_alg[i]);
947 else if (unformat (input, "ciphers %s", &ciphers))
949 tls_openssl_set_ciphers (ciphers);
952 return clib_error_return (0, "failed: unknown input `%U'",
953 format_unformat_error, input);
956 /* reset parameters if engine is not configured */
957 if (!engine_name_set)
959 clib_warning ("No engine provided! \n");
964 if (!openssl_engine_register (engine_name, engine_alg))
966 return clib_error_return (0, "failed to register %s polling",
975 VLIB_CLI_COMMAND (tls_openssl_set_command, static) =
977 .path = "tls openssl set",
978 .short_help = "tls openssl set [engine <engine name>] [alg [algorithm] [async]",
979 .function = tls_openssl_set_command_fn,
985 VLIB_PLUGIN_REGISTER () = {
986 .version = VPP_BUILD_VER,
987 .description = "Transport Layer Security (TLS) Engine, OpenSSL Based",
992 * fd.io coding-style-patch-verification: ON
995 * eval: (c-set-style "gnu")