LIBRESSL-SSL_CTX_SET_ALPN_SELECT_CB(3)
πΈ SSL_CTX_SET_ALPN_SELECT_(3) πΈ
okay so welcome to the Library Functions Manual page for SSL_CTX_SET_ALPN_SELECT_(3), bestie. This one is all about ALPN β application layer protocol negotiation β and itβs lowkey iconic for clients and servers agreeing on which protocol to speak. π¦
π NAME π
SSL_CTX_set_alpn_protos, SSL_set_alpn_protos, SSL_CTX_set_alpn_select_cb, SSL_select_next_proto, SSL_get0_alpn_selected β handle application layer protocol negotiation (ALPN).
β¨ SYNOPSIS β¨
library βlibsslβ libcrypto #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);
int
SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
const unsigned char *peer_list, unsigned int peer_list_len,
const unsigned char *supported_list,
unsigned int supported_list_len);
void
SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
unsigned int *len);
π DESCRIPTION π
okay so hereβs the tea, bestie:
SSL_CTX_set_alpn_protos()andSSL_set_alpn_protos()β these are the client-side besties. The client uses them to set the list of protocols available to be negotiated. Theprotosmust be in protocol-list format, described below. The length ofprotosis specified inprotos_len. π«Ά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.cbβ this is 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()β a helper function used to select protocols. It is expected that this function is called from the application callbackcb. IfSSL_select_next_proto()returnsOPENSSL_NPN_NO_OVERLAP,cbshould ignoreoutand fail by returningSSL_TLSEXT_ERR_ALERT_FATAL. The protocol data inpeer_list,peer_list_lenandsupported_list,supported_list_lenmust be two non-empty lists, validly encoded in the protocol-list format described below. The first item in thepeer_listthat matches an item in thesupported_listis selected, and returned inout,outlen. Theoutvalue will point into eitherpeer_listorsupported_list, so it must not be modified and should be copied immediately. If no match is found, the first item insupported_listis returned inout,outlen.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. π
The protocol-lists must be in wire-format, which is defined as a vector of non-empty, 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. The length of the vector is not in the vector itself, but in a separate variable.
For example:
const unsigned char *vector = "\x06" "spdy/1" "\x08" "http/1.1";
unsigned int length = strlen(vector);
The ALPN callback is executed after the servername callback; as that servername callback may update the SSL_CTX, and subsequently, the ALPN callback. π
If there is no ALPN proposed in the ClientHello, the ALPN callback is not invoked.
π₯ RETURN VALUES π₯
SSL_CTX_set_alpn_protos()andSSL_set_alpn_protos()β return0on success or non-zero on failure. WARNING: these functions reverse the return value convention. π¨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 insupported_list,supported_list_lenis returned inout,outlen.
- The ALPN select callback
cbmust 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.
π SEE ALSO π
β STANDARDS β
- TLS Application-Layer Protocol Negotiation Extension, RFC 7301.
- TLS Next Protocol Negotiation Extension,
https://datatracker.ietf.org/doc/html/draft-agl-tls-nextprotoneg.
π¦ HISTORY π¦
SSL_select_next_proto()first appeared in OpenSSL1.0.1and has been available since OpenBSD5.3.SSL_CTX_set_alpn_protos(),SSL_set_alpn_protos(),SSL_CTX_set_alpn_select_cb(), andSSL_get0_alpn_selected()first appeared in OpenSSL1.0.2and have been available since OpenBSD5.7.
π CAVEATS π
The fallback to the first supported protocol in SSL_select_next_proto() comes from the opportunistic fallback mechanism in the NPN extension. This behavior does not make sense for ALPN, where missing protocol overlap should result in a handshake failure. To avoid accidental selection of a protocol that the server does not support, it is recommended to pass the locally configured protocols as second pair of protocols in the ALPN callback. π
π BUGS π
The out argument of SSL_select_next_proto() should have been const.
xoxo, Debian Β· June 8, 2025 Β· SSL_CTX_SET_ALPN_SELECT_CB(3) π
π¦ where this page comes from
- alpine 3.24: libressl-doc 4.3.1-r0 Β· custom
- arch rolling: libressl 4.4.0-1 Β· ISC AND custom:OpenSSL
- Copyright (c) 2016 The OpenSSL Project. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms