3 #include <tlspicotls/certs.h>
4 #include <tlspicotls/tls_picotls.h>
5 #include <tlspicotls/pico_vpp_crypto.h>
7 picotls_main_t picotls_main;
9 #define MAX_QUEUE 12000
10 #define PTLS_MAX_PLAINTEXT_RECORD_SIZE 16384
12 static ptls_key_exchange_algorithm_t *default_key_exchange[] = {
13 #ifdef PTLS_OPENSSL_HAVE_X25519
16 #ifdef PTLS_OPENSSL_HAVE_SECP256R1
17 &ptls_openssl_secp256r1,
19 #ifdef PTLS_OPENSSL_HAVE_SECP384R1
20 &ptls_openssl_secp384r1,
22 #ifdef PTLS_OPENSSL_HAVE_SECP521R1
23 &ptls_openssl_secp521r1
28 picotls_ctx_alloc (void)
30 u32 thread_id = vlib_get_thread_index ();
31 picotls_main_t *pm = &picotls_main;
34 pool_get_aligned_safe (pm->ctx_pool[thread_id], ctx, CLIB_CACHE_LINE_BYTES);
36 *ctx = clib_mem_alloc (sizeof (picotls_ctx_t));
38 clib_memset (*ctx, 0, sizeof (picotls_ctx_t));
39 (*ctx)->ctx.c_thread_index = thread_id;
40 (*ctx)->ctx.tls_ctx_engine = CRYPTO_ENGINE_PICOTLS;
41 (*ctx)->ctx.app_session_handle = SESSION_INVALID_HANDLE;
42 (*ctx)->ptls_ctx_idx = ctx - pm->ctx_pool[thread_id];
43 return (*ctx)->ptls_ctx_idx;
47 picotls_ctx_free (tls_ctx_t * ctx)
49 picotls_ctx_t *ptls_ctx = (picotls_ctx_t *) ctx;
50 vec_free (ptls_ctx->rx_content);
51 ptls_free (ptls_ctx->tls);
52 pool_put_index (picotls_main.ctx_pool[ctx->c_thread_index],
53 ptls_ctx->ptls_ctx_idx);
57 picotls_listen_ctx_alloc (void)
59 picotls_main_t *pm = &picotls_main;
60 picotls_listen_ctx_t *ptls_lctx;
62 pool_get (pm->lctx_pool, ptls_lctx);
64 clib_memset (ptls_lctx, 0, sizeof (picotls_listen_ctx_t));
65 ptls_lctx->ptls_lctx_index = ptls_lctx - pm->lctx_pool;
66 return ptls_lctx->ptls_lctx_index;
70 picotls_listen_ctx_free (picotls_listen_ctx_t * lctx)
72 pool_put_index (picotls_main.lctx_pool, lctx->ptls_lctx_index);
76 picotls_ctx_get (u32 ctx_index)
80 pool_elt_at_index (picotls_main.ctx_pool[vlib_get_thread_index ()],
85 picotls_listen_ctx_t *
86 picotls_lctx_get (u32 lctx_index)
88 return pool_elt_at_index (picotls_main.lctx_pool, lctx_index);
92 picotls_handshake_is_over (tls_ctx_t * ctx)
94 picotls_ctx_t *ptls_ctx = (picotls_ctx_t *) ctx;
95 assert (ptls_ctx->tls);
96 return ptls_handshake_is_complete (ptls_ctx->tls);
100 picotls_try_handshake_write (picotls_ctx_t * ptls_ctx,
101 session_t * tls_session, ptls_buffer_t * buf)
103 u32 enq_max, enq_now;
110 f = tls_session->tx_fifo;
112 enq_max = svm_fifo_max_enqueue_prod (f);
116 enq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max);
117 enq_now = clib_min (enq_now, buf_left);
118 write = svm_fifo_enqueue (f, enq_now, buf->base);
120 tls_add_vpp_q_tx_evt (tls_session);
125 picotls_start_listen (tls_ctx_t * lctx)
127 picotls_listen_ctx_t *ptls_lctx;
128 ptls_context_t *ptls_ctx;
130 app_cert_key_pair_t *ckpair;
132 ckpair = app_cert_key_pair_get_if_valid (lctx->ckpair_index);
133 if (!ckpair || !ckpair->cert || !ckpair->key)
135 TLS_DBG (1, "tls cert and/or key not configured %d",
136 lctx->parent_app_wrk_index);
140 ptls_lctx_idx = picotls_listen_ctx_alloc ();
141 ptls_lctx = picotls_lctx_get (ptls_lctx_idx);
142 ptls_ctx = malloc (sizeof (ptls_context_t));
143 ptls_lctx->ptls_ctx = ptls_ctx;
145 memset (ptls_ctx, 0, sizeof (ptls_context_t));
146 ptls_ctx->update_open_count = NULL;
149 * Process certificate & private key.
151 load_bio_certificate_chain (ptls_ctx, (char *) ckpair->cert);
152 load_bio_private_key (ptls_ctx, (char *) ckpair->key);
154 /* setup protocol related functions */
155 ptls_ctx->key_exchanges = default_key_exchange;
156 ptls_ctx->random_bytes = ptls_openssl_random_bytes;
157 ptls_ctx->cipher_suites = ptls_vpp_crypto_cipher_suites;
158 ptls_ctx->get_time = &ptls_get_time;
160 lctx->tls_ssl_ctx = ptls_lctx_idx;
166 picotls_stop_listen (tls_ctx_t * lctx)
169 picotls_listen_ctx_t *ptls_lctx;
171 ptls_lctx_index = lctx->tls_ssl_ctx;
172 ptls_lctx = picotls_lctx_get (ptls_lctx_index);
174 picotls_listen_ctx_free (ptls_lctx);
180 picotls_handle_handshake_failure (tls_ctx_t * ctx)
182 ctx->flags |= TLS_CONN_F_NO_APP_SESSION;
183 ctx->c_s_index = SESSION_INVALID_INDEX;
184 tls_disconnect_transport (ctx);
188 picotls_confirm_app_close (tls_ctx_t * ctx)
190 tls_disconnect_transport (ctx);
191 session_transport_closed_notify (&ctx->connection);
195 picotls_transport_close (tls_ctx_t * ctx)
197 if (!picotls_handshake_is_over (ctx))
199 picotls_handle_handshake_failure (ctx);
202 session_transport_closing_notify (&ctx->connection);
207 picotls_transport_reset (tls_ctx_t *ctx)
209 if (!picotls_handshake_is_over (ctx))
211 picotls_handle_handshake_failure (ctx);
215 session_transport_reset_notify (&ctx->connection);
216 session_transport_closed_notify (&ctx->connection);
217 tls_disconnect_transport (ctx);
223 picotls_app_close (tls_ctx_t * ctx)
225 session_t *app_session;
227 app_session = session_get_from_handle (ctx->app_session_handle);
228 if (!svm_fifo_max_dequeue_cons (app_session->tx_fifo))
229 picotls_confirm_app_close (ctx);
231 ctx->flags |= TLS_CONN_F_APP_CLOSED;
237 picotls_do_handshake (picotls_ctx_t *ptls_ctx, session_t *tcp_session)
239 int rv = PTLS_ERROR_IN_PROGRESS, write = 0, i = 0, read = 0, len;
240 svm_fifo_t *tcp_rx_fifo = tcp_session->rx_fifo;
241 ptls_buffer_t *buf = &ptls_ctx->read_buffer;
242 u32 n_segs = 2, max_len = 16384;
243 ptls_t *tls = ptls_ctx->tls;
244 svm_fifo_seg_t fs[n_segs];
247 ptls_buffer_init (buf, "", 0);
249 len = svm_fifo_segments (tcp_rx_fifo, 0, fs, &n_segs, max_len);
253 while (read < len && i < n_segs)
256 rv = ptls_handshake (tls, buf, fs[i].data, &deq_now, NULL);
258 write += picotls_try_handshake_write (ptls_ctx, tcp_session, buf);
261 if (!(rv == 0 || rv == PTLS_ERROR_IN_PROGRESS))
263 clib_error ("unexpected error %u", rv);
270 if (deq_now < fs[i].len)
272 fs[i].data += deq_now;
273 fs[i].len -= deq_now;
280 svm_fifo_dequeue_drop (tcp_rx_fifo, read);
282 ptls_buffer_dispose (buf);
288 ptls_copy_buf_to_fs (ptls_buffer_t *buf, u32 to_copy, svm_fifo_seg_t *fs,
289 u32 *fs_idx, u32 max_fs)
295 if (fs[idx].len <= to_copy)
297 clib_memcpy_fast (fs[idx].data, buf->base + (buf->off - to_copy),
299 to_copy -= fs[idx].len;
301 /* no more space in the app's rx fifo */
307 clib_memcpy_fast (fs[idx].data, buf->base + (buf->off - to_copy),
309 fs[idx].len -= to_copy;
310 fs[idx].data += to_copy;
321 ptls_tcp_to_app_write (picotls_ctx_t *ptls_ctx, svm_fifo_t *app_rx_fifo,
322 svm_fifo_t *tcp_rx_fifo)
324 u32 ai = 0, thread_index, min_buf_len, to_copy, left, wrote = 0;
325 ptls_buffer_t *buf = &ptls_ctx->read_buffer;
326 int ret, i = 0, read = 0, tcp_len, n_fs_app;
327 u32 n_segs = 4, max_len = 1 << 16;
328 svm_fifo_seg_t tcp_fs[n_segs], app_fs[n_segs];
329 picotls_main_t *pm = &picotls_main;
333 thread_index = ptls_ctx->ctx.c_thread_index;
335 n_fs_app = svm_fifo_provision_chunks (app_rx_fifo, app_fs, n_segs, max_len);
339 tcp_len = svm_fifo_segments (tcp_rx_fifo, 0, tcp_fs, &n_segs, max_len);
343 if (ptls_ctx->read_buffer_offset)
345 to_copy = buf->off - ptls_ctx->read_buffer_offset;
346 left = ptls_copy_buf_to_fs (buf, to_copy, app_fs, &ai, n_fs_app);
347 wrote += to_copy - left;
350 ptls_ctx->read_buffer_offset = buf->off - left;
353 ptls_ctx->read_buffer_offset = 0;
356 while (ai < n_fs_app && read < tcp_len)
358 deq_now = clib_min (tcp_fs[i].len, tcp_len - read);
359 min_buf_len = deq_now + (16 << 10);
360 is_nocopy = app_fs[ai].len < min_buf_len ? 0 : 1;
363 ptls_buffer_init (buf, app_fs[ai].data, app_fs[ai].len);
364 ret = ptls_receive (ptls_ctx->tls, buf, tcp_fs[i].data, &deq_now);
365 assert (ret == 0 || ret == PTLS_ERROR_IN_PROGRESS);
368 if (buf->off == app_fs[ai].len)
374 app_fs[ai].len -= buf->off;
375 app_fs[ai].data += buf->off;
380 vec_validate (pm->rx_bufs[thread_index], min_buf_len);
381 ptls_buffer_init (buf, pm->rx_bufs[thread_index], min_buf_len);
382 ret = ptls_receive (ptls_ctx->tls, buf, tcp_fs[i].data, &deq_now);
383 assert (ret == 0 || ret == PTLS_ERROR_IN_PROGRESS);
385 left = ptls_copy_buf_to_fs (buf, buf->off, app_fs, &ai, n_fs_app);
388 ptls_ctx->read_buffer_offset = 0;
393 ptls_ctx->read_buffer_offset = buf->off - left;
394 wrote += ptls_ctx->read_buffer_offset;
398 assert (deq_now <= tcp_fs[i].len);
400 if (deq_now < tcp_fs[i].len)
402 tcp_fs[i].data += deq_now;
403 tcp_fs[i].len -= deq_now;
413 svm_fifo_dequeue_drop (tcp_rx_fifo, read);
414 if (svm_fifo_needs_deq_ntf (tcp_rx_fifo, read))
416 svm_fifo_clear_deq_ntf (tcp_rx_fifo);
417 session_send_io_evt_to_thread (tcp_rx_fifo, SESSION_IO_EVT_RX);
422 svm_fifo_enqueue_nocopy (app_rx_fifo, wrote);
428 picotls_ctx_read (tls_ctx_t *ctx, session_t *tcp_session)
430 picotls_ctx_t *ptls_ctx = (picotls_ctx_t *) ctx;
431 svm_fifo_t *tcp_rx_fifo;
432 session_t *app_session;
435 if (PREDICT_FALSE (!ptls_handshake_is_complete (ptls_ctx->tls)))
437 picotls_do_handshake (ptls_ctx, tcp_session);
438 if (picotls_handshake_is_over (ctx))
440 if (ptls_is_server (ptls_ctx->tls))
442 if (tls_notify_app_accept (ctx))
444 ctx->c_s_index = SESSION_INVALID_INDEX;
445 tls_disconnect_transport (ctx);
451 tls_notify_app_connected (ctx, SESSION_E_NONE);
455 ctx->flags |= TLS_CONN_F_HS_DONE;
456 if (!svm_fifo_max_dequeue (tcp_session->rx_fifo))
460 tcp_rx_fifo = tcp_session->rx_fifo;
461 app_session = session_get_from_handle (ctx->app_session_handle);
462 wrote = ptls_tcp_to_app_write (ptls_ctx, app_session->rx_fifo, tcp_rx_fifo);
465 tls_notify_app_enqueue (ctx, app_session);
467 if (ptls_ctx->read_buffer_offset || svm_fifo_max_dequeue (tcp_rx_fifo))
468 tls_add_vpp_q_builtin_rx_evt (tcp_session);
474 ptls_compute_deq_len (picotls_ctx_t *ptls_ctx, u32 dst_chunk, u32 src_chunk,
475 u32 dst_space, u8 *is_nocopy)
477 int record_overhead = ptls_get_record_overhead (ptls_ctx->tls);
479 u32 deq_len, total_overhead;
481 if (dst_chunk >= clib_min (8192, src_chunk + record_overhead))
484 deq_len = clib_min (src_chunk, dst_chunk);
485 num_records = ceil ((f64) deq_len / PTLS_MAX_PLAINTEXT_RECORD_SIZE);
486 total_overhead = num_records * record_overhead;
487 if (deq_len + total_overhead > dst_chunk)
488 deq_len = dst_chunk - total_overhead;
492 deq_len = clib_min (src_chunk, dst_space);
493 num_records = ceil ((f64) deq_len / PTLS_MAX_PLAINTEXT_RECORD_SIZE);
494 total_overhead = num_records * record_overhead;
495 if (deq_len + total_overhead > dst_space)
496 deq_len = dst_space - total_overhead;
503 ptls_app_to_tcp_write (picotls_ctx_t *ptls_ctx, session_t *app_session,
504 svm_fifo_t *tcp_tx_fifo, u32 max_len)
506 u32 wrote = 0, max_enq, thread_index, app_buf_len, left, ti = 0;
507 int read = 0, rv, i = 0, len, n_tcp_segs = 4, deq_len;
508 u32 n_app_segs = 2, min_chunk = 2048;
509 svm_fifo_seg_t app_fs[n_app_segs], tcp_fs[n_tcp_segs];
510 picotls_main_t *pm = &picotls_main;
511 ptls_buffer_t _buf, *buf = &_buf;
512 svm_fifo_t *app_tx_fifo;
513 u8 is_nocopy, *app_buf;
516 thread_index = app_session->thread_index;
517 app_tx_fifo = app_session->tx_fifo;
519 len = svm_fifo_segments (app_tx_fifo, 0, app_fs, &n_app_segs, max_len);
523 n_tcp_segs = svm_fifo_provision_chunks (tcp_tx_fifo, tcp_fs, n_tcp_segs,
528 while ((left = len - read) && ti < n_tcp_segs)
530 /* If we wrote something and are left with few bytes, postpone write
531 * as we may be able to encrypt a bigger chunk next time */
532 if (wrote && left < min_chunk)
535 /* Avoid short records if possible */
536 if (app_fs[i].len < min_chunk && min_chunk < left)
538 app_buf_len = app_fs[i].len + app_fs[i + 1].len;
539 app_buf = pm->rx_bufs[thread_index];
540 vec_validate (pm->rx_bufs[thread_index], app_buf_len);
541 clib_memcpy_fast (pm->rx_bufs[thread_index], app_fs[i].data,
543 clib_memcpy_fast (pm->rx_bufs[thread_index] + app_fs[i].len,
544 app_fs[i + 1].data, app_buf_len - app_fs[i].len);
545 first_chunk_len = app_fs[i].len;
550 app_buf = app_fs[i].data;
551 app_buf_len = app_fs[i].len;
556 max_enq = tcp_fs[ti].len;
557 max_enq += ti < (n_tcp_segs - 1) ? tcp_fs[ti + 1].len : 0;
559 deq_len = ptls_compute_deq_len (ptls_ctx, tcp_fs[ti].len, app_buf_len,
560 max_enq, &is_nocopy);
563 ptls_buffer_init (buf, tcp_fs[ti].data, tcp_fs[ti].len);
564 rv = ptls_send (ptls_ctx->tls, buf, app_buf, deq_len);
569 tcp_fs[ti].len -= buf->off;
570 tcp_fs[ti].data += buf->off;
576 vec_validate (pm->tx_bufs[thread_index], max_enq);
577 ptls_buffer_init (buf, pm->tx_bufs[thread_index], max_enq);
578 rv = ptls_send (ptls_ctx->tls, buf, app_buf, deq_len);
583 left = ptls_copy_buf_to_fs (buf, buf->off, tcp_fs, &ti, n_tcp_segs);
588 ASSERT (deq_len >= first_chunk_len);
590 if (deq_len == app_buf_len)
596 app_fs[i].len -= deq_len - first_chunk_len;
597 app_fs[i].data += deq_len - first_chunk_len;
603 svm_fifo_dequeue_drop (app_tx_fifo, read);
604 if (svm_fifo_needs_deq_ntf (app_tx_fifo, read))
605 session_dequeue_notify (app_session);
610 svm_fifo_enqueue_nocopy (tcp_tx_fifo, wrote);
611 if (svm_fifo_set_event (tcp_tx_fifo))
612 session_send_io_evt_to_thread (tcp_tx_fifo, SESSION_IO_EVT_TX);
619 picotls_ctx_write (tls_ctx_t *ctx, session_t *app_session,
620 transport_send_params_t *sp)
622 picotls_ctx_t *ptls_ctx = (picotls_ctx_t *) ctx;
623 u32 deq_max, deq_now, enq_max, enq_buf, wrote = 0;
624 svm_fifo_t *tcp_tx_fifo;
625 session_t *tcp_session;
627 tcp_session = session_get_from_handle (ctx->tls_session_handle);
628 tcp_tx_fifo = tcp_session->tx_fifo;
630 enq_max = svm_fifo_max_enqueue_prod (tcp_tx_fifo);
634 deq_max = svm_fifo_max_dequeue_cons (app_session->tx_fifo);
635 deq_max = clib_min (deq_max, enq_max);
639 deq_now = clib_min (deq_max, sp->max_burst_size);
640 wrote = ptls_app_to_tcp_write (ptls_ctx, app_session, tcp_tx_fifo, deq_now);
644 if (ctx->flags & TLS_CONN_F_APP_CLOSED)
645 picotls_app_close (ctx);
647 /* Deschedule and wait for deq notification if fifo is almost full */
648 enq_buf = clib_min (svm_fifo_size (tcp_tx_fifo) / 2, TLSP_MIN_ENQ_SPACE);
649 if (enq_max < wrote + enq_buf)
651 svm_fifo_add_want_deq_ntf (tcp_tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
652 transport_connection_deschedule (&ctx->connection);
653 sp->flags |= TRANSPORT_SND_F_DESCHED;
656 /* Request tx reschedule of the app session */
657 app_session->flags |= SESSION_F_CUSTOM_TX;
663 picotls_ctx_init_server (tls_ctx_t * ctx)
665 picotls_ctx_t *ptls_ctx = (picotls_ctx_t *) ctx;
666 u32 ptls_lctx_idx = ctx->tls_ssl_ctx;
667 picotls_listen_ctx_t *ptls_lctx;
669 ptls_lctx = picotls_lctx_get (ptls_lctx_idx);
670 ptls_ctx->tls = ptls_new (ptls_lctx->ptls_ctx, 1);
671 if (ptls_ctx->tls == NULL)
673 TLS_DBG (1, "Failed to initialize ptls_ssl structure");
677 ptls_ctx->rx_len = 0;
678 ptls_ctx->rx_offset = 0;
684 picotls_ctx_init_client (tls_ctx_t *ctx)
686 picotls_ctx_t *ptls_ctx = (picotls_ctx_t *) ctx;
687 picotls_main_t *pm = &picotls_main;
688 ptls_context_t *client_ptls_ctx = pm->client_ptls_ctx;
689 ptls_handshake_properties_t hsprop = { { { { NULL } } } };
691 session_t *tls_session = session_get_from_handle (ctx->tls_session_handle);
692 ptls_buffer_t hs_buf;
694 ptls_ctx->tls = ptls_new (client_ptls_ctx, 0);
695 if (ptls_ctx->tls == NULL)
697 TLS_DBG (1, "Failed to initialize ptls_ssl structure");
701 ptls_ctx->rx_len = 0;
702 ptls_ctx->rx_offset = 0;
704 ptls_buffer_init (&hs_buf, "", 0);
705 if (ptls_handshake (ptls_ctx->tls, &hs_buf, NULL, NULL, &hsprop) !=
706 PTLS_ERROR_IN_PROGRESS)
708 TLS_DBG (1, "Failed to initialize tls connection");
711 picotls_try_handshake_write (ptls_ctx, tls_session, &hs_buf);
713 ptls_buffer_dispose (&hs_buf);
719 picotls_ctx_get_w_thread (u32 ctx_index, u8 thread_index)
722 ctx = pool_elt_at_index (picotls_main.ctx_pool[thread_index], ctx_index);
727 picotls_init_client_ptls_ctx (ptls_context_t **client_ptls_ctx)
729 *client_ptls_ctx = clib_mem_alloc (sizeof (ptls_context_t));
730 memset (*client_ptls_ctx, 0, sizeof (ptls_context_t));
732 (*client_ptls_ctx)->update_open_count = NULL;
733 (*client_ptls_ctx)->key_exchanges = default_key_exchange;
734 (*client_ptls_ctx)->random_bytes = ptls_openssl_random_bytes;
735 (*client_ptls_ctx)->cipher_suites = ptls_vpp_crypto_cipher_suites;
736 (*client_ptls_ctx)->get_time = &ptls_get_time;
742 picotls_reinit_ca_chain (void)
744 /* Not supported yet */
748 const static tls_engine_vft_t picotls_engine = {
749 .ctx_alloc = picotls_ctx_alloc,
750 .ctx_free = picotls_ctx_free,
751 .ctx_get = picotls_ctx_get,
752 .ctx_get_w_thread = picotls_ctx_get_w_thread,
753 .ctx_handshake_is_over = picotls_handshake_is_over,
754 .ctx_start_listen = picotls_start_listen,
755 .ctx_stop_listen = picotls_stop_listen,
756 .ctx_init_server = picotls_ctx_init_server,
757 .ctx_init_client = picotls_ctx_init_client,
758 .ctx_read = picotls_ctx_read,
759 .ctx_write = picotls_ctx_write,
760 .ctx_transport_close = picotls_transport_close,
761 .ctx_transport_reset = picotls_transport_reset,
762 .ctx_app_close = picotls_app_close,
763 .ctx_reinit_cachain = picotls_reinit_ca_chain,
766 static clib_error_t *
767 tls_picotls_init (vlib_main_t * vm)
769 vlib_thread_main_t *vtm = vlib_get_thread_main ();
770 picotls_main_t *pm = &picotls_main;
771 clib_error_t *error = 0;
774 num_threads = 1 + vtm->n_threads;
776 vec_validate (pm->ctx_pool, num_threads - 1);
777 vec_validate (pm->rx_bufs, num_threads - 1);
778 vec_validate (pm->tx_bufs, num_threads - 1);
780 clib_rwlock_init (&picotls_main.crypto_keys_rw_lock);
782 tls_register_engine (&picotls_engine, CRYPTO_ENGINE_PICOTLS);
784 picotls_init_client_ptls_ctx (&pm->client_ptls_ctx);
789 VLIB_INIT_FUNCTION (tls_picotls_init) = {
790 .runs_after = VLIB_INITS ("tls_init"),
793 VLIB_PLUGIN_REGISTER () = {
794 .version = VPP_BUILD_VER,
795 .description = "Transport Layer Security (TLS) Engine, Picotls Based",
799 * fd.io coding-style-patch-verification: ON
802 * eval: (c-set-style "gnu")