SSL_send_fatal_alert¶
NAME¶
SSL_send_fatal_alert - send a fatal alert and abort a TLS or DTLS connection
SYNOPSIS¶
#include <openssl/ssl.h>
int SSL_send_fatal_alert(SSL *s, uint8_t alert);
DESCRIPTION¶
SSL_send_fatal_alert() sends a fatal alert with the description alert to the peer of the TLS or DTLS connection s and puts the connection into the same failed state as when libssl itself detects a fatal error. alert should be one of the SSL_AD_* alert descriptions, for example SSL_AD_ACCESS_DENIED or SSL_AD_INTERNAL_ERROR.
This function is intended for applications that detect a fatal error that the TLS implementation cannot see, such as an application level integrity check failing or a certificate pinning check failing after the handshake. RFC 9846 recommends that an implementation that encounters a fatal error sends an appropriate fatal alert and requires that it closes the connection.
Once SSL_send_fatal_alert() has been called, the connection cannot be used any more: subsequent calls to SSL_read_ex(3), SSL_write_ex(3), SSL_do_handshake(3) and related functions fail, and SSL_shutdown(3) fails without sending a close_notify alert. The session is removed from the session cache and cannot be resumed. For DTLS the retransmission timer is stopped, so DTLSv1_handle_timeout(3) does not retransmit any handshake messages after the alert. The application should then close the underlying transport.
The function may be called after the handshake has completed, or during the handshake between calls to SSL_do_handshake(3), SSL_connect(3) or SSL_accept(3), for example when using nonblocking I/O. Calls from within any callback invoked by libssl, for example a certificate verification callback, an info callback set with SSL_CTX_set_info_callback(3), a message callback set with SSL_CTX_set_msg_callback(3) or a record padding callback set with SSL_CTX_set_record_padding_callback(3), must not be made. A call made while the handshake state machine is running is detected and fails, but calls from other callbacks, such as those invoked by the record layer, are not detected. Callbacks that run during the handshake have their own means of aborting it with an alert. During the handshake the alert is protected with the keys that are current for the sender, so a peer that has moved to different keys, or that expects a different record version, may report a record layer error rather than the alert itself.
If an earlier call to SSL_write_ex(3) left a record only partially written because of a nonblocking write BIO, that record is written out first, followed by the alert. If the alert cannot be written and flushed immediately, SSL_send_fatal_alert() returns a value less than or equal to 0 and SSL_get_error(3) returns SSL_ERROR_WANT_WRITE. The connection is nevertheless already in the failed state. The application should call SSL_send_fatal_alert() again, with the same alert, once the write BIO is writable, to finish sending the alert. Such a retry is the only call that is accepted on a connection that has already failed; calling it with a different alert at that point fails.
The alert description must be valid for the protocol version in use. The closure alerts SSL_AD_CLOSE_NOTIFY and SSL_AD_USER_CANCELLED are not error alerts and are rejected; use SSL_shutdown(3) to close a connection cleanly. The function fails if a fatal alert has already been sent, if the connection has already failed, or if a close_notify alert has already been sent, and the state of the connection is then left unchanged. This includes a connection on which libssl itself detected a fatal error but could not yet write its own alert because the write BIO blocked: that alert is not sent by SSL_send_fatal_alert(), and no other alert is sent in its place.
SSL_send_fatal_alert() is not supported for QUIC connection or stream objects, which signal errors with a CONNECTION_CLOSE frame instead; see SSL_shutdown_ex(3).
RETURN VALUES¶
SSL_send_fatal_alert() returns 1 if the alert has been written to the write BIO and the write BIO has been flushed. It returns a value less than or equal to 0 if the alert could not be sent yet or if an error occurred. In that case SSL_get_error(3) should be called to find out whether the operation should be retried (SSL_ERROR_WANT_WRITE) or failed (SSL_ERROR_SSL or SSL_ERROR_SYSCALL).
SEE ALSO¶
ssl(7), SSL_shutdown(3), SSL_shutdown_ex(3), SSL_get_error(3), SSL_alert_type_string(3), SSL_CTX_set_info_callback(3)
HISTORY¶
The SSL_send_fatal_alert() function was added in OpenSSL 4.2.
COPYRIGHT¶
Copyright 2026 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.