home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / SRS / client / src / ssl.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-12  |  3.6 KB  |  138 lines

  1. #include "headers.h"
  2.  
  3. #ifndef NOSSL
  4.  
  5. /* make an official SSL connection */
  6. void makeSSLconn()
  7. {
  8.    int res;
  9.    int flags = SSL_VERIFY_PEER; /* | SSL_VERIFY_FAIL_IF_NO_PEER_CERT; */
  10.  
  11.    static int init = 0;
  12.  
  13.    char *clientpem = PARAMDIR "/client.pem";
  14.    char *serverpem = PARAMDIR "/server.pem";
  15.  
  16.    if (init == 0) 
  17.    {
  18.       init = 1;
  19.       SSL_load_error_strings();
  20.       SSLeay_add_all_algorithms(), SSLeay_add_ssl_algorithms();
  21.    }
  22.  
  23.    /* clean up after any previous connections */
  24.    if (sslconn != NULL) SSL_free(sslconn);
  25.    if (sslctx != NULL) SSL_CTX_free(sslctx);
  26.  
  27.    sslconn = NULL, sslctx = NULL;
  28.  
  29.    /* create a context */
  30.    sslctx = SSL_CTX_new(SSLv3_client_method());
  31.    if (sslctx == NULL) 
  32.    {
  33.       error("error with SSL_CTX_new(): %s\n\n", strerror(errno));
  34.  
  35.       ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  36.       if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  37.  
  38.       quit(ERROR);
  39.    }
  40.  
  41.    /* clear all options since they all have to do with bugs */
  42.    SSL_CTX_set_options(sslctx, 0);
  43.  
  44.    res = SSL_CTX_set_cipher_list(sslctx, "ADH:+DES:+EXP");
  45.  
  46.    if (res <= 0) 
  47.    {
  48.       error("error with SSL_CTX_set_cipher_list(): %s\n\n", strerror(errno));
  49.  
  50.       ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  51.       if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  52.  
  53.       quit(ERROR);
  54.    }
  55.          
  56.    /* indicate strong verification */
  57.    SSL_CTX_set_verify(sslctx, flags, NULL);
  58.  
  59.    /* indicate where we can find the server's certificate */
  60.    res = SSL_CTX_load_verify_locations(sslctx, serverpem, PARAMDIR);
  61.    if (res < 0) 
  62.    {
  63.       error("error with SSL_CTX_load_verify_locations(): %s\n\n", 
  64.             strerror(errno));
  65.  
  66.       ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  67.       if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  68.  
  69.       quit(ERROR);
  70.    }
  71.       
  72.    res = SSL_CTX_set_default_verify_paths(sslctx);
  73.    if (res < 0) 
  74.    {
  75.       error("error with SSL_CTX_set_default_verify_paths(): %s\n\n", 
  76.             strerror(errno));
  77.  
  78.       ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  79.       if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  80.  
  81.       quit(ERROR);
  82.    }
  83.       
  84.     
  85.    /* create a connection */
  86.    sslconn = SSL_new(sslctx);
  87.    if (sslconn == NULL) 
  88.    {
  89.       error("error with SSL_new(): %s\n\n", strerror(errno));
  90.  
  91.       ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  92.       if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  93.  
  94.       quit(ERROR);
  95.    }
  96.    
  97.    /* specify where our private key is */
  98.    res = SSL_use_PrivateKey_file(sslconn, clientpem, SSL_FILETYPE_PEM);
  99.    if (res <= 0) 
  100.    {
  101.       error("error with SSL_use_PrivateKey_file(): %s\n\n", strerror(errno));
  102.  
  103.       ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  104.       if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  105.  
  106.       quit(ERROR);
  107.    }
  108.       
  109.    /* specify where our certificate is */
  110.    res = SSL_use_certificate_file(sslconn, clientpem, SSL_FILETYPE_PEM);
  111.    if (res <= 0) 
  112.    {
  113.       error("error with SSL_use_certificate_file(): %s\n\n", strerror(errno));
  114.  
  115.       ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  116.       if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  117.  
  118.       quit(ERROR);
  119.    }
  120.     
  121.    /* communicate which socket we are using */
  122.    res = SSL_set_fd(sslconn, sockfd);
  123.    if (res <= 0) 
  124.    {
  125.       error("error with SSL_set_fd(): %s\n\n", strerror(errno));
  126.  
  127.       ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  128.       if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  129.  
  130.       quit(ERROR);
  131.    }
  132.       
  133.    /* setup to connect next time we send/recv data */
  134.    SSL_set_connect_state(sslconn);
  135. }
  136.  
  137. #endif /* NOSSL */
  138.