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

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

Issue 295423004: Expose WebCrypto's algorithm normalization. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Rebased, minor cleanups. Created 6 years, 7 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
Index: Source/modules/crypto/SubtleCrypto.cpp
diff --git a/Source/modules/crypto/SubtleCrypto.cpp b/Source/modules/crypto/SubtleCrypto.cpp
index 4ecb2874b99d442da1ac4945e4efa4f9f5812810..6eca26b7a84fbe9f0fd7553a72646d264a70ba88 100644
--- a/Source/modules/crypto/SubtleCrypto.cpp
+++ b/Source/modules/crypto/SubtleCrypto.cpp
@@ -38,6 +38,7 @@
#include "public/platform/Platform.h"
#include "public/platform/WebCrypto.h"
#include "public/platform/WebCryptoAlgorithm.h"
+#include "public/platform/WebCryptoAlgorithmOperation.h"
#include "wtf/ArrayBufferView.h"
namespace WebCore {
@@ -64,16 +65,25 @@ static bool ensureNotNull(Key* key, const char* paramName, CryptoResult* result)
return true;
}
-static ScriptPromise startCryptoOperation(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, AlgorithmOperation operationType, const ArrayPiece& signature, const ArrayPiece& dataBuffer)
+bool parseAlgorithm(const Dictionary& raw, blink::AlgorithmOperation op, blink::WebCryptoAlgorithm& algorithm, CryptoResult* result)
+{
+ AlgorithmError error;
+ bool success = normalizeAlgorithm(raw, op, algorithm, &error);
+ if (!success)
+ result->completeWithError(error.errorType, error.errorDetails);
+ return success;
+}
+
+static ScriptPromise startCryptoOperation(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, blink::AlgorithmOperation operationType, const ArrayPiece& signature, const ArrayPiece& dataBuffer)
{
RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState);
ScriptPromise promise = result->promise();
- bool requiresKey = operationType != Digest;
+ bool requiresKey = operationType != blink::Digest;
if (requiresKey && !ensureNotNull(key, "key", result.get()))
return promise;
- if (operationType == Verify && !ensureNotNull(signature, "signature", result.get()))
+ if (operationType == blink::Verify && !ensureNotNull(signature, "signature", result.get()))
return promise;
if (!ensureNotNull(dataBuffer, "dataBuffer", result.get()))
return promise;
@@ -89,19 +99,19 @@ static ScriptPromise startCryptoOperation(ScriptState* scriptState, const Dictio
unsigned dataSize = dataBuffer.byteLength();
switch (operationType) {
- case Encrypt:
+ case blink::Encrypt:
blink::Platform::current()->crypto()->encrypt(algorithm, key->key(), data, dataSize, result->result());
break;
- case Decrypt:
+ case blink::Decrypt:
blink::Platform::current()->crypto()->decrypt(algorithm, key->key(), data, dataSize, result->result());
break;
- case Sign:
+ case blink::Sign:
blink::Platform::current()->crypto()->sign(algorithm, key->key(), data, dataSize, result->result());
break;
- case Verify:
+ case blink::Verify:
blink::Platform::current()->crypto()->verifySignature(algorithm, key->key(), signature.bytes(), signature.byteLength(), data, dataSize, result->result());
break;
- case Digest:
+ case blink::Digest:
blink::Platform::current()->crypto()->digest(algorithm, data, dataSize, result->result());
break;
default:
@@ -119,27 +129,27 @@ SubtleCrypto::SubtleCrypto()
ScriptPromise SubtleCrypto::encrypt(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& data)
{
- return startCryptoOperation(scriptState, rawAlgorithm, key, Encrypt, ArrayPiece(), data);
+ return startCryptoOperation(scriptState, rawAlgorithm, key, blink::Encrypt, ArrayPiece(), data);
}
ScriptPromise SubtleCrypto::decrypt(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& data)
{
- return startCryptoOperation(scriptState, rawAlgorithm, key, Decrypt, ArrayPiece(), data);
+ return startCryptoOperation(scriptState, rawAlgorithm, key, blink::Decrypt, ArrayPiece(), data);
}
ScriptPromise SubtleCrypto::sign(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& data)
{
- return startCryptoOperation(scriptState, rawAlgorithm, key, Sign, ArrayPiece(), data);
+ return startCryptoOperation(scriptState, rawAlgorithm, key, blink::Sign, ArrayPiece(), data);
}
ScriptPromise SubtleCrypto::verifySignature(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& signature, const ArrayPiece& data)
{
- return startCryptoOperation(scriptState, rawAlgorithm, key, Verify, signature, data);
+ return startCryptoOperation(scriptState, rawAlgorithm, key, blink::Verify, signature, data);
}
ScriptPromise SubtleCrypto::digest(ScriptState* scriptState, const Dictionary& rawAlgorithm, const ArrayPiece& data)
{
- return startCryptoOperation(scriptState, rawAlgorithm, 0, Digest, ArrayPiece(), data);
+ return startCryptoOperation(scriptState, rawAlgorithm, 0, blink::Digest, ArrayPiece(), data);
}
ScriptPromise SubtleCrypto::generateKey(ScriptState* scriptState, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages)
@@ -152,7 +162,7 @@ ScriptPromise SubtleCrypto::generateKey(ScriptState* scriptState, const Dictiona
return promise;
blink::WebCryptoAlgorithm algorithm;
- if (!parseAlgorithm(rawAlgorithm, GenerateKey, algorithm, result.get()))
+ if (!parseAlgorithm(rawAlgorithm, blink::GenerateKey, algorithm, result.get()))
return promise;
blink::Platform::current()->crypto()->generateKey(algorithm, extractable, keyUsages, result->result());
@@ -176,7 +186,7 @@ ScriptPromise SubtleCrypto::importKey(ScriptState* scriptState, const String& ra
return promise;
blink::WebCryptoAlgorithm algorithm;
- if (!parseAlgorithm(rawAlgorithm, ImportKey, algorithm, result.get()))
+ if (!parseAlgorithm(rawAlgorithm, blink::ImportKey, algorithm, result.get()))
return promise;
blink::Platform::current()->crypto()->importKey(format, keyData.bytes(), keyData.byteLength(), algorithm, extractable, keyUsages, result->result());
@@ -220,7 +230,7 @@ ScriptPromise SubtleCrypto::wrapKey(ScriptState* scriptState, const String& rawF
return promise;
blink::WebCryptoAlgorithm wrapAlgorithm;
- if (!parseAlgorithm(rawWrapAlgorithm, WrapKey, wrapAlgorithm, result.get()))
+ if (!parseAlgorithm(rawWrapAlgorithm, blink::WrapKey, wrapAlgorithm, result.get()))
return promise;
if (!key->extractable()) {
@@ -228,7 +238,7 @@ ScriptPromise SubtleCrypto::wrapKey(ScriptState* scriptState, const String& rawF
return promise;
}
- if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, WrapKey, result.get()))
+ if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, blink::WrapKey, result.get()))
return promise;
blink::Platform::current()->crypto()->wrapKey(format, key->key(), wrappingKey->key(), wrapAlgorithm, result->result());
@@ -254,14 +264,14 @@ ScriptPromise SubtleCrypto::unwrapKey(ScriptState* scriptState, const String& ra
return promise;
blink::WebCryptoAlgorithm unwrapAlgorithm;
- if (!parseAlgorithm(rawUnwrapAlgorithm, UnwrapKey, unwrapAlgorithm, result.get()))
+ if (!parseAlgorithm(rawUnwrapAlgorithm, blink::UnwrapKey, unwrapAlgorithm, result.get()))
return promise;
blink::WebCryptoAlgorithm unwrappedKeyAlgorithm;
- if (!parseAlgorithm(rawUnwrappedKeyAlgorithm, ImportKey, unwrappedKeyAlgorithm, result.get()))
+ if (!parseAlgorithm(rawUnwrappedKeyAlgorithm, blink::ImportKey, unwrappedKeyAlgorithm, result.get()))
return promise;
- if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, UnwrapKey, result.get()))
+ if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, blink::UnwrapKey, result.get()))
return promise;
blink::Platform::current()->crypto()->unwrapKey(format, wrappedKey.bytes(), wrappedKey.byteLength(), unwrappingKey->key(), unwrapAlgorithm, unwrappedKeyAlgorithm, extractable, keyUsages, result->result());

Powered by Google App Engine
This is Rietveld 408576698