From 1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c Mon Sep 17 00:00:00 2001 From: Lorry Tar Creator Date: Tue, 27 Jun 2017 06:07:23 +0000 Subject: webkitgtk-2.16.5 --- Source/WTF/wtf/OSRandomSource.cpp | 43 ++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) (limited to 'Source/WTF/wtf/OSRandomSource.cpp') diff --git a/Source/WTF/wtf/OSRandomSource.cpp b/Source/WTF/wtf/OSRandomSource.cpp index 2495abf71..378795dc7 100644 --- a/Source/WTF/wtf/OSRandomSource.cpp +++ b/Source/WTF/wtf/OSRandomSource.cpp @@ -13,7 +13,7 @@ * THIS SOFTWARE IS PROVIDED BY GOOGLE, INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR @@ -29,7 +29,8 @@ #include #include -#if OS(UNIX) +#if !OS(DARWIN) && OS(UNIX) +#include #include #include #endif @@ -39,19 +40,47 @@ #include // windows.h must be included before wincrypt.h. #endif +#if OS(DARWIN) +#include "CommonCryptoSPI.h" +#endif + namespace WTF { +#if !OS(DARWIN) && OS(UNIX) +NEVER_INLINE NO_RETURN_DUE_TO_CRASH static void crashUnableToOpenURandom() +{ + CRASH(); +} + +NEVER_INLINE NO_RETURN_DUE_TO_CRASH static void crashUnableToReadFromURandom() +{ + CRASH(); +} +#endif + void cryptographicallyRandomValuesFromOS(unsigned char* buffer, size_t length) { -#if OS(UNIX) +#if OS(DARWIN) + RELEASE_ASSERT(!CCRandomCopyBytes(kCCRandomDefault, buffer, length)); +#elif OS(UNIX) int fd = open("/dev/urandom", O_RDONLY, 0); if (fd < 0) - CRASH(); // We need /dev/urandom for this API to work... - - if (read(fd, buffer, length) != static_cast(length)) - CRASH(); + crashUnableToOpenURandom(); // We need /dev/urandom for this API to work... + ssize_t amountRead = 0; + while (static_cast(amountRead) < length) { + ssize_t currentRead = read(fd, buffer + amountRead, length - amountRead); + // We need to check for both EAGAIN and EINTR since on some systems /dev/urandom + // is blocking and on others it is non-blocking. + if (currentRead == -1) { + if (!(errno == EAGAIN || errno == EINTR)) + crashUnableToReadFromURandom(); + } else + amountRead += currentRead; + } + close(fd); + #elif OS(WINDOWS) HCRYPTPROV hCryptProv = 0; if (!CryptAcquireContext(&hCryptProv, 0, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) -- cgit v1.2.1