ap@apple.com | a237529 | 2013-11-13 09:31:51 +0000 | [diff] [blame] | 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 | |
commit-queue@webkit.org | 553e8d3 | 2016-11-12 08:57:21 +0000 | [diff] [blame] | 26 | #pragma once |
ap@apple.com | a237529 | 2013-11-13 09:31:51 +0000 | [diff] [blame] | 27 | |
| 28 | #include "CryptoKey.h" |
jiewen_tan@apple.com | 64b29a3 | 2016-12-07 01:15:20 +0000 | [diff] [blame] | 29 | #include "ExceptionOr.h" |
jiewen_tan@apple.com | dfe64f4 | 2016-10-25 06:07:04 +0000 | [diff] [blame] | 30 | #include <wtf/Function.h> |
ap@apple.com | a237529 | 2013-11-13 09:31:51 +0000 | [diff] [blame] | 31 | |
| 32 | #if ENABLE(SUBTLE_CRYPTO) |
| 33 | |
mitz@apple.com | 28c9d4a | 2014-02-08 22:26:50 +0000 | [diff] [blame] | 34 | #if OS(DARWIN) && !PLATFORM(EFL) && !PLATFORM(GTK) |
ap@apple.com | a237529 | 2013-11-13 09:31:51 +0000 | [diff] [blame] | 35 | typedef struct _CCRSACryptor *CCRSACryptorRef; |
| 36 | typedef CCRSACryptorRef PlatformRSAKey; |
| 37 | #endif |
| 38 | |
evab.u-szeged@partner.samsung.com | ee6f5d2 | 2014-12-03 09:25:10 +0000 | [diff] [blame] | 39 | #if PLATFORM(GTK) || PLATFORM(EFL) |
| 40 | typedef struct _PlatformRSAKeyGnuTLS PlatformRSAKeyGnuTLS; |
| 41 | typedef PlatformRSAKeyGnuTLS *PlatformRSAKey; |
commit-queue@webkit.org | f5e23dc | 2014-08-11 08:59:15 +0000 | [diff] [blame] | 42 | #endif |
| 43 | |
ap@apple.com | a237529 | 2013-11-13 09:31:51 +0000 | [diff] [blame] | 44 | namespace WebCore { |
| 45 | |
| 46 | class CryptoKeyDataRSAComponents; |
ap@apple.com | a237529 | 2013-11-13 09:31:51 +0000 | [diff] [blame] | 47 | class PromiseWrapper; |
jiewen_tan@apple.com | dfe64f4 | 2016-10-25 06:07:04 +0000 | [diff] [blame] | 48 | class ScriptExecutionContext; |
ap@apple.com | a237529 | 2013-11-13 09:31:51 +0000 | [diff] [blame] | 49 | |
commit-queue@webkit.org | 3a87c39 | 2016-12-06 07:59:02 +0000 | [diff] [blame] | 50 | struct CryptoKeyPair; |
jiewen_tan@apple.com | df5276a | 2016-11-10 18:36:44 +0000 | [diff] [blame] | 51 | struct JsonWebKey; |
| 52 | |
jiewen_tan@apple.com | e942daa | 2016-09-15 00:19:12 +0000 | [diff] [blame] | 53 | class RsaKeyAlgorithm : public KeyAlgorithm { |
| 54 | public: |
| 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 | |
| 67 | private: |
| 68 | size_t m_modulusLength; |
| 69 | Vector<uint8_t> m_publicExponent; |
| 70 | }; |
| 71 | |
| 72 | class RsaHashedKeyAlgorithm final : public RsaKeyAlgorithm { |
| 73 | public: |
| 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 | |
| 84 | private: |
| 85 | String m_hash; |
| 86 | }; |
| 87 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 88 | class CryptoKeyRSA final : public CryptoKey { |
ap@apple.com | a237529 | 2013-11-13 09:31:51 +0000 | [diff] [blame] | 89 | public: |
jiewen_tan@apple.com | c738fda | 2016-11-11 20:12:00 +0000 | [diff] [blame] | 90 | static Ref<CryptoKeyRSA> create(CryptoAlgorithmIdentifier identifier, CryptoAlgorithmIdentifier hash, bool hasHash, CryptoKeyType type, PlatformRSAKey platformKey, bool extractable, CryptoKeyUsageBitmap usage) |
ap@apple.com | a237529 | 2013-11-13 09:31:51 +0000 | [diff] [blame] | 91 | { |
commit-queue@webkit.org | dace703 | 2015-11-07 04:44:02 +0000 | [diff] [blame] | 92 | return adoptRef(*new CryptoKeyRSA(identifier, hash, hasHash, type, platformKey, extractable, usage)); |
ap@apple.com | a237529 | 2013-11-13 09:31:51 +0000 | [diff] [blame] | 93 | } |
jiewen_tan@apple.com | c738fda | 2016-11-11 20:12:00 +0000 | [diff] [blame] | 94 | static RefPtr<CryptoKeyRSA> create(CryptoAlgorithmIdentifier, CryptoAlgorithmIdentifier hash, bool hasHash, const CryptoKeyDataRSAComponents&, bool extractable, CryptoKeyUsageBitmap); |
ap@apple.com | a237529 | 2013-11-13 09:31:51 +0000 | [diff] [blame] | 95 | virtual ~CryptoKeyRSA(); |
| 96 | |
ap@apple.com | a7a1d44 | 2013-11-18 08:42:41 +0000 | [diff] [blame] | 97 | bool isRestrictedToHash(CryptoAlgorithmIdentifier&) const; |
| 98 | |
| 99 | size_t keySizeInBits() const; |
ap@apple.com | a237529 | 2013-11-13 09:31:51 +0000 | [diff] [blame] | 100 | |
commit-queue@webkit.org | 3a87c39 | 2016-12-06 07:59:02 +0000 | [diff] [blame] | 101 | using KeyPairCallback = WTF::Function<void(CryptoKeyPair&&)>; |
jiewen_tan@apple.com | dfe64f4 | 2016-10-25 06:07:04 +0000 | [diff] [blame] | 102 | using VoidCallback = WTF::Function<void()>; |
jiewen_tan@apple.com | 83b6301 | 2016-11-18 21:31:42 +0000 | [diff] [blame] | 103 | 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.com | 4392696 | 2016-11-27 06:08:16 +0000 | [diff] [blame] | 104 | static RefPtr<CryptoKeyRSA> importJwk(CryptoAlgorithmIdentifier, std::optional<CryptoAlgorithmIdentifier> hash, JsonWebKey&&, bool extractable, CryptoKeyUsageBitmap); |
jiewen_tan@apple.com | 64b29a3 | 2016-12-07 01:15:20 +0000 | [diff] [blame] | 105 | static RefPtr<CryptoKeyRSA> importSpki(CryptoAlgorithmIdentifier, std::optional<CryptoAlgorithmIdentifier> hash, Vector<uint8_t>&&, bool extractable, CryptoKeyUsageBitmap); |
jiewen_tan@apple.com | e46b388 | 2016-12-09 23:05:29 +0000 | [diff] [blame^] | 106 | static RefPtr<CryptoKeyRSA> importPkcs8(CryptoAlgorithmIdentifier, std::optional<CryptoAlgorithmIdentifier> hash, Vector<uint8_t>&&, bool extractable, CryptoKeyUsageBitmap); |
ap@apple.com | a237529 | 2013-11-13 09:31:51 +0000 | [diff] [blame] | 107 | |
ap@apple.com | a237529 | 2013-11-13 09:31:51 +0000 | [diff] [blame] | 108 | PlatformRSAKey platformKey() const { return m_platformKey; } |
jiewen_tan@apple.com | d3f5b43 | 2016-11-15 19:08:25 +0000 | [diff] [blame] | 109 | JsonWebKey exportJwk() const; |
jiewen_tan@apple.com | 64b29a3 | 2016-12-07 01:15:20 +0000 | [diff] [blame] | 110 | ExceptionOr<Vector<uint8_t>> exportSpki() const; |
jiewen_tan@apple.com | e46b388 | 2016-12-09 23:05:29 +0000 | [diff] [blame^] | 111 | ExceptionOr<Vector<uint8_t>> exportPkcs8() const; |
jiewen_tan@apple.com | d3f5b43 | 2016-11-15 19:08:25 +0000 | [diff] [blame] | 112 | |
| 113 | CryptoAlgorithmIdentifier hashAlgorithmIdentifier() const { return m_hash; } |
ap@apple.com | a237529 | 2013-11-13 09:31:51 +0000 | [diff] [blame] | 114 | |
ap@apple.com | a237529 | 2013-11-13 09:31:51 +0000 | [diff] [blame] | 115 | private: |
jiewen_tan@apple.com | c738fda | 2016-11-11 20:12:00 +0000 | [diff] [blame] | 116 | CryptoKeyRSA(CryptoAlgorithmIdentifier, CryptoAlgorithmIdentifier hash, bool hasHash, CryptoKeyType, PlatformRSAKey, bool extractable, CryptoKeyUsageBitmap); |
ap@apple.com | a237529 | 2013-11-13 09:31:51 +0000 | [diff] [blame] | 117 | |
jiewen_tan@apple.com | e942daa | 2016-09-15 00:19:12 +0000 | [diff] [blame] | 118 | CryptoKeyClass keyClass() const final { return CryptoKeyClass::RSA; } |
ap@apple.com | a7a1d44 | 2013-11-18 08:42:41 +0000 | [diff] [blame] | 119 | |
jiewen_tan@apple.com | e942daa | 2016-09-15 00:19:12 +0000 | [diff] [blame] | 120 | std::unique_ptr<KeyAlgorithm> buildAlgorithm() const final; |
| 121 | std::unique_ptr<CryptoKeyData> exportData() const final; |
ap@apple.com | 32fdefd | 2013-11-14 21:44:25 +0000 | [diff] [blame] | 122 | |
ap@apple.com | a237529 | 2013-11-13 09:31:51 +0000 | [diff] [blame] | 123 | PlatformRSAKey m_platformKey; |
| 124 | |
| 125 | bool m_restrictedToSpecificHash; |
| 126 | CryptoAlgorithmIdentifier m_hash; |
| 127 | }; |
| 128 | |
ap@apple.com | a237529 | 2013-11-13 09:31:51 +0000 | [diff] [blame] | 129 | } // namespace WebCore |
| 130 | |
cdumez@apple.com | e5c7813 | 2014-10-06 19:20:19 +0000 | [diff] [blame] | 131 | SPECIALIZE_TYPE_TRAITS_CRYPTO_KEY(CryptoKeyRSA, CryptoKeyClass::RSA) |
| 132 | |
jiewen_tan@apple.com | e942daa | 2016-09-15 00:19:12 +0000 | [diff] [blame] | 133 | SPECIALIZE_TYPE_TRAITS_KEY_ALGORITHM(RsaKeyAlgorithm, KeyAlgorithmClass::RSA) |
| 134 | |
| 135 | SPECIALIZE_TYPE_TRAITS_KEY_ALGORITHM(RsaHashedKeyAlgorithm, KeyAlgorithmClass::HRSA) |
| 136 | |
ap@apple.com | a237529 | 2013-11-13 09:31:51 +0000 | [diff] [blame] | 137 | #endif // ENABLE(SUBTLE_CRYPTO) |