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