Upstream tarball 9445
[amule.git] / src / EncryptedStreamSocket.h
blob9fa858cdaaf2adc40ebd7472f776b5eca6f29322
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2008 Merkur ( devs@emule-project.net / http://www.emule-project.net )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 /* This class supports obfuscation and encryption for a *Mule tcp connection.
27 Right now only basic obfusication is supported, but this can be expanded, as their is a
28 dedicated handshake to negotiate the encryption method used.
30 Please note, even if obfuscation uses encryption methods, it does not fulfill cryptographic standards since it
31 doesn't use secret (and for rc4 important: unique) keys
34 #ifndef __ENCRYPTEDSTREAMSOCKET_H__
35 #define __ENCRYPTEDSTREAMSOCKET_H__
37 #include <wx/wx.h>
38 #include <wx/string.h>
40 // cryptoPP used for DH integer calculations
41 #include "CryptoPP_Inc.h" // Needed for Crypto functions
42 #include "Proxy.h"
43 #include "Types.h"
45 #include "RC4Encrypt.h"
47 #define ERR_WRONGHEADER 0x01
48 #define ERR_TOOBIG 0x02
49 #define ERR_ENCRYPTION 0x03
50 #define ERR_ENCRYPTION_NOTALLOWED 0x04
52 enum EStreamCryptState {
53 ECS_NONE = 0, // Disabled or not available
54 ECS_UNKNOWN, // Incoming connection, will test the first incoming data for encrypted protocol
55 ECS_PENDING, // Outgoing connection, will start sending encryption protocol
56 ECS_PENDING_SERVER, // Outgoing serverconnection, will start sending encryption protocol
57 ECS_NEGOTIATING, // Encryption supported, handshake still uncompleted
58 ECS_ENCRYPTING // Encryption enabled
61 enum ENegotiatingState {
62 ONS_NONE,
64 ONS_BASIC_CLIENTA_RANDOMPART,
65 ONS_BASIC_CLIENTA_MAGICVALUE,
66 ONS_BASIC_CLIENTA_METHODTAGSPADLEN,
67 ONS_BASIC_CLIENTA_PADDING,
69 ONS_BASIC_CLIENTB_MAGICVALUE,
70 ONS_BASIC_CLIENTB_METHODTAGSPADLEN,
71 ONS_BASIC_CLIENTB_PADDING,
73 ONS_BASIC_SERVER_DHANSWER,
74 ONS_BASIC_SERVER_MAGICVALUE,
75 ONS_BASIC_SERVER_METHODTAGSPADLEN,
76 ONS_BASIC_SERVER_PADDING,
77 ONS_BASIC_SERVER_DELAYEDSENDING,
79 ONS_COMPLETE
82 enum EEncryptionMethods {
83 ENM_OBFUSCATION = 0x00
86 class CRC4EncryptableBuffer;
88 class CEncryptedStreamSocket : public CSocketClientProxy
90 public:
91 CEncryptedStreamSocket(
92 wxSocketFlags flags = wxSOCKET_NONE,
93 const CProxyData *proxyData = NULL);
94 virtual ~CEncryptedStreamSocket();
96 void SetConnectionEncryption(
97 bool bEnabled,
98 const uint8 *pTargetClientHash,
99 bool bServerConnection);
101 //! Indicates how many bytes were received including obfusication
102 //! so that the parent knows if the receive limit was reached
103 uint32 GetRealReceivedBytes() const
105 return m_nObfusicationBytesReceived;
108 bool IsObfusicating() const
110 return m_StreamCryptState == ECS_ENCRYPTING &&
111 m_EncryptionMethod == ENM_OBFUSCATION;
114 bool IsServerCryptEnabledConnection() const { return m_bServerCrypt; }
116 uint8 m_dbgbyEncryptionSupported;
117 uint8 m_dbgbyEncryptionRequested;
118 uint8 m_dbgbyEncryptionMethodSet;
120 protected:
121 int Write(const void* lpBuf, wxUint32 nBufLen);
122 int Read(void* lpBuf, wxUint32 nBufLen);
124 virtual void OnError(int /*nErrorCode*/) {};
125 virtual void OnSend(int nErrorCode);
127 wxString DbgGetIPString();
128 void CryptPrepareSendData(uint8* pBuffer, uint32 nLen);
129 bool IsEncryptionLayerReady();
130 uint8 GetSemiRandomNotProtocolMarker() const;
132 uint32 m_nObfusicationBytesReceived;
133 EStreamCryptState m_StreamCryptState;
134 EEncryptionMethods m_EncryptionMethod;
135 bool m_bFullReceive;
136 bool m_bServerCrypt;
138 private:
139 int Negotiate(const uint8* pBuffer, uint32 nLen);
140 void StartNegotiation(bool bOutgoing);
141 int SendNegotiatingData(
142 const void *lpBuf,
143 uint32 nBufLen,
144 uint32 nStartCryptFromByte = 0,
145 bool bDelaySend = false);
147 ENegotiatingState m_NegotiatingState;
148 CRC4EncryptableBuffer m_pfiReceiveBuffer;
149 uint32 m_nReceiveBytesWanted;
150 CRC4EncryptableBuffer m_pfiSendBuffer;
151 uint32 m_nRandomKeyPart;
152 CryptoPP::Integer m_cryptDHA;
156 #endif // __ENCRYPTEDSTREAMSOCKET_H__