blob: 94e34fb4799ce0f87d3dc373a46e7e800de2a05e [file] [log] [blame]
Gilles Peskine27d806f2019-03-01 18:02:53 +01001/* 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
Gilles Peskine95c893d2020-01-21 21:26:36 +010012/* Used internally to report an error that indicates a bug in a parsing function. */
Gilles Peskine27d806f2019-03-01 18:02:53 +010013#define ERR_PARSE_INCONSISTENCY INT_MAX
14
Gilles Peskine95c893d2020-01-21 21:26:36 +010015/* Use this magic value in some tests to indicate that the expected result
16 * should not be checked. */
17#define UNPREDICTABLE_RESULT 0x5552
18
Gilles Peskine27d806f2019-03-01 18:02:53 +010019static 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 );
Gilles Peskine03c165e2019-10-10 19:15:18 +020067#else
68 *p = start + 1;
69 ret = mbedtls_asn1_get_len( p, end, &len );
70 *p += len;
Gilles Peskine27d806f2019-03-01 18:02:53 +010071#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 size_t parsed_length;
130 int ret;
131
132 test_set_step( buffer_size );
133 /* Allocate a new buffer of exactly the length to parse each time.
134 * This gives memory sanitizers a chance to catch buffer overreads. */
135 if( buffer_size == 0 )
136 {
137 ASSERT_ALLOC( buf, 1 );
138 p = buf + 1;
139 }
140 else
141 {
Gilles Peskine2cd8ecc2019-03-04 17:13:43 +0100142 ASSERT_ALLOC_WEAK( buf, buffer_size );
Gilles Peskine27d806f2019-03-01 18:02:53 +0100143 if( buffer_size > input->len )
144 {
145 memcpy( buf, input->x, input->len );
146 memset( buf + input->len, 'A', buffer_size - input->len );
147 }
148 else
149 {
150 memcpy( buf, input->x, buffer_size );
151 }
152 p = buf;
153 }
154
155 ret = mbedtls_asn1_get_len( &p, buf + buffer_size, &parsed_length );
156
157 if( buffer_size >= input->len + actual_length )
158 {
159 TEST_EQUAL( ret, 0 );
160 TEST_ASSERT( p == buf + input->len );
161 TEST_EQUAL( parsed_length, actual_length );
162 }
163 else
164 {
165 TEST_EQUAL( ret, MBEDTLS_ERR_ASN1_OUT_OF_DATA );
166 }
167 mbedtls_free( buf );
168 return( 1 );
169
170exit:
Gilles Peskine27d806f2019-03-01 18:02:53 +0100171 mbedtls_free( buf );
172 return( 0 );
173}
174
175/* END_HEADER */
176
177/* BEGIN_DEPENDENCIES
178 * depends_on:MBEDTLS_ASN1_PARSE_C
179 * END_DEPENDENCIES
180 */
181
182/* BEGIN_CASE */
183void parse_prefixes( const data_t *input,
Gilles Peskine95c893d2020-01-21 21:26:36 +0100184 int full_result,
185 int overfull_result )
Gilles Peskine27d806f2019-03-01 18:02:53 +0100186{
Gilles Peskine95c893d2020-01-21 21:26:36 +0100187 /* full_result: expected result from parsing the given string. */
188 /* overfull_result: expected_result from parsing the given string plus
189 * some trailing garbage. This may be UNPREDICTABLE_RESULT to accept
190 * any result: use this for invalid inputs that may or may not become
191 * valid depending on what the trailing garbage is. */
192
Gilles Peskine27d806f2019-03-01 18:02:53 +0100193 unsigned char *buf = NULL;
194 unsigned char *p = NULL;
195 size_t buffer_size;
196 int ret;
197
Gilles Peskineef418382020-01-21 18:56:27 +0100198 /* Test every prefix of the input, except the empty string.
199 * The first byte of the string is the tag. Without a tag byte,
200 * we wouldn't know what to parse the input as.
Gilles Peskine95c893d2020-01-21 21:26:36 +0100201 * Also test the input followed by an extra byte.
Gilles Peskineef418382020-01-21 18:56:27 +0100202 */
Gilles Peskine95c893d2020-01-21 21:26:36 +0100203 for( buffer_size = 1; buffer_size <= input->len + 1; buffer_size++ )
Gilles Peskine27d806f2019-03-01 18:02:53 +0100204 {
205 test_set_step( buffer_size );
206 /* Allocate a new buffer of exactly the length to parse each time.
207 * This gives memory sanitizers a chance to catch buffer overreads. */
208 ASSERT_ALLOC( buf, buffer_size );
209 memcpy( buf, input->x, buffer_size );
210 p = buf;
211 ret = nested_parse( &p, buf + buffer_size );
Gilles Peskine95c893d2020-01-21 21:26:36 +0100212
Gilles Peskine27d806f2019-03-01 18:02:53 +0100213 if( ret == ERR_PARSE_INCONSISTENCY )
214 goto exit;
Gilles Peskine95c893d2020-01-21 21:26:36 +0100215 if( buffer_size < input->len )
Gilles Peskine27d806f2019-03-01 18:02:53 +0100216 {
217 TEST_EQUAL( ret, MBEDTLS_ERR_ASN1_OUT_OF_DATA );
218 }
Gilles Peskine95c893d2020-01-21 21:26:36 +0100219 else if( buffer_size == input->len )
220 {
221 TEST_EQUAL( ret, full_result );
222 }
223 else /* ( buffer_size > input->len ) */
224 {
225 if( overfull_result != UNPREDICTABLE_RESULT )
226 TEST_EQUAL( ret, overfull_result );
227 }
228 if( ret == 0 )
229 TEST_ASSERT( p == buf + input->len );
230
Gilles Peskine27d806f2019-03-01 18:02:53 +0100231 mbedtls_free( buf );
232 buf = NULL;
233 }
234
235exit:
236 mbedtls_free( buf );
237}
238/* END_CASE */
239
240/* BEGIN_CASE */
241void get_len( const data_t *input, int actual_length_arg )
242{
243 size_t actual_length = actual_length_arg;
244 size_t buffer_size;
245
Gilles Peskineef418382020-01-21 18:56:27 +0100246 /* Test prefixes of a buffer containing the given length string
247 * followed by `actual_length` bytes of payload. To save a bit of
248 * time, we skip some "boring" prefixes: we don't test prefixes where
249 * the payload is truncated more than one byte away from either end,
250 * and we only test the empty string on a 1-byte input.
251 */
Gilles Peskine27d806f2019-03-01 18:02:53 +0100252 for( buffer_size = 1; buffer_size <= input->len + 1; buffer_size++ )
253 {
254 if( ! get_len_step( input, buffer_size, actual_length ) )
255 goto exit;
256 }
257 if( ! get_len_step( input, input->len + actual_length - 1, actual_length ) )
258 goto exit;
259 if( ! get_len_step( input, input->len + actual_length, actual_length ) )
260 goto exit;
261}
262/* END_CASE */
263
264/* BEGIN_CASE */
265void get_boolean( const data_t *input,
266 int expected_value, int expected_result )
267{
268 unsigned char *p = input->x;
269 int val;
270 int ret;
271 ret = mbedtls_asn1_get_bool( &p, input->x + input->len, &val );
272 TEST_EQUAL( ret, expected_result );
273 if( expected_result == 0 )
274 {
275 TEST_EQUAL( val, expected_value );
276 TEST_ASSERT( p == input->x + input->len );
277 }
278}
279/* END_CASE */
280
281/* BEGIN_CASE */
Gilles Peskine321adb22019-10-10 19:18:21 +0200282void empty_integer( const data_t *input )
283{
284 unsigned char *p;
285#if defined(MBEDTLS_BIGNUM_C)
286 mbedtls_mpi actual_mpi;
287#endif
288 int val;
289
290#if defined(MBEDTLS_BIGNUM_C)
291 mbedtls_mpi_init( & actual_mpi );
292#endif
293
294 /* An INTEGER with no content is not valid. */
295 p = input->x;
296 TEST_EQUAL( mbedtls_asn1_get_int( &p, input->x + input->len, &val ),
297 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
298
299#if defined(MBEDTLS_BIGNUM_C)
300 /* INTEGERs are sometimes abused as bitstrings, so the library accepts
301 * an INTEGER with empty content and gives it the value 0. */
302 p = input->x;
303 TEST_EQUAL( mbedtls_asn1_get_mpi( &p, input->x + input->len, &actual_mpi ),
304 0 );
305 TEST_EQUAL( mbedtls_mpi_cmp_int( &actual_mpi, 0 ), 0 );
306#endif
307
308exit:
309#if defined(MBEDTLS_BIGNUM_C)
310 mbedtls_mpi_free( &actual_mpi );
311#endif
312 /*empty cleanup in some configurations*/ ;
313}
314/* END_CASE */
315
316/* BEGIN_CASE */
Gilles Peskine27d806f2019-03-01 18:02:53 +0100317void get_integer( const data_t *input,
318 const char *expected_hex, int expected_result )
319{
320 unsigned char *p;
321#if defined(MBEDTLS_BIGNUM_C)
322 mbedtls_mpi expected_mpi;
323 mbedtls_mpi actual_mpi;
Gilles Peskine970dcbf2019-10-10 19:21:12 +0200324 mbedtls_mpi complement;
Gilles Peskine03c165e2019-10-10 19:15:18 +0200325 int expected_result_for_mpi = expected_result;
Gilles Peskine27d806f2019-03-01 18:02:53 +0100326#endif
327 long expected_value;
328 int expected_result_for_int = expected_result;
Gilles Peskine27d806f2019-03-01 18:02:53 +0100329 int val;
330 int ret;
331
332#if defined(MBEDTLS_BIGNUM_C)
333 mbedtls_mpi_init( &expected_mpi );
334 mbedtls_mpi_init( &actual_mpi );
Gilles Peskine970dcbf2019-10-10 19:21:12 +0200335 mbedtls_mpi_init( &complement );
Gilles Peskine27d806f2019-03-01 18:02:53 +0100336#endif
337
338 errno = 0;
339 expected_value = strtol( expected_hex, NULL, 16 );
340 if( expected_result == 0 &&
341 ( errno == ERANGE
342#if LONG_MAX > INT_MAX
343 || expected_value > INT_MAX || expected_value < INT_MIN
344#endif
345 ) )
346 {
Gilles Peskine970dcbf2019-10-10 19:21:12 +0200347 /* The library returns the dubious error code INVALID_LENGTH
348 * for integers that are out of range. */
349 expected_result_for_int = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
350 }
351 if( expected_result == 0 && expected_value < 0 )
352 {
353 /* The library does not support negative INTEGERs and
354 * returns the dubious error code INVALID_LENGTH.
355 * Test that we preserve the historical behavior. If we
356 * decide to change the behavior, we'll also change this test. */
Gilles Peskine27d806f2019-03-01 18:02:53 +0100357 expected_result_for_int = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
358 }
359
360 p = input->x;
361 ret = mbedtls_asn1_get_int( &p, input->x + input->len, &val );
362 TEST_EQUAL( ret, expected_result_for_int );
363 if( ret == 0 )
364 {
365 TEST_EQUAL( val, expected_value );
366 TEST_ASSERT( p == input->x + input->len );
367 }
368
369#if defined(MBEDTLS_BIGNUM_C)
370 ret = mbedtls_mpi_read_string( &expected_mpi, 16, expected_hex );
371 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
372 if( ret == MBEDTLS_ERR_MPI_BAD_INPUT_DATA )
373 {
374 /* The data overflows the maximum MPI size. */
375 expected_result_for_mpi = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
376 }
377 p = input->x;
378 ret = mbedtls_asn1_get_mpi( &p, input->x + input->len, &actual_mpi );
379 TEST_EQUAL( ret, expected_result_for_mpi );
380 if( ret == 0 )
381 {
Gilles Peskine970dcbf2019-10-10 19:21:12 +0200382 if( expected_value >= 0 )
383 {
384 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &actual_mpi,
385 &expected_mpi ) == 0 );
386 }
387 else
388 {
389 /* The library ignores the sign bit in ASN.1 INTEGERs
390 * (which makes sense insofar as INTEGERs are sometimes
391 * abused as bit strings), so the result of parsing them
392 * is a positive integer such that expected_mpi +
393 * actual_mpi = 2^n where n is the length of the content
394 * of the INTEGER. (Leading ff octets don't matter for the
395 * expected value, but they matter for the actual value.)
396 * Test that we don't change from this behavior. If we
397 * decide to fix the library to change the behavior on
398 * negative INTEGERs, we'll fix this test code. */
399 unsigned char *q = input->x + 1;
400 size_t len;
401 TEST_ASSERT( mbedtls_asn1_get_len( &q, input->x + input->len,
402 &len ) == 0 );
403 TEST_ASSERT( mbedtls_mpi_lset( &complement, 1 ) == 0 );
404 TEST_ASSERT( mbedtls_mpi_shift_l( &complement, len * 8 ) == 0 );
405 TEST_ASSERT( mbedtls_mpi_add_mpi( &complement, &complement,
406 &expected_mpi ) == 0 );
407 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &complement,
408 &actual_mpi ) == 0 );
409 }
Gilles Peskine27d806f2019-03-01 18:02:53 +0100410 TEST_ASSERT( p == input->x + input->len );
411 }
412#endif
413
414exit:
415#if defined(MBEDTLS_BIGNUM_C)
416 mbedtls_mpi_free( &expected_mpi );
417 mbedtls_mpi_free( &actual_mpi );
Gilles Peskine970dcbf2019-10-10 19:21:12 +0200418 mbedtls_mpi_free( &complement );
Gilles Peskine27d806f2019-03-01 18:02:53 +0100419#endif
Gilles Peskine03c165e2019-10-10 19:15:18 +0200420 /*empty cleanup in some configurations*/ ;
Gilles Peskine27d806f2019-03-01 18:02:53 +0100421}
422/* END_CASE */
423
Mykhailo Sopiha6af7bf92019-10-31 15:55:16 +0200424/* BEGIN_CASE */
425void get_enum( const data_t *input,
426 const char *expected_hex, int expected_result )
427{
428 unsigned char *p;
429 long expected_value;
430 int expected_result_for_enum = expected_result;
431 int val;
432 int ret;
433
434 errno = 0;
435 expected_value = strtol( expected_hex, NULL, 16 );
436 if( expected_result == 0 &&
437 ( errno == ERANGE
438#if LONG_MAX > INT_MAX
439 || expected_value > INT_MAX || expected_value < INT_MIN
440#endif
441 ) )
442 {
443 /* The library returns the dubious error code INVALID_LENGTH
444 * for integers that are out of range. */
445 expected_result_for_enum = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
446 }
447 if( expected_result == 0 && expected_value < 0 )
448 {
449 /* The library does not support negative INTEGERs and
450 * returns the dubious error code INVALID_LENGTH.
451 * Test that we preserve the historical behavior. If we
452 * decide to change the behavior, we'll also change this test. */
453 expected_result_for_enum = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
454 }
455
456 p = input->x;
457 ret = mbedtls_asn1_get_enum( &p, input->x + input->len, &val );
458 TEST_EQUAL( ret, expected_result_for_enum );
459 if( ret == 0 )
460 {
461 TEST_EQUAL( val, expected_value );
462 TEST_ASSERT( p == input->x + input->len );
463 }
464}
465/* END_CASE */
466
Gilles Peskine27d806f2019-03-01 18:02:53 +0100467/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
468void get_mpi_too_large( )
469{
470 unsigned char *buf = NULL;
471 unsigned char *p;
472 mbedtls_mpi actual_mpi;
473 size_t too_many_octets =
474 MBEDTLS_MPI_MAX_LIMBS * sizeof(mbedtls_mpi_uint) + 1;
475 size_t size = too_many_octets + 6;
476
477 mbedtls_mpi_init( &actual_mpi );
478
479 ASSERT_ALLOC( buf, size );
480 buf[0] = 0x02; /* tag: INTEGER */
481 buf[1] = 0x84; /* 4-octet length */
482 buf[2] = ( too_many_octets >> 24 ) & 0xff;
483 buf[3] = ( too_many_octets >> 16 ) & 0xff;
484 buf[4] = ( too_many_octets >> 8 ) & 0xff;
485 buf[5] = too_many_octets & 0xff;
486 buf[6] = 0x01; /* most significant octet */
487
488 p = buf;
489 TEST_EQUAL( mbedtls_asn1_get_mpi( &p, buf + size, &actual_mpi ),
490 MBEDTLS_ERR_MPI_ALLOC_FAILED );
491
492exit:
493 mbedtls_mpi_free( &actual_mpi );
494 mbedtls_free( buf );
495}
496/* END_CASE */
497
498/* BEGIN_CASE */
499void get_bitstring( const data_t *input,
500 int expected_length, int expected_unused_bits,
501 int expected_result, int expected_result_null )
502{
503 mbedtls_asn1_bitstring bs = { 0xdead, 0x21, NULL };
504 unsigned char *p = input->x;
505
506 TEST_EQUAL( mbedtls_asn1_get_bitstring( &p, input->x + input->len, &bs ),
507 expected_result );
508 if( expected_result == 0 )
509 {
510 TEST_EQUAL( bs.len, (size_t) expected_length );
511 TEST_EQUAL( bs.unused_bits, expected_unused_bits );
512 TEST_ASSERT( bs.p != NULL );
513 TEST_EQUAL( bs.p - input->x + bs.len, input->len );
514 TEST_ASSERT( p == input->x + input->len );
515 }
516
517 p = input->x;
518 TEST_EQUAL( mbedtls_asn1_get_bitstring_null( &p, input->x + input->len,
519 &bs.len ),
520 expected_result_null );
521 if( expected_result_null == 0 )
522 {
523 TEST_EQUAL( bs.len, (size_t) expected_length );
524 if( expected_result == 0 )
525 TEST_ASSERT( p == input->x + input->len - bs.len );
526 }
527}
528/* END_CASE */
529
530/* BEGIN_CASE */
531void get_sequence_of( const data_t *input, int tag,
532 const char *description,
533 int expected_result )
534{
535 mbedtls_asn1_sequence head = { { 0, 0, NULL }, NULL };
536 mbedtls_asn1_sequence *cur, *next;
537 unsigned char *p = input->x;
538 const char *rest = description;
539 unsigned long n;
540
541 TEST_EQUAL( mbedtls_asn1_get_sequence_of( &p, input->x + input->len,
542 &head, tag ),
543 expected_result );
544 if( expected_result == 0 )
545 {
546 TEST_ASSERT( p == input->x + input->len );
547
548 if( ! *rest )
549 {
550 TEST_EQUAL( head.buf.tag, 0 );
551 TEST_ASSERT( head.buf.p == NULL );
552 TEST_EQUAL( head.buf.len, 0 );
553 TEST_ASSERT( head.next == NULL );
554 }
555 else
556 {
557 cur = &head;
558 while( *rest )
559 {
560 ++test_info.step;
561 TEST_ASSERT( cur != NULL );
562 TEST_EQUAL( cur->buf.tag, tag );
563 n = strtoul( rest, (char **) &rest, 0 );
564 TEST_EQUAL( n, (size_t)( cur->buf.p - input->x ) );
565 ++rest;
566 n = strtoul( rest, (char **) &rest, 0 );
567 TEST_EQUAL( n, cur->buf.len );
568 if( *rest )
569 ++rest;
570 cur = cur->next;
571 }
572 TEST_ASSERT( cur == NULL );
573 }
574 }
575
576exit:
577 cur = head.next;
578 while( cur != NULL )
579 {
580 next = cur->next;
581 mbedtls_free( cur );
582 cur = next;
583 }
584}
585/* END_CASE */
586
587/* BEGIN_CASE */
588void get_alg( const data_t *input,
589 int oid_offset, int oid_length,
590 int params_tag, int params_offset, int params_length,
591 int total_length,
592 int expected_result )
593{
594 mbedtls_asn1_buf oid = { -1, 0, NULL };
595 mbedtls_asn1_buf params = { -1, 0, NULL };
596 unsigned char *p = input->x;
597 int ret;
598
599 TEST_EQUAL( mbedtls_asn1_get_alg( &p, input->x + input->len,
600 &oid, &params ),
601 expected_result );
602 if( expected_result == 0 )
603 {
604 TEST_EQUAL( oid.tag, MBEDTLS_ASN1_OID );
605 TEST_EQUAL( oid.p - input->x, oid_offset );
606 TEST_EQUAL( oid.len, (size_t) oid_length );
607 TEST_EQUAL( params.tag, params_tag );
608 if( params_offset != 0 )
609 TEST_EQUAL( params.p - input->x, params_offset );
610 else
611 TEST_ASSERT( params.p == NULL );
612 TEST_EQUAL( params.len, (size_t) params_length );
613 TEST_EQUAL( p - input->x, total_length );
614 }
615
616 ret = mbedtls_asn1_get_alg_null( &p, input->x + input->len, &oid );
617 if( expected_result == 0 && params_offset == 0 )
618 {
619 TEST_EQUAL( oid.tag, MBEDTLS_ASN1_OID );
620 TEST_EQUAL( oid.p - input->x, oid_offset );
621 TEST_EQUAL( oid.len, (size_t) oid_length );
622 TEST_EQUAL( p - input->x, total_length );
623 }
624 else
625 TEST_ASSERT( ret != 0 );
626}
627/* END_CASE */
628
629/* BEGIN_CASE */
630void find_named_data( data_t *oid0, data_t *oid1, data_t *oid2, data_t *oid3,
631 data_t *needle, int from, int position )
632{
633 mbedtls_asn1_named_data nd[] ={
634 { {0x06, oid0->len, oid0->x}, {0, 0, NULL}, NULL, 0 },
635 { {0x06, oid1->len, oid1->x}, {0, 0, NULL}, NULL, 0 },
636 { {0x06, oid2->len, oid2->x}, {0, 0, NULL}, NULL, 0 },
637 { {0x06, oid3->len, oid3->x}, {0, 0, NULL}, NULL, 0 },
638 };
639 mbedtls_asn1_named_data *pointers[ARRAY_LENGTH( nd ) + 1];
640 size_t i;
641 mbedtls_asn1_named_data *found;
642
643 for( i = 0; i < ARRAY_LENGTH( nd ); i++ )
644 pointers[i] = &nd[i];
645 pointers[ARRAY_LENGTH( nd )] = NULL;
646 for( i = 0; i < ARRAY_LENGTH( nd ); i++ )
647 nd[i].next = pointers[i+1];
648
649 found = mbedtls_asn1_find_named_data( pointers[from],
650 (const char *) needle->x,
651 needle->len );
652 TEST_ASSERT( found == pointers[position] );
653}
654/* END_CASE */
655
656/* BEGIN_CASE */
657void free_named_data_null( )
658{
659 mbedtls_asn1_free_named_data( NULL );
660 goto exit; /* Silence unused label warning */
661}
662/* END_CASE */
663
664/* BEGIN_CASE */
665void free_named_data( int with_oid, int with_val, int with_next )
666{
667 mbedtls_asn1_named_data next =
668 { {0x06, 0, NULL}, {0, 0xcafe, NULL}, NULL, 0 };
669 mbedtls_asn1_named_data head =
670 { {0x06, 0, NULL}, {0, 0, NULL}, NULL, 0 };
671
672 if( with_oid )
673 ASSERT_ALLOC( head.oid.p, 1 );
674 if( with_val )
675 ASSERT_ALLOC( head.val.p, 1 );
676 if( with_next )
677 head.next = &next;
678
679 mbedtls_asn1_free_named_data( &head );
680 TEST_ASSERT( head.oid.p == NULL );
681 TEST_ASSERT( head.val.p == NULL );
682 TEST_ASSERT( head.next == NULL );
683 TEST_ASSERT( next.val.len == 0xcafe );
684
685exit:
686 mbedtls_free( head.oid.p );
687 mbedtls_free( head.val.p );
688}
689/* END_CASE */
690
691/* BEGIN_CASE */
692void free_named_data_list( int length )
693{
694 mbedtls_asn1_named_data *head = NULL;
695 int i;
696
697 for( i = 0; i < length; i++ )
698 {
699 mbedtls_asn1_named_data *new = NULL;
700 ASSERT_ALLOC( new, sizeof( mbedtls_asn1_named_data ) );
Gilles Peskine88f136f2019-09-20 21:06:27 +0200701 new->next = head;
Gilles Peskine27d806f2019-03-01 18:02:53 +0100702 head = new;
703 }
704
705 mbedtls_asn1_free_named_data_list( &head );
706 TEST_ASSERT( head == NULL );
707 /* Most of the point of the test is that it doesn't leak memory.
708 * So this test is only really useful under a memory leak detection
709 * framework. */
710exit:
711 mbedtls_asn1_free_named_data_list( &head );
712}
713/* END_CASE */