00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __CODEX_APSS_MESSAGE_H__
00022 #define __CODEX_APSS_MESSAGE_H__
00023
00024 #include "CODEX_Ciphers/RSA.h"
00025 #include "CODEX_Exceptions/ExceptionBase.h"
00026 #include "Types.h"
00027 #include "CODEX_ASN1/Array.h"
00028
00029 namespace CODEX_APSS
00030 {
00031 using CODEX_Ciphers::RSASignature;
00032
00036 class Message : public CODEX_ASN1::Base
00037 {
00038 public :
00040 Message( bool initialized ) :
00041 Base( initialized )
00042 {}
00043
00045 virtual ~Message() {}
00046
00048 BIGNUM * digest( const CODEX_Ciphers::HashFunction& hf ) const;
00049 };
00050
00057 template< class MT, class ST >
00058 class SignedMessage : public Message
00059 {
00060 public :
00062 SignedMessage() : Message( false ) {}
00063
00065 SignedMessage( const MT& message, ST signature ) :
00066 Message( true ),
00067 m_message( message ),
00068 m_signature( signature )
00069 {}
00070
00072 SignedMessage( const SignedMessage& aMessage ) :
00073 Message( aMessage.m_initialized ),
00074 m_message( aMessage.m_message ),
00075 m_signature( aMessage.m_signature )
00076 {}
00077
00079 virtual ~SignedMessage() {}
00080
00082 void operator=( const SignedMessage& aMessage )
00083 {
00084 m_initialized = aMessage.m_initialized;
00085 m_message = aMessage.m_message;
00086 m_signature = aMessage.m_signature;
00087 }
00088
00090 const MT& message() const { return m_message; }
00092 const ST& signature() const { return m_signature; }
00093
00095 int marshal( unsigned char ** pp ) const
00096 {
00097 int r=0;
00098 int ret=0;
00099 unsigned char * p;
00100
00101 ret += m_message.marshal(0);
00102 ret += m_signature.marshal(0);
00103 M_ASN1_I2D_seq_total();
00104 m_message.marshal(&p);
00105 m_signature.marshal(&p);
00106 M_ASN1_I2D_finish();
00107 }
00108
00110 void* unmarshal( void* bogus,
00111 unsigned char ** pp,
00112 long length )
00113 {
00114 if ( m_initialized )
00115 {
00116 return 0;
00117 }
00118 if ( (0 == pp) || (0 == *pp) )
00119 {
00120 return 0;
00121 }
00122 ASN1_CTX c;
00123 c.pp = pp;
00124 c.q = *pp;
00125 c.error = ERR_R_NESTED_ASN1_ERROR;
00126 int i;
00127
00128 M_ASN1_D2I_Init();
00129 M_ASN1_D2I_start_sequence();
00130 M_ASN1_D2I_get(i, m_message.unmarshal);
00131 M_ASN1_D2I_get(i, m_signature.unmarshal);
00132 if ( !asn1_Finish(&c) )
00133 {
00134 return 0;
00135 }
00136 *pp=c.p;
00137 m_initialized = true;
00138 return this;
00139 err:
00140 return 0;
00141 }
00142
00143 private :
00144 MT m_message;
00145 ST m_signature;
00146 };
00147
00151 class InitMsg : public Message
00152 {
00153 public :
00155 InitMsg();
00156
00158 InitMsg( const CODEX_ASN1::Integer& version,
00159 const CODEX_ASN1::Integer& coordinator,
00160 const LabelType& label );
00161
00163 InitMsg( const InitMsg& aOther );
00164
00166 virtual ~InitMsg() {}
00167
00169 void operator=( const InitMsg& aOther );
00170
00175 const CODEX_ASN1::Integer& version() const { return m_version; }
00176
00178 const CODEX_ASN1::Integer& coordinator() const
00179 {
00180 return m_coordinator;
00181 }
00182
00187 const LabelType& label() const { return m_label; }
00188
00190 int marshal( unsigned char ** pp ) const;
00192 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00193
00194 private :
00195 CODEX_ASN1::Integer m_version;
00196 CODEX_ASN1::Integer m_coordinator;
00197 LabelType m_label;
00198 };
00200 typedef SignedMessage< InitMsg, RSASignature > SignedInitMsg;
00201
00206 class EstablishMsg : public Message
00207 {
00208 public :
00210 EstablishMsg();
00211
00213 EstablishMsg( const CODEX_ASN1::Integer& version,
00214 const SublabelType& sublabel,
00215 const CODEX_ASN1::Integer& establisher,
00216 const CODEX_ASN1::Integer& recipient,
00217 const ShareType& shares );
00218
00220 EstablishMsg( const EstablishMsg& aOther );
00221
00223 virtual ~EstablishMsg() {}
00224
00226 void operator=( const EstablishMsg& aOther );
00227
00229 const CODEX_ASN1::Integer& version() const { return m_version; }
00230
00232 const SublabelType& sublabel() const { return m_sublabel; }
00233
00235 const CODEX_ASN1::Integer& establisher() const
00236 {
00237 return m_establisher;
00238 }
00239
00241 const CODEX_ASN1::Integer& recipient() const { return m_recipient; }
00242
00244 const ShareType& shares() const { return m_shares; }
00245
00246 int marshal( unsigned char ** pp ) const;
00247 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00248
00249 private :
00250 CODEX_ASN1::Integer m_version;
00251 SublabelType m_sublabel;
00252 CODEX_ASN1::Integer m_establisher;
00253 CODEX_ASN1::Integer m_recipient;
00254 ShareType m_shares;
00255 };
00257 typedef SignedMessage< EstablishMsg, RSASignature > SignedEstablishMsg;
00258
00262 class EstablishedMsg : public Message
00263 {
00264 public :
00266 EstablishedMsg();
00267
00269 EstablishedMsg( const EstablishMsg& request );
00270
00272 EstablishedMsg( const EstablishedMsg& aOther );
00273
00275 virtual ~EstablishedMsg() {}
00276
00278 void operator=( const EstablishedMsg& aOther );
00279
00281 const CODEX_ASN1::Integer& version() const { return m_version; }
00282
00284 const SublabelType& sublabel() const { return m_sublabel; }
00285
00287 const CODEX_ASN1::Integer& recipient() const { return m_recipient; }
00288
00290 const CODEX_ASN1::Integer& establisher() const
00291 {
00292 return m_establisher;
00293 }
00294
00295 int marshal( unsigned char ** pp ) const;
00296 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00297
00298 private :
00299 CODEX_ASN1::Integer m_version;
00300 SublabelType m_sublabel;
00301 CODEX_ASN1::Integer m_recipient;
00302 CODEX_ASN1::Integer m_establisher;
00303 };
00305 typedef SignedMessage< EstablishedMsg, RSASignature > SignedEstablishedMsg;
00307 typedef CODEX_ASN1::Array< SignedEstablishedMsg > EstablishedArray;
00308
00313 class ContributeMsg : public Message
00314 {
00315 public :
00317 ContributeMsg();
00318
00320 ContributeMsg( const CODEX_ASN1::Integer& version,
00321 const CODEX_ASN1::Integer& coordinator,
00322 const CODEX_ASN1::Integer& contributor,
00323 const EstablishedArray& evidence );
00324
00326 ContributeMsg( const ContributeMsg& aOther );
00327
00329 virtual ~ContributeMsg() {}
00330
00332 void operator=( const ContributeMsg& aOther );
00333
00335 const CODEX_ASN1::Integer& version() const { return m_version; };
00336
00338 const CODEX_ASN1::Integer& coordinator() const
00339 {
00340 return m_coordinator;
00341 }
00342
00344 const CODEX_ASN1::Integer& contributor() const
00345 {
00346 return m_contributor;
00347 }
00348
00350 const EstablishedArray& evidence() const { return m_evidence; }
00351
00353 int marshal( unsigned char ** pp ) const;
00355 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00356
00357 private :
00358 CODEX_ASN1::Integer m_version;
00359 CODEX_ASN1::Integer m_coordinator;
00360 CODEX_ASN1::Integer m_contributor;
00361 EstablishedArray m_evidence;
00362 };
00364 typedef SignedMessage< ContributeMsg, RSASignature > SignedContributeMsg;
00365
00370 class ComputeMsg : public Message
00371 {
00372 public :
00374 const static unsigned int NumShares = ShareType::NumShares;
00375
00377 ComputeMsg();
00378
00380 ComputeMsg( const CODEX_ASN1::Integer& coordinator,
00381 const SublabelType subshareLabels[ NumShares ] );
00382
00384 ComputeMsg( const ComputeMsg& aOther );
00385
00387 virtual ~ComputeMsg() {}
00388
00390 void operator=( const ComputeMsg& aOther );
00391
00393 const CODEX_ASN1::Integer& coordinator() const
00394 {
00395 return m_coordinator;
00396 }
00397
00405 const SublabelType& subshareLabel( unsigned int i ) const
00406 {
00407 if ( i >= NumShares )
00408 {
00409 throw CODEX_Exceptions::IllegalIndexException( __FILE__ ,
00410 __LINE__ );
00411 }
00412 return m_subshareLabels[ i ];
00413 }
00414
00416 int marshal( unsigned char ** pp ) const;
00418 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00419
00420 private :
00421 CODEX_ASN1::Integer m_coordinator;
00422 SublabelType m_subshareLabels[ NumShares ];
00423 };
00425 typedef SignedMessage< ComputeMsg, RSASignature > SignedComputeMsg;
00426
00432 class ComputedMsg : public Message
00433 {
00434 public :
00436 ComputedMsg();
00437
00439 ComputedMsg( const LabelType& shareLabel,
00440 const CODEX_ASN1::Integer& computor );
00441
00443 ComputedMsg( const ComputedMsg& aOther );
00444
00446 virtual ~ComputedMsg() {}
00447
00449 void operator=( const ComputedMsg& aOther );
00450
00452 const LabelType& shareLabel() const { return m_shareLabel; }
00453
00455 const CODEX_ASN1::Integer& computor() const { return m_computor; };
00456
00458 int marshal( unsigned char ** pp ) const;
00460 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00461
00462 private :
00463 LabelType m_shareLabel;
00464 CODEX_ASN1::Integer m_computor;
00465 };
00467 typedef SignedMessage< ComputedMsg, RSASignature > SignedComputedMsg;
00469 typedef CODEX_ASN1::Array< SignedComputedMsg > ComputedArray;
00470
00475 class FinishedMsg : public Message
00476 {
00477 public :
00479 FinishedMsg();
00480
00482 FinishedMsg( const CODEX_ASN1::Integer& version,
00483 const CODEX_ASN1::Integer& coordinator,
00484 const ComputedArray& evidence );
00485
00487 FinishedMsg( const FinishedMsg& aOther );
00488
00490 virtual ~FinishedMsg() {}
00491
00493 void operator=( const FinishedMsg& aOther );
00494
00496 const CODEX_ASN1::Integer& version() const { return m_version; };
00497
00499 const CODEX_ASN1::Integer& coordinator() const
00500 {
00501 return m_coordinator;
00502 }
00503
00505 const ComputedArray& evidence() const { return m_evidence; }
00506
00508 int marshal( unsigned char ** pp ) const;
00510 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00511
00512 private :
00513 CODEX_ASN1::Integer m_version;
00514 CODEX_ASN1::Integer m_coordinator;
00515 ComputedArray m_evidence;
00516 };
00518 typedef SignedMessage< FinishedMsg, RSASignature > SignedFinishedMsg;
00519
00523 class RecoverMsg : public Message
00524 {
00525 public :
00527 RecoverMsg();
00528
00530 RecoverMsg( const CODEX_ASN1::Integer& version,
00531 const SublabelType& sublabel,
00532 const CODEX_ASN1::Integer& requester,
00533 const CODEX_ASN1::Integer& responder );
00534
00536 RecoverMsg( const RecoverMsg& aOther );
00537
00539 virtual ~RecoverMsg() {}
00540
00542 void operator=( const RecoverMsg& aOther );
00543
00545 const CODEX_ASN1::Integer& version() const { return m_version; };
00546
00548 const SublabelType& sublabel() const { return m_sublabel; }
00549
00551 const CODEX_ASN1::Integer& requester() const { return m_requester; }
00552
00554 const CODEX_ASN1::Integer& responder() const { return m_responder; }
00555
00557 int marshal( unsigned char ** pp ) const;
00559 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00560
00561 private :
00562 CODEX_ASN1::Integer m_version;
00563 SublabelType m_sublabel;
00564 CODEX_ASN1::Integer m_requester;
00565 CODEX_ASN1::Integer m_responder;
00566 };
00568 typedef SignedMessage< RecoverMsg, RSASignature > SignedRecoverMsg;
00569
00574 class RecoveredMsg : public Message
00575 {
00576 public :
00578 RecoveredMsg();
00579
00581 RecoveredMsg( const RecoverMsg& request,
00582 const ShareType& shareset );
00583
00585 RecoveredMsg( const RecoveredMsg& aOther );
00586
00588 virtual ~RecoveredMsg() {}
00589
00591 void operator=( const RecoveredMsg& aOther );
00592
00594 const CODEX_ASN1::Integer& version() const { return m_version; };
00595
00597 const SublabelType& sublabel() const { return m_sublabel; }
00598
00600 const CODEX_ASN1::Integer& responder() const
00601 {
00602 return m_responder;
00603 }
00604
00609 const ShareType& shares() const { return m_shares; }
00610
00612 const CODEX_ASN1::Integer& requester() const
00613 {
00614 return m_requester;
00615 }
00616
00618 int marshal( unsigned char ** pp ) const;
00620 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00621
00622 private :
00623 CODEX_ASN1::Integer m_version;
00624 SublabelType m_sublabel;
00625 CODEX_ASN1::Integer m_responder;
00626 ShareType m_shares;
00627 CODEX_ASN1::Integer m_requester;
00628 };
00630 typedef SignedMessage< RecoveredMsg, RSASignature > SignedRecoveredMsg;
00631
00639 enum MessageType
00640 {
00641 kInitMsg,
00642 kEstablishMsg,
00643 kEstablishedMsg,
00644 kContributeMsg,
00645 kComputeMsg,
00646 kComputedMsg,
00647 kFinishedMsg,
00648 kRecoverMsg,
00649 kRecoveredMsg,
00650 kBadRequest
00651 };
00652
00657 const unsigned char SignatureMask = 0x80;
00658 }
00659
00660 #endif