Skip to content

Commit

Permalink
implemented generic string write function to match correct string tag
Browse files Browse the repository at this point in the history
  • Loading branch information
tdoe committed Nov 2, 2017
1 parent fea07a3 commit cf43076
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 50 deletions.
15 changes: 15 additions & 0 deletions include/mbedtls/asn1write.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolea
*/
int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val );

/**
* \brief Write a given string tag and
* value in ASN.1 format
* Note: function works backwards in data buffer
*
* \param p reference to current position pointer
* \param start start of the buffer (for bounds-checking)
* \param tag the tag to write
* \param text the text to write
* \param text_len length of the text
*
* \return the length written or a negative error code
*/
int mbedtls_asn1_write_any_string( unsigned char **p, unsigned char *start,
int tag, const char *text, size_t text_len );
/**
* \brief Write a printable string tag (MBEDTLS_ASN1_PRINTABLE_STRING) and
* value in ASN.1 format
Expand Down
32 changes: 10 additions & 22 deletions library/asn1write.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
return( (int) len );
}

int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
int mbedtls_asn1_write_any_string( unsigned char **p, unsigned char *start, int tag,
const char *text, size_t text_len )
{
int ret;
Expand All @@ -267,39 +267,27 @@ int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
(const unsigned char *) text, text_len ) );

MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_UTF8_STRING ) );
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) );

return( (int) len );
}

int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
const char *text, size_t text_len )
{
return( mbedtls_asn1_write_any_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len) );
}

int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
const char *text, size_t text_len )
{
int ret;
size_t len = 0;

MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
(const unsigned char *) text, text_len ) );

MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_PRINTABLE_STRING ) );

return( (int) len );
return( mbedtls_asn1_write_any_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text, text_len) );
}

int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
const char *text, size_t text_len )
{
int ret;
size_t len = 0;

MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
(const unsigned char *) text, text_len ) );

MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_IA5_STRING ) );

return( (int) len );
return( mbedtls_asn1_write_any_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len) );
}

int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Expand Down
32 changes: 4 additions & 28 deletions library/x509_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,34 +210,10 @@ static int x509_write_name( unsigned char **p, unsigned char *start, mbedtls_asn
const unsigned char *name = cur_name->val.p;
size_t name_len = cur_name->val.len;

// Write PrintableString for all except MBEDTLS_OID_PKCS9_EMAIL
//
if( MBEDTLS_OID_SIZE( MBEDTLS_OID_PKCS9_EMAIL ) == oid_len &&
memcmp( oid, MBEDTLS_OID_PKCS9_EMAIL, oid_len ) == 0 )
{
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_ia5_string( p, start,
(const char *) name,
name_len ) );
}
else if (cur_name->val.tag == MBEDTLS_ASN1_UTF8_STRING)
{
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_utf8_string( p, start,
(const char *) name,
name_len ) );
}
else if (cur_name->val.tag == MBEDTLS_ASN1_IA5_STRING)
{
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_ia5_string( p, start,
(const char *) name,
name_len ) );
}
else
{
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_printable_string( p, start,
(const char *) name,
name_len ) );
}

// Write correct string tag and value
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_any_string( p, start, cur_name->val.tag,
(const char *) name,
name_len ) );
// Write OID
//
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Expand Down

0 comments on commit cf43076

Please sign in to comment.