FD.io VPP  v18.10-32-g1161dda
Vector Packet Processing
tls_openssl.c
Go to the documentation of this file.
1 /*
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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 
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>
21 #endif
22 #include <dlfcn.h>
23 #include <vnet/plugin/plugin.h>
24 #include <vpp/app/version.h>
25 #include <vnet/tls/tls.h>
26 #include <ctype.h>
27 #include <tlsopenssl/tls_openssl.h>
28 
29 #define MAX_CRYPTO_LEN 16
30 
32 static u32
34 {
35  u8 thread_index = vlib_get_thread_index ();
38 
39  pool_get (tm->ctx_pool[thread_index], ctx);
40  if (!(*ctx))
41  *ctx = clib_mem_alloc (sizeof (openssl_ctx_t));
42 
43  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);
49 }
50 
51 static void
53 {
54  openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
55 
56  if (SSL_is_init_finished (oc->ssl) && !ctx->is_passive_close)
57  SSL_shutdown (oc->ssl);
58 
59  SSL_free (oc->ssl);
60 
61  pool_put_index (openssl_main.ctx_pool[ctx->c_thread_index],
62  oc->openssl_ctx_index);
63 }
64 
65 tls_ctx_t *
66 openssl_ctx_get (u32 ctx_index)
67 {
69  ctx = pool_elt_at_index (openssl_main.ctx_pool[vlib_get_thread_index ()],
70  ctx_index);
71  return &(*ctx)->ctx;
72 }
73 
74 tls_ctx_t *
75 openssl_ctx_get_w_thread (u32 ctx_index, u8 thread_index)
76 {
78  ctx = pool_elt_at_index (openssl_main.ctx_pool[thread_index], ctx_index);
79  return &(*ctx)->ctx;
80 }
81 
82 static u32
84 {
87 
88  pool_get (om->lctx_pool, lctx);
89 
90  memset (lctx, 0, sizeof (openssl_listen_ctx_t));
91  lctx->openssl_lctx_index = lctx - om->lctx_pool;
92  return lctx->openssl_lctx_index;
93 }
94 
95 static void
97 {
98  pool_put_index (openssl_main.lctx_pool, lctx->openssl_lctx_index);
99 }
100 
102 openssl_lctx_get (u32 lctx_index)
103 {
104  return pool_elt_at_index (openssl_main.lctx_pool, lctx_index);
105 }
106 
107 static int
109  stream_session_t * tls_session)
110 {
111  u32 deq_max, deq_now;
112  svm_fifo_t *f;
113  int wrote, rv;
114 
115  f = tls_session->server_rx_fifo;
116  deq_max = svm_fifo_max_dequeue (f);
117  if (!deq_max)
118  return 0;
119 
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);
122  if (wrote <= 0)
123  return 0;
124 
125  svm_fifo_dequeue_drop (f, wrote);
126  if (wrote < deq_max)
127  {
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);
130  if (rv > 0)
131  {
132  svm_fifo_dequeue_drop (f, rv);
133  wrote += rv;
134  }
135  }
136  return wrote;
137 }
138 
139 static int
141  stream_session_t * tls_session)
142 {
143  u32 enq_max, deq_now;
144  svm_fifo_t *f;
145  int read, rv;
146 
147  if (BIO_ctrl_pending (oc->rbio) <= 0)
148  return 0;
149 
150  f = tls_session->server_tx_fifo;
151  enq_max = svm_fifo_max_enqueue (f);
152  if (!enq_max)
153  return 0;
154 
155  deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max);
156  read = BIO_read (oc->rbio, svm_fifo_tail (f), deq_now);
157  if (read <= 0)
158  return 0;
159 
160  svm_fifo_enqueue_nocopy (f, read);
161  tls_add_vpp_q_tx_evt (tls_session);
162 
163  if (read < enq_max)
164  {
165  deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max - read);
166  rv = BIO_read (oc->rbio, svm_fifo_tail (f), deq_now);
167  if (rv > 0)
168  {
169  svm_fifo_enqueue_nocopy (f, rv);
170  read += rv;
171  }
172  }
173 
174  return read;
175 }
176 
177 #ifdef HAVE_OPENSSL_ASYNC
178 static int
179 vpp_ssl_async_process_event (tls_ctx_t * ctx,
180  openssl_resume_handler * handler)
181 {
182  openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
183  openssl_tls_callback_t *engine_cb;
184 
185  engine_cb = vpp_add_async_pending_event (ctx, handler);
186  if (engine_cb)
187  {
188  SSL_set_async_callback (oc->ssl, (void *) engine_cb->callback,
189  (void *) engine_cb->arg);
190  TLS_DBG (2, "set callback to engine %p\n", engine_cb->callback);
191  }
192  return 0;
193 
194 }
195 
196 /* Due to engine busy stat, VPP need to retry later */
197 static int
198 vpp_ssl_async_retry_func (tls_ctx_t * ctx, openssl_resume_handler * handler)
199 {
200  openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
201 
202  if (vpp_add_async_run_event (ctx, handler))
203  {
204  SSL_set_async_estatus (oc->ssl, 0);
205  }
206  return 0;
207 
208 }
209 
210 #endif
211 
212 int
214 {
215  openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
216  int rv = 0, err;
217 #ifdef HAVE_OPENSSL_ASYNC
218  int estatus;
219  openssl_resume_handler *myself;
220 #endif
221 
222  while (SSL_in_init (oc->ssl))
223  {
224  if (ctx->resume)
225  {
226  ctx->resume = 0;
227  }
228  else if (!openssl_try_handshake_read (oc, tls_session))
229  {
230  break;
231  }
232 
233  rv = SSL_do_handshake (oc->ssl);
234  err = SSL_get_error (oc->ssl, rv);
235  openssl_try_handshake_write (oc, tls_session);
236 #ifdef HAVE_OPENSSL_ASYNC
237  myself = openssl_ctx_handshake_rx;
238  if (SSL_get_async_estatus (oc->ssl, &estatus)
239  && (estatus == ENGINE_STATUS_RETRY))
240  {
241  vpp_ssl_async_retry_func (ctx, myself);
242  }
243  else if (err == SSL_ERROR_WANT_ASYNC)
244  {
245  vpp_ssl_async_process_event (ctx, myself);
246  }
247 #endif
248 
249  if (err != SSL_ERROR_WANT_WRITE)
250  {
251  if (err == SSL_ERROR_SSL)
252  {
253  char buf[512];
254  ERR_error_string (ERR_get_error (), buf);
255  clib_warning ("Err: %s", buf);
256  }
257  break;
258  }
259  }
260  TLS_DBG (2, "tls state for %u is %s", oc->openssl_ctx_index,
261  SSL_state_string_long (oc->ssl));
262 
263  if (SSL_in_init (oc->ssl))
264  return 0;
265 
266  /*
267  * Handshake complete
268  */
269  if (!SSL_is_server (oc->ssl))
270  {
271  /*
272  * Verify server certificate
273  */
274  if ((rv = SSL_get_verify_result (oc->ssl)) != X509_V_OK)
275  {
276  TLS_DBG (1, " failed verify: %s\n",
277  X509_verify_cert_error_string (rv));
278 
279  /*
280  * Presence of hostname enforces strict certificate verification
281  */
282  if (ctx->srv_hostname)
283  {
284  tls_notify_app_connected (ctx, /* is failed */ 0);
285  return -1;
286  }
287  }
288  tls_notify_app_connected (ctx, /* is failed */ 0);
289  }
290  else
291  {
292  tls_notify_app_accept (ctx);
293  }
294 
295  TLS_DBG (1, "Handshake for %u complete. TLS cipher is %s",
296  oc->openssl_ctx_index, SSL_get_cipher (oc->ssl));
297  return rv;
298 }
299 
300 static inline int
302 {
303  openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
304  int wrote = 0, rv, read, max_buf = 100 * TLS_CHUNK_SIZE, max_space;
305  u32 enq_max, deq_max, deq_now, to_write;
306  stream_session_t *tls_session;
307  svm_fifo_t *f;
308 
309  f = app_session->server_tx_fifo;
310  deq_max = svm_fifo_max_dequeue (f);
311  if (!deq_max)
312  goto check_tls_fifo;
313 
314  max_space = max_buf - BIO_ctrl_pending (oc->rbio);
315  max_space = (max_space < 0) ? 0 : max_space;
316  deq_now = clib_min (deq_max, (u32) max_space);
317  to_write = clib_min (svm_fifo_max_read_chunk (f), deq_now);
318  wrote = SSL_write (oc->ssl, svm_fifo_head (f), to_write);
319  if (wrote <= 0)
320  {
321  tls_add_vpp_q_builtin_tx_evt (app_session);
322  goto check_tls_fifo;
323  }
324  svm_fifo_dequeue_drop (app_session->server_tx_fifo, wrote);
325  if (wrote < deq_now)
326  {
327  to_write = clib_min (svm_fifo_max_read_chunk (f), deq_now - wrote);
328  rv = SSL_write (oc->ssl, svm_fifo_head (f), to_write);
329  if (rv > 0)
330  {
331  svm_fifo_dequeue_drop (app_session->server_tx_fifo, rv);
332  wrote += rv;
333  }
334  }
335 
336  if (wrote < deq_max)
337  tls_add_vpp_q_builtin_tx_evt (app_session);
338 
339 check_tls_fifo:
340 
341  if (BIO_ctrl_pending (oc->rbio) <= 0)
342  return wrote;
343 
344  tls_session = session_get_from_handle (ctx->tls_session_handle);
345  f = tls_session->server_tx_fifo;
346  enq_max = svm_fifo_max_enqueue (f);
347  if (!enq_max)
348  {
349  tls_add_vpp_q_builtin_tx_evt (app_session);
350  return wrote;
351  }
352 
353  deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max);
354  read = BIO_read (oc->rbio, svm_fifo_tail (f), deq_now);
355  if (read <= 0)
356  {
357  tls_add_vpp_q_builtin_tx_evt (app_session);
358  return wrote;
359  }
360 
361  svm_fifo_enqueue_nocopy (f, read);
362  tls_add_vpp_q_tx_evt (tls_session);
363 
364  if (read < enq_max && BIO_ctrl_pending (oc->rbio) > 0)
365  {
366  deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max - read);
367  read = BIO_read (oc->rbio, svm_fifo_tail (f), deq_now);
368  if (read > 0)
369  svm_fifo_enqueue_nocopy (f, read);
370  }
371 
372  if (BIO_ctrl_pending (oc->rbio) > 0)
373  tls_add_vpp_q_builtin_tx_evt (app_session);
374 
375  return wrote;
376 }
377 
378 static inline int
380 {
381  int read, wrote = 0, max_space, max_buf = 100 * TLS_CHUNK_SIZE, rv;
382  openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
383  u32 deq_max, enq_max, deq_now, to_read;
384  stream_session_t *app_session;
385  svm_fifo_t *f;
386 
387  if (PREDICT_FALSE (SSL_in_init (oc->ssl)))
388  {
389  openssl_ctx_handshake_rx (ctx, tls_session);
390  return 0;
391  }
392 
393  f = tls_session->server_rx_fifo;
394  deq_max = svm_fifo_max_dequeue (f);
395  max_space = max_buf - BIO_ctrl_pending (oc->wbio);
396  max_space = max_space < 0 ? 0 : max_space;
397  deq_now = clib_min (deq_max, max_space);
398  if (!deq_now)
399  goto check_app_fifo;
400 
401  to_read = clib_min (svm_fifo_max_read_chunk (f), deq_now);
402  wrote = BIO_write (oc->wbio, svm_fifo_head (f), to_read);
403  if (wrote <= 0)
404  {
405  tls_add_vpp_q_builtin_rx_evt (tls_session);
406  goto check_app_fifo;
407  }
408  svm_fifo_dequeue_drop (f, wrote);
409  if (wrote < deq_now)
410  {
411  to_read = clib_min (svm_fifo_max_read_chunk (f), deq_now - wrote);
412  rv = BIO_write (oc->wbio, svm_fifo_head (f), to_read);
413  if (rv > 0)
414  {
415  svm_fifo_dequeue_drop (f, rv);
416  wrote += rv;
417  }
418  }
419  if (svm_fifo_max_dequeue (f))
420  tls_add_vpp_q_builtin_rx_evt (tls_session);
421 
422 check_app_fifo:
423 
424  if (BIO_ctrl_pending (oc->wbio) <= 0)
425  return wrote;
426 
427  app_session = session_get_from_handle (ctx->app_session_handle);
428  f = app_session->server_rx_fifo;
429  enq_max = svm_fifo_max_enqueue (f);
430  if (!enq_max)
431  {
432  tls_add_vpp_q_builtin_rx_evt (tls_session);
433  return wrote;
434  }
435 
436  deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max);
437  read = SSL_read (oc->ssl, svm_fifo_tail (f), deq_now);
438  if (read <= 0)
439  {
440  tls_add_vpp_q_builtin_rx_evt (tls_session);
441  return wrote;
442  }
443  svm_fifo_enqueue_nocopy (f, read);
444  if (read < enq_max && BIO_ctrl_pending (oc->wbio) > 0)
445  {
446  deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max - read);
447  read = SSL_read (oc->ssl, svm_fifo_tail (f), deq_now);
448  if (read > 0)
449  svm_fifo_enqueue_nocopy (f, read);
450  }
451 
452  tls_notify_app_enqueue (ctx, app_session);
453  if (BIO_ctrl_pending (oc->wbio) > 0)
454  tls_add_vpp_q_builtin_rx_evt (tls_session);
455 
456  return wrote;
457 }
458 
459 static int
461 {
462  long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
463  openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
465  stream_session_t *tls_session;
466  const SSL_METHOD *method;
467  int rv, err;
468 #ifdef HAVE_OPENSSL_ASYNC
469  openssl_resume_handler *handler;
470 #endif
471 
472  method = SSLv23_client_method ();
473  if (method == NULL)
474  {
475  TLS_DBG (1, "SSLv23_method returned null");
476  return -1;
477  }
478 
479  oc->ssl_ctx = SSL_CTX_new (method);
480  if (oc->ssl_ctx == NULL)
481  {
482  TLS_DBG (1, "SSL_CTX_new returned null");
483  return -1;
484  }
485 
486  SSL_CTX_set_ecdh_auto (oc->ssl_ctx, 1);
487  SSL_CTX_set_mode (oc->ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
488 #ifdef HAVE_OPENSSL_ASYNC
489  if (om->async)
490  SSL_CTX_set_mode (oc->ssl_ctx, SSL_MODE_ASYNC);
491 #endif
492  rv = SSL_CTX_set_cipher_list (oc->ssl_ctx, (const char *) om->ciphers);
493  if (rv != 1)
494  {
495  TLS_DBG (1, "Couldn't set cipher");
496  return -1;
497  }
498 
499  SSL_CTX_set_options (oc->ssl_ctx, flags);
500  SSL_CTX_set_cert_store (oc->ssl_ctx, om->cert_store);
501 
502  oc->ssl = SSL_new (oc->ssl_ctx);
503  if (oc->ssl == NULL)
504  {
505  TLS_DBG (1, "Couldn't initialize ssl struct");
506  return -1;
507  }
508 
509  oc->rbio = BIO_new (BIO_s_mem ());
510  oc->wbio = BIO_new (BIO_s_mem ());
511 
512  BIO_set_mem_eof_return (oc->rbio, -1);
513  BIO_set_mem_eof_return (oc->wbio, -1);
514 
515  SSL_set_bio (oc->ssl, oc->wbio, oc->rbio);
516  SSL_set_connect_state (oc->ssl);
517 
518  rv = SSL_set_tlsext_host_name (oc->ssl, ctx->srv_hostname);
519  if (rv != 1)
520  {
521  TLS_DBG (1, "Couldn't set hostname");
522  return -1;
523  }
524 
525  /*
526  * 2. Do the first steps in the handshake.
527  */
528  TLS_DBG (1, "Initiating handshake for [%u]%u", ctx->c_thread_index,
529  oc->openssl_ctx_index);
530 
531  tls_session = session_get_from_handle (ctx->tls_session_handle);
532  while (1)
533  {
534  rv = SSL_do_handshake (oc->ssl);
535  err = SSL_get_error (oc->ssl, rv);
536  openssl_try_handshake_write (oc, tls_session);
537 #ifdef HAVE_OPENSSL_ASYNC
538  if (err == SSL_ERROR_WANT_ASYNC)
539  {
541  vpp_ssl_async_process_event (ctx, handler);
542  break;
543  }
544 #endif
545  if (err != SSL_ERROR_WANT_WRITE)
546  break;
547  }
548 
549  TLS_DBG (2, "tls state for [%u]%u is su", ctx->c_thread_index,
550  oc->openssl_ctx_index, SSL_state_string_long (oc->ssl));
551  return 0;
552 }
553 
554 static int
556 {
557  application_t *app;
558  const SSL_METHOD *method;
559  SSL_CTX *ssl_ctx;
560  int rv;
561  BIO *cert_bio;
562  X509 *srvcert;
563  EVP_PKEY *pkey;
564  u32 olc_index;
566 
567  long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
569 
570  app = application_get (lctx->parent_app_index);
571  if (!app->tls_cert || !app->tls_key)
572  {
573  TLS_DBG (1, "tls cert and/or key not configured %d",
574  lctx->parent_app_index);
575  return -1;
576  }
577 
578  method = SSLv23_method ();
579  ssl_ctx = SSL_CTX_new (method);
580  if (!ssl_ctx)
581  {
582  clib_warning ("Unable to create SSL context");
583  return -1;
584  }
585 
586  SSL_CTX_set_mode (ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
587 #ifdef HAVE_OPENSSL_ASYNC
588  if (om->async)
589  SSL_CTX_set_mode (ssl_ctx, SSL_MODE_ASYNC);
590 #endif
591  SSL_CTX_set_options (ssl_ctx, flags);
592  SSL_CTX_set_ecdh_auto (ssl_ctx, 1);
593 
594  rv = SSL_CTX_set_cipher_list (ssl_ctx, (const char *) om->ciphers);
595  if (rv != 1)
596  {
597  TLS_DBG (1, "Couldn't set cipher");
598  return -1;
599  }
600 
601  /*
602  * Set the key and cert
603  */
604  cert_bio = BIO_new (BIO_s_mem ());
605  BIO_write (cert_bio, app->tls_cert, vec_len (app->tls_cert));
606  srvcert = PEM_read_bio_X509 (cert_bio, NULL, NULL, NULL);
607  if (!srvcert)
608  {
609  clib_warning ("unable to parse certificate");
610  return -1;
611  }
612  SSL_CTX_use_certificate (ssl_ctx, srvcert);
613  BIO_free (cert_bio);
614 
615  cert_bio = BIO_new (BIO_s_mem ());
616  BIO_write (cert_bio, app->tls_key, vec_len (app->tls_key));
617  pkey = PEM_read_bio_PrivateKey (cert_bio, NULL, NULL, NULL);
618  if (!pkey)
619  {
620  clib_warning ("unable to parse pkey");
621  return -1;
622  }
623  SSL_CTX_use_PrivateKey (ssl_ctx, pkey);
624  BIO_free (cert_bio);
625 
626  olc_index = openssl_listen_ctx_alloc ();
627  olc = openssl_lctx_get (olc_index);
628  olc->ssl_ctx = ssl_ctx;
629  olc->srvcert = srvcert;
630  olc->pkey = pkey;
631 
632  /* store SSL_CTX into TLS level structure */
633  lctx->tls_ssl_ctx = olc_index;
634 
635  return 0;
636 
637 }
638 
639 static int
641 {
642  u32 olc_index;
644 
645  olc_index = lctx->tls_ssl_ctx;
646  olc = openssl_lctx_get (olc_index);
647 
648  X509_free (olc->srvcert);
649  EVP_PKEY_free (olc->pkey);
650 
651  SSL_CTX_free (olc->ssl_ctx);
653 
654  return 0;
655 }
656 
657 static int
659 {
660  openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
661  u32 olc_index = ctx->tls_ssl_ctx;
663  stream_session_t *tls_session;
664  int rv, err;
665 #ifdef HAVE_OPENSSL_ASYNC
666  openssl_resume_handler *handler;
667 #endif
668 
669  /* Start a new connection */
670 
671  olc = openssl_lctx_get (olc_index);
672  oc->ssl = SSL_new (olc->ssl_ctx);
673  if (oc->ssl == NULL)
674  {
675  TLS_DBG (1, "Couldn't initialize ssl struct");
676  return -1;
677  }
678 
679  oc->rbio = BIO_new (BIO_s_mem ());
680  oc->wbio = BIO_new (BIO_s_mem ());
681 
682  BIO_set_mem_eof_return (oc->rbio, -1);
683  BIO_set_mem_eof_return (oc->wbio, -1);
684 
685  SSL_set_bio (oc->ssl, oc->wbio, oc->rbio);
686  SSL_set_accept_state (oc->ssl);
687 
688  TLS_DBG (1, "Initiating handshake for [%u]%u", ctx->c_thread_index,
689  oc->openssl_ctx_index);
690 
691  tls_session = session_get_from_handle (ctx->tls_session_handle);
692  while (1)
693  {
694  rv = SSL_do_handshake (oc->ssl);
695  err = SSL_get_error (oc->ssl, rv);
696  openssl_try_handshake_write (oc, tls_session);
697 #ifdef HAVE_OPENSSL_ASYNC
698  if (err == SSL_ERROR_WANT_ASYNC)
699  {
701  vpp_ssl_async_process_event (ctx, handler);
702  break;
703  }
704 #endif
705  if (err != SSL_ERROR_WANT_WRITE)
706  break;
707  }
708 
709  TLS_DBG (2, "tls state for [%u]%u is su", ctx->c_thread_index,
710  oc->openssl_ctx_index, SSL_state_string_long (oc->ssl));
711  return 0;
712 }
713 
714 static u8
716 {
717  openssl_ctx_t *mc = (openssl_ctx_t *) ctx;
718  if (!mc->ssl)
719  return 0;
720  return SSL_is_init_finished (mc->ssl);
721 }
722 
723 const static tls_engine_vft_t openssl_engine = {
725  .ctx_free = openssl_ctx_free,
726  .ctx_get = openssl_ctx_get,
727  .ctx_get_w_thread = openssl_ctx_get_w_thread,
728  .ctx_init_server = openssl_ctx_init_server,
729  .ctx_init_client = openssl_ctx_init_client,
730  .ctx_write = openssl_ctx_write,
731  .ctx_read = openssl_ctx_read,
732  .ctx_handshake_is_over = openssl_handshake_is_over,
733  .ctx_start_listen = openssl_start_listen,
734  .ctx_stop_listen = openssl_stop_listen,
735 };
736 
737 int
739 {
741  tls_main_t *tm = vnet_tls_get_main ();
742  BIO *cert_bio;
743  X509 *testcert;
744  int rv;
745 
746  if (access (tm->ca_cert_path, F_OK | R_OK) == -1)
747  {
748  clib_warning ("Could not initialize TLS CA certificates");
749  return -1;
750  }
751 
752  if (!(om->cert_store = X509_STORE_new ()))
753  {
754  clib_warning ("failed to create cert store");
755  return -1;
756  }
757 
758  rv = X509_STORE_load_locations (om->cert_store, tm->ca_cert_path, 0);
759  if (rv < 0)
760  {
761  clib_warning ("failed to load ca certificate");
762  }
763 
764  if (tm->use_test_cert_in_ca)
765  {
766  cert_bio = BIO_new (BIO_s_mem ());
767  BIO_write (cert_bio, test_srv_crt_rsa, test_srv_crt_rsa_len);
768  testcert = PEM_read_bio_X509 (cert_bio, NULL, NULL, NULL);
769  if (!testcert)
770  {
771  clib_warning ("unable to parse certificate");
772  return -1;
773  }
774  X509_STORE_add_cert (om->cert_store, testcert);
775  rv = 0;
776  }
777  return (rv < 0 ? -1 : 0);
778 }
779 
780 static int
781 tls_openssl_set_ciphers (char *ciphers)
782 {
784  int i;
785 
786  if (!ciphers)
787  {
788  return -1;
789  }
790 
791  vec_validate (om->ciphers, strlen (ciphers) - 1);
792  for (i = 0; i < vec_len (om->ciphers); i++)
793  {
794  om->ciphers[i] = toupper (ciphers[i]);
795  }
796 
797  return 0;
798 
799 }
800 
801 static clib_error_t *
803 {
806  clib_error_t *error;
807  u32 num_threads;
808 
809  num_threads = 1 /* main thread */ + vtm->n_threads;
810 
811  if ((error = vlib_call_init_function (vm, tls_init)))
812  return error;
813 
814  SSL_library_init ();
815  SSL_load_error_strings ();
816 
817  if (tls_init_ca_chain ())
818  {
819  clib_warning ("failed to initialize TLS CA chain");
820  return 0;
821  }
822 
823  vec_validate (om->ctx_pool, num_threads - 1);
824 
825  tls_register_engine (&openssl_engine, TLS_ENGINE_OPENSSL);
826 
827  om->engine_init = 0;
828 
829  /* default ciphers */
831  ("ALL:!ADH:!LOW:!EXP:!MD5:!RC4-SHA:!DES-CBC3-SHA:@STRENGTH");
832 
833  return 0;
834 }
835 
836 #ifdef HAVE_OPENSSL_ASYNC
837 static clib_error_t *
838 tls_openssl_set_command_fn (vlib_main_t * vm, unformat_input_t * input,
839  vlib_cli_command_t * cmd)
840 {
842  char *engine_name = NULL;
843  char *engine_alg = NULL;
844  char *ciphers = NULL;
845  u8 engine_name_set = 0;
846  int i;
847 
848  /* By present, it is not allowed to configure engine again after running */
849  if (om->engine_init)
850  {
851  clib_warning ("engine has started!\n");
852  return clib_error_return
853  (0, "engine has started, and no config is accepted");
854  }
855 
857  {
858  if (unformat (input, "engine %s", &engine_name))
859  {
860  engine_name_set = 1;
861  }
862  else if (unformat (input, "async"))
863  {
864  om->async = 1;
866  }
867  else if (unformat (input, "alg %s", &engine_alg))
868  {
869  for (i = 0; i < strnlen (engine_alg, MAX_CRYPTO_LEN); i++)
870  engine_alg[i] = toupper (engine_alg[i]);
871  }
872  else if (unformat (input, "ciphers %s", &ciphers))
873  {
874  tls_openssl_set_ciphers (ciphers);
875  }
876  else
877  return clib_error_return (0, "failed: unknown input `%U'",
878  format_unformat_error, input);
879  }
880 
881  /* reset parameters if engine is not configured */
882  if (!engine_name_set)
883  {
884  clib_warning ("No engine provided! \n");
885  om->async = 0;
886  }
887  else
888  {
889  if (!openssl_engine_register (engine_name, engine_alg))
890  {
891  return clib_error_return (0, "failed to register %s polling",
892  engine_name);
893  }
894  }
895 
896  return 0;
897 }
898 
899 /* *INDENT-OFF* */
900 VLIB_CLI_COMMAND (tls_openssl_set_command, static) =
901 {
902  .path = "tls openssl set",
903  .short_help = "tls openssl set [engine <engine name>] [alg [algorithm] [async]",
904  .function = tls_openssl_set_command_fn,
905 };
906 /* *INDENT-ON* */
907 #endif
908 
909 
911 
912 /* *INDENT-OFF* */
914  .version = VPP_BUILD_VER,
915  .description = "openssl based TLS Engine",
916 };
917 /* *INDENT-ON* */
918 
919 /*
920  * fd.io coding-style-patch-verification: ON
921  *
922  * Local Variables:
923  * eval: (c-set-style "gnu")
924  * End:
925  */
tls_main_t * vnet_tls_get_main(void)
Definition: tls.c:824
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:437
openssl_listen_ctx_t * lctx_pool
Definition: tls_openssl.h:46
static int tls_openssl_set_ciphers(char *ciphers)
Definition: tls_openssl.c:781
#define clib_min(x, y)
Definition: clib.h:291
static u8 openssl_handshake_is_over(tls_ctx_t *ctx)
Definition: tls_openssl.c:715
X509_STORE * cert_store
Definition: tls_openssl.h:48
static int openssl_try_handshake_write(openssl_ctx_t *oc, stream_session_t *tls_session)
Definition: tls_openssl.c:140
const u32 test_srv_crt_rsa_len
char * ca_cert_path
Definition: tls.h:90
#define NULL
Definition: clib.h:57
void openssl_async_node_enable_disable(u8 is_en)
Definition: tls_async.c:443
static u32 openssl_ctx_alloc(void)
Definition: tls_openssl.c:33
static void openssl_listen_ctx_free(openssl_listen_ctx_t *lctx)
Definition: tls_openssl.c:96
int i
static void openssl_ctx_free(tls_ctx_t *ctx)
Definition: tls_openssl.c:52
void tls_notify_app_enqueue(tls_ctx_t *ctx, stream_session_t *app_session)
Definition: tls.c:181
u8 use_test_cert_in_ca
Definition: tls.h:89
static u32 svm_fifo_max_enqueue(svm_fifo_t *f)
Definition: svm_fifo.h:132
int(* callback)(void *arg)
Definition: tls_openssl.h:56
static stream_session_t * session_get_from_handle(session_handle_t handle)
Definition: session.h:360
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:228
static u8 * svm_fifo_tail(svm_fifo_t *f)
Definition: svm_fifo.h:260
unsigned char u8
Definition: types.h:56
struct _svm_fifo svm_fifo_t
static void svm_fifo_enqueue_nocopy(svm_fifo_t *f, u32 bytes)
Advance tail pointer.
Definition: svm_fifo.h:246
memset(h->entries, 0, sizeof(h->entries[0])*entries)
SSL_CTX * ssl_ctx
Definition: tls_openssl.h:28
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
int tls_add_vpp_q_builtin_tx_evt(stream_session_t *s)
Definition: tls.c:78
static u32 svm_fifo_max_dequeue(svm_fifo_t *f)
Definition: svm_fifo.h:114
u8 is_passive_close
Definition: tls.h:72
int openssl_engine_register(char *engine_name, char *algorithm)
Definition: tls_async.c:127
#define MAX_CRYPTO_LEN
Definition: tls_openssl.c:29
#define clib_error_return(e, args...)
Definition: error.h:99
static int openssl_stop_listen(tls_ctx_t *lctx)
Definition: tls_openssl.c:640
static u32 svm_fifo_max_write_chunk(svm_fifo_t *f)
Max contiguous chunk of data that can be written.
Definition: svm_fifo.h:235
unsigned int u32
Definition: types.h:88
struct _stream_session_t stream_session_t
#define vlib_call_init_function(vm, x)
Definition: init.h:260
static u8 * svm_fifo_head(svm_fifo_t *f)
Definition: svm_fifo.h:254
static u32 svm_fifo_max_read_chunk(svm_fifo_t *f)
Max contiguous chunk of data that can be read.
Definition: svm_fifo.h:226
int tls_init_ca_chain(void)
Definition: tls_openssl.c:738
#define TLS_CHUNK_SIZE
Definition: tls.h:27
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:464
int vpp_add_async_run_event(tls_ctx_t *ctx, openssl_resume_handler *handler)
Definition: tls_async.c:315
long ctx[MAX_CONNS]
Definition: main.c:144
struct _unformat_input_t unformat_input_t
int tls_add_vpp_q_tx_evt(stream_session_t *s)
Definition: tls.c:70
Definition: tls.h:77
int tls_notify_app_connected(tls_ctx_t *ctx, u8 is_failed)
Definition: tls.c:232
#define PREDICT_FALSE(x)
Definition: clib.h:107
tls_ctx_t * openssl_ctx_get(u32 ctx_index)
Definition: tls_openssl.c:66
int openssl_resume_handler(tls_ctx_t *ctx, stream_session_t *tls_session)
Definition: tls_openssl.h:60
const char test_srv_crt_rsa[]
Definition: tls.h:53
u32(* ctx_alloc)(void)
Definition: tls.h:95
static int openssl_ctx_write(tls_ctx_t *ctx, stream_session_t *app_session)
Definition: tls_openssl.c:301
u32 flags
Definition: vhost_user.h:115
static int openssl_ctx_init_client(tls_ctx_t *ctx)
Definition: tls_openssl.c:460
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:211
openssl_ctx_t *** ctx_pool
Definition: tls_openssl.h:45
#define clib_warning(format, args...)
Definition: error.h:59
#define SESSION_INVALID_HANDLE
Definition: session_table.h:59
static openssl_main_t openssl_main
Definition: tls_openssl.c:31
void tls_register_engine(const tls_engine_vft_t *vft, tls_engine_type_t type)
Definition: tls.c:746
application_t * application_get(u32 app_index)
Definition: application.c:243
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:155
#define pool_put_index(p, i)
Free pool element with given index.
Definition: pool.h:299
static clib_error_t * tls_openssl_init(vlib_main_t *vm)
Definition: tls_openssl.c:802
u8 * tls_key
PEM encoded key.
Definition: application.h:164
static int openssl_try_handshake_read(openssl_ctx_t *oc, stream_session_t *tls_session)
Definition: tls_openssl.c:108
static void * clib_mem_alloc(uword size)
Definition: mem.h:132
tls_ctx_t * openssl_ctx_get_w_thread(u32 ctx_index, u8 thread_index)
Definition: tls_openssl.c:75
static int openssl_ctx_init_server(tls_ctx_t *ctx)
Definition: tls_openssl.c:658
int svm_fifo_dequeue_drop(svm_fifo_t *f, u32 max_bytes)
Definition: svm_fifo.c:726
openssl_tls_callback_t * vpp_add_async_pending_event(tls_ctx_t *ctx, openssl_resume_handler *handler)
Definition: tls_async.c:285
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
int tls_add_vpp_q_builtin_rx_evt(stream_session_t *s)
Definition: tls.c:62
static int openssl_start_listen(tls_ctx_t *lctx)
Definition: tls_openssl.c:555
u8 * srv_hostname
Definition: tls.h:74
VLIB_PLUGIN_REGISTER()
int tls_notify_app_accept(tls_ctx_t *ctx)
Definition: tls.c:190
u8 resume
Definition: tls.h:73
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
u8 * tls_cert
Certificate to be used for listen sessions.
Definition: application.h:161
openssl_listen_ctx_t * openssl_lctx_get(u32 lctx_index)
Definition: tls_openssl.c:102
static u32 openssl_listen_ctx_alloc(void)
Definition: tls_openssl.c:83
static int openssl_ctx_read(tls_ctx_t *ctx, stream_session_t *tls_session)
Definition: tls_openssl.c:379
int openssl_ctx_handshake_rx(tls_ctx_t *ctx, stream_session_t *tls_session)
Definition: tls_openssl.c:213
static clib_error_t * tls_init(vlib_main_t *vm)
Definition: tls.c:753
#define TLS_DBG(_lvl, _fmt, _args...)
Definition: tls.h:35
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170