Support multiple x509 field list to be username
PATCH 1/2 Support multiple x509 field list to be username
OpenVPN has the ability to choose different x509 field in case "CN" can't be use used to be unique connected username since commit 935c62be9c0c8a256112df818bfb8470586a23b6.
Unfortunately it's not enough in case client has multiple and valid certificates from PKI for different devices (ex. laptop, mobile, etc) with the same CN/UID.
Having --duplicate-cn as a workaround helps only partially - clients can be connected, but it breaks coexistance with --ifconfig-pool-persist, --client-config-dir and opens doors to DoS possibility since same client device (with the same cert) being reconnected doesn't replace previously connected session no more, so can exhaust server ressources (ex. address pool) and can prevent other clients to be connected.
With this patch, multiple x509 fields incl. "serialNumber" can be chosen to be username as --x509-username-files space-separated parameters. Multiple fields will be joined into one username using '/' delimeter for consistency with CN/addr logging and preserving ability for hierarchical ccd. As long as resulting username is unique, --duplicate-cn will not be required. Default value is preserved as "CN" only.
Openssl backend is the only supported at the moment, since so far MbedTLS has no alt user name support at all.
--- doc/man-sections/tls-options.rst | 9 ++++--- src/openvpn/init.c | 4 +-- src/openvpn/options.c | 46 ++++++++++++++++++-------------- src/openvpn/options.h | 4 +-- src/openvpn/ssl.h | 1 + src/openvpn/ssl_common.h | 8 +++++- src/openvpn/ssl_verify.c | 35 ++++++++++++++++-------- src/openvpn/ssl_verify_openssl.c | 15 +++++++++++ 8 files changed, 83 insertions(+), 39 deletions(-)
doc/man-sections/tls-options.rst +6 -3
@@ -632,13 +632,13 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa options can be defined to track multiple attributes. --x509-username-field args - Field in the X.509 certificate subject to be used as the username + Field list in the X.509 certificate subject to be used as the username (default :code:`CN`). Valid syntax: :: - x509-username-field [ext:]fieldname + x509-username-field [ext:]fieldname [[ext:]fieldname...] Typically, this option is specified with **fieldname** as either of the following:
@@ -646,6 +646,7 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa x509-username-field emailAddress x509-username-field ext:subjectAltName + x509-username-field CN serialNumber The first example uses the value of the :code:`emailAddress` attribute in the certificate's Subject field as the username. The second example
@@ -653,7 +654,9 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa ``fieldname`` :code:`subjectAltName` be searched for an rfc822Name (email) field to be used as the username. In cases where there are multiple email addresses in :code:`ext:fieldname`, the last occurrence - is chosen. + is chosen. The last example uses the value of :code:`CN` attribute in + the Subject field and the hex representation of certificate's serial + number delimited by slash symbol as the resulting username. When this option is used, the ``--verify-x509-name`` option will match against the chosen ``fieldname`` instead of the Common Name.
src/openvpn/init.c +2 -2
@@ -2912,9 +2912,9 @@ do_init_crypto_tls(struct context *c, const unsigned int flags) to.verify_hash = options->verify_hash; to.verify_hash_algo = options->verify_hash_algo;#ifdef ENABLE_X509ALTUSERNAME - to.x509_username_field = (char *) options->x509_username_field; +memmove (to.x509_username_field, options->x509_username_field,sizeof (to.x509_username_field));#else - to.x509_username_field =X509_USERNAME_FIELD_DEFAULT ; + to.x509_username_field[0 ] =X509_USERNAME_FIELD_DEFAULT ;#endif to.es = c->c2.es; to.net_ctx = &c->net_ctx;
src/openvpn/options.c +26 -20
72 lines not on this page · raw message
src/openvpn/options.h +2 -2
@@ -582,8 +582,8 @@ struct optionsint handshake_window;#ifdef ENABLE_X509ALTUSERNAME -/* Field used to be the username in X509 cert. */ -char *x509_username_field; +/* Field list used to be the username in X509 cert. */ +char *x509_username_field[MAX_PARMS ];#endif /* Old key allowed to live n seconds after new key goes active */
src/openvpn/ssl.h +1 -0
@@ -119,6 +119,7 @@/* Default field in X509 to be username */ #define X509_USERNAME_FIELD_DEFAULT "CN" +#define X509_USERNAME_FIELD_DELIMITER '/'#define KEY_METHOD_2 2
src/openvpn/ssl_common.h +7 -1
@@ -285,7 +285,13 @@ struct tls_optionsconst char *remote_cert_eku;uint8_t *verify_hash;hash_algo_type verify_hash_algo; -char *x509_username_field; + +/* Field list used to be the username in X509 cert. */ +#ifdef ENABLE_X509ALTUSERNAME +char *x509_username_field[MAX_PARMS ]; +#else +char *x509_username_field[2 ]; +#endif /* allow openvpn config info to be * passed over control channel */
src/openvpn/ssl_verify.c +24 -11
@@ -48,7 +48,7 @@#include "push.h" /** Maximum length of common name */ -#define TLS_USERNAME_LEN 64 +#define TLS_USERNAME_LEN 128static void string_mod_remap_name (char *str)
@@ -640,6 +640,7 @@ verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_depchar common_name[TLS_USERNAME_LEN +1 ] = {0 };/* null-terminated */ const struct tls_options *opt;struct gc_arena gc =gc_new (); +size_t i, size; opt = session->opt;ASSERT (opt);
@@ -660,19 +661,31 @@ verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_depstring_replace_leading (subject,'-' ,'_' );/* extract the username (default is CN) */ -if (SUCCESS !=backend_x509_get_username (common_name,sizeof (common_name), - opt->x509_username_field, cert)) + size =sizeof (common_name); +for (i =0 ; opt->x509_username_field[i] !=NULL && size > !!i; i++) { -if (!cert_depth) +char *buf = common_name +sizeof (common_name) - size; + +if (SUCCESS !=backend_x509_get_username (buf + !!i, size - !!i, + opt->x509_username_field[i], cert)) { -msg (D_TLS_ERRORS ,"VERIFY ERROR: could not extract %s from X509 " -"subject string ('%s') -- note that the username length is " -"limited to %d characters" , - opt->x509_username_field, - subject, -TLS_USERNAME_LEN ); - goto cleanup; +break ; + } +else if (i !=0 ) + { + *buf =X509_USERNAME_FIELD_DELIMITER ; } + size -=strlen (buf); + } +if (cert_depth ==0 && opt->x509_username_field[i] !=NULL ) + { +msg (D_TLS_ERRORS ,"VERIFY ERROR: could not extract %s from X509 " +"subject string ('%s') -- note that the username length is " +"limited to %d characters" , + opt->x509_username_field[i], + subject, +TLS_USERNAME_LEN ); + goto cleanup; }/* enforce character class restrictions in common name */
src/openvpn/ssl_verify_openssl.c +15 -0
@@ -268,6 +268,21 @@ backend_x509_get_username(char *common_name, int cn_len, return FAILURE; } } + else if (strcmp(LN_serialNumber,x509_username_field) == 0) + { +ASN1_INTEGER *asn1_i =X509_get_serialNumber (peer_cert); +struct gc_arena gc =gc_new (); +char *serial; + + serial =format_hex_ex (asn1_i->data, asn1_i->length,0 ,1 ,NULL , &gc); +if (!serial || cn_len <=strlen (serial)+2 ) + { +gc_free (&gc); +return FAILURE ; + } +openvpn_snprintf (common_name, cn_len,"0x%s" , serial); +gc_free (&gc); + } else #endif if (FAILURE == extract_x509_field_ssl(X509_get_subject_name(peer_cert),
Signature · 1 line
2.17.1
PATCH v2 1/1 Support x509 field list to be username
OpenVPN has the ability to choose different x509 field in case "CN" can't be use used to be unique connected username since commit 935c62be9c0c8a256112df818bfb8470586a23b6.
Unfortunately it's not enough in case client has multiple and valid certificates from PKI for different devices (ex. laptop, mobile, etc) with the same CN/UID.
Having --duplicate-cn as a workaround helps only partially: clients can be connected, but it breaks coexistance with --ifconfig-pool-persist, --client-config-dir and opens doors to DoS possibility since same client device (with the same cert) being reconnected no more replaces previously connected session, so it can exhaust server resources (ex. address pool) and can prevent other clients to be connected.
With this patch, multiple x509 fields incl. "serialNumber" can be chosen to be username as --x509-username-field space-separated parameters. Multiple fields will be joined into one username using '/' delimeter for consistency with CN/addr logging and preserving ability for hierarchical ccd. As long as resulting username is unique, --duplicate-cn will not be required. Default value is preserved as "CN" only.
Openssl backend is the only supported at the moment, since so far MbedTLS has no alt user name support at all.
v2: conform C99, man update, fix typos
--- doc/man-sections/tls-options.rst | 9 ++++-- src/openvpn/init.c | 4 +-- src/openvpn/options.c | 49 +++++++++++++++++--------------- src/openvpn/options.h | 4 +-- src/openvpn/ssl.h | 1 + src/openvpn/ssl_common.h | 8 +++++- src/openvpn/ssl_verify.c | 35 ++++++++++++++++------- src/openvpn/ssl_verify_openssl.c | 14 +++++++++ 8 files changed, 82 insertions(+), 42 deletions(-)
doc/man-sections/tls-options.rst +6 -3
@@ -632,13 +632,13 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa options can be defined to track multiple attributes. --x509-username-field args - Field in the X.509 certificate subject to be used as the username + Field list in the X.509 certificate subject to be used as the username (default :code:`CN`). Valid syntax: :: - x509-username-field [ext:]fieldname + x509-username-field [ext:]fieldname [[ext:]fieldname...] Typically, this option is specified with **fieldname** as either of the following:
@@ -646,6 +646,7 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa x509-username-field emailAddress x509-username-field ext:subjectAltName + x509-username-field CN serialNumber The first example uses the value of the :code:`emailAddress` attribute in the certificate's Subject field as the username. The second example
@@ -653,7 +654,9 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa ``fieldname`` :code:`subjectAltName` be searched for an rfc822Name (email) field to be used as the username. In cases where there are multiple email addresses in :code:`ext:fieldname`, the last occurrence - is chosen. + is chosen. The last example uses the value of :code:`CN` attribute in + the Subject field and the certificate's :code:`serialNumber` field in + hex delimited by slash symbol as the resulting username. When this option is used, the ``--verify-x509-name`` option will match against the chosen ``fieldname`` instead of the Common Name.
src/openvpn/init.c +2 -2
@@ -2912,9 +2912,9 @@ do_init_crypto_tls(struct context *c, const unsigned int flags) to.verify_hash = options->verify_hash; to.verify_hash_algo = options->verify_hash_algo;#ifdef ENABLE_X509ALTUSERNAME - to.x509_username_field = (char *) options->x509_username_field; +memmove (to.x509_username_field, options->x509_username_field,sizeof (to.x509_username_field));#else - to.x509_username_field =X509_USERNAME_FIELD_DEFAULT ; + to.x509_username_field[0 ] =X509_USERNAME_FIELD_DEFAULT ;#endif to.es = c->c2.es; to.net_ctx = &c->net_ctx;
src/openvpn/options.c +26 -23
74 lines not on this page · raw message
src/openvpn/options.h +2 -2
@@ -582,8 +582,8 @@ struct optionsint handshake_window;#ifdef ENABLE_X509ALTUSERNAME -/* Field used to be the username in X509 cert. */ -char *x509_username_field; +/* Field list used to be the username in X509 cert. */ +char *x509_username_field[MAX_PARMS ];#endif /* Old key allowed to live n seconds after new key goes active */
src/openvpn/ssl.h +1 -0
@@ -119,6 +119,7 @@/* Default field in X509 to be username */ #define X509_USERNAME_FIELD_DEFAULT "CN" +#define X509_USERNAME_FIELD_DELIMITER '/'#define KEY_METHOD_2 2
src/openvpn/ssl_common.h +7 -1
@@ -285,7 +285,13 @@ struct tls_optionsconst char *remote_cert_eku;uint8_t *verify_hash;hash_algo_type verify_hash_algo; -char *x509_username_field; + +/* Field list used to be the username in X509 cert. */ +#ifdef ENABLE_X509ALTUSERNAME +char *x509_username_field[MAX_PARMS ]; +#else +char *x509_username_field[2 ]; +#endif /* allow openvpn config info to be * passed over control channel */
src/openvpn/ssl_verify.c +24 -11
@@ -48,7 +48,7 @@#include "push.h" /** Maximum length of common name */ -#define TLS_USERNAME_LEN 64 +#define TLS_USERNAME_LEN 128static void string_mod_remap_name (char *str)
@@ -660,19 +660,32 @@ verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_depstring_replace_leading (subject,'-' ,'_' );/* extract the username (default is CN) */ -if (SUCCESS !=backend_x509_get_username (common_name,sizeof (common_name), - opt->x509_username_field, cert)) +for (size_t i =0 , len =0 ; opt->x509_username_field[i] !=NULL ; i++) { -if (!cert_depth) +char *buf = common_name + len; +int size =sizeof (common_name) - len - !!i; + +if (size <=0 || +SUCCESS !=backend_x509_get_username (buf + !!i, size, + opt->x509_username_field[i], cert)) { -msg (D_TLS_ERRORS ,"VERIFY ERROR: could not extract %s from X509 " -"subject string ('%s') -- note that the username length is " -"limited to %d characters" , - opt->x509_username_field, - subject, -TLS_USERNAME_LEN ); - goto cleanup; +if (!cert_depth) + { +msg (D_TLS_ERRORS ,"VERIFY ERROR: could not extract %s from X509 " +"subject string ('%s') -- note that the username length is " +"limited to %d characters" , + opt->x509_username_field[i], + subject, +TLS_USERNAME_LEN ); + goto cleanup; + } +break ; + } +if (i !=0 ) + { + *buf =X509_USERNAME_FIELD_DELIMITER ; } + len +=strlen (buf); }/* enforce character class restrictions in common name */
src/openvpn/ssl_verify_openssl.c +14 -0
@@ -268,6 +268,20 @@ backend_x509_get_username(char *common_name, int cn_len, return FAILURE; } } + else if (strcmp(LN_serialNumber,x509_username_field) == 0) + { +ASN1_INTEGER *asn1_i =X509_get_serialNumber (peer_cert); +struct gc_arena gc =gc_new (); +char *serial =format_hex_ex (asn1_i->data, asn1_i->length,0 ,1 ,NULL , &gc); + +if (!serial || cn_len <=strlen (serial)+2 ) + { +gc_free (&gc); +return FAILURE ; + } +openvpn_snprintf (common_name, cn_len,"0x%s" , serial); +gc_free (&gc); + } else #endif if (FAILURE == extract_x509_field_ssl(X509_get_subject_name(peer_cert),
Signature · 1 line
2.17.1
Am 28.07.20 um 00:13 schrieb Vladislav Grishenko:
Quoted 78 lines
OpenVPN has the ability to choose different x509 field in case "CN" can't be use used to be unique connected username since commit
935c62be9c0c8a256112df818bfb8470586a23b6.
Unfortunately it's not enough in case client has multiple and valid certificates from PKI for different devices (ex. laptop, mobile, etc) with the same CN/UID.Having --duplicate-cn as a workaround helps only partially - clients can be connected, but it breaks coexistance with --ifconfig-pool-persist, --client-config-dir and opens doors to DoS possibility since same client device (with the same cert) being reconnected doesn't replace previously connected session no more, so can exhaust server ressources (ex. address pool) and can prevent other clients to be connected.
With this patch, multiple x509 fields incl. "serialNumber" can be chosen to be username as --x509-username-files space-separated parameters. Multiple fields will be joined into one username using '/' delimeter for consistency with CN/addr logging and preserving ability for hierarchical ccd. As long as resulting username is unique, --duplicate-cn will not be required. Default value is preserved as "CN" only.
Openssl backend is the only supported at the moment, since so far MbedTLS has no alt user name support at all.
--- doc/man-sections/tls-options.rst | 9 ++++--- src/openvpn/init.c | 4 +-- src/openvpn/options.c | 46 ++++++++++++++++++-------------- src/openvpn/options.h | 4 +-- src/openvpn/ssl.h | 1 + src/openvpn/ssl_common.h | 8 +++++- src/openvpn/ssl_verify.c | 35 ++++++++++++++++-------- src/openvpn/ssl_verify_openssl.c | 15 +++++++++++ 8 files changed, 83 insertions(+), 39 deletions(-)doc/man-sections/tls-options.rst +6
-3@@ -632,13 +632,13 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa options can be defined to track multiple attributes. --x509-username-field args - Field in the X.509 certificate subject to be used as the username + Field list in the X.509 certificate subject to be used as the username (default :code:`CN`). Valid syntax: :: - x509-username-field [ext:]fieldname + x509-username-field [ext:]fieldname [[ext:]fieldname...] Typically, this option is specified with **fieldname** as either of the following:@@ -646,6 +646,7 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa x509-username-field emailAddress x509-username-field ext:subjectAltName + x509-username-field CN serialNumber The first example uses the value of the :code:`emailAddress` attribute in the certificate's Subject field as the username. The second example@@ -653,7 +654,9 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa ``fieldname`` :code:`subjectAltName` be searched for an rfc822Name (email) field to be used as the username. In cases where there are multiple email addresses in :code:`ext:fieldname`, the last occurrence - is chosen. + is chosen. The last example uses the value of :code:`CN` attribute in + the Subject field and the hex representation of certificate's serial + number delimited by slash symbol as the resulting username. When this option is used, the ``--verify-x509-name`` option will match against the chosen ``fieldname`` instead of the Common Name.src/openvpn/init.c +0
-0
src/openvpn/init.c do_init_crypto_tls(struct context *c, const unsigned int flags) · line 2918
Quoted 9 lines
src/openvpn/init.c +2
-2@@ -2912,9 +2912,9 @@ do_init_crypto_tls(struct context *c, const unsigned int flags) to.verify_hash = options->verify_hash; to.verify_hash_algo = options->verify_hash_algo;#ifdef ENABLE_X509ALTUSERNAME - to.x509_username_field = (char *) options->x509_username_field; +memmove (to.x509_username_field, options->x509_username_field,sizeof (to.x509_username_field));#else - to.x509_username_field =X509_USERNAME_FIELD_DEFAULT ; + to.x509_username_field[0 ] =X509_USERNAME_FIELD_DEFAULT ;#endif
Can't we use the same code in both cases? And why memmove instead memcpy?
Quoted 101 lines
src/openvpn/init.c +0
-0to.es = c->c2.es; to.net_ctx = &c->net_ctx;src/openvpn/options.c +26
-20@@ -877,7 +877,7 @@ init_options(struct options *o, const bool init_gc) o->tls_cert_profile =NULL ; o->ecdh_curve =NULL ;#ifdef ENABLE_X509ALTUSERNAME - o->x509_username_field =X509_USERNAME_FIELD_DEFAULT ; + o->x509_username_field[0 ] =X509_USERNAME_FIELD_DEFAULT ;#endif #ifdef ENABLE_PKCS11 o->pkcs11_pin_cache_period =-1 ;@@ -8434,7 +8434,7 @@ add_option(struct options *options,x509_track_add (&options->x509_track, p[1 ], msglevel, &options->gc); }#ifdef ENABLE_X509ALTUSERNAME - else if (streq(p[0], "x509-username-field") && p[1] && !p[2]) + else if (streq(p[0], "x509-username-field") && p[1]) {/* This option used to automatically upcase the fieldname passed as the * option argument, e.g., "ou" became "OU". Now, this "helpfulness" is @@ -8443,32 +8443,38 @@ add_option(struct options *options,* "emailAddress" are left as-is. An option parameter having the "ext:" * prefix for matching X.509v3 extended fields will also remain unchanged. */ -char *s = p[1 ]; +size_t j;VERIFY_PERMISSION (OPT_P_GENERAL ); -if (strncmp ("ext:" , s,4 ) !=0 ) + +for (j =1 ; j <MAX_PARMS && p[j] !=NULL ; ++j) { -size_t i =0 ; -while (s[i] && !isupper (s[i])) - { - i++; - } -if (strlen (s) == i) +char *s = p[j]; + +if (strncmp ("ext:" , s,4 ) !=0 ) { -while ((*s =toupper (*s)) !='\0' ) +size_t i =0 ; +while (s[i] && !isupper (s[i])) + { + i++; + } +if (strlen (s) == i) { - s++; +while ((*s =toupper (*s)) !='\0' ) + { + s++; + } +msg (M_WARN ,"DEPRECATED FEATURE: automatically upcased the " +"--x509-username-field parameter to '%s'; please update your" +"configuration" , p[j]); } -msg (M_WARN ,"DEPRECATED FEATURE: automatically upcased the " -"--x509-username-field parameter to '%s'; please update your" -"configuration" , p[1 ]); } +else if (!x509_username_field_ext_supported (s+4 )) + { +msg (msglevel,"Unsupported x509-username-field extension: %s" , s); + } + options->x509_username_field[j-1 ] = p[j]; } -else if (!x509_username_field_ext_supported (s+4 )) - { -msg (msglevel,"Unsupported x509-username-field extension: %s" , s); - } - options->x509_username_field = p[1]; }#endif /* ENABLE_X509ALTUSERNAME */ #ifdef ENABLE_PKCS11 src/openvpn/options.h +2
-2@@ -582,8 +582,8 @@ struct optionsint handshake_window;#ifdef ENABLE_X509ALTUSERNAME -/* Field used to be the username in X509 cert. */ -char *x509_username_field; +/* Field list used to be the username in X509 cert. */ +char *x509_username_field[MAX_PARMS ];#endif /* Old key allowed to live n seconds after new key goes active */ src/openvpn/ssl.h +0
-0
src/openvpn/ssl.h line 123
Quoted 5 lines
src/openvpn/ssl.h +1
-0@@ -119,6 +119,7 @@/* Default field in X509 to be username */ #define X509_USERNAME_FIELD_DEFAULT "CN" +#define X509_USERNAME_FIELD_DELIMITER '/'
This seems to be used nowhere.
Quoted 26 lines
src/openvpn/ssl.h +0
-0#define KEY_METHOD_2 2src/openvpn/ssl_common.h +7
-1@@ -285,7 +285,13 @@ struct tls_optionsconst char *remote_cert_eku;uint8_t *verify_hash;hash_algo_type verify_hash_algo; -char *x509_username_field; + +/* Field list used to be the username in X509 cert. */ +#ifdef ENABLE_X509ALTUSERNAME +char *x509_username_field[MAX_PARMS ]; +#else +char *x509_username_field[2 ]; +#endif /* allow openvpn config info to be * passed over control channel */ src/openvpn/ssl_verify.c +0
-0
src/openvpn/ssl_verify.c line 52
Quoted 6 lines
src/openvpn/ssl_verify.c +1
-1@@ -48,7 +48,7 @@#include "push.h" /** Maximum length of common name */ -#define TLS_USERNAME_LEN 64 +#define TLS_USERNAME_LEN 128
NAK on this one. The X509 CN name length is max 64 characeters. So you either have stick to that limit in your patch or have to untagle the CN name length vs the field you are using, which might end up in a seperate patch.
Quoted 11 lines
src/openvpn/ssl_verify.c +1
-0static void string_mod_remap_name (char *str)@@ -640,6 +640,7 @@ verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_depchar common_name[TLS_USERNAME_LEN +1 ] = {0 };/* null-terminated */ const struct tls_options *opt;struct gc_arena gc =gc_new (); +size_t i, size; opt = session->opt;ASSERT (opt);
src/openvpn/ssl_verify.c verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_dep · line 666
Quoted 8 lines
src/openvpn/ssl_verify.c +2
-2@@ -660,19 +661,31 @@ verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_depstring_replace_leading (subject,'-' ,'_' );/* extract the username (default is CN) */ -if (SUCCESS !=backend_x509_get_username (common_name,sizeof (common_name), - opt->x509_username_field, cert)) + size =sizeof (common_name); +for (i =0 ; opt->x509_username_field[i] !=NULL && size > !!i; i++) {
C89 style instead C99. The !!i feels weird. It is the same as max(i, 1) but less readable.
Quoted 21 lines
src/openvpn/ssl_verify.c +11
-8-if (!cert_depth) +char *buf = common_name +sizeof (common_name) - size; + +if (SUCCESS !=backend_x509_get_username (buf + !!i, size - !!i, + opt->x509_username_field[i], cert)) { -msg (D_TLS_ERRORS ,"VERIFY ERROR: could not extract %s from X509 " -"subject string ('%s') -- note that the username length is " -"limited to %d characters" , - opt->x509_username_field, - subject, -TLS_USERNAME_LEN ); - goto cleanup; +break ; + } +else if (i !=0 ) + { + *buf =X509_USERNAME_FIELD_DELIMITER ; } + size -=strlen (buf); + }
src/openvpn/ssl_verify.c verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_dep · line 689
Quoted 10 lines
src/openvpn/ssl_verify.c +9
-0+if (cert_depth ==0 && opt->x509_username_field[i] !=NULL ) + { +msg (D_TLS_ERRORS ,"VERIFY ERROR: could not extract %s from X509 " +"subject string ('%s') -- note that the username length is " +"limited to %d characters" , + opt->x509_username_field[i], + subject, +TLS_USERNAME_LEN ); + goto cleanup; }
Not really wrong but we normally do stuff like appending etc to a string with our buf method/ classes. I would prefer we do it the same here instead of doing it completely different (espeically the !! is nowhere else found in the code base).
Quoted 29 lines
src/openvpn/ssl_verify.c +0
-0/* enforce character class restrictions in common name */ src/openvpn/ssl_verify_openssl.c +15
-0@@ -268,6 +268,21 @@ backend_x509_get_username(char *common_name, int cn_len, return FAILURE; } } + else if (strcmp(LN_serialNumber,x509_username_field) == 0) + { +ASN1_INTEGER *asn1_i =X509_get_serialNumber (peer_cert); +struct gc_arena gc =gc_new (); +char *serial; + + serial =format_hex_ex (asn1_i->data, asn1_i->length,0 ,1 ,NULL , &gc); +if (!serial || cn_len <=strlen (serial)+2 ) + { +gc_free (&gc); +return FAILURE ; + } +openvpn_snprintf (common_name, cn_len,"0x%s" , serial); +gc_free (&gc); + } else #endif if (FAILURE == extract_x509_field_ssl(X509_get_subject_name(peer_cert),
Hi,
Quoted 9 lines
src/openvpn/init.c do_init_crypto_tls(struct context *c, const unsigned int flags) · line 2918
Quoted 7 lines
#ifdef
ENABLE_X509ALTUSERNAMEsrc/openvpn/init.c +2
-2- to.x509_username_field = (char *) options->x509_username_field; +memmove (to.x509_username_field, options->x509_username_field,sizeof (to.x509_username_field));#else - to.x509_username_field =X509_USERNAME_FIELD_DEFAULT ; + to.x509_username_field[0 ] =X509_USERNAME_FIELD_DEFAULT ;#endif Can't we use the same code in both cases? And why memmove instead memcpy?
Can't, options struct contains x509_username_field only with ENABLE_X509ALTUSERNAME.
Memmove is used to be consistent with remote_cert_ku copying several lines above, although I think memcpy is enough for here and above - tls and options mem regions can't be crossed.
Quoted 5 lines
Quoted 3 lines
/* Default field in X509 to be username */
#defineX509_USERNAME_FIELD_DEFAULT"CN"
+#defineX509_USERNAME_FIELD_DELIMITER'/'This seems to be used nowhere.
It's used down below as a field delimiter for possible easy change:
src/openvpn/ssl_verify.c verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_dep · line 677
Quoted 3 lines
src/openvpn/ssl_verify.c +2
-0+ { + *buf =X509_USERNAME_FIELD_DELIMITER ; }
I'll drop it with just "/%s" in v3.
Quoted 11 lines
Quoted 1 line
+#define
TLS_USERNAME_LEN128NAK on this one. The X509 CN name length is max 64 characeters. So you either have stick to that limit in your patch or have to untagle the CN name length vs the field you are using, which might end up in a seperate patch.
...
Not really wrong but we normally do stuff like appending etc to a string with our buf method/ classes. I would prefer we do it the same here instead of doing it completely different (espeically the !! is nowhere else found in the code base).
Good point, 64 limit can be kept for each backend_x509_get_username() call, subsequent appending will be done via buffer methods - this way buffer size will be untied from TLS_USERNAME_LEN.
Quoted 2 lines
C89 style instead C99. The !!i feels weird. It is the same as
max(i, 1)but less readable.
Yes, sure.
--
Best Regards, Vladislav Grishenko
-----Original Message-----
From: Arne Schwabe <[email protected]...>
Sent: Friday, August 14, 2020 6:56 PM
To: Vladislav Grishenko <[email protected]...>; [email protected]
Subject: Re: [Openvpn-devel] [PATCH 1/2] Support multiple x509 field list to be username
Am 28.07.20 um 00:13 schrieb Vladislav Grishenko:
Quoted 78 lines
OpenVPN has the ability to choose different x509 field in case "CN" can't be use used to be unique connected username since commit
935c62be9c0c8a256112df818bfb8470586a23b6.
Unfortunately it's not enough in case client has multiple and valid certificates from PKI for different devices (ex. laptop, mobile, etc) with the same CN/UID.Having --duplicate-cn as a workaround helps only partially - clients can be connected, but it breaks coexistance with --ifconfig-pool-persist, --client-config-dir and opens doors to DoS possibility since same client device (with the same cert) being reconnected doesn't replace previously connected session no more, so can exhaust server ressources (ex. address pool) and can prevent other clients to be connected.
With this patch, multiple x509 fields incl. "serialNumber" can be chosen to be username as --x509-username-files space-separated parameters. Multiple fields will be joined into one username using '/' delimeter for consistency with CN/addr logging and preserving ability for hierarchical ccd. As long as resulting username is unique, --duplicate-cn will not be required. Default value is preserved as "CN" only.
Openssl backend is the only supported at the moment, since so far MbedTLS has no alt user name support at all.
--- doc/man-sections/tls-options.rst | 9 ++++--- src/openvpn/init.c | 4 +-- src/openvpn/options.c | 46 ++++++++++++++++++-------------- src/openvpn/options.h | 4 +-- src/openvpn/ssl.h | 1 + src/openvpn/ssl_common.h | 8 +++++- src/openvpn/ssl_verify.c | 35 ++++++++++++++++-------- src/openvpn/ssl_verify_openssl.c | 15 +++++++++++ 8 files changed, 83 insertions(+), 39 deletions(-)doc/man-sections/tls-options.rst +6
-3@@ -632,13 +632,13 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa options can be defined to track multiple attributes. --x509-username-field args - Field in the X.509 certificate subject to be used as the username + Field list in the X.509 certificate subject to be used as the username (default :code:`CN`). Valid syntax: :: - x509-username-field [ext:]fieldname + x509-username-field [ext:]fieldname [[ext:]fieldname...] Typically, this option is specified with **fieldname** as either of the following:@@ -646,6 +646,7 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa x509-username-field emailAddress x509-username-field ext:subjectAltName + x509-username-field CN serialNumber The first example uses the value of the :code:`emailAddress` attribute in the certificate's Subject field as the username. The second example@@ -653,7 +654,9 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa ``fieldname`` :code:`subjectAltName` be searched for an rfc822Name (email) field to be used as the username. In cases where there are multiple email addresses in :code:`ext:fieldname`, the last occurrence - is chosen. + is chosen. The last example uses the value of :code:`CN` attribute in + the Subject field and the hex representation of certificate's serial + number delimited by slash symbol as the resulting username. When this option is used, the ``--verify-x509-name`` option will match against the chosen ``fieldname`` instead of the Common Name.src/openvpn/init.c +0
-0
src/openvpn/init.c do_init_crypto_tls(struct context *c, const unsigned int flags) · line 2918
Quoted 9 lines
src/openvpn/init.c +2
-2@@ -2912,9 +2912,9 @@ do_init_crypto_tls(struct context *c, const unsigned int flags) to.verify_hash = options->verify_hash; to.verify_hash_algo = options->verify_hash_algo;#ifdef ENABLE_X509ALTUSERNAME - to.x509_username_field = (char *) options->x509_username_field; +memmove (to.x509_username_field, options->x509_username_field,sizeof (to.x509_username_field));#else - to.x509_username_field =X509_USERNAME_FIELD_DEFAULT ; + to.x509_username_field[0 ] =X509_USERNAME_FIELD_DEFAULT ;#endif
Can't we use the same code in both cases? And why memmove instead memcpy?
Quoted 101 lines
src/openvpn/init.c +0
-0to.es = c->c2.es; to.net_ctx = &c->net_ctx;src/openvpn/options.c +26
-20@@ -877,7 +877,7 @@ init_options(struct options *o, const bool init_gc) o->tls_cert_profile =NULL ; o->ecdh_curve =NULL ;#ifdef ENABLE_X509ALTUSERNAME - o->x509_username_field =X509_USERNAME_FIELD_DEFAULT ; + o->x509_username_field[0 ] =X509_USERNAME_FIELD_DEFAULT ;#endif #ifdef ENABLE_PKCS11 o->pkcs11_pin_cache_period =-1 ;@@ -8434,7 +8434,7 @@ add_option(struct options *options,x509_track_add (&options->x509_track, p[1 ], msglevel, &options->gc); }#ifdef ENABLE_X509ALTUSERNAME - else if (streq(p[0], "x509-username-field") && p[1] && !p[2]) + else if (streq(p[0], "x509-username-field") && p[1]) {/* This option used to automatically upcase the fieldname passed as the * option argument, e.g., "ou" became "OU". Now, this "helpfulness" is @@ -8443,32 +8443,38 @@ add_option(struct options *options,* "emailAddress" are left as-is. An option parameter having the "ext:" * prefix for matching X.509v3 extended fields will also remain unchanged. */ -char *s = p[1 ]; +size_t j;VERIFY_PERMISSION (OPT_P_GENERAL ); -if (strncmp ("ext:" , s,4 ) !=0 ) + +for (j =1 ; j <MAX_PARMS && p[j] !=NULL ; ++j) { -size_t i =0 ; -while (s[i] && !isupper (s[i])) - { - i++; - } -if (strlen (s) == i) +char *s = p[j]; + +if (strncmp ("ext:" , s,4 ) !=0 ) { -while ((*s =toupper (*s)) !='\0' ) +size_t i =0 ; +while (s[i] && !isupper (s[i])) + { + i++; + } +if (strlen (s) == i) { - s++; +while ((*s =toupper (*s)) !='\0' ) + { + s++; + } +msg (M_WARN ,"DEPRECATED FEATURE: automatically upcased the " +"--x509-username-field parameter to '%s'; please update your" +"configuration" , p[j]); } -msg (M_WARN ,"DEPRECATED FEATURE: automatically upcased the " -"--x509-username-field parameter to '%s'; please update your" -"configuration" , p[1 ]); } +else if (!x509_username_field_ext_supported (s+4 )) + { +msg (msglevel,"Unsupported x509-username-field extension: %s" , s); + } + options->x509_username_field[j-1 ] = p[j]; } -else if (!x509_username_field_ext_supported (s+4 )) - { -msg (msglevel,"Unsupported x509-username-field extension: %s" , s); - } - options->x509_username_field = p[1]; }#endif /* ENABLE_X509ALTUSERNAME */ #ifdef ENABLE_PKCS11 src/openvpn/options.h +2
-2@@ -582,8 +582,8 @@ struct optionsint handshake_window;#ifdef ENABLE_X509ALTUSERNAME -/* Field used to be the username in X509 cert. */ -char *x509_username_field; +/* Field list used to be the username in X509 cert. */ +char *x509_username_field[MAX_PARMS ];#endif /* Old key allowed to live n seconds after new key goes active */ src/openvpn/ssl.h +0
-0
src/openvpn/ssl.h line 123
Quoted 5 lines
src/openvpn/ssl.h +1
-0@@ -119,6 +119,7 @@/* Default field in X509 to be username */ #define X509_USERNAME_FIELD_DEFAULT "CN" +#define X509_USERNAME_FIELD_DELIMITER '/'
This seems to be used nowhere.
Quoted 26 lines
src/openvpn/ssl.h +0
-0#define KEY_METHOD_2 2src/openvpn/ssl_common.h +7
-1@@ -285,7 +285,13 @@ struct tls_optionsconst char *remote_cert_eku;uint8_t *verify_hash;hash_algo_type verify_hash_algo; -char *x509_username_field; + +/* Field list used to be the username in X509 cert. */ +#ifdef ENABLE_X509ALTUSERNAME +char *x509_username_field[MAX_PARMS ]; +#else +char *x509_username_field[2 ]; +#endif /* allow openvpn config info to be * passed over control channel */ src/openvpn/ssl_verify.c +0
-0
src/openvpn/ssl_verify.c line 52
Quoted 6 lines
src/openvpn/ssl_verify.c +1
-1@@ -48,7 +48,7 @@#include "push.h" /** Maximum length of common name */ -#define TLS_USERNAME_LEN 64 +#define TLS_USERNAME_LEN 128
NAK on this one. The X509 CN name length is max 64 characeters. So you either have stick to that limit in your patch or have to untagle the CN name length vs the field you are using, which might end up in a seperate patch.
Quoted 11 lines
src/openvpn/ssl_verify.c +1
-0static void string_mod_remap_name (char *str)@@ -640,6 +640,7 @@ verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_depchar common_name[TLS_USERNAME_LEN +1 ] = {0 };/* null-terminated */ const struct tls_options *opt;struct gc_arena gc =gc_new (); +size_t i, size; opt = session->opt;ASSERT (opt);
src/openvpn/ssl_verify.c verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_dep · line 666
Quoted 8 lines
src/openvpn/ssl_verify.c +2
-2@@ -660,19 +661,31 @@ verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_depstring_replace_leading (subject,'-' ,'_' );/* extract the username (default is CN) */ -if (SUCCESS !=backend_x509_get_username (common_name,sizeof (common_name), - opt->x509_username_field, cert)) + size =sizeof (common_name); +for (i =0 ; opt->x509_username_field[i] !=NULL && size > !!i; i++) {
C89 style instead C99. The !!i feels weird. It is the same as max(i, 1) but less readable.
Quoted 21 lines
src/openvpn/ssl_verify.c +11
-8-if (!cert_depth) +char *buf = common_name +sizeof (common_name) - size; + +if (SUCCESS !=backend_x509_get_username (buf + !!i, size - !!i, + opt->x509_username_field[i], cert)) { -msg (D_TLS_ERRORS ,"VERIFY ERROR: could not extract %s from X509 " -"subject string ('%s') -- note that the username length is " -"limited to %d characters" , - opt->x509_username_field, - subject, -TLS_USERNAME_LEN ); - goto cleanup; +break ; + } +else if (i !=0 ) + { + *buf =X509_USERNAME_FIELD_DELIMITER ; } + size -=strlen (buf); + }
src/openvpn/ssl_verify.c verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_dep · line 689
Quoted 10 lines
src/openvpn/ssl_verify.c +9
-0+if (cert_depth ==0 && opt->x509_username_field[i] !=NULL ) + { +msg (D_TLS_ERRORS ,"VERIFY ERROR: could not extract %s from X509 " +"subject string ('%s') -- note that the username length is " +"limited to %d characters" , + opt->x509_username_field[i], + subject, +TLS_USERNAME_LEN ); + goto cleanup; }
Not really wrong but we normally do stuff like appending etc to a string with our buf method/ classes. I would prefer we do it the same here instead of doing it completely different (espeically the !! is nowhere else found in the code base).
Quoted 29 lines
src/openvpn/ssl_verify.c +0
-0/* enforce character class restrictions in common name */ src/openvpn/ssl_verify_openssl.c +15
-0@@ -268,6 +268,21 @@ backend_x509_get_username(char *common_name, int cn_len, return FAILURE; } } + else if (strcmp(LN_serialNumber,x509_username_field) == 0) + { +ASN1_INTEGER *asn1_i =X509_get_serialNumber (peer_cert); +struct gc_arena gc =gc_new (); +char *serial; + + serial =format_hex_ex (asn1_i->data, asn1_i->length,0 ,1 ,NULL , &gc); +if (!serial || cn_len <=strlen (serial)+2 ) + { +gc_free (&gc); +return FAILURE ; + } +openvpn_snprintf (common_name, cn_len,"0x%s" , serial); +gc_free (&gc); + } else #endif if (FAILURE == extract_x509_field_ssl(X509_get_subject_name(peer_cert),