|
SharkSSL C/C++ Reference
TLS, cryptography, certificates, and PSA integration
|
A successful encrypted connection is useful only when it reaches the peer your application expects. For a client, load the intended CA trust, enable SSL_VERIFY_PEER, and set the expected server name before the handshake. Server Name Indication (SNI) selects a server identity; it does not verify one.
The adapter's default is SSL_VERIFY_NONE. Follow connection setup for the call sequence and mutual TLS to authenticate clients. The policies below describe this adapter's behavior, including its differences from OpenSSL.
The default is SSL_VERIFY_NONE. A client may select SSL_VERIFY_PEER; a server may select PEER, optionally combined with FAIL_IF_NO_PEER_CERT for required authentication. See the mutual TLS contract for the single identity, missing-certificate policy and RSA signature support. To use a verification callback, compile SharkSSL and the adapter with SHARKSSL_ENABLE_CERT_VERIFY_HOOK=1. The default is zero. In the default build, pass NULL as the callback. The native connection contains one extra pointer only in the enabled build. Unsupported context verification settings prevent subsequent SSL_new calls; correcting the settings restores creation. Unsupported per-SSL settings or changes after starting I/O latch SSL_ERROR_SSL. These void APIs cannot report configuration errors directly. Context settings are copied at SSL_new. The metadata-only X509 subset exposes retained peer fields with independent reference ownership. Public trust-store objects are not implemented.
Native handshake processing performs chain signature and purpose checks. Before reporting handshake success or returning application plaintext, the wrapper checks SharkSslCon_trustedCA, native certificate dates, and any configured reference hostname. The adapter reads the native certificate metadata directly and uses sharkParseCertTime for dates.
The date policy deliberately permits notBefore up to 24 hours ahead and rejects expired certificates. Dates are checked even without a reference hostname. It inherits the native rule that skips the final root's dates in a multi-node chain. This is not strict OpenSSL X509 verification and does not add path building, revocation, CRL, OCSP, security levels, arbitrary verification flags, or constraints absent from native SharkSSL. Do not assume equivalence with OpenSSL's full X509 path validation.
SSL_set1_host copies one reference name, up to the native 253-byte limit. NULL or empty clears it. For DNS names, a DNS subject alternative name takes precedence over the common name. The common name is eligible only when the certificate has no DNS subject alternative names. Handshakes use the same DNS wildcard and leading-dot matching rules as standalone X509_check_host with flags set to zero. DNS labels in a handshake reference have a 63-byte limit. IP literals require a matching binary IP SAN. Shortened or noncanonical numeric IPv4 references fail verification.
Enable SHARKSSL_ENABLE_CERT_COMMON_NAMES throughout the build to check every common name using its encoded string type. With the default setting, the handshake can still use SharkSSL's single retained common name. A standalone check that needs typed common names in this configuration returns -1. The handshake borrows the native metadata and needs no X509 snapshot for hostname matching. Standalone DNS checks
The subset omits SSL_set1_ip and handshake hostname flags. For SNI, SSL_set_tlsext_host_name copies a client name of at most 64 bytes, or clears it with NULL/empty. Setting SNI does not enable hostname verification. Configure both before starting I/O; failed name changes preserve the previous name.
With the hook enabled, a presented chain reaches the adapter during the handshake after native chain checks. A veto under SSL_VERIFY_PEER sends a fatal bad_certificate alert through the write BIO, including normal BIO retries. A native validation failure bypasses the callback. TLS proof of private-key possession and Finished verification still run on a continued handshake. Empty client certificates follow the server's verification mode.
The callback receives the selected chain from the highest issuer to the leaf at depth zero. Use X509_STORE_CTX_get_current_cert, _get_error_depth and _get_error during the call. The current metadata-only X509 is borrowed. Use X509_up_ref to keep it. Retrieve the connection with X509_STORE_CTX_get_ex_data(store, SSL_get_ex_data_X509_STORE_CTX_idx()). This is the only supported store-context index. Callback data is synchronous. Keep the SSL alive and avoid reentering its I/O.
Returning zero rejects verification. A nonzero return on an error continues with the remaining date and hostname checks. The adapter reports a missing CA anchor as X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY. That code covers both missing issuers and untrusted self-signed peers. A waiver preserves the last error for SSL_get_verify_result unless the callback changes it with X509_STORE_CTX_set_error. A veto on a successful check with no saved error records X509_V_ERR_APPLICATION_VERIFICATION. The native CA trust flag keeps its original value. A client using SSL_VERIFY_NONE can inspect a policy failure and continue.
With the hook disabled, the adapter enforces trust, dates and hostname locally before reporting handshake success or exposing plaintext. The peer may already have completed the handshake when that check fails. In either build, zero before verification or following an earlier native failure does not establish peer identity. Close the connection after a fatal SSL result.
For public brokers, build the TLS adapter and its callers with SHARKSSL_OSSL_USE_EMBEDDED_CA_LIST=1. Link the firmware's single definition of sharkSslCAList. The shipped examples/CA-list.h defines the array, so include it in exactly one C translation unit. A firmware image that already links the array can share it with the adapter.
On an idle context, SSL_CTX_set_default_verify_paths(ctx) returns 1 after installing the compiled list through the native trusted-data API. The context borrows the static bytes for its lifetime. Setup needs no heap allocation, PEM files or environment access. Peer verification still requires SSL_VERIFY_PEER and the expected hostname. The native TLS build checks the peer's chain during the handshake. Repeated calls on an idle context succeed.
For a private broker, use Paho's explicit PEM trustStore on a fresh context. Paho then calls SSL_CTX_load_verify_locations and skips the default. Firmware without file I/O can supply the same CA bytes through SharkSslOssl_SSL_CTX_load_verify_pem. An attempt to combine compiled and PEM trust on one context returns 0 and preserves the first source. Use a fresh context when switching policies.
A hosted build with the embedded option off can expose SSL_CTX_set_default_verify_paths by selecting the PEM files component and defining a default filename in its build configuration. Use the same definitions for the adapter and the caller.
Call SSL_CTX_set_default_verify_paths(ctx) on an idle context. A return of 1 means the adapter read and validated the file and its anchors are available. A return of 0 leaves the previous trust configuration intact and queues a PEM error. Verification still requires SSL_VERIFY_PEER and the client's expected hostname. Reloading identical PEM text consumes no additional CA capacity. Successful loads add anchors. Create a fresh context to remove old anchors.
The fixed filename is optional. A hosted build can also define SHARKSSL_OSSL_TRUST_GETENV as an accessor taking a const char* name and returning a borrowed string or NULL. Use an accessor appropriate to the process's privilege model. A returned SSL_CERT_FILE value overrides the fixed filename. NULL selects the fixed filename if configured. An empty or unreadable override fails. A nonempty SSL_CERT_DIR also fails, including when a file was supplied, because the adapter cannot load the requested directory. With only a fixed filename the adapter never reads the environment. With the embedded option off, at least one hosted setting is required to expose the function.
The existing PEM limits apply, including 65,536 bytes and 16 certificate blocks per file. This interface supports a deployment's curated trust bundle. OS trust-store discovery and hashed-directory lookup remain outside the adapter.