Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

AugmentedEGPublicKey.cc

00001 /*
00002  * Copyright 2003 Michael A. Marsh, Cornell University. All rights reserved.
00003  * This software is released under the modified BSD license.
00004  * See the file LICENSE in the top-level directory for details.
00005  */
00006 //
00007 // $Id: AugmentedEGPublicKey.cc,v 1.2 2004/05/19 15:56:57 mmarsh Exp $
00008 //
00009 // $Log: AugmentedEGPublicKey.cc,v $
00010 // Revision 1.2  2004/05/19 15:56:57  mmarsh
00011 // *** empty log message ***
00012 //
00013 // Revision 1.1  2003/11/04 21:56:58  mmarsh
00014 // The ElGamal public key with additional generator, and the signed
00015 // version thereof, are now in the CODEX_Server package rather than
00016 // CODEX_Client, since the generic server should not depend on the
00017 // key service client library.
00018 //
00019 //
00020 
00021 #include "AugmentedEGPublicKey.h"
00022 #include "CODEX_Exceptions/BignumExceptions.h"
00023 
00024 using namespace CODEX_Server;
00025 using namespace CODEX_ASN1;
00026 using namespace CODEX_Ciphers;
00027 
00028 AugmentedEGPublicKey::AugmentedEGPublicKey() :
00029    CODEX_ASN1::Base( false )
00030 {
00031 }
00032 
00033 AugmentedEGPublicKey::AugmentedEGPublicKey( const ElGamalPublicKey& key,
00034                                             const BigNumber& h ) :
00035    CODEX_ASN1::Base( true ),
00036    m_key( key ),
00037    m_h( h )
00038 {
00039 }
00040 
00041 AugmentedEGPublicKey::AugmentedEGPublicKey(
00042    const AugmentedEGPublicKey& aAPK ) :
00043    CODEX_ASN1::Base( aAPK.m_initialized ),
00044    m_key( aAPK.m_key ),
00045    m_h( aAPK.m_h )
00046 {
00047 }
00048 
00049 void
00050 AugmentedEGPublicKey::operator=( const AugmentedEGPublicKey& aAPK )
00051 {
00052    m_initialized = aAPK.m_initialized;
00053    m_key         = aAPK.m_key;
00054    m_h           = aAPK.m_h;
00055 }
00056 
00057 BIGNUM*
00058 AugmentedEGPublicKey::digest( const CODEX_Ciphers::HashFunction& hashFunc )
00059    const
00060 {
00061    if ( ! m_initialized )
00062    {
00063       return 0;
00064    }
00065 
00066    unsigned char* buff = NULL;
00067    BIGNUM * retVal = NULL;
00068    CODEX_ASN1::ustring* str = NULL;
00069 
00070    try
00071    {
00072       int length = marshal(0);
00073       buff = new unsigned char[ length ];
00074       unsigned char* pBuff = buff;
00075       marshal(&pBuff);
00076       str = hashFunc( CODEX_ASN1::ustring( buff, length ) );
00077       delete [] buff;
00078       buff = NULL;
00079       retVal = BN_new();
00080       if ( NULL == retVal )
00081       {
00082          throw CODEX_Exceptions::BignumNullException( __FILE__ , __LINE__ );
00083       }
00084       if ( NULL == BN_bin2bn( str->data(), str->length(), retVal ) )
00085       {
00086          throw CODEX_Exceptions::BignumBin2BNException( __FILE__ , __LINE__ );
00087       }
00088       delete str;
00089 
00090       return retVal;
00091    }
00092    catch ( ... )
00093    {
00094       if ( NULL != buff ) delete buff;
00095       if ( NULL != str ) delete str;
00096       if ( NULL != retVal ) BN_free( retVal );
00097       throw;
00098    }
00099 }
00100 
00101 int
00102 AugmentedEGPublicKey::marshal( unsigned char ** pp ) const
00103 {
00104    int r=0;
00105    int ret=0;
00106    unsigned char * p;
00107 
00108    ret += m_key.marshal(0);
00109    ret += m_h.marshal(0);
00110    M_ASN1_I2D_seq_total();
00111    m_key.marshal(&p);
00112    m_h.marshal(&p);
00113    M_ASN1_I2D_finish();
00114 }
00115 
00116 void*
00117 AugmentedEGPublicKey::unmarshal( void* bogus,
00118                                  unsigned char ** pp,
00119                                  long length )
00120 {
00121    if ( m_initialized )
00122    {
00123       return NULL;
00124    }
00125    if ( (NULL == pp) || (NULL == *pp) )
00126    {
00127       return NULL;
00128    }
00129    ASN1_CTX c;
00130    c.pp = pp;
00131    c.q = *pp;
00132    c.error = ERR_R_NESTED_ASN1_ERROR;
00133    int i;
00134 
00135    M_ASN1_D2I_Init();
00136    M_ASN1_D2I_start_sequence();
00137    M_ASN1_D2I_get(i, m_key.unmarshal);
00138    M_ASN1_D2I_get(i, m_h.unmarshal);
00139    if ( !asn1_Finish(&c) )
00140    {
00141       return NULL;
00142    }
00143    *pp=c.p;
00144    m_initialized = true;
00145    return this;
00146   err: // needed by ASN.1 macros
00147    return NULL;
00148 }
00149 
00150 SignedAugmentedEGPublicKey::SignedAugmentedEGPublicKey() :
00151    CODEX_ASN1::Base( false )
00152 {
00153 }
00154 
00155 SignedAugmentedEGPublicKey::SignedAugmentedEGPublicKey(
00156    const AugmentedEGPublicKey& aAPK,
00157    const CODEX_Ciphers::RSASignature& sig ) :
00158    CODEX_ASN1::Base( true ),
00159    m_key( aAPK ),
00160    m_signature( sig )
00161 {
00162 }
00163 
00164 SignedAugmentedEGPublicKey::SignedAugmentedEGPublicKey(
00165    const AugmentedEGPublicKey& aAPK,
00166    const CODEX_Ciphers::RSAPrivateKey& key,
00167    const CODEX_Ciphers::HashFunction& hashFunc ) :
00168    CODEX_ASN1::Base( true ),
00169    m_key( aAPK )
00170 {
00171    BIGNUM * digest = 0;
00172    CODEX_Ciphers::RSASignature* sig = 0;
00173    try
00174    {
00175       digest = m_key.digest(hashFunc);
00176       sig = key.sign( digest );
00177       BN_free( digest );
00178       digest = 0;
00179       m_signature = *sig;
00180       delete sig;
00181    }
00182    catch ( ... )
00183    {
00184       if ( 0 != digest ) BN_free( digest );
00185       if ( 0 != sig ) delete sig;
00186       throw;
00187    }
00188 }
00189 
00190 SignedAugmentedEGPublicKey::SignedAugmentedEGPublicKey(
00191    const SignedAugmentedEGPublicKey& aOther ) :
00192    CODEX_ASN1::Base( aOther.m_initialized ),
00193    m_key( aOther.m_key ),
00194    m_signature( aOther.m_signature )
00195 {
00196 }
00197 
00198 void
00199 SignedAugmentedEGPublicKey::operator=(
00200    const SignedAugmentedEGPublicKey& aOther )
00201 {
00202    m_initialized = aOther.m_initialized;
00203    m_key         = aOther.m_key;
00204    m_signature   = aOther.m_signature;
00205 }
00206 
00207 bool
00208 SignedAugmentedEGPublicKey::verify(
00209    const CODEX_Ciphers::RSAPublicKey& key,
00210    const CODEX_Ciphers::HashFunction& hashFunc ) const
00211 {
00212    BIGNUM * digest = 0;
00213    try
00214    {
00215       digest = m_key.digest( hashFunc );
00216       bool retval = key.verifySignature( m_signature, digest );
00217       BN_free( digest );
00218       return retval;
00219    }
00220    catch ( ... )
00221    {
00222       if ( 0 != digest ) BN_free( digest );
00223       throw;
00224    }
00225 }
00226 
00227 int
00228 SignedAugmentedEGPublicKey::marshal( unsigned char ** pp ) const
00229 {
00230    int r=0;
00231    int ret=0;
00232    unsigned char * p;
00233 
00234    ret += m_key.marshal(0);
00235    ret += m_signature.marshal(0);
00236    M_ASN1_I2D_seq_total();
00237    m_key.marshal(&p);
00238    m_signature.marshal(&p);
00239    M_ASN1_I2D_finish();
00240 }
00241 
00242 void*
00243 SignedAugmentedEGPublicKey::unmarshal( void* bogus,
00244                                  unsigned char ** pp,
00245                                  long length )
00246 {
00247    if ( m_initialized )
00248    {
00249       return NULL;
00250    }
00251    if ( (NULL == pp) || (NULL == *pp) )
00252    {
00253       return NULL;
00254    }
00255    ASN1_CTX c;
00256    c.pp = pp;
00257    c.q = *pp;
00258    c.error = ERR_R_NESTED_ASN1_ERROR;
00259    int i;
00260 
00261    M_ASN1_D2I_Init();
00262    M_ASN1_D2I_start_sequence();
00263    M_ASN1_D2I_get(i, m_key.unmarshal);
00264    M_ASN1_D2I_get(i, m_signature.unmarshal);
00265    if ( !asn1_Finish(&c) )
00266    {
00267       return NULL;
00268    }
00269    *pp=c.p;
00270    m_initialized = true;
00271    return this;
00272   err: // needed by ASN.1 macros
00273    return NULL;
00274 }

Generated on Fri May 6 17:38:34 2005 for COrnell Data EXchange (CODEX) by  doxygen 1.4.1