00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __CODEX_CLIENT_MESSAGE_H__
00022 #define __CODEX_CLIENT_MESSAGE_H__
00023
00024 #include "CODEX_ASN1/OctetString.h"
00025 #include "CODEX_ASN1/BigNumber.h"
00026 #include "CODEX_ASN1/Certificate.h"
00027 #include "CODEX_Ciphers/ElGamal.h"
00028 #include "CODEX_Ciphers/RSA.h"
00029 #include "CODEX_Ciphers/VarRSA.h"
00030 #include "CODEX_Ciphers/Policy.h"
00031 #include "CODEX_Ciphers/Credentials.h"
00032 #include "CODEX_Ciphers/TranslationCertificate.h"
00033 #include "CODEX_Ciphers/RSAPlaintextPK.h"
00034 #include "CODEX_Ciphers/HashFunction.h"
00035 #include "CODEX_Server/AugmentedEGPublicKey.h"
00036
00037
00038
00046 namespace CODEX_Client
00047 {
00048
00049 #ifndef ELGAMAL
00050
00051 typedef CODEX_Ciphers::VarRSACipherText CipherTextType;
00053 typedef CipherTextType RequestCipherTextType;
00055 typedef CODEX_Ciphers::RSACipherText BlindingCipherTextType;
00057 typedef CODEX_Ciphers::VarRSABlindCipherText BlindCipherTextType;
00059 typedef CODEX_Ciphers::VarRSABlindPlainText BlindPlainTextType;
00060 #else
00061
00062 typedef CODEX_Ciphers::ElGamalCipherText CipherTextType;
00064 typedef CODEX_Ciphers::ElGamalSchnorrCipherText RequestCipherTextType;
00066 typedef RequestCipherTextType BlindingCipherTextType;
00068 typedef CipherTextType BlindCipherTextType;
00070 typedef CODEX_ASN1::BigNumber BlindPlainTextType;
00071 #endif
00072
00076 class Message : public CODEX_ASN1::Base
00077 {
00078 public :
00080 Message( bool initialized ) :
00081 Base( initialized )
00082 {}
00083
00085 virtual ~Message() {}
00086
00088 BIGNUM * digest( const CODEX_Ciphers::HashFunction& hf ) const;
00089 };
00090
00097 template< class MT, class ST >
00098 class SignedMessage : public Message
00099 {
00100 public :
00102 SignedMessage() : Message( false ) {}
00103
00105 SignedMessage( const MT& message, ST signature ) :
00106 Message( true ),
00107 m_message( message ),
00108 m_signature( signature )
00109 {}
00110
00112 SignedMessage( const SignedMessage& aMessage ) :
00113 Message( aMessage.m_initialized ),
00114 m_message( aMessage.m_message ),
00115 m_signature( aMessage.m_signature )
00116 {}
00117
00119 virtual ~SignedMessage() {}
00120
00122 void operator=( const SignedMessage& aMessage )
00123 {
00124 m_initialized = aMessage.m_initialized;
00125 m_message = aMessage.m_message;
00126 m_signature = aMessage.m_signature;
00127 }
00128
00130 const MT& message() const { return m_message; }
00132 const ST& signature() const { return m_signature; }
00133
00135 int marshal( unsigned char ** pp ) const
00136 {
00137 int r=0;
00138 int ret=0;
00139 unsigned char * p;
00140
00141 ret += m_message.marshal(0);
00142 ret += m_signature.marshal(0);
00143 M_ASN1_I2D_seq_total();
00144 m_message.marshal(&p);
00145 m_signature.marshal(&p);
00146 M_ASN1_I2D_finish();
00147 }
00148
00150 void* unmarshal( void* bogus,
00151 unsigned char ** pp,
00152 long length )
00153 {
00154 if ( m_initialized )
00155 {
00156 return NULL;
00157 }
00158 if ( (NULL == pp) || (NULL == *pp) )
00159 {
00160 return NULL;
00161 }
00162 ASN1_CTX c;
00163 c.pp = pp;
00164 c.q = *pp;
00165 c.error = ERR_R_NESTED_ASN1_ERROR;
00166 int i;
00167
00168 M_ASN1_D2I_Init();
00169 M_ASN1_D2I_start_sequence();
00170 M_ASN1_D2I_get(i, m_message.unmarshal);
00171 M_ASN1_D2I_get(i, m_signature.unmarshal);
00172 if ( !asn1_Finish(&c) )
00173 {
00174 return NULL;
00175 }
00176 *pp=c.p;
00177 m_initialized = true;
00178 return this;
00179 err:
00180 return NULL;
00181 }
00182
00183 private :
00184 MT m_message;
00185 ST m_signature;
00186 };
00187
00192 class RequestKeyMsg : public Message
00193 {
00194 public :
00196 RequestKeyMsg() : Message( true ) {}
00197
00199 virtual ~RequestKeyMsg() {}
00200
00202 void operator=( const RequestKeyMsg& aMsg ) {}
00203
00205 int marshal( unsigned char ** pp ) const { return 0; }
00207 void* unmarshal( void* bogus, unsigned char ** pp, long length )
00208 {
00209 return this;
00210 }
00211 };
00212
00213
00215 typedef CODEX_Server::SignedAugmentedEGPublicKey SignedPublicKeyMsg;
00216
00232 class CreateKeyMsg : public Message
00233 {
00234 public :
00236 CreateKeyMsg();
00237
00239 CreateKeyMsg( const CODEX_ASN1::OctetString& name,
00240 const CODEX_ASN1::Certificate& owner,
00241 const CODEX_Ciphers::Policy& readP,
00242 const CODEX_Ciphers::Policy& writeP );
00243
00245 CreateKeyMsg( const CreateKeyMsg& aCKM );
00246
00248 virtual ~CreateKeyMsg() {}
00249
00251 void operator=( const CreateKeyMsg& aCKM );
00252
00254 const CODEX_ASN1::OctetString& name() const { return m_name; }
00255
00257 const CODEX_ASN1::Certificate& owner() const { return m_owner; }
00258
00260 const CODEX_Ciphers::Policy& readP() const { return m_readP; }
00261
00263 const CODEX_Ciphers::Policy& writeP() const { return m_writeP; }
00264
00266 int marshal( unsigned char ** pp ) const;
00268 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00269
00270 private :
00271 CODEX_ASN1::OctetString m_name;
00272 CODEX_ASN1::Certificate m_owner;
00273 CODEX_Ciphers::Policy m_readP;
00274 CODEX_Ciphers::Policy m_writeP;
00275 };
00276
00278 typedef SignedMessage< CreateKeyMsg, CODEX_Ciphers::RSASignature >
00279 SignedCreateKeyMsg;
00280
00290 class BoundNameMsg : public Message
00291 {
00292 public :
00294 BoundNameMsg();
00295
00297 BoundNameMsg( const CODEX_ASN1::OctetString& name,
00298 const SignedCreateKeyMsg& request );
00299
00301 BoundNameMsg( const BoundNameMsg& aBNM );
00302
00304 virtual ~BoundNameMsg() {}
00305
00307 void operator=( const BoundNameMsg& aBNM );
00308
00310 const CODEX_ASN1::OctetString& name() const { return m_name; }
00311
00313 const SignedCreateKeyMsg& request() const { return m_request; }
00314
00316 int marshal( unsigned char ** pp ) const;
00318 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00319
00320 private :
00321 CODEX_ASN1::OctetString m_name;
00322 SignedCreateKeyMsg m_request;
00323 };
00324
00326 typedef SignedMessage< BoundNameMsg, CODEX_Ciphers::RSASignature >
00327 SignedBoundNameMsg;
00328
00342 class WriteKeyMsg : public Message
00343 {
00344 public :
00346 WriteKeyMsg();
00347
00349 WriteKeyMsg( const CODEX_ASN1::OctetString& name,
00350 const RequestCipherTextType& encryption,
00351 #ifndef ELGAMAL
00352 const CODEX_Ciphers::RSAPlaintextPK& proof,
00353 #endif
00354 const CODEX_Ciphers::Credentials& credentials,
00355 const SignedBoundNameMsg& binding );
00356
00358 WriteKeyMsg( const WriteKeyMsg& aWKM );
00359
00361 virtual ~WriteKeyMsg() {}
00362
00364 void operator=( const WriteKeyMsg& aWKM );
00365
00367 const CODEX_ASN1::OctetString& name() const { return m_name; }
00368
00370 const RequestCipherTextType& encryption() const
00371 {
00372 return m_encryption;
00373 }
00374
00375 #ifndef ELGAMAL
00376
00377 const CODEX_Ciphers::RSAPlaintextPK& proof() const
00378 {
00379 return m_proof;
00380 }
00381 #endif
00382
00384 const CODEX_Ciphers::Credentials& credentials() const
00385 {
00386 return m_credentials;
00387 }
00388
00390 const SignedBoundNameMsg& binding() const { return m_binding; }
00391
00393 int marshal( unsigned char ** pp ) const;
00395 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00396
00397 private :
00398 CODEX_ASN1::OctetString m_name;
00399 RequestCipherTextType m_encryption;
00400 #ifndef ELGAMAL
00401 CODEX_Ciphers::RSAPlaintextPK m_proof;
00402 #endif
00403 CODEX_Ciphers::Credentials m_credentials;
00404 SignedBoundNameMsg m_binding;
00405 };
00406
00408 typedef SignedMessage< WriteKeyMsg, CODEX_Ciphers::RSASignature >
00409 SignedWriteKeyMsg;
00410
00421 class KeyStoredMsg : public Message
00422 {
00423 public :
00425 KeyStoredMsg();
00426
00428 KeyStoredMsg( const CODEX_ASN1::OctetString& name,
00429 const CODEX_Ciphers::RSASignature& requestSignature );
00430
00432 KeyStoredMsg( const KeyStoredMsg& aKSM );
00433
00435 virtual ~KeyStoredMsg() {}
00436
00438 void operator=( const KeyStoredMsg& aKSM );
00439
00441 const CODEX_ASN1::OctetString& name() const { return m_name; }
00442
00444 const CODEX_Ciphers::RSASignature& requestSignature() const
00445 {
00446 return m_requestSignature;
00447 }
00448
00450 int marshal( unsigned char ** pp ) const;
00452 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00453
00454 private :
00455 CODEX_ASN1::OctetString m_name;
00456 CODEX_Ciphers::RSASignature m_requestSignature;
00457 };
00458
00460 typedef SignedMessage< KeyStoredMsg, CODEX_Ciphers::RSASignature >
00461 SignedKeyStoredMsg;
00462
00471 class ReadKeyMsg : public Message
00472 {
00473 public :
00475 ReadKeyMsg();
00476
00478 ReadKeyMsg( const CODEX_ASN1::OctetString& name,
00479 const BlindingCipherTextType& blinding,
00480 #ifndef ELGAMAL
00481 const CODEX_Ciphers::RSAPlaintextPK& proof,
00482 #endif
00483 const CODEX_Ciphers::Credentials& credentials );
00484
00486 ReadKeyMsg( const ReadKeyMsg& aRKM );
00487
00489 virtual ~ReadKeyMsg() {}
00490
00492 void operator=( const ReadKeyMsg& aRKM );
00493
00495 const CODEX_ASN1::OctetString& name() const { return m_name; }
00496
00498 const BlindingCipherTextType& blinding() const
00499 {
00500 return m_blinding;
00501 }
00502
00503 #ifndef ELGAMAL
00504
00505 const CODEX_Ciphers::RSAPlaintextPK& proof() const
00506 {
00507 return m_proof;
00508 }
00509 #endif
00510
00512 const CODEX_Ciphers::Credentials& credentials() const
00513 {
00514 return m_credentials;
00515 }
00516
00518 int marshal( unsigned char ** pp ) const;
00520 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00521
00522 private :
00523 CODEX_ASN1::OctetString m_name;
00524 BlindingCipherTextType m_blinding;
00525 #ifndef ELGAMAL
00526 CODEX_Ciphers::RSAPlaintextPK m_proof;
00527 #endif
00528 CODEX_Ciphers::Credentials m_credentials;
00529 };
00530
00532 typedef SignedMessage< ReadKeyMsg, CODEX_Ciphers::RSASignature >
00533 SignedReadKeyMsg;
00534
00545 class BlindKeyMsg : public Message
00546 {
00547 public :
00549 BlindKeyMsg();
00550
00552 BlindKeyMsg( const CODEX_ASN1::OctetString& name,
00553 const BlindPlainTextType& blindedKey,
00554 const CODEX_Ciphers::RSASignature& requestSignature );
00555
00557 BlindKeyMsg( const BlindKeyMsg& aBKM );
00558
00560 virtual ~BlindKeyMsg() {}
00561
00563 void operator=( const BlindKeyMsg& aBKM );
00564
00566 const CODEX_ASN1::OctetString& name() const { return m_name; }
00567
00569 const BlindPlainTextType& blindedKey() const
00570 {
00571 return m_blindedKey;
00572 }
00573
00575 const CODEX_Ciphers::RSASignature& requestSignature() const
00576 {
00577 return m_requestSignature;
00578 }
00579
00581 int marshal( unsigned char ** pp ) const;
00583 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00584
00585 private :
00586 CODEX_ASN1::OctetString m_name;
00587 BlindPlainTextType m_blindedKey;
00588 CODEX_Ciphers::RSASignature m_requestSignature;
00589 };
00590
00592 typedef SignedMessage< BlindKeyMsg, CODEX_Ciphers::RSASignature >
00593 SignedBlindKeyMsg;
00594
00602 enum MessageType
00603 {
00604 kRequestKeyMsg,
00605 kPublicKeyMsg,
00606 kCreateKeyMsg,
00607 kBoundNameMsg,
00608 kWriteKeyMsg,
00609 kKeyStoredMsg,
00610 kReadKeyMsg,
00611 kBlindKeyMsg
00612 };
00613
00618 const unsigned char SignatureMask = 0x80;
00619 }
00620
00621 #endif