Skip to content

Commit f40fccf

Browse files
Hanno Beckergilles-peskine-arm
authored andcommitted
ASN.1: Reimplement mbedtls_asn1_get_sequence_of() via traversal API
1 parent b8fccb7 commit f40fccf

File tree

1 file changed

+39
-42
lines changed

1 file changed

+39
-42
lines changed

library/asn1parse.c

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -314,59 +314,56 @@ void mbedtls_asn1_sequence_free( mbedtls_asn1_sequence *seq )
314314
}
315315
}
316316

317-
/*
318-
* Parses and splits an ASN.1 "SEQUENCE OF <tag>"
319-
*/
320-
int mbedtls_asn1_get_sequence_of( unsigned char **p,
321-
const unsigned char *end,
322-
mbedtls_asn1_sequence *cur,
323-
int tag)
317+
typedef struct
324318
{
325-
int ret;
326-
size_t len;
327-
mbedtls_asn1_buf *buf;
328-
329-
/* Get main sequence tag */
330-
if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
331-
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
332-
return( ret );
333-
334-
if( *p + len != end )
335-
return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
319+
int tag;
320+
mbedtls_asn1_sequence *cur;
321+
} asn1_get_sequence_of_cb_ctx_t;
322+
323+
static int asn1_get_sequence_of_cb( void *ctx,
324+
int tag,
325+
unsigned char *start,
326+
size_t len )
327+
{
328+
asn1_get_sequence_of_cb_ctx_t *cb_ctx =
329+
(asn1_get_sequence_of_cb_ctx_t *) ctx;
330+
mbedtls_asn1_sequence *cur =
331+
cb_ctx->cur;
336332

337-
while( *p < end )
333+
if( cur->buf.p != NULL )
338334
{
339-
buf = &(cur->buf);
340-
buf->tag = **p;
335+
cur->next =
336+
mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
341337

342-
if( ( ret = mbedtls_asn1_get_tag( p, end, &buf->len, tag ) ) != 0 )
343-
return( ret );
344-
345-
buf->p = *p;
346-
*p += buf->len;
347-
348-
/* Allocate and assign next pointer */
349-
if( *p < end )
350-
{
351-
cur->next = (mbedtls_asn1_sequence*)mbedtls_calloc( 1,
352-
sizeof( mbedtls_asn1_sequence ) );
353-
354-
if( cur->next == NULL )
355-
return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
338+
if( cur->next == NULL )
339+
return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
356340

357-
cur = cur->next;
358-
}
341+
cur = cur->next;
359342
}
360343

361-
/* Set final sequence entry's next pointer to NULL */
362-
cur->next = NULL;
363-
364-
if( *p != end )
365-
return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
344+
cur->buf.p = start;
345+
cur->buf.len = len;
346+
cur->buf.tag = tag;
366347

348+
cb_ctx->cur = cur;
367349
return( 0 );
368350
}
369351

352+
/*
353+
* Parses and splits an ASN.1 "SEQUENCE OF <tag>"
354+
*/
355+
int mbedtls_asn1_get_sequence_of( unsigned char **p,
356+
const unsigned char *end,
357+
mbedtls_asn1_sequence *cur,
358+
int tag)
359+
{
360+
asn1_get_sequence_of_cb_ctx_t cb_ctx = { tag, cur };
361+
memset( cur, 0, sizeof( mbedtls_asn1_sequence ) );
362+
return( mbedtls_asn1_traverse_sequence_of(
363+
p, end, 0xFF, tag, 0, 0,
364+
asn1_get_sequence_of_cb, &cb_ctx ) );
365+
}
366+
370367
int mbedtls_asn1_get_alg( unsigned char **p,
371368
const unsigned char *end,
372369
mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params )

0 commit comments

Comments
 (0)