split out crypto++-specific verify code; prefer to create our own RageFileBasic
This commit is contained in:
@@ -14,11 +14,12 @@ static const CString PRIVATE_KEY_PATH = "Data/private.rsa";
|
|||||||
static const CString PUBLIC_KEY_PATH = "Data/public.rsa";
|
static const CString PUBLIC_KEY_PATH = "Data/public.rsa";
|
||||||
static const CString ALTERNATE_PUBLIC_KEY_DIR = "Data/keys/";
|
static const CString ALTERNATE_PUBLIC_KEY_DIR = "Data/keys/";
|
||||||
|
|
||||||
#if !defined(HAVE_CRYPTOPP)
|
#if !defined(XHAVE_CRYPTOPP)
|
||||||
CryptManager::CryptManager() { }
|
CryptManager::CryptManager() { }
|
||||||
CryptManager::~CryptManager() { }
|
CryptManager::~CryptManager() { }
|
||||||
void CryptManager::GenerateRSAKey( unsigned int keyLength, CString privFilename, CString pubFilename, CString seed ) { }
|
void CryptManager::GenerateRSAKey( unsigned int keyLength, CString privFilename, CString pubFilename, CString seed ) { }
|
||||||
void CryptManager::SignFileToFile( CString sPath, CString sSignatureFile ) { }
|
void CryptManager::SignFileToFile( CString sPath, CString sSignatureFile ) { }
|
||||||
|
bool CryptManager::VerifyFile( RageFileBasic &file, CString sSignature, CString sPublicKey, CString &sError ) { return true; }
|
||||||
bool CryptManager::VerifyFileWithFile( CString sPath, CString sSignatureFile )
|
bool CryptManager::VerifyFileWithFile( CString sPath, CString sSignatureFile )
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
@@ -192,6 +193,25 @@ bool CryptManager::VerifyFileWithFile( CString sPath, CString sSignatureFile, CS
|
|||||||
if( !GetFileContents(sSignatureFile, sSignature) )
|
if( !GetFileContents(sSignatureFile, sSignature) )
|
||||||
return false;
|
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( !VerifyFile(file, sSignature, sPublicKey, sError) )
|
||||||
|
{
|
||||||
|
LOG->Warn( "VerifyFile failed: %s", sPath.c_str(), sError.c_str() );
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CryptManager::VerifyFile( RageFileBasic &file, CString sSignature, CString sPublicKey, CString &sError )
|
||||||
|
{
|
||||||
try {
|
try {
|
||||||
StringSource pubFile( sPublicKey, true );
|
StringSource pubFile( sPublicKey, true );
|
||||||
RSASSA_PKCS1v15_SHA_Verifier pub(pubFile);
|
RSASSA_PKCS1v15_SHA_Verifier pub(pubFile);
|
||||||
@@ -201,11 +221,13 @@ bool CryptManager::VerifyFileWithFile( CString sPath, CString sSignatureFile, CS
|
|||||||
|
|
||||||
VerifierFilter *verifierFilter = new VerifierFilter(pub);
|
VerifierFilter *verifierFilter = new VerifierFilter(pub);
|
||||||
verifierFilter->Put( (byte *) sSignature.data(), sSignature.size() );
|
verifierFilter->Put( (byte *) sSignature.data(), sSignature.size() );
|
||||||
RageFileSource f(sMessageFilename, true, verifierFilter);
|
|
||||||
|
/* RageFileSource will delete the file we give to it, so make a copy. */
|
||||||
|
RageFileSource f( file.Copy(), true, verifierFilter );
|
||||||
|
|
||||||
return verifierFilter->GetLastResult();
|
return verifierFilter->GetLastResult();
|
||||||
} catch( const CryptoPP::Exception &s ) {
|
} catch( const CryptoPP::Exception &s ) {
|
||||||
LOG->Warn( "VerifyFileWithFile(%s,%s) failed: %s", sPath.c_str(), sSignatureFile.c_str(), s.what() );
|
sError = s.what();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -221,22 +243,21 @@ bool CryptManager::Verify( CString sPath, CString sSignature )
|
|||||||
if( !GetFileContents(sPublicKeyFile, sPublicKey) )
|
if( !GetFileContents(sPublicKeyFile, sPublicKey) )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
try {
|
RageFile file;
|
||||||
StringSource pubFile( sPublicKey, true );
|
if( !file.Open(sPath) )
|
||||||
RSASSA_PKCS1v15_SHA_Verifier pub(pubFile);
|
{
|
||||||
|
LOG->Warn( "Verify: open(%s) failed: %s", sPath.c_str(), file.GetError().c_str() );
|
||||||
if( sSignature.size() != pub.SignatureLength() )
|
|
||||||
return false;
|
|
||||||
|
|
||||||
VerifierFilter *verifierFilter = new VerifierFilter(pub);
|
|
||||||
verifierFilter->Put( (byte *) sSignature.data(), sSignature.size() );
|
|
||||||
RageFileSource f(sMessageFilename, true, verifierFilter);
|
|
||||||
|
|
||||||
return verifierFilter->GetLastResult();
|
|
||||||
} catch( const CryptoPP::Exception &s ) {
|
|
||||||
LOG->Warn( "Verify(%s,sig) failed: %s", sPath.c_str(), s.what() );
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CString sError;
|
||||||
|
if( !VerifyFile(file, sSignature, sPublicKey, sError) )
|
||||||
|
{
|
||||||
|
LOG->Warn( "Verify failed: %s", sPath.c_str(), sError.c_str() );
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#ifndef CryptManager_H
|
#ifndef CryptManager_H
|
||||||
#define CryptManager_H
|
#define CryptManager_H
|
||||||
|
|
||||||
|
class RageFileBasic;
|
||||||
|
|
||||||
const CString SIGNATURE_APPEND = ".sig";
|
const CString SIGNATURE_APPEND = ".sig";
|
||||||
|
|
||||||
class CryptManager
|
class CryptManager
|
||||||
@@ -12,6 +14,7 @@ public:
|
|||||||
static void GenerateRSAKey( unsigned int keyLength, CString privFilename, CString pubFilename, CString seed );
|
static void GenerateRSAKey( unsigned int keyLength, CString privFilename, CString pubFilename, CString seed );
|
||||||
|
|
||||||
static void SignFileToFile( CString sPath, CString sSignatureFile = "" );
|
static void SignFileToFile( CString sPath, CString sSignatureFile = "" );
|
||||||
|
static bool VerifyFile( RageFileBasic &file, CString sSignature, CString sPublicKey, CString &sError );
|
||||||
static bool VerifyFileWithFile( CString sPath, CString sSignatureFile = "" );
|
static bool VerifyFileWithFile( CString sPath, CString sSignatureFile = "" );
|
||||||
static bool VerifyFileWithFile( CString sPath, CString sSignatureFile, CString sPublicKeyFile );
|
static bool VerifyFileWithFile( CString sPath, CString sSignatureFile, CString sPublicKeyFile );
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user