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 : logp("Rewriting conf file: %s\n", conffile);
68 0 : snprintf(p, sizeof(p), ".%d", getpid());
69 0 : if(!(tmp=prepend(conffile, p)))
70 : goto end;
71 0 : if(!(sp=fzp_open(conffile, "rb"))
72 0 : || !(dp=fzp_open(tmp, "wb")))
73 : goto end;
74 :
75 0 : while(fzp_gets(sp, buf, sizeof(buf)))
76 : {
77 0 : char *copy=NULL;
78 0 : char *field=NULL;
79 0 : char *value=NULL;
80 0 : int r=0;
81 :
82 0 : if(!(copy=strdup_w(buf, __func__)))
83 : goto end;
84 0 : if(conf_get_pair(buf, &field, &value, &r)
85 0 : || !field || !value
86 0 : || strcmp(field, "ssl_peer_cn"))
87 : {
88 0 : fzp_printf(dp, "%s", copy);
89 0 : free_w(©);
90 0 : continue;
91 : }
92 0 : free_w(©);
93 : #ifdef HAVE_WIN32
94 : fzp_printf(dp, "ssl_peer_cn = %s\r\n", ssl_peer_cn);
95 : #else
96 0 : fzp_printf(dp, "ssl_peer_cn = %s\n", ssl_peer_cn);
97 : #endif
98 : }
99 0 : fzp_close(&sp);
100 0 : if(fzp_close(&dp))
101 : {
102 0 : logp("error closing %s in %s\n", tmp, __func__);
103 0 : goto end;
104 : }
105 : // Nasty race conditions going on here. However, the new config
106 : // file will get left behind, so at worse you will have to move
107 : // the new file into the correct place by hand. Or delete everything
108 : // and start again.
109 : #ifdef HAVE_WIN32
110 : // Need to delete the destination, or Windows gets upset.
111 : unlink(conffile);
112 : #endif
113 0 : if(do_rename(tmp, conffile)) goto end;
114 :
115 0 : ret=0;
116 : end:
117 0 : fzp_close(&sp);
118 0 : fzp_close(&dp);
119 0 : if(ret)
120 : {
121 0 : logp("Rewrite failed\n");
122 0 : unlink(tmp);
123 : }
124 0 : free_w(&tmp);
125 0 : return ret;
126 : }
127 :
128 0 : static enum asl_ret csr_client_func(struct asfd *asfd,
129 : struct conf **confs, __attribute__((unused)) void *param)
130 : {
131 0 : if(strncmp_w(asfd->rbuf->buf, "csr ok:"))
132 : {
133 0 : iobuf_log_unexpected(asfd->rbuf, __func__);
134 0 : return ASL_END_ERROR;
135 : }
136 : // The server appends its name after 'csr ok:'
137 0 : if(set_string(confs[OPT_SSL_PEER_CN],
138 0 : asfd->rbuf->buf+strlen("csr ok:")))
139 : return ASL_END_ERROR;
140 0 : return ASL_END_OK;
141 : }
142 :
143 : /* Return 1 for everything OK, signed and returned, -1 for error, 0 for
144 : nothing done. */
145 0 : int ca_client_setup(struct asfd *asfd, struct conf **confs)
146 : {
147 0 : int ret=-1;
148 : struct stat statp;
149 0 : char csr_path[256]="";
150 0 : char ssl_cert_tmp[512]="";
151 0 : char ssl_cert_ca_tmp[512]="";
152 0 : const char *ca_burp_ca=get_string(confs[OPT_CA_BURP_CA]);
153 0 : const char *ca_csr_dir=get_string(confs[OPT_CA_CSR_DIR]);
154 0 : const char *cname=get_string(confs[OPT_CNAME]);
155 0 : const char *ssl_key=get_string(confs[OPT_SSL_KEY]);
156 0 : const char *ssl_cert=get_string(confs[OPT_SSL_CERT]);
157 0 : const char *ssl_cert_ca=get_string(confs[OPT_SSL_CERT_CA]);
158 0 : struct cntr *cntr=get_cntr(confs);
159 :
160 : // Do not continue if we have one of the following things not set.
161 0 : if( !ca_burp_ca
162 0 : || !ca_csr_dir
163 0 : || !ssl_cert_ca
164 0 : || !ssl_cert
165 0 : || !ssl_key
166 : // Do not try to get a new certificate if we already have a key.
167 0 : || !lstat(ssl_key, &statp))
168 : {
169 0 : if(asfd->write_str(asfd, CMD_GEN, "nocsr")
170 0 : || asfd_read_expect(asfd, CMD_GEN, "nocsr ok"))
171 : {
172 0 : logp("problem reading from server nocsr\n");
173 0 : goto end;
174 : }
175 0 : logp("nocsr ok\n");
176 0 : ret=0;
177 0 : goto end;
178 : }
179 :
180 : // Tell the server we want to do a signing request.
181 0 : if(asfd->write_str(asfd, CMD_GEN, "csr")
182 0 : || asfd->simple_loop(asfd, confs, NULL, __func__, csr_client_func))
183 : goto end;
184 :
185 0 : logp("Server will sign a certificate request\n");
186 :
187 : // First need to generate a client key and a certificate signing
188 : // request.
189 : snprintf(csr_path, sizeof(csr_path), "%s/%s.csr", ca_csr_dir, cname);
190 0 : if(generate_key_and_csr(asfd, confs, csr_path)) goto end_cleanup;
191 :
192 : // Then copy the csr to the server.
193 0 : if(send_a_file(asfd, csr_path, cntr)) goto end_cleanup;
194 :
195 0 : snprintf(ssl_cert_tmp, sizeof(ssl_cert_tmp), "%s.%d",
196 : ssl_cert, getpid());
197 0 : snprintf(ssl_cert_ca_tmp, sizeof(ssl_cert_ca_tmp), "%s.%d",
198 : ssl_cert_ca, getpid());
199 :
200 : // The server will then sign it, and give it back.
201 0 : if(receive_a_file(asfd, ssl_cert_tmp, cntr)) goto end_cleanup;
202 :
203 : // The server will also send the CA certificate.
204 0 : if(receive_a_file(asfd, ssl_cert_ca_tmp, cntr)) goto end_cleanup;
205 :
206 : // Possible race condition - the rename can delete the destination
207 : // and then fail. Worse case, the user has to rename them by hand.
208 0 : if(do_rename(ssl_cert_tmp, ssl_cert)
209 0 : || do_rename(ssl_cert_ca_tmp, ssl_cert_ca))
210 : goto end_cleanup;
211 :
212 : // Need to rewrite our configuration file to contain the server
213 : // name (ssl_peer_cn)
214 0 : if(rewrite_client_conf(confs)) goto end_cleanup;
215 :
216 : // My goodness, everything seems to have gone OK. Stand back!
217 0 : ret=1;
218 : end_cleanup:
219 0 : if(ret<0)
220 : {
221 : // On error, remove any possibly newly created files, so that
222 : // this function might run again on another go.
223 0 : unlink(csr_path);
224 0 : unlink(ssl_key);
225 0 : unlink(ssl_cert);
226 0 : unlink(ssl_cert_ca);
227 0 : unlink(ssl_cert_tmp);
228 0 : unlink(ssl_cert_ca_tmp);
229 : }
230 : end:
231 0 : return ret;
232 : }
|