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#include <wtf/Vector.h>
29
30#if ENABLE(WEB_CRYPTO)
31
32namespace WebCore {
33
34class CryptoKeyRSAComponents {
35public:
36 enum class Type {
37 Public,
38 Private
39 };
40
41 struct PrimeInfo {
42 Vector<uint8_t> primeFactor;
43 Vector<uint8_t> factorCRTExponent;
44 Vector<uint8_t> factorCRTCoefficient;
45 };
46
47 static std::unique_ptr<CryptoKeyRSAComponents> createPublic(const Vector<uint8_t>& modulus, const Vector<uint8_t>& exponent)
48 {
49 return std::unique_ptr<CryptoKeyRSAComponents>(new CryptoKeyRSAComponents(modulus, exponent));
50 }
51 static std::unique_ptr<CryptoKeyRSAComponents> createPublic(Vector<uint8_t>&& modulus, Vector<uint8_t>&& exponent)
52 {
53 return std::unique_ptr<CryptoKeyRSAComponents>(new CryptoKeyRSAComponents(WTFMove(modulus), WTFMove(exponent)));
54 }
55
56 static std::unique_ptr<CryptoKeyRSAComponents> createPrivate(const Vector<uint8_t>& modulus, const Vector<uint8_t>& exponent, const Vector<uint8_t>& privateExponent)
57 {
58 return std::unique_ptr<CryptoKeyRSAComponents>(new CryptoKeyRSAComponents(modulus, exponent, privateExponent));
59 }
60 static std::unique_ptr<CryptoKeyRSAComponents> createPrivate(Vector<uint8_t>&& modulus, Vector<uint8_t>&& exponent, Vector<uint8_t>&& privateExponent)
61 {
62 return std::unique_ptr<CryptoKeyRSAComponents>(new CryptoKeyRSAComponents(WTFMove(modulus), WTFMove(exponent), WTFMove(privateExponent)));
63 }
64
65 static std::unique_ptr<CryptoKeyRSAComponents> createPrivateWithAdditionalData(const Vector<uint8_t>& modulus, const Vector<uint8_t>& exponent, const Vector<uint8_t>& privateExponent, const PrimeInfo& firstPrimeInfo, const PrimeInfo& secondPrimeInfo, const Vector<PrimeInfo>& otherPrimeInfos)
66 {
67 return std::unique_ptr<CryptoKeyRSAComponents>(new CryptoKeyRSAComponents(modulus, exponent, privateExponent, firstPrimeInfo, secondPrimeInfo, otherPrimeInfos));
68 }
69 static std::unique_ptr<CryptoKeyRSAComponents> createPrivateWithAdditionalData(Vector<uint8_t>&& modulus, Vector<uint8_t>&& exponent, Vector<uint8_t>&& privateExponent, PrimeInfo&& firstPrimeInfo, PrimeInfo&& secondPrimeInfo, Vector<PrimeInfo>&& otherPrimeInfos)
70 {
71 return std::unique_ptr<CryptoKeyRSAComponents>(new CryptoKeyRSAComponents(WTFMove(modulus), WTFMove(exponent), WTFMove(privateExponent), WTFMove(firstPrimeInfo), WTFMove(secondPrimeInfo), WTFMove(otherPrimeInfos)));
72 }
73
74 virtual ~CryptoKeyRSAComponents();
75
76 Type type() const { return m_type; }
77
78 // Private and public keys.
79 const Vector<uint8_t>& modulus() const { return m_modulus; }
80 const Vector<uint8_t>& exponent() const { return m_exponent; }
81
82 // Only private keys.
83 const Vector<uint8_t>& privateExponent() const { return m_privateExponent; }
84 bool hasAdditionalPrivateKeyParameters() const { return m_hasAdditionalPrivateKeyParameters; }
85 const PrimeInfo& firstPrimeInfo() const { return m_firstPrimeInfo; }
86 const PrimeInfo& secondPrimeInfo() const { return m_secondPrimeInfo; }
87 const Vector<PrimeInfo>& otherPrimeInfos() const { return m_otherPrimeInfos; }
88
89private:
90 CryptoKeyRSAComponents(const Vector<uint8_t>& modulus, const Vector<uint8_t>& exponent);
91 CryptoKeyRSAComponents(Vector<uint8_t>&& modulus, Vector<uint8_t>&& exponent);
92
93 CryptoKeyRSAComponents(const Vector<uint8_t>& modulus, const Vector<uint8_t>& exponent, const Vector<uint8_t>& privateExponent);
94 CryptoKeyRSAComponents(Vector<uint8_t>&& modulus, Vector<uint8_t>&& exponent, Vector<uint8_t>&& privateExponent);
95
96 CryptoKeyRSAComponents(const Vector<uint8_t>& modulus, const Vector<uint8_t>& exponent, const Vector<uint8_t>& privateExponent, const PrimeInfo& firstPrimeInfo, const PrimeInfo& secondPrimeInfo, const Vector<PrimeInfo>& otherPrimeInfos);
97 CryptoKeyRSAComponents(Vector<uint8_t>&& modulus, Vector<uint8_t>&& exponent, Vector<uint8_t>&& privateExponent, PrimeInfo&& firstPrimeInfo, PrimeInfo&& secondPrimeInfo, Vector<PrimeInfo>&& otherPrimeInfos);
98
99 Type m_type;
100
101 // Private and public keys.
102 Vector<uint8_t> m_modulus;
103 Vector<uint8_t> m_exponent;
104
105 // Only private keys.
106 Vector<uint8_t> m_privateExponent;
107 bool m_hasAdditionalPrivateKeyParameters;
108 PrimeInfo m_firstPrimeInfo;
109 PrimeInfo m_secondPrimeInfo;
110 Vector<PrimeInfo> m_otherPrimeInfos; // When three or more primes have been used, the number of array elements is be the number of primes used minus two.
111};
112
113} // namespace WebCore
114
115#endif // ENABLE(WEB_CRYPTO)
116