Changeset 159578 in webkit


Ignore:
Timestamp:
Nov 20, 2013, 1:18:26 PM (11 years ago)
Author:
ap@apple.com
Message:

Use std::function callbacks in CryptoAlgorithm instead of JS promises
https://bugs.webkit.org/show_bug.cgi?id=124673

Reviewed by Anders Carlsson.

To implement key wrapping/unwrapping, we'll need to chain existing operations.
It's much easier to do with C++ callbacks than with functions fulfilling JS
promises directly.

Also, this will decouple CryptoAlgorithm from JS, which is nice.

SubtleCrypto IDL says that all functions return Promise<any>, but in reality,
there is very little polymorphism, the only function whose return type depends
on algorithm is generateKey (it can create a Key or a KeyPair).

  • bindings/js/JSDOMPromise.cpp:

(WebCore::PromiseWrapper::PromiseWrapper):
(WebCore::PromiseWrapper::operator=):

  • bindings/js/JSDOMPromise.h:

Made it copyable, as each crypto function wraps the promise in success and failure
functional objects now.

  • bindings/js/JSSubtleCryptoCustom.cpp:

(WebCore::JSSubtleCrypto::encrypt):
(WebCore::JSSubtleCrypto::decrypt):
(WebCore::JSSubtleCrypto::sign):
(WebCore::JSSubtleCrypto::verify):
(WebCore::JSSubtleCrypto::digest):
(WebCore::JSSubtleCrypto::generateKey):
(WebCore::JSSubtleCrypto::importKey):
(WebCore::JSSubtleCrypto::exportKey):

  • crypto/CryptoAlgorithm.cpp:

(WebCore::CryptoAlgorithm::encrypt):
(WebCore::CryptoAlgorithm::decrypt):
(WebCore::CryptoAlgorithm::sign):
(WebCore::CryptoAlgorithm::verify):
(WebCore::CryptoAlgorithm::digest):
(WebCore::CryptoAlgorithm::generateKey):
(WebCore::CryptoAlgorithm::deriveKey):
(WebCore::CryptoAlgorithm::deriveBits):
(WebCore::CryptoAlgorithm::importKey):

  • crypto/CryptoAlgorithm.h:
  • crypto/CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp:

(WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::sign):
(WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::verify):

  • crypto/algorithms/CryptoAlgorithmAES_CBC.cpp:

(WebCore::CryptoAlgorithmAES_CBC::generateKey):
(WebCore::CryptoAlgorithmAES_CBC::importKey):

  • crypto/algorithms/CryptoAlgorithmAES_CBC.h:
  • crypto/algorithms/CryptoAlgorithmHMAC.cpp:

(WebCore::CryptoAlgorithmHMAC::generateKey):
(WebCore::CryptoAlgorithmHMAC::importKey):

  • crypto/algorithms/CryptoAlgorithmHMAC.h:
  • crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.cpp:

(WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::generateKey):
(WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::importKey):

  • crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.h:
  • crypto/algorithms/CryptoAlgorithmSHA1.cpp:

(WebCore::CryptoAlgorithmSHA1::digest):

  • crypto/algorithms/CryptoAlgorithmSHA1.h:
  • crypto/algorithms/CryptoAlgorithmSHA224.cpp:

(WebCore::CryptoAlgorithmSHA224::digest):

  • crypto/algorithms/CryptoAlgorithmSHA224.h:
  • crypto/algorithms/CryptoAlgorithmSHA256.cpp:

(WebCore::CryptoAlgorithmSHA256::digest):

  • crypto/algorithms/CryptoAlgorithmSHA256.h:
  • crypto/algorithms/CryptoAlgorithmSHA384.cpp:

(WebCore::CryptoAlgorithmSHA384::digest):

  • crypto/algorithms/CryptoAlgorithmSHA384.h:
  • crypto/algorithms/CryptoAlgorithmSHA512.cpp:

(WebCore::CryptoAlgorithmSHA512::digest):

  • crypto/algorithms/CryptoAlgorithmSHA512.h:
  • crypto/keys/CryptoKeyRSA.h:
  • crypto/mac/CryptoAlgorithmAES_CBCMac.cpp:

(WebCore::transformAES_CBC):
(WebCore::CryptoAlgorithmAES_CBC::encrypt):
(WebCore::CryptoAlgorithmAES_CBC::decrypt):

  • crypto/mac/CryptoAlgorithmHMACMac.cpp:

(WebCore::CryptoAlgorithmHMAC::sign):
(WebCore::CryptoAlgorithmHMAC::verify):

  • crypto/mac/CryptoKeyRSAMac.cpp:

(WebCore::CryptoKeyRSA::generatePair):

Location:
trunk/Source/WebCore
Files:
27 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r159575 r159578  
     12013-11-20  Alexey Proskuryakov  <ap@apple.com>
     2
     3        Use std::function callbacks in CryptoAlgorithm instead of JS promises
     4        https://bugs.webkit.org/show_bug.cgi?id=124673
     5
     6        Reviewed by Anders Carlsson.
     7
     8        To implement key wrapping/unwrapping, we'll need to chain existing operations.
     9        It's much easier to do with C++ callbacks than with functions fulfilling JS
     10        promises directly.
     11
     12        Also, this will decouple CryptoAlgorithm from JS, which is nice.
     13
     14        SubtleCrypto IDL says that all functions return Promise<any>, but in reality,
     15        there is very little polymorphism, the only function whose return type depends
     16        on algorithm is generateKey (it can create a Key or a KeyPair).
     17
     18        * bindings/js/JSDOMPromise.cpp:
     19        (WebCore::PromiseWrapper::PromiseWrapper):
     20        (WebCore::PromiseWrapper::operator=):
     21        * bindings/js/JSDOMPromise.h:
     22        Made it copyable, as each crypto function wraps the promise in success and failure
     23        functional objects now.
     24
     25        * bindings/js/JSSubtleCryptoCustom.cpp:
     26        (WebCore::JSSubtleCrypto::encrypt):
     27        (WebCore::JSSubtleCrypto::decrypt):
     28        (WebCore::JSSubtleCrypto::sign):
     29        (WebCore::JSSubtleCrypto::verify):
     30        (WebCore::JSSubtleCrypto::digest):
     31        (WebCore::JSSubtleCrypto::generateKey):
     32        (WebCore::JSSubtleCrypto::importKey):
     33        (WebCore::JSSubtleCrypto::exportKey):
     34        * crypto/CryptoAlgorithm.cpp:
     35        (WebCore::CryptoAlgorithm::encrypt):
     36        (WebCore::CryptoAlgorithm::decrypt):
     37        (WebCore::CryptoAlgorithm::sign):
     38        (WebCore::CryptoAlgorithm::verify):
     39        (WebCore::CryptoAlgorithm::digest):
     40        (WebCore::CryptoAlgorithm::generateKey):
     41        (WebCore::CryptoAlgorithm::deriveKey):
     42        (WebCore::CryptoAlgorithm::deriveBits):
     43        (WebCore::CryptoAlgorithm::importKey):
     44        * crypto/CryptoAlgorithm.h:
     45        * crypto/CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp:
     46        (WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::sign):
     47        (WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::verify):
     48        * crypto/algorithms/CryptoAlgorithmAES_CBC.cpp:
     49        (WebCore::CryptoAlgorithmAES_CBC::generateKey):
     50        (WebCore::CryptoAlgorithmAES_CBC::importKey):
     51        * crypto/algorithms/CryptoAlgorithmAES_CBC.h:
     52        * crypto/algorithms/CryptoAlgorithmHMAC.cpp:
     53        (WebCore::CryptoAlgorithmHMAC::generateKey):
     54        (WebCore::CryptoAlgorithmHMAC::importKey):
     55        * crypto/algorithms/CryptoAlgorithmHMAC.h:
     56        * crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.cpp:
     57        (WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::generateKey):
     58        (WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::importKey):
     59        * crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.h:
     60        * crypto/algorithms/CryptoAlgorithmSHA1.cpp:
     61        (WebCore::CryptoAlgorithmSHA1::digest):
     62        * crypto/algorithms/CryptoAlgorithmSHA1.h:
     63        * crypto/algorithms/CryptoAlgorithmSHA224.cpp:
     64        (WebCore::CryptoAlgorithmSHA224::digest):
     65        * crypto/algorithms/CryptoAlgorithmSHA224.h:
     66        * crypto/algorithms/CryptoAlgorithmSHA256.cpp:
     67        (WebCore::CryptoAlgorithmSHA256::digest):
     68        * crypto/algorithms/CryptoAlgorithmSHA256.h:
     69        * crypto/algorithms/CryptoAlgorithmSHA384.cpp:
     70        (WebCore::CryptoAlgorithmSHA384::digest):
     71        * crypto/algorithms/CryptoAlgorithmSHA384.h:
     72        * crypto/algorithms/CryptoAlgorithmSHA512.cpp:
     73        (WebCore::CryptoAlgorithmSHA512::digest):
     74        * crypto/algorithms/CryptoAlgorithmSHA512.h:
     75        * crypto/keys/CryptoKeyRSA.h:
     76        * crypto/mac/CryptoAlgorithmAES_CBCMac.cpp:
     77        (WebCore::transformAES_CBC):
     78        (WebCore::CryptoAlgorithmAES_CBC::encrypt):
     79        (WebCore::CryptoAlgorithmAES_CBC::decrypt):
     80        * crypto/mac/CryptoAlgorithmHMACMac.cpp:
     81        (WebCore::CryptoAlgorithmHMAC::sign):
     82        (WebCore::CryptoAlgorithmHMAC::verify):
     83        * crypto/mac/CryptoKeyRSAMac.cpp:
     84        (WebCore::CryptoKeyRSA::generatePair):
     85
    1862013-11-20  Robert Hogan  <robert@webkit.org>
    287
  • trunk/Source/WebCore/bindings/js/JSDOMPromise.cpp

    r158317 r159578  
    3535}
    3636
     37PromiseWrapper::PromiseWrapper(const PromiseWrapper& other)
     38    : m_globalObject(other.m_globalObject)
     39    , m_promise(other.m_promise)
     40{
    3741}
     42
     43PromiseWrapper& PromiseWrapper::operator=(const PromiseWrapper& other)
     44{
     45    m_globalObject = other.m_globalObject;
     46    m_promise = other.m_promise;
     47    return *this;
     48}
     49
     50}
  • trunk/Source/WebCore/bindings/js/JSDOMPromise.h

    r159068 r159578  
    3434#include <runtime/JSPromiseResolver.h>
    3535#include <heap/StrongInlines.h>
    36 #include <wtf/Noncopyable.h>
    3736
    3837namespace WebCore {
    3938
    40 // FIXME: Using this class in DOM code makes it dependent on JS bindings.
    4139class PromiseWrapper {
    42     WTF_MAKE_NONCOPYABLE(PromiseWrapper)
    4340public:
    44     static std::unique_ptr<PromiseWrapper> create(JSDOMGlobalObject* globalObject, JSC::JSPromise* promise)
    45     {
    46         return std::unique_ptr<PromiseWrapper>(new PromiseWrapper(globalObject, promise));
    47     }
     41    PromiseWrapper(JSDOMGlobalObject*, JSC::JSPromise*);
     42
     43    PromiseWrapper(const PromiseWrapper&);
     44    PromiseWrapper& operator=(const PromiseWrapper&);
    4845
    4946    template<class FulfillResultType>
     
    5451
    5552private:
    56     PromiseWrapper(JSDOMGlobalObject*, JSC::JSPromise*);
    57 
    5853    JSC::Strong<JSDOMGlobalObject> m_globalObject;
    5954    JSC::Strong<JSC::JSPromise> m_promise;
  • trunk/Source/WebCore/bindings/js/JSSubtleCryptoCustom.cpp

    r159392 r159578  
    163163
    164164    JSPromise* promise = JSPromise::createWithResolver(exec->vm(), globalObject());
    165     auto promiseWrapper = PromiseWrapper::create(globalObject(), promise);
     165    PromiseWrapper promiseWrapper(globalObject(), promise);
     166    auto successCallback = [promiseWrapper](const Vector<uint8_t>& result) mutable {
     167        promiseWrapper.fulfill(result);
     168    };
     169    auto failureCallback = [promiseWrapper]() mutable {
     170        promiseWrapper.reject(nullptr);
     171    };
    166172
    167173    ExceptionCode ec = 0;
    168     algorithm->encrypt(*parameters, *key, data, std::move(promiseWrapper), ec);
     174    algorithm->encrypt(*parameters, *key, data, std::move(successCallback), std::move(failureCallback), ec);
    169175    if (ec) {
    170176        setDOMException(exec, ec);
     
    209215
    210216    JSPromise* promise = JSPromise::createWithResolver(exec->vm(), globalObject());
    211     auto promiseWrapper = PromiseWrapper::create(globalObject(), promise);
     217    PromiseWrapper promiseWrapper(globalObject(), promise);
     218    auto successCallback = [promiseWrapper](const Vector<uint8_t>& result) mutable {
     219        promiseWrapper.fulfill(result);
     220    };
     221    auto failureCallback = [promiseWrapper]() mutable {
     222        promiseWrapper.reject(nullptr);
     223    };
    212224
    213225    ExceptionCode ec = 0;
    214     algorithm->decrypt(*parameters, *key, data, std::move(promiseWrapper), ec);
     226    algorithm->decrypt(*parameters, *key, data, std::move(successCallback), std::move(failureCallback), ec);
    215227    if (ec) {
    216228        setDOMException(exec, ec);
     
    255267
    256268    JSPromise* promise = JSPromise::createWithResolver(exec->vm(), globalObject());
    257     auto promiseWrapper = PromiseWrapper::create(globalObject(), promise);
     269    PromiseWrapper promiseWrapper(globalObject(), promise);
     270    auto successCallback = [promiseWrapper](const Vector<uint8_t>& result) mutable {
     271        promiseWrapper.fulfill(result);
     272    };
     273    auto failureCallback = [promiseWrapper]() mutable {
     274        promiseWrapper.reject(nullptr);
     275    };
    258276
    259277    ExceptionCode ec = 0;
    260     algorithm->sign(*parameters, *key, data, std::move(promiseWrapper), ec);
     278    algorithm->sign(*parameters, *key, data, std::move(successCallback), std::move(failureCallback), ec);
    261279    if (ec) {
    262280        setDOMException(exec, ec);
     
    307325
    308326    JSPromise* promise = JSPromise::createWithResolver(exec->vm(), globalObject());
    309     auto promiseWrapper = PromiseWrapper::create(globalObject(), promise);
     327    PromiseWrapper promiseWrapper(globalObject(), promise);
     328    auto successCallback = [promiseWrapper](bool result) mutable {
     329        promiseWrapper.fulfill(result);
     330    };
     331    auto failureCallback = [promiseWrapper]() mutable {
     332        promiseWrapper.reject(nullptr);
     333    };
    310334
    311335    ExceptionCode ec = 0;
    312     algorithm->verify(*parameters, *key, signature, data, std::move(promiseWrapper), ec);
     336    algorithm->verify(*parameters, *key, signature, data, std::move(successCallback), std::move(failureCallback), ec);
    313337    if (ec) {
    314338        setDOMException(exec, ec);
     
    343367
    344368    JSPromise* promise = JSPromise::createWithResolver(exec->vm(), globalObject());
    345     std::unique_ptr<PromiseWrapper> promiseWrapper = PromiseWrapper::create(globalObject(), promise);
     369    PromiseWrapper promiseWrapper(globalObject(), promise);
     370    auto successCallback = [promiseWrapper](const Vector<uint8_t>& result) mutable {
     371        promiseWrapper.fulfill(result);
     372    };
     373    auto failureCallback = [promiseWrapper]() mutable {
     374        promiseWrapper.reject(nullptr);
     375    };
    346376
    347377    ExceptionCode ec = 0;
    348     algorithm->digest(*parameters, data, std::move(promiseWrapper), ec);
     378    algorithm->digest(*parameters, data, std::move(successCallback), std::move(failureCallback), ec);
    349379    if (ec) {
    350380        setDOMException(exec, ec);
     
    388418
    389419    JSPromise* promise = JSPromise::createWithResolver(exec->vm(), globalObject());
    390     auto promiseWrapper = PromiseWrapper::create(globalObject(), promise);
     420    PromiseWrapper promiseWrapper(globalObject(), promise);
     421    auto successCallback = [promiseWrapper](CryptoKey* key, CryptoKeyPair* keyPair) mutable {
     422        ASSERT(key || keyPair);
     423        ASSERT(!key || !keyPair);
     424        if (key)
     425            promiseWrapper.fulfill(key);
     426        else
     427            promiseWrapper.fulfill(keyPair);
     428    };
     429    auto failureCallback = [promiseWrapper]() mutable {
     430        promiseWrapper.reject(nullptr);
     431    };
    391432
    392433    ExceptionCode ec = 0;
    393     algorithm->generateKey(*parameters, extractable, keyUsages, std::move(promiseWrapper), ec);
     434    algorithm->generateKey(*parameters, extractable, keyUsages, std::move(successCallback), std::move(failureCallback), ec);
    394435    if (ec) {
    395436        setDOMException(exec, ec);
     
    495536
    496537    JSPromise* promise = JSPromise::createWithResolver(exec->vm(), globalObject());
    497     auto promiseWrapper = PromiseWrapper::create(globalObject(), promise);
     538    PromiseWrapper promiseWrapper(globalObject(), promise);
     539    auto successCallback = [promiseWrapper](CryptoKey& result) mutable {
     540        promiseWrapper.fulfill(&result);
     541    };
     542    auto failureCallback = [promiseWrapper]() mutable {
     543        promiseWrapper.reject(nullptr);
     544    };
    498545
    499546    ExceptionCode ec = 0;
    500     algorithm->importKey(*parameters, *keyData, extractable, keyUsages, std::move(promiseWrapper), ec);
     547    algorithm->importKey(*parameters, *keyData, extractable, keyUsages, std::move(successCallback), std::move(failureCallback), ec);
    501548    if (ec) {
    502549        setDOMException(exec, ec);
     
    523570
    524571    JSPromise* promise = JSPromise::createWithResolver(exec->vm(), globalObject());
    525     auto promiseWrapper = PromiseWrapper::create(globalObject(), promise);
     572    PromiseWrapper promiseWrapper(globalObject(), promise);
    526573
    527574    if (!key->extractable()) {
    528575        m_impl->document()->addConsoleMessage(JSMessageSource, ErrorMessageLevel, "Key is not extractable");
    529         promiseWrapper->reject(nullptr);
     576        promiseWrapper.reject(nullptr);
    530577        return promise;
    531578    }
     
    535582        Vector<unsigned char> result;
    536583        if (CryptoKeySerializationRaw::serialize(*key, result))
    537             promiseWrapper->fulfill(result);
     584            promiseWrapper.fulfill(result);
    538585        else {
    539586            m_impl->document()->addConsoleMessage(JSMessageSource, ErrorMessageLevel, "Key cannot be exported to raw format");
    540             promiseWrapper->reject(nullptr);
     587            promiseWrapper.reject(nullptr);
    541588        }
    542589        break;
     
    549596        Vector<unsigned char> resultBuffer;
    550597        resultBuffer.append(utf8String.data(), utf8String.length());
    551         promiseWrapper->fulfill(resultBuffer);
     598        promiseWrapper.fulfill(resultBuffer);
    552599        break;
    553600    }
  • trunk/Source/WebCore/crypto/CryptoAlgorithm.cpp

    r159379 r159578  
    4141}
    4242
    43 void CryptoAlgorithm::encrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode& ec)
     43void CryptoAlgorithm::encrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, VectorCallback, VoidCallback, ExceptionCode& ec)
    4444{
    4545    ec = NOT_SUPPORTED_ERR;
    4646}
    4747
    48 void CryptoAlgorithm::decrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode& ec)
     48void CryptoAlgorithm::decrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, VectorCallback, VoidCallback, ExceptionCode& ec)
    4949{
    5050    ec = NOT_SUPPORTED_ERR;
    5151}
    5252
    53 void CryptoAlgorithm::sign(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode& ec)
     53void CryptoAlgorithm::sign(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, VectorCallback, VoidCallback, ExceptionCode& ec)
    5454{
    5555    ec = NOT_SUPPORTED_ERR;
    5656}
    5757
    58 void CryptoAlgorithm::verify(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode& ec)
     58void CryptoAlgorithm::verify(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, const CryptoOperationData&, BoolCallback, VoidCallback, ExceptionCode& ec)
    5959{
    6060    ec = NOT_SUPPORTED_ERR;
    6161}
    6262
    63 void CryptoAlgorithm::digest(const CryptoAlgorithmParameters&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode& ec)
     63void CryptoAlgorithm::digest(const CryptoAlgorithmParameters&, const CryptoOperationData&, VectorCallback, VoidCallback, ExceptionCode& ec)
    6464{
    6565    ec = NOT_SUPPORTED_ERR;
    6666}
    6767
    68 void CryptoAlgorithm::generateKey(const CryptoAlgorithmParameters&, bool, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode& ec)
     68void CryptoAlgorithm::generateKey(const CryptoAlgorithmParameters&, bool, CryptoKeyUsage, KeyOrKeyPairCallback, VoidCallback, ExceptionCode& ec)
    6969{
    7070    ec = NOT_SUPPORTED_ERR;
    7171}
    7272
    73 void CryptoAlgorithm::deriveKey(const CryptoAlgorithmParameters&, const CryptoKey&, CryptoAlgorithm*, bool, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode& ec)
     73void CryptoAlgorithm::deriveKey(const CryptoAlgorithmParameters&, const CryptoKey&, CryptoAlgorithm*, bool, CryptoKeyUsage, KeyCallback, VoidCallback, ExceptionCode& ec)
    7474{
    7575    ec = NOT_SUPPORTED_ERR;
    7676}
    7777
    78 void CryptoAlgorithm::deriveBits(const CryptoAlgorithmParameters&, const CryptoKey&, unsigned long, std::unique_ptr<PromiseWrapper>, ExceptionCode& ec)
     78void CryptoAlgorithm::deriveBits(const CryptoAlgorithmParameters&, const CryptoKey&, unsigned long, VectorCallback, VoidCallback, ExceptionCode& ec)
    7979{
    8080    ec = NOT_SUPPORTED_ERR;
    8181}
    8282
    83 void CryptoAlgorithm::importKey(const CryptoAlgorithmParameters&, const CryptoKeyData&, bool, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode& ec)
     83void CryptoAlgorithm::importKey(const CryptoAlgorithmParameters&, const CryptoKeyData&, bool, CryptoKeyUsage, KeyCallback, VoidCallback, ExceptionCode& ec)
    8484{
    8585    ec = NOT_SUPPORTED_ERR;
  • trunk/Source/WebCore/crypto/CryptoAlgorithm.h

    r159379 r159578  
    2929#include "CryptoAlgorithmIdentifier.h"
    3030#include "CryptoKeyUsage.h"
     31#include <functional>
    3132#include <wtf/Noncopyable.h>
     33#include <wtf/Vector.h>
    3234
    3335#if ENABLE(SUBTLE_CRYPTO)
     
    3941class CryptoAlgorithmParameters;
    4042class CryptoKey;
     43class CryptoKeyPair;
    4144class CryptoKeyData;
    42 class PromiseWrapper;
    4345
    4446// Data is mutable, so async operations should copy it first.
     
    5254    virtual CryptoAlgorithmIdentifier identifier() const = 0;
    5355
    54     virtual void encrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode&);
    55     virtual void decrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode&);
    56     virtual void sign(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode&);
    57     virtual void verify(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData& signature, const CryptoOperationData& data, std::unique_ptr<PromiseWrapper>, ExceptionCode&);
    58     virtual void digest(const CryptoAlgorithmParameters&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode&);
    59     virtual void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode&);
    60     virtual void deriveKey(const CryptoAlgorithmParameters&, const CryptoKey& baseKey, CryptoAlgorithm* derivedKeyType, bool extractable, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode&);
    61     virtual void deriveBits(const CryptoAlgorithmParameters&, const CryptoKey& baseKey, unsigned long length, std::unique_ptr<PromiseWrapper>, ExceptionCode&);
    62     virtual void importKey(const CryptoAlgorithmParameters&, const CryptoKeyData&, bool extractable, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode&);
     56    typedef std::function<void(bool)> BoolCallback;
     57    typedef std::function<void(CryptoKey&)> KeyCallback;
     58    typedef std::function<void(CryptoKey*, CryptoKeyPair*)> KeyOrKeyPairCallback;
     59    typedef std::function<void(const Vector<uint8_t>&)> VectorCallback;
     60    typedef std::function<void()> VoidCallback;
     61
     62    virtual void encrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&);
     63    virtual void decrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&);
     64    virtual void sign(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&);
     65    virtual void verify(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData& signature, const CryptoOperationData& data, BoolCallback, VoidCallback failureCallback, ExceptionCode&);
     66    virtual void digest(const CryptoAlgorithmParameters&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&);
     67    virtual void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsage, KeyOrKeyPairCallback, VoidCallback failureCallback, ExceptionCode&);
     68    virtual void deriveKey(const CryptoAlgorithmParameters&, const CryptoKey& baseKey, CryptoAlgorithm* derivedKeyType, bool extractable, CryptoKeyUsage, KeyCallback, VoidCallback failureCallback, ExceptionCode&);
     69    virtual void deriveBits(const CryptoAlgorithmParameters&, const CryptoKey& baseKey, unsigned long length, VectorCallback, VoidCallback failureCallback, ExceptionCode&);
     70    virtual void importKey(const CryptoAlgorithmParameters&, const CryptoKeyData&, bool extractable, CryptoKeyUsage, KeyCallback, VoidCallback failureCallback, ExceptionCode&);
    6371
    6472protected:
  • trunk/Source/WebCore/crypto/CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp

    r159390 r159578  
    3333#include "CryptoKeyRSA.h"
    3434#include "ExceptionCode.h"
    35 #include "JSDOMPromise.h"
    3635#include <CommonCrypto/CommonCryptor.h>
    3736
     
    9089}
    9190
    92 void CryptoAlgorithmRSASSA_PKCS1_v1_5::sign(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& data, std::unique_ptr<PromiseWrapper> promise, ExceptionCode& ec)
     91void CryptoAlgorithmRSASSA_PKCS1_v1_5::sign(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& data, VectorCallback callback, VoidCallback failureCallback, ExceptionCode& ec)
    9392{
    9493    const CryptoAlgorithmRsaSsaParams& rsaSSAParameters = toCryptoAlgorithmRsaSsaParams(parameters);
     
    121120    CCCryptorStatus status = CCRSACryptorSign(rsaKey.platformKey(), ccPKCS1Padding, digestData.data(), digestData.size(), digestAlgorithm, 0, signature.data(), &signatureSize);
    122121    if (status) {
    123         promise->reject(nullptr);
     122        failureCallback();
    124123        return;
    125124    }
    126125
    127126    signature.resize(signatureSize);
    128     promise->fulfill(signature);
     127    callback(signature);
    129128}
    130129
    131 void CryptoAlgorithmRSASSA_PKCS1_v1_5::verify(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& signature, const CryptoOperationData& data, std::unique_ptr<PromiseWrapper> promise, ExceptionCode& ec)
     130void CryptoAlgorithmRSASSA_PKCS1_v1_5::verify(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& signature, const CryptoOperationData& data, BoolCallback callback, VoidCallback failureCallback, ExceptionCode& ec)
    132131{
    133132    const CryptoAlgorithmRsaSsaParams& rsaSSAParameters = toCryptoAlgorithmRsaSsaParams(parameters);
     
    157156    CCCryptorStatus status = CCRSACryptorVerify(rsaKey.platformKey(), ccPKCS1Padding, digestData.data(), digestData.size(), digestAlgorithm, 0, signature.first, signature.second);
    158157    if (!status)
    159         promise->fulfill(true);
     158        callback(true);
    160159    else if (status == kCCNotVerified || kCCDecodeError) // <rdar://problem/15464982> CCRSACryptorVerify returns kCCDecodeError instead of kCCNotVerified sometimes
    161         promise->fulfill(false);
     160        callback(false);
    162161    else
    163         promise->reject(nullptr);
     162        failureCallback();
    164163}
    165164
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmAES_CBC.cpp

    r159213 r159578  
    3333#include "CryptoKeyDataOctetSequence.h"
    3434#include "ExceptionCode.h"
    35 #include "JSDOMPromise.h"
    3635
    3736namespace WebCore {
     
    5756}
    5857
    59 void CryptoAlgorithmAES_CBC::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsage usages, std::unique_ptr<PromiseWrapper> promise, ExceptionCode&)
     58void CryptoAlgorithmAES_CBC::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsage usages, KeyOrKeyPairCallback callback, VoidCallback failureCallback, ExceptionCode&)
    6059{
    6160    const CryptoAlgorithmAesKeyGenParams& aesParameters = toCryptoAlgorithmAesKeyGenParams(parameters);
     
    6362    RefPtr<CryptoKeyAES> result = CryptoKeyAES::generate(CryptoAlgorithmIdentifier::AES_CBC, aesParameters.length, extractable, usages);
    6463    if (!result) {
    65         promise->reject(nullptr);
     64        failureCallback();
    6665        return;
    6766    }
    6867
    69     promise->fulfill(result.release());
     68    callback(result.get(), nullptr);
    7069}
    7170
    72 void CryptoAlgorithmAES_CBC::importKey(const CryptoAlgorithmParameters&, const CryptoKeyData& keyData, bool extractable, CryptoKeyUsage usage, std::unique_ptr<PromiseWrapper> promise, ExceptionCode& ec)
     71void CryptoAlgorithmAES_CBC::importKey(const CryptoAlgorithmParameters&, const CryptoKeyData& keyData, bool extractable, CryptoKeyUsage usage, KeyCallback callback, VoidCallback, ExceptionCode& ec)
    7372{
    7473    if (keyData.format() != CryptoKeyData::Format::OctetSequence) {
     
    7877    const CryptoKeyDataOctetSequence& keyDataOctetSequence = toCryptoKeyDataOctetSequence(keyData);
    7978    RefPtr<CryptoKeyAES> result = CryptoKeyAES::create(CryptoAlgorithmIdentifier::AES_CBC, keyDataOctetSequence.octetSequence(), extractable, usage);
    80     promise->fulfill(result.release());
     79    callback(*result);
    8180}
    8281
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmAES_CBC.h

    r159379 r159578  
    4242    virtual CryptoAlgorithmIdentifier identifier() const OVERRIDE;
    4343
    44     virtual void encrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
    45     virtual void decrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
    46     virtual void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
    47     virtual void importKey(const CryptoAlgorithmParameters&, const CryptoKeyData&, bool extractable, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
     44    virtual void encrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
     45    virtual void decrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
     46    virtual void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsage, KeyOrKeyPairCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
     47    virtual void importKey(const CryptoAlgorithmParameters&, const CryptoKeyData&, bool extractable, CryptoKeyUsage, KeyCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
    4848
    4949private:
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmHMAC.cpp

    r159213 r159578  
    3434#include "CryptoKeyHMAC.h"
    3535#include "ExceptionCode.h"
    36 #include "JSDOMPromise.h"
    3736
    3837namespace WebCore {
     
    5857}
    5958
    60 void CryptoAlgorithmHMAC::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsage usages, std::unique_ptr<PromiseWrapper> promise, ExceptionCode&)
     59void CryptoAlgorithmHMAC::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsage usages, KeyOrKeyPairCallback callback, VoidCallback failureCallback, ExceptionCode&)
    6160{
    6261    const CryptoAlgorithmHmacKeyParams& hmacParameters = toCryptoAlgorithmHmacKeyParams(parameters);
     
    6463    RefPtr<CryptoKeyHMAC> result = CryptoKeyHMAC::generate(hmacParameters.hasLength ? hmacParameters.length : 0, hmacParameters.hash, extractable, usages);
    6564    if (!result) {
    66         promise->reject(nullptr);
     65        failureCallback();
    6766        return;
    6867    }
    6968
    70     promise->fulfill(result.release());
     69    callback(result.get(), nullptr);
    7170}
    7271
    73 void CryptoAlgorithmHMAC::importKey(const CryptoAlgorithmParameters& parameters, const CryptoKeyData& keyData, bool extractable, CryptoKeyUsage usage, std::unique_ptr<PromiseWrapper> promise, ExceptionCode& ec)
     72void CryptoAlgorithmHMAC::importKey(const CryptoAlgorithmParameters& parameters, const CryptoKeyData& keyData, bool extractable, CryptoKeyUsage usage, KeyCallback callback, VoidCallback, ExceptionCode& ec)
    7473{
    7574    if (keyData.format() != CryptoKeyData::Format::OctetSequence) {
     
    8281
    8382    RefPtr<CryptoKeyHMAC> result = CryptoKeyHMAC::create(keyDataOctetSequence.octetSequence(), hmacParameters.hash, extractable, usage);
    84     promise->fulfill(result.release());
     83    callback(*result);
    8584}
    8685
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmHMAC.h

    r159379 r159578  
    4242    virtual CryptoAlgorithmIdentifier identifier() const OVERRIDE;
    4343
    44     virtual void sign(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
    45     virtual void verify(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData& signature, const CryptoOperationData& data, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
    46     virtual void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
    47     virtual void importKey(const CryptoAlgorithmParameters&, const CryptoKeyData&, bool extractable, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
     44    virtual void sign(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
     45    virtual void verify(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData& signature, const CryptoOperationData& data, BoolCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
     46    virtual void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsage, KeyOrKeyPairCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
     47    virtual void importKey(const CryptoAlgorithmParameters&, const CryptoKeyData&, bool extractable, CryptoKeyUsage, KeyCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
    4848
    4949private:
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.cpp

    r159213 r159578  
    3333#include "CryptoKeyDataRSAComponents.h"
    3434#include "CryptoKeyRSA.h"
    35 #include "JSDOMPromise.h"
    3635
    3736namespace WebCore {
     
    5756}
    5857
    59 void CryptoAlgorithmRSASSA_PKCS1_v1_5::importKey(const CryptoAlgorithmParameters& parameters, const CryptoKeyData& keyData, bool extractable, CryptoKeyUsage usage, std::unique_ptr<PromiseWrapper> promise, ExceptionCode&)
     58void CryptoAlgorithmRSASSA_PKCS1_v1_5::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsage usages, KeyOrKeyPairCallback callback, VoidCallback failureCallback, ExceptionCode&)
     59{
     60    const CryptoAlgorithmRsaKeyGenParams& rsaParameters = toCryptoAlgorithmRsaKeyGenParams(parameters);
     61
     62    auto keyPairCallback = [callback](CryptoKeyPair& pair) {
     63        callback(nullptr, &pair);
     64    };
     65
     66    CryptoKeyRSA::generatePair(CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5, rsaParameters.modulusLength, rsaParameters.publicExponent, extractable, usages, std::move(keyPairCallback), std::move(failureCallback));
     67}
     68
     69void CryptoAlgorithmRSASSA_PKCS1_v1_5::importKey(const CryptoAlgorithmParameters& parameters, const CryptoKeyData& keyData, bool extractable, CryptoKeyUsage usage, KeyCallback callback, VoidCallback failureCallback, ExceptionCode&)
    6070{
    6171    const CryptoAlgorithmRsaSsaKeyParams& rsaSSAParameters = toCryptoAlgorithmRsaSsaKeyParams(parameters);
     
    6474    RefPtr<CryptoKeyRSA> result = CryptoKeyRSA::create(CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5, rsaComponents, extractable, usage);
    6575    if (!result) {
    66         promise->reject(nullptr);
     76        failureCallback();
    6777        return;
    6878    }
     
    7181        result->restrictToHash(rsaSSAParameters.hash);
    7282
    73     promise->fulfill(result.release());
    74 }
    75 
    76 void CryptoAlgorithmRSASSA_PKCS1_v1_5::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsage usages, std::unique_ptr<PromiseWrapper> promise, ExceptionCode&)
    77 {
    78     const CryptoAlgorithmRsaKeyGenParams& rsaParameters = toCryptoAlgorithmRsaKeyGenParams(parameters);
    79 
    80     CryptoKeyRSA::generatePair(CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5, rsaParameters.modulusLength, rsaParameters.publicExponent, extractable, usages, std::move(promise));
     83    callback(*result);
    8184}
    8285
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.h

    r159379 r159578  
    4242    virtual CryptoAlgorithmIdentifier identifier() const OVERRIDE;
    4343
    44     virtual void sign(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
    45     virtual void verify(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData& signature, const CryptoOperationData& data, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
    46     virtual void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
    47     virtual void importKey(const CryptoAlgorithmParameters&, const CryptoKeyData&, bool extractable, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
     44    virtual void sign(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
     45    virtual void verify(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData& signature, const CryptoOperationData& data, BoolCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
     46    virtual void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsage, KeyOrKeyPairCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
     47    virtual void importKey(const CryptoAlgorithmParameters&, const CryptoKeyData&, bool extractable, CryptoKeyUsage, KeyCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
    4848
    4949private:
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA1.cpp

    r159379 r159578  
    3030
    3131#include "CryptoDigest.h"
    32 #include "JSDOMPromise.h"
    3332
    3433namespace WebCore {
     
    5453}
    5554
    56 void CryptoAlgorithmSHA1::digest(const CryptoAlgorithmParameters&, const CryptoOperationData& data, std::unique_ptr<PromiseWrapper> promise, ExceptionCode&)
     55void CryptoAlgorithmSHA1::digest(const CryptoAlgorithmParameters&, const CryptoOperationData& data, VectorCallback callback, VoidCallback failureCallback, ExceptionCode&)
    5756{
    5857    std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(CryptoAlgorithmIdentifier::SHA_1);
    5958    if (!digest) {
    60         promise->reject(nullptr);
     59        failureCallback();
    6160        return;
    6261    }
     
    6463    digest->addBytes(data.first, data.second);
    6564
    66     promise->fulfill(digest->computeHash());
     65    callback(digest->computeHash());
    6766}
    6867
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA1.h

    r159379 r159578  
    4242    virtual CryptoAlgorithmIdentifier identifier() const OVERRIDE;
    4343
    44     virtual void digest(const CryptoAlgorithmParameters&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
     44    virtual void digest(const CryptoAlgorithmParameters&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
    4545
    4646private:
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA224.cpp

    r159379 r159578  
    3030
    3131#include "CryptoDigest.h"
    32 #include "JSDOMPromise.h"
    3332
    3433namespace WebCore {
     
    5453}
    5554
    56 void CryptoAlgorithmSHA224::digest(const CryptoAlgorithmParameters&, const CryptoOperationData& data, std::unique_ptr<PromiseWrapper> promise, ExceptionCode&)
     55void CryptoAlgorithmSHA224::digest(const CryptoAlgorithmParameters&, const CryptoOperationData& data, VectorCallback callback, VoidCallback failureCallback, ExceptionCode&)
    5756{
    5857    std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(CryptoAlgorithmIdentifier::SHA_224);
    5958    if (!digest) {
    60         promise->reject(nullptr);
     59        failureCallback();
    6160        return;
    6261    }
     
    6463    digest->addBytes(data.first, data.second);
    6564
    66     promise->fulfill(digest->computeHash());
     65    callback(digest->computeHash());
    6766}
    6867
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA224.h

    r159379 r159578  
    4242    virtual CryptoAlgorithmIdentifier identifier() const OVERRIDE;
    4343
    44     virtual void digest(const CryptoAlgorithmParameters&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
     44    virtual void digest(const CryptoAlgorithmParameters&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
    4545
    4646private:
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA256.cpp

    r159379 r159578  
    3030
    3131#include "CryptoDigest.h"
    32 #include "JSDOMPromise.h"
    3332
    3433namespace WebCore {
     
    5453}
    5554
    56 void CryptoAlgorithmSHA256::digest(const CryptoAlgorithmParameters&, const CryptoOperationData& data, std::unique_ptr<PromiseWrapper> promise, ExceptionCode&)
     55void CryptoAlgorithmSHA256::digest(const CryptoAlgorithmParameters&, const CryptoOperationData& data, VectorCallback callback, VoidCallback failureCallback, ExceptionCode&)
    5756{
    5857    std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(CryptoAlgorithmIdentifier::SHA_256);
    5958    if (!digest) {
    60         promise->reject(nullptr);
     59        failureCallback();
    6160        return;
    6261    }
     
    6463    digest->addBytes(data.first, data.second);
    6564
    66     promise->fulfill(digest->computeHash());
     65    callback(digest->computeHash());
    6766}
    6867
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA256.h

    r159379 r159578  
    4242    virtual CryptoAlgorithmIdentifier identifier() const OVERRIDE;
    4343
    44     virtual void digest(const CryptoAlgorithmParameters&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
     44    virtual void digest(const CryptoAlgorithmParameters&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
    4545
    4646private:
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA384.cpp

    r159379 r159578  
    3030
    3131#include "CryptoDigest.h"
    32 #include "JSDOMPromise.h"
    3332
    3433namespace WebCore {
     
    5453}
    5554
    56 void CryptoAlgorithmSHA384::digest(const CryptoAlgorithmParameters&, const CryptoOperationData& data, std::unique_ptr<PromiseWrapper> promise, ExceptionCode&)
     55void CryptoAlgorithmSHA384::digest(const CryptoAlgorithmParameters&, const CryptoOperationData& data, VectorCallback callback, VoidCallback failureCallback, ExceptionCode&)
    5756{
    5857    std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(CryptoAlgorithmIdentifier::SHA_384);
    5958    if (!digest) {
    60         promise->reject(nullptr);
     59        failureCallback();
    6160        return;
    6261    }
     
    6463    digest->addBytes(data.first, data.second);
    6564
    66     promise->fulfill(digest->computeHash());
     65    callback(digest->computeHash());
    6766}
    6867
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA384.h

    r159379 r159578  
    4242    virtual CryptoAlgorithmIdentifier identifier() const OVERRIDE;
    4343
    44     virtual void digest(const CryptoAlgorithmParameters&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
     44    virtual void digest(const CryptoAlgorithmParameters&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
    4545
    4646private:
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA512.cpp

    r159379 r159578  
    3030
    3131#include "CryptoDigest.h"
    32 #include "JSDOMPromise.h"
    3332
    3433namespace WebCore {
     
    5453}
    5554
    56 void CryptoAlgorithmSHA512::digest(const CryptoAlgorithmParameters&, const CryptoOperationData& data, std::unique_ptr<PromiseWrapper> promise, ExceptionCode&)
     55void CryptoAlgorithmSHA512::digest(const CryptoAlgorithmParameters&, const CryptoOperationData& data, VectorCallback callback, VoidCallback failureCallback, ExceptionCode&)
    5756{
    5857    std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(CryptoAlgorithmIdentifier::SHA_512);
    5958    if (!digest) {
    60         promise->reject(nullptr);
     59        failureCallback();
    6160        return;
    6261    }
     
    6463    digest->addBytes(data.first, data.second);
    6564
    66     promise->fulfill(digest->computeHash());
     65    callback(digest->computeHash());
    6766}
    6867
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA512.h

    r159379 r159578  
    4242    virtual CryptoAlgorithmIdentifier identifier() const OVERRIDE;
    4343
    44     virtual void digest(const CryptoAlgorithmParameters&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
     44    virtual void digest(const CryptoAlgorithmParameters&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
    4545
    4646private:
  • trunk/Source/WebCore/crypto/keys/CryptoKeyRSA.h

    r159403 r159578  
    2828
    2929#include "CryptoKey.h"
     30#include <functional>
    3031
    3132#if ENABLE(SUBTLE_CRYPTO)
     
    5657    size_t keySizeInBits() const;
    5758
    58     static void generatePair(CryptoAlgorithmIdentifier, unsigned modulusLength, const Vector<uint8_t>& publicExponent, bool extractable, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>);
     59    typedef std::function<void(CryptoKeyPair&)> KeyPairCallback;
     60    typedef std::function<void()> VoidCallback;
     61    static void generatePair(CryptoAlgorithmIdentifier, unsigned modulusLength, const Vector<uint8_t>& publicExponent, bool extractable, CryptoKeyUsage, KeyPairCallback, VoidCallback failureCallback);
    5962
    6063    PlatformRSAKey platformKey() const { return m_platformKey; }
  • trunk/Source/WebCore/crypto/mac/CryptoAlgorithmAES_CBCMac.cpp

    r159390 r159578  
    3232#include "CryptoKeyAES.h"
    3333#include "ExceptionCode.h"
    34 #include "JSDOMPromise.h"
    3534#include <CommonCrypto/CommonCrypto.h>
    3635
    3736namespace WebCore {
    3837
    39 static void transformAES_CBC(CCOperation operation, const CryptoAlgorithmAesCbcParams& parameters, const CryptoKeyAES& key, const CryptoOperationData& data, std::unique_ptr<PromiseWrapper> promise)
     38static void transformAES_CBC(CCOperation operation, const CryptoAlgorithmAesCbcParams& parameters, const CryptoKeyAES& key, const CryptoOperationData& data, CryptoAlgorithm::VectorCallback callback, CryptoAlgorithm::VoidCallback failureCallback)
    4039{
    4140    static_assert(sizeof(parameters.iv) == kCCBlockSizeAES128, "Initialization vector size must be the same as algorithm block size");
     
    4342    size_t keyLengthInBytes = key.key().size();
    4443    if (keyLengthInBytes != 16 && keyLengthInBytes != 24 && keyLengthInBytes != 32) {
    45         promise->reject(nullptr);
     44        failureCallback();
    4645        return;
    4746    }
     
    5554    CCCryptorStatus status = CCCryptorCreate(operation, aesAlgorithm, kCCOptionPKCS7Padding, key.key().data(), keyLengthInBytes, parameters.iv.data(), &cryptor);
    5655    if (status) {
    57         promise->reject(nullptr);
     56        failureCallback();
    5857        return;
    5958    }
     
    6463    status = CCCryptorUpdate(cryptor, data.first, data.second, result.data(), result.size(), &bytesWritten);
    6564    if (status) {
    66         promise->reject(nullptr);
     65        failureCallback();
    6766        return;
    6867    }
     
    7271    p += bytesWritten;
    7372    if (status) {
    74         promise->reject(nullptr);
     73        failureCallback();
    7574        return;
    7675    }
     
    8180    CCCryptorRelease(cryptor);
    8281
    83     promise->fulfill(result);
     82    callback(result);
    8483}
    8584
    86 void CryptoAlgorithmAES_CBC::encrypt(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& data, std::unique_ptr<PromiseWrapper> promise, ExceptionCode& ec)
     85void CryptoAlgorithmAES_CBC::encrypt(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& data, VectorCallback callback, VoidCallback failureCallback, ExceptionCode& ec)
    8786{
    8887    const CryptoAlgorithmAesCbcParams& aesCBCParameters = toCryptoAlgorithmAesCbcParams(parameters);
     
    9493    const CryptoKeyAES& aesKey = toCryptoKeyAES(key);
    9594
    96     transformAES_CBC(kCCEncrypt, aesCBCParameters, aesKey, data, std::move(promise));
     95    transformAES_CBC(kCCEncrypt, aesCBCParameters, aesKey, data, std::move(callback), std::move(failureCallback));
    9796}
    9897
    99 void CryptoAlgorithmAES_CBC::decrypt(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& data, std::unique_ptr<PromiseWrapper> promise, ExceptionCode& ec)
     98void CryptoAlgorithmAES_CBC::decrypt(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& data, VectorCallback callback, VoidCallback failureCallback, ExceptionCode& ec)
    10099{
    101100    const CryptoAlgorithmAesCbcParams& aesCBCParameters = toCryptoAlgorithmAesCbcParams(parameters);
     
    107106    const CryptoKeyAES& aesKey = toCryptoKeyAES(key);
    108107
    109     transformAES_CBC(kCCDecrypt, aesCBCParameters, aesKey, data, std::move(promise));
     108    transformAES_CBC(kCCDecrypt, aesCBCParameters, aesKey, data, std::move(callback), std::move(failureCallback));
    110109}
    111110
  • trunk/Source/WebCore/crypto/mac/CryptoAlgorithmHMACMac.cpp

    r159390 r159578  
    3232#include "CryptoKeyHMAC.h"
    3333#include "ExceptionCode.h"
    34 #include "JSDOMPromise.h"
    3534#include <CommonCrypto/CommonHMAC.h>
    3635
     
    9089}
    9190
    92 void CryptoAlgorithmHMAC::sign(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& data, std::unique_ptr<PromiseWrapper> promise, ExceptionCode& ec)
     91void CryptoAlgorithmHMAC::sign(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& data, VectorCallback callback, VoidCallback, ExceptionCode& ec)
    9392{
    9493    const CryptoAlgorithmHmacParams& hmacParameters = toCryptoAlgorithmHmacParams(parameters);
     
    108107    Vector<uint8_t> signature = calculateSignature(algorithm, hmacKey.key(), data);
    109108
    110     promise->fulfill(signature);
     109    callback(signature);
    111110}
    112111
    113 void CryptoAlgorithmHMAC::verify(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& expectedSignature, const CryptoOperationData& data, std::unique_ptr<PromiseWrapper> promise, ExceptionCode& ec)
     112void CryptoAlgorithmHMAC::verify(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& expectedSignature, const CryptoOperationData& data, BoolCallback callback, VoidCallback, ExceptionCode& ec)
    114113{
    115114    const CryptoAlgorithmHmacParams& hmacParameters = toCryptoAlgorithmHmacParams(parameters);
     
    131130    bool result = signature.size() == expectedSignature.second && !memcmp(signature.data(), expectedSignature.first, signature.size());
    132131
    133     promise->fulfill(result);
     132    callback(result);
    134133}
    135134
  • trunk/Source/WebCore/crypto/mac/CryptoKeyRSAMac.cpp

    r159403 r159578  
    3333#include "CryptoKeyDataRSAComponents.h"
    3434#include "CryptoKeyPair.h"
    35 #include "JSDOMPromise.h"
    3635#include <CommonCrypto/CommonCryptor.h>
    3736
     
    210209}
    211210
    212 void CryptoKeyRSA::generatePair(CryptoAlgorithmIdentifier algorithm, unsigned modulusLength, const Vector<uint8_t>& publicExponent, bool extractable, CryptoKeyUsage usage, std::unique_ptr<PromiseWrapper> promise)
     211void CryptoKeyRSA::generatePair(CryptoAlgorithmIdentifier algorithm, unsigned modulusLength, const Vector<uint8_t>& publicExponent, bool extractable, CryptoKeyUsage usage, KeyPairCallback callback, VoidCallback failureCallback)
    213212{
    214213    uint32_t e;
     
    216215        // Adding support is tracked as <rdar://problem/15444034>.
    217216        WTFLogAlways("Public exponent is too big, not supported");
    218         promise->reject(nullptr);
     217        failureCallback();
    219218        return;
    220219    }
    221220
    222     PromiseWrapper* localPromise = promise.release();
     221    // We only use the callback functions when back on the main thread, but captured variables are copied on a secondary thread too.
     222    KeyPairCallback* localCallback = new KeyPairCallback(std::move(callback));
     223    VoidCallback* localFailureCallback = new VoidCallback(std::move(failureCallback));
    223224
    224225    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
     
    229230            WTFLogAlways("Could not generate a key pair, status %d", status);
    230231            dispatch_async(dispatch_get_main_queue(), ^{
    231                 localPromise->reject(nullptr);
    232                 delete localPromise;
     232                (*localFailureCallback)();
     233                delete localFailureCallback;
    233234            });
    234235            return;
     
    237238            RefPtr<CryptoKeyRSA> publicKey = CryptoKeyRSA::create(algorithm, CryptoKeyType::Public, ccPublicKey, extractable, usage);
    238239            RefPtr<CryptoKeyRSA> privateKey = CryptoKeyRSA::create(algorithm, CryptoKeyType::Private, ccPrivateKey, extractable, usage);
    239             localPromise->fulfill(CryptoKeyPair::create(publicKey.release(), privateKey.release()));
    240             delete localPromise;
     240            (*localCallback)(*CryptoKeyPair::create(publicKey.release(), privateKey.release()));
     241            delete localCallback;
    241242        });
    242243    });
Note: See TracChangeset for help on using the changeset viewer.