tls: limit openssl engine max read burst
[vpp.git] / src / plugins / tlsopenssl / tls_openssl.c
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
20 #ifdef HAVE_OPENSSL_ASYNC
21 #include <openssl/async.h>
22 #endif
23 #include <dlfcn.h>
24 #include <vnet/plugin/plugin.h>
25 #include <vpp/app/version.h>
26 #include <vnet/tls/tls.h>
27 #include <ctype.h>
28 #include <tlsopenssl/tls_openssl.h>
29 #include <tlsopenssl/tls_bios.h>
30 #include <openssl/x509_vfy.h>
31 #include <openssl/x509v3.h>
32
33 #define MAX_CRYPTO_LEN 64
34
35 openssl_main_t openssl_main;
36
37 static u32
38 openssl_ctx_alloc_w_thread (u32 thread_index)
39 {
40   openssl_main_t *om = &openssl_main;
41   openssl_ctx_t **ctx;
42
43   pool_get_aligned_safe (om->ctx_pool[thread_index], ctx, 0);
44
45   if (!(*ctx))
46     *ctx = clib_mem_alloc (sizeof (openssl_ctx_t));
47
48   clib_memset (*ctx, 0, sizeof (openssl_ctx_t));
49   (*ctx)->ctx.c_thread_index = thread_index;
50   (*ctx)->ctx.tls_ctx_engine = CRYPTO_ENGINE_OPENSSL;
51   (*ctx)->ctx.app_session_handle = SESSION_INVALID_HANDLE;
52   (*ctx)->openssl_ctx_index = ctx - om->ctx_pool[thread_index];
53   return ((*ctx)->openssl_ctx_index);
54 }
55
56 static u32
57 openssl_ctx_alloc (void)
58 {
59   return openssl_ctx_alloc_w_thread (vlib_get_thread_index ());
60 }
61
62 static void
63 openssl_ctx_free (tls_ctx_t * ctx)
64 {
65   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
66
67   /* Cleanup ssl ctx unless migrated */
68   if (!ctx->is_migrated)
69     {
70       if (SSL_is_init_finished (oc->ssl) && !ctx->is_passive_close)
71         SSL_shutdown (oc->ssl);
72
73       SSL_free (oc->ssl);
74       vec_free (ctx->srv_hostname);
75
76 #ifdef HAVE_OPENSSL_ASYNC
77   openssl_evt_free (ctx->evt_index, ctx->c_thread_index);
78 #endif
79     }
80
81   pool_put_index (openssl_main.ctx_pool[ctx->c_thread_index],
82                   oc->openssl_ctx_index);
83 }
84
85 static void *
86 openssl_ctx_detach (tls_ctx_t *ctx)
87 {
88   openssl_ctx_t *oc = (openssl_ctx_t *) ctx, *oc_copy;
89
90   oc_copy = clib_mem_alloc (sizeof (*oc_copy));
91   clib_memcpy (oc_copy, oc, sizeof (*oc));
92
93   return oc_copy;
94 }
95
96 static u32
97 openssl_ctx_attach (u32 thread_index, void *ctx_ptr)
98 {
99   openssl_main_t *om = &openssl_main;
100   session_handle_t sh;
101   openssl_ctx_t **oc;
102
103   pool_get_aligned_safe (om->ctx_pool[thread_index], oc, 0);
104   /* Free the old instance instead of looking for an empty spot */
105   if (*oc)
106     clib_mem_free (*oc);
107
108   *oc = ctx_ptr;
109   (*oc)->openssl_ctx_index = oc - om->ctx_pool[thread_index];
110   (*oc)->ctx.c_thread_index = thread_index;
111
112   sh = (*oc)->ctx.tls_session_handle;
113   BIO_set_data ((*oc)->rbio, uword_to_pointer (sh, void *));
114   BIO_set_data ((*oc)->wbio, uword_to_pointer (sh, void *));
115
116   return ((*oc)->openssl_ctx_index);
117 }
118
119 tls_ctx_t *
120 openssl_ctx_get (u32 ctx_index)
121 {
122   openssl_ctx_t **ctx;
123   ctx = pool_elt_at_index (openssl_main.ctx_pool[vlib_get_thread_index ()],
124                            ctx_index);
125   return &(*ctx)->ctx;
126 }
127
128 tls_ctx_t *
129 openssl_ctx_get_w_thread (u32 ctx_index, u8 thread_index)
130 {
131   openssl_ctx_t **ctx;
132   ctx = pool_elt_at_index (openssl_main.ctx_pool[thread_index], ctx_index);
133   return &(*ctx)->ctx;
134 }
135
136 static u32
137 openssl_listen_ctx_alloc (void)
138 {
139   openssl_main_t *om = &openssl_main;
140   openssl_listen_ctx_t *lctx;
141
142   pool_get (om->lctx_pool, lctx);
143
144   clib_memset (lctx, 0, sizeof (openssl_listen_ctx_t));
145   lctx->openssl_lctx_index = lctx - om->lctx_pool;
146   return lctx->openssl_lctx_index;
147 }
148
149 static void
150 openssl_listen_ctx_free (openssl_listen_ctx_t * lctx)
151 {
152   pool_put_index (openssl_main.lctx_pool, lctx->openssl_lctx_index);
153 }
154
155 openssl_listen_ctx_t *
156 openssl_lctx_get (u32 lctx_index)
157 {
158   return pool_elt_at_index (openssl_main.lctx_pool, lctx_index);
159 }
160
161 #define ossl_check_err_is_fatal(_ssl, _rv)                                    \
162   if (PREDICT_FALSE (_rv < 0 && SSL_get_error (_ssl, _rv) == SSL_ERROR_SSL))  \
163     return -1;
164
165 static int
166 openssl_read_from_ssl_into_fifo (svm_fifo_t *f, SSL *ssl, u32 max_len)
167 {
168   int read, rv, n_fs, i;
169   const int n_segs = 2;
170   svm_fifo_seg_t fs[n_segs];
171   u32 max_enq;
172
173   max_enq = svm_fifo_max_enqueue_prod (f);
174   if (!max_enq)
175     return 0;
176
177   max_enq = clib_min (max_len, max_enq);
178   n_fs = svm_fifo_provision_chunks (f, fs, n_segs, max_enq);
179   if (n_fs < 0)
180     return 0;
181
182   /* Return early if we can't read anything */
183   read = SSL_read (ssl, fs[0].data, fs[0].len);
184   if (read <= 0)
185     {
186       ossl_check_err_is_fatal (ssl, read);
187       return 0;
188     }
189
190   if (read == (int) fs[0].len)
191     {
192       for (i = 1; i < n_fs; i++)
193         {
194           rv = SSL_read (ssl, fs[i].data, fs[i].len);
195           read += rv > 0 ? rv : 0;
196
197           if (rv < (int) fs[i].len)
198             {
199               ossl_check_err_is_fatal (ssl, rv);
200               break;
201             }
202         }
203     }
204   svm_fifo_enqueue_nocopy (f, read);
205
206   return read;
207 }
208
209 static int
210 openssl_write_from_fifo_into_ssl (svm_fifo_t *f, SSL *ssl, u32 max_len)
211 {
212   int wrote = 0, rv, i = 0, len;
213   u32 n_segs = 2;
214   svm_fifo_seg_t fs[n_segs];
215
216   len = svm_fifo_segments (f, 0, fs, &n_segs, max_len);
217   if (len <= 0)
218     return 0;
219
220   while (wrote < len && i < n_segs)
221     {
222       rv = SSL_write (ssl, fs[i].data, fs[i].len);
223       wrote += (rv > 0) ? rv : 0;
224       if (rv < (int) fs[i].len)
225         {
226           ossl_check_err_is_fatal (ssl, rv);
227           break;
228         }
229       i++;
230     }
231
232   if (wrote)
233     svm_fifo_dequeue_drop (f, wrote);
234
235   return wrote;
236 }
237
238 #ifdef HAVE_OPENSSL_ASYNC
239 static int
240 openssl_check_async_status (tls_ctx_t * ctx, openssl_resume_handler * handler,
241                             session_t * session)
242 {
243   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
244   int estatus;
245
246   SSL_get_async_status (oc->ssl, &estatus);
247   if (estatus == ASYNC_STATUS_EAGAIN)
248     {
249       vpp_tls_async_update_event (ctx, 1);
250     }
251   else
252     {
253       vpp_tls_async_update_event (ctx, 0);
254     }
255
256   return 1;
257
258 }
259
260 #endif
261
262 static void
263 openssl_handle_handshake_failure (tls_ctx_t * ctx)
264 {
265   session_t *app_session;
266
267   if (SSL_is_server (((openssl_ctx_t *) ctx)->ssl))
268     {
269       /*
270        * Cleanup pre-allocated app session and close transport
271        */
272       app_session =
273         session_get_if_valid (ctx->c_s_index, ctx->c_thread_index);
274       if (app_session)
275         {
276           session_free (app_session);
277           ctx->c_s_index = SESSION_INVALID_INDEX;
278           tls_disconnect_transport (ctx);
279         }
280       ctx->no_app_session = 1;
281     }
282   else
283     {
284       /*
285        * Also handles cleanup of the pre-allocated session
286        */
287       tls_notify_app_connected (ctx, SESSION_E_TLS_HANDSHAKE);
288       tls_disconnect_transport (ctx);
289     }
290 }
291
292 int
293 openssl_ctx_handshake_rx (tls_ctx_t * ctx, session_t * tls_session)
294 {
295   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
296   int rv = 0, err;
297
298   while (SSL_in_init (oc->ssl))
299     {
300       if (ctx->resume)
301         {
302           ctx->resume = 0;
303         }
304       else if (!svm_fifo_max_dequeue_cons (tls_session->rx_fifo))
305         break;
306
307       rv = SSL_do_handshake (oc->ssl);
308       err = SSL_get_error (oc->ssl, rv);
309
310 #ifdef HAVE_OPENSSL_ASYNC
311       if (err == SSL_ERROR_WANT_ASYNC)
312         {
313           openssl_check_async_status (ctx, openssl_ctx_handshake_rx,
314                                       tls_session);
315         }
316 #endif
317       if (err == SSL_ERROR_SSL)
318         {
319           char buf[512];
320           ERR_error_string (ERR_get_error (), buf);
321           clib_warning ("Err: %s", buf);
322
323           openssl_handle_handshake_failure (ctx);
324           return -1;
325         }
326
327       if (err != SSL_ERROR_WANT_WRITE && err != SSL_ERROR_WANT_READ)
328         break;
329     }
330   TLS_DBG (2, "tls state for %u is %s", oc->openssl_ctx_index,
331            SSL_state_string_long (oc->ssl));
332
333   if (SSL_in_init (oc->ssl))
334     return -1;
335
336   /*
337    * Handshake complete
338    */
339   if (!SSL_is_server (oc->ssl))
340     {
341       /*
342        * Verify server certificate
343        */
344       if ((rv = SSL_get_verify_result (oc->ssl)) != X509_V_OK)
345         {
346           TLS_DBG (1, " failed verify: %s\n",
347                    X509_verify_cert_error_string (rv));
348
349           /*
350            * Presence of hostname enforces strict certificate verification
351            */
352           if (ctx->srv_hostname)
353             {
354               openssl_handle_handshake_failure (ctx);
355               return -1;
356             }
357         }
358       if (tls_notify_app_connected (ctx, SESSION_E_NONE))
359         {
360           tls_disconnect_transport (ctx);
361           return -1;
362         }
363     }
364   else
365     {
366       /* Need to check transport status */
367       if (ctx->is_passive_close)
368         {
369           openssl_handle_handshake_failure (ctx);
370           return -1;
371         }
372
373       /* Accept failed, cleanup */
374       if (tls_notify_app_accept (ctx))
375         {
376           ctx->c_s_index = SESSION_INVALID_INDEX;
377           tls_disconnect_transport (ctx);
378           return -1;
379         }
380     }
381
382   TLS_DBG (1, "Handshake for %u complete. TLS cipher is %s",
383            oc->openssl_ctx_index, SSL_get_cipher (oc->ssl));
384   return rv;
385 }
386
387 static void
388 openssl_confirm_app_close (tls_ctx_t * ctx)
389 {
390   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
391   SSL_shutdown (oc->ssl);
392   tls_disconnect_transport (ctx);
393   session_transport_closed_notify (&ctx->connection);
394 }
395
396 static int
397 openssl_ctx_write_tls (tls_ctx_t *ctx, session_t *app_session,
398                        transport_send_params_t *sp)
399 {
400   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
401   u32 deq_max, space, enq_buf;
402   session_t *ts;
403   int wrote = 0;
404   svm_fifo_t *f;
405
406   ts = session_get_from_handle (ctx->tls_session_handle);
407   space = svm_fifo_max_enqueue_prod (ts->tx_fifo);
408   /* Leave a bit of extra space for tls ctrl data, if any needed */
409   space = clib_max ((int) space - TLSO_CTRL_BYTES, 0);
410
411   f = app_session->tx_fifo;
412
413   deq_max = svm_fifo_max_dequeue_cons (f);
414   deq_max = clib_min (deq_max, space);
415   if (!deq_max)
416     goto check_tls_fifo;
417
418   deq_max = clib_min (deq_max, sp->max_burst_size);
419
420   /* Make sure tcp's tx fifo can actually buffer all bytes to be dequeued.
421    * If under memory pressure, tls's fifo segment might not be able to
422    * allocate the chunks needed. This also avoids errors from the underlying
423    * custom bio to the ssl infra which at times can get stuck. */
424   if (svm_fifo_provision_chunks (ts->tx_fifo, 0, 0, deq_max + TLSO_CTRL_BYTES))
425     goto check_tls_fifo;
426
427   wrote = openssl_write_from_fifo_into_ssl (f, oc->ssl, deq_max);
428
429   /* Unrecoverable protocol error. Reset connection */
430   if (PREDICT_FALSE (wrote < 0))
431     {
432       tls_notify_app_io_error (ctx);
433       return 0;
434     }
435
436   if (!wrote)
437     goto check_tls_fifo;
438
439   if (svm_fifo_needs_deq_ntf (f, wrote))
440     session_dequeue_notify (app_session);
441
442 check_tls_fifo:
443
444   if (PREDICT_FALSE (ctx->app_closed && BIO_ctrl_pending (oc->rbio) <= 0))
445     openssl_confirm_app_close (ctx);
446
447   /* Deschedule and wait for deq notification if fifo is almost full */
448   enq_buf = clib_min (svm_fifo_size (ts->tx_fifo) / 2, TLSO_MIN_ENQ_SPACE);
449   if (space < wrote + enq_buf)
450     {
451       svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
452       transport_connection_deschedule (&ctx->connection);
453       sp->flags |= TRANSPORT_SND_F_DESCHED;
454     }
455   else
456     /* Request tx reschedule of the app session */
457     app_session->flags |= SESSION_F_CUSTOM_TX;
458
459   return wrote;
460 }
461
462 static int
463 openssl_ctx_write_dtls (tls_ctx_t *ctx, session_t *app_session,
464                         transport_send_params_t *sp)
465 {
466   openssl_main_t *om = &openssl_main;
467   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
468   u32 read = 0, to_deq, dgram_sz, enq_max;
469   session_dgram_pre_hdr_t hdr;
470   session_t *us;
471   int wrote, rv;
472   u8 *buf;
473
474   us = session_get_from_handle (ctx->tls_session_handle);
475   to_deq = svm_fifo_max_dequeue_cons (app_session->tx_fifo);
476   buf = om->tx_bufs[ctx->c_thread_index];
477
478   while (to_deq > 0)
479     {
480       /* Peeking only pre-header dgram because the session is connected */
481       rv = svm_fifo_peek (app_session->tx_fifo, 0, sizeof (hdr), (u8 *) &hdr);
482       ASSERT (rv == sizeof (hdr) && hdr.data_length < vec_len (buf));
483       ASSERT (to_deq >= hdr.data_length + SESSION_CONN_HDR_LEN);
484
485       dgram_sz = hdr.data_length + SESSION_CONN_HDR_LEN;
486       enq_max = dgram_sz + TLSO_CTRL_BYTES;
487       if (svm_fifo_max_enqueue_prod (us->tx_fifo) < enq_max ||
488           svm_fifo_provision_chunks (us->tx_fifo, 0, 0, enq_max))
489         {
490           svm_fifo_add_want_deq_ntf (us->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
491           transport_connection_deschedule (&ctx->connection);
492           sp->flags |= TRANSPORT_SND_F_DESCHED;
493           goto done;
494         }
495
496       rv = svm_fifo_peek (app_session->tx_fifo, SESSION_CONN_HDR_LEN,
497                           hdr.data_length, buf);
498       ASSERT (rv == hdr.data_length);
499       svm_fifo_dequeue_drop (app_session->tx_fifo, dgram_sz);
500
501       wrote = SSL_write (oc->ssl, buf, rv);
502       ASSERT (wrote > 0);
503
504       read += rv;
505       to_deq -= dgram_sz;
506     }
507
508 done:
509
510   if (svm_fifo_needs_deq_ntf (app_session->tx_fifo, read))
511     session_dequeue_notify (app_session);
512
513   if (read)
514     tls_add_vpp_q_tx_evt (us);
515
516   if (PREDICT_FALSE (ctx->app_closed &&
517                      !svm_fifo_max_enqueue_prod (us->rx_fifo)))
518     openssl_confirm_app_close (ctx);
519
520   return read;
521 }
522
523 static inline int
524 openssl_ctx_write (tls_ctx_t *ctx, session_t *app_session,
525                    transport_send_params_t *sp)
526 {
527   if (ctx->tls_type == TRANSPORT_PROTO_TLS)
528     return openssl_ctx_write_tls (ctx, app_session, sp);
529   else
530     return openssl_ctx_write_dtls (ctx, app_session, sp);
531 }
532
533 static inline int
534 openssl_ctx_read_tls (tls_ctx_t *ctx, session_t *tls_session)
535 {
536   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
537   const u32 max_len = 128 << 10;
538   session_t *app_session;
539   svm_fifo_t *f;
540   int read;
541
542   if (PREDICT_FALSE (SSL_in_init (oc->ssl)))
543     {
544       if (openssl_ctx_handshake_rx (ctx, tls_session) < 0)
545         return 0;
546
547       /* Application might force a session pool realloc on accept */
548       tls_session = session_get_from_handle (ctx->tls_session_handle);
549     }
550
551   app_session = session_get_from_handle (ctx->app_session_handle);
552   f = app_session->rx_fifo;
553
554   read = openssl_read_from_ssl_into_fifo (f, oc->ssl, max_len);
555
556   /* Unrecoverable protocol error. Reset connection */
557   if (PREDICT_FALSE (read < 0))
558     {
559       tls_notify_app_io_error (ctx);
560       return 0;
561     }
562
563   /* If handshake just completed, session may still be in accepting state */
564   if (read && app_session->session_state >= SESSION_STATE_READY)
565     tls_notify_app_enqueue (ctx, app_session);
566
567   if ((SSL_pending (oc->ssl) > 0) ||
568       svm_fifo_max_dequeue_cons (tls_session->rx_fifo))
569     tls_add_vpp_q_builtin_rx_evt (tls_session);
570
571   return read;
572 }
573
574 static inline int
575 openssl_ctx_read_dtls (tls_ctx_t *ctx, session_t *us)
576 {
577   openssl_main_t *om = &openssl_main;
578   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
579   session_dgram_hdr_t hdr;
580   session_t *app_session;
581   u32 wrote = 0;
582   int read, rv;
583   u8 *buf;
584
585   if (PREDICT_FALSE (SSL_in_init (oc->ssl)))
586     {
587       u32 us_index = us->session_index;
588       if (openssl_ctx_handshake_rx (ctx, us) < 0)
589         return 0;
590       /* Session pool might grow when allocating the app's session */
591       us = session_get (us_index, ctx->c_thread_index);
592     }
593
594   buf = om->rx_bufs[ctx->c_thread_index];
595   app_session = session_get_from_handle (ctx->app_session_handle);
596   svm_fifo_fill_chunk_list (app_session->rx_fifo);
597
598   while (svm_fifo_max_dequeue_cons (us->rx_fifo) > 0)
599     {
600       if (svm_fifo_max_enqueue_prod (app_session->rx_fifo) < DTLSO_MAX_DGRAM)
601         {
602           tls_add_vpp_q_builtin_rx_evt (us);
603           goto done;
604         }
605
606       read = SSL_read (oc->ssl, buf, vec_len (buf));
607       if (PREDICT_FALSE (read <= 0))
608         {
609           if (read < 0)
610             tls_add_vpp_q_builtin_rx_evt (us);
611           goto done;
612         }
613       wrote += read;
614
615       hdr.data_length = read;
616       hdr.data_offset = 0;
617
618       svm_fifo_seg_t segs[2] = { { (u8 *) &hdr, sizeof (hdr) },
619                                  { buf, read } };
620
621       rv = svm_fifo_enqueue_segments (app_session->rx_fifo, segs, 2,
622                                       0 /* allow partial */);
623       ASSERT (rv > 0);
624     }
625
626 done:
627
628   /* If handshake just completed, session may still be in accepting state */
629   if (app_session->session_state >= SESSION_STATE_READY)
630     tls_notify_app_enqueue (ctx, app_session);
631
632   return wrote;
633 }
634
635 static inline int
636 openssl_ctx_read (tls_ctx_t *ctx, session_t *ts)
637 {
638   if (ctx->tls_type == TRANSPORT_PROTO_TLS)
639     return openssl_ctx_read_tls (ctx, ts);
640   else
641     return openssl_ctx_read_dtls (ctx, ts);
642 }
643
644 static int
645 openssl_set_ckpair (SSL *ssl, u32 ckpair_index)
646 {
647   app_cert_key_pair_t *ckpair;
648   BIO *cert_bio;
649   EVP_PKEY *pkey;
650   X509 *srvcert;
651
652   /* Configure a ckpair index only if non-default/test provided */
653   if (ckpair_index == 0)
654     return 0;
655
656   ckpair = app_cert_key_pair_get_if_valid (ckpair_index);
657   if (!ckpair)
658     return -1;
659
660   if (!ckpair->cert || !ckpair->key)
661     {
662       TLS_DBG (1, "tls cert and/or key not configured");
663       return -1;
664     }
665   /*
666    * Set the key and cert
667    */
668   cert_bio = BIO_new (BIO_s_mem ());
669   BIO_write (cert_bio, ckpair->cert, vec_len (ckpair->cert));
670   srvcert = PEM_read_bio_X509 (cert_bio, NULL, NULL, NULL);
671   if (!srvcert)
672     {
673       clib_warning ("unable to parse certificate");
674       return -1;
675     }
676   SSL_use_certificate (ssl, srvcert);
677   BIO_free (cert_bio);
678
679   cert_bio = BIO_new (BIO_s_mem ());
680   BIO_write (cert_bio, ckpair->key, vec_len (ckpair->key));
681   pkey = PEM_read_bio_PrivateKey (cert_bio, NULL, NULL, NULL);
682   if (!pkey)
683     {
684       clib_warning ("unable to parse pkey");
685       return -1;
686     }
687   SSL_use_PrivateKey (ssl, pkey);
688   BIO_free (cert_bio);
689   TLS_DBG (1, "TLS client using ckpair index: %d", ckpair_index);
690   return 0;
691 }
692
693 static int
694 openssl_client_init_verify (SSL *ssl, const char *srv_hostname,
695                             int set_hostname_verification,
696                             int set_hostname_strict_check)
697 {
698   if (set_hostname_verification)
699     {
700       X509_VERIFY_PARAM *param = SSL_get0_param (ssl);
701       if (!param)
702         {
703           TLS_DBG (1, "Couldn't fetch SSL param");
704           return -1;
705         }
706
707       if (set_hostname_strict_check)
708         X509_VERIFY_PARAM_set_hostflags (param,
709                                          X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
710
711       if (!X509_VERIFY_PARAM_set1_host (param, srv_hostname, 0))
712         {
713           TLS_DBG (1, "Couldn't set hostname for verification");
714           return -1;
715         }
716       SSL_set_verify (ssl, SSL_VERIFY_PEER, 0);
717     }
718   if (!SSL_set_tlsext_host_name (ssl, srv_hostname))
719     {
720       TLS_DBG (1, "Couldn't set hostname");
721       return -1;
722     }
723   return 0;
724 }
725
726 static int
727 openssl_ctx_init_client (tls_ctx_t * ctx)
728 {
729   long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
730   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
731   openssl_main_t *om = &openssl_main;
732   const SSL_METHOD *method;
733   int rv, err;
734
735   method = ctx->tls_type == TRANSPORT_PROTO_TLS ? SSLv23_client_method () :
736                                                   DTLS_client_method ();
737   if (method == NULL)
738     {
739       TLS_DBG (1, "(D)TLS_method returned null");
740       return -1;
741     }
742
743   oc->ssl_ctx = SSL_CTX_new (method);
744   if (oc->ssl_ctx == NULL)
745     {
746       TLS_DBG (1, "SSL_CTX_new returned null");
747       return -1;
748     }
749
750   SSL_CTX_set_ecdh_auto (oc->ssl_ctx, 1);
751   SSL_CTX_set_mode (oc->ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
752 #ifdef HAVE_OPENSSL_ASYNC
753   if (om->async)
754     SSL_CTX_set_mode (oc->ssl_ctx, SSL_MODE_ASYNC);
755 #endif
756   rv = SSL_CTX_set_cipher_list (oc->ssl_ctx, (const char *) om->ciphers);
757   if (rv != 1)
758     {
759       TLS_DBG (1, "Couldn't set cipher");
760       return -1;
761     }
762
763   SSL_CTX_set_options (oc->ssl_ctx, flags);
764   SSL_CTX_set_cert_store (oc->ssl_ctx, om->cert_store);
765
766   oc->ssl = SSL_new (oc->ssl_ctx);
767   if (oc->ssl == NULL)
768     {
769       TLS_DBG (1, "Couldn't initialize ssl struct");
770       return -1;
771     }
772
773   if (ctx->tls_type == TRANSPORT_PROTO_TLS)
774     {
775       oc->rbio = BIO_new_tls (ctx->tls_session_handle);
776       oc->wbio = BIO_new_tls (ctx->tls_session_handle);
777     }
778   else
779     {
780       oc->rbio = BIO_new_dtls (ctx->tls_session_handle);
781       oc->wbio = BIO_new_dtls (ctx->tls_session_handle);
782     }
783
784   SSL_set_bio (oc->ssl, oc->wbio, oc->rbio);
785   SSL_set_connect_state (oc->ssl);
786
787   /* Hostname validation and strict check by name are disabled by default */
788   rv = openssl_client_init_verify (oc->ssl, (const char *) ctx->srv_hostname,
789                                    0, 0);
790   if (rv)
791     {
792       TLS_DBG (1, "ERROR:verify init failed:%d", rv);
793       return -1;
794     }
795   if (openssl_set_ckpair (oc->ssl, ctx->ckpair_index))
796     {
797       TLS_DBG (1, "Couldn't set client certificate-key pair");
798     }
799
800   /*
801    * 2. Do the first steps in the handshake.
802    */
803   TLS_DBG (1, "Initiating handshake for [%u]%u", ctx->c_thread_index,
804            oc->openssl_ctx_index);
805
806 #ifdef HAVE_OPENSSL_ASYNC
807   session_t *tls_session = session_get_from_handle (ctx->tls_session_handle);
808   vpp_tls_async_init_event (ctx, openssl_ctx_handshake_rx, tls_session);
809 #endif
810   while (1)
811     {
812       rv = SSL_do_handshake (oc->ssl);
813       err = SSL_get_error (oc->ssl, rv);
814 #ifdef HAVE_OPENSSL_ASYNC
815       if (err == SSL_ERROR_WANT_ASYNC)
816         {
817           openssl_check_async_status (ctx, openssl_ctx_handshake_rx,
818                                       tls_session);
819           break;
820         }
821 #endif
822       if (err != SSL_ERROR_WANT_WRITE)
823         break;
824     }
825
826   TLS_DBG (2, "tls state for [%u]%u is su", ctx->c_thread_index,
827            oc->openssl_ctx_index, SSL_state_string_long (oc->ssl));
828   return 0;
829 }
830
831 static int
832 openssl_start_listen (tls_ctx_t * lctx)
833 {
834   const SSL_METHOD *method;
835   SSL_CTX *ssl_ctx;
836   int rv;
837   BIO *cert_bio;
838   X509 *srvcert;
839   EVP_PKEY *pkey;
840   u32 olc_index;
841   openssl_listen_ctx_t *olc;
842   app_cert_key_pair_t *ckpair;
843
844   long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
845   openssl_main_t *om = &openssl_main;
846
847   ckpair = app_cert_key_pair_get_if_valid (lctx->ckpair_index);
848   if (!ckpair)
849     return -1;
850
851   if (!ckpair->cert || !ckpair->key)
852     {
853       TLS_DBG (1, "tls cert and/or key not configured %d",
854                lctx->parent_app_wrk_index);
855       return -1;
856     }
857
858   method = lctx->tls_type == TRANSPORT_PROTO_TLS ? SSLv23_server_method () :
859                                                    DTLS_server_method ();
860   ssl_ctx = SSL_CTX_new (method);
861   if (!ssl_ctx)
862     {
863       clib_warning ("Unable to create SSL context");
864       return -1;
865     }
866
867   SSL_CTX_set_mode (ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
868 #ifdef HAVE_OPENSSL_ASYNC
869   if (om->async)
870     {
871       SSL_CTX_set_mode (ssl_ctx, SSL_MODE_ASYNC);
872       SSL_CTX_set_async_callback (ssl_ctx, tls_async_openssl_callback);
873     }
874 #endif
875   SSL_CTX_set_options (ssl_ctx, flags);
876   SSL_CTX_set_ecdh_auto (ssl_ctx, 1);
877
878   rv = SSL_CTX_set_cipher_list (ssl_ctx, (const char *) om->ciphers);
879   if (rv != 1)
880     {
881       TLS_DBG (1, "Couldn't set cipher");
882       return -1;
883     }
884
885   /* use the default OpenSSL built-in DH parameters */
886   rv = SSL_CTX_set_dh_auto (ssl_ctx, 1);
887   if (rv != 1)
888     {
889       TLS_DBG (1, "Couldn't set temp DH parameters");
890       return -1;
891     }
892
893   /*
894    * Set the key and cert
895    */
896   cert_bio = BIO_new (BIO_s_mem ());
897   if (!cert_bio)
898     {
899       clib_warning ("unable to allocate memory");
900       return -1;
901     }
902   BIO_write (cert_bio, ckpair->cert, vec_len (ckpair->cert));
903   srvcert = PEM_read_bio_X509 (cert_bio, NULL, NULL, NULL);
904   if (!srvcert)
905     {
906       clib_warning ("unable to parse certificate");
907       goto err;
908     }
909   rv = SSL_CTX_use_certificate (ssl_ctx, srvcert);
910   if (rv != 1)
911     {
912       clib_warning ("unable to use SSL certificate");
913       goto err;
914     }
915
916   BIO_free (cert_bio);
917
918   cert_bio = BIO_new (BIO_s_mem ());
919   if (!cert_bio)
920     {
921       clib_warning ("unable to allocate memory");
922       return -1;
923     }
924   BIO_write (cert_bio, ckpair->key, vec_len (ckpair->key));
925   pkey = PEM_read_bio_PrivateKey (cert_bio, NULL, NULL, NULL);
926   if (!pkey)
927     {
928       clib_warning ("unable to parse pkey");
929       goto err;
930     }
931   rv = SSL_CTX_use_PrivateKey (ssl_ctx, pkey);
932   if (rv != 1)
933     {
934       clib_warning ("unable to use SSL PrivateKey");
935       goto err;
936     }
937
938   BIO_free (cert_bio);
939
940   olc_index = openssl_listen_ctx_alloc ();
941   olc = openssl_lctx_get (olc_index);
942   olc->ssl_ctx = ssl_ctx;
943   olc->srvcert = srvcert;
944   olc->pkey = pkey;
945
946   /* store SSL_CTX into TLS level structure */
947   lctx->tls_ssl_ctx = olc_index;
948
949   return 0;
950
951 err:
952   if (cert_bio)
953     BIO_free (cert_bio);
954   return -1;
955 }
956
957 static int
958 openssl_stop_listen (tls_ctx_t * lctx)
959 {
960   u32 olc_index;
961   openssl_listen_ctx_t *olc;
962
963   olc_index = lctx->tls_ssl_ctx;
964   olc = openssl_lctx_get (olc_index);
965
966   X509_free (olc->srvcert);
967   EVP_PKEY_free (olc->pkey);
968
969   SSL_CTX_free (olc->ssl_ctx);
970   openssl_listen_ctx_free (olc);
971
972   return 0;
973 }
974
975 static int
976 openssl_ctx_init_server (tls_ctx_t * ctx)
977 {
978   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
979   u32 olc_index = ctx->tls_ssl_ctx;
980   openssl_listen_ctx_t *olc;
981   int rv, err;
982
983   /* Start a new connection */
984
985   olc = openssl_lctx_get (olc_index);
986   oc->ssl = SSL_new (olc->ssl_ctx);
987   if (oc->ssl == NULL)
988     {
989       TLS_DBG (1, "Couldn't initialize ssl struct");
990       return -1;
991     }
992
993   if (ctx->tls_type == TRANSPORT_PROTO_TLS)
994     {
995       oc->rbio = BIO_new_tls (ctx->tls_session_handle);
996       oc->wbio = BIO_new_tls (ctx->tls_session_handle);
997     }
998   else
999     {
1000       oc->rbio = BIO_new_dtls (ctx->tls_session_handle);
1001       oc->wbio = BIO_new_dtls (ctx->tls_session_handle);
1002     }
1003
1004   SSL_set_bio (oc->ssl, oc->wbio, oc->rbio);
1005   SSL_set_accept_state (oc->ssl);
1006
1007   TLS_DBG (1, "Initiating handshake for [%u]%u", ctx->c_thread_index,
1008            oc->openssl_ctx_index);
1009
1010 #ifdef HAVE_OPENSSL_ASYNC
1011   session_t *tls_session = session_get_from_handle (ctx->tls_session_handle);
1012   vpp_tls_async_init_event (ctx, openssl_ctx_handshake_rx, tls_session);
1013 #endif
1014   while (1)
1015     {
1016       rv = SSL_do_handshake (oc->ssl);
1017       err = SSL_get_error (oc->ssl, rv);
1018 #ifdef HAVE_OPENSSL_ASYNC
1019       if (err == SSL_ERROR_WANT_ASYNC)
1020         {
1021           openssl_check_async_status (ctx, openssl_ctx_handshake_rx,
1022                                       tls_session);
1023           break;
1024         }
1025 #endif
1026       if (err != SSL_ERROR_WANT_WRITE)
1027         break;
1028     }
1029
1030   TLS_DBG (2, "tls state for [%u]%u is su", ctx->c_thread_index,
1031            oc->openssl_ctx_index, SSL_state_string_long (oc->ssl));
1032   return 0;
1033 }
1034
1035 static u8
1036 openssl_handshake_is_over (tls_ctx_t * ctx)
1037 {
1038   openssl_ctx_t *mc = (openssl_ctx_t *) ctx;
1039   if (!mc->ssl)
1040     return 0;
1041   return SSL_is_init_finished (mc->ssl);
1042 }
1043
1044 static int
1045 openssl_transport_close (tls_ctx_t * ctx)
1046 {
1047 #ifdef HAVE_OPENSSL_ASYNC
1048   if (vpp_openssl_is_inflight (ctx))
1049     return 0;
1050 #endif
1051
1052   if (!openssl_handshake_is_over (ctx))
1053     {
1054       openssl_handle_handshake_failure (ctx);
1055       return 0;
1056     }
1057   session_transport_closing_notify (&ctx->connection);
1058   return 0;
1059 }
1060
1061 static int
1062 openssl_app_close (tls_ctx_t * ctx)
1063 {
1064   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
1065   session_t *app_session;
1066
1067   /* Wait for all data to be written to tcp */
1068   app_session = session_get_from_handle (ctx->app_session_handle);
1069   if (BIO_ctrl_pending (oc->rbio) <= 0
1070       && !svm_fifo_max_dequeue_cons (app_session->tx_fifo))
1071     openssl_confirm_app_close (ctx);
1072   else
1073     ctx->app_closed = 1;
1074   return 0;
1075 }
1076
1077 int
1078 tls_init_ca_chain (void)
1079 {
1080   openssl_main_t *om = &openssl_main;
1081   tls_main_t *tm = vnet_tls_get_main ();
1082   BIO *cert_bio;
1083   X509 *testcert;
1084   int rv;
1085
1086   if (access (tm->ca_cert_path, F_OK | R_OK) == -1)
1087     {
1088       clib_warning ("Could not initialize TLS CA certificates");
1089       return -1;
1090     }
1091
1092   if (!(om->cert_store = X509_STORE_new ()))
1093     {
1094       clib_warning ("failed to create cert store");
1095       return -1;
1096     }
1097
1098 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1099   rv = X509_STORE_load_file (om->cert_store, tm->ca_cert_path);
1100 #else
1101   rv = X509_STORE_load_locations (om->cert_store, tm->ca_cert_path, 0);
1102 #endif
1103
1104   if (rv < 0)
1105     {
1106       clib_warning ("failed to load ca certificate");
1107     }
1108
1109   if (tm->use_test_cert_in_ca)
1110     {
1111       cert_bio = BIO_new (BIO_s_mem ());
1112       BIO_write (cert_bio, test_srv_crt_rsa, test_srv_crt_rsa_len);
1113       testcert = PEM_read_bio_X509 (cert_bio, NULL, NULL, NULL);
1114       if (!testcert)
1115         {
1116           clib_warning ("unable to parse certificate");
1117           return -1;
1118         }
1119       X509_STORE_add_cert (om->cert_store, testcert);
1120       rv = 0;
1121     }
1122   return (rv < 0 ? -1 : 0);
1123 }
1124
1125 int
1126 openssl_reinit_ca_chain (void)
1127 {
1128   openssl_main_t *om = &openssl_main;
1129
1130   /* Remove/free existing x509_store */
1131   if (om->cert_store)
1132     {
1133       X509_STORE_free (om->cert_store);
1134     }
1135   return tls_init_ca_chain ();
1136 }
1137
1138 const static tls_engine_vft_t openssl_engine = {
1139   .ctx_alloc = openssl_ctx_alloc,
1140   .ctx_alloc_w_thread = openssl_ctx_alloc_w_thread,
1141   .ctx_free = openssl_ctx_free,
1142   .ctx_attach = openssl_ctx_attach,
1143   .ctx_detach = openssl_ctx_detach,
1144   .ctx_get = openssl_ctx_get,
1145   .ctx_get_w_thread = openssl_ctx_get_w_thread,
1146   .ctx_init_server = openssl_ctx_init_server,
1147   .ctx_init_client = openssl_ctx_init_client,
1148   .ctx_write = openssl_ctx_write,
1149   .ctx_read = openssl_ctx_read,
1150   .ctx_handshake_is_over = openssl_handshake_is_over,
1151   .ctx_start_listen = openssl_start_listen,
1152   .ctx_stop_listen = openssl_stop_listen,
1153   .ctx_transport_close = openssl_transport_close,
1154   .ctx_app_close = openssl_app_close,
1155   .ctx_reinit_cachain = openssl_reinit_ca_chain,
1156 };
1157
1158 int
1159 tls_openssl_set_ciphers (char *ciphers)
1160 {
1161   openssl_main_t *om = &openssl_main;
1162   int i;
1163
1164   if (!ciphers)
1165     {
1166       return -1;
1167     }
1168
1169   vec_validate (om->ciphers, strlen (ciphers));
1170   for (i = 0; i < vec_len (om->ciphers) - 1; i++)
1171     {
1172       om->ciphers[i] = toupper (ciphers[i]);
1173     }
1174
1175   return 0;
1176
1177 }
1178
1179 static clib_error_t *
1180 tls_openssl_init (vlib_main_t * vm)
1181 {
1182   vlib_thread_main_t *vtm = vlib_get_thread_main ();
1183   openssl_main_t *om = &openssl_main;
1184   clib_error_t *error = 0;
1185   u32 num_threads, i;
1186
1187   error = tls_openssl_api_init (vm);
1188   num_threads = 1 /* main thread */  + vtm->n_threads;
1189
1190   SSL_library_init ();
1191   SSL_load_error_strings ();
1192
1193   vec_validate (om->ctx_pool, num_threads - 1);
1194   vec_validate (om->rx_bufs, num_threads - 1);
1195   vec_validate (om->tx_bufs, num_threads - 1);
1196   for (i = 0; i < num_threads; i++)
1197     {
1198       vec_validate (om->rx_bufs[i], DTLSO_MAX_DGRAM);
1199       vec_validate (om->tx_bufs[i], DTLSO_MAX_DGRAM);
1200     }
1201   tls_register_engine (&openssl_engine, CRYPTO_ENGINE_OPENSSL);
1202
1203   om->engine_init = 0;
1204
1205   /* default ciphers */
1206   tls_openssl_set_ciphers
1207     ("ALL:!ADH:!LOW:!EXP:!MD5:!RC4-SHA:!DES-CBC3-SHA:@STRENGTH");
1208
1209   if (tls_init_ca_chain ())
1210     {
1211       clib_warning ("failed to initialize TLS CA chain");
1212       return 0;
1213     }
1214
1215   return error;
1216 }
1217 /* *INDENT-OFF* */
1218 VLIB_INIT_FUNCTION (tls_openssl_init) =
1219 {
1220   .runs_after = VLIB_INITS("tls_init"),
1221 };
1222 /* *INDENT-ON* */
1223
1224 #ifdef HAVE_OPENSSL_ASYNC
1225 static clib_error_t *
1226 tls_openssl_set_command_fn (vlib_main_t * vm, unformat_input_t * input,
1227                             vlib_cli_command_t * cmd)
1228 {
1229   openssl_main_t *om = &openssl_main;
1230   char *engine_name = NULL;
1231   char *engine_alg = NULL;
1232   char *ciphers = NULL;
1233   u8 engine_name_set = 0;
1234   int i, async = 0;
1235
1236   /* By present, it is not allowed to configure engine again after running */
1237   if (om->engine_init)
1238     {
1239       clib_warning ("engine has started!\n");
1240       return clib_error_return
1241         (0, "engine has started, and no config is accepted");
1242     }
1243
1244   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1245     {
1246       if (unformat (input, "engine %s", &engine_name))
1247         {
1248           engine_name_set = 1;
1249         }
1250       else if (unformat (input, "async"))
1251         {
1252           async = 1;
1253         }
1254       else if (unformat (input, "alg %s", &engine_alg))
1255         {
1256           for (i = 0; i < strnlen (engine_alg, MAX_CRYPTO_LEN); i++)
1257             engine_alg[i] = toupper (engine_alg[i]);
1258         }
1259       else if (unformat (input, "ciphers %s", &ciphers))
1260         {
1261           tls_openssl_set_ciphers (ciphers);
1262         }
1263       else
1264         return clib_error_return (0, "failed: unknown input `%U'",
1265                                   format_unformat_error, input);
1266     }
1267
1268   /* reset parameters if engine is not configured */
1269   if (!engine_name_set)
1270     {
1271       clib_warning ("No engine provided! \n");
1272       async = 0;
1273     }
1274   else
1275     {
1276       vnet_session_enable_disable (vm, 1);
1277       if (openssl_engine_register (engine_name, engine_alg, async) < 0)
1278         {
1279           return clib_error_return (0, "Failed to register %s polling",
1280                                     engine_name);
1281         }
1282       else
1283         {
1284           vlib_cli_output (vm, "Successfully register engine %s\n",
1285                            engine_name);
1286         }
1287     }
1288   om->async = async;
1289
1290   return 0;
1291 }
1292
1293 /* *INDENT-OFF* */
1294 VLIB_CLI_COMMAND (tls_openssl_set_command, static) =
1295 {
1296   .path = "tls openssl set",
1297   .short_help = "tls openssl set [engine <engine name>] [alg [algorithm] [async]",
1298   .function = tls_openssl_set_command_fn,
1299 };
1300 /* *INDENT-ON* */
1301 #endif
1302
1303 /* *INDENT-OFF* */
1304 VLIB_PLUGIN_REGISTER () = {
1305     .version = VPP_BUILD_VER,
1306     .description = "Transport Layer Security (TLS) Engine, OpenSSL Based",
1307 };
1308 /* *INDENT-ON* */
1309
1310 /*
1311  * fd.io coding-style-patch-verification: ON
1312  *
1313  * Local Variables:
1314  * eval: (c-set-style "gnu")
1315  * End:
1316  */