Index: Source/modules/crypto/SubtleCrypto.cpp |
diff --git a/Source/modules/crypto/SubtleCrypto.cpp b/Source/modules/crypto/SubtleCrypto.cpp |
index 84e0d868b0314332d7d63a5af6907075dd50b382..e5d25857c9f307d99c3d923cbeadeefbb8593815 100644 |
--- a/Source/modules/crypto/SubtleCrypto.cpp |
+++ b/Source/modules/crypto/SubtleCrypto.cpp |
@@ -33,6 +33,7 @@ |
#include "bindings/v8/ExceptionState.h" |
#include "core/dom/ExceptionCode.h" |
+#include "core/platform/NotImplemented.h" |
#include "modules/crypto/CryptoOperation.h" |
#include "modules/crypto/Key.h" |
#include "modules/crypto/KeyOperation.h" |
@@ -49,8 +50,15 @@ namespace WebCore { |
namespace { |
-// FIXME: Temporary |
-PassRefPtr<CryptoOperation> dummyOperation(const Dictionary& rawAlgorithm, AlgorithmOperation operationType, ExceptionState& es) |
+bool keyCanBeUsedForAlgorithm(const WebKit::WebCryptoKey& key, const WebKit::WebCryptoAlgorithm& algorithm, ExceptionState& es) |
+{ |
+ // FIXME: Need to enforce that the key's algorithm matches the operation, |
+ // and that the key's usages allow it to be used with this operation. |
+ notImplemented(); |
+ return true; |
+} |
+ |
+PassRefPtr<CryptoOperation> createCryptoOperation(const Dictionary& rawAlgorithm, Key* key, AlgorithmOperation operationType, ExceptionState& es) |
{ |
WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto(); |
if (!platformCrypto) { |
@@ -62,9 +70,39 @@ PassRefPtr<CryptoOperation> dummyOperation(const Dictionary& rawAlgorithm, Algor |
if (!normalizeAlgorithm(rawAlgorithm, operationType, algorithm, es)) |
return 0; |
+ // All operations other than Digest require a valid Key. |
+ if (operationType != Digest) { |
+ if (!key) { |
+ es.throwDOMException(TypeError); |
+ return 0; |
+ } |
+ |
+ if (!keyCanBeUsedForAlgorithm(key->key(), algorithm, es)) { |
+ return 0; |
+ } |
+ } |
+ |
RefPtr<CryptoOperationImpl> opImpl = CryptoOperationImpl::create(); |
WebKit::WebCryptoOperationResult result(opImpl.get()); |
- platformCrypto->digest(algorithm, result); |
+ |
+ switch (operationType) { |
+ case Encrypt: |
+ platformCrypto->encrypt(algorithm, key->key(), result); |
+ break; |
+ case Decrypt: |
+ platformCrypto->decrypt(algorithm, key->key(), result); |
+ break; |
+ case Sign: |
+ platformCrypto->sign(algorithm, key->key(), result); |
+ break; |
+ case Digest: |
+ platformCrypto->digest(algorithm, result); |
+ break; |
+ default: |
+ ASSERT_NOT_REACHED(); |
+ return 0; |
+ } |
+ |
if (opImpl->throwInitializationError(es)) |
return 0; |
return CryptoOperation::create(algorithm, opImpl.get()); |
@@ -77,44 +115,30 @@ SubtleCrypto::SubtleCrypto() |
ScriptWrappable::init(this); |
} |
-PassRefPtr<CryptoOperation> SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, Key*, ExceptionState& es) |
+PassRefPtr<CryptoOperation> SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, Key* key, ExceptionState& es) |
{ |
- return dummyOperation(rawAlgorithm, Encrypt, es); |
+ return createCryptoOperation(rawAlgorithm, key, Encrypt, es); |
} |
-PassRefPtr<CryptoOperation> SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, Key*, ExceptionState& es) |
+PassRefPtr<CryptoOperation> SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, Key* key, ExceptionState& es) |
{ |
- return dummyOperation(rawAlgorithm, Decrypt, es); |
+ return createCryptoOperation(rawAlgorithm, key, Decrypt, es); |
} |
-PassRefPtr<CryptoOperation> SubtleCrypto::sign(const Dictionary& rawAlgorithm, Key*, ExceptionState& es) |
+PassRefPtr<CryptoOperation> SubtleCrypto::sign(const Dictionary& rawAlgorithm, Key* key, ExceptionState& es) |
{ |
- return dummyOperation(rawAlgorithm, Sign, es); |
+ return createCryptoOperation(rawAlgorithm, key, Sign, es); |
} |
-PassRefPtr<CryptoOperation> SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, Key*, ExceptionState& es) |
+PassRefPtr<CryptoOperation> SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, Key* key, ExceptionState& es) |
{ |
- return dummyOperation(rawAlgorithm, Verify, es); |
+ // FIXME |
+ return 0; |
} |
PassRefPtr<CryptoOperation> SubtleCrypto::digest(const Dictionary& rawAlgorithm, ExceptionState& es) |
{ |
- WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto(); |
- if (!platformCrypto) { |
- es.throwDOMException(NotSupportedError); |
- return 0; |
- } |
- |
- WebKit::WebCryptoAlgorithm algorithm; |
- if (!normalizeAlgorithm(rawAlgorithm, Digest, algorithm, es)) |
- return 0; |
- |
- RefPtr<CryptoOperationImpl> opImpl = CryptoOperationImpl::create(); |
- WebKit::WebCryptoOperationResult result(opImpl.get()); |
- platformCrypto->digest(algorithm, result); |
- if (opImpl->throwInitializationError(es)) |
- return 0; |
- return CryptoOperation::create(algorithm, opImpl.get()); |
+ return createCryptoOperation(rawAlgorithm, 0, Digest, es); |
} |
ScriptObject SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView* keyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages, ExceptionState& es) |