00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __CODEX_SERVER_SERVERSTATE_H__
00022 #define __CODEX_SERVER_SERVERSTATE_H__
00023
00024 #include <map>
00025 #include <list>
00026
00027 #include <openssl/conf.h>
00028
00029 #include "CODEX_Quorum/StaticByzantineQuorumSystem.h"
00030 #include "CODEX_Quorum/SocketBuilder.h"
00031 #include "CODEX_Quorum/LoopbackSocket.h"
00032 #include "CODEX_SSL/SSLSocket.h"
00033
00034 #include "CODEX_ASN1/Base.h"
00035 #include "CODEX_ASN1/OctetString.h"
00036 #include "CODEX_ASN1/Certificate.h"
00037
00038 #include "CODEX_Events/Activity.h"
00039 #include "CODEX_Quorum/LocalServer.h"
00040
00041 #include "CODEX_Ciphers/SHA1HashFunction.h"
00042 #include "CODEX_Ciphers/RSA.h"
00043 #include "CODEX_Ciphers/ElGamal.h"
00044
00045 #include "CODEX_ThresholdCrypto/CombinatoricThresholdRSA.h"
00046 #include "CODEX_ThresholdCrypto/CombinatoricThresholdElGamal.h"
00047 #include "CODEX_ThresholdCrypto/CombinatoricThresholdVarRSA.h"
00048 #include "CODEX_VSS/Combinatoric.h"
00049 #include "CODEX_VSS/CombinatoricFeldman.h"
00050 #include "ShareCallback.h"
00051
00052 #include "AugmentedEGPublicKey.h"
00053
00062 namespace CODEX_Server
00063 {
00064 class ShareLabelChallenge;
00065
00078 class ServerState
00079 {
00080
00081
00082
00083
00084 public :
00086 static const unsigned int nServers = 4;
00088 static const unsigned int nFaults = 1;
00089
00091 static const size_t nSID = 1;
00093 static const size_t nMID = 8;
00094
00096 static const unsigned char OutgoingMask = 0x80;
00097
00099 static const unsigned int RSAKeyNum = 0;
00100
00102 static const unsigned int EGKeyNum = 1;
00103
00104 private :
00105 typedef
00106 CODEX_Quorum::StaticByzantineQuorumSystem< nServers, nFaults >
00107 QSType;
00108
00109 typedef CODEX_Ciphers::SHA1HashFunction HashType;
00110
00111 typedef CODEX_Quorum::SocketBuilder ClientSocketBuilderType;
00112 typedef CODEX_SSL::SSLSocketBuilder ServerSocketBuilderType;
00113
00114 public :
00115
00116
00117
00118
00119
00121 typedef list< CODEX_Quorum::LocalServer* > ServerList;
00122
00124 typedef CODEX_VSS::Combinatoric< nServers , nFaults+1 > ShareType;
00125
00127 typedef CODEX_VSS::ModExpFunctional OneWay;
00128
00130 typedef CODEX_VSS::LabeledShare< ShareType , OneWay > LSType;
00131
00133 typedef CODEX_VSS::SecretWitness< ShareType , OneWay > WitnessType;
00134
00136 typedef CODEX_VSS::ShareSet< ShareType > ShareSetType;
00137
00139 typedef CODEX_ThresholdCrypto::ThresholdRSACrypto< ShareType >
00140 ThresholdRSAType;
00141
00143 typedef CODEX_ThresholdCrypto::ThresholdElGamalCrypto< ShareType >
00144 ThresholdElGamalType;
00145
00147 typedef CODEX_ThresholdCrypto::ThresholdVarRSACrypto< ShareType >
00148 ThresholdVarRSAType;
00149
00151 typedef ThresholdRSAType ThresholdSignatureType;
00152
00154 typedef ShareCallbackBase< ShareType , OneWay > ShareCallback;
00155
00157 typedef vector< ShareLabelChallenge* > ChallengeVector;
00158
00160 typedef map< const unsigned char* ,
00161 ChallengeVector ,
00162 CODEX_Quorum::memless< CODEX_Server::ServerState::nMID > >
00163 ChallengeMap;
00164
00165
00166
00167
00168
00170 static ServerState* instance();
00171
00173 static void destroy();
00174
00176 const CODEX_Ciphers::HashFunction& hashFunc() const
00177 {
00178 return m_hashFunc;
00179 }
00180
00181
00182
00183
00184
00185
00187 void configure( const CONF* conf,
00188 const char* sec,
00189 const string& fname );
00190
00191
00192
00193
00194
00195
00197 void addServer( CODEX_Quorum::LocalServer* server );
00198
00200 void removeServer( CODEX_Quorum::LocalServer* server );
00201
00203 ServerList::const_iterator serverListBegin()
00204 {
00205 return m_serverList.begin();
00206 }
00207
00209 ServerList::const_iterator serverListEnd()
00210 {
00211 return m_serverList.end();
00212 }
00213
00215 int serverListSize() { return m_serverList.size(); }
00216
00218 void cleanServer( CODEX_Quorum::LocalServer* server );
00219
00221 void garbageCollectServers();
00222
00223
00224
00225
00226
00227
00229 const CODEX_ASN1::Certificate& serverCert() const
00230 {
00231 return m_serverCerts[m_hostNum];
00232 }
00233
00235 const CODEX_ASN1::Certificate& serverCert( unsigned int i ) const;
00236
00238 const CODEX_Ciphers::RSAPublicKey& publicKey();
00239
00241 const CODEX_Ciphers::RSAPublicKey& publicKey( unsigned int i );
00242
00244 const CODEX_Ciphers::RSAPrivateKey& privateKey() const
00245 {
00246 return m_privateKey;
00247 }
00248
00249
00250
00251
00252
00253
00255 const CODEX_ASN1::Certificate& caCert() const { return m_caCert; }
00256
00258 const EVP_PKEY* caKey();
00259
00260
00261
00262
00263
00264
00266 const CODEX_Ciphers::RSAPublicKey& serviceKey();
00267
00269 const CODEX_Ciphers::ElGamalPublicKey& publicEGKey() const
00270 {
00271 return m_signedEGKey.key().key();
00272 }
00273
00275 const CODEX_ASN1::BigNumber& h() const {
00276 return m_signedEGKey.key().h();
00277 }
00278
00280 const SignedAugmentedEGPublicKey& signedEGKey() const
00281 {
00282 return m_signedEGKey;
00283 }
00284
00285
00292 const LSType& rsaShares( const LSType::LabelType& label,
00293 unsigned int server ) const;
00294
00296 const LSType::LabelType& defaultRSALabel() const;
00297
00299 void setRSAShareCallback( const ShareCallback* cb )
00300 {
00301
00302 if ( m_rsaShares.initialized() )
00303 {
00304 m_rsaShares = LSType();
00305 }
00306 m_rsaShareCallback = cb;
00307 }
00308
00310 void setRSADefaultLabel( const LSType::LabelType* label )
00311 {
00312 m_rsaDefaultLabel = label;
00313 }
00314
00316 const ThresholdRSAType& thresholdRSA() const
00317 {
00318 return m_thresholdRSA;
00319 }
00320
00322 const ThresholdVarRSAType& thresholdVarRSA() const
00323 {
00324 return m_thresholdVarRSA;
00325 }
00326
00328 const WitnessType& rsaWitness() const
00329 {
00330 return m_rsaWitness;
00331 }
00332
00333
00340 const LSType& elgamalShares( const LSType::LabelType& label,
00341 unsigned int server ) const;
00342
00344 const LSType::LabelType& defaultEGLabel() const;
00345
00347 void setEGShareCallback( const ShareCallback* cb )
00348 {
00349
00350 if ( m_elgamalShares.initialized() )
00351 {
00352 m_elgamalShares = LSType();
00353 }
00354 m_egShareCallback = cb;
00355 }
00356
00358 void setEGDefaultLabel( const LSType::LabelType* label )
00359 {
00360 m_egDefaultLabel = label;
00361 }
00362
00364 const ThresholdElGamalType& thresholdEG() const
00365 {
00366 return m_thresholdEG;
00367 }
00368
00370 const WitnessType& egWitness() const
00371 {
00372 return m_egWitness;
00373 }
00374
00375
00382 const LSType::LabelType& defaultLabel( unsigned int num ) const;
00383
00389 void setShareCallback( unsigned int num, const ShareCallback* cb );
00390
00397 void setDefaultLabel( const LSType::LabelType* label );
00398
00399
00409 const LSType& signatureShares( const LSType::LabelType& label,
00410 unsigned int server ) const
00411 {
00412 return rsaShares( label, server );
00413 }
00414
00416 const LSType::LabelType& defaultSignatureLabel() const
00417 {
00418 return defaultRSALabel();
00419 }
00420
00422 const ThresholdSignatureType& thresholdSignature() const;
00423
00434 void rationalizeSharings( const vector< ShareType* >& sharings,
00435 ShareSetType& result ) const;
00436
00445 BIGNUM* thresholdOperation( const vector< ShareType* >& sharings,
00446 const BIGNUM * operand ) const;
00447
00448
00449
00450
00451
00452
00468 unsigned char* newSequenceNumber();
00469
00483 void addChallenge( const unsigned char* request,
00484 ShareLabelChallenge* challenge );
00485
00489 const ServerState::ChallengeMap& challengeMap()
00490 {
00491 return m_challengeMap;
00492 }
00493
00500 void removeChallenge( const unsigned char* request );
00501
00502
00503
00504
00505
00506
00508 CODEX_Quorum::QuorumSystem* quorumSystem() const
00509 {
00510 return m_quorumSystem;
00511 }
00512
00514 long hostNum() const { return m_hostNum; }
00515
00517 const string& hostName( long i );
00518
00520 long serverPort() const { return m_serverPorts[ m_hostNum ]; }
00521
00523 long serverPort( long i );
00524
00535 bool addQuorumServer(
00536 int host,
00537 CODEX_Quorum::AsynchronousRemoteServer* server );
00538
00543 const CODEX_Quorum::RemoteServer* getQuorumServer( int host );
00544
00548 bool haveQuorumServer( int host ) const;
00549
00550
00551
00552
00553
00554
00556 const CODEX_Quorum::SocketBuilder& clientSocketBuilder() const
00557 {
00558 return m_clientSocketBuilder;
00559 }
00560
00562 const CODEX_Quorum::SocketBuilder& serverOutSocketBuilder() const
00563 {
00564
00565 return *m_serverOutSocketBuilder;
00566 }
00567
00569 const CODEX_Quorum::SocketBuilder& serverInSocketBuilder() const
00570 {
00571
00572 return *m_serverInSocketBuilder;
00573 }
00574
00575
00576 protected :
00578 ServerState();
00579
00581 virtual ~ServerState();
00582
00584 static ServerState* m_instance;
00585
00587 void readPrivateKey( const char* fname, const char* passwd=NULL );
00588
00590 void readCACert( const char* fname );
00591
00593 void readServiceCert( const char* fname );
00594
00596 void readPublicEGKey( const char* fname );
00597
00599 void readRSAShares( const char* fname );
00600
00602 void readElGamalShares( const char* fname );
00603
00604 private :
00605
00606 QSType* m_quorumSystem;
00607
00608 ClientSocketBuilderType m_clientSocketBuilder;
00609 ServerSocketBuilderType* m_serverOutSocketBuilder;
00610 ServerSocketBuilderType* m_serverInSocketBuilder;
00611 CODEX_Quorum::LoopbackSocketBuilder m_loopbackSocketBuilder;
00612
00613 unsigned char m_seqNum[nMID];
00614 ChallengeMap m_challengeMap;
00615
00616 long m_hostNum;
00617 string m_hostnames[nServers];
00618 long m_serverPorts[nServers];
00619 CODEX_ASN1::Certificate m_serverCerts[nServers];
00620
00621 HashType m_hashFunc;
00622
00623
00624 CODEX_Ciphers::RSAPublicKey m_publicKeys[nServers];
00625 RSA* m_rsa;
00626 CODEX_Ciphers::RSAPrivateKey m_privateKey;
00627 LSType m_rsaShares;
00628 ThresholdRSAType m_thresholdRSA;
00629 ThresholdVarRSAType m_thresholdVarRSA;
00630 ShareCallback* m_defaultRSAShareCB;
00631 const ShareCallback* m_rsaShareCallback;
00632 const LSType::LabelType* m_rsaDefaultLabel;
00633 LSType::LabelType m_rsaInitialLabel;
00634 WitnessType m_rsaWitness;
00635 LSType m_elgamalShares;
00636 ThresholdElGamalType m_thresholdEG;
00637 ShareCallback* m_defaultEGShareCB;
00638 const ShareCallback* m_egShareCallback;
00639 const LSType::LabelType* m_egDefaultLabel;
00640 LSType::LabelType m_egInitialLabel;
00641 WitnessType m_egWitness;
00642
00643 ServerList m_serverList;
00644 ServerList m_deadServerList;
00645
00646 CODEX_ASN1::Certificate m_caCert;
00647 EVP_PKEY * m_caKey;
00648
00649 CODEX_ASN1::Certificate m_serviceCert;
00650 CODEX_Ciphers::RSAPublicKey m_serviceKey;
00651
00652 SignedAugmentedEGPublicKey m_signedEGKey;
00653 };
00654
00655 }
00656
00657 #endif