16 #include <openssl/ssl.h> 17 #include <openssl/conf.h> 18 #include <openssl/err.h> 20 #include <vpp/app/version.h> 55 (*ctx)->ctx.c_thread_index = thread_index;
58 (*ctx)->openssl_ctx_index = ctx - tm->
ctx_pool[thread_index];
59 return ((*ctx)->openssl_ctx_index);
68 SSL_shutdown (oc->
ssl);
70 if (SSL_is_server (oc->
ssl))
73 EVP_PKEY_free (oc->
pkey);
102 u32 deq_max, deq_now;
106 f = tls_session->server_rx_fifo;
134 u32 enq_max, deq_now;
138 if (BIO_ctrl_pending (oc->
rbio) <= 0)
141 f = tls_session->server_tx_fifo;
173 while (SSL_in_init (oc->
ssl))
178 rv = SSL_do_handshake (oc->
ssl);
179 err = SSL_get_error (oc->
ssl, rv);
181 if (err != SSL_ERROR_WANT_WRITE)
183 if (err == SSL_ERROR_SSL)
186 ERR_error_string (ERR_get_error (), buf);
193 SSL_state_string_long (oc->
ssl));
195 if (SSL_in_init (oc->
ssl))
201 if (!SSL_is_server (oc->
ssl))
206 if ((rv = SSL_get_verify_result (oc->
ssl)) != X509_V_OK)
208 TLS_DBG (1,
" failed verify: %s\n",
209 X509_verify_cert_error_string (rv));
227 TLS_DBG (1,
"Handshake for %u complete. TLS cipher is %s",
236 int wrote = 0, rv, read, max_buf = 100 *
TLS_CHUNK_SIZE, max_space;
237 u32 enq_max, deq_max, deq_now, to_write;
241 f = app_session->server_tx_fifo;
246 max_space = max_buf - BIO_ctrl_pending (oc->
rbio);
247 max_space = (max_space < 0) ? 0 : max_space;
268 if (deq_now < deq_max)
273 if (BIO_ctrl_pending (oc->
rbio) <= 0)
277 f = tls_session->server_tx_fifo;
296 if (read < enq_max && BIO_ctrl_pending (oc->
rbio) > 0)
304 if (BIO_ctrl_pending (oc->
rbio) > 0)
313 int read, wrote = 0, max_space, max_buf = 100 *
TLS_CHUNK_SIZE, rv;
315 u32 deq_max, enq_max, deq_now, to_read;
325 f = tls_session->server_rx_fifo;
327 max_space = max_buf - BIO_ctrl_pending (oc->
wbio);
328 max_space = max_space < 0 ? 0 : max_space;
329 deq_now =
clib_min (deq_max, max_space);
356 if (BIO_ctrl_pending (oc->
wbio) <= 0)
360 f = app_session->server_rx_fifo;
376 if (read < enq_max && BIO_ctrl_pending (oc->
wbio) > 0)
385 if (BIO_ctrl_pending (oc->
wbio) > 0)
394 char *ciphers =
"ALL:!ADH:!LOW:!EXP:!MD5:!RC4-SHA:!DES-CBC3-SHA:@STRENGTH";
395 long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
399 const SSL_METHOD *method;
402 method = SSLv23_client_method ();
405 TLS_DBG (1,
"SSLv23_method returned null");
409 oc->
ssl_ctx = SSL_CTX_new (method);
412 TLS_DBG (1,
"SSL_CTX_new returned null");
416 SSL_CTX_set_ecdh_auto (oc->
ssl_ctx, 1);
417 SSL_CTX_set_mode (oc->
ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
418 rv = SSL_CTX_set_cipher_list (oc->
ssl_ctx, (
const char *) ciphers);
421 TLS_DBG (1,
"Couldn't set cipher");
425 SSL_CTX_set_options (oc->
ssl_ctx, flags);
431 TLS_DBG (1,
"Couldn't initialize ssl struct");
435 oc->
rbio = BIO_new (BIO_s_mem ());
436 oc->
wbio = BIO_new (BIO_s_mem ());
438 BIO_set_mem_eof_return (oc->
rbio, -1);
439 BIO_set_mem_eof_return (oc->
wbio, -1);
442 SSL_set_connect_state (oc->
ssl);
447 TLS_DBG (1,
"Couldn't set hostname");
454 TLS_DBG (1,
"Initiating handshake for [%u]%u", ctx->c_thread_index,
460 rv = SSL_do_handshake (oc->
ssl);
461 err = SSL_get_error (oc->
ssl, rv);
463 if (err != SSL_ERROR_WANT_WRITE)
467 TLS_DBG (2,
"tls state for [%u]%u is su", ctx->c_thread_index,
475 char *ciphers =
"ALL:!ADH:!LOW:!EXP:!MD5:!RC4-SHA:!DES-CBC3-SHA:@STRENGTH";
476 long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
479 const SSL_METHOD *method;
485 if (!app->tls_cert || !app->tls_key)
487 TLS_DBG (1,
"tls cert and/or key not configured %d",
488 ctx->parent_app_index);
492 method = SSLv23_method ();
493 oc->
ssl_ctx = SSL_CTX_new (method);
500 SSL_CTX_set_mode (oc->
ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
501 SSL_CTX_set_options (oc->
ssl_ctx, flags);
502 SSL_CTX_set_ecdh_auto (oc->
ssl_ctx, 1);
504 rv = SSL_CTX_set_cipher_list (oc->
ssl_ctx, (
const char *) ciphers);
507 TLS_DBG (1,
"Couldn't set cipher");
514 cert_bio = BIO_new (BIO_s_mem ());
515 BIO_write (cert_bio, app->tls_cert,
vec_len (app->tls_cert));
523 cert_bio = BIO_new (BIO_s_mem ());
524 BIO_write (cert_bio, app->tls_key,
vec_len (app->tls_key));
537 TLS_DBG (1,
"Couldn't initialize ssl struct");
541 oc->
rbio = BIO_new (BIO_s_mem ());
542 oc->
wbio = BIO_new (BIO_s_mem ());
544 BIO_set_mem_eof_return (oc->
rbio, -1);
545 BIO_set_mem_eof_return (oc->
wbio, -1);
548 SSL_set_accept_state (oc->
ssl);
550 TLS_DBG (1,
"Initiating handshake for [%u]%u", ctx->c_thread_index,
556 rv = SSL_do_handshake (oc->
ssl);
557 err = SSL_get_error (oc->
ssl, rv);
559 if (err != SSL_ERROR_WANT_WRITE)
563 TLS_DBG (2,
"tls state for [%u]%u is su", ctx->c_thread_index,
574 return SSL_is_init_finished (mc->
ssl);
600 clib_warning (
"Could not initialize TLS CA certificates");
618 cert_bio = BIO_new (BIO_s_mem ());
620 testcert = PEM_read_bio_X509 (cert_bio,
NULL,
NULL,
NULL);
626 X509_STORE_add_cert (om->
cert_store, testcert);
629 return (rv < 0 ? -1 : 0);
646 SSL_load_error_strings ();
664 .version = VPP_BUILD_VER,
665 .description =
"openssl based TLS Engine",
tls_main_t * vnet_tls_get_main(void)
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
static u8 openssl_handshake_is_over(tls_ctx_t *ctx)
static int openssl_try_handshake_write(openssl_ctx_t *oc, stream_session_t *tls_session)
const u32 test_srv_crt_rsa_len
static u32 openssl_ctx_alloc(void)
static void openssl_ctx_free(tls_ctx_t *ctx)
void tls_notify_app_enqueue(tls_ctx_t *ctx, stream_session_t *app_session)
static u32 svm_fifo_max_enqueue(svm_fifo_t *f)
static tls_ctx_t * openssl_ctx_get(u32 ctx_index)
static stream_session_t * session_get_from_handle(session_handle_t handle)
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
static u8 * svm_fifo_tail(svm_fifo_t *f)
struct _svm_fifo svm_fifo_t
static void svm_fifo_enqueue_nocopy(svm_fifo_t *f, u32 bytes)
Advance tail pointer.
#define VLIB_INIT_FUNCTION(x)
static u32 svm_fifo_max_dequeue(svm_fifo_t *f)
struct tls_ctx_openssl_ openssl_ctx_t
static u32 svm_fifo_max_write_chunk(svm_fifo_t *f)
Max contiguous chunk of data that can be written.
struct _stream_session_t stream_session_t
#define vlib_call_init_function(vm, x)
static u8 * svm_fifo_head(svm_fifo_t *f)
static u32 svm_fifo_max_read_chunk(svm_fifo_t *f)
Max contiguous chunk of data that can be read.
int tls_init_ca_chain(void)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
int tls_notify_app_connected(tls_ctx_t *ctx, u8 is_failed)
const char test_srv_crt_rsa[]
static int openssl_ctx_write(tls_ctx_t *ctx, stream_session_t *app_session)
static int openssl_ctx_init_client(tls_ctx_t *ctx)
static_always_inline uword vlib_get_thread_index(void)
openssl_ctx_t *** ctx_pool
#define clib_warning(format, args...)
#define SESSION_INVALID_HANDLE
static openssl_main_t openssl_main
struct openssl_main_ openssl_main_t
struct _application application_t
#define TLS_DBG(_fmt, _args...)
void tls_register_engine(const tls_engine_vft_t *vft, tls_engine_type_t type)
#define pool_put_index(p, i)
Free pool element with given index.
static clib_error_t * tls_openssl_init(vlib_main_t *vm)
static int openssl_try_handshake_read(openssl_ctx_t *oc, stream_session_t *tls_session)
static void * clib_mem_alloc(uword size)
static int openssl_ctx_init_server(tls_ctx_t *ctx)
int svm_fifo_dequeue_drop(svm_fifo_t *f, u32 max_bytes)
static tls_ctx_t * openssl_ctx_get_w_thread(u32 ctx_index, u8 thread_index)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
application_t * application_get(u32 index)
int tls_add_vpp_q_evt(svm_fifo_t *f, u8 evt_type)
static int openssl_ctx_handshake_rx(tls_ctx_t *ctx, stream_session_t *tls_session)
int tls_notify_app_accept(tls_ctx_t *ctx)
static vlib_thread_main_t * vlib_get_thread_main()
static int openssl_ctx_read(tls_ctx_t *ctx, stream_session_t *tls_session)
static clib_error_t * tls_init(vlib_main_t *vm)