ssl.c revision 1.2 1 1.2 christos /* $NetBSD: ssl.c,v 1.2 2012/12/24 22:12:28 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 1998-2004 Dag-Erling Codan Smrgrav
5 1.1 christos * Copyright (c) 2008, 2010 Joerg Sonnenberger <joerg (at) NetBSD.org>
6 1.1 christos * All rights reserved.
7 1.1 christos *
8 1.1 christos * Redistribution and use in source and binary forms, with or without
9 1.1 christos * modification, are permitted provided that the following conditions
10 1.1 christos * are met:
11 1.1 christos * 1. Redistributions of source code must retain the above copyright
12 1.1 christos * notice, this list of conditions and the following disclaimer
13 1.1 christos * in this position and unchanged.
14 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 christos * notice, this list of conditions and the following disclaimer in the
16 1.1 christos * documentation and/or other materials provided with the distribution.
17 1.1 christos * 3. The name of the author may not be used to endorse or promote products
18 1.1 christos * derived from this software without specific prior written permission
19 1.1 christos *
20 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos *
31 1.1 christos * $FreeBSD: common.c,v 1.53 2007/12/19 00:26:36 des Exp $
32 1.1 christos */
33 1.1 christos
34 1.1 christos #include <sys/cdefs.h>
35 1.1 christos #ifndef lint
36 1.2 christos __RCSID("$NetBSD: ssl.c,v 1.2 2012/12/24 22:12:28 christos Exp $");
37 1.1 christos #endif
38 1.1 christos
39 1.1 christos #include <time.h>
40 1.1 christos #include <unistd.h>
41 1.1 christos #include <fcntl.h>
42 1.1 christos
43 1.1 christos #include <sys/param.h>
44 1.1 christos #include <sys/select.h>
45 1.1 christos #include <sys/uio.h>
46 1.1 christos
47 1.1 christos #include <netinet/tcp.h>
48 1.1 christos #include <netinet/in.h>
49 1.1 christos #include <openssl/crypto.h>
50 1.1 christos #include <openssl/x509.h>
51 1.1 christos #include <openssl/pem.h>
52 1.1 christos #include <openssl/ssl.h>
53 1.1 christos #include <openssl/err.h>
54 1.1 christos
55 1.1 christos #include "ssl.h"
56 1.1 christos
57 1.1 christos extern int quit_time, verbose, ftp_debug;
58 1.1 christos extern FILE *ttyout;
59 1.1 christos
60 1.1 christos struct fetch_connect {
61 1.1 christos int sd; /* file/socket descriptor */
62 1.1 christos char *buf; /* buffer */
63 1.1 christos size_t bufsize; /* buffer size */
64 1.1 christos size_t bufpos; /* position of buffer */
65 1.1 christos size_t buflen; /* length of buffer contents */
66 1.1 christos struct { /* data cached after an
67 1.1 christos interrupted read */
68 1.1 christos char *buf;
69 1.1 christos size_t size;
70 1.1 christos size_t pos;
71 1.1 christos size_t len;
72 1.1 christos } cache;
73 1.1 christos int issock;
74 1.1 christos int iserr;
75 1.1 christos int iseof;
76 1.1 christos SSL *ssl; /* SSL handle */
77 1.1 christos };
78 1.1 christos
79 1.1 christos /*
80 1.1 christos * Write a vector to a connection w/ timeout
81 1.1 christos * Note: can modify the iovec.
82 1.1 christos */
83 1.1 christos static ssize_t
84 1.1 christos fetch_writev(struct fetch_connect *conn, struct iovec *iov, int iovcnt)
85 1.1 christos {
86 1.1 christos struct timeval now, timeout, delta;
87 1.1 christos fd_set writefds;
88 1.1 christos ssize_t len, total;
89 1.1 christos int r;
90 1.1 christos
91 1.1 christos if (quit_time > 0) {
92 1.1 christos FD_ZERO(&writefds);
93 1.1 christos gettimeofday(&timeout, NULL);
94 1.1 christos timeout.tv_sec += quit_time;
95 1.1 christos }
96 1.1 christos
97 1.1 christos total = 0;
98 1.1 christos while (iovcnt > 0) {
99 1.1 christos while (quit_time > 0 && !FD_ISSET(conn->sd, &writefds)) {
100 1.1 christos FD_SET(conn->sd, &writefds);
101 1.1 christos gettimeofday(&now, NULL);
102 1.1 christos delta.tv_sec = timeout.tv_sec - now.tv_sec;
103 1.1 christos delta.tv_usec = timeout.tv_usec - now.tv_usec;
104 1.1 christos if (delta.tv_usec < 0) {
105 1.1 christos delta.tv_usec += 1000000;
106 1.1 christos delta.tv_sec--;
107 1.1 christos }
108 1.1 christos if (delta.tv_sec < 0) {
109 1.1 christos errno = ETIMEDOUT;
110 1.1 christos return -1;
111 1.1 christos }
112 1.1 christos errno = 0;
113 1.1 christos r = select(conn->sd + 1, NULL, &writefds, NULL, &delta);
114 1.1 christos if (r == -1) {
115 1.1 christos if (errno == EINTR)
116 1.1 christos continue;
117 1.1 christos return -1;
118 1.1 christos }
119 1.1 christos }
120 1.1 christos errno = 0;
121 1.1 christos if (conn->ssl != NULL)
122 1.1 christos len = SSL_write(conn->ssl, iov->iov_base, iov->iov_len);
123 1.1 christos else
124 1.1 christos len = writev(conn->sd, iov, iovcnt);
125 1.1 christos if (len == 0) {
126 1.1 christos /* we consider a short write a failure */
127 1.1 christos /* XXX perhaps we shouldn't in the SSL case */
128 1.1 christos errno = EPIPE;
129 1.1 christos return -1;
130 1.1 christos }
131 1.1 christos if (len < 0) {
132 1.1 christos if (errno == EINTR)
133 1.1 christos continue;
134 1.1 christos return -1;
135 1.1 christos }
136 1.1 christos total += len;
137 1.1 christos while (iovcnt > 0 && len >= (ssize_t)iov->iov_len) {
138 1.1 christos len -= iov->iov_len;
139 1.1 christos iov++;
140 1.1 christos iovcnt--;
141 1.1 christos }
142 1.1 christos if (iovcnt > 0) {
143 1.1 christos iov->iov_len -= len;
144 1.1 christos iov->iov_base = (char *)iov->iov_base + len;
145 1.1 christos }
146 1.1 christos }
147 1.1 christos return total;
148 1.1 christos }
149 1.1 christos
150 1.1 christos /*
151 1.1 christos * Write to a connection w/ timeout
152 1.1 christos */
153 1.1 christos static int
154 1.1 christos fetch_write(struct fetch_connect *conn, const char *str, size_t len)
155 1.1 christos {
156 1.1 christos struct iovec iov[1];
157 1.1 christos
158 1.1 christos iov[0].iov_base = (char *)__UNCONST(str);
159 1.1 christos iov[0].iov_len = len;
160 1.1 christos return fetch_writev(conn, iov, 1);
161 1.1 christos }
162 1.1 christos
163 1.1 christos /*
164 1.1 christos * Send a formatted line; optionally echo to terminal
165 1.1 christos */
166 1.1 christos int
167 1.1 christos fetch_printf(struct fetch_connect *conn, const char *fmt, ...)
168 1.1 christos {
169 1.1 christos va_list ap;
170 1.1 christos size_t len;
171 1.1 christos char *msg;
172 1.1 christos int r;
173 1.1 christos
174 1.1 christos va_start(ap, fmt);
175 1.1 christos len = vasprintf(&msg, fmt, ap);
176 1.1 christos va_end(ap);
177 1.1 christos
178 1.1 christos if (msg == NULL) {
179 1.1 christos errno = ENOMEM;
180 1.1 christos return -1;
181 1.1 christos }
182 1.1 christos
183 1.1 christos r = fetch_write(conn, msg, len);
184 1.1 christos free(msg);
185 1.1 christos return r;
186 1.1 christos }
187 1.1 christos
188 1.1 christos int
189 1.1 christos fetch_fileno(struct fetch_connect *conn)
190 1.1 christos {
191 1.1 christos
192 1.1 christos return conn->sd;
193 1.1 christos }
194 1.1 christos
195 1.1 christos int
196 1.1 christos fetch_error(struct fetch_connect *conn)
197 1.1 christos {
198 1.1 christos
199 1.1 christos return conn->iserr;
200 1.1 christos }
201 1.1 christos
202 1.1 christos static void
203 1.1 christos fetch_clearerr(struct fetch_connect *conn)
204 1.1 christos {
205 1.1 christos
206 1.1 christos conn->iserr = 0;
207 1.1 christos }
208 1.1 christos
209 1.1 christos int
210 1.1 christos fetch_flush(struct fetch_connect *conn)
211 1.1 christos {
212 1.1 christos int v;
213 1.1 christos
214 1.1 christos if (conn->issock) {
215 1.1 christos #ifdef TCP_NOPUSH
216 1.1 christos v = 0;
217 1.1 christos setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &v, sizeof(v));
218 1.1 christos #endif
219 1.1 christos v = 1;
220 1.1 christos setsockopt(conn->sd, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
221 1.1 christos }
222 1.1 christos return 0;
223 1.1 christos }
224 1.1 christos
225 1.1 christos /*ARGSUSED*/
226 1.1 christos struct fetch_connect *
227 1.1 christos fetch_open(const char *fname, const char *fmode)
228 1.1 christos {
229 1.1 christos struct fetch_connect *conn;
230 1.1 christos int fd;
231 1.1 christos
232 1.1 christos fd = open(fname, O_RDONLY); /* XXX: fmode */
233 1.1 christos if (fd < 0)
234 1.1 christos return NULL;
235 1.1 christos
236 1.1 christos if ((conn = calloc(1, sizeof(*conn))) == NULL) {
237 1.1 christos close(fd);
238 1.1 christos return NULL;
239 1.1 christos }
240 1.1 christos
241 1.1 christos conn->sd = fd;
242 1.1 christos conn->issock = 0;
243 1.1 christos return conn;
244 1.1 christos }
245 1.1 christos
246 1.1 christos /*ARGSUSED*/
247 1.1 christos struct fetch_connect *
248 1.1 christos fetch_fdopen(int sd, const char *fmode)
249 1.1 christos {
250 1.1 christos struct fetch_connect *conn;
251 1.2 christos #if defined(SO_NOSIGPIPE) || defined(TCP_NOPUSH)
252 1.1 christos int opt = 1;
253 1.2 christos #endif
254 1.1 christos
255 1.1 christos if ((conn = calloc(1, sizeof(*conn))) == NULL)
256 1.1 christos return NULL;
257 1.1 christos
258 1.1 christos conn->sd = sd;
259 1.1 christos conn->issock = 1;
260 1.1 christos fcntl(sd, F_SETFD, FD_CLOEXEC);
261 1.2 christos #ifdef SO_NOSIGPIPE
262 1.1 christos setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
263 1.2 christos #endif
264 1.1 christos #ifdef TCP_NOPUSH
265 1.1 christos setsockopt(sd, IPPROTO_TCP, TCP_NOPUSH, &opt, sizeof(opt));
266 1.1 christos #endif
267 1.1 christos return conn;
268 1.1 christos }
269 1.1 christos
270 1.1 christos int
271 1.1 christos fetch_close(struct fetch_connect *conn)
272 1.1 christos {
273 1.1 christos int rv = 0;
274 1.1 christos
275 1.1 christos if (conn != NULL) {
276 1.1 christos fetch_flush(conn);
277 1.1 christos SSL_free(conn->ssl);
278 1.1 christos rv = close(conn->sd);
279 1.1 christos if (rv < 0) {
280 1.1 christos errno = rv;
281 1.1 christos rv = EOF;
282 1.1 christos }
283 1.1 christos free(conn->cache.buf);
284 1.1 christos free(conn->buf);
285 1.1 christos free(conn);
286 1.1 christos }
287 1.1 christos return rv;
288 1.1 christos }
289 1.1 christos
290 1.1 christos #define FETCH_READ_WAIT -2
291 1.1 christos #define FETCH_READ_ERROR -1
292 1.1 christos
293 1.1 christos static ssize_t
294 1.1 christos fetch_ssl_read(SSL *ssl, void *buf, size_t len)
295 1.1 christos {
296 1.1 christos ssize_t rlen;
297 1.1 christos int ssl_err;
298 1.1 christos
299 1.1 christos rlen = SSL_read(ssl, buf, len);
300 1.1 christos if (rlen < 0) {
301 1.1 christos ssl_err = SSL_get_error(ssl, rlen);
302 1.1 christos if (ssl_err == SSL_ERROR_WANT_READ ||
303 1.1 christos ssl_err == SSL_ERROR_WANT_WRITE) {
304 1.1 christos return FETCH_READ_WAIT;
305 1.1 christos }
306 1.1 christos ERR_print_errors_fp(ttyout);
307 1.1 christos return FETCH_READ_ERROR;
308 1.1 christos }
309 1.1 christos return rlen;
310 1.1 christos }
311 1.1 christos
312 1.1 christos static ssize_t
313 1.1 christos fetch_nonssl_read(int sd, void *buf, size_t len)
314 1.1 christos {
315 1.1 christos ssize_t rlen;
316 1.1 christos
317 1.1 christos rlen = read(sd, buf, len);
318 1.1 christos if (rlen < 0) {
319 1.1 christos if (errno == EAGAIN || errno == EINTR)
320 1.1 christos return FETCH_READ_WAIT;
321 1.1 christos return FETCH_READ_ERROR;
322 1.1 christos }
323 1.1 christos return rlen;
324 1.1 christos }
325 1.1 christos
326 1.1 christos /*
327 1.1 christos * Cache some data that was read from a socket but cannot be immediately
328 1.1 christos * returned because of an interrupted system call.
329 1.1 christos */
330 1.1 christos static int
331 1.1 christos fetch_cache_data(struct fetch_connect *conn, char *src, size_t nbytes)
332 1.1 christos {
333 1.1 christos
334 1.1 christos if (conn->cache.size < nbytes) {
335 1.1 christos char *tmp = realloc(conn->cache.buf, nbytes);
336 1.1 christos if (tmp == NULL)
337 1.1 christos return -1;
338 1.1 christos
339 1.1 christos conn->cache.buf = tmp;
340 1.1 christos conn->cache.size = nbytes;
341 1.1 christos }
342 1.1 christos
343 1.1 christos memcpy(conn->cache.buf, src, nbytes);
344 1.1 christos conn->cache.len = nbytes;
345 1.1 christos conn->cache.pos = 0;
346 1.1 christos return 0;
347 1.1 christos }
348 1.1 christos
349 1.1 christos ssize_t
350 1.1 christos fetch_read(void *ptr, size_t size, size_t nmemb, struct fetch_connect *conn)
351 1.1 christos {
352 1.1 christos struct timeval now, timeout, delta;
353 1.1 christos fd_set readfds;
354 1.1 christos ssize_t rlen, total;
355 1.1 christos size_t len;
356 1.1 christos char *start, *buf;
357 1.1 christos
358 1.1 christos if (quit_time > 0) {
359 1.1 christos gettimeofday(&timeout, NULL);
360 1.1 christos timeout.tv_sec += quit_time;
361 1.1 christos }
362 1.1 christos
363 1.1 christos total = 0;
364 1.1 christos start = buf = ptr;
365 1.1 christos len = size * nmemb;
366 1.1 christos
367 1.1 christos if (conn->cache.len > 0) {
368 1.1 christos /*
369 1.1 christos * The last invocation of fetch_read was interrupted by a
370 1.1 christos * signal after some data had been read from the socket. Copy
371 1.1 christos * the cached data into the supplied buffer before trying to
372 1.1 christos * read from the socket again.
373 1.1 christos */
374 1.1 christos total = (conn->cache.len < len) ? conn->cache.len : len;
375 1.1 christos memcpy(buf, conn->cache.buf, total);
376 1.1 christos
377 1.1 christos conn->cache.len -= total;
378 1.1 christos conn->cache.pos += total;
379 1.1 christos len -= total;
380 1.1 christos buf += total;
381 1.1 christos }
382 1.1 christos
383 1.1 christos while (len > 0) {
384 1.1 christos /*
385 1.1 christos * The socket is non-blocking. Instead of the canonical
386 1.1 christos * select() -> read(), we do the following:
387 1.1 christos *
388 1.1 christos * 1) call read() or SSL_read().
389 1.1 christos * 2) if an error occurred, return -1.
390 1.1 christos * 3) if we received data but we still expect more,
391 1.1 christos * update our counters and loop.
392 1.1 christos * 4) if read() or SSL_read() signaled EOF, return.
393 1.1 christos * 5) if we did not receive any data but we're not at EOF,
394 1.1 christos * call select().
395 1.1 christos *
396 1.1 christos * In the SSL case, this is necessary because if we
397 1.1 christos * receive a close notification, we have to call
398 1.1 christos * SSL_read() one additional time after we've read
399 1.1 christos * everything we received.
400 1.1 christos *
401 1.1 christos * In the non-SSL case, it may improve performance (very
402 1.1 christos * slightly) when reading small amounts of data.
403 1.1 christos */
404 1.1 christos if (conn->ssl != NULL)
405 1.1 christos rlen = fetch_ssl_read(conn->ssl, buf, len);
406 1.1 christos else
407 1.1 christos rlen = fetch_nonssl_read(conn->sd, buf, len);
408 1.1 christos if (rlen == 0) {
409 1.1 christos break;
410 1.1 christos } else if (rlen > 0) {
411 1.1 christos len -= rlen;
412 1.1 christos buf += rlen;
413 1.1 christos total += rlen;
414 1.1 christos continue;
415 1.1 christos } else if (rlen == FETCH_READ_ERROR) {
416 1.1 christos if (errno == EINTR)
417 1.1 christos fetch_cache_data(conn, start, total);
418 1.1 christos return -1;
419 1.1 christos }
420 1.1 christos FD_ZERO(&readfds);
421 1.1 christos while (!FD_ISSET(conn->sd, &readfds)) {
422 1.1 christos FD_SET(conn->sd, &readfds);
423 1.1 christos if (quit_time > 0) {
424 1.1 christos gettimeofday(&now, NULL);
425 1.1 christos if (!timercmp(&timeout, &now, >)) {
426 1.1 christos errno = ETIMEDOUT;
427 1.1 christos return -1;
428 1.1 christos }
429 1.1 christos timersub(&timeout, &now, &delta);
430 1.1 christos }
431 1.1 christos errno = 0;
432 1.1 christos if (select(conn->sd + 1, &readfds, NULL, NULL,
433 1.1 christos quit_time > 0 ? &delta : NULL) < 0) {
434 1.1 christos if (errno == EINTR)
435 1.1 christos continue;
436 1.1 christos return -1;
437 1.1 christos }
438 1.1 christos }
439 1.1 christos }
440 1.1 christos return total;
441 1.1 christos }
442 1.1 christos
443 1.1 christos #define MIN_BUF_SIZE 1024
444 1.1 christos
445 1.1 christos /*
446 1.1 christos * Read a line of text from a connection w/ timeout
447 1.1 christos */
448 1.1 christos char *
449 1.1 christos fetch_getln(char *str, int size, struct fetch_connect *conn)
450 1.1 christos {
451 1.1 christos size_t tmpsize;
452 1.1 christos ssize_t len;
453 1.1 christos char c;
454 1.1 christos
455 1.1 christos if (conn->buf == NULL) {
456 1.1 christos if ((conn->buf = malloc(MIN_BUF_SIZE)) == NULL) {
457 1.1 christos errno = ENOMEM;
458 1.1 christos conn->iserr = 1;
459 1.1 christos return NULL;
460 1.1 christos }
461 1.1 christos conn->bufsize = MIN_BUF_SIZE;
462 1.1 christos }
463 1.1 christos
464 1.1 christos if (conn->iserr || conn->iseof)
465 1.1 christos return NULL;
466 1.1 christos
467 1.1 christos if (conn->buflen - conn->bufpos > 0)
468 1.1 christos goto done;
469 1.1 christos
470 1.1 christos conn->buf[0] = '\0';
471 1.1 christos conn->bufpos = 0;
472 1.1 christos conn->buflen = 0;
473 1.1 christos do {
474 1.1 christos len = fetch_read(&c, sizeof(c), 1, conn);
475 1.1 christos if (len == -1) {
476 1.1 christos conn->iserr = 1;
477 1.1 christos return NULL;
478 1.1 christos }
479 1.1 christos if (len == 0) {
480 1.1 christos conn->iseof = 1;
481 1.1 christos break;
482 1.1 christos }
483 1.1 christos conn->buf[conn->buflen++] = c;
484 1.1 christos if (conn->buflen == conn->bufsize) {
485 1.1 christos char *tmp = conn->buf;
486 1.1 christos tmpsize = conn->bufsize * 2 + 1;
487 1.1 christos if ((tmp = realloc(tmp, tmpsize)) == NULL) {
488 1.1 christos errno = ENOMEM;
489 1.1 christos conn->iserr = 1;
490 1.1 christos return NULL;
491 1.1 christos }
492 1.1 christos conn->buf = tmp;
493 1.1 christos conn->bufsize = tmpsize;
494 1.1 christos }
495 1.1 christos } while (c != '\n');
496 1.1 christos
497 1.1 christos if (conn->buflen == 0)
498 1.1 christos return NULL;
499 1.1 christos done:
500 1.1 christos tmpsize = MIN(size - 1, (int)(conn->buflen - conn->bufpos));
501 1.1 christos memcpy(str, conn->buf + conn->bufpos, tmpsize);
502 1.1 christos str[tmpsize] = '\0';
503 1.1 christos conn->bufpos += tmpsize;
504 1.1 christos return str;
505 1.1 christos }
506 1.1 christos
507 1.1 christos int
508 1.1 christos fetch_getline(struct fetch_connect *conn, char *buf, size_t buflen,
509 1.1 christos const char **errormsg)
510 1.1 christos {
511 1.1 christos size_t len;
512 1.1 christos int rv;
513 1.1 christos
514 1.1 christos if (fetch_getln(buf, buflen, conn) == NULL) {
515 1.1 christos if (conn->iseof) { /* EOF */
516 1.1 christos rv = -2;
517 1.1 christos if (errormsg)
518 1.1 christos *errormsg = "\nEOF received";
519 1.1 christos } else { /* error */
520 1.1 christos rv = -1;
521 1.1 christos if (errormsg)
522 1.1 christos *errormsg = "Error encountered";
523 1.1 christos }
524 1.1 christos fetch_clearerr(conn);
525 1.1 christos return rv;
526 1.1 christos }
527 1.1 christos len = strlen(buf);
528 1.1 christos if (buf[len - 1] == '\n') { /* clear any trailing newline */
529 1.1 christos buf[--len] = '\0';
530 1.1 christos } else if (len == buflen - 1) { /* line too long */
531 1.1 christos while (1) {
532 1.1 christos char c;
533 1.1 christos ssize_t rlen = fetch_read(&c, sizeof(c), 1, conn);
534 1.1 christos if (rlen <= 0 || c == '\n')
535 1.1 christos break;
536 1.1 christos }
537 1.1 christos if (errormsg)
538 1.1 christos *errormsg = "Input line is too long";
539 1.1 christos fetch_clearerr(conn);
540 1.1 christos return -3;
541 1.1 christos }
542 1.1 christos if (errormsg)
543 1.1 christos *errormsg = NULL;
544 1.1 christos return len;
545 1.1 christos }
546 1.1 christos
547 1.1 christos void *
548 1.1 christos fetch_start_ssl(int sock)
549 1.1 christos {
550 1.1 christos SSL *ssl;
551 1.1 christos SSL_CTX *ctx;
552 1.1 christos int ret, ssl_err;
553 1.1 christos
554 1.1 christos /* Init the SSL library and context */
555 1.1 christos if (!SSL_library_init()){
556 1.1 christos fprintf(ttyout, "SSL library init failed\n");
557 1.1 christos return NULL;
558 1.1 christos }
559 1.1 christos
560 1.1 christos SSL_load_error_strings();
561 1.1 christos
562 1.1 christos ctx = SSL_CTX_new(SSLv23_client_method());
563 1.1 christos SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
564 1.1 christos
565 1.1 christos ssl = SSL_new(ctx);
566 1.1 christos if (ssl == NULL){
567 1.1 christos fprintf(ttyout, "SSL context creation failed\n");
568 1.1 christos SSL_CTX_free(ctx);
569 1.1 christos return NULL;
570 1.1 christos }
571 1.1 christos SSL_set_fd(ssl, sock);
572 1.1 christos while ((ret = SSL_connect(ssl)) == -1) {
573 1.1 christos ssl_err = SSL_get_error(ssl, ret);
574 1.1 christos if (ssl_err != SSL_ERROR_WANT_READ &&
575 1.1 christos ssl_err != SSL_ERROR_WANT_WRITE) {
576 1.1 christos ERR_print_errors_fp(ttyout);
577 1.1 christos SSL_free(ssl);
578 1.1 christos return NULL;
579 1.1 christos }
580 1.1 christos }
581 1.1 christos
582 1.1 christos if (ftp_debug && verbose) {
583 1.1 christos X509 *cert;
584 1.1 christos X509_NAME *name;
585 1.1 christos char *str;
586 1.1 christos
587 1.1 christos fprintf(ttyout, "SSL connection established using %s\n",
588 1.1 christos SSL_get_cipher(ssl));
589 1.1 christos cert = SSL_get_peer_certificate(ssl);
590 1.1 christos name = X509_get_subject_name(cert);
591 1.1 christos str = X509_NAME_oneline(name, 0, 0);
592 1.1 christos fprintf(ttyout, "Certificate subject: %s\n", str);
593 1.1 christos free(str);
594 1.1 christos name = X509_get_issuer_name(cert);
595 1.1 christos str = X509_NAME_oneline(name, 0, 0);
596 1.1 christos fprintf(ttyout, "Certificate issuer: %s\n", str);
597 1.1 christos free(str);
598 1.1 christos }
599 1.1 christos
600 1.1 christos return ssl;
601 1.1 christos }
602 1.1 christos
603 1.1 christos
604 1.1 christos void
605 1.1 christos fetch_set_ssl(struct fetch_connect *conn, void *ssl)
606 1.1 christos {
607 1.1 christos conn->ssl = ssl;
608 1.1 christos }
609