Commit c1767f0a authored by ap@apple.com's avatar ap@apple.com

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):



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@159578 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent fccf9a61
2013-11-20 Alexey Proskuryakov <ap@apple.com>
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):
2013-11-20 Robert Hogan <robert@webkit.org>
REGRESSION(r127163): Respect clearance set on ancestors when placing floats
......
......@@ -34,4 +34,17 @@ PromiseWrapper::PromiseWrapper(JSDOMGlobalObject* globalObject, JSC::JSPromise*
{
}
PromiseWrapper::PromiseWrapper(const PromiseWrapper& other)
: m_globalObject(other.m_globalObject)
, m_promise(other.m_promise)
{
}
PromiseWrapper& PromiseWrapper::operator=(const PromiseWrapper& other)
{
m_globalObject = other.m_globalObject;
m_promise = other.m_promise;
return *this;
}
}
......@@ -33,18 +33,15 @@
#include <runtime/JSPromise.h>
#include <runtime/JSPromiseResolver.h>
#include <heap/StrongInlines.h>
#include <wtf/Noncopyable.h>
namespace WebCore {
// FIXME: Using this class in DOM code makes it dependent on JS bindings.
class PromiseWrapper {
WTF_MAKE_NONCOPYABLE(PromiseWrapper)
public:
static std::unique_ptr<PromiseWrapper> create(JSDOMGlobalObject* globalObject, JSC::JSPromise* promise)
{
return std::unique_ptr<PromiseWrapper>(new PromiseWrapper(globalObject, promise));
}
PromiseWrapper(JSDOMGlobalObject*, JSC::JSPromise*);
PromiseWrapper(const PromiseWrapper&);
PromiseWrapper& operator=(const PromiseWrapper&);
template<class FulfillResultType>
void fulfill(const FulfillResultType&);
......@@ -53,8 +50,6 @@ public:
void reject(const RejectResultType&);
private:
PromiseWrapper(JSDOMGlobalObject*, JSC::JSPromise*);
JSC::Strong<JSDOMGlobalObject> m_globalObject;
JSC::Strong<JSC::JSPromise> m_promise;
};
......
......@@ -162,10 +162,16 @@ JSValue JSSubtleCrypto::encrypt(ExecState* exec)
}
JSPromise* promise = JSPromise::createWithResolver(exec->vm(), globalObject());
auto promiseWrapper = PromiseWrapper::create(globalObject(), promise);
PromiseWrapper promiseWrapper(globalObject(), promise);
auto successCallback = [promiseWrapper](const Vector<uint8_t>& result) mutable {
promiseWrapper.fulfill(result);
};
auto failureCallback = [promiseWrapper]() mutable {
promiseWrapper.reject(nullptr);
};
ExceptionCode ec = 0;
algorithm->encrypt(*parameters, *key, data, std::move(promiseWrapper), ec);
algorithm->encrypt(*parameters, *key, data, std::move(successCallback), std::move(failureCallback), ec);
if (ec) {
setDOMException(exec, ec);
return jsUndefined();
......@@ -208,10 +214,16 @@ JSValue JSSubtleCrypto::decrypt(ExecState* exec)
}
JSPromise* promise = JSPromise::createWithResolver(exec->vm(), globalObject());
auto promiseWrapper = PromiseWrapper::create(globalObject(), promise);
PromiseWrapper promiseWrapper(globalObject(), promise);
auto successCallback = [promiseWrapper](const Vector<uint8_t>& result) mutable {
promiseWrapper.fulfill(result);
};
auto failureCallback = [promiseWrapper]() mutable {
promiseWrapper.reject(nullptr);
};
ExceptionCode ec = 0;
algorithm->decrypt(*parameters, *key, data, std::move(promiseWrapper), ec);
algorithm->decrypt(*parameters, *key, data, std::move(successCallback), std::move(failureCallback), ec);
if (ec) {
setDOMException(exec, ec);
return jsUndefined();
......@@ -254,10 +266,16 @@ JSValue JSSubtleCrypto::sign(ExecState* exec)
}
JSPromise* promise = JSPromise::createWithResolver(exec->vm(), globalObject());
auto promiseWrapper = PromiseWrapper::create(globalObject(), promise);
PromiseWrapper promiseWrapper(globalObject(), promise);
auto successCallback = [promiseWrapper](const Vector<uint8_t>& result) mutable {
promiseWrapper.fulfill(result);
};
auto failureCallback = [promiseWrapper]() mutable {
promiseWrapper.reject(nullptr);
};
ExceptionCode ec = 0;
algorithm->sign(*parameters, *key, data, std::move(promiseWrapper), ec);
algorithm->sign(*parameters, *key, data, std::move(successCallback), std::move(failureCallback), ec);
if (ec) {
setDOMException(exec, ec);
return jsUndefined();
......@@ -306,10 +324,16 @@ JSValue JSSubtleCrypto::verify(ExecState* exec)
}
JSPromise* promise = JSPromise::createWithResolver(exec->vm(), globalObject());
auto promiseWrapper = PromiseWrapper::create(globalObject(), promise);
PromiseWrapper promiseWrapper(globalObject(), promise);
auto successCallback = [promiseWrapper](bool result) mutable {
promiseWrapper.fulfill(result);
};
auto failureCallback = [promiseWrapper]() mutable {
promiseWrapper.reject(nullptr);
};
ExceptionCode ec = 0;
algorithm->verify(*parameters, *key, signature, data, std::move(promiseWrapper), ec);
algorithm->verify(*parameters, *key, signature, data, std::move(successCallback), std::move(failureCallback), ec);
if (ec) {
setDOMException(exec, ec);
return jsUndefined();
......@@ -342,10 +366,16 @@ JSValue JSSubtleCrypto::digest(ExecState* exec)
}
JSPromise* promise = JSPromise::createWithResolver(exec->vm(), globalObject());
std::unique_ptr<PromiseWrapper> promiseWrapper = PromiseWrapper::create(globalObject(), promise);
PromiseWrapper promiseWrapper(globalObject(), promise);
auto successCallback = [promiseWrapper](const Vector<uint8_t>& result) mutable {
promiseWrapper.fulfill(result);
};
auto failureCallback = [promiseWrapper]() mutable {
promiseWrapper.reject(nullptr);
};
ExceptionCode ec = 0;
algorithm->digest(*parameters, data, std::move(promiseWrapper), ec);
algorithm->digest(*parameters, data, std::move(successCallback), std::move(failureCallback), ec);
if (ec) {
setDOMException(exec, ec);
return jsUndefined();
......@@ -387,10 +417,21 @@ JSValue JSSubtleCrypto::generateKey(JSC::ExecState* exec)
}
JSPromise* promise = JSPromise::createWithResolver(exec->vm(), globalObject());
auto promiseWrapper = PromiseWrapper::create(globalObject(), promise);
PromiseWrapper promiseWrapper(globalObject(), promise);
auto successCallback = [promiseWrapper](CryptoKey* key, CryptoKeyPair* keyPair) mutable {
ASSERT(key || keyPair);
ASSERT(!key || !keyPair);
if (key)
promiseWrapper.fulfill(key);
else
promiseWrapper.fulfill(keyPair);
};
auto failureCallback = [promiseWrapper]() mutable {
promiseWrapper.reject(nullptr);
};
ExceptionCode ec = 0;
algorithm->generateKey(*parameters, extractable, keyUsages, std::move(promiseWrapper), ec);
algorithm->generateKey(*parameters, extractable, keyUsages, std::move(successCallback), std::move(failureCallback), ec);
if (ec) {
setDOMException(exec, ec);
return jsUndefined();
......@@ -494,10 +535,16 @@ JSValue JSSubtleCrypto::importKey(JSC::ExecState* exec)
return jsUndefined();
JSPromise* promise = JSPromise::createWithResolver(exec->vm(), globalObject());
auto promiseWrapper = PromiseWrapper::create(globalObject(), promise);
PromiseWrapper promiseWrapper(globalObject(), promise);
auto successCallback = [promiseWrapper](CryptoKey& result) mutable {
promiseWrapper.fulfill(&result);
};
auto failureCallback = [promiseWrapper]() mutable {
promiseWrapper.reject(nullptr);
};
ExceptionCode ec = 0;
algorithm->importKey(*parameters, *keyData, extractable, keyUsages, std::move(promiseWrapper), ec);
algorithm->importKey(*parameters, *keyData, extractable, keyUsages, std::move(successCallback), std::move(failureCallback), ec);
if (ec) {
setDOMException(exec, ec);
return jsUndefined();
......@@ -522,11 +569,11 @@ JSValue JSSubtleCrypto::exportKey(JSC::ExecState* exec)
return throwTypeError(exec);
JSPromise* promise = JSPromise::createWithResolver(exec->vm(), globalObject());
auto promiseWrapper = PromiseWrapper::create(globalObject(), promise);
PromiseWrapper promiseWrapper(globalObject(), promise);
if (!key->extractable()) {
m_impl->document()->addConsoleMessage(JSMessageSource, ErrorMessageLevel, "Key is not extractable");
promiseWrapper->reject(nullptr);
promiseWrapper.reject(nullptr);
return promise;
}
......@@ -534,10 +581,10 @@ JSValue JSSubtleCrypto::exportKey(JSC::ExecState* exec)
case CryptoKeyFormat::Raw: {
Vector<unsigned char> result;
if (CryptoKeySerializationRaw::serialize(*key, result))
promiseWrapper->fulfill(result);
promiseWrapper.fulfill(result);
else {
m_impl->document()->addConsoleMessage(JSMessageSource, ErrorMessageLevel, "Key cannot be exported to raw format");
promiseWrapper->reject(nullptr);
promiseWrapper.reject(nullptr);
}
break;
}
......@@ -548,7 +595,7 @@ JSValue JSSubtleCrypto::exportKey(JSC::ExecState* exec)
CString utf8String = result.utf8(StrictConversion);
Vector<unsigned char> resultBuffer;
resultBuffer.append(utf8String.data(), utf8String.length());
promiseWrapper->fulfill(resultBuffer);
promiseWrapper.fulfill(resultBuffer);
break;
}
default:
......
......@@ -40,47 +40,47 @@ CryptoAlgorithm::~CryptoAlgorithm()
{
}
void CryptoAlgorithm::encrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode& ec)
void CryptoAlgorithm::encrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, VectorCallback, VoidCallback, ExceptionCode& ec)
{
ec = NOT_SUPPORTED_ERR;
}
void CryptoAlgorithm::decrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode& ec)
void CryptoAlgorithm::decrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, VectorCallback, VoidCallback, ExceptionCode& ec)
{
ec = NOT_SUPPORTED_ERR;
}
void CryptoAlgorithm::sign(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode& ec)
void CryptoAlgorithm::sign(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, VectorCallback, VoidCallback, ExceptionCode& ec)
{
ec = NOT_SUPPORTED_ERR;
}
void CryptoAlgorithm::verify(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode& ec)
void CryptoAlgorithm::verify(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, const CryptoOperationData&, BoolCallback, VoidCallback, ExceptionCode& ec)
{
ec = NOT_SUPPORTED_ERR;
}
void CryptoAlgorithm::digest(const CryptoAlgorithmParameters&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode& ec)
void CryptoAlgorithm::digest(const CryptoAlgorithmParameters&, const CryptoOperationData&, VectorCallback, VoidCallback, ExceptionCode& ec)
{
ec = NOT_SUPPORTED_ERR;
}
void CryptoAlgorithm::generateKey(const CryptoAlgorithmParameters&, bool, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode& ec)
void CryptoAlgorithm::generateKey(const CryptoAlgorithmParameters&, bool, CryptoKeyUsage, KeyOrKeyPairCallback, VoidCallback, ExceptionCode& ec)
{
ec = NOT_SUPPORTED_ERR;
}
void CryptoAlgorithm::deriveKey(const CryptoAlgorithmParameters&, const CryptoKey&, CryptoAlgorithm*, bool, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode& ec)
void CryptoAlgorithm::deriveKey(const CryptoAlgorithmParameters&, const CryptoKey&, CryptoAlgorithm*, bool, CryptoKeyUsage, KeyCallback, VoidCallback, ExceptionCode& ec)
{
ec = NOT_SUPPORTED_ERR;
}
void CryptoAlgorithm::deriveBits(const CryptoAlgorithmParameters&, const CryptoKey&, unsigned long, std::unique_ptr<PromiseWrapper>, ExceptionCode& ec)
void CryptoAlgorithm::deriveBits(const CryptoAlgorithmParameters&, const CryptoKey&, unsigned long, VectorCallback, VoidCallback, ExceptionCode& ec)
{
ec = NOT_SUPPORTED_ERR;
}
void CryptoAlgorithm::importKey(const CryptoAlgorithmParameters&, const CryptoKeyData&, bool, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode& ec)
void CryptoAlgorithm::importKey(const CryptoAlgorithmParameters&, const CryptoKeyData&, bool, CryptoKeyUsage, KeyCallback, VoidCallback, ExceptionCode& ec)
{
ec = NOT_SUPPORTED_ERR;
}
......
......@@ -28,7 +28,9 @@
#include "CryptoAlgorithmIdentifier.h"
#include "CryptoKeyUsage.h"
#include <functional>
#include <wtf/Noncopyable.h>
#include <wtf/Vector.h>
#if ENABLE(SUBTLE_CRYPTO)
......@@ -38,8 +40,8 @@ typedef int ExceptionCode;
class CryptoAlgorithmParameters;
class CryptoKey;
class CryptoKeyPair;
class CryptoKeyData;
class PromiseWrapper;
// Data is mutable, so async operations should copy it first.
typedef std::pair<const char*, size_t> CryptoOperationData;
......@@ -51,15 +53,21 @@ public:
virtual CryptoAlgorithmIdentifier identifier() const = 0;
virtual void encrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode&);
virtual void decrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode&);
virtual void sign(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode&);
virtual void verify(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData& signature, const CryptoOperationData& data, std::unique_ptr<PromiseWrapper>, ExceptionCode&);
virtual void digest(const CryptoAlgorithmParameters&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode&);
virtual void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode&);
virtual void deriveKey(const CryptoAlgorithmParameters&, const CryptoKey& baseKey, CryptoAlgorithm* derivedKeyType, bool extractable, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode&);
virtual void deriveBits(const CryptoAlgorithmParameters&, const CryptoKey& baseKey, unsigned long length, std::unique_ptr<PromiseWrapper>, ExceptionCode&);
virtual void importKey(const CryptoAlgorithmParameters&, const CryptoKeyData&, bool extractable, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode&);
typedef std::function<void(bool)> BoolCallback;
typedef std::function<void(CryptoKey&)> KeyCallback;
typedef std::function<void(CryptoKey*, CryptoKeyPair*)> KeyOrKeyPairCallback;
typedef std::function<void(const Vector<uint8_t>&)> VectorCallback;
typedef std::function<void()> VoidCallback;
virtual void encrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&);
virtual void decrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&);
virtual void sign(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&);
virtual void verify(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData& signature, const CryptoOperationData& data, BoolCallback, VoidCallback failureCallback, ExceptionCode&);
virtual void digest(const CryptoAlgorithmParameters&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&);
virtual void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsage, KeyOrKeyPairCallback, VoidCallback failureCallback, ExceptionCode&);
virtual void deriveKey(const CryptoAlgorithmParameters&, const CryptoKey& baseKey, CryptoAlgorithm* derivedKeyType, bool extractable, CryptoKeyUsage, KeyCallback, VoidCallback failureCallback, ExceptionCode&);
virtual void deriveBits(const CryptoAlgorithmParameters&, const CryptoKey& baseKey, unsigned long length, VectorCallback, VoidCallback failureCallback, ExceptionCode&);
virtual void importKey(const CryptoAlgorithmParameters&, const CryptoKeyData&, bool extractable, CryptoKeyUsage, KeyCallback, VoidCallback failureCallback, ExceptionCode&);
protected:
CryptoAlgorithm();
......
......@@ -32,7 +32,6 @@
#include "CryptoDigest.h"
#include "CryptoKeyRSA.h"
#include "ExceptionCode.h"
#include "JSDOMPromise.h"
#include <CommonCrypto/CommonCryptor.h>
#if defined(__has_include)
......@@ -89,7 +88,7 @@ static bool getCommonCryptoDigestAlgorithm(CryptoAlgorithmIdentifier hashFunctio
}
}
void CryptoAlgorithmRSASSA_PKCS1_v1_5::sign(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& data, std::unique_ptr<PromiseWrapper> promise, ExceptionCode& ec)
void CryptoAlgorithmRSASSA_PKCS1_v1_5::sign(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& data, VectorCallback callback, VoidCallback failureCallback, ExceptionCode& ec)
{
const CryptoAlgorithmRsaSsaParams& rsaSSAParameters = toCryptoAlgorithmRsaSsaParams(parameters);
......@@ -120,15 +119,15 @@ void CryptoAlgorithmRSASSA_PKCS1_v1_5::sign(const CryptoAlgorithmParameters& par
CCCryptorStatus status = CCRSACryptorSign(rsaKey.platformKey(), ccPKCS1Padding, digestData.data(), digestData.size(), digestAlgorithm, 0, signature.data(), &signatureSize);
if (status) {
promise->reject(nullptr);
failureCallback();
return;
}
signature.resize(signatureSize);
promise->fulfill(signature);
callback(signature);
}
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)
void CryptoAlgorithmRSASSA_PKCS1_v1_5::verify(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& signature, const CryptoOperationData& data, BoolCallback callback, VoidCallback failureCallback, ExceptionCode& ec)
{
const CryptoAlgorithmRsaSsaParams& rsaSSAParameters = toCryptoAlgorithmRsaSsaParams(parameters);
......@@ -156,11 +155,11 @@ void CryptoAlgorithmRSASSA_PKCS1_v1_5::verify(const CryptoAlgorithmParameters& p
CCCryptorStatus status = CCRSACryptorVerify(rsaKey.platformKey(), ccPKCS1Padding, digestData.data(), digestData.size(), digestAlgorithm, 0, signature.first, signature.second);
if (!status)
promise->fulfill(true);
callback(true);
else if (status == kCCNotVerified || kCCDecodeError) // <rdar://problem/15464982> CCRSACryptorVerify returns kCCDecodeError instead of kCCNotVerified sometimes
promise->fulfill(false);
callback(false);
else
promise->reject(nullptr);
failureCallback();
}
} // namespace WebCore
......
......@@ -32,7 +32,6 @@
#include "CryptoKeyAES.h"
#include "CryptoKeyDataOctetSequence.h"
#include "ExceptionCode.h"
#include "JSDOMPromise.h"
namespace WebCore {
......@@ -56,20 +55,20 @@ CryptoAlgorithmIdentifier CryptoAlgorithmAES_CBC::identifier() const
return s_identifier;
}
void CryptoAlgorithmAES_CBC::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsage usages, std::unique_ptr<PromiseWrapper> promise, ExceptionCode&)
void CryptoAlgorithmAES_CBC::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsage usages, KeyOrKeyPairCallback callback, VoidCallback failureCallback, ExceptionCode&)
{
const CryptoAlgorithmAesKeyGenParams& aesParameters = toCryptoAlgorithmAesKeyGenParams(parameters);
RefPtr<CryptoKeyAES> result = CryptoKeyAES::generate(CryptoAlgorithmIdentifier::AES_CBC, aesParameters.length, extractable, usages);
if (!result) {
promise->reject(nullptr);
failureCallback();
return;
}
promise->fulfill(result.release());
callback(result.get(), nullptr);
}
void CryptoAlgorithmAES_CBC::importKey(const CryptoAlgorithmParameters&, const CryptoKeyData& keyData, bool extractable, CryptoKeyUsage usage, std::unique_ptr<PromiseWrapper> promise, ExceptionCode& ec)
void CryptoAlgorithmAES_CBC::importKey(const CryptoAlgorithmParameters&, const CryptoKeyData& keyData, bool extractable, CryptoKeyUsage usage, KeyCallback callback, VoidCallback, ExceptionCode& ec)
{
if (keyData.format() != CryptoKeyData::Format::OctetSequence) {
ec = NOT_SUPPORTED_ERR;
......@@ -77,7 +76,7 @@ void CryptoAlgorithmAES_CBC::importKey(const CryptoAlgorithmParameters&, const C
}
const CryptoKeyDataOctetSequence& keyDataOctetSequence = toCryptoKeyDataOctetSequence(keyData);
RefPtr<CryptoKeyAES> result = CryptoKeyAES::create(CryptoAlgorithmIdentifier::AES_CBC, keyDataOctetSequence.octetSequence(), extractable, usage);
promise->fulfill(result.release());
callback(*result);
}
}
......
......@@ -41,10 +41,10 @@ public:
virtual CryptoAlgorithmIdentifier identifier() const OVERRIDE;
virtual void encrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
virtual void decrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
virtual void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
virtual void importKey(const CryptoAlgorithmParameters&, const CryptoKeyData&, bool extractable, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
virtual void encrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
virtual void decrypt(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
virtual void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsage, KeyOrKeyPairCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
virtual void importKey(const CryptoAlgorithmParameters&, const CryptoKeyData&, bool extractable, CryptoKeyUsage, KeyCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
private:
CryptoAlgorithmAES_CBC();
......
......@@ -33,7 +33,6 @@
#include "CryptoKeyDataOctetSequence.h"
#include "CryptoKeyHMAC.h"
#include "ExceptionCode.h"
#include "JSDOMPromise.h"
namespace WebCore {
......@@ -57,20 +56,20 @@ CryptoAlgorithmIdentifier CryptoAlgorithmHMAC::identifier() const
return s_identifier;
}
void CryptoAlgorithmHMAC::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsage usages, std::unique_ptr<PromiseWrapper> promise, ExceptionCode&)
void CryptoAlgorithmHMAC::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsage usages, KeyOrKeyPairCallback callback, VoidCallback failureCallback, ExceptionCode&)
{
const CryptoAlgorithmHmacKeyParams& hmacParameters = toCryptoAlgorithmHmacKeyParams(parameters);
RefPtr<CryptoKeyHMAC> result = CryptoKeyHMAC::generate(hmacParameters.hasLength ? hmacParameters.length : 0, hmacParameters.hash, extractable, usages);
if (!result) {
promise->reject(nullptr);
failureCallback();
return;
}
promise->fulfill(result.release());
callback(result.get(), nullptr);
}
void CryptoAlgorithmHMAC::importKey(const CryptoAlgorithmParameters& parameters, const CryptoKeyData& keyData, bool extractable, CryptoKeyUsage usage, std::unique_ptr<PromiseWrapper> promise, ExceptionCode& ec)
void CryptoAlgorithmHMAC::importKey(const CryptoAlgorithmParameters& parameters, const CryptoKeyData& keyData, bool extractable, CryptoKeyUsage usage, KeyCallback callback, VoidCallback, ExceptionCode& ec)
{
if (keyData.format() != CryptoKeyData::Format::OctetSequence) {
ec = NOT_SUPPORTED_ERR;
......@@ -81,7 +80,7 @@ void CryptoAlgorithmHMAC::importKey(const CryptoAlgorithmParameters& parameters,
const CryptoAlgorithmHmacParams& hmacParameters = toCryptoAlgorithmHmacParams(parameters);
RefPtr<CryptoKeyHMAC> result = CryptoKeyHMAC::create(keyDataOctetSequence.octetSequence(), hmacParameters.hash, extractable, usage);
promise->fulfill(result.release());
callback(*result);
}
}
......
......@@ -41,10 +41,10 @@ public:
virtual CryptoAlgorithmIdentifier identifier() const OVERRIDE;
virtual void sign(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
virtual void verify(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData& signature, const CryptoOperationData& data, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
virtual void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
virtual void importKey(const CryptoAlgorithmParameters&, const CryptoKeyData&, bool extractable, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode&) OVERRIDE;
virtual void sign(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
virtual void verify(const CryptoAlgorithmParameters&, const CryptoKey&, const CryptoOperationData& signature, const CryptoOperationData& data, BoolCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
virtual void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsage, KeyOrKeyPairCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
virtual void importKey(const CryptoAlgorithmParameters&, const CryptoKeyData&, bool extractable, CryptoKeyUsage, KeyCallback, VoidCallback failureCallback, ExceptionCode&) OVERRIDE;
private:
CryptoAlgorithmHMAC();
......