Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   Related Pages  

client_functions.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: client_functions.cc,v 1.2 2004/05/19 15:56:49 mmarsh Exp $
00008 //
00009 // $Log: client_functions.cc,v $
00010 // Revision 1.2  2004/05/19 15:56:49  mmarsh
00011 // *** empty log message ***
00012 //
00013 // Revision 1.1  2003/11/06 17:01:02  mmarsh
00014 // Added the implementations for the C wrappers.  This required some changes
00015 // in the prototypes and structures, as well.
00016 //
00017 // The namespace CODEX_Client::Interface was incorrectly defined.  This has
00018 // now been fixed.
00019 //
00020 //
00021 
00022 #include "client_functions.h"
00023 
00024 using namespace CODEX_Client::Interface;
00025 
00026 Interface* Interface::m_instance = 0;
00027 
00028 Interface::Interface()
00029 {
00030 }
00031 
00032 Interface*
00033 Interface::instance()
00034 {
00035    if ( 0 == m_instance )
00036    {
00037       m_instance = new Interface;
00038    }
00039    return m_instance;
00040 }
00041 
00042 void
00043 Interface::destroy()
00044 {
00045    if ( 0 != m_instance )
00046    {
00047       delete m_instance;
00048    }
00049    m_instance = 0;
00050 }
00051 
00052 
00053 void
00054 CODEX_Client::Interface::codex_zero_policy( codex_policy_t* policy )
00055 {
00056    policy->data = 0;
00057    policy->length = 0;
00058 }
00059 
00060 void
00061 CODEX_Client::Interface::codex_zero_credentials(
00062    codex_credentials_t* credentials )
00063 {
00064    credentials->data = 0;
00065    credentials->length = 0;
00066 }
00067 
00068 void
00069 CODEX_Client::Interface::codex_zero_binding( codex_binding_t* binding )
00070 {
00071    binding->data = 0;
00072    binding->length = 0;
00073    binding->name = 0;
00074    binding->namelen = 0;
00075    binding->owner = 0;
00076    binding->read_policy = 0;
00077    binding->write_policy = 0;
00078    binding->signature = 0;
00079 }
00080 
00081 
00082 void
00083 CODEX_Client::Interface::codex_clear_policy( codex_policy_t* policy )
00084 {
00085    if ( 0 != policy->data ) delete [] policy->data;
00086    codex_zero_policy( policy );
00087 }
00088 
00089 void
00090 CODEX_Client::Interface::codex_clear_credentials(
00091    codex_credentials_t* credentials )
00092 {
00093    if ( 0 != credentials->data ) delete [] credentials->data;
00094    codex_zero_credentials( credentials );
00095 }
00096 
00097 void
00098 CODEX_Client::Interface::codex_clear_binding( codex_binding_t* binding )
00099 {
00100    if ( 0 != binding->data ) delete [] binding->data;
00101    if ( 0 != binding->name ) delete [] binding->name;
00102    if ( 0 != binding->owner ) X509_free( binding->owner );
00103    if ( 0 != binding->read_policy )
00104       codex_free_policy( binding->read_policy );
00105    if ( 0 != binding->write_policy )
00106       codex_free_policy( binding->write_policy );
00107    if ( 0 != binding->signature ) BN_free( binding->signature );
00108    codex_zero_binding( binding );
00109 }
00110 
00111 
00112 codex_policy_t*
00113 CODEX_Client::Interface::codex_new_policy()
00114 {
00115    codex_policy_t* retval = new codex_policy_t;
00116    codex_zero_policy( retval );
00117    return retval;
00118 }
00119 
00120 codex_credentials_t*
00121 CODEX_Client::Interface::codex_new_credentials()
00122 {
00123    codex_credentials_t* retval = new codex_credentials_t;
00124    codex_zero_credentials( retval );
00125    return retval;
00126 }
00127 
00128 codex_binding_t*
00129 CODEX_Client::Interface::codex_new_binding()
00130 {
00131    codex_binding_t* retval = new codex_binding_t;
00132    codex_zero_binding( retval );
00133    return retval;
00134 }
00135 
00136 
00138 int
00139 CODEX_Client::Interface::codex_parse_binding( codex_binding_t* binding )
00140 {
00141    if ( 0 == binding )
00142    {
00143       return 0;
00144    }
00145    if ( 0 == binding->data )
00146    {
00147       return 0;
00148    }
00149    if ( ( 0 != binding->name         ) ||
00150         ( 0 != binding->owner        ) ||
00151         ( 0 != binding->read_policy  ) ||
00152         ( 0 != binding->write_policy ) ||
00153         ( 0 != binding->signature    ) )
00154    {
00155       return -1;
00156    }
00157 
00158    try
00159    {
00160       CODEX_Client::SignedBoundNameMsg aBinding;
00161       unsigned char* pBuff = binding->data;
00162       aBinding.unmarshal( 0, &pBuff, binding->length );
00163 
00164       binding->namelen = aBinding.message().name().value().length();
00165       binding->name = new unsigned char [binding->namelen];
00166       memcpy( binding->name,
00167               aBinding.message().name().value().data(),
00168               binding->namelen );
00169 
00170       const X509* cert =
00171          aBinding.message().request().message().owner().value();
00172       binding->owner = X509_dup( (X509*)cert );
00173 
00174       binding->read_policy->length =
00175          aBinding.message().request().message().readP().marshal(0);
00176       binding->read_policy->data =
00177          new unsigned char[ binding->read_policy->length ];
00178       pBuff = binding->read_policy->data;
00179       aBinding.message().request().message().readP().marshal(&pBuff);
00180 
00181       binding->write_policy->length =
00182          aBinding.message().request().message().writeP().marshal(0);
00183       binding->write_policy->data =
00184          new unsigned char[ binding->write_policy->length ];
00185       pBuff = binding->write_policy->data;
00186       aBinding.message().request().message().writeP().marshal(&pBuff);
00187 
00188       binding->signature = BN_dup( aBinding.signature().value() );
00189 
00190       return 1;
00191    }
00192    catch ( ... )
00193    {
00194       // cache the marshalled data
00195       unsigned char* data = binding->data;
00196       long length = binding->length;
00197 
00198       // hide it from the utility function
00199       binding->data = 0;
00200       binding->length = 0;
00201 
00202       // clear everything else
00203       codex_clear_binding( binding );
00204 
00205       // restore the marshalled data
00206       binding->data = data;
00207       binding->length = length;
00208 
00209       return 0;
00210    }
00211 
00212    // We should never reach here.
00213    return 0;
00214 }
00215 
00216 
00217 void
00218 CODEX_Client::Interface::codex_free_policy( codex_policy_t* policy )
00219 {
00220    codex_clear_policy( policy );
00221    delete policy;
00222 }
00223 
00224 void
00225 CODEX_Client::Interface::codex_free_credentials(
00226    codex_credentials_t* credentials )
00227 {
00228    codex_clear_credentials( credentials );
00229    delete credentials;
00230 }
00231 
00232 void
00233 CODEX_Client::Interface::codex_free_binding( codex_binding_t* binding )
00234 {
00235    codex_clear_binding( binding );
00236    delete binding;
00237 }
00238 
00239 
00240 int
00241 CODEX_Client::Interface::codex_set_server( const char* name, int port )
00242 {
00243    Interface* instance = Interface::instance();
00244    if ( 0 == instance )
00245    {
00246       return 0;
00247    }
00248    try
00249    {
00250       instance->setRemoteServer( name, port );
00251    }
00252    catch ( ... )
00253    {
00254       return 0;
00255    }
00256    return 1;
00257 }
00258 
00259 int
00260 CODEX_Client::Interface::codex_set_service_key( const X509* cert )
00261 {
00262    Interface* instance = Interface::instance();
00263    if ( 0 == instance )
00264    {
00265       return 0;
00266    }
00267    try
00268    {
00269       instance->setServiceKey( new CODEX_Ciphers::RSAPublicKey(cert) );
00270    }
00271    catch ( ... )
00272    {
00273       return 0;
00274    }
00275    return 1;
00276 }
00277 
00279 int
00280 CODEX_Client::Interface::codex_set_key_pair( const X509* cert, const RSA* key )
00281 {
00282    Interface* instance = Interface::instance();
00283    if ( 0 == instance )
00284    {
00285       return 0;
00286    }
00287    try
00288    {
00289       instance->setKeyPair(
00290          new CODEX_ASN1::Certificate( X509_dup((X509*)cert) ),
00291          new CODEX_Ciphers::RSAPrivateKey( BN_dup(key->p),
00292                                            BN_dup(key->q),
00293                                            BN_dup(key->d),
00294                                            BN_dup(key->n) ) );
00295    }
00296    catch ( ... )
00297    {
00298       return 0;
00299    }
00300    return 1;
00301 }
00302 
00303 int
00304 CODEX_Client::Interface::codex_create_policy( const RSA* policy_pub_key,
00305                                               const RSA* owner_priv_key,
00306                                               codex_policy_t* policy )
00307 {
00308    if ( 0 == policy )
00309    {
00310       return 0;
00311    }
00312    codex_clear_policy( policy );
00313 
00314    Interface* instance = Interface::instance();
00315    if ( 0 == instance )
00316    {
00317       return 0;
00318    }
00319    CODEX_Ciphers::Policy* pCCP = 0;
00320    try
00321    {
00322       CODEX_Ciphers::RSAPublicKey aCCPub( BN_dup(policy_pub_key->n),
00323                                           BN_dup(policy_pub_key->e) );
00324 
00325       CODEX_Ciphers::RSAPrivateKey aCCPriv( BN_dup(owner_priv_key->p),
00326                                             BN_dup(owner_priv_key->q),
00327                                             BN_dup(owner_priv_key->d),
00328                                             BN_dup(owner_priv_key->n) );
00329 
00330       pCCP = instance->createPolicy( aCCPub, aCCPriv );
00331 
00332       policy->length = pCCP->marshal(0);
00333       policy->data = new unsigned char[policy->length];
00334       unsigned char* pBuff = policy->data;
00335       pCCP->marshal(&pBuff);
00336       delete pCCP; pCCP = 0;
00337    }
00338    catch ( ... )
00339    {
00340       if ( 0 != pCCP ) delete pCCP;
00341       codex_clear_policy( policy );
00342       return 0;
00343    }
00344    return 1;
00345 }
00346 
00347 int
00348 CODEX_Client::Interface::codex_issue_credentials(
00349    const RSA* client_pub_key,
00350    const RSA* policy_priv_key,
00351    codex_credentials_t* credentials )
00352 {
00353    if ( 0 == credentials )
00354    {
00355       return 0;
00356    }
00357    codex_clear_credentials( credentials );
00358 
00359    Interface* instance = Interface::instance();
00360    if ( 0 == instance )
00361    {
00362       return 0;
00363    }
00364    CODEX_Ciphers::Credentials* pCCC = 0;
00365    try
00366    {
00367       CODEX_Ciphers::RSAPublicKey aCCPub( BN_dup(client_pub_key->n),
00368                                           BN_dup(client_pub_key->e) );
00369 
00370       CODEX_Ciphers::RSAPrivateKey aCCPriv( BN_dup(policy_priv_key->p),
00371                                             BN_dup(policy_priv_key->q),
00372                                             BN_dup(policy_priv_key->d),
00373                                             BN_dup(policy_priv_key->n) );
00374 
00375       pCCC = instance->issueCredentials( aCCPub, aCCPriv );
00376 
00377       credentials->length = pCCC->marshal(0);
00378       credentials->data = new unsigned char[credentials->length];
00379       unsigned char* pBuff = credentials->data;
00380       pCCC->marshal(&pBuff);
00381       delete pCCC; pCCC = 0;
00382    }
00383    catch ( ... )
00384    {
00385       if ( 0 != pCCC ) delete pCCC;
00386       codex_clear_credentials( credentials );
00387       return 0;
00388    }
00389    return 1;
00390 }
00391 
00394 int
00395 CODEX_Client::Interface::codex_create_key(
00396    const unsigned char* name,
00397    int length,
00398    const X509* owner_cert,
00399    const codex_policy_t* read_policy,
00400    const codex_policy_t* write_policy,
00401    codex_binding_t* binding )
00402 {
00403    if ( ( 0 == name         ) ||
00404         ( 0 == owner_cert   ) ||
00405         ( 0 == read_policy  ) ||
00406         ( 0 == write_policy ) ||
00407         ( 0 == binding      ) )
00408    {
00409       return 0;
00410    }
00411 
00412    // Clear the binding object
00413    codex_clear_binding( binding );
00414 
00415    Interface* instance = Interface::instance();
00416    if ( 0 == instance )
00417    {
00418       return 0;
00419    }
00420    try
00421    {
00422       CODEX_ASN1::ustring aName( name, length );
00423       CODEX_ASN1::Certificate aCert( X509_dup((X509*)owner_cert) );
00424 
00425       CODEX_Ciphers::Policy aReadP;
00426       CODEX_Ciphers::Policy aWriteP;
00427 
00428       unsigned char* pBuff = (unsigned char*)(read_policy->data);
00429       aReadP.unmarshal( 0,
00430                         &pBuff,
00431                         read_policy->length );
00432       pBuff = (unsigned char*)(write_policy->data);
00433       aWriteP.unmarshal( 0,
00434                          &pBuff,
00435                          write_policy->length );
00436 
00437       CODEX_Client::SignedBoundNameMsg aBinding;
00438       bool retval =
00439          instance->createKey( aName, aCert, aReadP, aWriteP, aBinding );
00440 
00441       if ( ! retval )
00442       {
00443          return 0;
00444       }
00445 
00446       binding->length = aBinding.marshal(0);
00447       binding->data = new unsigned char[binding->length];
00448       pBuff = binding->data;
00449       aBinding.marshal(&pBuff);
00450       return retval ? 1 : 0;
00451    }
00452    catch ( ... )
00453    {
00454       codex_clear_binding( binding );
00455       return 0;
00456    }
00457 
00458    // We shouldn't reach here.
00459    return 0;
00460 }
00461 
00463 int
00464 CODEX_Client::Interface::codex_write_key(
00465    const unsigned char* name,
00466    int length,
00467    const BIGNUM* key_value,
00468    const codex_credentials_t* credentials,
00469    const RSA* cred_private_key,
00470    const codex_binding_t* binding )
00471 {
00472    if ( ( 0 == name             ) ||
00473         ( 0 == key_value        ) ||
00474         ( 0 == credentials      ) ||
00475         ( 0 == cred_private_key ) ||
00476         ( 0 == binding          ) )
00477    {
00478       return 0;
00479    }
00480 
00481    Interface* instance = Interface::instance();
00482    if ( 0 == instance )
00483    {
00484       return 0;
00485    }
00486    try
00487    {
00488       CODEX_ASN1::ustring aName( name, length );
00489 
00490       CODEX_Ciphers::RSAPrivateKey aKey( BN_dup(cred_private_key->p),
00491                                          BN_dup(cred_private_key->q),
00492                                          BN_dup(cred_private_key->d),
00493                                          BN_dup(cred_private_key->n) );
00494 
00495       CODEX_Ciphers::Credentials aCred;
00496       CODEX_Client::SignedBoundNameMsg aBinding;
00497 
00498       const unsigned char* pBuff = credentials->data;
00499       aCred.unmarshal( 0,
00500                        &(unsigned char*)(pBuff),
00501                        credentials->length );
00502       pBuff = binding->data;
00503       aBinding.unmarshal( 0,
00504                           &(unsigned char*)(pBuff),
00505                           binding->length );
00506 
00507       bool retval = instance->writeKey( aName,
00508                                         key_value,
00509                                         aCred,
00510                                         aKey,
00511                                         aBinding );
00512 
00513       return retval ? 1 : 0;
00514    }
00515    catch ( ... )
00516    {
00517       return 0;
00518    }
00519 
00520    // We shouldn't reach here.
00521    return 0;
00522 }
00523 
00525 int
00526 CODEX_Client::Interface::codex_read_key(
00527    const unsigned char* name,
00528    int length,
00529    const codex_credentials_t* credentials,
00530    const RSA* cred_private_key,
00531    BIGNUM** returned_key_value )
00532 {
00533    if ( ( 0 == name               ) ||
00534         ( 0 == credentials        ) ||
00535         ( 0 == cred_private_key   ) ||
00536         ( 0 == returned_key_value ) )
00537    {
00538       return 0;
00539    }
00540 
00541    Interface* instance = Interface::instance();
00542    if ( 0 == instance )
00543    {
00544       return 0;
00545    }
00546    try
00547    {
00548       CODEX_ASN1::ustring aName( name, length );
00549 
00550       CODEX_Ciphers::RSAPrivateKey aKey( BN_dup(cred_private_key->p),
00551                                          BN_dup(cred_private_key->q),
00552                                          BN_dup(cred_private_key->d),
00553                                          BN_dup(cred_private_key->n) );
00554 
00555 
00556       CODEX_Ciphers::Credentials aCred;
00557 
00558       const unsigned char* pBuff = credentials->data;
00559       aCred.unmarshal( 0,
00560                        &(unsigned char*)(pBuff),
00561                        credentials->length );
00562 
00563       bool retval = instance->readKey( aName,
00564                                        aCred,
00565                                        aKey,
00566                                        returned_key_value );
00567 
00568       return retval ? 1 : 0;
00569    }
00570    catch ( ... )
00571    {
00572       return 0;
00573    }
00574 
00575    // We shouldn't reach here.
00576    return 0;
00577 }
00578 
00579 int
00580 CODEX_Client::Interface::codex_to_file( const char* fname )
00581 {
00582    Interface* instance = Interface::instance();
00583    if ( 0 == instance )
00584    {
00585       return 0;
00586    }
00587    try
00588    {
00589       instance->toFile( fname );
00590    }
00591    catch ( ... )
00592    {
00593       return 0;
00594    }
00595    return 1;
00596 }
00597 
00598 int
00599 CODEX_Client::Interface::codex_from_file( const char* fname )
00600 {
00601    Interface* instance = Interface::instance();
00602    if ( 0 == instance )
00603    {
00604       return 0;
00605    }
00606    try
00607    {
00608       instance->fromFile( fname );
00609    }
00610    catch ( ... )
00611    {
00612       return 0;
00613    }
00614    return 1;
00615 }
00616 
00617 void
00618 CODEX_Client::Interface::codex_clean_up()
00619 {
00620    Interface::destroy();
00621 }

Generated on Wed Jun 2 16:32:54 2004 for COrnell Data EXchange (CODEX) by doxygen1.2.18