3proxy/src/ssl.h
Vladimir Dubrovin 4edb94d09e Read TLS records ahead with OpenSSL 1.1.0 and later
Read-ahead was off, so OpenSSL read every record in two calls, its
header and then its body. It was off because ssl_poll() asks
ssl_pending(), and SSL_pending() does not count data read ahead and not
yet decrypted: poll() would wait while data waits in OpenSSL.
SSL_has_pending() does count it. With OpenSSL 1.1.0 and later, which
have it, ssl_pending() uses it and records are read ahead; with older
ones and wolfSSL nothing changes.

SSL_has_pending() is also true when only a part of a record has been
read ahead, which is no data yet: poll() would then return at once, over
and over and without its timeout, until the rest came. ssl_pending()
says so only when decrypted data waits, or when what was read ahead
makes a whole record (SSL_peek() of one byte, the sockets not blocking);
otherwise the socket is polled for the rest.

Decrypting TLS streams takes 9-14% less time for the same data (ARM).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 12:04:08 +03:00

108 lines
2.5 KiB
C

#ifndef __ssl_h__
#define __ssl_h__
//
// opaque connection structure
//
typedef void *SSL_CONN;
//
// opaque certificate structure
//
typedef void *SSL_CERT;
typedef struct _ssl_conn {
SSL_CTX *ctx;
SSL *ssl;
} ssl_conn;
struct alpn {
unsigned char *protos;
unsigned int protos_len;
};
struct ssl_config {
X509 *CA_cert;
X509 *server_cert;
X509 *client_cert;
EVP_PKEY *CA_key;
EVP_PKEY *server_key;
EVP_PKEY *client_key;
SSL_CTX *cli_ctx;
SSL_CTX *srv_ctx;
char *certcache;
char * client_ciphersuites;
char * server_ciphersuites;
char * client_cipher_list;
char * server_cipher_list;
char * client_ca_file;
char * client_ca_dir;
char * client_ca_store;
char * server_ca_file;
char * server_ca_dir;
char * server_ca_store;
char * client_sni;
struct alpn client_alpn_protos;
int mitm;
int serv;
int cli;
int client_min_proto_version;
int client_max_proto_version;
int server_min_proto_version;
int server_max_proto_version;
int client_verify;
int server_verify;
int client_mode;
};
typedef struct ssl_config SSL_CONFIG;
//
// Create copy of certificate signed by "other" CA
//
SSL_CERT ssl_copy_cert(SSL_CERT cert, SSL_CONFIG *config);
//
// SSL/TLS handshakes
//
SSL_CTX * ssl_cli_ctx(SSL_CONFIG *config, X509 *server_cert, EVP_PKEY *server_key,char** errSSL);
SSL_CONN ssl_handshake_to_client(SOCKET s, SSL_CONFIG *config, X509 *server_cert, EVP_PKEY *server_key, char **errSSL);
SSL_CONN ssl_handshake_to_server(SOCKET s, char * hostname, SSL_CONFIG *config, SSL_CERT *server_cert, char **errSSL);
//
// SSL/TLS Read/Write
//
int ssl_read(SSL_CONN connection, void * buf, int bufsize);
int ssl_write(SSL_CONN connection, void * buf, int bufsize);
int ssl_pending(SSL_CONN connection);
/* TLS records are read ahead, a buffer at a time rather than two reads for
each record, where what is read ahead and not yet decrypted shows as
pending (SSL_has_pending(), OpenSSL 1.1.0 and later): poll() would wait
on it otherwise. */
#if !defined(WITH_WOLFSSL) && defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
#define SSL_READAHEAD 1
#else
#define SSL_READAHEAD 0
#endif
//
// Release of opaque structures
//
void ssl_conn_free(SSL_CONN connection);
void _ssl_cert_free(SSL_CERT cert);
//
// Global (de)initialization
//
void ssl_init(void);
char * getSSLErr(void);
//
// Built-in SSL installation (called from 3proxy.c)
//
void ssl_install(void);
extern struct sockfuncs sso;
#endif // __ssl_h__