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-02-17 22:25:13 +00:00
|
|
|
|
|
|
|
|
static const CString SIGNATURE_APPEND = ".sig.rsa";
|
|
|
|
|
static const CString PRIVATE_KEY_PATH = "private.key.rsa";
|
|
|
|
|
static const CString PUBLIC_KEY_PATH = "public.key.rsa";
|
|
|
|
|
static const int KEY_LENGTH = 1024;
|
|
|
|
|
|
|
|
|
|
CryptManager* CRYPTMAN = NULL; // global and accessable from anywhere in our program
|
|
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
|
CryptManager::CryptManager() { }
|
|
|
|
|
CryptManager::~CryptManager() { }
|
|
|
|
|
void CryptManager::GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed ) { }
|
|
|
|
|
void CryptManager::SignFile( CString sPath ) { }
|
|
|
|
|
bool CryptManager::VerifyFile( CString sPath ) { return true; }
|
|
|
|
|
CString CryptManager::GetFileSignature( CString sPath ) { return ""; }
|
|
|
|
|
bool CryptManager::VerifyFile( CString sPath, CString sSignature ) { return true; }
|
|
|
|
|
void CryptManager::DigestFile(const char *filename) { }
|
|
|
|
|
#else
|
|
|
|
|
|
2004-02-15 04:47:32 +00:00
|
|
|
// crypt headers
|
2004-02-17 22:25:13 +00:00
|
|
|
#include "CryptHelpers.h"
|
2004-02-15 05:56:46 +00:00
|
|
|
#include "crypto51/sha.h"
|
|
|
|
|
#include "crypto51/hex.h"
|
|
|
|
|
#include "crypto51/channels.h"
|
|
|
|
|
#include "crypto51/rsa.h"
|
|
|
|
|
#include "crypto51/md5.h"
|
|
|
|
|
#include "crypto51/osrng.h"
|
2004-02-15 04:47:32 +00:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
|
|
using namespace CryptoPP;
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
2004-02-15 05:45:18 +00:00
|
|
|
CryptManager::CryptManager()
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
// generate keys if none are available
|
|
|
|
|
//
|
2004-02-15 23:34:31 +00:00
|
|
|
/* This is crashing in crypto51/integer.cpp CryptoPP::RecursiveInverseModPower2
|
|
|
|
|
* in Linux. -glenn */
|
2004-02-19 03:56:03 +00:00
|
|
|
// if( PREFSMAN->m_bSignProfileData )
|
|
|
|
|
// {
|
|
|
|
|
// 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-15 04:47:32 +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
|
|
|
void CryptManager::GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed )
|
2004-02-15 04:47:32 +00:00
|
|
|
{
|
2004-02-19 03:56:03 +00:00
|
|
|
ASSERT( PREFSMAN->m_bSignProfileData );
|
|
|
|
|
|
2004-02-15 05:45:18 +00:00
|
|
|
AutoSeededRandomPool rng;
|
2004-02-15 04:47:32 +00:00
|
|
|
|
2004-02-15 05:45:18 +00:00
|
|
|
RSAES_OAEP_SHA_Decryptor priv(rng, keyLength);
|
2004-02-15 04:47:32 +00:00
|
|
|
HexEncoder privFile(new RageFileSink(privFilename));
|
|
|
|
|
priv.DEREncode(privFile);
|
|
|
|
|
privFile.MessageEnd();
|
|
|
|
|
|
|
|
|
|
RSAES_OAEP_SHA_Encryptor pub(priv);
|
|
|
|
|
HexEncoder pubFile(new RageFileSink(pubFilename));
|
|
|
|
|
pub.DEREncode(pubFile);
|
|
|
|
|
pubFile.MessageEnd();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CryptManager::SignFile( CString sPath )
|
|
|
|
|
{
|
2004-02-19 03:56:03 +00:00
|
|
|
ASSERT( PREFSMAN->m_bSignProfileData );
|
|
|
|
|
|
2004-02-15 04:47:32 +00:00
|
|
|
CString sPrivFilename = PRIVATE_KEY_PATH;
|
|
|
|
|
CString sMessageFilename = sPath;;
|
2004-02-17 22:25:13 +00:00
|
|
|
CString sSignatureFilename = sPath + SIGNATURE_APPEND;
|
2004-02-15 04:47:32 +00:00
|
|
|
|
2004-02-16 09:23:34 +00:00
|
|
|
if( !IsAFile(sPrivFilename) )
|
|
|
|
|
return;
|
2004-02-15 05:45:18 +00:00
|
|
|
|
|
|
|
|
if( !IsAFile(sMessageFilename) )
|
2004-02-15 04:47:32 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// CAREFUL: These classes can throw all kinds of exceptions. Should this
|
|
|
|
|
// be wrapped in a try catch?
|
|
|
|
|
|
|
|
|
|
RageFileSource privFile(sPrivFilename, true, new HexDecoder);
|
|
|
|
|
RSASSA_PKCS1v15_SHA_Signer priv(privFile);
|
2004-02-15 05:45:18 +00:00
|
|
|
AutoSeededRandomPool rng;
|
2004-02-15 04:47:32 +00:00
|
|
|
RageFileSource f(sMessageFilename, true, new SignerFilter(rng, priv, new HexEncoder(new RageFileSink(sSignatureFilename))));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CryptManager::VerifyFile( CString sPath )
|
|
|
|
|
{
|
2004-02-19 03:56:03 +00:00
|
|
|
ASSERT( PREFSMAN->m_bSignProfileData );
|
|
|
|
|
|
2004-02-15 04:47:32 +00:00
|
|
|
CString sPubFilename = PUBLIC_KEY_PATH;
|
|
|
|
|
CString sMessageFilename = sPath;;
|
2004-02-17 22:25:13 +00:00
|
|
|
CString sSignatureFilename = sPath + SIGNATURE_APPEND;
|
2004-02-15 04:47:32 +00:00
|
|
|
|
2004-02-16 09:23:34 +00:00
|
|
|
if( !IsAFile(sPubFilename) )
|
|
|
|
|
return false;
|
2004-02-15 05:45:18 +00:00
|
|
|
|
|
|
|
|
if( !IsAFile(sSignatureFilename) )
|
2004-02-15 04:47:32 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// CAREFUL: These classes can throw all kinds of exceptions. Should this
|
|
|
|
|
// be wrapped in a try catch?
|
|
|
|
|
|
2004-02-17 22:25:13 +00:00
|
|
|
/* XXX: This is opening sPubFilename for RageFile::WRITE instead of READ. */
|
2004-02-15 04:47:32 +00:00
|
|
|
RageFileSource pubFile(sPubFilename, true, new HexDecoder);
|
|
|
|
|
RSASSA_PKCS1v15_SHA_Verifier pub(pubFile);
|
|
|
|
|
|
|
|
|
|
RageFileSource signatureFile(sSignatureFilename, true, new HexDecoder);
|
|
|
|
|
if (signatureFile.MaxRetrievable() != pub.SignatureLength())
|
|
|
|
|
return false;
|
|
|
|
|
SecByteBlock signature(pub.SignatureLength());
|
|
|
|
|
signatureFile.Get(signature, signature.size());
|
|
|
|
|
|
|
|
|
|
VerifierFilter *verifierFilter = new VerifierFilter(pub);
|
|
|
|
|
verifierFilter->Put(signature, pub.SignatureLength());
|
|
|
|
|
RageFileSource f(sMessageFilename, true, verifierFilter);
|
|
|
|
|
|
|
|
|
|
return verifierFilter->GetLastResult();
|
|
|
|
|
}
|
|
|
|
|
|
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-17 01:16:57 +00:00
|
|
|
CString sPrivFilename = PRIVATE_KEY_PATH;
|
|
|
|
|
CString sMessageFilename = sPath;
|
|
|
|
|
|
|
|
|
|
if( !IsAFile(sPrivFilename) )
|
|
|
|
|
return "";
|
|
|
|
|
|
|
|
|
|
if( !IsAFile(sMessageFilename) )
|
|
|
|
|
return "";
|
|
|
|
|
|
|
|
|
|
// CAREFUL: These classes can throw all kinds of exceptions. Should this
|
|
|
|
|
// be wrapped in a try catch?
|
|
|
|
|
|
|
|
|
|
RageFileSource privFile(sPrivFilename, true, new HexDecoder);
|
|
|
|
|
RSASSA_PKCS1v15_SHA_Signer priv(privFile);
|
|
|
|
|
AutoSeededRandomPool rng;
|
|
|
|
|
CString sSignature;
|
|
|
|
|
RageFileSource f(sMessageFilename, true, new SignerFilter(rng, priv, new HexEncoder(new StringSink(sSignature))));
|
|
|
|
|
return sSignature;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// CAREFUL: These classes can throw all kinds of exceptions. Should this
|
|
|
|
|
// be wrapped in a try catch?
|
|
|
|
|
|
|
|
|
|
RageFileSource pubFile(sPubFilename, true, new HexDecoder);
|
|
|
|
|
RSASSA_PKCS1v15_SHA_Verifier pub(pubFile);
|
|
|
|
|
|
|
|
|
|
StringSource signatureFile(sSignature, true);
|
|
|
|
|
if (signatureFile.MaxRetrievable() != pub.SignatureLength())
|
|
|
|
|
return false;
|
|
|
|
|
SecByteBlock signature(pub.SignatureLength());
|
|
|
|
|
signatureFile.Get(signature, signature.size());
|
|
|
|
|
|
|
|
|
|
VerifierFilter *verifierFilter = new VerifierFilter(pub);
|
|
|
|
|
verifierFilter->Put(signature, pub.SignatureLength());
|
|
|
|
|
RageFileSource f(sMessageFilename, true, verifierFilter);
|
|
|
|
|
|
|
|
|
|
return verifierFilter->GetLastResult();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-15 04:47:32 +00:00
|
|
|
void CryptManager::DigestFile(const char *filename)
|
|
|
|
|
{
|
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-17 22:25:13 +00:00
|
|
|
#endif
|
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;
|
|
|
|
|
}
|