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

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