# Configure options.
AC_ARG_WITH([ssl], [AC_HELP_STRING([--with-ssl=@<:@LIB@:>@], [SSL library (openssl|polarssl) @<:@default=polarssl@:>@])], [], [with_ssl=polarssl])
AC_ARG_ENABLE(polarssl-test-cert, [ --enable-polarssl-test-cert Link to PolarSSL test certificate and key @<:@default=no@:>@], [enable_polarssl_test_cert=yes])
+AC_ARG_ENABLE(polarssl-havege, [ --enable-polarssl-havege Link to PolarSSL HAVEGE random generator key @<:@default=no@:>@ Deafult: /dev/urandom], [enable_polarssl_havege=yes])
# Checks for programs.
AC_PROG_CC
AC_CHECK_LIB([polarssl], [test_srv_crt], [], [AC_MSG_ERROR([could not find test_srv_crt])])
AC_DEFINE([USE_POLARSSL_TESTCERT], [], [Use PolarSSL test certificate])
])
+ AS_IF([test "x$enable_polarssl_havege" = xyes], [
+ AC_CHECK_LIB([polarssl], [havege_init], [], [AC_MSG_ERROR([could not find havege_init])])
+ AC_DEFINE([USE_POLARSSL_HAVEGE], [], [Use PolarSSL HAVEGE random generator])
+ ])
])
AS_IF([test "x$with_ssl" = xopenssl], [
AC_CHECK_HEADERS([openssl/ssl.h], [], [AC_MSG_ERROR([could not find openssl/ssl.h])])
#include "crypt.h"
#include "ssl.h"
-#ifdef USE_POLARSSL
-#include <polarssl/havege.h>
+#ifdef USE_POLARSSL_HAVEGE
extern havege_state hs;
#endif
*/
#include <string.h>
#include <stdlib.h>
+#include <fcntl.h>
#include "conf.h"
#include "log.h"
static rsa_context key;
bool_t builtInTestCertificate;
-havege_state hs; /* exported to crypt.c */
+#ifdef USE_POLARSSL_HAVEGE
+havege_state hs;
+#else
+int urandom_fd;
+#endif
/* DH prime */
char *my_dhm_P =
Log_fatal("Could not read RSA key file %s", keyfile);
}
+#ifndef USE_POLARSSL_HAVEGE
+int urandom_bytes(void *ctx, unsigned char *dest, size_t len)
+{
+ int cur;
+
+ while (len) {
+ cur = read(urandom_fd, dest, len);
+ if (cur < 0)
+ continue;
+ len -= cur;
+ }
+}
+#endif
+
#define DEBUG_LEVEL 0
static void pssl_debug(void *ctx, int level, const char *str)
{
#else
initKey();
#endif
+
+ /* Initialize random number generator */
+#ifdef USE_POLARSSL_HAVEGE
havege_init(&hs);
+#else
+ urandom_fd = open("/dev/urandom", O_RDONLY);
+ if (urandom_fd < 0)
+ Log_fatal("Cannot open /dev/urandom");
+ Log_info("Using random number generator /dev/urandom");
+#endif
#ifdef POLARSSL_VERSION_MAJOR
version_get_string(verstring);
ssl_set_endpoint(ssl, SSL_IS_SERVER);
ssl_set_authmode(ssl, SSL_VERIFY_OPTIONAL);
-
+
+#ifdef USE_POLARSSL_HAVEGE
ssl_set_rng(ssl, HAVEGE_RAND, &hs);
+#else
+ ssl_set_rng(ssl, urandom_bytes, NULL);
+#endif
+
ssl_set_dbg(ssl, pssl_debug, NULL);
ssl_set_bio(ssl, net_recv, fd, net_send, fd);
#include <polarssl/ssl.h>
#include <polarssl/version.h>
-#ifndef POLARSSL_VERSION_MAJOR
- #define POLARSSL_API_V0
-#else
-#if (POLARSSL_VERSION_MAJOR == 0)
- #define POLARSSL_API_V0
- #define HAVEGE_RAND (havege_rand)
- #define RAND_bytes(_dst_, _size_) do { \
- int i; \
- for (i = 0; i < _size_; i++) { \
- _dst_[i] = havege_rand(&hs); \
- } \
- } while (0)
-#else
- #define POLARSSL_API_V1
+#define POLARSSL_API_V1
+#ifdef USE_POLARSSL_HAVEGE
#if (POLARSSL_VERSION_MINOR >= 1)
#define HAVEGE_RAND (havege_random)
#define RAND_bytes(_dst_, _size_) do { \
havege_random(&hs, _dst_, _size_); \
- } while (0)
+ } while (0)
#else
#define HAVEGE_RAND (havege_rand)
#define RAND_bytes(_dst_, _size_) do { \
- int i; \
- for (i = 0; i < _size_; i++) { \
- _dst_[i] = havege_rand(&hs); \
- } \
+ int i; \
+ for (i = 0; i < _size_; i++) { \
+ _dst_[i] = havege_rand(&hs); \
+ } \
} while (0)
#endif
- #if (POLARSSL_VERSION_MINOR >= 2)
- #define POLARSSL_API_V1_2
- #endif
+#else
+#define RAND_bytes(_dst_, _size_) do { urandom_bytes(NULL, _dst_, _size_); } while (0)
+int urandom_bytes(void *ctx, unsigned char *dest, size_t len);
#endif
+
+#if (POLARSSL_VERSION_MINOR >= 2)
+ #define POLARSSL_API_V1_2
#endif
#else /* OpenSSL */