diff --git a/stepmania/src/CryptManager.cpp b/stepmania/src/CryptManager.cpp index bc9b7b0f8c..9f2a67337d 100644 --- a/stepmania/src/CryptManager.cpp +++ b/stepmania/src/CryptManager.cpp @@ -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 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); diff --git a/stepmania/src/CryptManager.h b/stepmania/src/CryptManager.h index ac328cc72b..29995b4d88 100644 --- a/stepmania/src/CryptManager.h +++ b/stepmania/src/CryptManager.h @@ -4,7 +4,10 @@ class CryptManager { public: - static void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed); + CryptManager(); + ~CryptManager(); + + static void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed ); static void SignFile( CString sPath ); static bool VerifyFile( CString sPath ); @@ -12,4 +15,6 @@ public: static void DigestFile(const char *filename); }; +extern CryptManager* CRYPTMAN; // global and accessable from anywhere in our program + #endif diff --git a/stepmania/src/FontManager.cpp b/stepmania/src/FontManager.cpp index 4615e339c7..6bc3b96da7 100644 --- a/stepmania/src/FontManager.cpp +++ b/stepmania/src/FontManager.cpp @@ -19,7 +19,7 @@ #include "RageException.h" #include -FontManager* FONT = NULL; +FontManager* FONT = NULL; // global and accessable from anywhere in our program // map from file name to a texture holder typedef pair FontName; diff --git a/stepmania/src/Makefile.am b/stepmania/src/Makefile.am index 6928f191f0..83cc1f03c0 100644 --- a/stepmania/src/Makefile.am +++ b/stepmania/src/Makefile.am @@ -211,7 +211,7 @@ ProfileManager.cpp ProfileManager.h ScreenManager.cpp ScreenManager.h SongManage UnlockSystem.cpp UnlockSystem.h cryptlib = \ -crypto51/algparam.cpp crypto51/asn.cpp crypto51/basecode.cpp crypto51/channels.cpp crypto51/cryptlib.cpp crypto51/des.cpp crypto51/dessp.cpp crypto51/files.cpp crypto51/filters.cpp crypto51/fips140.cpp crypto51/hex.cpp crypto51/integer.cpp crypto51/iterhash.cpp crypto51/md5.cpp crypto51/misc.cpp crypto51/modes.cpp crypto51/mqueue.cpp crypto51/nbtheory.cpp crypto51/pkcspad.cpp crypto51/pssr.cpp crypto51/pubkey.cpp crypto51/queue.cpp crypto51/randpool.cpp crypto51/rsa.cpp crypto51/sha.cpp crypto51/strciphr.cpp +crypto51/algparam.cpp crypto51/asn.cpp crypto51/basecode.cpp crypto51/channels.cpp crypto51/cryptlib.cpp crypto51/des.cpp crypto51/dessp.cpp crypto51/files.cpp crypto51/filters.cpp crypto51/fips140.cpp crypto51/hex.cpp crypto51/integer.cpp crypto51/iterhash.cpp crypto51/md5.cpp crypto51/misc.cpp crypto51/modes.cpp crypto51/mqueue.cpp crypto51/nbtheory.cpp crypto51/osrng.cpp crypto51/pkcspad.cpp crypto51/pssr.cpp crypto51/pubkey.cpp crypto51/queue.cpp crypto51/randpool.cpp crypto51/rsa.cpp crypto51/sha.cpp crypto51/strciphr.cpp stepmania_SOURCES = $(Screens) $(DataStructures) $(FileTypes) $(StepMania) $(Arch) \ $(ActorsInGameplayAndMenus) $(ActorsInMenus) $(ActorsInGameplay) \ diff --git a/stepmania/src/StepMania.cpp b/stepmania/src/StepMania.cpp index 6f97194c27..9261057941 100644 --- a/stepmania/src/StepMania.cpp +++ b/stepmania/src/StepMania.cpp @@ -32,7 +32,6 @@ #include "SDL_utils.h" #include "CodeDetector.h" -#include "CryptManager.h" // // StepMania global classes @@ -59,6 +58,7 @@ #include "Bookkeeper.h" #include "LightsManager.h" #include "ModelManager.h" +#include "CryptManager.h" #if defined(_XBOX) @@ -960,6 +960,7 @@ int main(int argc, char* argv[]) /* depends on SONGINDEX: */ SONGMAN = new SongManager( loading_window ); // this takes a long time to load + CRYPTMAN = new CryptManager; // need to do this before ProfileMan MEMCARDMAN = new MemoryCardManager; PROFILEMAN = new ProfileManager; // must load after SONGMAN UNLOCKSYS = new UnlockSystem; @@ -1044,6 +1045,7 @@ int main(int argc, char* argv[]) SAFE_DELETE( UNLOCKSYS ); SAFE_DELETE( MODELMAN ); SAFE_DELETE( PROFILEMAN ); // PROFILEMAN needs the songs still loaded + SAFE_DELETE( CRYPTMAN ); SAFE_DELETE( MEMCARDMAN ); SAFE_DELETE( SONGMAN ); SAFE_DELETE( BANNERCACHE ); diff --git a/stepmania/src/StepMania.dsp b/stepmania/src/StepMania.dsp index fa12deb750..5d196cade2 100644 --- a/stepmania/src/StepMania.dsp +++ b/stepmania/src/StepMania.dsp @@ -65,7 +65,7 @@ IntDir=.\../Debug6 TargetDir=\stepmania\stepmania\Program TargetName=StepMania-debug SOURCE="$(InputPath)" -PreLink_Cmds=disasm\verinc cl /Zl /nologo /c verstub.cpp /Fo$(IntDir)\ +PreLink_Cmds=disasm\verinc cl /Zl /nologo /c verstub.cpp /Fo$(IntDir)\ PostBuild_Cmds=disasm\mapconv $(IntDir)\$(TargetName).map $(TargetDir)\StepMania.vdi # End Special Build Tool @@ -142,7 +142,7 @@ IntDir=.\../Release6 TargetDir=\stepmania\stepmania\Program TargetName=StepMania SOURCE="$(InputPath)" -PreLink_Cmds=disasm\verinc cl /Zl /nologo /c verstub.cpp /Fo$(IntDir)\ +PreLink_Cmds=disasm\verinc cl /Zl /nologo /c verstub.cpp /Fo$(IntDir)\ PostBuild_Cmds=disasm\mapconv $(IntDir)\$(TargetName).map $(TargetDir)\StepMania.vdi # End Special Build Tool @@ -6416,6 +6416,21 @@ SOURCE=.\crypto51\nbtheory.cpp # End Source File # Begin Source File +SOURCE=.\crypto51\osrng.cpp + +!IF "$(CFG)" == "StepMania - Win32 Debug" + +!ELSEIF "$(CFG)" == "StepMania - Xbox Debug" + +!ELSEIF "$(CFG)" == "StepMania - Win32 Release" + +!ELSEIF "$(CFG)" == "StepMania - Xbox Release" + +!ENDIF + +# End Source File +# Begin Source File + SOURCE=.\crypto51\pkcspad.cpp !IF "$(CFG)" == "StepMania - Win32 Debug" diff --git a/stepmania/src/StepMania.vcproj b/stepmania/src/StepMania.vcproj index ff3fe1c73f..0d8aec01dd 100644 --- a/stepmania/src/StepMania.vcproj +++ b/stepmania/src/StepMania.vcproj @@ -2141,6 +2141,9 @@ cl /Zl /nologo /c verstub.cpp /Fo"$(IntDir)"\ + +