Title: [214550] trunk/Source/WebCore
Revision
214550
Author
[email protected]
Date
2017-03-29 11:44:57 -0700 (Wed, 29 Mar 2017)

Log Message

[GCrypt] Add a Handle<> class to help with GCrypt object lifetime control
https://bugs.webkit.org/show_bug.cgi?id=170238

Reviewed by Michael Catanzaro.

Source/WebCore:

The platform-specific CryptoAlgorithmHMAC implementation is modified
to showcase the GCrypt::Handle<> use. HandleDeleter<gcry_mac_hd_t>
is added accordingly.

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

Source/WebCore/PAL:

Add a GCrypt-specific Handle<> template class, inside the GCrypt namespace.
Objects of this class should be used as 'smart handles', cleaning up upon
destruction the GCrypt object that's represented by the handle they manage.

This mimics the std::unique_ptr<> idea, but is narrowly focused towards
how such handles are used in the libgcrypt API. A GCrypt::Handle<> object
can be consturcted from an existing handle or with the default null value.
It can be cleared upon request via clear(), and the managed handle can be
released via release().

The address of the managed handle can be retrieved through the address-of
operator. An implicit conversion operator is also added. This allows
frictionless use of GCrypt::Handle<> objects with existing libgcrypt APIs.

The negation operator is implemented to support testing the nullness of
the managed handle. The raw handle value is also retrieveable through
the handle() method.

The copy and move constructors and assignment operators are deleted.
They are not at the moment required anywhere in the work-in-progress
implementation of subtle crypto functionality.

As with other resource management classes, upon destruction, the
GCrypt::Handle<> object destroys the resource it manages. This is done
through objects of the HandleDeleter<> template class. Specializations
of this class have to implement the call operator that properly
releases the resource. Because the operator is deleted by default,
a compilation error will be thrown when deleting a resource of some
type for which the proper HandleDeleter specialization isn't provided.

std::unique_ptr<> could be used, but it could also be mis-used. I find
a mini-class with an interface that's specific to libgcrypt API
interactions to be preferrable to a std::unique_ptr<> with a custom
deleter.

* pal/crypto/gcrypt/Handle.h: Added.
(PAL::GCrypt::Handle::Handle):
(PAL::GCrypt::Handle::~Handle):
(PAL::GCrypt::Handle::clear):
(PAL::GCrypt::Handle::release):
(PAL::GCrypt::Handle::operator&):
(PAL::GCrypt::Handle::handle):
(PAL::GCrypt::Handle::operator T):
(PAL::GCrypt::Handle::operator!):
(PAL::GCrypt::HandleDeleter<gcry_mac_hd_t>::operator()):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (214549 => 214550)


--- trunk/Source/WebCore/ChangeLog	2017-03-29 18:38:10 UTC (rev 214549)
+++ trunk/Source/WebCore/ChangeLog	2017-03-29 18:44:57 UTC (rev 214550)
@@ -1,3 +1,17 @@
+2017-03-29  Zan Dobersek  <[email protected]>
+
+        [GCrypt] Add a Handle<> class to help with GCrypt object lifetime control
+        https://bugs.webkit.org/show_bug.cgi?id=170238
+
+        Reviewed by Michael Catanzaro.
+
+        The platform-specific CryptoAlgorithmHMAC implementation is modified
+        to showcase the GCrypt::Handle<> use. HandleDeleter<gcry_mac_hd_t>
+        is added accordingly.
+
+        * crypto/gcrypt/CryptoAlgorithmHMACGCrypt.cpp:
+        (WebCore::calculateSignature):
+
 2017-03-29  Myles C. Maxfield  <[email protected]>
 
         Variation fonts: Make sure that feature detection and preprocessor macros are right

Modified: trunk/Source/WebCore/PAL/ChangeLog (214549 => 214550)


--- trunk/Source/WebCore/PAL/ChangeLog	2017-03-29 18:38:10 UTC (rev 214549)
+++ trunk/Source/WebCore/PAL/ChangeLog	2017-03-29 18:44:57 UTC (rev 214550)
@@ -1,5 +1,58 @@
 2017-03-29  Zan Dobersek  <[email protected]>
 
+        [GCrypt] Add a Handle<> class to help with GCrypt object lifetime control
+        https://bugs.webkit.org/show_bug.cgi?id=170238
+
+        Reviewed by Michael Catanzaro.
+
+        Add a GCrypt-specific Handle<> template class, inside the GCrypt namespace.
+        Objects of this class should be used as 'smart handles', cleaning up upon
+        destruction the GCrypt object that's represented by the handle they manage.
+
+        This mimics the std::unique_ptr<> idea, but is narrowly focused towards
+        how such handles are used in the libgcrypt API. A GCrypt::Handle<> object
+        can be consturcted from an existing handle or with the default null value.
+        It can be cleared upon request via clear(), and the managed handle can be
+        released via release().
+
+        The address of the managed handle can be retrieved through the address-of
+        operator. An implicit conversion operator is also added. This allows
+        frictionless use of GCrypt::Handle<> objects with existing libgcrypt APIs.
+
+        The negation operator is implemented to support testing the nullness of
+        the managed handle. The raw handle value is also retrieveable through
+        the handle() method.
+
+        The copy and move constructors and assignment operators are deleted.
+        They are not at the moment required anywhere in the work-in-progress
+        implementation of subtle crypto functionality.
+
+        As with other resource management classes, upon destruction, the
+        GCrypt::Handle<> object destroys the resource it manages. This is done
+        through objects of the HandleDeleter<> template class. Specializations
+        of this class have to implement the call operator that properly
+        releases the resource. Because the operator is deleted by default,
+        a compilation error will be thrown when deleting a resource of some
+        type for which the proper HandleDeleter specialization isn't provided.
+
+        std::unique_ptr<> could be used, but it could also be mis-used. I find
+        a mini-class with an interface that's specific to libgcrypt API
+        interactions to be preferrable to a std::unique_ptr<> with a custom
+        deleter.
+
+        * pal/crypto/gcrypt/Handle.h: Added.
+        (PAL::GCrypt::Handle::Handle):
+        (PAL::GCrypt::Handle::~Handle):
+        (PAL::GCrypt::Handle::clear):
+        (PAL::GCrypt::Handle::release):
+        (PAL::GCrypt::Handle::operator&):
+        (PAL::GCrypt::Handle::handle):
+        (PAL::GCrypt::Handle::operator T):
+        (PAL::GCrypt::Handle::operator!):
+        (PAL::GCrypt::HandleDeleter<gcry_mac_hd_t>::operator()):
+
+2017-03-29  Zan Dobersek  <[email protected]>
+
         [GnuTLS] Remove unused CryptoDigestGnuTLS, CryptoAlgorithmHMACGnuTLS implementation files
         https://bugs.webkit.org/show_bug.cgi?id=170231
 

Added: trunk/Source/WebCore/PAL/pal/crypto/gcrypt/Handle.h (0 => 214550)


--- trunk/Source/WebCore/PAL/pal/crypto/gcrypt/Handle.h	                        (rev 0)
+++ trunk/Source/WebCore/PAL/pal/crypto/gcrypt/Handle.h	2017-03-29 18:44:57 UTC (rev 214550)
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2017 Metrological Group B.V.
+ * Copyright (C) 2017 Igalia S.L.
+ *
+ * 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.
+ */
+
+#pragma once
+
+#include <gcrypt.h>
+
+namespace PAL {
+namespace GCrypt {
+
+template<typename T>
+struct HandleDeleter {
+public:
+    void operator()(T handle) = delete;
+};
+
+template<typename T>
+class Handle {
+public:
+    Handle() = default;
+
+    explicit Handle(T handle)
+        : m_handle(handle)
+    { }
+
+    ~Handle()
+    {
+        clear();
+    }
+
+    Handle(const Handle&) = delete;
+    Handle& operator=(const Handle&) = delete;
+
+    Handle(Handle&&) = delete;
+    Handle& operator=(Handle&&) = delete;
+
+    void clear()
+    {
+        if (m_handle)
+            HandleDeleter<T>()(m_handle);
+        m_handle = nullptr;
+    }
+
+    T release()
+    {
+        T handle = m_handle;
+        m_handle = nullptr;
+        return handle;
+    }
+
+    T* operator&() { return &m_handle; }
+
+    T handle() const { return m_handle; }
+    operator T() const { return m_handle; }
+
+    bool operator!() const { return !m_handle; }
+
+private:
+    T m_handle { nullptr };
+};
+
+template<>
+struct HandleDeleter<gcry_mac_hd_t> {
+    void operator()(gcry_mac_hd_t handle)
+    {
+        gcry_mac_close(handle);
+    }
+};
+
+} // namespace GCrypt
+} // namespace PAL

Modified: trunk/Source/WebCore/crypto/gcrypt/CryptoAlgorithmHMACGCrypt.cpp (214549 => 214550)


--- trunk/Source/WebCore/crypto/gcrypt/CryptoAlgorithmHMACGCrypt.cpp	2017-03-29 18:38:10 UTC (rev 214549)
+++ trunk/Source/WebCore/crypto/gcrypt/CryptoAlgorithmHMACGCrypt.cpp	2017-03-29 18:44:57 UTC (rev 214550)
@@ -35,7 +35,7 @@
 #include "CryptoKeyHMAC.h"
 #include "ExceptionCode.h"
 #include "ScriptExecutionContext.h"
-#include <gcrypt.h>
+#include <pal/crypto/gcrypt/Handle.h>
 #include <wtf/CryptographicUtilities.h>
 
 namespace WebCore {
@@ -60,42 +60,28 @@
 
 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*>("");
 
-    bool result = false;
-    Vector<uint8_t> signature;
-
-    gcry_mac_hd_t hd;
-    gcry_error_t err;
-
-    err = gcry_mac_open(&hd, algorithm, 0, nullptr);
+    PAL::GCrypt::Handle<gcry_mac_hd_t> hd;
+    gcry_error_t err = gcry_mac_open(&hd, algorithm, 0, nullptr);
     if (err)
-        goto cleanup;
+        return std::nullopt;
 
     err = gcry_mac_setkey(hd, keyData, key.size());
     if (err)
-        goto cleanup;
+        return std::nullopt;
 
     err = gcry_mac_write(hd, data, dataLength);
     if (err)
-        goto cleanup;
+        return std::nullopt;
 
-    signature.resize(digestLength);
+    size_t digestLength = gcry_mac_get_algo_maclen(algorithm);
+    Vector<uint8_t> signature(digestLength);
     err = gcry_mac_read(hd, signature.data(), &digestLength);
     if (err)
-        goto cleanup;
+        return std::nullopt;
 
     signature.resize(digestLength);
-    result = true;
-
-cleanup:
-    if (hd)
-        gcry_mac_close(hd);
-
-    if (!result)
-        return std::nullopt;
-
     return WTFMove(signature);
 }
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to