OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / libs / utils / ssh / sshcryptofacility_p.h
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #ifndef SSHABSTRACTCRYPTOFACILITY_P_H
34 #define SSHABSTRACTCRYPTOFACILITY_P_H
35
36 #include <botan/auto_rng.h>
37 #include <botan/symkey.h>
38
39 #include <QtCore/QByteArray>
40 #include <QtCore/QScopedPointer>
41
42 namespace Botan {
43     class BigInt;
44     class BlockCipher;
45     class BlockCipherMode;
46     class BlockCipherModePaddingMethod;
47     class HashFunction;
48     class HMAC;
49     class Pipe;
50     class PK_Signing_Key;
51 }
52
53 namespace Utils {
54 namespace Internal {
55
56 class SshKeyExchange;
57
58 class SshAbstractCryptoFacility
59 {
60 public:
61     virtual ~SshAbstractCryptoFacility();
62
63     void clearKeys();
64     void recreateKeys(const SshKeyExchange &kex);
65     QByteArray generateMac(const QByteArray &data, quint32 dataSize) const;
66     quint32 cipherBlockSize() const { return m_cipherBlockSize; }
67     quint32 macLength() const { return m_macLength; }
68
69 protected:
70     SshAbstractCryptoFacility();
71     void convert(QByteArray &data, quint32 offset, quint32 dataSize) const;
72     QByteArray sessionId() const { return m_sessionId; }
73
74 private:
75     SshAbstractCryptoFacility(const SshAbstractCryptoFacility &);
76     SshAbstractCryptoFacility &operator=(const SshAbstractCryptoFacility &);
77
78     virtual QByteArray cryptAlgoName(const SshKeyExchange &kex) const=0;
79     virtual QByteArray hMacAlgoName(const SshKeyExchange &kex) const=0;
80     virtual Botan::BlockCipherMode *makeCipherMode(Botan::BlockCipher *cipher,
81         Botan::BlockCipherModePaddingMethod *paddingMethod,
82         const Botan::InitializationVector &iv,
83         const Botan::SymmetricKey &key)=0;
84     virtual char ivChar() const=0;
85     virtual char keyChar() const=0;
86     virtual char macChar() const=0;
87
88     QByteArray generateHash(const SshKeyExchange &kex, char c, quint32 length);
89     void checkInvariant() const;
90
91     QByteArray m_sessionId;
92     QScopedPointer<Botan::Pipe> m_pipe;
93     QScopedPointer<Botan::HMAC> m_hMac;
94     quint32 m_cipherBlockSize;
95     quint32 m_macLength;
96 };
97
98 class SshEncryptionFacility : public SshAbstractCryptoFacility
99 {
100 public:
101     void encrypt(QByteArray &data) const;
102
103     void createAuthenticationKey(const QByteArray &privKeyFileContents);
104     QByteArray authenticationAlgorithmName() const;
105     QByteArray authenticationPublicKey() const { return m_authPubKeyBlob; }
106     QByteArray authenticationKeySignature(const QByteArray &data) const;
107     QByteArray getRandomNumbers(int count) const;
108
109     ~SshEncryptionFacility();
110
111 private:
112     virtual QByteArray cryptAlgoName(const SshKeyExchange &kex) const;
113     virtual QByteArray hMacAlgoName(const SshKeyExchange &kex) const;
114     virtual Botan::BlockCipherMode *makeCipherMode(Botan::BlockCipher *cipher,
115         Botan::BlockCipherModePaddingMethod *paddingMethod,
116         const Botan::InitializationVector &iv, const Botan::SymmetricKey &key);
117     virtual char ivChar() const { return 'A'; }
118     virtual char keyChar() const { return 'C'; }
119     virtual char macChar() const { return 'E'; }
120
121     void createAuthenticationKeyFromPKCS8(const QByteArray &privKeyFileContents,
122         QList<Botan::BigInt> &pubKeyParams, QList<Botan::BigInt> &allKeyParams);
123     void createAuthenticationKeyFromOpenSSL(const QByteArray &privKeyFileContents,
124         QList<Botan::BigInt> &pubKeyParams, QList<Botan::BigInt> &allKeyParams);
125
126     static const QByteArray PrivKeyFileStartLineRsa;
127     static const QByteArray PrivKeyFileStartLineDsa;
128     static const QByteArray PrivKeyFileEndLineRsa;
129     static const QByteArray PrivKeyFileEndLineDsa;
130
131     QByteArray m_authKeyAlgoName;
132     QByteArray m_authPubKeyBlob;
133     QByteArray m_cachedPrivKeyContents;
134     QScopedPointer<Botan::PK_Signing_Key> m_authKey;
135     mutable Botan::AutoSeeded_RNG m_rng;
136 };
137
138 class SshDecryptionFacility : public SshAbstractCryptoFacility
139 {
140 public:
141     void decrypt(QByteArray &data, quint32 offset, quint32 dataSize) const;
142
143 private:
144     virtual QByteArray cryptAlgoName(const SshKeyExchange &kex) const;
145     virtual QByteArray hMacAlgoName(const SshKeyExchange &kex) const;
146     virtual Botan::BlockCipherMode *makeCipherMode(Botan::BlockCipher *cipher,
147         Botan::BlockCipherModePaddingMethod *paddingMethod,
148         const Botan::InitializationVector &iv, const Botan::SymmetricKey &key);
149     virtual char ivChar() const { return 'B'; }
150     virtual char keyChar() const { return 'D'; }
151     virtual char macChar() const { return 'F'; }
152 };
153
154 } // namespace Internal
155 } // namespace Utils
156
157 #endif // SSHABSTRACTCRYPTOFACILITY_P_H