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