From d8b399cfc9f950fb87cc3ca36834329808632810 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Sat, 24 Nov 2018 21:58:07 +0000 Subject: [PATCH] New coap_show_tls_version() and coap_string_tls_version() functions These new functions can be used to either update a buffer with, or coap_log() out the current runtime TLS library along with the version of the library that libcoap was built against. include/coap2/debug.h src/debug.c libcoap-2.map libcoap-2.sym Support for the new functions coap_show_tls_version() and coap_string_tls_version(). --- include/coap2/debug.h | 18 +++++++++ libcoap-2.map | 2 + libcoap-2.sym | 2 + src/debug.c | 88 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+) diff --git a/include/coap2/debug.h b/include/coap2/debug.h index bcc01df4..e57865b5 100644 --- a/include/coap2/debug.h +++ b/include/coap2/debug.h @@ -145,6 +145,24 @@ void coap_set_show_pdu_output(int use_fprintf); */ void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu); +/** + * Display the current (D)TLS library linked with and built for version. + * + * @param level The required minimum logging level. + */ +void coap_show_tls_version(coap_log_t level); + +/** + * Build a string containing the current (D)TLS library linked with and + * built for version. + * + * @param buffer The buffer to put the string into. + * @param bufsize The size of the buffer to put the string into. + * + * @return A pointer to the provided buffer. + */ +char *coap_string_tls_version(char *buffer, size_t bufsize); + struct coap_address_t; /** diff --git a/libcoap-2.map b/libcoap-2.map index 3ce7adb3..a7baaf0c 100644 --- a/libcoap-2.map +++ b/libcoap-2.map @@ -173,11 +173,13 @@ global: coap_set_log_level; coap_set_show_pdu_output; coap_show_pdu; + coap_show_tls_version; coap_socket_strerror; coap_split_path; coap_split_query; coap_split_uri; coap_startup; + coap_string_tls_version; coap_subscription_init; coap_ticks; coap_ticks_from_rt_us; diff --git a/libcoap-2.sym b/libcoap-2.sym index 15918eda..05bf66a1 100644 --- a/libcoap-2.sym +++ b/libcoap-2.sym @@ -171,11 +171,13 @@ coap_set_log_handler coap_set_log_level coap_set_show_pdu_output coap_show_pdu +coap_show_tls_version coap_socket_strerror coap_split_path coap_split_query coap_split_uri coap_startup +coap_string_tls_version coap_subscription_init coap_ticks coap_ticks_from_rt_us diff --git a/src/debug.c b/src/debug.c index efbc8242..f89926ca 100644 --- a/src/debug.c +++ b/src/debug.c @@ -648,6 +648,94 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) { COAP_DO_SHOW_OUTPUT_LINE; } +void coap_show_tls_version(coap_log_t level) +{ + char buffer[64]; + coap_string_tls_version(buffer, sizeof(buffer)); + coap_log(level, "%s\n", buffer); +} + +char *coap_string_tls_version(char *buffer, size_t bufsize) +{ + coap_tls_version_t *tls_version = coap_get_tls_library_version(); + char beta[8]; + char sub[2]; + char b_beta[8]; + char b_sub[2]; + + switch (tls_version->type) { + case COAP_TLS_LIBRARY_NOTLS: + snprintf(buffer, bufsize, "TLS Library: None"); + break; + case COAP_TLS_LIBRARY_TINYDTLS: + snprintf(buffer, bufsize, "TLS Library: TinyDTLS - runtime %ld.%ld.%ld, " + "libcoap built for %ld.%ld.%ld", + tls_version->version >> 16, + (tls_version->version >> 8) & 0xff, + tls_version->version & 0xff, + tls_version->built_version >> 16, + (tls_version->built_version >> 8) & 0xff, + tls_version->built_version & 0xff); + break; + case COAP_TLS_LIBRARY_OPENSSL: + switch (tls_version->version &0xf) { + case 0: + strcpy(beta, "-dev"); + break; + case 0xf: + strcpy(beta, ""); + break; + default: + strcpy(beta, "-beta"); + beta[5] = (tls_version->version &0xf) + '0'; + beta[6] = '\000'; + break; + } + sub[0] = ((tls_version->version >> 4) & 0xff) ? + ((tls_version->version >> 4) & 0xff) + 'a' -1 : '\000'; + sub[1] = '\000'; + switch (tls_version->built_version &0xf) { + case 0: + strcpy(b_beta, "-dev"); + break; + case 0xf: + strcpy(b_beta, ""); + break; + default: + strcpy(b_beta, "-beta"); + b_beta[5] = (tls_version->built_version &0xf) + '0'; + b_beta[6] = '\000'; + break; + } + b_sub[0] = ((tls_version->built_version >> 4) & 0xff) ? + ((tls_version->built_version >> 4) & 0xff) + 'a' -1 : '\000'; + b_sub[1] = '\000'; + snprintf(buffer, bufsize, "TLS Library: OpenSSL - runtime " + "%ld.%ld.%ld%s%s, libcoap built for %ld.%ld.%ld%s%s", + tls_version->version >> 28, + (tls_version->version >> 20) & 0xff, + (tls_version->version >> 12) & 0xff, sub, beta, + tls_version->built_version >> 28, + (tls_version->built_version >> 20) & 0xff, + (tls_version->built_version >> 12) & 0xff, b_sub, b_beta); + break; + case COAP_TLS_LIBRARY_GNUTLS: + snprintf(buffer, bufsize, "TLS Library: GnuTLS - runtime %ld.%ld.%ld, " + "libcoap built for %ld.%ld.%ld", + tls_version->version >> 16, + (tls_version->version >> 8) & 0xff, + tls_version->version & 0xff, + tls_version->built_version >> 16, + (tls_version->built_version >> 8) & 0xff, + tls_version->built_version & 0xff); + break; + default: + snprintf(buffer, bufsize, "Library type %d unknown", tls_version->type); + break; + } + return buffer; +} + static coap_log_handler_t log_handler = NULL; void coap_set_log_handler(coap_log_handler_t handler) {