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#ifndef CryptoKeyDataRSAComponents_h
27#define CryptoKeyDataRSAComponents_h
28
29#include "CryptoKeyData.h"
30#include <wtf/Vector.h>
31
32#if ENABLE(SUBTLE_CRYPTO)
33
34namespace WebCore {
35
36class CryptoKeyDataRSAComponents final : public CryptoKeyData {
37public:
38    enum class Type {
39        Public,
40        Private
41    };
42
43    struct PrimeInfo {
44        Vector<uint8_t> primeFactor;
45        Vector<uint8_t> factorCRTExponent;
46        Vector<uint8_t> factorCRTCoefficient;
47    };
48
49    static std::unique_ptr<CryptoKeyDataRSAComponents> createPublic(const Vector<uint8_t>& modulus, const Vector<uint8_t>& exponent)
50    {
51        return std::unique_ptr<CryptoKeyDataRSAComponents>(new CryptoKeyDataRSAComponents(modulus, exponent));
52    }
53
54    static std::unique_ptr<CryptoKeyDataRSAComponents> createPrivate(const Vector<uint8_t>& modulus, const Vector<uint8_t>& exponent, const Vector<uint8_t>& privateExponent)
55    {
56        return std::unique_ptr<CryptoKeyDataRSAComponents>(new CryptoKeyDataRSAComponents(modulus, exponent, privateExponent));
57    }
58
59    static std::unique_ptr<CryptoKeyDataRSAComponents> 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)
60    {
61        return std::unique_ptr<CryptoKeyDataRSAComponents>(new CryptoKeyDataRSAComponents(modulus, exponent, privateExponent, firstPrimeInfo, secondPrimeInfo, otherPrimeInfos));
62    }
63
64    virtual ~CryptoKeyDataRSAComponents();
65
66    Type type() const { return m_type; }
67
68    // Private and public keys.
69    const Vector<uint8_t>& modulus() const { return m_modulus; }
70    const Vector<uint8_t>& exponent() const { return m_exponent; }
71
72    // Only private keys.
73    const Vector<uint8_t>& privateExponent() const { return m_privateExponent; }
74    bool hasAdditionalPrivateKeyParameters() const { return m_hasAdditionalPrivateKeyParameters; }
75    const PrimeInfo& firstPrimeInfo() const { return m_firstPrimeInfo; }
76    const PrimeInfo& secondPrimeInfo() const { return m_secondPrimeInfo; }
77    const Vector<PrimeInfo>& otherPrimeInfos() const { return m_otherPrimeInfos; }
78
79private:
80    CryptoKeyDataRSAComponents(const Vector<uint8_t>& modulus, const Vector<uint8_t>& exponent);
81    CryptoKeyDataRSAComponents(const Vector<uint8_t>& modulus, const Vector<uint8_t>& exponent, const Vector<uint8_t>& privateExponent);
82    CryptoKeyDataRSAComponents(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);
83
84    Type m_type;
85
86    // Private and public keys.
87    Vector<uint8_t> m_modulus;
88    Vector<uint8_t> m_exponent;
89
90    // Only private keys.
91    Vector<uint8_t> m_privateExponent;
92    bool m_hasAdditionalPrivateKeyParameters;
93    PrimeInfo m_firstPrimeInfo;
94    PrimeInfo m_secondPrimeInfo;
95    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.
96};
97
98inline bool isCryptoKeyDataRSAComponents(const CryptoKeyData& data)
99{
100    return data.format() == CryptoKeyData::Format::RSAComponents;
101}
102
103CRYPTO_KEY_DATA_CASTS(CryptoKeyDataRSAComponents)
104
105} // namespace WebCore
106
107#endif // ENABLE(SUBTLE_CRYPTO)
108#endif // CryptoKeyDataRSAComponents_h
109