2004-02-15 04:47:32 +00:00
|
|
|
#include "global.h"
|
|
|
|
|
#include "CryptManager.h"
|
|
|
|
|
#include "RageUtil.h"
|
2004-02-15 05:45:18 +00:00
|
|
|
#include "RageLog.h"
|
2004-02-19 03:56:03 +00:00
|
|
|
#include "PrefsManager.h"
|
2004-02-20 02:28:23 +00:00
|
|
|
#include "RageFile.h"
|
2004-02-15 04:47:32 +00:00
|
|
|
|
2004-02-20 02:28:23 +00:00
|
|
|
#include "crypto/CryptRSA.h"
|
|
|
|
|
#include "crypto/CryptRand.h"
|
2004-02-17 22:25:13 +00:00
|
|
|
|
|
|
|
|
static const CString SIGNATURE_APPEND = ".sig.rsa";
|
2004-02-19 04:01:14 +00:00
|
|
|
static const CString PRIVATE_KEY_PATH = "Data/private.key.rsa";
|
|
|
|
|
static const CString PUBLIC_KEY_PATH = "Data/public.key.rsa";
|
2004-02-17 22:25:13 +00:00
|
|
|
static const int KEY_LENGTH = 1024;
|
|
|
|
|
|
|
|
|
|
CryptManager* CRYPTMAN = NULL; // global and accessable from anywhere in our program
|
|
|
|
|
|
2004-02-15 05:45:18 +00:00
|
|
|
CryptManager::CryptManager()
|
2004-02-15 04:47:32 +00:00
|
|
|
{
|
2004-02-20 02:28:23 +00:00
|
|
|
if( !PREFSMAN->m_bSignProfileData )
|
|
|
|
|
return;
|
|
|
|
|
|
2004-02-15 05:45:18 +00:00
|
|
|
//
|
|
|
|
|
// generate keys if none are available
|
|
|
|
|
//
|
2004-02-20 02:28:23 +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-15 05:45:18 +00:00
|
|
|
}
|
2004-02-15 04:47:32 +00:00
|
|
|
|
2004-02-15 05:45:18 +00:00
|
|
|
CryptManager::~CryptManager()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2004-02-20 02:28:23 +00:00
|
|
|
void CryptManager::GenerateRSAKey( unsigned int keyLength, CString privFilename, CString pubFilename, CString seed )
|
2004-02-15 05:45:18 +00:00
|
|
|
{
|
2004-02-19 03:56:03 +00:00
|
|
|
ASSERT( PREFSMAN->m_bSignProfileData );
|
|
|
|
|
|
2004-02-20 02:28:23 +00:00
|
|
|
random_init();
|
|
|
|
|
random_add_noise( seed );
|
2004-02-15 05:45:18 +00:00
|
|
|
|
2004-02-20 02:28:23 +00:00
|
|
|
RSAKey key;
|
|
|
|
|
key.Generate( keyLength );
|
2004-02-15 04:47:32 +00:00
|
|
|
|
2004-02-20 02:28:23 +00:00
|
|
|
RageFile out;
|
|
|
|
|
|
|
|
|
|
CString sPublic;
|
|
|
|
|
key.PublicBlob( sPublic );
|
|
|
|
|
if( !out.Open( pubFilename, RageFile::WRITE ) )
|
|
|
|
|
RageException::Throw( "Error opening %s: %s", pubFilename.c_str(), out.GetError().c_str() );
|
|
|
|
|
out.Write( sPublic );
|
|
|
|
|
out.Close();
|
|
|
|
|
|
|
|
|
|
CString sPrivate;
|
|
|
|
|
key.PrivateBlob( sPrivate );
|
|
|
|
|
if( !out.Open( privFilename, RageFile::WRITE ) )
|
|
|
|
|
RageException::Throw( "Error opening %s: %s", privFilename.c_str(), out.GetError().c_str() );
|
|
|
|
|
out.Write( sPrivate );
|
|
|
|
|
out.Close();
|
2004-02-15 04:47:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CryptManager::SignFile( CString sPath )
|
|
|
|
|
{
|
2004-02-20 02:28:23 +00:00
|
|
|
LOG->Trace("SignFile(%s)", sPath.c_str());
|
2004-02-19 03:56:03 +00:00
|
|
|
ASSERT( PREFSMAN->m_bSignProfileData );
|
|
|
|
|
|
2004-02-20 02:28:23 +00:00
|
|
|
if( !IsAFile(PRIVATE_KEY_PATH) )
|
2004-02-16 09:23:34 +00:00
|
|
|
return;
|
2004-02-15 05:45:18 +00:00
|
|
|
|
2004-02-20 02:28:23 +00:00
|
|
|
if( !IsAFile(sPath) )
|
2004-02-15 04:47:32 +00:00
|
|
|
return;
|
|
|
|
|
|
2004-02-20 02:28:23 +00:00
|
|
|
const CString sig = GetFileSignature( sPath );
|
2004-02-15 04:47:32 +00:00
|
|
|
|
2004-02-20 02:28:23 +00:00
|
|
|
RageFile out;
|
|
|
|
|
const CString sSignatureFilename = sPath + SIGNATURE_APPEND;
|
|
|
|
|
if( !out.Open( sSignatureFilename, RageFile::WRITE ) )
|
|
|
|
|
RageException::Throw( "Error opening %s: %s", sSignatureFilename.c_str(), out.GetError().c_str() );
|
|
|
|
|
out.Write( sig );
|
2004-02-15 04:47:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CryptManager::VerifyFile( CString sPath )
|
|
|
|
|
{
|
2004-02-20 02:28:23 +00:00
|
|
|
LOG->Trace("VerifyFile(%s)", sPath.c_str());
|
2004-02-19 03:56:03 +00:00
|
|
|
ASSERT( PREFSMAN->m_bSignProfileData );
|
|
|
|
|
|
2004-02-20 02:28:23 +00:00
|
|
|
if( !IsAFile(PUBLIC_KEY_PATH) )
|
2004-02-16 09:23:34 +00:00
|
|
|
return false;
|
2004-02-15 05:45:18 +00:00
|
|
|
|
2004-02-20 02:28:23 +00:00
|
|
|
const CString sSignatureFilename = sPath + SIGNATURE_APPEND;
|
2004-02-15 05:45:18 +00:00
|
|
|
if( !IsAFile(sSignatureFilename) )
|
2004-02-15 04:47:32 +00:00
|
|
|
return false;
|
|
|
|
|
|
2004-02-20 02:28:23 +00:00
|
|
|
CString sig;
|
|
|
|
|
{
|
|
|
|
|
RageFile in;
|
|
|
|
|
if( !in.Open( sSignatureFilename, RageFile::READ ) )
|
|
|
|
|
RageException::Throw( "Error opening %s: %s", sSignatureFilename.c_str(), in.GetError().c_str() );
|
|
|
|
|
in.Read( sig );
|
|
|
|
|
}
|
2004-02-15 04:47:32 +00:00
|
|
|
|
2004-02-20 02:28:23 +00:00
|
|
|
return VerifyFile( sPath, sig );
|
2004-02-15 04:47:32 +00:00
|
|
|
}
|
|
|
|
|
|
2004-02-17 01:16:57 +00:00
|
|
|
CString CryptManager::GetFileSignature( CString sPath )
|
|
|
|
|
{
|
2004-02-19 03:56:03 +00:00
|
|
|
ASSERT( PREFSMAN->m_bSignProfileData );
|
|
|
|
|
|
2004-02-20 02:28:23 +00:00
|
|
|
if( !IsAFile(PRIVATE_KEY_PATH) )
|
2004-02-17 01:16:57 +00:00
|
|
|
return "";
|
|
|
|
|
|
2004-02-20 02:28:23 +00:00
|
|
|
if( !IsAFile(sPath) )
|
2004-02-17 01:16:57 +00:00
|
|
|
return "";
|
|
|
|
|
|
2004-02-20 02:28:23 +00:00
|
|
|
CString data;
|
|
|
|
|
{
|
|
|
|
|
RageFile in;
|
|
|
|
|
if( !in.Open( sPath, RageFile::READ ) )
|
|
|
|
|
RageException::Throw( "Error opening %s: %s", sPath.c_str(), in.GetError().c_str() );
|
|
|
|
|
in.Read( data );
|
|
|
|
|
}
|
2004-02-17 01:16:57 +00:00
|
|
|
|
2004-02-20 02:28:23 +00:00
|
|
|
RSAKey key;
|
|
|
|
|
{
|
|
|
|
|
RageFile keyfile;
|
|
|
|
|
if( !keyfile.Open( PRIVATE_KEY_PATH ) )
|
|
|
|
|
RageException::Throw( "Error opening %s: %s", PRIVATE_KEY_PATH.c_str(), keyfile.GetError().c_str() );
|
|
|
|
|
CString private_blob;
|
|
|
|
|
keyfile.Read( private_blob );
|
|
|
|
|
key.LoadFromPrivateBlob( private_blob );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CString sig;
|
|
|
|
|
key.Sign( data, sig );
|
|
|
|
|
|
|
|
|
|
return sig;
|
2004-02-17 01:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CryptManager::VerifyFile( CString sPath, CString sSignature )
|
|
|
|
|
{
|
2004-02-19 03:56:03 +00:00
|
|
|
ASSERT( PREFSMAN->m_bSignProfileData );
|
|
|
|
|
|
2004-02-17 01:16:57 +00:00
|
|
|
CString sPubFilename = PUBLIC_KEY_PATH;
|
|
|
|
|
CString sMessageFilename = sPath;
|
|
|
|
|
|
|
|
|
|
if( !IsAFile(sPubFilename) )
|
|
|
|
|
return false;
|
|
|
|
|
|
2004-02-20 02:28:23 +00:00
|
|
|
CString data;
|
|
|
|
|
{
|
|
|
|
|
RageFile in;
|
|
|
|
|
if( !in.Open( sPath, RageFile::READ ) )
|
|
|
|
|
RageException::Throw( "Error opening %s: %s", sPath.c_str(), in.GetError().c_str() );
|
|
|
|
|
in.Read( data );
|
|
|
|
|
}
|
2004-02-17 01:16:57 +00:00
|
|
|
|
2004-02-20 02:28:23 +00:00
|
|
|
RSAKey key;
|
|
|
|
|
{
|
|
|
|
|
RageFile keyfile;
|
|
|
|
|
if( !keyfile.Open( PRIVATE_KEY_PATH ) )
|
|
|
|
|
RageException::Throw( "Error opening %s: %s", PRIVATE_KEY_PATH.c_str(), keyfile.GetError().c_str() );
|
|
|
|
|
CString private_blob;
|
|
|
|
|
keyfile.Read( private_blob );
|
|
|
|
|
key.LoadFromPrivateBlob( private_blob );
|
|
|
|
|
}
|
2004-02-17 01:16:57 +00:00
|
|
|
|
2004-02-20 02:28:23 +00:00
|
|
|
return key.Verify( data, sSignature );
|
2004-02-17 01:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-20 02:28:23 +00:00
|
|
|
void CryptManager::DigestFile( CString fn )
|
2004-02-15 04:47:32 +00:00
|
|
|
{
|
2004-02-19 03:56:03 +00:00
|
|
|
ASSERT( PREFSMAN->m_bSignProfileData );
|
|
|
|
|
|
2004-02-15 04:47:32 +00:00
|
|
|
// MD5 md5;
|
|
|
|
|
// HashFilter md5Filter(md5);
|
|
|
|
|
//
|
|
|
|
|
// auto_ptr<ChannelSwitch> channelSwitch(new ChannelSwitch);
|
|
|
|
|
// channelSwitch->AddDefaultRoute(md5Filter);
|
|
|
|
|
// RageFileSource(filename, true, channelSwitch.release());
|
|
|
|
|
//
|
|
|
|
|
// HexEncoder encoder(new RageFileSink(cout), false);
|
|
|
|
|
// cout << "\nMD5: ";
|
|
|
|
|
// md5Filter.TransferTo(encoder);
|
|
|
|
|
}
|
2004-02-16 05:35:06 +00:00
|
|
|
|
|
|
|
|
CString CryptManager::GetPublicKeyFileName()
|
|
|
|
|
{
|
2004-02-19 03:56:03 +00:00
|
|
|
ASSERT( PREFSMAN->m_bSignProfileData );
|
|
|
|
|
|
2004-02-16 05:35:06 +00:00
|
|
|
return PUBLIC_KEY_PATH;
|
|
|
|
|
}
|