SharkSSL C/C++ Reference
TLS, cryptography, certificates, and PSA integration
Negotiate an application protocol with ALPN

Application-Layer Protocol Negotiation (ALPN) lets a TLS client and server agree on an application protocol, such as h2 or http/1.1. This subset adds the OpenSSL ALPN interfaces using native SharkSSL byte-list APIs. It supports binary names, including embedded NUL and comma bytes.

The following excerpt assumes a live client SSL* ssl; drive and check the handshake between the configuration and inspection steps.

/* Configure a client before SSL_connect. ALPN setters return zero on success. */
static const unsigned char protocols[] = {2, 'h', '2'};
if (SSL_set_alpn_protos(ssl, protocols, sizeof(protocols)) != 0)
{
/* Handle rejected input or allocation failure. */
}
/* After successful handshake, inspect without assuming NUL termination. */
const unsigned char* selected;
unsigned int selectedLen;
SSL_get0_alpn_selected(ssl, &selected, &selectedLen);

The example requires <openssl/ssl.h> from the adapter include directory. No new library source or socket dependency is needed. ALPN does not implement the chosen application protocol or require certificate trust by itself; configure verification separately.

Wrapper interfaces

Interface Contract
SSL_CTX_set_alpn_protos(SSL_CTX* ctx, const unsigned char* protos, unsigned int size) Copies a context's client offer; returns int, zero on success and one on failure
SSL_set_alpn_protos(SSL* ssl, const unsigned char* protos, unsigned int size) Replaces that connection's copied client offer before I/O starts; same return convention
SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx, SSL_CTX_alpn_select_cb_func callback, void* arg) void; installs a server callback and borrowed argument; NULL callback disables selection
SSL_get0_alpn_selected(const SSL* ssl, const unsigned char** data, unsigned int* size) void; returns borrowed selected bytes and byte count after wrapper handshake success
SSL_select_next_proto Finds the first protocol in server preference order also offered by the client

A server callback can pass its preference list first and the client's offer second to SSL_select_next_proto. On OPENSSL_NPN_NEGOTIATED, the selected bytes borrow storage from the server list. On OPENSSL_NPN_NO_OVERLAP, the helper returns the first entry of a valid, nonempty client list as a fallback. An ALPN callback must ignore that fallback. Return SSL_TLSEXT_ERR_NOACK to continue without a protocol or SSL_TLSEXT_ERR_ALERT_FATAL to require a match.

A list contains consecutive entries, each starting with one nonzero length byte followed by that many protocol bytes. The length excludes the prefix. Names contain 1..255 bytes. The list contains at most 65533 bytes; native ClientHello and buffer limits may further restrict a usable offer. Zero total length clears it, with either a NULL or non-NULL pointer. A nonzero length requires a valid pointer. Empty entries, truncated entries and oversized lists fail without changing the old setting. Duplicates and order are preserved.

SSL_new copies the context offer. Changing the context afterward affects future SSL objects only. The input bytes may be changed or freed after a successful setter. Per-SSL setters reject changes once I/O processing starts. Server contexts may store an offer, but only clients transmit it.

The callback signature is:

/* Runs synchronously during SSL I/O; the offered bytes are borrowed. */
int callback(SSL* ssl, const unsigned char** out, unsigned char* outLen,
const unsigned char* in, unsigned int inLen, void* arg);

ssl is the connection being processed. in is the complete validated offered vector, borrowed only for the call; inLen is its byte count. arg is the application's borrowed argument. Set *out and *outLen to one nonempty offered name and return SSL_TLSEXT_ERR_OK. The output may point into in or another buffer valid through callback return; native SharkSSL validates membership and copies the selected bytes. Return SSL_TLSEXT_ERR_NOACK to continue without selection, or SSL_TLSEXT_ERR_ALERT_FATAL to reject with no_application_protocol. Other results, including the warning constant, are fatal in this subset. An OK result with NULL, zero-length or unoffered output is rejected. The callback is not invoked when the client omits ALPN.

The callback and argument are read from the owning context when selection runs. Unlike the client offer, they are not snapshotted by SSL_new. Serialize context changes with handshakes. Callbacks run synchronously during SSL I/O; do not recursively perform SSL I/O, free the SSL/context or change ALPN settings inside a callback. Keep the callback code and argument alive while used.

Selected bytes are not NUL-terminated and must not be freed or changed. They remain valid until SSL destruction in this subset, including after shutdown. Before wrapper handshake success, or when no protocol was selected, the getter writes NULL and zero. Either output pointer may be NULL. A NULL SSL has no selection. Pending selection during a callback is not exposed by the wrapper.

With SHARKSSL_ENABLE_ALPN_EXTENSION=0, nonempty list setters fail and clearing succeeds. A configured server callback makes handshake setup fail explicitly; clearing it restores the no-ALPN configuration. No callback or list silently enables a disabled native feature. Session resumption, renegotiation, SNI context switching and NPN negotiation remain outside this wrapper subset.

Native integration

Build the adapter and native SharkSSL with matching headers and configuration. The adapter copies offered lists and retains the negotiated result. The native SharkSslCon_setALPNProtocolsEx, SharkSslCon_setALPNFunctionEx and SharkSslCon_getALPNProtocolEx APIs have different borrowing and callback contracts; use their native API reference when calling them directly.