OSDN Git Service

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