Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////
  2. // OpenTibia - an opensource roleplaying game
  3. //////////////////////////////////////////////////////////////////////
  4. //
  5. //////////////////////////////////////////////////////////////////////
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU General Public License
  8. // as published by the Free Software Foundation; either version 2
  9. // of the License, or (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software Foundation,
  18. // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. //////////////////////////////////////////////////////////////////////
  20.  
  21.  
  22. #include "definitions.h"
  23.  
  24. #if defined WIN32
  25. #include <winerror.h>
  26. #endif
  27.  
  28. #include "protocol.h"
  29. #include "scheduler.h"
  30. #include "connection.h"
  31. #include "outputmessage.h"
  32. #include "rsa.h"
  33.  
  34. void Protocol::onSendMessage(OutputMessage_ptr msg)
  35. {
  36. #ifdef __DEBUG_NET_DETAIL__
  37. std::cout << "Protocol::onSendMessage" << std::endl;
  38. #endif
  39.  
  40. if(!m_rawMessages){
  41. msg->writeMessageLength();
  42.  
  43. if(m_encryptionEnabled){
  44. #ifdef __DEBUG_NET_DETAIL__
  45. std::cout << "Protocol::onSendMessage - encrypt" << std::endl;
  46. #endif
  47.  
  48. XTEA_encrypt(*msg);
  49. msg->addCryptoHeader();
  50. }
  51.  
  52. if(msg == m_outputBuffer){
  53. m_outputBuffer.reset();
  54. }
  55. }
  56. }
  57.  
  58. void Protocol::onRecvMessage(NetworkMessage& msg)
  59. {
  60. #ifdef __DEBUG_NET_DETAIL__
  61. std::cout << "Protocol::onRecvMessage" << std::endl;
  62. #endif
  63.  
  64. if(m_encryptionEnabled){
  65. #ifdef __DEBUG_NET_DETAIL__
  66. std::cout << "Protocol::onRecvMessage - decrypt" << std::endl;
  67. #endif
  68.  
  69. XTEA_decrypt(msg);
  70. }
  71.  
  72. parsePacket(msg);
  73. }
  74.  
  75. OutputMessage_ptr Protocol::getOutputBuffer()
  76. {
  77. if(m_outputBuffer && m_outputBuffer->getMessageLength() < 12672){
  78. return m_outputBuffer;
  79. }
  80. else if(m_connection){
  81. m_outputBuffer = OutputMessagePool::getInstance()->getOutputMessage(this);
  82. return m_outputBuffer;
  83. }
  84. else{
  85. return OutputMessage_ptr();
  86. }
  87. }
  88.  
  89. void Protocol::releaseProtocol()
  90. {
  91. if(m_refCount > 0){
  92. //Reschedule it and try again.
  93. Scheduler::getScheduler().addEvent( createSchedulerTask(SCHEDULER_MINTICKS,
  94. boost::bind(&Protocol::releaseProtocol, this)));
  95. }
  96. else{
  97. deleteProtocolTask();
  98. }
  99. }
  100.  
  101. void Protocol::deleteProtocolTask()
  102. {
  103. //dispather thread
  104. assert(m_refCount == 0);
  105. setConnection(NULL);
  106.  
  107. delete this;
  108. }
  109.  
  110. void Protocol::XTEA_encrypt(OutputMessage& msg)
  111. {
  112. uint32_t k[4];
  113. k[0] = m_key[0]; k[1] = m_key[1]; k[2] = m_key[2]; k[3] = m_key[3];
  114.  
  115. //add bytes until reach 8 multiple
  116. uint32_t n;
  117. if(((msg.getMessageLength() + 2) % 8) != 0){
  118. n = 8 - ((msg.getMessageLength() + 2) % 8);
  119. msg.AddPaddingBytes(n);
  120. }
  121.  
  122. int read_pos = 0;
  123. uint32_t* buffer = (uint32_t*)msg.getBodyBuffer();
  124. int32_t messageLength = msg.getMessageLength();
  125. while(read_pos < messageLength/4){
  126. uint32_t v0 = buffer[read_pos], v1 = buffer[read_pos + 1];
  127. uint32_t delta = 0x61C88647;
  128. uint32_t sum = 0;
  129.  
  130. for(int32_t i = 0; i<32; i++) {
  131. v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
  132. sum -= delta;
  133. v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
  134. }
  135. buffer[read_pos] = v0; buffer[read_pos + 1] = v1;
  136. read_pos = read_pos + 2;
  137. }
  138.  
  139. msg.addCryptoHeader();
  140. }
  141.  
  142. bool Protocol::XTEA_decrypt(NetworkMessage& msg)
  143. {
  144. if((msg.getMessageLength() - 2) % 8 != 0){
  145. std::cout << "Failure: [Protocol::XTEA_decrypt]. Not valid encrypted message size" << std::endl;
  146. return false;
  147. }
  148.  
  149. //
  150. uint32_t k[4];
  151. k[0] = m_key[0]; k[1] = m_key[1]; k[2] = m_key[2]; k[3] = m_key[3];
  152.  
  153. uint32_t* buffer = (uint32_t*)msg.getBodyBuffer();
  154. int read_pos = 0;
  155. int32_t messageLength = msg.getMessageLength();
  156. while(read_pos < messageLength/4){
  157. uint32_t v0 = buffer[read_pos], v1 = buffer[read_pos + 1];
  158. uint32_t delta = 0x61C88647;
  159. uint32_t sum = 0xC6EF3720;
  160.  
  161. for(int32_t i = 0; i<32; i++) {
  162. v1 -= ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
  163. sum += delta;
  164. v0 -= ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
  165. }
  166. buffer[read_pos] = v0; buffer[read_pos + 1] = v1;
  167. read_pos = read_pos + 2;
  168. }
  169. //
  170.  
  171. int tmp = msg.GetU16();
  172. if(tmp > msg.getMessageLength() - 4){
  173. std::cout << "Failure: [Protocol::XTEA_decrypt]. Not valid unencrypted message size" << std::endl;
  174. return false;
  175. }
  176.  
  177. msg.setMessageLength(tmp);
  178. return true;
  179. }
  180.  
  181. bool Protocol::RSA_decrypt(RSA* rsa, NetworkMessage& msg)
  182. {
  183. if(msg.getMessageLength() - msg.getReadPos() != 128){
  184. std::cout << "Warning: [Protocol::RSA_decrypt]. Not valid packet size" << std::endl;
  185. return false;
  186. }
  187.  
  188. if(!rsa->decrypt((char*)(msg.getBuffer() + msg.getReadPos()), 128)){
  189. return false;
  190. }
  191.  
  192. if(msg.GetByte() != 0){
  193. std::cout << "Warning: [Protocol::RSA_decrypt]. First byte != 0" << std::endl;
  194. return false;
  195. }
  196.  
  197. return true;
  198. }
  199.  
  200. uint32_t Protocol::getIP() const
  201. {
  202. if(getConnection()){
  203. return getConnection()->getIP();
  204. }
  205.  
  206. return 0;
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement