diff options
| author | eunhhu <[email protected]> | 2026-10-01 09:10:55 +0100 |
|---|---|---|
| committer | eunhhu <[email protected]> | 2026-10-01 09:10:55 +0100 |
| commit | 5f5603ce239dce08ae2ec4b0fa828787b220493c (patch) | |
| tree | 04c77f6202f0de2ac61ccc3d58cdf54e790b9165 /link | |
| parent | 2128089d94a9a7345775e2725347713a66987039 (diff) | |
| download | pure-5f5603ce239dce08ae2ec4b0fa828787b220493c.tar.gz pure-5f5603ce239dce08ae2ec4b0fa828787b220493c.zip | |
Deliver wires on the sending loop, a step at a time (017, 2b-2)
A wire is now a sender of link/sending.h: its request goes on a
non-blocking socket as far as the socket takes it (connect, TLS
handshake, write, answer read), and its round waits for the socket or
its deadline (10 s with nothing moved). The answer is read as it comes:
the head whole, then a sized body counted down, a chunked one (few and
small) parsed again as it grows, or a body to the close. The crypto
table (version 6) takes the TLS handshake a step at a time, and reads
and writes say what they wait for. The owner pushes a wire once a turn,
as it does a link. A host whose app sends to ten links and ten wires
has 5 threads where it had 24 (none: 3).
Diffstat (limited to 'link')
| -rw-r--r-- | link/crypto-openssl.c | 44 | ||||
| -rw-r--r-- | link/crypto.c | 1 | ||||
| -rw-r--r-- | link/crypto.h | 8 | ||||
| -rw-r--r-- | link/tls.h | 15 |
4 files changed, 55 insertions, 13 deletions
diff --git a/link/crypto-openssl.c b/link/crypto-openssl.c index 96daa82..16651a2 100644 --- a/link/crypto-openssl.c +++ b/link/crypto-openssl.c @@ -7,6 +7,7 @@ #include <errno.h> #include <fcntl.h> #include <openssl/crypto.h> +#include <openssl/err.h> #include <openssl/evp.h> #include <openssl/core_names.h> #include <openssl/hpke.h> @@ -34,13 +35,13 @@ static bool tls_start(const char *ca_file) { return false; } SSL_CTX_set_verify(context, SSL_VERIFY_PEER, NULL); - SSL_CTX_set_mode(context, SSL_MODE_AUTO_RETRY); + SSL_CTX_set_mode(context, SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); tls_context = context; return true; } -/* TLS over a connected socket: the receiver's certificate must name the host - * (a DNS name, or an IP address written as one). */ +/* TLS over a connected, non-blocking socket: the receiver's certificate + * must name the host (a DNS name, or an IP address written as one). */ static PureTls *tls_open(const char *host, int fd) { SSL *ssl = SSL_new(tls_context); if (!ssl) return NULL; @@ -48,15 +49,44 @@ static PureTls *tls_open(const char *host, int fd) { bool literal = inet_pton(AF_INET, host, address) == 1 || inet_pton(AF_INET6, host, address) == 1; bool named = literal ? X509_VERIFY_PARAM_set1_ip_asc(SSL_get0_param(ssl), host) == 1 : SSL_set_tlsext_host_name(ssl, host) == 1 && SSL_set1_host(ssl, host) == 1; - if (!named || SSL_set_fd(ssl, fd) != 1 || SSL_connect(ssl) != 1 || SSL_get_verify_result(ssl) != X509_V_OK) { + if (!named || SSL_set_fd(ssl, fd) != 1) { SSL_free(ssl); return NULL; } + SSL_set_connect_state(ssl); return (PureTls *)ssl; } -static int tls_read(PureTls *tls, void *bytes, int size) { return SSL_read((SSL *)tls, bytes, size); } -static int tls_write(PureTls *tls, const void *bytes, int size) { return SSL_write((SSL *)tls, bytes, size); } +/* What a call that moved nothing waits for (link/tls.h). */ +static int wants(SSL *ssl, int result) { + switch (SSL_get_error(ssl, result)) { + case SSL_ERROR_WANT_READ: return PURE_TLS_WANTS_READ; + case SSL_ERROR_WANT_WRITE: return PURE_TLS_WANTS_WRITE; + case SSL_ERROR_ZERO_RETURN: return PURE_TLS_CLOSED; + default: return PURE_TLS_FAILED; + } +} + +static int tls_step(PureTls *tls) { + SSL *ssl = (SSL *)tls; + ERR_clear_error(); /* SSL_get_error reads the thread's queue */ + int result = SSL_do_handshake(ssl); + if (result == 1) return SSL_get_verify_result(ssl) == X509_V_OK ? 1 : PURE_TLS_FAILED; + int waits = wants(ssl, result); + return waits == PURE_TLS_CLOSED ? PURE_TLS_FAILED : waits; +} + +static int tls_read(PureTls *tls, void *bytes, int size) { + ERR_clear_error(); + int n = SSL_read((SSL *)tls, bytes, size); + return n > 0 ? n : wants((SSL *)tls, n); +} + +static int tls_write(PureTls *tls, const void *bytes, int size) { + ERR_clear_error(); + int n = SSL_write((SSL *)tls, bytes, size); + return n > 0 ? n : wants((SSL *)tls, n); +} static void tls_close(PureTls *tls) { SSL_free((SSL *)tls); } /* ---- Sealing: HPKE (RFC 9180), X25519 and HKDF-SHA256, with the AEAD the @@ -305,7 +335,7 @@ static bool aead_open(PureAead *a, const unsigned char *key, const unsigned char EVP_DecryptFinal_ex(a->open, plain + body, &last) == 1; } -const PureCrypto pure_crypto = {PURE_CRYPTO_VERSION, tls_start, tls_open, tls_read, tls_write, tls_close, +const PureCrypto pure_crypto = {PURE_CRYPTO_VERSION, tls_start, tls_open, tls_step, tls_read, tls_write, tls_close, seal_key, seal_key_free, sealer, seal, sealer_free, opener, unseal, opener_free, link_secret, link_ephemeral, link_agree, link_derive, aead, aead_seal, aead_open, aead_free}; diff --git a/link/crypto.c b/link/crypto.c index c8fb9bd..bec57c5 100644 --- a/link/crypto.c +++ b/link/crypto.c @@ -58,6 +58,7 @@ bool pure_tls_start(const char *ca_file, char *error, size_t size) { /* A session exists only once the table is loaded. */ PureTls *pure_tls_open(const char *host, int fd) { return crypto ? crypto->tls_open(host, fd) : NULL; } +int pure_tls_step(PureTls *tls) { return crypto->tls_step(tls); } int pure_tls_read(PureTls *tls, void *bytes, int size) { return crypto->tls_read(tls, bytes, size); } int pure_tls_write(PureTls *tls, const void *bytes, int size) { return crypto->tls_write(tls, bytes, size); } void pure_tls_close(PureTls *tls) { if (tls) crypto->tls_close(tls); } diff --git a/link/crypto.h b/link/crypto.h index ede5fb9..4d3fff0 100644 --- a/link/crypto.h +++ b/link/crypto.h @@ -8,15 +8,17 @@ * first time a server seals, opens a seal or connects to an https receiver * (link/crypto.c): one that does none of these maps no OpenSSL, 2.3 MiB * less resident and 0.6 MiB less of its own. */ -enum { PURE_CRYPTO_VERSION = 5 }; +enum { PURE_CRYPTO_VERSION = 6 }; typedef struct { unsigned version; /* TLS client: the context (verifying against ca_file, or the system's - * CAs when NULL), sessions over connected sockets, their reads and - * writes (as SSL_read and SSL_write: bytes, or <= 0), and their end. */ + * CAs when NULL), sessions over connected non-blocking sockets, their + * handshakes' steps, reads and writes (as link/tls.h says), and their + * end. */ bool (*tls_start)(const char *ca_file); PureTls *(*tls_open)(const char *host, int fd); + int (*tls_step)(PureTls *); int (*tls_read)(PureTls *, void *bytes, int size); int (*tls_write)(PureTls *, const void *bytes, int size); void (*tls_close)(PureTls *); @@ -8,10 +8,19 @@ typedef struct PureTls PureTls; /* The context, made once; false with why in error when it cannot be. */ bool pure_tls_start(const char *ca_file, char *error, size_t size); -/* A session over a connected socket to host (a DNS name, or an IP address - * written as one), its certificate verified; NULL when it fails. */ +/* A session over a connected, non-blocking socket to host (a DNS name, or + * an IP address written as one); NULL when it cannot be. Its handshake is + * made a step at a time, as the socket is ready, by pure_tls_step. */ PureTls *pure_tls_open(const char *host, int fd); -/* As SSL_read and SSL_write: the bytes moved, or <= 0. */ +/* What a step, a read or a write says when it moved no bytes: the session + * ended (the peer closed it), failed, or waits for the socket to be + * readable or writable (then the same again). */ +enum { PURE_TLS_CLOSED = 0, PURE_TLS_FAILED = -1, PURE_TLS_WANTS_READ = -2, PURE_TLS_WANTS_WRITE = -3 }; +/* The handshake: 1 once it is made and the receiver's certificate + * verified, else what it waits for, or PURE_TLS_FAILED. */ +int pure_tls_step(PureTls *); +/* As SSL_read and SSL_write: the bytes moved (a write may move part), or + * one of the above. */ int pure_tls_read(PureTls *, void *bytes, int size); int pure_tls_write(PureTls *, const void *bytes, int size); void pure_tls_close(PureTls *); |
