diff options
| author | William Ahern <william+ubuntu@25thandClement.com> | 2016-10-19 13:15:48 -0700 |
|---|---|---|
| committer | William Ahern <william+ubuntu@25thandClement.com> | 2016-10-19 13:15:48 -0700 |
| commit | 5949f01087291c0a6431f90264fc9795b133867f (patch) | |
| tree | ebffcc6761d18d090da01f755eae781a4041c6e8 /src | |
| parent | 0e2d4c797e6eb22d87621928af252b6264d28ae0 (diff) | |
| download | luaossl-5949f01087291c0a6431f90264fc9795b133867f.tar.gz luaossl-5949f01087291c0a6431f90264fc9795b133867f.tar.bz2 luaossl-5949f01087291c0a6431f90264fc9795b133867f.zip | |
there are no kernel headers installed by default in Alpine Linux (Linux/musl) so use alternate feature test; support running binaries built with getrandom on older kernels with only sysctl(RANDOM_UUID); change preprocessor test for feature macros so they can be easily overridden with build flags; and remove feature macro definitions in favor of those in config.h (config.h.guess)
Diffstat (limited to 'src')
| -rw-r--r-- | src/openssl.c | 56 |
1 files changed, 25 insertions, 31 deletions
diff --git a/src/openssl.c b/src/openssl.c index c25651b..78dbd65 100644 --- a/src/openssl.c +++ b/src/openssl.c | |||
| @@ -23,6 +23,10 @@ | |||
| 23 | * USE OR OTHER DEALINGS IN THE SOFTWARE. | 23 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | * ========================================================================== | 24 | * ========================================================================== |
| 25 | */ | 25 | */ |
| 26 | #if HAVE_CONFIG_H | ||
| 27 | #include "config.h" | ||
| 28 | #endif | ||
| 29 | |||
| 26 | #include <limits.h> /* INT_MAX INT_MIN LLONG_MAX LLONG_MIN UCHAR_MAX ULLONG_MAX */ | 30 | #include <limits.h> /* INT_MAX INT_MIN LLONG_MAX LLONG_MIN UCHAR_MAX ULLONG_MAX */ |
| 27 | #include <stdint.h> /* uintptr_t */ | 31 | #include <stdint.h> /* uintptr_t */ |
| 28 | #include <string.h> /* memset(3) strerror_r(3) */ | 32 | #include <string.h> /* memset(3) strerror_r(3) */ |
| @@ -48,22 +52,6 @@ | |||
| 48 | 52 | ||
| 49 | #if __APPLE__ | 53 | #if __APPLE__ |
| 50 | #include <mach/mach_time.h> /* mach_absolute_time() */ | 54 | #include <mach/mach_time.h> /* mach_absolute_time() */ |
| 51 | #define HAVE_ARC4RANDOM | ||
| 52 | #endif | ||
| 53 | |||
| 54 | #if defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(BSD) | ||
| 55 | #define HAVE_ARC4RANDOM | ||
| 56 | #endif | ||
| 57 | |||
| 58 | #if defined(__linux__) | ||
| 59 | #include <linux/version.h> | ||
| 60 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) | ||
| 61 | #define HAVE_GETRANDOM | ||
| 62 | #include <sys/syscall.h> | ||
| 63 | #include <linux/random.h> | ||
| 64 | #else | ||
| 65 | #define HAVE_SYS_SYSCTL_H | ||
| 66 | #endif | ||
| 67 | #endif | 55 | #endif |
| 68 | 56 | ||
| 69 | #include <openssl/opensslconf.h> | 57 | #include <openssl/opensslconf.h> |
| @@ -95,10 +83,6 @@ | |||
| 95 | #define LIBRESSL_PREREQ(M, m, p) \ | 83 | #define LIBRESSL_PREREQ(M, m, p) \ |
| 96 | (LIBRESSL_VERSION_NUMBER >= (((M) << 28) | ((m) << 20) | ((p) << 12))) | 84 | (LIBRESSL_VERSION_NUMBER >= (((M) << 28) | ((m) << 20) | ((p) << 12))) |
| 97 | 85 | ||
| 98 | #ifndef HAVE_DLADDR | ||
| 99 | #define HAVE_DLADDR (!defined _AIX) /* TODO: https://root.cern.ch/drupal/content/aix-and-dladdr */ | ||
| 100 | #endif | ||
| 101 | |||
| 102 | #ifndef HAVE_SSL_CTX_SET_ALPN_PROTOS | 86 | #ifndef HAVE_SSL_CTX_SET_ALPN_PROTOS |
| 103 | #define HAVE_SSL_CTX_SET_ALPN_PROTOS OPENSSL_PREREQ(1, 0, 2) | 87 | #define HAVE_SSL_CTX_SET_ALPN_PROTOS OPENSSL_PREREQ(1, 0, 2) |
| 104 | #endif | 88 | #endif |
| @@ -7827,19 +7811,20 @@ static struct randL_state *randL_getstate(lua_State *L) { | |||
| 7827 | return lua_touserdata(L, lua_upvalueindex(1)); | 7811 | return lua_touserdata(L, lua_upvalueindex(1)); |
| 7828 | } /* randL_getstate() */ | 7812 | } /* randL_getstate() */ |
| 7829 | 7813 | ||
| 7830 | #if HAVE_SYS_SYSCTL_H | 7814 | #if HAVE_SYS_SYSCALL_H |
| 7831 | #include <sys/sysctl.h> /* CTL_KERN KERN_RANDOM RANDOM_UUID KERN_URND KERN_ARND sysctl(2) */ | 7815 | #include <sys/syscall.h> /* SYS_getrandom syscall(2) */ |
| 7832 | #endif | 7816 | #endif |
| 7833 | 7817 | ||
| 7834 | #ifndef HAVE_RANDOM_UUID | 7818 | #if HAVE_SYS_SYSCTL_H |
| 7835 | #define HAVE_RANDOM_UUID (HAVE_SYS_SYSCTL_H && defined __linux__) /* RANDOM_UUID is an enum, not macro */ | 7819 | #include <sys/sysctl.h> /* CTL_KERN KERN_RANDOM RANDOM_UUID sysctl(2) */ |
| 7836 | #endif | 7820 | #endif |
| 7837 | 7821 | ||
| 7838 | static int randL_stir(struct randL_state *st, unsigned rqstd) { | 7822 | static int randL_stir(struct randL_state *st, unsigned rqstd) { |
| 7839 | unsigned count = 0; | 7823 | unsigned count = 0; |
| 7840 | int error; | 7824 | int error; |
| 7841 | unsigned char data[256]; | 7825 | unsigned char data[256]; |
| 7842 | #if defined(HAVE_ARC4RANDOM) | 7826 | |
| 7827 | #if HAVE_ARC4RANDOM | ||
| 7843 | while (count < rqstd) { | 7828 | while (count < rqstd) { |
| 7844 | size_t n = MIN(rqstd - count, sizeof data); | 7829 | size_t n = MIN(rqstd - count, sizeof data); |
| 7845 | 7830 | ||
| @@ -7849,11 +7834,14 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { | |||
| 7849 | 7834 | ||
| 7850 | count += n; | 7835 | count += n; |
| 7851 | } | 7836 | } |
| 7852 | #elif defined(HAVE_GETRANDOM) | 7837 | #endif |
| 7838 | |||
| 7839 | #if HAVE_SYSCALL && HAVE_DECL_SYS_GETRANDOM | ||
| 7853 | while (count < rqstd) { | 7840 | while (count < rqstd) { |
| 7854 | size_t n = MIN(rqstd - count, sizeof data); | 7841 | size_t lim = MIN(rqstd - count, sizeof data); |
| 7842 | int n; | ||
| 7855 | 7843 | ||
| 7856 | n = syscall(SYS_getrandom, data, n, 0); | 7844 | n = syscall(SYS_getrandom, data, lim, 0); |
| 7857 | 7845 | ||
| 7858 | if (n == -1) { | 7846 | if (n == -1) { |
| 7859 | break; | 7847 | break; |
| @@ -7863,10 +7851,11 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { | |||
| 7863 | 7851 | ||
| 7864 | count += n; | 7852 | count += n; |
| 7865 | } | 7853 | } |
| 7866 | #elif HAVE_RANDOM_UUID | 7854 | #endif |
| 7867 | int mib[] = { CTL_KERN, KERN_RANDOM, RANDOM_UUID }; | ||
| 7868 | 7855 | ||
| 7856 | #if HAVE_SYS_SYSCTL_H && HAVE_DECL_RANDOM_UUID | ||
| 7869 | while (count < rqstd) { | 7857 | while (count < rqstd) { |
| 7858 | int mib[] = { CTL_KERN, KERN_RANDOM, RANDOM_UUID }; | ||
| 7870 | size_t n = MIN(rqstd - count, sizeof data); | 7859 | size_t n = MIN(rqstd - count, sizeof data); |
| 7871 | 7860 | ||
| 7872 | if (0 != sysctl(mib, countof(mib), data, &n, (void *)0, 0)) | 7861 | if (0 != sysctl(mib, countof(mib), data, &n, (void *)0, 0)) |
| @@ -7945,7 +7934,12 @@ error:; | |||
| 7945 | #elif defined __sun | 7934 | #elif defined __sun |
| 7946 | /* | 7935 | /* |
| 7947 | * NOTE: Linux requires -lrt for clock_gettime, and in any event | 7936 | * NOTE: Linux requires -lrt for clock_gettime, and in any event |
| 7948 | * already has RANDOM_UUID. The BSDs have KERN_URND and KERN_ARND. | 7937 | * should have RANDOM_UUID or getrandom. (Though, some middle-aged |
| 7938 | * kernels might have neither). The BSDs have arc4random which | ||
| 7939 | * should be using KERN_URND, KERN_ARND, and more recently | ||
| 7940 | * getentropy. (Though, again, some older BSD kernels used an | ||
| 7941 | * arc4random implementation that opened /dev/urandom.) | ||
| 7942 | * | ||
| 7949 | * Just do this for Solaris to keep things simple. We've already | 7943 | * Just do this for Solaris to keep things simple. We've already |
| 7950 | * crossed the line of what can be reasonably accomplished on | 7944 | * crossed the line of what can be reasonably accomplished on |
| 7951 | * unreasonable platforms. | 7945 | * unreasonable platforms. |
