Skip to content

Commit 5d972e7

Browse files
author
Hanno Becker
committed
ASN.1: Reimplement mbedtls_asn1_get_sequence_of() via traversal API
1 parent 3c25ddd commit 5d972e7

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
@@ -292,59 +292,56 @@ void mbedtls_asn1_sequence_free( mbedtls_asn1_sequence *seq )
292292
}
293293
}
294294

295-
/*
296-
* Parses and splits an ASN.1 "SEQUENCE OF <tag>"
297-
*/
298-
int mbedtls_asn1_get_sequence_of( unsigned char **p,
299-
const unsigned char *end,
300-
mbedtls_asn1_sequence *cur,
301-
int tag)
295+
typedef struct
302296
{
303-
int ret;
304-
size_t len;
305-
mbedtls_asn1_buf *buf;
306-
307-
/* Get main sequence tag */
308-
if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
309-
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
310-
return( ret );
311-
312-
if( *p + len != end )
313-
return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
297+
int tag;
298+
mbedtls_asn1_sequence *cur;
299+
} asn1_get_sequence_of_cb_ctx_t;
300+
301+
static int asn1_get_sequence_of_cb( void *ctx,
302+
int tag,
303+
unsigned char *start,
304+
size_t len )
305+
{
306+
asn1_get_sequence_of_cb_ctx_t *cb_ctx =
307+
(asn1_get_sequence_of_cb_ctx_t *) ctx;
308+
mbedtls_asn1_sequence *cur =
309+
cb_ctx->cur;
314310

315-
while( *p < end )
311+
if( cur->buf.p != NULL )
316312
{
317-
buf = &(cur->buf);
318-
buf->tag = **p;
313+
cur->next =
314+
mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
319315

320-
if( ( ret = mbedtls_asn1_get_tag( p, end, &buf->len, tag ) ) != 0 )
321-
return( ret );
322-
323-
buf->p = *p;
324-
*p += buf->len;
325-
326-
/* Allocate and assign next pointer */
327-
if( *p < end )
328-
{
329-
cur->next = (mbedtls_asn1_sequence*)mbedtls_calloc( 1,
330-
sizeof( mbedtls_asn1_sequence ) );
331-
332-
if( cur->next == NULL )
333-
return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
316+
if( cur->next == NULL )
317+
return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
334318

335-
cur = cur->next;
336-
}
319+
cur = cur->next;
337320
}
338321

339-
/* Set final sequence entry's next pointer to NULL */
340-
cur->next = NULL;
341-
342-
if( *p != end )
343-
return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
322+
cur->buf.p = start;
323+
cur->buf.len = len;
324+
cur->buf.tag = tag;
344325

326+
cb_ctx->cur = cur;
345327
return( 0 );
346328
}
347329

330+
/*
331+
* Parses and splits an ASN.1 "SEQUENCE OF <tag>"
332+
*/
333+
int mbedtls_asn1_get_sequence_of( unsigned char **p,
334+
const unsigned char *end,
335+
mbedtls_asn1_sequence *cur,
336+
int tag)
337+
{
338+
asn1_get_sequence_of_cb_ctx_t cb_ctx = { tag, cur };
339+
memset( cur, 0, sizeof( mbedtls_asn1_sequence ) );
340+
return( mbedtls_asn1_traverse_sequence_of(
341+
p, end, 0xFF, tag, 0, 0,
342+
asn1_get_sequence_of_cb, &cb_ctx ) );
343+
}
344+
348345
int mbedtls_asn1_get_alg( unsigned char **p,
349346
const unsigned char *end,
350347
mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params )

0 commit comments

Comments
 (0)