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

  1. #include "headers.h" /* include all important things */
  2.  
  3. #ifndef NOSSL
  4.  
  5. /* load the DH parameters */
  6. void loadDHparams()
  7. {
  8.    FILE *parmfd;
  9.  
  10.    while(1)
  11.    {
  12.       /* FIX - no hardcode */
  13.       parmfd = fopen(PARAMDIR "/dh.params", "r");
  14.  
  15.       if (parmfd == NULL) 
  16.       {
  17.          if (errno == EINTR) continue;
  18.  
  19.          error("error opening %s: %s\n\n", PARAMDIR "/dh.params",
  20.                strerror(errno));
  21.  
  22.          quit(ERROR);
  23.       }
  24.  
  25.       else break;
  26.    }
  27.  
  28.    dhparms = PEM_read_DHparams(parmfd, NULL, NULL);
  29.    if (dhparms == NULL) 
  30.    {
  31.       error("error in PEM_read_DHparams(): %s\n\n", strerror(errno));
  32.  
  33.       ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  34.       if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  35.  
  36.       if (parmfd != NULL)(void)fclose(parmfd);
  37.       quit(ERROR);
  38.    }
  39.  
  40. }
  41.  
  42.  
  43. /* ------------------------------- */
  44.  
  45.  
  46. /* initialize the SSL connection */
  47. void makeSSLconn(int fd)
  48. {
  49.    int res;
  50.    static int init = 0;
  51.    int flags = SSL_VERIFY_PEER; /* | SSL_VERIFY_FAIL_IF_NO_PEER_CERT; */
  52.  
  53.    /* FIX - no hardcode */
  54.    char *allcert = PARAMDIR "/all.cert.pem";
  55.    char    *serverpem = PARAMDIR "/server.pem";
  56.  
  57.    if (init == 0) 
  58.    {
  59.       init = 1;
  60.       SSL_load_error_strings();
  61.       SSLeay_add_all_algorithms(), SSLeay_add_ssl_algorithms();
  62.    }
  63.  
  64.    /* clean up after any previous connections */
  65.    if (clients[curClient].sslconn != NULL) 
  66.       SSL_free(clients[curClient].sslconn);
  67.  
  68.    if (clients[curClient].sslctx != NULL) 
  69.       SSL_CTX_free(clients[curClient].sslctx);
  70.  
  71.    clients[curClient].sslconn = NULL;
  72.    clients[curClient].sslctx = NULL;
  73.  
  74.    /* create a context */
  75.    clients[curClient].sslctx = SSL_CTX_new(SSLv3_server_method());
  76.  
  77.    if (clients[curClient].sslctx == NULL) 
  78.    {
  79.       error("error in SSL_CTX_new(): %s\n\n", strerror(errno));
  80.  
  81.       ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  82.       if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  83.  
  84.       quit(ERROR);
  85.    }
  86.  
  87.    /* don't set any options since they always have to do with bugs */
  88.    SSL_CTX_set_options(clients[curClient].sslctx, 0);
  89.  
  90.    /* indicate where we should load the CA's public key */
  91.    res = SSL_CTX_load_verify_locations(clients[curClient].sslctx, 
  92.                                        serverpem, PARAMDIR);
  93.  
  94.    if (res <= 0) 
  95.    {
  96.       error("error in SSL_CTX_load_verify_locations(): %s\n\n",
  97.             strerror(errno));
  98.  
  99.       ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  100.       if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  101.  
  102.       quit(ERROR);
  103.    }
  104.  
  105.    res = SSL_CTX_set_default_verify_paths(clients[curClient].sslctx);
  106.    if (res <= 0) 
  107.    {
  108.       error("error in SSL_CTX_set_default_verify_paths(): %s\n\n",
  109.             strerror(errno));
  110.  
  111.       ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  112.       if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  113.  
  114.       quit(ERROR);
  115.    }
  116.  
  117.    /* setup DH parameters */
  118.    loadDHparams();
  119.  
  120.    SSL_CTX_set_tmp_dh(clients[curClient].sslctx, dhparms);
  121.    DH_free(dhparms);
  122.  
  123.    /* indicate where our certificate is */
  124.    res = SSL_CTX_use_certificate_file(clients[curClient].sslctx, 
  125.         PARAMDIR "/server.pem", SSL_FILETYPE_PEM);
  126.  
  127.    if (res <= 0) 
  128.    {
  129.       error("error in SSL_CTX_use_certificate_file(): %s\n\n",
  130.             strerror(errno));
  131.  
  132.       ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  133.       if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  134.  
  135.       quit(ERROR);
  136.    }
  137.  
  138.    res = SSL_CTX_set_cipher_list(clients[curClient].sslctx, "ADH:+DES:+EXP");
  139.  
  140.    if (res <= 0) 
  141.    {
  142.       error("error in SSL_CTX_set_cipher_list(): %s\n\n",
  143.             strerror(errno));
  144.  
  145.       ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  146.       if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  147.  
  148.       quit(ERROR);
  149.    }
  150.  
  151.    /* FIX - do I need SSL_VERIFY_CLIENT_ONCE? */
  152.    /* FIX - mark doesn't use the verify fail.. */
  153.  
  154.    /* we want the client verified */
  155.    SSL_CTX_set_verify(clients[curClient].sslctx, flags, NULL);
  156.  
  157.    /* indicate where the clients' certificates can be found */
  158.    SSL_CTX_set_client_CA_list(clients[curClient].sslctx, 
  159.                               SSL_load_client_CA_file(allcert));
  160.  
  161.    /* create a connection */
  162.    clients[curClient].sslconn = SSL_new(clients[curClient].sslctx);
  163.    if (clients[curClient].sslconn == NULL) 
  164.    {
  165.       error("error in SSL_new(): %s\n\n", strerror(errno));
  166.  
  167.       ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  168.       if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  169.  
  170.       quit(ERROR);
  171.    }
  172.  
  173.    /* setup to perform a connection next time we send/recv data */
  174.    SSL_set_accept_state(clients[curClient].sslconn);
  175.  
  176.    /* information the connection about the socket */
  177.    res = SSL_set_fd(clients[curClient].sslconn, fd);
  178.    if (res <= 0) 
  179.    {
  180.       error("error in SSL_set_fd(): %s\n\n", strerror(errno));
  181.  
  182.       ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  183.       if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  184.  
  185.       quit(ERROR);
  186.    }
  187. }
  188.  
  189. #endif /* NOSSL */
  190.