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

timing_client.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: timing_client.cc,v 1.6 2005/01/21 19:44:16 mmarsh Exp $
00008 //
00009 // $Log: timing_client.cc,v $
00010 // Revision 1.6  2005/01/21 19:44:16  mmarsh
00011 // Updated for compatibility with Doxygen 1.4.1
00012 //
00013 // Revision 1.5  2004/05/19 15:56:46  mmarsh
00014 // *** empty log message ***
00015 //
00016 // Revision 1.4  2003/11/06 21:45:51  mmarsh
00017 // Added a sample configuration file.
00018 //
00019 // Revision 1.3  2003/11/06 18:12:47  mmarsh
00020 // Added doxygen comments.
00021 //
00022 // Revision 1.2  2003/11/04 22:07:36  mmarsh
00023 // General code cleanup and reorganization.
00024 //
00025 //
00026 
00068 #include <unistd.h>
00069 #include <openssl/ssl.h>
00070 #include <openssl/conf.h>
00071 #include <iostream>
00072 
00073 #include "CODEX_Client/Client.h"
00074 
00075 int main( int argc, char** argv )
00076 {
00077    SSLeay_add_ssl_algorithms();
00078 
00079    int arg = 0;
00080    string config_file;
00081    string config_section;
00082    string usage_string =
00083       "Usage: example_client -c <config_file> [-s <section>]";
00084    while ( -1 != arg )
00085    {
00086       arg = getopt(argc,argv,"c:s:");
00087       switch(arg)
00088       {
00089          case 'c' :
00090             config_file = optarg;
00091             break;
00092          case 's' :
00093             config_section = optarg;
00094             break;
00095          case ':' :
00096          case '?' :
00097             cerr << usage_string << endl;
00098             ::exit(1);
00099       }
00100    }
00101    if ( 0 == config_file.size() )
00102    {
00103       cerr << usage_string << endl;
00104       ::exit(1);
00105    }
00106    CONF* conf = NCONF_new(NCONF_default());
00107    if ( 0 == NCONF_load(conf,config_file.c_str(),0) )
00108    {
00109       cerr << "Cannot open " << config_file << endl;
00110       ::exit(1);
00111    }
00112    const char* sec = config_section.c_str();
00113 
00114    long dummy;
00115    if ( ! NCONF_get_number_e(conf,sec,"remote_port",&dummy) )
00116    {
00117       cerr << "remote_port not defined in " << config_file << endl;
00118       ::exit(1);
00119    }
00120    unsigned long remote_port = dummy;
00121    const char* remote_host = NCONF_get_string(conf,sec,"remote_host");
00122    if ( 0 == remote_host )
00123    {
00124       cerr << "remote_host not defined in " << config_file << endl;
00125       ::exit(1);
00126    }
00127    CODEX_Client::Client client;
00128    client.setRemoteServer( remote_host , remote_port );
00129 
00130 
00131    // Get the certificate and public key
00132    CODEX_ASN1::Certificate* clientCert = new CODEX_ASN1::Certificate;
00133    clientCert->fromPEMFile( NCONF_get_string(conf,sec,"client_cert_file") );
00134 
00135    CODEX_Ciphers::RSAPublicKey pubKey( clientCert->value() );
00136 
00137 
00138    // Get the private key
00139    CODEX_Ciphers::RSAPrivateKey* privKey = new CODEX_Ciphers::RSAPrivateKey;
00140    string private_file = NCONF_get_string(conf,sec,"client_private_file");
00141    string private_pwd = NCONF_get_string(conf,sec,"private_key_passwd");
00142    privKey->fromPEMFile( private_file.data(), private_pwd.data() );
00143 
00144 
00145    // This takes ownership of the memory.
00146    client.setKeyPair( clientCert, privKey );
00147 
00148    // Initialize the service's public key.
00149    CODEX_ASN1::Certificate serviceCert;
00150    serviceCert.fromPEMFile( NCONF_get_string(conf,sec,"service_cert_file") );
00151    CODEX_Ciphers::RSAPublicKey* serviceKey =
00152       new CODEX_Ciphers::RSAPublicKey( serviceCert.value() );
00153    client.setServiceKey( serviceKey );
00154 
00155    // Create a policy
00156    CODEX_Ciphers::Policy* policy = client.createPolicy( pubKey, *privKey );
00157 
00158    // Pause for awhile to allow transients to settle out on the servers.
00159    sleep(300);
00160 
00161    // Now loop a number of times to collect statistics.
00162    for ( unsigned char i = 0 ; i < 110 ; ++i )
00163    {
00164       // Begin by waiting, to space the requests out and avoid overloading
00165       // the network.
00166       sleep(30);
00167       cout << (unsigned int)i << endl;
00168 
00169       CODEX_ASN1::ustring keyName;
00170       keyName += i;
00171 
00172       CODEX_Client::SignedBoundNameMsg boundNameMsg;
00173 
00174       if ( ! client.createKey( keyName,
00175                                *clientCert,
00176                                *policy,
00177                                *policy,
00178                                boundNameMsg ) )
00179       {
00180          cerr << "error in createKey" << endl;
00181          return 1;
00182       }
00183 
00184       const BIGNUM * keyVal = privKey->d().value();
00185       CODEX_Ciphers::Credentials* credentials =
00186          client.issueCredentials( pubKey, *privKey );
00187       if ( ! client.writeKey( keyName,
00188                               keyVal,
00189                               *credentials,
00190                               *privKey,
00191                               boundNameMsg ) )
00192       {
00193          cerr << "error in writeKey" << endl;
00194          return 1;
00195       }
00196 
00197       BIGNUM * pKeyVal;
00198       if ( ! client.readKey( keyName,
00199                              *credentials,
00200                              *privKey,
00201                              &pKeyVal ) )
00202       {
00203          cerr << "error in readKey" << endl;
00204          return 1;
00205       }
00206       if ( 0 != BN_cmp( keyVal, pKeyVal ) )
00207       {
00208          cerr << "values do not match!" << endl;
00209          return 1;
00210       }
00211    }
00212 
00213    return 0;
00214 }

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