Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4564)

Unified Diff: Source/modules/crypto/SubtleCrypto.cpp

Issue 141413003: [webcrypto] Match the error handling defined by the spec. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Rebase Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/modules/crypto/NormalizeAlgorithm.cpp ('k') | public/platform/WebCrypto.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/modules/crypto/SubtleCrypto.cpp
diff --git a/Source/modules/crypto/SubtleCrypto.cpp b/Source/modules/crypto/SubtleCrypto.cpp
index ef175c3eccb18a037c1f336d64242fcb94e3a1d8..60b7056e4849734e4e307952c14d4766bde69038 100644
--- a/Source/modules/crypto/SubtleCrypto.cpp
+++ b/Source/modules/crypto/SubtleCrypto.cpp
@@ -46,6 +46,18 @@ namespace WebCore {
namespace {
+bool parseAlgorithm(const Dictionary& rawAlgorithm, AlgorithmOperation operationType, blink::WebCryptoAlgorithm &algorithm, ExceptionState& exceptionState, CryptoResult* result)
+{
+ String errorDetails;
+ if (parseAlgorithm(rawAlgorithm, operationType, algorithm, errorDetails, exceptionState))
+ return true;
+
+ if (!exceptionState.hadException())
+ result->completeWithError(errorDetails);
+
+ return false;
+}
+
ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, AlgorithmOperation operationType, ArrayBufferView* signature, ArrayBufferView* dataBuffer, ExceptionState& exceptionState)
{
bool requiresKey = operationType != Digest;
@@ -65,19 +77,22 @@ ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, Alg
return ScriptPromise();
}
+ ScriptPromise promise = ScriptPromise::createPending();
+ RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
+
blink::WebCryptoAlgorithm algorithm;
- if (!normalizeAlgorithm(rawAlgorithm, operationType, algorithm, exceptionState))
- return ScriptPromise();
+ if (!parseAlgorithm(rawAlgorithm, operationType, algorithm, exceptionState, result.get()))
+ return promise;
- if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, exceptionState))
- return ScriptPromise();
+ String errorDetails;
+ if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, errorDetails)) {
+ result->completeWithError(errorDetails);
+ return promise;
+ }
const unsigned char* data = static_cast<const unsigned char*>(dataBuffer->baseAddress());
unsigned dataSize = dataBuffer->byteLength();
- ScriptPromise promise = ScriptPromise::createPending();
- RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
-
switch (operationType) {
case Encrypt:
blink::Platform::current()->crypto()->encrypt(algorithm, key->key(), data, dataSize, result->result());
@@ -140,12 +155,13 @@ ScriptPromise SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool ext
if (!Key::parseUsageMask(rawKeyUsages, keyUsages, exceptionState))
return ScriptPromise();
- blink::WebCryptoAlgorithm algorithm;
- if (!normalizeAlgorithm(rawAlgorithm, GenerateKey, algorithm, exceptionState))
- return ScriptPromise();
-
ScriptPromise promise = ScriptPromise::createPending();
RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
+
+ blink::WebCryptoAlgorithm algorithm;
+ if (!parseAlgorithm(rawAlgorithm, GenerateKey, algorithm, exceptionState, result.get()))
+ return promise;
+
blink::Platform::current()->crypto()->generateKey(algorithm, extractable, keyUsages, result->result());
return promise;
}
@@ -165,15 +181,16 @@ ScriptPromise SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView*
if (!Key::parseUsageMask(rawKeyUsages, keyUsages, exceptionState))
return ScriptPromise();
+ ScriptPromise promise = ScriptPromise::createPending();
+ RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
+
// The algorithm is optional.
blink::WebCryptoAlgorithm algorithm;
- if (!rawAlgorithm.isUndefinedOrNull() && !normalizeAlgorithm(rawAlgorithm, ImportKey, algorithm, exceptionState))
- return ScriptPromise();
+ if (!rawAlgorithm.isUndefinedOrNull() && !parseAlgorithm(rawAlgorithm, ImportKey, algorithm, exceptionState, result.get()))
+ return promise;
const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->baseAddress());
- ScriptPromise promise = ScriptPromise::createPending();
- RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
blink::Platform::current()->crypto()->importKey(format, keyDataBytes, keyData->byteLength(), algorithm, extractable, keyUsages, result->result());
return promise;
}
@@ -189,13 +206,14 @@ ScriptPromise SubtleCrypto::exportKey(const String& rawFormat, Key* key, Excepti
return ScriptPromise();
}
+ ScriptPromise promise = ScriptPromise::createPending();
+ RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
+
if (!key->extractable()) {
- exceptionState.throwDOMException(NotSupportedError, "key is not extractable");
- return ScriptPromise();
+ result->completeWithError("key is not extractable");
+ return promise;
}
- ScriptPromise promise = ScriptPromise::createPending();
- RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
blink::Platform::current()->crypto()->exportKey(format, key->key(), result->result());
return promise;
}
@@ -216,20 +234,24 @@ ScriptPromise SubtleCrypto::wrapKey(const String& rawFormat, Key* key, Key* wrap
return ScriptPromise();
}
+ ScriptPromise promise = ScriptPromise::createPending();
+ RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
+
blink::WebCryptoAlgorithm wrapAlgorithm;
- if (!normalizeAlgorithm(rawWrapAlgorithm, WrapKey, wrapAlgorithm, exceptionState))
- return ScriptPromise();
+ if (!parseAlgorithm(rawWrapAlgorithm, WrapKey, wrapAlgorithm, exceptionState, result.get()))
+ return promise;
if (!key->extractable()) {
- exceptionState.throwDOMException(NotSupportedError, "key is not extractable");
- return ScriptPromise();
+ result->completeWithError("key is not extractable");
+ return promise;
}
- if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, WrapKey, exceptionState))
- return ScriptPromise();
+ String errorDetails;
+ if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, WrapKey, errorDetails)) {
+ result->completeWithError(errorDetails);
+ return promise;
+ }
- ScriptPromise promise = ScriptPromise::createPending();
- RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
blink::Platform::current()->crypto()->wrapKey(format, key->key(), wrappingKey->key(), wrapAlgorithm, result->result());
return promise;
}
@@ -250,27 +272,31 @@ ScriptPromise SubtleCrypto::unwrapKey(const String& rawFormat, ArrayBufferView*
return ScriptPromise();
}
- blink::WebCryptoAlgorithm unwrapAlgorithm;
- if (!normalizeAlgorithm(rawUnwrapAlgorithm, UnwrapKey, unwrapAlgorithm, exceptionState))
+ blink::WebCryptoKeyUsageMask keyUsages;
+ if (!Key::parseUsageMask(rawKeyUsages, keyUsages, exceptionState))
return ScriptPromise();
+ ScriptPromise promise = ScriptPromise::createPending();
+ RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
+
+ blink::WebCryptoAlgorithm unwrapAlgorithm;
+ if (!parseAlgorithm(rawUnwrapAlgorithm, UnwrapKey, unwrapAlgorithm, exceptionState, result.get()))
+ return promise;
+
// The unwrappedKeyAlgorithm is optional.
blink::WebCryptoAlgorithm unwrappedKeyAlgorithm;
- if (!rawUnwrappedKeyAlgorithm.isUndefinedOrNull() && !normalizeAlgorithm(rawUnwrappedKeyAlgorithm, ImportKey, unwrappedKeyAlgorithm, exceptionState))
- return ScriptPromise();
-
- blink::WebCryptoKeyUsageMask keyUsages;
- if (!Key::parseUsageMask(rawKeyUsages, keyUsages, exceptionState))
- return ScriptPromise();
+ if (!rawUnwrappedKeyAlgorithm.isUndefinedOrNull() && !parseAlgorithm(rawUnwrappedKeyAlgorithm, ImportKey, unwrappedKeyAlgorithm, exceptionState, result.get()))
+ return promise;
- if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, UnwrapKey, exceptionState))
- return ScriptPromise();
+ String errorDetails;
+ if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, UnwrapKey, errorDetails)) {
+ result->completeWithError(errorDetails);
+ return promise;
+ }
const unsigned char* wrappedKeyData = static_cast<const unsigned char*>(wrappedKey->baseAddress());
unsigned wrappedKeyDataSize = wrappedKey->byteLength();
- ScriptPromise promise = ScriptPromise::createPending();
- RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
blink::Platform::current()->crypto()->unwrapKey(format, wrappedKeyData, wrappedKeyDataSize, unwrappingKey->key(), unwrapAlgorithm, unwrappedKeyAlgorithm, extractable, keyUsages, result->result());
return promise;
}
« no previous file with comments | « Source/modules/crypto/NormalizeAlgorithm.cpp ('k') | public/platform/WebCrypto.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698