Skip to content

Commit 12ae27d

Browse files
author
Hanno Becker
committed
ASN.1: Introduce helper function to free ASN.1 sequence
1 parent 63e38fe commit 12ae27d

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

include/mbedtls/asn1.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,9 @@ int mbedtls_asn1_get_bitstring_null( unsigned char **p,
343343
* \brief Parses and splits an ASN.1 "SEQUENCE OF <tag>".
344344
* Updates the pointer to immediately behind the full sequence tag.
345345
*
346+
* This function allocates memory for the sequence elements. You can free
347+
* the allocated memory with mbedtls_asn1_sequence_free().
348+
*
346349
* \note On error, this function may return a partial list in \p cur.
347350
* You must set `cur->next = NULL` before calling this function!
348351
* Otherwise it is impossible to distinguish a previously non-null
@@ -384,6 +387,28 @@ int mbedtls_asn1_get_sequence_of( unsigned char **p,
384387
const unsigned char *end,
385388
mbedtls_asn1_sequence *cur,
386389
int tag );
390+
/**
391+
* \brief Free a heap-allocated linked list presentation of
392+
* an ASN.1 sequence, including the first element.
393+
*
394+
* There are two common ways to manage the memory used for the representation
395+
* of a parsed ASN.1 sequence:
396+
* - Allocate a head node `mbedtls_asn1_sequence *head` with mbedtls_calloc().
397+
* Pass this node as the `cur` argument to mbedtls_asn1_get_sequence_of().
398+
* When you have finished processing the sequence,
399+
* call mbedtls_asn1_sequence_free() on `head`.
400+
* - Allocate a head node `mbedtls_asn1_sequence *head` in any manner,
401+
* for example on the stack. Make sure that `head->next == NULL`.
402+
* Pass `head` as the `cur` argument to mbedtls_asn1_get_sequence_of().
403+
* When you have finished processing the sequence,
404+
* call mbedtls_asn1_sequence_free() on `head->cur`,
405+
* then free `head` itself in the appropriate manner.
406+
*
407+
* \param seq The address of the first sequence component. This may
408+
* be \c NULL, in which case this functions returns
409+
* immediately.
410+
*/
411+
void mbedtls_asn1_sequence_free( mbedtls_asn1_sequence *seq );
387412

388413
#if defined(MBEDTLS_BIGNUM_C)
389414
/**

library/asn1parse.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,16 @@ int mbedtls_asn1_get_bitstring_null( unsigned char **p, const unsigned char *end
269269
return( 0 );
270270
}
271271

272-
272+
void mbedtls_asn1_sequence_free( mbedtls_asn1_sequence *seq )
273+
{
274+
while( seq != NULL )
275+
{
276+
mbedtls_asn1_sequence *next = seq->next;
277+
mbedtls_platform_zeroize( seq, sizeof( *seq ) );
278+
mbedtls_free( seq );
279+
seq = next;
280+
}
281+
}
273282

274283
/*
275284
* Parses and splits an ASN.1 "SEQUENCE OF <tag>"

tests/suites/test_suite_asn1parse.function

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ void get_sequence_of( const data_t *input, int tag,
508508
int expected_result )
509509
{
510510
mbedtls_asn1_sequence head = { { 0, 0, NULL }, NULL };
511-
mbedtls_asn1_sequence *cur, *next;
511+
mbedtls_asn1_sequence *cur;
512512
unsigned char *p = input->x;
513513
const char *rest = description;
514514
unsigned long n;
@@ -549,13 +549,7 @@ void get_sequence_of( const data_t *input, int tag,
549549
}
550550

551551
exit:
552-
cur = head.next;
553-
while( cur != NULL )
554-
{
555-
next = cur->next;
556-
mbedtls_free( cur );
557-
cur = next;
558-
}
552+
mbedtls_asn1_sequence_free( head.next );
559553
}
560554
/* END_CASE */
561555

0 commit comments

Comments
 (0)