|
SharkSSL C/C++ Reference
TLS, cryptography, certificates, and PSA integration
|
Use the OpenSSL-style TLS calls to configure a context, create connections, exchange protected data and close each connection. The adapter maps those calls to SharkSSL while your application owns the transport and scheduling. The complete TLS example connects a client and server through memory BIOs, with no socket dependency.
SSL_CTX* with TLS_client_method(), TLS_server_method() or TLS_method(). Check for NULL. Configure versions and ciphers before creating connections.SSL_VERIFY_PEER; servers can require mutual TLS.SSL_new(ctx). On a client, set the expected name with SSL_set1_host; set SNI separately when needed. Check both return values.SSL_connect or SSL_accept. On a retry, wait for the direction returned by SSL_get_error and repeat the same operation.SSL_shutdown, then free the SSL and context references you own. Freeing SSL is not a substitute for TLS shutdown.Configure identity and trust before creating any connections from the context. For peer policy and callback details, read verification.
| Area | Interfaces |
|---|---|
| Methods | TLS_method, TLS_client_method, TLS_server_method |
| Context | SSL_CTX_new, SSL_CTX_new_ex, SSL_CTX_up_ref, SSL_CTX_free |
| Connection | SSL_new, SSL_up_ref, SSL_free, SSL_get_SSL_CTX |
| BIO ownership | SSL_set_bio, SSL_set0_rbio, SSL_set0_wbio, SSL_get_rbio, SSL_get_wbio |
| Hosted socket attachment | SSL_set_fd with SHARKSSL_OSSL_ENABLE_SOCKET=1 |
| Identity loading | SSL_CTX_use_certificate_file, SSL_CTX_use_certificate_chain_file, SSL_CTX_use_PrivateKey_file, SSL_CTX_check_private_key |
| CA loading | SSL_CTX_load_verify_file, SSL_CTX_load_verify_locations with NULL directory, optional SSL_CTX_set_default_verify_paths |
| PEM buffers | Four SharkSSL-prefixed context loaders in SharkSslOsslPem.h; see the buffer contract |
| Verification configuration | SSL_CTX_set_verify, SSL_set_verify, SSL_set1_host, SSL_get_verify_result |
| SNI | SSL_set_tlsext_host_name |
| Handshake | SSL_connect, SSL_accept, SSL_set_accept_state, SSL_set_connect_state, SSL_is_server, SSL_do_handshake, SSL_is_init_finished, SSL_get_version |
| Record I/O | SSL_read, SSL_write, SSL_read_ex, SSL_write_ex, SSL_pending, SSL_get_error |
| Shutdown | SSL_shutdown, SSL_get_shutdown |
| Protocol/cipher controls | Context/SSL min/max version setters/getters, SSL_CTX_set_cipher_list, SSL_set_cipher_list, SSL_CTX_set_ciphersuites, SSL_set_ciphersuites, SSL_get_cipher_list |
| Negotiated information | SSL_version, SSL_get_current_cipher, six cipher property getters and four convenience macros; see the information contract |
| ALPN | SSL_CTX_set_alpn_protos, SSL_set_alpn_protos, SSL_CTX_set_alpn_select_cb, SSL_get0_alpn_selected; see the binary ALPN contract |
A role-specific context owns one native SharkSsl. With TLS_method(), each SSL selects its role before I/O. A context used in both roles owns two native coordinators which borrow the same configured identity and CA store. Each SSL retains its context and acquires its native connection when the role is known. Freeing the application's context reference therefore leaves existing SSL objects usable. The last SSL reference releases its BIO references and native connection before releasing its context. Freeing NULL contexts and SSL objects is harmless.
Each set0 BIO call transfers one reference and frees the previous reference, including when the pointer is unchanged. Attaching one BIO for both reading and writing requires two references. Getters return borrowed pointers. BIO chains are outside the existing BIO subset. No ownership is transferred when SSL creation fails.
With SSL_set_bio(ssl, rbio, wbio), two newly attached, distinct BIOs each transfer one caller reference. A single new BIO passed for both directions transfers one caller reference in total. When replacing an existing pair, use these rules before freeing or reusing a BIO.
| New attachment | Caller references consumed |
|---|---|
| Same pair as before | None |
| Share a BIO already attached on one side | None |
| Change only the write BIO | One for the new write BIO |
| Change only the read BIO when the old pair was distinct | One for the new read BIO |
| Change only the read BIO when the old pair was shared | One for the new read BIO and one for the retained write BIO. Call BIO_up_ref on the retained BIO before the call if you only hold a borrowed pointer. |
| Change both directions to distinct BIOs, including a swap | One for each BIO |
A NULL direction consumes no reference. Set up the BIOs before I/O. Passing the identical pair again during a retry leaves the operation pending. OpenSSL implementation of the replacement rules
SSL_CTX_new_ex accepts only NULL library context and NULL/empty properties. A role-specific method returns NULL when its native role is disabled. An unknown method or a NULL argument fails context creation. Native session caching is disabled for these objects. Input/output starting capacities are set to 4096 bytes; native input growth handles larger peer records. Compile-time native record and handshake bounds still apply. The TLS controls contract describes version bounds and explicit cipher-name lists. Runtime group, signature-scheme and buffer-size controls remain outside this subset.
Reference updates use the existing host atomic hooks. All mutable operations need caller serialization. In particular, creation and final destruction of SSL objects sharing a context must be serialized because the native connection count is not atomic. The wrapper rejects creation at the native U16 count limit. This is not OpenSSL's general shared-context concurrency contract.
Attach both BIO directions before I/O. With TLS_method(), use SSL_connect or SSL_accept to select the role. Alternatively call SSL_set_connect_state or SSL_set_accept_state before SSL_do_handshake, a read, or a write. The handshake calls SSL_connect, SSL_accept and SSL_do_handshake return 1 on success and -1 on retry or failure. The role setters return void. Read and write can start the handshake implicitly. The native build determines which TLS versions and ciphers are available.
A fresh SSL from a role-specific method accepts the corresponding state setter. A flexible SSL may change roles before its first I/O. Starting a new handshake after I/O requires a fresh SSL. Serialize role selection, creation and final release for connections sharing a context.
SSL_read_ex and SSL_write_ex return 1 on success, otherwise 0. This adapter zeros their byte-count outputs on failure; general OpenSSL callers should only use those counts on success. A read can return fewer bytes than requested; SSL_pending reports remaining plaintext in the current decoded record, not encrypted bytes waiting in the BIO. The wrapper retains that plaintext across opposite-direction writes. A zero-length write succeeds without application data after handshake progress. A zero-length SSL_read_ex returns 0/SYSCALL after the handshake.
SSL_read and SSL_write return the plaintext byte count on success. Pass a nonpositive result directly to SSL_get_error on the same SSL. A retry returns -1. A read returns 0 with SSL_ERROR_ZERO_RETURN after the peer's TLS close alert. A negative length raises SSL_R_BAD_LENGTH before any I/O.
A zero-length integer read still processes input. It can request a retry, report the peer's close alert, or return 0 with SSL_ERROR_SYSCALL when plaintext is available. That plaintext stays pending. A completed zero-length integer write returns 0 with SSL_ERROR_SYSCALL.
By default a successful write consumes the full request. After WANT_READ or WANT_WRITE, retry with the original pointer, length and contents. SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER permits a different pointer to identical data of the original length. SSL_MODE_ENABLE_PARTIAL_WRITE permits success once a complete application record reaches the BIO. Advance the caller's buffer by the returned plaintext count. Pending ciphertext always drains before the adapter reports its corresponding plaintext as written.
Use SSL_CTX_set_mode, SSL_CTX_clear_mode and SSL_CTX_get_mode to configure future connections. The corresponding SSL_set_mode, SSL_clear_mode and SSL_get_mode operate on one SSL. The returned mask contains implemented bits. Rebinding BIOs during an active operation or violating the write retry contract records a terminal SSL error. Use the returned retry direction, not the name of the original operation.
Call SSL_get_error immediately after a failed SSL operation on the same serialized object:
| Classification | Meaning in this subset |
|---|---|
SSL_ERROR_WANT_READ / SSL_ERROR_WANT_WRITE | BIO retry direction; wait using the application's transport/event loop |
SSL_ERROR_ZERO_RETURN | Received TLS close_notify, after cached plaintext is drained |
SSL_ERROR_SSL | Native TLS failure, wrapper policy/state error, zero-progress positive-size BIO success, or unexpected transport EOF |
SSL_ERROR_SYSCALL | Terminal BIO failure other than EOF; optional dispatcher result API retains its native error |
The latest I/O result is stored per SSL. Fatal results remain latched. The bounded per-thread ERR subset records the first fatal I/O transition in the calling thread's queue. Raw TCP FIN is never treated as close_notify. Native fatal-alert output is retained through write retries before the adapter reports its final SSL error. Unsupported warning alerts also fail the connection.
SSL_shutdown generates and flushes local close_notify without closing the BIO/socket. It returns 0 when only the local close is complete, 1 when both directions are complete, or -1 with a retry/failure classification. Do not pass its normal zero result to SSL_get_error. After the local close, application writes are prohibited but reads may continue. A peer that has received the close may finish its own writes before closing its direction. Drain incoming application data before completing shutdown: this subset rejects shutdown with cached or newly encountered application plaintext. There is no quiet or automatic shutdown on SSL_free.
For nonblocking transports, wait for readiness in the application. BIO callbacks must follow byte-count and retry contracts and make progress on successful positive-size I/O. This adapter has no internal timeout, socket wait, task, dispatcher registration or locking. The optional dispatcher BIO still requires the application to own synchronization, event callbacks, connection storage and dispatcher lifetime. SSL shutdown does not change BIO_CLOSE/BIO_NOCLOSE ownership rules.
Select tls and its dependencies as described in build integration. For manual source selection, compile src/SharkSslOsslTls.c, src/SharkSslOsslTlsCredentials.c, src/SharkSslOsslTlsIo.c and src/SharkSslOsslX509.c, plus src/SharkSslOsslErr.c with the portable BIO implementation and matching native SharkSSL sources/configuration. Add transport/SharkSslOsslSocket.c for hosted sockets. Include src/SharkSslOsslTlsFiles.c when using the file APIs. Native PEM, certificate chains, CA list, certificate-store, date and information support are required. Disable SHARKSSL_ENABLE_SECURE_RENEGOTIATION and SHARKSSL_ENABLE_POST_HANDSHAKE_AUTH for this adapter. TLS 1.3 KeyUpdate processing remains native; no OpenSSL-style request API is exposed. The native backend is required; PSA consumer integration is rejected. Select a matching native backend and its required source/assembly objects. Application-owned RNG initialization is required; constructing these objects does not seed the generator. Automatic host entropy collection is outside this subset.
References: context creation, SSL lifetimes, and BIO reference transfer. PEM behavior references: certificate/key loading and CA file loading. I/O references: handshake, write retries, error classification, and shutdown.