Skip to content

Commit 8fd7e87

Browse files
authored
Merge pull request #2787 from cesanta/nicer
play nicer to intermediate OOMs in OpenSSL
2 parents c436194 + fcd160c commit 8fd7e87

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

mongoose.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12497,13 +12497,23 @@ static void ssl_keylog_cb(const SSL *ssl, const char *line) {
1249712497
}
1249812498
#endif
1249912499

12500+
void mg_tls_free(struct mg_connection *c) {
12501+
struct mg_tls *tls = (struct mg_tls *) c->tls;
12502+
if (tls == NULL) return;
12503+
SSL_free(tls->ssl);
12504+
SSL_CTX_free(tls->ctx);
12505+
BIO_meth_free(tls->bm);
12506+
free(tls);
12507+
c->tls = NULL;
12508+
}
12509+
1250012510
void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
1250112511
struct mg_tls *tls = (struct mg_tls *) calloc(1, sizeof(*tls));
1250212512
const char *id = "mongoose";
1250312513
static unsigned char s_initialised = 0;
1250412514
BIO *bio = NULL;
1250512515
int rc;
12506-
12516+
c->tls = tls;
1250712517
if (tls == NULL) {
1250812518
mg_error(c, "TLS OOM");
1250912519
goto fail;
@@ -12603,7 +12613,6 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
1260312613
BIO_set_data(bio, c);
1260412614
SSL_set_bio(tls->ssl, bio, bio);
1260512615

12606-
c->tls = tls;
1260712616
c->is_tls = 1;
1260812617
c->is_tls_hs = 1;
1260912618
if (c->is_client && c->is_resolving == 0 && c->is_connecting == 0) {
@@ -12612,7 +12621,7 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
1261212621
MG_DEBUG(("%lu SSL %s OK", c->id, c->is_accepted ? "accept" : "client"));
1261312622
return;
1261412623
fail:
12615-
free(tls);
12624+
mg_tls_free(c);
1261612625
}
1261712626

1261812627
void mg_tls_handshake(struct mg_connection *c) {
@@ -12628,16 +12637,6 @@ void mg_tls_handshake(struct mg_connection *c) {
1262812637
}
1262912638
}
1263012639

12631-
void mg_tls_free(struct mg_connection *c) {
12632-
struct mg_tls *tls = (struct mg_tls *) c->tls;
12633-
if (tls == NULL) return;
12634-
SSL_free(tls->ssl);
12635-
SSL_CTX_free(tls->ctx);
12636-
BIO_meth_free(tls->bm);
12637-
free(tls);
12638-
c->tls = NULL;
12639-
}
12640-
1264112640
size_t mg_tls_pending(struct mg_connection *c) {
1264212641
struct mg_tls *tls = (struct mg_tls *) c->tls;
1264312642
return tls == NULL ? 0 : (size_t) SSL_pending(tls->ssl);

src/tls_openssl.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,23 @@ static void ssl_keylog_cb(const SSL *ssl, const char *line) {
106106
}
107107
#endif
108108

109+
void mg_tls_free(struct mg_connection *c) {
110+
struct mg_tls *tls = (struct mg_tls *) c->tls;
111+
if (tls == NULL) return;
112+
SSL_free(tls->ssl);
113+
SSL_CTX_free(tls->ctx);
114+
BIO_meth_free(tls->bm);
115+
free(tls);
116+
c->tls = NULL;
117+
}
118+
109119
void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
110120
struct mg_tls *tls = (struct mg_tls *) calloc(1, sizeof(*tls));
111121
const char *id = "mongoose";
112122
static unsigned char s_initialised = 0;
113123
BIO *bio = NULL;
114124
int rc;
115-
125+
c->tls = tls;
116126
if (tls == NULL) {
117127
mg_error(c, "TLS OOM");
118128
goto fail;
@@ -212,7 +222,6 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
212222
BIO_set_data(bio, c);
213223
SSL_set_bio(tls->ssl, bio, bio);
214224

215-
c->tls = tls;
216225
c->is_tls = 1;
217226
c->is_tls_hs = 1;
218227
if (c->is_client && c->is_resolving == 0 && c->is_connecting == 0) {
@@ -221,7 +230,7 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
221230
MG_DEBUG(("%lu SSL %s OK", c->id, c->is_accepted ? "accept" : "client"));
222231
return;
223232
fail:
224-
free(tls);
233+
mg_tls_free(c);
225234
}
226235

227236
void mg_tls_handshake(struct mg_connection *c) {
@@ -237,16 +246,6 @@ void mg_tls_handshake(struct mg_connection *c) {
237246
}
238247
}
239248

240-
void mg_tls_free(struct mg_connection *c) {
241-
struct mg_tls *tls = (struct mg_tls *) c->tls;
242-
if (tls == NULL) return;
243-
SSL_free(tls->ssl);
244-
SSL_CTX_free(tls->ctx);
245-
BIO_meth_free(tls->bm);
246-
free(tls);
247-
c->tls = NULL;
248-
}
249-
250249
size_t mg_tls_pending(struct mg_connection *c) {
251250
struct mg_tls *tls = (struct mg_tls *) c->tls;
252251
return tls == NULL ? 0 : (size_t) SSL_pending(tls->ssl);

0 commit comments

Comments
 (0)