1/* BEGIN_HEADER */
2#include <errno.h>
3#include <stdlib.h>
4#include <limits.h>
5
6#include "mbedtls/bignum.h"
7#include "mbedtls/asn1.h"
8#if defined(MBEDTLS_ASN1_WRITE_C)
9#include "mbedtls/asn1write.h"
10#endif
11
12/* Used internally to report an error that indicates a bug in a parsing function. */
13#define ERR_PARSE_INCONSISTENCY INT_MAX
14
15/* Use this magic value in some tests to indicate that the expected result
16 * should not be checked. */
17#define UNPREDICTABLE_RESULT 0x5552
18
19static int nested_parse( unsigned char **const p,
20                         const unsigned char *const end )
21{
22    int ret;
23    size_t len = 0;
24    size_t len2 = 0;
25    unsigned char *const start = *p;
26    unsigned char *content_start;
27    unsigned char tag;
28
29    /* First get the length, skipping over the tag. */
30    content_start = start + 1;
31    ret = mbedtls_asn1_get_len( &content_start, end, &len );
32    TEST_ASSERT( content_start <= end );
33    if( ret != 0 )
34        return( ret );
35
36    /* Since we have a valid element start (tag and length), retrieve and
37     * check the tag. */
38    tag = start[0];
39    TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len2, tag ^ 1 ),
40                MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
41    *p = start;
42    TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len2, tag ), 0 );
43    TEST_EQUAL( len, len2 );
44    TEST_ASSERT( *p == content_start );
45    *p = content_start;
46
47    switch( tag & 0x1f )
48    {
49        case MBEDTLS_ASN1_BOOLEAN:
50        {
51            int val = -257;
52            *p = start;
53            ret = mbedtls_asn1_get_bool( p, end, &val );
54            if( ret == 0 )
55                TEST_ASSERT( val == 0 || val == 1 );
56            break;
57        }
58
59        case MBEDTLS_ASN1_INTEGER:
60        {
61#if defined(MBEDTLS_BIGNUM_C)
62            mbedtls_mpi mpi;
63            mbedtls_mpi_init( &mpi );
64            *p = start;
65            ret = mbedtls_asn1_get_mpi( p, end, &mpi );
66            mbedtls_mpi_free( &mpi );
67#else
68            *p = start + 1;
69            ret = mbedtls_asn1_get_len( p, end, &len );
70            *p += len;
71#endif
72            /* If we're sure that the number fits in an int, also
73             * call mbedtls_asn1_get_int(). */
74            if( ret == 0 && len < sizeof( int ) )
75            {
76                int val = -257;
77                unsigned char *q = start;
78                ret = mbedtls_asn1_get_int( &q, end, &val );
79                TEST_ASSERT( *p == q );
80            }
81            break;
82        }
83
84        case MBEDTLS_ASN1_BIT_STRING:
85        {
86            mbedtls_asn1_bitstring bs;
87            *p = start;
88            ret = mbedtls_asn1_get_bitstring( p, end, &bs );
89            break;
90        }
91
92        case MBEDTLS_ASN1_SEQUENCE:
93        {
94            while( *p <= end && *p < content_start + len && ret == 0 )
95                ret = nested_parse( p, content_start + len );
96            break;
97        }
98
99        case MBEDTLS_ASN1_OCTET_STRING:
100        case MBEDTLS_ASN1_NULL:
101        case MBEDTLS_ASN1_OID:
102        case MBEDTLS_ASN1_UTF8_STRING:
103        case MBEDTLS_ASN1_SET:
104        case MBEDTLS_ASN1_PRINTABLE_STRING:
105        case MBEDTLS_ASN1_T61_STRING:
106        case MBEDTLS_ASN1_IA5_STRING:
107        case MBEDTLS_ASN1_UTC_TIME:
108        case MBEDTLS_ASN1_GENERALIZED_TIME:
109        case MBEDTLS_ASN1_UNIVERSAL_STRING:
110        case MBEDTLS_ASN1_BMP_STRING:
111        default:
112            /* No further testing implemented for this tag. */
113            *p += len;
114            return( 0 );
115    }
116
117    TEST_ASSERT( *p <= end );
118    return( ret );
119
120exit:
121    return( ERR_PARSE_INCONSISTENCY );
122}
123
124int get_len_step( const data_t *input, size_t buffer_size,
125                  size_t actual_length )
126{
127    unsigned char *buf = NULL;
128    unsigned char *p = NULL;
129    unsigned char *end;
130    size_t parsed_length;
131    int ret;
132
133    test_set_step( buffer_size );
134    /* Allocate a new buffer of exactly the length to parse each time.
135     * This gives memory sanitizers a chance to catch buffer overreads. */
136    if( buffer_size == 0 )
137    {
138        ASSERT_ALLOC( buf, 1 );
139        end = buf + 1;
140        p = end;
141    }
142    else
143    {
144        ASSERT_ALLOC_WEAK( buf, buffer_size );
145        if( buffer_size > input->len )
146        {
147            memcpy( buf, input->x, input->len );
148            memset( buf + input->len, 'A', buffer_size - input->len );
149        }
150        else
151        {
152            memcpy( buf, input->x, buffer_size );
153        }
154        p = buf;
155        end = buf + buffer_size;
156    }
157
158    ret = mbedtls_asn1_get_len( &p, end, &parsed_length );
159
160    if( buffer_size >= input->len + actual_length )
161    {
162        TEST_EQUAL( ret, 0 );
163        TEST_ASSERT( p == buf + input->len );
164        TEST_EQUAL( parsed_length, actual_length );
165    }
166    else
167    {
168        TEST_EQUAL( ret, MBEDTLS_ERR_ASN1_OUT_OF_DATA );
169    }
170    mbedtls_free( buf );
171    return( 1 );
172
173exit:
174    mbedtls_free( buf );
175    return( 0 );
176}
177
178typedef struct
179{
180    const unsigned char *input_start;
181    const char *description;
182} traverse_state_t;
183
184/* Value returned by traverse_callback if description runs out. */
185#define RET_TRAVERSE_STOP 1
186/* Value returned by traverse_callback if description has an invalid format
187 * (see traverse_sequence_of). */
188#define RET_TRAVERSE_ERROR 2
189
190
191static int traverse_callback( void *ctx, int tag,
192                              unsigned char *content, size_t len )
193{
194    traverse_state_t *state = ctx;
195    size_t offset;
196    const char *rest = state->description;
197    unsigned long n;
198
199    TEST_ASSERT( content > state->input_start );
200    offset = content - state->input_start;
201    test_set_step( offset );
202
203    if( *rest == 0 )
204        return( RET_TRAVERSE_STOP );
205    n = strtoul( rest, (char **) &rest, 0 );
206    TEST_EQUAL( n, offset );
207    TEST_EQUAL( *rest, ',' );
208    ++rest;
209    n = strtoul( rest, (char **) &rest, 0 );
210    TEST_EQUAL( n, (unsigned) tag );
211    TEST_EQUAL( *rest, ',' );
212    ++rest;
213    n = strtoul( rest, (char **) &rest, 0 );
214    TEST_EQUAL( n, len );
215    if( *rest == ',' )
216        ++rest;
217
218    state->description = rest;
219    return( 0 );
220
221exit:
222    return( RET_TRAVERSE_ERROR );
223}
224
225/* END_HEADER */
226
227/* BEGIN_DEPENDENCIES
228 * depends_on:MBEDTLS_ASN1_PARSE_C
229 * END_DEPENDENCIES
230 */
231
232/* BEGIN_CASE */
233void parse_prefixes( const data_t *input,
234                     int full_result,
235                     int overfull_result )
236{
237    /* full_result: expected result from parsing the given string. */
238    /* overfull_result: expected_result from parsing the given string plus
239     * some trailing garbage. This may be UNPREDICTABLE_RESULT to accept
240     * any result: use this for invalid inputs that may or may not become
241     * valid depending on what the trailing garbage is. */
242
243    unsigned char *buf = NULL;
244    unsigned char *p = NULL;
245    size_t buffer_size;
246    int ret;
247
248    /* Test every prefix of the input, except the empty string.
249     * The first byte of the string is the tag. Without a tag byte,
250     * we wouldn't know what to parse the input as.
251     * Also test the input followed by an extra byte.
252     */
253    for( buffer_size = 1; buffer_size <= input->len + 1; buffer_size++ )
254    {
255        test_set_step( buffer_size );
256        /* Allocate a new buffer of exactly the length to parse each time.
257         * This gives memory sanitizers a chance to catch buffer overreads. */
258        ASSERT_ALLOC( buf, buffer_size );
259        memcpy( buf, input->x, buffer_size );
260        p = buf;
261        ret = nested_parse( &p, buf + buffer_size );
262
263        if( ret == ERR_PARSE_INCONSISTENCY )
264            goto exit;
265        if( buffer_size < input->len )
266        {
267            TEST_EQUAL( ret, MBEDTLS_ERR_ASN1_OUT_OF_DATA );
268        }
269        else if( buffer_size == input->len )
270        {
271            TEST_EQUAL( ret, full_result );
272        }
273        else /* ( buffer_size > input->len ) */
274        {
275            if( overfull_result != UNPREDICTABLE_RESULT )
276                TEST_EQUAL( ret, overfull_result );
277        }
278        if( ret == 0 )
279            TEST_ASSERT( p == buf + input->len );
280
281        mbedtls_free( buf );
282        buf = NULL;
283    }
284
285exit:
286    mbedtls_free( buf );
287}
288/* END_CASE */
289
290/* BEGIN_CASE */
291void get_len( const data_t *input, int actual_length_arg )
292{
293    size_t actual_length = actual_length_arg;
294    size_t buffer_size;
295
296    /* Test prefixes of a buffer containing the given length string
297     * followed by `actual_length` bytes of payload. To save a bit of
298     * time, we skip some "boring" prefixes: we don't test prefixes where
299     * the payload is truncated more than one byte away from either end,
300     * and we only test the empty string on a 1-byte input.
301     */
302    for( buffer_size = 1; buffer_size <= input->len + 1; buffer_size++ )
303    {
304        if( ! get_len_step( input, buffer_size, actual_length ) )
305            goto exit;
306    }
307    if( ! get_len_step( input, input->len + actual_length - 1, actual_length ) )
308        goto exit;
309    if( ! get_len_step( input, input->len + actual_length, actual_length ) )
310        goto exit;
311}
312/* END_CASE */
313
314/* BEGIN_CASE */
315void get_boolean( const data_t *input,
316                  int expected_value, int expected_result )
317{
318    unsigned char *p = input->x;
319    int val;
320    int ret;
321    ret = mbedtls_asn1_get_bool( &p, input->x + input->len, &val );
322    TEST_EQUAL( ret, expected_result );
323    if( expected_result == 0 )
324    {
325        TEST_EQUAL( val, expected_value );
326        TEST_ASSERT( p == input->x + input->len );
327    }
328}
329/* END_CASE */
330
331/* BEGIN_CASE */
332void empty_integer( const data_t *input )
333{
334    unsigned char *p;
335#if defined(MBEDTLS_BIGNUM_C)
336    mbedtls_mpi actual_mpi;
337#endif
338    int val;
339
340#if defined(MBEDTLS_BIGNUM_C)
341    mbedtls_mpi_init( & actual_mpi );
342#endif
343
344    /* An INTEGER with no content is not valid. */
345    p = input->x;
346    TEST_EQUAL( mbedtls_asn1_get_int( &p, input->x + input->len, &val ),
347                MBEDTLS_ERR_ASN1_INVALID_LENGTH );
348
349#if defined(MBEDTLS_BIGNUM_C)
350    /* INTEGERs are sometimes abused as bitstrings, so the library accepts
351     * an INTEGER with empty content and gives it the value 0. */
352    p = input->x;
353    TEST_EQUAL( mbedtls_asn1_get_mpi( &p, input->x + input->len, &actual_mpi ),
354                0 );
355    TEST_EQUAL( mbedtls_mpi_cmp_int( &actual_mpi, 0 ), 0 );
356#endif
357
358exit:
359#if defined(MBEDTLS_BIGNUM_C)
360    mbedtls_mpi_free( &actual_mpi );
361#endif
362    /*empty cleanup in some configurations*/ ;
363}
364/* END_CASE */
365
366/* BEGIN_CASE */
367void get_integer( const data_t *input,
368                  const char *expected_hex, int expected_result )
369{
370    unsigned char *p;
371#if defined(MBEDTLS_BIGNUM_C)
372    mbedtls_mpi expected_mpi;
373    mbedtls_mpi actual_mpi;
374    mbedtls_mpi complement;
375    int expected_result_for_mpi = expected_result;
376#endif
377    long expected_value;
378    int expected_result_for_int = expected_result;
379    int val;
380    int ret;
381
382#if defined(MBEDTLS_BIGNUM_C)
383    mbedtls_mpi_init( &expected_mpi );
384    mbedtls_mpi_init( &actual_mpi );
385    mbedtls_mpi_init( &complement );
386#endif
387
388    errno = 0;
389    expected_value = strtol( expected_hex, NULL, 16 );
390    if( expected_result == 0 &&
391        ( errno == ERANGE
392#if LONG_MAX > INT_MAX
393          || expected_value > INT_MAX || expected_value < INT_MIN
394#endif
395            ) )
396    {
397        /* The library returns the dubious error code INVALID_LENGTH
398         * for integers that are out of range. */
399        expected_result_for_int = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
400    }
401    if( expected_result == 0 && expected_value < 0 )
402    {
403        /* The library does not support negative INTEGERs and
404         * returns the dubious error code INVALID_LENGTH.
405         * Test that we preserve the historical behavior. If we
406         * decide to change the behavior, we'll also change this test. */
407        expected_result_for_int = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
408    }
409
410    p = input->x;
411    ret = mbedtls_asn1_get_int( &p, input->x + input->len, &val );
412    TEST_EQUAL( ret, expected_result_for_int );
413    if( ret == 0 )
414    {
415        TEST_EQUAL( val, expected_value );
416        TEST_ASSERT( p == input->x + input->len );
417    }
418
419#if defined(MBEDTLS_BIGNUM_C)
420    ret = mbedtls_mpi_read_string( &expected_mpi, 16, expected_hex );
421    TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
422    if( ret == MBEDTLS_ERR_MPI_BAD_INPUT_DATA )
423    {
424        /* The data overflows the maximum MPI size. */
425        expected_result_for_mpi = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
426    }
427    p = input->x;
428    ret = mbedtls_asn1_get_mpi( &p, input->x + input->len, &actual_mpi );
429    TEST_EQUAL( ret, expected_result_for_mpi );
430    if( ret == 0 )
431    {
432        if( expected_value >= 0 )
433        {
434            TEST_ASSERT( mbedtls_mpi_cmp_mpi( &actual_mpi,
435                                              &expected_mpi ) == 0 );
436        }
437        else
438        {
439            /* The library ignores the sign bit in ASN.1 INTEGERs
440             * (which makes sense insofar as INTEGERs are sometimes
441             * abused as bit strings), so the result of parsing them
442             * is a positive integer such that expected_mpi +
443             * actual_mpi = 2^n where n is the length of the content
444             * of the INTEGER. (Leading ff octets don't matter for the
445             * expected value, but they matter for the actual value.)
446             * Test that we don't change from this behavior. If we
447             * decide to fix the library to change the behavior on
448             * negative INTEGERs, we'll fix this test code. */
449            unsigned char *q = input->x + 1;
450            size_t len;
451            TEST_ASSERT( mbedtls_asn1_get_len( &q, input->x + input->len,
452                                               &len ) == 0 );
453            TEST_ASSERT( mbedtls_mpi_lset( &complement, 1 ) == 0 );
454            TEST_ASSERT( mbedtls_mpi_shift_l( &complement, len * 8 ) == 0 );
455            TEST_ASSERT( mbedtls_mpi_add_mpi( &complement, &complement,
456                                              &expected_mpi ) == 0 );
457            TEST_ASSERT( mbedtls_mpi_cmp_mpi( &complement,
458                                              &actual_mpi ) == 0 );
459        }
460        TEST_ASSERT( p == input->x + input->len );
461    }
462#endif
463
464exit:
465#if defined(MBEDTLS_BIGNUM_C)
466    mbedtls_mpi_free( &expected_mpi );
467    mbedtls_mpi_free( &actual_mpi );
468    mbedtls_mpi_free( &complement );
469#endif
470    /*empty cleanup in some configurations*/ ;
471}
472/* END_CASE */
473
474/* BEGIN_CASE */
475void get_enum( const data_t *input,
476               const char *expected_hex, int expected_result )
477{
478    unsigned char *p;
479    long expected_value;
480    int expected_result_for_enum = expected_result;
481    int val;
482    int ret;
483
484    errno = 0;
485    expected_value = strtol( expected_hex, NULL, 16 );
486    if( expected_result == 0 &&
487        ( errno == ERANGE
488#if LONG_MAX > INT_MAX
489          || expected_value > INT_MAX || expected_value < INT_MIN
490#endif
491            ) )
492    {
493        /* The library returns the dubious error code INVALID_LENGTH
494         * for integers that are out of range. */
495        expected_result_for_enum = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
496    }
497    if( expected_result == 0 && expected_value < 0 )
498    {
499        /* The library does not support negative INTEGERs and
500         * returns the dubious error code INVALID_LENGTH.
501         * Test that we preserve the historical behavior. If we
502         * decide to change the behavior, we'll also change this test. */
503        expected_result_for_enum = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
504    }
505
506    p = input->x;
507    ret = mbedtls_asn1_get_enum( &p, input->x + input->len, &val );
508    TEST_EQUAL( ret, expected_result_for_enum );
509    if( ret == 0 )
510    {
511        TEST_EQUAL( val, expected_value );
512        TEST_ASSERT( p == input->x + input->len );
513    }
514}
515/* END_CASE */
516
517/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
518void get_mpi_too_large( )
519{
520    unsigned char *buf = NULL;
521    unsigned char *p;
522    mbedtls_mpi actual_mpi;
523    size_t too_many_octets =
524        MBEDTLS_MPI_MAX_LIMBS * sizeof(mbedtls_mpi_uint) + 1;
525    size_t size = too_many_octets + 6;
526
527    mbedtls_mpi_init( &actual_mpi );
528
529    ASSERT_ALLOC( buf, size );
530    buf[0] = 0x02; /* tag: INTEGER */
531    buf[1] = 0x84; /* 4-octet length */
532    buf[2] = ( too_many_octets >> 24 ) & 0xff;
533    buf[3] = ( too_many_octets >> 16 ) & 0xff;
534    buf[4] = ( too_many_octets >> 8 ) & 0xff;
535    buf[5] = too_many_octets & 0xff;
536    buf[6] = 0x01; /* most significant octet */
537
538    p = buf;
539    TEST_EQUAL( mbedtls_asn1_get_mpi( &p, buf + size, &actual_mpi ),
540                MBEDTLS_ERR_MPI_ALLOC_FAILED );
541
542exit:
543    mbedtls_mpi_free( &actual_mpi );
544    mbedtls_free( buf );
545}
546/* END_CASE */
547
548/* BEGIN_CASE */
549void get_bitstring( const data_t *input,
550                    int expected_length, int expected_unused_bits,
551                    int expected_result, int expected_result_null )
552{
553    mbedtls_asn1_bitstring bs = { 0xdead, 0x21, NULL };
554    unsigned char *p = input->x;
555
556    TEST_EQUAL( mbedtls_asn1_get_bitstring( &p, input->x + input->len, &bs ),
557                expected_result );
558    if( expected_result == 0 )
559    {
560        TEST_EQUAL( bs.len, (size_t) expected_length );
561        TEST_EQUAL( bs.unused_bits, expected_unused_bits );
562        TEST_ASSERT( bs.p != NULL );
563        TEST_EQUAL( bs.p - input->x + bs.len, input->len );
564        TEST_ASSERT( p == input->x + input->len );
565    }
566
567    p = input->x;
568    TEST_EQUAL( mbedtls_asn1_get_bitstring_null( &p, input->x + input->len,
569                                                 &bs.len ),
570                expected_result_null );
571    if( expected_result_null == 0 )
572    {
573        TEST_EQUAL( bs.len, (size_t) expected_length );
574        if( expected_result == 0 )
575            TEST_ASSERT( p == input->x + input->len - bs.len );
576    }
577}
578/* END_CASE */
579
580/* BEGIN_CASE */
581void get_sequence_of( const data_t *input, int tag,
582                      const char *description,
583                      int expected_result )
584{
585    /* The description string is a comma-separated list of integers.
586     * For each element in the SEQUENCE in input, description contains
587     * two integers: the offset of the element (offset from the start
588     * of input to the tag of the element) and the length of the
589     * element's contents.
590     * "offset1,length1,..." */
591
592    mbedtls_asn1_sequence head = { { 0, 0, NULL }, NULL };
593    mbedtls_asn1_sequence *cur;
594    unsigned char *p = input->x;
595    const char *rest = description;
596    unsigned long n;
597
598    TEST_EQUAL( mbedtls_asn1_get_sequence_of( &p, input->x + input->len,
599                                              &head, tag ),
600                expected_result );
601    if( expected_result == 0 )
602    {
603        TEST_ASSERT( p == input->x + input->len );
604
605        if( ! *rest )
606        {
607            TEST_EQUAL( head.buf.tag, 0 );
608            TEST_ASSERT( head.buf.p == NULL );
609            TEST_EQUAL( head.buf.len, 0 );
610            TEST_ASSERT( head.next == NULL );
611        }
612        else
613        {
614            cur = &head;
615            while( *rest )
616            {
617                ++test_info.step;
618                TEST_ASSERT( cur != NULL );
619                TEST_EQUAL( cur->buf.tag, tag );
620                n = strtoul( rest, (char **) &rest, 0 );
621                TEST_EQUAL( n, (size_t)( cur->buf.p - input->x ) );
622                ++rest;
623                n = strtoul( rest, (char **) &rest, 0 );
624                TEST_EQUAL( n, cur->buf.len );
625                if( *rest )
626                    ++rest;
627                cur = cur->next;
628            }
629            TEST_ASSERT( cur == NULL );
630        }
631    }
632
633exit:
634    mbedtls_asn1_sequence_free( head.next );
635}
636/* END_CASE */
637
638/* BEGIN_CASE */
639void traverse_sequence_of( const data_t *input,
640                           int tag_must_mask, int tag_must_val,
641                           int tag_may_mask, int tag_may_val,
642                           const char *description,
643                           int expected_result )
644{
645    /* The description string is a comma-separated list of integers.
646     * For each element in the SEQUENCE in input, description contains
647     * three integers: the offset of the element's content (offset from
648     * the start of input to the content of the element), the element's tag,
649     * and the length of the element's contents.
650     * "offset1,tag1,length1,..." */
651
652    unsigned char *p = input->x;
653    traverse_state_t traverse_state = {input->x, description};
654    int ret;
655
656    ret = mbedtls_asn1_traverse_sequence_of( &p, input->x + input->len,
657                                             (uint8_t) tag_must_mask, (uint8_t) tag_must_val,
658                                             (uint8_t) tag_may_mask, (uint8_t) tag_may_val,
659                                             traverse_callback, &traverse_state );
660    if( ret == RET_TRAVERSE_ERROR )
661        goto exit;
662    TEST_EQUAL( ret, expected_result );
663    TEST_EQUAL( *traverse_state.description, 0 );
664}
665/* END_CASE */
666
667/* BEGIN_CASE */
668void get_alg( const data_t *input,
669              int oid_offset, int oid_length,
670              int params_tag, int params_offset, int params_length,
671              int total_length,
672              int expected_result )
673{
674    mbedtls_asn1_buf oid = { -1, 0, NULL };
675    mbedtls_asn1_buf params = { -1, 0, NULL };
676    unsigned char *p = input->x;
677    int ret;
678
679    TEST_EQUAL( mbedtls_asn1_get_alg( &p, input->x + input->len,
680                                      &oid, &params ),
681                expected_result );
682    if( expected_result == 0 )
683    {
684        TEST_EQUAL( oid.tag, MBEDTLS_ASN1_OID );
685        TEST_EQUAL( oid.p - input->x, oid_offset );
686        TEST_EQUAL( oid.len, (size_t) oid_length );
687        TEST_EQUAL( params.tag, params_tag );
688        if( params_offset != 0 )
689            TEST_EQUAL( params.p - input->x, params_offset );
690        else
691            TEST_ASSERT( params.p == NULL );
692        TEST_EQUAL( params.len, (size_t) params_length );
693        TEST_EQUAL( p - input->x, total_length );
694    }
695
696    ret = mbedtls_asn1_get_alg_null( &p, input->x + input->len, &oid );
697    if( expected_result == 0 && params_offset == 0 )
698    {
699        TEST_EQUAL( oid.tag, MBEDTLS_ASN1_OID );
700        TEST_EQUAL( oid.p - input->x, oid_offset );
701        TEST_EQUAL( oid.len, (size_t) oid_length );
702        TEST_EQUAL( p - input->x, total_length );
703    }
704    else
705        TEST_ASSERT( ret != 0 );
706}
707/* END_CASE */
708
709/* BEGIN_CASE */
710void find_named_data( data_t *oid0, data_t *oid1, data_t *oid2, data_t *oid3,
711                      data_t *needle, int from, int position )
712{
713    mbedtls_asn1_named_data nd[] ={
714        { {0x06, oid0->len, oid0->x}, {0, 0, NULL}, NULL, 0 },
715        { {0x06, oid1->len, oid1->x}, {0, 0, NULL}, NULL, 0 },
716        { {0x06, oid2->len, oid2->x}, {0, 0, NULL}, NULL, 0 },
717        { {0x06, oid3->len, oid3->x}, {0, 0, NULL}, NULL, 0 },
718    };
719    mbedtls_asn1_named_data *pointers[ARRAY_LENGTH( nd ) + 1];
720    size_t i;
721    mbedtls_asn1_named_data *found;
722
723    for( i = 0; i < ARRAY_LENGTH( nd ); i++ )
724        pointers[i] = &nd[i];
725    pointers[ARRAY_LENGTH( nd )] = NULL;
726    for( i = 0; i < ARRAY_LENGTH( nd ); i++ )
727        nd[i].next = pointers[i+1];
728
729    found = mbedtls_asn1_find_named_data( pointers[from],
730                                          (const char *) needle->x,
731                                          needle->len );
732    TEST_ASSERT( found == pointers[position] );
733}
734/* END_CASE */
735
736/* BEGIN_CASE */
737void free_named_data_null( )
738{
739    mbedtls_asn1_free_named_data( NULL );
740    goto exit; /* Silence unused label warning */
741}
742/* END_CASE */
743
744/* BEGIN_CASE */
745void free_named_data( int with_oid, int with_val, int with_next )
746{
747    mbedtls_asn1_named_data next =
748        { {0x06, 0, NULL}, {0, 0xcafe, NULL}, NULL, 0 };
749    mbedtls_asn1_named_data head =
750        { {0x06, 0, NULL}, {0, 0, NULL}, NULL, 0 };
751
752    if( with_oid )
753        ASSERT_ALLOC( head.oid.p, 1 );
754    if( with_val )
755        ASSERT_ALLOC( head.val.p, 1 );
756    if( with_next )
757        head.next = &next;
758
759    mbedtls_asn1_free_named_data( &head );
760    TEST_ASSERT( head.oid.p == NULL );
761    TEST_ASSERT( head.val.p == NULL );
762    TEST_ASSERT( head.next == NULL );
763    TEST_ASSERT( next.val.len == 0xcafe );
764
765exit:
766    mbedtls_free( head.oid.p );
767    mbedtls_free( head.val.p );
768}
769/* END_CASE */
770
771/* BEGIN_CASE */
772void free_named_data_list( int length )
773{
774    mbedtls_asn1_named_data *head = NULL;
775    int i;
776
777    for( i = 0; i < length; i++ )
778    {
779        mbedtls_asn1_named_data *new = NULL;
780        ASSERT_ALLOC( new, sizeof( mbedtls_asn1_named_data ) );
781        new->next = head;
782        head = new;
783    }
784
785    mbedtls_asn1_free_named_data_list( &head );
786    TEST_ASSERT( head == NULL );
787    /* Most of the point of the test is that it doesn't leak memory.
788     * So this test is only really useful under a memory leak detection
789     * framework. */
790exit:
791    mbedtls_asn1_free_named_data_list( &head );
792}
793/* END_CASE */
794