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

test_c_client.c

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: test_c_client.c,v 1.2 2004/05/19 15:56:46 mmarsh Exp $
00008  *
00009  * $Log: test_c_client.c,v $
00010  * Revision 1.2  2004/05/19 15:56:46  mmarsh
00011  * *** empty log message ***
00012  *
00013  * Revision 1.1  2003/11/06 18:16:08  mmarsh
00014  * New example client written in C.  This is based on timing_client.cc,
00015  * and has the same functionality.  The intent is to
00016  * (a) provide sample code for using the C functions in
00017  *     CODEX_Client/client_functions.h, and
00018  * (b) test the implementation of the C functions.
00019  *
00020  *
00021  */
00022 
00031 #include <unistd.h>
00032 #include <string.h>
00033 #include <openssl/ssl.h>
00034 #include <openssl/conf.h>
00035 
00036 #include "CODEX_Client/client_functions.h"
00037 
00038 void get_cert( CONF* conf,
00039                const char* sec,
00040                const char* name,
00041                X509** cert )
00042 {
00043    const char* fname;
00044    FILE* fp;
00045 
00046    fname = NCONF_get_string(conf,sec,name);
00047    if ( NULL == fname )
00048    {
00049       fprintf(stderr,
00050               "No variable %s found in section %s of %s\n",name,sec,fname);
00051       exit(1);
00052    }
00053    fp = fopen( fname, "r" );
00054    if ( NULL == fp )
00055    {
00056       fprintf(stderr,"Could not open file %s\n",fname);
00057       exit(1);
00058    }
00059    if ( ! PEM_read_X509( fp, cert, NULL, NULL ) )
00060    {
00061       fprintf(stderr,"Error reading %s\n",fname);
00062       fclose(fp);
00063       exit(1);
00064    }
00065    fclose(fp);
00066 }
00067 
00068 void get_priv_key( CONF* conf,
00069                    const char* sec,
00070                    const char* name,
00071                    const char* passwd,
00072                    RSA** key )
00073 {
00074    const char* fname;
00075    const char* pwd;
00076    FILE* fp;
00077 
00078    fname = NCONF_get_string(conf,sec,name);
00079    pwd = NCONF_get_string(conf,sec,passwd);
00080    if ( NULL == fname )
00081    {
00082       fprintf(stderr,
00083               "No variable %s found in section %s of %s\n",name,sec,fname);
00084       exit(1);
00085    }
00086    fp = fopen( fname, "r" );
00087    if ( NULL == fp )
00088    {
00089       fprintf(stderr,"Could not open file %s\n",fname);
00090       exit(1);
00091    }
00092    if ( ! PEM_read_RSAPrivateKey( fp,
00093                                   key,
00094                                   NULL,
00095                                   (void*)pwd ) )
00096    {
00097       fprintf(stderr,"Error reading %s\n",fname);
00098       fclose(fp);
00099       exit(1);
00100    }
00101    fclose(fp);
00102 }
00103 
00104 
00105 int main( int argc, char** argv )
00106 {
00107    /* variables */
00108    int arg = 0;
00109    char* config_file = 0;
00110    char* config_section = 0;
00111    const char* usage_string =
00112       "Usage: example_client -c <config_file> [-s <section>]";
00113    CONF* conf;
00114    const char* sec;
00115    long dummy;
00116    unsigned long remote_port;
00117    const char* remote_host;
00118    X509* client_cert = 0;
00119    X509* service_cert = 0;
00120    RSA* client_pub_key;
00121    RSA* client_priv_key = 0;
00122    unsigned char i;
00123    unsigned char* keyName;
00124    int length;
00125    codex_binding_t binding;
00126    codex_policy_t policy;
00127    const BIGNUM* keyVal;
00128    codex_credentials_t credentials;
00129    BIGNUM * pKeyVal;
00130 
00131 
00132    /* initialize structures */
00133    codex_zero_binding(&binding);
00134    codex_zero_policy(&policy);
00135    codex_zero_credentials(&credentials);
00136 
00137 
00138    /* initialize OpenSSL */
00139    SSLeay_add_ssl_algorithms();
00140 
00141 
00142    /* parse command line */
00143    while ( -1 != arg )
00144    {
00145       arg = getopt(argc,argv,"c:s:");
00146       switch(arg)
00147       {
00148          case 'c' :
00149             config_file = optarg;
00150             break;
00151          case 's' :
00152             config_section = optarg;
00153             break;
00154          case ':' :
00155          case '?' :
00156             fprintf(stderr,"%s\n",usage_string);
00157             return 1;
00158       }
00159    }
00160    if ( ( 0 == config_file ) || ( 0 == strlen(config_file) ) )
00161    {
00162       fprintf(stderr,"%s\n",usage_string);
00163       return 1;
00164    }
00165    conf = NCONF_new(NCONF_default());
00166    if ( 0 == NCONF_load(conf,config_file,0) )
00167    {
00168       fprintf(stderr,"Cannot open %s\n",config_file);
00169       return 1;
00170    }
00171    sec = config_section;
00172 
00173 
00174    /* get remote server information */
00175    if ( ! NCONF_get_number_e(conf,sec,"remote_port",&dummy) )
00176    {
00177       fprintf(stderr,"remove_port not defined in %s\n",config_file);
00178       return 1;
00179    }
00180    remote_port = dummy;
00181    remote_host = NCONF_get_string(conf,sec,"remote_host");
00182    if ( 0 == remote_host )
00183    {
00184       fprintf(stderr,"remote_host not defined in %s\n",config_file);
00185       return 1;
00186    }
00187 
00188    codex_set_server( remote_host, remote_port );
00189 
00190 
00191    /* Get the certificate and public key */
00192    get_cert(conf,sec,"client_cert_file",&client_cert);
00193    client_pub_key = EVP_PKEY_get1_RSA(X509_get_pubkey(client_cert));
00194 
00195    /* Get the private key */
00196    get_priv_key( conf, sec,
00197                  "client_private_file", "private_key_passwd",
00198                  &client_priv_key );
00199 
00200 
00201    /* This does not take ownership of the memory. */
00202    codex_set_key_pair( client_cert, client_priv_key );
00203 
00204    /* Initialize the service's public key. */
00205    get_cert(conf,sec,"service_cert_file",&service_cert);
00206    codex_set_service_key( service_cert );
00207 
00208    /* Create a policy */
00209    if ( ! codex_create_policy( client_pub_key,
00210                                client_priv_key,
00211                                &policy ) )
00212    {
00213       fprintf(stderr,"Error creating policy\n");
00214       exit(1);
00215    }
00216 
00217    /* Pause for awhile to allow transients to settle out on the servers. */
00218    sleep(10);
00219 
00220    /* Now loop a number of times to collect statistics. */
00221    for ( i = 0 ; i < 10 ; ++i )
00222    {
00223       /* Begin by waiting, to space the requests out and avoid overloading
00224        * the network.
00225        */
00226       sleep(3);
00227       fprintf(stdout,"%d\n",i);
00228       fflush(stdout);
00229 
00230       length = 1;
00231       keyName = (unsigned char*) malloc(length*sizeof(unsigned char));
00232       keyName[0] = i;
00233 
00234       if ( ! codex_create_key( keyName,
00235                                length,
00236                                client_cert,
00237                                &policy,
00238                                &policy,
00239                                &binding ) )
00240       {
00241          fprintf(stderr,"error in codex_create_key\n");
00242          return 1;
00243       }
00244 
00245       keyVal = client_priv_key->d;
00246       if ( ! codex_issue_credentials( client_pub_key,
00247                                       client_priv_key,
00248                                       &credentials ) )
00249       {
00250          fprintf(stderr,"error in codex_issue_credentials\n");
00251          return 1;
00252       }
00253       if ( ! codex_write_key( keyName,
00254                               length,
00255                               keyVal,
00256                               &credentials,
00257                               client_priv_key,
00258                               &binding ) )
00259       {
00260          fprintf(stderr,"error in codex_write_key\n");
00261          return 1;
00262       }
00263 
00264       if ( ! codex_read_key( keyName,
00265                              length,
00266                              &credentials,
00267                              client_priv_key,
00268                              &pKeyVal ) )
00269       {
00270          fprintf(stderr,"error in codex_read_key\n");
00271          return 1;
00272       }
00273       if ( 0 != BN_cmp( keyVal, pKeyVal ) )
00274       {
00275          fprintf(stderr,"values do not match!\n");
00276          return 1;
00277       }
00278    }
00279 
00280    return 0;
00281 }

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