diff --git a/mongoose.c b/mongoose.c index ba7bc6bd..96077289 100644 --- a/mongoose.c +++ b/mongoose.c @@ -14038,6 +14038,13 @@ void mg_tls_ctx_free(struct mg_mgr *mgr) { #define MG_MBEDTLS_RNG_GET #endif +static int mg_tls_err(struct mg_connection *c, int rc) { + char s[80]; + mbedtls_strerror(rc, s, sizeof(s)); + MG_ERROR(("%lu %s", ((struct mg_connection *) c)->id, s)); + return rc; +} + static int mg_mbed_rng(void *ctx, unsigned char *buf, size_t len) { mg_random(buf, len); (void) ctx; @@ -14113,7 +14120,7 @@ void mg_tls_handshake(struct mg_connection *c) { MG_VERBOSE(("%lu pending, %d%d %d (-%#x)", c->id, c->is_connecting, c->is_tls_hs, rc, -rc)); } else { - mg_error(c, "TLS handshake: -%#x", -rc); // Error + mg_error(c, "TLS handshake: -%#x", -mg_tls_err(c, rc)); // Error } } @@ -14151,7 +14158,7 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) { &tls->conf, c->is_client ? MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT)) != 0) { - mg_error(c, "tls defaults %#x", -rc); + mg_error(c, "tls defaults %#x", -mg_tls_err(c, rc)); goto fail; } mbedtls_ssl_conf_rng(&tls->conf, mg_mbed_rng, c); @@ -14175,7 +14182,7 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) { if (!mg_load_key(opts->key, &tls->pk)) goto fail; if (tls->cert.version && (rc = mbedtls_ssl_conf_own_cert(&tls->conf, &tls->cert, &tls->pk)) != 0) { - mg_error(c, "own cert %#x", -rc); + mg_error(c, "own cert %#x", -mg_tls_err(c, rc)); goto fail; } @@ -14186,7 +14193,7 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) { #endif if ((rc = mbedtls_ssl_setup(&tls->ssl, &tls->conf)) != 0) { - mg_error(c, "setup err %#x", -rc); + mg_error(c, "setup err %#x", -mg_tls_err(c, rc)); goto fail; } c->is_tls = 1; diff --git a/mongoose.h b/mongoose.h index 5c32efa3..380471fd 100644 --- a/mongoose.h +++ b/mongoose.h @@ -2657,6 +2657,7 @@ int mg_rsa_mod_pow(const uint8_t *mod, size_t modsz, const uint8_t *exp, size_t #if MG_TLS == MG_TLS_MBED #include +#include #include #include #include diff --git a/src/tls_mbed.c b/src/tls_mbed.c index 707fdb31..0c466ef2 100644 --- a/src/tls_mbed.c +++ b/src/tls_mbed.c @@ -12,6 +12,13 @@ #define MG_MBEDTLS_RNG_GET #endif +static int mg_tls_err(struct mg_connection *c, int rc) { + char s[80]; + mbedtls_strerror(rc, s, sizeof(s)); + MG_ERROR(("%lu %s", ((struct mg_connection *) c)->id, s)); + return rc; +} + static int mg_mbed_rng(void *ctx, unsigned char *buf, size_t len) { mg_random(buf, len); (void) ctx; @@ -87,7 +94,7 @@ void mg_tls_handshake(struct mg_connection *c) { MG_VERBOSE(("%lu pending, %d%d %d (-%#x)", c->id, c->is_connecting, c->is_tls_hs, rc, -rc)); } else { - mg_error(c, "TLS handshake: -%#x", -rc); // Error + mg_error(c, "TLS handshake: -%#x", -mg_tls_err(c, rc)); // Error } } @@ -125,7 +132,7 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) { &tls->conf, c->is_client ? MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT)) != 0) { - mg_error(c, "tls defaults %#x", -rc); + mg_error(c, "tls defaults %#x", -mg_tls_err(c, rc)); goto fail; } mbedtls_ssl_conf_rng(&tls->conf, mg_mbed_rng, c); @@ -149,7 +156,7 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) { if (!mg_load_key(opts->key, &tls->pk)) goto fail; if (tls->cert.version && (rc = mbedtls_ssl_conf_own_cert(&tls->conf, &tls->cert, &tls->pk)) != 0) { - mg_error(c, "own cert %#x", -rc); + mg_error(c, "own cert %#x", -mg_tls_err(c, rc)); goto fail; } @@ -160,7 +167,7 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) { #endif if ((rc = mbedtls_ssl_setup(&tls->ssl, &tls->conf)) != 0) { - mg_error(c, "setup err %#x", -rc); + mg_error(c, "setup err %#x", -mg_tls_err(c, rc)); goto fail; } c->is_tls = 1; diff --git a/src/tls_mbed.h b/src/tls_mbed.h index 14fd3e11..e7bce2ae 100644 --- a/src/tls_mbed.h +++ b/src/tls_mbed.h @@ -7,6 +7,7 @@ #if MG_TLS == MG_TLS_MBED #include +#include #include #include #include