SSL_CTX_SET_ALPN_SELECT_CB(3ossl)
πΈ NAME πΈ
Hiii bestie, welcome to the 3ossl OpenSSL manual page β thatβs the manual name, iykyk. This one is all about handling application layer protocol negotiation (ALPN), and the whole crew is:
SSL_CTX_set_alpn_protosSSL_set_alpn_protosSSL_CTX_set_alpn_select_cbSSL_CTX_set_next_proto_select_cbSSL_CTX_set_next_protos_advertised_cbSSL_select_next_protoSSL_get0_alpn_selectedSSL_get0_next_proto_negotiated
β¨ SYNOPSIS β¨
Okay bestie, hereβs the exact signature lineup β do not freestyle these, okay? periodt.
#include <openssl/ssl.h>
int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos,
unsigned int protos_len);
int SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos,
unsigned int protos_len);
void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
int (*cb) (SSL *ssl,
const unsigned char **out,
unsigned char *outlen,
const unsigned char *in,
unsigned int inlen,
void *arg), void *arg);
void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
unsigned int *len);
void SSL_CTX_set_next_protos_advertised_cb(SSL_CTX *ctx,
int (*cb)(SSL *ssl,
const unsigned char **out,
unsigned int *outlen,
void *arg),
void *arg);
void SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx,
int (*cb)(SSL *s,
unsigned char **out,
unsigned char *outlen,
const unsigned char *in,
unsigned int inlen,
void *arg),
void *arg);
int SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
const unsigned char *server,
unsigned int server_len,
const unsigned char *client,
unsigned int client_len);
void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data,
unsigned *len);
π DESCRIPTION π
Okay so, quick vibe check: this whole page is giving βprotocol negotiation main character energyβ β¨. Hereβs the tea, function by function.
SSL_CTX_set_alpn_protos()andSSL_set_alpn_protos()β these are used by the client to set the list of protocols available to be negotiated. Theprotosmust be in protocol-list format, described below. The length ofprotosis specified inprotos_len. Settingprotos_lento0clears any existing list of ALPN protocols, and no ALPN extension will be sent to the server. Clean slate, no ALPN invite.SSL_CTX_set_alpn_select_cb()β sets the application callbackcbused by a server to select which protocol to use for the incoming connection. WhencbisNULL, ALPN is not used. Theargvalue is a pointer which is passed to the application callback.- The ALPN select callback
cbitself βcbis the application-defined callback. Thein,inlenparameters are a vector in protocol-list format. The value of theout,outlenvector should be set to the value of a single protocol selected from thein,inlenvector. Theoutbuffer may point directly intoin, or to a buffer that outlives the handshake. Theargparameter is the pointer set viaSSL_CTX_set_alpn_select_cb(). SSL_select_next_proto()β this is a helper function used to select protocols. It implements the standard protocol selection. It is expected that this function is called from the application callbackcb. The protocol data inserver,server_lenandclient,client_lenmust be in the protocol-list format described below. The first item in theserver,server_lenlist that matches an item in theclient,client_lenlist is selected, and returned inout,outlen. Theoutvalue will point into eitherserverorclient, so it should be copied immediately. Theclientlist must include at least one valid (nonempty) protocol entry in the list. Copy it fast, bestie.- The helper can also do NPN stuff β the
SSL_select_next_proto()helper function can be useful from either the ALPN callback or the NPN callback (described below). If no match is found, the first item inclient,client_lenis returned inout,outlenandOPENSSL_NPN_NO_OVERLAPis returned. This can be useful when implementing the NPN callback. In the ALPN case, the value returned inoutandoutlenmust be ignored ifOPENSSL_NPN_NO_OVERLAPhas been returned fromSSL_select_next_proto(). SSL_CTX_set_next_proto_select_cb()β sets a callbackcbthat is called when a client needs to select a protocol from the serverβs provided list, and a user-defined pointer argumentargwhich will be passed to this callback. For the callback itself,outmust be set to point to the selected protocol (which may be withinin). The length of the protocol name must be written intooutlen. The serverβs advertised protocols are provided ininandinlen. The callback can assume thatinis syntactically valid. The client must select a protocol (although it may be an empty, zero length protocol). It is fatal to the connection if this callback returns a value other thanSSL_TLSEXT_ERR_OKor if the zero length protocol is selected. Theargparameter is the pointer set viaSSL_CTX_set_next_proto_select_cb().SSL_CTX_set_next_protos_advertised_cb()β sets a callbackcbthat is called when a TLS server needs a list of supported protocols for Next Protocol Negotiation. The returned list must be in protocol-list format, described below. The list is returned by settingoutto point to it andoutlento its length. This memory will not be modified, but the SSL does keep a reference to it. The callback should returnSSL_TLSEXT_ERR_OKif it wishes to advertise. Otherwise, no such extension will be included in the ServerHello.SSL_get0_alpn_selected()β returns a pointer to the selected protocol indatawith lengthlen. It is not NUL-terminated.datais set toNULLandlenis set to0if no protocol has been selected.datamust not be freed. Do not free it, fr fr.SSL_get0_next_proto_negotiated()β setsdataandlento point to the clientβs requested protocol for this connection. If the client did not request any protocol or NPN is not enabled, thendatais set toNULLandlento0. Note that the client can request any protocol it chooses. The value returned from this function need not be a member of the list of supported protocols provided by the callback.
QUIC tea: NPN functionality cannot be used with QUIC SSL objects. Use of ALPN is mandatory when using QUIC SSL objects.SSL_CTX_set_next_protos_advertised_cb()andSSL_CTX_set_next_proto_select_cb()have no effect if called on a QUIC SSL context.
π§ NOTES π§
The protocol-lists must be in wire-format, which is defined as a vector of nonempty, 8-bit length-prefixed, byte strings. The length-prefix byte is not included in the length. Each string is limited to 255 bytes. A byte-string length of 0 is invalid. A truncated byte-string is invalid. The length of the vector is not in the vector itself, but in a separate variable. Structure matters, bestie.
Example:
unsigned char vector[] = {
6, 's', 'p', 'd', 'y', '/', '1',
8, 'h', 't', 't', 'p', '/', '1', '.', '1'
};
unsigned int length = sizeof(vector);
Timing tea β: the ALPN callback is executed after the servername callback; as that servername callback may update the SSL_CTX, and subsequently, the ALPN callback.
And if there is no ALPN proposed in the ClientHello, the ALPN callback is not invoked. No invite, no party.
π RETURN VALUES π
SSL_CTX_set_alpn_protos()andSSL_set_alpn_protos()β return0on success, and non-0on failure.
WARNING: these functions reverse the return value convention. Not me checking twice.
SSL_select_next_proto() returns one of the following:
OPENSSL_NPN_NEGOTIATEDβ a match was found and is returned inout,outlen.OPENSSL_NPN_NO_OVERLAPβ no match was found. The first item inclient,client_lenis returned inout,outlen(orNULLand0in the case where the first entry inclientis invalid).
The ALPN select callback cb, must return one of the following:
SSL_TLSEXT_ERR_OKβ ALPN protocol selected.SSL_TLSEXT_ERR_ALERT_FATALβ there was no overlap between the clientβs supplied list and the server configuration.SSL_TLSEXT_ERR_NOACKβ ALPN protocol not selected, e.g., because no ALPN protocols are configured for this connection.
The callback set using SSL_CTX_set_next_proto_select_cb() should return SSL_TLSEXT_ERR_OK if successful. Any other value is fatal to the connection.
The callback set using SSL_CTX_set_next_protos_advertised_cb() should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no such extension will be included in the ServerHello.
π SEE ALSO π
π COPYRIGHT π
Copyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved.
Licensed under the Apache License 2.0 (the βLicenseβ). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.
Made with π for OpenSSL 3.5.0, dated 2026-09-25 β thatβs the source/version and date from the footer, bestie. slay.
π¦ where this page comes from
- opensuse-leap 16.0: openssl-3-doc 3.5.0-160000.11.1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms