NAME
SSL_get_error — obtain result code for TLS/SSL I/O operation
SYNOPSIS
#include <openssl/ssl.h>
int SSL_get_error(const SSL *ssl, int ret);
DESCRIPTION
SSL_get_error() returns a result code (suitable for the C «switch» statement) for a preceding call to SSL_connect(), SSL_accept(), SSL_do_handshake(), SSL_read_ex(), SSL_read(), SSL_peek_ex(), SSL_peek(), SSL_shutdown(), SSL_write_ex() or SSL_write() on ssl. The value returned by that TLS/SSL I/O function must be passed to SSL_get_error() in parameter ret.
In addition to ssl and ret, SSL_get_error() inspects the current thread’s OpenSSL error queue. Thus, SSL_get_error() must be used in the same thread that performed the TLS/SSL I/O operation, and no other OpenSSL function calls should appear in between. The current thread’s error queue must be empty before the TLS/SSL I/O operation is attempted, or SSL_get_error() will not work reliably.
RETURN VALUES
The following return values can currently occur:
- SSL_ERROR_NONE
-
The TLS/SSL I/O operation completed. This result code is returned if and only if ret > 0.
- SSL_ERROR_ZERO_RETURN
-
The TLS/SSL peer has closed the connection for writing by sending the close_notify alert. No more data can be read. Note that SSL_ERROR_ZERO_RETURN does not necessarily indicate that the underlying transport has been closed.
- SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE
-
The operation did not complete and can be retried later.
SSL_ERROR_WANT_READ is returned when the last operation was a read operation from a nonblocking BIO. It means that not enough data was available at this time to complete the operation. If at a later time the underlying BIO has data available for reading the same function can be called again.
SSL_read() and SSL_read_ex() can also set SSL_ERROR_WANT_READ when there is still unprocessed data available at either the SSL or the BIO layer, even for a blocking BIO. See SSL_read(3) for more information.
SSL_ERROR_WANT_WRITE is returned when the last operation was a write to a nonblocking BIO and it was unable to sent all data to the BIO. When the BIO is writable again, the same function can be called again.
Note that the retry may again lead to an SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE condition. There is no fixed upper limit for the number of iterations that may be necessary until progress becomes visible at application protocol level.
It is safe to call SSL_read() or SSL_read_ex() when more data is available even when the call that set this error was an SSL_write() or SSL_write_ex(). However, if the call was an SSL_write() or SSL_write_ex(), it should be called again to continue sending the application data.
For socket BIOs (e.g. when SSL_set_fd() was used), select() or poll() on the underlying socket can be used to find out when the TLS/SSL I/O function should be retried.
Caveat: Any TLS/SSL I/O function can lead to either of SSL_ERROR_WANT_READ and SSL_ERROR_WANT_WRITE. In particular, SSL_read_ex(), SSL_read(), SSL_peek_ex(), or SSL_peek() may want to write data and SSL_write() or SSL_write_ex() may want to read data. This is mainly because TLS/SSL handshakes may occur at any time during the protocol (initiated by either the client or the server); SSL_read_ex(), SSL_read(), SSL_peek_ex(), SSL_peek(), SSL_write_ex(), and SSL_write() will handle any pending handshakes.
- SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_ACCEPT
-
The operation did not complete; the same TLS/SSL I/O function should be called again later. The underlying BIO was not connected yet to the peer and the call would block in connect()/accept(). The SSL function should be called again when the connection is established. These messages can only appear with a BIO_s_connect() or BIO_s_accept() BIO, respectively. In order to find out, when the connection has been successfully established, on many platforms select() or poll() for writing on the socket file descriptor can be used.
- SSL_ERROR_WANT_X509_LOOKUP
-
The operation did not complete because an application callback set by SSL_CTX_set_client_cert_cb() has asked to be called again. The TLS/SSL I/O function should be called again later. Details depend on the application.
- SSL_ERROR_WANT_ASYNC
-
The operation did not complete because an asynchronous engine is still processing data. This will only occur if the mode has been set to SSL_MODE_ASYNC using SSL_CTX_set_mode(3) or SSL_set_mode(3) and an asynchronous capable engine is being used. An application can determine whether the engine has completed its processing using select() or poll() on the asynchronous wait file descriptor. This file descriptor is available by calling SSL_get_all_async_fds(3) or SSL_get_changed_async_fds(3). The TLS/SSL I/O function should be called again later. The function must be called from the same thread that the original call was made from.
- SSL_ERROR_WANT_ASYNC_JOB
-
The asynchronous job could not be started because there were no async jobs available in the pool (see ASYNC_init_thread(3)). This will only occur if the mode has been set to SSL_MODE_ASYNC using SSL_CTX_set_mode(3) or SSL_set_mode(3) and a maximum limit has been set on the async job pool through a call to ASYNC_init_thread(3). The application should retry the operation after a currently executing asynchronous operation for the current thread has completed.
- SSL_ERROR_WANT_CLIENT_HELLO_CB
-
The operation did not complete because an application callback set by SSL_CTX_set_client_hello_cb() has asked to be called again. The TLS/SSL I/O function should be called again later. Details depend on the application.
- SSL_ERROR_SYSCALL
-
Some non-recoverable, fatal I/O error occurred. The OpenSSL error queue may contain more information on the error. For socket I/O on Unix systems, consult errno for details. If this error occurs then no further I/O operations should be performed on the connection and SSL_shutdown() must not be called.
This value can also be returned for other errors, check the error queue for details.
- SSL_ERROR_SSL
-
A non-recoverable, fatal error in the SSL library occurred, usually a protocol error. The OpenSSL error queue contains more information on the error. If this error occurs then no further I/O operations should be performed on the connection and SSL_shutdown() must not be called.
BUGS
The SSL_ERROR_SYSCALL with errno value of 0 indicates unexpected EOF from the peer. This will be properly reported as SSL_ERROR_SSL with reason code SSL_R_UNEXPECTED_EOF_WHILE_READING in the OpenSSL 3.0 release because it is truly a TLS protocol error to terminate the connection without a SSL_shutdown().
The issue is kept unfixed in OpenSSL 1.1.1 releases because many applications which choose to ignore this protocol error depend on the existing way of reporting the error.
SEE ALSO
ssl(7)
HISTORY
The SSL_ERROR_WANT_ASYNC error code was added in OpenSSL 1.1.0. The SSL_ERROR_WANT_CLIENT_HELLO_CB error code was added in OpenSSL 1.1.1.
COPYRIGHT
Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
Licensed under the OpenSSL license (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.
NAME
SSL_get_error — obtain result code for TLS/SSL I/O operation
SYNOPSIS
#include <openssl/ssl.h>
int SSL_get_error(const SSL *ssl, int ret);
DESCRIPTION
SSL_get_error() returns a result code (suitable for the C «switch» statement) for a preceding call to SSL_connect(), SSL_accept(), SSL_do_handshake(), SSL_read_ex(), SSL_read(), SSL_peek_ex(), SSL_peek(), SSL_shutdown(), SSL_write_ex() or SSL_write() on ssl. The value returned by that TLS/SSL I/O function must be passed to SSL_get_error() in parameter ret.
In addition to ssl and ret, SSL_get_error() inspects the current thread’s OpenSSL error queue. Thus, SSL_get_error() must be used in the same thread that performed the TLS/SSL I/O operation, and no other OpenSSL function calls should appear in between. The current thread’s error queue must be empty before the TLS/SSL I/O operation is attempted, or SSL_get_error() will not work reliably.
NOTES
Some TLS implementations do not send a close_notify alert on shutdown.
On an unexpected EOF, versions before OpenSSL 3.0 returned SSL_ERROR_SYSCALL, nothing was added to the error stack, and errno was 0. Since OpenSSL 3.0 the returned error is SSL_ERROR_SSL with a meaningful error on the error stack.
RETURN VALUES
The following return values can currently occur:
- SSL_ERROR_NONE
-
The TLS/SSL I/O operation completed. This result code is returned if and only if ret > 0.
- SSL_ERROR_ZERO_RETURN
-
The TLS/SSL peer has closed the connection for writing by sending the close_notify alert. No more data can be read. Note that SSL_ERROR_ZERO_RETURN does not necessarily indicate that the underlying transport has been closed.
This error can also appear when the option SSL_OP_IGNORE_UNEXPECTED_EOF is set. See SSL_CTX_set_options(3) for more details.
- SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE
-
The operation did not complete and can be retried later.
For non-QUIC SSL objects, SSL_ERROR_WANT_READ is returned when the last operation was a read operation from a nonblocking BIO. It means that not enough data was available at this time to complete the operation. If at a later time the underlying BIO has data available for reading the same function can be called again.
SSL_read() and SSL_read_ex() can also set SSL_ERROR_WANT_READ when there is still unprocessed data available at either the SSL or the BIO layer, even for a blocking BIO. See SSL_read(3) for more information.
For non-QUIC SSL objects, SSL_ERROR_WANT_WRITE is returned when the last operation was a write to a nonblocking BIO and it was unable to sent all data to the BIO. When the BIO is writable again, the same function can be called again.
Note that the retry may again lead to an SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE condition. There is no fixed upper limit for the number of iterations that may be necessary until progress becomes visible at application protocol level.
For QUIC SSL objects, the meaning of SSL_ERROR_WANT_READ and SSL_ERROR_WANT_WRITE have different but largely compatible semantics. Since QUIC implements its own flow control and uses UDP datagrams, backpressure conditions in terms of the underlying BIO providing network I/O are not directly relevant to the circumstances in which these errors are produced. In particular, SSL_ERROR_WANT_WRITE indicates that the OpenSSL internal send buffer for a given QUIC stream has been filled. Likewise, SSL_ERROR_WANT_READ indicates that the OpenSSL internal receive buffer for a given QUIC stream is empty.
It is safe to call SSL_read() or SSL_read_ex() when more data is available even when the call that set this error was an SSL_write() or SSL_write_ex(). However, if the call was an SSL_write() or SSL_write_ex(), it should be called again to continue sending the application data. If you get SSL_ERROR_WANT_WRITE from SSL_write() or SSL_write_ex() then you should not do any other operation that could trigger IO other than to repeat the previous SSL_write() call.
For socket BIOs (e.g. when SSL_set_fd() was used), select() or poll() on the underlying socket can be used to find out when the TLS/SSL I/O function should be retried.
Caveat: Any TLS/SSL I/O function can lead to either of SSL_ERROR_WANT_READ and SSL_ERROR_WANT_WRITE. In particular, SSL_read_ex(), SSL_read(), SSL_peek_ex(), or SSL_peek() may want to write data and SSL_write() or SSL_write_ex() may want to read data. This is mainly because TLS/SSL handshakes may occur at any time during the protocol (initiated by either the client or the server); SSL_read_ex(), SSL_read(), SSL_peek_ex(), SSL_peek(), SSL_write_ex(), and SSL_write() will handle any pending handshakes.
- SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_ACCEPT
-
The operation did not complete; the same TLS/SSL I/O function should be called again later. The underlying BIO was not connected yet to the peer and the call would block in connect()/accept(). The SSL function should be called again when the connection is established. These messages can only appear with a BIO_s_connect() or BIO_s_accept() BIO, respectively. In order to find out, when the connection has been successfully established, on many platforms select() or poll() for writing on the socket file descriptor can be used.
- SSL_ERROR_WANT_X509_LOOKUP
-
The operation did not complete because an application callback set by SSL_CTX_set_client_cert_cb() has asked to be called again. The TLS/SSL I/O function should be called again later. Details depend on the application.
- SSL_ERROR_WANT_ASYNC
-
The operation did not complete because an asynchronous engine is still processing data. This will only occur if the mode has been set to SSL_MODE_ASYNC using SSL_CTX_set_mode(3) or SSL_set_mode(3) and an asynchronous capable engine is being used. An application can determine whether the engine has completed its processing using select() or poll() on the asynchronous wait file descriptor. This file descriptor is available by calling SSL_get_all_async_fds(3) or SSL_get_changed_async_fds(3). The TLS/SSL I/O function should be called again later. The function must be called from the same thread that the original call was made from.
- SSL_ERROR_WANT_ASYNC_JOB
-
The asynchronous job could not be started because there were no async jobs available in the pool (see ASYNC_init_thread(3)). This will only occur if the mode has been set to SSL_MODE_ASYNC using SSL_CTX_set_mode(3) or SSL_set_mode(3) and a maximum limit has been set on the async job pool through a call to ASYNC_init_thread(3). The application should retry the operation after a currently executing asynchronous operation for the current thread has completed.
- SSL_ERROR_WANT_CLIENT_HELLO_CB
-
The operation did not complete because an application callback set by SSL_CTX_set_client_hello_cb() has asked to be called again. The TLS/SSL I/O function should be called again later. Details depend on the application.
- SSL_ERROR_SYSCALL
-
Some non-recoverable, fatal I/O error occurred. The OpenSSL error queue may contain more information on the error. For socket I/O on Unix systems, consult errno for details. If this error occurs then no further I/O operations should be performed on the connection and SSL_shutdown() must not be called.
This value can also be returned for other errors, check the error queue for details.
- SSL_ERROR_SSL
-
A non-recoverable, fatal error in the SSL library occurred, usually a protocol error. The OpenSSL error queue contains more information on the error. If this error occurs then no further I/O operations should be performed on the connection and SSL_shutdown() must not be called.
SEE ALSO
ssl(7)
HISTORY
The SSL_ERROR_WANT_ASYNC error code was added in OpenSSL 1.1.0. The SSL_ERROR_WANT_CLIENT_HELLO_CB error code was added in OpenSSL 1.1.1.
COPYRIGHT
Copyright 2000-2021 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.
In the below example a TLS 1.3 connection will return SSL_ERROR_WANT_READ
when calling SSL_write
. Changing from TLS_method
to TLSv1_2_method
eliminates the error, suggesting that the issue is within the TLS 1.3 implementation.
#include <assert.h> #include <fcntl.h> #include <arpa/inet.h> #include <sys/socket.h> #include <stdbool.h> #include <string.h> #include <openssl/err.h> #include <openssl/ssl.h> char SERVER_KEY[] = "-----BEGIN RSA PRIVATE KEY-----n" "MIICWwIBAAKBgQC+pvhuud1dLaQQvzipdtlcTotgr5SuE2LvSx0gz/bg1U3u1eQ+n" "U5eqsxaEUceaX5p5Kk+QflvW8qdjVNxQuYS5uc0gK2+OZnlIYxCf4n5GYGzVIx3Qn" "SBj/TAEFB2WuVinZBiCbxgL7PFM1Kpa+EwVkCAduPpSflJJPwkYGrK2MHQIDAQABn" "AoGAbwuZ0AR6JveahBaczjfnSpiFHf+mve2UxoQdpyr6ROJ4zg/PLW5K/KXrC48Gn" "j6f3tXMrfKHcpEoZrQWUfYBRCUsGD5DCazEhD8zlxEHahIsqpwA0WWssJA2VOLENn" "j6DuV2pCFbw67rfTBkTSo32ahfXxEKev5KswZk0JIzH3ooECQQDgzS9AI89h0gs8n" "Dt+1m11Rzqo3vZML7ZIyGApUzVan+a7hbc33nbGRkAXjHaUBJO31it/H6dTO+uwXn" "msWwNG5ZAkEA2RyFKs5xR5USTFaKLWCgpH/ydV96KPOpBND7TKQx62snDenFNNbnn" "FwwOhpahld+vqhYk+pfuWWUpQciE+Bu7ZQJASjfT4sQv4qbbKK/scePicnDdx9thn" "4e1EeB9xwb+tXXXUo/6Bor/AcUNwfiQ6Zt9PZOK9sR3lMZSsP7rMi7kzuQJABie6n" "1sXXjFH7nNJvRG4S39cIxq8YRYTy68II/dlB2QzGpKxV/POCxbJ/zu0CU79tuYK7n" "NaeNCFfH3aeTrX0LyQJAMBWjWmeKM2G2sCExheeQK0ROnaBC8itCECD4Jsve4nqfn" "r50+LF74iLXFwqysVCebPKMOpDWp/qQ1BbJQIPs7/A==n" "-----END RSA PRIVATE KEY-----"; char SERVER_CERT[] = "-----BEGIN CERTIFICATE-----n" "MIICKDCCAZGgAwIBAgIJAJn/HpR21r/8MA0GCSqGSIb3DQEBBQUAMFgxCzAJBgNVn" "BAYTAlVTMQswCQYDVQQIEwJJTDEQMA4GA1UEBxMHQ2hpY2FnbzEQMA4GA1UEChMHn" "VGVzdGluZzEYMBYGA1UEAxMPVGVzdGluZyBSb290IENBMCIYDzIwMDkwMzI1MTIzn" "NzUzWhgPMjAxNzA2MTExMjM3NTNaMBgxFjAUBgNVBAMTDWxvdmVseSBzZXJ2ZXIwn" "gZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAL6m+G653V0tpBC/OKl22VxOi2Cvn" "lK4TYu9LHSDP9uDVTe7V5D5Tl6qzFoRRx5pfmnkqT5B+W9byp2NU3FC5hLm5zSArn" "b45meUhjEJ/ifkZgbNUjHdBIGP9MAQUHZa5WKdkGIJvGAvs8UzUqlr4TBWQIB24+n" "lJ+Ukk/CRgasrYwdAgMBAAGjNjA0MB0GA1UdDgQWBBS4kC7Ij0W1TZXZqXQFAM2en" "gKEG2DATBgNVHSUEDDAKBggrBgEFBQcDATANBgkqhkiG9w0BAQUFAAOBgQBh30Lin" "dJ+NlxIOx5343WqIBka3UbsOb2kxWrbkVCrvRapCMLCASO4FqiKWM+L0VDBprqIpn" "2mgpFQ6FHpoIENGvJhdEKpptQ5i7KaGhnDNTfdy3x1+h852G99f1iyj0RmbuFcM8n" "uzujnS8YXWvM7DM1Ilozk4MzPug8jzFp5uhKCQ==n" "-----END CERTIFICATE-----"; void assert_errno(bool condition, char *msg) { if (!condition) { perror(msg); exit(1); } } void assert_openssl(bool condition) { if (!condition) { printf("OpenSSL errorn"); exit(1); } } int get_listening_port(int listener) { struct sockaddr_in server_bound_addr; socklen_t len = sizeof(server_bound_addr); int result = getsockname(listener, (struct sockaddr *)&server_bound_addr, &len); assert_errno(result != -1, "getsockname"); return ntohs(server_bound_addr.sin_port); } EVP_PKEY *load_privatekey(char *key_bytes) { BIO *bio = BIO_new_mem_buf(key_bytes, strlen(key_bytes)); EVP_PKEY *key = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL); assert_openssl(key != NULL); return key; } X509 *load_certificate(char *cert_bytes) { BIO *bio = BIO_new_mem_buf(cert_bytes, strlen(cert_bytes)); X509 *cert = PEM_read_bio_X509(bio, NULL, NULL, NULL); assert_openssl(cert != NULL); return cert; } void create_loopback(SSL **server, SSL **client) { int result; int listener = socket(AF_INET, SOCK_STREAM, 0); assert_errno(listener != -1, "socket"); struct sockaddr_in server_addr; server_addr.sin_port = 0; server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_family = AF_INET; result = bind(listener, (struct sockaddr *)&server_addr, sizeof(server_addr)); assert_errno(result != -1, "bind"); result = listen(listener, 1); assert_errno(result != -1, "listen"); int client_sock = socket(AF_INET, SOCK_STREAM, 0); assert_errno(client_sock != -1, "socket"); struct sockaddr_in dest_addr; dest_addr.sin_port = htons(get_listening_port(listener)); inet_aton("127.0.0.1", &dest_addr.sin_addr); dest_addr.sin_family = AF_INET; result = connect(client_sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr)); assert_errno(result != -1, "connect"); int server_sock = accept(listener, NULL, NULL); assert_errno(server_sock != -1, "accept"); SSL_CTX *server_ctx = SSL_CTX_new(TLS_method()); assert_openssl(server_ctx != NULL); SSL_CTX_use_PrivateKey(server_ctx, load_privatekey(SERVER_KEY)); SSL_CTX_use_certificate(server_ctx, load_certificate(SERVER_CERT)); *server = SSL_new(server_ctx); assert_openssl(*server != NULL); SSL_set_fd(*server, server_sock); SSL_set_accept_state(*server); SSL_CTX *client_ctx = SSL_CTX_new(TLS_method()); assert_openssl(client_ctx != NULL); *client = SSL_new(client_ctx); assert_openssl(*client != NULL); SSL_set_fd(*client, client_sock); SSL_set_connect_state(*client); } void set_blocking(SSL *s, bool blocking) { int fd = SSL_get_fd(s); int flags = fcntl(fd, F_GETFL, 0); assert_errno(flags != -1, "fcntl"); if (blocking) { flags = flags & (~O_NONBLOCK); } else { flags |= O_NONBLOCK; } flags = fcntl(fd, F_SETFL, flags); assert_errno(flags != -1, "fcntl"); } void handshake(SSL *client, SSL *server) { set_blocking(client, false); set_blocking(server, false); SSL *conns[] = {client, server}; int nconns = 2; while (nconns) { for (size_t i = 0; i < 2; i++) { if (conns[i] == NULL) { continue; } int result = SSL_do_handshake(conns[i]); int error = SSL_get_error(conns[i], result); if (error == SSL_ERROR_NONE) { conns[i] = NULL; nconns--; } else if (error != SSL_ERROR_WANT_READ) { assert_openssl(false); } } } set_blocking(client, true); set_blocking(server, true); } int main() { SSL *server, *client; create_loopback(&server, &client); handshake(client, server); printf("Writing...n"); int result = SSL_write(server, "xyz", 3); assert_openssl(result == 3); char out[4]; printf("Reading...n"); result = SSL_read(client, out, 3); if (result != 3) { result = SSL_get_error(client, result); printf("OpenSSL error: %dn", result); exit(1); } }
obtain result code for TLS/SSL I/O operation
Synopsis
#include <openssl/ssl.h> int SSL_get_error(const SSL *ssl, int ret);
Description
SSL_get_error() returns a result code (suitable for the C «switch» statement) for a preceding call to SSL_connect(), SSL_accept(), SSL_do_handshake(), SSL_read_ex(), SSL_read(), SSL_peek_ex(), SSL_peek(), SSL_shutdown(), SSL_write_ex() or SSL_write() on ssl. The value returned by that TLS/SSL I/O function must be passed to SSL_get_error() in parameter ret.
In addition to ssl and ret, SSL_get_error() inspects the current thread’s OpenSSL error queue. Thus, SSL_get_error() must be used in the same thread that performed the TLS/SSL I/O operation, and no other OpenSSL function calls should appear in between. The current thread’s error queue must be empty before the TLS/SSL I/O operation is attempted, or SSL_get_error() will not work reliably.
Return Values
The following return values can currently occur:
- SSL_ERROR_NONE
-
The TLS/SSL I/O operation completed. This result code is returned if and only if ret > 0.
- SSL_ERROR_ZERO_RETURN
-
The TLS/SSL peer has closed the connection for writing by sending the close_notify alert. No more data can be read. Note that SSL_ERROR_ZERO_RETURN does not necessarily indicate that the underlying transport has been closed.
- SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE
-
The operation did not complete and can be retried later.
SSL_ERROR_WANT_READ is returned when the last operation was a read operation from a nonblocking BIO. It means that not enough data was available at this time to complete the operation. If at a later time the underlying BIO has data available for reading the same function can be called again.
SSL_read() and SSL_read_ex() can also set SSL_ERROR_WANT_READ when there is still unprocessed data available at either the SSL or the BIO layer, even for a blocking BIO. See SSL_read(3) for more information.
SSL_ERROR_WANT_WRITE is returned when the last operation was a write to a nonblocking BIO and it was unable to sent all data to the BIO. When the BIO is writable again, the same function can be called again.
Note that the retry may again lead to an SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE condition. There is no fixed upper limit for the number of iterations that may be necessary until progress becomes visible at application protocol level.
It is safe to call SSL_read() or SSL_read_ex() when more data is available even when the call that set this error was an SSL_write() or SSL_write_ex(). However, if the call was an SSL_write() or SSL_write_ex(), it should be called again to continue sending the application data.
For socket BIOs (e.g. when SSL_set_fd() was used), select() or poll() on the underlying socket can be used to find out when the TLS/SSL I/O function should be retried.
Caveat: Any TLS/SSL I/O function can lead to either of SSL_ERROR_WANT_READ and SSL_ERROR_WANT_WRITE. In particular, SSL_read_ex(), SSL_read(), SSL_peek_ex(), or SSL_peek() may want to write data and SSL_write() or SSL_write_ex() may want to read data. This is mainly because TLS/SSL handshakes may occur at any time during the protocol (initiated by either the client or the server); SSL_read_ex(), SSL_read(), SSL_peek_ex(), SSL_peek(), SSL_write_ex(), and SSL_write() will handle any pending handshakes.
- SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_ACCEPT
-
The operation did not complete; the same TLS/SSL I/O function should be called again later. The underlying BIO was not connected yet to the peer and the call would block in connect()/accept(). The SSL function should be called again when the connection is established. These messages can only appear with a BIO_s_connect() or BIO_s_accept() BIO, respectively. In order to find out, when the connection has been successfully established, on many platforms select() or poll() for writing on the socket file descriptor can be used.
- SSL_ERROR_WANT_X509_LOOKUP
-
The operation did not complete because an application callback set by SSL_CTX_set_client_cert_cb() has asked to be called again. The TLS/SSL I/O function should be called again later. Details depend on the application.
- SSL_ERROR_WANT_ASYNC
-
The operation did not complete because an asynchronous engine is still processing data. This will only occur if the mode has been set to SSL_MODE_ASYNC using SSL_CTX_set_mode(3) or SSL_set_mode(3) and an asynchronous capable engine is being used. An application can determine whether the engine has completed its processing using select() or poll() on the asynchronous wait file descriptor. This file descriptor is available by calling SSL_get_all_async_fds(3) or SSL_get_changed_async_fds(3). The TLS/SSL I/O function should be called again later. The function must be called from the same thread that the original call was made from.
- SSL_ERROR_WANT_ASYNC_JOB
-
The asynchronous job could not be started because there were no async jobs available in the pool (see ASYNC_init_thread(3)). This will only occur if the mode has been set to SSL_MODE_ASYNC using SSL_CTX_set_mode(3) or SSL_set_mode(3) and a maximum limit has been set on the async job pool through a call to ASYNC_init_thread(3). The application should retry the operation after a currently executing asynchronous operation for the current thread has completed.
- SSL_ERROR_WANT_CLIENT_HELLO_CB
-
The operation did not complete because an application callback set by SSL_CTX_set_client_hello_cb() has asked to be called again. The TLS/SSL I/O function should be called again later. Details depend on the application.
- SSL_ERROR_SYSCALL
-
Some non-recoverable, fatal I/O error occurred. The OpenSSL error queue may contain more information on the error. For socket I/O on Unix systems, consult errno for details. If this error occurs then no further I/O operations should be performed on the connection and SSL_shutdown() must not be called.
This value can also be returned for other errors, check the error queue for details.
- SSL_ERROR_SSL
-
A non-recoverable, fatal error in the SSL library occurred, usually a protocol error. The OpenSSL error queue contains more information on the error. If this error occurs then no further I/O operations should be performed on the connection and SSL_shutdown() must not be called.
Bugs
The SSL_ERROR_SYSCALL with errno value of 0 indicates unexpected EOF from the peer. This will be properly reported as SSL_ERROR_SSL with reason code SSL_R_UNEXPECTED_EOF_WHILE_READING in the OpenSSL 3.0 release because it is truly a TLS protocol error to terminate the connection without a SSL_shutdown().
The issue is kept unfixed in OpenSSL 1.1.1 releases because many applications which choose to ignore this protocol error depend on the existing way of reporting the error.
See Also
ssl(7)
History
The SSL_ERROR_WANT_ASYNC error code was added in OpenSSL 1.1.0. The SSL_ERROR_WANT_CLIENT_HELLO_CB error code was added in OpenSSL 1.1.1.
Copyright
Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
Licensed under the OpenSSL license (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>.
Referenced By
DTLSv1_listen.3ssl(3), SSL_accept.3ssl(3), SSL_connect.3ssl(3), SSL_CTX_set_cert_cb.3ssl(3), SSL_CTX_set_client_cert_cb.3ssl(3), SSL_CTX_set_client_hello_cb.3ssl(3), SSL_CTX_set_mode.3ssl(3), SSL_do_handshake.3ssl(3), SSL_get_all_async_fds.3ssl(3), SSL_read.3ssl(3), SSL_read_early_data.3ssl(3), SSL_shutdown.3ssl(3), SSL_want.3ssl(3), SSL_write.3ssl(3).
2023-01-19 1.1.1q OpenSSL
������������� ������� ��������� ��������� ���������� (man-��)
SSL_get_error (3)
NAME
SSL_get_error - obtain result code for TLS/SSL I/O operation
SYNOPSIS
#include <openssl/ssl.h>
int SSL_get_error(const SSL *ssl, int ret);
DESCRIPTION
SSL_get_error() returns a result code (suitable for the C «switch»
statement) for a preceding call to SSL_connect(), SSL_accept(), SSL_do_handshake(),
SSL_read(), SSL_peek(), or SSL_write() on ssl. The value returned by
that TLS/SSL I/O function must be passed to SSL_get_error() in parameter
ret.
In addition to ssl and ret, SSL_get_error() inspects the
current thread’s OpenSSL error queue. Thus, SSL_get_error() must be
used in the same thread that performed the TLS/SSL I/O operation, and no
other OpenSSL function calls should appear in between. The current
thread’s error queue must be empty before the TLS/SSL I/O operation is
attempted, or SSL_get_error() will not work reliably.
RETURN VALUES
The following return values can currently occur:
- SSL_ERROR_NONE
-
The TLS/SSL I/O operation completed. This result code is returned
if and only if ret > 0. - SSL_ERROR_ZERO_RETURN
-
The TLS/SSL connection has been closed. If the protocol version is SSL 3.0
or TLS 1.0, this result code is returned only if a closure
alert has occurred in the protocol, i.e. if the connection has been
closed cleanly. Note that in this case SSL_ERROR_ZERO_RETURN
does not necessarily indicate that the underlying transport
has been closed. - SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE
-
The operation did not complete; the same TLS/SSL I/O function should be
called again later. If, by then, the underlying BIO has data
available for reading (if the result code is SSL_ERROR_WANT_READ)
or allows writing data (SSL_ERROR_WANT_WRITE), then some TLS/SSL
protocol progress will take place, i.e. at least part of an TLS/SSL
record will be read or written. Note that the retry may again lead to
a SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE condition.
There is no fixed upper limit for the number of iterations that
may be necessary until progress becomes visible at application
protocol level.For socket BIOs (e.g. when SSL_set_fd() was used), select() or
poll() on the underlying socket can be used to find out when the
TLS/SSL I/O function should be retried.Caveat: Any TLS/SSL I/O function can lead to either of
SSL_ERROR_WANT_READ and SSL_ERROR_WANT_WRITE. In particular,
SSL_read() or SSL_peek() may want to write data and SSL_write() may want
to read data. This is mainly because TLS/SSL handshakes may occur at any
time during the protocol (initiated by either the client or the server);
SSL_read(), SSL_peek(), and SSL_write() will handle any pending handshakes. - SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_ACCEPT
-
The operation did not complete; the same TLS/SSL I/O function should be
called again later. The underlying BIO was not connected yet to the peer
and the call would block in connect()/accept(). The SSL function should be
called again when the connection is established. These messages can only
appear with a BIO_s_connect() or BIO_s_accept() BIO, respectively.
In order to find out, when the connection has been successfully established,
on many platforms select() or poll() for writing on the socket file descriptor
can be used. - SSL_ERROR_WANT_X509_LOOKUP
-
The operation did not complete because an application callback set by
SSL_CTX_set_client_cert_cb() has asked to be called again.
The TLS/SSL I/O function should be called again later.
Details depend on the application. - SSL_ERROR_SYSCALL
-
Some I/O error occurred. The OpenSSL error queue may contain more
information on the error. If the error queue is empty
(i.e. ERR_get_error() returns 0), ret can be used to find out more
about the error: If ret == 0, an EOF was observed that violates
the protocol. If ret == -1, the underlying BIO reported an
I/O error (for socket I/O on Unix systems, consult errno for details). - SSL_ERROR_SSL
-
A failure in the SSL library occurred, usually a protocol error. The
OpenSSL error queue contains more information on the error.
SEE ALSO
ssl(3), err(3)
HISTORY
SSL_get_error() was added in SSLeay 0.8.
Index
- NAME
- SYNOPSIS
- DESCRIPTION
- RETURN VALUES
- SEE ALSO
- HISTORY
SSL (Secure Socket Layer) or its successor TLS (Transport Layer Security) are protocols to facilitate end-to-end security. These protocols are used when accessing web sites (https), delivering or retrieving email, and in lots of other use cases. In the following documentation we will refer to both SSL and TLS as simply ‘SSL’.
When you debug SSL issues, you can use Linux curl command to test. In this article, we will cover the following topics:
- Curl and its backend OpenSSL
- Which CA certificate was used
- Unknown SSL protocol error
- Useful Tools for SSL Debugging
Curl and Its Backend OpenSSL
Depending on which working SSL library that your curl was built with, curl command may sometimes fail to negotiate SSL handshake because of older version of OpenSSL. So, the first thing to check is which OpenSSL your curl command is built with by typing:
$ curl —version
curl 7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.51.0 OpenSSL/1.0.2j zlib/1.2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile NTLM SSL libz
In this curl, it is built with OpenSSL 1.0.2j.
Specify CA Certificate with —cacert Option
You can specify which CA certificate to verify peer against by using
—cacert
$ curl -v —cacert /u01/data/secure_artifacts/ssl/certs/cert-8b3a107e0d914aed91828f4414ad69f0_USPSRDEVCERTSIGN.pem https://10.240.81.24:3651
* Rebuilt URL to: https://10.240.81.24:3651/
* Trying 10.240.81.24…
* TCP_NODELAY set
* Connected to 10.240.81.24 (10.240.81.24) port 3651 (#0)
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
* CAfile: /u01/data/secure_artifacts/ssl/certs/cert-8b3a107e0d914aed91828f4414ad69f0_USPSRDEVCERTSIGN.pem
CApath: none
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* Unknown SSL protocol error in connection to 10.240.81.24:3651
* Curl_http_done: called premature == 1
* Closing connection 0
Default Certificate
If you don’t specify a CA certificate, the default certificate is used. Notice that the default certificate used below is:
/etc/pki/tls/certs/ca-bundle.crt
$ curl -v https://10.240.81.24:3651
* Rebuilt URL to: https://10.240.81.24:3651/
* Trying 10.240.81.24…
* TCP_NODELAY set
* Connected to 10.240.81.24 (10.240.81.24) port 3651 (#0)
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
* CAfile: /etc/pki/tls/certs/ca-bundle.crt
CApath: none
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* Unknown SSL protocol error in connection to 10.240.81.24:3651
* Curl_http_done: called premature == 1
* Closing connection 0
Unknown SSL protocol
From both curl commands’ output, we have found the following error:
* Unknown SSL protocol error in connection to 10.240.81.24:3651
* Curl_http_done: called premature == 1
The reason turns out to be that listen port 3651 on server 10.240.81.24 is non-SSL.
Useful Tools for SSL Debugging
Often an error message alone is not sufficient to solve the problem. In this case the following tools can be of help:[2]
- SSLLabs can be used to check problems with public accessible HTTPS servers. It shows problems about certificate verification and also about potential problems with specific SSL clients.
- In case it is not https or the server is not public accessible analyze.pl from my SSL tools can help. It can be used to debug SSL problems with plain SSL or explicit SSL on SMTP, IMAP, POP3 and FTPS and with HTTP proxies.
- openssl helps with debugging too, especially with the s_client, s_server and x509 commands.
- And wireshark can be used to analyse packet captures done by tcpdump or wireshark. It is able to show lots of details about the SSL handshake.
For example, below outputs are from analyze.pl:
D:Todo>perl analyze-ssl.pl —all-ciphers -v3 10.240.24.81:3651
+ checking host=10.240.24.81(10.240.24.81) port=3651
* version SSLv23, no verification, ciphers= -> FAIL! SSL wants a read first
* version SSLv23, no verification, ciphers=HIGH:ALL -> FAIL! SSL wants a read first
* version TLSv1_2, no verification, ciphers= -> FAIL! SSL wants a read first
* version TLSv1_2, no verification, ciphers=HIGH:ALL -> FAIL! SSL wants a read first
* version TLSv1_1, no verification, ciphers= -> FAIL! SSL wants a read first
* version TLSv1_1, no verification, ciphers=HIGH:ALL -> FAIL! SSL wants a read first
* version TLSv1, no verification, ciphers= -> FAIL! SSL wants a read first
* version TLSv1, no verification, ciphers=HIGH:ALL -> FAIL! SSL wants a read first
* version SSLv3, no verification, ciphers= -> FAIL! SSL wants a read first
* version SSLv3, no verification, ciphers=HIGH:ALL -> FAIL! SSL wants a read first
10.240.24.81 failed basic SSL connect: SSL wants a read first
Note that the SSL_ERROR_WANT_READ means SSL handshake between client and server didn’t complete. In our case, this is because listen port 3651 is non-SSL.
References
- how to install curl and libcurl
- SSL/TLS — Typical problems and how to debug them
- Debugging SSL Handshake
- CentOS PHP cURL NSS error 5938
- Curl — SSL certificate problem
- Nginx server closes ssl connection for some clients
- Problem with Client SSL certificates
- SSL routines:SSL23_WRITE:ssl handshake failure
- Nginx 499 error codes
- Debugging Nginx Configuration
- Making curl not use default certificates
- curl: (35) Unknown SSL protocol error in connection
- Configuring a Plug-In for One-Way SSL
- Configuring Two-Way SSL Between the Plug-In and Oracle WebLogic Server