Skip to content

Commit 1505f63

Browse files
author
Hanno Becker
committed
ASN.1: Reimplement mbedtls_asn1_get_sequence_of() via traversal API
1 parent 199b709 commit 1505f63

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
@@ -332,59 +332,56 @@ void mbedtls_asn1_sequence_free( mbedtls_asn1_sequence *seq )
332332
}
333333
}
334334

335-
/*
336-
* Parses and splits an ASN.1 "SEQUENCE OF <tag>"
337-
*/
338-
int mbedtls_asn1_get_sequence_of( unsigned char **p,
339-
const unsigned char *end,
340-
mbedtls_asn1_sequence *cur,
341-
int tag)
335+
typedef struct
342336
{
343-
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
344-
size_t len;
345-
mbedtls_asn1_buf *buf;
346-
347-
/* Get main sequence tag */
348-
if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
349-
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
350-
return( ret );
351-
352-
if( *p + len != end )
353-
return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
337+
int tag;
338+
mbedtls_asn1_sequence *cur;
339+
} asn1_get_sequence_of_cb_ctx_t;
340+
341+
static int asn1_get_sequence_of_cb( void *ctx,
342+
int tag,
343+
unsigned char *start,
344+
size_t len )
345+
{
346+
asn1_get_sequence_of_cb_ctx_t *cb_ctx =
347+
(asn1_get_sequence_of_cb_ctx_t *) ctx;
348+
mbedtls_asn1_sequence *cur =
349+
cb_ctx->cur;
354350

355-
while( *p < end )
351+
if( cur->buf.p != NULL )
356352
{
357-
buf = &(cur->buf);
358-
buf->tag = **p;
353+
cur->next =
354+
mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
359355

360-
if( ( ret = mbedtls_asn1_get_tag( p, end, &buf->len, tag ) ) != 0 )
361-
return( ret );
362-
363-
buf->p = *p;
364-
*p += buf->len;
365-
366-
/* Allocate and assign next pointer */
367-
if( *p < end )
368-
{
369-
cur->next = (mbedtls_asn1_sequence*)mbedtls_calloc( 1,
370-
sizeof( mbedtls_asn1_sequence ) );
371-
372-
if( cur->next == NULL )
373-
return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
356+
if( cur->next == NULL )
357+
return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
374358

375-
cur = cur->next;
376-
}
359+
cur = cur->next;
377360
}
378361

379-
/* Set final sequence entry's next pointer to NULL */
380-
cur->next = NULL;
381-
382-
if( *p != end )
383-
return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
362+
cur->buf.p = start;
363+
cur->buf.len = len;
364+
cur->buf.tag = tag;
384365

366+
cb_ctx->cur = cur;
385367
return( 0 );
386368
}
387369

370+
/*
371+
* Parses and splits an ASN.1 "SEQUENCE OF <tag>"
372+
*/
373+
int mbedtls_asn1_get_sequence_of( unsigned char **p,
374+
const unsigned char *end,
375+
mbedtls_asn1_sequence *cur,
376+
int tag)
377+
{
378+
asn1_get_sequence_of_cb_ctx_t cb_ctx = { tag, cur };
379+
memset( cur, 0, sizeof( mbedtls_asn1_sequence ) );
380+
return( mbedtls_asn1_traverse_sequence_of(
381+
p, end, 0xFF, tag, 0, 0,
382+
asn1_get_sequence_of_cb, &cb_ctx ) );
383+
}
384+
388385
int mbedtls_asn1_get_alg( unsigned char **p,
389386
const unsigned char *end,
390387
mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params )

0 commit comments

Comments
 (0)