PageRenderTime 28ms CodeModel.GetById 10ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 1ms

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