Commit 15170bb8 authored by pneubeck@chromium.org's avatar pneubeck@chromium.org

Expose WebCrypto's lookupAlgorithmInfo.

Will allow reuse of this function in the implementation of the enterprise.platfromKeys Chrome extension API.

BUG=364435

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

git-svn-id: svn://svn.chromium.org/blink/trunk@176190 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 063b2a76
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include "modules/crypto/NormalizeAlgorithm.h" #include "modules/crypto/NormalizeAlgorithm.h"
#include "modules/crypto/RsaHashedKeyAlgorithm.h" #include "modules/crypto/RsaHashedKeyAlgorithm.h"
#include "modules/crypto/RsaKeyAlgorithm.h" #include "modules/crypto/RsaKeyAlgorithm.h"
#include "public/platform/WebCryptoAlgorithm.h"
#include "wtf/text/WTFString.h" #include "wtf/text/WTFString.h"
namespace WebCore { namespace WebCore {
...@@ -75,7 +76,8 @@ KeyAlgorithm::KeyAlgorithm(const blink::WebCryptoKeyAlgorithm& algorithm) ...@@ -75,7 +76,8 @@ KeyAlgorithm::KeyAlgorithm(const blink::WebCryptoKeyAlgorithm& algorithm)
String KeyAlgorithm::name() String KeyAlgorithm::name()
{ {
return algorithmIdToName(m_algorithm.id()); const blink::WebCryptoAlgorithmInfo* info = blink::WebCryptoAlgorithm::lookupAlgorithmInfo(m_algorithm.id());
return info->name;
} }
bool KeyAlgorithm::isAesKeyAlgorithm() const bool KeyAlgorithm::isAesKeyAlgorithm() const
......
...@@ -75,194 +75,6 @@ const AlgorithmNameMapping algorithmNameMappings[] = { ...@@ -75,194 +75,6 @@ const AlgorithmNameMapping algorithmNameMappings[] = {
{"RSASSA-PKCS1-V1_5", 17, blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5}, {"RSASSA-PKCS1-V1_5", 17, blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5},
}; };
typedef char ParamsTypeOrUndefined;
const ParamsTypeOrUndefined Undefined = -1;
struct AlgorithmInfo {
// The canonical (case-sensitive) name for the algorithm.
const char* name;
// A map from the operation to the expected parameter type of the algorithm.
// If an operation is not applicable for the algorithm, set to Undefined.
const ParamsTypeOrUndefined operationToParamsType[blink::WebCryptoOperationLast + 1];
};
// A mapping from the algorithm ID to information about the algorithm.
const AlgorithmInfo algorithmIdToInfo[] = {
{ // Index 0
"AES-CBC", {
blink::WebCryptoAlgorithmParamsTypeAesCbcParams, // Encrypt
blink::WebCryptoAlgorithmParamsTypeAesCbcParams, // Decrypt
Undefined, // Sign
Undefined, // Verify
Undefined, // Digest
blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
Undefined, // DeriveKey
Undefined, // DeriveBits
blink::WebCryptoAlgorithmParamsTypeAesCbcParams, // WrapKey
blink::WebCryptoAlgorithmParamsTypeAesCbcParams // UnwrapKey
}
}, { // Index 1
"HMAC", {
Undefined, // Encrypt
Undefined, // Decrypt
blink::WebCryptoAlgorithmParamsTypeNone, // Sign
blink::WebCryptoAlgorithmParamsTypeNone, // Verify
Undefined, // Digest
blink::WebCryptoAlgorithmParamsTypeHmacKeyGenParams, // GenerateKey
blink::WebCryptoAlgorithmParamsTypeHmacImportParams, // ImportKey
Undefined, // DeriveKey
Undefined, // DeriveBits
Undefined, // WrapKey
Undefined // UnwrapKey
}
}, { // Index 2
"RSASSA-PKCS1-v1_5", {
Undefined, // Encrypt
Undefined, // Decrypt
blink::WebCryptoAlgorithmParamsTypeNone, // Sign
blink::WebCryptoAlgorithmParamsTypeNone, // Verify
Undefined, // Digest
blink::WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams, // GenerateKey
blink::WebCryptoAlgorithmParamsTypeRsaHashedImportParams, // ImportKey
Undefined, // DeriveKey
Undefined, // DeriveBits
Undefined, // WrapKey
Undefined // UnwrapKey
}
}, { // Index 3
"SHA-1", {
Undefined, // Encrypt
Undefined, // Decrypt
Undefined, // Sign
Undefined, // Verify
blink::WebCryptoAlgorithmParamsTypeNone, // Digest
Undefined, // GenerateKey
Undefined, // ImportKey
Undefined, // DeriveKey
Undefined, // DeriveBits
Undefined, // WrapKey
Undefined // UnwrapKey
}
}, { // Index 4
"SHA-256", {
Undefined, // Encrypt
Undefined, // Decrypt
Undefined, // Sign
Undefined, // Verify
blink::WebCryptoAlgorithmParamsTypeNone, // Digest
Undefined, // GenerateKey
Undefined, // ImportKey
Undefined, // DeriveKey
Undefined, // DeriveBits
Undefined, // WrapKey
Undefined // UnwrapKey
}
}, { // Index 5
"SHA-384", {
Undefined, // Encrypt
Undefined, // Decrypt
Undefined, // Sign
Undefined, // Verify
blink::WebCryptoAlgorithmParamsTypeNone, // Digest
Undefined, // GenerateKey
Undefined, // ImportKey
Undefined, // DeriveKey
Undefined, // DeriveBits
Undefined, // WrapKey
Undefined // UnwrapKey
}
}, { // Index 6
"SHA-512", {
Undefined, // Encrypt
Undefined, // Decrypt
Undefined, // Sign
Undefined, // Verify
blink::WebCryptoAlgorithmParamsTypeNone, // Digest
Undefined, // GenerateKey
Undefined, // ImportKey
Undefined, // DeriveKey
Undefined, // DeriveBits
Undefined, // WrapKey
Undefined // UnwrapKey
}
}, { // Index 7
"AES-GCM", {
blink::WebCryptoAlgorithmParamsTypeAesGcmParams, // Encrypt
blink::WebCryptoAlgorithmParamsTypeAesGcmParams, // Decrypt
Undefined, // Sign
Undefined, // Verify
Undefined, // Digest
blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
Undefined, // DeriveKey
Undefined, // DeriveBits
blink::WebCryptoAlgorithmParamsTypeAesGcmParams, // WrapKey
blink::WebCryptoAlgorithmParamsTypeAesGcmParams // UnwrapKey
}
}, { // Index 8
"RSA-OAEP", {
blink::WebCryptoAlgorithmParamsTypeRsaOaepParams, // Encrypt
blink::WebCryptoAlgorithmParamsTypeRsaOaepParams, // Decrypt
Undefined, // Sign
Undefined, // Verify
Undefined, // Digest
blink::WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams, // GenerateKey
blink::WebCryptoAlgorithmParamsTypeRsaHashedImportParams, // ImportKey
Undefined, // DeriveKey
Undefined, // DeriveBits
blink::WebCryptoAlgorithmParamsTypeRsaOaepParams, // WrapKey
blink::WebCryptoAlgorithmParamsTypeRsaOaepParams // UnwrapKey
}
}, { // Index 9
"AES-CTR", {
blink::WebCryptoAlgorithmParamsTypeAesCtrParams, // Encrypt
blink::WebCryptoAlgorithmParamsTypeAesCtrParams, // Decrypt
Undefined, // Sign
Undefined, // Verify
Undefined, // Digest
blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
Undefined, // DeriveKey
Undefined, // DeriveBits
blink::WebCryptoAlgorithmParamsTypeAesCtrParams, // WrapKey
blink::WebCryptoAlgorithmParamsTypeAesCtrParams // UnwrapKey
}
}, { // Index 10
"AES-KW", {
Undefined, // Encrypt
Undefined, // Decrypt
Undefined, // Sign
Undefined, // Verify
Undefined, // Digest
blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams, // GenerateKey
blink::WebCryptoAlgorithmParamsTypeNone, // ImportKey
Undefined, // DeriveKey
Undefined, // DeriveBits
blink::WebCryptoAlgorithmParamsTypeNone, // WrapKey
blink::WebCryptoAlgorithmParamsTypeNone // UnwrapKey
}
},
};
// Initializing the algorithmIdToInfo table above depends on knowing the enum
// values for algorithm IDs. If those ever change, the table will need to be
// updated.
COMPILE_ASSERT(blink::WebCryptoAlgorithmIdAesCbc == 0, AesCbc_idDoesntMatch);
COMPILE_ASSERT(blink::WebCryptoAlgorithmIdHmac == 1, Hmac_idDoesntMatch);
COMPILE_ASSERT(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 == 2, RsaSsaPkcs1v1_5_idDoesntMatch);
COMPILE_ASSERT(blink::WebCryptoAlgorithmIdSha1 == 3, Sha1_idDoesntMatch);
COMPILE_ASSERT(blink::WebCryptoAlgorithmIdSha256 == 4, Sha256_idDoesntMatch);
COMPILE_ASSERT(blink::WebCryptoAlgorithmIdSha384 == 5, Sha384_idDoesntMatch);
COMPILE_ASSERT(blink::WebCryptoAlgorithmIdSha512 == 6, Sha512_idDoesntMatch);
COMPILE_ASSERT(blink::WebCryptoAlgorithmIdAesGcm == 7, AesGcm_idDoesntMatch);
COMPILE_ASSERT(blink::WebCryptoAlgorithmIdRsaOaep == 8, RsaOaep_idDoesntMatch);
COMPILE_ASSERT(blink::WebCryptoAlgorithmIdAesCtr == 9, AesCtr_idDoesntMatch);
COMPILE_ASSERT(blink::WebCryptoAlgorithmIdAesKw == 10, AesKw_idDoesntMatch);
COMPILE_ASSERT(blink::WebCryptoAlgorithmIdLast == 10, Last_idDoesntMatch);
COMPILE_ASSERT(10 == blink::WebCryptoOperationLast, UpdateParamsMapping);
#if ASSERT_ENABLED #if ASSERT_ENABLED
// Essentially std::is_sorted() (however that function is new to C++11). // Essentially std::is_sorted() (however that function is new to C++11).
...@@ -372,13 +184,6 @@ bool lookupAlgorithmIdByName(const String& algorithmName, blink::WebCryptoAlgori ...@@ -372,13 +184,6 @@ bool lookupAlgorithmIdByName(const String& algorithmName, blink::WebCryptoAlgori
return true; return true;
} }
const AlgorithmInfo* lookupAlgorithmInfo(blink::WebCryptoAlgorithmId id)
{
if (id < 0 || id >= WTF_ARRAY_LENGTH(algorithmIdToInfo))
return 0;
return &algorithmIdToInfo[id];
}
void setSyntaxError(const String& message, AlgorithmError* error) void setSyntaxError(const String& message, AlgorithmError* error)
{ {
error->errorType = blink::WebCryptoErrorTypeSyntax; error->errorType = blink::WebCryptoErrorTypeSyntax;
...@@ -896,10 +701,10 @@ bool parseAlgorithm(const Dictionary& raw, blink::WebCryptoOperation op, blink:: ...@@ -896,10 +701,10 @@ bool parseAlgorithm(const Dictionary& raw, blink::WebCryptoOperation op, blink::
// Remove the "Algorithm:" prefix for all subsequent errors. // Remove the "Algorithm:" prefix for all subsequent errors.
context.removeLast(); context.removeLast();
const AlgorithmInfo* algorithmInfo = lookupAlgorithmInfo(algorithmId); const blink::WebCryptoAlgorithmInfo* algorithmInfo = blink::WebCryptoAlgorithm::lookupAlgorithmInfo(algorithmId);
if (algorithmInfo->operationToParamsType[op] == Undefined) { if (algorithmInfo->operationToParamsType[op] == blink::WebCryptoAlgorithmInfo::Undefined) {
context.add(algorithmIdToName(algorithmId)); context.add(algorithmInfo->name);
setNotSupportedError(context.toString("Unsupported operation", operationToString(op)), error); setNotSupportedError(context.toString("Unsupported operation", operationToString(op)), error);
return false; return false;
} }
...@@ -921,9 +726,4 @@ bool normalizeAlgorithm(const Dictionary& raw, blink::WebCryptoOperation op, bli ...@@ -921,9 +726,4 @@ bool normalizeAlgorithm(const Dictionary& raw, blink::WebCryptoOperation op, bli
return parseAlgorithm(raw, op, algorithm, ErrorContext(), error); return parseAlgorithm(raw, op, algorithm, ErrorContext(), error);
} }
const char* algorithmIdToName(blink::WebCryptoAlgorithmId id)
{
return lookupAlgorithmInfo(id)->name;
}
} // namespace WebCore } // namespace WebCore
...@@ -37,8 +37,6 @@ ...@@ -37,8 +37,6 @@
#include "wtf/Assertions.h" #include "wtf/Assertions.h"
#include "wtf/Forward.h" #include "wtf/Forward.h"
namespace blink { class WebCryptoAlgorithm; }
namespace WebCore { namespace WebCore {
class Dictionary; class Dictionary;
...@@ -61,10 +59,6 @@ struct AlgorithmError { ...@@ -61,10 +59,6 @@ struct AlgorithmError {
// [1] http://www.w3.org/TR/WebCryptoAPI/#algorithm-normalizing-rules // [1] http://www.w3.org/TR/WebCryptoAPI/#algorithm-normalizing-rules
bool normalizeAlgorithm(const Dictionary&, blink::WebCryptoOperation, blink::WebCryptoAlgorithm&, AlgorithmError*) 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
// will be valid for the program's entire runtime.
const char* algorithmIdToName(blink::WebCryptoAlgorithmId);
} // namespace WebCore } // namespace WebCore
#endif #endif
...@@ -85,6 +85,20 @@ enum WebCryptoAlgorithmParamsType { ...@@ -85,6 +85,20 @@ enum WebCryptoAlgorithmParamsType {
WebCryptoAlgorithmParamsTypeAesCtrParams, WebCryptoAlgorithmParamsTypeAesCtrParams,
}; };
struct WebCryptoAlgorithmInfo {
typedef char ParamsTypeOrUndefined;
static const ParamsTypeOrUndefined Undefined = -1;
// The canonical (case-sensitive) name for the algorithm as a
// null-terminated C-string literal.
const char* name;
// A map from the operation to the expected parameter type of the algorithm.
// If an operation is not applicable for the algorithm, set to Undefined.
const ParamsTypeOrUndefined operationToParamsType[WebCryptoOperationLast + 1];
};
class WebCryptoAesCbcParams; class WebCryptoAesCbcParams;
class WebCryptoAesKeyGenParams; class WebCryptoAesKeyGenParams;
class WebCryptoHmacImportParams; class WebCryptoHmacImportParams;
...@@ -115,6 +129,11 @@ public: ...@@ -115,6 +129,11 @@ public:
BLINK_PLATFORM_EXPORT static WebCryptoAlgorithm createNull(); BLINK_PLATFORM_EXPORT static WebCryptoAlgorithm createNull();
BLINK_PLATFORM_EXPORT static WebCryptoAlgorithm adoptParamsAndCreate(WebCryptoAlgorithmId, WebCryptoAlgorithmParams*); BLINK_PLATFORM_EXPORT static WebCryptoAlgorithm adoptParamsAndCreate(WebCryptoAlgorithmId, WebCryptoAlgorithmParams*);
// Returns a WebCryptoAlgorithmInfo for the algorithm with the given ID. If
// the ID is invalid, return 0. The caller can assume the pointer will be
// valid for the program's entire runtime.
BLINK_PLATFORM_EXPORT static const WebCryptoAlgorithmInfo* lookupAlgorithmInfo(WebCryptoAlgorithmId);
~WebCryptoAlgorithm() { reset(); } ~WebCryptoAlgorithm() { reset(); }
WebCryptoAlgorithm(const WebCryptoAlgorithm& other) { assign(other); } WebCryptoAlgorithm(const WebCryptoAlgorithm& other) { assign(other); }
......
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