Skip to content

Commit 83ef814

Browse files
mbedtls_asn1_store_named_data: clarify val allocation behavior
Document how mbedtls_asn1_store_named_data allocates val.p in the new or modified entry. Change the behavior to be more regular, always setting the new length to val_len. This does not affect the previous documented behavior since this aspect was not documented. This does not affect current usage in Mbed TLS's X.509 module where calls with the same OID always use the same size for the associated value.
1 parent 38ba27f commit 83ef814

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

include/mbedtls/asn1write.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,13 @@ int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
333333
* through (will be updated in case of a new entry).
334334
* \param oid The OID to look for.
335335
* \param oid_len The size of the OID.
336-
* \param val The data to store (can be \c NULL if you want to fill
337-
* it by hand).
336+
* \param val The associated data to store. If this is \c NULL,
337+
* no data is copied to the new or existing buffer.
338338
* \param val_len The minimum length of the data buffer needed.
339+
* If this is 0, do not allocate a buffer for the associated
340+
* data.
341+
* If the OID was already present, enlarge, shrink or free
342+
* the existing buffer to fit \p val_len.
339343
*
340344
* \return A pointer to the new / existing entry on success.
341345
* \return \c NULL if if there was a memory allocation error.

library/asn1write.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -432,18 +432,26 @@ mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
432432
memcpy( cur->oid.p, oid, oid_len );
433433

434434
cur->val.len = val_len;
435-
cur->val.p = mbedtls_calloc( 1, val_len );
436-
if( cur->val.p == NULL )
435+
if( val_len != 0 )
437436
{
438-
mbedtls_free( cur->oid.p );
439-
mbedtls_free( cur );
440-
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+
}
441444
}
442445

443446
cur->next = *head;
444447
*head = cur;
445448
}
446-
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 )
447455
{
448456
/*
449457
* Enlarge existing value buffer if needed

0 commit comments

Comments
 (0)