1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #ifndef SSHABSTRACTCRYPTOFACILITY_P_H
34 #define SSHABSTRACTCRYPTOFACILITY_P_H
36 #include <botan/auto_rng.h>
37 #include <botan/symkey.h>
39 #include <QtCore/QByteArray>
40 #include <QtCore/QScopedPointer>
45 class BlockCipherMode;
46 class BlockCipherModePaddingMethod;
58 class SshAbstractCryptoFacility
61 virtual ~SshAbstractCryptoFacility();
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; }
70 SshAbstractCryptoFacility();
71 void convert(QByteArray &data, quint32 offset, quint32 dataSize) const;
72 QByteArray sessionId() const { return m_sessionId; }
75 SshAbstractCryptoFacility(const SshAbstractCryptoFacility &);
76 SshAbstractCryptoFacility &operator=(const SshAbstractCryptoFacility &);
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;
88 QByteArray generateHash(const SshKeyExchange &kex, char c, quint32 length);
89 void checkInvariant() const;
91 QByteArray m_sessionId;
92 QScopedPointer<Botan::Pipe> m_pipe;
93 QScopedPointer<Botan::HMAC> m_hMac;
94 quint32 m_cipherBlockSize;
98 class SshEncryptionFacility : public SshAbstractCryptoFacility
101 void encrypt(QByteArray &data) const;
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;
109 ~SshEncryptionFacility();
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'; }
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);
126 static const QByteArray PrivKeyFileStartLineRsa;
127 static const QByteArray PrivKeyFileStartLineDsa;
128 static const QByteArray PrivKeyFileEndLineRsa;
129 static const QByteArray PrivKeyFileEndLineDsa;
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;
138 class SshDecryptionFacility : public SshAbstractCryptoFacility
141 void decrypt(QByteArray &data, quint32 offset, quint32 dataSize) const;
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'; }
154 } // namespace Internal
157 #endif // SSHABSTRACTCRYPTOFACILITY_P_H