mbedtls: disable TLS 1.3 in client mode when skipping verification
[project/ustream-ssl.git] / ustream-mbedtls.c
index e176afe71f02399717a3e219bbb8e28053f7a784..73c4a5eee1be378b0062ef5408a3a5e0fa4ee424 100644 (file)
@@ -17,6 +17,7 @@
  */
 
 #include <sys/types.h>
+#include <sys/random.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <stdlib.h>
 
 #include "ustream-ssl.h"
 #include "ustream-internal.h"
+#include <psa/crypto.h>
+#include <mbedtls/debug.h>
 
-static int urandom_fd = -1;
+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)
 {
@@ -37,7 +61,7 @@ static int s_ustream_read(void *ctx, unsigned char *buf, size_t len)
                return 0;
 
        sbuf = ustream_get_read_buf(s, &slen);
-       if (slen > len)
+       if ((size_t) slen > len)
                slen = len;
 
        if (!slen)
@@ -66,55 +90,82 @@ __hidden void ustream_set_io(struct ustream_ssl_ctx *ctx, void *ssl, struct ustr
        mbedtls_ssl_set_bio(ssl, conn, s_ustream_write, s_ustream_read, NULL);
 }
 
-static bool urandom_init(void)
+static int _random(void *ctx, unsigned char *out, size_t len)
 {
-       if (urandom_fd > -1)
-               return true;
-
-       urandom_fd = open("/dev/urandom", O_RDONLY);
-       if (urandom_fd < 0)
-               return false;
-
-       return true;
-}
+#ifdef linux
+       if (getrandom(out, len, 0) != (ssize_t) len)
+               return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
+#else
+       static FILE *f;
 
-static int _urandom(void *ctx, unsigned char *out, size_t len)
-{
-       if (read(urandom_fd, out, len) < 0)
+       if (!f)
+               f = fopen("/dev/urandom", "r");
+       if (fread(out, len, 1, f) != 1)
                return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
+#endif
 
        return 0;
 }
 
-#define TLS_DEFAULT_CIPHERS                    \
-    TLS_CIPHER(AES_256_CBC_SHA256)             \
-    TLS_CIPHER(AES_256_GCM_SHA384)             \
-    TLS_CIPHER(AES_256_CBC_SHA)                        \
-    TLS_CIPHER(CAMELLIA_256_CBC_SHA256)                \
-    TLS_CIPHER(CAMELLIA_256_CBC_SHA)           \
-    TLS_CIPHER(AES_128_CBC_SHA256)             \
-    TLS_CIPHER(AES_128_GCM_SHA256)             \
-    TLS_CIPHER(AES_128_CBC_SHA)                        \
-    TLS_CIPHER(CAMELLIA_128_CBC_SHA256)                \
-    TLS_CIPHER(CAMELLIA_128_CBC_SHA)           \
-    TLS_CIPHER(3DES_EDE_CBC_SHA)
-
-static const int default_ciphersuites_nodhe[] =
+#define AES_GCM_CIPHERS(v)                             \
+       MBEDTLS_TLS_##v##_WITH_AES_128_GCM_SHA256,      \
+       MBEDTLS_TLS_##v##_WITH_AES_256_GCM_SHA384
+
+#define AES_CBC_CIPHERS(v)                             \
+       MBEDTLS_TLS_##v##_WITH_AES_128_CBC_SHA,         \
+       MBEDTLS_TLS_##v##_WITH_AES_256_CBC_SHA
+
+#define AES_CIPHERS(v)                                 \
+       AES_GCM_CIPHERS(v),                             \
+       AES_CBC_CIPHERS(v)
+
+static const int default_ciphersuites_server[] =
 {
-#define TLS_CIPHER(v)                          \
-       MBEDTLS_TLS_RSA_WITH_##v,
-       TLS_DEFAULT_CIPHERS
-#undef TLS_CIPHER
+#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,
+       AES_GCM_CIPHERS(ECDHE_RSA),
+       AES_CBC_CIPHERS(ECDHE_RSA),
+       AES_CIPHERS(RSA),
        0
 };
 
-static const int default_ciphersuites[] =
+static const int default_ciphersuites_client[] =
 {
-#define TLS_CIPHER(v)                          \
-       MBEDTLS_TLS_DHE_RSA_WITH_##v,           \
-       MBEDTLS_TLS_RSA_WITH_##v,
-       TLS_DEFAULT_CIPHERS
-#undef TLS_CIPHER
+#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,
+       AES_GCM_CIPHERS(ECDHE_RSA),
+       MBEDTLS_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
+       AES_GCM_CIPHERS(DHE_RSA),
+       AES_CBC_CIPHERS(ECDHE_ECDSA),
+       AES_CBC_CIPHERS(ECDHE_RSA),
+       AES_CBC_CIPHERS(DHE_RSA),
+/* Removed in Mbed TLS 3.0.0 */
+#ifdef MBEDTLS_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA
+       MBEDTLS_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA,
+#endif
+       AES_CIPHERS(RSA),
+/* Removed in Mbed TLS 3.0.0 */
+#ifdef MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA
+       MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA,
+#endif
        0
 };
 
@@ -126,8 +177,12 @@ __ustream_ssl_context_new(bool server)
        mbedtls_ssl_config *conf;
        int ep;
 
-       if (!urandom_init())
-               return NULL;
+#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)
@@ -138,22 +193,36 @@ __ustream_ssl_context_new(bool server)
        mbedtls_x509_crt_init(&ctx->cert);
        mbedtls_x509_crt_init(&ctx->ca_cert);
 
+#if defined(MBEDTLS_SSL_CACHE_C)
+       mbedtls_ssl_cache_init(&ctx->cache);
+       mbedtls_ssl_cache_set_timeout(&ctx->cache, 30 * 60);
+       mbedtls_ssl_cache_set_max_entries(&ctx->cache, 5);
+#endif
+
        conf = &ctx->conf;
        mbedtls_ssl_config_init(conf);
 
-       if (server) {
-               mbedtls_ssl_conf_ciphersuites(conf, default_ciphersuites_nodhe);
-               ep = MBEDTLS_SSL_IS_SERVER;
-       } else {
-               mbedtls_ssl_conf_ciphersuites(conf, default_ciphersuites);
-               ep = MBEDTLS_SSL_IS_CLIENT;
-       }
+       ep = server ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT;
 
        mbedtls_ssl_config_defaults(conf, ep, MBEDTLS_SSL_TRANSPORT_STREAM,
                                    MBEDTLS_SSL_PRESET_DEFAULT);
-       mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_NONE);
-       mbedtls_ssl_conf_rng(conf, _urandom, NULL);
+       mbedtls_ssl_conf_rng(conf, _random, NULL);
+
+       if (server) {
+               mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_NONE);
+               mbedtls_ssl_conf_ciphersuites(conf, default_ciphersuites_server);
+               mbedtls_ssl_conf_min_version(conf, MBEDTLS_SSL_MAJOR_VERSION_3,
+                                            MBEDTLS_SSL_MINOR_VERSION_3);
+       } else {
+               mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
+               mbedtls_ssl_conf_ciphersuites(conf, default_ciphersuites_client);
+       }
 
+#if defined(MBEDTLS_SSL_CACHE_C)
+       mbedtls_ssl_conf_session_cache(conf, &ctx->cache,
+                                      mbedtls_ssl_cache_get,
+                                      mbedtls_ssl_cache_set);
+#endif
        return ctx;
 }
 
@@ -162,16 +231,9 @@ static void ustream_ssl_update_own_cert(struct ustream_ssl_ctx *ctx)
        if (!ctx->cert.version)
                return;
 
-       if (!ctx->server) {
-               mbedtls_ssl_conf_ca_chain(&ctx->conf, &ctx->cert, NULL);
-               return;
-       }
-
-       if (!ctx->key.pk_info)
+       if (mbedtls_pk_get_type(&ctx->key) == MBEDTLS_PK_NONE)
                return;
 
-       if (ctx->cert.next)
-               mbedtls_ssl_conf_ca_chain(&ctx->conf, ctx->cert.next, NULL);
        mbedtls_ssl_conf_own_cert(&ctx->conf, &ctx->cert, &ctx->key);
 }
 
@@ -204,7 +266,11 @@ __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char
 {
        int ret;
 
+#if (MBEDTLS_VERSION_NUMBER >= 0x03000000)
+       ret = mbedtls_pk_parse_keyfile(&ctx->key, file, NULL, _random, NULL);
+#else
        ret = mbedtls_pk_parse_keyfile(&ctx->key, file, NULL);
+#endif
        if (ret)
                return -1;
 
@@ -212,12 +278,97 @@ __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char
        return 0;
 }
 
+__hidden int __ustream_ssl_set_ciphers(struct ustream_ssl_ctx *ctx, const char *ciphers)
+{
+       int *ciphersuites = NULL, *tmp, id;
+       char *cipherstr, *p, *last, c;
+       size_t len = 0;
+
+       if (ciphers == NULL)
+               return -1;
+
+       cipherstr = strdup(ciphers);
+
+       if (cipherstr == NULL)
+               return -1;
+
+       for (p = cipherstr, last = p;; p++) {
+               if (*p == ':' || *p == 0) {
+                       c = *p;
+                       *p = 0;
+
+                       id = mbedtls_ssl_get_ciphersuite_id(last);
+
+                       if (id != 0) {
+                               tmp = realloc(ciphersuites, (len + 2) * sizeof(int));
+
+                               if (tmp == NULL) {
+                                       free(ciphersuites);
+                                       free(cipherstr);
+
+                                       return -1;
+                               }
+
+                               ciphersuites = tmp;
+                               ciphersuites[len++] = id;
+                               ciphersuites[len] = 0;
+                       }
+
+                       if (c == 0)
+                               break;
+
+                       last = p + 1;
+               }
+
+               /*
+                * mbedTLS expects cipher names with dashes while many sources elsewhere
+                * like the Firefox wiki or Wireshark specify ciphers with underscores,
+                * so simply convert all underscores to dashes to accept both notations.
+                */
+               else if (*p == '_') {
+                       *p = '-';
+               }
+       }
+
+       free(cipherstr);
+
+       if (len == 0)
+               return -1;
+
+       mbedtls_ssl_conf_ciphersuites(&ctx->conf, ciphersuites);
+       free(ctx->ciphersuites);
+
+       ctx->ciphersuites = ciphersuites;
+
+       return 0;
+}
+
+__hidden int __ustream_ssl_set_require_validation(struct ustream_ssl_ctx *ctx, bool require)
+{
+       int mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
+
+       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;
+}
+
 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
 {
+#if defined(MBEDTLS_SSL_CACHE_C)
+       mbedtls_ssl_cache_free(&ctx->cache);
+#endif
        mbedtls_pk_free(&ctx->key);
        mbedtls_x509_crt_free(&ctx->ca_cert);
        mbedtls_x509_crt_free(&ctx->cert);
        mbedtls_ssl_config_free(&ctx->conf);
+       free(ctx->ciphersuites);
        free(ctx);
 }
 
@@ -326,6 +477,15 @@ __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
        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);
+       mbedtls_debug_set_threshold(level);
+}
+
 __hidden void *__ustream_ssl_session_new(struct ustream_ssl_ctx *ctx)
 {
        mbedtls_ssl_context *ssl;