]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-sasl: sasl-server-mech-digest-md5 - Move parse_next() to lib-auth/auth-digest...
authorStephan Bosch <stephan.bosch@open-xchange.com>
Wed, 19 Feb 2025 00:49:12 +0000 (01:49 +0100)
committertimo.sirainen <timo.sirainen@open-xchange.com>
Thu, 9 Oct 2025 08:41:22 +0000 (08:41 +0000)
src/lib-auth/auth-digest.c
src/lib-auth/auth-digest.h
src/lib-sasl/sasl-server-mech-digest-md5.c

index 281dac3b94ec2ccc5a9b680550b0455058e9b501..06df4da42b8288b5d5f7683c202f194d89f3b515 100644 (file)
@@ -5,6 +5,69 @@
 
 #include "auth-digest.h"
 
+/*
+ * Parsing
+ */
+
+/* Linear whitespace */
+#define IS_LWS(c) ((c) == ' ' || (c) == '\t')
+
+bool auth_digest_parse_keyvalue(char **data, char **key_r, char **value_r)
+{
+       /* @UNSAFE */
+       char *p, *dest;
+
+       p = *data;
+       while (IS_LWS(*p)) p++;
+
+       /* get key */
+       *key_r = p;
+       while (*p != '\0' && *p != '=' && *p != ',')
+               p++;
+
+       if (*p != '=') {
+               *data = p;
+               return FALSE;
+       }
+
+       *value_r = p+1;
+
+       /* skip trailing whitespace in key */
+       while (p > *data && IS_LWS(p[-1]))
+               p--;
+       *p = '\0';
+
+       /* get value */
+       p = *value_r;
+       while (IS_LWS(*p)) p++;
+
+       if (*p != '"') {
+               while (*p != '\0' && *p != ',')
+                       p++;
+
+               *data = p;
+               /* If there is more to parse, ensure it won't get skipped
+                  because *p is set to NUL below */
+               if (**data != '\0') (*data)++;
+               while (IS_LWS(p[-1]))
+                       p--;
+               *p = '\0';
+       } else {
+               /* quoted string */
+               *value_r = dest = ++p;
+               while (*p != '\0' && *p != '"') {
+                       if (*p == '\\' && p[1] != '\0')
+                               p++;
+                       *dest++ = *p++;
+               }
+
+               *data = *p == '"' ? p+1 : p;
+               *dest = '\0';
+       }
+
+       return TRUE;
+}
+
 /*
  * Processing
  */
index 9b8f74896da0edbb93ddf2b7a75a595eea7fda58..83c430218277eb2b0c8d6631b3234bc20ce14c24 100644 (file)
@@ -1,6 +1,12 @@
 #ifndef AUTH_DIGEST_H
 #define AUTH_DIGEST_H
 
+/*
+ * Parsing
+ */
+
+bool auth_digest_parse_keyvalue(char **data, char **key_r, char **value_r);
+
 /*
  * Processing
  */
index 35441fd72f5cb8f0be0b43afd23ea75d659ff32f..26353fff069b09595b737714a5e884396a823198 100644 (file)
 #include "str.h"
 #include "str-sanitize.h"
 #include "settings-parser.h"
+#include "auth-digest.h"
 #include "password-scheme.h"
 
 #include "sasl-server-protected.h"
 
-/* Linear whitespace */
-#define IS_LWS(c) ((c) == ' ' || (c) == '\t')
-
 enum qop_option {
        QOP_AUTH        = 0x01, /* authenticate */
        QOP_AUTH_INT    = 0x02, /* + integrity protection, not supported yet */
@@ -228,62 +226,6 @@ verify_credentials(struct sasl_server_mech_request *auth_request,
                                    strlen(request->rspauth));
 }
 
-static bool parse_next(char **data, char **key, char **value)
-{
-       /* @UNSAFE */
-       char *p, *dest;
-
-       p = *data;
-       while (IS_LWS(*p)) p++;
-
-       /* get key */
-       *key = p;
-       while (*p != '\0' && *p != '=' && *p != ',')
-               p++;
-
-       if (*p != '=') {
-               *data = p;
-               return FALSE;
-       }
-
-       *value = p+1;
-
-       /* skip trailing whitespace in key */
-       while (p > *data && IS_LWS(p[-1]))
-               p--;
-       *p = '\0';
-
-       /* get value */
-       p = *value;
-       while (IS_LWS(*p)) p++;
-
-       if (*p != '"') {
-               while (*p != '\0' && *p != ',')
-                       p++;
-
-               *data = p;
-               /* If there is more to parse, ensure it won't get skipped
-                  because *p is set to NUL below */
-               if (**data != '\0') (*data)++;
-               while (IS_LWS(p[-1]))
-                       p--;
-               *p = '\0';
-       } else {
-               /* quoted string */
-               *value = dest = ++p;
-               while (*p != '\0' && *p != '"') {
-                       if (*p == '\\' && p[1] != '\0')
-                               p++;
-                       *dest++ = *p++;
-               }
-
-               *data = *p == '"' ? p+1 : p;
-               *dest = '\0';
-       }
-
-       return TRUE;
-}
-
 static bool
 auth_handle_response(struct digest_auth_request *request,
                     char *key, char *value, const char **error)
@@ -494,7 +436,7 @@ parse_digest_response(struct digest_auth_request *request,
           potential problems with NUL characters in strings. */
        copy = t_strdup_noconst(t_strndup(data, size));
        while (*copy != '\0') {
-               if (parse_next(&copy, &key, &value)) {
+               if (auth_digest_parse_keyvalue(&copy, &key, &value)) {
                        if (!auth_handle_response(request, key, value, error)) {
                                failed = TRUE;
                                break;