Store DHParams in sqlite.

This commit is contained in:
Arthur Britto
2012-04-29 16:48:51 -07:00
parent 2d9eff0ecf
commit 024f14efa4
4 changed files with 38 additions and 37 deletions

View File

@@ -56,12 +56,11 @@ const char *WalletDBInit[] = {
Comment TEXT \ 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 ( \ "CREATE TABLE NodeIdentity ( \
PublicKey CHARACTER(53), \ PublicKey CHARACTER(53), \
PrivateKey CHARACTER(52) \ PrivateKey CHARACTER(52), \
Dh512 TEXT, \
Dh1024 TEXT \
);", );",
// Miscellaneous persistent information // Miscellaneous persistent information

View File

@@ -6,9 +6,10 @@
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <boost/mem_fn.hpp> #include <boost/mem_fn.hpp>
//#include <boost/log/trivial.hpp> //#include <boost/log/trivial.hpp>
#include <openssl/dh.h>
#include "Application.h"
#include "Config.h" #include "Config.h"
#include "utils.h"
using namespace std; using namespace std;
using namespace boost::asio::ip; using namespace boost::asio::ip;
@@ -16,33 +17,7 @@ using namespace boost::asio::ip;
// Generate DH for SSL connection. // Generate DH for SSL connection.
static DH* handleTmpDh(SSL* ssl, int is_export, int iKeyLength) static DH* handleTmpDh(SSL* ssl, int is_export, int iKeyLength)
{ {
// We don't care if for export. return 512 == iKeyLength ? theApp->getWallet().getDh512() : theApp->getWallet().getDh1024();
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;
} }
PeerDoor::PeerDoor(boost::asio::io_service& io_service) : PeerDoor::PeerDoor(boost::asio::io_service& io_service) :

View File

@@ -14,6 +14,7 @@
#include "Ledger.h" #include "Ledger.h"
#include "NewcoinAddress.h" #include "NewcoinAddress.h"
#include "Application.h" #include "Application.h"
#include "utils.h"
// TEMPORARY // TEMPORARY
#ifndef CHECK_NEW_FAMILIES #ifndef CHECK_NEW_FAMILIES
@@ -381,6 +382,14 @@ bool Wallet::nodeIdentityLoad()
mNodePublicKey.setNodePublic(strPublicKey); mNodePublicKey.setNodePublic(strPublicKey);
mNodePrivateKey.setNodePrivate(strPrivateKey); 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(); db->endIterRows();
bSuccess = true; bSuccess = true;
} }
@@ -390,6 +399,8 @@ bool Wallet::nodeIdentityLoad()
// Create and store a network identity. // Create and store a network identity.
bool Wallet::nodeIdentityCreate() { bool Wallet::nodeIdentityCreate() {
std::cerr << "NodeIdentity: Creating." << std::endl;
// //
// Generate the public and private key // Generate the public and private key
// //
@@ -403,7 +414,14 @@ bool Wallet::nodeIdentityCreate() {
nodePublicKey.setNodePublic(key.GetPubKey()); nodePublicKey.setNodePublic(key.GetPubKey());
nodePrivateKey.setNodePrivate(key.GetSecret()); 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 // Store the node information
@@ -411,11 +429,15 @@ bool Wallet::nodeIdentityCreate() {
Database* db = theApp->getWalletDB()->getDB(); Database* db = theApp->getWalletDB()->getDB();
ScopedLock sl(theApp->getWalletDB()->getDBLock()); 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(nodePublicKey.humanNodePublic())
% db->escape(nodePrivateKey.humanNodePrivate()))); % db->escape(nodePrivateKey.humanNodePrivate())
% db->escape(strDh512)
% db->escape(strDh1024)));
// XXX Check error result. // XXX Check error result.
std::cerr << "NodeIdentity: Created." << std::endl;
return true; return true;
} }

View File

@@ -10,6 +10,7 @@
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include "openssl/ec.h" #include "openssl/ec.h"
#include "openssl/dh.h"
#include "../json/value.h" #include "../json/value.h"
@@ -31,6 +32,8 @@ protected:
NewcoinAddress mNodePublicKey; NewcoinAddress mNodePublicKey;
NewcoinAddress mNodePrivateKey; NewcoinAddress mNodePrivateKey;
DH* mDh512;
DH* mDh1024;
std::map<NewcoinAddress, LocalAccountFamily::pointer> mFamilies; std::map<NewcoinAddress, LocalAccountFamily::pointer> mFamilies;
std::map<NewcoinAddress, LocalAccount::pointer> mAccounts; std::map<NewcoinAddress, LocalAccount::pointer> mAccounts;
@@ -52,6 +55,8 @@ public:
NewcoinAddress& getNodePublic() { return mNodePublicKey; } NewcoinAddress& getNodePublic() { return mNodePublicKey; }
NewcoinAddress& getNodePrivate() { return mNodePrivateKey; } 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 std::string& passPhrase, bool lock);
NewcoinAddress addFamily(const NewcoinAddress& familySeed, bool lock); NewcoinAddress addFamily(const NewcoinAddress& familySeed, bool lock);