<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[214550] trunk/Source/WebCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/214550">214550</a></dd>
<dt>Author</dt> <dd>zandobersek@gmail.com</dd>
<dt>Date</dt> <dd>2017-03-29 11:44:57 -0700 (Wed, 29 Mar 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>[GCrypt] Add a Handle&lt;&gt; 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&lt;&gt; use. HandleDeleter&lt;gcry_mac_hd_t&gt;
is added accordingly.

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

Source/WebCore/PAL:

Add a GCrypt-specific Handle&lt;&gt; 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&lt;&gt; idea, but is narrowly focused towards
how such handles are used in the libgcrypt API. A GCrypt::Handle&lt;&gt; 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&lt;&gt; 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&lt;&gt; object destroys the resource it manages. This is done
through objects of the HandleDeleter&lt;&gt; 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&lt;&gt; 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&lt;&gt; 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&amp;):
(PAL::GCrypt::Handle::handle):
(PAL::GCrypt::Handle::operator T):
(PAL::GCrypt::Handle::operator!):
(PAL::GCrypt::HandleDeleter&lt;gcry_mac_hd_t&gt;::operator()):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorePALChangeLog">trunk/Source/WebCore/PAL/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorecryptogcryptCryptoAlgorithmHMACGCryptcpp">trunk/Source/WebCore/crypto/gcrypt/CryptoAlgorithmHMACGCrypt.cpp</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceWebCorePALpalcryptogcryptHandleh">trunk/Source/WebCore/PAL/pal/crypto/gcrypt/Handle.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (214549 => 214550)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -1,3 +1,17 @@
</span><ins>+2017-03-29  Zan Dobersek  &lt;zdobersek@igalia.com&gt;
+
+        [GCrypt] Add a Handle&lt;&gt; 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&lt;&gt; use. HandleDeleter&lt;gcry_mac_hd_t&gt;
+        is added accordingly.
+
+        * crypto/gcrypt/CryptoAlgorithmHMACGCrypt.cpp:
+        (WebCore::calculateSignature):
+
</ins><span class="cx"> 2017-03-29  Myles C. Maxfield  &lt;mmaxfield@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Variation fonts: Make sure that feature detection and preprocessor macros are right
</span></span></pre></div>
<a id="trunkSourceWebCorePALChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/PAL/ChangeLog (214549 => 214550)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -1,5 +1,58 @@
</span><span class="cx"> 2017-03-29  Zan Dobersek  &lt;zdobersek@igalia.com&gt;
</span><span class="cx"> 
</span><ins>+        [GCrypt] Add a Handle&lt;&gt; 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&lt;&gt; 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&lt;&gt; idea, but is narrowly focused towards
+        how such handles are used in the libgcrypt API. A GCrypt::Handle&lt;&gt; 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&lt;&gt; 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&lt;&gt; object destroys the resource it manages. This is done
+        through objects of the HandleDeleter&lt;&gt; 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&lt;&gt; 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&lt;&gt; 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&amp;):
+        (PAL::GCrypt::Handle::handle):
+        (PAL::GCrypt::Handle::operator T):
+        (PAL::GCrypt::Handle::operator!):
+        (PAL::GCrypt::HandleDeleter&lt;gcry_mac_hd_t&gt;::operator()):
+
+2017-03-29  Zan Dobersek  &lt;zdobersek@igalia.com&gt;
+
</ins><span class="cx">         [GnuTLS] Remove unused CryptoDigestGnuTLS, CryptoAlgorithmHMACGnuTLS implementation files
</span><span class="cx">         https://bugs.webkit.org/show_bug.cgi?id=170231
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCorePALpalcryptogcryptHandleh"></a>
<div class="addfile"><h4>Added: trunk/Source/WebCore/PAL/pal/crypto/gcrypt/Handle.h (0 => 214550)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -0,0 +1,94 @@
</span><ins>+/*
+ * 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 &lt;gcrypt.h&gt;
+
+namespace PAL {
+namespace GCrypt {
+
+template&lt;typename T&gt;
+struct HandleDeleter {
+public:
+    void operator()(T handle) = delete;
+};
+
+template&lt;typename T&gt;
+class Handle {
+public:
+    Handle() = default;
+
+    explicit Handle(T handle)
+        : m_handle(handle)
+    { }
+
+    ~Handle()
+    {
+        clear();
+    }
+
+    Handle(const Handle&amp;) = delete;
+    Handle&amp; operator=(const Handle&amp;) = delete;
+
+    Handle(Handle&amp;&amp;) = delete;
+    Handle&amp; operator=(Handle&amp;&amp;) = delete;
+
+    void clear()
+    {
+        if (m_handle)
+            HandleDeleter&lt;T&gt;()(m_handle);
+        m_handle = nullptr;
+    }
+
+    T release()
+    {
+        T handle = m_handle;
+        m_handle = nullptr;
+        return handle;
+    }
+
+    T* operator&amp;() { return &amp;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&lt;&gt;
+struct HandleDeleter&lt;gcry_mac_hd_t&gt; {
+    void operator()(gcry_mac_hd_t handle)
+    {
+        gcry_mac_close(handle);
+    }
+};
+
+} // namespace GCrypt
+} // namespace PAL
</ins></span></pre></div>
<a id="trunkSourceWebCorecryptogcryptCryptoAlgorithmHMACGCryptcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/crypto/gcrypt/CryptoAlgorithmHMACGCrypt.cpp (214549 => 214550)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -35,7 +35,7 @@
</span><span class="cx"> #include &quot;CryptoKeyHMAC.h&quot;
</span><span class="cx"> #include &quot;ExceptionCode.h&quot;
</span><span class="cx"> #include &quot;ScriptExecutionContext.h&quot;
</span><del>-#include &lt;gcrypt.h&gt;
</del><ins>+#include &lt;pal/crypto/gcrypt/Handle.h&gt;
</ins><span class="cx"> #include &lt;wtf/CryptographicUtilities.h&gt;
</span><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="lines">@@ -60,42 +60,28 @@
</span><span class="cx"> 
</span><span class="cx"> static std::optional&lt;Vector&lt;uint8_t&gt;&gt; calculateSignature(int algorithm, const Vector&lt;uint8_t&gt;&amp; key, const uint8_t* data, size_t dataLength)
</span><span class="cx"> {
</span><del>-    size_t digestLength = gcry_mac_get_algo_maclen(algorithm);
</del><span class="cx">     const void* keyData = key.data() ? key.data() : reinterpret_cast&lt;const uint8_t*&gt;(&quot;&quot;);
</span><span class="cx"> 
</span><del>-    bool result = false;
-    Vector&lt;uint8_t&gt; signature;
-
-    gcry_mac_hd_t hd;
-    gcry_error_t err;
-
-    err = gcry_mac_open(&amp;hd, algorithm, 0, nullptr);
</del><ins>+    PAL::GCrypt::Handle&lt;gcry_mac_hd_t&gt; hd;
+    gcry_error_t err = gcry_mac_open(&amp;hd, algorithm, 0, nullptr);
</ins><span class="cx">     if (err)
</span><del>-        goto cleanup;
</del><ins>+        return std::nullopt;
</ins><span class="cx"> 
</span><span class="cx">     err = gcry_mac_setkey(hd, keyData, key.size());
</span><span class="cx">     if (err)
</span><del>-        goto cleanup;
</del><ins>+        return std::nullopt;
</ins><span class="cx"> 
</span><span class="cx">     err = gcry_mac_write(hd, data, dataLength);
</span><span class="cx">     if (err)
</span><del>-        goto cleanup;
</del><ins>+        return std::nullopt;
</ins><span class="cx"> 
</span><del>-    signature.resize(digestLength);
</del><ins>+    size_t digestLength = gcry_mac_get_algo_maclen(algorithm);
+    Vector&lt;uint8_t&gt; signature(digestLength);
</ins><span class="cx">     err = gcry_mac_read(hd, signature.data(), &amp;digestLength);
</span><span class="cx">     if (err)
</span><del>-        goto cleanup;
</del><ins>+        return std::nullopt;
</ins><span class="cx"> 
</span><span class="cx">     signature.resize(digestLength);
</span><del>-    result = true;
-
-cleanup:
-    if (hd)
-        gcry_mac_close(hd);
-
-    if (!result)
-        return std::nullopt;
-
</del><span class="cx">     return WTFMove(signature);
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>