#include "modules/crypto/SubtleCrypto.h"
#include "bindings/v8/Dictionary.h"
-#include "bindings/v8/ExceptionState.h"
#include "modules/crypto/CryptoResultImpl.h"
#include "modules/crypto/Key.h"
#include "modules/crypto/NormalizeAlgorithm.h"
namespace {
-bool parseAlgorithm(const Dictionary& rawAlgorithm, AlgorithmOperation operationType, blink::WebCryptoAlgorithm &algorithm, ExceptionState& exceptionState, CryptoResult* result)
+// Seems like the generated bindings should take care of these however it
+// currently doesn't. See also http://crbug.com/264520
+bool ensureNotNull(const ArrayPiece& x, const char* paramName, CryptoResult* result)
{
- if (!rawAlgorithm.isObject()) {
- exceptionState.throwTypeError("Algorithm: Not an object");
+ if (x.isNull()) {
+ String message = String("Invalid ") + paramName + String(" argument");
+ result->completeWithError(blink::WebCryptoErrorTypeType, blink::WebString(message));
return false;
}
- return parseAlgorithm(rawAlgorithm, operationType, algorithm, result);
+ return true;
}
-ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, AlgorithmOperation operationType, ArrayBufferView* signature, ArrayBufferView* dataBuffer, ExceptionState& exceptionState)
+bool ensureNotNull(Key* key, const char* paramName, CryptoResult* result)
{
- bool requiresKey = operationType != Digest;
-
- // Seems like the generated bindings should take care of these however it
- // currently doesn't. See also http://crbugh.com/264520
- if (requiresKey && !key) {
- exceptionState.throwTypeError("Invalid key argument");
- return ScriptPromise();
- }
- if (operationType == Verify && !signature) {
- exceptionState.throwTypeError("Invalid signature argument");
- return ScriptPromise();
- }
- if (!dataBuffer) {
- exceptionState.throwTypeError("Invalid dataBuffer argument");
- return ScriptPromise();
+ if (!key) {
+ String message = String("Invalid ") + paramName + String(" argument");
+ result->completeWithError(blink::WebCryptoErrorTypeType, blink::WebString(message));
+ return false;
}
+ return true;
+}
+ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, AlgorithmOperation operationType, const ArrayPiece& signature, const ArrayPiece& dataBuffer)
+{
RefPtr<CryptoResultImpl> result = CryptoResultImpl::create();
ScriptPromise promise = result->promise();
+ bool requiresKey = operationType != Digest;
+
+ if (requiresKey && !ensureNotNull(key, "key", result.get()))
+ return promise;
+ if (operationType == Verify && !ensureNotNull(signature, "signature", result.get()))
+ return promise;
+ if (!ensureNotNull(dataBuffer, "dataBuffer", result.get()))
+ return promise;
+
blink::WebCryptoAlgorithm algorithm;
- if (!parseAlgorithm(rawAlgorithm, operationType, algorithm, exceptionState, result.get()))
+ if (!parseAlgorithm(rawAlgorithm, operationType, algorithm, result.get()))
return promise;
if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, result.get()))
return promise;
- const unsigned char* data = static_cast<const unsigned char*>(dataBuffer->baseAddress());
- unsigned dataSize = dataBuffer->byteLength();
+ const unsigned char* data = dataBuffer.bytes();
+ unsigned dataSize = dataBuffer.byteLength();
switch (operationType) {
case Encrypt:
blink::Platform::current()->crypto()->sign(algorithm, key->key(), data, dataSize, result->result());
break;
case Verify:
- blink::Platform::current()->crypto()->verifySignature(algorithm, key->key(), reinterpret_cast<const unsigned char*>(signature->baseAddress()), signature->byteLength(), data, dataSize, result->result());
+ blink::Platform::current()->crypto()->verifySignature(algorithm, key->key(), signature.bytes(), signature.byteLength(), data, dataSize, result->result());
break;
case Digest:
blink::Platform::current()->crypto()->digest(algorithm, data, dataSize, result->result());
ScriptWrappable::init(this);
}
-ScriptPromise SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, Key* key, ArrayBufferView* data, ExceptionState& exceptionState)
+ScriptPromise SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& data)
{
- return startCryptoOperation(rawAlgorithm, key, Encrypt, 0, data, exceptionState);
+ return startCryptoOperation(rawAlgorithm, key, Encrypt, ArrayPiece(), data);
}
-ScriptPromise SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, Key* key, ArrayBufferView* data, ExceptionState& exceptionState)
+ScriptPromise SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& data)
{
- return startCryptoOperation(rawAlgorithm, key, Decrypt, 0, data, exceptionState);
+ return startCryptoOperation(rawAlgorithm, key, Decrypt, ArrayPiece(), data);
}
-ScriptPromise SubtleCrypto::sign(const Dictionary& rawAlgorithm, Key* key, ArrayBufferView* data, ExceptionState& exceptionState)
+ScriptPromise SubtleCrypto::sign(const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& data)
{
- return startCryptoOperation(rawAlgorithm, key, Sign, 0, data, exceptionState);
+ return startCryptoOperation(rawAlgorithm, key, Sign, ArrayPiece(), data);
}
-ScriptPromise SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, Key* key, ArrayBufferView* signature, ArrayBufferView* data, ExceptionState& exceptionState)
+ScriptPromise SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& signature, const ArrayPiece& data)
{
- return startCryptoOperation(rawAlgorithm, key, Verify, signature, data, exceptionState);
+ return startCryptoOperation(rawAlgorithm, key, Verify, signature, data);
}
-ScriptPromise SubtleCrypto::digest(const Dictionary& rawAlgorithm, ArrayBufferView* data, ExceptionState& exceptionState)
+ScriptPromise SubtleCrypto::digest(const Dictionary& rawAlgorithm, const ArrayPiece& data)
{
- return startCryptoOperation(rawAlgorithm, 0, Digest, 0, data, exceptionState);
+ return startCryptoOperation(rawAlgorithm, 0, Digest, ArrayPiece(), data);
}
-ScriptPromise SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages, ExceptionState& exceptionState)
+ScriptPromise SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages)
{
RefPtr<CryptoResultImpl> result = CryptoResultImpl::create();
ScriptPromise promise = result->promise();
return promise;
blink::WebCryptoAlgorithm algorithm;
- if (!parseAlgorithm(rawAlgorithm, GenerateKey, algorithm, exceptionState, result.get()))
+ if (!parseAlgorithm(rawAlgorithm, GenerateKey, algorithm, result.get()))
return promise;
blink::Platform::current()->crypto()->generateKey(algorithm, extractable, keyUsages, result->result());
return promise;
}
-ScriptPromise SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView* keyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages, ExceptionState& exceptionState)
+ScriptPromise SubtleCrypto::importKey(const String& rawFormat, const ArrayPiece& keyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages)
{
- if (!keyData) {
- exceptionState.throwTypeError("Invalid keyData argument");
- return ScriptPromise();
- }
-
RefPtr<CryptoResultImpl> result = CryptoResultImpl::create();
ScriptPromise promise = result->promise();
+ if (!ensureNotNull(keyData, "keyData", result.get()))
+ return promise;
+
blink::WebCryptoKeyFormat format;
if (!Key::parseFormat(rawFormat, format, result.get()))
return promise;
return promise;
blink::WebCryptoAlgorithm algorithm;
- if (!parseAlgorithm(rawAlgorithm, ImportKey, algorithm, exceptionState, result.get()))
+ if (!parseAlgorithm(rawAlgorithm, ImportKey, algorithm, result.get()))
return promise;
- const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->baseAddress());
-
- blink::Platform::current()->crypto()->importKey(format, keyDataBytes, keyData->byteLength(), algorithm, extractable, keyUsages, result->result());
+ blink::Platform::current()->crypto()->importKey(format, keyData.bytes(), keyData.byteLength(), algorithm, extractable, keyUsages, result->result());
return promise;
}
-ScriptPromise SubtleCrypto::exportKey(const String& rawFormat, Key* key, ExceptionState& exceptionState)
+ScriptPromise SubtleCrypto::exportKey(const String& rawFormat, Key* key)
{
- if (!key) {
- exceptionState.throwTypeError("Invalid key argument");
- return ScriptPromise();
- }
-
RefPtr<CryptoResultImpl> result = CryptoResultImpl::create();
ScriptPromise promise = result->promise();
+ if (!ensureNotNull(key, "key", result.get()))
+ return promise;
+
blink::WebCryptoKeyFormat format;
if (!Key::parseFormat(rawFormat, format, result.get()))
return promise;
if (!key->extractable()) {
- result->completeWithError("key is not extractable");
+ result->completeWithError(blink::WebCryptoErrorTypeInvalidAccess, "key is not extractable");
return promise;
}
return promise;
}
-ScriptPromise SubtleCrypto::wrapKey(const String& rawFormat, Key* key, Key* wrappingKey, const Dictionary& rawWrapAlgorithm, ExceptionState& exceptionState)
+ScriptPromise SubtleCrypto::wrapKey(const String& rawFormat, Key* key, Key* wrappingKey, const Dictionary& rawWrapAlgorithm)
{
- if (!key) {
- exceptionState.throwTypeError("Invalid key argument");
- return ScriptPromise();
- }
-
- if (!wrappingKey) {
- exceptionState.throwTypeError("Invalid wrappingKey argument");
- return ScriptPromise();
- }
-
RefPtr<CryptoResultImpl> result = CryptoResultImpl::create();
ScriptPromise promise = result->promise();
+ if (!ensureNotNull(key, "key", result.get()))
+ return promise;
+
+ if (!ensureNotNull(wrappingKey, "wrappingKey", result.get()))
+ return promise;
+
blink::WebCryptoKeyFormat format;
if (!Key::parseFormat(rawFormat, format, result.get()))
return promise;
blink::WebCryptoAlgorithm wrapAlgorithm;
- if (!parseAlgorithm(rawWrapAlgorithm, WrapKey, wrapAlgorithm, exceptionState, result.get()))
+ if (!parseAlgorithm(rawWrapAlgorithm, WrapKey, wrapAlgorithm, result.get()))
return promise;
if (!key->extractable()) {
- result->completeWithError("key is not extractable");
+ result->completeWithError(blink::WebCryptoErrorTypeInvalidAccess, "key is not extractable");
return promise;
}
return promise;
}
-ScriptPromise SubtleCrypto::unwrapKey(const String& rawFormat, ArrayBufferView* wrappedKey, Key* unwrappingKey, const Dictionary& rawUnwrapAlgorithm, const Dictionary& rawUnwrappedKeyAlgorithm, bool extractable, const Vector<String>& rawKeyUsages, ExceptionState& exceptionState)
+ScriptPromise SubtleCrypto::unwrapKey(const String& rawFormat, const ArrayPiece& wrappedKey, Key* unwrappingKey, const Dictionary& rawUnwrapAlgorithm, const Dictionary& rawUnwrappedKeyAlgorithm, bool extractable, const Vector<String>& rawKeyUsages)
{
- if (!wrappedKey) {
- exceptionState.throwTypeError("Invalid wrappedKey argument");
- return ScriptPromise();
- }
-
- if (!unwrappingKey) {
- exceptionState.throwTypeError("Invalid unwrappingKey argument");
- return ScriptPromise();
- }
-
RefPtr<CryptoResultImpl> result = CryptoResultImpl::create();
ScriptPromise promise = result->promise();
+ if (!ensureNotNull(wrappedKey, "wrappedKey", result.get()))
+ return promise;
+ if (!ensureNotNull(unwrappingKey, "unwrappingKey", result.get()))
+ return promise;
+
blink::WebCryptoKeyFormat format;
if (!Key::parseFormat(rawFormat, format, result.get()))
return promise;
return promise;
blink::WebCryptoAlgorithm unwrapAlgorithm;
- if (!parseAlgorithm(rawUnwrapAlgorithm, UnwrapKey, unwrapAlgorithm, exceptionState, result.get()))
+ if (!parseAlgorithm(rawUnwrapAlgorithm, UnwrapKey, unwrapAlgorithm, result.get()))
return promise;
blink::WebCryptoAlgorithm unwrappedKeyAlgorithm;
- if (!parseAlgorithm(rawUnwrappedKeyAlgorithm, ImportKey, unwrappedKeyAlgorithm, exceptionState, result.get()))
+ if (!parseAlgorithm(rawUnwrappedKeyAlgorithm, ImportKey, unwrappedKeyAlgorithm, result.get()))
return promise;
if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, UnwrapKey, result.get()))
return promise;
- const unsigned char* wrappedKeyData = static_cast<const unsigned char*>(wrappedKey->baseAddress());
- unsigned wrappedKeyDataSize = wrappedKey->byteLength();
-
- blink::Platform::current()->crypto()->unwrapKey(format, wrappedKeyData, wrappedKeyDataSize, unwrappingKey->key(), unwrapAlgorithm, unwrappedKeyAlgorithm, extractable, keyUsages, result->result());
+ blink::Platform::current()->crypto()->unwrapKey(format, wrappedKey.bytes(), wrappedKey.byteLength(), unwrappingKey->key(), unwrapAlgorithm, unwrappedKeyAlgorithm, extractable, keyUsages, result->result());
return promise;
}