Skip to content

Commit 19933a4

Browse files
authored
Merge pull request #3222 from cesanta/mbedrbose
Add verbose MbedTLS errors
2 parents 76963f7 + 51fe22a commit 19933a4

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

mongoose.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14038,6 +14038,13 @@ void mg_tls_ctx_free(struct mg_mgr *mgr) {
1403814038
#define MG_MBEDTLS_RNG_GET
1403914039
#endif
1404014040

14041+
static int mg_tls_err(struct mg_connection *c, int rc) {
14042+
char s[80];
14043+
mbedtls_strerror(rc, s, sizeof(s));
14044+
MG_ERROR(("%lu %s", ((struct mg_connection *) c)->id, s));
14045+
return rc;
14046+
}
14047+
1404114048
static int mg_mbed_rng(void *ctx, unsigned char *buf, size_t len) {
1404214049
mg_random(buf, len);
1404314050
(void) ctx;
@@ -14113,7 +14120,7 @@ void mg_tls_handshake(struct mg_connection *c) {
1411314120
MG_VERBOSE(("%lu pending, %d%d %d (-%#x)", c->id, c->is_connecting,
1411414121
c->is_tls_hs, rc, -rc));
1411514122
} else {
14116-
mg_error(c, "TLS handshake: -%#x", -rc); // Error
14123+
mg_error(c, "TLS handshake: -%#x", -mg_tls_err(c, rc)); // Error
1411714124
}
1411814125
}
1411914126

@@ -14151,7 +14158,7 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
1415114158
&tls->conf,
1415214159
c->is_client ? MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER,
1415314160
MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
14154-
mg_error(c, "tls defaults %#x", -rc);
14161+
mg_error(c, "tls defaults %#x", -mg_tls_err(c, rc));
1415514162
goto fail;
1415614163
}
1415714164
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) {
1417514182
if (!mg_load_key(opts->key, &tls->pk)) goto fail;
1417614183
if (tls->cert.version &&
1417714184
(rc = mbedtls_ssl_conf_own_cert(&tls->conf, &tls->cert, &tls->pk)) != 0) {
14178-
mg_error(c, "own cert %#x", -rc);
14185+
mg_error(c, "own cert %#x", -mg_tls_err(c, rc));
1417914186
goto fail;
1418014187
}
1418114188

@@ -14186,7 +14193,7 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
1418614193
#endif
1418714194

1418814195
if ((rc = mbedtls_ssl_setup(&tls->ssl, &tls->conf)) != 0) {
14189-
mg_error(c, "setup err %#x", -rc);
14196+
mg_error(c, "setup err %#x", -mg_tls_err(c, rc));
1419014197
goto fail;
1419114198
}
1419214199
c->is_tls = 1;

mongoose.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2657,6 +2657,7 @@ int mg_rsa_mod_pow(const uint8_t *mod, size_t modsz, const uint8_t *exp, size_t
26572657

26582658
#if MG_TLS == MG_TLS_MBED
26592659
#include <mbedtls/debug.h>
2660+
#include <mbedtls/error.h>
26602661
#include <mbedtls/net_sockets.h>
26612662
#include <mbedtls/ssl.h>
26622663
#include <mbedtls/ssl_ticket.h>

src/tls_mbed.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
#define MG_MBEDTLS_RNG_GET
1313
#endif
1414

15+
static int mg_tls_err(struct mg_connection *c, int rc) {
16+
char s[80];
17+
mbedtls_strerror(rc, s, sizeof(s));
18+
MG_ERROR(("%lu %s", ((struct mg_connection *) c)->id, s));
19+
return rc;
20+
}
21+
1522
static int mg_mbed_rng(void *ctx, unsigned char *buf, size_t len) {
1623
mg_random(buf, len);
1724
(void) ctx;
@@ -87,7 +94,7 @@ void mg_tls_handshake(struct mg_connection *c) {
8794
MG_VERBOSE(("%lu pending, %d%d %d (-%#x)", c->id, c->is_connecting,
8895
c->is_tls_hs, rc, -rc));
8996
} else {
90-
mg_error(c, "TLS handshake: -%#x", -rc); // Error
97+
mg_error(c, "TLS handshake: -%#x", -mg_tls_err(c, rc)); // Error
9198
}
9299
}
93100

@@ -125,7 +132,7 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
125132
&tls->conf,
126133
c->is_client ? MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER,
127134
MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
128-
mg_error(c, "tls defaults %#x", -rc);
135+
mg_error(c, "tls defaults %#x", -mg_tls_err(c, rc));
129136
goto fail;
130137
}
131138
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) {
149156
if (!mg_load_key(opts->key, &tls->pk)) goto fail;
150157
if (tls->cert.version &&
151158
(rc = mbedtls_ssl_conf_own_cert(&tls->conf, &tls->cert, &tls->pk)) != 0) {
152-
mg_error(c, "own cert %#x", -rc);
159+
mg_error(c, "own cert %#x", -mg_tls_err(c, rc));
153160
goto fail;
154161
}
155162

@@ -160,7 +167,7 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
160167
#endif
161168

162169
if ((rc = mbedtls_ssl_setup(&tls->ssl, &tls->conf)) != 0) {
163-
mg_error(c, "setup err %#x", -rc);
170+
mg_error(c, "setup err %#x", -mg_tls_err(c, rc));
164171
goto fail;
165172
}
166173
c->is_tls = 1;

src/tls_mbed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#if MG_TLS == MG_TLS_MBED
99
#include <mbedtls/debug.h>
10+
#include <mbedtls/error.h>
1011
#include <mbedtls/net_sockets.h>
1112
#include <mbedtls/ssl.h>
1213
#include <mbedtls/ssl_ticket.h>

0 commit comments

Comments
 (0)