]>
Commit | Line | Data |
---|---|---|
5294dd70 | 1 | /* -*- Mode: C; c-file-style: "bsd" -*- */ |
28470b60 | 2 | /*- |
5294dd70 | 3 | * easy-tls.c -- generic TLS proxy. |
93683c3c | 4 | * $Id: easy-tls.c,v 1.4 2002/03/05 09:07:16 bodo Exp $ |
5294dd70 | 5 | */ |
1d97c843 | 6 | /*- |
5294dd70 BM |
7 | (c) Copyright 1999 Bodo Moeller. All rights reserved. |
8 | ||
9 | This is free software; you can redistributed and/or modify it | |
10 | unter the terms of either | |
11 | - the GNU General Public License as published by the | |
12 | Free Software Foundation, version 1, or (at your option) | |
13 | any later version, | |
14 | or | |
15 | - the following license: | |
16 | */ | |
1d97c843 | 17 | /*- |
5294dd70 BM |
18 | * Redistribution and use in source and binary forms, with or without |
19 | * modification, are permitted provided that each of the following | |
20 | * conditions is met: | |
21 | * | |
22 | * 1. Redistributions qualify as "freeware" or "Open Source Software" under | |
23 | * one of the following terms: | |
0f113f3e | 24 | * |
5294dd70 BM |
25 | * (a) Redistributions are made at no charge beyond the reasonable cost of |
26 | * materials and delivery. | |
0f113f3e | 27 | * |
5294dd70 BM |
28 | * (b) Redistributions are accompanied by a copy of the Source Code |
29 | * or by an irrevocable offer to provide a copy of the Source Code | |
30 | * for up to three years at the cost of materials and delivery. | |
31 | * Such redistributions must allow further use, modification, and | |
32 | * redistribution of the Source Code under substantially the same | |
33 | * terms as this license. | |
34 | * | |
35 | * 2. Redistributions of source code must retain the above copyright | |
0f113f3e | 36 | * notice, this list of conditions and the following disclaimer. |
5294dd70 BM |
37 | * |
38 | * 3. Redistributions in binary form must reproduce the above copyright | |
39 | * notice, this list of conditions and the following disclaimer in | |
40 | * the documentation and/or other materials provided with the | |
41 | * distribution. | |
42 | * | |
43 | * 4. All advertising materials mentioning features or use of this | |
44 | * software must display the following acknowledgment: | |
45 | * "This product includes software developed by Bodo Moeller." | |
46 | * (If available, substitute umlauted o for oe.) | |
47 | * | |
48 | * 5. Redistributions of any form whatsoever must retain the following | |
49 | * acknowledgment: | |
50 | * "This product includes software developed by Bodo Moeller." | |
51 | * | |
52 | * THIS SOFTWARE IS PROVIDED BY BODO MOELLER ``AS IS'' AND ANY | |
53 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
54 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
55 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BODO MOELLER OR | |
56 | * HIS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
57 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
58 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
59 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
60 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
61 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
62 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
63 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
64 | */ | |
28470b60 | 65 | /*- |
5294dd70 BM |
66 | * Attribution for OpenSSL library: |
67 | * | |
68 | * This product includes cryptographic software written by Eric Young | |
69 | * (eay@cryptsoft.com). This product includes software written by Tim | |
70 | * Hudson (tjh@cryptsoft.com). | |
71 | * This product includes software developed by the OpenSSL Project | |
72 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/) | |
73 | */ | |
74 | ||
75 | static char const rcsid[] = | |
0f113f3e | 76 | "$Id: easy-tls.c,v 1.4 2002/03/05 09:07:16 bodo Exp $"; |
5294dd70 BM |
77 | |
78 | #include <assert.h> | |
79 | #include <errno.h> | |
80 | #include <fcntl.h> | |
81 | #include <limits.h> | |
82 | #include <stdarg.h> | |
83 | #include <stdio.h> | |
84 | #include <string.h> | |
85 | #include <sys/select.h> | |
86 | #include <sys/socket.h> | |
87 | #include <sys/stat.h> | |
88 | #include <sys/time.h> | |
89 | #include <sys/types.h> | |
90 | #include <sys/utsname.h> | |
91 | #include <unistd.h> | |
92 | ||
93 | #include <openssl/crypto.h> | |
94 | #include <openssl/dh.h> | |
95 | #include <openssl/dsa.h> | |
96 | #include <openssl/err.h> | |
97 | #include <openssl/evp.h> | |
98 | #include <openssl/opensslv.h> | |
99 | #include <openssl/pem.h> | |
100 | #include <openssl/rand.h> | |
101 | #ifndef NO_RSA | |
0f113f3e | 102 | # include <openssl/rsa.h> |
5294dd70 BM |
103 | #endif |
104 | #include <openssl/ssl.h> | |
105 | #include <openssl/x509.h> | |
106 | #include <openssl/x509_vfy.h> | |
107 | ||
108 | #if OPENSSL_VERSION_NUMBER < 0x00904000L /* 0.9.4-dev */ | |
109 | # error "This program needs OpenSSL 0.9.4 or later." | |
110 | #endif | |
111 | ||
0f113f3e MC |
112 | #include "easy-tls.h" /* include after <openssl/ssl.h> if both are |
113 | * needed */ | |
5294dd70 BM |
114 | |
115 | #if TLS_INFO_SIZE > PIPE_BUF | |
116 | # if PIPE_BUF < 512 | |
0f113f3e | 117 | # error "PIPE_BUF < 512" /* non-POSIX */ |
5294dd70 BM |
118 | # endif |
119 | # error "TLS_INFO_SIZE > PIPE_BUF" | |
120 | #endif | |
121 | ||
122 | /*****************************************************************************/ | |
123 | ||
124 | #ifdef TLS_APP | |
125 | # include TLS_APP | |
126 | #endif | |
127 | ||
c80fd6b2 MC |
128 | /*- |
129 | * Applications can define: | |
5294dd70 | 130 | * TLS_APP_PROCESS_INIT -- void ...(int fd, int client_p, void *apparg) |
0f113f3e | 131 | * TLS_CUMULATE_ERRORS |
5294dd70 BM |
132 | * TLS_ERROR_BUFSIZ |
133 | * TLS_APP_ERRFLUSH -- void ...(int child_p, char *, size_t, void *apparg) | |
134 | */ | |
135 | ||
136 | #ifndef TLS_APP_PROCESS_INIT | |
137 | # define TLS_APP_PROCESS_INIT(fd, client_p, apparg) ((void) 0) | |
138 | #endif | |
139 | ||
140 | #ifndef TLS_ERROR_BUFSIZ | |
141 | # define TLS_ERROR_BUFSIZ (10*160) | |
142 | #endif | |
0f113f3e | 143 | #if TLS_ERROR_BUFSIZ < 2 /* {'\n',0} */ |
5294dd70 BM |
144 | # error "TLS_ERROR_BUFSIZE is too small." |
145 | #endif | |
146 | ||
147 | #ifndef TLS_APP_ERRFLUSH | |
148 | # define TLS_APP_ERRFLUSH tls_app_errflush | |
149 | static void | |
150 | tls_app_errflush(int child_p, char *errbuf, size_t num, void *apparg) | |
151 | { | |
152 | fputs(errbuf, stderr); | |
153 | } | |
154 | #endif | |
155 | ||
156 | /*****************************************************************************/ | |
157 | ||
158 | #ifdef DEBUG_TLS | |
159 | # define DEBUG_MSG(x) fprintf(stderr," %s\n",x) | |
160 | # define DEBUG_MSG2(x,y) fprintf(stderr, " %s: %d\n",x,y) | |
161 | static int tls_loop_count = 0; | |
162 | static int tls_select_count = 0; | |
163 | #else | |
164 | # define DEBUG_MSG(x) (void)0 | |
165 | # define DEBUG_MSG2(x,y) (void)0 | |
166 | #endif | |
167 | ||
168 | static void tls_rand_seed_uniquely(void); | |
0f113f3e MC |
169 | static void tls_proxy(int clear_fd, int tls_fd, int info_fd, SSL_CTX *ctx, |
170 | int client_p); | |
5294dd70 BM |
171 | static int tls_socket_nonblocking(int fd); |
172 | ||
173 | static int tls_child_p = 0; | |
174 | static void *tls_child_apparg; | |
175 | ||
0f113f3e | 176 | struct tls_start_proxy_args tls_start_proxy_defaultargs(void) |
5294dd70 BM |
177 | { |
178 | struct tls_start_proxy_args ret; | |
179 | ||
180 | ret.fd = -1; | |
181 | ret.client_p = -1; | |
182 | ret.ctx = NULL; | |
183 | ret.pid = NULL; | |
184 | ret.infofd = NULL; | |
0f113f3e | 185 | |
5294dd70 BM |
186 | return ret; |
187 | } | |
188 | ||
0f113f3e | 189 | /*- |
1d97c843 | 190 | * Slice in TLS proxy process at fd. |
5294dd70 BM |
191 | * Return value: |
192 | * 0 ok (*pid is set to child's PID if pid != NULL), | |
193 | * < 0 look at errno | |
194 | * > 0 other error | |
195 | * (return value encodes place of error) | |
196 | * | |
197 | */ | |
0f113f3e | 198 | int tls_start_proxy(struct tls_start_proxy_args a, void *apparg) |
5294dd70 | 199 | { |
0f113f3e MC |
200 | int fds[2] = { -1, -1 }; |
201 | int infofds[2] = { -1, -1 }; | |
5294dd70 BM |
202 | int r, getfd, getfl; |
203 | int ret; | |
204 | ||
205 | DEBUG_MSG2("tls_start_proxy fd", a.fd); | |
206 | DEBUG_MSG2("tls_start_proxy client_p", a.client_p); | |
207 | ||
208 | if (a.fd == -1 || a.client_p == -1 || a.ctx == NULL) | |
0f113f3e | 209 | return 1; |
5294dd70 BM |
210 | |
211 | if (a.pid != NULL) { | |
0f113f3e | 212 | *a.pid = 0; |
5294dd70 BM |
213 | } |
214 | if (a.infofd != NULL) { | |
0f113f3e | 215 | *a.infofd = -1; |
5294dd70 BM |
216 | } |
217 | ||
218 | r = socketpair(AF_UNIX, SOCK_STREAM, 0, fds); | |
219 | if (r == -1) | |
0f113f3e | 220 | return -1; |
5294dd70 | 221 | if (a.fd >= FD_SETSIZE || fds[0] >= FD_SETSIZE) { |
0f113f3e MC |
222 | ret = 2; |
223 | goto err; | |
5294dd70 BM |
224 | } |
225 | if (a.infofd != NULL) { | |
0f113f3e MC |
226 | r = pipe(infofds); |
227 | if (r == -1) { | |
228 | ret = -3; | |
229 | goto err; | |
230 | } | |
5294dd70 BM |
231 | } |
232 | ||
233 | r = fork(); | |
234 | if (r == -1) { | |
0f113f3e MC |
235 | ret = -4; |
236 | goto err; | |
5294dd70 BM |
237 | } |
238 | if (r == 0) { | |
0f113f3e MC |
239 | DEBUG_MSG("fork"); |
240 | tls_child_p = 1; | |
241 | tls_child_apparg = apparg; | |
242 | close(fds[1]); | |
243 | if (infofds[0] != -1) | |
244 | close(infofds[0]); | |
245 | TLS_APP_PROCESS_INIT(a.fd, a.client_p, apparg); | |
246 | DEBUG_MSG("TLS_APP_PROCESS_INIT"); | |
247 | tls_proxy(fds[0], a.fd, infofds[1], a.ctx, a.client_p); | |
248 | exit(0); | |
5294dd70 BM |
249 | } |
250 | if (a.pid != NULL) | |
0f113f3e | 251 | *a.pid = r; |
5294dd70 | 252 | if (infofds[1] != -1) { |
0f113f3e MC |
253 | close(infofds[1]); |
254 | infofds[1] = -1; | |
5294dd70 BM |
255 | } |
256 | /* install fds[1] in place of fd: */ | |
257 | close(fds[0]); | |
258 | fds[0] = -1; | |
259 | getfd = fcntl(a.fd, F_GETFD); | |
260 | getfl = fcntl(a.fd, F_GETFL); | |
261 | r = dup2(fds[1], a.fd); | |
262 | close(fds[1]); | |
263 | fds[1] = -1; | |
264 | if (r == -1) { | |
0f113f3e MC |
265 | ret = -5; |
266 | goto err; | |
5294dd70 BM |
267 | } |
268 | if (getfd != 1) | |
0f113f3e | 269 | fcntl(a.fd, F_SETFD, getfd); |
5294dd70 | 270 | if (getfl & O_NONBLOCK) |
0f113f3e | 271 | (void)tls_socket_nonblocking(a.fd); |
5294dd70 | 272 | if (a.infofd != NULL) |
0f113f3e | 273 | *a.infofd = infofds[0]; |
5294dd70 | 274 | return 0; |
0f113f3e MC |
275 | |
276 | err: | |
5294dd70 | 277 | if (fds[0] != -1) |
0f113f3e | 278 | close(fds[0]); |
5294dd70 | 279 | if (fds[1] != -1) |
0f113f3e | 280 | close(fds[1]); |
5294dd70 | 281 | if (infofds[0] != -1) |
0f113f3e | 282 | close(infofds[0]); |
5294dd70 | 283 | if (infofds[1] != -1) |
0f113f3e | 284 | close(infofds[1]); |
5294dd70 BM |
285 | return ret; |
286 | } | |
287 | ||
288 | /*****************************************************************************/ | |
289 | ||
290 | static char errbuf[TLS_ERROR_BUFSIZ]; | |
291 | static size_t errbuf_i = 0; | |
292 | ||
0f113f3e | 293 | static void tls_errflush(void *apparg) |
5294dd70 BM |
294 | { |
295 | if (errbuf_i == 0) | |
0f113f3e MC |
296 | return; |
297 | ||
5294dd70 BM |
298 | assert(errbuf_i < sizeof errbuf); |
299 | assert(errbuf[errbuf_i] == 0); | |
300 | if (errbuf_i == sizeof errbuf - 1) { | |
0f113f3e MC |
301 | /* make sure we have a newline, even if string has been truncated */ |
302 | errbuf[errbuf_i - 1] = '\n'; | |
5294dd70 BM |
303 | } |
304 | ||
0f113f3e MC |
305 | /* |
306 | * TLS_APP_ERRFLUSH may modify the string as needed, e.g. substitute | |
307 | * other characters for \n for convenience | |
308 | */ | |
5294dd70 BM |
309 | TLS_APP_ERRFLUSH(tls_child_p, errbuf, errbuf_i, apparg); |
310 | ||
311 | errbuf_i = 0; | |
312 | } | |
313 | ||
0f113f3e | 314 | static void tls_errprintf(int flush, void *apparg, const char *fmt, ...) |
5294dd70 BM |
315 | { |
316 | va_list args; | |
317 | int r; | |
0f113f3e | 318 | |
5294dd70 | 319 | if (errbuf_i < sizeof errbuf - 1) { |
0f113f3e MC |
320 | size_t n; |
321 | ||
322 | va_start(args, fmt); | |
323 | n = (sizeof errbuf) - errbuf_i; | |
324 | r = vsnprintf(errbuf + errbuf_i, n, fmt, args); | |
325 | if (r >= n) | |
326 | r = n - 1; | |
327 | if (r >= 0) { | |
328 | errbuf_i += r; | |
329 | } else { | |
330 | errbuf_i = sizeof errbuf - 1; | |
331 | errbuf[errbuf_i] = '\0'; | |
332 | } | |
333 | assert(errbuf_i < sizeof errbuf); | |
334 | assert(errbuf[errbuf_i] == 0); | |
5294dd70 BM |
335 | } |
336 | #ifndef TLS_CUMULATE_ERRORS | |
337 | tls_errflush(apparg); | |
338 | #else | |
339 | if (flush) | |
0f113f3e | 340 | tls_errflush(apparg); |
5294dd70 BM |
341 | #endif |
342 | } | |
343 | ||
0f113f3e MC |
344 | /* |
345 | * app_prefix.. are for additional information provided by caller. If OpenSSL | |
346 | * error queue is empty, print default_text ("???" if NULL). | |
5294dd70 | 347 | */ |
0f113f3e MC |
348 | static char *tls_openssl_errors(const char *app_prefix_1, |
349 | const char *app_prefix_2, | |
350 | const char *default_text, void *apparg) | |
5294dd70 BM |
351 | { |
352 | static char reasons[255]; | |
353 | size_t reasons_i; | |
354 | unsigned long err; | |
355 | const char *file; | |
356 | int line; | |
357 | const char *data; | |
358 | int flags; | |
359 | char *errstring; | |
360 | int printed_something = 0; | |
0f113f3e | 361 | |
5294dd70 BM |
362 | reasons_i = 0; |
363 | ||
364 | assert(app_prefix_1 != NULL); | |
365 | assert(app_prefix_2 != NULL); | |
366 | ||
367 | if (default_text == NULL) | |
0f113f3e MC |
368 | default_text = "?" "?" "?"; |
369 | ||
370 | while ((err = ERR_get_error_line_data(&file, &line, &data, &flags)) != 0) { | |
371 | if (reasons_i < sizeof reasons) { | |
372 | size_t n; | |
373 | int r; | |
374 | ||
375 | n = (sizeof reasons) - reasons_i; | |
376 | r = snprintf(reasons + reasons_i, n, "%s%s", | |
377 | (reasons_i > 0 ? ", " : ""), | |
378 | ERR_reason_error_string(err)); | |
379 | if (r >= n) | |
380 | r = n - 1; | |
381 | if (r >= 0) { | |
382 | reasons_i += r; | |
383 | } else { | |
384 | reasons_i = sizeof reasons; | |
385 | } | |
386 | assert(reasons_i <= sizeof reasons); | |
387 | } | |
388 | ||
389 | errstring = ERR_error_string(err, NULL); | |
390 | assert(errstring != NULL); | |
391 | tls_errprintf(0, apparg, "OpenSSL error%s%s: %s:%s:%d:%s\n", | |
392 | app_prefix_1, app_prefix_2, errstring, file, line, | |
393 | (flags & ERR_TXT_STRING) ? data : ""); | |
394 | printed_something = 1; | |
5294dd70 BM |
395 | } |
396 | ||
397 | if (!printed_something) { | |
0f113f3e MC |
398 | assert(reasons_i == 0); |
399 | snprintf(reasons, sizeof reasons, "%s", default_text); | |
400 | tls_errprintf(0, apparg, "OpenSSL error%s%s: %s\n", app_prefix_1, | |
401 | app_prefix_2, default_text); | |
5294dd70 | 402 | } |
0f113f3e | 403 | #ifdef TLS_CUMULATE_ERRORS |
5294dd70 BM |
404 | tls_errflush(apparg); |
405 | #endif | |
406 | assert(errbuf_i == 0); | |
407 | ||
408 | return reasons; | |
409 | } | |
410 | ||
411 | /*****************************************************************************/ | |
412 | ||
413 | static int tls_init_done = 0; | |
414 | ||
0f113f3e | 415 | static int tls_init(void *apparg) |
5294dd70 BM |
416 | { |
417 | if (tls_init_done) | |
0f113f3e MC |
418 | return 0; |
419 | ||
5294dd70 BM |
420 | SSL_load_error_strings(); |
421 | if (!SSL_library_init() /* aka SSLeay_add_ssl_algorithms() */ ) { | |
0f113f3e MC |
422 | tls_errprintf(1, apparg, "SSL_library_init failed.\n"); |
423 | return -1; | |
5294dd70 BM |
424 | } |
425 | tls_init_done = 1; | |
426 | tls_rand_seed(); | |
427 | return 0; | |
428 | } | |
429 | ||
430 | /*****************************************************************************/ | |
431 | ||
0f113f3e | 432 | static void tls_rand_seed_uniquely(void) |
5294dd70 BM |
433 | { |
434 | struct { | |
0f113f3e MC |
435 | pid_t pid; |
436 | time_t time; | |
437 | void *stack; | |
5294dd70 BM |
438 | } data; |
439 | ||
440 | data.pid = getpid(); | |
441 | data.time = time(NULL); | |
442 | data.stack = (void *)&data; | |
443 | ||
444 | RAND_seed((const void *)&data, sizeof data); | |
445 | } | |
446 | ||
0f113f3e | 447 | void tls_rand_seed(void) |
5294dd70 BM |
448 | { |
449 | struct { | |
0f113f3e MC |
450 | struct utsname uname; |
451 | int uname_1; | |
452 | int uname_2; | |
453 | uid_t uid; | |
454 | uid_t euid; | |
455 | gid_t gid; | |
456 | gid_t egid; | |
5294dd70 | 457 | } data; |
0f113f3e | 458 | |
5294dd70 | 459 | data.uname_1 = uname(&data.uname); |
0f113f3e | 460 | data.uname_2 = errno; /* Let's hope that uname fails randomly :-) */ |
5294dd70 BM |
461 | |
462 | data.uid = getuid(); | |
463 | data.euid = geteuid(); | |
464 | data.gid = getgid(); | |
465 | data.egid = getegid(); | |
0f113f3e | 466 | |
5294dd70 BM |
467 | RAND_seed((const void *)&data, sizeof data); |
468 | tls_rand_seed_uniquely(); | |
469 | } | |
470 | ||
471 | static int tls_rand_seeded_p = 0; | |
472 | ||
0f113f3e MC |
473 | #define my_MIN_SEED_BYTES 256 /* struct stat can be larger than 128 */ |
474 | int tls_rand_seed_from_file(const char *filename, size_t n, void *apparg) | |
5294dd70 | 475 | { |
0f113f3e MC |
476 | /* |
477 | * Seed OpenSSL's random number generator from file. Try to read n bytes | |
478 | * if n > 0, whole file if n == 0. | |
479 | */ | |
5294dd70 BM |
480 | |
481 | int r; | |
482 | ||
483 | if (tls_init(apparg) == -1) | |
0f113f3e | 484 | return -1; |
5294dd70 BM |
485 | tls_rand_seed(); |
486 | ||
0f113f3e MC |
487 | r = RAND_load_file(filename, |
488 | (n > 0 && n < LONG_MAX) ? (long)n : LONG_MAX); | |
489 | /* | |
490 | * r is the number of bytes filled into the random number generator, | |
491 | * which are taken from "stat(filename, ...)" in addition to the file | |
492 | * contents. | |
5294dd70 BM |
493 | */ |
494 | assert(1 < my_MIN_SEED_BYTES); | |
0f113f3e MC |
495 | /* |
496 | * We need to detect at least those cases when the file does not exist at | |
497 | * all. With current versions of OpenSSL, this should do it: | |
498 | */ | |
5294dd70 | 499 | if (n == 0) |
0f113f3e | 500 | n = my_MIN_SEED_BYTES; |
5294dd70 | 501 | if (r < n) { |
0f113f3e MC |
502 | tls_errprintf(1, apparg, |
503 | "rand_seed_from_file: could not read %d bytes from %s.\n", | |
504 | n, filename); | |
505 | return -1; | |
5294dd70 | 506 | } else { |
0f113f3e MC |
507 | tls_rand_seeded_p = 1; |
508 | return 0; | |
5294dd70 BM |
509 | } |
510 | } | |
511 | ||
0f113f3e | 512 | void tls_rand_seed_from_memory(const void *buf, size_t n) |
5294dd70 BM |
513 | { |
514 | size_t i = 0; | |
0f113f3e | 515 | |
5294dd70 | 516 | while (i < n) { |
0f113f3e MC |
517 | size_t rest = n - i; |
518 | int chunk = rest < INT_MAX ? (int)rest : INT_MAX; | |
519 | RAND_seed((const char *)buf + i, chunk); | |
520 | i += chunk; | |
5294dd70 BM |
521 | } |
522 | tls_rand_seeded_p = 1; | |
523 | } | |
524 | ||
5294dd70 BM |
525 | /*****************************************************************************/ |
526 | ||
527 | struct tls_x509_name_string { | |
528 | char str[100]; | |
529 | }; | |
530 | ||
531 | static void | |
0f113f3e MC |
532 | tls_get_x509_subject_name_oneline(X509 *cert, |
533 | struct tls_x509_name_string *namestring) | |
5294dd70 BM |
534 | { |
535 | X509_NAME *name; | |
536 | ||
537 | if (cert == NULL) { | |
0f113f3e MC |
538 | namestring->str[0] = '\0'; |
539 | return; | |
5294dd70 | 540 | } |
0f113f3e MC |
541 | |
542 | name = X509_get_subject_name(cert); /* does not increment any reference | |
543 | * counter */ | |
5294dd70 BM |
544 | |
545 | assert(sizeof namestring->str >= 4); /* "?" or "...", plus 0 */ | |
0f113f3e | 546 | |
5294dd70 | 547 | if (name == NULL) { |
0f113f3e MC |
548 | namestring->str[0] = '?'; |
549 | namestring->str[1] = 0; | |
5294dd70 | 550 | } else { |
0f113f3e MC |
551 | size_t len; |
552 | ||
553 | X509_NAME_oneline(name, namestring->str, sizeof namestring->str); | |
554 | len = strlen(namestring->str); | |
555 | assert(namestring->str[len] == 0); | |
556 | assert(len < sizeof namestring->str); | |
557 | ||
558 | if (len + 1 == sizeof namestring->str) { | |
559 | /* | |
560 | * (Probably something was cut off.) Does not really work -- | |
561 | * X509_NAME_oneline truncates after name components, we cannot | |
562 | * tell from the result whether anything is missing. | |
563 | */ | |
564 | ||
565 | assert(namestring->str[len] == 0); | |
566 | namestring->str[--len] = '.'; | |
567 | namestring->str[--len] = '.'; | |
568 | namestring->str[--len] = '.'; | |
569 | } | |
5294dd70 BM |
570 | } |
571 | } | |
572 | ||
573 | /*****************************************************************************/ | |
574 | ||
575 | /* to hinder OpenSSL from asking for passphrases */ | |
0f113f3e | 576 | static int no_passphrase_callback(char *buf, int num, int w, void *arg) |
5294dd70 BM |
577 | { |
578 | return -1; | |
579 | } | |
580 | ||
93683c3c | 581 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
0f113f3e | 582 | static int verify_dont_fail_cb(X509_STORE_CTX *c, void *unused_arg) |
93683c3c | 583 | #else |
0f113f3e | 584 | static int verify_dont_fail_cb(X509_STORE_CTX *c) |
93683c3c | 585 | #endif |
5294dd70 BM |
586 | { |
587 | int i; | |
0f113f3e MC |
588 | |
589 | i = X509_verify_cert(c); /* sets c->error */ | |
5294dd70 | 590 | #if OPENSSL_VERSION_NUMBER >= 0x00905000L /* don't allow unverified |
0f113f3e MC |
591 | * certificates -- they could |
592 | * survive session reuse, but | |
593 | * OpenSSL < 0.9.5-dev does not | |
594 | * preserve their verify_result */ | |
5294dd70 | 595 | if (i == 0) |
0f113f3e | 596 | return 1; |
5294dd70 BM |
597 | else |
598 | #endif | |
0f113f3e | 599 | return i; |
5294dd70 BM |
600 | } |
601 | ||
0f113f3e MC |
602 | static DH *tls_dhe1024 = NULL; /* generating these takes a while, so do it |
603 | * just once */ | |
5294dd70 | 604 | |
0f113f3e | 605 | void tls_set_dhe1024(int i, void *apparg) |
5294dd70 BM |
606 | { |
607 | DSA *dsaparams; | |
608 | DH *dhparams; | |
609 | const char *seed[] = { ";-) :-( :-) :-( ", | |
0f113f3e MC |
610 | ";-) :-( :-) :-( ", |
611 | "Random String no. 12", | |
612 | ";-) :-( :-) :-( ", | |
613 | "hackers have even mo", /* from jargon file */ | |
5294dd70 BM |
614 | }; |
615 | unsigned char seedbuf[20]; | |
0f113f3e | 616 | |
5294dd70 BM |
617 | tls_init(apparg); |
618 | if (i >= 0) { | |
0f113f3e MC |
619 | i %= sizeof seed / sizeof seed[0]; |
620 | assert(strlen(seed[i]) == 20); | |
621 | memcpy(seedbuf, seed[i], 20); | |
622 | dsaparams = | |
623 | DSA_generate_parameters(1024, seedbuf, 20, NULL, NULL, 0, NULL); | |
5294dd70 | 624 | } else { |
0f113f3e MC |
625 | /* random parameters (may take a while) */ |
626 | dsaparams = | |
627 | DSA_generate_parameters(1024, NULL, 0, NULL, NULL, 0, NULL); | |
5294dd70 | 628 | } |
0f113f3e | 629 | |
5294dd70 | 630 | if (dsaparams == NULL) { |
0f113f3e MC |
631 | tls_openssl_errors("", "", NULL, apparg); |
632 | return; | |
5294dd70 BM |
633 | } |
634 | dhparams = DSA_dup_DH(dsaparams); | |
635 | DSA_free(dsaparams); | |
636 | if (dhparams == NULL) { | |
0f113f3e MC |
637 | tls_openssl_errors("", "", NULL, apparg); |
638 | return; | |
5294dd70 | 639 | } |
d6407083 | 640 | DH_free(tls_dhe1024); |
5294dd70 BM |
641 | tls_dhe1024 = dhparams; |
642 | } | |
643 | ||
0f113f3e | 644 | struct tls_create_ctx_args tls_create_ctx_defaultargs(void) |
5294dd70 | 645 | { |
0f113f3e | 646 | struct tls_create_ctx_args ret; |
5294dd70 | 647 | |
0f113f3e MC |
648 | ret.client_p = 0; |
649 | ret.certificate_file = NULL; | |
650 | ret.key_file = NULL; | |
651 | ret.ca_file = NULL; | |
652 | ret.verify_depth = -1; | |
653 | ret.fail_unless_verified = 0; | |
654 | ret.export_p = 0; | |
5294dd70 | 655 | |
0f113f3e | 656 | return ret; |
5294dd70 BM |
657 | } |
658 | ||
0f113f3e | 659 | SSL_CTX *tls_create_ctx(struct tls_create_ctx_args a, void *apparg) |
5294dd70 BM |
660 | { |
661 | int r; | |
662 | static long context_num = 0; | |
663 | SSL_CTX *ret; | |
664 | const char *err_pref_1 = "", *err_pref_2 = ""; | |
0f113f3e | 665 | |
5294dd70 | 666 | if (tls_init(apparg) == -1) |
0f113f3e | 667 | return NULL; |
5294dd70 | 668 | |
0f113f3e | 669 | ret = |
13c9bb3e | 670 | SSL_CTX_new((a.client_p ? TLS_client_method : |
32ec4153 | 671 | TLS_server_method) ()); |
5294dd70 BM |
672 | |
673 | if (ret == NULL) | |
0f113f3e | 674 | goto err; |
5294dd70 BM |
675 | |
676 | SSL_CTX_set_default_passwd_cb(ret, no_passphrase_callback); | |
677 | SSL_CTX_set_mode(ret, SSL_MODE_ENABLE_PARTIAL_WRITE); | |
0f113f3e | 678 | |
5294dd70 | 679 | if ((a.certificate_file != NULL) || (a.key_file != NULL)) { |
0f113f3e MC |
680 | if (a.key_file == NULL) { |
681 | tls_errprintf(1, apparg, "Need a key file.\n"); | |
682 | goto err_return; | |
683 | } | |
684 | if (a.certificate_file == NULL) { | |
685 | tls_errprintf(1, apparg, "Need a certificate chain file.\n"); | |
686 | goto err_return; | |
687 | } | |
688 | ||
689 | if (!SSL_CTX_use_PrivateKey_file(ret, a.key_file, SSL_FILETYPE_PEM)) | |
690 | goto err; | |
691 | if (!tls_rand_seeded_p) { | |
692 | /* | |
693 | * particularly paranoid people may not like this -- so provide | |
694 | * your own random seeding before calling this | |
695 | */ | |
696 | if (tls_rand_seed_from_file(a.key_file, 0, apparg) == -1) | |
697 | goto err_return; | |
698 | } | |
699 | if (!SSL_CTX_use_certificate_chain_file(ret, a.certificate_file)) | |
700 | goto err; | |
701 | if (!SSL_CTX_check_private_key(ret)) { | |
702 | tls_errprintf(1, apparg, | |
703 | "Private key \"%s\" does not match certificate \"%s\".\n", | |
704 | a.key_file, a.certificate_file); | |
705 | goto err_peek; | |
706 | } | |
5294dd70 | 707 | } |
0f113f3e | 708 | |
5294dd70 | 709 | if ((a.ca_file != NULL) || (a.verify_depth > 0)) { |
0f113f3e MC |
710 | context_num++; |
711 | r = SSL_CTX_set_session_id_context(ret, (const void *)&context_num, | |
712 | (unsigned int)sizeof context_num); | |
713 | if (!r) | |
714 | goto err; | |
715 | ||
716 | SSL_CTX_set_verify(ret, | |
717 | SSL_VERIFY_PEER | (a.fail_unless_verified ? | |
718 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT | |
719 | : 0), 0); | |
720 | if (!a.fail_unless_verified) | |
721 | SSL_CTX_set_cert_verify_callback(ret, verify_dont_fail_cb, NULL); | |
722 | ||
723 | if (a.verify_depth > 0) | |
724 | SSL_CTX_set_verify_depth(ret, a.verify_depth); | |
725 | ||
726 | if (a.ca_file != NULL) { | |
727 | /* does not report failure if file does not exist ... */ | |
728 | /* NULL argument means no CA-directory */ | |
729 | r = SSL_CTX_load_verify_locations(ret, a.ca_file, NULL); | |
730 | if (!r) { | |
731 | err_pref_1 = " while processing certificate file "; | |
732 | err_pref_2 = a.ca_file; | |
733 | goto err; | |
734 | } | |
735 | ||
736 | if (!a.client_p) { | |
737 | /* | |
738 | * SSL_load_client_CA_file is a misnomer, it just creates a | |
739 | * list of CNs. | |
740 | */ | |
741 | SSL_CTX_set_client_CA_list(ret, | |
742 | SSL_load_client_CA_file | |
743 | (a.ca_file)); | |
744 | /* | |
745 | * SSL_CTX_set_client_CA_list does not have a return value; | |
746 | * it does not really need one, but make sure (we really test | |
747 | * if SSL_load_client_CA_file worked) | |
748 | */ | |
749 | if (SSL_CTX_get_client_CA_list(ret) == NULL) { | |
750 | tls_errprintf(1, apparg, | |
751 | "Could not set client CA list from \"%s\".\n", | |
752 | a.ca_file); | |
753 | goto err_peek; | |
754 | } | |
755 | } | |
756 | } | |
5294dd70 | 757 | } |
0f113f3e | 758 | |
5294dd70 | 759 | if (!a.client_p) { |
0f113f3e MC |
760 | if (tls_dhe1024 == NULL) { |
761 | int i; | |
762 | ||
266483d2 MC |
763 | if (RAND_bytes((unsigned char *)&i, sizeof i) <= 0) |
764 | goto err_return; | |
0f113f3e MC |
765 | /* |
766 | * make sure that i is non-negative -- pick one of the provided | |
767 | * seeds | |
768 | */ | |
769 | if (i < 0) | |
770 | i = -i; | |
771 | if (i < 0) | |
772 | i = 0; | |
773 | tls_set_dhe1024(i, apparg); | |
774 | if (tls_dhe1024 == NULL) | |
775 | goto err_return; | |
776 | } | |
777 | ||
778 | if (!SSL_CTX_set_tmp_dh(ret, tls_dhe1024)) | |
779 | goto err; | |
780 | ||
781 | /* avoid small subgroup attacks: */ | |
782 | SSL_CTX_set_options(ret, SSL_OP_SINGLE_DH_USE); | |
5294dd70 | 783 | } |
5294dd70 BM |
784 | #ifndef NO_RSA |
785 | if (!a.client_p && a.export_p) { | |
0f113f3e MC |
786 | RSA *tmpkey; |
787 | ||
788 | tmpkey = RSA_generate_key(512, RSA_F4, 0, NULL); | |
789 | if (tmpkey == NULL) | |
790 | goto err; | |
791 | if (!SSL_CTX_set_tmp_rsa(ret, tmpkey)) { | |
792 | RSA_free(tmpkey); | |
793 | goto err; | |
794 | } | |
795 | RSA_free(tmpkey); /* SSL_CTX_set_tmp_rsa uses a duplicate. */ | |
5294dd70 BM |
796 | } |
797 | #endif | |
0f113f3e | 798 | |
5294dd70 | 799 | return ret; |
0f113f3e | 800 | |
5294dd70 BM |
801 | err_peek: |
802 | if (!ERR_peek_error()) | |
0f113f3e | 803 | goto err_return; |
5294dd70 BM |
804 | err: |
805 | tls_openssl_errors(err_pref_1, err_pref_2, NULL, apparg); | |
806 | err_return: | |
62adbcee | 807 | SSL_CTX_free(ret); |
5294dd70 BM |
808 | return NULL; |
809 | } | |
810 | ||
5294dd70 BM |
811 | /*****************************************************************************/ |
812 | ||
0f113f3e | 813 | static int tls_socket_nonblocking(int fd) |
5294dd70 BM |
814 | { |
815 | int v, r; | |
816 | ||
817 | v = fcntl(fd, F_GETFL, 0); | |
818 | if (v == -1) { | |
0f113f3e MC |
819 | if (errno == EINVAL) |
820 | return 0; /* already shut down -- ignore */ | |
821 | return -1; | |
5294dd70 BM |
822 | } |
823 | r = fcntl(fd, F_SETFL, v | O_NONBLOCK); | |
824 | if (r == -1) { | |
0f113f3e MC |
825 | if (errno == EINVAL) |
826 | return 0; /* already shut down -- ignore */ | |
827 | return -1; | |
5294dd70 BM |
828 | } |
829 | return 0; | |
830 | } | |
831 | ||
0f113f3e | 832 | static int max(int a, int b) |
5294dd70 BM |
833 | { |
834 | return a > b ? a : b; | |
835 | } | |
836 | ||
e636e2ac | 837 | /* timeout, -1 means no timeout */ |
5294dd70 | 838 | static void |
0f113f3e MC |
839 | tls_sockets_select(int read_select_1, int read_select_2, int write_select_1, |
840 | int write_select_2, int seconds) | |
5294dd70 BM |
841 | { |
842 | int maxfd, n; | |
843 | fd_set reads, writes; | |
844 | struct timeval timeout; | |
845 | struct timeval *timeout_p; | |
5294dd70 | 846 | |
0f113f3e MC |
847 | assert(read_select_1 >= -1 && read_select_2 >= -1 && write_select_1 >= -1 |
848 | && write_select_2 >= -1); | |
849 | assert(read_select_1 < FD_SETSIZE && read_select_2 < FD_SETSIZE - 1 | |
850 | && write_select_1 < FD_SETSIZE - 1 | |
851 | && write_select_2 < FD_SETSIZE - 1); | |
852 | ||
853 | maxfd = | |
854 | max(max(read_select_1, read_select_2), | |
855 | max(write_select_1, write_select_2)); | |
5294dd70 BM |
856 | assert(maxfd >= 0); |
857 | ||
858 | FD_ZERO(&reads); | |
859 | FD_ZERO(&writes); | |
0f113f3e MC |
860 | |
861 | for (n = 0; n < 4; ++n) { | |
862 | int i = n % 2; | |
863 | int w = n >= 2; | |
864 | /* loop over all (i, w) in {0,1}x{0,1} */ | |
865 | int fd; | |
866 | ||
867 | if (i == 0 && w == 0) | |
868 | fd = read_select_1; | |
869 | else if (i == 1 && w == 0) | |
870 | fd = read_select_2; | |
871 | else if (i == 0 && w == 1) | |
872 | fd = write_select_1; | |
873 | else { | |
874 | assert(i == 1 && w == 1); | |
875 | fd = write_select_2; | |
876 | } | |
877 | ||
878 | if (fd >= 0) { | |
879 | if (w == 0) | |
880 | FD_SET(fd, &reads); | |
881 | else /* w == 1 */ | |
882 | FD_SET(fd, &writes); | |
883 | } | |
5294dd70 BM |
884 | } |
885 | ||
886 | if (seconds >= 0) { | |
0f113f3e MC |
887 | timeout.tv_sec = seconds; |
888 | timeout.tv_usec = 0; | |
889 | timeout_p = &timeout; | |
890 | } else | |
891 | timeout_p = NULL; | |
5294dd70 BM |
892 | |
893 | DEBUG_MSG2("select no.", ++tls_select_count); | |
894 | select(maxfd + 1, &reads, &writes, (fd_set *) NULL, timeout_p); | |
895 | DEBUG_MSG("cont."); | |
896 | } | |
897 | ||
898 | /*****************************************************************************/ | |
899 | ||
900 | #define TUNNELBUFSIZE (16*1024) | |
901 | struct tunnelbuf { | |
902 | char buf[TUNNELBUFSIZE]; | |
903 | size_t len; | |
904 | size_t offset; | |
905 | }; | |
906 | ||
0f113f3e MC |
907 | static int tls_connect_attempt(SSL *, int *write_select, int *read_select, |
908 | int *closed, int *progress, | |
909 | const char **err_pref); | |
5294dd70 | 910 | |
0f113f3e MC |
911 | static int tls_accept_attempt(SSL *, int *write_select, int *read_select, |
912 | int *closed, int *progress, | |
913 | const char **err_pref); | |
5294dd70 | 914 | |
0f113f3e MC |
915 | static int tls_write_attempt(SSL *, struct tunnelbuf *, int *write_select, |
916 | int *read_select, int *closed, int *progress, | |
917 | const char **err_pref); | |
5294dd70 | 918 | |
0f113f3e MC |
919 | static int tls_read_attempt(SSL *, struct tunnelbuf *, int *write_select, |
920 | int *read_select, int *closed, int *progress, | |
921 | const char **err_pref); | |
5294dd70 | 922 | |
0f113f3e MC |
923 | static int write_attempt(int fd, struct tunnelbuf *, int *select, int *closed, |
924 | int *progress); | |
5294dd70 | 925 | |
0f113f3e MC |
926 | static int read_attempt(int fd, struct tunnelbuf *, int *select, int *closed, |
927 | int *progress); | |
5294dd70 BM |
928 | |
929 | static void write_info(SSL *ssl, int *info_fd) | |
930 | { | |
931 | if (*info_fd != -1) { | |
0f113f3e MC |
932 | long v; |
933 | int v_ok; | |
934 | struct tls_x509_name_string peer; | |
935 | char infobuf[TLS_INFO_SIZE]; | |
936 | int r; | |
937 | ||
938 | DEBUG_MSG("write_info"); | |
939 | v = SSL_get_verify_result(ssl); | |
940 | v_ok = (v == X509_V_OK) ? 'A' : 'E'; /* Auth./Error */ | |
941 | { | |
942 | X509 *peercert; | |
943 | ||
944 | peercert = SSL_get_peer_certificate(ssl); | |
945 | tls_get_x509_subject_name_oneline(peercert, &peer); | |
222561fe | 946 | X509_free(peercert); |
0f113f3e MC |
947 | } |
948 | if (peer.str[0] == '\0') | |
949 | v_ok = '0'; /* no cert at all */ | |
950 | else if (strchr(peer.str, '\n')) { | |
951 | /* should not happen, but make sure */ | |
952 | *strchr(peer.str, '\n') = '\0'; | |
953 | } | |
954 | r = snprintf(infobuf, sizeof infobuf, "%c:%s\n%s\n", v_ok, | |
955 | X509_verify_cert_error_string(v), peer.str); | |
956 | DEBUG_MSG2("snprintf", r); | |
957 | if (r == -1 || r >= sizeof infobuf) | |
958 | r = sizeof infobuf - 1; | |
959 | write(*info_fd, infobuf, r); | |
960 | close(*info_fd); | |
961 | *info_fd = -1; | |
5294dd70 BM |
962 | } |
963 | } | |
964 | ||
5294dd70 BM |
965 | /* tls_proxy expects that all fds are closed after return */ |
966 | static void | |
967 | tls_proxy(int clear_fd, int tls_fd, int info_fd, SSL_CTX *ctx, int client_p) | |
968 | { | |
969 | struct tunnelbuf clear_to_tls, tls_to_clear; | |
970 | SSL *ssl; | |
971 | BIO *rbio, *wbio; | |
972 | int closed, in_handshake; | |
973 | const char *err_pref_1 = "", *err_pref_2 = ""; | |
974 | const char *err_def = NULL; | |
975 | ||
976 | assert(clear_fd != -1); | |
977 | assert(tls_fd != -1); | |
978 | assert(clear_fd < FD_SETSIZE); | |
979 | assert(tls_fd < FD_SETSIZE); | |
980 | /* info_fd may be -1 */ | |
981 | assert(ctx != NULL); | |
982 | ||
983 | tls_rand_seed_uniquely(); | |
984 | ||
985 | tls_socket_nonblocking(clear_fd); | |
986 | DEBUG_MSG2("clear_fd", clear_fd); | |
987 | tls_socket_nonblocking(tls_fd); | |
988 | DEBUG_MSG2("tls_fd", tls_fd); | |
989 | ||
990 | ssl = SSL_new(ctx); | |
991 | if (ssl == NULL) | |
0f113f3e | 992 | goto err; |
5294dd70 BM |
993 | DEBUG_MSG("SSL_new"); |
994 | if (!SSL_set_fd(ssl, tls_fd)) | |
0f113f3e | 995 | goto err; |
5294dd70 | 996 | rbio = SSL_get_rbio(ssl); |
0f113f3e | 997 | wbio = SSL_get_wbio(ssl); /* should be the same, but who cares */ |
5294dd70 BM |
998 | assert(rbio != NULL); |
999 | assert(wbio != NULL); | |
1000 | if (client_p) | |
0f113f3e | 1001 | SSL_set_connect_state(ssl); |
5294dd70 | 1002 | else |
0f113f3e MC |
1003 | SSL_set_accept_state(ssl); |
1004 | ||
5294dd70 BM |
1005 | closed = 0; |
1006 | in_handshake = 1; | |
1007 | tls_to_clear.len = 0; | |
1008 | tls_to_clear.offset = 0; | |
1009 | clear_to_tls.len = 0; | |
1010 | clear_to_tls.offset = 0; | |
1011 | ||
1012 | err_def = "I/O error"; | |
0f113f3e MC |
1013 | |
1014 | /* | |
1015 | * loop finishes as soon as we detect that one side closed; when all | |
1016 | * (program and OS) buffers have enough space, the data from the last | |
1017 | * successful read in each direction is transferred before close | |
1018 | */ | |
5294dd70 | 1019 | do { |
0f113f3e MC |
1020 | int clear_read_select = 0, clear_write_select = 0, |
1021 | tls_read_select = 0, tls_write_select = 0, progress = 0; | |
1022 | int r; | |
1023 | unsigned long num_read = BIO_number_read(rbio), | |
1024 | num_written = BIO_number_written(wbio); | |
1025 | ||
1026 | DEBUG_MSG2("loop iteration", ++tls_loop_count); | |
1027 | ||
1028 | if (in_handshake) { | |
1029 | DEBUG_MSG("in_handshake"); | |
1030 | if (client_p) | |
1031 | r = tls_connect_attempt(ssl, &tls_write_select, | |
1032 | &tls_read_select, &closed, &progress, | |
1033 | &err_pref_1); | |
1034 | else | |
1035 | r = tls_accept_attempt(ssl, &tls_write_select, | |
1036 | &tls_read_select, &closed, &progress, | |
1037 | &err_pref_1); | |
1038 | if (r != 0) { | |
1039 | write_info(ssl, &info_fd); | |
1040 | goto err; | |
1041 | } | |
1042 | if (closed) | |
1043 | goto err_return; | |
1044 | if (!SSL_in_init(ssl)) { | |
1045 | in_handshake = 0; | |
1046 | write_info(ssl, &info_fd); | |
1047 | } | |
1048 | } | |
1049 | ||
1050 | if (clear_to_tls.len != 0 && !in_handshake) { | |
1051 | assert(!closed); | |
1052 | ||
1053 | r = tls_write_attempt(ssl, &clear_to_tls, &tls_write_select, | |
1054 | &tls_read_select, &closed, &progress, | |
1055 | &err_pref_1); | |
1056 | if (r != 0) | |
1057 | goto err; | |
1058 | if (closed) { | |
1059 | assert(progress); | |
1060 | tls_to_clear.offset = 0; | |
1061 | tls_to_clear.len = 0; | |
1062 | } | |
1063 | } | |
1064 | ||
1065 | if (tls_to_clear.len != 0) { | |
1066 | assert(!closed); | |
1067 | ||
1068 | r = write_attempt(clear_fd, &tls_to_clear, &clear_write_select, | |
1069 | &closed, &progress); | |
1070 | if (r != 0) | |
1071 | goto err_return; | |
1072 | if (closed) { | |
1073 | assert(progress); | |
1074 | clear_to_tls.offset = 0; | |
1075 | clear_to_tls.len = 0; | |
1076 | } | |
1077 | } | |
1078 | ||
1079 | if (!closed) { | |
1080 | if (clear_to_tls.offset + clear_to_tls.len < | |
1081 | sizeof clear_to_tls.buf) { | |
1082 | r = read_attempt(clear_fd, &clear_to_tls, &clear_read_select, | |
1083 | &closed, &progress); | |
1084 | if (r != 0) | |
1085 | goto err_return; | |
1086 | if (closed) { | |
1087 | r = SSL_shutdown(ssl); | |
1088 | DEBUG_MSG2("SSL_shutdown", r); | |
1089 | } | |
1090 | } | |
1091 | } | |
1092 | ||
1093 | if (!closed && !in_handshake) { | |
1094 | if (tls_to_clear.offset + tls_to_clear.len < | |
1095 | sizeof tls_to_clear.buf) { | |
1096 | r = tls_read_attempt(ssl, &tls_to_clear, &tls_write_select, | |
1097 | &tls_read_select, &closed, &progress, | |
1098 | &err_pref_1); | |
1099 | if (r != 0) | |
1100 | goto err; | |
1101 | if (closed) { | |
1102 | r = SSL_shutdown(ssl); | |
1103 | DEBUG_MSG2("SSL_shutdown", r); | |
1104 | } | |
1105 | } | |
1106 | } | |
1107 | ||
1108 | if (!progress) { | |
1109 | DEBUG_MSG("!progress?"); | |
1110 | if (num_read != BIO_number_read(rbio) | |
1111 | || num_written != BIO_number_written(wbio)) | |
1112 | progress = 1; | |
1113 | ||
1114 | if (!progress) { | |
1115 | DEBUG_MSG("!progress"); | |
1116 | assert(clear_read_select || tls_read_select | |
1117 | || clear_write_select || tls_write_select); | |
1118 | tls_sockets_select(clear_read_select ? clear_fd : -1, | |
1119 | tls_read_select ? tls_fd : -1, | |
1120 | clear_write_select ? clear_fd : -1, | |
1121 | tls_write_select ? tls_fd : -1, -1); | |
1122 | } | |
1123 | } | |
5294dd70 BM |
1124 | } while (!closed); |
1125 | return; | |
1126 | ||
1127 | err: | |
1128 | tls_openssl_errors(err_pref_1, err_pref_2, err_def, tls_child_apparg); | |
1129 | err_return: | |
1130 | return; | |
1131 | } | |
1132 | ||
5294dd70 | 1133 | static int |
0f113f3e MC |
1134 | tls_get_error(SSL *ssl, int r, int *write_select, int *read_select, |
1135 | int *closed, int *progress) | |
5294dd70 BM |
1136 | { |
1137 | int err = SSL_get_error(ssl, r); | |
1138 | ||
1139 | if (err == SSL_ERROR_NONE) { | |
0f113f3e MC |
1140 | assert(r > 0); |
1141 | *progress = 1; | |
1142 | return 0; | |
5294dd70 BM |
1143 | } |
1144 | ||
1145 | assert(r <= 0); | |
1146 | ||
1147 | switch (err) { | |
1148 | case SSL_ERROR_ZERO_RETURN: | |
0f113f3e MC |
1149 | assert(r == 0); |
1150 | *closed = 1; | |
1151 | *progress = 1; | |
1152 | return 0; | |
5294dd70 BM |
1153 | |
1154 | case SSL_ERROR_WANT_WRITE: | |
0f113f3e MC |
1155 | *write_select = 1; |
1156 | return 0; | |
1157 | ||
5294dd70 | 1158 | case SSL_ERROR_WANT_READ: |
0f113f3e MC |
1159 | *read_select = 1; |
1160 | return 0; | |
5294dd70 BM |
1161 | } |
1162 | ||
1163 | return -1; | |
1164 | } | |
1165 | ||
1166 | static int | |
0f113f3e MC |
1167 | tls_connect_attempt(SSL *ssl, int *write_select, int *read_select, |
1168 | int *closed, int *progress, const char **err_pref) | |
5294dd70 BM |
1169 | { |
1170 | int n, r; | |
1171 | ||
1172 | DEBUG_MSG("tls_connect_attempt"); | |
1173 | n = SSL_connect(ssl); | |
0f113f3e | 1174 | DEBUG_MSG2("SSL_connect", n); |
5294dd70 BM |
1175 | r = tls_get_error(ssl, n, write_select, read_select, closed, progress); |
1176 | if (r == -1) | |
0f113f3e | 1177 | *err_pref = " during SSL_connect"; |
5294dd70 BM |
1178 | return r; |
1179 | } | |
1180 | ||
1181 | static int | |
0f113f3e MC |
1182 | tls_accept_attempt(SSL *ssl, int *write_select, int *read_select, int *closed, |
1183 | int *progress, const char **err_pref) | |
5294dd70 BM |
1184 | { |
1185 | int n, r; | |
1186 | ||
1187 | DEBUG_MSG("tls_accept_attempt"); | |
1188 | n = SSL_accept(ssl); | |
0f113f3e | 1189 | DEBUG_MSG2("SSL_accept", n); |
5294dd70 BM |
1190 | r = tls_get_error(ssl, n, write_select, read_select, closed, progress); |
1191 | if (r == -1) | |
0f113f3e | 1192 | *err_pref = " during SSL_accept"; |
5294dd70 BM |
1193 | return r; |
1194 | } | |
1195 | ||
1196 | static int | |
0f113f3e MC |
1197 | tls_write_attempt(SSL *ssl, struct tunnelbuf *buf, int *write_select, |
1198 | int *read_select, int *closed, int *progress, | |
1199 | const char **err_pref) | |
5294dd70 BM |
1200 | { |
1201 | int n, r; | |
1202 | ||
1203 | DEBUG_MSG("tls_write_attempt"); | |
1204 | n = SSL_write(ssl, buf->buf + buf->offset, buf->len); | |
0f113f3e | 1205 | DEBUG_MSG2("SSL_write", n); |
5294dd70 BM |
1206 | r = tls_get_error(ssl, n, write_select, read_select, closed, progress); |
1207 | if (n > 0) { | |
0f113f3e MC |
1208 | buf->len -= n; |
1209 | assert(buf->len >= 0); | |
1210 | if (buf->len == 0) | |
1211 | buf->offset = 0; | |
1212 | else | |
1213 | buf->offset += n; | |
5294dd70 BM |
1214 | } |
1215 | if (r == -1) | |
0f113f3e | 1216 | *err_pref = " during SSL_write"; |
5294dd70 BM |
1217 | return r; |
1218 | } | |
1219 | ||
1220 | static int | |
0f113f3e MC |
1221 | tls_read_attempt(SSL *ssl, struct tunnelbuf *buf, int *write_select, |
1222 | int *read_select, int *closed, int *progress, | |
1223 | const char **err_pref) | |
5294dd70 BM |
1224 | { |
1225 | int n, r; | |
1226 | size_t total; | |
1227 | ||
1228 | DEBUG_MSG("tls_read_attempt"); | |
1229 | total = buf->offset + buf->len; | |
1230 | assert(total < sizeof buf->buf); | |
1231 | n = SSL_read(ssl, buf->buf + total, (sizeof buf->buf) - total); | |
0f113f3e | 1232 | DEBUG_MSG2("SSL_read", n); |
5294dd70 BM |
1233 | r = tls_get_error(ssl, n, write_select, read_select, closed, progress); |
1234 | if (n > 0) { | |
0f113f3e MC |
1235 | buf->len += n; |
1236 | assert(buf->offset + buf->len <= sizeof buf->buf); | |
5294dd70 BM |
1237 | } |
1238 | if (r == -1) | |
0f113f3e | 1239 | *err_pref = " during SSL_read"; |
5294dd70 BM |
1240 | return r; |
1241 | } | |
1242 | ||
0f113f3e | 1243 | static int get_error(int r, int *select, int *closed, int *progress) |
5294dd70 BM |
1244 | { |
1245 | if (r >= 0) { | |
0f113f3e MC |
1246 | *progress = 1; |
1247 | if (r == 0) | |
1248 | *closed = 1; | |
1249 | return 0; | |
5294dd70 | 1250 | } else { |
0f113f3e MC |
1251 | assert(r == -1); |
1252 | if (errno == EAGAIN || errno == EWOULDBLOCK) { | |
1253 | *select = 1; | |
1254 | return 0; | |
1255 | } else if (errno == EPIPE) { | |
1256 | *progress = 1; | |
1257 | *closed = 1; | |
1258 | return 0; | |
1259 | } else | |
1260 | return -1; | |
5294dd70 BM |
1261 | } |
1262 | } | |
1263 | ||
0f113f3e MC |
1264 | static int write_attempt(int fd, struct tunnelbuf *buf, int *select, |
1265 | int *closed, int *progress) | |
5294dd70 BM |
1266 | { |
1267 | int n, r; | |
1268 | ||
1269 | DEBUG_MSG("write_attempt"); | |
1270 | n = write(fd, buf->buf + buf->offset, buf->len); | |
0f113f3e | 1271 | DEBUG_MSG2("write", n); |
5294dd70 BM |
1272 | r = get_error(n, select, closed, progress); |
1273 | if (n > 0) { | |
0f113f3e MC |
1274 | buf->len -= n; |
1275 | assert(buf->len >= 0); | |
1276 | if (buf->len == 0) | |
1277 | buf->offset = 0; | |
1278 | else | |
1279 | buf->offset += n; | |
5294dd70 BM |
1280 | } |
1281 | if (r == -1) | |
0f113f3e MC |
1282 | tls_errprintf(1, tls_child_apparg, "write error: %s\n", |
1283 | strerror(errno)); | |
5294dd70 BM |
1284 | return r; |
1285 | } | |
0f113f3e | 1286 | |
5294dd70 | 1287 | static int |
0f113f3e MC |
1288 | read_attempt(int fd, struct tunnelbuf *buf, int *select, int *closed, |
1289 | int *progress) | |
5294dd70 BM |
1290 | { |
1291 | int n, r; | |
1292 | size_t total; | |
1293 | ||
1294 | DEBUG_MSG("read_attempt"); | |
1295 | total = buf->offset + buf->len; | |
1296 | assert(total < sizeof buf->buf); | |
1297 | n = read(fd, buf->buf + total, (sizeof buf->buf) - total); | |
0f113f3e | 1298 | DEBUG_MSG2("read", n); |
5294dd70 BM |
1299 | r = get_error(n, select, closed, progress); |
1300 | if (n > 0) { | |
0f113f3e MC |
1301 | buf->len += n; |
1302 | assert(buf->offset + buf->len <= sizeof buf->buf); | |
5294dd70 BM |
1303 | } |
1304 | if (r == -1) | |
0f113f3e MC |
1305 | tls_errprintf(1, tls_child_apparg, "read error: %s\n", |
1306 | strerror(errno)); | |
5294dd70 BM |
1307 | return r; |
1308 | } |