Commit d4745597 authored by pneubeck@chromium.org's avatar pneubeck@chromium.org

Expose WebCrypto's algorithm normalization.

This exposes the WebCore::parseAlgorithm function through  blink::WebScriptBindings.

This will allow reusing the normalization in the implementation of the Chrome extension API enterprise.platformKeys.

To keep the exposure of types minimal, the dependency of parseAlgorithm on CryptoResult was removed.
On the way, renames parseAlgorithm to normalizeAlgorithm in order to match the WebCrypto spec and the filename.

BUG=364435

Review URL: https://codereview.chromium.org/295423004

git-svn-id: svn://svn.chromium.org/blink/trunk@175657 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent a15c15c6
...@@ -47,9 +47,7 @@ ...@@ -47,9 +47,7 @@
namespace WebCore { namespace WebCore {
namespace { ExceptionCode webCryptoErrorToExceptionCode(blink::WebCryptoErrorType errorType)
ExceptionCode toExceptionCode(blink::WebCryptoErrorType errorType)
{ {
switch (errorType) { switch (errorType) {
case blink::WebCryptoErrorTypeNotSupported: case blink::WebCryptoErrorTypeNotSupported:
...@@ -77,8 +75,6 @@ ExceptionCode toExceptionCode(blink::WebCryptoErrorType errorType) ...@@ -77,8 +75,6 @@ ExceptionCode toExceptionCode(blink::WebCryptoErrorType errorType)
return 0; return 0;
} }
} // namespace
// The PromiseState class contains all the state which is tied to an // The PromiseState class contains all the state which is tied to an
// ExecutionContext. Whereas CryptoResultImpl can be deleted from any thread, // ExecutionContext. Whereas CryptoResultImpl can be deleted from any thread,
// PromiseState is not thread safe and must only be accessed and deleted from // PromiseState is not thread safe and must only be accessed and deleted from
...@@ -109,7 +105,7 @@ public: ...@@ -109,7 +105,7 @@ public:
void completeWithError(blink::WebCryptoErrorType errorType, const blink::WebString& errorDetails) void completeWithError(blink::WebCryptoErrorType errorType, const blink::WebString& errorDetails)
{ {
m_promiseResolver->reject(DOMException::create(toExceptionCode(errorType), errorDetails)); m_promiseResolver->reject(DOMException::create(webCryptoErrorToExceptionCode(errorType), errorDetails));
delete this; delete this;
} }
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#define CryptoResultImpl_h #define CryptoResultImpl_h
#include "bindings/v8/ScriptPromise.h" #include "bindings/v8/ScriptPromise.h"
#include "core/dom/ExceptionCode.h"
#include "platform/CryptoResult.h" #include "platform/CryptoResult.h"
#include "public/platform/WebCrypto.h" #include "public/platform/WebCrypto.h"
#include "wtf/Forward.h" #include "wtf/Forward.h"
...@@ -39,6 +40,8 @@ ...@@ -39,6 +40,8 @@
namespace WebCore { namespace WebCore {
ExceptionCode webCryptoErrorToExceptionCode(blink::WebCryptoErrorType);
// Wrapper around a Promise to notify completion of the crypto operation. // Wrapper around a Promise to notify completion of the crypto operation.
// //
// The thread on which CryptoResultImpl was created on is referred to as the // The thread on which CryptoResultImpl was created on is referred to as the
......
...@@ -95,28 +95,28 @@ blink::WebCryptoKeyUsageMask keyUsageStringToMask(const String& usageString) ...@@ -95,28 +95,28 @@ blink::WebCryptoKeyUsageMask keyUsageStringToMask(const String& usageString)
return 0; return 0;
} }
blink::WebCryptoKeyUsageMask toKeyUsage(AlgorithmOperation operation) blink::WebCryptoKeyUsageMask toKeyUsage(blink::WebCryptoOperation operation)
{ {
switch (operation) { switch (operation) {
case Encrypt: case blink::WebCryptoOperationEncrypt:
return blink::WebCryptoKeyUsageEncrypt; return blink::WebCryptoKeyUsageEncrypt;
case Decrypt: case blink::WebCryptoOperationDecrypt:
return blink::WebCryptoKeyUsageDecrypt; return blink::WebCryptoKeyUsageDecrypt;
case Sign: case blink::WebCryptoOperationSign:
return blink::WebCryptoKeyUsageSign; return blink::WebCryptoKeyUsageSign;
case Verify: case blink::WebCryptoOperationVerify:
return blink::WebCryptoKeyUsageVerify; return blink::WebCryptoKeyUsageVerify;
case DeriveKey: case blink::WebCryptoOperationDeriveKey:
return blink::WebCryptoKeyUsageDeriveKey; return blink::WebCryptoKeyUsageDeriveKey;
case DeriveBits: case blink::WebCryptoOperationDeriveBits:
return blink::WebCryptoKeyUsageDeriveBits; return blink::WebCryptoKeyUsageDeriveBits;
case WrapKey: case blink::WebCryptoOperationWrapKey:
return blink::WebCryptoKeyUsageWrapKey; return blink::WebCryptoKeyUsageWrapKey;
case UnwrapKey: case blink::WebCryptoOperationUnwrapKey:
return blink::WebCryptoKeyUsageUnwrapKey; return blink::WebCryptoKeyUsageUnwrapKey;
case Digest: case blink::WebCryptoOperationDigest:
case GenerateKey: case blink::WebCryptoOperationGenerateKey:
case ImportKey: case blink::WebCryptoOperationImportKey:
break; break;
} }
...@@ -168,7 +168,7 @@ Vector<String> Key::usages() const ...@@ -168,7 +168,7 @@ Vector<String> Key::usages() const
return result; return result;
} }
bool Key::canBeUsedForAlgorithm(const blink::WebCryptoAlgorithm& algorithm, AlgorithmOperation op, CryptoResult* result) const bool Key::canBeUsedForAlgorithm(const blink::WebCryptoAlgorithm& algorithm, blink::WebCryptoOperation op, CryptoResult* result) const
{ {
if (!(m_key.usages() & toKeyUsage(op))) { if (!(m_key.usages() & toKeyUsage(op))) {
result->completeWithError(blink::WebCryptoErrorTypeInvalidAccess, "key.usages does not permit this operation"); result->completeWithError(blink::WebCryptoErrorTypeInvalidAccess, "key.usages does not permit this operation");
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include "bindings/v8/ScriptWrappable.h" #include "bindings/v8/ScriptWrappable.h"
#include "modules/crypto/NormalizeAlgorithm.h" #include "modules/crypto/NormalizeAlgorithm.h"
#include "platform/heap/Handle.h" #include "platform/heap/Handle.h"
#include "public/platform/WebCryptoAlgorithm.h"
#include "public/platform/WebCryptoKey.h" #include "public/platform/WebCryptoKey.h"
#include "wtf/Forward.h" #include "wtf/Forward.h"
#include "wtf/RefCounted.h" #include "wtf/RefCounted.h"
...@@ -62,7 +63,7 @@ public: ...@@ -62,7 +63,7 @@ public:
// If the key cannot be used with the indicated algorithm, returns false // If the key cannot be used with the indicated algorithm, returns false
// and completes the CryptoResult with an error. // and completes the CryptoResult with an error.
bool canBeUsedForAlgorithm(const blink::WebCryptoAlgorithm&, AlgorithmOperation, CryptoResult*) const; bool canBeUsedForAlgorithm(const blink::WebCryptoAlgorithm&, blink::WebCryptoOperation, CryptoResult*) const;
// On failure, these return false and complete the CryptoResult with an error. // On failure, these return false and complete the CryptoResult with an error.
static bool parseFormat(const String&, blink::WebCryptoKeyFormat&, CryptoResult*); static bool parseFormat(const String&, blink::WebCryptoKeyFormat&, CryptoResult*);
......
...@@ -31,7 +31,9 @@ ...@@ -31,7 +31,9 @@
#ifndef NormalizeAlgorithm_h #ifndef NormalizeAlgorithm_h
#define NormalizeAlgorithm_h #define NormalizeAlgorithm_h
#include "public/platform/WebCrypto.h"
#include "public/platform/WebCryptoAlgorithm.h" #include "public/platform/WebCryptoAlgorithm.h"
#include "public/platform/WebString.h"
#include "wtf/Assertions.h" #include "wtf/Assertions.h"
#include "wtf/Forward.h" #include "wtf/Forward.h"
...@@ -39,23 +41,11 @@ namespace blink { class WebCryptoAlgorithm; } ...@@ -39,23 +41,11 @@ namespace blink { class WebCryptoAlgorithm; }
namespace WebCore { namespace WebCore {
class CryptoResult;
class Dictionary; class Dictionary;
enum AlgorithmOperation { struct AlgorithmError {
Encrypt, blink::WebCryptoErrorType errorType;
Decrypt, blink::WebString errorDetails;
Sign,
Verify,
Digest,
GenerateKey,
ImportKey,
DeriveKey,
DeriveBits,
WrapKey,
UnwrapKey,
// <---- End of list (keep this up-to-date)
LastAlgorithmOperation = UnwrapKey,
}; };
// Converts a javascript Dictionary to a WebCryptoAlgorithm object. // Converts a javascript Dictionary to a WebCryptoAlgorithm object.
...@@ -65,11 +55,11 @@ enum AlgorithmOperation { ...@@ -65,11 +55,11 @@ enum AlgorithmOperation {
// //
// On success returns true and sets the WebCryptoAlgorithm. // On success returns true and sets the WebCryptoAlgorithm.
// //
// On failure parseAlgorithm returns false and completes the CryptoResult // On failure normalizeAlgorithm returns false and sets the AlgorithmError with
// with a (non-localized) debug string. // a error type and a (non-localized) debug string.
// //
// [1] http://www.w3.org/TR/WebCryptoAPI/#algorithm-normalizing-rules // [1] http://www.w3.org/TR/WebCryptoAPI/#algorithm-normalizing-rules
bool parseAlgorithm(const Dictionary&, AlgorithmOperation, blink::WebCryptoAlgorithm&, CryptoResult*) WARN_UNUSED_RETURN; bool normalizeAlgorithm(const Dictionary&, blink::WebCryptoOperation, blink::WebCryptoAlgorithm&, AlgorithmError*) WARN_UNUSED_RETURN;
// Returns a null-terminated C-string literal. Caller can assume the pointer // Returns a null-terminated C-string literal. Caller can assume the pointer
// will be valid for the program's entire runtime. // will be valid for the program's entire runtime.
......
...@@ -64,16 +64,25 @@ static bool ensureNotNull(Key* key, const char* paramName, CryptoResult* result) ...@@ -64,16 +64,25 @@ static bool ensureNotNull(Key* key, const char* paramName, CryptoResult* result)
return true; 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::WebCryptoOperation 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::WebCryptoOperation operationType, const ArrayPiece& signature, const ArrayPiece& dataBuffer)
{ {
RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState);
ScriptPromise promise = result->promise(); ScriptPromise promise = result->promise();
bool requiresKey = operationType != Digest; bool requiresKey = operationType != blink::WebCryptoOperationDigest;
if (requiresKey && !ensureNotNull(key, "key", result.get())) if (requiresKey && !ensureNotNull(key, "key", result.get()))
return promise; return promise;
if (operationType == Verify && !ensureNotNull(signature, "signature", result.get())) if (operationType == blink::WebCryptoOperationVerify && !ensureNotNull(signature, "signature", result.get()))
return promise; return promise;
if (!ensureNotNull(dataBuffer, "dataBuffer", result.get())) if (!ensureNotNull(dataBuffer, "dataBuffer", result.get()))
return promise; return promise;
...@@ -89,19 +98,19 @@ static ScriptPromise startCryptoOperation(ScriptState* scriptState, const Dictio ...@@ -89,19 +98,19 @@ static ScriptPromise startCryptoOperation(ScriptState* scriptState, const Dictio
unsigned dataSize = dataBuffer.byteLength(); unsigned dataSize = dataBuffer.byteLength();
switch (operationType) { switch (operationType) {
case Encrypt: case blink::WebCryptoOperationEncrypt:
blink::Platform::current()->crypto()->encrypt(algorithm, key->key(), data, dataSize, result->result()); blink::Platform::current()->crypto()->encrypt(algorithm, key->key(), data, dataSize, result->result());
break; break;
case Decrypt: case blink::WebCryptoOperationDecrypt:
blink::Platform::current()->crypto()->decrypt(algorithm, key->key(), data, dataSize, result->result()); blink::Platform::current()->crypto()->decrypt(algorithm, key->key(), data, dataSize, result->result());
break; break;
case Sign: case blink::WebCryptoOperationSign:
blink::Platform::current()->crypto()->sign(algorithm, key->key(), data, dataSize, result->result()); blink::Platform::current()->crypto()->sign(algorithm, key->key(), data, dataSize, result->result());
break; break;
case Verify: case blink::WebCryptoOperationVerify:
blink::Platform::current()->crypto()->verifySignature(algorithm, key->key(), signature.bytes(), signature.byteLength(), data, dataSize, result->result()); blink::Platform::current()->crypto()->verifySignature(algorithm, key->key(), signature.bytes(), signature.byteLength(), data, dataSize, result->result());
break; break;
case Digest: case blink::WebCryptoOperationDigest:
blink::Platform::current()->crypto()->digest(algorithm, data, dataSize, result->result()); blink::Platform::current()->crypto()->digest(algorithm, data, dataSize, result->result());
break; break;
default: default:
...@@ -119,27 +128,27 @@ SubtleCrypto::SubtleCrypto() ...@@ -119,27 +128,27 @@ SubtleCrypto::SubtleCrypto()
ScriptPromise SubtleCrypto::encrypt(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& data) 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::WebCryptoOperationEncrypt, ArrayPiece(), data);
} }
ScriptPromise SubtleCrypto::decrypt(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, const 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::WebCryptoOperationDecrypt, ArrayPiece(), data);
} }
ScriptPromise SubtleCrypto::sign(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, const 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::WebCryptoOperationSign, ArrayPiece(), data);
} }
ScriptPromise SubtleCrypto::verifySignature(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& signature, const 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::WebCryptoOperationVerify, signature, data);
} }
ScriptPromise SubtleCrypto::digest(ScriptState* scriptState, const Dictionary& rawAlgorithm, const ArrayPiece& 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::WebCryptoOperationDigest, ArrayPiece(), data);
} }
ScriptPromise SubtleCrypto::generateKey(ScriptState* scriptState, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages) ScriptPromise SubtleCrypto::generateKey(ScriptState* scriptState, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages)
...@@ -152,7 +161,7 @@ ScriptPromise SubtleCrypto::generateKey(ScriptState* scriptState, const Dictiona ...@@ -152,7 +161,7 @@ ScriptPromise SubtleCrypto::generateKey(ScriptState* scriptState, const Dictiona
return promise; return promise;
blink::WebCryptoAlgorithm algorithm; blink::WebCryptoAlgorithm algorithm;
if (!parseAlgorithm(rawAlgorithm, GenerateKey, algorithm, result.get())) if (!parseAlgorithm(rawAlgorithm, blink::WebCryptoOperationGenerateKey, algorithm, result.get()))
return promise; return promise;
blink::Platform::current()->crypto()->generateKey(algorithm, extractable, keyUsages, result->result()); blink::Platform::current()->crypto()->generateKey(algorithm, extractable, keyUsages, result->result());
...@@ -176,7 +185,7 @@ ScriptPromise SubtleCrypto::importKey(ScriptState* scriptState, const String& ra ...@@ -176,7 +185,7 @@ ScriptPromise SubtleCrypto::importKey(ScriptState* scriptState, const String& ra
return promise; return promise;
blink::WebCryptoAlgorithm algorithm; blink::WebCryptoAlgorithm algorithm;
if (!parseAlgorithm(rawAlgorithm, ImportKey, algorithm, result.get())) if (!parseAlgorithm(rawAlgorithm, blink::WebCryptoOperationImportKey, algorithm, result.get()))
return promise; return promise;
blink::Platform::current()->crypto()->importKey(format, keyData.bytes(), keyData.byteLength(), algorithm, extractable, keyUsages, result->result()); blink::Platform::current()->crypto()->importKey(format, keyData.bytes(), keyData.byteLength(), algorithm, extractable, keyUsages, result->result());
...@@ -220,7 +229,7 @@ ScriptPromise SubtleCrypto::wrapKey(ScriptState* scriptState, const String& rawF ...@@ -220,7 +229,7 @@ ScriptPromise SubtleCrypto::wrapKey(ScriptState* scriptState, const String& rawF
return promise; return promise;
blink::WebCryptoAlgorithm wrapAlgorithm; blink::WebCryptoAlgorithm wrapAlgorithm;
if (!parseAlgorithm(rawWrapAlgorithm, WrapKey, wrapAlgorithm, result.get())) if (!parseAlgorithm(rawWrapAlgorithm, blink::WebCryptoOperationWrapKey, wrapAlgorithm, result.get()))
return promise; return promise;
if (!key->extractable()) { if (!key->extractable()) {
...@@ -228,7 +237,7 @@ ScriptPromise SubtleCrypto::wrapKey(ScriptState* scriptState, const String& rawF ...@@ -228,7 +237,7 @@ ScriptPromise SubtleCrypto::wrapKey(ScriptState* scriptState, const String& rawF
return promise; return promise;
} }
if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, WrapKey, result.get())) if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, blink::WebCryptoOperationWrapKey, result.get()))
return promise; return promise;
blink::Platform::current()->crypto()->wrapKey(format, key->key(), wrappingKey->key(), wrapAlgorithm, result->result()); blink::Platform::current()->crypto()->wrapKey(format, key->key(), wrappingKey->key(), wrapAlgorithm, result->result());
...@@ -254,14 +263,14 @@ ScriptPromise SubtleCrypto::unwrapKey(ScriptState* scriptState, const String& ra ...@@ -254,14 +263,14 @@ ScriptPromise SubtleCrypto::unwrapKey(ScriptState* scriptState, const String& ra
return promise; return promise;
blink::WebCryptoAlgorithm unwrapAlgorithm; blink::WebCryptoAlgorithm unwrapAlgorithm;
if (!parseAlgorithm(rawUnwrapAlgorithm, UnwrapKey, unwrapAlgorithm, result.get())) if (!parseAlgorithm(rawUnwrapAlgorithm, blink::WebCryptoOperationUnwrapKey, unwrapAlgorithm, result.get()))
return promise; return promise;
blink::WebCryptoAlgorithm unwrappedKeyAlgorithm; blink::WebCryptoAlgorithm unwrappedKeyAlgorithm;
if (!parseAlgorithm(rawUnwrappedKeyAlgorithm, ImportKey, unwrappedKeyAlgorithm, result.get())) if (!parseAlgorithm(rawUnwrappedKeyAlgorithm, blink::WebCryptoOperationImportKey, unwrappedKeyAlgorithm, result.get()))
return promise; return promise;
if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, UnwrapKey, result.get())) if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, blink::WebCryptoOperationUnwrapKey, result.get()))
return promise; return promise;
blink::Platform::current()->crypto()->unwrapKey(format, wrappedKey.bytes(), wrappedKey.byteLength(), 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());
......
/*
* Copyright (C) 2013 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "public/web/WebCryptoNormalize.h"
#include "bindings/v8/Dictionary.h"
#include "modules/crypto/CryptoResultImpl.h"
#include "modules/crypto/NormalizeAlgorithm.h"
#include "platform/CryptoResult.h"
#include "public/platform/WebString.h"
#include <v8.h>
using namespace WebCore;
namespace blink {
WebCryptoAlgorithm normalizeCryptoAlgorithm(v8::Handle<v8::Object> algorithmObject, WebCryptoOperation operation, int* exceptionCode, WebString* errorDetails, v8::Isolate* isolate)
{
WebCore::Dictionary algorithmDictionary(algorithmObject, isolate);
if (!algorithmDictionary.isUndefinedOrNull() && !algorithmDictionary.isObject())
return WebCryptoAlgorithm();
WebCryptoAlgorithm algorithm;
WebCore::AlgorithmError error;
if (!normalizeAlgorithm(algorithmDictionary, operation, algorithm, &error)) {
*exceptionCode = WebCore::webCryptoErrorToExceptionCode(error.errorType);
*errorDetails = error.errorDetails;
return WebCryptoAlgorithm();
}
return algorithm;
}
} // namespace blink
...@@ -112,6 +112,7 @@ ...@@ -112,6 +112,7 @@
'WebCachedURLRequest.cpp', 'WebCachedURLRequest.cpp',
'WebColorName.cpp', 'WebColorName.cpp',
'WebColorSuggestion.cpp', 'WebColorSuggestion.cpp',
'WebCryptoNormalize.cpp',
'WebCustomElement.cpp', 'WebCustomElement.cpp',
'WebDOMActivityLogger.cpp', 'WebDOMActivityLogger.cpp',
'WebDOMCustomEvent.cpp', 'WebDOMCustomEvent.cpp',
......
...@@ -40,6 +40,21 @@ ...@@ -40,6 +40,21 @@
namespace blink { namespace blink {
enum WebCryptoOperation {
WebCryptoOperationEncrypt,
WebCryptoOperationDecrypt,
WebCryptoOperationSign,
WebCryptoOperationVerify,
WebCryptoOperationDigest,
WebCryptoOperationGenerateKey,
WebCryptoOperationImportKey,
WebCryptoOperationDeriveKey,
WebCryptoOperationDeriveBits,
WebCryptoOperationWrapKey,
WebCryptoOperationUnwrapKey,
WebCryptoOperationLast = WebCryptoOperationUnwrapKey,
};
enum WebCryptoAlgorithmId { enum WebCryptoAlgorithmId {
WebCryptoAlgorithmIdAesCbc, WebCryptoAlgorithmIdAesCbc,
WebCryptoAlgorithmIdHmac, WebCryptoAlgorithmIdHmac,
......
/*
* Copyright (C) 2014 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef WebCryptoNormalize_h
#define WebCryptoNormalize_h
#include "../platform/WebCommon.h"
#include "../platform/WebCryptoAlgorithm.h"
namespace v8 {
class Isolate;
class Object;
template <class T> class Handle;
}
namespace blink {
class WebString;
// Converts a javascript Dictionary to a WebCryptoAlgorithm object.
//
// This corresponds with "normalizing" [1] the algorithm, and then validating
// the expected parameters for the algorithm/operation combination.
//
// On failure returns an null WebCryptoAlgorithm, sets the int to the
// ExceptionCode and the WebString to a (non-localized) debug string.
//
// [1] http://www.w3.org/TR/WebCryptoAPI/#algorithm-normalizing-rules
BLINK_EXPORT WebCryptoAlgorithm normalizeCryptoAlgorithm(v8::Handle<v8::Object>, WebCryptoOperation, int* exceptionCode, WebString* errorDetails, v8::Isolate*);
} // namespace blink
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment