Skip to contentSkip to conversation

Character classes in the tls-verify script

Openvpn-devel Victor Wagner 4 participants 11 messages Oct 23, 2009 – Nov 13, 2009

lore
Share & tools
lore
#0 lore raw

I've found out that string_mod family of function do very bad job with certificates with cyrillic characters in the subject.

As of OpenVPN 2.1_rc19 class CC_PRINT is determined by function isprint from ctype.h, which does wrong job if there was no setlocale call (and there is no setlocale call in the OpenVPN).

Moreover, all printable data except ones produced by X509_NAME_oneline function is produced by ASN1_STRING_to_UTF8 functions and no one guaranties that current locale of openvpn process has UTF-8 codeset.

I've fixed this problem by defining new character class CC_MSB, which all bytes from UTF-8 multibyte sequence belong to, allow this character class everywhere where CC_PRINT is allowed and include it into macro definition of X509_NAME_CHAR_CLASS and COMMON_NAME_CHAR_CLASS

I think that it is enough for security purposes, because no shell would ever treat unicode codepoints above 128 as word or command separators.

And it gives me valid utf-8 string in the script variables and logs.

But may be there exist better solution.

#1 lore raw
Quoted 16 lines · Victor Wagner

I've found out that string_mod family of function do very bad job with certificates with cyrillic characters in the subject.

As of OpenVPN 2.1_rc19 class CC_PRINT is determined by function isprint from ctype.h, which does wrong job if there was no setlocale call (and there is no setlocale call in the OpenVPN).

Moreover, all printable data except ones produced by X509_NAME_oneline function is produced by ASN1_STRING_to_UTF8 functions and no one guaranties that current locale of openvpn process has UTF-8 codeset.

I've fixed this problem by defining new character class CC_MSB, which all bytes from UTF-8 multibyte sequence belong to, allow this character class everywhere where CC_PRINT is allowed and include it into macro definition of X509_NAME_CHAR_CLASS and COMMON_NAME_CHAR_CLASS

Quoted 6 lines

I think that it is enough for security purposes, because no shell would ever treat unicode codepoints above 128 as word or command separators.

And it gives me valid utf-8 string in the script variables and logs.

But may be there exist better solution.

Can you submit a patch (as an email attachment) with this fix?

My only concern would be if there are ways an attacker could use chars that are >= 128 to attack a shell or server-side auth script.

I think it would be better to have this turned off by default, but have an option to enable it, like the no-name-remapping option in 2.1.

James

#2 lore raw
Quoted 9 lines · James Yonan
Quoted 7 lines · Victor Wagner

I've found out that string_mod family of function do very bad job with certificates with cyrillic characters in the subject.

As of OpenVPN 2.1_rc19 class CC_PRINT is determined by function isprint from ctype.h, which does wrong job if there was no setlocale call (and there is no setlocale call in the OpenVPN).

Quoted 7 lines

Can you submit a patch (as an email attachment) with this fix?

My only concern would be if there are ways an attacker could use chars that are >= 128 to attack a shell or server-side auth script.

I think it would be better to have this turned off by default, but have an option to enable it, like the no-name-remapping option in 2.1.

If this option wouldn't conflict with script-security 3 system (as "no-name-remapping" does, it would be nice.

Tomorrow I'd cut this patch out of my patch to openvpn 2.1rc19 It would require some time, because I now use OpenVPN with following addititonal functionality:
1. Support for non-HMAC packet authentication algorithm (requires OpenSSL 1.0, where support for non-HMAC MAC algorithms appeared)
2. Support for loading private keys from crypto hardware module via OpenSSL engine API, not PKCS11 API, using other engine than set by engine configuration parameter.
3. Export of certificate extension subjectAltName to scripts.
4. Support for CC_MSB character class.

#3 lore raw
In reply to James Yonan
Quoted 2 lines · James Yonan

Can you submit a patch (as an email attachment) with this fix?

Attached

This patch also contains X509_NAME_oneline replacement, which handles MSB characters.

I've not checked if this patch applies cleanly to unmodified source. I've just diffed original 2.1_rc19 source, imported in our subversion repository with current working copy and removed all irrelevant chunks.

ssl.c +19 -6
Index: ssl.c
===================================================================
@@ -435,8 +435,8 @@
       name_expand = (char *) malloc (name_expand_size);
       check_malloc_return (name_expand);
       openvpn_snprintf (name_expand, name_expand_size, "X509_%d_%s", error_depth, objbuf);
-      string_mod (name_expand, CC_PRINT, CC_CRLF, '_');
-      string_mod ((char*)buf, CC_PRINT, CC_CRLF, '_');
+      string_mod (name_expand, CC_PRINT|CC_MSB, CC_CRLF, '_');
+      string_mod ((char*)buf, CC_PRINT|CC_MSB, CC_CRLF, '_');
       setenv_str (es, name_expand, (char*)buf);
       free (name_expand);
       OPENSSL_free (buf);
@@ -584,11 +584,24 @@
 string_mod_sslname (char *str, const unsigned int restrictive_flags, const unsigned int ssl_flags)
 {
   if (ssl_flags & SSLF_NO_NAME_REMAPPING)
-    string_mod (str, CC_PRINT, CC_CRLF, '_');
+    string_mod (str, CC_PRINT|CC_MSB, CC_CRLF, '_');
   else
     string_mod (str, restrictive_flags, 0, '_');
 }
 
+static char *my_X509_NAME_oneline(X509_NAME *name) {
+	BIO *b = BIO_new(BIO_s_mem());
+	long datalen;
+	char *p,*q;
+	X509_NAME_print_ex(b, name, 0, (XN_FLAG_ONELINE | ASN1_STRFLGS_UTF8_CONVERT) & ~ASN1_STRFLGS_ESC_MSB);
+	datalen = BIO_get_mem_data(b,&p);
+	q=OPENSSL_malloc(datalen+1);
+	strncpy(q,p,datalen);
+	q[datalen]=0;
+	BIO_free(b);
+	return q;
+}
+
 /*
  * Our verify callback function -- check
  * that an incoming peer certificate is good.
@@ -617,7 +630,7 @@
   session->verified = false;
 
   /* get the X509 name */
-  subject = X509_NAME_oneline (X509_get_subject_name (ctx->current_cert), NULL, 0);
+  subject = my_X509_NAME_oneline (X509_get_subject_name (ctx->current_cert));
   if (!subject)
     {
       msg (D_TLS_ERRORS, "VERIFY ERROR: depth=%d, could not extract X509 subject string from certificate", ctx->error_depth);
@@ -3356,11 +3519,11 @@
       /* preserve raw username before string_mod remapping, for plugins */
       ALLOC_ARRAY_CLEAR_GC (raw_username, char, USER_PASS_LEN, &gc);
       strcpy (raw_username, up->username);
-      string_mod (raw_username, CC_PRINT, CC_CRLF, '_');
+      string_mod (raw_username, CC_PRINT|CC_MSB, CC_CRLF, '_');
 
       /* enforce character class restrictions in username/password */
       string_mod_sslname (up->username, COMMON_NAME_CHAR_CLASS, session->opt->ssl_flags);
-      string_mod (up->password, CC_PRINT, CC_CRLF, '_');
+      string_mod (up->password, CC_PRINT|CC_MSB, CC_CRLF, '_');
 
       /* call plugin(s) and/or script */
 #ifdef MANAGEMENT_DEF_AUTH
ssl.h +2 -2
Index: ssl.h
===================================================================
@@ -284,8 +284,8 @@
 #define TLS_CN_LEN 64
 
 /* Legal characters in an X509 or common name */
-#define X509_NAME_CHAR_CLASS   (CC_ALNUM|CC_UNDERBAR|CC_DASH|CC_DOT|CC_AT|CC_COLON|CC_SLASH|CC_EQUAL)
-#define COMMON_NAME_CHAR_CLASS (CC_ALNUM|CC_UNDERBAR|CC_DASH|CC_DOT|CC_AT|CC_SLASH)
+#define X509_NAME_CHAR_CLASS   (CC_ALNUM|CC_UNDERBAR|CC_DASH|CC_DOT|CC_AT|CC_COLON|CC_SLASH|CC_EQUAL|CC_MSB)
+#define COMMON_NAME_CHAR_CLASS (CC_ALNUM|CC_UNDERBAR|CC_DASH|CC_DOT|CC_AT|CC_SLASH|CC_MSB)
 
 /* Maximum length of OCC options string passed as part of auth handshake */
 #define TLS_OPTIONS_LEN 512
buffer.c +2 -1
Index: buffer.c
===================================================================
@@ -692,7 +692,8 @@
 
   if ((flags & CC_NULL) && c == '\0')
     return true;
-
+  if ((flags & CC_MSB) && (c & 0x80)) 
+  	return true;
   if ((flags & CC_ALNUM) && isalnum (c))
     return true;
   if ((flags & CC_ALPHA) && isalpha (c))
buffer.h +2 -1
Index: buffer.h
===================================================================
@@ -684,9 +684,10 @@
 #define CC_REVERSE_QUOTE      (1<<23)
 #define CC_AT                 (1<<24)
 #define CC_EQUAL              (1<<25)
+#define CC_MSB				  (1<<26)
 
 /* macro classes */
-#define CC_NAME               (CC_ALNUM|CC_UNDERBAR)
+#define CC_NAME               (CC_ALNUM|CC_UNDERBAR|CC_MSB)
 #define CC_CRLF               (CC_CR|CC_NEWLINE)
 
 bool char_class (const unsigned char c, const unsigned int flags);
#4 lore raw
Quoted 11 lines · Victor Wagner
Quoted 1 line · James Yonan

Can you submit a patch (as an email attachment) with this fix?

Attached

This patch also contains X509_NAME_oneline replacement, which handles MSB characters.

I've not checked if this patch applies cleanly to unmodified source. I've just diffed original 2.1_rc19 source, imported in our subversion repository with current working copy and removed all irrelevant chunks.

Thanks for putting this together.

I'm a bit concerned about changing any of the remapping behavior unless no-name-remapping is specified. I see that in some areas, you test the no-name-remapping flag before you modify existing behavior, but in other areas such as my_X509_NAME_oneline, X509_NAME_CHAR_CLASS, and COMMON_NAME_CHAR_CLASS you don't.

Would it be possible for you to rework the patch so that it's a no-op unless no-name-remapping is specified?

James

Quoted 30 lines · Victor Wagner
Quoted 1 line · James Yonan

Can you submit a patch (as an email attachment) with this fix?

Attached

This patch also contains X509_NAME_oneline replacement, which handles MSB characters.

I've not checked if this patch applies cleanly to unmodified source. I've just diffed original 2.1_rc19 source, imported in our subversion repository with current working copy and removed all irrelevant chunks.

------------------------------------------------------------------------

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference

------------------------------------------------------------------------

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#5 lore raw
Quoted 13 lines · James Yonan
Quoted 11 lines · Victor Wagner
Quoted 1 line · James Yonan

Can you submit a patch (as an email attachment) with this fix?

Attached

This patch also contains X509_NAME_oneline replacement, which handles MSB characters.

I've not checked if this patch applies cleanly to unmodified source. I've just diffed original 2.1_rc19 source, imported in our subversion repository with current working copy and removed all irrelevant chunks.

Quoted 7 lines

Thanks for putting this together.

I'm a bit concerned about changing any of the remapping behavior unless no-name-remapping is specified. I see that in some areas, you test the no-name-remapping flag before you modify existing behavior, but in other areas such as my_X509_NAME_oneline, X509_NAME_CHAR_CLASS, and COMMON_NAME_CHAR_CLASS you don't.

Problem is that existing behavoir seems to be designed without non-ascii alphabets in mind.

I think that "preserving existing behavoir" is "exporting alpabetic characters as they are". Since it is quite hard to distinguish between "alphabetic" and "non-alphabetic" outside of ASCII range, and no shell use non-ascii as separators (which is primary reason of remapping), I've considered safe way to treat all 8-bit characters as alphabetic.

no-name-remapping has side effects, i.e. disables system method of script execution.

So, all non-latin-writting users would face choice - either use their native language and loose some functionality of openvpn or have full fuctionality and loose native languge.

Really, I think than name remapping shouldn't be applied to environment variables at all. May be for command line arguments should be protected this way. But people who use environment variables typically are clever enough to handle shell special characters.

Moreover, if script is written in some better language than shell (i.e. perl, tcl on even is a C program, which should be probably considered worse language, as it is prone of buffer overflows), or for dlopened plugin same remapping rules are applied.

#6 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(let's try to get this into the mailing list as well, sorry about that)

Quoted 26 lines · Victor Wagner
Quoted 13 lines · James Yonan
Quoted 11 lines · Victor Wagner
Quoted 1 line · James Yonan

Can you submit a patch (as an email attachment) with this fix?

Attached

This patch also contains X509_NAME_oneline replacement, which handles MSB characters.

I've not checked if this patch applies cleanly to unmodified source. I've just diffed original 2.1_rc19 source, imported in our subversion repository with current working copy and removed all irrelevant chunks.

Quoted 7 lines

Thanks for putting this together.

I'm a bit concerned about changing any of the remapping behavior unless no-name-remapping is specified. I see that in some areas, you test the no-name-remapping flag before you modify existing behavior, but in other areas such as my_X509_NAME_oneline, X509_NAME_CHAR_CLASS, and COMMON_NAME_CHAR_CLASS you don't.

Problem is that existing behavoir seems to be designed without non-ascii alphabets in mind.

Quoted 8 lines

I think that "preserving existing behavoir" is "exporting alpabetic characters as they are". Since it is quite hard to distinguish between "alphabetic" and "non-alphabetic" outside of ASCII range, and no shell use non-ascii as separators (which is primary reason of remapping), I've considered safe way to treat all 8-bit characters as alphabetic.

no-name-remapping has side effects, i.e. disables system method of script execution.

I'd have to disagree here. OpenVPN should not change the default behaviour at all, as that can break a lot of already implemented installations if the default remapping goes away. And forcing values to stay inside the 7bit ASCII region is most likely the expected behaviour as well.

Quoted 8 lines

So, all non-latin-writting users would face choice - either use their native language and loose some functionality of openvpn or have full fuctionality and loose native languge.

Really, I think than name remapping shouldn't be applied to environment variables at all. May be for command line arguments should be protected this way. But people who use environment variables typically are clever enough to handle shell special characters.

I am willing to agree with you to some extent here, but OpenVPN cannot change the current default behaviour of remapping. This would have to be thought of when this feature was initially implemented in OpenVPN, now it is too late to change the defaults.

To get the behaviour you would like to implement, I am in favour of adding a configuration parameter which enables that when _explicitly_ requested. That's the best way how to avoid breaking already existing installations.

Quoted 4 lines

Moreover, if script is written in some better language than shell (i.e. perl, tcl on even is a C program, which should be probably considered worse language, as it is prone of buffer overflows), or for dlopened plugin same remapping rules are applied.

Trust me, it's not that easy with non-ascii characters. It depends on the locale setup, and in some cases which character set the file is saved as (I've spent hours debugging a Perl script which looked 100% correct, but it was saved as a file with ISO-8859-1 and not UTF-8, so Perl then processed all data as ISO-8859-1. Even more fun is this when the file is saved as plain ASCII). And Python is also a nightmare in regards to character sets outside the standard ASCII range (7bit).

And if you don't know C well, of course you will get buffer overflows as well. But if you expect multi-byte input, you should also be able to handle that wihtout overflow issues, already from the design and implementation phase. Being sloppy with the design and implementation gives flaws in most programming and script languages. And this is why I favour forcing data to stay inside the 7bit ASCII region. If you know what you are doing, you'll know which config option to enable in OpenVPN as well.

Unfortunately, not many thinks about characters outside the standard 7bit ASCII. I've even experienced developers who got non-ASCII characters in their names, and they forgot about 8bit and multi-byte characters when implementing solutions processing names.

So having to enable characters outside the 7bit ASCII region explicitly is most likely a better approach, IMHO.

kind regards,

David Sommerseth
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/

iEYEARECAAYFAkr8UwkACgkQDC186MBRfrpb+wCdE+umDv+3FpLO1LYlocBA+X57 7rsAn3o1vPMOHLSgfEfInPFot3aXi4T8
=Zboj
-----END PGP SIGNATURE-----

#7 lore raw
Quoted 8 lines · David Sommerseth
Quoted 2 lines

no-name-remapping has side effects, i.e. disables system method of script execution.

I'd have to disagree here. OpenVPN should not change the default behaviour at all, as that can break a lot of already implemented installations if the default remapping goes away. And forcing values to stay inside the 7bit ASCII region is most likely the expected behaviour as well.

I doubt so.

If you don't believe me, ask someone whose native language is French or German and who has characters with umlauts in his name - does he like this character to be replaced with underscore.

Even for Western Europe restricting character set of certificates to 7-bit is the source of problem, not to mention rest of world, where any name would be replaced by string of underscores.

If someone want to limit characters allowed in the certificates, it should be done on the CA level, not when parsing certificates.

On this level only reason to map characters is to prevent common mistakes in the shell script. Characters outside of ASCII range never can be misinterpreted by shells, so they should be allowed.

Quoted 9 lines
Quoted 4 lines

Really, I think than name remapping shouldn't be applied to environment variables at all. May be for command line arguments should be protected this way. But people who use environment variables typically are clever enough to handle shell special characters.

I am willing to agree with you to some extent here, but OpenVPN cannot change the current default behaviour of remapping. This would have to be thought of when this feature was initially implemented in OpenVPN, now it is too late to change the defaults.

It is possible to add ADDITIONAL configuration directive such as --allow-unicode-in-names, which doesn't have such side-effect as no-name-remapping does now.

But I think that this should be enabled by default. If someone cannot handle normal letters, he can disable them.

Quoted 2 lines

Unfortunately, not many thinks about characters outside the standard 7bit ASCII. I've even experienced developers who got non-ASCII

Just all Russia, all Japan and all China.

We just got too tired of persuading english speaking developers, that world is somewhat bigger than Northern America and
often prefer to fork and maintain our own localized version of software.

#8 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 11 lines · Victor Wagner
Quoted 8 lines · David Sommerseth
Quoted 2 lines

no-name-remapping has side effects, i.e. disables system method of script execution.

I'd have to disagree here. OpenVPN should not change the default behaviour at all, as that can break a lot of already implemented installations if the default remapping goes away. And forcing values to stay inside the 7bit ASCII region is most likely the expected behaviour as well.

Quoted 5 lines

I doubt so.

If you don't believe me, ask someone whose native language is French or German and who has characters with umlauts in his name - does he like this character to be replaced with underscore.

You are almost talking to one of those right now, I've basically grown up using CP850, CP865, ISO-8859-1, ISO-8859-2 and UTF-8. I've even worked in companies since late 90s where charset conversion between all of the above (and even EBCDIC) was crucial for day-to-day business.

Quoted 10 lines

Even for Western Europe restricting character set of certificates to 7-bit is the source of problem, not to mention rest of world, where any name would be replaced by string of underscores.

If someone want to limit characters allowed in the certificates, it should be done on the CA level, not when parsing certificates.

On this level only reason to map characters is to prevent common mistakes in the shell script. Characters outside of ASCII range never can be misinterpreted by shells, so they should be allowed.

But UTF-8 with multibyte characters might be, as they use more than one byte per character. It is easy to say that this won't be a security issue with shells, but have you validated all shells on all platforms
OpenVPN supports to check for this? Not all shells or programs do
handle well characters above the 7th bit (char >= 128), unfortunately! I've experienced this on some embedded systems which is using busybox.

Quoted 10 lines
Quoted 9 lines
Quoted 4 lines

Really, I think than name remapping shouldn't be applied to environment variables at all. May be for command line arguments should be protected this way. But people who use environment variables typically are clever enough to handle shell special characters.

I am willing to agree with you to some extent here, but OpenVPN cannot change the current default behaviour of remapping. This would have to be thought of when this feature was initially implemented in OpenVPN, now it is too late to change the defaults.

Quoted 6 lines

It is possible to add ADDITIONAL configuration directive such as --allow-unicode-in-names, which doesn't have such side-effect as no-name-remapping does now.

But I think that this should be enabled by default. If someone cannot handle normal letters, he can disable them.

I do not agree to this. Because, still too few considers the implications of more than 7bit ASCII. You are more likely to get in trouble when the application is initially based on this scenario and then changing to a broader scope. Characters above the 7th bit are not necessarily "normal" letters on all systems.

Quoted 8 lines
Quoted 2 lines

Unfortunately, not many thinks about characters outside the standard 7bit ASCII. I've even experienced developers who got non-ASCII

Just all Russia, all Japan and all China.

We just got too tired of persuading english speaking developers, that world is somewhat bigger than Northern America and
often prefer to fork and maintain our own localized version of software.

I agree, that this is a struggle. But as long as the OpenVPN has been on the marked for almost a decade, changing the defaults in such basics can really break a lot of installations. And doing that will harm the reputation OpenVPN already have in regards to stability.

I do agree with you that introducing support for a broader set of characters (8bit/UTF-8) is important to implement, and OpenVPN does indeed need that! Thus, your patch is important here! But I disagree that this must be done as default.

It should preferably, IMHO, be a runtime parameter and people who then need it can enable it, and if they get troubles *they know why* because *they* did that change. And then they will be more able to fix that according to their own time schedule and feature need, not when an OpenVPN release forces every one to adopt.

When a broad part of the users have tested this over time, used it in production environment and bugs connected to this is fixed ... then we can consider to change the default behaviour, which normally would be done in connection to a new major release. But doing that change now, that is simply too early. It's even not a topic which has been discussed much since I joined the mailing lists, not even on the IRC channel.

Summary: I agree that we need this support, but don't change the default behaviour now.

kind regards,

David Sommerseth
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/

iEYEARECAAYFAkr9WZwACgkQDC186MBRfrrhggCghUIv9fIC23J026kPavAH6mOR hCgAn2CrLG0Y5/sOiP+sFgOK4yHF1y0F
=GP8d
-----END PGP SIGNATURE-----

#9 lore raw
Quoted 5 lines · David Sommerseth

When a broad part of the users have tested this over time, used it in production environment and bugs connected to this is fixed ... then we
can consider to change the default behaviour, which normally would be done in connection to a new major release.

I agree that changes that break backwards compatibility should be what triggers a change in the major release number. But, and I may be wrong here, I don't believe that OpenVPN has adopted the practice -- in which case it makes little sense to have a change in a major release number be the deciding factor.

Your argument that the patch is not well enough tested is enough to make me agree that the default should not be changed now.

Karl <[email protected]...>
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein

#10 lore raw
In reply to Victor Wagner
Quoted 7 lines · Victor Wagner

It is possible to add ADDITIONAL configuration directive such as --allow-unicode-in-names, which doesn't have such side-effect as no-name-remapping
does now.

But I think that this should be enabled by default. If someone cannot handle normal letters, he can disable them.

This makes sense.

But...

I am not very familiar with localization but there surely must be commonly accepted idioms with respect to defaults such as this and the locale settings. Without knowing anything about it my suggestion would be to enable multi-byte characters by default _unless_ the locale is C. (Perhaps this is exactly what the locale settings are supposed to be about?)

Regards,

Karl <[email protected]...>
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein