/ghost-legacy/bncsutilinterface.cpp

http://ghostcb.googlecode.com/ · C++ · 161 lines · 101 code · 27 blank · 33 comment · 19 complexity · f9b250fb2af5af415c61aec71cccdddd MD5 · raw file

  1. /*
  2. Copyright [2008] [Trevor Hogan]
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. CODE PORTED FROM THE ORIGINAL GHOST PROJECT: http://ghost.pwner.org/
  13. */
  14. #include "ghost.h"
  15. #include "util.h"
  16. #include "bncsutilinterface.h"
  17. #include <bncsutil/bncsutil.h>
  18. //
  19. // CBNCSUtilInterface
  20. //
  21. CBNCSUtilInterface :: CBNCSUtilInterface( string userName, string userPassword )
  22. {
  23. // m_nls = (void *)nls_init( userName.c_str( ), userPassword.c_str( ) );
  24. m_NLS = new NLS( userName, userPassword );
  25. }
  26. CBNCSUtilInterface :: ~CBNCSUtilInterface( )
  27. {
  28. // nls_free( (nls_t *)m_nls );
  29. delete (NLS *)m_NLS;
  30. }
  31. void CBNCSUtilInterface :: Reset( string userName, string userPassword )
  32. {
  33. // nls_free( (nls_t *)m_nls );
  34. // m_nls = (void *)nls_init( userName.c_str( ), userPassword.c_str( ) );
  35. delete (NLS *)m_NLS;
  36. m_NLS = new NLS( userName, userPassword );
  37. }
  38. bool CBNCSUtilInterface :: HELP_SID_AUTH_CHECK( bool TFT, string war3Path, string keyROC, string keyTFT, string valueStringFormula, string mpqFileName, BYTEARRAY clientToken, BYTEARRAY serverToken )
  39. {
  40. // set m_EXEVersion, m_EXEVersionHash, m_EXEInfo, m_InfoROC, m_InfoTFT
  41. string FileWar3EXE = war3Path + "war3.exe";
  42. string FileStormDLL = war3Path + "Storm.dll";
  43. if( !UTIL_FileExists( FileStormDLL ) )
  44. FileStormDLL = war3Path + "storm.dll";
  45. string FileGameDLL = war3Path + "game.dll";
  46. bool ExistsWar3EXE = UTIL_FileExists( FileWar3EXE );
  47. bool ExistsStormDLL = UTIL_FileExists( FileStormDLL );
  48. bool ExistsGameDLL = UTIL_FileExists( FileGameDLL );
  49. if( ExistsWar3EXE && ExistsStormDLL && ExistsGameDLL )
  50. {
  51. // todotodo: check getExeInfo return value to ensure 1024 bytes was enough
  52. char buf[1024];
  53. uint32_t EXEVersion;
  54. getExeInfo( FileWar3EXE.c_str( ), (char *)&buf, 1024, (uint32_t *)&EXEVersion, BNCSUTIL_PLATFORM_X86 );
  55. m_EXEInfo = buf;
  56. m_EXEVersion = UTIL_CreateByteArray( EXEVersion, false );
  57. uint32_t EXEVersionHash;
  58. checkRevisionFlat( valueStringFormula.c_str( ), FileWar3EXE.c_str( ), FileStormDLL.c_str( ), FileGameDLL.c_str( ), extractMPQNumber( mpqFileName.c_str( ) ), (unsigned long *)&EXEVersionHash );
  59. m_EXEVersionHash = UTIL_CreateByteArray( EXEVersionHash, false );
  60. m_KeyInfoROC = CreateKeyInfo( keyROC, UTIL_ByteArrayToUInt32( clientToken, false ), UTIL_ByteArrayToUInt32( serverToken, false ) );
  61. if( TFT )
  62. m_KeyInfoTFT = CreateKeyInfo( keyTFT, UTIL_ByteArrayToUInt32( clientToken, false ), UTIL_ByteArrayToUInt32( serverToken, false ) );
  63. if( m_KeyInfoROC.size( ) == 36 && ( !TFT || m_KeyInfoTFT.size( ) == 36 ) )
  64. return true;
  65. else
  66. {
  67. if( m_KeyInfoROC.size( ) != 36 )
  68. CONSOLE_Print( "[BNCSUI] unable to create ROC key info - invalid ROC key" );
  69. if( TFT && m_KeyInfoTFT.size( ) != 36 )
  70. CONSOLE_Print( "[BNCSUI] unable to create TFT key info - invalid TFT key" );
  71. }
  72. }
  73. else
  74. {
  75. if( !ExistsWar3EXE )
  76. CONSOLE_Print( "[BNCSUI] unable to open [" + FileWar3EXE + "]" );
  77. if( !ExistsStormDLL )
  78. CONSOLE_Print( "[BNCSUI] unable to open [" + FileStormDLL + "]" );
  79. if( !ExistsGameDLL )
  80. CONSOLE_Print( "[BNCSUI] unable to open [" + FileGameDLL + "]" );
  81. }
  82. return false;
  83. }
  84. bool CBNCSUtilInterface :: HELP_SID_AUTH_ACCOUNTLOGON( )
  85. {
  86. // set m_ClientKey
  87. char buf[32];
  88. // nls_get_A( (nls_t *)m_nls, buf );
  89. ( (NLS *)m_NLS )->getPublicKey( buf );
  90. m_ClientKey = UTIL_CreateByteArray( (unsigned char *)buf, 32 );
  91. return true;
  92. }
  93. bool CBNCSUtilInterface :: HELP_SID_AUTH_ACCOUNTLOGONPROOF( BYTEARRAY salt, BYTEARRAY serverKey )
  94. {
  95. // set m_M1
  96. char buf[20];
  97. // nls_get_M1( (nls_t *)m_nls, buf, string( serverKey.begin( ), serverKey.end( ) ).c_str( ), string( salt.begin( ), salt.end( ) ).c_str( ) );
  98. ( (NLS *)m_NLS )->getClientSessionKey( buf, string( salt.begin( ), salt.end( ) ).c_str( ), string( serverKey.begin( ), serverKey.end( ) ).c_str( ) );
  99. m_M1 = UTIL_CreateByteArray( (unsigned char *)buf, 20 );
  100. return true;
  101. }
  102. bool CBNCSUtilInterface :: HELP_PvPGNPasswordHash( string userPassword )
  103. {
  104. // set m_PvPGNPasswordHash
  105. char buf[20];
  106. hashPassword( userPassword.c_str( ), buf );
  107. m_PvPGNPasswordHash = UTIL_CreateByteArray( (unsigned char *)buf, 20 );
  108. return true;
  109. }
  110. BYTEARRAY CBNCSUtilInterface :: CreateKeyInfo( string key, uint32_t clientToken, uint32_t serverToken )
  111. {
  112. unsigned char Zeros[] = { 0, 0, 0, 0 };
  113. BYTEARRAY KeyInfo;
  114. CDKeyDecoder Decoder( key.c_str( ), key.size( ) );
  115. if( Decoder.isKeyValid( ) )
  116. {
  117. UTIL_AppendByteArray( KeyInfo, UTIL_CreateByteArray( (uint32_t)key.size( ), false ) );
  118. UTIL_AppendByteArray( KeyInfo, UTIL_CreateByteArray( Decoder.getProduct( ), false ) );
  119. UTIL_AppendByteArray( KeyInfo, UTIL_CreateByteArray( Decoder.getVal1( ), false ) );
  120. UTIL_AppendByteArray( KeyInfo, UTIL_CreateByteArray( Zeros, 4 ) );
  121. size_t Length = Decoder.calculateHash( clientToken, serverToken );
  122. char *buf = new char[Length];
  123. Length = Decoder.getHash( buf );
  124. UTIL_AppendByteArray( KeyInfo, UTIL_CreateByteArray( (unsigned char *)buf, Length ) );
  125. delete [] buf;
  126. }
  127. return KeyInfo;
  128. }