LCOV - code coverage report
Current view: top level - src/client - ca.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 0 120 0.0 %
Date: 2018-05-06 01:36:20 Functions: 0 4 0.0 %

          Line data    Source code
       1             : #include "../burp.h"
       2             : #include "../asfd.h"
       3             : #include "../async.h"
       4             : #include "../cmd.h"
       5             : #include "../conf.h"
       6             : #include "../conffile.h"
       7             : #include "../fsops.h"
       8             : #include "../handy.h"
       9             : #include "../iobuf.h"
      10             : #include "../log.h"
      11             : #include "../run_script.h"
      12             : #include "cvss.h"
      13             : #include "ca.h"
      14             : 
      15           0 : static int generate_key_and_csr(struct asfd *asfd,
      16             :         struct conf **confs, const char *csr_path)
      17             : {
      18           0 :         int a=0;
      19             :         const char *args[12];
      20           0 :         const char *ca_burp_ca=get_string(confs[OPT_CA_BURP_CA]);
      21           0 :         const char *cname=get_string(confs[OPT_CNAME]);
      22           0 :         const char *ssl_key=get_string(confs[OPT_SSL_KEY]);
      23             : 
      24           0 :         logp("Generating SSL key and certificate signing request\n");
      25           0 :         logp("Running '%s --key --keypath %s --request --requestpath %s --name %s'\n", ca_burp_ca, ssl_key, csr_path, cname);
      26             : #ifdef HAVE_WIN32
      27             :         win32_enable_backup_privileges();
      28             : #else
      29             :         // FIX THIS
      30           0 :         signal(SIGPIPE, SIG_IGN);
      31             : #endif
      32           0 :         args[a++]=ca_burp_ca;
      33           0 :         args[a++]="--key";
      34           0 :         args[a++]="--keypath";
      35           0 :         args[a++]=ssl_key;
      36           0 :         args[a++]="--request";
      37           0 :         args[a++]="--requestpath";
      38           0 :         args[a++]=csr_path;
      39           0 :         args[a++]="--name";
      40           0 :         args[a++]=cname;
      41           0 :         args[a++]=NULL;
      42           0 :         if(run_script(asfd, args, NULL, confs, 1 /* wait */,
      43             :                 0, 0 /* do not use logp - stupid openssl prints lots of dots
      44             :                         one at a time with no way to turn it off */))
      45             :         {
      46           0 :                 logp("error when running '%s --key --keypath %s --request --requestpath %s --name %s'\n",
      47             :                         ca_burp_ca, ssl_key, csr_path, cname);
      48           0 :                 return -1;
      49             :         }
      50             : 
      51             :         return 0;
      52             : }
      53             : 
      54             : /* Rewrite the conf file with the ssl_peer_cn value changed to what the
      55             :    server told us it should be. */
      56           0 : static int rewrite_client_conf(struct conf **confs)
      57             : {
      58           0 :         int ret=-1;
      59           0 :         char p[32]="";
      60           0 :         struct fzp *dp=NULL;
      61           0 :         struct fzp *sp=NULL;
      62           0 :         char *tmp=NULL;
      63           0 :         char buf[4096]="";
      64           0 :         const char *conffile=get_string(confs[OPT_CONFFILE]);
      65           0 :         const char *ssl_peer_cn=get_string(confs[OPT_SSL_PEER_CN]);
      66             : 
      67           0 :         snprintf(p, sizeof(p), ".%d", getpid());
      68           0 :         if(!(tmp=prepend(conffile, p)))
      69             :                 goto end;
      70           0 :         if(!(sp=fzp_open(conffile, "rb"))
      71           0 :           || !(dp=fzp_open(tmp, "wb")))
      72             :                 goto end;
      73             : 
      74           0 :         while(fzp_gets(sp, buf, sizeof(buf)))
      75             :         {
      76           0 :                 char *copy=NULL;
      77           0 :                 char *field=NULL;
      78           0 :                 char *value=NULL;
      79           0 :                 int r=0;
      80             : 
      81           0 :                 if(!(copy=strdup_w(buf, __func__)))
      82             :                         goto end;
      83           0 :                 if(conf_get_pair(buf, &field, &value, &r)
      84           0 :                   || !field || !value
      85           0 :                   || strcmp(field, "ssl_peer_cn"))
      86             :                 {
      87           0 :                         fzp_printf(dp, "%s", copy);
      88           0 :                         free_w(&copy);
      89           0 :                         continue;
      90             :                 }
      91           0 :                 free_w(&copy);
      92             : #ifdef HAVE_WIN32
      93             :                 fzp_printf(dp, "ssl_peer_cn = %s\r\n", ssl_peer_cn);
      94             : #else
      95           0 :                 fzp_printf(dp, "ssl_peer_cn = %s\n", ssl_peer_cn);
      96             : #endif
      97             :         }
      98           0 :         fzp_close(&sp);
      99           0 :         if(fzp_close(&dp))
     100             :         {
     101           0 :                 logp("error closing %s in %s\n", tmp, __func__);
     102           0 :                 goto end;
     103             :         }
     104             : 
     105           0 :         if(files_equal(conffile, tmp, 0/*compressed*/))
     106             :         {
     107             :                 // No need to overwrite if there were no differences.
     108           0 :                 ret=0;
     109           0 :                 unlink(tmp);
     110           0 :                 goto end;
     111             :         }
     112             : 
     113           0 :         logp("Rewriting conf file: %s\n", conffile);
     114             : 
     115             :         // Nasty race conditions going on here. However, the new config
     116             :         // file will get left behind, so at worse you will have to move
     117             :         // the new file into the correct place by hand. Or delete everything
     118             :         // and start again.
     119             : #ifdef HAVE_WIN32
     120             :         // Need to delete the destination, or Windows gets upset.
     121             :         unlink(conffile);
     122             : #endif
     123           0 :         if(do_rename(tmp, conffile)) goto end;
     124             : 
     125           0 :         ret=0;
     126             : end:
     127           0 :         fzp_close(&sp);
     128           0 :         fzp_close(&dp);
     129           0 :         if(ret)
     130             :         {
     131           0 :                 logp("%s with %s failed\n", __func__, conffile);
     132           0 :                 unlink(tmp);
     133             :         }
     134           0 :         free_w(&tmp);
     135           0 :         return ret;
     136             : }
     137             : 
     138           0 : static enum asl_ret csr_client_func(struct asfd *asfd,
     139             :         struct conf **confs, __attribute__((unused)) void *param)
     140             : {
     141           0 :         if(strncmp_w(asfd->rbuf->buf, "csr ok:"))
     142             :         {
     143           0 :                 iobuf_log_unexpected(asfd->rbuf, __func__);
     144           0 :                 return ASL_END_ERROR;
     145             :         }
     146             :         // The server appends its name after 'csr ok:'
     147           0 :         if(set_string(confs[OPT_SSL_PEER_CN], 
     148           0 :                 asfd->rbuf->buf+strlen("csr ok:")))
     149             :                         return ASL_END_ERROR;
     150           0 :         return ASL_END_OK;
     151             : }
     152             : 
     153             : /* Return 1 for everything OK, signed and returned, -1 for error, 0 for
     154             :    nothing done. */
     155           0 : int ca_client_setup(struct asfd *asfd, struct conf **confs)
     156             : {
     157           0 :         int ret=-1;
     158             :         struct stat statp;
     159           0 :         char csr_path[256]="";
     160           0 :         char ssl_cert_tmp[512]="";
     161           0 :         char ssl_cert_ca_tmp[512]="";
     162           0 :         const char *ca_burp_ca=get_string(confs[OPT_CA_BURP_CA]);
     163           0 :         const char *ca_csr_dir=get_string(confs[OPT_CA_CSR_DIR]);
     164           0 :         const char *cname=get_string(confs[OPT_CNAME]);
     165           0 :         const char *ssl_key=get_string(confs[OPT_SSL_KEY]);
     166           0 :         const char *ssl_cert=get_string(confs[OPT_SSL_CERT]);
     167           0 :         const char *ssl_cert_ca=get_string(confs[OPT_SSL_CERT_CA]);
     168           0 :         struct cntr *cntr=get_cntr(confs);
     169             : 
     170             :         // Do not continue if we have one of the following things not set.
     171           0 :         if(  !ca_burp_ca
     172           0 :           || !ca_csr_dir
     173           0 :           || !ssl_cert_ca
     174           0 :           || !ssl_cert
     175           0 :           || !ssl_key
     176             :         // Do not try to get a new certificate if we already have a key.
     177           0 :           || !lstat(ssl_key, &statp))
     178             :         {
     179           0 :                 if(asfd->write_str(asfd, CMD_GEN, "nocsr")
     180           0 :                   || asfd_read_expect(asfd, CMD_GEN, "nocsr ok"))
     181             :                 {
     182           0 :                         logp("problem reading from server nocsr\n");
     183           0 :                         goto end;
     184             :                 }
     185           0 :                 logp("nocsr ok\n");
     186           0 :                 ret=0;
     187           0 :                 goto end;
     188             :         }
     189             : 
     190             :         // Tell the server we want to do a signing request.
     191           0 :         if(asfd->write_str(asfd, CMD_GEN, "csr")
     192           0 :           || asfd->simple_loop(asfd, confs, NULL, __func__, csr_client_func))
     193             :                 goto end;
     194             : 
     195           0 :         logp("Server will sign a certificate request\n");
     196             : 
     197             :         // First need to generate a client key and a certificate signing
     198             :         // request.
     199           0 :         snprintf(csr_path, sizeof(csr_path), "%s/%s.csr", ca_csr_dir, cname);
     200           0 :         if(generate_key_and_csr(asfd, confs, csr_path)) goto end_cleanup;
     201             : 
     202             :         // Then copy the csr to the server.
     203           0 :         if(send_a_file(asfd, csr_path, cntr)) goto end_cleanup;
     204             : 
     205           0 :         snprintf(ssl_cert_tmp, sizeof(ssl_cert_tmp), "%s.%d",
     206             :                 ssl_cert, getpid());
     207           0 :         snprintf(ssl_cert_ca_tmp, sizeof(ssl_cert_ca_tmp), "%s.%d",
     208             :                 ssl_cert_ca, getpid());
     209             : 
     210             :         // The server will then sign it, and give it back.
     211           0 :         if(receive_a_file(asfd, ssl_cert_tmp, cntr)) goto end_cleanup;
     212             : 
     213             :         // The server will also send the CA certificate.
     214           0 :         if(receive_a_file(asfd, ssl_cert_ca_tmp, cntr)) goto end_cleanup;
     215             : 
     216             :         // Possible race condition - the rename can delete the destination
     217             :         // and then fail. Worse case, the user has to rename them by hand.
     218           0 :         if(do_rename(ssl_cert_tmp, ssl_cert)
     219           0 :           || do_rename(ssl_cert_ca_tmp, ssl_cert_ca))
     220             :                 goto end_cleanup;
     221             : 
     222             :         // Need to rewrite our configuration file to contain the server
     223             :         // name (ssl_peer_cn)
     224           0 :         if(rewrite_client_conf(confs)) goto end_cleanup;
     225             : 
     226             :         // My goodness, everything seems to have gone OK. Stand back!
     227           0 :         ret=1;
     228             : end_cleanup:
     229           0 :         if(ret<0)
     230             :         {
     231             :                 // On error, remove any possibly newly created files, so that
     232             :                 // this function might run again on another go.
     233           0 :                 unlink(csr_path);
     234           0 :                 unlink(ssl_key);
     235           0 :                 unlink(ssl_cert);
     236           0 :                 unlink(ssl_cert_ca);
     237           0 :                 unlink(ssl_cert_tmp);
     238           0 :                 unlink(ssl_cert_ca_tmp);
     239             :         }
     240             : end:
     241           0 :         return ret;
     242             : }

Generated by: LCOV version 1.13