ustream-mbedtls: Add compatibility with Mbed TLS 3.0.0
[project/ustream-ssl.git] / ustream-mbedtls.c
index 85bbb1c7c9ea18c04ba9bfcbba1e919a72e13c68..1c70cac91ef1f1f59d56a2be10d7f551f9b9a600 100644 (file)
@@ -17,6 +17,7 @@
  */
 
 #include <sys/types.h>
+#include <sys/random.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <stdlib.h>
@@ -25,8 +26,6 @@
 #include "ustream-ssl.h"
 #include "ustream-internal.h"
 
-static int urandom_fd = -1;
-
 static int s_ustream_read(void *ctx, unsigned char *buf, size_t len)
 {
        struct ustream *s = ctx;
@@ -37,7 +36,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,21 +65,12 @@ __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;
+       ssize_t ret;
 
-       return true;
-}
-
-static int _urandom(void *ctx, unsigned char *out, size_t len)
-{
-       if (read(urandom_fd, out, len) < 0)
+       ret = getrandom(out, len, 0);
+       if (ret < 0 || (size_t)ret != len)
                return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
 
        return 0;
@@ -120,9 +110,15 @@ static const int default_ciphersuites_client[] =
        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
 };
 
@@ -134,9 +130,6 @@ __ustream_ssl_context_new(bool server)
        mbedtls_ssl_config *conf;
        int ep;
 
-       if (!urandom_init())
-               return NULL;
-
        ctx = calloc(1, sizeof(*ctx));
        if (!ctx)
                return NULL;
@@ -159,15 +152,17 @@ __ustream_ssl_context_new(bool server)
 
        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
+       } 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,
@@ -182,16 +177,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);
+       if (mbedtls_pk_get_type(&ctx->key) == MBEDTLS_PK_NONE)
                return;
-       }
 
-       if (!ctx->key.pk_info)
-               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);
 }
 
@@ -224,7 +212,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;
 
@@ -232,6 +224,83 @@ __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;
+
+       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)
@@ -241,6 +310,7 @@ __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
        mbedtls_x509_crt_free(&ctx->ca_cert);
        mbedtls_x509_crt_free(&ctx->cert);
        mbedtls_ssl_config_free(&ctx->conf);
+       free(ctx->ciphersuites);
        free(ctx);
 }