rewrite parse_hash_fingerprint()
PATCH 1/1 rewrite parse_hash_fingerprint()
The existing code was doing far too much work for too little gain - copying the string segment for scanf(), checking extra for spaces, making the result quite unreadable.
Verify each segment with (short-circuited) isxdigit() checks, then feed directly to scanf(), which will stop parsing on ':' or end-of-string.
Rewrite error message to differenciate "hash too short" (including number of bytes read) and "hash too long" (it did not terminate when we had enough bytes).
While at it, add an option printer for the resulting o->verify_hash list to show_settings().
--- src/openvpn/options.c | 64 +++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 32 deletions(-)
src/openvpn/options.c +32 -32
@@ -1082,56 +1082,43 @@ string_substitute(const char *src, int from, int to, struct gc_arena *gc)static struct verify_hash_list *parse_hash_fingerprint (const char *str,int nbytes,int msglevel,struct gc_arena *gc) { -int i; +int i=0 ;const char *cp = str;struct verify_hash_list *ret; ALLOC_OBJ_CLEAR_GC(ret, struct verify_hash_list, gc); -char term =1 ; +char term =0 ;int byte; -char bs[3 ]; -for (i =0 ; i < nbytes; ++i) +while (*cp && i < nbytes) { -if (strlen (cp) <2 ) +/* valid segments consist of exactly two hex digits, then ':' or EOS */ +if (!isxdigit (cp[0 ]) + || !isxdigit (cp[1 ]) + || (cp[2 ] !=':' && cp[2 ] !='\0' ) + ||sscanf (cp,"%x" , &byte) !=1 ) {msg (msglevel,"format error in hash fingerprint: %s" , str); +break ; } - bs[0 ] = *cp++; - bs[1 ] = *cp++; - bs[2 ] =0 ; -/* the format string "%x" passed to sscanf will ignore any space and -* will still try to parse the other character. However, this is not -* expected format for a fingerprint, therefore explictly check for -* blanks in the string and error out if any is found -*/ -if (bs[0 ] ==' ' || bs[1 ] ==' ' ) - { -msg (msglevel,"format error in hash fingerprint unexpected blank: %s" , - str); - } + ret->hash[i++] = (uint8_t )byte; - byte =0 ; -if (sscanf (bs,"%x" , &byte) !=1 ) - { -msg (msglevel,"format error in hash fingerprint hex byte: %s" , str); - } - ret->hash[i] = (uint8_t )byte; - term = *cp++; -if (term !=':' && term !=0 ) - { -msg (msglevel,"format error in hash fingerprint delimiter: %s" , str); - } -if (term ==0 ) + term = cp[2 ]; +if (term =='\0' ) {break ; } + cp +=3 ; } -if (term !=0 || i != nbytes-1 ) +if (i < nbytes) { -msg (msglevel,"hash fingerprint is different length than expected (%d bytes): %s" , nbytes, str); +msg (msglevel,"hash fingerprint is wrong length - expected %d bytes, got %d: %s" , nbytes, i, str); + } +else if (term !='\0' ) + { +msg (msglevel,"hash fingerprint too long - expected only %d bytes: %s" , nbytes, str); }return ret; }
@@ -1791,6 +1778,19 @@ show_settings(const struct options *o) } }SHOW_STR (remote_cert_eku); +if (o->verify_hash) + { +struct gc_arena gc =gc_new (); +struct verify_hash_list *hl = o->verify_hash; +while (hl) + { +char *s =format_hex_ex (hl->hash,sizeof (hl->hash),0 , +1 ,":" , &gc); +SHOW_PARM (verify_hash, s,"%s" ); + hl = hl->next; + } +gc_free (&gc); + }SHOW_INT (ssl_flags);SHOW_INT (tls_timeout);
Signature · 1 line
2.26.3
Hi,
Quoted 9 lines · Gert Doering
The existing code was doing far too much work for too little gain - copying the string segment for
scanf(), checking extra for spaces, making the result quite unreadable.Verify each segment with (short-circuited)
isxdigit()checks, then feed directly toscanf(), which will stop parsing on ':' or end-of-string.Rewrite error message to differenciate "hash too short" (including
differenciate -> differenTiate
Quoted 6 lines
number of bytes read) and "hash too long" (it did not terminate when we had enough bytes).
While at it, add an option printer for the resulting
o->verify_hashlist toshow_settings().
Thanks for this patch!
Simplifying existing code is as important as introducing new features and this proves that the project is still well maintained. (Feature-ACK)
Quoted 10 lines
Signed-off-by Gert Doering <[email protected]...>--- src/openvpn/options.c | 64 +++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 32 deletions(-)src/openvpn/options.c +0
-0
src/openvpn/options.c string_substitute(const char *src, int from, int to, struct gc_arena *gc) · line 1085
Quoted 5 lines
src/openvpn/options.c +1
-1@@ -1082,56 +1082,43 @@ string_substitute(const char *src, int from, int to, struct gc_arena *gc)static struct verify_hash_list *parse_hash_fingerprint (const char *str,int nbytes,int msglevel,struct gc_arena *gc) { -int i; +int i=0 ;
spaces around the '='
Quoted 7 lines
src/openvpn/options.c +1
-1const char *cp = str;struct verify_hash_list *ret; ALLOC_OBJ_CLEAR_GC(ret, struct verify_hash_list, gc); -char term =1 ; +char term =0 ;
src/openvpn/options.c string_substitute(const char *src, int from, int to, struct gc_arena *gc) · line 1098
Quoted 10 lines
src/openvpn/options.c +4
-3int byte; -char bs[3 ]; -for (i =0 ; i < nbytes; ++i) +while (*cp && i < nbytes) { -if (strlen (cp) <2 ) +/* valid segments consist of exactly two hex digits, then ':' or EOS */ +if (!isxdigit (cp[0 ]) + || !isxdigit(cp[1])
since we have a limit of 80 chars per line, how about putting the two conditions above on the same line?
src/openvpn/options.c string_substitute(const char *src, int from, int to, struct gc_arena *gc) · line 1100
Quoted 2 lines
src/openvpn/options.c +2
-0+ || (cp[2] != ':' && cp[2] != '\0') + || sscanf(cp, "%x", &byte) != 1)
same for this two, but less important as they are not really related.
Regarding the format string, we concluded on IRC that %hhx is part of C99 - why not using it here then?
src/openvpn/options.c string_substitute(const char *src, int from, int to, struct gc_arena *gc) · line 1103
Quoted 3 lines
src/openvpn/options.c +1
-0{msg (msglevel,"format error in hash fingerprint: %s" , str); +break ;
We had no break here because this function is always invoked with the FATAL bit set in the msglevel, therefore the msg() call will terminate the program.
This said, I prefer moving away from this assumption, so I personally welcome the break.
Quoted 6 lines
src/openvpn/options.c +0
-4} - bs[0 ] = *cp++; - bs[1 ] = *cp++; - bs[2 ] =0 ; -/* the format string "%x" passed to sscanf will ignore any space and
src/openvpn/options.c string_substitute(const char *src, int from, int to, struct gc_arena *gc) · line 1106
Quoted 10 lines
src/openvpn/options.c +1
-9-* will still try to parse the other character. However, this is not -* expected format for a fingerprint, therefore explictly check for -* blanks in the string and error out if any is found -*/ -if (bs[0 ] ==' ' || bs[1 ] ==' ' ) - { -msg (msglevel,"format error in hash fingerprint unexpected blank: %s" , - str); - } + ret->hash[i++] = (uint8_t )byte;
This cast would go away if byte is declared as uint8_t and we use %hhx in the sscanf().
Quoted 5 lines
src/openvpn/options.c +0
-4- byte =0 ; -if (sscanf (bs,"%x" , &byte) !=1 ) - { -msg (msglevel,"format error in hash fingerprint hex byte: %s" , str);
src/openvpn/options.c string_substitute(const char *src, int from, int to, struct gc_arena *gc) · line 1109
Quoted 10 lines
src/openvpn/options.c +2
-8- } - ret->hash[i] = (uint8_t )byte; - term = *cp++; -if (term !=':' && term !=0 ) - { -msg (msglevel,"format error in hash fingerprint delimiter: %s" , str); - } -if (term ==0 ) + term = cp[2 ]; +if (term =='\0' )
do we really need "term" at al at this point?
we could directly use cp[2] in the if condition.
src/openvpn/options.c string_substitute(const char *src, int from, int to, struct gc_arena *gc) · line 1119
Quoted 12 lines
src/openvpn/options.c +5
-2{break ; } + cp +=3 ; } -if (term !=0 || i != nbytes-1 ) +if (i < nbytes) { -msg (msglevel,"hash fingerprint is different length than expected (%d bytes): %s" , nbytes, str); +msg (msglevel,"hash fingerprint is wrong length - expected %d bytes, got %d: %s" , nbytes, i, str); + } +else if (term !='\0' )
same here: if we hit the else branch, it means that cp[0,1,2] are all defined so we can again compare cp[2] to '\0'.
Quoted 12 lines
src/openvpn/options.c +5
-0+ { +msg (msglevel,"hash fingerprint too long - expected only %d bytes: %s" , nbytes, str); }return ret; }@@ -1791,6 +1778,19 @@ show_settings(const struct options *o) } }SHOW_STR (remote_cert_eku); +if (o->verify_hash) + { +struct gc_arena gc =gc_new ();
src/openvpn/options.c show_settings(const struct options *o) · line 1793
Quoted 10 lines
src/openvpn/options.c +10
-0+struct verify_hash_list *hl = o->verify_hash; +while (hl) + { +char *s =format_hex_ex (hl->hash,sizeof (hl->hash),0 , +1 ,":" , &gc); +SHOW_PARM (verify_hash, s,"%s" ); + hl = hl->next; + } +gc_free (&gc); + }
Should we also print options->verify_hash_depth while at it?
src/openvpn/options.c show_settings(const struct options *o) · line 1796
Quoted 4 lines
src/openvpn/options.c +0
-0SHOW_INT (ssl_flags);SHOW_INT (tls_timeout);
Rest looks good!
I performed a bunch of tests with various strings and they all passed as expected.
Regards,
Signature · 1 line
Antonio Quartulli
Hi,
Quoted 2 lines · Antonio Quartulli
Quoted 1 line
Rewrite error message to differenciate "hash too short" (including
differenciate -> differenTiate
fixed.
Quoted 4 lines
src/openvpn/options.c string_substitute(const char *src, int from, int to, struct gc_arena *gc) · line 1085
Quoted 2 lines
src/openvpn/options.c +1
-1-int i; +int i=0 ;spaces around the '='
fixed.
Quoted 7 lines
src/openvpn/options.c string_substitute(const char *src, int from, int to, struct gc_arena *gc) · line 1098
Quoted 4 lines
src/openvpn/options.c +3
-1-if (strlen (cp) <2 ) +/* valid segments consist of exactly two hex digits, then ':' or EOS */ +if (!isxdigit (cp[0 ]) + || !isxdigit(cp[1])since we have a limit of 80 chars per line, how about putting the two conditions above on the same line?
I tried, but found it "not easier to read".
The way it is, it's really "3 lines for the 3 checks for the 3 bytes" (and then one check for "all of it"), so I find it more logical.
Quoted 2 lines
Regarding the format string, we concluded on IRC that %hhx is part of C99 - why not using it here then?
As I said there: C99 is a compiler standard, but %hhx is something the library has to provide. I do not trust C libraries in that regard - imagine someone compiling OpenVPN with a nice and shiny C99 compiler and then linking with some stripped down C library for an embedded system, which doesn't do %hhx. Interesting memory corruption, impossible to diagnose.
Now, I have no idea whether uc-libc, musl, ... *do* support %hhx, but I do not see use of this as particular readability-enhancing while it carries a portability risk. %x -> (int) works, everywhere.
Quoted 4 lines
Quoted 1 line
+ ret->hash[i++] = (uint8_t)byte;
This cast would go away if byte is declared as
uint8_tand we use %hhx in thesscanf().
We could sscanf() to ret->hash[i] right away, then - but again, I do not think the extra step would be better for readability, and it does carry a risk.
Quoted 6 lines
src/openvpn/options.c string_substitute(const char *src, int from, int to, struct gc_arena *gc) · line 1109
Quoted 3 lines
src/openvpn/options.c +2
-1-if (term ==0 ) + term = cp[2 ]; +if (term =='\0' )do we really need "term" at al at this point?
we could directly use cp[2] in the if condition.
If we exit the loop because we have reached (i == nbytes), cp will point at the character "after the end-of-string" (because of cp +=3), so cp[2] is well into "undefined territory" land.
Quoted 9 lines
src/openvpn/options.c string_substitute(const char *src, int from, int to, struct gc_arena *gc) · line 1119
Quoted 6 lines
src/openvpn/options.c +4
-1+if (i < nbytes) { -msg (msglevel,"hash fingerprint is different length than expected (%d bytes): %s" , nbytes, str); +msg (msglevel,"hash fingerprint is wrong length - expected %d bytes, got %d: %s" , nbytes, i, str); + } +else if (term !='\0' )same here: if we hit the else branch, it means that cp[0,1,2] are all defined so we can again compare cp[2] to '\0'.
Actually, no. We hit this branch when we exit the loop due to (i==nbytes) after the cp +=3; This happens if you have malformed input that is too long, so after having found "nbytes", term is ":", so cp += 3, and then the loop ends.
Pure OpenVPN style would be, of course, to do the cp += 3 first (and alway), and then look at cp[-1], but no, I'm not going there.
Quoted 1 line
Should we also print
options->verify_hash_depthwhile at it?
Done!
v2 incoming... including a bugfix to the original commit :)
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...PATCH v2 1/1 rewrite parse_hash_fingerprint()
In reply to Gert DoeringThe existing code was doing far too much work for too little gain - copying the string segment for scanf(), checking extra for spaces, making the result quite unreadable.
Verify each segment with (short-circuited) isxdigit() checks, then feed directly to scanf(), which will stop parsing on ':' or end-of-string.
Rewrite error message to differentiate "hash too short" (including number of bytes read) and "hash too long" (it did not terminate when we had enough bytes).
While at it, add an option printer for the resulting o->verify_hash list to show_settings().
v2:
fix typo in commit message
appease whitespace dragon
add printing of verify_hash_algo and verify_hash_depth print correct hash length for SHA1 certs
fix incorrect assignment to options->verify_hash_algo in c3a7065d5
--- src/openvpn/options.c | 69 ++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 33 deletions(-)
src/openvpn/options.c +36 -33
@@ -1082,56 +1082,43 @@ string_substitute(const char *src, int from, int to, struct gc_arena *gc)static struct verify_hash_list *parse_hash_fingerprint (const char *str,int nbytes,int msglevel,struct gc_arena *gc) { -int i; +int i =0 ;const char *cp = str;struct verify_hash_list *ret; ALLOC_OBJ_CLEAR_GC(ret, struct verify_hash_list, gc); -char term =1 ; +char term =0 ;int byte; -char bs[3 ]; -for (i =0 ; i < nbytes; ++i) +while (*cp && i < nbytes) { -if (strlen (cp) <2 ) +/* valid segments consist of exactly two hex digits, then ':' or EOS */ +if (!isxdigit (cp[0 ]) + || !isxdigit (cp[1 ]) + || (cp[2 ] !=':' && cp[2 ] !='\0' ) + ||sscanf (cp,"%x" , &byte) !=1 ) {msg (msglevel,"format error in hash fingerprint: %s" , str); +break ; } - bs[0 ] = *cp++; - bs[1 ] = *cp++; - bs[2 ] =0 ; -/* the format string "%x" passed to sscanf will ignore any space and -* will still try to parse the other character. However, this is not -* expected format for a fingerprint, therefore explictly check for -* blanks in the string and error out if any is found -*/ -if (bs[0 ] ==' ' || bs[1 ] ==' ' ) - { -msg (msglevel,"format error in hash fingerprint unexpected blank: %s" , - str); - } + ret->hash[i++] = (uint8_t )byte; - byte =0 ; -if (sscanf (bs,"%x" , &byte) !=1 ) - { -msg (msglevel,"format error in hash fingerprint hex byte: %s" , str); - } - ret->hash[i] = (uint8_t )byte; - term = *cp++; -if (term !=':' && term !=0 ) - { -msg (msglevel,"format error in hash fingerprint delimiter: %s" , str); - } -if (term ==0 ) + term = cp[2 ]; +if (term =='\0' ) {break ; } + cp +=3 ; } -if (term !=0 || i != nbytes-1 ) +if (i < nbytes) { -msg (msglevel,"hash fingerprint is different length than expected (%d bytes): %s" , nbytes, str); +msg (msglevel,"hash fingerprint is wrong length - expected %d bytes, got %d: %s" , nbytes, i, str); + } +else if (term !='\0' ) + { +msg (msglevel,"hash fingerprint too long - expected only %d bytes: %s" , nbytes, str); }return ret; }
@@ -1791,6 +1778,23 @@ show_settings(const struct options *o) } }SHOW_STR (remote_cert_eku); +if (o->verify_hash) + { +SHOW_INT (verify_hash_algo); +SHOW_INT (verify_hash_depth); +struct gc_arena gc =gc_new (); +struct verify_hash_list *hl = o->verify_hash; +int digest_len = (o->verify_hash_algo ==MD_SHA1 ) ?SHA_DIGEST_LENGTH : +SHA256_DIGEST_LENGTH ; +while (hl) + { +char *s =format_hex_ex (hl->hash, digest_len,0 , +1 ,":" , &gc); +SHOW_PARM (verify_hash, s,"%s" ); + hl = hl->next; + } +gc_free (&gc); + }SHOW_INT (ssl_flags);SHOW_INT (tls_timeout);
@@ -8190,7 +8194,6 @@ add_option(struct options *options,if ((!p[2 ] && !is_inline) || (p[2 ] &&streq (p[2 ],"SHA1" ))) { options->verify_hash_algo =MD_SHA1 ; - options->verify_hash_algo =SHA_DIGEST_LENGTH ; digest_len =SHA_DIGEST_LENGTH ; }else if (p[2 ] && !streq (p[2 ],"SHA256" ))
Signature · 1 line
2.26.3
Hi,
Quoted 15 lines · Gert Doering
The existing code was doing far too much work for too little gain - copying the string segment for
scanf(), checking extra for spaces, making the result quite unreadable.Verify each segment with (short-circuited)
isxdigit()checks, then feed directly toscanf(), which will stop parsing on ':' or end-of-string.Rewrite error message to differentiate "hash too short" (including number of bytes read) and "hash too long" (it did not terminate when we had enough bytes).
While at it, add an option printer for the resulting
o->verify_hashlist toshow_settings().
Quoted 7 lines
v2:
fix typo in commit message
appease whitespace dragon
add printing ofverify_hash_algoandverify_hash_depthprint correct hash length for SHA1 certs
fix incorrect assignment tooptions->verify_hash_algoinc3a7065d5
Thanks for the changes :)
Quoted 1 line
Signed-off-by Gert Doering <[email protected]...>
Patch looks good and does what it says. Couldn't break it in any way.
Compile tests passed.
Basic tests on various broken/non-broken hex strings passed too.
Regards,
Signature · 1 line
Antonio Quartulli
Patch has been applied to the master branch.
.. and I still have no test setup for this... "next FOOM!"...
commit 925f0180318033f9ea7885b40b4b8200b35abbca (master)
Author: Gert Doering Date: Tue Apr 27 13:03:00 2021 +0200
rewrite parse_hash_fingerprint()
Signed-off-by: Gert Doering <[email protected]...>
Acked-by: Antonio Quartulli <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg22241.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering