<!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>[215085] 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/215085">215085</a></dd>
<dt>Author</dt> <dd>zandobersek@gmail.com</dd>
<dt>Date</dt> <dd>2017-04-07 00:35:21 -0700 (Fri, 07 Apr 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>[GCrypt] Implement CryptoKeyRSA::generatePair()
https://bugs.webkit.org/show_bug.cgi?id=170350

Reviewed by Michael Catanzaro.

Start implementing the libgcrypt-based platform bits of CryptoKeyRSA.

Implement generatePair() by constructing a genkey s-expression
that requests a generation of an RSA key that should use a modulus
of the specified bit-length and the specified exponent. The exponent
is extracted from an uint8_t array through a helper function. The
modulus length value is checked to be at least 16, since libgcrypt
does not support generating primes of less than that length in bits.

The returned s-expression upon request will contain the data for
both the public and the private key. gcry_sexp_t handles representing
those s-expressions are then passed to CryptoKeyRSA::create() before
invoking the success callback with a new CryptoKeyPair object in a
separate ScriptExecutionContext task.

The CryptoKeyRSA constructor simply has the notImplemented() call
removed. The destructor now invokes the HandleDeleter&lt;gcry_sexp_t&gt;
object instance to destroy the object represented by the
m_platformKey handle.

The methods in CryptoKeyRSA.cpp are also reordered to follow the
declaration order used in the header.

No new tests -- current ones cover this sufficiently, but are not yet
enabled due to other missing platform-specific SUBTLE_CRYPTO
implementations.

* crypto/gcrypt/CryptoKeyRSAGCrypt.cpp:
(WebCore::CryptoKeyRSA::CryptoKeyRSA):
(WebCore::CryptoKeyRSA::~CryptoKeyRSA):
(WebCore::exponentVectorToUInt32):
(WebCore::CryptoKeyRSA::generatePair):
(WebCore::CryptoKeyRSA::importSpki):
(WebCore::CryptoKeyRSA::importPkcs8):
(WebCore::CryptoKeyRSA::exportPkcs8):
(WebCore::CryptoKeyRSA::buildAlgorithm):
(WebCore::CryptoKeyRSA::exportData):
* crypto/keys/CryptoKeyRSA.h:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorecryptogcryptCryptoKeyRSAGCryptcpp">trunk/Source/WebCore/crypto/gcrypt/CryptoKeyRSAGCrypt.cpp</a></li>
<li><a href="#trunkSourceWebCorecryptokeysCryptoKeyRSAh">trunk/Source/WebCore/crypto/keys/CryptoKeyRSA.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (215084 => 215085)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2017-04-07 03:45:32 UTC (rev 215084)
+++ trunk/Source/WebCore/ChangeLog        2017-04-07 07:35:21 UTC (rev 215085)
</span><span class="lines">@@ -1,3 +1,49 @@
</span><ins>+2017-04-07  Zan Dobersek  &lt;zdobersek@igalia.com&gt;
+
+        [GCrypt] Implement CryptoKeyRSA::generatePair()
+        https://bugs.webkit.org/show_bug.cgi?id=170350
+
+        Reviewed by Michael Catanzaro.
+
+        Start implementing the libgcrypt-based platform bits of CryptoKeyRSA.
+
+        Implement generatePair() by constructing a genkey s-expression
+        that requests a generation of an RSA key that should use a modulus
+        of the specified bit-length and the specified exponent. The exponent
+        is extracted from an uint8_t array through a helper function. The
+        modulus length value is checked to be at least 16, since libgcrypt
+        does not support generating primes of less than that length in bits.
+
+        The returned s-expression upon request will contain the data for
+        both the public and the private key. gcry_sexp_t handles representing
+        those s-expressions are then passed to CryptoKeyRSA::create() before
+        invoking the success callback with a new CryptoKeyPair object in a
+        separate ScriptExecutionContext task.
+
+        The CryptoKeyRSA constructor simply has the notImplemented() call
+        removed. The destructor now invokes the HandleDeleter&lt;gcry_sexp_t&gt;
+        object instance to destroy the object represented by the
+        m_platformKey handle.
+
+        The methods in CryptoKeyRSA.cpp are also reordered to follow the
+        declaration order used in the header.
+
+        No new tests -- current ones cover this sufficiently, but are not yet
+        enabled due to other missing platform-specific SUBTLE_CRYPTO
+        implementations.
+
+        * crypto/gcrypt/CryptoKeyRSAGCrypt.cpp:
+        (WebCore::CryptoKeyRSA::CryptoKeyRSA):
+        (WebCore::CryptoKeyRSA::~CryptoKeyRSA):
+        (WebCore::exponentVectorToUInt32):
+        (WebCore::CryptoKeyRSA::generatePair):
+        (WebCore::CryptoKeyRSA::importSpki):
+        (WebCore::CryptoKeyRSA::importPkcs8):
+        (WebCore::CryptoKeyRSA::exportPkcs8):
+        (WebCore::CryptoKeyRSA::buildAlgorithm):
+        (WebCore::CryptoKeyRSA::exportData):
+        * crypto/keys/CryptoKeyRSA.h:
+
</ins><span class="cx"> 2017-04-06  Youenn Fablet  &lt;youenn@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [Debug] ASSERT(!throwScope.exception()) on imported/w3c/web-platform-tests/fetch/api/cors/cors-preflight-status-worker.html
</span></span></pre></div>
<a id="trunkSourceWebCorecryptogcryptCryptoKeyRSAGCryptcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/crypto/gcrypt/CryptoKeyRSAGCrypt.cpp (215084 => 215085)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/crypto/gcrypt/CryptoKeyRSAGCrypt.cpp        2017-04-07 03:45:32 UTC (rev 215084)
+++ trunk/Source/WebCore/crypto/gcrypt/CryptoKeyRSAGCrypt.cpp        2017-04-07 07:35:21 UTC (rev 215085)
</span><span class="lines">@@ -33,21 +33,12 @@
</span><span class="cx"> #include &quot;CryptoKeyPair.h&quot;
</span><span class="cx"> #include &quot;ExceptionCode.h&quot;
</span><span class="cx"> #include &quot;NotImplemented.h&quot;
</span><ins>+#include &quot;ScriptExecutionContext.h&quot;
+#include &lt;pal/crypto/gcrypt/Handle.h&gt;
+#include &lt;pal/crypto/gcrypt/Utilities.h&gt;
</ins><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><del>-struct _PlatformRSAKeyGnuTLS {
-};
-
-CryptoKeyRSA::CryptoKeyRSA(CryptoAlgorithmIdentifier identifier, CryptoAlgorithmIdentifier hash, bool hasHash, CryptoKeyType type, PlatformRSAKey platformKey, bool extractable, CryptoKeyUsageBitmap usage)
-    : CryptoKey(identifier, type, extractable, usage)
-    , m_platformKey(platformKey)
-    , m_restrictedToSpecificHash(hasHash)
-    , m_hash(hash)
-{
-    notImplemented();
-}
-
</del><span class="cx"> RefPtr&lt;CryptoKeyRSA&gt; CryptoKeyRSA::create(CryptoAlgorithmIdentifier identifier, CryptoAlgorithmIdentifier hash, bool hasHash, const CryptoKeyDataRSAComponents&amp; keyData, bool extractable, CryptoKeyUsageBitmap usage)
</span><span class="cx"> {
</span><span class="cx">     notImplemented();
</span><span class="lines">@@ -61,9 +52,18 @@
</span><span class="cx">     return nullptr;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+CryptoKeyRSA::CryptoKeyRSA(CryptoAlgorithmIdentifier identifier, CryptoAlgorithmIdentifier hash, bool hasHash, CryptoKeyType type, PlatformRSAKey platformKey, bool extractable, CryptoKeyUsageBitmap usage)
+    : CryptoKey(identifier, type, extractable, usage)
+    , m_platformKey(platformKey)
+    , m_restrictedToSpecificHash(hasHash)
+    , m_hash(hash)
+{
+}
+
</ins><span class="cx"> CryptoKeyRSA::~CryptoKeyRSA()
</span><span class="cx"> {
</span><del>-    notImplemented();
</del><ins>+    if (m_platformKey)
+        PAL::GCrypt::HandleDeleter&lt;gcry_sexp_t&gt;()(m_platformKey);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> bool CryptoKeyRSA::isRestrictedToHash(CryptoAlgorithmIdentifier&amp; identifier) const
</span><span class="lines">@@ -81,38 +81,80 @@
</span><span class="cx">     return 0;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-std::unique_ptr&lt;KeyAlgorithm&gt; CryptoKeyRSA::buildAlgorithm() const
</del><ins>+// Convert the exponent vector to a 32-bit value, if possible.
+static std::optional&lt;uint32_t&gt; exponentVectorToUInt32(const Vector&lt;uint8_t&gt;&amp; exponent)
</ins><span class="cx"> {
</span><del>-    notImplemented();
-    Vector&lt;uint8_t&gt; publicExponent;
-    return std::make_unique&lt;RsaKeyAlgorithm&gt;(emptyString(), 0, WTFMove(publicExponent));
</del><ins>+    if (exponent.size() &gt; 4) {
+        if (std::any_of(exponent.begin(), exponent.end() - 4, [](uint8_t element) { return !!element; }))
+            return std::nullopt;
+    }
+
+    uint32_t result = 0;
+    for (size_t size = exponent.size(), i = std::min&lt;size_t&gt;(4, size); i &gt; 0; --i) {
+        result &lt;&lt;= 8;
+        result += exponent[size - i];
+    }
+
+    return result;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-std::unique_ptr&lt;CryptoKeyData&gt; CryptoKeyRSA::exportData() const
</del><ins>+void CryptoKeyRSA::generatePair(CryptoAlgorithmIdentifier algorithm, CryptoAlgorithmIdentifier hash, bool hasHash, unsigned modulusLength, const Vector&lt;uint8_t&gt;&amp; publicExponent, bool extractable, CryptoKeyUsageBitmap usage, KeyPairCallback&amp;&amp; callback, VoidCallback&amp;&amp; failureCallback, ScriptExecutionContext* context)
</ins><span class="cx"> {
</span><del>-    ASSERT(extractable());
</del><ins>+    // libgcrypt doesn't report an error if the exponent is smaller than three or even.
+    auto e = exponentVectorToUInt32(publicExponent);
+    if (!e || *e &lt; 3 || !(*e &amp; 0x1)) {
+        failureCallback();
+        return;
+    }
</ins><span class="cx"> 
</span><del>-    notImplemented();
-    return nullptr;
</del><ins>+    // libgcrypt doesn't support generating primes of less than 16 bits.
+    if (modulusLength &lt; 16) {
+        failureCallback();
+        return;
+    }
+
+    PAL::GCrypt::Handle&lt;gcry_sexp_t&gt; genkeySexp;
+    gcry_error_t error = gcry_sexp_build(&amp;genkeySexp, nullptr, &quot;(genkey(rsa(nbits %d)(rsa-use-e %d)))&quot;, modulusLength, *e);
+    if (error != GPG_ERR_NO_ERROR) {
+        PAL::GCrypt::logError(error);
+        failureCallback();
+        return;
+    }
+
+    PAL::GCrypt::Handle&lt;gcry_sexp_t&gt; keyPairSexp;
+    error = gcry_pk_genkey(&amp;keyPairSexp, genkeySexp);
+    if (error != GPG_ERR_NO_ERROR) {
+        PAL::GCrypt::logError(error);
+        failureCallback();
+        return;
+    }
+
+    PAL::GCrypt::Handle&lt;gcry_sexp_t&gt; publicKeySexp(gcry_sexp_find_token(keyPairSexp, &quot;public-key&quot;, 0));
+    PAL::GCrypt::Handle&lt;gcry_sexp_t&gt; privateKeySexp(gcry_sexp_find_token(keyPairSexp, &quot;private-key&quot;, 0));
+    if (!publicKeySexp || !privateKeySexp) {
+        failureCallback();
+        return;
+    }
+
+    context-&gt;ref();
+    context-&gt;postTask(
+        [algorithm, hash, hasHash, extractable, usage, publicKeySexp = publicKeySexp.release(), privateKeySexp = privateKeySexp.release(), callback = WTFMove(callback)](ScriptExecutionContext&amp; context) mutable {
+            auto publicKey = CryptoKeyRSA::create(algorithm, hash, hasHash, CryptoKeyType::Public, publicKeySexp, true, usage);
+            auto privateKey = CryptoKeyRSA::create(algorithm, hash, hasHash, CryptoKeyType::Private, privateKeySexp, extractable, usage);
+
+            callback(CryptoKeyPair { WTFMove(publicKey), WTFMove(privateKey) });
+            context.deref();
+        });
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-void CryptoKeyRSA::generatePair(CryptoAlgorithmIdentifier algorithm, CryptoAlgorithmIdentifier hash, bool hasHash, unsigned modulusLength, const Vector&lt;uint8_t&gt;&amp; publicExponent, bool extractable, CryptoKeyUsageBitmap usage, KeyPairCallback&amp;&amp; callback, VoidCallback&amp;&amp; failureCallback, ScriptExecutionContext* context)
</del><ins>+RefPtr&lt;CryptoKeyRSA&gt; CryptoKeyRSA::importSpki(CryptoAlgorithmIdentifier, std::optional&lt;CryptoAlgorithmIdentifier&gt;, Vector&lt;uint8_t&gt;&amp;&amp;, bool, CryptoKeyUsageBitmap)
</ins><span class="cx"> {
</span><span class="cx">     notImplemented();
</span><del>-    failureCallback();
</del><span class="cx"> 
</span><del>-    UNUSED_PARAM(algorithm);
-    UNUSED_PARAM(hash);
-    UNUSED_PARAM(hasHash);
-    UNUSED_PARAM(modulusLength);
-    UNUSED_PARAM(publicExponent);
-    UNUSED_PARAM(extractable);
-    UNUSED_PARAM(usage);
-    UNUSED_PARAM(callback);
-    UNUSED_PARAM(context);
</del><ins>+    return nullptr;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-RefPtr&lt;CryptoKeyRSA&gt; CryptoKeyRSA::importSpki(CryptoAlgorithmIdentifier, std::optional&lt;CryptoAlgorithmIdentifier&gt;, Vector&lt;uint8_t&gt;&amp;&amp;, bool, CryptoKeyUsageBitmap)
</del><ins>+RefPtr&lt;CryptoKeyRSA&gt; CryptoKeyRSA::importPkcs8(CryptoAlgorithmIdentifier, std::optional&lt;CryptoAlgorithmIdentifier&gt;, Vector&lt;uint8_t&gt;&amp;&amp;, bool, CryptoKeyUsageBitmap)
</ins><span class="cx"> {
</span><span class="cx">     notImplemented();
</span><span class="cx"> 
</span><span class="lines">@@ -126,18 +168,26 @@
</span><span class="cx">     return Exception { NOT_SUPPORTED_ERR };
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-RefPtr&lt;CryptoKeyRSA&gt; CryptoKeyRSA::importPkcs8(CryptoAlgorithmIdentifier, std::optional&lt;CryptoAlgorithmIdentifier&gt;, Vector&lt;uint8_t&gt;&amp;&amp;, bool, CryptoKeyUsageBitmap)
</del><ins>+ExceptionOr&lt;Vector&lt;uint8_t&gt;&gt; CryptoKeyRSA::exportPkcs8() const
</ins><span class="cx"> {
</span><span class="cx">     notImplemented();
</span><span class="cx"> 
</span><del>-    return nullptr;
</del><ins>+    return Exception { NOT_SUPPORTED_ERR };
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-ExceptionOr&lt;Vector&lt;uint8_t&gt;&gt; CryptoKeyRSA::exportPkcs8() const
</del><ins>+std::unique_ptr&lt;KeyAlgorithm&gt; CryptoKeyRSA::buildAlgorithm() const
</ins><span class="cx"> {
</span><span class="cx">     notImplemented();
</span><ins>+    Vector&lt;uint8_t&gt; publicExponent;
+    return std::make_unique&lt;RsaKeyAlgorithm&gt;(emptyString(), 0, WTFMove(publicExponent));
+}
</ins><span class="cx"> 
</span><del>-    return Exception { NOT_SUPPORTED_ERR };
</del><ins>+std::unique_ptr&lt;CryptoKeyData&gt; CryptoKeyRSA::exportData() const
+{
+    ASSERT(extractable());
+
+    notImplemented();
+    return nullptr;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> } // namespace WebCore
</span></span></pre></div>
<a id="trunkSourceWebCorecryptokeysCryptoKeyRSAh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/crypto/keys/CryptoKeyRSA.h (215084 => 215085)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/crypto/keys/CryptoKeyRSA.h        2017-04-07 03:45:32 UTC (rev 215084)
+++ trunk/Source/WebCore/crypto/keys/CryptoKeyRSA.h        2017-04-07 07:35:21 UTC (rev 215085)
</span><span class="lines">@@ -37,8 +37,9 @@
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx"> #if PLATFORM(GTK)
</span><del>-typedef struct _PlatformRSAKeyGnuTLS PlatformRSAKeyGnuTLS;
-typedef PlatformRSAKeyGnuTLS *PlatformRSAKey;
</del><ins>+// gcry_sexp* equates gcry_sexp_t.
+struct gcry_sexp;
+typedef gcry_sexp* PlatformRSAKey;
</ins><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span></span></pre>
</div>
</div>

</body>
</html>