blob: 335bd4401e6cceb4106360ec9e700de49101597b [file] [log] [blame]
ap@apple.coma2375292013-11-13 09:31:51 +00001/*
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
commit-queue@webkit.org553e8d32016-11-12 08:57:21 +000026#pragma once
ap@apple.coma2375292013-11-13 09:31:51 +000027
28#include "CryptoKey.h"
jiewen_tan@apple.com64b29a32016-12-07 01:15:20 +000029#include "ExceptionOr.h"
jiewen_tan@apple.comdfe64f42016-10-25 06:07:04 +000030#include <wtf/Function.h>
ap@apple.coma2375292013-11-13 09:31:51 +000031
32#if ENABLE(SUBTLE_CRYPTO)
33
mitz@apple.com28c9d4a2014-02-08 22:26:50 +000034#if OS(DARWIN) && !PLATFORM(EFL) && !PLATFORM(GTK)
ap@apple.coma2375292013-11-13 09:31:51 +000035typedef struct _CCRSACryptor *CCRSACryptorRef;
36typedef CCRSACryptorRef PlatformRSAKey;
37#endif
38
evab.u-szeged@partner.samsung.comee6f5d22014-12-03 09:25:10 +000039#if PLATFORM(GTK) || PLATFORM(EFL)
40typedef struct _PlatformRSAKeyGnuTLS PlatformRSAKeyGnuTLS;
41typedef PlatformRSAKeyGnuTLS *PlatformRSAKey;
commit-queue@webkit.orgf5e23dc2014-08-11 08:59:15 +000042#endif
43
ap@apple.coma2375292013-11-13 09:31:51 +000044namespace WebCore {
45
46class CryptoKeyDataRSAComponents;
ap@apple.coma2375292013-11-13 09:31:51 +000047class PromiseWrapper;
jiewen_tan@apple.comdfe64f42016-10-25 06:07:04 +000048class ScriptExecutionContext;
ap@apple.coma2375292013-11-13 09:31:51 +000049
commit-queue@webkit.org3a87c392016-12-06 07:59:02 +000050struct CryptoKeyPair;
jiewen_tan@apple.comdf5276a2016-11-10 18:36:44 +000051struct JsonWebKey;
52
jiewen_tan@apple.come942daa2016-09-15 00:19:12 +000053class RsaKeyAlgorithm : public KeyAlgorithm {
54public:
55 RsaKeyAlgorithm(const String& name, size_t modulusLength, Vector<uint8_t>&& publicExponent)
56 : KeyAlgorithm(name)
57 , m_modulusLength(modulusLength)
58 , m_publicExponent(WTFMove(publicExponent))
59 {
60 }
61
62 KeyAlgorithmClass keyAlgorithmClass() const override { return KeyAlgorithmClass::RSA; }
63
64 size_t modulusLength() const { return m_modulusLength; }
65 const Vector<uint8_t>& publicExponent() const { return m_publicExponent; }
66
67private:
68 size_t m_modulusLength;
69 Vector<uint8_t> m_publicExponent;
70};
71
72class RsaHashedKeyAlgorithm final : public RsaKeyAlgorithm {
73public:
74 RsaHashedKeyAlgorithm(const String& name, size_t modulusLength, Vector<uint8_t>&& publicExponent, const String& hash)
75 : RsaKeyAlgorithm(name, modulusLength, WTFMove(publicExponent))
76 , m_hash(hash)
77 {
78 }
79
80 KeyAlgorithmClass keyAlgorithmClass() const final { return KeyAlgorithmClass::HRSA; }
81
82 const String& hash() const { return m_hash; }
83
84private:
85 String m_hash;
86};
87
andersca@apple.com16d2dd42014-01-16 23:08:24 +000088class CryptoKeyRSA final : public CryptoKey {
ap@apple.coma2375292013-11-13 09:31:51 +000089public:
jiewen_tan@apple.comc738fda2016-11-11 20:12:00 +000090 static Ref<CryptoKeyRSA> create(CryptoAlgorithmIdentifier identifier, CryptoAlgorithmIdentifier hash, bool hasHash, CryptoKeyType type, PlatformRSAKey platformKey, bool extractable, CryptoKeyUsageBitmap usage)
ap@apple.coma2375292013-11-13 09:31:51 +000091 {
commit-queue@webkit.orgdace7032015-11-07 04:44:02 +000092 return adoptRef(*new CryptoKeyRSA(identifier, hash, hasHash, type, platformKey, extractable, usage));
ap@apple.coma2375292013-11-13 09:31:51 +000093 }
jiewen_tan@apple.comc738fda2016-11-11 20:12:00 +000094 static RefPtr<CryptoKeyRSA> create(CryptoAlgorithmIdentifier, CryptoAlgorithmIdentifier hash, bool hasHash, const CryptoKeyDataRSAComponents&, bool extractable, CryptoKeyUsageBitmap);
ap@apple.coma2375292013-11-13 09:31:51 +000095 virtual ~CryptoKeyRSA();
96
ap@apple.coma7a1d442013-11-18 08:42:41 +000097 bool isRestrictedToHash(CryptoAlgorithmIdentifier&) const;
98
99 size_t keySizeInBits() const;
ap@apple.coma2375292013-11-13 09:31:51 +0000100
commit-queue@webkit.org3a87c392016-12-06 07:59:02 +0000101 using KeyPairCallback = WTF::Function<void(CryptoKeyPair&&)>;
jiewen_tan@apple.comdfe64f42016-10-25 06:07:04 +0000102 using VoidCallback = WTF::Function<void()>;
jiewen_tan@apple.com83b63012016-11-18 21:31:42 +0000103 static void generatePair(CryptoAlgorithmIdentifier, CryptoAlgorithmIdentifier hash, bool hasHash, unsigned modulusLength, const Vector<uint8_t>& publicExponent, bool extractable, CryptoKeyUsageBitmap, KeyPairCallback&&, VoidCallback&& failureCallback, ScriptExecutionContext*);
utatane.tea@gmail.com43926962016-11-27 06:08:16 +0000104 static RefPtr<CryptoKeyRSA> importJwk(CryptoAlgorithmIdentifier, std::optional<CryptoAlgorithmIdentifier> hash, JsonWebKey&&, bool extractable, CryptoKeyUsageBitmap);
jiewen_tan@apple.com64b29a32016-12-07 01:15:20 +0000105 static RefPtr<CryptoKeyRSA> importSpki(CryptoAlgorithmIdentifier, std::optional<CryptoAlgorithmIdentifier> hash, Vector<uint8_t>&&, bool extractable, CryptoKeyUsageBitmap);
jiewen_tan@apple.come46b3882016-12-09 23:05:29 +0000106 static RefPtr<CryptoKeyRSA> importPkcs8(CryptoAlgorithmIdentifier, std::optional<CryptoAlgorithmIdentifier> hash, Vector<uint8_t>&&, bool extractable, CryptoKeyUsageBitmap);
ap@apple.coma2375292013-11-13 09:31:51 +0000107
ap@apple.coma2375292013-11-13 09:31:51 +0000108 PlatformRSAKey platformKey() const { return m_platformKey; }
jiewen_tan@apple.comd3f5b432016-11-15 19:08:25 +0000109 JsonWebKey exportJwk() const;
jiewen_tan@apple.com64b29a32016-12-07 01:15:20 +0000110 ExceptionOr<Vector<uint8_t>> exportSpki() const;
jiewen_tan@apple.come46b3882016-12-09 23:05:29 +0000111 ExceptionOr<Vector<uint8_t>> exportPkcs8() const;
jiewen_tan@apple.comd3f5b432016-11-15 19:08:25 +0000112
113 CryptoAlgorithmIdentifier hashAlgorithmIdentifier() const { return m_hash; }
ap@apple.coma2375292013-11-13 09:31:51 +0000114
ap@apple.coma2375292013-11-13 09:31:51 +0000115private:
jiewen_tan@apple.comc738fda2016-11-11 20:12:00 +0000116 CryptoKeyRSA(CryptoAlgorithmIdentifier, CryptoAlgorithmIdentifier hash, bool hasHash, CryptoKeyType, PlatformRSAKey, bool extractable, CryptoKeyUsageBitmap);
ap@apple.coma2375292013-11-13 09:31:51 +0000117
jiewen_tan@apple.come942daa2016-09-15 00:19:12 +0000118 CryptoKeyClass keyClass() const final { return CryptoKeyClass::RSA; }
ap@apple.coma7a1d442013-11-18 08:42:41 +0000119
jiewen_tan@apple.come942daa2016-09-15 00:19:12 +0000120 std::unique_ptr<KeyAlgorithm> buildAlgorithm() const final;
121 std::unique_ptr<CryptoKeyData> exportData() const final;
ap@apple.com32fdefd2013-11-14 21:44:25 +0000122
ap@apple.coma2375292013-11-13 09:31:51 +0000123 PlatformRSAKey m_platformKey;
124
125 bool m_restrictedToSpecificHash;
126 CryptoAlgorithmIdentifier m_hash;
127};
128
ap@apple.coma2375292013-11-13 09:31:51 +0000129} // namespace WebCore
130
cdumez@apple.come5c78132014-10-06 19:20:19 +0000131SPECIALIZE_TYPE_TRAITS_CRYPTO_KEY(CryptoKeyRSA, CryptoKeyClass::RSA)
132
jiewen_tan@apple.come942daa2016-09-15 00:19:12 +0000133SPECIALIZE_TYPE_TRAITS_KEY_ALGORITHM(RsaKeyAlgorithm, KeyAlgorithmClass::RSA)
134
135SPECIALIZE_TYPE_TRAITS_KEY_ALGORITHM(RsaHashedKeyAlgorithm, KeyAlgorithmClass::HRSA)
136
ap@apple.coma2375292013-11-13 09:31:51 +0000137#endif // ENABLE(SUBTLE_CRYPTO)