Shared code to free x509 structs

Signed-off-by: Glenn Strauss <gstrauss@gluelogic.com>
diff --git a/library/x509.c b/library/x509.c
index c5b0161..362e036 100644
--- a/library/x509.c
+++ b/library/x509.c
@@ -472,7 +472,6 @@
     size_t set_len;
     const unsigned char *end_set;
     mbedtls_x509_name *head = cur;
-    mbedtls_x509_name *prev, *allocated;
 
     /* don't use recursion, we'd risk stack overflow if not optimized */
     while( 1 )
@@ -530,18 +529,8 @@
 
 error:
     /* Skip the first element as we did not allocate it */
-    allocated = head->next;
-
-    while( allocated != NULL )
-    {
-        prev = allocated;
-        allocated = allocated->next;
-
-        mbedtls_platform_zeroize( prev, sizeof( *prev ) );
-        mbedtls_free( prev );
-    }
-
-    mbedtls_platform_zeroize( head, sizeof( *head ) );
+    mbedtls_asn1_free_named_data_list_shallow( head->next );
+    head->next = NULL;
 
     return( ret );
 }