From e3589577ed3a130b82d0e81252a9c34a76427c7c Mon Sep 17 00:00:00 2001
From: Sergey Lyubka <valenok@gmail.com>
Date: Sun, 29 Aug 2021 16:53:29 +0100
Subject: [PATCH] Update OpenSSL handshake error codepath

---
 examples/http-client/Makefile |  4 ++--
 mongoose.c                    | 19 ++++++++++---------
 src/tls.c                     | 19 ++++++++++---------
 3 files changed, 22 insertions(+), 20 deletions(-)

diff --git a/examples/http-client/Makefile b/examples/http-client/Makefile
index 66c5e9f6d..112c8fff1 100644
--- a/examples/http-client/Makefile
+++ b/examples/http-client/Makefile
@@ -14,7 +14,7 @@ CFLAGS += -L$(OPENSSL)/lib -lssl -lcrypto
 endif
 
 all: $(PROG)
-	$(DEBUGGER) ./$(PROG) $(ARGS)
+	$(RUN) ./$(PROG) $(ARGS)
 
 $(PROG): main.c
 	$(CC) ../../mongoose.c -I../.. -W -Wall $(CFLAGS) -o $(PROG) main.c
@@ -22,7 +22,7 @@ $(PROG): main.c
 linux: all
 linux: CFLAGS += -O2 -g -fsanitize=address,undefined,shift,null,return,bounds,alignment,object-size,bool,enum -static-libasan
 linux: CC = $(LIN) cc
-linux: DEBUGGER = $(LIN)
+linux: RUN = $(LIN)
 
 clean:
 	rm -rf $(PROG) *.o *.dSYM *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb
diff --git a/mongoose.c b/mongoose.c
index ee990d01d..bc26f1dc0 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -3725,13 +3725,15 @@ static int rng_get(void *p_rng, unsigned char *buf, size_t len) {
 void mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) {
   struct mg_tls *tls = (struct mg_tls *) calloc(1, sizeof(*tls));
   int rc = 0;
-  const char *ca =
-      opts->ca == NULL ? "-" : opts->ca[0] == '-' ? "(emb)" : opts->ca;
-  const char *cert =
-      opts->cert == NULL ? "-" : opts->cert[0] == '-' ? "(emb)" : opts->cert;
-  const char *certkey = opts->certkey == NULL
-                            ? "-"
-                            : opts->certkey[0] == '-' ? "(emb)" : opts->certkey;
+  const char *ca = opts->ca == NULL     ? "-"
+                   : opts->ca[0] == '-' ? "(emb)"
+                                        : opts->ca;
+  const char *cert = opts->cert == NULL     ? "-"
+                     : opts->cert[0] == '-' ? "(emb)"
+                                            : opts->cert;
+  const char *certkey = opts->certkey == NULL     ? "-"
+                        : opts->certkey[0] == '-' ? "(emb)"
+                                                  : opts->certkey;
   if (tls == NULL) {
     mg_error(c, "TLS OOM");
     goto fail;
@@ -3979,9 +3981,8 @@ void mg_tls_handshake(struct mg_connection *c) {
     LOG(LL_DEBUG, ("%lu success", c->id));
     c->is_tls_hs = 0;
   } else {
-    int code;
+    int code = mg_tls_err(tls, rc);
     ERR_print_errors_fp(stderr);
-    code = mg_tls_err(tls, rc);
     if (code != 0) mg_error(c, "tls hs: rc %d, err %d", rc, code);
   }
 }
diff --git a/src/tls.c b/src/tls.c
index 0066c8d6b..d75bcadc4 100644
--- a/src/tls.c
+++ b/src/tls.c
@@ -77,13 +77,15 @@ static int rng_get(void *p_rng, unsigned char *buf, size_t len) {
 void mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) {
   struct mg_tls *tls = (struct mg_tls *) calloc(1, sizeof(*tls));
   int rc = 0;
-  const char *ca =
-      opts->ca == NULL ? "-" : opts->ca[0] == '-' ? "(emb)" : opts->ca;
-  const char *cert =
-      opts->cert == NULL ? "-" : opts->cert[0] == '-' ? "(emb)" : opts->cert;
-  const char *certkey = opts->certkey == NULL
-                            ? "-"
-                            : opts->certkey[0] == '-' ? "(emb)" : opts->certkey;
+  const char *ca = opts->ca == NULL     ? "-"
+                   : opts->ca[0] == '-' ? "(emb)"
+                                        : opts->ca;
+  const char *cert = opts->cert == NULL     ? "-"
+                     : opts->cert[0] == '-' ? "(emb)"
+                                            : opts->cert;
+  const char *certkey = opts->certkey == NULL     ? "-"
+                        : opts->certkey[0] == '-' ? "(emb)"
+                                                  : opts->certkey;
   if (tls == NULL) {
     mg_error(c, "TLS OOM");
     goto fail;
@@ -331,9 +333,8 @@ void mg_tls_handshake(struct mg_connection *c) {
     LOG(LL_DEBUG, ("%lu success", c->id));
     c->is_tls_hs = 0;
   } else {
-    int code;
+    int code = mg_tls_err(tls, rc);
     ERR_print_errors_fp(stderr);
-    code = mg_tls_err(tls, rc);
     if (code != 0) mg_error(c, "tls hs: rc %d, err %d", rc, code);
   }
 }
-- 
GitLab