Changeset 214825 in webkit


Ignore:
Timestamp:
Apr 3, 2017 11:40:54 AM (7 years ago)
Author:
zandobersek@gmail.com
Message:

[GCrypt] Implement CryptoKeyEC::keySizeInBits(), ::platformGeneratePair()
https://bugs.webkit.org/show_bug.cgi?id=170345

Reviewed by Michael Catanzaro.

Source/WebCore:

Start implementing the libgcrypt-based platform bits of CryptoKeyEC.

Implement keySizeInBits() by returning the appropriate size for this
object's curve type. An assertion is added to ensure that this size
matches the one that's returned by gcry_pk_get_nbits() for this
object's EC key as represented by the m_platformKey gcry_sexp_t object.

Implement platformGeneratePair() by constructing a genkey s-expression
that requests a generation of an EC key for the specified curve type.
The s-expression is then passed to gcry_pk_genkey(), and the public
and private key data is then retrieved from the returned s-expression
upon success and used to create the public and private CryptoKeyEC
objects.

The PlatformECKey type alias is changed to match gcry_sexp_t. The
CryptoKeyEC destructor releases the gcry_sexp_t object through
a PAL::GCrypt::HandleDeleter<gcry_sexp_t> instance.

The method definitions in the CryptoKeyECGCrypt.cpp file are also
sorted to match the declaration order 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/CryptoKeyECGCrypt.cpp:

(WebCore::curveSize):
(WebCore::curveName):
(WebCore::CryptoKeyEC::~CryptoKeyEC):
(WebCore::CryptoKeyEC::keySizeInBits):
(WebCore::CryptoKeyEC::platformGeneratePair):
(WebCore::CryptoKeyEC::platformImportSpki):
(WebCore::CryptoKeyEC::platformImportPkcs8):
(WebCore::CryptoKeyEC::platformExportRaw):
(WebCore::CryptoKeyEC::platformAddFieldElements):
(WebCore::CryptoKeyEC::platformExportSpki):

  • crypto/keys/CryptoKeyEC.h:

Source/WebCore/PAL:

  • pal/crypto/gcrypt/Handle.h:

(PAL::GCrypt::HandleDeleter<gcry_sexp_t>::operator()): Add a HandleDeleter
specialization for the gcry_sexp_t type.

Location:
trunk/Source/WebCore
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r214823 r214825  
     12017-04-03  Zan Dobersek  <zdobersek@igalia.com>
     2
     3        [GCrypt] Implement CryptoKeyEC::keySizeInBits(), ::platformGeneratePair()
     4        https://bugs.webkit.org/show_bug.cgi?id=170345
     5
     6        Reviewed by Michael Catanzaro.
     7
     8        Start implementing the libgcrypt-based platform bits of CryptoKeyEC.
     9
     10        Implement keySizeInBits() by returning the appropriate size for this
     11        object's curve type. An assertion is added to ensure that this size
     12        matches the one that's returned by gcry_pk_get_nbits() for this
     13        object's EC key as represented by the m_platformKey gcry_sexp_t object.
     14
     15        Implement platformGeneratePair() by constructing a genkey s-expression
     16        that requests a generation of an EC key for the specified curve type.
     17        The s-expression is then passed to gcry_pk_genkey(), and the public
     18        and private key data is then retrieved from the returned s-expression
     19        upon success and used to create the public and private CryptoKeyEC
     20        objects.
     21
     22        The PlatformECKey type alias is changed to match gcry_sexp_t. The
     23        CryptoKeyEC destructor releases the gcry_sexp_t object through
     24        a PAL::GCrypt::HandleDeleter<gcry_sexp_t> instance.
     25
     26        The method definitions in the CryptoKeyECGCrypt.cpp file are also
     27        sorted to match the declaration order in the header.
     28
     29        No new tests -- current ones cover this sufficiently, but are not yet
     30        enabled due to other missing platform-specific SUBTLE_CRYPTO
     31        implementations.
     32
     33        * crypto/gcrypt/CryptoKeyECGCrypt.cpp:
     34        (WebCore::curveSize):
     35        (WebCore::curveName):
     36        (WebCore::CryptoKeyEC::~CryptoKeyEC):
     37        (WebCore::CryptoKeyEC::keySizeInBits):
     38        (WebCore::CryptoKeyEC::platformGeneratePair):
     39        (WebCore::CryptoKeyEC::platformImportSpki):
     40        (WebCore::CryptoKeyEC::platformImportPkcs8):
     41        (WebCore::CryptoKeyEC::platformExportRaw):
     42        (WebCore::CryptoKeyEC::platformAddFieldElements):
     43        (WebCore::CryptoKeyEC::platformExportSpki):
     44        * crypto/keys/CryptoKeyEC.h:
     45
    1462017-04-03  Zan Dobersek  <zdobersek@igalia.com>
    247
  • trunk/Source/WebCore/PAL/ChangeLog

    r214822 r214825  
     12017-04-03  Zan Dobersek  <zdobersek@igalia.com>
     2
     3        [GCrypt] Implement CryptoKeyEC::keySizeInBits(), ::platformGeneratePair()
     4        https://bugs.webkit.org/show_bug.cgi?id=170345
     5
     6        Reviewed by Michael Catanzaro.
     7
     8        * pal/crypto/gcrypt/Handle.h:
     9        (PAL::GCrypt::HandleDeleter<gcry_sexp_t>::operator()): Add a HandleDeleter
     10        specialization for the gcry_sexp_t type.
     11
    1122017-04-03  Zan Dobersek  <zdobersek@igalia.com>
    213
  • trunk/Source/WebCore/PAL/pal/crypto/gcrypt/Handle.h

    r214822 r214825  
    9999};
    100100
     101template<>
     102struct HandleDeleter<gcry_sexp_t> {
     103    void operator()(gcry_sexp_t handle)
     104    {
     105        gcry_sexp_release(handle);
     106    }
     107};
     108
    101109} // namespace GCrypt
    102110} // namespace PAL
  • trunk/Source/WebCore/crypto/gcrypt/CryptoKeyECGCrypt.cpp

    r214538 r214825  
    3131#include "CryptoKeyPair.h"
    3232#include "NotImplemented.h"
     33#include <pal/crypto/gcrypt/Handle.h>
     34#include <pal/crypto/gcrypt/Utilities.h>
    3335
    3436namespace WebCore {
    3537
    36 struct _PlatformECKeyGnuTLS {
    37 };
     38static size_t curveSize(CryptoKeyEC::NamedCurve curve)
     39{
     40    switch (curve) {
     41    case CryptoKeyEC::NamedCurve::P256:
     42        return 256;
     43    case CryptoKeyEC::NamedCurve::P384:
     44        return 384;
     45    }
     46}
     47
     48static const char* curveName(CryptoKeyEC::NamedCurve curve)
     49{
     50    switch (curve) {
     51    case CryptoKeyEC::NamedCurve::P256:
     52        return "NIST P-256";
     53    case CryptoKeyEC::NamedCurve::P384:
     54        return "NIST P-384";
     55    }
     56}
    3857
    3958CryptoKeyEC::~CryptoKeyEC()
    4059{
    41     notImplemented();
     60    if (m_platformKey)
     61        PAL::GCrypt::HandleDeleter<gcry_sexp_t>()(m_platformKey);
    4262}
    4363
    4464size_t CryptoKeyEC::keySizeInBits() const
    4565{
    46     notImplemented();
    47 
    48     return 0;
     66    size_t size = curveSize(m_curve);
     67    ASSERT(size == gcry_pk_get_nbits(m_platformKey));
     68    return size;
    4969}
    5070
    51 Vector<uint8_t> CryptoKeyEC::platformExportRaw() const
     71std::optional<CryptoKeyPair> CryptoKeyEC::platformGeneratePair(CryptoAlgorithmIdentifier identifier, NamedCurve curve, bool extractable, CryptoKeyUsageBitmap usages)
    5272{
    53     notImplemented();
     73    PAL::GCrypt::Handle<gcry_sexp_t> genkeySexp;
     74    gcry_error_t error = gcry_sexp_build(&genkeySexp, nullptr, "(genkey(ecc(curve %s)))", curveName(curve));
     75    if (error != GPG_ERR_NO_ERROR) {
     76        PAL::GCrypt::logError(error);
     77        return std::nullopt;
     78    }
    5479
    55     return { };
    56 }
     80    PAL::GCrypt::Handle<gcry_sexp_t> keyPairSexp;
     81    error = gcry_pk_genkey(&keyPairSexp, genkeySexp);
     82    if (error != GPG_ERR_NO_ERROR) {
     83        PAL::GCrypt::logError(error);
     84        return std::nullopt;
     85    }
    5786
    58 std::optional<CryptoKeyPair> CryptoKeyEC::platformGeneratePair(CryptoAlgorithmIdentifier, NamedCurve, bool, CryptoKeyUsageBitmap)
    59 {
    60     notImplemented();
     87    PAL::GCrypt::Handle<gcry_sexp_t> publicKeySexp(gcry_sexp_find_token(keyPairSexp, "public-key", 0));
     88    PAL::GCrypt::Handle<gcry_sexp_t> privateKeySexp(gcry_sexp_find_token(keyPairSexp, "private-key", 0));
     89    if (!publicKeySexp || !privateKeySexp)
     90        return std::nullopt;
    6191
    62     return std::nullopt;
     92    auto publicKey = CryptoKeyEC::create(identifier, curve, CryptoKeyType::Public, publicKeySexp.release(), true, usages);
     93    auto privateKey = CryptoKeyEC::create(identifier, curve, CryptoKeyType::Private, privateKeySexp.release(), extractable, usages);
     94    return CryptoKeyPair { WTFMove(publicKey), WTFMove(privateKey) };
    6395}
    6496
     
    84116}
    85117
    86 void CryptoKeyEC::platformAddFieldElements(JsonWebKey&) const
    87 {
    88     notImplemented();
    89 }
    90 
    91118RefPtr<CryptoKeyEC> CryptoKeyEC::platformImportSpki(CryptoAlgorithmIdentifier, NamedCurve, Vector<uint8_t>&&, bool, CryptoKeyUsageBitmap)
    92119{
     
    96123}
    97124
    98 Vector<uint8_t> CryptoKeyEC::platformExportSpki() const
     125RefPtr<CryptoKeyEC> CryptoKeyEC::platformImportPkcs8(CryptoAlgorithmIdentifier, NamedCurve, Vector<uint8_t>&&, bool, CryptoKeyUsageBitmap)
     126{
     127    notImplemented();
     128
     129    return nullptr;
     130}
     131
     132Vector<uint8_t> CryptoKeyEC::platformExportRaw() const
    99133{
    100134    notImplemented();
     
    103137}
    104138
    105 RefPtr<CryptoKeyEC> CryptoKeyEC::platformImportPkcs8(CryptoAlgorithmIdentifier, NamedCurve, Vector<uint8_t>&&, bool, CryptoKeyUsageBitmap)
     139void CryptoKeyEC::platformAddFieldElements(JsonWebKey&) const
     140{
     141    notImplemented();
     142}
     143
     144Vector<uint8_t> CryptoKeyEC::platformExportSpki() const
    106145{
    107146    notImplemented();
    108147
    109     return nullptr;
     148    return { };
    110149}
    111150
  • trunk/Source/WebCore/crypto/keys/CryptoKeyEC.h

    r214122 r214825  
    3838
    3939#if PLATFORM(GTK)
    40 typedef struct _PlatformECKeyGnuTLS PlatformECKeyGnuTLS;
    41 typedef PlatformECKeyGnuTLS *PlatformECKey;
     40// gcry_sexp* equates gcry_sexp_t.
     41struct gcry_sexp;
     42typedef gcry_sexp* PlatformECKey;
    4243#endif
    4344
Note: See TracChangeset for help on using the changeset viewer.