-/* $OpenBSD: iobuf.c,v 1.10 2017/03/17 20:56:04 eric Exp $ */
+/* $OpenBSD: iobuf.c,v 1.11 2019/06/12 17:42:53 eric Exp $ */
/*
* Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
*
#include <string.h>
#include <unistd.h>
-#ifdef IO_SSL
+#ifdef IO_TLS
#include <openssl/err.h>
#include <openssl/ssl.h>
#endif
return (0);
}
-#ifdef IO_SSL
+#ifdef IO_TLS
int
-iobuf_flush_ssl(struct iobuf *io, void *ssl)
+iobuf_flush_tls(struct iobuf *io, void *tls)
{
ssize_t s;
while (io->queued)
- if ((s = iobuf_write_ssl(io, ssl)) < 0)
+ if ((s = iobuf_write_tls(io, tls)) < 0)
return (s);
return (0);
}
ssize_t
-iobuf_write_ssl(struct iobuf *io, void *ssl)
+iobuf_write_tls(struct iobuf *io, void *tls)
{
struct ioqbuf *q;
int r;
ssize_t n;
q = io->outq;
- n = SSL_write(ssl, q->buf + q->rpos, q->wpos - q->rpos);
+ n = SSL_write(tls, q->buf + q->rpos, q->wpos - q->rpos);
if (n <= 0) {
- switch ((r = SSL_get_error(ssl, n))) {
+ switch ((r = SSL_get_error(tls, n))) {
case SSL_ERROR_WANT_READ:
return (IOBUF_WANT_READ);
case SSL_ERROR_WANT_WRITE:
return (IOBUF_CLOSED);
case SSL_ERROR_SYSCALL:
if (ERR_peek_last_error())
- return (IOBUF_SSLERROR);
+ return (IOBUF_TLSERROR);
if (r == 0)
errno = EPIPE;
return (IOBUF_ERROR);
default:
- return (IOBUF_SSLERROR);
+ return (IOBUF_TLSERROR);
}
}
iobuf_drain(io, n);
}
ssize_t
-iobuf_read_ssl(struct iobuf *io, void *ssl)
+iobuf_read_tls(struct iobuf *io, void *tls)
{
ssize_t n;
int r;
- n = SSL_read(ssl, io->buf + io->wpos, iobuf_left(io));
+ n = SSL_read(tls, io->buf + io->wpos, iobuf_left(io));
if (n < 0) {
- switch ((r = SSL_get_error(ssl, n))) {
+ switch ((r = SSL_get_error(tls, n))) {
case SSL_ERROR_WANT_READ:
return (IOBUF_WANT_READ);
case SSL_ERROR_WANT_WRITE:
return (IOBUF_WANT_WRITE);
case SSL_ERROR_SYSCALL:
if (ERR_peek_last_error())
- return (IOBUF_SSLERROR);
+ return (IOBUF_TLSERROR);
if (r == 0)
errno = EPIPE;
return (IOBUF_ERROR);
default:
- return (IOBUF_SSLERROR);
+ return (IOBUF_TLSERROR);
}
} else if (n == 0)
return (IOBUF_CLOSED);
return (n);
}
-#endif /* IO_SSL */
+#endif /* IO_TLS */
-/* $OpenBSD: ioev.c,v 1.41 2017/05/17 14:00:06 deraadt Exp $ */
+/* $OpenBSD: ioev.c,v 1.42 2019/06/12 17:42:53 eric Exp $ */
/*
* Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
*
#include "ioev.h"
#include "iobuf.h"
-#ifdef IO_SSL
+#ifdef IO_TLS
#include <openssl/err.h>
#include <openssl/ssl.h>
#endif
enum {
IO_STATE_NONE,
IO_STATE_CONNECT,
- IO_STATE_CONNECT_SSL,
- IO_STATE_ACCEPT_SSL,
+ IO_STATE_CONNECT_TLS,
+ IO_STATE_ACCEPT_TLS,
IO_STATE_UP,
IO_STATE_MAX,
int flags;
int state;
struct event ev;
- void *ssl;
+ void *tls;
const char *error; /* only valid immediately on callback */
};
void io_frame_enter(const char *, struct io *, int);
void io_frame_leave(struct io *);
-#ifdef IO_SSL
+#ifdef IO_TLS
void ssl_error(const char *); /* XXX external */
-static const char* io_ssl_error(void);
-void io_dispatch_accept_ssl(int, short, void *);
-void io_dispatch_connect_ssl(int, short, void *);
-void io_dispatch_read_ssl(int, short, void *);
-void io_dispatch_write_ssl(int, short, void *);
-void io_reload_ssl(struct io *io);
+static const char* io_tls_error(void);
+void io_dispatch_accept_tls(int, short, void *);
+void io_dispatch_connect_tls(int, short, void *);
+void io_dispatch_read_tls(int, short, void *);
+void io_dispatch_write_tls(int, short, void *);
+void io_reload_tls(struct io *io);
#endif
static struct io *current = NULL;
char ssl[128];
ssl[0] = '\0';
-#ifdef IO_SSL
- if (io->ssl) {
- (void)snprintf(ssl, sizeof ssl, " ssl=%s:%s:%d",
- SSL_get_version(io->ssl),
- SSL_get_cipher_name(io->ssl),
- SSL_get_cipher_bits(io->ssl, NULL));
+#ifdef IO_TLS
+ if (io->tls) {
+ (void)snprintf(ssl, sizeof ssl, " tls=%s:%s:%d",
+ SSL_get_version(io->tls),
+ SSL_get_cipher_name(io->tls),
+ SSL_get_cipher_bits(io->tls, NULL));
}
#endif
if (io == current)
current = NULL;
-#ifdef IO_SSL
- SSL_free(io->ssl);
- io->ssl = NULL;
+#ifdef IO_TLS
+ SSL_free(io->tls);
+ io->tls = NULL;
#endif
if (event_initialized(&io->ev))
}
void *
-io_ssl(struct io *io)
+io_tls(struct io *io)
{
- return io->ssl;
+ return io->tls;
}
int
iobuf_normalize(&io->iobuf);
-#ifdef IO_SSL
- if (io->ssl) {
- io_reload_ssl(io);
+#ifdef IO_TLS
+ if (io->tls) {
+ io_reload_tls(io);
return;
}
#endif
io_frame_leave(io);
}
-#ifdef IO_SSL
+#ifdef IO_TLS
static const char*
-io_ssl_error(void)
+io_tls_error(void)
{
static char buf[128];
unsigned long e;
return (buf);
}
- return ("No SSL error");
+ return ("No TLS error");
}
int
-io_start_tls(struct io *io, void *ssl)
+io_start_tls(struct io *io, void *tls)
{
int mode;
if (mode == 0 || mode == IO_RW)
errx(1, "io_start_tls(): full-duplex or unset");
- if (io->ssl)
- errx(1, "io_start_tls(): SSL already started");
- io->ssl = ssl;
+ if (io->tls)
+ errx(1, "io_start_tls(): TLS already started");
+ io->tls = tls;
- if (SSL_set_fd(io->ssl, io->sock) == 0) {
- ssl_error("io_start_ssl:SSL_set_fd");
+ if (SSL_set_fd(io->tls, io->sock) == 0) {
+ ssl_error("io_start_tls:SSL_set_fd");
return (-1);
}
if (mode == IO_WRITE) {
- io->state = IO_STATE_CONNECT_SSL;
- SSL_set_connect_state(io->ssl);
- io_reset(io, EV_WRITE, io_dispatch_connect_ssl);
+ io->state = IO_STATE_CONNECT_TLS;
+ SSL_set_connect_state(io->tls);
+ io_reset(io, EV_WRITE, io_dispatch_connect_tls);
} else {
- io->state = IO_STATE_ACCEPT_SSL;
- SSL_set_accept_state(io->ssl);
- io_reset(io, EV_READ, io_dispatch_accept_ssl);
+ io->state = IO_STATE_ACCEPT_TLS;
+ SSL_set_accept_state(io->tls);
+ io_reset(io, EV_READ, io_dispatch_accept_tls);
}
return (0);
}
void
-io_dispatch_accept_ssl(int fd, short event, void *humppa)
+io_dispatch_accept_tls(int fd, short event, void *humppa)
{
struct io *io = humppa;
int e, ret;
- io_frame_enter("io_dispatch_accept_ssl", io, event);
+ io_frame_enter("io_dispatch_accept_tls", io, event);
if (event == EV_TIMEOUT) {
io_callback(io, IO_TIMEOUT);
goto leave;
}
- if ((ret = SSL_accept(io->ssl)) > 0) {
+ if ((ret = SSL_accept(io->tls)) > 0) {
io->state = IO_STATE_UP;
io_callback(io, IO_TLSREADY);
goto leave;
}
- switch ((e = SSL_get_error(io->ssl, ret))) {
+ switch ((e = SSL_get_error(io->tls, ret))) {
case SSL_ERROR_WANT_READ:
- io_reset(io, EV_READ, io_dispatch_accept_ssl);
+ io_reset(io, EV_READ, io_dispatch_accept_tls);
break;
case SSL_ERROR_WANT_WRITE:
- io_reset(io, EV_WRITE, io_dispatch_accept_ssl);
+ io_reset(io, EV_WRITE, io_dispatch_accept_tls);
break;
default:
- io->error = io_ssl_error();
- ssl_error("io_dispatch_accept_ssl:SSL_accept");
+ io->error = io_tls_error();
+ ssl_error("io_dispatch_accept_tls:SSL_accept");
io_callback(io, IO_ERROR);
break;
}
}
void
-io_dispatch_connect_ssl(int fd, short event, void *humppa)
+io_dispatch_connect_tls(int fd, short event, void *humppa)
{
struct io *io = humppa;
int e, ret;
- io_frame_enter("io_dispatch_connect_ssl", io, event);
+ io_frame_enter("io_dispatch_connect_tls", io, event);
if (event == EV_TIMEOUT) {
io_callback(io, IO_TIMEOUT);
goto leave;
}
- if ((ret = SSL_connect(io->ssl)) > 0) {
+ if ((ret = SSL_connect(io->tls)) > 0) {
io->state = IO_STATE_UP;
io_callback(io, IO_TLSREADY);
goto leave;
}
- switch ((e = SSL_get_error(io->ssl, ret))) {
+ switch ((e = SSL_get_error(io->tls, ret))) {
case SSL_ERROR_WANT_READ:
- io_reset(io, EV_READ, io_dispatch_connect_ssl);
+ io_reset(io, EV_READ, io_dispatch_connect_tls);
break;
case SSL_ERROR_WANT_WRITE:
- io_reset(io, EV_WRITE, io_dispatch_connect_ssl);
+ io_reset(io, EV_WRITE, io_dispatch_connect_tls);
break;
default:
- io->error = io_ssl_error();
+ io->error = io_tls_error();
ssl_error("io_dispatch_connect_ssl:SSL_connect");
io_callback(io, IO_TLSERROR);
break;
}
void
-io_dispatch_read_ssl(int fd, short event, void *humppa)
+io_dispatch_read_tls(int fd, short event, void *humppa)
{
struct io *io = humppa;
int n, saved_errno;
- io_frame_enter("io_dispatch_read_ssl", io, event);
+ io_frame_enter("io_dispatch_read_tls", io, event);
if (event == EV_TIMEOUT) {
io_callback(io, IO_TIMEOUT);
again:
iobuf_normalize(&io->iobuf);
- switch ((n = iobuf_read_ssl(&io->iobuf, (SSL*)io->ssl))) {
+ switch ((n = iobuf_read_tls(&io->iobuf, (SSL*)io->tls))) {
case IOBUF_WANT_READ:
- io_reset(io, EV_READ, io_dispatch_read_ssl);
+ io_reset(io, EV_READ, io_dispatch_read_tls);
break;
case IOBUF_WANT_WRITE:
- io_reset(io, EV_WRITE, io_dispatch_read_ssl);
+ io_reset(io, EV_WRITE, io_dispatch_read_tls);
break;
case IOBUF_CLOSED:
io_callback(io, IO_DISCONNECTED);
errno = saved_errno;
io_callback(io, IO_ERROR);
break;
- case IOBUF_SSLERROR:
- io->error = io_ssl_error();
- ssl_error("io_dispatch_read_ssl:SSL_read");
+ case IOBUF_TLSERROR:
+ io->error = io_tls_error();
+ ssl_error("io_dispatch_read_tls:SSL_read");
io_callback(io, IO_ERROR);
break;
default:
- io_debug("io_dispatch_read_ssl(...) -> r=%d\n", n);
+ io_debug("io_dispatch_read_tls(...) -> r=%d\n", n);
io_callback(io, IO_DATAIN);
- if (current == io && IO_READING(io) && SSL_pending(io->ssl))
+ if (current == io && IO_READING(io) && SSL_pending(io->tls))
goto again;
}
}
void
-io_dispatch_write_ssl(int fd, short event, void *humppa)
+io_dispatch_write_tls(int fd, short event, void *humppa)
{
struct io *io = humppa;
int n, saved_errno;
size_t w2, w;
- io_frame_enter("io_dispatch_write_ssl", io, event);
+ io_frame_enter("io_dispatch_write_tls", io, event);
if (event == EV_TIMEOUT) {
io_callback(io, IO_TIMEOUT);
}
w = io_queued(io);
- switch ((n = iobuf_write_ssl(&io->iobuf, (SSL*)io->ssl))) {
+ switch ((n = iobuf_write_tls(&io->iobuf, (SSL*)io->tls))) {
case IOBUF_WANT_READ:
- io_reset(io, EV_READ, io_dispatch_write_ssl);
+ io_reset(io, EV_READ, io_dispatch_write_tls);
break;
case IOBUF_WANT_WRITE:
- io_reset(io, EV_WRITE, io_dispatch_write_ssl);
+ io_reset(io, EV_WRITE, io_dispatch_write_tls);
break;
case IOBUF_CLOSED:
io_callback(io, IO_DISCONNECTED);
errno = saved_errno;
io_callback(io, IO_ERROR);
break;
- case IOBUF_SSLERROR:
- io->error = io_ssl_error();
- ssl_error("io_dispatch_write_ssl:SSL_write");
+ case IOBUF_TLSERROR:
+ io->error = io_tls_error();
+ ssl_error("io_dispatch_write_tls:SSL_write");
io_callback(io, IO_ERROR);
break;
default:
- io_debug("io_dispatch_write_ssl(...) -> w=%d\n", n);
+ io_debug("io_dispatch_write_tls(...) -> w=%d\n", n);
w2 = io_queued(io);
if (w > io->lowat && w2 <= io->lowat)
io_callback(io, IO_LOWAT);
}
void
-io_reload_ssl(struct io *io)
+io_reload_tls(struct io *io)
{
short ev = 0;
void (*dispatch)(int, short, void*) = NULL;
switch (io->state) {
- case IO_STATE_CONNECT_SSL:
+ case IO_STATE_CONNECT_TLS:
ev = EV_WRITE;
- dispatch = io_dispatch_connect_ssl;
+ dispatch = io_dispatch_connect_tls;
break;
- case IO_STATE_ACCEPT_SSL:
+ case IO_STATE_ACCEPT_TLS:
ev = EV_READ;
- dispatch = io_dispatch_accept_ssl;
+ dispatch = io_dispatch_accept_tls;
break;
case IO_STATE_UP:
ev = 0;
if (IO_READING(io) && !(io->flags & IO_PAUSE_IN)) {
ev = EV_READ;
- dispatch = io_dispatch_read_ssl;
+ dispatch = io_dispatch_read_tls;
}
else if (IO_WRITING(io) && !(io->flags & IO_PAUSE_OUT) &&
io_queued(io)) {
ev = EV_WRITE;
- dispatch = io_dispatch_write_ssl;
+ dispatch = io_dispatch_write_tls;
}
if (!ev)
return; /* paused */
break;
default:
- errx(1, "io_reload_ssl(): bad state");
+ errx(1, "io_reload_tls(): bad state");
}
io_reset(io, ev, dispatch);
}
-#endif /* IO_SSL */
+#endif /* IO_TLS */