Fix parsing of special chars in X509 DN values

Use escape mechanism defined in RFC 1779 when parsing commas and other
special characters in X509 DN values. Resolves failures when generating
a certificate with a CSR containing a comma in subject value.
Fixes #769.

Signed-off-by: Werner Lewis <werner.lewis@arm.com>
This commit is contained in:
Werner Lewis 2022-05-20 12:48:46 +01:00 committed by Werner Lewis
parent 07040bb179
commit b33dacdb50
7 changed files with 69 additions and 9 deletions

View file

@ -741,7 +741,7 @@ int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t i, n;
size_t i, j, n;
unsigned char c, merge = 0;
const mbedtls_x509_name *name;
const char *short_name = NULL;
@ -775,17 +775,24 @@ int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
ret = mbedtls_snprintf( p, n, "\?\?=" );
MBEDTLS_X509_SAFE_SNPRINTF;
for( i = 0; i < name->val.len; i++ )
for( i = 0, j = 0; i < name->val.len; i++, j++ )
{
if( i >= sizeof( s ) - 1 )
break;
if( j >= sizeof( s ) - 1 )
return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
c = name->val.p[i];
// Special characters requiring escaping, RFC 1779
if( c && strchr( ",=+<>#;\"\\", c ) )
{
if( j + 1 >= sizeof( s ) - 1 )
continue;
s[j++] = '\\';
}
if( c < 32 || c >= 127 )
s[i] = '?';
else s[i] = c;
s[j] = '?';
else s[j] = c;
}
s[i] = '\0';
s[j] = '\0';
ret = mbedtls_snprintf( p, n, "%s", s );
MBEDTLS_X509_SAFE_SNPRINTF;