From 024f14efa4ff90e4391a48af5a841fdc5726d92a Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Sun, 29 Apr 2012 16:48:51 -0700 Subject: [PATCH] Store DHParams in sqlite. --- src/DBInit.cpp | 7 +++---- src/PeerDoor.cpp | 31 +++---------------------------- src/Wallet.cpp | 28 +++++++++++++++++++++++++--- src/Wallet.h | 9 +++++++-- 4 files changed, 38 insertions(+), 37 deletions(-) diff --git a/src/DBInit.cpp b/src/DBInit.cpp index 57b0e3a0e..27304efd7 100644 --- a/src/DBInit.cpp +++ b/src/DBInit.cpp @@ -56,12 +56,11 @@ const char *WalletDBInit[] = { Comment TEXT \ );", - // XXX Don't really need this. - // We should generate communication identity per launch. - // Validation id is provided via rpc or stored in config. "CREATE TABLE NodeIdentity ( \ PublicKey CHARACTER(53), \ - PrivateKey CHARACTER(52) \ + PrivateKey CHARACTER(52), \ + Dh512 TEXT, \ + Dh1024 TEXT \ );", // Miscellaneous persistent information diff --git a/src/PeerDoor.cpp b/src/PeerDoor.cpp index 13dc15e5b..60f40b969 100644 --- a/src/PeerDoor.cpp +++ b/src/PeerDoor.cpp @@ -6,9 +6,10 @@ #include #include //#include -#include +#include "Application.h" #include "Config.h" +#include "utils.h" using namespace std; using namespace boost::asio::ip; @@ -16,33 +17,7 @@ using namespace boost::asio::ip; // Generate DH for SSL connection. static DH* handleTmpDh(SSL* ssl, int is_export, int iKeyLength) { - // We don't care if for export. - static DH* sdh512 = 0; - static DH* sdh1024 = 0; - - if (!sdh512 && 512 == iKeyLength) - { - int iCodes; - - do { - sdh512 = DH_generate_parameters(512, DH_GENERATOR_5, NULL, NULL); - iCodes = 0; - DH_check(sdh512, &iCodes); - } while (iCodes & (DH_CHECK_P_NOT_PRIME|DH_CHECK_P_NOT_SAFE_PRIME|DH_UNABLE_TO_CHECK_GENERATOR|DH_NOT_SUITABLE_GENERATOR)); - } - - if (!sdh1024 && 512 != iKeyLength) - { - int iCodes; - - do { - sdh1024 = DH_generate_parameters(1024, DH_GENERATOR_5, NULL, NULL); - iCodes = 0; - DH_check(sdh1024, &iCodes); - } while (iCodes & (DH_CHECK_P_NOT_PRIME|DH_CHECK_P_NOT_SAFE_PRIME|DH_UNABLE_TO_CHECK_GENERATOR|DH_NOT_SUITABLE_GENERATOR)); - } - - return 512 == iKeyLength ? sdh512 : sdh1024; + return 512 == iKeyLength ? theApp->getWallet().getDh512() : theApp->getWallet().getDh1024(); } PeerDoor::PeerDoor(boost::asio::io_service& io_service) : diff --git a/src/Wallet.cpp b/src/Wallet.cpp index dae7b2ef2..5ed60a168 100644 --- a/src/Wallet.cpp +++ b/src/Wallet.cpp @@ -14,6 +14,7 @@ #include "Ledger.h" #include "NewcoinAddress.h" #include "Application.h" +#include "utils.h" // TEMPORARY #ifndef CHECK_NEW_FAMILIES @@ -381,6 +382,14 @@ bool Wallet::nodeIdentityLoad() mNodePublicKey.setNodePublic(strPublicKey); mNodePrivateKey.setNodePrivate(strPrivateKey); + std::string strDh512, strDh1024; + + db->getStr("Dh512", strDh512); + db->getStr("Dh1024", strDh1024); + + mDh512 = DH_der_load_hex(strDh512); + mDh1024 = DH_der_load_hex(strDh1024); + db->endIterRows(); bSuccess = true; } @@ -390,6 +399,8 @@ bool Wallet::nodeIdentityLoad() // Create and store a network identity. bool Wallet::nodeIdentityCreate() { + std::cerr << "NodeIdentity: Creating." << std::endl; + // // Generate the public and private key // @@ -403,7 +414,14 @@ bool Wallet::nodeIdentityCreate() { nodePublicKey.setNodePublic(key.GetPubKey()); nodePrivateKey.setNodePrivate(key.GetSecret()); - std::cerr << "NodeIdentity: Created." << std::endl; + std::string strDh512, strDh1024; + + DH_der_gen_hex(strDh512, 512); // Using hex as db->escape in insufficient. +#if 1 + strDh1024 = strDh512; // For testing and most cases 512 is fine. +#else + DH_der_gen_hex(strDh1024, 1024); +#endif // // Store the node information @@ -411,11 +429,15 @@ bool Wallet::nodeIdentityCreate() { Database* db = theApp->getWalletDB()->getDB(); ScopedLock sl(theApp->getWalletDB()->getDBLock()); - db->executeSQL(str(boost::format("INSERT INTO NodeIdentity (PublicKey,PrivateKey) VALUES (%s,%s);") + db->executeSQL(str(boost::format("INSERT INTO NodeIdentity (PublicKey,PrivateKey,Dh512,Dh1024) VALUES (%s,%s,%s,%s);") % db->escape(nodePublicKey.humanNodePublic()) - % db->escape(nodePrivateKey.humanNodePrivate()))); + % db->escape(nodePrivateKey.humanNodePrivate()) + % db->escape(strDh512) + % db->escape(strDh1024))); // XXX Check error result. + std::cerr << "NodeIdentity: Created." << std::endl; + return true; } diff --git a/src/Wallet.h b/src/Wallet.h index 8730fa798..df549f8dd 100644 --- a/src/Wallet.h +++ b/src/Wallet.h @@ -10,6 +10,7 @@ #include #include "openssl/ec.h" +#include "openssl/dh.h" #include "../json/value.h" @@ -31,6 +32,8 @@ protected: NewcoinAddress mNodePublicKey; NewcoinAddress mNodePrivateKey; + DH* mDh512; + DH* mDh1024; std::map mFamilies; std::map mAccounts; @@ -50,8 +53,10 @@ public: // - Maintain peer connectivity through validation and peer management. void start(); - NewcoinAddress& getNodePublic() { return mNodePublicKey; } - NewcoinAddress& getNodePrivate() { return mNodePrivateKey; } + NewcoinAddress& getNodePublic() { return mNodePublicKey; } + NewcoinAddress& getNodePrivate() { return mNodePrivateKey; } + DH* getDh512() { return DHparams_dup(mDh512); } + DH* getDh1024() { return DHparams_dup(mDh1024); } NewcoinAddress addFamily(const std::string& passPhrase, bool lock); NewcoinAddress addFamily(const NewcoinAddress& familySeed, bool lock);