2 7a554149 2024-02-18 op * This is free and unencumbered software released into the public domain.
4 7a554149 2024-02-18 op * Anyone is free to copy, modify, publish, use, compile, sell, or
5 7a554149 2024-02-18 op * distribute this software, either in source code form or as a compiled
6 7a554149 2024-02-18 op * binary, for any purpose, commercial or non-commercial, and by any
9 7a554149 2024-02-18 op * In jurisdictions that recognize copyright laws, the author or authors
10 7a554149 2024-02-18 op * of this software dedicate any and all copyright interest in the
11 7a554149 2024-02-18 op * software to the public domain. We make this dedication for the benefit
12 7a554149 2024-02-18 op * of the public at large and to the detriment of our heirs and
13 7a554149 2024-02-18 op * successors. We intend this dedication to be an overt act of
14 7a554149 2024-02-18 op * relinquishment in perpetuity of all present and future rights to this
15 7a554149 2024-02-18 op * software under copyright law.
17 7a554149 2024-02-18 op * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 7a554149 2024-02-18 op * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 7a554149 2024-02-18 op * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 7a554149 2024-02-18 op * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 7a554149 2024-02-18 op * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 7a554149 2024-02-18 op * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 7a554149 2024-02-18 op * OTHER DEALINGS IN THE SOFTWARE.
26 7a554149 2024-02-18 op #include "compat.h"
28 7a554149 2024-02-18 op #include <assert.h>
29 7a554149 2024-02-18 op #include <errno.h>
30 7a554149 2024-02-18 op #include <stdarg.h>
31 7a554149 2024-02-18 op #include <stdint.h>
32 7a554149 2024-02-18 op #include <stdio.h>
33 7a554149 2024-02-18 op #include <stdlib.h>
34 7a554149 2024-02-18 op #include <string.h>
35 7a554149 2024-02-18 op #include <tls.h>
36 7a554149 2024-02-18 op #include <unistd.h>
38 7a554149 2024-02-18 op #include "bufio.h"
41 7a554149 2024-02-18 op buf_init(struct buf *buf)
43 7a554149 2024-02-18 op const size_t cap = BIO_CHUNK;
45 7a554149 2024-02-18 op memset(buf, 0, sizeof(*buf));
46 7a554149 2024-02-18 op if ((buf->buf = malloc(cap)) == NULL)
48 7a554149 2024-02-18 op buf->cap = cap;
53 7a554149 2024-02-18 op buf_grow(struct buf *buf)
55 7a554149 2024-02-18 op size_t newcap;
58 7a554149 2024-02-18 op newcap = buf->cap + BIO_CHUNK;
59 7a554149 2024-02-18 op t = realloc(buf->buf, newcap);
60 7a554149 2024-02-18 op if (t == NULL)
63 7a554149 2024-02-18 op buf->cap = newcap;
68 7a554149 2024-02-18 op buf_has_line(struct buf *buf, const char *nl)
70 7a554149 2024-02-18 op return (memmem(buf->buf, buf->len, nl, strlen(nl)) != NULL);
74 7a554149 2024-02-18 op buf_drain(struct buf *buf, size_t l)
78 7a554149 2024-02-18 op if (l >= buf->len) {
83 7a554149 2024-02-18 op memmove(buf->buf, buf->buf + l, buf->len - l);
84 7a554149 2024-02-18 op buf->len -= l;
88 7a554149 2024-02-18 op buf_drain_line(struct buf *buf, const char *nl)
90 7a554149 2024-02-18 op uint8_t *endln;
93 7a554149 2024-02-18 op nlen = strlen(nl);
94 7a554149 2024-02-18 op if ((endln = memmem(buf->buf, buf->len, nl, nlen)) == NULL)
96 7a554149 2024-02-18 op buf_drain(buf, endln + nlen - buf->buf);
100 7a554149 2024-02-18 op buf_free(struct buf *buf)
102 7a554149 2024-02-18 op free(buf->buf);
103 7a554149 2024-02-18 op memset(buf, 0, sizeof(*buf));
107 7a554149 2024-02-18 op bufio_init(struct bufio *bio)
109 7a554149 2024-02-18 op memset(bio, 0, sizeof(*bio));
110 7a554149 2024-02-18 op bio->fd = -1;
112 7a554149 2024-02-18 op if (buf_init(&bio->wbuf) == -1)
114 7a554149 2024-02-18 op if (buf_init(&bio->rbuf) == -1) {
115 7a554149 2024-02-18 op buf_free(&bio->wbuf);
122 0d6af421 2024-02-18 op bufio_free(struct bufio *bio)
124 7a554149 2024-02-18 op if (bio->ctx)
125 0d6af421 2024-02-18 op tls_free(bio->ctx);
126 0d6af421 2024-02-18 op bio->ctx = NULL;
128 7a554149 2024-02-18 op if (bio->fd != -1)
129 7a554149 2024-02-18 op close(bio->fd);
130 0d6af421 2024-02-18 op bio->fd = -1;
132 7a554149 2024-02-18 op buf_free(&bio->rbuf);
133 7a554149 2024-02-18 op buf_free(&bio->wbuf);
137 8b00d570 2024-02-18 op bufio_close(struct bufio *bio)
139 8b00d570 2024-02-18 op if (bio->ctx == NULL)
142 8b00d570 2024-02-18 op switch (tls_close(bio->ctx)) {
145 8b00d570 2024-02-18 op case TLS_WANT_POLLIN:
146 ce023433 2024-02-22 op errno = EAGAIN;
147 ce023433 2024-02-22 op bio->pflags = BUFIO_WANT_READ;
149 8b00d570 2024-02-18 op case TLS_WANT_POLLOUT:
150 8b00d570 2024-02-18 op errno = EAGAIN;
151 ce023433 2024-02-22 op bio->pflags = BUFIO_WANT_WRITE;
159 0d6af421 2024-02-18 op bufio_reset(struct bufio *bio)
161 0d6af421 2024-02-18 op bufio_free(bio);
162 7a554149 2024-02-18 op return (bufio_init(bio));
166 7a554149 2024-02-18 op bufio_set_fd(struct bufio *bio, int fd)
168 7a554149 2024-02-18 op bio->fd = fd;
172 9c5586a7 2024-02-18 op bufio_starttls(struct bufio *bio, const char *host, int insecure,
173 9c5586a7 2024-02-18 op const uint8_t *cert, size_t certlen, const uint8_t *key, size_t keylen)
175 7a554149 2024-02-18 op struct tls_config *conf;
177 7a554149 2024-02-18 op if ((conf = tls_config_new()) == NULL)
180 7a554149 2024-02-18 op if (insecure) {
181 7a554149 2024-02-18 op tls_config_insecure_noverifycert(conf);
182 7a554149 2024-02-18 op tls_config_insecure_noverifyname(conf);
183 7a554149 2024-02-18 op tls_config_insecure_noverifytime(conf);
186 9c5586a7 2024-02-18 op if (cert && tls_config_set_keypair_mem(conf, cert, certlen,
187 9c5586a7 2024-02-18 op key, keylen) == -1) {
188 9c5586a7 2024-02-18 op tls_config_free(conf);
192 7a554149 2024-02-18 op if ((bio->ctx = tls_client()) == NULL) {
193 7a554149 2024-02-18 op tls_config_free(conf);
197 7a554149 2024-02-18 op if (tls_configure(bio->ctx, conf) == -1) {
198 7a554149 2024-02-18 op tls_config_free(conf);
202 7a554149 2024-02-18 op tls_config_free(conf);
204 7a554149 2024-02-18 op if (tls_connect_socket(bio->ctx, bio->fd, host) == -1)
211 e3693ed9 2024-02-18 op bufio_ev(struct bufio *bio)
215 7a554149 2024-02-18 op if (bio->pflags)
216 7a554149 2024-02-18 op return (bio->pflags);
218 ce023433 2024-02-22 op ev = BUFIO_WANT_READ;
219 7a554149 2024-02-18 op if (bio->wbuf.len != 0)
220 ce023433 2024-02-22 op ev |= BUFIO_WANT_WRITE;
226 0e0994b2 2024-02-18 op bufio_handshake(struct bufio *bio)
228 0e0994b2 2024-02-18 op if (bio->ctx == NULL) {
229 0e0994b2 2024-02-18 op errno = EINVAL;
233 0e0994b2 2024-02-18 op switch (tls_handshake(bio->ctx)) {
236 0e0994b2 2024-02-18 op case TLS_WANT_POLLIN:
237 ce023433 2024-02-22 op errno = EAGAIN;
238 ce023433 2024-02-22 op bio->pflags = BUFIO_WANT_READ;
240 0e0994b2 2024-02-18 op case TLS_WANT_POLLOUT:
241 0e0994b2 2024-02-18 op errno = EAGAIN;
242 ce023433 2024-02-22 op bio->pflags = BUFIO_WANT_WRITE;
250 7a554149 2024-02-18 op bufio_read(struct bufio *bio)
252 7a554149 2024-02-18 op struct buf *rbuf = &bio->rbuf;
255 7a554149 2024-02-18 op assert(rbuf->cap >= rbuf->len);
256 7a554149 2024-02-18 op if (rbuf->cap - rbuf->len < BIO_CHUNK) {
257 7a554149 2024-02-18 op if (buf_grow(rbuf) == -1)
261 7a554149 2024-02-18 op if (bio->ctx) {
262 7a554149 2024-02-18 op r = tls_read(bio->ctx, rbuf->buf + rbuf->len,
263 e634cfa6 2024-02-18 op rbuf->cap - rbuf->len);
265 7a554149 2024-02-18 op case TLS_WANT_POLLIN:
266 ce023433 2024-02-22 op errno = EAGAIN;
267 ce023433 2024-02-22 op bio->pflags = BUFIO_WANT_READ;
269 7a554149 2024-02-18 op case TLS_WANT_POLLOUT:
270 7a554149 2024-02-18 op errno = EAGAIN;
271 ce023433 2024-02-22 op bio->pflags = BUFIO_WANT_WRITE;
276 7a554149 2024-02-18 op bio->pflags = 0;
277 7a554149 2024-02-18 op rbuf->len += r;
282 7a554149 2024-02-18 op r = read(bio->fd, rbuf->buf + rbuf->len, rbuf->cap - rbuf->len);
285 7a554149 2024-02-18 op rbuf->len += r;
290 b19b8dbc 2024-02-18 op bufio_drain(struct bufio *bio, void *d, size_t len)
292 b19b8dbc 2024-02-18 op struct buf *rbuf = &bio->rbuf;
294 b19b8dbc 2024-02-18 op if (len > rbuf->len)
295 b19b8dbc 2024-02-18 op len = rbuf->len;
296 b19b8dbc 2024-02-18 op memcpy(d, rbuf->buf, len);
297 b19b8dbc 2024-02-18 op buf_drain(rbuf, len);
298 b19b8dbc 2024-02-18 op return (len);
302 7a554149 2024-02-18 op bufio_write(struct bufio *bio)
304 7a554149 2024-02-18 op struct buf *wbuf = &bio->wbuf;
307 7a554149 2024-02-18 op if (bio->ctx) {
308 7a554149 2024-02-18 op switch (w = tls_write(bio->ctx, wbuf->buf, wbuf->len)) {
309 7a554149 2024-02-18 op case TLS_WANT_POLLIN:
310 ce023433 2024-02-22 op errno = EAGAIN;
311 ce023433 2024-02-22 op bio->pflags = BUFIO_WANT_READ;
313 7a554149 2024-02-18 op case TLS_WANT_POLLOUT:
314 7a554149 2024-02-18 op errno = EAGAIN;
315 ce023433 2024-02-22 op bio->pflags = BUFIO_WANT_WRITE;
320 7a554149 2024-02-18 op bio->pflags = 0;
321 7a554149 2024-02-18 op buf_drain(wbuf, w);
326 7a554149 2024-02-18 op w = write(bio->fd, wbuf->buf, wbuf->len);
329 7a554149 2024-02-18 op buf_drain(wbuf, w);
334 7a554149 2024-02-18 op bufio_compose(struct bufio *bio, const void *d, size_t len)
336 7a554149 2024-02-18 op struct buf *wbuf = &bio->wbuf;
338 7a554149 2024-02-18 op while (wbuf->cap - wbuf->len < len) {
339 7a554149 2024-02-18 op if (buf_grow(wbuf) == -1)
343 7a554149 2024-02-18 op memcpy(wbuf->buf + wbuf->len, d, len);
344 7a554149 2024-02-18 op wbuf->len += len;
349 7a554149 2024-02-18 op bufio_compose_str(struct bufio *bio, const char *str)
351 7a554149 2024-02-18 op return (bufio_compose(bio, str, strlen(str)));
355 7a554149 2024-02-18 op bufio_compose_fmt(struct bufio *bio, const char *fmt, ...)
361 7a554149 2024-02-18 op va_start(ap, fmt);
362 7a554149 2024-02-18 op r = vasprintf(&str, fmt, ap);
367 7a554149 2024-02-18 op r = bufio_compose(bio, str, r);
373 7a554149 2024-02-18 op bufio_rewind_cursor(struct bufio *bio)
375 7a554149 2024-02-18 op bio->rbuf.cur = 0;
379 7a554149 2024-02-18 op bufio_get_cb(void *d)
381 7a554149 2024-02-18 op struct bufio *bio = d;
382 7a554149 2024-02-18 op struct buf *rbuf = &bio->rbuf;
384 7a554149 2024-02-18 op if (rbuf->cur >= rbuf->len)
385 7a554149 2024-02-18 op return (EOF);
386 7a554149 2024-02-18 op return (rbuf->buf[rbuf->cur++]);
390 7a554149 2024-02-18 op bufio_peek_cb(void *d)
392 7a554149 2024-02-18 op struct bufio *bio = d;
393 7a554149 2024-02-18 op struct buf *rbuf = &bio->rbuf;
395 7a554149 2024-02-18 op if (rbuf->cur >= rbuf->len)
396 7a554149 2024-02-18 op return (EOF);
397 7a554149 2024-02-18 op return (rbuf->buf[rbuf->cur]);