iTranslated by AI
[PHP] Undocumented SSL Context Options
While looking through ext/openssl/xp_ssl.c, I found several options not listed in the SSL context manual, so I decided to write this article. This post is based on information as of July 5, 2024.
-
alpn_protocolsProtocols presented by the socket during the SSL/TLS handshake. A comma-separated string (GET_VER_OPT_STRING) -
min_proto_versionThe lower limit of the protocol version. An integer (GET_VER_OPT_LONG). -
max_proto_versionThe upper limit of the protocol version. An integer (GET_VER_OPT_LONG) -
no_ticketPrevents the client-side from requesting session tickets (corresponds to OpenSSL'sSSL_OP_NO_TICKET). A boolean (GET_VER_OPT) -
security_levelIt only says to refer to the manual forvoid SSL_CTX_set_security_level(SSL_CTX *ctx, int level). Levels range from 0 to 5 (up to 256-bit security).
According to the OpenSSL manual for C, it is recommended to select TLS_method(), TLS_server_method(), or TLS_client_method() when creating an SSL context (SSL_CTX_new()), and use them in conjunction with SSL_CTX_set_min_proto_version() and SSL_CTX_set_max_proto_version() to set the lower and upper bounds of the TLS version.
Conversely, it is said that you should not create an SSL context by hard-coding a specific version like TLSv1_2_method(), TLSv1_2_server_method(), or TLSv1_2_client_method(). In fact, there is no specific method for TLS 1.3.
Returning to PHP's SSL context, the lower and upper bounds for the version are specified using min_proto_version or max_proto_version. At the time of writing, it would be best to set the lower bound to STREAM_CRYPTO_METHOD_TLSv1_2_SERVER or STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT.
In PHP's curl extension, you should specify CURL_SSLVERSION_TLSv1_2 for the CURLOPT_SSLVERSION option as the lower bound.
In OpenSSL, constants such as SSL_OP_NO_TLSv1_2 and SSL_OP_NO_TLSv1_3 can be used with SSL_CTX_set_options(), but it is reportedly not recommended.
As a side note, in ext/openssl/xp_ssl.c, the default value for the method passed to SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) was an old one: method = sslsock->is_client ? SSLv23_client_method() : SSLv23_server_method();. According to the official manual, SSLv23_method(), SSLv23_server_method(), and SSLv23_client_method() have been removed and are replaced by TLS_method(), TLS_server_method(), and TLS_client_method() via preprocessor macros.
Looking at the specification for alpn_protocols, it is a string (GET_VER_OPT_STRING), and php_openssl_alpn_protos_parse parses the comma-separated string and passes it to SSL_CTX_set_alpn_protos. Whether an example like h2,http/1 works correctly would need to be tested.
Reading the manual for int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos, unsigned int protos_len), the specification for const unsigned char protos is "a sequence of protocol strings, each preceded by its length in bytes." I will reproduce the code from the manual here. The author of nghttp2 has also published an explanatory article.
unsigned char vector[] = {
6, 's', 'p', 'd', 'y', '/', '1',
8, 'h', 't', 't', 'p', '/', '1', '.', '1'
};
unsigned int length = sizeof(vector);
I read the manual for SSL_OP_NO_TICKET, which corresponds to no_ticket, but there doesn't seem to be much benefit. It might be enough to remember that OpenSSL uses stateless session tickets by default.
As a side note, when I checked the ssl module in Python 3.13.0b3, the minimum version was TLS 1.2 and there was no maximum version.
>>> import ssl
>>> ssl.create_default_context()
<ssl.SSLContext object at 0x7f4a13f629f0>
>>> ctx = ssl.create_default_context()
>>> ctx.minimum_version
<TLSVersion.TLSv1_2: 771>
>>> ctx.maximum_version
<TLSVersion.MAXIMUM_SUPPORTED: -1>
The values for ctx.options are as follows:
>>> ctx.options
<Options.OP_NO_COMPRESSION|OP_ENABLE_MIDDLEBOX_COMPAT|OP_CIPHER_SERVER_PREFERENCE|OP_NO_SSLv3|OP_ALL: 2186412112>
Adding ssl.OP_NO_TICKET results in the following:
>>> ctx.options |= ssl.OP_NO_TICKET
>>> ctx.options
<Options.OP_NO_TICKET|OP_NO_COMPRESSION|OP_ENABLE_MIDDLEBOX_COMPAT|OP_CIPHER_SERVER_PREFERENCE|OP_NO_SSLv3|OP_ALL: 2186428496>
>>>
ALPN is configured as follows:
>>> ctx.set_alpn_protocols(['h2', 'http/1.1'])
Discussion