00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
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
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
00143 client.setKeyPair( clientCert, privKey );
00144
00145
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
00153 CODEX_Ciphers::Policy* policy = client.createPolicy( pubKey, *privKey );
00154
00155
00156 sleep(300);
00157
00158
00159 for ( unsigned char i = 0 ; i < 110 ; ++i )
00160 {
00161
00162
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 }