blob: 9e9f509499ecc16f55bbd2e4f609bc38de45f989 [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
12#define ERR_PARSE_INCONSISTENCY INT_MAX
13
14static int nested_parse( unsigned char **const p,
15 const unsigned char *const end )
16{
17 int ret;
18 size_t len = 0;
19 size_t len2 = 0;
20 unsigned char *const start = *p;
21 unsigned char *content_start;
22 unsigned char tag;
23
24 /* First get the length, skipping over the tag. */
25 content_start = start + 1;
26 ret = mbedtls_asn1_get_len( &content_start, end, &len );
27 TEST_ASSERT( content_start <= end );
28 if( ret != 0 )
29 return( ret );
30
31 /* Since we have a valid element start (tag and length), retrieve and
32 * check the tag. */
33 tag = start[0];
34 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len2, tag ^ 1 ),
35 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
36 *p = start;
37 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len2, tag ), 0 );
38 TEST_EQUAL( len, len2 );
39 TEST_ASSERT( *p == content_start );
40 *p = content_start;
41
42 switch( tag & 0x1f )
43 {
44 case MBEDTLS_ASN1_BOOLEAN:
45 {
46 int val = -257;
47 *p = start;
48 ret = mbedtls_asn1_get_bool( p, end, &val );
49 if( ret == 0 )
50 TEST_ASSERT( val == 0 || val == 1 );
51 break;
52 }
53
54 case MBEDTLS_ASN1_INTEGER:
55 {
56#if defined(MBEDTLS_BIGNUM_C)
57 mbedtls_mpi mpi;
58 mbedtls_mpi_init( &mpi );
59 *p = start;
60 ret = mbedtls_asn1_get_mpi( p, end, &mpi );
61 mbedtls_mpi_free( &mpi );
62#endif
63 /* If we're sure that the number fits in an int, also
64 * call mbedtls_asn1_get_int(). */
65 if( ret == 0 && len < sizeof( int ) )
66 {
67 int val = -257;
68 unsigned char *q = start;
69 ret = mbedtls_asn1_get_int( &q, end, &val );
70 TEST_ASSERT( *p == q );
71 }
72 break;
73 }
74
75 case MBEDTLS_ASN1_BIT_STRING:
76 {
77 mbedtls_asn1_bitstring bs;
78 *p = start;
79 ret = mbedtls_asn1_get_bitstring( p, end, &bs );
80 break;
81 }
82
83 case MBEDTLS_ASN1_SEQUENCE:
84 {
85 while( *p <= end && *p < content_start + len && ret == 0 )
86 ret = nested_parse( p, content_start + len );
87 break;
88 }
89
90 case MBEDTLS_ASN1_OCTET_STRING:
91 case MBEDTLS_ASN1_NULL:
92 case MBEDTLS_ASN1_OID:
93 case MBEDTLS_ASN1_UTF8_STRING:
94 case MBEDTLS_ASN1_SET:
95 case MBEDTLS_ASN1_PRINTABLE_STRING:
96 case MBEDTLS_ASN1_T61_STRING:
97 case MBEDTLS_ASN1_IA5_STRING:
98 case MBEDTLS_ASN1_UTC_TIME:
99 case MBEDTLS_ASN1_GENERALIZED_TIME:
100 case MBEDTLS_ASN1_UNIVERSAL_STRING:
101 case MBEDTLS_ASN1_BMP_STRING:
102 default:
103 /* No further testing implemented for this tag. */
104 *p += len;
105 return( 0 );
106 }
107
108 TEST_ASSERT( *p <= end );
109 return( ret );
110
111exit:
112 return( ERR_PARSE_INCONSISTENCY );
113}
114
115int get_len_step( const data_t *input, size_t buffer_size,
116 size_t actual_length )
117{
118 unsigned char *buf = NULL;
119 unsigned char *p = NULL;
120 size_t parsed_length;
121 int ret;
122
123 test_set_step( buffer_size );
124 /* Allocate a new buffer of exactly the length to parse each time.
125 * This gives memory sanitizers a chance to catch buffer overreads. */
126 if( buffer_size == 0 )
127 {
128 ASSERT_ALLOC( buf, 1 );
129 p = buf + 1;
130 }
131 else
132 {
133 ASSERT_ALLOC( buf, buffer_size );
134 if( buffer_size > input->len )
135 {
136 memcpy( buf, input->x, input->len );
137 memset( buf + input->len, 'A', buffer_size - input->len );
138 }
139 else
140 {
141 memcpy( buf, input->x, buffer_size );
142 }
143 p = buf;
144 }
145
146 ret = mbedtls_asn1_get_len( &p, buf + buffer_size, &parsed_length );
147
148 if( buffer_size >= input->len + actual_length )
149 {
150 TEST_EQUAL( ret, 0 );
151 TEST_ASSERT( p == buf + input->len );
152 TEST_EQUAL( parsed_length, actual_length );
153 }
154 else
155 {
156 TEST_EQUAL( ret, MBEDTLS_ERR_ASN1_OUT_OF_DATA );
157 }
158 mbedtls_free( buf );
159 return( 1 );
160
161exit:
162 /* It may be impossible to allocate large lengths on embedded platforms.
163 * Pass in this case (though it would be better to mark the test
164 * as skipped). */
165 if( buf == NULL )
166 return( 1 );
167
168 mbedtls_free( buf );
169 return( 0 );
170}
171
172/* END_HEADER */
173
174/* BEGIN_DEPENDENCIES
175 * depends_on:MBEDTLS_ASN1_PARSE_C
176 * END_DEPENDENCIES
177 */
178
179/* BEGIN_CASE */
180void parse_prefixes( const data_t *input,
181 int actual_length_arg,
182 int last_result )
183{
184 size_t actual_length = actual_length_arg;
185 unsigned char *buf = NULL;
186 unsigned char *p = NULL;
187 size_t buffer_size;
188 int ret;
189
190 for( buffer_size = 1; buffer_size <= input->len; buffer_size++ )
191 {
192 test_set_step( buffer_size );
193 /* Allocate a new buffer of exactly the length to parse each time.
194 * This gives memory sanitizers a chance to catch buffer overreads. */
195 ASSERT_ALLOC( buf, buffer_size );
196 memcpy( buf, input->x, buffer_size );
197 p = buf;
198 ret = nested_parse( &p, buf + buffer_size );
199 if( ret == ERR_PARSE_INCONSISTENCY )
200 goto exit;
201 if( actual_length > 0 && buffer_size >= actual_length )
202 {
203 TEST_EQUAL( ret, last_result );
204 if( ret == 0 )
205 TEST_ASSERT( p == buf + actual_length );
206 }
207 else
208 {
209 TEST_EQUAL( ret, MBEDTLS_ERR_ASN1_OUT_OF_DATA );
210 }
211 mbedtls_free( buf );
212 buf = NULL;
213 }
214
215exit:
216 mbedtls_free( buf );
217}
218/* END_CASE */
219
220/* BEGIN_CASE */
221void get_len( const data_t *input, int actual_length_arg )
222{
223 size_t actual_length = actual_length_arg;
224 size_t buffer_size;
225
226 for( buffer_size = 1; buffer_size <= input->len + 1; buffer_size++ )
227 {
228 if( ! get_len_step( input, buffer_size, actual_length ) )
229 goto exit;
230 }
231 if( ! get_len_step( input, input->len + actual_length - 1, actual_length ) )
232 goto exit;
233 if( ! get_len_step( input, input->len + actual_length, actual_length ) )
234 goto exit;
235}
236/* END_CASE */
237
238/* BEGIN_CASE */
239void get_boolean( const data_t *input,
240 int expected_value, int expected_result )
241{
242 unsigned char *p = input->x;
243 int val;
244 int ret;
245 ret = mbedtls_asn1_get_bool( &p, input->x + input->len, &val );
246 TEST_EQUAL( ret, expected_result );
247 if( expected_result == 0 )
248 {
249 TEST_EQUAL( val, expected_value );
250 TEST_ASSERT( p == input->x + input->len );
251 }
252}
253/* END_CASE */
254
255/* BEGIN_CASE */
256void get_integer( const data_t *input,
257 const char *expected_hex, int expected_result )
258{
259 unsigned char *p;
260#if defined(MBEDTLS_BIGNUM_C)
261 mbedtls_mpi expected_mpi;
262 mbedtls_mpi actual_mpi;
263#endif
264 long expected_value;
265 int expected_result_for_int = expected_result;
266 int expected_result_for_mpi = expected_result;
267 int val;
268 int ret;
269
270#if defined(MBEDTLS_BIGNUM_C)
271 mbedtls_mpi_init( &expected_mpi );
272 mbedtls_mpi_init( &actual_mpi );
273#endif
274
275 errno = 0;
276 expected_value = strtol( expected_hex, NULL, 16 );
277 if( expected_result == 0 &&
278 ( errno == ERANGE
279#if LONG_MAX > INT_MAX
280 || expected_value > INT_MAX || expected_value < INT_MIN
281#endif
282 ) )
283 {
284 expected_result_for_int = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
285 }
286
287 p = input->x;
288 ret = mbedtls_asn1_get_int( &p, input->x + input->len, &val );
289 TEST_EQUAL( ret, expected_result_for_int );
290 if( ret == 0 )
291 {
292 TEST_EQUAL( val, expected_value );
293 TEST_ASSERT( p == input->x + input->len );
294 }
295
296#if defined(MBEDTLS_BIGNUM_C)
297 ret = mbedtls_mpi_read_string( &expected_mpi, 16, expected_hex );
298 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
299 if( ret == MBEDTLS_ERR_MPI_BAD_INPUT_DATA )
300 {
301 /* The data overflows the maximum MPI size. */
302 expected_result_for_mpi = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
303 }
304 p = input->x;
305 ret = mbedtls_asn1_get_mpi( &p, input->x + input->len, &actual_mpi );
306 TEST_EQUAL( ret, expected_result_for_mpi );
307 if( ret == 0 )
308 {
309 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &actual_mpi , &expected_mpi ) == 0 );
310 TEST_ASSERT( p == input->x + input->len );
311 }
312#endif
313
314exit:
315#if defined(MBEDTLS_BIGNUM_C)
316 mbedtls_mpi_free( &expected_mpi );
317 mbedtls_mpi_free( &actual_mpi );
318#endif
319}
320/* END_CASE */
321
322/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
323void get_mpi_too_large( )
324{
325 unsigned char *buf = NULL;
326 unsigned char *p;
327 mbedtls_mpi actual_mpi;
328 size_t too_many_octets =
329 MBEDTLS_MPI_MAX_LIMBS * sizeof(mbedtls_mpi_uint) + 1;
330 size_t size = too_many_octets + 6;
331
332 mbedtls_mpi_init( &actual_mpi );
333
334 ASSERT_ALLOC( buf, size );
335 buf[0] = 0x02; /* tag: INTEGER */
336 buf[1] = 0x84; /* 4-octet length */
337 buf[2] = ( too_many_octets >> 24 ) & 0xff;
338 buf[3] = ( too_many_octets >> 16 ) & 0xff;
339 buf[4] = ( too_many_octets >> 8 ) & 0xff;
340 buf[5] = too_many_octets & 0xff;
341 buf[6] = 0x01; /* most significant octet */
342
343 p = buf;
344 TEST_EQUAL( mbedtls_asn1_get_mpi( &p, buf + size, &actual_mpi ),
345 MBEDTLS_ERR_MPI_ALLOC_FAILED );
346
347exit:
348 mbedtls_mpi_free( &actual_mpi );
349 mbedtls_free( buf );
350}
351/* END_CASE */
352
353/* BEGIN_CASE */
354void get_bitstring( const data_t *input,
355 int expected_length, int expected_unused_bits,
356 int expected_result, int expected_result_null )
357{
358 mbedtls_asn1_bitstring bs = { 0xdead, 0x21, NULL };
359 unsigned char *p = input->x;
360
361 TEST_EQUAL( mbedtls_asn1_get_bitstring( &p, input->x + input->len, &bs ),
362 expected_result );
363 if( expected_result == 0 )
364 {
365 TEST_EQUAL( bs.len, (size_t) expected_length );
366 TEST_EQUAL( bs.unused_bits, expected_unused_bits );
367 TEST_ASSERT( bs.p != NULL );
368 TEST_EQUAL( bs.p - input->x + bs.len, input->len );
369 TEST_ASSERT( p == input->x + input->len );
370 }
371
372 p = input->x;
373 TEST_EQUAL( mbedtls_asn1_get_bitstring_null( &p, input->x + input->len,
374 &bs.len ),
375 expected_result_null );
376 if( expected_result_null == 0 )
377 {
378 TEST_EQUAL( bs.len, (size_t) expected_length );
379 if( expected_result == 0 )
380 TEST_ASSERT( p == input->x + input->len - bs.len );
381 }
382}
383/* END_CASE */
384
385/* BEGIN_CASE */
386void get_sequence_of( const data_t *input, int tag,
387 const char *description,
388 int expected_result )
389{
390 mbedtls_asn1_sequence head = { { 0, 0, NULL }, NULL };
391 mbedtls_asn1_sequence *cur, *next;
392 unsigned char *p = input->x;
393 const char *rest = description;
394 unsigned long n;
395
396 TEST_EQUAL( mbedtls_asn1_get_sequence_of( &p, input->x + input->len,
397 &head, tag ),
398 expected_result );
399 if( expected_result == 0 )
400 {
401 TEST_ASSERT( p == input->x + input->len );
402
403 if( ! *rest )
404 {
405 TEST_EQUAL( head.buf.tag, 0 );
406 TEST_ASSERT( head.buf.p == NULL );
407 TEST_EQUAL( head.buf.len, 0 );
408 TEST_ASSERT( head.next == NULL );
409 }
410 else
411 {
412 cur = &head;
413 while( *rest )
414 {
415 ++test_info.step;
416 TEST_ASSERT( cur != NULL );
417 TEST_EQUAL( cur->buf.tag, tag );
418 n = strtoul( rest, (char **) &rest, 0 );
419 TEST_EQUAL( n, (size_t)( cur->buf.p - input->x ) );
420 ++rest;
421 n = strtoul( rest, (char **) &rest, 0 );
422 TEST_EQUAL( n, cur->buf.len );
423 if( *rest )
424 ++rest;
425 cur = cur->next;
426 }
427 TEST_ASSERT( cur == NULL );
428 }
429 }
430
431exit:
432 cur = head.next;
433 while( cur != NULL )
434 {
435 next = cur->next;
436 mbedtls_free( cur );
437 cur = next;
438 }
439}
440/* END_CASE */
441
442/* BEGIN_CASE */
443void get_alg( const data_t *input,
444 int oid_offset, int oid_length,
445 int params_tag, int params_offset, int params_length,
446 int total_length,
447 int expected_result )
448{
449 mbedtls_asn1_buf oid = { -1, 0, NULL };
450 mbedtls_asn1_buf params = { -1, 0, NULL };
451 unsigned char *p = input->x;
452 int ret;
453
454 TEST_EQUAL( mbedtls_asn1_get_alg( &p, input->x + input->len,
455 &oid, &params ),
456 expected_result );
457 if( expected_result == 0 )
458 {
459 TEST_EQUAL( oid.tag, MBEDTLS_ASN1_OID );
460 TEST_EQUAL( oid.p - input->x, oid_offset );
461 TEST_EQUAL( oid.len, (size_t) oid_length );
462 TEST_EQUAL( params.tag, params_tag );
463 if( params_offset != 0 )
464 TEST_EQUAL( params.p - input->x, params_offset );
465 else
466 TEST_ASSERT( params.p == NULL );
467 TEST_EQUAL( params.len, (size_t) params_length );
468 TEST_EQUAL( p - input->x, total_length );
469 }
470
471 ret = mbedtls_asn1_get_alg_null( &p, input->x + input->len, &oid );
472 if( expected_result == 0 && params_offset == 0 )
473 {
474 TEST_EQUAL( oid.tag, MBEDTLS_ASN1_OID );
475 TEST_EQUAL( oid.p - input->x, oid_offset );
476 TEST_EQUAL( oid.len, (size_t) oid_length );
477 TEST_EQUAL( p - input->x, total_length );
478 }
479 else
480 TEST_ASSERT( ret != 0 );
481}
482/* END_CASE */
483
484/* BEGIN_CASE */
485void find_named_data( data_t *oid0, data_t *oid1, data_t *oid2, data_t *oid3,
486 data_t *needle, int from, int position )
487{
488 mbedtls_asn1_named_data nd[] ={
489 { {0x06, oid0->len, oid0->x}, {0, 0, NULL}, NULL, 0 },
490 { {0x06, oid1->len, oid1->x}, {0, 0, NULL}, NULL, 0 },
491 { {0x06, oid2->len, oid2->x}, {0, 0, NULL}, NULL, 0 },
492 { {0x06, oid3->len, oid3->x}, {0, 0, NULL}, NULL, 0 },
493 };
494 mbedtls_asn1_named_data *pointers[ARRAY_LENGTH( nd ) + 1];
495 size_t i;
496 mbedtls_asn1_named_data *found;
497
498 for( i = 0; i < ARRAY_LENGTH( nd ); i++ )
499 pointers[i] = &nd[i];
500 pointers[ARRAY_LENGTH( nd )] = NULL;
501 for( i = 0; i < ARRAY_LENGTH( nd ); i++ )
502 nd[i].next = pointers[i+1];
503
504 found = mbedtls_asn1_find_named_data( pointers[from],
505 (const char *) needle->x,
506 needle->len );
507 TEST_ASSERT( found == pointers[position] );
508}
509/* END_CASE */
510
511/* BEGIN_CASE */
512void free_named_data_null( )
513{
514 mbedtls_asn1_free_named_data( NULL );
515 goto exit; /* Silence unused label warning */
516}
517/* END_CASE */
518
519/* BEGIN_CASE */
520void free_named_data( int with_oid, int with_val, int with_next )
521{
522 mbedtls_asn1_named_data next =
523 { {0x06, 0, NULL}, {0, 0xcafe, NULL}, NULL, 0 };
524 mbedtls_asn1_named_data head =
525 { {0x06, 0, NULL}, {0, 0, NULL}, NULL, 0 };
526
527 if( with_oid )
528 ASSERT_ALLOC( head.oid.p, 1 );
529 if( with_val )
530 ASSERT_ALLOC( head.val.p, 1 );
531 if( with_next )
532 head.next = &next;
533
534 mbedtls_asn1_free_named_data( &head );
535 TEST_ASSERT( head.oid.p == NULL );
536 TEST_ASSERT( head.val.p == NULL );
537 TEST_ASSERT( head.next == NULL );
538 TEST_ASSERT( next.val.len == 0xcafe );
539
540exit:
541 mbedtls_free( head.oid.p );
542 mbedtls_free( head.val.p );
543}
544/* END_CASE */
545
546/* BEGIN_CASE */
547void free_named_data_list( int length )
548{
549 mbedtls_asn1_named_data *head = NULL;
550 int i;
551
552 for( i = 0; i < length; i++ )
553 {
554 mbedtls_asn1_named_data *new = NULL;
555 ASSERT_ALLOC( new, sizeof( mbedtls_asn1_named_data ) );
556 head->next = new;
557 head = new;
558 }
559
560 mbedtls_asn1_free_named_data_list( &head );
561 TEST_ASSERT( head == NULL );
562 /* Most of the point of the test is that it doesn't leak memory.
563 * So this test is only really useful under a memory leak detection
564 * framework. */
565exit:
566 mbedtls_asn1_free_named_data_list( &head );
567}
568/* END_CASE */