Title: [197905] trunk/Source/WebCore
Revision
197905
Author
[email protected]
Date
2016-03-09 17:09:03 -0800 (Wed, 09 Mar 2016)

Log Message

[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.

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (197904 => 197905)


--- trunk/Source/WebCore/ChangeLog	2016-03-10 01:02:35 UTC (rev 197904)
+++ trunk/Source/WebCore/ChangeLog	2016-03-10 01:09:03 UTC (rev 197905)
@@ -1,3 +1,26 @@
+2016-03-09  Daniel Bates  <[email protected]>
+
+        [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  <[email protected]>
 
         REGRESSION (r197149): Missing availability checks when soft-linking DataDetectors.framework

Modified: trunk/Source/WebCore/PlatformWin.cmake (197904 => 197905)


--- trunk/Source/WebCore/PlatformWin.cmake	2016-03-10 01:02:35 UTC (rev 197904)
+++ trunk/Source/WebCore/PlatformWin.cmake	2016-03-10 01:09:03 UTC (rev 197905)
@@ -70,6 +70,8 @@
 
     platform/cf/win/CertificateCFWin.cpp
 
+    platform/crypto/win/CryptoDigestWin.cpp
+
     platform/graphics/FontPlatformData.cpp
     platform/graphics/GraphicsContext3DPrivate.cpp
     platform/graphics/WOFFFileFormat.cpp

Modified: trunk/Source/WebCore/PlatformWinCairo.cmake (197904 => 197905)


--- trunk/Source/WebCore/PlatformWinCairo.cmake	2016-03-10 01:02:35 UTC (rev 197904)
+++ trunk/Source/WebCore/PlatformWinCairo.cmake	2016-03-10 01:09:03 UTC (rev 197905)
@@ -35,6 +35,8 @@
 
     platform/cf/win/CertificateCFWin.cpp
 
+    platform/crypto/win/CryptoDigestWin.cpp
+
     platform/graphics/GLContext.cpp
     platform/graphics/GraphicsLayer.cpp
     platform/graphics/ImageSource.cpp

Added: trunk/Source/WebCore/platform/crypto/win/CryptoDigestWin.cpp (0 => 197905)


--- trunk/Source/WebCore/platform/crypto/win/CryptoDigestWin.cpp	                        (rev 0)
+++ trunk/Source/WebCore/platform/crypto/win/CryptoDigestWin.cpp	2016-03-10 01:09:03 UTC (rev 197905)
@@ -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
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to