blob: eabda2b75b495b54846eb8da0fd85f4d9beda53a [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.comdfe64f42016-10-25 06:07:04 +000029#include <wtf/Function.h>
ap@apple.coma2375292013-11-13 09:31:51 +000030
31#if ENABLE(SUBTLE_CRYPTO)
32
mitz@apple.com28c9d4a2014-02-08 22:26:50 +000033#if OS(DARWIN) && !PLATFORM(EFL) && !PLATFORM(GTK)
ap@apple.coma2375292013-11-13 09:31:51 +000034typedef struct _CCRSACryptor *CCRSACryptorRef;
35typedef CCRSACryptorRef PlatformRSAKey;
36#endif
37
evab.u-szeged@partner.samsung.comee6f5d22014-12-03 09:25:10 +000038#if PLATFORM(GTK) || PLATFORM(EFL)
39typedef struct _PlatformRSAKeyGnuTLS PlatformRSAKeyGnuTLS;
40typedef PlatformRSAKeyGnuTLS *PlatformRSAKey;
commit-queue@webkit.orgf5e23dc2014-08-11 08:59:15 +000041#endif
42
ap@apple.coma2375292013-11-13 09:31:51 +000043namespace WebCore {
44
45class CryptoKeyDataRSAComponents;
46class CryptoKeyPair;
47class PromiseWrapper;
jiewen_tan@apple.comdfe64f42016-10-25 06:07:04 +000048class ScriptExecutionContext;
ap@apple.coma2375292013-11-13 09:31:51 +000049
jiewen_tan@apple.comdf5276a2016-11-10 18:36:44 +000050struct JsonWebKey;
51
jiewen_tan@apple.come942daa2016-09-15 00:19:12 +000052class RsaKeyAlgorithm : public KeyAlgorithm {
53public:
54 RsaKeyAlgorithm(const String& name, size_t modulusLength, Vector<uint8_t>&& publicExponent)
55 : KeyAlgorithm(name)
56 , m_modulusLength(modulusLength)
57 , m_publicExponent(WTFMove(publicExponent))
58 {
59 }
60
61 KeyAlgorithmClass keyAlgorithmClass() const override { return KeyAlgorithmClass::RSA; }
62
63 size_t modulusLength() const { return m_modulusLength; }
64 const Vector<uint8_t>& publicExponent() const { return m_publicExponent; }
65
66private:
67 size_t m_modulusLength;
68 Vector<uint8_t> m_publicExponent;
69};
70
71class RsaHashedKeyAlgorithm final : public RsaKeyAlgorithm {
72public:
73 RsaHashedKeyAlgorithm(const String& name, size_t modulusLength, Vector<uint8_t>&& publicExponent, const String& hash)
74 : RsaKeyAlgorithm(name, modulusLength, WTFMove(publicExponent))
75 , m_hash(hash)
76 {
77 }
78
79 KeyAlgorithmClass keyAlgorithmClass() const final { return KeyAlgorithmClass::HRSA; }
80
81 const String& hash() const { return m_hash; }
82
83private:
84 String m_hash;
85};
86
andersca@apple.com16d2dd42014-01-16 23:08:24 +000087class CryptoKeyRSA final : public CryptoKey {
ap@apple.coma2375292013-11-13 09:31:51 +000088public:
jiewen_tan@apple.comc738fda2016-11-11 20:12:00 +000089 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 +000090 {
commit-queue@webkit.orgdace7032015-11-07 04:44:02 +000091 return adoptRef(*new CryptoKeyRSA(identifier, hash, hasHash, type, platformKey, extractable, usage));
ap@apple.coma2375292013-11-13 09:31:51 +000092 }
jiewen_tan@apple.comc738fda2016-11-11 20:12:00 +000093 static RefPtr<CryptoKeyRSA> create(CryptoAlgorithmIdentifier, CryptoAlgorithmIdentifier hash, bool hasHash, const CryptoKeyDataRSAComponents&, bool extractable, CryptoKeyUsageBitmap);
ap@apple.coma2375292013-11-13 09:31:51 +000094 virtual ~CryptoKeyRSA();
95
ap@apple.coma7a1d442013-11-18 08:42:41 +000096 bool isRestrictedToHash(CryptoAlgorithmIdentifier&) const;
97
98 size_t keySizeInBits() const;
ap@apple.coma2375292013-11-13 09:31:51 +000099
jiewen_tan@apple.comdfe64f42016-10-25 06:07:04 +0000100 using KeyPairCallback = WTF::Function<void(CryptoKeyPair&)>;
101 using VoidCallback = WTF::Function<void()>;
jiewen_tan@apple.comc738fda2016-11-11 20:12:00 +0000102 static void generatePair(CryptoAlgorithmIdentifier, CryptoAlgorithmIdentifier hash, bool hasHash, unsigned modulusLength, const Vector<uint8_t>& publicExponent, bool extractable, CryptoKeyUsageBitmap, KeyPairCallback, VoidCallback failureCallback, ScriptExecutionContext*);
103 static RefPtr<CryptoKeyRSA> importJwk(CryptoAlgorithmIdentifier, Optional<CryptoAlgorithmIdentifier> hash, JsonWebKey&&, bool extractable, CryptoKeyUsageBitmap);
ap@apple.coma2375292013-11-13 09:31:51 +0000104
ap@apple.coma2375292013-11-13 09:31:51 +0000105 PlatformRSAKey platformKey() const { return m_platformKey; }
jiewen_tan@apple.comd3f5b432016-11-15 19:08:25 +0000106 JsonWebKey exportJwk() const;
107
108 CryptoAlgorithmIdentifier hashAlgorithmIdentifier() const { return m_hash; }
ap@apple.coma2375292013-11-13 09:31:51 +0000109
ap@apple.coma2375292013-11-13 09:31:51 +0000110private:
jiewen_tan@apple.comc738fda2016-11-11 20:12:00 +0000111 CryptoKeyRSA(CryptoAlgorithmIdentifier, CryptoAlgorithmIdentifier hash, bool hasHash, CryptoKeyType, PlatformRSAKey, bool extractable, CryptoKeyUsageBitmap);
ap@apple.coma2375292013-11-13 09:31:51 +0000112
jiewen_tan@apple.come942daa2016-09-15 00:19:12 +0000113 CryptoKeyClass keyClass() const final { return CryptoKeyClass::RSA; }
ap@apple.coma7a1d442013-11-18 08:42:41 +0000114
jiewen_tan@apple.come942daa2016-09-15 00:19:12 +0000115 std::unique_ptr<KeyAlgorithm> buildAlgorithm() const final;
116 std::unique_ptr<CryptoKeyData> exportData() const final;
ap@apple.com32fdefd2013-11-14 21:44:25 +0000117
ap@apple.coma2375292013-11-13 09:31:51 +0000118 PlatformRSAKey m_platformKey;
119
120 bool m_restrictedToSpecificHash;
121 CryptoAlgorithmIdentifier m_hash;
122};
123
ap@apple.coma2375292013-11-13 09:31:51 +0000124} // namespace WebCore
125
cdumez@apple.come5c78132014-10-06 19:20:19 +0000126SPECIALIZE_TYPE_TRAITS_CRYPTO_KEY(CryptoKeyRSA, CryptoKeyClass::RSA)
127
jiewen_tan@apple.come942daa2016-09-15 00:19:12 +0000128SPECIALIZE_TYPE_TRAITS_KEY_ALGORITHM(RsaKeyAlgorithm, KeyAlgorithmClass::RSA)
129
130SPECIALIZE_TYPE_TRAITS_KEY_ALGORITHM(RsaHashedKeyAlgorithm, KeyAlgorithmClass::HRSA)
131
ap@apple.coma2375292013-11-13 09:31:51 +0000132#endif // ENABLE(SUBTLE_CRYPTO)