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-15 04:47:32 +00:00
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
// crypt headers
|
|
|
|
|
#include "CryptHelpers.h"
|
|
|
|
|
#include "crypto51/sha.h"
|
|
|
|
|
#include "crypto51/channels.h"
|
|
|
|
|
#include "crypto51/hex.h"
|
|
|
|
|
#include "crypto51/rsa.h"
|
|
|
|
|
#include "crypto51/md5.h"
|
|
|
|
|
#include "crypto51/osrng.h"
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
|
|
using namespace CryptoPP;
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
#pragma comment(lib, "crypto51\\Release\\cryptlib.lib")
|
|
|
|
|
#else
|
|
|
|
|
#pragma comment(lib, "crypto51\\Debug\\cryptlib.lib")
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
2004-02-17 22:25:13 +00:00
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
// generate keys if none are available
|
|
|
|
|
//
|
2004-05-13 06:02:32 +00:00
|
|
|
/* This is crashing in crypto51/integer.cpp CryptoPP::RecursiveInverseModPower2
|
|
|
|
|
* in Linux. -glenn */
|
|
|
|
|
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 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-15 04:47:32 +00:00
|
|
|
|
2004-02-15 05:45:18 +00:00
|
|
|
}
|
2004-02-15 04:47:32 +00:00
|
|
|
|
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
|
|
|
{
|
2004-02-19 03:56:03 +00:00
|
|
|
ASSERT( PREFSMAN->m_bSignProfileData );
|
|
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
AutoSeededRandomPool rng;
|
2004-02-15 04:47:32 +00:00
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
RSAES_OAEP_SHA_Decryptor priv(rng, keyLength);
|
|
|
|
|
RageFileSink privFile(privFilename);
|
|
|
|
|
priv.DEREncode(privFile);
|
|
|
|
|
privFile.MessageEnd();
|
2004-02-20 02:28:23 +00:00
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
RSAES_OAEP_SHA_Encryptor pub(priv);
|
|
|
|
|
RageFileSink pubFile(pubFilename);
|
|
|
|
|
pub.DEREncode(pubFile);
|
|
|
|
|
pubFile.MessageEnd();
|
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
|
|
|
{
|
2004-02-19 03:56:03 +00:00
|
|
|
ASSERT( PREFSMAN->m_bSignProfileData );
|
|
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
CString sPrivFilename = PRIVATE_KEY_PATH;
|
|
|
|
|
CString sMessageFilename = sPath;;
|
|
|
|
|
if( sSignatureFile.empty() )
|
|
|
|
|
sSignatureFile = sPath + SIGNATURE_APPEND;
|
|
|
|
|
|
|
|
|
|
if( !IsAFile(sPrivFilename) )
|
2004-02-16 09:23:34 +00:00
|
|
|
return;
|
2004-02-15 05:45:18 +00:00
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
if( !IsAFile(sMessageFilename) )
|
2004-02-15 04:47:32 +00:00
|
|
|
return;
|
|
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
// CAREFUL: These classes can throw all kinds of exceptions. Should this
|
|
|
|
|
// be wrapped in a try catch?
|
2004-02-15 04:47:32 +00:00
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
RageFileSource privFile(sPrivFilename, true);
|
|
|
|
|
RSASSA_PKCS1v15_SHA_Signer priv(privFile);
|
|
|
|
|
AutoSeededRandomPool rng;
|
|
|
|
|
RageFileSource f(sMessageFilename, true, new SignerFilter(rng, priv, new RageFileSink(sSignatureFile)));
|
2004-02-15 04:47:32 +00:00
|
|
|
}
|
|
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
bool CryptManager::VerifyFileWithFile( CString sPath, CString sSignatureFile )
|
2004-02-15 04:47:32 +00:00
|
|
|
{
|
2004-02-19 03:56:03 +00:00
|
|
|
ASSERT( PREFSMAN->m_bSignProfileData );
|
|
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
CString sPubFilename = PUBLIC_KEY_PATH;
|
|
|
|
|
CString sMessageFilename = sPath;;
|
|
|
|
|
if( sSignatureFile.empty() )
|
|
|
|
|
sSignatureFile = sPath + SIGNATURE_APPEND;
|
2004-02-15 05:45:18 +00:00
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
if( !IsAFile(sPubFilename) )
|
2004-02-15 04:47:32 +00:00
|
|
|
return false;
|
|
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
if( !IsAFile(sSignatureFile) )
|
|
|
|
|
return false;
|
2004-02-17 01:16:57 +00:00
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
// CAREFUL: These classes can throw all kinds of exceptions. Should this
|
|
|
|
|
// be wrapped in a try catch?
|
2004-02-17 01:16:57 +00:00
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
/* XXX: This is opening sPubFilename for RageFile::WRITE instead of READ. */
|
|
|
|
|
RageFileSource pubFile(sPubFilename, true);
|
|
|
|
|
RSASSA_PKCS1v15_SHA_Verifier pub(pubFile);
|
2004-02-20 02:28:23 +00:00
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
RageFileSource signatureFile(sSignatureFile, true);
|
|
|
|
|
if (signatureFile.MaxRetrievable() != pub.SignatureLength())
|
|
|
|
|
return false;
|
|
|
|
|
SecByteBlock signature(pub.SignatureLength());
|
|
|
|
|
signatureFile.Get(signature, signature.size());
|
2004-02-20 02:28:23 +00:00
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
VerifierFilter *verifierFilter = new VerifierFilter(pub);
|
|
|
|
|
verifierFilter->Put(signature, pub.SignatureLength());
|
|
|
|
|
RageFileSource f(sMessageFilename, true, verifierFilter);
|
2004-02-20 02:28:23 +00:00
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
return verifierFilter->GetLastResult();
|
2004-02-17 01:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
2004-02-22 02:15:34 +00:00
|
|
|
bool CryptManager::Verify( CString sPath, CString sSignature )
|
2004-02-17 01:16:57 +00:00
|
|
|
{
|
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-05-13 06:02:32 +00:00
|
|
|
// CAREFUL: These classes can throw all kinds of exceptions. Should this
|
|
|
|
|
// be wrapped in a try catch?
|
2004-02-20 02:28:23 +00:00
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
RageFileSource pubFile(sPubFilename, true);
|
|
|
|
|
RSASSA_PKCS1v15_SHA_Verifier pub(pubFile);
|
2004-02-20 02:28:23 +00:00
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
StringSource signatureFile(sSignature, true);
|
|
|
|
|
if (signatureFile.MaxRetrievable() != pub.SignatureLength())
|
|
|
|
|
return false;
|
|
|
|
|
SecByteBlock signature(pub.SignatureLength());
|
|
|
|
|
signatureFile.Get(signature, signature.size());
|
2004-02-17 01:16:57 +00:00
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
VerifierFilter *verifierFilter = new VerifierFilter(pub);
|
|
|
|
|
verifierFilter->Put(signature, pub.SignatureLength());
|
|
|
|
|
RageFileSource f(sMessageFilename, true, verifierFilter);
|
|
|
|
|
|
|
|
|
|
return verifierFilter->GetLastResult();
|
2004-02-22 02:15:34 +00:00
|
|
|
}
|
2004-02-17 01:16:57 +00:00
|
|
|
|
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-13 06:02:32 +00:00
|
|
|
ASSERT( PREFSMAN->m_bSignProfileData );
|
2004-02-22 02:15:34 +00:00
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
MD5 md5;
|
|
|
|
|
HashFilter md5Filter(md5);
|
2004-02-22 02:15:34 +00:00
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
auto_ptr<ChannelSwitch> channelSwitch(new ChannelSwitch);
|
|
|
|
|
channelSwitch->AddDefaultRoute(md5Filter);
|
|
|
|
|
RageFileSource(fn, true, channelSwitch.release());
|
|
|
|
|
|
|
|
|
|
HexEncoder encoder(new RageFileSink("temp.txt"), false);
|
|
|
|
|
cout << "\nMD5: ";
|
|
|
|
|
md5Filter.TransferTo(encoder);
|
2004-02-22 02:15:34 +00:00
|
|
|
|
2004-05-13 06:02:32 +00:00
|
|
|
ASSERT(0);
|
|
|
|
|
return "";
|
2004-02-15 04:47:32 +00:00
|
|
|
}
|
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;
|
|
|
|
|
}
|