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