-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix string downcast to PrintableString
as issued in #1033
#1641
Changes from 5 commits
23f954d
c5c9aaf
c150f0d
020c823
eba6c9b
d2c9009
d0e21fb
d355e69
cfc47ba
35b6854
1624e2e
f745733
56e8463
50cb93a
b837775
ebc1f40
0dd1139
386f99c
b963081
52acdb5
0f6903d
cb93813
b12fd31
381c77c
6e1adee
beffcd8
ee334a3
cec1c26
5517755
710f203
f143a78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,6 +152,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 | ||
hanno-becker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* \param p reference to current position pointer | ||
* \param start start of the buffer (for bounds-checking) | ||
* \param tag the tag to write | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: |
||
* \param text the text to write | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: |
||
* \param text_len length of the text | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: |
||
* | ||
* \return the length written or a negative error code | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As below, it should be clarified that "length" refers to the total length in bytes of the ASN.1 data that has been written, and the failing case should be mentioned in a separate |
||
*/ | ||
int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start, | ||
simonbutcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int tag, const char *text, size_t text_len ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The indentation is wrong here.
hanno-becker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* \brief Write a printable string tag (MBEDTLS_ASN1_PRINTABLE_STRING) and | ||
* value in ASN.1 format | ||
|
@@ -167,6 +182,21 @@ int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val ); | |
int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start, | ||
const char *text, size_t text_len ); | ||
|
||
/** | ||
* \brief Write a UTF8 string tag (MBEDTLS_ASN1_UTF8_STRING) 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 text the text to write | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pre-existing, but in line with the above suggestion I'd find it clearer to write |
||
* \param text_len length of the text | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pre-existing, but in line with the above suggestion I'd find it clearer to write |
||
* | ||
* \return the length written or a negative error code | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pre-existing, but it should be clarified that "length" refers to the entire ASN.1 data, so @thomas-dee Do you mind making these changes while at it? |
||
*/ | ||
int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start, | ||
const char *text, size_t text_len ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The indentation is wrong here. |
||
|
||
/** | ||
* \brief Write an IA5 string tag (MBEDTLS_ASN1_IA5_STRING) and | ||
* value in ASN.1 format | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,44 +37,46 @@ typedef struct { | |
const char *name; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pre-existing: Even though it's internal, this structure should be properly documented. E.g.:
|
||
size_t name_len; | ||
const char*oid; | ||
int tag; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to call this |
||
} x509_attr_descriptor_t; | ||
|
||
#define ADD_STRLEN( s ) s, sizeof( s ) - 1 | ||
|
||
// note: preset tag types as proposed in RFC3280 and widely used | ||
static const x509_attr_descriptor_t x509_attrs[] = | ||
{ | ||
{ ADD_STRLEN( "CN" ), MBEDTLS_OID_AT_CN }, | ||
{ ADD_STRLEN( "commonName" ), MBEDTLS_OID_AT_CN }, | ||
{ ADD_STRLEN( "C" ), MBEDTLS_OID_AT_COUNTRY }, | ||
{ ADD_STRLEN( "countryName" ), MBEDTLS_OID_AT_COUNTRY }, | ||
{ ADD_STRLEN( "O" ), MBEDTLS_OID_AT_ORGANIZATION }, | ||
{ ADD_STRLEN( "organizationName" ), MBEDTLS_OID_AT_ORGANIZATION }, | ||
{ ADD_STRLEN( "L" ), MBEDTLS_OID_AT_LOCALITY }, | ||
{ ADD_STRLEN( "locality" ), MBEDTLS_OID_AT_LOCALITY }, | ||
{ ADD_STRLEN( "R" ), MBEDTLS_OID_PKCS9_EMAIL }, | ||
{ ADD_STRLEN( "OU" ), MBEDTLS_OID_AT_ORG_UNIT }, | ||
{ ADD_STRLEN( "organizationalUnitName" ), MBEDTLS_OID_AT_ORG_UNIT }, | ||
{ ADD_STRLEN( "ST" ), MBEDTLS_OID_AT_STATE }, | ||
{ ADD_STRLEN( "stateOrProvinceName" ), MBEDTLS_OID_AT_STATE }, | ||
{ ADD_STRLEN( "emailAddress" ), MBEDTLS_OID_PKCS9_EMAIL }, | ||
{ ADD_STRLEN( "serialNumber" ), MBEDTLS_OID_AT_SERIAL_NUMBER }, | ||
{ ADD_STRLEN( "postalAddress" ), MBEDTLS_OID_AT_POSTAL_ADDRESS }, | ||
{ ADD_STRLEN( "postalCode" ), MBEDTLS_OID_AT_POSTAL_CODE }, | ||
{ ADD_STRLEN( "dnQualifier" ), MBEDTLS_OID_AT_DN_QUALIFIER }, | ||
{ ADD_STRLEN( "title" ), MBEDTLS_OID_AT_TITLE }, | ||
{ ADD_STRLEN( "surName" ), MBEDTLS_OID_AT_SUR_NAME }, | ||
{ ADD_STRLEN( "SN" ), MBEDTLS_OID_AT_SUR_NAME }, | ||
{ ADD_STRLEN( "givenName" ), MBEDTLS_OID_AT_GIVEN_NAME }, | ||
{ ADD_STRLEN( "GN" ), MBEDTLS_OID_AT_GIVEN_NAME }, | ||
{ ADD_STRLEN( "initials" ), MBEDTLS_OID_AT_INITIALS }, | ||
{ ADD_STRLEN( "pseudonym" ), MBEDTLS_OID_AT_PSEUDONYM }, | ||
{ ADD_STRLEN( "generationQualifier" ), MBEDTLS_OID_AT_GENERATION_QUALIFIER }, | ||
{ ADD_STRLEN( "domainComponent" ), MBEDTLS_OID_DOMAIN_COMPONENT }, | ||
{ ADD_STRLEN( "DC" ), MBEDTLS_OID_DOMAIN_COMPONENT }, | ||
{ NULL, 0, NULL } | ||
{ ADD_STRLEN( "CN" ), MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING }, | ||
{ ADD_STRLEN( "commonName" ), MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING }, | ||
{ ADD_STRLEN( "C" ), MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING }, | ||
{ ADD_STRLEN( "countryName" ), MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING }, | ||
{ ADD_STRLEN( "O" ), MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING }, | ||
{ ADD_STRLEN( "organizationName" ), MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING }, | ||
{ ADD_STRLEN( "L" ), MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING }, | ||
{ ADD_STRLEN( "locality" ), MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING }, | ||
{ ADD_STRLEN( "R" ), MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A whitespace is missing near the end of this line |
||
{ ADD_STRLEN( "OU" ), MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING }, | ||
{ ADD_STRLEN( "organizationalUnitName" ), MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING }, | ||
{ ADD_STRLEN( "ST" ), MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING }, | ||
{ ADD_STRLEN( "stateOrProvinceName" ), MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING }, | ||
{ ADD_STRLEN( "emailAddress" ), MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING }, | ||
{ ADD_STRLEN( "serialNumber" ), MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING }, | ||
{ ADD_STRLEN( "postalAddress" ), MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING }, | ||
{ ADD_STRLEN( "postalCode" ), MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING }, | ||
{ ADD_STRLEN( "dnQualifier" ), MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @thomas-dee Could you explain the varying choices of encodings here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned in line 45 I tried to default to the types given in RFC3280. For the given types it was not clear for me, so I defaulted to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @thomas-dee I overlooked the reference to RFC 3280, thanks for pointing that out. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should reference the newer RFC 5280 here. |
||
{ ADD_STRLEN( "title" ), MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING }, | ||
{ ADD_STRLEN( "surName" ), MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING }, | ||
{ ADD_STRLEN( "SN" ), MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING }, | ||
{ ADD_STRLEN( "givenName" ), MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING }, | ||
{ ADD_STRLEN( "GN" ), MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING }, | ||
{ ADD_STRLEN( "initials" ), MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING }, | ||
{ ADD_STRLEN( "pseudonym" ), MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING }, | ||
{ ADD_STRLEN( "generationQualifier" ), MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING }, | ||
{ ADD_STRLEN( "domainComponent" ), MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING }, | ||
{ ADD_STRLEN( "DC" ), MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING }, | ||
{ NULL, 0, NULL, MBEDTLS_ASN1_NULL } | ||
}; | ||
|
||
static const char *x509_at_oid_from_name( const char *name, size_t name_len ) | ||
static const x509_attr_descriptor_t *x509_attr_descr_from_name( const char *name, size_t name_len ) | ||
{ | ||
const x509_attr_descriptor_t *cur; | ||
|
||
|
@@ -83,7 +85,10 @@ static const char *x509_at_oid_from_name( const char *name, size_t name_len ) | |
strncmp( cur->name, name, name_len ) == 0 ) | ||
break; | ||
|
||
return( cur->oid ); | ||
if ( cur->name == NULL ) | ||
return( NULL ); | ||
|
||
return( cur ); | ||
} | ||
|
||
int mbedtls_x509_string_to_names( mbedtls_asn1_named_data **head, const char *name ) | ||
|
@@ -92,6 +97,7 @@ int mbedtls_x509_string_to_names( mbedtls_asn1_named_data **head, const char *na | |
const char *s = name, *c = s; | ||
const char *end = s + strlen( s ); | ||
const char *oid = NULL; | ||
const x509_attr_descriptor_t* attr_descr = NULL; | ||
int in_tag = 1; | ||
char data[MBEDTLS_X509_MAX_DN_NAME_SIZE]; | ||
char *d = data; | ||
|
@@ -103,12 +109,13 @@ int mbedtls_x509_string_to_names( mbedtls_asn1_named_data **head, const char *na | |
{ | ||
if( in_tag && *c == '=' ) | ||
{ | ||
if( ( oid = x509_at_oid_from_name( s, c - s ) ) == NULL ) | ||
if( ( attr_descr = x509_attr_descr_from_name( s, c - s ) ) == NULL ) | ||
{ | ||
ret = MBEDTLS_ERR_X509_UNKNOWN_OID; | ||
goto exit; | ||
} | ||
|
||
oid = attr_descr->oid; | ||
s = c + 1; | ||
in_tag = 0; | ||
d = data; | ||
|
@@ -127,13 +134,18 @@ int mbedtls_x509_string_to_names( mbedtls_asn1_named_data **head, const char *na | |
} | ||
else if( !in_tag && ( *c == ',' || c == end ) ) | ||
{ | ||
if( mbedtls_asn1_store_named_data( head, oid, strlen( oid ), | ||
(unsigned char *) data, | ||
d - data ) == NULL ) | ||
mbedtls_asn1_named_data* cur = mbedtls_asn1_store_named_data( head, oid, strlen( oid ), | ||
hanno-becker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(unsigned char *) data, | ||
d - data ); | ||
|
||
if(cur == NULL ) | ||
{ | ||
return( MBEDTLS_ERR_X509_ALLOC_FAILED ); | ||
} | ||
|
||
// set tagType | ||
cur->val.tag = attr_descr->tag; | ||
|
||
while( c < end && *(c + 1) == ' ' ) | ||
c++; | ||
|
||
|
@@ -192,29 +204,19 @@ int mbedtls_x509_set_extension( mbedtls_asn1_named_data **head, const char *oid, | |
* | ||
* AttributeValue ::= ANY DEFINED BY AttributeType | ||
*/ | ||
static int x509_write_name( unsigned char **p, unsigned char *start, | ||
const char *oid, size_t oid_len, | ||
const unsigned char *name, size_t name_len ) | ||
static int x509_write_name( unsigned char **p, unsigned char *start, mbedtls_asn1_named_data* cur_name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line has got too long. Please break it. |
||
{ | ||
int ret; | ||
size_t len = 0; | ||
|
||
// 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 | ||
{ | ||
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_printable_string( p, start, | ||
(const char *) name, | ||
name_len ) ); | ||
} | ||
|
||
const char *oid = (const char*)cur_name->oid.p; | ||
size_t oid_len = cur_name->oid.len; | ||
const unsigned char *name = cur_name->val.p; | ||
size_t name_len = cur_name->val.len; | ||
|
||
// Write correct string tag and value | ||
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tagged_string( p, start, cur_name->val.tag, | ||
(const char *) name, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The indentation looks off by one character here. |
||
name_len ) ); | ||
// Write OID | ||
// | ||
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) ); | ||
|
@@ -239,9 +241,7 @@ int mbedtls_x509_write_names( unsigned char **p, unsigned char *start, | |
|
||
while( cur != NULL ) | ||
{ | ||
MBEDTLS_ASN1_CHK_ADD( len, x509_write_name( p, start, (char *) cur->oid.p, | ||
cur->oid.len, | ||
cur->val.p, cur->val.len ) ); | ||
MBEDTLS_ASN1_CHK_ADD( len, x509_write_name( p, start, cur ) ); | ||
cur = cur->next; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation should be expanded here. Suggestion:
Write a string in ASN.1 format using a specific encoding tag.