6e18551f6d5c3a9d8584d7bc67452824edc117b4
[vpp.git] / src / plugins / tlspicotls / tls_picotls.c
1 #include <math.h>
2
3 #include "certs.h"
4 #include "tls_picotls.h"
5
6 picotls_main_t picotls_main;
7
8 #define MAX_QUEUE 12000
9 #define PTLS_MAX_PLAINTEXT_RECORD_SIZE 16384
10
11 static u32
12 picotls_ctx_alloc (void)
13 {
14   u8 thread_id = vlib_get_thread_index ();
15   picotls_main_t *pm = &picotls_main;
16   picotls_ctx_t **ctx;
17
18   pool_get (pm->ctx_pool[thread_id], ctx);
19   if (!(*ctx))
20     *ctx = clib_mem_alloc (sizeof (picotls_ctx_t));
21
22   clib_memset (*ctx, 0, sizeof (picotls_ctx_t));
23   (*ctx)->ctx.c_thread_index = thread_id;
24   (*ctx)->ctx.tls_ctx_engine = CRYPTO_ENGINE_PICOTLS;
25   (*ctx)->ctx.app_session_handle = SESSION_INVALID_HANDLE;
26   (*ctx)->ptls_ctx_idx = ctx - pm->ctx_pool[thread_id];
27   return (*ctx)->ptls_ctx_idx;
28 }
29
30 static void
31 picotls_ctx_free (tls_ctx_t * ctx)
32 {
33   picotls_ctx_t *ptls_ctx = (picotls_ctx_t *) ctx;
34   free (ptls_ctx->rx_content);
35   pool_put_index (picotls_main.ctx_pool[ctx->c_thread_index],
36                   ptls_ctx->ptls_ctx_idx);
37 }
38
39 static u32
40 picotls_listen_ctx_alloc (void)
41 {
42   picotls_main_t *pm = &picotls_main;
43   picotls_listen_ctx_t *ptls_lctx;
44
45   pool_get (pm->lctx_pool, ptls_lctx);
46
47   clib_memset (ptls_lctx, 0, sizeof (picotls_listen_ctx_t));
48   ptls_lctx->ptls_lctx_index = ptls_lctx - pm->lctx_pool;
49   return ptls_lctx->ptls_lctx_index;
50 }
51
52 static void
53 picotls_listen_ctx_free (picotls_listen_ctx_t * lctx)
54 {
55   pool_put_index (picotls_main.lctx_pool, lctx->ptls_lctx_index);
56 }
57
58 tls_ctx_t *
59 picotls_ctx_get (u32 ctx_index)
60 {
61   picotls_ctx_t **ctx;
62   ctx =
63     pool_elt_at_index (picotls_main.ctx_pool[vlib_get_thread_index ()],
64                        ctx_index);
65   return &(*ctx)->ctx;
66 }
67
68 picotls_listen_ctx_t *
69 picotls_lctx_get (u32 lctx_index)
70 {
71   return pool_elt_at_index (picotls_main.lctx_pool, lctx_index);
72 }
73
74 static u8
75 picotls_handshake_is_over (tls_ctx_t * ctx)
76 {
77   picotls_ctx_t *ptls_ctx = (picotls_ctx_t *) ctx;
78   assert (ptls_ctx->tls);
79   return ptls_handshake_is_complete (ptls_ctx->tls);
80 }
81
82 static int
83 picotls_try_handshake_write (picotls_ctx_t * ptls_ctx,
84                              session_t * tls_session, ptls_buffer_t * buf)
85 {
86   u32 enq_max, enq_now;
87   svm_fifo_t *f;
88   int write, buf_left;
89
90   if (buf->off <= 0)
91     return 0;
92
93   f = tls_session->tx_fifo;
94   buf_left = buf->off;
95   enq_max = svm_fifo_max_enqueue_prod (f);
96   if (!enq_max)
97     return 0;
98
99   enq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max);
100   enq_now = clib_min (enq_now, buf_left);
101   write = svm_fifo_enqueue (f, enq_now, buf->base);
102   buf_left -= write;
103   tls_add_vpp_q_tx_evt (tls_session);
104   return write;
105 }
106
107 static int
108 picotls_start_listen (tls_ctx_t * lctx)
109 {
110   picotls_listen_ctx_t *ptls_lctx;
111   ptls_context_t *ptls_ctx;
112   u32 ptls_lctx_idx;
113   app_cert_key_pair_t *ckpair;
114   static ptls_key_exchange_algorithm_t *key_exchange[] = {
115 #ifdef PTLS_OPENSSL_HAVE_X25519
116     &ptls_openssl_x25519,
117 #endif
118 #ifdef PTLS_OPENSSL_HAVE_SECP256r1
119     &ptls_openssl_secp256r1,
120 #endif
121 #ifdef PTLS_OPENSSL_HAVE_SECP384r1
122     &ptls_openssl_secp384r1,
123 #endif
124 #ifdef PTLS_OPENSSL_HAVE_SECP521r1
125     &ptls_openssl_secp521r1
126 #endif
127   };
128
129   ckpair = app_cert_key_pair_get_if_valid (lctx->ckpair_index);
130   if (!ckpair || !ckpair->cert || !ckpair->key)
131     {
132       TLS_DBG (1, "tls cert and/or key not configured %d",
133                ctx->parent_app_wrk_index);
134       return -1;
135     }
136
137   ptls_lctx_idx = picotls_listen_ctx_alloc ();
138   ptls_lctx = picotls_lctx_get (ptls_lctx_idx);
139   ptls_ctx = malloc (sizeof (ptls_context_t));
140   ptls_lctx->ptls_ctx = ptls_ctx;
141
142   memset (ptls_ctx, 0, sizeof (ptls_context_t));
143   ptls_ctx->update_open_count = NULL;
144
145   /*
146    * Process certificate & private key.
147    */
148   load_bio_certificate_chain (ptls_ctx, (char *) ckpair->cert);
149   load_bio_private_key (ptls_ctx, (char *) ckpair->key);
150
151   /* setup protocol related functions */
152   ptls_ctx->key_exchanges = key_exchange;
153   ptls_ctx->random_bytes = ptls_openssl_random_bytes;
154   ptls_ctx->cipher_suites = ptls_openssl_cipher_suites;
155   ptls_ctx->get_time = &ptls_get_time;
156
157   lctx->tls_ssl_ctx = ptls_lctx_idx;
158
159   return 0;
160 }
161
162 static int
163 picotls_stop_listen (tls_ctx_t * lctx)
164 {
165   u32 ptls_lctx_index;
166   picotls_listen_ctx_t *ptls_lctx;
167
168   ptls_lctx_index = lctx->tls_ssl_ctx;
169   ptls_lctx = picotls_lctx_get (ptls_lctx_index);
170
171   picotls_listen_ctx_free (ptls_lctx);
172
173   return 0;
174 }
175
176 static void
177 picotls_handle_handshake_failure (tls_ctx_t * ctx)
178 {
179   session_free (session_get (ctx->c_s_index, ctx->c_thread_index));
180   ctx->no_app_session = 1;
181   ctx->c_s_index = SESSION_INVALID_INDEX;
182   tls_disconnect_transport (ctx);
183 }
184
185 static void
186 picotls_confirm_app_close (tls_ctx_t * ctx)
187 {
188   tls_disconnect_transport (ctx);
189   session_transport_closed_notify (&ctx->connection);
190 }
191
192 static int
193 picotls_transport_close (tls_ctx_t * ctx)
194 {
195   if (!picotls_handshake_is_over (ctx))
196     {
197       picotls_handle_handshake_failure (ctx);
198       return 0;
199     }
200   picotls_ctx_t *ptls_ctx = (picotls_ctx_t *) ctx;
201   ptls_free (ptls_ctx->tls);
202   session_transport_closed_notify (&ctx->connection);
203   return 0;
204 }
205
206 static int
207 picotls_app_close (tls_ctx_t * ctx)
208 {
209   session_t *app_session;
210
211   app_session = session_get_from_handle (ctx->app_session_handle);
212   if (!svm_fifo_max_dequeue_cons (app_session->tx_fifo))
213     picotls_confirm_app_close (ctx);
214   else
215     ctx->app_closed = 1;
216
217   return 0;
218 }
219
220 static inline int
221 picotls_do_handshake (picotls_ctx_t * ptls_ctx, session_t * tls_session,
222                       u8 * input, int input_len)
223 {
224   ptls_t *tls = ptls_ctx->tls;
225   ptls_buffer_t buf;
226   int rv = PTLS_ERROR_IN_PROGRESS;
227   int write = 0, off;
228
229   do
230     {
231       off = 0;
232       do
233         {
234           ptls_buffer_init (&buf, "", 0);
235           size_t consumed = input_len - off;
236           rv = ptls_handshake (tls, &buf, input + off, &consumed, NULL);
237           off += consumed;
238           ptls_ctx->rx_offset += consumed;
239           if ((rv == 0 || rv == PTLS_ERROR_IN_PROGRESS) && buf.off != 0)
240             {
241               write = picotls_try_handshake_write (ptls_ctx, tls_session,
242                                                    &buf);
243             }
244           ptls_buffer_dispose (&buf);
245         }
246       while (rv == PTLS_ERROR_IN_PROGRESS && input_len != off);
247     }
248   while (rv == PTLS_ERROR_IN_PROGRESS);
249
250   return write;
251 }
252
253 static inline int
254 picotls_ctx_read (tls_ctx_t * ctx, session_t * tls_session)
255 {
256   picotls_ctx_t *ptls_ctx = (picotls_ctx_t *) ctx;
257   int from_tls_len = 0, to_app_len = 0, crypto_len, ret;
258   u32 deq_max, deq_now;
259   u32 enq_max, enq_now;
260   ptls_buffer_t _buf, *buf = &_buf;
261   svm_fifo_t *tls_rx_fifo, *app_rx_fifo;
262   session_t *app_session;
263
264   tls_rx_fifo = tls_session->rx_fifo;
265
266   if (!picotls_handshake_is_over (ctx))
267     {
268       deq_max = svm_fifo_max_dequeue_cons (tls_rx_fifo);
269       deq_now = clib_min (deq_max, svm_fifo_max_read_chunk (tls_rx_fifo));
270       if (!deq_now)
271         goto done_hs;
272
273       from_tls_len +=
274         svm_fifo_dequeue (tls_rx_fifo, deq_now, TLS_RX_LEN (ptls_ctx));
275       if (from_tls_len <= 0)
276         {
277           tls_add_vpp_q_builtin_rx_evt (tls_session);
278           goto done_hs;
279         }
280       if (from_tls_len < deq_max)
281         {
282           deq_now = clib_min (svm_fifo_max_read_chunk (tls_rx_fifo),
283                               deq_max - from_tls_len);
284           from_tls_len += svm_fifo_dequeue (tls_rx_fifo, deq_now,
285                                             TLS_RX_LEN (ptls_ctx));
286         }
287       ptls_ctx->rx_len += from_tls_len;
288       picotls_do_handshake (ptls_ctx, tls_session, TLS_RX_OFFSET (ptls_ctx),
289                             from_tls_len);
290       if (picotls_handshake_is_over (ctx))
291         tls_notify_app_accept (ctx);
292
293     done_hs:
294       if (!TLS_RX_IS_LEFT (ptls_ctx))
295         return 0;
296     }
297
298   app_session = session_get_from_handle (ctx->app_session_handle);
299   app_rx_fifo = app_session->rx_fifo;
300
301   ptls_buffer_init (buf, "", 0);
302
303   if (!TLS_RX_IS_LEFT (ptls_ctx))
304     {
305       deq_max = svm_fifo_max_dequeue_cons (tls_rx_fifo);
306       if (!deq_max)
307         goto app_fifo;
308
309       deq_now = clib_min (deq_max, svm_fifo_max_read_chunk (tls_rx_fifo));
310
311       from_tls_len =
312         svm_fifo_dequeue (tls_rx_fifo, deq_now, TLS_RX_LEN (ptls_ctx));
313
314       if (from_tls_len < deq_max)
315         {
316           deq_now =
317             clib_min (svm_fifo_max_read_chunk (tls_rx_fifo),
318                       deq_max - from_tls_len);
319           from_tls_len +=
320             svm_fifo_dequeue (tls_rx_fifo, deq_now, TLS_RX_LEN (ptls_ctx));
321         }
322
323       ptls_ctx->rx_len += from_tls_len;
324
325       if (from_tls_len < deq_max)
326         tls_add_vpp_q_builtin_rx_evt (tls_session);
327     }
328
329 app_fifo:
330
331   enq_max = svm_fifo_max_enqueue_prod (app_rx_fifo);
332   if (!enq_max)
333     {
334       tls_add_vpp_q_builtin_rx_evt (tls_session);
335       goto final;
336     }
337
338   crypto_len = clib_min (enq_max, TLS_RX_LEFT_LEN (ptls_ctx));
339   int off = 0;
340
341   do
342     {
343       size_t consumed = crypto_len - off;
344       ret =
345         ptls_receive (ptls_ctx->tls, buf,
346                       TLS_RX_OFFSET (ptls_ctx), &consumed);
347       off += consumed;
348       ptls_ctx->rx_offset += consumed;
349     }
350   while (ret == 0 && off < crypto_len);
351
352   if (ret == 0)
353     {
354       enq_now = clib_min (buf->off, svm_fifo_max_write_chunk (app_rx_fifo));
355       to_app_len = svm_fifo_enqueue (app_rx_fifo, enq_now, buf->base);
356       if (to_app_len < buf->off)
357         {
358           enq_now =
359             clib_min (svm_fifo_max_write_chunk (app_rx_fifo),
360                       buf->off - to_app_len);
361           to_app_len +=
362             svm_fifo_enqueue (app_rx_fifo, enq_now, buf->base + to_app_len);
363         }
364     }
365
366   if (ptls_ctx->rx_len != 0 && !TLS_RX_IS_LEFT (ptls_ctx))
367     {
368       ptls_ctx->rx_len = 0;
369       ptls_ctx->rx_offset = 0;
370     }
371
372 final:
373   ptls_buffer_dispose (buf);
374
375   if (app_session->session_state >= SESSION_STATE_READY)
376     tls_notify_app_enqueue (ctx, app_session);
377
378   if (TLS_RX_IS_LEFT (ptls_ctx))
379     tls_add_vpp_q_builtin_rx_evt (tls_session);
380
381   return from_tls_len;
382 }
383
384 static inline int
385 picotls_content_process (ptls_t * tls, svm_fifo_t * src_fifo,
386                          svm_fifo_t * dst_fifo, int content_len,
387                          int total_record_overhead, int is_no_copy)
388 {
389   ptls_buffer_t _buf, *buf = &_buf;
390   int total_length = content_len + total_record_overhead;
391   int to_dst_len;
392   if (is_no_copy)
393     {
394       ptls_buffer_init (buf, svm_fifo_tail (dst_fifo), total_length);
395       ptls_send (tls, buf, svm_fifo_head (src_fifo), content_len);
396
397       assert (!buf->is_allocated);
398       assert (buf->base == svm_fifo_tail (dst_fifo));
399
400       svm_fifo_dequeue_drop (src_fifo, content_len);
401       svm_fifo_enqueue_nocopy (dst_fifo, buf->off);
402       to_dst_len = buf->off;
403     }
404   else
405     {
406       uint8_t *buf_content = malloc (total_length);
407       ptls_buffer_init (buf, buf_content, total_length);
408
409       ptls_send (tls, buf, svm_fifo_head (src_fifo), content_len);
410       svm_fifo_dequeue_drop (src_fifo, content_len);
411
412       to_dst_len = svm_fifo_enqueue (dst_fifo, buf->off, buf->base);
413       if (to_dst_len < buf->off)
414         to_dst_len +=
415           svm_fifo_enqueue (dst_fifo, buf->off - to_dst_len,
416                             buf->base + to_dst_len);
417       ptls_buffer_dispose (buf);
418     }
419   return to_dst_len;
420 }
421
422 static inline int
423 picotls_ctx_write (tls_ctx_t * ctx, session_t * app_session)
424 {
425   picotls_ctx_t *ptls_ctx = (picotls_ctx_t *) ctx;
426   u32 deq_max, deq_now;
427   u32 enq_max, enq_now;
428   int from_app_len = 0, to_tls_len = 0, is_nocopy = 0;
429   svm_fifo_t *tls_tx_fifo, *app_tx_fifo;
430   session_t *tls_session;
431
432   int record_overhead = ptls_get_record_overhead (ptls_ctx->tls);
433   int num_records, total_overhead;
434
435   tls_session = session_get_from_handle (ctx->tls_session_handle);
436   tls_tx_fifo = tls_session->tx_fifo;
437   app_tx_fifo = app_session->tx_fifo;
438
439   deq_max = svm_fifo_max_dequeue_cons (app_tx_fifo);
440   if (!deq_max)
441     return deq_max;
442   deq_now = clib_min (deq_max, svm_fifo_max_read_chunk (app_tx_fifo));
443
444   enq_max = svm_fifo_max_enqueue_prod (tls_tx_fifo);
445
446     /** There is no engough enqueue space for one record **/
447   if (enq_max < record_overhead)
448     {
449       tls_add_vpp_q_builtin_tx_evt (app_session);
450       return 0;
451     }
452
453   enq_now = clib_min (enq_max, svm_fifo_max_write_chunk (tls_tx_fifo));
454
455     /** Allowed to execute no-copy crypto operation **/
456   if (enq_now > record_overhead)
457     {
458       is_nocopy = 1;
459       from_app_len = clib_min (deq_now, enq_now);
460       num_records =
461         ceil ((f64) from_app_len / PTLS_MAX_PLAINTEXT_RECORD_SIZE);
462       total_overhead = num_records * record_overhead;
463       if (from_app_len + total_overhead > enq_now)
464         from_app_len = enq_now - total_overhead;
465
466
467     }
468   else
469     {
470       from_app_len = clib_min (deq_now, enq_max);
471       num_records =
472         ceil ((f64) from_app_len / PTLS_MAX_PLAINTEXT_RECORD_SIZE);
473       total_overhead = num_records * record_overhead;
474       if (from_app_len + total_overhead > enq_max)
475         from_app_len = enq_max - total_overhead;
476
477     }
478
479   to_tls_len =
480     picotls_content_process (ptls_ctx->tls, app_tx_fifo, tls_tx_fifo,
481                              from_app_len, total_overhead, is_nocopy);
482
483   if (svm_fifo_needs_deq_ntf (app_tx_fifo, from_app_len))
484     session_dequeue_notify (app_session);
485
486   if (to_tls_len)
487     tls_add_vpp_q_tx_evt (tls_session);
488
489   if (from_app_len < deq_max)
490     tls_add_vpp_q_builtin_tx_evt (app_session);
491
492   if (ctx->app_closed)
493     picotls_app_close (ctx);
494
495   return 0;
496 }
497
498 static int
499 picotls_ctx_init_server (tls_ctx_t * ctx)
500 {
501   picotls_ctx_t *ptls_ctx = (picotls_ctx_t *) ctx;
502   u32 ptls_lctx_idx = ctx->tls_ssl_ctx;
503   picotls_listen_ctx_t *ptls_lctx;
504
505   ptls_lctx = picotls_lctx_get (ptls_lctx_idx);
506   ptls_ctx->tls = ptls_new (ptls_lctx->ptls_ctx, 1);
507   if (ptls_ctx->tls == NULL)
508     {
509       TLS_DBG (1, "Failed to initialize ptls_ssl structure");
510       return -1;
511     }
512
513   ptls_ctx->rx_len = 0;
514   ptls_ctx->rx_offset = 0;
515
516   ptls_ctx->rx_content = malloc (MAX_QUEUE);
517
518   return 0;
519 }
520
521 tls_ctx_t *
522 picotls_ctx_get_w_thread (u32 ctx_index, u8 thread_index)
523 {
524   picotls_ctx_t **ctx;
525   ctx = pool_elt_at_index (picotls_main.ctx_pool[thread_index], ctx_index);
526   return &(*ctx)->ctx;
527 }
528
529 const static tls_engine_vft_t picotls_engine = {
530   .ctx_alloc = picotls_ctx_alloc,
531   .ctx_free = picotls_ctx_free,
532   .ctx_get = picotls_ctx_get,
533   .ctx_get_w_thread = picotls_ctx_get_w_thread,
534   .ctx_handshake_is_over = picotls_handshake_is_over,
535   .ctx_start_listen = picotls_start_listen,
536   .ctx_stop_listen = picotls_stop_listen,
537   .ctx_init_server = picotls_ctx_init_server,
538   .ctx_read = picotls_ctx_read,
539   .ctx_write = picotls_ctx_write,
540   .ctx_transport_close = picotls_transport_close,
541   .ctx_app_close = picotls_app_close,
542 };
543
544 static clib_error_t *
545 tls_picotls_init (vlib_main_t * vm)
546 {
547   vlib_thread_main_t *vtm = vlib_get_thread_main ();
548   picotls_main_t *pm = &picotls_main;
549   clib_error_t *error = 0;
550   u32 num_threads;
551
552   num_threads = 1 + vtm->n_threads;
553
554   vec_validate (pm->ctx_pool, num_threads - 1);
555
556   tls_register_engine (&picotls_engine, CRYPTO_ENGINE_PICOTLS);
557
558   return error;
559 }
560
561 /* *INDENT-OFF* */
562 VLIB_INIT_FUNCTION (tls_picotls_init) = {
563   .runs_after = VLIB_INITS ("tls_init"),
564 };
565 /* *INDENT-ON* */
566
567 /* *INDENT-OFF* */
568 VLIB_PLUGIN_REGISTER () = {
569   .version = VPP_BUILD_VER,
570   .description = "Transport Layer Security (TLS) Engine, Picotls Based",
571 };
572 /* *INDENT-ON* */
573
574 /*
575  * fd.io coding-style-patch-verification: ON
576  *
577  * Local Variables:
578  * eval: (c-set-style "gnu")
579  * End:
580  */