[Win] Implement CryptoDigest
authordbates@webkit.org <dbates@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 10 Mar 2016 01:09:03 +0000 (01:09 +0000)
committerdbates@webkit.org <dbates@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 10 Mar 2016 01:09:03 +0000 (01:09 +0000)
https://bugs.webkit.org/show_bug.cgi?id=155247
<rdar://problem/25065843>

Reviewed by Brent Fulgham.

Implement the CryptoDigest abstraction for Windows so that we can compute cryptographically
secure hashes. This will allow us to support Content Security Policy inline script and inline
stylesheet hashes on Windows.

* PlatformWin.cmake: Add file CryptoDigestWin.cpp.
* PlatformWinCairo.cmake: Ditto.
* platform/crypto/win/CryptoDigestWin.cpp: Added.
(WebCore::CryptoDigest::CryptoDigest): Instantiate a CryptoDigestContext object.
(WebCore::CryptoDigest::~CryptoDigest): Destroy the cryptographic service provider and hash
object if non-null.
(WebCore::CryptoDigest::create): Acquire a handle to a cryptographic service provider (HCRYPTPROV)
and a handle to a hash object (HCRYPTHASH).
(WebCore::CryptoDigest::addBytes): Add the contents of the specified buffer to the hash object.
(WebCore::CryptoDigest::computeHash): Compute and return a Vector of bytes that represent the digest.

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

Source/WebCore/ChangeLog
Source/WebCore/PlatformWin.cmake
Source/WebCore/PlatformWinCairo.cmake
Source/WebCore/platform/crypto/win/CryptoDigestWin.cpp [new file with mode: 0644]

index dda9898..34624d3 100644 (file)
@@ -1,3 +1,26 @@
+2016-03-09  Daniel Bates  <dabates@apple.com>
+
+        [Win] Implement CryptoDigest
+        https://bugs.webkit.org/show_bug.cgi?id=155247
+        <rdar://problem/25065843>
+
+        Reviewed by Brent Fulgham.
+
+        Implement the CryptoDigest abstraction for Windows so that we can compute cryptographically
+        secure hashes. This will allow us to support Content Security Policy inline script and inline
+        stylesheet hashes on Windows.
+
+        * PlatformWin.cmake: Add file CryptoDigestWin.cpp.
+        * PlatformWinCairo.cmake: Ditto.
+        * platform/crypto/win/CryptoDigestWin.cpp: Added.
+        (WebCore::CryptoDigest::CryptoDigest): Instantiate a CryptoDigestContext object.
+        (WebCore::CryptoDigest::~CryptoDigest): Destroy the cryptographic service provider and hash
+        object if non-null.
+        (WebCore::CryptoDigest::create): Acquire a handle to a cryptographic service provider (HCRYPTPROV)
+        and a handle to a hash object (HCRYPTHASH).
+        (WebCore::CryptoDigest::addBytes): Add the contents of the specified buffer to the hash object.
+        (WebCore::CryptoDigest::computeHash): Compute and return a Vector of bytes that represent the digest.
+
 2016-03-09  David Kilzer  <ddkilzer@apple.com>
 
         REGRESSION (r197149): Missing availability checks when soft-linking DataDetectors.framework
index 1e68d72..71561e2 100644 (file)
@@ -70,6 +70,8 @@ list(APPEND WebCore_SOURCES
 
     platform/cf/win/CertificateCFWin.cpp
 
+    platform/crypto/win/CryptoDigestWin.cpp
+
     platform/graphics/FontPlatformData.cpp
     platform/graphics/GraphicsContext3DPrivate.cpp
     platform/graphics/WOFFFileFormat.cpp
index c3e4859..38a028c 100644 (file)
@@ -35,6 +35,8 @@ list(APPEND WebCore_SOURCES
 
     platform/cf/win/CertificateCFWin.cpp
 
+    platform/crypto/win/CryptoDigestWin.cpp
+
     platform/graphics/GLContext.cpp
     platform/graphics/GraphicsLayer.cpp
     platform/graphics/ImageSource.cpp
diff --git a/Source/WebCore/platform/crypto/win/CryptoDigestWin.cpp b/Source/WebCore/platform/crypto/win/CryptoDigestWin.cpp
new file mode 100644 (file)
index 0000000..faca723
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * Copyright (C) 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. AND ITS CONTRIBUTORS ``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 ITS 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.
+ */
+
+#include "config.h"
+#include "CryptoDigest.h"
+
+#include <wincrypt.h>
+
+namespace WebCore {
+
+struct CryptoDigestContext {
+    CryptoDigest::Algorithm algorithm;
+    HCRYPTPROV hContext { 0 };
+    HCRYPTHASH hHash { 0 };
+};
+
+CryptoDigest::CryptoDigest()
+    : m_context(new CryptoDigestContext)
+{
+}
+
+CryptoDigest::~CryptoDigest()
+{
+    if (HCRYPTHASH hHash = m_context->hHash)
+        CryptDestroyHash(hHash);
+    if (HCRYPTPROV hContext = m_context->hContext)
+        CryptReleaseContext(hContext, 0);
+}
+
+std::unique_ptr<CryptoDigest> CryptoDigest::create(Algorithm algorithm)
+{
+    auto digest = std::make_unique<CryptoDigest>();
+    digest->m_context->algorithm = algorithm;
+    if (!CryptAcquireContext(&digest->m_context->hContext, nullptr, nullptr /* use default provider */, PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
+        return nullptr;
+
+    bool succeeded = false;
+    switch (algorithm) {
+    case CryptoDigest::Algorithm::SHA_1: {
+        succeeded = CryptCreateHash(digest->m_context->hContext, CALG_SHA1, 0, 0, &digest->m_context->hHash);
+        break;
+    }
+    case CryptoDigest::Algorithm::SHA_256: {
+        succeeded = CryptCreateHash(digest->m_context->hContext, CALG_SHA_256, 0, 0, &digest->m_context->hHash);
+        break;
+    }
+    case CryptoDigest::Algorithm::SHA_384: {
+        succeeded = CryptCreateHash(digest->m_context->hContext, CALG_SHA_384, 0, 0, &digest->m_context->hHash);
+        break;
+    }
+    case CryptoDigest::Algorithm::SHA_512: {
+        succeeded = CryptCreateHash(digest->m_context->hContext, CALG_SHA_512, 0, 0, &digest->m_context->hHash);
+        break;
+    }
+    }
+    if (succeeded)
+        return digest;
+    return nullptr;
+}
+
+void CryptoDigest::addBytes(const void* input, size_t length)
+{
+    if (!input || !length)
+        return;
+    RELEASE_ASSERT(CryptHashData(m_context->hHash, reinterpret_cast<const BYTE*>(input), length, 0));
+}
+
+Vector<uint8_t> CryptoDigest::computeHash()
+{
+    Vector<uint8_t> result;
+    DWORD digestLengthBuffer;
+    DWORD digestLengthBufferSize = sizeof(digestLengthBuffer);
+
+    RELEASE_ASSERT(CryptGetHashParam(m_context->hHash, HP_HASHSIZE, reinterpret_cast<BYTE*>(&digestLengthBuffer), &digestLengthBufferSize, 0));
+    result.resize(digestLengthBuffer);
+
+    RELEASE_ASSERT(CryptGetHashParam(m_context->hHash, HP_HASHVAL, result.data(), &digestLengthBuffer, 0));
+    RELEASE_ASSERT(result.size() == digestLengthBuffer);
+    return result;
+}
+
+} // namespace WebCore