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