gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libmicrohttpd] branch master updated (6ed5b52d -> f8c9870a)


From: gnunet
Subject: [libmicrohttpd] branch master updated (6ed5b52d -> f8c9870a)
Date: Sat, 22 Jan 2022 10:21:43 +0100

This is an automated email from the git hooks/post-receive script.

karlson2k pushed a change to branch master
in repository libmicrohttpd.

    from 6ed5b52d Fixed early response with digest auth in tests, examples, and 
documentation
     new 0074fcbd digestauth: Moved hex printing function to mhd_str
     new caf7bcd3 gitignore: updated
     new f8c9870a check_nonce_nc(): reworked mutex handling

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .gitignore                  |   1 +
 src/microhttpd/digestauth.c | 128 +++++++++++++++++---------------------------
 src/microhttpd/mhd_str.c    |  21 ++++++++
 src/microhttpd/mhd_str.h    |  13 +++++
 4 files changed, 85 insertions(+), 78 deletions(-)

diff --git a/.gitignore b/.gitignore
index 09545a38..62ce5a08 100644
--- a/.gitignore
+++ b/.gitignore
@@ -64,3 +64,4 @@ src/microhttpd_ws/test_websocket
 /po/aclocal.m4
 /po/po-configure.ac
 /po/configure.ac
+*.exe.manifest
diff --git a/src/microhttpd/digestauth.c b/src/microhttpd/digestauth.c
index 81ca2ac9..59f999f4 100644
--- a/src/microhttpd/digestauth.c
+++ b/src/microhttpd/digestauth.c
@@ -183,32 +183,6 @@ struct DigestAlgorithm
 };
 
 
-/**
- * convert bin to hex
- *
- * @param bin binary data
- * @param len number of bytes in bin
- * @param hex pointer to len*2+1 bytes
- */
-static void
-cvthex (const unsigned char *bin,
-        size_t len,
-        char *hex)
-{
-  size_t i;
-  unsigned int j;
-
-  for (i = 0; i < len; ++i)
-  {
-    j = (bin[i] >> 4) & 0x0f;
-    hex[i * 2] = (char) ((j <= 9) ? (j + '0') : (j - 10 + 'a'));
-    j = bin[i] & 0x0f;
-    hex[i * 2 + 1] = (char) ((j <= 9) ? (j + '0') : (j - 10 + 'a'));
-  }
-  hex[len * 2] = '\0';
-}
-
-
 /**
  * calculate H(A1) from given hash as per RFC2617 spec
  * and store the * result in 'sessionkey'.
@@ -258,15 +232,15 @@ digest_calc_ha1_from_digest (const char *alg,
                 strlen (cnonce));
     da->digest (da->ctx,
                 dig);
-    cvthex (dig,
-            digest_size,
-            da->sessionkey);
+    MHD_bin_to_hex (dig,
+                    digest_size,
+                    da->sessionkey);
   }
   else
   {
-    cvthex (digest,
-            digest_size,
-            da->sessionkey);
+    MHD_bin_to_hex (digest,
+                    digest_size,
+                    da->sessionkey);
   }
 }
 
@@ -383,9 +357,9 @@ digest_calc_response (const char *ha1,
 #endif
   da->digest (da->ctx,
               ha2);
-  cvthex (ha2,
-          digest_size,
-          da->sessionkey);
+  MHD_bin_to_hex (ha2,
+                  digest_size,
+                  da->sessionkey);
   da->init (da->ctx);
   /* calculate response */
   da->update (da->ctx,
@@ -426,9 +400,9 @@ digest_calc_response (const char *ha1,
               digest_size * 2);
   da->digest (da->ctx,
               resphash);
-  cvthex (resphash,
-          digest_size,
-          da->sessionkey);
+  MHD_bin_to_hex (resphash,
+                  digest_size,
+                  da->sessionkey);
 }
 
 
@@ -550,7 +524,10 @@ check_nonce_nc (struct MHD_Connection *connection,
   uint32_t mod;
   const char *np;
   size_t noncelen;
+  enum MHD_Result ret;
+  bool stale;
 
+  stale = false;
   noncelen = strlen (nonce) + 1;
   if (MAX_NONCE_LENGTH < noncelen)
     return MHD_NO; /* This should be impossible, but static analysis
@@ -575,9 +552,8 @@ check_nonce_nc (struct MHD_Connection *connection,
    * then only increase the nonce counter by one.
    */
   nn = &daemon->nnc[off];
-#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
+
   MHD_mutex_lock_chk_ (&daemon->nnc_lock);
-#endif
   if (0 == nc)
   {
     /* Fresh nonce, reinitialize array */
@@ -586,51 +562,47 @@ check_nonce_nc (struct MHD_Connection *connection,
             noncelen);
     nn->nc = 0;
     nn->nmask = 0;
-#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
-    MHD_mutex_unlock_chk_ (&daemon->nnc_lock);
-#endif
-    return MHD_YES;
+    ret = MHD_YES;
   }
   /* Note that we use 64 here, as we do not store the
      bit for 'nn->nc' itself in 'nn->nmask' */
-  if ( (nc < nn->nc) &&
-       (nc + 64 > nc /* checking for overflow */) &&
-       (nc + 64 >= nn->nc) &&
-       (0 == ((1LLU << (nn->nc - nc - 1)) & nn->nmask)) )
+  else if ( (nc < nn->nc) &&
+            (nc + 64 > nc /* checking for overflow */) &&
+            (nc + 64 >= nn->nc) &&
+            (0 == ((1LLU << (nn->nc - nc - 1)) & nn->nmask)) )
   {
     /* Out-of-order nonce, but within 64-bit bitmask, set bit */
     nn->nmask |= (1LLU << (nn->nc - nc - 1));
-#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
-    MHD_mutex_unlock_chk_ (&daemon->nnc_lock);
-#endif
-    return MHD_YES;
+    ret = MHD_YES;
   }
-
-  if ( (nc <= nn->nc) ||
-       (0 != strcmp (nn->nonce,
-                     nonce)) )
+  else if ( (nc <= nn->nc) ||
+            (0 != strcmp (nn->nonce,
+                          nonce)) )
   {
     /* Nonce does not match, fail */
-#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
-    MHD_mutex_unlock_chk_ (&daemon->nnc_lock);
-#endif
-#ifdef HAVE_MESSAGES
-    MHD_DLOG (daemon,
-              _ (
-                "Stale nonce received.  If this happens a lot, you should 
probably increase the size of the nonce array.\n"));
-#endif
-    return MHD_NO;
+    stale = true;
+    ret = MHD_NO;
   }
-  /* Nonce is larger, shift bitmask and bump limit */
-  if (64 > nc - nn->nc)
-    nn->nmask <<= (nc - nn->nc);  /* small jump, less than mask width */
   else
-    nn->nmask = 0;                /* big jump, unset all bits in the mask */
-  nn->nc = nc;
-#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
+  {
+    /* Nonce is larger, shift bitmask and bump limit */
+    if (64 > nc - nn->nc)
+      nn->nmask <<= (nc - nn->nc);  /* small jump, less than mask width */
+    else
+      nn->nmask = 0;                /* big jump, unset all bits in the mask */
+    nn->nc = nc;
+    ret = MHD_YES;
+  }
   MHD_mutex_unlock_chk_ (&daemon->nnc_lock);
+#ifdef HAVE_MESSAGES
+  if (stale)
+    MHD_DLOG (daemon,
+              _ ("Stale nonce received. If this happens a lot, you should "
+                 "probably increase the size of the nonce array.\n"));
+#else
+  (void) stale; /* Mute compiler warning */
 #endif
-  return MHD_YES;
+  return ret;
 }
 
 
@@ -736,12 +708,12 @@ calculate_nonce (uint32_t nonce_time,
               strlen (realm));
   da->digest (da->ctx,
               tmpnonce);
-  cvthex (tmpnonce,
-          digest_size,
-          nonce);
-  cvthex (timestamp,
-          sizeof (timestamp),
-          nonce + digest_size * 2);
+  MHD_bin_to_hex (tmpnonce,
+                  digest_size,
+                  nonce);
+  MHD_bin_to_hex (timestamp,
+                  sizeof (timestamp),
+                  nonce + digest_size * 2);
 }
 
 
diff --git a/src/microhttpd/mhd_str.c b/src/microhttpd/mhd_str.c
index 822feccc..08781cb9 100644
--- a/src/microhttpd/mhd_str.c
+++ b/src/microhttpd/mhd_str.c
@@ -1354,3 +1354,24 @@ MHD_uint8_to_str_pad (uint8_t val,
   buf[pos++] = '0' + val;
   return pos;
 }
+
+
+size_t
+MHD_bin_to_hex (const void *bin,
+                size_t size,
+                char *hex)
+{
+  size_t i;
+
+  for (i = 0; i < size; ++i)
+  {
+    uint8_t j;
+    const uint8_t b = ((uint8_t *) bin)[i];
+    j = b >> 4;
+    hex[i * 2] = (char) ((j < 10) ? (j + '0') : (j - 10 + 'a'));
+    j = b & 0x0f;
+    hex[i * 2 + 1] = (char) ((j < 10) ? (j + '0') : (j - 10 + 'a'));
+  }
+  hex[i * 2] = 0;
+  return i;
+}
diff --git a/src/microhttpd/mhd_str.h b/src/microhttpd/mhd_str.h
index 09c63578..6455d84e 100644
--- a/src/microhttpd/mhd_str.h
+++ b/src/microhttpd/mhd_str.h
@@ -457,5 +457,18 @@ MHD_uint8_to_str_pad (uint8_t val,
                       char *buf,
                       size_t buf_size);
 
+/**
+ * Convert @a size bytes from input binary data to lower case
+ * hexadecimal digits, zero-terminate the result.
+ * @param bin the pointer to the binary data to convert
+ * @param size the size in bytes of the binary data to convert
+ * @param hex the output buffer, but be at least 2 * @a size + 1
+ * @return The number of characters written to the output buffer,
+ *         not including terminating zero.
+ */
+size_t
+MHD_bin_to_hex (const void *bin,
+                size_t size,
+                char *hex);
 
 #endif /* MHD_STR_H */

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]