Replace CommonRandom SPI with API
authorjiewen_tan@apple.com <jiewen_tan@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 2 Nov 2018 06:35:59 +0000 (06:35 +0000)
committerjiewen_tan@apple.com <jiewen_tan@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 2 Nov 2018 06:35:59 +0000 (06:35 +0000)
https://bugs.webkit.org/show_bug.cgi?id=191178
<rdar://problem/45722391>

Reviewed by Brent Fulgham.

Source/bmalloc:

* bmalloc/CryptoRandom.cpp:
(bmalloc::ARC4RandomNumberGenerator::stir):

Source/WebCore:

The API is available since macOS 10.10 and iOS 10, and therefore it is safe to replace
all SPI usages with API.

No change of behaviors.

* crypto/CommonCryptoUtilities.h:
* crypto/mac/CryptoKeyMac.cpp:
(WebCore::CryptoKey::randomData):
* crypto/mac/SerializedCryptoKeyWrapMac.mm:
(WebCore::createAndStoreMasterKey):
(WebCore::wrapSerializedCryptoKey):
* page/Crypto.cpp:
(WebCore::Crypto::getRandomValues):

Source/WTF:

* WTF.xcodeproj/project.pbxproj:
* wtf/CMakeLists.txt:
* wtf/RandomDevice.cpp:
(WTF::RandomDevice::cryptographicallyRandomValues):
* wtf/spi/darwin/CommonCryptoSPI.h: Removed.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@237721 268f45cc-cd09-0410-ab3c-d52691b4dbfc

12 files changed:
Source/WTF/ChangeLog
Source/WTF/WTF.xcodeproj/project.pbxproj
Source/WTF/wtf/CMakeLists.txt
Source/WTF/wtf/RandomDevice.cpp
Source/WTF/wtf/spi/darwin/CommonCryptoSPI.h [deleted file]
Source/WebCore/ChangeLog
Source/WebCore/crypto/CommonCryptoUtilities.h
Source/WebCore/crypto/mac/CryptoKeyMac.cpp
Source/WebCore/crypto/mac/SerializedCryptoKeyWrapMac.mm
Source/WebCore/page/Crypto.cpp
Source/bmalloc/ChangeLog
Source/bmalloc/bmalloc/CryptoRandom.cpp

index 22bfb70..6244eaa 100644 (file)
@@ -1,3 +1,17 @@
+2018-11-01  Jiewen Tan  <jiewen_tan@apple.com>
+
+        Replace CommonRandom SPI with API
+        https://bugs.webkit.org/show_bug.cgi?id=191178
+        <rdar://problem/45722391>
+
+        Reviewed by Brent Fulgham.
+
+        * WTF.xcodeproj/project.pbxproj:
+        * wtf/CMakeLists.txt:
+        * wtf/RandomDevice.cpp:
+        (WTF::RandomDevice::cryptographicallyRandomValues):
+        * wtf/spi/darwin/CommonCryptoSPI.h: Removed.
+
 2018-11-01  Fujii Hironori  <Hironori.Fujii@sony.com>
 
         Rename <wtf/unicode/UTF8.h> to <wtf/unicode/UTF8Conversion.h> in order to avoid conflicting with ICU's unicode/utf8.h
index b0de1cc..a7eca33 100644 (file)
                DCEE21FC1CEA7551000C2396 /* BlockObjCExceptions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BlockObjCExceptions.h; sourceTree = "<group>"; };
                DCEE21FD1CEA7551000C2396 /* BlockObjCExceptions.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = BlockObjCExceptions.mm; sourceTree = "<group>"; };
                DCEE22041CEB9869000C2396 /* BackwardsGraph.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BackwardsGraph.h; sourceTree = "<group>"; };
-               DE5A09FB1BA36992003D4424 /* CommonCryptoSPI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CommonCryptoSPI.h; sourceTree = "<group>"; };
                E15556F318A0CC18006F48FB /* CryptographicUtilities.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CryptographicUtilities.cpp; sourceTree = "<group>"; };
                E15556F418A0CC18006F48FB /* CryptographicUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CryptographicUtilities.h; sourceTree = "<group>"; };
                E300E521203D645F00DA79BE /* UniqueArray.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UniqueArray.h; sourceTree = "<group>"; };
                CE73E02319DCB7AB00580D5C /* darwin */ = {
                        isa = PBXGroup;
                        children = (
-                               DE5A09FB1BA36992003D4424 /* CommonCryptoSPI.h */,
                                E431CC4A21187ADB000C8A07 /* DispatchSPI.h */,
                                93DDE9311CDC052D00FD3491 /* dyldSPI.h */,
                                A5098AFF1C169E0700087797 /* SandboxSPI.h */,
index ac6f0e9..e2de209 100644 (file)
@@ -465,15 +465,6 @@ set(WTF_LIBRARIES
     ${CMAKE_DL_LIBS}
 )
 
-if (CMAKE_SYSTEM_NAME MATCHES "Darwin")
-    list(APPEND WTF_PUBLIC_HEADERS
-        spi/darwin/CommonCryptoSPI.h
-    )
-list(APPEND WTF_PRIVATE_INCLUDE_DIRECTORIES
-        "${WTF_DIR}/wtf/spi/darwin"
-    )
-endif ()
-
 if (NOT USE_SYSTEM_MALLOC)
     list(APPEND WTF_LIBRARIES bmalloc)
 endif ()
index 39ff658..89165f0 100644 (file)
@@ -42,7 +42,8 @@
 #endif
 
 #if OS(DARWIN)
-#include <wtf/spi/darwin/CommonCryptoSPI.h>
+#include <CommonCrypto/CommonCryptoError.h>
+#include <CommonCrypto/CommonRandom.h>
 #endif
 
 #if OS(FUCHSIA)
@@ -88,7 +89,7 @@ RandomDevice::~RandomDevice()
 void RandomDevice::cryptographicallyRandomValues(unsigned char* buffer, size_t length)
 {
 #if OS(DARWIN)
-    RELEASE_ASSERT(!CCRandomCopyBytes(kCCRandomDefault, buffer, length));
+    RELEASE_ASSERT(!CCRandomGenerateBytes(buffer, length));
 #elif OS(FUCHSIA)
     zx_cprng_draw(buffer, length);
 #elif OS(UNIX)
diff --git a/Source/WTF/wtf/spi/darwin/CommonCryptoSPI.h b/Source/WTF/wtf/spi/darwin/CommonCryptoSPI.h
deleted file mode 100644 (file)
index c3dc398..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Copyright (C) 2015-2016 Apple Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY APPLE 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 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
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#pragma once
-
-#if OS(DARWIN)
-
-#if USE(APPLE_INTERNAL_SDK)
-#include <CommonCrypto/CommonRandomSPI.h>
-#endif
-
-typedef struct __CCRandom* CCRandomRef;
-
-WTF_EXTERN_C_BEGIN
-
-extern const CCRandomRef kCCRandomDefault;
-int CCRandomCopyBytes(CCRandomRef rnd, void *bytes, size_t count);
-
-WTF_EXTERN_C_END
-
-#endif // OS(DARWIN)
index 425855a..5ebe766 100644 (file)
@@ -1,3 +1,25 @@
+2018-11-01  Jiewen Tan  <jiewen_tan@apple.com>
+
+        Replace CommonRandom SPI with API
+        https://bugs.webkit.org/show_bug.cgi?id=191178
+        <rdar://problem/45722391>
+
+        Reviewed by Brent Fulgham.
+
+        The API is available since macOS 10.10 and iOS 10, and therefore it is safe to replace
+        all SPI usages with API.
+
+        No change of behaviors.
+
+        * crypto/CommonCryptoUtilities.h:
+        * crypto/mac/CryptoKeyMac.cpp:
+        (WebCore::CryptoKey::randomData):
+        * crypto/mac/SerializedCryptoKeyWrapMac.mm:
+        (WebCore::createAndStoreMasterKey):
+        (WebCore::wrapSerializedCryptoKey):
+        * page/Crypto.cpp:
+        (WebCore::Crypto::getRandomValues):
+
 2018-11-01  Chris Dumez  <cdumez@apple.com>
 
         [WebIDL] Rename CallWith=ScriptState to CallWith=ExecState
index 1ca7179..9285515 100644 (file)
@@ -33,6 +33,7 @@
 
 #include "CryptoAlgorithmIdentifier.h"
 #include <CommonCrypto/CommonCryptor.h>
+#include <CommonCrypto/CommonRandom.h>
 #include <wtf/Vector.h>
 
 #if USE(APPLE_INTERNAL_SDK)
@@ -41,7 +42,6 @@
 // FIXME: <rdar://problem/31508959>
 // #include <CommonCrypto/CommonKeyDerivationSPI.h>
 #include <CommonCrypto/CommonRSACryptor.h>
-#include <CommonCrypto/CommonRandomSPI.h>
 #endif
 
 #if USE(APPLE_INTERNAL_SDK) && HAVE(CCRSAGetCRTComponents)
@@ -79,10 +79,6 @@ enum {
 
 typedef struct _CCBigNumRef *CCBigNumRef;
 
-typedef struct __CCRandom *CCRandomRef;
-extern const CCRandomRef kCCRandomDefault;
-extern "C" int CCRandomCopyBytes(CCRandomRef rnd, void *bytes, size_t count);
-
 typedef struct _CCRSACryptor *CCRSACryptorRef;
 extern "C" CCCryptorStatus CCRSACryptorEncrypt(CCRSACryptorRef publicKey, CCAsymmetricPadding padding, const void *plainText, size_t plainTextLen, void *cipherText, size_t *cipherTextLen, const void *tagData, size_t tagDataLen, CCDigestAlgorithm digestType);
 extern "C" CCCryptorStatus CCRSACryptorDecrypt(CCRSACryptorRef privateKey, CCAsymmetricPadding padding, const void *cipherText, size_t cipherTextLen, void *plainText, size_t *plainTextLen, const void *tagData, size_t tagDataLen, CCDigestAlgorithm digestType);
index 13e2e57..292c1a7 100644 (file)
@@ -35,7 +35,7 @@ namespace WebCore {
 Vector<uint8_t> CryptoKey::randomData(size_t size)
 {
     Vector<uint8_t> result(size);
-    int rc = CCRandomCopyBytes(kCCRandomDefault, result.data(), result.size());
+    auto rc = CCRandomGenerateBytes(result.data(), result.size());
     RELEASE_ASSERT(rc == kCCSuccess);
     return result;
 }
index 74fe5df..999b3ba 100644 (file)
@@ -84,7 +84,7 @@ static bool createAndStoreMasterKey(Vector<uint8_t>& masterKeyData)
     RELEASE_ASSERT(hasProcessPrivilege(ProcessPrivilege::CanAccessCredentials));
 
     masterKeyData.resize(masterKeySizeInBytes);
-    int rc = CCRandomCopyBytes(kCCRandomDefault, masterKeyData.data(), masterKeyData.size());
+    auto rc = CCRandomGenerateBytes(masterKeyData.data(), masterKeyData.size());
     RELEASE_ASSERT(rc == kCCSuccess);
 
 #if PLATFORM(IOS_FAMILY)
@@ -204,7 +204,7 @@ bool deleteDefaultWebCryptoMasterKey()
 bool wrapSerializedCryptoKey(const Vector<uint8_t>& masterKey, const Vector<uint8_t>& key, Vector<uint8_t>& result)
 {
     Vector<uint8_t> kek(16);
-    int rc = CCRandomCopyBytes(kCCRandomDefault, kek.data(), kek.size());
+    auto rc = CCRandomGenerateBytes(kek.data(), kek.size());
     RELEASE_ASSERT(rc == kCCSuccess);
 
     Vector<uint8_t> wrappedKEK(CCSymmetricWrappedSize(kCCWRAPAES, kek.size()));
index bc37cec..0c9be35 100644 (file)
@@ -59,7 +59,7 @@ ExceptionOr<void> Crypto::getRandomValues(ArrayBufferView& array)
     if (array.byteLength() > 65536)
         return Exception { QuotaExceededError };
 #if OS(DARWIN)
-    int rc = CCRandomCopyBytes(kCCRandomDefault, array.baseAddress(), array.byteLength());
+    auto rc = CCRandomGenerateBytes(array.baseAddress(), array.byteLength());
     RELEASE_ASSERT(rc == kCCSuccess);
 #else
     cryptographicallyRandomValues(array.baseAddress(), array.byteLength());
index 9b5602f..37adfe8 100644 (file)
@@ -1,3 +1,14 @@
+2018-11-01  Jiewen Tan  <jiewen_tan@apple.com>
+
+        Replace CommonRandom SPI with API
+        https://bugs.webkit.org/show_bug.cgi?id=191178
+        <rdar://problem/45722391>
+
+        Reviewed by Brent Fulgham.
+
+        * bmalloc/CryptoRandom.cpp:
+        (bmalloc::ARC4RandomNumberGenerator::stir):
+
 2018-10-29  Mark Lam  <mark.lam@apple.com>
 
         Correctly detect string overflow when using the 'Function' constructor.
index 4fef713..8c415ee 100644 (file)
 #endif
 
 #if BOS(DARWIN)
-typedef struct __CCRandom* CCRandomRef;
-
-extern "C" {
-extern const CCRandomRef kCCRandomDefault;
-int CCRandomCopyBytes(CCRandomRef rnd, void *bytes, size_t count);
-}
+#include <CommonCrypto/CommonCryptoError.h>
+#include <CommonCrypto/CommonRandom.h>
 #endif
 
 namespace bmalloc {
@@ -113,7 +109,7 @@ void ARC4RandomNumberGenerator::stir()
     size_t length = sizeof(randomness);
 
 #if BOS(DARWIN)
-    RELEASE_BASSERT(!CCRandomCopyBytes(kCCRandomDefault, randomness, length));
+    RELEASE_BASSERT(!CCRandomGenerateBytes(randomness, length));
 #else
     static std::once_flag onceFlag;
     static int fd;