1/*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#if ENABLE(WEB_CRYPTO)
29
30#include "CryptoAesKeyAlgorithm.h"
31#include "CryptoAlgorithmIdentifier.h"
32#include "CryptoEcKeyAlgorithm.h"
33#include "CryptoHmacKeyAlgorithm.h"
34#include "CryptoKeyAlgorithm.h"
35#include "CryptoKeyType.h"
36#include "CryptoKeyUsage.h"
37#include "CryptoRsaHashedKeyAlgorithm.h"
38#include "CryptoRsaKeyAlgorithm.h"
39#include <wtf/Forward.h>
40#include <wtf/ThreadSafeRefCounted.h>
41#include <wtf/TypeCasts.h>
42#include <wtf/Variant.h>
43#include <wtf/Vector.h>
44#include <wtf/text/WTFString.h>
45
46namespace WebCore {
47
48enum class CryptoKeyClass {
49 AES,
50 EC,
51 HMAC,
52 RSA,
53 Raw,
54};
55
56class CryptoKey : public ThreadSafeRefCounted<CryptoKey> {
57public:
58 using Type = CryptoKeyType;
59 using KeyAlgorithm = Variant<CryptoKeyAlgorithm, CryptoAesKeyAlgorithm, CryptoEcKeyAlgorithm, CryptoHmacKeyAlgorithm, CryptoRsaHashedKeyAlgorithm, CryptoRsaKeyAlgorithm>;
60
61 CryptoKey(CryptoAlgorithmIdentifier, Type, bool extractable, CryptoKeyUsageBitmap);
62 virtual ~CryptoKey();
63
64 Type type() const;
65 bool extractable() const { return m_extractable; }
66 Vector<CryptoKeyUsage> usages() const;
67 virtual KeyAlgorithm algorithm() const = 0;
68
69 virtual CryptoKeyClass keyClass() const = 0;
70
71 CryptoAlgorithmIdentifier algorithmIdentifier() const { return m_algorithmIdentifier; }
72 CryptoKeyUsageBitmap usagesBitmap() const { return m_usages; }
73 void setUsagesBitmap(CryptoKeyUsageBitmap usage) { m_usages = usage; };
74 bool allows(CryptoKeyUsageBitmap usage) const { return usage == (m_usages & usage); }
75
76 static Vector<uint8_t> randomData(size_t);
77
78private:
79 CryptoAlgorithmIdentifier m_algorithmIdentifier;
80 Type m_type;
81 bool m_extractable;
82 CryptoKeyUsageBitmap m_usages;
83};
84
85inline auto CryptoKey::type() const -> Type
86{
87 return m_type;
88}
89
90} // namespace WebCore
91
92#define SPECIALIZE_TYPE_TRAITS_CRYPTO_KEY(ToClassName, KeyClass) \
93SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::ToClassName) \
94 static bool isType(const WebCore::CryptoKey& key) { return key.keyClass() == WebCore::KeyClass; } \
95SPECIALIZE_TYPE_TRAITS_END()
96
97#endif // ENABLE(WEB_CRYPTO)
98