mbedtls: fix build on non-linux systems
authorFelix Fietkau <nbd@nbd.name>
Tue, 26 Mar 2024 18:40:51 +0000 (19:40 +0100)
committerFelix Fietkau <nbd@nbd.name>
Tue, 26 Mar 2024 18:40:58 +0000 (19:40 +0100)
Deal with missing getrandom. Comment out linker flag.

Signed-off-by: Felix Fietkau <nbd@nbd.name>
CMakeLists.txt
ustream-mbedtls.c

index f4dca0dba358db483fcef82e8fabde4dff587fdd..2e591ee76b4a2c9d8642ae9620266c43fa245eda 100644 (file)
@@ -10,7 +10,9 @@ ENDIF()
 ADD_DEFINITIONS(-Wno-unused-parameter -Wmissing-declarations)
 
 SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
-SET(CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-undefined")
+IF (NOT APPLE)
+  SET(CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-undefined")
+ENDIF()
 
 IF(MBEDTLS)
   ADD_DEFINITIONS(-DHAVE_MBEDTLS)
index 1c70cac91ef1f1f59d56a2be10d7f551f9b9a600..18ffe065a5eb891603ef257b1e9493fb451ccb63 100644 (file)
@@ -67,11 +67,17 @@ __hidden void ustream_set_io(struct ustream_ssl_ctx *ctx, void *ssl, struct ustr
 
 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;
 }