1/*
2 * Copyright (C) 2013-2019 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#include "CryptoKey.h"
29#include "ExceptionOr.h"
30#include <wtf/Function.h>
31
32#if ENABLE(WEB_CRYPTO)
33
34#if OS(DARWIN) && !PLATFORM(GTK)
35#include "CommonCryptoUtilities.h"
36
37typedef CCRSACryptorRef PlatformRSAKey;
38namespace WebCore {
39struct CCRSACryptorRefDeleter {
40 void operator()(CCRSACryptorRef key) const { CCRSACryptorRelease(key); }
41};
42}
43typedef std::unique_ptr<typename std::remove_pointer<CCRSACryptorRef>::type, WebCore::CCRSACryptorRefDeleter> PlatformRSAKeyContainer;
44#endif
45
46#if PLATFORM(GTK) || PLATFORM(WPE)
47#include <pal/crypto/gcrypt/Handle.h>
48
49typedef gcry_sexp_t PlatformRSAKey;
50typedef std::unique_ptr<typename std::remove_pointer<gcry_sexp_t>::type, PAL::GCrypt::HandleDeleter<gcry_sexp_t>> PlatformRSAKeyContainer;
51#endif
52
53namespace WebCore {
54
55class CryptoKeyRSAComponents;
56class PromiseWrapper;
57class ScriptExecutionContext;
58
59struct CryptoKeyPair;
60struct JsonWebKey;
61
62class CryptoKeyRSA final : public CryptoKey {
63public:
64 static Ref<CryptoKeyRSA> create(CryptoAlgorithmIdentifier identifier, CryptoAlgorithmIdentifier hash, bool hasHash, CryptoKeyType type, PlatformRSAKeyContainer&& platformKey, bool extractable, CryptoKeyUsageBitmap usage)
65 {
66 return adoptRef(*new CryptoKeyRSA(identifier, hash, hasHash, type, WTFMove(platformKey), extractable, usage));
67 }
68 static RefPtr<CryptoKeyRSA> create(CryptoAlgorithmIdentifier, CryptoAlgorithmIdentifier hash, bool hasHash, const CryptoKeyRSAComponents&, bool extractable, CryptoKeyUsageBitmap);
69 virtual ~CryptoKeyRSA() = default;
70
71 bool isRestrictedToHash(CryptoAlgorithmIdentifier&) const;
72
73 size_t keySizeInBits() const;
74
75 using KeyPairCallback = WTF::Function<void(CryptoKeyPair&&)>;
76 using VoidCallback = WTF::Function<void()>;
77 static void generatePair(CryptoAlgorithmIdentifier, CryptoAlgorithmIdentifier hash, bool hasHash, unsigned modulusLength, const Vector<uint8_t>& publicExponent, bool extractable, CryptoKeyUsageBitmap, KeyPairCallback&&, VoidCallback&& failureCallback, ScriptExecutionContext*);
78 static RefPtr<CryptoKeyRSA> importJwk(CryptoAlgorithmIdentifier, Optional<CryptoAlgorithmIdentifier> hash, JsonWebKey&&, bool extractable, CryptoKeyUsageBitmap);
79 static RefPtr<CryptoKeyRSA> importSpki(CryptoAlgorithmIdentifier, Optional<CryptoAlgorithmIdentifier> hash, Vector<uint8_t>&&, bool extractable, CryptoKeyUsageBitmap);
80 static RefPtr<CryptoKeyRSA> importPkcs8(CryptoAlgorithmIdentifier, Optional<CryptoAlgorithmIdentifier> hash, Vector<uint8_t>&&, bool extractable, CryptoKeyUsageBitmap);
81
82 PlatformRSAKey platformKey() const { return m_platformKey.get(); }
83 JsonWebKey exportJwk() const;
84 ExceptionOr<Vector<uint8_t>> exportSpki() const;
85 ExceptionOr<Vector<uint8_t>> exportPkcs8() const;
86
87 std::unique_ptr<CryptoKeyRSAComponents> exportData() const;
88
89 CryptoAlgorithmIdentifier hashAlgorithmIdentifier() const { return m_hash; }
90
91private:
92 CryptoKeyRSA(CryptoAlgorithmIdentifier, CryptoAlgorithmIdentifier hash, bool hasHash, CryptoKeyType, PlatformRSAKeyContainer&&, bool extractable, CryptoKeyUsageBitmap);
93
94 CryptoKeyClass keyClass() const final { return CryptoKeyClass::RSA; }
95
96 KeyAlgorithm algorithm() const final;
97
98 PlatformRSAKeyContainer m_platformKey;
99
100 bool m_restrictedToSpecificHash;
101 CryptoAlgorithmIdentifier m_hash;
102};
103
104} // namespace WebCore
105
106SPECIALIZE_TYPE_TRAITS_CRYPTO_KEY(CryptoKeyRSA, CryptoKeyClass::RSA)
107
108#endif // ENABLE(WEB_CRYPTO)
109