ustream-mbedtls: Add compatibility with Mbed TLS 3.0.0
[project/ustream-ssl.git] / ustream-mbedtls.c
index 9f73c58360348fc62534ad69d696a71ed7c636dd..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;
-
-       return true;
-}
+       ssize_t ret;
 
-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,7 +177,7 @@ static void ustream_ssl_update_own_cert(struct ustream_ssl_ctx *ctx)
        if (!ctx->cert.version)
                return;
 
-       if (!ctx->key.pk_info)
+       if (mbedtls_pk_get_type(&ctx->key) == MBEDTLS_PK_NONE)
                return;
 
        mbedtls_ssl_conf_own_cert(&ctx->conf, &ctx->cert, &ctx->key);
@@ -217,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;
 
@@ -290,6 +289,18 @@ __hidden int __ustream_ssl_set_ciphers(struct ustream_ssl_ctx *ctx, const char *
        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)