Add a new communication channel between VPP and openssl engine
[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 #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
31 static openssl_main_t openssl_main;
32 static u32
33 openssl_ctx_alloc (void)
34 {
35   u8 thread_index = vlib_get_thread_index ();
36   openssl_main_t *tm = &openssl_main;
37   openssl_ctx_t **ctx;
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
52 openssl_ctx_free (tls_ctx_t * ctx)
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 {
73   openssl_ctx_t **ctx;
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 {
82   openssl_ctx_t **ctx;
83   ctx = pool_elt_at_index (openssl_main.ctx_pool[thread_index], ctx_index);
84   return &(*ctx)->ctx;
85 }
86
87 static int
88 openssl_try_handshake_read (openssl_ctx_t * oc,
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
120 openssl_try_handshake_write (openssl_ctx_t * oc,
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);
141   tls_add_vpp_q_evt (f, FIFO_EVENT_APP_TX);
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 (2, "set callback to engine %p\n", engine_cb->callback);
171     }
172   return 0;
173
174 }
175
176 /* Due to engine busy stat, VPP need to retry later */
177 static int
178 vpp_ssl_async_retry_func (tls_ctx_t * ctx, openssl_resume_handler * handler)
179 {
180   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
181
182   if (vpp_add_async_run_event (ctx, handler))
183     {
184       SSL_set_async_estatus (oc->ssl, 0);
185     }
186   return 0;
187
188 }
189
190 #endif
191
192 int
193 openssl_ctx_handshake_rx (tls_ctx_t * ctx, stream_session_t * tls_session)
194 {
195   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
196   int rv = 0, err;
197 #ifdef HAVE_OPENSSL_ASYNC
198   int estatus;
199   openssl_resume_handler *myself;
200 #endif
201
202   while (SSL_in_init (oc->ssl))
203     {
204       if (ctx->resume)
205         {
206           ctx->resume = 0;
207         }
208       else if (!openssl_try_handshake_read (oc, tls_session))
209         {
210           break;
211         }
212
213       rv = SSL_do_handshake (oc->ssl);
214       err = SSL_get_error (oc->ssl, rv);
215       openssl_try_handshake_write (oc, tls_session);
216 #ifdef HAVE_OPENSSL_ASYNC
217       myself = openssl_ctx_handshake_rx;
218       if (SSL_get_async_estatus (oc->ssl, &estatus)
219           && (estatus == ENGINE_STATUS_RETRY))
220         {
221           vpp_ssl_async_retry_func (ctx, myself);
222         }
223       if (err == SSL_ERROR_WANT_ASYNC)
224         {
225           vpp_ssl_async_process_event (ctx, myself);
226         }
227 #endif
228
229       if (err != SSL_ERROR_WANT_WRITE)
230         {
231           if (err == SSL_ERROR_SSL)
232             {
233               char buf[512];
234               ERR_error_string (ERR_get_error (), buf);
235               clib_warning ("Err: %s", buf);
236             }
237           break;
238         }
239     }
240   TLS_DBG (2, "tls state for %u is %s", oc->openssl_ctx_index,
241            SSL_state_string_long (oc->ssl));
242
243   if (SSL_in_init (oc->ssl))
244     return 0;
245
246   /*
247    * Handshake complete
248    */
249   if (!SSL_is_server (oc->ssl))
250     {
251       /*
252        * Verify server certificate
253        */
254       if ((rv = SSL_get_verify_result (oc->ssl)) != X509_V_OK)
255         {
256           TLS_DBG (1, " failed verify: %s\n",
257                    X509_verify_cert_error_string (rv));
258
259           /*
260            * Presence of hostname enforces strict certificate verification
261            */
262           if (ctx->srv_hostname)
263             {
264               tls_notify_app_connected (ctx, /* is failed */ 0);
265               return -1;
266             }
267         }
268       tls_notify_app_connected (ctx, /* is failed */ 0);
269     }
270   else
271     {
272       tls_notify_app_accept (ctx);
273     }
274
275   TLS_DBG (1, "Handshake for %u complete. TLS cipher is %s",
276            oc->openssl_ctx_index, SSL_get_cipher (oc->ssl));
277   return rv;
278 }
279
280 static inline int
281 openssl_ctx_write (tls_ctx_t * ctx, stream_session_t * app_session)
282 {
283   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
284   int wrote = 0, rv, read, max_buf = 100 * TLS_CHUNK_SIZE, max_space;
285   u32 enq_max, deq_max, deq_now, to_write;
286   stream_session_t *tls_session;
287   svm_fifo_t *f;
288
289   f = app_session->server_tx_fifo;
290   deq_max = svm_fifo_max_dequeue (f);
291   if (!deq_max)
292     goto check_tls_fifo;
293
294   max_space = max_buf - BIO_ctrl_pending (oc->rbio);
295   max_space = (max_space < 0) ? 0 : max_space;
296   deq_now = clib_min (deq_max, (u32) max_space);
297   to_write = clib_min (svm_fifo_max_read_chunk (f), deq_now);
298   wrote = SSL_write (oc->ssl, svm_fifo_head (f), to_write);
299   if (wrote <= 0)
300     {
301       tls_add_vpp_q_evt (app_session->server_tx_fifo, FIFO_EVENT_APP_TX);
302       goto check_tls_fifo;
303     }
304   svm_fifo_dequeue_drop (app_session->server_tx_fifo, wrote);
305   if (wrote < deq_now)
306     {
307       to_write = clib_min (svm_fifo_max_read_chunk (f), deq_now - wrote);
308       rv = SSL_write (oc->ssl, svm_fifo_head (f), to_write);
309       if (rv > 0)
310         {
311           svm_fifo_dequeue_drop (app_session->server_tx_fifo, rv);
312           wrote += rv;
313         }
314     }
315
316   if (deq_now < deq_max)
317     tls_add_vpp_q_evt (app_session->server_tx_fifo, FIFO_EVENT_APP_TX);
318
319 check_tls_fifo:
320
321   if (BIO_ctrl_pending (oc->rbio) <= 0)
322     return wrote;
323
324   tls_session = session_get_from_handle (ctx->tls_session_handle);
325   f = tls_session->server_tx_fifo;
326   enq_max = svm_fifo_max_enqueue (f);
327   if (!enq_max)
328     {
329       tls_add_vpp_q_evt (app_session->server_tx_fifo, FIFO_EVENT_APP_TX);
330       return wrote;
331     }
332
333   deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max);
334   read = BIO_read (oc->rbio, svm_fifo_tail (f), deq_now);
335   if (read <= 0)
336     {
337       tls_add_vpp_q_evt (app_session->server_tx_fifo, FIFO_EVENT_APP_TX);
338       return wrote;
339     }
340
341   svm_fifo_enqueue_nocopy (f, read);
342   tls_add_vpp_q_evt (f, FIFO_EVENT_APP_TX);
343
344   if (read < enq_max && BIO_ctrl_pending (oc->rbio) > 0)
345     {
346       deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max - read);
347       read = BIO_read (oc->rbio, svm_fifo_tail (f), deq_now);
348       if (read > 0)
349         svm_fifo_enqueue_nocopy (f, read);
350     }
351
352   if (BIO_ctrl_pending (oc->rbio) > 0)
353     tls_add_vpp_q_evt (app_session->server_tx_fifo, FIFO_EVENT_APP_TX);
354
355   return wrote;
356 }
357
358 static inline int
359 openssl_ctx_read (tls_ctx_t * ctx, stream_session_t * tls_session)
360 {
361   int read, wrote = 0, max_space, max_buf = 100 * TLS_CHUNK_SIZE, rv;
362   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
363   u32 deq_max, enq_max, deq_now, to_read;
364   stream_session_t *app_session;
365   svm_fifo_t *f;
366
367   if (PREDICT_FALSE (SSL_in_init (oc->ssl)))
368     {
369       openssl_ctx_handshake_rx (ctx, tls_session);
370       return 0;
371     }
372
373   f = tls_session->server_rx_fifo;
374   deq_max = svm_fifo_max_dequeue (f);
375   max_space = max_buf - BIO_ctrl_pending (oc->wbio);
376   max_space = max_space < 0 ? 0 : max_space;
377   deq_now = clib_min (deq_max, max_space);
378   if (!deq_now)
379     goto check_app_fifo;
380
381   to_read = clib_min (svm_fifo_max_read_chunk (f), deq_now);
382   wrote = BIO_write (oc->wbio, svm_fifo_head (f), to_read);
383   if (wrote <= 0)
384     {
385       tls_add_vpp_q_evt (tls_session->server_rx_fifo, FIFO_EVENT_BUILTIN_RX);
386       goto check_app_fifo;
387     }
388   svm_fifo_dequeue_drop (f, wrote);
389   if (wrote < deq_now)
390     {
391       to_read = clib_min (svm_fifo_max_read_chunk (f), deq_now - wrote);
392       rv = BIO_write (oc->wbio, svm_fifo_head (f), to_read);
393       if (rv > 0)
394         {
395           svm_fifo_dequeue_drop (f, rv);
396           wrote += rv;
397         }
398     }
399   if (svm_fifo_max_dequeue (f))
400     tls_add_vpp_q_evt (tls_session->server_rx_fifo, FIFO_EVENT_BUILTIN_RX);
401
402 check_app_fifo:
403
404   if (BIO_ctrl_pending (oc->wbio) <= 0)
405     return wrote;
406
407   app_session = session_get_from_handle (ctx->app_session_handle);
408   f = app_session->server_rx_fifo;
409   enq_max = svm_fifo_max_enqueue (f);
410   if (!enq_max)
411     {
412       tls_add_vpp_q_evt (tls_session->server_rx_fifo, FIFO_EVENT_BUILTIN_RX);
413       return wrote;
414     }
415
416   deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max);
417   read = SSL_read (oc->ssl, svm_fifo_tail (f), deq_now);
418   if (read <= 0)
419     {
420       tls_add_vpp_q_evt (tls_session->server_rx_fifo, FIFO_EVENT_BUILTIN_RX);
421       return wrote;
422     }
423   svm_fifo_enqueue_nocopy (f, read);
424   if (read < enq_max && BIO_ctrl_pending (oc->wbio) > 0)
425     {
426       deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max - read);
427       read = SSL_read (oc->ssl, svm_fifo_tail (f), deq_now);
428       if (read > 0)
429         svm_fifo_enqueue_nocopy (f, read);
430     }
431
432   tls_notify_app_enqueue (ctx, app_session);
433   if (BIO_ctrl_pending (oc->wbio) > 0)
434     tls_add_vpp_q_evt (tls_session->server_rx_fifo, FIFO_EVENT_BUILTIN_RX);
435
436   return wrote;
437 }
438
439 static int
440 openssl_ctx_init_client (tls_ctx_t * ctx)
441 {
442   char *ciphers = "ALL:!ADH:!LOW:!EXP:!MD5:!RC4-SHA:!DES-CBC3-SHA:@STRENGTH";
443   long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
444   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
445   openssl_main_t *om = &openssl_main;
446   stream_session_t *tls_session;
447   const SSL_METHOD *method;
448   int rv, err;
449 #ifdef HAVE_OPENSSL_ASYNC
450   openssl_resume_handler *handler;
451 #endif
452
453   method = SSLv23_client_method ();
454   if (method == NULL)
455     {
456       TLS_DBG (1, "SSLv23_method returned null");
457       return -1;
458     }
459
460   oc->ssl_ctx = SSL_CTX_new (method);
461   if (oc->ssl_ctx == NULL)
462     {
463       TLS_DBG (1, "SSL_CTX_new returned null");
464       return -1;
465     }
466
467   SSL_CTX_set_ecdh_auto (oc->ssl_ctx, 1);
468   SSL_CTX_set_mode (oc->ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
469 #ifdef HAVE_OPENSSL_ASYNC
470   if (om->async)
471     SSL_CTX_set_mode (oc->ssl_ctx, SSL_MODE_ASYNC);
472 #endif
473   rv = SSL_CTX_set_cipher_list (oc->ssl_ctx, (const char *) ciphers);
474   if (rv != 1)
475     {
476       TLS_DBG (1, "Couldn't set cipher");
477       return -1;
478     }
479
480   SSL_CTX_set_options (oc->ssl_ctx, flags);
481   SSL_CTX_set_cert_store (oc->ssl_ctx, om->cert_store);
482
483   oc->ssl = SSL_new (oc->ssl_ctx);
484   if (oc->ssl == NULL)
485     {
486       TLS_DBG (1, "Couldn't initialize ssl struct");
487       return -1;
488     }
489
490   oc->rbio = BIO_new (BIO_s_mem ());
491   oc->wbio = BIO_new (BIO_s_mem ());
492
493   BIO_set_mem_eof_return (oc->rbio, -1);
494   BIO_set_mem_eof_return (oc->wbio, -1);
495
496   SSL_set_bio (oc->ssl, oc->wbio, oc->rbio);
497   SSL_set_connect_state (oc->ssl);
498
499   rv = SSL_set_tlsext_host_name (oc->ssl, ctx->srv_hostname);
500   if (rv != 1)
501     {
502       TLS_DBG (1, "Couldn't set hostname");
503       return -1;
504     }
505
506   /*
507    * 2. Do the first steps in the handshake.
508    */
509   TLS_DBG (1, "Initiating handshake for [%u]%u", ctx->c_thread_index,
510            oc->openssl_ctx_index);
511
512   tls_session = session_get_from_handle (ctx->tls_session_handle);
513   while (1)
514     {
515       rv = SSL_do_handshake (oc->ssl);
516       err = SSL_get_error (oc->ssl, rv);
517       openssl_try_handshake_write (oc, tls_session);
518 #ifdef HAVE_OPENSSL_ASYNC
519       if (err == SSL_ERROR_WANT_ASYNC)
520         {
521           handler = (openssl_resume_handler *) openssl_ctx_handshake_rx;
522           vpp_ssl_async_process_event (ctx, handler);
523           break;
524         }
525 #endif
526       if (err != SSL_ERROR_WANT_WRITE)
527         break;
528     }
529
530   TLS_DBG (2, "tls state for [%u]%u is su", ctx->c_thread_index,
531            oc->openssl_ctx_index, SSL_state_string_long (oc->ssl));
532   return 0;
533 }
534
535 static int
536 openssl_ctx_init_server (tls_ctx_t * ctx)
537 {
538   char *ciphers = "ALL:!ADH:!LOW:!EXP:!MD5:!RC4-SHA:!DES-CBC3-SHA:@STRENGTH";
539   long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
540   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
541   stream_session_t *tls_session;
542   const SSL_METHOD *method;
543   application_t *app;
544   int rv, err;
545   BIO *cert_bio;
546 #ifdef HAVE_OPENSSL_ASYNC
547   openssl_main_t *om = &openssl_main;
548   openssl_resume_handler *handler;
549 #endif
550
551   app = application_get (ctx->parent_app_index);
552   if (!app->tls_cert || !app->tls_key)
553     {
554       TLS_DBG (1, "tls cert and/or key not configured %d",
555                ctx->parent_app_index);
556       return -1;
557     }
558
559   method = SSLv23_method ();
560   oc->ssl_ctx = SSL_CTX_new (method);
561   if (!oc->ssl_ctx)
562     {
563       clib_warning ("Unable to create SSL context");
564       return -1;
565     }
566
567   SSL_CTX_set_mode (oc->ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
568 #ifdef HAVE_OPENSSL_ASYNC
569   if (om->async)
570     SSL_CTX_set_mode (oc->ssl_ctx, SSL_MODE_ASYNC);
571 #endif
572   SSL_CTX_set_options (oc->ssl_ctx, flags);
573   SSL_CTX_set_ecdh_auto (oc->ssl_ctx, 1);
574
575   rv = SSL_CTX_set_cipher_list (oc->ssl_ctx, (const char *) ciphers);
576   if (rv != 1)
577     {
578       TLS_DBG (1, "Couldn't set cipher");
579       return -1;
580     }
581
582   /*
583    * Set the key and cert
584    */
585   cert_bio = BIO_new (BIO_s_mem ());
586   BIO_write (cert_bio, app->tls_cert, vec_len (app->tls_cert));
587   oc->srvcert = PEM_read_bio_X509 (cert_bio, NULL, NULL, NULL);
588   if (!oc->srvcert)
589     {
590       clib_warning ("unable to parse certificate");
591       return -1;
592     }
593   SSL_CTX_use_certificate (oc->ssl_ctx, oc->srvcert);
594   BIO_free (cert_bio);
595
596
597   cert_bio = BIO_new (BIO_s_mem ());
598   BIO_write (cert_bio, app->tls_key, vec_len (app->tls_key));
599   oc->pkey = PEM_read_bio_PrivateKey (cert_bio, NULL, NULL, NULL);
600   if (!oc->pkey)
601     {
602       clib_warning ("unable to parse pkey");
603       return -1;
604     }
605   SSL_CTX_use_PrivateKey (oc->ssl_ctx, oc->pkey);
606
607   SSL_CTX_use_PrivateKey (oc->ssl_ctx, oc->pkey);
608   BIO_free (cert_bio);
609
610   /* Start a new connection */
611
612   oc->ssl = SSL_new (oc->ssl_ctx);
613   if (oc->ssl == NULL)
614     {
615       TLS_DBG (1, "Couldn't initialize ssl struct");
616       return -1;
617     }
618
619   oc->rbio = BIO_new (BIO_s_mem ());
620   oc->wbio = BIO_new (BIO_s_mem ());
621
622   BIO_set_mem_eof_return (oc->rbio, -1);
623   BIO_set_mem_eof_return (oc->wbio, -1);
624
625   SSL_set_bio (oc->ssl, oc->wbio, oc->rbio);
626   SSL_set_accept_state (oc->ssl);
627
628   TLS_DBG (1, "Initiating handshake for [%u]%u", ctx->c_thread_index,
629            oc->openssl_ctx_index);
630
631   tls_session = session_get_from_handle (ctx->tls_session_handle);
632   while (1)
633     {
634       rv = SSL_do_handshake (oc->ssl);
635       err = SSL_get_error (oc->ssl, rv);
636       openssl_try_handshake_write (oc, tls_session);
637 #ifdef HAVE_OPENSSL_ASYNC
638       if (err == SSL_ERROR_WANT_ASYNC)
639         {
640           handler = (openssl_resume_handler *) openssl_ctx_handshake_rx;
641           vpp_ssl_async_process_event (ctx, handler);
642           break;
643         }
644 #endif
645       if (err != SSL_ERROR_WANT_WRITE)
646         break;
647     }
648
649   TLS_DBG (2, "tls state for [%u]%u is su", ctx->c_thread_index,
650            oc->openssl_ctx_index, SSL_state_string_long (oc->ssl));
651   return 0;
652 }
653
654 static u8
655 openssl_handshake_is_over (tls_ctx_t * ctx)
656 {
657   openssl_ctx_t *mc = (openssl_ctx_t *) ctx;
658   if (!mc->ssl)
659     return 0;
660   return SSL_is_init_finished (mc->ssl);
661 }
662
663 const static tls_engine_vft_t openssl_engine = {
664   .ctx_alloc = openssl_ctx_alloc,
665   .ctx_free = openssl_ctx_free,
666   .ctx_get = openssl_ctx_get,
667   .ctx_get_w_thread = openssl_ctx_get_w_thread,
668   .ctx_init_server = openssl_ctx_init_server,
669   .ctx_init_client = openssl_ctx_init_client,
670   .ctx_write = openssl_ctx_write,
671   .ctx_read = openssl_ctx_read,
672   .ctx_handshake_is_over = openssl_handshake_is_over,
673 };
674
675 int
676 tls_init_ca_chain (void)
677 {
678   openssl_main_t *om = &openssl_main;
679   tls_main_t *tm = vnet_tls_get_main ();
680   BIO *cert_bio;
681   X509 *testcert;
682   int rv;
683
684   if (access (tm->ca_cert_path, F_OK | R_OK) == -1)
685     {
686       clib_warning ("Could not initialize TLS CA certificates");
687       return -1;
688     }
689
690   if (!(om->cert_store = X509_STORE_new ()))
691     {
692       clib_warning ("failed to create cert store");
693       return -1;
694     }
695
696   rv = X509_STORE_load_locations (om->cert_store, tm->ca_cert_path, 0);
697   if (rv < 0)
698     {
699       clib_warning ("failed to load ca certificate");
700     }
701
702   if (tm->use_test_cert_in_ca)
703     {
704       cert_bio = BIO_new (BIO_s_mem ());
705       BIO_write (cert_bio, test_srv_crt_rsa, test_srv_crt_rsa_len);
706       testcert = PEM_read_bio_X509 (cert_bio, NULL, NULL, NULL);
707       if (!testcert)
708         {
709           clib_warning ("unable to parse certificate");
710           return -1;
711         }
712       X509_STORE_add_cert (om->cert_store, testcert);
713       rv = 0;
714     }
715   return (rv < 0 ? -1 : 0);
716 }
717
718 static clib_error_t *
719 tls_openssl_init (vlib_main_t * vm)
720 {
721   vlib_thread_main_t *vtm = vlib_get_thread_main ();
722   openssl_main_t *om = &openssl_main;
723   clib_error_t *error;
724   u32 num_threads;
725
726   num_threads = 1 /* main thread */  + vtm->n_threads;
727
728   if ((error = vlib_call_init_function (vm, tls_init)))
729     return error;
730
731   SSL_library_init ();
732   SSL_load_error_strings ();
733
734   if (tls_init_ca_chain ())
735     {
736       clib_warning ("failed to initialize TLS CA chain");
737       return 0;
738     }
739
740   vec_validate (om->ctx_pool, num_threads - 1);
741
742   tls_register_engine (&openssl_engine, TLS_ENGINE_OPENSSL);
743
744   om->engine_init = 0;
745
746   return 0;
747 }
748
749 #ifdef HAVE_OPENSSL_ASYNC
750 static clib_error_t *
751 tls_openssl_set_command_fn (vlib_main_t * vm, unformat_input_t * input,
752                             vlib_cli_command_t * cmd)
753 {
754   openssl_main_t *om = &openssl_main;
755   char *engine_name = NULL;
756   char *engine_alg = NULL;
757   u8 engine_name_set = 0;
758   int i;
759
760   /* By present, it is not allowed to configure engine again after running */
761   if (om->engine_init)
762     {
763       clib_warning ("engine has started!\n");
764       return clib_error_return
765         (0, "engine has started, and no config is accepted");
766     }
767
768   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
769     {
770       if (unformat (input, "engine %s", &engine_name))
771         {
772           engine_name_set = 1;
773         }
774       else if (unformat (input, "async"))
775         {
776           om->async = 1;
777           openssl_async_node_enable_disable (1);
778         }
779       else if (unformat (input, "alg %s", &engine_alg))
780         {
781           for (i = 0; i < strnlen (engine_alg, MAX_CRYPTO_LEN); i++)
782             engine_alg[i] = toupper (engine_alg[i]);
783         }
784       else
785         return clib_error_return (0, "failed: unknown input `%U'",
786                                   format_unformat_error, input);
787     }
788
789   /* reset parameters if engine is not configured */
790   if (!engine_name_set)
791     {
792       clib_warning ("No engine provided! \n");
793       om->async = 0;
794     }
795   else
796     {
797       if (!openssl_engine_register (engine_name, engine_alg))
798         {
799           return clib_error_return (0, "failed to register %s polling",
800                                     engine_name);
801         }
802     }
803
804   return 0;
805 }
806
807 /* *INDENT-OFF* */
808 VLIB_CLI_COMMAND (tls_openssl_set_command, static) =
809 {
810   .path = "tls openssl set",
811   .short_help = "tls openssl set [engine <engine name>] [alg [algorithm] [async]",
812   .function = tls_openssl_set_command_fn,
813 };
814 /* *INDENT-ON* */
815 #endif
816
817
818 VLIB_INIT_FUNCTION (tls_openssl_init);
819
820 /* *INDENT-OFF* */
821 VLIB_PLUGIN_REGISTER () = {
822     .version = VPP_BUILD_VER,
823     .description = "openssl based TLS Engine",
824 };
825 /* *INDENT-ON* */
826
827 /*
828  * fd.io coding-style-patch-verification: ON
829  *
830  * Local Variables:
831  * eval: (c-set-style "gnu")
832  * End:
833  */