Search code examples
openssltls1.2

How to read the openssl alert messages?


I am trying to read OpenSSL alert messages programmatically, but can't find out the way how to do it. The OpenSSL API provides functions like:

const char *SSL_alert_type_string(int value);
const char *SSL_alert_type_string_long(int value);

const char *SSL_alert_desc_string(int value);
const char *SSL_alert_desc_string_long(int value);

but there is no info where to get the "int value" from.

For example when the TLS conenction resets from the reason of expired certificate, there is an alert (45). How to receive this alert number, so I could use the above API to print a message?


Solution

  • You can obtain the alert information code by setting the callback function with void SSL_CTX_set_info_callback(SSL_CTX *ctx, void (*callback)()); and handling those codes however you would like.

    More information on this feature is in the OpenSSL documentation. There is a complete callback function in the example section of this page. It's declaration is void apps_ssl_info_callback(SSL *s, int where, int ret), where ret is the code (given where is also appropriately set, please consult the Notes section for more thorough explanation).

    Examples of registering the callback are in the s_client and s_server applications.