Add ASN.1 API to free linked list representation of ASN.1 sequences
diff --git a/include/mbedtls/asn1.h b/include/mbedtls/asn1.h
index 5b7b2b8..b471a94 100644
--- a/include/mbedtls/asn1.h
+++ b/include/mbedtls/asn1.h
@@ -276,13 +276,38 @@
size_t *len );
/**
- * \brief Parses and splits an ASN.1 "SEQUENCE OF <tag>"
- * Updated the pointer to immediately behind the full sequence tag.
+ * \brief Free a heap-allocated linked list presentation of
+ * an ASN.1 sequence, including the first element.
*
- * \param p The position in the ASN.1 data
- * \param end End of data
- * \param cur First variable in the chain to fill
- * \param tag Type of sequence
+ * \param seq The address of the first sequence component. This may
+ * be \c NULL, in which case this functions returns
+ * immediately.
+ */
+void mbedtls_asn1_sequence_free( mbedtls_asn1_sequence *seq );
+
+/**
+ * \brief This function parses and splits an ASN.1 "SEQUENCE OF <tag>"
+ * and updates the source buffer pointer to immediately behind
+ * the full sequence.
+ *
+ * \param p The address of the pointer to the beginning of the
+ * ASN.1 SEQUENCE OF structure, including ASN.1 tag+length header.
+ * On success, `*p` is advanced to point to the first byte
+ * following the parsed ASN.1 sequence.
+ * \param end The end of the ASN.1 input buffer starting at \p p. This is
+ * used for bounds checking.
+ * \param cur The address at which to store the first entry in the parsed
+ * sequence. Further entries are heap-allocated and referenced
+ * from \p cur.
+ * \param tag The common tag of the entries in the ASN.1 sequence.
+ *
+ * \note Ownership for the heap-allocated elements \c cur->next,
+ * \c cur->next->next, ..., is passed to the caller. It
+ * is hence the caller's responsibility to free them when
+ * no longer needed, and mbedtls_asn1_sequence_free() can
+ * be used for that, passing \c cur->next as the \c seq
+ * argument (or \p cur if \p cur itself was heap-allocated
+ * by the caller).
*
* \return 0 if successful or a specific ASN.1 error code.
*/
diff --git a/include/mbedtls/x509.h b/include/mbedtls/x509.h
index 753019a..5d091bc 100644
--- a/include/mbedtls/x509.h
+++ b/include/mbedtls/x509.h
@@ -296,7 +296,10 @@
* be \c NULL, in which case this functions returns
* immediately.
*/
-void mbedtls_x509_sequence_free( mbedtls_x509_sequence *seq );
+static inline void mbedtls_x509_sequence_free( mbedtls_x509_sequence *seq )
+{
+ mbedtls_asn1_sequence_free( (mbedtls_asn1_sequence*) seq );
+}
#if defined(MBEDTLS_SELF_TEST)
diff --git a/library/asn1parse.c b/library/asn1parse.c
index f24fee6..68a70e6 100644
--- a/library/asn1parse.c
+++ b/library/asn1parse.c
@@ -229,6 +229,16 @@
return( 0 );
}
+void mbedtls_asn1_sequence_free( mbedtls_asn1_sequence *seq )
+{
+ while( seq != NULL )
+ {
+ mbedtls_asn1_sequence *next = seq->next;
+ mbedtls_platform_zeroize( seq, sizeof( *seq ) );
+ mbedtls_free( seq );
+ seq = next;
+ }
+}
/*
* Traverse an ASN.1 "SEQUENCE OF <tag>"
diff --git a/library/x509.c b/library/x509.c
index 627a5a3..f1c96a8 100644
--- a/library/x509.c
+++ b/library/x509.c
@@ -1220,17 +1220,6 @@
}
}
-void mbedtls_x509_sequence_free( mbedtls_x509_sequence *seq )
-{
- while( seq != NULL )
- {
- mbedtls_x509_sequence *next = seq->next;
- mbedtls_platform_zeroize( seq, sizeof( *seq ) );
- mbedtls_free( seq );
- seq = next;
- }
-}
-
#if defined(MBEDTLS_SELF_TEST)
#include "mbedtls/x509_crt.h"