From: Fabian Keil <fk@fabiankeil.de>
Date: Wed, 26 Jun 2024 14:23:46 +0000 (+0200)
Subject: Factor out create_hexadecimal_hash_of_host()
X-Git-Tag: v_4_0_0~60^2~22
X-Git-Url: http://www.privoxy.org/gitweb/%22https:/developer-manual/static/howto.html?a=commitdiff_plain;h=a52bdf9a8c1ce8dcb222d67be845d58fd6fb6975;p=privoxy.git

Factor out create_hexadecimal_hash_of_host()
---

diff --git a/openssl.c b/openssl.c
index d647543b..f3920370 100644
--- a/openssl.c
+++ b/openssl.c
@@ -718,24 +718,11 @@ exit:
  *********************************************************************/
 static int host_to_hash(struct client_state *csp)
 {
-   int ret = 0;
-   size_t i;
-
    SHA256((unsigned char *)csp->http->host, strlen(csp->http->host),
       csp->http->hash_of_host);
 
-   /* Converting hash into string with hex */
-   for (i = 0; i < HASH_OF_HOST_BUF_SIZE; i++)
-   {
-      if ((ret = sprintf((char *)csp->http->hash_of_host_hex + 2 * i, "%02x",
-         csp->http->hash_of_host[i])) < 0)
-      {
-         log_error(LOG_LEVEL_ERROR, "Sprintf return value: %d", ret);
-         return -1;
-      }
-   }
+   return create_hexadecimal_hash_of_host(csp);
 
-   return 0;
 }
 
 
diff --git a/ssl.c b/ssl.c
index 04963541..e8007cd9 100644
--- a/ssl.c
+++ b/ssl.c
@@ -1799,24 +1799,10 @@ static int ssl_verify_callback(void *csp_void, mbedtls_x509_crt *crt,
  *********************************************************************/
 static int host_to_hash(struct client_state *csp)
 {
-   int ret;
-   size_t i;
-
    mbedtls_sha256((unsigned char *)csp->http->host,
       strlen(csp->http->host), csp->http->hash_of_host, 0);
 
-   /* Converting hash into string with hex */
-   for (i = 0; i < HASH_OF_HOST_BUF_SIZE; i++)
-   {
-      if ((ret = sprintf((char *)csp->http->hash_of_host_hex + 2 * i, "%02x",
-         csp->http->hash_of_host[i])) < 0)
-      {
-         log_error(LOG_LEVEL_ERROR, "Sprintf return value: %d", ret);
-         return -1;
-      }
-   }
-
-   return 0;
+   return create_hexadecimal_hash_of_host(csp);
 
 }
 
diff --git a/ssl_common.c b/ssl_common.c
index 45f1e761..1bf866ab 100644
--- a/ssl_common.c
+++ b/ssl_common.c
@@ -736,3 +736,38 @@ extern int enforce_sane_certificate_state(const char *certificate, const char *k
    return 0;
 
 }
+
+
+/*********************************************************************
+ *
+ * Function    :  create_hexadecimal_hash_of_host
+ *
+ * Description :  Converts the binary hash of a host into a
+ *                hexadecimal string.
+ *
+ * Parameters  :
+ *          1  :  csp = Current client state (buffers, headers, etc...)
+ *
+ * Returns     : -1 => Error while creating hash
+ *                0 => Hash created successfully
+ *
+ *********************************************************************/
+int create_hexadecimal_hash_of_host(struct client_state *csp)
+{
+   int i;
+   int ret;
+
+   for (i = 0; i < HASH_OF_HOST_BUF_SIZE; i++)
+   {
+      ret = sprintf((char *)csp->http->hash_of_host_hex + 2 * i, "%02x",
+         csp->http->hash_of_host[i]);
+      if (ret < 0)
+      {
+         log_error(LOG_LEVEL_ERROR, "sprintf() return value: %d", ret);
+         return -1;
+      }
+   }
+
+   return 0;
+
+}
diff --git a/ssl_common.h b/ssl_common.h
index 66fb5374..3458b232 100644
--- a/ssl_common.h
+++ b/ssl_common.h
@@ -73,5 +73,6 @@ extern unsigned long get_certificate_serial(struct client_state *csp);
 extern int get_certificate_valid_from_date(char *buffer, size_t buffer_size, const char *fmt);
 extern int get_certificate_valid_to_date(char *buffer, size_t buffer_size, const char *fmt);
 extern int enforce_sane_certificate_state(const char *certificate, const char *key);
+extern int create_hexadecimal_hash_of_host(struct client_state *csp);
 
 #endif /* ndef SSL_COMMON_H_INCLUDED */
diff --git a/wolfssl.c b/wolfssl.c
index 717be050..78880bec 100644
--- a/wolfssl.c
+++ b/wolfssl.c
@@ -737,7 +737,6 @@ exit:
 static int host_to_hash(struct client_state *csp)
 {
    int ret;
-   size_t i;
 
    ret = wc_Sha256Hash((const byte *)csp->http->host,
       (word32)strlen(csp->http->host), (byte *)csp->http->hash_of_host);
@@ -746,20 +745,7 @@ static int host_to_hash(struct client_state *csp)
         return -1;
    }
 
-   /* Converting hash into string with hex */
-   for (i = 0; i < HASH_OF_HOST_BUF_SIZE; i++)
-   {
-      ret = snprintf((char *)csp->http->hash_of_host_hex + 2 * i,
-         sizeof(csp->http->hash_of_host_hex) - 2 * i,
-         "%02x", csp->http->hash_of_host[i]);
-      if (ret < 0)
-      {
-         log_error(LOG_LEVEL_ERROR, "sprintf() failed. Return value: %d", ret);
-         return -1;
-      }
-   }
-
-   return 0;
+   return create_hexadecimal_hash_of_host(csp);
 
 }