isolate low-level crypto stuff in CryptHelpers, so it can be used without dragging in PREFSMAN
This commit is contained in:
@@ -5,6 +5,55 @@
|
|||||||
|
|
||||||
// crypt headers
|
// crypt headers
|
||||||
#include "crypto51/files.h"
|
#include "crypto51/files.h"
|
||||||
|
#include "crypto51/filters.h"
|
||||||
|
#include "crypto51/cryptlib.h"
|
||||||
|
#include "crypto51/files.h"
|
||||||
|
#include "crypto51/sha.h"
|
||||||
|
#include "crypto51/rsa.h"
|
||||||
|
#include "crypto51/osrng.h"
|
||||||
|
|
||||||
|
using namespace CryptoPP;
|
||||||
|
|
||||||
|
class RageFileStore : public Store, private FilterPutSpaceHelper
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
class Err : public Exception
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Err(const std::string &s) : Exception(IO_ERROR, s) {}
|
||||||
|
};
|
||||||
|
class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileStore: error opening file for reading: " + filename) {}};
|
||||||
|
struct ReadErr : public Err { ReadErr( const RageFileBasic &f ); };
|
||||||
|
|
||||||
|
RageFileStore( RageFileBasic *pFile ); /* pFile will be deleted */
|
||||||
|
~RageFileStore();
|
||||||
|
RageFileStore( const RageFileStore &cpy );
|
||||||
|
RageFileStore(const char *filename)
|
||||||
|
{StoreInitialize(MakeParameters("InputFileName", filename));}
|
||||||
|
|
||||||
|
unsigned long MaxRetrievable() const;
|
||||||
|
unsigned int TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true);
|
||||||
|
unsigned int CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end=ULONG_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void StoreInitialize(const NameValuePairs ¶meters);
|
||||||
|
|
||||||
|
mutable RageFileBasic *m_pFile; // mutable because reading from a file is not a const operation
|
||||||
|
byte *m_space;
|
||||||
|
int m_len;
|
||||||
|
bool m_waiting;
|
||||||
|
};
|
||||||
|
|
||||||
|
class RageFileSource : public SourceTemplate<RageFileStore>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
typedef FileStore::Err Err;
|
||||||
|
typedef FileStore::OpenErr OpenErr;
|
||||||
|
typedef FileStore::ReadErr ReadErr;
|
||||||
|
|
||||||
|
RageFileSource( RageFileBasic *pFile, bool pumpAll, BufferedTransformation *attachment = NULL, bool binary=true )
|
||||||
|
: SourceTemplate<RageFileStore>(attachment,RageFileStore(pFile)) {SourceInitialize(pumpAll, MakeParameters("InputBinaryMode", binary));}
|
||||||
|
};
|
||||||
|
|
||||||
RageFileStore::ReadErr::ReadErr( const RageFileBasic &f ):
|
RageFileStore::ReadErr::ReadErr( const RageFileBasic &f ):
|
||||||
Err( "RageFileStore read error: " + f.GetError() )
|
Err( "RageFileStore read error: " + f.GetError() )
|
||||||
@@ -140,6 +189,68 @@ unsigned int RageFileStore::CopyRangeTo2(BufferedTransformation &target, unsigne
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CryptHelpers::GenerateRSAKey( unsigned int keyLength, CString sSeed, CString &sPublicKey, CString &sPrivateKey )
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
NonblockingRng rng;
|
||||||
|
|
||||||
|
RSASSA_PKCS1v15_SHA_Signer priv(rng, keyLength);
|
||||||
|
StringSink privFile( sPrivateKey );
|
||||||
|
priv.DEREncode(privFile);
|
||||||
|
privFile.MessageEnd();
|
||||||
|
|
||||||
|
RSASSA_PKCS1v15_SHA_Verifier pub(priv);
|
||||||
|
StringSink pubFile( sPublicKey );
|
||||||
|
pub.DEREncode(pubFile);
|
||||||
|
pubFile.MessageEnd();
|
||||||
|
} catch( const CryptoPP::Exception &s ) {
|
||||||
|
LOG->Warn( "GenerateRSAKey failed: %s", s.what() );
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CryptHelpers::SignFile( RageFileBasic &file, CString sPrivKey, CString &sSignatureOut, CString &sError )
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
StringSource privFile( sPrivKey, true );
|
||||||
|
RSASSA_PKCS1v15_SHA_Signer priv(privFile);
|
||||||
|
NonblockingRng rng;
|
||||||
|
|
||||||
|
/* RageFileSource will delete the file we give to it, so make a copy. */
|
||||||
|
RageFileSource f( file.Copy(), true, new SignerFilter(rng, priv, new StringSink(sSignatureOut)) );
|
||||||
|
} catch( const CryptoPP::Exception &s ) {
|
||||||
|
LOG->Warn( "SignFileToFile failed: %s", s.what() );
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CryptHelpers::VerifyFile( RageFileBasic &file, CString sSignature, CString sPublicKey, CString &sError )
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
StringSource pubFile( sPublicKey, true );
|
||||||
|
RSASSA_PKCS1v15_SHA_Verifier pub(pubFile);
|
||||||
|
|
||||||
|
if( sSignature.size() != pub.SignatureLength() )
|
||||||
|
return false;
|
||||||
|
|
||||||
|
VerifierFilter *verifierFilter = new VerifierFilter(pub);
|
||||||
|
verifierFilter->Put( (byte *) sSignature.data(), sSignature.size() );
|
||||||
|
|
||||||
|
/* RageFileSource will delete the file we give to it, so make a copy. */
|
||||||
|
RageFileSource f( file.Copy(), true, verifierFilter );
|
||||||
|
|
||||||
|
return verifierFilter->GetLastResult();
|
||||||
|
} catch( const CryptoPP::Exception &s ) {
|
||||||
|
sError = s.what();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* (c) 2001-2004 Chris Danford
|
* (c) 2001-2004 Chris Danford
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
|
|||||||
@@ -1,53 +1,13 @@
|
|||||||
#ifndef CryptHelpers_H
|
#ifndef CryptHelpers_H
|
||||||
#define CryptHelpers_H
|
#define CryptHelpers_H
|
||||||
|
|
||||||
// crypt headers
|
|
||||||
#include "crypto51/files.h"
|
|
||||||
#include "crypto51/filters.h"
|
|
||||||
#include "crypto51/cryptlib.h"
|
|
||||||
|
|
||||||
using namespace CryptoPP;
|
|
||||||
class RageFileBasic;
|
class RageFileBasic;
|
||||||
|
|
||||||
class RageFileStore : public Store, private FilterPutSpaceHelper
|
namespace CryptHelpers
|
||||||
{
|
{
|
||||||
public:
|
bool GenerateRSAKey( unsigned int keyLength, CString sSeed, CString &sPublicKey, CString &sPrivateKey );
|
||||||
class Err : public Exception
|
bool SignFile( RageFileBasic &file, CString sPrivKey, CString &sSignatureOut, CString &sError );
|
||||||
{
|
bool VerifyFile( RageFileBasic &file, CString sSignature, CString sPublicKey, CString &sError );
|
||||||
public:
|
|
||||||
Err(const std::string &s) : Exception(IO_ERROR, s) {}
|
|
||||||
};
|
|
||||||
class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileStore: error opening file for reading: " + filename) {}};
|
|
||||||
struct ReadErr : public Err { ReadErr( const RageFileBasic &f ); };
|
|
||||||
|
|
||||||
RageFileStore( RageFileBasic *pFile ); /* pFile will be deleted */
|
|
||||||
~RageFileStore();
|
|
||||||
RageFileStore( const RageFileStore &cpy );
|
|
||||||
RageFileStore(const char *filename)
|
|
||||||
{StoreInitialize(MakeParameters("InputFileName", filename));}
|
|
||||||
|
|
||||||
unsigned long MaxRetrievable() const;
|
|
||||||
unsigned int TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true);
|
|
||||||
unsigned int CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end=ULONG_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
void StoreInitialize(const NameValuePairs ¶meters);
|
|
||||||
|
|
||||||
mutable RageFileBasic *m_pFile; // mutable because reading from a file is not a const operation
|
|
||||||
byte *m_space;
|
|
||||||
int m_len;
|
|
||||||
bool m_waiting;
|
|
||||||
};
|
|
||||||
|
|
||||||
class RageFileSource : public SourceTemplate<RageFileStore>
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
typedef FileStore::Err Err;
|
|
||||||
typedef FileStore::OpenErr OpenErr;
|
|
||||||
typedef FileStore::ReadErr ReadErr;
|
|
||||||
|
|
||||||
RageFileSource( RageFileBasic *pFile, bool pumpAll, BufferedTransformation *attachment = NULL, bool binary=true )
|
|
||||||
: SourceTemplate<RageFileStore>(attachment,RageFileStore(pFile)) {SourceInitialize(pumpAll, MakeParameters("InputBinaryMode", binary));}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -34,12 +34,6 @@ bool CryptManager::Verify( CString sPath, CString sSignature )
|
|||||||
|
|
||||||
// crypt headers
|
// crypt headers
|
||||||
#include "CryptHelpers.h"
|
#include "CryptHelpers.h"
|
||||||
#include "crypto51/sha.h"
|
|
||||||
#include "crypto51/rsa.h"
|
|
||||||
#include "crypto51/osrng.h"
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
using namespace CryptoPP;
|
|
||||||
|
|
||||||
static const int KEY_LENGTH = 1024;
|
static const int KEY_LENGTH = 1024;
|
||||||
#define MAX_SIGNATURE_SIZE_BYTES 1024 // 1 KB
|
#define MAX_SIGNATURE_SIZE_BYTES 1024 // 1 KB
|
||||||
@@ -90,23 +84,9 @@ void CryptManager::GenerateRSAKey( unsigned int keyLength, CString privFilename,
|
|||||||
ASSERT( PREFSMAN->m_bSignProfileData );
|
ASSERT( PREFSMAN->m_bSignProfileData );
|
||||||
|
|
||||||
CString sPubKey, sPrivKey;
|
CString sPubKey, sPrivKey;
|
||||||
try
|
|
||||||
{
|
|
||||||
NonblockingRng rng;
|
|
||||||
|
|
||||||
RSASSA_PKCS1v15_SHA_Signer priv(rng, keyLength);
|
if( !CryptHelpers::GenerateRSAKey(keyLength, seed, sPubKey, sPrivKey) )
|
||||||
StringSink privFile( sPrivKey );
|
|
||||||
priv.DEREncode(privFile);
|
|
||||||
privFile.MessageEnd();
|
|
||||||
|
|
||||||
RSASSA_PKCS1v15_SHA_Verifier pub(priv);
|
|
||||||
StringSink pubFile( sPubKey );
|
|
||||||
pub.DEREncode(pubFile);
|
|
||||||
pubFile.MessageEnd();
|
|
||||||
} catch( const CryptoPP::Exception &s ) {
|
|
||||||
LOG->Warn( "GenerateRSAKey failed: %s", s.what() );
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
if( !WriteFile(pubFilename, sPubKey) )
|
if( !WriteFile(pubFilename, sPubKey) )
|
||||||
return;
|
return;
|
||||||
@@ -146,7 +126,7 @@ void CryptManager::SignFileToFile( CString sPath, CString sSignatureFile )
|
|||||||
|
|
||||||
CString sSignature;
|
CString sSignature;
|
||||||
CString sError;
|
CString sError;
|
||||||
if( !SignFile(file, sPrivKey, sSignature, sError) )
|
if( !CryptHelpers::SignFile(file, sPrivKey, sSignature, sError) )
|
||||||
{
|
{
|
||||||
LOG->Warn( "SignFileToFile failed: %s", sError.c_str() );
|
LOG->Warn( "SignFileToFile failed: %s", sError.c_str() );
|
||||||
return;
|
return;
|
||||||
@@ -155,23 +135,6 @@ void CryptManager::SignFileToFile( CString sPath, CString sSignatureFile )
|
|||||||
WriteFile( sSignatureFile, sSignature );
|
WriteFile( sSignatureFile, sSignature );
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CryptManager::SignFile( RageFileBasic &file, CString sPrivKey, CString &sSignatureOut, CString &sError )
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
StringSource privFile( sPrivKey, true );
|
|
||||||
RSASSA_PKCS1v15_SHA_Signer priv(privFile);
|
|
||||||
NonblockingRng rng;
|
|
||||||
|
|
||||||
/* RageFileSource will delete the file we give to it, so make a copy. */
|
|
||||||
RageFileSource f( file.Copy(), true, new SignerFilter(rng, priv, new StringSink(sSignatureOut)) );
|
|
||||||
} catch( const CryptoPP::Exception &s ) {
|
|
||||||
LOG->Warn( "SignFileToFile failed: %s", s.what() );
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CryptManager::VerifyFileWithFile( CString sPath, CString sSignatureFile )
|
bool CryptManager::VerifyFileWithFile( CString sPath, CString sSignatureFile )
|
||||||
{
|
{
|
||||||
ASSERT( PREFSMAN->m_bSignProfileData );
|
ASSERT( PREFSMAN->m_bSignProfileData );
|
||||||
@@ -221,7 +184,7 @@ bool CryptManager::VerifyFileWithFile( CString sPath, CString sSignatureFile, CS
|
|||||||
}
|
}
|
||||||
|
|
||||||
CString sError;
|
CString sError;
|
||||||
if( !VerifyFile(file, sSignature, sPublicKey, sError) )
|
if( !CryptHelpers::VerifyFile(file, sSignature, sPublicKey, sError) )
|
||||||
{
|
{
|
||||||
LOG->Warn( "VerifyFile(%s) failed: %s", sPath.c_str(), sError.c_str() );
|
LOG->Warn( "VerifyFile(%s) failed: %s", sPath.c_str(), sError.c_str() );
|
||||||
return false;
|
return false;
|
||||||
@@ -230,28 +193,6 @@ bool CryptManager::VerifyFileWithFile( CString sPath, CString sSignatureFile, CS
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CryptManager::VerifyFile( RageFileBasic &file, CString sSignature, CString sPublicKey, CString &sError )
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
StringSource pubFile( sPublicKey, true );
|
|
||||||
RSASSA_PKCS1v15_SHA_Verifier pub(pubFile);
|
|
||||||
|
|
||||||
if( sSignature.size() != pub.SignatureLength() )
|
|
||||||
return false;
|
|
||||||
|
|
||||||
VerifierFilter *verifierFilter = new VerifierFilter(pub);
|
|
||||||
verifierFilter->Put( (byte *) sSignature.data(), sSignature.size() );
|
|
||||||
|
|
||||||
/* RageFileSource will delete the file we give to it, so make a copy. */
|
|
||||||
RageFileSource f( file.Copy(), true, verifierFilter );
|
|
||||||
|
|
||||||
return verifierFilter->GetLastResult();
|
|
||||||
} catch( const CryptoPP::Exception &s ) {
|
|
||||||
sError = s.what();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CryptManager::Verify( CString sPath, CString sSignature )
|
bool CryptManager::Verify( CString sPath, CString sSignature )
|
||||||
{
|
{
|
||||||
ASSERT( PREFSMAN->m_bSignProfileData );
|
ASSERT( PREFSMAN->m_bSignProfileData );
|
||||||
@@ -271,7 +212,7 @@ bool CryptManager::Verify( CString sPath, CString sSignature )
|
|||||||
}
|
}
|
||||||
|
|
||||||
CString sError;
|
CString sError;
|
||||||
if( !VerifyFile(file, sSignature, sPublicKey, sError) )
|
if( !CryptHelpers::VerifyFile(file, sSignature, sPublicKey, sError) )
|
||||||
{
|
{
|
||||||
LOG->Warn( "Verify(%s) failed: %s", sPath.c_str(), sError.c_str() );
|
LOG->Warn( "Verify(%s) failed: %s", sPath.c_str(), sError.c_str() );
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -12,10 +12,7 @@ public:
|
|||||||
~CryptManager();
|
~CryptManager();
|
||||||
|
|
||||||
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 SignFile( RageFileBasic &file, CString sPrivKey, CString &sSignatureOut, CString &sError );
|
|
||||||
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