|
SharkSSL C/C++ Reference
TLS, cryptography, certificates, and PSA integration
|
Choose this guide when an application already owns a connected socket or uses Paho or libevent. The TLS adapter leaves connection setup, timeouts and event scheduling to the application. For custom callbacks or dispatcher attachment, see BIO transports.
Enable SHARKSSL_OSSL_ENABLE_SOCKET=1 for the TLS adapter and its callers. Manual builds also compile transport/SharkSslOsslSocket.c and link ws2_32.lib on Windows. The option defaults to zero.
Connect the stream socket, choose its blocking mode and pass its descriptor to SSL_set_fd(ssl, fd) before the first handshake or record operation. Success returns 1. One socket BIO serves both directions and starts with BIO_NOCLOSE. The caller can replace it before I/O and can close the original descriptor after SSL_free. An allocation failure, a negative descriptor or an attempt to reattach after the handshake starts returns 0 and preserves the attached BIOs. On Windows, check that a valid SOCKET fits in a nonnegative int before casting it to the OpenSSL API parameter.
After the handshake, each socket read stops at the end of the current TLS header or payload. Subsequent records stay on the descriptor so they can wake a caller using select or poll. A partial header or payload can require several read retries. Handshake reads can also buffer application bytes sent with the final handshake flight. SSL_pending reports unread plaintext in the current decoded record. If the peer can send first, call SSL_read on the nonblocking connection after the handshake and keep reading while it returns data. Wait on the socket when SSL_get_error reports a retry.
On a nonblocking socket, pass a failed SSL operation's result directly to SSL_get_error. Wait for the requested read or write readiness, then retry the same operation. Socket reads and writes translate would-block and interrupted system calls into directional retries. A read returning zero marks transport EOF. Use SSL_shutdown to exchange TLS close alerts before reusing the connection for raw TCP. On POSIX systems that lack MSG_NOSIGNAL, the caller supplies its usual SIGPIPE handling.
The same option exposes socket BIOs. Use BIO_new_socket(int fd, int closeFlag) for an attached socket, or attach one to BIO_new(BIO_s_socket()) with BIO_set_fd. Replacing a descriptor or freeing the BIO closes a descriptor owned with BIO_CLOSE. Use BIO_NOCLOSE to retain ownership. For an SSL object, SSL_get_fd and SSL_get_rfd return the read descriptor and SSL_get_wfd returns the write descriptor. An absent descriptor returns -1. SSL_peek and SSL_peek_ex use the normal read retry contract and leave the returned plaintext available to the next peek or read.
To use libevent 2.1.12 with the adapter, enable the hosted socket option and include SharkSslOsslLibevent.h before libevent's OpenSSL headers. When compiling libevent's bufferevent_openssl.c, also define SHARKSSL_OSSL_LIBEVENT_TRANSPORT. Its SSL_renegotiate(ssl) call macro translates failure to the negative return value this version of libevent checks. Application calls to SSL_renegotiate return zero and queue an unsupported-operation error.
Both the socket and filter constructors accept a fresh, configured SSL*. SSL_clear supports libevent's pre-handshake initialization. Allocate another SSL* to reconnect after I/O has started. Verification-mode and callback getters report the policy on the supplied context or connection. The optional native verification hook must be enabled to install a verification callback. libevent transport source
Define SHARKSSL_OSSL_ENABLE_TRACE_STUBS=1 consistently for the adapter and the consumer to expose SSL_CTX_set_msg_callback and SSL_CTX_set_info_callback. Both take an SSL_CTX* and a callback with the OpenSSL signature. Both return void and accept a NULL callback. The setters discard the pointer immediately. A registered callback receives zero events, including during a failed handshake. This mode adds no callback storage to a connection or its context. Applications that use an info callback for policy or require a handshake transcript need real callbacks.
For Eclipse Paho MQTT C 1.3.14, include SharkSslOsslPaho.h when compiling the Paho client. This opt-in header selects Paho's modern hostname and ALPN paths. Build the adapter with the TLS, PEM file and EVP hash components, hosted sockets, default verification paths and the trace switch above. Seed SharkSSL before starting Paho or its worker threads. Choose the embedded CA list or an explicit trust file for the brokers your application connects to.
The Paho session, ex-data lookup and password-registration shims are function-like macros. Call them directly. Code that needs a function pointer can wrap the call in a function with the required signature.
| Optional Paho operation | Behavior in this profile |
|---|---|
| TLS message and state tracing | Both setters discard the callback. Paho can still log its own MQTT operations and TLS errors. |
| Legacy initialization and locking | Algorithms and error strings are statically available. The obsolete locking callbacks are ignored as in modern OpenSSL. |
| TLS session reuse | Session lookup returns NULL. Paho's synchronous client completes a fresh TLS handshake and can recover an MQTT 3.1.1 session with cleansession=0. |
| Password callbacks | Registration is ignored. The credential loader rejects encrypted private keys. Use an unencrypted key file in this profile. |
| Application ex-data and external PSK | Index allocation returns -1, lookup returns NULL, and Paho is compiled with OPENSSL_NO_PSK. Use certificate-based authentication. |
For secure WebSocket transport, include Paho's portable SHA-1 and Base64 helpers for its HTTP upgrade. MQTT session recovery and TLS session reuse are separate: this profile starts a fresh TLS handshake on reconnect.