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_try_handshake_read (openssl_ctx_t * oc, session_t * tls_session)
115 u32 deq_max, deq_now;
119 f = tls_session->rx_fifo;
120 deq_max = svm_fifo_max_dequeue_cons (f);
124 deq_now = clib_min (svm_fifo_max_read_chunk (f), deq_max);
125 wrote = BIO_write (oc->wbio, svm_fifo_head (f), deq_now);
129 svm_fifo_dequeue_drop (f, wrote);
132 deq_now = clib_min (svm_fifo_max_read_chunk (f), deq_max - wrote);
133 rv = BIO_write (oc->wbio, svm_fifo_head (f), deq_now);
136 svm_fifo_dequeue_drop (f, rv);
144 openssl_try_handshake_write (openssl_ctx_t * oc, session_t * tls_session)
146 u32 enq_max, deq_now;
150 if (BIO_ctrl_pending (oc->rbio) <= 0)
153 f = tls_session->tx_fifo;
154 enq_max = svm_fifo_max_enqueue_prod (f);
158 deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max);
159 read = BIO_read (oc->rbio, svm_fifo_tail (f), deq_now);
163 svm_fifo_enqueue_nocopy (f, read);
164 tls_add_vpp_q_tx_evt (tls_session);
168 deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max - read);
169 rv = BIO_read (oc->rbio, svm_fifo_tail (f), deq_now);
172 svm_fifo_enqueue_nocopy (f, rv);
180 #ifdef HAVE_OPENSSL_ASYNC
182 openssl_check_async_status (tls_ctx_t * ctx, openssl_resume_handler * handler,
185 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
188 SSL_get_async_status (oc->ssl, &estatus);
189 if (estatus == ASYNC_STATUS_EAGAIN)
191 vpp_tls_async_update_event (ctx, 1);
195 vpp_tls_async_update_event (ctx, 0);
205 openssl_handle_handshake_failure (tls_ctx_t * ctx)
207 if (SSL_is_server (((openssl_ctx_t *) ctx)->ssl))
210 * Cleanup pre-allocated app session and close transport
212 session_free (session_get (ctx->c_s_index, ctx->c_thread_index));
213 ctx->no_app_session = 1;
214 ctx->c_s_index = SESSION_INVALID_INDEX;
215 tls_disconnect_transport (ctx);
220 * Also handles cleanup of the pre-allocated session
222 tls_notify_app_connected (ctx, /* is failed */ 1);
227 openssl_ctx_handshake_rx (tls_ctx_t * ctx, session_t * tls_session)
229 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
232 while (SSL_in_init (oc->ssl))
238 else if (!openssl_try_handshake_read (oc, tls_session))
241 rv = SSL_do_handshake (oc->ssl);
242 err = SSL_get_error (oc->ssl, rv);
244 #ifdef HAVE_OPENSSL_ASYNC
245 if (err == SSL_ERROR_WANT_ASYNC)
247 openssl_check_async_status (ctx, openssl_ctx_handshake_rx,
251 if (err == SSL_ERROR_SSL)
254 ERR_error_string (ERR_get_error (), buf);
255 clib_warning ("Err: %s", buf);
257 openssl_handle_handshake_failure (ctx);
261 openssl_try_handshake_write (oc, tls_session);
263 if (err != SSL_ERROR_WANT_WRITE)
266 TLS_DBG (2, "tls state for %u is %s", oc->openssl_ctx_index,
267 SSL_state_string_long (oc->ssl));
269 if (SSL_in_init (oc->ssl))
275 if (!SSL_is_server (oc->ssl))
278 * Verify server certificate
280 if ((rv = SSL_get_verify_result (oc->ssl)) != X509_V_OK)
282 TLS_DBG (1, " failed verify: %s\n",
283 X509_verify_cert_error_string (rv));
286 * Presence of hostname enforces strict certificate verification
288 if (ctx->srv_hostname)
290 tls_notify_app_connected (ctx, /* is failed */ 0);
294 tls_notify_app_connected (ctx, /* is failed */ 0);
298 tls_notify_app_accept (ctx);
301 TLS_DBG (1, "Handshake for %u complete. TLS cipher is %s",
302 oc->openssl_ctx_index, SSL_get_cipher (oc->ssl));
307 openssl_confirm_app_close (tls_ctx_t * ctx)
309 tls_disconnect_transport (ctx);
310 session_transport_closed_notify (&ctx->connection);
314 openssl_ctx_write (tls_ctx_t * ctx, session_t * app_session)
316 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
317 int wrote = 0, rv, read, max_buf = 100 * TLS_CHUNK_SIZE, max_space;
318 u32 enq_max, deq_max, deq_now, to_write;
319 session_t *tls_session;
322 f = app_session->tx_fifo;
323 deq_max = svm_fifo_max_dequeue_cons (f);
327 max_space = max_buf - BIO_ctrl_pending (oc->rbio);
328 max_space = (max_space < 0) ? 0 : max_space;
329 deq_now = clib_min (deq_max, (u32) max_space);
330 to_write = clib_min (svm_fifo_max_read_chunk (f), deq_now);
331 wrote = SSL_write (oc->ssl, svm_fifo_head (f), to_write);
334 tls_add_vpp_q_builtin_tx_evt (app_session);
337 svm_fifo_dequeue_drop (app_session->tx_fifo, wrote);
340 to_write = clib_min (svm_fifo_max_read_chunk (f), deq_now - wrote);
341 rv = SSL_write (oc->ssl, svm_fifo_head (f), to_write);
344 svm_fifo_dequeue_drop (app_session->tx_fifo, rv);
349 if (svm_fifo_needs_deq_ntf (app_session->tx_fifo, wrote))
350 session_dequeue_notify (app_session);
353 tls_add_vpp_q_builtin_tx_evt (app_session);
357 if (BIO_ctrl_pending (oc->rbio) <= 0)
360 tls_session = session_get_from_handle (ctx->tls_session_handle);
361 f = tls_session->tx_fifo;
362 enq_max = svm_fifo_max_enqueue_prod (f);
365 tls_add_vpp_q_builtin_tx_evt (app_session);
369 deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max);
370 read = BIO_read (oc->rbio, svm_fifo_tail (f), deq_now);
373 tls_add_vpp_q_builtin_tx_evt (app_session);
377 svm_fifo_enqueue_nocopy (f, read);
378 tls_add_vpp_q_tx_evt (tls_session);
380 if (read < enq_max && BIO_ctrl_pending (oc->rbio) > 0)
382 deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max - read);
383 read = BIO_read (oc->rbio, svm_fifo_tail (f), deq_now);
385 svm_fifo_enqueue_nocopy (f, read);
388 if (BIO_ctrl_pending (oc->rbio) > 0)
389 tls_add_vpp_q_builtin_tx_evt (app_session);
390 else if (ctx->app_closed)
391 openssl_confirm_app_close (ctx);
397 openssl_ctx_read (tls_ctx_t * ctx, session_t * tls_session)
399 int read, wrote = 0, max_space, max_buf = 100 * TLS_CHUNK_SIZE, rv;
400 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
401 u32 deq_max, enq_max, deq_now, to_read;
402 session_t *app_session;
405 if (PREDICT_FALSE (SSL_in_init (oc->ssl)))
407 if (openssl_ctx_handshake_rx (ctx, tls_session) < 0)
413 f = tls_session->rx_fifo;
414 deq_max = svm_fifo_max_dequeue_cons (f);
415 max_space = max_buf - BIO_ctrl_pending (oc->wbio);
416 max_space = max_space < 0 ? 0 : max_space;
417 deq_now = clib_min (deq_max, max_space);
421 to_read = clib_min (svm_fifo_max_read_chunk (f), deq_now);
422 wrote = BIO_write (oc->wbio, svm_fifo_head (f), to_read);
425 tls_add_vpp_q_builtin_rx_evt (tls_session);
428 svm_fifo_dequeue_drop (f, wrote);
431 to_read = clib_min (svm_fifo_max_read_chunk (f), deq_now - wrote);
432 rv = BIO_write (oc->wbio, svm_fifo_head (f), to_read);
435 svm_fifo_dequeue_drop (f, rv);
439 if (svm_fifo_max_dequeue_cons (f))
440 tls_add_vpp_q_builtin_rx_evt (tls_session);
444 if (BIO_ctrl_pending (oc->wbio) <= 0)
447 app_session = session_get_from_handle (ctx->app_session_handle);
448 f = app_session->rx_fifo;
449 enq_max = svm_fifo_max_enqueue_prod (f);
452 tls_add_vpp_q_builtin_rx_evt (tls_session);
456 deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max);
457 read = SSL_read (oc->ssl, svm_fifo_tail (f), deq_now);
460 tls_add_vpp_q_builtin_rx_evt (tls_session);
463 svm_fifo_enqueue_nocopy (f, read);
464 if (read < enq_max && SSL_pending (oc->ssl) > 0)
466 deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max - read);
467 read = SSL_read (oc->ssl, svm_fifo_tail (f), deq_now);
469 svm_fifo_enqueue_nocopy (f, read);
472 /* If handshake just completed, session may still be in accepting state */
473 if (app_session->session_state >= SESSION_STATE_READY)
474 tls_notify_app_enqueue (ctx, app_session);
475 if (SSL_pending (oc->ssl) > 0)
476 tls_add_vpp_q_builtin_rx_evt (tls_session);
482 openssl_ctx_init_client (tls_ctx_t * ctx)
484 long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
485 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
486 openssl_main_t *om = &openssl_main;
487 session_t *tls_session;
488 const SSL_METHOD *method;
491 method = SSLv23_client_method ();
494 TLS_DBG (1, "SSLv23_method returned null");
498 oc->ssl_ctx = SSL_CTX_new (method);
499 if (oc->ssl_ctx == NULL)
501 TLS_DBG (1, "SSL_CTX_new returned null");
505 SSL_CTX_set_ecdh_auto (oc->ssl_ctx, 1);
506 SSL_CTX_set_mode (oc->ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
507 #ifdef HAVE_OPENSSL_ASYNC
509 SSL_CTX_set_mode (oc->ssl_ctx, SSL_MODE_ASYNC);
511 rv = SSL_CTX_set_cipher_list (oc->ssl_ctx, (const char *) om->ciphers);
514 TLS_DBG (1, "Couldn't set cipher");
518 SSL_CTX_set_options (oc->ssl_ctx, flags);
519 SSL_CTX_set_cert_store (oc->ssl_ctx, om->cert_store);
521 oc->ssl = SSL_new (oc->ssl_ctx);
524 TLS_DBG (1, "Couldn't initialize ssl struct");
528 oc->rbio = BIO_new (BIO_s_mem ());
529 oc->wbio = BIO_new (BIO_s_mem ());
531 BIO_set_mem_eof_return (oc->rbio, -1);
532 BIO_set_mem_eof_return (oc->wbio, -1);
534 SSL_set_bio (oc->ssl, oc->wbio, oc->rbio);
535 SSL_set_connect_state (oc->ssl);
537 rv = SSL_set_tlsext_host_name (oc->ssl, ctx->srv_hostname);
540 TLS_DBG (1, "Couldn't set hostname");
545 * 2. Do the first steps in the handshake.
547 TLS_DBG (1, "Initiating handshake for [%u]%u", ctx->c_thread_index,
548 oc->openssl_ctx_index);
550 tls_session = session_get_from_handle (ctx->tls_session_handle);
552 #ifdef HAVE_OPENSSL_ASYNC
553 vpp_tls_async_init_event (ctx, openssl_ctx_handshake_rx, tls_session);
557 rv = SSL_do_handshake (oc->ssl);
558 err = SSL_get_error (oc->ssl, rv);
559 openssl_try_handshake_write (oc, tls_session);
560 #ifdef HAVE_OPENSSL_ASYNC
561 if (err == SSL_ERROR_WANT_ASYNC)
563 openssl_check_async_status (ctx, openssl_ctx_handshake_rx,
568 if (err != SSL_ERROR_WANT_WRITE)
572 TLS_DBG (2, "tls state for [%u]%u is su", ctx->c_thread_index,
573 oc->openssl_ctx_index, SSL_state_string_long (oc->ssl));
578 openssl_start_listen (tls_ctx_t * lctx)
580 const SSL_METHOD *method;
587 openssl_listen_ctx_t *olc;
588 app_cert_key_pair_t *ckpair;
590 long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
591 openssl_main_t *om = &openssl_main;
593 ckpair = app_cert_key_pair_get_if_valid (lctx->ckpair_index);
597 if (!ckpair->cert || !ckpair->key)
599 TLS_DBG (1, "tls cert and/or key not configured %d",
600 lctx->parent_app_wrk_index);
604 method = SSLv23_method ();
605 ssl_ctx = SSL_CTX_new (method);
608 clib_warning ("Unable to create SSL context");
612 SSL_CTX_set_mode (ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
613 #ifdef HAVE_OPENSSL_ASYNC
616 SSL_CTX_set_mode (ssl_ctx, SSL_MODE_ASYNC);
617 SSL_CTX_set_async_callback (ssl_ctx, tls_async_openssl_callback);
620 SSL_CTX_set_options (ssl_ctx, flags);
621 SSL_CTX_set_ecdh_auto (ssl_ctx, 1);
623 rv = SSL_CTX_set_cipher_list (ssl_ctx, (const char *) om->ciphers);
626 TLS_DBG (1, "Couldn't set cipher");
631 * Set the key and cert
633 cert_bio = BIO_new (BIO_s_mem ());
634 BIO_write (cert_bio, ckpair->cert, vec_len (ckpair->cert));
635 srvcert = PEM_read_bio_X509 (cert_bio, NULL, NULL, NULL);
638 clib_warning ("unable to parse certificate");
641 SSL_CTX_use_certificate (ssl_ctx, srvcert);
644 cert_bio = BIO_new (BIO_s_mem ());
645 BIO_write (cert_bio, ckpair->key, vec_len (ckpair->key));
646 pkey = PEM_read_bio_PrivateKey (cert_bio, NULL, NULL, NULL);
649 clib_warning ("unable to parse pkey");
652 SSL_CTX_use_PrivateKey (ssl_ctx, pkey);
655 olc_index = openssl_listen_ctx_alloc ();
656 olc = openssl_lctx_get (olc_index);
657 olc->ssl_ctx = ssl_ctx;
658 olc->srvcert = srvcert;
661 /* store SSL_CTX into TLS level structure */
662 lctx->tls_ssl_ctx = olc_index;
669 openssl_stop_listen (tls_ctx_t * lctx)
672 openssl_listen_ctx_t *olc;
674 olc_index = lctx->tls_ssl_ctx;
675 olc = openssl_lctx_get (olc_index);
677 X509_free (olc->srvcert);
678 EVP_PKEY_free (olc->pkey);
680 SSL_CTX_free (olc->ssl_ctx);
681 openssl_listen_ctx_free (olc);
687 openssl_ctx_init_server (tls_ctx_t * ctx)
689 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
690 u32 olc_index = ctx->tls_ssl_ctx;
691 openssl_listen_ctx_t *olc;
692 session_t *tls_session;
695 /* Start a new connection */
697 olc = openssl_lctx_get (olc_index);
698 oc->ssl = SSL_new (olc->ssl_ctx);
701 TLS_DBG (1, "Couldn't initialize ssl struct");
705 oc->rbio = BIO_new (BIO_s_mem ());
706 oc->wbio = BIO_new (BIO_s_mem ());
708 BIO_set_mem_eof_return (oc->rbio, -1);
709 BIO_set_mem_eof_return (oc->wbio, -1);
711 SSL_set_bio (oc->ssl, oc->wbio, oc->rbio);
712 SSL_set_accept_state (oc->ssl);
714 TLS_DBG (1, "Initiating handshake for [%u]%u", ctx->c_thread_index,
715 oc->openssl_ctx_index);
717 tls_session = session_get_from_handle (ctx->tls_session_handle);
718 #ifdef HAVE_OPENSSL_ASYNC
719 vpp_tls_async_init_event (ctx, openssl_ctx_handshake_rx, tls_session);
723 rv = SSL_do_handshake (oc->ssl);
724 err = SSL_get_error (oc->ssl, rv);
725 openssl_try_handshake_write (oc, tls_session);
726 #ifdef HAVE_OPENSSL_ASYNC
727 if (err == SSL_ERROR_WANT_ASYNC)
729 openssl_check_async_status (ctx, openssl_ctx_handshake_rx,
734 if (err != SSL_ERROR_WANT_WRITE)
738 TLS_DBG (2, "tls state for [%u]%u is su", ctx->c_thread_index,
739 oc->openssl_ctx_index, SSL_state_string_long (oc->ssl));
744 openssl_handshake_is_over (tls_ctx_t * ctx)
746 openssl_ctx_t *mc = (openssl_ctx_t *) ctx;
749 return SSL_is_init_finished (mc->ssl);
753 openssl_transport_close (tls_ctx_t * ctx)
755 if (!openssl_handshake_is_over (ctx))
757 openssl_handle_handshake_failure (ctx);
760 session_transport_closing_notify (&ctx->connection);
765 openssl_app_close (tls_ctx_t * ctx)
767 openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
768 session_t *app_session;
770 /* Wait for all data to be written to tcp */
771 app_session = session_get_from_handle (ctx->app_session_handle);
772 if (BIO_ctrl_pending (oc->rbio) <= 0
773 && !svm_fifo_max_dequeue_cons (app_session->tx_fifo))
774 openssl_confirm_app_close (ctx);
780 const static tls_engine_vft_t openssl_engine = {
781 .ctx_alloc = openssl_ctx_alloc,
782 .ctx_free = openssl_ctx_free,
783 .ctx_get = openssl_ctx_get,
784 .ctx_get_w_thread = openssl_ctx_get_w_thread,
785 .ctx_init_server = openssl_ctx_init_server,
786 .ctx_init_client = openssl_ctx_init_client,
787 .ctx_write = openssl_ctx_write,
788 .ctx_read = openssl_ctx_read,
789 .ctx_handshake_is_over = openssl_handshake_is_over,
790 .ctx_start_listen = openssl_start_listen,
791 .ctx_stop_listen = openssl_stop_listen,
792 .ctx_transport_close = openssl_transport_close,
793 .ctx_app_close = openssl_app_close,
797 tls_init_ca_chain (void)
799 openssl_main_t *om = &openssl_main;
800 tls_main_t *tm = vnet_tls_get_main ();
805 if (access (tm->ca_cert_path, F_OK | R_OK) == -1)
807 clib_warning ("Could not initialize TLS CA certificates");
811 if (!(om->cert_store = X509_STORE_new ()))
813 clib_warning ("failed to create cert store");
817 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
818 rv = X509_STORE_load_file (om->cert_store, tm->ca_cert_path);
820 rv = X509_STORE_load_locations (om->cert_store, tm->ca_cert_path, 0);
825 clib_warning ("failed to load ca certificate");
828 if (tm->use_test_cert_in_ca)
830 cert_bio = BIO_new (BIO_s_mem ());
831 BIO_write (cert_bio, test_srv_crt_rsa, test_srv_crt_rsa_len);
832 testcert = PEM_read_bio_X509 (cert_bio, NULL, NULL, NULL);
835 clib_warning ("unable to parse certificate");
838 X509_STORE_add_cert (om->cert_store, testcert);
841 return (rv < 0 ? -1 : 0);
845 tls_openssl_set_ciphers (char *ciphers)
847 openssl_main_t *om = &openssl_main;
855 vec_validate (om->ciphers, strlen (ciphers) - 1);
856 for (i = 0; i < vec_len (om->ciphers); i++)
858 om->ciphers[i] = toupper (ciphers[i]);
865 static clib_error_t *
866 tls_openssl_init (vlib_main_t * vm)
868 vlib_thread_main_t *vtm = vlib_get_thread_main ();
869 openssl_main_t *om = &openssl_main;
870 clib_error_t *error = 0;
873 error = tls_openssl_api_init (vm);
874 num_threads = 1 /* main thread */ + vtm->n_threads;
877 SSL_load_error_strings ();
879 if (tls_init_ca_chain ())
881 clib_warning ("failed to initialize TLS CA chain");
885 vec_validate (om->ctx_pool, num_threads - 1);
887 tls_register_engine (&openssl_engine, CRYPTO_ENGINE_OPENSSL);
891 /* default ciphers */
892 tls_openssl_set_ciphers
893 ("ALL:!ADH:!LOW:!EXP:!MD5:!RC4-SHA:!DES-CBC3-SHA:@STRENGTH");
898 VLIB_INIT_FUNCTION (tls_openssl_init) =
900 .runs_after = VLIB_INITS("tls_init"),
904 #ifdef HAVE_OPENSSL_ASYNC
905 static clib_error_t *
906 tls_openssl_set_command_fn (vlib_main_t * vm, unformat_input_t * input,
907 vlib_cli_command_t * cmd)
909 openssl_main_t *om = &openssl_main;
910 char *engine_name = NULL;
911 char *engine_alg = NULL;
912 char *ciphers = NULL;
913 u8 engine_name_set = 0;
916 /* By present, it is not allowed to configure engine again after running */
919 clib_warning ("engine has started!\n");
920 return clib_error_return
921 (0, "engine has started, and no config is accepted");
924 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
926 if (unformat (input, "engine %s", &engine_name))
930 else if (unformat (input, "async"))
934 else if (unformat (input, "alg %s", &engine_alg))
936 for (i = 0; i < strnlen (engine_alg, MAX_CRYPTO_LEN); i++)
937 engine_alg[i] = toupper (engine_alg[i]);
939 else if (unformat (input, "ciphers %s", &ciphers))
941 tls_openssl_set_ciphers (ciphers);
944 return clib_error_return (0, "failed: unknown input `%U'",
945 format_unformat_error, input);
948 /* reset parameters if engine is not configured */
949 if (!engine_name_set)
951 clib_warning ("No engine provided! \n");
956 vnet_session_enable_disable (vm, 1);
957 if (openssl_engine_register (engine_name, engine_alg, async) < 0)
959 return clib_error_return (0, "Failed to register %s polling",
964 vlib_cli_output (vm, "Successfully register engine %s\n",
974 VLIB_CLI_COMMAND (tls_openssl_set_command, static) =
976 .path = "tls openssl set",
977 .short_help = "tls openssl set [engine <engine name>] [alg [algorithm] [async]",
978 .function = tls_openssl_set_command_fn,
984 VLIB_PLUGIN_REGISTER () = {
985 .version = VPP_BUILD_VER,
986 .description = "Transport Layer Security (TLS) Engine, OpenSSL Based",
991 * fd.io coding-style-patch-verification: ON
994 * eval: (c-set-style "gnu")