create keypair on startup if one doesn't already exist
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#include "CryptManager.h"
|
||||
#include "RageUtil.h"
|
||||
#include "CryptHelpers.h"
|
||||
#include "RageLog.h"
|
||||
|
||||
// crypt headers
|
||||
#include "sha.h"
|
||||
@@ -9,7 +10,7 @@
|
||||
#include "channels.h"
|
||||
#include "rsa.h"
|
||||
#include "md5.h"
|
||||
#include "randpool.h"
|
||||
#include "osrng.h"
|
||||
#include <memory>
|
||||
|
||||
using namespace CryptoPP;
|
||||
@@ -18,21 +19,33 @@ using namespace std;
|
||||
static const CString SIGNATURE_POSPEND = ".sig.rsa";
|
||||
static const CString PRIVATE_KEY_PATH = "private.rsa";
|
||||
static const CString PUBLIC_KEY_PATH = "public.rsa";
|
||||
static const int KEY_LENGTH = 1024;
|
||||
|
||||
CryptManager* CRYPTMAN = NULL; // global and accessable from anywhere in our program
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void CryptManager::GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed)
|
||||
CryptManager::CryptManager()
|
||||
{
|
||||
RandomPool randPool;
|
||||
randPool.Put((byte *)seed, strlen(seed));
|
||||
//
|
||||
// generate keys if none are available
|
||||
//
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
RSAES_OAEP_SHA_Decryptor priv(randPool, keyLength);
|
||||
CryptManager::~CryptManager()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void CryptManager::GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed )
|
||||
{
|
||||
AutoSeededRandomPool rng;
|
||||
|
||||
RSAES_OAEP_SHA_Decryptor priv(rng, keyLength);
|
||||
HexEncoder privFile(new RageFileSink(privFilename));
|
||||
priv.DEREncode(privFile);
|
||||
privFile.MessageEnd();
|
||||
@@ -49,16 +62,17 @@ void CryptManager::SignFile( CString sPath )
|
||||
CString sMessageFilename = sPath;;
|
||||
CString sSignatureFilename = sPath + SIGNATURE_POSPEND;
|
||||
|
||||
if( !IsAFile(sMessageFilename) || !IsAFile(sPrivFilename) )
|
||||
ASSERT( IsAFile(sPrivFilename) );
|
||||
|
||||
if( !IsAFile(sMessageFilename) )
|
||||
return;
|
||||
|
||||
// CAREFUL: These classes can throw all kinds of exceptions. Should this
|
||||
// be wrapped in a try catch?
|
||||
|
||||
// TODO: use RageFile here
|
||||
RageFileSource privFile(sPrivFilename, true, new HexDecoder);
|
||||
RSASSA_PKCS1v15_SHA_Signer priv(privFile);
|
||||
RandomNumberGenerator &rng = RandomPool();
|
||||
AutoSeededRandomPool rng;
|
||||
RageFileSource f(sMessageFilename, true, new SignerFilter(rng, priv, new HexEncoder(new RageFileSink(sSignatureFilename))));
|
||||
}
|
||||
|
||||
@@ -68,13 +82,14 @@ bool CryptManager::VerifyFile( CString sPath )
|
||||
CString sMessageFilename = sPath;;
|
||||
CString sSignatureFilename = sPath + SIGNATURE_POSPEND;
|
||||
|
||||
if( !IsAFile(sSignatureFilename) || !IsAFile(sPubFilename) )
|
||||
ASSERT( IsAFile(sPubFilename) );
|
||||
|
||||
if( !IsAFile(sSignatureFilename) )
|
||||
return false;
|
||||
|
||||
// CAREFUL: These classes can throw all kinds of exceptions. Should this
|
||||
// be wrapped in a try catch?
|
||||
|
||||
// TODO: use RageFile here
|
||||
RageFileSource pubFile(sPubFilename, true, new HexDecoder);
|
||||
RSASSA_PKCS1v15_SHA_Verifier pub(pubFile);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user