ustream-ssl: add support for using a fd instead of ustream as backing
[project/ustream-ssl.git] / ustream-mbedtls.c
index 1c70cac91ef1f1f59d56a2be10d7f551f9b9a600..361ff9939fa93696dc97e38b620406917506123e 100644 (file)
 
 #include "ustream-ssl.h"
 #include "ustream-internal.h"
+#include <psa/crypto.h>
+#include <mbedtls/debug.h>
+
+static void debug_cb(void *ctx_p, int level,
+                     const char *file, int line,
+                     const char *str)
+{
+       struct ustream_ssl_ctx *ctx = ctx_p;
+       const char *fstr;
+       char buf[512];
+       int len;
+
+       if (!ctx->debug_cb)
+               return;
+
+       while ((fstr = strstr(file + 1, "library/")) != NULL)
+               file = fstr;
+
+       len = snprintf(buf, sizeof(buf), "%s:%04d: %s", file, line, str);
+       if (len >= (int)sizeof(buf))
+               len = (int)sizeof(buf) - 1;
+       if (buf[len - 1] == '\n')
+               buf[len - 1] = 0;
+       ctx->debug_cb(ctx->debug_cb_priv, level, buf);
+}
 
 static int s_ustream_read(void *ctx, unsigned char *buf, size_t len)
 {
@@ -60,18 +85,47 @@ static int s_ustream_write(void *ctx, const unsigned char *buf, size_t len)
        return ret;
 }
 
-__hidden void ustream_set_io(struct ustream_ssl_ctx *ctx, void *ssl, struct ustream *conn)
+static int s_fd_read(void *ctx, unsigned char *buf, size_t len)
+{
+       struct uloop_fd *ufd = ctx;
+       mbedtls_net_context net = {
+               .fd = ufd->fd
+       };
+
+       return mbedtls_net_recv(&net, buf, len);
+}
+
+static int s_fd_write(void *ctx, const unsigned char *buf, size_t len)
+{
+       struct uloop_fd *ufd = ctx;
+       mbedtls_net_context net = {
+               .fd = ufd->fd
+       };
+
+       return mbedtls_net_send(&net, buf, len);
+}
+
+__hidden void ustream_set_io(struct ustream_ssl *us)
 {
-       mbedtls_ssl_set_bio(ssl, conn, s_ustream_write, s_ustream_read, NULL);
+       if (us->conn)
+               mbedtls_ssl_set_bio(us->ssl, us->conn, s_ustream_write, s_ustream_read, NULL);
+       else
+               mbedtls_ssl_set_bio(us->ssl, &us->fd, s_fd_write, s_fd_read, NULL);
 }
 
 static int _random(void *ctx, unsigned char *out, size_t len)
 {
-       ssize_t ret;
+#ifdef linux
+       if (getrandom(out, len, 0) != (ssize_t) len)
+               return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
+#else
+       static FILE *f;
 
-       ret = getrandom(out, len, 0);
-       if (ret < 0 || (size_t)ret != len)
+       if (!f)
+               f = fopen("/dev/urandom", "r");
+       if (fread(out, len, 1, f) != 1)
                return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
+#endif
 
        return 0;
 }
@@ -90,6 +144,14 @@ static int _random(void *ctx, unsigned char *out, size_t len)
 
 static const int default_ciphersuites_server[] =
 {
+#ifdef MBEDTLS_SSL_PROTO_TLS1_3
+       MBEDTLS_TLS1_3_CHACHA20_POLY1305_SHA256,
+       MBEDTLS_TLS1_3_AES_256_GCM_SHA384,
+       MBEDTLS_TLS1_3_AES_128_GCM_SHA256,
+       MBEDTLS_TLS1_3_AES_128_CCM_SHA256,
+       MBEDTLS_TLS1_3_AES_128_CCM_8_SHA256,
+#endif
+
        MBEDTLS_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
        AES_GCM_CIPHERS(ECDHE_ECDSA),
        MBEDTLS_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
@@ -101,6 +163,14 @@ static const int default_ciphersuites_server[] =
 
 static const int default_ciphersuites_client[] =
 {
+#ifdef MBEDTLS_SSL_PROTO_TLS1_3
+       MBEDTLS_TLS1_3_CHACHA20_POLY1305_SHA256,
+       MBEDTLS_TLS1_3_AES_256_GCM_SHA384,
+       MBEDTLS_TLS1_3_AES_128_GCM_SHA256,
+       MBEDTLS_TLS1_3_AES_128_CCM_SHA256,
+       MBEDTLS_TLS1_3_AES_128_CCM_8_SHA256,
+#endif
+
        MBEDTLS_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
        AES_GCM_CIPHERS(ECDHE_ECDSA),
        MBEDTLS_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
@@ -130,6 +200,13 @@ __ustream_ssl_context_new(bool server)
        mbedtls_ssl_config *conf;
        int ep;
 
+#ifdef MBEDTLS_PSA_CRYPTO_C
+       static bool psa_init;
+
+       if (!psa_init && !psa_crypto_init())
+               psa_init = true;
+#endif
+
        ctx = calloc(1, sizeof(*ctx));
        if (!ctx)
                return NULL;
@@ -296,6 +373,10 @@ __hidden int __ustream_ssl_set_require_validation(struct ustream_ssl_ctx *ctx, b
        if (!require)
                mode = MBEDTLS_SSL_VERIFY_NONE;
 
+       /* force TLS 1.2 when not requiring validation for now */
+       if (!require && !ctx->server)
+               mbedtls_ssl_conf_max_version(&ctx->conf, MBEDTLS_SSL_MAJOR_VERSION_3,
+                                            MBEDTLS_SSL_MINOR_VERSION_3);
        mbedtls_ssl_conf_authmode(&ctx->conf, mode);
 
        return 0;
@@ -303,6 +384,7 @@ __hidden int __ustream_ssl_set_require_validation(struct ustream_ssl_ctx *ctx, b
 
 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
 {
+       free(ctx->session_data);
 #if defined(MBEDTLS_SSL_CACHE_C)
        mbedtls_ssl_cache_free(&ctx->cache);
 #endif
@@ -320,14 +402,53 @@ static void ustream_ssl_error(struct ustream_ssl *us, int ret)
        uloop_timeout_set(&us->error_timer, 0);
 }
 
-static bool ssl_do_wait(int ret)
+#ifdef MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET
+static void
+__ustream_ssl_save_session(struct ustream_ssl *us)
+{
+       struct ustream_ssl_ctx *ctx = us->ctx;
+       mbedtls_ssl_session sess;
+
+       if (ctx->server)
+               return;
+
+       free(ctx->session_data);
+       ctx->session_data = NULL;
+
+       mbedtls_ssl_session_init(&sess);
+       if (mbedtls_ssl_get_session(us->ssl, &sess) != 0)
+               return;
+
+       mbedtls_ssl_session_save(&sess, NULL, 0, &ctx->session_data_len);
+       ctx->session_data = malloc(ctx->session_data_len);
+       if (mbedtls_ssl_session_save(&sess, ctx->session_data, ctx->session_data_len,
+                                    &ctx->session_data_len))
+               ctx->session_data_len = 0;
+       mbedtls_ssl_session_free(&sess);
+}
+#endif
+
+static int ssl_check_return(struct ustream_ssl *us, int ret)
 {
        switch(ret) {
        case MBEDTLS_ERR_SSL_WANT_READ:
        case MBEDTLS_ERR_SSL_WANT_WRITE:
-               return true;
+               return U_SSL_PENDING;
+#ifdef MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET
+       case MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET:
+               __ustream_ssl_save_session(us);
+               return U_SSL_RETRY;
+#endif
+#ifdef MBEDTLS_ECP_RESTARTABLE
+       case MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS:
+               return U_SSL_RETRY;
+#endif
+       case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
+       case MBEDTLS_ERR_NET_CONN_RESET:
+               return 0;
        default:
-               return false;
+               ustream_ssl_error(us, ret);
+               return U_SSL_ERROR;
        }
 }
 
@@ -366,17 +487,17 @@ __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
        void *ssl = us->ssl;
        int r;
 
-       r = mbedtls_ssl_handshake(ssl);
-       if (r == 0) {
-               ustream_ssl_verify_cert(us);
-               return U_SSL_OK;
-       }
+       do {
+               r = mbedtls_ssl_handshake(ssl);
+               if (r == 0) {
+                       ustream_ssl_verify_cert(us);
+                       return U_SSL_OK;
+               }
 
-       if (ssl_do_wait(r))
-               return U_SSL_PENDING;
+               r = ssl_check_return(us, r);
+       } while (r == U_SSL_RETRY);
 
-       ustream_ssl_error(us, r);
-       return U_SSL_ERROR;
+       return r;
 }
 
 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
@@ -386,12 +507,14 @@ __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int le
 
        while (done != len) {
                ret = mbedtls_ssl_write(ssl, (const unsigned char *) buf + done, len - done);
-
                if (ret < 0) {
-                       if (ssl_do_wait(ret))
+                       ret = ssl_check_return(us, ret);
+                       if (ret == U_SSL_RETRY)
+                               continue;
+
+                       if (ret == U_SSL_PENDING)
                                return done;
 
-                       ustream_ssl_error(us, ret);
                        return -1;
                }
 
@@ -403,25 +526,34 @@ __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int le
 
 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
 {
-       int ret = mbedtls_ssl_read(us->ssl, (unsigned char *) buf, len);
-
-       if (ret < 0) {
-               if (ssl_do_wait(ret))
-                       return U_SSL_PENDING;
+       int ret;
 
-               if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
-                       return 0;
+       do {
+               ret = mbedtls_ssl_read(us->ssl, (unsigned char *) buf, len);
+               if (ret >= 0)
+                       return ret;
 
-               ustream_ssl_error(us, ret);
-               return U_SSL_ERROR;
-       }
+               ret = ssl_check_return(us, ret);
+       } while (ret == U_SSL_RETRY);
 
        return ret;
 }
 
+__hidden void __ustream_ssl_set_debug(struct ustream_ssl_ctx *ctx, int level,
+                                     ustream_ssl_debug_cb cb, void *cb_priv)
+{
+       ctx->debug_cb = cb;
+       ctx->debug_cb_priv = cb_priv;
+       mbedtls_ssl_conf_dbg(&ctx->conf, debug_cb, ctx);
+#ifdef MBEDTLS_DEBUG_C
+       mbedtls_debug_set_threshold(level);
+#endif
+}
+
 __hidden void *__ustream_ssl_session_new(struct ustream_ssl_ctx *ctx)
 {
        mbedtls_ssl_context *ssl;
+       mbedtls_ssl_session sess;
 
        ssl = calloc(1, sizeof(*ssl));
        if (!ssl)
@@ -434,11 +566,18 @@ __hidden void *__ustream_ssl_session_new(struct ustream_ssl_ctx *ctx)
                return NULL;
        }
 
+       if (!ctx->session_data_len)
+               return ssl;
+
+       mbedtls_ssl_session_init(&sess);
+       if (mbedtls_ssl_session_load(&sess, ctx->session_data, ctx->session_data_len) == 0)
+               mbedtls_ssl_set_session(ssl, &sess);
+
        return ssl;
 }
 
-__hidden void __ustream_ssl_session_free(void *ssl)
+__hidden void __ustream_ssl_session_free(struct ustream_ssl *us)
 {
-       mbedtls_ssl_free(ssl);
-       free(ssl);
+       mbedtls_ssl_free(us->ssl);
+       free(us->ssl);
 }