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(©);
89 0 : continue;
90 : }
91 0 : free_w(©);
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 : /* Store setting, compared later to decide whether to rewrite the config */
171 0 : char *ssl_peer_cn_old=strdup_w(get_string(confs[OPT_SSL_PEER_CN]), __func__);
172 0 : if(!ssl_peer_cn_old) goto end;
173 :
174 : // Do not continue if we have one of the following things not set.
175 0 : if( !ca_burp_ca
176 0 : || !ca_csr_dir
177 0 : || !ssl_cert_ca
178 0 : || !ssl_cert
179 0 : || !ssl_key
180 : // Do not try to get a new certificate if we already have a key.
181 0 : || !lstat(ssl_key, &statp))
182 : {
183 0 : if(asfd->write_str(asfd, CMD_GEN, "nocsr")
184 0 : || asfd_read_expect(asfd, CMD_GEN, "nocsr ok"))
185 : {
186 0 : logp("problem reading from server nocsr\n");
187 0 : goto end;
188 : }
189 0 : logp("nocsr ok\n");
190 0 : ret=0;
191 0 : goto end;
192 : }
193 :
194 : // Tell the server we want to do a signing request and store the servers name in ssl_peer_cn.
195 0 : if(asfd->write_str(asfd, CMD_GEN, "csr")
196 0 : || asfd->simple_loop(asfd, confs, NULL, __func__, csr_client_func))
197 : goto end;
198 :
199 0 : logp("Server will sign a certificate request\n");
200 :
201 : // First need to generate a client key and a certificate signing
202 : // request.
203 0 : snprintf(csr_path, sizeof(csr_path), "%s/%s.csr", ca_csr_dir, cname);
204 0 : if(generate_key_and_csr(asfd, confs, csr_path)) goto end_cleanup;
205 :
206 : // Then copy the csr to the server.
207 0 : if(send_a_file(asfd, csr_path, cntr)) goto end_cleanup;
208 :
209 0 : snprintf(ssl_cert_tmp, sizeof(ssl_cert_tmp), "%s.%d",
210 : ssl_cert, getpid());
211 0 : snprintf(ssl_cert_ca_tmp, sizeof(ssl_cert_ca_tmp), "%s.%d",
212 : ssl_cert_ca, getpid());
213 :
214 : // The server will then sign it, and give it back.
215 0 : if(receive_a_file(asfd, ssl_cert_tmp, cntr)) goto end_cleanup;
216 :
217 : // The server will also send the CA certificate.
218 0 : if(receive_a_file(asfd, ssl_cert_ca_tmp, cntr)) goto end_cleanup;
219 :
220 : // Possible race condition - the rename can delete the destination
221 : // and then fail. Worse case, the user has to rename them by hand.
222 0 : if(do_rename(ssl_cert_tmp, ssl_cert)
223 0 : || do_rename(ssl_cert_ca_tmp, ssl_cert_ca))
224 : goto end_cleanup;
225 :
226 : // Need to rewrite our configuration file to contain the server
227 : // name (ssl_peer_cn) if the name differs from the config file.
228 0 : if(strncmp_w(ssl_peer_cn_old, get_string(confs[OPT_SSL_PEER_CN])))
229 : {
230 0 : if(rewrite_client_conf(confs)) goto end_cleanup;
231 : }
232 :
233 : // My goodness, everything seems to have gone OK. Stand back!
234 : ret=1;
235 : end_cleanup:
236 0 : if(ret<0)
237 : {
238 : // On error, remove any possibly newly created files, so that
239 : // this function might run again on another go.
240 0 : unlink(csr_path);
241 0 : unlink(ssl_key);
242 0 : unlink(ssl_cert);
243 0 : unlink(ssl_cert_ca);
244 0 : unlink(ssl_cert_tmp);
245 0 : unlink(ssl_cert_ca_tmp);
246 : }
247 : end:
248 0 : if(ssl_peer_cn_old) free_w(&ssl_peer_cn_old);
249 0 : return ret;
250 : }
|