Skip to content

Commit 9ab7c07

Browse files
authored
Merge pull request #75 from gilles-peskine-arm/asn1-tests-without-x509
ASN.1 tests without x509
2 parents 6b2a779 + 88f136f commit 9ab7c07

13 files changed

+1833
-211
lines changed

include/mbedtls/asn1.h

Lines changed: 164 additions & 69 deletions
Large diffs are not rendered by default.

include/mbedtls/asn1write.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
100100
* \param p The reference to the current position pointer.
101101
* \param start The start of the buffer, for bounds-checking.
102102
* \param X The MPI to write.
103+
* It must be non-negative.
103104
*
104105
* \return The number of bytes written to \p p on success.
105106
* \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
@@ -184,6 +185,7 @@ int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start,
184185
* \param p The reference to the current position pointer.
185186
* \param start The start of the buffer, for bounds-checking.
186187
* \param val The integer value to write.
188+
* It must be non-negative.
187189
*
188190
* \return The number of bytes written to \p p on success.
189191
* \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
@@ -232,7 +234,7 @@ int mbedtls_asn1_write_printable_string( unsigned char **p,
232234

233235
/**
234236
* \brief Write a UTF8 string in ASN.1 format using the UTF8String
235-
* string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING).
237+
* string encoding tag (#MBEDTLS_ASN1_UTF8_STRING).
236238
*
237239
* \note This function works backwards in data buffer.
238240
*
@@ -332,9 +334,13 @@ int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
332334
* through (will be updated in case of a new entry).
333335
* \param oid The OID to look for.
334336
* \param oid_len The size of the OID.
335-
* \param val The data to store (can be \c NULL if you want to fill
336-
* it by hand).
337+
* \param val The associated data to store. If this is \c NULL,
338+
* no data is copied to the new or existing buffer.
337339
* \param val_len The minimum length of the data buffer needed.
340+
* If this is 0, do not allocate a buffer for the associated
341+
* data.
342+
* If the OID was already present, enlarge, shrink or free
343+
* the existing buffer to fit \p val_len.
338344
*
339345
* \return A pointer to the new / existing entry on success.
340346
* \return \c NULL if if there was a memory allocation error.

library/asn1parse.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,18 @@ int mbedtls_asn1_get_int( unsigned char **p,
149149
if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
150150
return( ret );
151151

152-
if( len == 0 || len > sizeof( int ) || ( **p & 0x80 ) != 0 )
152+
if( len == 0 || ( **p & 0x80 ) != 0 )
153153
return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
154154

155-
*val = 0;
155+
while( len > 0 && **p == 0 )
156+
{
157+
++( *p );
158+
--len;
159+
}
160+
if( len > sizeof( int ) )
161+
return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
156162

163+
*val = 0;
157164
while( len-- > 0 )
158165
{
159166
*val = ( *val << 8 ) | **p;
@@ -223,8 +230,13 @@ int mbedtls_asn1_get_bitstring_null( unsigned char **p, const unsigned char *end
223230
if( ( ret = mbedtls_asn1_get_tag( p, end, len, MBEDTLS_ASN1_BIT_STRING ) ) != 0 )
224231
return( ret );
225232

226-
if( (*len)-- < 2 || *(*p)++ != 0 )
233+
if( *len == 0 )
234+
return( MBEDTLS_ERR_ASN1_INVALID_DATA );
235+
--( *len );
236+
237+
if( **p != 0 )
227238
return( MBEDTLS_ERR_ASN1_INVALID_DATA );
239+
++( *p );
228240

229241
return( 0 );
230242
}

library/asn1write.c

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -236,17 +236,20 @@ int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
236236
int ret;
237237
size_t len = 0;
238238

239-
if( *p - start < 1 )
240-
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
241-
242-
len += 1;
243-
*--(*p) = val;
244-
245-
if( val > 0 && **p & 0x80 )
239+
do
246240
{
247241
if( *p - start < 1 )
248242
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
243+
len += 1;
244+
*--(*p) = val & 0xff;
245+
val >>= 8;
246+
}
247+
while( val > 0 );
249248

249+
if( **p & 0x80 )
250+
{
251+
if( *p - start < 1 )
252+
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
250253
*--(*p) = 0x00;
251254
len += 1;
252255
}
@@ -429,18 +432,26 @@ mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
429432
memcpy( cur->oid.p, oid, oid_len );
430433

431434
cur->val.len = val_len;
432-
cur->val.p = mbedtls_calloc( 1, val_len );
433-
if( cur->val.p == NULL )
435+
if( val_len != 0 )
434436
{
435-
mbedtls_free( cur->oid.p );
436-
mbedtls_free( cur );
437-
return( NULL );
437+
cur->val.p = mbedtls_calloc( 1, val_len );
438+
if( cur->val.p == NULL )
439+
{
440+
mbedtls_free( cur->oid.p );
441+
mbedtls_free( cur );
442+
return( NULL );
443+
}
438444
}
439445

440446
cur->next = *head;
441447
*head = cur;
442448
}
443-
else if( cur->val.len < val_len )
449+
else if( val_len == 0 )
450+
{
451+
mbedtls_free( cur->val.p );
452+
cur->val.p = NULL;
453+
}
454+
else if( cur->val.len != val_len )
444455
{
445456
/*
446457
* Enlarge existing value buffer if needed

library/error.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ void mbedtls_strerror( int ret, char *buf, size_t buflen )
467467
if( use_ret == -(MBEDTLS_ERR_ASN1_LENGTH_MISMATCH) )
468468
mbedtls_snprintf( buf, buflen, "ASN1 - Actual length differs from expected length" );
469469
if( use_ret == -(MBEDTLS_ERR_ASN1_INVALID_DATA) )
470-
mbedtls_snprintf( buf, buflen, "ASN1 - Data is invalid. (not used)" );
470+
mbedtls_snprintf( buf, buflen, "ASN1 - Data is invalid" );
471471
if( use_ret == -(MBEDTLS_ERR_ASN1_ALLOC_FAILED) )
472472
mbedtls_snprintf( buf, buflen, "ASN1 - Memory allocation failed" );
473473
if( use_ret == -(MBEDTLS_ERR_ASN1_BUF_TOO_SMALL) )

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ add_test_suite(aes aes.rest)
7979
add_test_suite(aes aes.xts)
8080
add_test_suite(arc4)
8181
add_test_suite(aria)
82+
add_test_suite(asn1parse)
8283
add_test_suite(asn1write)
8384
add_test_suite(base64)
8485
add_test_suite(blowfish)

tests/suites/helpers.function

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,26 @@ typedef enum
158158
} \
159159
while( 0 )
160160

161+
/** Allocate memory dynamically. Exit the test if this fails, but do
162+
* not mark the test as failed.
163+
*
164+
* This macro behaves like #ASSERT_ALLOC, except that if the allocation
165+
* fails, it jumps to the \c exit label without calling test_fail().
166+
*/
167+
#define ASSERT_ALLOC_WEAK( pointer, length ) \
168+
do \
169+
{ \
170+
TEST_ASSERT( ( pointer ) == NULL ); \
171+
if( ( length ) != 0 ) \
172+
{ \
173+
( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
174+
( length ) ); \
175+
if( ( pointer ) == NULL ) \
176+
goto exit; \
177+
} \
178+
} \
179+
while( 0 )
180+
161181
/** Compare two buffers and fail the test case if they differ.
162182
*
163183
* This macro expands to an instruction, not an expression.
@@ -393,6 +413,7 @@ static struct
393413
const char *test;
394414
const char *filename;
395415
int line_no;
416+
unsigned long step;
396417
}
397418
test_info;
398419

@@ -423,6 +444,19 @@ jmp_buf jmp_tmp;
423444
/*----------------------------------------------------------------------------*/
424445
/* Helper Functions */
425446

447+
/** Set the test step number for failure reports.
448+
*
449+
* Call this function to display "step NNN" in addition to the line number
450+
* and file name if a test fails. Typically the "step number" is the index
451+
* of a for loop but it can be whatever you want.
452+
*
453+
* \param step The step number to report.
454+
*/
455+
void test_set_step( unsigned long step )
456+
{
457+
test_info.step = step;
458+
}
459+
426460
void test_fail( const char *test, int line_no, const char* filename )
427461
{
428462
test_info.result = TEST_RESULT_FAILED;

tests/suites/host_test.function

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ int execute_tests( int argc , const char ** argv )
548548
{
549549
test_info.result = TEST_RESULT_SUCCESS;
550550
test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_IDLE;
551+
test_info.step = (unsigned long)( -1 );
551552

552553
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
553554
/* Suppress all output from the library unless we're verbose
@@ -624,9 +625,15 @@ int execute_tests( int argc , const char ** argv )
624625
{
625626
total_errors++;
626627
mbedtls_fprintf( stdout, "FAILED\n" );
627-
mbedtls_fprintf( stdout, " %s\n at line %d, %s\n",
628-
test_info.test, test_info.line_no,
629-
test_info.filename );
628+
mbedtls_fprintf( stdout, " %s\n at ",
629+
test_info.test );
630+
if( test_info.step != (unsigned long)( -1 ) )
631+
{
632+
mbedtls_fprintf( stdout, "step %lu, ",
633+
test_info.step );
634+
}
635+
mbedtls_fprintf( stdout, "line %d, %s",
636+
test_info.line_no, test_info.filename );
630637
}
631638
fflush( stdout );
632639
}

tests/suites/target_test.function

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ int execute_tests( int args, const char ** argv )
376376
{
377377
ret = 0;
378378
test_info.result = TEST_RESULT_SUCCESS;
379+
test_info.step = (unsigned long)( -1 );
379380
data_len = 0;
380381

381382
data = receive_data( &data_len );

0 commit comments

Comments
 (0)