Files
itgmania212121/stepmania/src/CryptManager.cpp
T

293 lines
7.7 KiB
C++
Raw Normal View History

2004-02-15 04:47:32 +00:00
#include "global.h"
#include "CryptManager.h"
#include "RageUtil.h"
#include "RageLog.h"
2005-09-03 21:26:04 +00:00
#include "RageFile.h"
#include "PrefsManager.h"
2004-05-22 19:50:16 +00:00
#include "RageFileManager.h"
2005-04-27 01:17:47 +00:00
#include "crypto/CryptMD5.h"
2005-05-19 21:27:14 +00:00
#include "arch/arch_platform.h" // hack: HAVE_ stuff should not be defined by arch
2005-04-27 01:17:47 +00:00
CryptManager* CRYPTMAN = NULL; // global and accessable from anywhere in our program
static const CString PRIVATE_KEY_PATH = "Data/private.rsa";
static const CString PUBLIC_KEY_PATH = "Data/public.rsa";
2005-05-19 21:27:14 +00:00
static const CString ALTERNATE_PUBLIC_KEY_DIR = "Data/keys/";
2005-04-27 01:17:47 +00:00
#if !defined(HAVE_CRYPTOPP)
2005-04-27 01:17:47 +00:00
CryptManager::CryptManager() { }
CryptManager::~CryptManager() { }
void CryptManager::GenerateRSAKey( unsigned int keyLength, CString privFilename, CString pubFilename, CString seed ) { }
void CryptManager::SignFileToFile( CString sPath, CString sSignatureFile ) { }
2005-06-02 08:33:38 +00:00
bool CryptManager::VerifyFileWithFile( CString sPath, CString sSignatureFile, CString sPublicKeyFile ) { return true; }
2005-04-27 01:17:47 +00:00
bool CryptManager::VerifyFileWithFile( CString sPath, CString sSignatureFile )
{
return true;
}
bool CryptManager::Verify( CString sPath, CString sSignature )
{
return true;
}
#else
2004-02-15 04:47:32 +00:00
2004-05-13 06:02:32 +00:00
// crypt headers
#include "CryptHelpers.h"
2004-02-17 22:25:13 +00:00
static const int KEY_LENGTH = 1024;
2004-05-22 19:50:16 +00:00
#define MAX_SIGNATURE_SIZE_BYTES 1024 // 1 KB
2004-02-17 22:25:13 +00:00
CryptManager::CryptManager()
{
//
// generate keys if none are available
//
2004-05-13 06:02:32 +00:00
if( PREFSMAN->m_bSignProfileData )
2004-02-20 02:28:23 +00:00
{
2004-05-13 06:02:32 +00:00
if( !DoesFileExist(PRIVATE_KEY_PATH) || !DoesFileExist(PUBLIC_KEY_PATH) )
{
LOG->Warn( "Keys missing. Generating new keys" );
GenerateRSAKey( KEY_LENGTH, PRIVATE_KEY_PATH, PUBLIC_KEY_PATH, "aoksdjaksd" );
FlushDirCache();
}
2004-02-20 02:28:23 +00:00
}
}
2004-02-15 04:47:32 +00:00
CryptManager::~CryptManager()
{
2004-02-15 04:47:32 +00:00
}
2004-02-15 04:47:32 +00:00
static bool WriteFile( CString sFile, CString sBuf )
{
RageFile output;
if( !output.Open(sFile, RageFile::WRITE) )
{
LOG->Warn( "WriteFile: opening %s failed: %s", sFile.c_str(), output.GetError().c_str() );
return false;
}
if( output.Write(sBuf) == -1 || output.Flush() == -1 )
{
LOG->Warn( "WriteFile: writing %s failed: %s", sFile.c_str(), output.GetError().c_str() );
output.Close();
FILEMAN->Remove( sFile );
return false;
}
return true;
}
2004-02-20 02:28:23 +00:00
void CryptManager::GenerateRSAKey( unsigned int keyLength, CString privFilename, CString pubFilename, CString seed )
2004-02-15 04:47:32 +00:00
{
ASSERT( PREFSMAN->m_bSignProfileData );
CString sPubKey, sPrivKey;
if( !CryptHelpers::GenerateRSAKey(keyLength, seed, sPubKey, sPrivKey) )
return;
if( !WriteFile(pubFilename, sPubKey) )
return;
if( !WriteFile(privFilename, sPrivKey) )
{
FILEMAN->Remove( privFilename );
return;
}
2004-02-15 04:47:32 +00:00
}
2004-05-13 06:02:32 +00:00
void CryptManager::SignFileToFile( CString sPath, CString sSignatureFile )
2004-02-15 04:47:32 +00:00
{
ASSERT( PREFSMAN->m_bSignProfileData );
2004-05-13 06:02:32 +00:00
CString sPrivFilename = PRIVATE_KEY_PATH;
2004-05-14 00:58:05 +00:00
CString sMessageFilename = sPath;
2004-05-13 06:02:32 +00:00
if( sSignatureFile.empty() )
sSignatureFile = sPath + SIGNATURE_APPEND;
2005-05-20 00:58:09 +00:00
CString sPrivKey;
if( !GetFileContents(sPrivFilename, sPrivKey) )
return;
2004-05-13 06:02:32 +00:00
if( !IsAFile(sMessageFilename) )
2005-04-24 22:04:39 +00:00
{
LOG->Trace( "SignFileToFile: \"%s\" doesn't exist", sMessageFilename.c_str() );
2004-02-15 04:47:32 +00:00
return;
2005-04-24 22:04:39 +00:00
}
2004-02-15 04:47:32 +00:00
RageFile file;
if( !file.Open(sMessageFilename) )
{
LOG->Warn( "VerifyFileWithFile: open(%s) failed: %s", sPath.c_str(), file.GetError().c_str() );
return;
}
CString sSignature;
CString sError;
if( !CryptHelpers::SignFile(file, sPrivKey, sSignature, sError) )
{
LOG->Warn( "SignFileToFile failed: %s", sError.c_str() );
return;
}
WriteFile( sSignatureFile, sSignature );
}
2004-05-13 06:02:32 +00:00
bool CryptManager::VerifyFileWithFile( CString sPath, CString sSignatureFile )
2004-02-15 04:47:32 +00:00
{
ASSERT( PREFSMAN->m_bSignProfileData );
2005-05-19 21:27:14 +00:00
if( VerifyFileWithFile(sPath, sSignatureFile, PUBLIC_KEY_PATH) )
return true;
vector<CString> asKeys;
GetDirListing( ALTERNATE_PUBLIC_KEY_DIR, asKeys, false, true );
for( unsigned i = 0; i < asKeys.size(); ++i )
{
const CString &sKey = asKeys[i];
LOG->Trace( "Trying alternate key \"%s\" ...", sKey.c_str() );
if( VerifyFileWithFile(sPath, sSignatureFile, sKey) )
return true;
}
return false;
}
bool CryptManager::VerifyFileWithFile( CString sPath, CString sSignatureFile, CString sPublicKeyFile )
{
ASSERT( PREFSMAN->m_bSignProfileData );
2004-05-14 00:58:05 +00:00
CString sMessageFilename = sPath;
2004-05-13 06:02:32 +00:00
if( sSignatureFile.empty() )
sSignatureFile = sPath + SIGNATURE_APPEND;
CString sPublicKey;
if( !GetFileContents(sPublicKeyFile, sPublicKey) )
2004-02-15 04:47:32 +00:00
return false;
2004-05-22 19:50:16 +00:00
int iBytes = FILEMAN->GetFileSizeInBytes( sSignatureFile );
if( iBytes > MAX_SIGNATURE_SIZE_BYTES )
return false;
2005-05-20 01:02:56 +00:00
CString sSignature;
if( !GetFileContents(sSignatureFile, sSignature) )
return false;
RageFile file;
if( !file.Open(sPath) )
{
LOG->Warn( "VerifyFileWithFile: open(%s) failed: %s", sPath.c_str(), file.GetError().c_str() );
return false;
}
CString sError;
if( !CryptHelpers::VerifyFile(file, sSignature, sPublicKey, sError) )
{
2005-05-20 03:50:47 +00:00
LOG->Warn( "VerifyFile(%s) failed: %s", sPath.c_str(), sError.c_str() );
return false;
}
return true;
}
2004-02-22 02:15:34 +00:00
bool CryptManager::Verify( CString sPath, CString sSignature )
2004-02-17 01:16:57 +00:00
{
ASSERT( PREFSMAN->m_bSignProfileData );
2005-05-20 00:58:09 +00:00
CString sPublicKeyFile = PUBLIC_KEY_PATH;
2004-02-17 01:16:57 +00:00
CString sMessageFilename = sPath;
2005-05-20 00:58:09 +00:00
CString sPublicKey;
if( !GetFileContents(sPublicKeyFile, sPublicKey) )
2004-02-17 01:16:57 +00:00
return false;
RageFile file;
if( !file.Open(sPath) )
{
LOG->Warn( "Verify: open(%s) failed: %s", sPath.c_str(), file.GetError().c_str() );
return false;
}
2004-05-13 06:02:32 +00:00
CString sError;
if( !CryptHelpers::VerifyFile(file, sSignature, sPublicKey, sError) )
{
2005-05-20 03:50:47 +00:00
LOG->Warn( "Verify(%s) failed: %s", sPath.c_str(), sError.c_str() );
2004-05-13 23:40:55 +00:00
return false;
}
return true;
2004-02-22 02:15:34 +00:00
}
2005-04-27 01:17:47 +00:00
#endif
2004-02-17 01:16:57 +00:00
2004-05-14 00:31:53 +00:00
static CString BinaryToHex( const unsigned char *string, int iNumBytes )
{
CString s;
for( int i=0; i<iNumBytes; i++ )
{
unsigned val = string[i];
s += ssprintf( "%x", val );
}
return s;
}
2004-05-13 06:02:32 +00:00
2004-02-22 02:15:34 +00:00
CString CryptManager::GetMD5( CString fn )
2004-02-15 04:47:32 +00:00
{
2004-05-14 00:31:53 +00:00
struct MD5Context md5c;
unsigned char digest[16];
int iBytesRead;
unsigned char buffer[1024];
RageFile file;
if( !file.Open( fn, RageFile::READ ) )
{
LOG->Warn( "GetMD5: Failed to open file '%s'", fn.c_str() );
return CString();
2004-05-14 00:31:53 +00:00
}
MD5Init(&md5c);
while( !file.AtEOF() && file.GetError().empty() )
{
iBytesRead = file.Read( buffer, sizeof(buffer) );
MD5Update(&md5c, buffer, iBytesRead);
}
MD5Final(digest, &md5c);
return BinaryToHex( digest, sizeof(digest) );
2004-02-15 04:47:32 +00:00
}
2004-02-16 05:35:06 +00:00
CString CryptManager::GetPublicKeyFileName()
{
ASSERT( PREFSMAN->m_bSignProfileData );
2004-02-16 05:35:06 +00:00
return PUBLIC_KEY_PATH;
}
2004-06-08 01:24:17 +00:00
/*
* (c) 2004 Chris Danford
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/