FD.io VPP  v19.08.3-2-gbabecb413
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  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);
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  vec_free (ctx->srv_hostname);
62  pool_put_index (openssl_main.ctx_pool[ctx->c_thread_index],
63  oc->openssl_ctx_index);
64 }
65 
66 tls_ctx_t *
67 openssl_ctx_get (u32 ctx_index)
68 {
70  ctx = pool_elt_at_index (openssl_main.ctx_pool[vlib_get_thread_index ()],
71  ctx_index);
72  return &(*ctx)->ctx;
73 }
74 
75 tls_ctx_t *
76 openssl_ctx_get_w_thread (u32 ctx_index, u8 thread_index)
77 {
79  ctx = pool_elt_at_index (openssl_main.ctx_pool[thread_index], ctx_index);
80  return &(*ctx)->ctx;
81 }
82 
83 static u32
85 {
88 
89  pool_get (om->lctx_pool, lctx);
90 
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;
94 }
95 
96 static void
98 {
99  pool_put_index (openssl_main.lctx_pool, lctx->openssl_lctx_index);
100 }
101 
103 openssl_lctx_get (u32 lctx_index)
104 {
105  return pool_elt_at_index (openssl_main.lctx_pool, lctx_index);
106 }
107 
108 static int
110 {
111  u32 deq_max, deq_now;
112  svm_fifo_t *f;
113  int wrote, rv;
114 
115  f = tls_session->rx_fifo;
116  deq_max = svm_fifo_max_dequeue_cons (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 {
142  u32 enq_max, deq_now;
143  svm_fifo_t *f;
144  int read, rv;
145 
146  if (BIO_ctrl_pending (oc->rbio) <= 0)
147  return 0;
148 
149  f = tls_session->tx_fifo;
150  enq_max = svm_fifo_max_enqueue_prod (f);
151  if (!enq_max)
152  return 0;
153 
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);
156  if (read <= 0)
157  return 0;
158 
159  svm_fifo_enqueue_nocopy (f, read);
160  tls_add_vpp_q_tx_evt (tls_session);
161 
162  if (read < enq_max)
163  {
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);
166  if (rv > 0)
167  {
168  svm_fifo_enqueue_nocopy (f, rv);
169  read += rv;
170  }
171  }
172 
173  return read;
174 }
175 
176 #ifdef HAVE_OPENSSL_ASYNC
177 static int
178 vpp_ssl_async_process_event (tls_ctx_t * ctx,
179  openssl_resume_handler * handler)
180 {
181  openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
182  openssl_tls_callback_t *engine_cb;
183 
184  engine_cb = vpp_add_async_pending_event (ctx, handler);
185  if (engine_cb)
186  {
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);
189  }
190  return 0;
191 
192 }
193 
194 /* Due to engine busy stat, VPP need to retry later */
195 static int
196 vpp_ssl_async_retry_func (tls_ctx_t * ctx, openssl_resume_handler * handler)
197 {
198 
199  if (vpp_add_async_run_event (ctx, handler))
200  return 1;
201 
202  return 0;
203 
204 }
205 
206 #endif
207 
208 static void
210 {
211  if (SSL_is_server (((openssl_ctx_t *) ctx)->ssl))
212  {
213  /*
214  * Cleanup pre-allocated app session and close transport
215  */
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;
220  }
221  else
222  {
223  /*
224  * Also handles cleanup of the pre-allocated session
225  */
226  tls_notify_app_connected (ctx, /* is failed */ 1);
227  }
228 }
229 
230 int
232 {
233  openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
234  int rv = 0, err;
235 #ifdef HAVE_OPENSSL_ASYNC
236  int estatus;
237  openssl_resume_handler *myself;
238 #endif
239 
240  while (SSL_in_init (oc->ssl))
241  {
242  if (ctx->resume)
243  {
244  ctx->resume = 0;
245  }
246  else if (!openssl_try_handshake_read (oc, tls_session))
247  {
248  break;
249  }
250 
251 #ifdef HAVE_OPENSSL_ASYNC
252  myself = openssl_ctx_handshake_rx;
253  vpp_ssl_async_process_event (ctx, myself);
254 #endif
255 
256  rv = SSL_do_handshake (oc->ssl);
257  err = SSL_get_error (oc->ssl, rv);
258 
259  if (err == SSL_ERROR_SSL)
260  {
261  char buf[512];
262  ERR_error_string (ERR_get_error (), buf);
263  clib_warning ("Err: %s", buf);
264 
266  return -1;
267  }
268 
269  openssl_try_handshake_write (oc, tls_session);
270 #ifdef HAVE_OPENSSL_ASYNC
271  if (err == SSL_ERROR_WANT_ASYNC)
272  {
273  SSL_get_async_status (oc->ssl, &estatus);
274 
275  if (estatus == ASYNC_STATUS_EAGAIN)
276  {
277  vpp_ssl_async_retry_func (ctx, myself);
278  }
279  }
280 #endif
281 
282  if (err != SSL_ERROR_WANT_WRITE)
283  break;
284  }
285  TLS_DBG (2, "tls state for %u is %s", oc->openssl_ctx_index,
286  SSL_state_string_long (oc->ssl));
287 
288  if (SSL_in_init (oc->ssl))
289  return 0;
290 
291  /*
292  * Handshake complete
293  */
294  if (!SSL_is_server (oc->ssl))
295  {
296  /*
297  * Verify server certificate
298  */
299  if ((rv = SSL_get_verify_result (oc->ssl)) != X509_V_OK)
300  {
301  TLS_DBG (1, " failed verify: %s\n",
302  X509_verify_cert_error_string (rv));
303 
304  /*
305  * Presence of hostname enforces strict certificate verification
306  */
307  if (ctx->srv_hostname)
308  {
309  tls_notify_app_connected (ctx, /* is failed */ 0);
310  return -1;
311  }
312  }
313  tls_notify_app_connected (ctx, /* is failed */ 0);
314  }
315  else
316  {
317  tls_notify_app_accept (ctx);
318  }
319 
320  TLS_DBG (1, "Handshake for %u complete. TLS cipher is %s",
321  oc->openssl_ctx_index, SSL_get_cipher (oc->ssl));
322  return rv;
323 }
324 
325 static void
327 {
330 }
331 
332 static inline int
333 openssl_ctx_write (tls_ctx_t * ctx, session_t * app_session)
334 {
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;
339  svm_fifo_t *f;
340 
341  f = app_session->tx_fifo;
342  deq_max = svm_fifo_max_dequeue_cons (f);
343  if (!deq_max)
344  goto check_tls_fifo;
345 
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);
351  if (wrote <= 0)
352  {
353  tls_add_vpp_q_builtin_tx_evt (app_session);
354  goto check_tls_fifo;
355  }
356  svm_fifo_dequeue_drop (app_session->tx_fifo, wrote);
357  if (wrote < deq_now)
358  {
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);
361  if (rv > 0)
362  {
363  svm_fifo_dequeue_drop (app_session->tx_fifo, rv);
364  wrote += rv;
365  }
366  }
367 
368  if (svm_fifo_needs_deq_ntf (app_session->tx_fifo, wrote))
369  session_dequeue_notify (app_session);
370 
371  if (wrote < deq_max)
372  tls_add_vpp_q_builtin_tx_evt (app_session);
373 
374 check_tls_fifo:
375 
376  if (BIO_ctrl_pending (oc->rbio) <= 0)
377  return wrote;
378 
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);
382  if (!enq_max)
383  {
384  tls_add_vpp_q_builtin_tx_evt (app_session);
385  return wrote;
386  }
387 
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);
390  if (read <= 0)
391  {
392  tls_add_vpp_q_builtin_tx_evt (app_session);
393  return wrote;
394  }
395 
396  svm_fifo_enqueue_nocopy (f, read);
397  tls_add_vpp_q_tx_evt (tls_session);
398 
399  if (read < enq_max && BIO_ctrl_pending (oc->rbio) > 0)
400  {
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);
403  if (read > 0)
404  svm_fifo_enqueue_nocopy (f, read);
405  }
406 
407  if (BIO_ctrl_pending (oc->rbio) > 0)
408  tls_add_vpp_q_builtin_tx_evt (app_session);
409  else if (ctx->app_closed)
411 
412  return wrote;
413 }
414 
415 static inline int
416 openssl_ctx_read (tls_ctx_t * ctx, session_t * tls_session)
417 {
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;
422  svm_fifo_t *f;
423 
424  if (PREDICT_FALSE (SSL_in_init (oc->ssl)))
425  {
426  if (openssl_ctx_handshake_rx (ctx, tls_session) < 0)
427  return 0;
428  else
429  goto check_app_fifo;
430  }
431 
432  f = tls_session->rx_fifo;
433  deq_max = svm_fifo_max_dequeue_cons (f);
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);
437  if (!deq_now)
438  goto check_app_fifo;
439 
440  to_read = clib_min (svm_fifo_max_read_chunk (f), deq_now);
441  wrote = BIO_write (oc->wbio, svm_fifo_head (f), to_read);
442  if (wrote <= 0)
443  {
444  tls_add_vpp_q_builtin_rx_evt (tls_session);
445  goto check_app_fifo;
446  }
447  svm_fifo_dequeue_drop (f, wrote);
448  if (wrote < deq_now)
449  {
450  to_read = clib_min (svm_fifo_max_read_chunk (f), deq_now - wrote);
451  rv = BIO_write (oc->wbio, svm_fifo_head (f), to_read);
452  if (rv > 0)
453  {
454  svm_fifo_dequeue_drop (f, rv);
455  wrote += rv;
456  }
457  }
459  tls_add_vpp_q_builtin_rx_evt (tls_session);
460 
461 check_app_fifo:
462 
463  if (BIO_ctrl_pending (oc->wbio) <= 0)
464  return wrote;
465 
466  app_session = session_get_from_handle (ctx->app_session_handle);
467  f = app_session->rx_fifo;
468  enq_max = svm_fifo_max_enqueue_prod (f);
469  if (!enq_max)
470  {
471  tls_add_vpp_q_builtin_rx_evt (tls_session);
472  return wrote;
473  }
474 
475  deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max);
476  read = SSL_read (oc->ssl, svm_fifo_tail (f), deq_now);
477  if (read <= 0)
478  {
479  tls_add_vpp_q_builtin_rx_evt (tls_session);
480  return wrote;
481  }
482  svm_fifo_enqueue_nocopy (f, read);
483  if (read < enq_max && SSL_pending (oc->ssl) > 0)
484  {
485  deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max - read);
486  read = SSL_read (oc->ssl, svm_fifo_tail (f), deq_now);
487  if (read > 0)
488  svm_fifo_enqueue_nocopy (f, read);
489  }
490 
491  /* If handshake just completed, session may still be in accepting state */
492  if (app_session->session_state >= SESSION_STATE_READY)
493  tls_notify_app_enqueue (ctx, app_session);
494  if (SSL_pending (oc->ssl) > 0)
495  tls_add_vpp_q_builtin_rx_evt (tls_session);
496 
497  return wrote;
498 }
499 
500 static int
502 {
503  long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
504  openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
506  session_t *tls_session;
507  const SSL_METHOD *method;
508  int rv, err;
509 #ifdef HAVE_OPENSSL_ASYNC
510  openssl_resume_handler *handler;
511 #endif
512 
513  method = SSLv23_client_method ();
514  if (method == NULL)
515  {
516  TLS_DBG (1, "SSLv23_method returned null");
517  return -1;
518  }
519 
520  oc->ssl_ctx = SSL_CTX_new (method);
521  if (oc->ssl_ctx == NULL)
522  {
523  TLS_DBG (1, "SSL_CTX_new returned null");
524  return -1;
525  }
526 
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
530  if (om->async)
531  SSL_CTX_set_mode (oc->ssl_ctx, SSL_MODE_ASYNC);
532 #endif
533  rv = SSL_CTX_set_cipher_list (oc->ssl_ctx, (const char *) om->ciphers);
534  if (rv != 1)
535  {
536  TLS_DBG (1, "Couldn't set cipher");
537  return -1;
538  }
539 
540  SSL_CTX_set_options (oc->ssl_ctx, flags);
541  SSL_CTX_set_cert_store (oc->ssl_ctx, om->cert_store);
542 
543  oc->ssl = SSL_new (oc->ssl_ctx);
544  if (oc->ssl == NULL)
545  {
546  TLS_DBG (1, "Couldn't initialize ssl struct");
547  return -1;
548  }
549 
550  oc->rbio = BIO_new (BIO_s_mem ());
551  oc->wbio = BIO_new (BIO_s_mem ());
552 
553  BIO_set_mem_eof_return (oc->rbio, -1);
554  BIO_set_mem_eof_return (oc->wbio, -1);
555 
556  SSL_set_bio (oc->ssl, oc->wbio, oc->rbio);
557  SSL_set_connect_state (oc->ssl);
558 
559  rv = SSL_set_tlsext_host_name (oc->ssl, ctx->srv_hostname);
560  if (rv != 1)
561  {
562  TLS_DBG (1, "Couldn't set hostname");
563  return -1;
564  }
565 
566  /*
567  * 2. Do the first steps in the handshake.
568  */
569  TLS_DBG (1, "Initiating handshake for [%u]%u", ctx->c_thread_index,
570  oc->openssl_ctx_index);
571 
572  tls_session = session_get_from_handle (ctx->tls_session_handle);
573  while (1)
574  {
575  rv = SSL_do_handshake (oc->ssl);
576  err = SSL_get_error (oc->ssl, rv);
577  openssl_try_handshake_write (oc, tls_session);
578 #ifdef HAVE_OPENSSL_ASYNC
579  if (err == SSL_ERROR_WANT_ASYNC)
580  {
582  vpp_ssl_async_process_event (ctx, handler);
583  break;
584  }
585 #endif
586  if (err != SSL_ERROR_WANT_WRITE)
587  break;
588  }
589 
590  TLS_DBG (2, "tls state for [%u]%u is su", ctx->c_thread_index,
591  oc->openssl_ctx_index, SSL_state_string_long (oc->ssl));
592  return 0;
593 }
594 
595 static int
597 {
598  application_t *app;
599  const SSL_METHOD *method;
600  SSL_CTX *ssl_ctx;
601  int rv;
602  BIO *cert_bio;
603  X509 *srvcert;
604  EVP_PKEY *pkey;
605  u32 olc_index;
607  app_worker_t *app_wrk;
608 
609  long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
611 
612  app_wrk = app_worker_get (lctx->parent_app_wrk_index);
613  if (!app_wrk)
614  return -1;
615 
616  app = application_get (app_wrk->app_index);
617  if (!app->tls_cert || !app->tls_key)
618  {
619  TLS_DBG (1, "tls cert and/or key not configured %d",
620  lctx->parent_app_wrk_index);
621  return -1;
622  }
623 
624  method = SSLv23_method ();
625  ssl_ctx = SSL_CTX_new (method);
626  if (!ssl_ctx)
627  {
628  clib_warning ("Unable to create SSL context");
629  return -1;
630  }
631 
632  SSL_CTX_set_mode (ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
633 #ifdef HAVE_OPENSSL_ASYNC
634  if (om->async)
635  SSL_CTX_set_mode (ssl_ctx, SSL_MODE_ASYNC);
636  SSL_CTX_set_async_callback (ssl_ctx, tls_async_openssl_callback);
637 #endif
638  SSL_CTX_set_options (ssl_ctx, flags);
639  SSL_CTX_set_ecdh_auto (ssl_ctx, 1);
640 
641  rv = SSL_CTX_set_cipher_list (ssl_ctx, (const char *) om->ciphers);
642  if (rv != 1)
643  {
644  TLS_DBG (1, "Couldn't set cipher");
645  return -1;
646  }
647 
648  /*
649  * Set the key and cert
650  */
651  cert_bio = BIO_new (BIO_s_mem ());
652  BIO_write (cert_bio, app->tls_cert, vec_len (app->tls_cert));
653  srvcert = PEM_read_bio_X509 (cert_bio, NULL, NULL, NULL);
654  if (!srvcert)
655  {
656  clib_warning ("unable to parse certificate");
657  return -1;
658  }
659  SSL_CTX_use_certificate (ssl_ctx, srvcert);
660  BIO_free (cert_bio);
661 
662  cert_bio = BIO_new (BIO_s_mem ());
663  BIO_write (cert_bio, app->tls_key, vec_len (app->tls_key));
664  pkey = PEM_read_bio_PrivateKey (cert_bio, NULL, NULL, NULL);
665  if (!pkey)
666  {
667  clib_warning ("unable to parse pkey");
668  return -1;
669  }
670  SSL_CTX_use_PrivateKey (ssl_ctx, pkey);
671  BIO_free (cert_bio);
672 
673  olc_index = openssl_listen_ctx_alloc ();
674  olc = openssl_lctx_get (olc_index);
675  olc->ssl_ctx = ssl_ctx;
676  olc->srvcert = srvcert;
677  olc->pkey = pkey;
678 
679  /* store SSL_CTX into TLS level structure */
680  lctx->tls_ssl_ctx = olc_index;
681 
682  return 0;
683 
684 }
685 
686 static int
688 {
689  u32 olc_index;
691 
692  olc_index = lctx->tls_ssl_ctx;
693  olc = openssl_lctx_get (olc_index);
694 
695  X509_free (olc->srvcert);
696  EVP_PKEY_free (olc->pkey);
697 
698  SSL_CTX_free (olc->ssl_ctx);
700 
701  return 0;
702 }
703 
704 static int
706 {
707  openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
708  u32 olc_index = ctx->tls_ssl_ctx;
710  session_t *tls_session;
711  int rv, err;
712 #ifdef HAVE_OPENSSL_ASYNC
713  openssl_resume_handler *handler;
714 #endif
715 
716  /* Start a new connection */
717 
718  olc = openssl_lctx_get (olc_index);
719  oc->ssl = SSL_new (olc->ssl_ctx);
720  if (oc->ssl == NULL)
721  {
722  TLS_DBG (1, "Couldn't initialize ssl struct");
723  return -1;
724  }
725 
726  oc->rbio = BIO_new (BIO_s_mem ());
727  oc->wbio = BIO_new (BIO_s_mem ());
728 
729  BIO_set_mem_eof_return (oc->rbio, -1);
730  BIO_set_mem_eof_return (oc->wbio, -1);
731 
732  SSL_set_bio (oc->ssl, oc->wbio, oc->rbio);
733  SSL_set_accept_state (oc->ssl);
734 
735  TLS_DBG (1, "Initiating handshake for [%u]%u", ctx->c_thread_index,
736  oc->openssl_ctx_index);
737 
738  tls_session = session_get_from_handle (ctx->tls_session_handle);
739  while (1)
740  {
741  rv = SSL_do_handshake (oc->ssl);
742  err = SSL_get_error (oc->ssl, rv);
743  openssl_try_handshake_write (oc, tls_session);
744 #ifdef HAVE_OPENSSL_ASYNC
745  if (err == SSL_ERROR_WANT_ASYNC)
746  {
748  vpp_ssl_async_process_event (ctx, handler);
749  break;
750  }
751 #endif
752  if (err != SSL_ERROR_WANT_WRITE)
753  break;
754  }
755 
756  TLS_DBG (2, "tls state for [%u]%u is su", ctx->c_thread_index,
757  oc->openssl_ctx_index, SSL_state_string_long (oc->ssl));
758  return 0;
759 }
760 
761 static u8
763 {
764  openssl_ctx_t *mc = (openssl_ctx_t *) ctx;
765  if (!mc->ssl)
766  return 0;
767  return SSL_is_init_finished (mc->ssl);
768 }
769 
770 static int
772 {
773  if (!openssl_handshake_is_over (ctx))
774  {
776  return 0;
777  }
779  return 0;
780 }
781 
782 static int
784 {
785  openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
786  session_t *app_session;
787 
788  /* Wait for all data to be written to tcp */
789  app_session = session_get_from_handle (ctx->app_session_handle);
790  if (BIO_ctrl_pending (oc->rbio) <= 0
791  && !svm_fifo_max_dequeue_cons (app_session->tx_fifo))
793  else
794  ctx->app_closed = 1;
795  return 0;
796 }
797 
798 const static tls_engine_vft_t openssl_engine = {
800  .ctx_free = openssl_ctx_free,
801  .ctx_get = openssl_ctx_get,
802  .ctx_get_w_thread = openssl_ctx_get_w_thread,
803  .ctx_init_server = openssl_ctx_init_server,
804  .ctx_init_client = openssl_ctx_init_client,
805  .ctx_write = openssl_ctx_write,
806  .ctx_read = openssl_ctx_read,
807  .ctx_handshake_is_over = openssl_handshake_is_over,
808  .ctx_start_listen = openssl_start_listen,
809  .ctx_stop_listen = openssl_stop_listen,
810  .ctx_transport_close = openssl_transport_close,
811  .ctx_app_close = openssl_app_close,
812 };
813 
814 int
816 {
818  tls_main_t *tm = vnet_tls_get_main ();
819  BIO *cert_bio;
820  X509 *testcert;
821  int rv;
822 
823  if (access (tm->ca_cert_path, F_OK | R_OK) == -1)
824  {
825  clib_warning ("Could not initialize TLS CA certificates");
826  return -1;
827  }
828 
829  if (!(om->cert_store = X509_STORE_new ()))
830  {
831  clib_warning ("failed to create cert store");
832  return -1;
833  }
834 
835 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
836  rv = X509_STORE_load_file (om->cert_store, tm->ca_cert_path);
837 #else
838  rv = X509_STORE_load_locations (om->cert_store, tm->ca_cert_path, 0);
839 #endif
840 
841  if (rv < 0)
842  {
843  clib_warning ("failed to load ca certificate");
844  }
845 
846  if (tm->use_test_cert_in_ca)
847  {
848  cert_bio = BIO_new (BIO_s_mem ());
849  BIO_write (cert_bio, test_srv_crt_rsa, test_srv_crt_rsa_len);
850  testcert = PEM_read_bio_X509 (cert_bio, NULL, NULL, NULL);
851  if (!testcert)
852  {
853  clib_warning ("unable to parse certificate");
854  return -1;
855  }
856  X509_STORE_add_cert (om->cert_store, testcert);
857  rv = 0;
858  }
859  return (rv < 0 ? -1 : 0);
860 }
861 
862 static int
863 tls_openssl_set_ciphers (char *ciphers)
864 {
866  int i;
867 
868  if (!ciphers)
869  {
870  return -1;
871  }
872 
873  vec_validate (om->ciphers, strlen (ciphers) - 1);
874  for (i = 0; i < vec_len (om->ciphers); i++)
875  {
876  om->ciphers[i] = toupper (ciphers[i]);
877  }
878 
879  return 0;
880 
881 }
882 
883 static clib_error_t *
885 {
888  u32 num_threads;
889 
890  num_threads = 1 /* main thread */ + vtm->n_threads;
891 
892  SSL_library_init ();
893  SSL_load_error_strings ();
894 
895  if (tls_init_ca_chain ())
896  {
897  clib_warning ("failed to initialize TLS CA chain");
898  return 0;
899  }
900 
901  vec_validate (om->ctx_pool, num_threads - 1);
902 
903  tls_register_engine (&openssl_engine, TLS_ENGINE_OPENSSL);
904 
905  om->engine_init = 0;
906 
907  /* default ciphers */
909  ("ALL:!ADH:!LOW:!EXP:!MD5:!RC4-SHA:!DES-CBC3-SHA:@STRENGTH");
910 
911  return 0;
912 }
913 /* *INDENT-OFF* */
915 {
916  .runs_after = VLIB_INITS("tls_init"),
917 };
918 /* *INDENT-ON* */
919 
920 #ifdef HAVE_OPENSSL_ASYNC
921 static clib_error_t *
922 tls_openssl_set_command_fn (vlib_main_t * vm, unformat_input_t * input,
923  vlib_cli_command_t * cmd)
924 {
926  char *engine_name = NULL;
927  char *engine_alg = NULL;
928  char *ciphers = NULL;
929  u8 engine_name_set = 0;
930  int i;
931 
932  /* By present, it is not allowed to configure engine again after running */
933  if (om->engine_init)
934  {
935  clib_warning ("engine has started!\n");
936  return clib_error_return
937  (0, "engine has started, and no config is accepted");
938  }
939 
941  {
942  if (unformat (input, "engine %s", &engine_name))
943  {
944  engine_name_set = 1;
945  }
946  else if (unformat (input, "async"))
947  {
948  om->async = 1;
950  }
951  else if (unformat (input, "alg %s", &engine_alg))
952  {
953  for (i = 0; i < strnlen (engine_alg, MAX_CRYPTO_LEN); i++)
954  engine_alg[i] = toupper (engine_alg[i]);
955  }
956  else if (unformat (input, "ciphers %s", &ciphers))
957  {
958  tls_openssl_set_ciphers (ciphers);
959  }
960  else
961  return clib_error_return (0, "failed: unknown input `%U'",
962  format_unformat_error, input);
963  }
964 
965  /* reset parameters if engine is not configured */
966  if (!engine_name_set)
967  {
968  clib_warning ("No engine provided! \n");
969  om->async = 0;
970  }
971  else
972  {
973  if (!openssl_engine_register (engine_name, engine_alg))
974  {
975  return clib_error_return (0, "failed to register %s polling",
976  engine_name);
977  }
978  }
979 
980  return 0;
981 }
982 
983 /* *INDENT-OFF* */
984 VLIB_CLI_COMMAND (tls_openssl_set_command, static) =
985 {
986  .path = "tls openssl set",
987  .short_help = "tls openssl set [engine <engine name>] [alg [algorithm] [async]",
988  .function = tls_openssl_set_command_fn,
989 };
990 /* *INDENT-ON* */
991 #endif
992 
993 /* *INDENT-OFF* */
995  .version = VPP_BUILD_VER,
996  .description = "Transport Layer Security (TLS) Engine, OpenSSL Based",
997 };
998 /* *INDENT-ON* */
999 
1000 /*
1001  * fd.io coding-style-patch-verification: ON
1002  *
1003  * Local Variables:
1004  * eval: (c-set-style "gnu")
1005  * End:
1006  */
tls_main_t * vnet_tls_get_main(void)
Definition: tls.c:920
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:439
openssl_listen_ctx_t * lctx_pool
Definition: tls_openssl.h:46
int(* callback)(SSL *ssl, void *arg)
Definition: tls_openssl.h:56
static int openssl_transport_close(tls_ctx_t *ctx)
Definition: tls_openssl.c:771
static int tls_openssl_set_ciphers(char *ciphers)
Definition: tls_openssl.c:863
u32 flags
Definition: vhost_user.h:141
#define clib_min(x, y)
Definition: clib.h:302
static u8 openssl_handshake_is_over(tls_ctx_t *ctx)
Definition: tls_openssl.c:762
X509_STORE * cert_store
Definition: tls_openssl.h:48
static u32 svm_fifo_max_enqueue_prod(svm_fifo_t *f)
Maximum number of bytes that can be enqueued into fifo.
Definition: svm_fifo.h:618
void tls_disconnect_transport(tls_ctx_t *ctx)
Definition: tls.c:30
svm_fifo_t * tx_fifo
static void openssl_handle_handshake_failure(tls_ctx_t *ctx)
Definition: tls_openssl.c:209
char * ca_cert_path
Definition: tls.h:97
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)
Definition: tls_async.c:426
int tls_add_vpp_q_builtin_rx_evt(session_t *s)
Definition: tls.c:62
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:97
int tls_async_openssl_callback(SSL *s, void *evt)
Definition: tls_async.c:248
void svm_fifo_enqueue_nocopy(svm_fifo_t *f, u32 len)
Advance tail.
Definition: svm_fifo.c:964
int i
static void openssl_ctx_free(tls_ctx_t *ctx)
Definition: tls_openssl.c:52
u8 use_test_cert_in_ca
Definition: tls.h:96
static session_t * session_get(u32 si, u32 thread_index)
Definition: session.h:295
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:236
static u8 * svm_fifo_tail(svm_fifo_t *f)
Definition: svm_fifo.h:673
unsigned char u8
Definition: types.h:56
struct _svm_fifo svm_fifo_t
void session_transport_closing_notify(transport_connection_t *tc)
Notification from transport that connection is being closed.
Definition: session.c:865
SSL_CTX * ssl_ctx
Definition: tls_openssl.h:28
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
static u32 svm_fifo_max_dequeue_cons(svm_fifo_t *f)
Fifo max bytes to dequeue optimized for consumer.
Definition: svm_fifo.h:498
u8 is_passive_close
Definition: tls.h:77
int openssl_engine_register(char *engine_name, char *algorithm)
Definition: tls_async.c:124
#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:687
static u32 svm_fifo_max_write_chunk(svm_fifo_t *f)
Max contiguous chunk of data that can be written.
Definition: svm_fifo.h:658
unsigned int u32
Definition: types.h:88
#define SESSION_INVALID_HANDLE
Definition: session_types.h:23
static u8 * svm_fifo_head(svm_fifo_t *f)
Definition: svm_fifo.h:666
int tls_add_vpp_q_builtin_tx_evt(session_t *s)
Definition: tls.c:78
static u32 svm_fifo_max_read_chunk(svm_fifo_t *f)
Max contiguous chunk of data that can be read.
Definition: svm_fifo.h:647
int tls_init_ca_chain(void)
Definition: tls_openssl.c:815
int session_dequeue_notify(session_t *s)
Definition: session.c:634
#define TLS_CHUNK_SIZE
Definition: tls.h:28
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
int vpp_add_async_run_event(tls_ctx_t *ctx, openssl_resume_handler *handler)
Definition: tls_async.c:314
static session_t * session_get_from_handle(session_handle_t handle)
Definition: session.h:315
long ctx[MAX_CONNS]
Definition: main.c:144
struct _unformat_input_t unformat_input_t
Definition: tls.h:84
int tls_notify_app_connected(tls_ctx_t *ctx, u8 is_failed)
Definition: tls.c:222
#define PREDICT_FALSE(x)
Definition: clib.h:112
tls_ctx_t * openssl_ctx_get(u32 ctx_index)
Definition: tls_openssl.c:67
#define SESSION_INVALID_INDEX
Definition: session_types.h:22
static const char test_srv_crt_rsa[]
Definition: tls_test.h:23
Definition: tls.h:58
u32(* ctx_alloc)(void)
Definition: tls.h:104
static int openssl_ctx_init_client(tls_ctx_t *ctx)
Definition: tls_openssl.c:501
static u8 svm_fifo_needs_deq_ntf(svm_fifo_t *f, u32 n_last_deq)
Check if fifo needs dequeue notification.
Definition: svm_fifo.h:839
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:218
static int openssl_app_close(tls_ctx_t *ctx)
Definition: tls_openssl.c:783
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
void session_free(session_t *s)
Definition: session.c:196
openssl_ctx_t *** ctx_pool
Definition: tls_openssl.h:45
#define clib_warning(format, args...)
Definition: error.h:59
static int openssl_ctx_write(tls_ctx_t *ctx, session_t *app_session)
Definition: tls_openssl.c:333
static int openssl_try_handshake_write(openssl_ctx_t *oc, session_t *tls_session)
Definition: tls_openssl.c:140
static int openssl_ctx_read(tls_ctx_t *ctx, session_t *tls_session)
Definition: tls_openssl.c:416
static openssl_main_t openssl_main
Definition: tls_openssl.c:31
int tls_add_vpp_q_tx_evt(session_t *s)
Definition: tls.c:70
transport_connection_t connection
Definition: tls.h:62
void tls_register_engine(const tls_engine_vft_t *vft, tls_engine_type_t type)
Definition: tls.c:827
application_t * application_get(u32 app_index)
Definition: application.c:418
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:161
#define pool_put_index(p, i)
Free pool element with given index.
Definition: pool.h:311
static void openssl_confirm_app_close(tls_ctx_t *ctx)
Definition: tls_openssl.c:326
int openssl_resume_handler(tls_ctx_t *ctx, session_t *tls_session)
Definition: tls_openssl.h:60
static clib_error_t * tls_openssl_init(vlib_main_t *vm)
Definition: tls_openssl.c:884
u8 * tls_key
PEM encoded key.
Definition: application.h:122
static void * clib_mem_alloc(uword size)
Definition: mem.h:153
tls_ctx_t * openssl_ctx_get_w_thread(u32 ctx_index, u8 thread_index)
Definition: tls_openssl.c:76
static int openssl_ctx_init_server(tls_ctx_t *ctx)
Definition: tls_openssl.c:705
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)
Definition: tls_async.c:284
u8 app_closed
Definition: tls.h:79
#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.
Definition: session.c:953
static int openssl_start_listen(tls_ctx_t *lctx)
Definition: tls_openssl.c:596
int openssl_ctx_handshake_rx(tls_ctx_t *ctx, session_t *tls_session)
Definition: tls_openssl.c:231
u8 * srv_hostname
Definition: tls.h:81
VLIB_PLUGIN_REGISTER()
int tls_notify_app_accept(tls_ctx_t *ctx)
Definition: tls.c:190
u32 app_index
Index of owning app.
Definition: application.h:43
u8 resume
Definition: tls.h:78
void tls_notify_app_enqueue(tls_ctx_t *ctx, session_t *app_session)
Definition: tls.c:181
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
static int openssl_try_handshake_read(openssl_ctx_t *oc, session_t *tls_session)
Definition: tls_openssl.c:109
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:119
openssl_listen_ctx_t * openssl_lctx_get(u32 lctx_index)
Definition: tls_openssl.c:103
static u32 openssl_listen_ctx_alloc(void)
Definition: tls_openssl.c:84
int svm_fifo_dequeue_drop(svm_fifo_t *f, u32 len)
Dequeue and drop bytes from fifo.
Definition: svm_fifo.c:1029
#define VLIB_INITS(...)
Definition: init.h:344
#define TLS_DBG(_lvl, _fmt, _args...)
Definition: tls.h:36
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
static const u32 test_srv_crt_rsa_len
Definition: tls_test.h:47
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171