#include "config.h"
#include "CryptoKey.h"
#if ENABLE(SUBTLE_CRYPTO)
#include "CryptoAlgorithmDescriptionBuilder.h"
#include "CryptoAlgorithmRegistry.h"
#include <wtf/CryptographicallyRandomNumber.h>
#include <wtf/text/WTFString.h>
namespace WebCore {
CryptoKey::CryptoKey(CryptoAlgorithmIdentifier algorithm, CryptoKeyType type, bool extractable, CryptoKeyUsage usages)
: m_algorithm(algorithm)
, m_type(type)
, m_extractable(extractable)
, m_usages(usages)
{
}
CryptoKey::~CryptoKey()
{
}
String CryptoKey::type() const
{
switch (m_type) {
case CryptoKeyType::Secret:
return ASCIILiteral("secret");
case CryptoKeyType::Public:
return ASCIILiteral("public");
case CryptoKeyType::Private:
return ASCIILiteral("private");
}
RELEASE_ASSERT_NOT_REACHED();
return emptyString();
}
void CryptoKey::buildAlgorithmDescription(CryptoAlgorithmDescriptionBuilder& builder) const
{
builder.add("name", CryptoAlgorithmRegistry::singleton().nameForIdentifier(m_algorithm));
}
Vector<String> CryptoKey::usages() const
{
Vector<String> result;
if (m_usages & CryptoKeyUsageDecrypt)
result.append(ASCIILiteral("decrypt"));
if (m_usages & CryptoKeyUsageDeriveBits)
result.append(ASCIILiteral("deriveBits"));
if (m_usages & CryptoKeyUsageDeriveKey)
result.append(ASCIILiteral("deriveKey"));
if (m_usages & CryptoKeyUsageEncrypt)
result.append(ASCIILiteral("encrypt"));
if (m_usages & CryptoKeyUsageSign)
result.append(ASCIILiteral("sign"));
if (m_usages & CryptoKeyUsageUnwrapKey)
result.append(ASCIILiteral("unwrapKey"));
if (m_usages & CryptoKeyUsageVerify)
result.append(ASCIILiteral("verify"));
if (m_usages & CryptoKeyUsageWrapKey)
result.append(ASCIILiteral("wrapKey"));
return result;
}
#if !OS(DARWIN) || PLATFORM(EFL) || PLATFORM(GTK)
Vector<uint8_t> CryptoKey::randomData(size_t size)
{
Vector<uint8_t> result(size);
cryptographicallyRandomValues(result.data(), result.size());
return result;
}
#endif
}
#endif // ENABLE(SUBTLE_CRYPTO)