blob: f5ecd5515b78805a85d21c4f99efef16357d8017 [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 {
Gilles Peskine2cd8ecc2019-03-04 17:13:43 +0100133 ASSERT_ALLOC_WEAK( buf, buffer_size );
Gilles Peskine27d806f2019-03-01 18:02:53 +0100134 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:
Gilles Peskine27d806f2019-03-01 18:02:53 +0100162 mbedtls_free( buf );
163 return( 0 );
164}
165
166/* END_HEADER */
167
168/* BEGIN_DEPENDENCIES
169 * depends_on:MBEDTLS_ASN1_PARSE_C
170 * END_DEPENDENCIES
171 */
172
173/* BEGIN_CASE */
174void parse_prefixes( const data_t *input,
175 int actual_length_arg,
176 int last_result )
177{
178 size_t actual_length = actual_length_arg;
179 unsigned char *buf = NULL;
180 unsigned char *p = NULL;
181 size_t buffer_size;
182 int ret;
183
184 for( buffer_size = 1; buffer_size <= input->len; buffer_size++ )
185 {
186 test_set_step( buffer_size );
187 /* Allocate a new buffer of exactly the length to parse each time.
188 * This gives memory sanitizers a chance to catch buffer overreads. */
189 ASSERT_ALLOC( buf, buffer_size );
190 memcpy( buf, input->x, buffer_size );
191 p = buf;
192 ret = nested_parse( &p, buf + buffer_size );
193 if( ret == ERR_PARSE_INCONSISTENCY )
194 goto exit;
195 if( actual_length > 0 && buffer_size >= actual_length )
196 {
197 TEST_EQUAL( ret, last_result );
198 if( ret == 0 )
199 TEST_ASSERT( p == buf + actual_length );
200 }
201 else
202 {
203 TEST_EQUAL( ret, MBEDTLS_ERR_ASN1_OUT_OF_DATA );
204 }
205 mbedtls_free( buf );
206 buf = NULL;
207 }
208
209exit:
210 mbedtls_free( buf );
211}
212/* END_CASE */
213
214/* BEGIN_CASE */
215void get_len( const data_t *input, int actual_length_arg )
216{
217 size_t actual_length = actual_length_arg;
218 size_t buffer_size;
219
220 for( buffer_size = 1; buffer_size <= input->len + 1; buffer_size++ )
221 {
222 if( ! get_len_step( input, buffer_size, actual_length ) )
223 goto exit;
224 }
225 if( ! get_len_step( input, input->len + actual_length - 1, actual_length ) )
226 goto exit;
227 if( ! get_len_step( input, input->len + actual_length, actual_length ) )
228 goto exit;
229}
230/* END_CASE */
231
232/* BEGIN_CASE */
233void get_boolean( const data_t *input,
234 int expected_value, int expected_result )
235{
236 unsigned char *p = input->x;
237 int val;
238 int ret;
239 ret = mbedtls_asn1_get_bool( &p, input->x + input->len, &val );
240 TEST_EQUAL( ret, expected_result );
241 if( expected_result == 0 )
242 {
243 TEST_EQUAL( val, expected_value );
244 TEST_ASSERT( p == input->x + input->len );
245 }
246}
247/* END_CASE */
248
249/* BEGIN_CASE */
250void get_integer( const data_t *input,
251 const char *expected_hex, int expected_result )
252{
253 unsigned char *p;
254#if defined(MBEDTLS_BIGNUM_C)
255 mbedtls_mpi expected_mpi;
256 mbedtls_mpi actual_mpi;
257#endif
258 long expected_value;
259 int expected_result_for_int = expected_result;
260 int expected_result_for_mpi = expected_result;
261 int val;
262 int ret;
263
264#if defined(MBEDTLS_BIGNUM_C)
265 mbedtls_mpi_init( &expected_mpi );
266 mbedtls_mpi_init( &actual_mpi );
267#endif
268
269 errno = 0;
270 expected_value = strtol( expected_hex, NULL, 16 );
271 if( expected_result == 0 &&
272 ( errno == ERANGE
273#if LONG_MAX > INT_MAX
274 || expected_value > INT_MAX || expected_value < INT_MIN
275#endif
276 ) )
277 {
278 expected_result_for_int = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
279 }
280
281 p = input->x;
282 ret = mbedtls_asn1_get_int( &p, input->x + input->len, &val );
283 TEST_EQUAL( ret, expected_result_for_int );
284 if( ret == 0 )
285 {
286 TEST_EQUAL( val, expected_value );
287 TEST_ASSERT( p == input->x + input->len );
288 }
289
290#if defined(MBEDTLS_BIGNUM_C)
291 ret = mbedtls_mpi_read_string( &expected_mpi, 16, expected_hex );
292 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
293 if( ret == MBEDTLS_ERR_MPI_BAD_INPUT_DATA )
294 {
295 /* The data overflows the maximum MPI size. */
296 expected_result_for_mpi = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
297 }
298 p = input->x;
299 ret = mbedtls_asn1_get_mpi( &p, input->x + input->len, &actual_mpi );
300 TEST_EQUAL( ret, expected_result_for_mpi );
301 if( ret == 0 )
302 {
303 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &actual_mpi , &expected_mpi ) == 0 );
304 TEST_ASSERT( p == input->x + input->len );
305 }
306#endif
307
308exit:
309#if defined(MBEDTLS_BIGNUM_C)
310 mbedtls_mpi_free( &expected_mpi );
311 mbedtls_mpi_free( &actual_mpi );
312#endif
313}
314/* END_CASE */
315
316/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
317void get_mpi_too_large( )
318{
319 unsigned char *buf = NULL;
320 unsigned char *p;
321 mbedtls_mpi actual_mpi;
322 size_t too_many_octets =
323 MBEDTLS_MPI_MAX_LIMBS * sizeof(mbedtls_mpi_uint) + 1;
324 size_t size = too_many_octets + 6;
325
326 mbedtls_mpi_init( &actual_mpi );
327
328 ASSERT_ALLOC( buf, size );
329 buf[0] = 0x02; /* tag: INTEGER */
330 buf[1] = 0x84; /* 4-octet length */
331 buf[2] = ( too_many_octets >> 24 ) & 0xff;
332 buf[3] = ( too_many_octets >> 16 ) & 0xff;
333 buf[4] = ( too_many_octets >> 8 ) & 0xff;
334 buf[5] = too_many_octets & 0xff;
335 buf[6] = 0x01; /* most significant octet */
336
337 p = buf;
338 TEST_EQUAL( mbedtls_asn1_get_mpi( &p, buf + size, &actual_mpi ),
339 MBEDTLS_ERR_MPI_ALLOC_FAILED );
340
341exit:
342 mbedtls_mpi_free( &actual_mpi );
343 mbedtls_free( buf );
344}
345/* END_CASE */
346
347/* BEGIN_CASE */
348void get_bitstring( const data_t *input,
349 int expected_length, int expected_unused_bits,
350 int expected_result, int expected_result_null )
351{
352 mbedtls_asn1_bitstring bs = { 0xdead, 0x21, NULL };
353 unsigned char *p = input->x;
354
355 TEST_EQUAL( mbedtls_asn1_get_bitstring( &p, input->x + input->len, &bs ),
356 expected_result );
357 if( expected_result == 0 )
358 {
359 TEST_EQUAL( bs.len, (size_t) expected_length );
360 TEST_EQUAL( bs.unused_bits, expected_unused_bits );
361 TEST_ASSERT( bs.p != NULL );
362 TEST_EQUAL( bs.p - input->x + bs.len, input->len );
363 TEST_ASSERT( p == input->x + input->len );
364 }
365
366 p = input->x;
367 TEST_EQUAL( mbedtls_asn1_get_bitstring_null( &p, input->x + input->len,
368 &bs.len ),
369 expected_result_null );
370 if( expected_result_null == 0 )
371 {
372 TEST_EQUAL( bs.len, (size_t) expected_length );
373 if( expected_result == 0 )
374 TEST_ASSERT( p == input->x + input->len - bs.len );
375 }
376}
377/* END_CASE */
378
379/* BEGIN_CASE */
380void get_sequence_of( const data_t *input, int tag,
381 const char *description,
382 int expected_result )
383{
384 mbedtls_asn1_sequence head = { { 0, 0, NULL }, NULL };
385 mbedtls_asn1_sequence *cur, *next;
386 unsigned char *p = input->x;
387 const char *rest = description;
388 unsigned long n;
389
390 TEST_EQUAL( mbedtls_asn1_get_sequence_of( &p, input->x + input->len,
391 &head, tag ),
392 expected_result );
393 if( expected_result == 0 )
394 {
395 TEST_ASSERT( p == input->x + input->len );
396
397 if( ! *rest )
398 {
399 TEST_EQUAL( head.buf.tag, 0 );
400 TEST_ASSERT( head.buf.p == NULL );
401 TEST_EQUAL( head.buf.len, 0 );
402 TEST_ASSERT( head.next == NULL );
403 }
404 else
405 {
406 cur = &head;
407 while( *rest )
408 {
409 ++test_info.step;
410 TEST_ASSERT( cur != NULL );
411 TEST_EQUAL( cur->buf.tag, tag );
412 n = strtoul( rest, (char **) &rest, 0 );
413 TEST_EQUAL( n, (size_t)( cur->buf.p - input->x ) );
414 ++rest;
415 n = strtoul( rest, (char **) &rest, 0 );
416 TEST_EQUAL( n, cur->buf.len );
417 if( *rest )
418 ++rest;
419 cur = cur->next;
420 }
421 TEST_ASSERT( cur == NULL );
422 }
423 }
424
425exit:
426 cur = head.next;
427 while( cur != NULL )
428 {
429 next = cur->next;
430 mbedtls_free( cur );
431 cur = next;
432 }
433}
434/* END_CASE */
435
436/* BEGIN_CASE */
437void get_alg( const data_t *input,
438 int oid_offset, int oid_length,
439 int params_tag, int params_offset, int params_length,
440 int total_length,
441 int expected_result )
442{
443 mbedtls_asn1_buf oid = { -1, 0, NULL };
444 mbedtls_asn1_buf params = { -1, 0, NULL };
445 unsigned char *p = input->x;
446 int ret;
447
448 TEST_EQUAL( mbedtls_asn1_get_alg( &p, input->x + input->len,
449 &oid, &params ),
450 expected_result );
451 if( expected_result == 0 )
452 {
453 TEST_EQUAL( oid.tag, MBEDTLS_ASN1_OID );
454 TEST_EQUAL( oid.p - input->x, oid_offset );
455 TEST_EQUAL( oid.len, (size_t) oid_length );
456 TEST_EQUAL( params.tag, params_tag );
457 if( params_offset != 0 )
458 TEST_EQUAL( params.p - input->x, params_offset );
459 else
460 TEST_ASSERT( params.p == NULL );
461 TEST_EQUAL( params.len, (size_t) params_length );
462 TEST_EQUAL( p - input->x, total_length );
463 }
464
465 ret = mbedtls_asn1_get_alg_null( &p, input->x + input->len, &oid );
466 if( expected_result == 0 && params_offset == 0 )
467 {
468 TEST_EQUAL( oid.tag, MBEDTLS_ASN1_OID );
469 TEST_EQUAL( oid.p - input->x, oid_offset );
470 TEST_EQUAL( oid.len, (size_t) oid_length );
471 TEST_EQUAL( p - input->x, total_length );
472 }
473 else
474 TEST_ASSERT( ret != 0 );
475}
476/* END_CASE */
477
478/* BEGIN_CASE */
479void find_named_data( data_t *oid0, data_t *oid1, data_t *oid2, data_t *oid3,
480 data_t *needle, int from, int position )
481{
482 mbedtls_asn1_named_data nd[] ={
483 { {0x06, oid0->len, oid0->x}, {0, 0, NULL}, NULL, 0 },
484 { {0x06, oid1->len, oid1->x}, {0, 0, NULL}, NULL, 0 },
485 { {0x06, oid2->len, oid2->x}, {0, 0, NULL}, NULL, 0 },
486 { {0x06, oid3->len, oid3->x}, {0, 0, NULL}, NULL, 0 },
487 };
488 mbedtls_asn1_named_data *pointers[ARRAY_LENGTH( nd ) + 1];
489 size_t i;
490 mbedtls_asn1_named_data *found;
491
492 for( i = 0; i < ARRAY_LENGTH( nd ); i++ )
493 pointers[i] = &nd[i];
494 pointers[ARRAY_LENGTH( nd )] = NULL;
495 for( i = 0; i < ARRAY_LENGTH( nd ); i++ )
496 nd[i].next = pointers[i+1];
497
498 found = mbedtls_asn1_find_named_data( pointers[from],
499 (const char *) needle->x,
500 needle->len );
501 TEST_ASSERT( found == pointers[position] );
502}
503/* END_CASE */
504
505/* BEGIN_CASE */
506void free_named_data_null( )
507{
508 mbedtls_asn1_free_named_data( NULL );
509 goto exit; /* Silence unused label warning */
510}
511/* END_CASE */
512
513/* BEGIN_CASE */
514void free_named_data( int with_oid, int with_val, int with_next )
515{
516 mbedtls_asn1_named_data next =
517 { {0x06, 0, NULL}, {0, 0xcafe, NULL}, NULL, 0 };
518 mbedtls_asn1_named_data head =
519 { {0x06, 0, NULL}, {0, 0, NULL}, NULL, 0 };
520
521 if( with_oid )
522 ASSERT_ALLOC( head.oid.p, 1 );
523 if( with_val )
524 ASSERT_ALLOC( head.val.p, 1 );
525 if( with_next )
526 head.next = &next;
527
528 mbedtls_asn1_free_named_data( &head );
529 TEST_ASSERT( head.oid.p == NULL );
530 TEST_ASSERT( head.val.p == NULL );
531 TEST_ASSERT( head.next == NULL );
532 TEST_ASSERT( next.val.len == 0xcafe );
533
534exit:
535 mbedtls_free( head.oid.p );
536 mbedtls_free( head.val.p );
537}
538/* END_CASE */
539
540/* BEGIN_CASE */
541void free_named_data_list( int length )
542{
543 mbedtls_asn1_named_data *head = NULL;
544 int i;
545
546 for( i = 0; i < length; i++ )
547 {
548 mbedtls_asn1_named_data *new = NULL;
549 ASSERT_ALLOC( new, sizeof( mbedtls_asn1_named_data ) );
550 head->next = new;
551 head = new;
552 }
553
554 mbedtls_asn1_free_named_data_list( &head );
555 TEST_ASSERT( head == NULL );
556 /* Most of the point of the test is that it doesn't leak memory.
557 * So this test is only really useful under a memory leak detection
558 * framework. */
559exit:
560 mbedtls_asn1_free_named_data_list( &head );
561}
562/* END_CASE */