[GTK] Support latest SubtleCrypto HMAC APIs
authorutatane.tea@gmail.com <utatane.tea@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 15 Dec 2016 02:31:37 +0000 (02:31 +0000)
committerutatane.tea@gmail.com <utatane.tea@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 15 Dec 2016 02:31:37 +0000 (02:31 +0000)
https://bugs.webkit.org/show_bug.cgi?id=165863

Reviewed by Sam Weinig.

Source/WebCore:

SubtleCrypto spec is updated, and new delegate implementations are required to support these interfaces.
This patch implements platformSign and platformVerify with WorkQueues for gcrypt backend.

* crypto/gcrypt/CryptoAlgorithmHMACGCrypt.cpp:
(WebCore::calculateSignature):
(WebCore::CryptoAlgorithmHMAC::platformSign):
(WebCore::CryptoAlgorithmHMAC::platformVerify):

LayoutTests:

Enable HMAC tests.

* platform/gtk/TestExpectations:

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

LayoutTests/ChangeLog
LayoutTests/platform/gtk/TestExpectations
Source/WebCore/ChangeLog
Source/WebCore/crypto/gcrypt/CryptoAlgorithmHMACGCrypt.cpp

index e955b8f..f8970f2 100644 (file)
@@ -1,3 +1,14 @@
+2016-12-14  Yusuke Suzuki  <utatane.tea@gmail.com>
+
+        [GTK] Support latest SubtleCrypto HMAC APIs
+        https://bugs.webkit.org/show_bug.cgi?id=165863
+
+        Reviewed by Sam Weinig.
+
+        Enable HMAC tests.
+
+        * platform/gtk/TestExpectations:
+
 2016-12-14  Simon Fraser  <simon.fraser@apple.com>
 
         Fix cause of viewport-related flakiness in iOS tests
index 4119c37..98e156c 100644 (file)
@@ -365,6 +365,46 @@ Bug(GTK) editing/secure-input [ Failure ]
 webkit.org/b/133122 crypto/subtle [ Skip ]
 webkit.org/b/133122 crypto/webkitSubtle [ Skip ]
 webkit.org/b/133122 crypto/workers/subtle [ Skip ]
+crypto/subtle/hmac-export-key-malformed-parameters.html [ Pass ]
+crypto/subtle/hmac-generate-export-key-jwk-sha1.html [ Pass ]
+crypto/subtle/hmac-generate-export-key-jwk-sha224.html [ Pass ]
+crypto/subtle/hmac-generate-export-key-jwk-sha256.html [ Pass ]
+crypto/subtle/hmac-generate-export-key-jwk-sha384.html [ Pass ]
+crypto/subtle/hmac-generate-export-key-jwk-sha512.html [ Pass ]
+crypto/subtle/hmac-generate-export-raw-key.html [ Pass ]
+crypto/subtle/hmac-generate-key-customized-length.html [ Pass ]
+crypto/subtle/hmac-generate-key-hash-object.html [ Pass ]
+crypto/subtle/hmac-generate-key-malformed-parameters.html [ Pass ]
+crypto/subtle/hmac-generate-key-sha1.html [ Pass ]
+crypto/subtle/hmac-generate-key-sha224.html [ Pass ]
+crypto/subtle/hmac-generate-key-sha256.html [ Pass ]
+crypto/subtle/hmac-generate-key-sha384.html [ Pass ]
+crypto/subtle/hmac-generate-key-sha512.html [ Pass ]
+crypto/subtle/hmac-generate-key-sign-verify.html [ Pass ]
+crypto/subtle/hmac-import-jwk-key-export-jwk-key.html [ Pass ]
+crypto/subtle/hmac-import-jwk-key-export-raw-key.html [ Pass ]
+crypto/subtle/hmac-import-jwk-key-minimum.html [ Pass ]
+crypto/subtle/hmac-import-jwk-key-non-extractable.html [ Pass ]
+crypto/subtle/hmac-import-jwk-key-sha1.html [ Pass ]
+crypto/subtle/hmac-import-jwk-key-sha224.html [ Pass ]
+crypto/subtle/hmac-import-jwk-key-sha256.html [ Pass ]
+crypto/subtle/hmac-import-jwk-key-sha384.html [ Pass ]
+crypto/subtle/hmac-import-jwk-key-sha512.html [ Pass ]
+crypto/subtle/hmac-import-key-malformed-parameters.html [ Pass ]
+crypto/subtle/hmac-import-key-sign-sha1.html [ Pass ]
+crypto/subtle/hmac-import-key-sign-sha224.html [ Pass ]
+crypto/subtle/hmac-import-key-sign-sha256.html [ Pass ]
+crypto/subtle/hmac-import-key-sign-sha384.html [ Pass ]
+crypto/subtle/hmac-import-key-sign-sha512.html [ Pass ]
+crypto/subtle/hmac-import-key-verify-sha1.html [ Pass ]
+crypto/subtle/hmac-import-key-verify-sha224.html [ Pass ]
+crypto/subtle/hmac-import-key-verify-sha256.html [ Pass ]
+crypto/subtle/hmac-import-key-verify-sha384.html [ Pass ]
+crypto/subtle/hmac-import-key-verify-sha512.html [ Pass ]
+crypto/subtle/hmac-import-raw-key-customized-length.html [ Pass ]
+crypto/subtle/hmac-import-raw-key-export-jwk-key.html [ Pass ]
+crypto/subtle/hmac-import-raw-key-export-raw-key.html [ Pass ]
+crypto/subtle/hmac-import-raw-key.html [ Pass ]
 webkit.org/b/133319 crypto/webkitSubtle/sha-1.html [ Pass ]
 webkit.org/b/133319 crypto/webkitSubtle/sha-224.html [ Pass ]
 webkit.org/b/133319 crypto/webkitSubtle/sha-256.html [ Pass ]
index 5cb1426..46eaf0a 100644 (file)
@@ -1,3 +1,18 @@
+2016-12-14  Yusuke Suzuki  <utatane.tea@gmail.com>
+
+        [GTK] Support latest SubtleCrypto HMAC APIs
+        https://bugs.webkit.org/show_bug.cgi?id=165863
+
+        Reviewed by Sam Weinig.
+
+        SubtleCrypto spec is updated, and new delegate implementations are required to support these interfaces.
+        This patch implements platformSign and platformVerify with WorkQueues for gcrypt backend.
+
+        * crypto/gcrypt/CryptoAlgorithmHMACGCrypt.cpp:
+        (WebCore::calculateSignature):
+        (WebCore::CryptoAlgorithmHMAC::platformSign):
+        (WebCore::CryptoAlgorithmHMAC::platformVerify):
+
 2016-12-14  Chris Dumez  <cdumez@apple.com>
 
         Unreviewed, rolling out r209415.
index 45a6688..746e3be 100644 (file)
@@ -1,6 +1,8 @@
 /*
  * Copyright (C) 2014 Igalia S.L.
  * Copyright (C) 2016 SoftAtHome
+ * Copyright (C) 2016 Apple Inc.
+ * Copyright (C) 2016 Yusuke Suzuki <utatane.tea@gmail.com>.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -32,7 +34,7 @@
 #include "CryptoAlgorithmHmacParamsDeprecated.h"
 #include "CryptoKeyHMAC.h"
 #include "ExceptionCode.h"
-#include "NotImplemented.h"
+#include "ScriptExecutionContext.h"
 #include <gcrypt.h>
 #include <wtf/CryptographicUtilities.h>
 
@@ -56,7 +58,7 @@ static int getGCryptDigestAlgorithm(CryptoAlgorithmIdentifier hashFunction)
     }
 }
 
-static std::optional<Vector<uint8_t>> calculateSignature(int algorithm, const Vector<uint8_t>& key, const CryptoOperationData& data)
+static std::optional<Vector<uint8_t>> calculateSignature(int algorithm, const Vector<uint8_t>& key, const uint8_t* data, size_t dataLength)
 {
     size_t digestLength = gcry_mac_get_algo_maclen(algorithm);
     const void* keyData = key.data() ? key.data() : reinterpret_cast<const uint8_t*>("");
@@ -75,7 +77,7 @@ static std::optional<Vector<uint8_t>> calculateSignature(int algorithm, const Ve
     if (err)
         goto cleanup;
 
-    err = gcry_mac_write(hd, data.first, data.second);
+    err = gcry_mac_write(hd, data, dataLength);
     if (err)
         goto cleanup;
 
@@ -97,14 +99,61 @@ cleanup:
     return WTFMove(signature);
 }
 
-void CryptoAlgorithmHMAC::platformSign(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&)
+static std::optional<Vector<uint8_t>> calculateSignature(int algorithm, const Vector<uint8_t>& key, const CryptoOperationData& data)
+{
+    return calculateSignature(algorithm, key, data.first, data.second);
+}
+
+void CryptoAlgorithmHMAC::platformSign(Ref<CryptoKey>&& key, Vector<uint8_t>&& data, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
 {
-    notImplemented();
+    context.ref();
+    workQueue.dispatch([key = WTFMove(key), data = WTFMove(data), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
+        auto& hmacKey = downcast<CryptoKeyHMAC>(key.get());
+        auto algorithm = getGCryptDigestAlgorithm(hmacKey.hashAlgorithmIdentifier());
+        if (algorithm != GCRY_MAC_NONE) {
+            auto result = calculateSignature(algorithm, hmacKey.key(), data.data(), data.size());
+            if (result) {
+                // We should only dereference callbacks after being back to the Document/Worker threads.
+                context.postTask([callback = WTFMove(callback), result = WTFMove(*result), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
+                    callback(result);
+                    context.deref();
+                });
+                return;
+            }
+        }
+        // We should only dereference callbacks after being back to the Document/Worker threads.
+        context.postTask([exceptionCallback = WTFMove(exceptionCallback), callback = WTFMove(callback)](ScriptExecutionContext& context) {
+            exceptionCallback(OperationError);
+            context.deref();
+        });
+    });
 }
 
-void CryptoAlgorithmHMAC::platformVerify(Ref<CryptoKey>&&, Vector<uint8_t>&&, Vector<uint8_t>&&, BoolCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&)
+void CryptoAlgorithmHMAC::platformVerify(Ref<CryptoKey>&& key, Vector<uint8_t>&& signature, Vector<uint8_t>&& data, BoolCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
 {
-    notImplemented();
+    context.ref();
+    workQueue.dispatch([key = WTFMove(key), signature = WTFMove(signature), data = WTFMove(data), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
+        auto& hmacKey = downcast<CryptoKeyHMAC>(key.get());
+        auto algorithm = getGCryptDigestAlgorithm(hmacKey.hashAlgorithmIdentifier());
+        if (algorithm != GCRY_MAC_NONE) {
+            auto expectedSignature = calculateSignature(algorithm, hmacKey.key(), data.data(), data.size());
+            if (expectedSignature) {
+                // Using a constant time comparison to prevent timing attacks.
+                bool result = signature.size() == expectedSignature->size() && !constantTimeMemcmp(expectedSignature->data(), signature.data(), expectedSignature->size());
+                // We should only dereference callbacks after being back to the Document/Worker threads.
+                context.postTask([callback = WTFMove(callback), result, exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
+                    callback(result);
+                    context.deref();
+                });
+                return;
+            }
+        }
+        // We should only dereference callbacks after being back to the Document/Worker threads.
+        context.postTask([exceptionCallback = WTFMove(exceptionCallback), callback = WTFMove(callback)](ScriptExecutionContext& context) {
+            exceptionCallback(OperationError);
+            context.deref();
+        });
+    });
 }
 
 ExceptionOr<void> CryptoAlgorithmHMAC::platformSign(const CryptoAlgorithmHmacParamsDeprecated& parameters, const CryptoKeyHMAC& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback)