This source file includes following definitions.
- keyTypeToString
- keyUsageToString
- keyUsageStringToMask
- type
- extractable
- algorithm
- usages
- canBeUsedForAlgorithm
- parseFormat
- parseUsageMask
- trace
#include "config.h"
#include "modules/crypto/Key.h"
#include "bindings/v8/ExceptionState.h"
#include "core/dom/ExceptionCode.h"
#include "modules/crypto/KeyAlgorithm.h"
#include "platform/CryptoResult.h"
#include "public/platform/WebCryptoAlgorithmParams.h"
#include "public/platform/WebString.h"
namespace WebCore {
namespace {
const char* keyTypeToString(blink::WebCryptoKeyType type)
{
switch (type) {
case blink::WebCryptoKeyTypeSecret:
return "secret";
case blink::WebCryptoKeyTypePublic:
return "public";
case blink::WebCryptoKeyTypePrivate:
return "private";
}
ASSERT_NOT_REACHED();
return 0;
}
struct KeyUsageMapping {
blink::WebCryptoKeyUsage value;
const char* const name;
};
const KeyUsageMapping keyUsageMappings[] = {
{ blink::WebCryptoKeyUsageEncrypt, "encrypt" },
{ blink::WebCryptoKeyUsageDecrypt, "decrypt" },
{ blink::WebCryptoKeyUsageSign, "sign" },
{ blink::WebCryptoKeyUsageVerify, "verify" },
{ blink::WebCryptoKeyUsageDeriveKey, "deriveKey" },
{ blink::WebCryptoKeyUsageWrapKey, "wrapKey" },
{ blink::WebCryptoKeyUsageUnwrapKey, "unwrapKey" },
};
COMPILE_ASSERT(blink::EndOfWebCryptoKeyUsage == (1 << 6) + 1, update_keyUsageMappings);
const char* keyUsageToString(blink::WebCryptoKeyUsage usage)
{
for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) {
if (keyUsageMappings[i].value == usage)
return keyUsageMappings[i].name;
}
ASSERT_NOT_REACHED();
return 0;
}
blink::WebCryptoKeyUsageMask keyUsageStringToMask(const String& usageString)
{
for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) {
if (keyUsageMappings[i].name == usageString)
return keyUsageMappings[i].value;
}
return 0;
}
blink::WebCryptoKeyUsageMask toKeyUsage(AlgorithmOperation operation)
{
switch (operation) {
case Encrypt:
return blink::WebCryptoKeyUsageEncrypt;
case Decrypt:
return blink::WebCryptoKeyUsageDecrypt;
case Sign:
return blink::WebCryptoKeyUsageSign;
case Verify:
return blink::WebCryptoKeyUsageVerify;
case DeriveKey:
return blink::WebCryptoKeyUsageDeriveKey;
case WrapKey:
return blink::WebCryptoKeyUsageWrapKey;
case UnwrapKey:
return blink::WebCryptoKeyUsageUnwrapKey;
case Digest:
case GenerateKey:
case ImportKey:
break;
}
ASSERT_NOT_REACHED();
return 0;
}
}
Key::~Key()
{
}
Key::Key(const blink::WebCryptoKey& key)
: m_key(key)
{
ScriptWrappable::init(this);
}
String Key::type() const
{
return keyTypeToString(m_key.type());
}
bool Key::extractable() const
{
return m_key.extractable();
}
KeyAlgorithm* Key::algorithm()
{
if (!m_algorithm)
m_algorithm = KeyAlgorithm::create(m_key.algorithm());
return m_algorithm.get();
}
Vector<String> Key::usages() const
{
Vector<String> result;
for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) {
blink::WebCryptoKeyUsage usage = keyUsageMappings[i].value;
if (m_key.usages() & usage)
result.append(keyUsageToString(usage));
}
return result;
}
bool Key::canBeUsedForAlgorithm(const blink::WebCryptoAlgorithm& algorithm, AlgorithmOperation op, CryptoResult* result) const
{
if (!(m_key.usages() & toKeyUsage(op))) {
result->completeWithError("key.usages does not permit this operation");
return false;
}
if (m_key.algorithm().id() != algorithm.id()) {
result->completeWithError("key.algorithm does not match that of operation");
return false;
}
return true;
}
bool Key::parseFormat(const String& formatString, blink::WebCryptoKeyFormat& format, CryptoResult* result)
{
if (formatString == "raw") {
format = blink::WebCryptoKeyFormatRaw;
return true;
}
if (formatString == "pkcs8") {
format = blink::WebCryptoKeyFormatPkcs8;
return true;
}
if (formatString == "spki") {
format = blink::WebCryptoKeyFormatSpki;
return true;
}
if (formatString == "jwk") {
format = blink::WebCryptoKeyFormatJwk;
return true;
}
result->completeWithError("Invalid keyFormat argument");
return false;
}
bool Key::parseUsageMask(const Vector<String>& usages, blink::WebCryptoKeyUsageMask& mask, CryptoResult* result)
{
mask = 0;
for (size_t i = 0; i < usages.size(); ++i) {
blink::WebCryptoKeyUsageMask usage = keyUsageStringToMask(usages[i]);
if (!usage) {
result->completeWithError("Invalid keyUsages argument");
return false;
}
mask |= usage;
}
return true;
}
void Key::trace(Visitor* visitor)
{
visitor->trace(m_algorithm);
}
}