|
SharkSSL C/C++ Reference
TLS, cryptography, certificates, and PSA integration
|
The ERR subset provides a bounded queue for each calling thread. Include <openssl/err.h> and link src/SharkSslOsslErr.c with the BIO, TLS or cryptography wrappers. It needs no native SharkSSL, dispatcher, allocator or locking dependency. All function exports retain the SharkSslOssl_ prefix.
| Interface | Behavior |
|---|---|
ERR_get_error | Removes and returns the oldest code, or zero when empty |
ERR_peek_error | Returns the oldest code without removal |
ERR_peek_last_error | Returns the newest code without removal |
ERR_clear_error | Clears the calling thread's queue |
ERR_set_mark | Marks the newest error. Returns 1 on success or 0 for an empty queue or a full mark counter. |
ERR_pop_to_mark | Removes newer errors and one mark, preserving the marked error. Returns 1 on success. Without a mark it empties the queue and returns 0. |
ERR_clear_last_mark | Removes the most recent mark and keeps every error. Returns 1 when it finds a mark, otherwise 0. |
ERR_raise | Appends a library/reason code, without source-location metadata |
ERR_GET_LIB, ERR_GET_REASON | Extract the library and reason from a supported code |
ERR_lib_error_string, ERR_reason_error_string | Borrow immutable strings, or return NULL for an unknown code |
ERR_error_string_n | Formats into caller storage, truncating with a final NUL when size is nonzero |
ERR_print_errors_cb | Delivers one newline-terminated message per entry in queue order. A nonpositive callback return stops after consuming that entry. |
ERR_print_errors | Writes the messages to a BIO when the BIO component is enabled. |
ERR_print_errors_fp | Writes to a caller-owned FILE* when the hosted PEM file component is enabled. |
Each printed line contains the numeric code, library and reason available in the adapter queue. File, line and thread identifiers are outside its stored metadata. The callback borrows its line for the duration of the call.
Clear the queue before the operation whose errors you want to inspect. Success does not clear earlier errors. Drain it after a failure; strings/getters do not consume entries. Retry results and a normal TLS close do not add entries.
This helper takes a live client SSL* with its transport and verification configured, plus a required int* want. It returns 1 when connected, 0 when the event loop must wait, and -1 on failure. On a zero return, *want contains SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE; otherwise it is zero.
Use SSL_get_error immediately after an SSL I/O result to distinguish retry, clean shutdown and failure. The adapter keeps that classification in the SSL object; stale queue entries do not change it. Queue reads, clearing and formatting also leave the SSL object's last result unchanged. The first fatal transition detected by the I/O layer adds a diagnostic. Repeatedly using a failed connection does not add the same error again. Calls must still follow the documented same-thread, serialized SSL contract.
By default, the queue holds eight codes. SHARKSSL_OSSL_ERR_CAPACITY may be 1..64 and must have the same value in every translation unit using the state type. A full queue discards its oldest entry when a new code arrives. No error operation allocates memory, and queue storage remains available after an allocation failure. This bounded overflow policy and capacity are explicit subset limits, not a promise of OpenSSL's internal queue size.
Marks can nest on the same error. Each slot has one byte for up to UCHAR_MAX outstanding marks. Reading or evicting the marked error also discards its marks. Compile all users of SharkSslOsslErrorState against the same header and capacity setting.
MSVC Windows builds use compiler thread-local storage. Linux GCC/Clang builds use compiler thread-local storage. State starts empty for each new thread and needs no heap cleanup at thread exit. These defaults are selected by the target/compiler macros, not by the native SharkSSL transport port.
For other environments, the application port header named by SHARKSSL_OSSL_PORT_HEADER must define SHARKSSL_OSSL_ERR_STATE() to return a non-NULL SharkSslOsslErrorState* for the calling thread. The state type is in <SharkSslOsslErrPort.h>. Declare the accessor in the port header using struct SharkSslOsslErrorState*: allocator/atomic code can include that header before the typedef is visible. Define the accessor and allocate its state after including the complete type. Initialize the entire state to zero before first use and before assigning recycled storage to a new thread. The accessor must not call ERR functions recursively, allocate memory, or return shared mutable storage to concurrent threads. Keep the returned storage live throughout the call.
An explicitly single-thread program may instead define SHARKSSL_OSSL_ERR_SINGLE_THREAD; this selects one static queue. It is not safe for concurrent tasks, interrupt handlers or reentrant signal handlers. Defining both this option and a storage hook fails compilation. Unsupported targets without either choice also fail compilation.
The supported producers are:
1000 + alert description. The same description can originate locally or from the peer. Common alerts have strings; every code retains its numeric value in formatted output.SHARKSSL_OSSL_R_OPERATION_FAILED reason.ERR_R_MALLOC_FAILURE.The detecting layer adds an entry; its callers propagate the result without adding the same diagnostic again. This does not compare the newest queue code to detect duplicates: the same failure can legitimately occur twice, and a full queue can replace an entry without changing its count. Success and cleanup preserve earlier entries. A recoverable short HMAC output buffer reports an argument/state entry while leaving the operation available for another call.
These returns intentionally remain quiet:
| Return family | Diagnostic policy |
|---|---|
| BIO retry, EOF and zero-length reads; TLS retry and normal close | No new entry |
| TLS shutdown's first zero result; zero-length TLS read after handshake | Existing return convention, no new entry |
| Missing peer certificate, unnegotiated TLS information, empty pending count | Query result, no new entry |
| Metadata lookup miss, unsupported NID, absent getter input | Query result, no new entry |
| Unsupported BIO control or missing control callback | Capability/query result, no new entry |
| NULL cleanup/reset where already accepted | No operation, no new entry |
| Uncompiled TLS role method | NULL capability result, no new entry |
Low-level BIO data/init/shutdown/flag accessors still require a live object. Error reporting does not validate arbitrary pointers or object lifetimes. Application-defined BIO callbacks own their diagnostics; a zero callback result alone cannot distinguish an ordinary EOF from a failure. The core preserves their queue entries and retry flags. A failing create callback must also release its partial state. TLS independently reports its first terminal transition, so a callback-specific entry and an SSL entry may both be present.
The optional dispatcher extension keeps its existing explicit transport-result contract. Its constructor and native status accessor do not promise an ERR entry; TLS maps terminal transport outcomes to SSL diagnostics. The portable ERR producer contract does not add diagnostics to these dispatcher extension calls.
Direct results remain authoritative. Broad plugin reasons group invalid arguments, unsupported operation parameters and unusable state; they are not a promise of OpenSSL's individual reason numbers or provider stack. Even the presence of an entry can differ from OpenSSL for some rejected operations.
The supported standard library/reason constants use OpenSSL 3 packing. Plugin-specific reasons are named SHARKSSL_OSSL_R_* in the storage header. System-error packing, provider diagnostics, custom string registration, source locations, attached text, legacy static-buffer formatting and deprecated APIs are absent. Reason strings are descriptive; their exact spelling is not a compatibility contract. A NULL formatting buffer or zero size performs no write. An all-zero code is not enqueued. Unknown codes have NULL string getters and a numeric formatted representation.