Path: blob/master/thirdparty/mbedtls/library/debug.c
10279 views
/*1* Debugging routines2*3* Copyright The Mbed TLS Contributors4* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later5*/67#include "common.h"89#if defined(MBEDTLS_DEBUG_C)1011#include "mbedtls/platform.h"1213#include "debug_internal.h"14#include "mbedtls/error.h"1516#include <stdarg.h>17#include <stdio.h>18#include <string.h>1920/* DEBUG_BUF_SIZE must be at least 2 */21#define DEBUG_BUF_SIZE 5122223static int debug_threshold = 0;2425void mbedtls_debug_set_threshold(int threshold)26{27debug_threshold = threshold;28}2930/*31* All calls to f_dbg must be made via this function32*/33static inline void debug_send_line(const mbedtls_ssl_context *ssl, int level,34const char *file, int line,35const char *str)36{37/*38* If in a threaded environment, we need a thread identifier.39* Since there is no portable way to get one, use the address of the ssl40* context instead, as it shouldn't be shared between threads.41*/42#if defined(MBEDTLS_THREADING_C)43char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */44mbedtls_snprintf(idstr, sizeof(idstr), "%p: %s", (void *) ssl, str);45ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, idstr);46#else47ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, str);48#endif49}5051MBEDTLS_PRINTF_ATTRIBUTE(5, 6)52void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level,53const char *file, int line,54const char *format, ...)55{56va_list argp;57char str[DEBUG_BUF_SIZE];58int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;5960MBEDTLS_STATIC_ASSERT(DEBUG_BUF_SIZE >= 2, "DEBUG_BUF_SIZE too small");6162if (NULL == ssl ||63NULL == ssl->conf ||64NULL == ssl->conf->f_dbg ||65level > debug_threshold) {66return;67}6869va_start(argp, format);70ret = mbedtls_vsnprintf(str, DEBUG_BUF_SIZE, format, argp);71va_end(argp);7273if (ret < 0) {74ret = 0;75} else {76if (ret >= DEBUG_BUF_SIZE - 1) {77ret = DEBUG_BUF_SIZE - 2;78}79}80str[ret] = '\n';81str[ret + 1] = '\0';8283debug_send_line(ssl, level, file, line, str);84}8586void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,87const char *file, int line,88const char *text, int ret)89{90char str[DEBUG_BUF_SIZE];9192if (NULL == ssl ||93NULL == ssl->conf ||94NULL == ssl->conf->f_dbg ||95level > debug_threshold) {96return;97}9899/*100* With non-blocking I/O and examples that just retry immediately,101* the logs would be quickly flooded with WANT_READ, so ignore that.102* Don't ignore WANT_WRITE however, since it is usually rare.103*/104if (ret == MBEDTLS_ERR_SSL_WANT_READ) {105return;106}107108mbedtls_snprintf(str, sizeof(str), "%s() returned %d (-0x%04x)\n",109text, ret, (unsigned int) -ret);110111debug_send_line(ssl, level, file, line, str);112}113114void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,115const char *file, int line, const char *text,116const unsigned char *buf, size_t len)117{118char str[DEBUG_BUF_SIZE];119char txt[17];120size_t i, idx = 0;121122if (NULL == ssl ||123NULL == ssl->conf ||124NULL == ssl->conf->f_dbg ||125level > debug_threshold) {126return;127}128129mbedtls_snprintf(str + idx, sizeof(str) - idx, "dumping '%s' (%u bytes)\n",130text, (unsigned int) len);131132debug_send_line(ssl, level, file, line, str);133134memset(txt, 0, sizeof(txt));135for (i = 0; i < len; i++) {136if (i >= 4096) {137break;138}139140if (i % 16 == 0) {141if (i > 0) {142mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);143debug_send_line(ssl, level, file, line, str);144145idx = 0;146memset(txt, 0, sizeof(txt));147}148149idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ",150(unsigned int) i);151152}153154idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",155(unsigned int) buf[i]);156txt[i % 16] = (buf[i] > 31 && buf[i] < 127) ? buf[i] : '.';157}158159if (len > 0) {160for (/* i = i */; i % 16 != 0; i++) {161idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");162}163164mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);165debug_send_line(ssl, level, file, line, str);166}167}168169#if defined(MBEDTLS_ECP_LIGHT)170void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,171const char *file, int line,172const char *text, const mbedtls_ecp_point *X)173{174char str[DEBUG_BUF_SIZE];175176if (NULL == ssl ||177NULL == ssl->conf ||178NULL == ssl->conf->f_dbg ||179level > debug_threshold) {180return;181}182183mbedtls_snprintf(str, sizeof(str), "%s(X)", text);184mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->X);185186mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);187mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->Y);188}189#endif /* MBEDTLS_ECP_LIGHT */190191#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)192static void mbedtls_debug_print_ec_coord(const mbedtls_ssl_context *ssl, int level,193const char *file, int line, const char *text,194const unsigned char *buf, size_t len)195{196char str[DEBUG_BUF_SIZE];197size_t i, idx = 0;198199mbedtls_snprintf(str + idx, sizeof(str) - idx, "value of '%s' (%u bits) is:\n",200text, (unsigned int) len * 8);201202debug_send_line(ssl, level, file, line, str);203204for (i = 0; i < len; i++) {205if (i >= 4096) {206break;207}208209if (i % 16 == 0) {210if (i > 0) {211mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");212debug_send_line(ssl, level, file, line, str);213214idx = 0;215}216}217218idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",219(unsigned int) buf[i]);220}221222if (len > 0) {223for (/* i = i */; i % 16 != 0; i++) {224idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");225}226227mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");228debug_send_line(ssl, level, file, line, str);229}230}231232void mbedtls_debug_print_psa_ec(const mbedtls_ssl_context *ssl, int level,233const char *file, int line,234const char *text, const mbedtls_pk_context *pk)235{236char str[DEBUG_BUF_SIZE];237const uint8_t *coord_start;238size_t coord_len;239240if (NULL == ssl ||241NULL == ssl->conf ||242NULL == ssl->conf->f_dbg ||243level > debug_threshold) {244return;245}246247/* For the description of pk->pk_raw content please refer to the description248* psa_export_public_key() function. */249coord_len = (pk->pub_raw_len - 1)/2;250251/* X coordinate */252coord_start = pk->pub_raw + 1;253mbedtls_snprintf(str, sizeof(str), "%s(X)", text);254mbedtls_debug_print_ec_coord(ssl, level, file, line, str, coord_start, coord_len);255256/* Y coordinate */257coord_start = coord_start + coord_len;258mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);259mbedtls_debug_print_ec_coord(ssl, level, file, line, str, coord_start, coord_len);260}261#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */262263#if defined(MBEDTLS_BIGNUM_C)264void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,265const char *file, int line,266const char *text, const mbedtls_mpi *X)267{268char str[DEBUG_BUF_SIZE];269size_t bitlen;270size_t idx = 0;271272if (NULL == ssl ||273NULL == ssl->conf ||274NULL == ssl->conf->f_dbg ||275NULL == X ||276level > debug_threshold) {277return;278}279280bitlen = mbedtls_mpi_bitlen(X);281282mbedtls_snprintf(str, sizeof(str), "value of '%s' (%u bits) is:\n",283text, (unsigned) bitlen);284debug_send_line(ssl, level, file, line, str);285286if (bitlen == 0) {287str[0] = ' '; str[1] = '0'; str[2] = '0';288idx = 3;289} else {290int n;291for (n = (int) ((bitlen - 1) / 8); n >= 0; n--) {292size_t limb_offset = n / sizeof(mbedtls_mpi_uint);293size_t offset_in_limb = n % sizeof(mbedtls_mpi_uint);294unsigned char octet =295(X->p[limb_offset] >> (offset_in_limb * 8)) & 0xff;296mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", octet);297idx += 3;298/* Wrap lines after 16 octets that each take 3 columns */299if (idx >= 3 * 16) {300mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");301debug_send_line(ssl, level, file, line, str);302idx = 0;303}304}305}306307if (idx != 0) {308mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");309debug_send_line(ssl, level, file, line, str);310}311}312#endif /* MBEDTLS_BIGNUM_C */313314#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)315static void debug_print_pk(const mbedtls_ssl_context *ssl, int level,316const char *file, int line,317const char *text, const mbedtls_pk_context *pk)318{319size_t i;320mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];321char name[16];322323memset(items, 0, sizeof(items));324325if (mbedtls_pk_debug(pk, items) != 0) {326debug_send_line(ssl, level, file, line,327"invalid PK context\n");328return;329}330331for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) {332if (items[i].type == MBEDTLS_PK_DEBUG_NONE) {333return;334}335336mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name);337name[sizeof(name) - 1] = '\0';338339#if defined(MBEDTLS_RSA_C)340if (items[i].type == MBEDTLS_PK_DEBUG_MPI) {341mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value);342} else343#endif /* MBEDTLS_RSA_C */344#if defined(MBEDTLS_ECP_LIGHT)345if (items[i].type == MBEDTLS_PK_DEBUG_ECP) {346mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value);347} else348#endif /* MBEDTLS_ECP_LIGHT */349#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)350if (items[i].type == MBEDTLS_PK_DEBUG_PSA_EC) {351mbedtls_debug_print_psa_ec(ssl, level, file, line, name, items[i].value);352} else353#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */354{ debug_send_line(ssl, level, file, line,355"should not happen\n"); }356}357}358359static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level,360const char *file, int line, const char *text)361{362char str[DEBUG_BUF_SIZE];363const char *start, *cur;364365start = text;366for (cur = text; *cur != '\0'; cur++) {367if (*cur == '\n') {368size_t len = (size_t) (cur - start) + 1;369if (len > DEBUG_BUF_SIZE - 1) {370len = DEBUG_BUF_SIZE - 1;371}372373memcpy(str, start, len);374str[len] = '\0';375376debug_send_line(ssl, level, file, line, str);377378start = cur + 1;379}380}381}382383void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,384const char *file, int line,385const char *text, const mbedtls_x509_crt *crt)386{387char str[DEBUG_BUF_SIZE];388int i = 0;389390if (NULL == ssl ||391NULL == ssl->conf ||392NULL == ssl->conf->f_dbg ||393NULL == crt ||394level > debug_threshold) {395return;396}397398while (crt != NULL) {399char buf[1024];400401mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i);402debug_send_line(ssl, level, file, line, str);403404mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);405debug_print_line_by_line(ssl, level, file, line, buf);406407debug_print_pk(ssl, level, file, line, "crt->", &crt->pk);408409crt = crt->next;410}411}412#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_X509_REMOVE_INFO */413414#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) && \415defined(MBEDTLS_ECDH_C)416static void mbedtls_debug_printf_ecdh_internal(const mbedtls_ssl_context *ssl,417int level, const char *file,418int line,419const mbedtls_ecdh_context *ecdh,420mbedtls_debug_ecdh_attr attr)421{422#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)423const mbedtls_ecdh_context *ctx = ecdh;424#else425const mbedtls_ecdh_context_mbed *ctx = &ecdh->ctx.mbed_ecdh;426#endif427428switch (attr) {429case MBEDTLS_DEBUG_ECDH_Q:430mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Q",431&ctx->Q);432break;433case MBEDTLS_DEBUG_ECDH_QP:434mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Qp",435&ctx->Qp);436break;437case MBEDTLS_DEBUG_ECDH_Z:438mbedtls_debug_print_mpi(ssl, level, file, line, "ECDH: z",439&ctx->z);440break;441default:442break;443}444}445446void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level,447const char *file, int line,448const mbedtls_ecdh_context *ecdh,449mbedtls_debug_ecdh_attr attr)450{451#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)452mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, attr);453#else454switch (ecdh->var) {455default:456mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh,457attr);458}459#endif460}461#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED &&462MBEDTLS_ECDH_C */463464#endif /* MBEDTLS_DEBUG_C */465466467