summaryrefslogtreecommitdiffhomepage
path: root/src/event/ngx_event_openssl.c (follow)
AgeCommit message (Collapse)AuthorFilesLines
2025-06-26Remove short read checksno-short-read-checksAndrew Clayton1-7/+0
This removes a bunch of checks for cases where we _read_ less bytes than asked for. There are several reasons why we may _read_ less bytes including reading data from a network file system. This has proven to be an issue. Closes: https://github.com/nginx/nginx/issues/265 Signed-off-by: Andrew Clayton <ac@sigsegv.uk>
2025-05-23QUIC: logging level of handshake errors.Sergey Kandaurov1-3/+1
Various errors reported by SSL_do_handshake() are now logged at the "info" or "crit" level, akin to handshakes on regular TCP connections.
2025-04-25SSL: fixed build with OPENSSL_NO_DH.Sergey Kandaurov1-0/+4
2025-04-10SSL: external groups support in $ssl_curve and $ssl_curves.Sergey Kandaurov1-5/+30
Starting with OpenSSL 3.0, groups may be added externally with pluggable KEM providers. Using SSL_get_negotiated_group(), which makes lookup in a static table with known groups, doesn't allow to list such groups by names leaving them in hex. Adding X25519MLKEM768 to the default group list in OpenSSL 3.5 made this problem more visible. SSL_get0_group_name() and, apparently, SSL_group_to_name() allow to resolve such provider-implemented groups, which is also "generally preferred" over SSL_get_negotiated_group() as documented in OpenSSL git commit 93d4f6133f. This change makes external groups listing by name using SSL_group_to_name() available since OpenSSL 3.0. To preserve "prime256v1" naming for the group 0x0017, and to avoid breaking BoringSSL and older OpenSSL versions support, it is used supplementary for a group that appears to be unknown. See https://github.com/openssl/openssl/issues/27137 for related discussion.
2025-02-26SSL: using static storage for NGX_SSL_MAX_SESSION_SIZE buffers.Sergey Kandaurov1-6/+7
All such transient buffers are converted to the single storage in BSS. In preparation to raise the limit.
2025-02-26SSL: style.Sergey Kandaurov1-1/+1
2025-01-17SSL: avoid using mismatched certificate/key cached pairs.Sergey Kandaurov1-3/+25
This can happen with certificates and certificate keys specified with variables due to partial cache update in various scenarios: - cache expiration with only one element of pair evicted - on-disk update with non-cacheable encrypted keys - non-atomic on-disk update The fix is to retry with fresh data on X509_R_KEY_VALUES_MISMATCH.
2025-01-17SSL: caching certificates and certificate keys with variables.Sergey Kandaurov1-5/+6
A new directive "ssl_certificate_cache max=N [valid=time] [inactive=time]" enables caching of SSL certificate chain and secret key objects specified by "ssl_certificate" and "ssl_certificate_key" directives with variables. Co-authored-by: Aleksei Bavshin <a.bavshin@nginx.com>
2024-11-20Fixed missing double quote.Nathan Mentze1-1/+1
2024-10-01SSL: caching CA certificates.Sergey Kandaurov1-28/+118
This can potentially provide a large amount of savings, because CA certificates can be quite large. Based on previous work by Mini Hawthorne.
2024-10-01SSL: caching CRLs.Sergey Kandaurov1-16/+56
Based on previous work by Mini Hawthorne.
2024-10-01SSL: caching certificate keys.Sergey Kandaurov1-151/+3
EVP_KEY objects are a reference-counted container for key material, shallow copies and OpenSSL stack management aren't needed as with certificates. Based on previous work by Mini Hawthorne.
2024-10-01SSL: caching certificates.Sergey Kandaurov1-96/+9
Certificate chains are now loaded once. The certificate cache provides each chain as a unique stack of reference counted elements. This shallow copy is required because OpenSSL stacks aren't reference counted. Based on previous work by Mini Hawthorne.
2024-10-01SSL: moved certificate storage out of exdata.Sergey Kandaurov1-48/+30
Instead of cross-linking the objects using exdata, pointers to configured certificates are now stored in ngx_ssl_t, and OCSP staples are now accessed with rbtree in it. This allows sharing these objects between SSL contexts. Based on previous work by Mini Hawthorne.
2024-05-03SSL: fixed possible configuration overwrite loading "engine:" keys.Sergey Kandaurov1-2/+2
When loading certificate keys via ENGINE_load_private_key() in runtime, it was possible to overwrite configuration on ENGINE_by_id() failure. OpenSSL documention doesn't describe errors in details, the only reason I found in the comment to example is when the engine is not available.
2024-01-30SSL: fixed $ssl_curves allocation error handling.Sergey Kandaurov1-0/+3
2023-12-25SSL: disabled renegotiation checks with LibreSSL.Sergey Kandaurov1-7/+8
Similar to 7356:e3ba4026c02d, as long as SSL_OP_NO_CLIENT_RENEGOTIATION is defined, it is the library responsibility to prevent renegotiation. Additionally, this allows to raise LibreSSL version used to redefine OPENSSL_VERSION_NUMBER to 0x1010000fL, such that this won't result in attempts to dereference SSL objects made opaque in LibreSSL 3.4.0. Patch by Maxim Dounin.
2023-06-21SSL: avoid using OpenSSL config in build directory (ticket #2404).Maxim Dounin1-1/+20
With this change, the NGX_OPENSSL_NO_CONFIG macro is defined when nginx is asked to build OpenSSL itself. And with this macro automatic loading of OpenSSL configuration (from the build directory) is prevented unless the OPENSSL_CONF environment variable is explicitly set. Note that not loading configuration is broken in OpenSSL 1.1.1 and 1.1.1a (fixed in OpenSSL 1.1.1b, see https://github.com/openssl/openssl/issues/7350). If nginx is used to compile these OpenSSL versions, configuring nginx with NGX_OPENSSL_NO_CONFIG explicitly set to 0 might be used as a workaround.
2023-06-21SSL: provided "nginx" appname when loading OpenSSL configs.Maxim Dounin1-3/+21
Following OpenSSL 0.9.8f, OpenSSL tries to load application-specific configuration section first, and then falls back to the "openssl_conf" default section if application-specific section is not found, by using CONF_modules_load_file(CONF_MFLAGS_DEFAULT_SECTION). Therefore this change is not expected to introduce any compatibility issues with existing configurations. It does, however, make it easier to configure specific OpenSSL settings for nginx in system-wide OpenSSL configuration (ticket #2449). Instead of checking OPENSSL_VERSION_NUMBER when using the OPENSSL_init_ssl() interface, the code now tests for OPENSSL_INIT_LOAD_CONFIG to be defined and true, and also explicitly excludes LibreSSL. This ensures that this interface is not used with BoringSSL and LibreSSL, which do not provide additional library initialization settings, notably the OPENSSL_INIT_set_config_appname() call.
2023-03-29Merged with the default branch.Sergey Kandaurov1-1/+70
2023-03-08SSL: logging levels of errors observed with BoringSSL.Maxim Dounin1-0/+21
As tested with tlsfuzzer with BoringSSL, the following errors are certainly client-related: SSL_do_handshake() failed (SSL: error:10000066:SSL routines:OPENSSL_internal:BAD_ALERT) SSL_do_handshake() failed (SSL: error:10000089:SSL routines:OPENSSL_internal:DECODE_ERROR) SSL_do_handshake() failed (SSL: error:100000dc:SSL routines:OPENSSL_internal:TOO_MANY_WARNING_ALERTS) SSL_do_handshake() failed (SSL: error:10000100:SSL routines:OPENSSL_internal:INVALID_COMPRESSION_LIST) SSL_do_handshake() failed (SSL: error:10000102:SSL routines:OPENSSL_internal:MISSING_KEY_SHARE) SSL_do_handshake() failed (SSL: error:1000010e:SSL routines:OPENSSL_internal:TOO_MUCH_SKIPPED_EARLY_DATA) SSL_read() failed (SSL: error:100000b6:SSL routines:OPENSSL_internal:NO_RENEGOTIATION) Accordingly, the SSL_R_BAD_ALERT, SSL_R_DECODE_ERROR, SSL_R_TOO_MANY_WARNING_ALERTS, SSL_R_INVALID_COMPRESSION_LIST, SSL_R_MISSING_KEY_SHARE, SSL_R_TOO_MUCH_SKIPPED_EARLY_DATA, and SSL_R_NO_RENEGOTIATION errors are now logged at the "info" level.
2023-03-08SSL: logging levels of errors observed with tlsfuzzer and LibreSSL.Maxim Dounin1-0/+7
As tested with tlsfuzzer with LibreSSL 3.7.0, the following errors are certainly client-related: SSL_do_handshake() failed (SSL: error:14026073:SSL routines:ACCEPT_SR_CLNT_HELLO:bad packet length) SSL_do_handshake() failed (SSL: error:1402612C:SSL routines:ACCEPT_SR_CLNT_HELLO:ssl3 session id too long) SSL_do_handshake() failed (SSL: error:140380EA:SSL routines:ACCEPT_SR_KEY_EXCH:tls rsa encrypted value length is wrong) Accordingly, the SSL_R_BAD_PACKET_LENGTH ("bad packet length"), SSL_R_SSL3_SESSION_ID_TOO_LONG ("ssl3 session id too long"), SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG ("tls rsa encrypted value length is wrong") errors are now logged at the "info" level.
2023-03-08SSL: logging levels of various errors reported with tlsfuzzer.Maxim Dounin1-0/+41
To further differentiate client-related errors and adjust logging levels of various SSL errors, nginx was tested with tlsfuzzer with multiple OpenSSL versions (3.1.0-beta1, 3.0.8, 1.1.1t, 1.1.0l, 1.0.2u, 1.0.1u, 1.0.0s, 0.9.8zh). The following errors were observed during tlsfuzzer runs with OpenSSL 3.0.8, and are clearly client-related: SSL_do_handshake() failed (SSL: error:0A000092:SSL routines::data length too long) SSL_do_handshake() failed (SSL: error:0A0000A0:SSL routines::length too short) SSL_do_handshake() failed (SSL: error:0A000124:SSL routines::bad legacy version) SSL_do_handshake() failed (SSL: error:0A000178:SSL routines::no shared signature algorithms) Accordingly, the SSL_R_DATA_LENGTH_TOO_LONG ("data length too long"), SSL_R_LENGTH_TOO_SHORT ("length too short"), SSL_R_BAD_LEGACY_VERSION ("bad legacy version"), and SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS ("no shared signature algorithms", misspelled as "sigature" in OpenSSL 1.0.2) errors are now logged at the "info" level. Additionally, the following errors were observed with OpenSSL 3.0.8 and with TLSv1.3 enabled: SSL_do_handshake() failed (SSL: error:0A00006F:SSL routines::bad digest length) SSL_do_handshake() failed (SSL: error:0A000070:SSL routines::missing sigalgs extension) SSL_do_handshake() failed (SSL: error:0A000096:SSL routines::encrypted length too long) SSL_do_handshake() failed (SSL: error:0A00010F:SSL routines::bad length) SSL_read() failed (SSL: error:0A00007A:SSL routines::bad key update) SSL_read() failed (SSL: error:0A000125:SSL routines::mixed handshake and non handshake data) Accordingly, the SSL_R_BAD_DIGEST_LENGTH ("bad digest length"), SSL_R_MISSING_SIGALGS_EXTENSION ("missing sigalgs extension"), SSL_R_ENCRYPTED_LENGTH_TOO_LONG ("encrypted length too long"), SSL_R_BAD_LENGTH ("bad length"), SSL_R_BAD_KEY_UPDATE ("bad key update"), and SSL_R_MIXED_HANDSHAKE_AND_NON_HANDSHAKE_DATA ("mixed handshake and non handshake data") errors are now logged at the "info" level. Additionally, the following errors were observed with OpenSSL 1.1.1t: SSL_do_handshake() failed (SSL: error:14094091:SSL routines:ssl3_read_bytes:data between ccs and finished) SSL_do_handshake() failed (SSL: error:14094199:SSL routines:ssl3_read_bytes:too many warn alerts) SSL_read() failed (SSL: error:1408F0C6:SSL routines:ssl3_get_record:packet length too long) SSL_read() failed (SSL: error:14094085:SSL routines:ssl3_read_bytes:ccs received early) Accordingly, the SSL_R_CCS_RECEIVED_EARLY ("ccs received early"), SSL_R_DATA_BETWEEN_CCS_AND_FINISHED ("data between ccs and finished"), SSL_R_PACKET_LENGTH_TOO_LONG ("packet length too long"), and SSL_R_TOO_MANY_WARN_ALERTS ("too many warn alerts") errors are now logged at the "info" level. Additionally, the following errors were observed with OpenSSL 1.0.2u: SSL_do_handshake() failed (SSL: error:1407612A:SSL routines:SSL23_GET_CLIENT_HELLO:record too small) SSL_do_handshake() failed (SSL: error:1408C09A:SSL routines:ssl3_get_finished:got a fin before a ccs) Accordingly, the SSL_R_RECORD_TOO_SMALL ("record too small") and SSL_R_GOT_A_FIN_BEFORE_A_CCS ("got a fin before a ccs") errors are now logged at the "info" level. No additional client-related errors were observed while testing with OpenSSL 3.1.0-beta1, OpenSSL 1.1.0l, OpenSSL 1.0.1u, OpenSSL 1.0.0s, and OpenSSL 0.9.8zh.
2023-03-08SSL: switched to detect log level based on the last error.Maxim Dounin1-1/+1
In some cases there might be multiple errors in the OpenSSL error queue, notably when a libcrypto call fails, and then the SSL layer generates an error itself. For example, the following errors were observed with OpenSSL 3.0.8 with TLSv1.3 enabled: SSL_do_handshake() failed (SSL: error:02800066:Diffie-Hellman routines::invalid public key error:0A000132:SSL routines::bad ecpoint) SSL_do_handshake() failed (SSL: error:08000066:elliptic curve routines::invalid encoding error:0A000132:SSL routines::bad ecpoint) SSL_do_handshake() failed (SSL: error:0800006B:elliptic curve routines::point is not on curve error:0A000132:SSL routines::bad ecpoint) In such cases it seems to be better to determine logging level based on the last error in the error queue (the one added by the SSL layer, SSL_R_BAD_ECPOINT in all of the above example example errors). To do so, the ngx_ssl_connection_error() function was changed to use ERR_peek_last_error().
2023-02-23QUIC: using ngx_ssl_handshake_log().Sergey Kandaurov1-4/+1
2022-12-15Merged with the default branch.Sergey Kandaurov1-1/+6
2022-12-01SSL: fixed ngx_ssl_recv() to reset c->read->ready after errors.Maxim Dounin1-0/+5
With this change, behaviour of ngx_ssl_recv() now matches ngx_unix_recv(), which used to always reset c->read->ready to 0 when returning errors. This fixes an infinite loop in unbuffered SSL proxying if writing to the client is blocked and an SSL error happens (ticket #2418). With this change, the fix for a similar issue in the stream module (6868:ee3645078759), which used a different approach of explicitly testing c->read->error instead, is no longer needed and was reverted.
2022-11-24SSL: fixed debug logging of SSL_sendfile() return value.Sergey Kandaurov1-1/+1
2022-10-13SSL: removed cast not needed after 5ffd76a9ccf3.Sergey Kandaurov1-1/+1
2022-10-12SSL: workaround for session timeout handling with TLSv1.3.Maxim Dounin1-0/+47
OpenSSL with TLSv1.3 updates the session creation time on session resumption and keeps the session timeout unmodified, making it possible to maintain the session forever, bypassing client certificate expiration and revocation. To make sure session timeouts are actually used, we now update the session creation time and reduce the session timeout accordingly. BoringSSL with TLSv1.3 ignores configured session timeouts and uses a hardcoded timeout instead, 7 days. So we update session timeout to the configured value as soon as a session is created.
2022-10-12SSL: optimized rotation of session ticket keys.Maxim Dounin1-18/+46
Instead of syncing keys with shared memory on each ticket operation, the code now does this only when the worker is going to change expiration of the current key, or going to switch to a new key: that is, usually at most once per second. To do so without races, the code maintains 3 keys: current, previous, and next. If a worker will switch to the next key earlier, other workers will still be able to decrypt new tickets, since they will be encrypted with the next key.
2022-10-12SSL: automatic rotation of session ticket keys.Maxim Dounin1-18/+149
As long as ssl_session_cache in shared memory is configured, session ticket keys are now automatically generated in shared memory, and rotated periodically. This can be beneficial from forward secrecy point of view, and also avoids increased CPU usage after configuration reloads. This also helps BoringSSL to properly resume sessions in configurations with multiple worker processes and no ssl_session_ticket_key directives, as BoringSSL tries to automatically rotate session ticket keys and does this independently in different worker processes, thus breaking session resumption between worker processes.
2022-10-12SSL: shorter debug messages about session tickets.Maxim Dounin1-3/+3
2022-10-12SSL: renamed session ticket key functions and data index.Maxim Dounin1-15/+12
Previously used names are way too long, renamed to simplify writing code.
2022-10-12SSL: renamed session ticket key type.Maxim Dounin1-20/+20
The ngx_ssl_session_ticket_key_t is way too long, renamed to ngx_ssl_ticket_key_t to simplify writing code.
2022-10-12SSL: explicit clearing of expired sessions.Maxim Dounin1-0/+6
This reduces lifetime of session keying material in server's memory, and therefore can be beneficial from forward secrecy point of view.
2022-10-12SSL: single allocation in session cache on 32-bit platforms.Maxim Dounin1-44/+21
Given the present typical SSL session sizes, on 32-bit platforms it is now beneficial to store all data in a single allocation, since rbtree node + session id + ASN1 representation of a session takes 256 bytes of shared memory (36 + 32 + 150 = about 218 bytes plus SNI server name). Storing all data in a single allocation is beneficial for SNI names up to about 40 characters long and makes it possible to store about 4000 sessions in one megabyte (instead of about 3000 sessions now). This also slightly simplifies the code.
2022-10-12SSL: explicit session id length checking.Maxim Dounin1-2/+8
Session ids are not expected to be longer than 32 bytes, but this is theoretically possible with TLSv1.3, where session ids are essentially arbitrary and sent as session tickets. Since on 64-bit platforms we use fixed 32-byte buffer for session ids, added an explicit length check to make sure the buffer is large enough.
2022-10-12SSL: updated comment about session sizes.Maxim Dounin1-6/+6
Previous numbers are somewhat outdated, typical ASN1 representations of sessions are slightly bigger now.
2022-10-12SSL: reduced logging of session cache failures (ticket #621).Maxim Dounin1-2/+7
Session cache allocations might fail as long as the new session is different in size from the one least recently used (and freed when the first allocation fails). In particular, it might not be possible to allocate space for sessions with client certificates, since they are noticeably bigger than normal sessions. To ensure such allocation failures won't clutter logs, logging level changed to "warn", and logging is now limited to at most one warning per second.
2022-10-12SSL: disabled saving tickets to session cache.Maxim Dounin1-0/+17
OpenSSL tries to save TLSv1.3 sessions into session cache even when using tickets for stateless session resumption, "because some applications just want to know about the creation of a session". To avoid trashing session cache with useless data, we do not save such sessions now.
2022-09-08SSL: silenced GCC warnings when building with BoringSSL.Sergey Kandaurov1-1/+1
BoringSSL uses macro stub for SSL_CTX_set_ecdh_auto that expands to 1, which triggers -Wunused-value "statement with no effect" warnings.
2022-09-07Win32: fixed build on Windows with OpenSSL 3.0.x (ticket #2379).Maxim Dounin1-3/+3
SSL_sendfile() expects integer file descriptor as an argument, but nginx uses OS file handles (HANDLE) to work with files on Windows, and passing HANDLE instead of an integer correctly results in build failure. Since SSL_sendfile() is not expected to work on Windows anyway, the code is now disabled on Windows with appropriate compile-time checks.
2022-09-07SSL: fixed incorrect usage of #if instead of #ifdef.Maxim Dounin1-1/+1
In 2014ed60f17f, "#if SSL_CTRL_SET_ECDH_AUTO" test was incorrectly used instead of "#ifdef SSL_CTRL_SET_ECDH_AUTO". There is no practical difference, since SSL_CTRL_SET_ECDH_AUTO evaluates to a non-zero numeric value when defined, but anyway it's better to correctly test if the value is defined.
2022-10-20Merged with the default branch.Sergey Kandaurov1-114/+331
2022-08-09SSL: logging level of "bad record type" errors.Murilo Andrade1-0/+3
The SSL_R_BAD_RECORD_TYPE ("bad record type") errors are reported by OpenSSL 1.1.1 or newer when using TLSv1.3 if the client sends a record with unknown or unexpected type. These errors are now logged at the "info" level.
2022-07-12SSL: logging levels of various errors added in OpenSSL 1.1.1.Maxim Dounin1-0/+12
Starting with OpenSSL 1.1.1, various additional errors can be reported by OpenSSL in case of client-related issues, most notably during TLSv1.3 handshakes. In particular, SSL_R_BAD_KEY_SHARE ("bad key share"), SSL_R_BAD_EXTENSION ("bad extension"), SSL_R_BAD_CIPHER ("bad cipher"), SSL_R_BAD_ECPOINT ("bad ecpoint"). These are now logged at the "info" level.
2022-07-26Merged with the default branch.Sergey Kandaurov1-0/+12
2022-02-08SSL: logging level of "application data after close notify".Sergey Kandaurov1-0/+6
Such fatal errors are reported by OpenSSL 1.1.1, and similarly by BoringSSL, if application data is encountered during SSL shutdown, which started to be observed on the second SSL_shutdown() call after SSL shutdown fixes made in 09fb2135a589 (1.19.2). The error means that the client continues to send application data after receiving the "close_notify" alert (ticket #2318). Previously it was reported as SSL_shutdown() error of SSL_ERROR_SYSCALL.
2022-02-14Merged with the default branch.Sergey Kandaurov1-0/+6