From: "flichtenheld (Code Review)" Date: Fri, 8 Dec 2023 16:29:54 +0000 References: Reply-To: frank@...2641..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net Message-ID: <76c50ee7fb8a7f7f54543ce7b5abe5be887f126b-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="HiF9i+MFgYM="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: misc: make get_auth_challenge static List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos Cc: openvpn-devel --HiF9i+MFgYM= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: plaisthos. Hello plaisthos, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/476?usp=3Demail to review the following change. Change subject: misc: make get_auth_challenge static ...................................................................... misc: make get_auth_challenge static Not used outside of misc.c. Rename to parse_auth_challenge since it really just parses the string that you put in into the struct. Add doxygen documentation. Change-Id: I0abeec9f862aea1f6a8fdf350fa0008cf2e5d613 Signed-off-by: Frank Lichtenheld --- M src/openvpn/misc.c M src/openvpn/misc.h M src/openvpn/ssl.h 3 files changed, 103 insertions(+), 85 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/76/476/1 diff --git a/src/openvpn/misc.c b/src/openvpn/misc.c index ce6e4fd..9352843 100644 --- a/src/openvpn/misc.c +++ b/src/openvpn/misc.c @@ -124,6 +124,88 @@ } return true; } + +/** + * Parses an authentication challenge string and returns an auth_challenge= _info structure. + * The authentication challenge string should follow the dynamic challenge= /response protocol. + * + * See doc/management-notes.txt for more info on the the dynamic challenge= /response protocol + * implemented here. + * + * @param auth_challenge The authentication challenge string to parse. + * @param gc The gc_arena structure for memory allocation. + * + * @return A pointer to the parsed auth_challenge_info struc= ture, or NULL if parsing fails. + */ +static struct auth_challenge_info * +parse_auth_challenge(const char *auth_challenge, struct gc_arena *gc) +{ + if (auth_challenge) + { + struct auth_challenge_info *ac; + const int len =3D strlen(auth_challenge); + char *work =3D (char *) gc_malloc(len+1, false, gc); + char *cp; + + struct buffer b; + buf_set_read(&b, (const uint8_t *)auth_challenge, len); + + ALLOC_OBJ_CLEAR_GC(ac, struct auth_challenge_info, gc); + + /* parse prefix */ + if (!buf_parse(&b, ':', work, len)) + { + return NULL; + } + if (strcmp(work, "CRV1")) + { + return NULL; + } + + /* parse flags */ + if (!buf_parse(&b, ':', work, len)) + { + return NULL; + } + for (cp =3D work; *cp !=3D '\0'; ++cp) + { + const char c =3D *cp; + if (c =3D=3D 'E') + { + ac->flags |=3D CR_ECHO; + } + else if (c =3D=3D 'R') + { + ac->flags |=3D CR_RESPONSE; + } + } + + /* parse state ID */ + if (!buf_parse(&b, ':', work, len)) + { + return NULL; + } + ac->state_id =3D string_alloc(work, gc); + + /* parse user name */ + if (!buf_parse(&b, ':', work, len)) + { + return NULL; + } + ac->user =3D (char *) gc_malloc(strlen(work)+1, true, gc); + openvpn_base64_decode(work, (void *)ac->user, -1); + + /* parse challenge text */ + ac->challenge_text =3D string_alloc(BSTR(&b), gc); + + return ac; + } + else + { + return NULL; + } +} + #endif /* ifdef ENABLE_MANAGEMENT */ /* @@ -287,7 +369,7 @@ #ifdef ENABLE_MANAGEMENT if (auth_challenge && (flags & GET_USER_PASS_DYNAMIC_CHALLENGE= ) && response_from_stdin) { - struct auth_challenge_info *ac =3D get_auth_challenge(auth= _challenge, &gc); + struct auth_challenge_info *ac =3D parse_auth_challenge(au= th_challenge, &gc); if (ac) { char *response =3D (char *) gc_malloc(USER_PASS_LEN, f= alse, &gc); @@ -392,83 +474,6 @@ return true; } -#ifdef ENABLE_MANAGEMENT - -/* - * See management/management-notes.txt for more info on the - * the dynamic challenge/response protocol implemented here. - */ -struct auth_challenge_info * -get_auth_challenge(const char *auth_challenge, struct gc_arena *gc) -{ - if (auth_challenge) - { - struct auth_challenge_info *ac; - const int len =3D strlen(auth_challenge); - char *work =3D (char *) gc_malloc(len+1, false, gc); - char *cp; - - struct buffer b; - buf_set_read(&b, (const uint8_t *)auth_challenge, len); - - ALLOC_OBJ_CLEAR_GC(ac, struct auth_challenge_info, gc); - - /* parse prefix */ - if (!buf_parse(&b, ':', work, len)) - { - return NULL; - } - if (strcmp(work, "CRV1")) - { - return NULL; - } - - /* parse flags */ - if (!buf_parse(&b, ':', work, len)) - { - return NULL; - } - for (cp =3D work; *cp !=3D '\0'; ++cp) - { - const char c =3D *cp; - if (c =3D=3D 'E') - { - ac->flags |=3D CR_ECHO; - } - else if (c =3D=3D 'R') - { - ac->flags |=3D CR_RESPONSE; - } - } - - /* parse state ID */ - if (!buf_parse(&b, ':', work, len)) - { - return NULL; - } - ac->state_id =3D string_alloc(work, gc); - - /* parse user name */ - if (!buf_parse(&b, ':', work, len)) - { - return NULL; - } - ac->user =3D (char *) gc_malloc(strlen(work)+1, true, gc); - openvpn_base64_decode(work, (void *)ac->user, -1); - - /* parse challenge text */ - ac->challenge_text =3D string_alloc(BSTR(&b), gc); - - return ac; - } - else - { - return NULL; - } -} - -#endif /* ifdef ENABLE_MANAGEMENT */ - void purge_user_pass(struct user_pass *up, const bool force) { diff --git a/src/openvpn/misc.h b/src/openvpn/misc.h index b000b72..98de90a 100644 --- a/src/openvpn/misc.h +++ b/src/openvpn/misc.h @@ -86,8 +86,6 @@ const char *challenge_text; }; -struct auth_challenge_info *get_auth_challenge(const char *auth_challenge,= struct gc_arena *gc); - /* * Challenge response info on client as pushed by server. */ @@ -120,12 +118,31 @@ #define GET_USER_PASS_INLINE_CREDS (1<<10) /* indicates that auth_file is= actually inline creds */ +/** + * Retrieves the user credentials from various sources depending on the fl= ags. + * + * @param up The user_pass structure to store the retrieved credentials. + * @param auth_file The path to the authentication file. Might be NULL. + * @param prefix The prefix to prepend to user prompts. + * @param flags Additional flags to control the behavior of the function. + * @param auth_challenge The authentication challenge string. + * @return true if the user credentials were successfully retrieved, false= otherwise. + */ bool get_user_pass_cr(struct user_pass *up, const char *auth_file, const char *prefix, const unsigned int flags, const char *auth_challenge); +/** + * Retrieves the user credentials from various sources depending on the fl= ags. + * + * @param up The user_pass structure to store the retrieved credentials. + * @param auth_file The path to the authentication file. Might be NULL. + * @param prefix The prefix to prepend to user prompts. + * @param flags Additional flags to control the behavior of the function. + * @return true if the user credentials were successfully retrieved, false= otherwise. + */ static inline bool get_user_pass(struct user_pass *up, const char *auth_file, diff --git a/src/openvpn/ssl.h b/src/openvpn/ssl.h index 6ba6ff8..71b99db 100644 --- a/src/openvpn/ssl.h +++ b/src/openvpn/ssl.h @@ -410,11 +410,7 @@ bool ssl_clean_auth_token(void); #ifdef ENABLE_MANAGEMENT -/* - * ssl_get_auth_challenge will parse the server-pushed auth-failed - * reason string and return a dynamically allocated - * auth_challenge_info struct. - */ + void ssl_purge_auth_challenge(void); void ssl_put_auth_challenge(const char *cr_str); -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/476?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I0abeec9f862aea1f6a8fdf350fa0008cf2e5d613 Gerrit-Change-Number: 476 Gerrit-PatchSet: 1 Gerrit-Owner: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-MessageType: newchange --HiF9i+MFgYM= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: plaisthos.

flichtenheld would like plaisthos to review this change.

Vi= ew Change

misc: make get_auth_challenge static

Not used outside = of misc.c.

Rename to parse_auth_challenge since it really just parse= s
the string that you put in into the struct.

Add doxygen documen= tation.

Change-Id: I0abeec9f862aea1f6a8fdf350fa0008cf2e5d613
Sign= ed-off-by: Frank Lichtenheld <frank@...2641...>
---
M src/openv= pn/misc.c
M src/openvpn/misc.h
M src/openvpn/ssl.h
3 files changed= , 103 insertions(+), 85 deletions(-)

git pull ssh://gerrit.openvp=
n.net:29418/openvpn refs/changes/76/476/1
diff --git a/src/openvpn/=
misc.c b/src/openvpn/misc.c
index ce6e4fd..9352843 100644
--- a/src/openvpn/misc.c
+++ b/src/openvpn/m= isc.c
@@ -124,6 +124,88 @@
} return true;
}
+
+/**
+ * Parses a= n authentication challenge string and returns an auth_challenge_info struct= ure.
+ * The authenti= cation challenge string should follow the dynamic challenge/response protoc= ol.
+ *
+ * See doc/management-notes.txt f= or more info on the the dynamic challenge/response protocol

+ * implemented here.
+ *
+ * @param auth_challenge The authentication challen= ge string to parse.
+= * @param gc The gc_arena structure for memory allocation.

+ *
+ * @return A pointer to the= parsed auth_challenge_info structure, or NULL if parsing fails.
= + */
+static struct auth_challenge_info *
= +parse_auth_challenge(const cha= r *auth_challenge, struct gc_arena *gc)
+{
+ if (auth_challenge)
+ {
+ = struct auth_challenge_info *ac;
+ const int len =3D strlen(auth_challenge);
+ char *work =3D (char *) g= c_malloc(len+1, false, gc);
+ char *cp;
+
+ struc= t buffer b;
+ = buf_set_read(&b, (const uint8_t *)auth_challenge, len);
+
+ ALLOC_OBJ_CLEAR_GC(ac, struct auth_challenge_i= nfo, gc);
++ /* parse prefix */
+ if (!buf_parse= (&b, ':', work, len))
+ {
+ return NULL;
+ }
+ = if (strcmp(work, "CRV1"))
+ {
+ return NULL;
+ }
+
+ /* = parse flags */
+ = if (!buf_parse(&b, ':', work, len))
+ {
+ return NULL;
+ }
+ for (cp =3D work; *cp !=3D '\0'; ++cp)
+ {
+ const char c =3D *cp;<= /span>
+ if (c = =3D=3D 'E')
+= {
+ = ac->flags |=3D CR_ECHO;
+ }
+ else if (c =3D=3D 'R')
+ {
+ ac->flags |=3D CR_RESPONSE;<= /span>
+ }=
+ }
+
+ /* parse state ID */
+ if (!buf_parse(&b, ':', wo= rk, len))
+ {<= /span>
+ return N= ULL;
+ }

+ ac->state_id = =3D string_alloc(work, gc);
+
+ /* = parse user name */
+ = if (!buf_parse(&b, ':', work, len))
+ {
+ return NULL;
+ }
+ ac->user =3D (char *) gc_malloc(strlen(work)= +1, true, gc);
+ = openvpn_base64_decode(work, (void *)ac->user, -1);
+
+ /* parse challenge text */
+ ac->challenge_text =3D string_= alloc(BSTR(&b), gc);
+
+ return= ac;
+ }+ else
+ {
+ return NULL;
+ }
+}
+ #endif /* ifdef ENABLE_MANAGEMENT */

/*
@@ -287,7 +369,7 @@
#ifdef ENABLE_= MANAGEMENT
if (auth_challenge && (flag= s & GET_USER_PASS_DYNAMIC_CHALLENGE) && response_from_stdin)

{
- struct auth_challenge_info *ac =3D get_auth_challeng= e(auth_challenge, &gc);
+ struct auth_challenge_info *ac =3D parse_auth_chall= enge(auth_challenge, &gc);
if (ac)

{
char *= response =3D (char *) gc_malloc(USER_PASS_LEN, false, &gc);
<= span>@@ -392,83 +474,6 @@
return true;
}

-= #ifdef ENABLE_MANAGEMENT
-/*
- * See management/management-notes.tx= t for more info on the
= - * the dynamic challenge/response protocol implemented here.
- */
-struct auth_challenge_info *
-get_auth_challenge(const char *auth_challenge,= struct gc_arena *gc)
-= {
- if (auth_challen= ge)
- {
- struct auth_challenge_info= *ac;
- const in= t len =3D strlen(auth_challenge);
- char *work =3D (char *) gc_malloc(len+1, false, gc);

- char *cp;=
-
- struct buffer b;
- buf_set_read(&b, (const uint8_t *)= auth_challenge, len);
-=
- ALLOC_OBJ_CLE= AR_GC(ac, struct auth_challenge_info, gc);
-
-= /* parse prefix */
- if (!buf_parse(&b, ':', work, len))
- {
- return NULL;
- }
- if (strcmp(work, "CRV1"))
<= span style=3D"color: hsl(0, 100%, 40%);">- {
- return NULL;
- }
-
- /* parse flags */
- if (!buf_parse(&b, ':', work, len))
- {
- return NULL;
- }
- for (cp =3D work; *cp !=3D '\0'; ++cp)<= /span>
- {
- const char c =3D *cp;<= /span>
- if (c =3D= =3D 'E')
- = {
- = ac->flags |=3D CR_ECHO;
- }
-= else if (c =3D=3D 'R')
- {
- ac->flags |=3D CR_RESPONSE;
- }

- }
-
- = /* parse state ID */
- if (!buf_parse(&b, ':', work, len))
- {
- return NULL;
- }
- ac->state_id =3D string_alloc(work, gc);

-
- /* parse user name */
- if (!buf_parse(&b, ':&= #39;, work, len))
- = {
- retu= rn NULL;
- }

- ac->user =3D (= char *) gc_malloc(strlen(work)+1, true, gc);
- openvpn_base64_decode(work, (void *)ac->u= ser, -1);
-
<= span style=3D"color: hsl(0, 100%, 40%);">- /* parse challenge text *= /
- ac->chall= enge_text =3D string_alloc(BSTR(&b), gc);
-
-= }
- else<= br>- {
- return NULL;
- }
-}
-

-#endif /* ifdef ENABLE_MA= NAGEMENT */
- void
purge_user_pass(struct user_pass *up, const b= ool force)
{
diff --git a/src/openvpn/misc= .h b/src/openvpn/misc.h
index b000b72..98de90a 100644

--- a/src/openvpn/misc.h
+++ b/src/openvpn/misc.= h
@@ -86,8 +86,6 @@
const char *challe= nge_text;
};

-struct auth_challenge_info *get_auth_challenge(co= nst char *auth_challenge, struct gc_arena *gc);
-
/*
* Challeng= e response info on client as pushed by server.
*/<= br>@@ -120,12 +118,31 @@

#define G= ET_USER_PASS_INLINE_CREDS (1<<10) /* indicates that auth_file is act= ually inline creds */

+/**
+ * Retrieves the user credentials from various sources depending on the= flags.
+ *+ * @param up The user_pass st= ructure to store the retrieved credentials.
+ * @param auth_file The path to the authentication = file. Might be NULL.
= + * @param prefix The prefix to prepend to user prompts.
+ * @param flags Additional flags to co= ntrol the behavior of the function.
+ * @param auth_challenge The authentication challenge strin= g.
+ * @return true i= f the user credentials were successfully retrieved, false otherwise.=
+ */
bool = get_user_pass_cr(struct user_pass *up,
= const char *auth_file,
const char= *prefix,
const unsigned int flags,<= /span>
const char *auth_challenge);<= br>
+/**

+ * Retrieves the user cre= dentials from various sources depending on the flags.
+ *
+ * @param up The user_pass structure to store the retrieve= d credentials.
+ * @p= aram auth_file The path to the authentication file. Might be NULL.+ * @param prefix The prefix = to prepend to user prompts.
+ * @param flags Additional flags to control the behavior of the fun= ction.
+ * @return tr= ue if the user credentials were successfully retrieved, false otherwise.
+ */
s= tatic inline bool
get_user_pass(struct user_pass *up,
const char *auth_file,
diff --gi= t a/src/openvpn/ssl.h b/src/openvpn/ssl.h
index 6ba6ff8..71= b99db 100644
--- a/src/openvpn/ssl.h
+++ b/= src/openvpn/ssl.h
@@ -410,11 +410,7 @@
boo= l ssl_clean_auth_token(void);

#ifdef ENA= BLE_MANAGEMENT
-/*
- * ssl_get_auth_challenge = will parse the server-pushed auth-failed
- * reason string and return a dynamically allocated
- * auth_challenge_info str= uct.
- */
+

void ssl_purge_= auth_challenge(void);

void ssl_put_auth_= challenge(const char *cr_str);

To view,= visit ch= ange 476. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I0abeec9f862aea1f6a8fdf350fa= 0008cf2e5d613
Gerrit-Change-Number: 476
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-MessageType: newchange
--HiF9i+MFgYM=--