#pragma once
#if ENABLE(SUBTLE_CRYPTO)
#include "CryptoAlgorithmIdentifier.h"
#include "CryptoKeyType.h"
#include "CryptoKeyUsage.h"
#include <wtf/Forward.h>
#include <wtf/ThreadSafeRefCounted.h>
#include <wtf/TypeCasts.h>
#include <wtf/Vector.h>
#include <wtf/text/WTFString.h>
namespace WebCore {
class CryptoAlgorithmDescriptionBuilder;
class CryptoKeyData;
enum class CryptoKeyClass {
AES,
HMAC,
RSA
};
enum class KeyAlgorithmClass {
AES,
HMAC,
HRSA,
RSA,
};
class KeyAlgorithm {
public:
virtual ~KeyAlgorithm()
{
}
virtual KeyAlgorithmClass keyAlgorithmClass() const = 0;
const String& name() const { return m_name; }
protected:
explicit KeyAlgorithm(const String& name)
: m_name(name)
{
}
private:
String m_name;
};
class CryptoKey : public ThreadSafeRefCounted<CryptoKey> {
public:
using Type = CryptoKeyType;
CryptoKey(CryptoAlgorithmIdentifier, Type, bool extractable, CryptoKeyUsageBitmap);
virtual ~CryptoKey();
virtual CryptoKeyClass keyClass() const = 0;
Type type() const;
bool extractable() const { return m_extractable; }
virtual std::unique_ptr<KeyAlgorithm> buildAlgorithm() const = 0;
Vector<CryptoKeyUsage> usages() const;
CryptoAlgorithmIdentifier algorithmIdentifier() const { return m_algorithmIdentifier; }
CryptoKeyUsageBitmap usagesBitmap() const { return m_usages; }
void setUsagesBitmap(CryptoKeyUsageBitmap usage) { m_usages = usage; };
bool allows(CryptoKeyUsageBitmap usage) const { return usage == (m_usages & usage); }
virtual std::unique_ptr<CryptoKeyData> exportData() const = 0;
static Vector<uint8_t> randomData(size_t);
private:
CryptoAlgorithmIdentifier m_algorithmIdentifier;
Type m_type;
bool m_extractable;
CryptoKeyUsageBitmap m_usages;
};
inline auto CryptoKey::type() const -> Type
{
return m_type;
}
}
#define SPECIALIZE_TYPE_TRAITS_CRYPTO_KEY(ToClassName, KeyClass) \
SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::ToClassName) \
static bool isType(const WebCore::CryptoKey& key) { return key.keyClass() == WebCore::KeyClass; } \
SPECIALIZE_TYPE_TRAITS_END()
#define SPECIALIZE_TYPE_TRAITS_KEY_ALGORITHM(ToClassName, KeyAlgorithmClass) \
SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::ToClassName) \
static bool isType(const WebCore::KeyAlgorithm& algorithm) { return algorithm.keyAlgorithmClass() == WebCore::KeyAlgorithmClass; } \
SPECIALIZE_TYPE_TRAITS_END()
#endif // ENABLE(SUBTLE_CRYPTO)