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

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