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>
20 #ifdef HAVE_OPENSSL_ASYNC
21 #include <openssl/async.h>
24 #include <vnet/plugin/plugin.h>
25 #include <vpp/app/version.h>
26 #include <vnet/tls/tls.h>
28 #include <tlsopenssl/tls_openssl.h>
30 #define MAX_CRYPTO_LEN 64
32 openssl_main_t openssl_main;
34 openssl_ctx_alloc (void)
36 u8 thread_index = vlib_get_thread_index ();
37 openssl_main_t *tm = &openssl_main;
40 pool_get (tm->ctx_pool[thread_index], ctx);
42 *ctx = clib_mem_alloc (sizeof (openssl_ctx_t));
44 clib_memset (*ctx, 0, sizeof (openssl_ctx_t));
45 (*ctx)->ctx.c_thread_index = thread_index;
46 (*ctx)->ctx.tls_ctx_engine = CRYPTO_ENGINE_OPENSSL;
47 (*ctx)->ctx.app_session_handle = SESSION_INVALID_HANDLE;
48 (*ctx)->openssl_ctx_index = ctx - tm->ctx_pool[thread_index];
49 return ((*ctx)->openssl_ctx_index);
53 openssl_ctx_free (tls_ctx_t * ctx)
55 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
57 if (SSL_is_init_finished (oc->ssl) && !ctx->is_passive_close)
58 SSL_shutdown (oc->ssl);
62 #ifdef HAVE_OPENSSL_ASYNC
63 openssl_evt_free (ctx->evt_index, ctx->c_thread_index);
65 vec_free (ctx->srv_hostname);
66 pool_put_index (openssl_main.ctx_pool[ctx->c_thread_index],
67 oc->openssl_ctx_index);
71 openssl_ctx_get (u32 ctx_index)
74 ctx = pool_elt_at_index (openssl_main.ctx_pool[vlib_get_thread_index ()],
80 openssl_ctx_get_w_thread (u32 ctx_index, u8 thread_index)
83 ctx = pool_elt_at_index (openssl_main.ctx_pool[thread_index], ctx_index);
88 openssl_listen_ctx_alloc (void)
90 openssl_main_t *om = &openssl_main;
91 openssl_listen_ctx_t *lctx;
93 pool_get (om->lctx_pool, lctx);
95 clib_memset (lctx, 0, sizeof (openssl_listen_ctx_t));
96 lctx->openssl_lctx_index = lctx - om->lctx_pool;
97 return lctx->openssl_lctx_index;
101 openssl_listen_ctx_free (openssl_listen_ctx_t * lctx)
103 pool_put_index (openssl_main.lctx_pool, lctx->openssl_lctx_index);
106 openssl_listen_ctx_t *
107 openssl_lctx_get (u32 lctx_index)
109 return pool_elt_at_index (openssl_main.lctx_pool, lctx_index);
113 openssl_read_from_bio_into_fifo (svm_fifo_t * f, BIO * bio)
115 u32 enq_now, enq_max;
119 enq_max = svm_fifo_max_enqueue_prod (f);
123 svm_fifo_fill_chunk_list (f);
125 enq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max);
129 read = BIO_read (bio, svm_fifo_tail (f), enq_now);
133 c = svm_fifo_tail_chunk (f);
134 while ((c = c->next) && read < enq_max)
136 enq_now = clib_min (c->length, enq_max - read);
137 rv = BIO_read (bio, c->data, enq_now);
138 read += rv > 0 ? rv : 0;
144 svm_fifo_enqueue_nocopy (f, read);
150 openssl_read_from_ssl_into_fifo (svm_fifo_t * f, SSL * ssl)
152 u32 enq_now, enq_max;
156 enq_max = svm_fifo_max_enqueue_prod (f);
160 svm_fifo_fill_chunk_list (f);
162 enq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max);
166 read = SSL_read (ssl, svm_fifo_tail (f), enq_now);
170 c = svm_fifo_tail_chunk (f);
171 while ((c = c->next) && read < enq_max)
173 enq_now = clib_min (c->length, enq_max - read);
174 rv = SSL_read (ssl, c->data, enq_now);
175 read += rv > 0 ? rv : 0;
181 svm_fifo_enqueue_nocopy (f, read);
187 openssl_write_from_fifo_into_bio (svm_fifo_t * f, BIO * bio, u32 len)
193 deq_now = clib_min (svm_fifo_max_read_chunk (f), len);
194 wrote = BIO_write (bio, svm_fifo_head (f), deq_now);
198 c = svm_fifo_head_chunk (f);
199 while ((c = c->next) && wrote < len)
201 deq_now = clib_min (c->length, len - wrote);
202 rv = BIO_write (bio, c->data, deq_now);
203 wrote += rv > 0 ? rv : 0;
209 svm_fifo_dequeue_drop (f, wrote);
215 openssl_write_from_fifo_into_ssl (svm_fifo_t * f, SSL * ssl, u32 len)
221 deq_now = clib_min (svm_fifo_max_read_chunk (f), len);
222 wrote = SSL_write (ssl, svm_fifo_head (f), deq_now);
226 c = svm_fifo_head_chunk (f);
227 while ((c = c->next) && wrote < len)
229 deq_now = clib_min (c->length, len - wrote);
230 rv = SSL_write (ssl, c->data, deq_now);
231 wrote += rv > 0 ? rv : 0;
237 svm_fifo_dequeue_drop (f, wrote);
243 openssl_try_handshake_read (openssl_ctx_t * oc, session_t * tls_session)
248 f = tls_session->rx_fifo;
249 deq_max = svm_fifo_max_dequeue_cons (f);
253 return openssl_write_from_fifo_into_bio (f, oc->wbio, deq_max);
257 openssl_try_handshake_write (openssl_ctx_t * oc, session_t * tls_session)
261 if (BIO_ctrl_pending (oc->rbio) <= 0)
264 read = openssl_read_from_bio_into_fifo (tls_session->tx_fifo, oc->rbio);
266 tls_add_vpp_q_tx_evt (tls_session);
271 #ifdef HAVE_OPENSSL_ASYNC
273 openssl_check_async_status (tls_ctx_t * ctx, openssl_resume_handler * handler,
276 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
279 SSL_get_async_status (oc->ssl, &estatus);
280 if (estatus == ASYNC_STATUS_EAGAIN)
282 vpp_tls_async_update_event (ctx, 1);
286 vpp_tls_async_update_event (ctx, 0);
296 openssl_handle_handshake_failure (tls_ctx_t * ctx)
298 session_t *app_session;
300 if (SSL_is_server (((openssl_ctx_t *) ctx)->ssl))
303 * Cleanup pre-allocated app session and close transport
306 session_get_if_valid (ctx->c_s_index, ctx->c_thread_index);
309 session_free (app_session);
310 ctx->no_app_session = 1;
311 ctx->c_s_index = SESSION_INVALID_INDEX;
312 tls_disconnect_transport (ctx);
318 * Also handles cleanup of the pre-allocated session
320 tls_notify_app_connected (ctx, /* is failed */ 1);
325 openssl_ctx_handshake_rx (tls_ctx_t * ctx, session_t * tls_session)
327 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
330 while (SSL_in_init (oc->ssl))
336 else if (!openssl_try_handshake_read (oc, tls_session))
339 rv = SSL_do_handshake (oc->ssl);
340 err = SSL_get_error (oc->ssl, rv);
342 #ifdef HAVE_OPENSSL_ASYNC
343 if (err == SSL_ERROR_WANT_ASYNC)
345 openssl_check_async_status (ctx, openssl_ctx_handshake_rx,
349 if (err == SSL_ERROR_SSL)
352 ERR_error_string (ERR_get_error (), buf);
353 clib_warning ("Err: %s", buf);
355 openssl_handle_handshake_failure (ctx);
359 openssl_try_handshake_write (oc, tls_session);
361 if (err != SSL_ERROR_WANT_WRITE)
364 TLS_DBG (2, "tls state for %u is %s", oc->openssl_ctx_index,
365 SSL_state_string_long (oc->ssl));
367 if (SSL_in_init (oc->ssl))
373 if (!SSL_is_server (oc->ssl))
376 * Verify server certificate
378 if ((rv = SSL_get_verify_result (oc->ssl)) != X509_V_OK)
380 TLS_DBG (1, " failed verify: %s\n",
381 X509_verify_cert_error_string (rv));
384 * Presence of hostname enforces strict certificate verification
386 if (ctx->srv_hostname)
388 tls_notify_app_connected (ctx, /* is failed */ 0);
392 tls_notify_app_connected (ctx, /* is failed */ 0);
396 /* Need to check transport status */
397 if (ctx->is_passive_close)
398 openssl_handle_handshake_failure (ctx);
400 tls_notify_app_accept (ctx);
403 TLS_DBG (1, "Handshake for %u complete. TLS cipher is %s",
404 oc->openssl_ctx_index, SSL_get_cipher (oc->ssl));
409 openssl_confirm_app_close (tls_ctx_t * ctx)
411 tls_disconnect_transport (ctx);
412 session_transport_closed_notify (&ctx->connection);
416 openssl_ctx_write (tls_ctx_t * ctx, session_t * app_session)
418 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
419 int wrote = 0, read, max_buf = 4 * TLS_CHUNK_SIZE, max_space;
420 u32 deq_max, to_write;
421 session_t *tls_session;
424 f = app_session->tx_fifo;
426 deq_max = svm_fifo_max_dequeue_cons (f);
430 /* Figure out how much data to write */
431 max_space = max_buf - BIO_ctrl_pending (oc->rbio);
432 max_space = (max_space < 0) ? 0 : max_space;
433 to_write = clib_min (deq_max, (u32) max_space);
435 wrote = openssl_write_from_fifo_into_ssl (f, oc->ssl, to_write);
438 tls_add_vpp_q_builtin_tx_evt (app_session);
442 if (svm_fifo_needs_deq_ntf (f, wrote))
443 session_dequeue_notify (app_session);
445 if (svm_fifo_max_dequeue_cons (f))
446 tls_add_vpp_q_builtin_tx_evt (app_session);
450 if (BIO_ctrl_pending (oc->rbio) <= 0)
453 tls_session = session_get_from_handle (ctx->tls_session_handle);
455 read = openssl_read_from_bio_into_fifo (tls_session->tx_fifo, oc->rbio);
458 tls_add_vpp_q_builtin_tx_evt (app_session);
462 tls_add_vpp_q_tx_evt (tls_session);
464 if (BIO_ctrl_pending (oc->rbio) > 0)
465 tls_add_vpp_q_builtin_tx_evt (app_session);
466 else if (ctx->app_closed)
467 openssl_confirm_app_close (ctx);
473 openssl_ctx_read (tls_ctx_t * ctx, session_t * tls_session)
475 int read, wrote = 0, max_space, max_buf = 4 * TLS_CHUNK_SIZE;
476 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
477 u32 deq_max, to_write;
478 session_t *app_session;
481 if (PREDICT_FALSE (SSL_in_init (oc->ssl)))
483 if (openssl_ctx_handshake_rx (ctx, tls_session) < 0)
489 f = tls_session->rx_fifo;
491 deq_max = svm_fifo_max_dequeue_cons (f);
492 max_space = max_buf - BIO_ctrl_pending (oc->wbio);
493 max_space = max_space < 0 ? 0 : max_space;
494 to_write = clib_min (deq_max, max_space);
498 wrote = openssl_write_from_fifo_into_bio (f, oc->wbio, to_write);
501 tls_add_vpp_q_builtin_rx_evt (tls_session);
505 if (svm_fifo_max_dequeue_cons (f))
506 tls_add_vpp_q_builtin_rx_evt (tls_session);
510 if (BIO_ctrl_pending (oc->wbio) <= 0)
513 app_session = session_get_from_handle (ctx->app_session_handle);
514 f = app_session->rx_fifo;
516 read = openssl_read_from_ssl_into_fifo (f, oc->ssl);
519 tls_add_vpp_q_builtin_rx_evt (tls_session);
523 /* If handshake just completed, session may still be in accepting state */
524 if (app_session->session_state >= SESSION_STATE_READY)
525 tls_notify_app_enqueue (ctx, app_session);
526 if (SSL_pending (oc->ssl) > 0)
527 tls_add_vpp_q_builtin_rx_evt (tls_session);
533 openssl_ctx_init_client (tls_ctx_t * ctx)
535 long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
536 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
537 openssl_main_t *om = &openssl_main;
538 session_t *tls_session;
539 const SSL_METHOD *method;
542 method = SSLv23_client_method ();
545 TLS_DBG (1, "SSLv23_method returned null");
549 oc->ssl_ctx = SSL_CTX_new (method);
550 if (oc->ssl_ctx == NULL)
552 TLS_DBG (1, "SSL_CTX_new returned null");
556 SSL_CTX_set_ecdh_auto (oc->ssl_ctx, 1);
557 SSL_CTX_set_mode (oc->ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
558 #ifdef HAVE_OPENSSL_ASYNC
560 SSL_CTX_set_mode (oc->ssl_ctx, SSL_MODE_ASYNC);
562 rv = SSL_CTX_set_cipher_list (oc->ssl_ctx, (const char *) om->ciphers);
565 TLS_DBG (1, "Couldn't set cipher");
569 SSL_CTX_set_options (oc->ssl_ctx, flags);
570 SSL_CTX_set_cert_store (oc->ssl_ctx, om->cert_store);
572 oc->ssl = SSL_new (oc->ssl_ctx);
575 TLS_DBG (1, "Couldn't initialize ssl struct");
579 oc->rbio = BIO_new (BIO_s_mem ());
580 oc->wbio = BIO_new (BIO_s_mem ());
582 BIO_set_mem_eof_return (oc->rbio, -1);
583 BIO_set_mem_eof_return (oc->wbio, -1);
585 SSL_set_bio (oc->ssl, oc->wbio, oc->rbio);
586 SSL_set_connect_state (oc->ssl);
588 rv = SSL_set_tlsext_host_name (oc->ssl, ctx->srv_hostname);
591 TLS_DBG (1, "Couldn't set hostname");
596 * 2. Do the first steps in the handshake.
598 TLS_DBG (1, "Initiating handshake for [%u]%u", ctx->c_thread_index,
599 oc->openssl_ctx_index);
601 tls_session = session_get_from_handle (ctx->tls_session_handle);
603 #ifdef HAVE_OPENSSL_ASYNC
604 vpp_tls_async_init_event (ctx, openssl_ctx_handshake_rx, tls_session);
608 rv = SSL_do_handshake (oc->ssl);
609 err = SSL_get_error (oc->ssl, rv);
610 openssl_try_handshake_write (oc, tls_session);
611 #ifdef HAVE_OPENSSL_ASYNC
612 if (err == SSL_ERROR_WANT_ASYNC)
614 openssl_check_async_status (ctx, openssl_ctx_handshake_rx,
619 if (err != SSL_ERROR_WANT_WRITE)
623 TLS_DBG (2, "tls state for [%u]%u is su", ctx->c_thread_index,
624 oc->openssl_ctx_index, SSL_state_string_long (oc->ssl));
629 openssl_start_listen (tls_ctx_t * lctx)
631 const SSL_METHOD *method;
638 openssl_listen_ctx_t *olc;
639 app_cert_key_pair_t *ckpair;
641 long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
642 openssl_main_t *om = &openssl_main;
644 ckpair = app_cert_key_pair_get_if_valid (lctx->ckpair_index);
648 if (!ckpair->cert || !ckpair->key)
650 TLS_DBG (1, "tls cert and/or key not configured %d",
651 lctx->parent_app_wrk_index);
655 method = SSLv23_method ();
656 ssl_ctx = SSL_CTX_new (method);
659 clib_warning ("Unable to create SSL context");
663 SSL_CTX_set_mode (ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
664 #ifdef HAVE_OPENSSL_ASYNC
667 SSL_CTX_set_mode (ssl_ctx, SSL_MODE_ASYNC);
668 SSL_CTX_set_async_callback (ssl_ctx, tls_async_openssl_callback);
671 SSL_CTX_set_options (ssl_ctx, flags);
672 SSL_CTX_set_ecdh_auto (ssl_ctx, 1);
674 rv = SSL_CTX_set_cipher_list (ssl_ctx, (const char *) om->ciphers);
677 TLS_DBG (1, "Couldn't set cipher");
682 * Set the key and cert
684 cert_bio = BIO_new (BIO_s_mem ());
685 BIO_write (cert_bio, ckpair->cert, vec_len (ckpair->cert));
686 srvcert = PEM_read_bio_X509 (cert_bio, NULL, NULL, NULL);
689 clib_warning ("unable to parse certificate");
692 SSL_CTX_use_certificate (ssl_ctx, srvcert);
695 cert_bio = BIO_new (BIO_s_mem ());
696 BIO_write (cert_bio, ckpair->key, vec_len (ckpair->key));
697 pkey = PEM_read_bio_PrivateKey (cert_bio, NULL, NULL, NULL);
700 clib_warning ("unable to parse pkey");
703 SSL_CTX_use_PrivateKey (ssl_ctx, pkey);
706 olc_index = openssl_listen_ctx_alloc ();
707 olc = openssl_lctx_get (olc_index);
708 olc->ssl_ctx = ssl_ctx;
709 olc->srvcert = srvcert;
712 /* store SSL_CTX into TLS level structure */
713 lctx->tls_ssl_ctx = olc_index;
720 openssl_stop_listen (tls_ctx_t * lctx)
723 openssl_listen_ctx_t *olc;
725 olc_index = lctx->tls_ssl_ctx;
726 olc = openssl_lctx_get (olc_index);
728 X509_free (olc->srvcert);
729 EVP_PKEY_free (olc->pkey);
731 SSL_CTX_free (olc->ssl_ctx);
732 openssl_listen_ctx_free (olc);
738 openssl_ctx_init_server (tls_ctx_t * ctx)
740 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
741 u32 olc_index = ctx->tls_ssl_ctx;
742 openssl_listen_ctx_t *olc;
743 session_t *tls_session;
746 /* Start a new connection */
748 olc = openssl_lctx_get (olc_index);
749 oc->ssl = SSL_new (olc->ssl_ctx);
752 TLS_DBG (1, "Couldn't initialize ssl struct");
756 oc->rbio = BIO_new (BIO_s_mem ());
757 oc->wbio = BIO_new (BIO_s_mem ());
759 BIO_set_mem_eof_return (oc->rbio, -1);
760 BIO_set_mem_eof_return (oc->wbio, -1);
762 SSL_set_bio (oc->ssl, oc->wbio, oc->rbio);
763 SSL_set_accept_state (oc->ssl);
765 TLS_DBG (1, "Initiating handshake for [%u]%u", ctx->c_thread_index,
766 oc->openssl_ctx_index);
768 tls_session = session_get_from_handle (ctx->tls_session_handle);
769 #ifdef HAVE_OPENSSL_ASYNC
770 vpp_tls_async_init_event (ctx, openssl_ctx_handshake_rx, tls_session);
774 rv = SSL_do_handshake (oc->ssl);
775 err = SSL_get_error (oc->ssl, rv);
776 openssl_try_handshake_write (oc, tls_session);
777 #ifdef HAVE_OPENSSL_ASYNC
778 if (err == SSL_ERROR_WANT_ASYNC)
780 openssl_check_async_status (ctx, openssl_ctx_handshake_rx,
785 if (err != SSL_ERROR_WANT_WRITE)
789 TLS_DBG (2, "tls state for [%u]%u is su", ctx->c_thread_index,
790 oc->openssl_ctx_index, SSL_state_string_long (oc->ssl));
795 openssl_handshake_is_over (tls_ctx_t * ctx)
797 openssl_ctx_t *mc = (openssl_ctx_t *) ctx;
800 return SSL_is_init_finished (mc->ssl);
804 openssl_transport_close (tls_ctx_t * ctx)
806 #ifdef HAVE_OPENSSL_ASYNC
807 if (vpp_openssl_is_inflight (ctx))
811 if (!openssl_handshake_is_over (ctx))
813 openssl_handle_handshake_failure (ctx);
816 session_transport_closing_notify (&ctx->connection);
821 openssl_app_close (tls_ctx_t * ctx)
823 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
824 session_t *app_session;
826 /* Wait for all data to be written to tcp */
827 app_session = session_get_from_handle (ctx->app_session_handle);
828 if (BIO_ctrl_pending (oc->rbio) <= 0
829 && !svm_fifo_max_dequeue_cons (app_session->tx_fifo))
830 openssl_confirm_app_close (ctx);
836 const static tls_engine_vft_t openssl_engine = {
837 .ctx_alloc = openssl_ctx_alloc,
838 .ctx_free = openssl_ctx_free,
839 .ctx_get = openssl_ctx_get,
840 .ctx_get_w_thread = openssl_ctx_get_w_thread,
841 .ctx_init_server = openssl_ctx_init_server,
842 .ctx_init_client = openssl_ctx_init_client,
843 .ctx_write = openssl_ctx_write,
844 .ctx_read = openssl_ctx_read,
845 .ctx_handshake_is_over = openssl_handshake_is_over,
846 .ctx_start_listen = openssl_start_listen,
847 .ctx_stop_listen = openssl_stop_listen,
848 .ctx_transport_close = openssl_transport_close,
849 .ctx_app_close = openssl_app_close,
853 tls_init_ca_chain (void)
855 openssl_main_t *om = &openssl_main;
856 tls_main_t *tm = vnet_tls_get_main ();
861 if (access (tm->ca_cert_path, F_OK | R_OK) == -1)
863 clib_warning ("Could not initialize TLS CA certificates");
867 if (!(om->cert_store = X509_STORE_new ()))
869 clib_warning ("failed to create cert store");
873 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
874 rv = X509_STORE_load_file (om->cert_store, tm->ca_cert_path);
876 rv = X509_STORE_load_locations (om->cert_store, tm->ca_cert_path, 0);
881 clib_warning ("failed to load ca certificate");
884 if (tm->use_test_cert_in_ca)
886 cert_bio = BIO_new (BIO_s_mem ());
887 BIO_write (cert_bio, test_srv_crt_rsa, test_srv_crt_rsa_len);
888 testcert = PEM_read_bio_X509 (cert_bio, NULL, NULL, NULL);
891 clib_warning ("unable to parse certificate");
894 X509_STORE_add_cert (om->cert_store, testcert);
897 return (rv < 0 ? -1 : 0);
901 tls_openssl_set_ciphers (char *ciphers)
903 openssl_main_t *om = &openssl_main;
911 vec_validate (om->ciphers, strlen (ciphers) - 1);
912 for (i = 0; i < vec_len (om->ciphers); i++)
914 om->ciphers[i] = toupper (ciphers[i]);
921 static clib_error_t *
922 tls_openssl_init (vlib_main_t * vm)
924 vlib_thread_main_t *vtm = vlib_get_thread_main ();
925 openssl_main_t *om = &openssl_main;
926 clib_error_t *error = 0;
929 error = tls_openssl_api_init (vm);
930 num_threads = 1 /* main thread */ + vtm->n_threads;
933 SSL_load_error_strings ();
935 if (tls_init_ca_chain ())
937 clib_warning ("failed to initialize TLS CA chain");
941 vec_validate (om->ctx_pool, num_threads - 1);
943 tls_register_engine (&openssl_engine, CRYPTO_ENGINE_OPENSSL);
947 /* default ciphers */
948 tls_openssl_set_ciphers
949 ("ALL:!ADH:!LOW:!EXP:!MD5:!RC4-SHA:!DES-CBC3-SHA:@STRENGTH");
954 VLIB_INIT_FUNCTION (tls_openssl_init) =
956 .runs_after = VLIB_INITS("tls_init"),
960 #ifdef HAVE_OPENSSL_ASYNC
961 static clib_error_t *
962 tls_openssl_set_command_fn (vlib_main_t * vm, unformat_input_t * input,
963 vlib_cli_command_t * cmd)
965 openssl_main_t *om = &openssl_main;
966 char *engine_name = NULL;
967 char *engine_alg = NULL;
968 char *ciphers = NULL;
969 u8 engine_name_set = 0;
972 /* By present, it is not allowed to configure engine again after running */
975 clib_warning ("engine has started!\n");
976 return clib_error_return
977 (0, "engine has started, and no config is accepted");
980 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
982 if (unformat (input, "engine %s", &engine_name))
986 else if (unformat (input, "async"))
990 else if (unformat (input, "alg %s", &engine_alg))
992 for (i = 0; i < strnlen (engine_alg, MAX_CRYPTO_LEN); i++)
993 engine_alg[i] = toupper (engine_alg[i]);
995 else if (unformat (input, "ciphers %s", &ciphers))
997 tls_openssl_set_ciphers (ciphers);
1000 return clib_error_return (0, "failed: unknown input `%U'",
1001 format_unformat_error, input);
1004 /* reset parameters if engine is not configured */
1005 if (!engine_name_set)
1007 clib_warning ("No engine provided! \n");
1012 vnet_session_enable_disable (vm, 1);
1013 if (openssl_engine_register (engine_name, engine_alg, async) < 0)
1015 return clib_error_return (0, "Failed to register %s polling",
1020 vlib_cli_output (vm, "Successfully register engine %s\n",
1030 VLIB_CLI_COMMAND (tls_openssl_set_command, static) =
1032 .path = "tls openssl set",
1033 .short_help = "tls openssl set [engine <engine name>] [alg [algorithm] [async]",
1034 .function = tls_openssl_set_command_fn,
1040 VLIB_PLUGIN_REGISTER () = {
1041 .version = VPP_BUILD_VER,
1042 .description = "Transport Layer Security (TLS) Engine, OpenSSL Based",
1047 * fd.io coding-style-patch-verification: ON
1050 * eval: (c-set-style "gnu")