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

PolCredBase.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: PolCredBase.cc,v 1.3 2004/05/19 15:56:47 mmarsh Exp $
00008 //
00009 // $Log: PolCredBase.cc,v $
00010 // Revision 1.3  2004/05/19 15:56:47  mmarsh
00011 // *** empty log message ***
00012 //
00013 // Revision 1.2  2003/11/04 22:31:47  mmarsh
00014 // *** empty log message ***
00015 //
00016 //
00017 
00018 #include "PolCredBase.h"
00019 #include "CODEX_Exceptions/BignumExceptions.h"
00020 
00021 using namespace CODEX_Ciphers;
00022 using namespace CODEX_Exceptions;
00023 
00024 PolCredBase::PolCredBase() :
00025    CODEX_ASN1::Base( false )
00026 {
00027 }
00028 
00029 PolCredBase::PolCredBase( const RSAPublicKey& publicKey,
00030                           const RSASignature& signature ) :
00031    CODEX_ASN1::Base( true ),
00032    m_publicKey( publicKey ),
00033    m_signature( signature )
00034 {
00035 }
00036 
00037 PolCredBase::PolCredBase( const RSAPublicKey& publicKey,
00038                           const RSAPrivateKey& signingKey,
00039                           const HashFunction& hashFunc ) :
00040    CODEX_ASN1::Base( true ),
00041    m_publicKey( publicKey )
00042 {
00043    BIGNUM * pubKeyBN = NULL;
00044    RSASignature* signature = NULL;
00045 
00046    try
00047    {
00048       pubKeyBN = digest( m_publicKey, hashFunc );
00049       signature = signingKey.sign( pubKeyBN );
00050       BN_free( pubKeyBN );
00051       pubKeyBN = NULL;
00052       m_signature = *signature;
00053       delete signature;
00054       signature = NULL;
00055    }
00056    catch ( ... )
00057    {
00058       if ( NULL != pubKeyBN ) BN_free( pubKeyBN );
00059       if ( NULL != signature ) delete signature;
00060       throw;
00061    }
00062 }
00063 
00064 PolCredBase::PolCredBase( const PolCredBase& aCred ) :
00065    CODEX_ASN1::Base( aCred.m_initialized ),
00066    m_publicKey( aCred.m_publicKey ),
00067    m_signature( aCred.m_signature )
00068 {
00069 }
00070 
00071 void
00072 PolCredBase::operator=( const PolCredBase& aCred )
00073 {
00074    m_initialized = aCred.m_initialized;
00075    m_publicKey   = aCred.m_publicKey;
00076    m_signature   = aCred.m_signature;
00077 }
00078 
00079 bool
00080 PolCredBase::verify( const RSAPublicKey& delegator,
00081                      const HashFunction& hashFunc ) const
00082 {
00083    BIGNUM * pkDigest = NULL;
00084 
00085    try
00086    {
00087       pkDigest = digest( m_publicKey, hashFunc );
00088       bool retVal = delegator.verifySignature( m_signature, pkDigest );
00089       BN_free( pkDigest );
00090       pkDigest = NULL;
00091       return retVal;
00092    }
00093    catch ( ... )
00094    {
00095       if ( NULL != pkDigest ) BN_free( pkDigest );
00096       throw;
00097    }
00098 }
00099 
00100 int
00101 PolCredBase::marshal( unsigned char ** pp ) const
00102 {
00103    int r=0;
00104    int ret=0;
00105    unsigned char * p;
00106 
00107    ret += m_publicKey.marshal(0);
00108    ret += m_signature.marshal(0);
00109    M_ASN1_I2D_seq_total();
00110    m_publicKey.marshal(&p);
00111    m_signature.marshal(&p);
00112    M_ASN1_I2D_finish();
00113 }
00114 
00115 void*
00116 PolCredBase::unmarshal( void* bogus, unsigned char ** pp, long length )
00117 {
00118    if ( m_initialized )
00119    {
00120       return NULL;
00121    }
00122    if ( (NULL == pp) || (NULL == *pp) )
00123    {
00124       return NULL;
00125    }
00126 
00127    ASN1_CTX c;
00128    c.pp = pp;
00129    c.q = *pp;
00130    c.error = ERR_R_NESTED_ASN1_ERROR;
00131    int i;
00132 
00133    M_ASN1_D2I_Init();
00134    M_ASN1_D2I_start_sequence();
00135    M_ASN1_D2I_get(i, m_publicKey.unmarshal);
00136    M_ASN1_D2I_get(i, m_signature.unmarshal);
00137    if ( !asn1_Finish(&c) )
00138    {
00139       return NULL;
00140    }
00141    *pp=c.p;
00142    m_initialized = true;
00143    return this;
00144   err: // needed by ASN.1 macros
00145    return NULL;
00146 }
00147 
00148 BIGNUM *
00149 PolCredBase::digest( const RSAPublicKey& publicKey,
00150                      const HashFunction& hashFunc )
00151 {
00152    unsigned char* buff = NULL;
00153    CODEX_ASN1::ustring* str = NULL;
00154    BIGNUM * pubKeyBN = NULL;
00155 
00156    try
00157    {
00158       int length = publicKey.marshal(0);
00159       buff = new unsigned char[ length ];
00160       unsigned char* pBuff = buff;
00161       publicKey.marshal(&pBuff);
00162       str = hashFunc( CODEX_ASN1::ustring( buff, length ) );
00163       delete [] buff;
00164       buff = NULL;
00165       pubKeyBN = BN_new();
00166       if ( NULL == pubKeyBN )
00167       {
00168          throw BignumNullException( __FILE__ , __LINE__ );
00169       }
00170       if ( NULL == BN_bin2bn( str->data(), str->length(), pubKeyBN ) )
00171       {
00172          throw BignumBin2BNException( __FILE__ , __LINE__ );
00173       }
00174       delete str;
00175       str = NULL;
00176       return pubKeyBN;
00177    }
00178    catch ( ... )
00179    {
00180       if ( NULL != buff ) delete buff;
00181       if ( NULL != str ) delete str;
00182       if ( NULL != pubKeyBN ) BN_free( pubKeyBN );
00183       throw;
00184    }
00185 }

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