| Gilles Peskine | 27d806f | 2019-03-01 18:02:53 +0100 | [diff] [blame] | 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 | #define ERR_PARSE_INCONSISTENCY INT_MAX | 
|  | 13 |  | 
|  | 14 | static 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 ); | 
| Gilles Peskine | 03c165e | 2019-10-10 19:15:18 +0200 | [diff] [blame] | 62 | #else | 
|  | 63 | *p = start + 1; | 
|  | 64 | ret = mbedtls_asn1_get_len( p, end, &len ); | 
|  | 65 | *p += len; | 
| Gilles Peskine | 27d806f | 2019-03-01 18:02:53 +0100 | [diff] [blame] | 66 | #endif | 
|  | 67 | /* If we're sure that the number fits in an int, also | 
|  | 68 | * call mbedtls_asn1_get_int(). */ | 
|  | 69 | if( ret == 0 && len < sizeof( int ) ) | 
|  | 70 | { | 
|  | 71 | int val = -257; | 
|  | 72 | unsigned char *q = start; | 
|  | 73 | ret = mbedtls_asn1_get_int( &q, end, &val ); | 
|  | 74 | TEST_ASSERT( *p == q ); | 
|  | 75 | } | 
|  | 76 | break; | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | case MBEDTLS_ASN1_BIT_STRING: | 
|  | 80 | { | 
|  | 81 | mbedtls_asn1_bitstring bs; | 
|  | 82 | *p = start; | 
|  | 83 | ret = mbedtls_asn1_get_bitstring( p, end, &bs ); | 
|  | 84 | break; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | case MBEDTLS_ASN1_SEQUENCE: | 
|  | 88 | { | 
|  | 89 | while( *p <= end && *p < content_start + len && ret == 0 ) | 
|  | 90 | ret = nested_parse( p, content_start + len ); | 
|  | 91 | break; | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | case MBEDTLS_ASN1_OCTET_STRING: | 
|  | 95 | case MBEDTLS_ASN1_NULL: | 
|  | 96 | case MBEDTLS_ASN1_OID: | 
|  | 97 | case MBEDTLS_ASN1_UTF8_STRING: | 
|  | 98 | case MBEDTLS_ASN1_SET: | 
|  | 99 | case MBEDTLS_ASN1_PRINTABLE_STRING: | 
|  | 100 | case MBEDTLS_ASN1_T61_STRING: | 
|  | 101 | case MBEDTLS_ASN1_IA5_STRING: | 
|  | 102 | case MBEDTLS_ASN1_UTC_TIME: | 
|  | 103 | case MBEDTLS_ASN1_GENERALIZED_TIME: | 
|  | 104 | case MBEDTLS_ASN1_UNIVERSAL_STRING: | 
|  | 105 | case MBEDTLS_ASN1_BMP_STRING: | 
|  | 106 | default: | 
|  | 107 | /* No further testing implemented for this tag. */ | 
|  | 108 | *p += len; | 
|  | 109 | return( 0 ); | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | TEST_ASSERT( *p <= end ); | 
|  | 113 | return( ret ); | 
|  | 114 |  | 
|  | 115 | exit: | 
|  | 116 | return( ERR_PARSE_INCONSISTENCY ); | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | int get_len_step( const data_t *input, size_t buffer_size, | 
|  | 120 | size_t actual_length ) | 
|  | 121 | { | 
|  | 122 | unsigned char *buf = NULL; | 
|  | 123 | unsigned char *p = NULL; | 
|  | 124 | size_t parsed_length; | 
|  | 125 | int ret; | 
|  | 126 |  | 
|  | 127 | test_set_step( buffer_size ); | 
|  | 128 | /* Allocate a new buffer of exactly the length to parse each time. | 
|  | 129 | * This gives memory sanitizers a chance to catch buffer overreads. */ | 
|  | 130 | if( buffer_size == 0 ) | 
|  | 131 | { | 
|  | 132 | ASSERT_ALLOC( buf, 1 ); | 
|  | 133 | p = buf + 1; | 
|  | 134 | } | 
|  | 135 | else | 
|  | 136 | { | 
| Gilles Peskine | 2cd8ecc | 2019-03-04 17:13:43 +0100 | [diff] [blame] | 137 | ASSERT_ALLOC_WEAK( buf, buffer_size ); | 
| Gilles Peskine | 27d806f | 2019-03-01 18:02:53 +0100 | [diff] [blame] | 138 | if( buffer_size > input->len ) | 
|  | 139 | { | 
|  | 140 | memcpy( buf, input->x, input->len ); | 
|  | 141 | memset( buf + input->len, 'A', buffer_size - input->len ); | 
|  | 142 | } | 
|  | 143 | else | 
|  | 144 | { | 
|  | 145 | memcpy( buf, input->x, buffer_size ); | 
|  | 146 | } | 
|  | 147 | p = buf; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | ret = mbedtls_asn1_get_len( &p, buf + buffer_size, &parsed_length ); | 
|  | 151 |  | 
|  | 152 | if( buffer_size >= input->len + actual_length ) | 
|  | 153 | { | 
|  | 154 | TEST_EQUAL( ret, 0 ); | 
|  | 155 | TEST_ASSERT( p == buf + input->len ); | 
|  | 156 | TEST_EQUAL( parsed_length, actual_length ); | 
|  | 157 | } | 
|  | 158 | else | 
|  | 159 | { | 
|  | 160 | TEST_EQUAL( ret, MBEDTLS_ERR_ASN1_OUT_OF_DATA ); | 
|  | 161 | } | 
|  | 162 | mbedtls_free( buf ); | 
|  | 163 | return( 1 ); | 
|  | 164 |  | 
|  | 165 | exit: | 
| Gilles Peskine | 27d806f | 2019-03-01 18:02:53 +0100 | [diff] [blame] | 166 | mbedtls_free( buf ); | 
|  | 167 | return( 0 ); | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 | /* END_HEADER */ | 
|  | 171 |  | 
|  | 172 | /* BEGIN_DEPENDENCIES | 
|  | 173 | * depends_on:MBEDTLS_ASN1_PARSE_C | 
|  | 174 | * END_DEPENDENCIES | 
|  | 175 | */ | 
|  | 176 |  | 
|  | 177 | /* BEGIN_CASE */ | 
|  | 178 | void parse_prefixes( const data_t *input, | 
|  | 179 | int actual_length_arg, | 
|  | 180 | int last_result ) | 
|  | 181 | { | 
|  | 182 | size_t actual_length = actual_length_arg; | 
|  | 183 | unsigned char *buf = NULL; | 
|  | 184 | unsigned char *p = NULL; | 
|  | 185 | size_t buffer_size; | 
|  | 186 | int ret; | 
|  | 187 |  | 
|  | 188 | for( buffer_size = 1; buffer_size <= input->len; buffer_size++ ) | 
|  | 189 | { | 
|  | 190 | test_set_step( buffer_size ); | 
|  | 191 | /* Allocate a new buffer of exactly the length to parse each time. | 
|  | 192 | * This gives memory sanitizers a chance to catch buffer overreads. */ | 
|  | 193 | ASSERT_ALLOC( buf, buffer_size ); | 
|  | 194 | memcpy( buf, input->x, buffer_size ); | 
|  | 195 | p = buf; | 
|  | 196 | ret = nested_parse( &p, buf + buffer_size ); | 
|  | 197 | if( ret == ERR_PARSE_INCONSISTENCY ) | 
|  | 198 | goto exit; | 
|  | 199 | if( actual_length > 0 && buffer_size >= actual_length ) | 
|  | 200 | { | 
|  | 201 | TEST_EQUAL( ret, last_result ); | 
|  | 202 | if( ret == 0 ) | 
|  | 203 | TEST_ASSERT( p == buf + actual_length ); | 
|  | 204 | } | 
|  | 205 | else | 
|  | 206 | { | 
|  | 207 | TEST_EQUAL( ret, MBEDTLS_ERR_ASN1_OUT_OF_DATA ); | 
|  | 208 | } | 
|  | 209 | mbedtls_free( buf ); | 
|  | 210 | buf = NULL; | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | exit: | 
|  | 214 | mbedtls_free( buf ); | 
|  | 215 | } | 
|  | 216 | /* END_CASE */ | 
|  | 217 |  | 
|  | 218 | /* BEGIN_CASE */ | 
|  | 219 | void get_len( const data_t *input, int actual_length_arg ) | 
|  | 220 | { | 
|  | 221 | size_t actual_length = actual_length_arg; | 
|  | 222 | size_t buffer_size; | 
|  | 223 |  | 
|  | 224 | for( buffer_size = 1; buffer_size <= input->len + 1; buffer_size++ ) | 
|  | 225 | { | 
|  | 226 | if( ! get_len_step( input, buffer_size, actual_length ) ) | 
|  | 227 | goto exit; | 
|  | 228 | } | 
|  | 229 | if( ! get_len_step( input, input->len + actual_length - 1, actual_length ) ) | 
|  | 230 | goto exit; | 
|  | 231 | if( ! get_len_step( input, input->len + actual_length, actual_length ) ) | 
|  | 232 | goto exit; | 
|  | 233 | } | 
|  | 234 | /* END_CASE */ | 
|  | 235 |  | 
|  | 236 | /* BEGIN_CASE */ | 
|  | 237 | void get_boolean( const data_t *input, | 
|  | 238 | int expected_value, int expected_result ) | 
|  | 239 | { | 
|  | 240 | unsigned char *p = input->x; | 
|  | 241 | int val; | 
|  | 242 | int ret; | 
|  | 243 | ret = mbedtls_asn1_get_bool( &p, input->x + input->len, &val ); | 
|  | 244 | TEST_EQUAL( ret, expected_result ); | 
|  | 245 | if( expected_result == 0 ) | 
|  | 246 | { | 
|  | 247 | TEST_EQUAL( val, expected_value ); | 
|  | 248 | TEST_ASSERT( p == input->x + input->len ); | 
|  | 249 | } | 
|  | 250 | } | 
|  | 251 | /* END_CASE */ | 
|  | 252 |  | 
|  | 253 | /* BEGIN_CASE */ | 
| Gilles Peskine | 321adb2 | 2019-10-10 19:18:21 +0200 | [diff] [blame] | 254 | void empty_integer( const data_t *input ) | 
|  | 255 | { | 
|  | 256 | unsigned char *p; | 
|  | 257 | #if defined(MBEDTLS_BIGNUM_C) | 
|  | 258 | mbedtls_mpi actual_mpi; | 
|  | 259 | #endif | 
|  | 260 | int val; | 
|  | 261 |  | 
|  | 262 | #if defined(MBEDTLS_BIGNUM_C) | 
|  | 263 | mbedtls_mpi_init( & actual_mpi ); | 
|  | 264 | #endif | 
|  | 265 |  | 
|  | 266 | /* An INTEGER with no content is not valid. */ | 
|  | 267 | p = input->x; | 
|  | 268 | TEST_EQUAL( mbedtls_asn1_get_int( &p, input->x + input->len, &val ), | 
|  | 269 | MBEDTLS_ERR_ASN1_INVALID_LENGTH ); | 
|  | 270 |  | 
|  | 271 | #if defined(MBEDTLS_BIGNUM_C) | 
|  | 272 | /* INTEGERs are sometimes abused as bitstrings, so the library accepts | 
|  | 273 | * an INTEGER with empty content and gives it the value 0. */ | 
|  | 274 | p = input->x; | 
|  | 275 | TEST_EQUAL( mbedtls_asn1_get_mpi( &p, input->x + input->len, &actual_mpi ), | 
|  | 276 | 0 ); | 
|  | 277 | TEST_EQUAL( mbedtls_mpi_cmp_int( &actual_mpi, 0 ), 0 ); | 
|  | 278 | #endif | 
|  | 279 |  | 
|  | 280 | exit: | 
|  | 281 | #if defined(MBEDTLS_BIGNUM_C) | 
|  | 282 | mbedtls_mpi_free( &actual_mpi ); | 
|  | 283 | #endif | 
|  | 284 | /*empty cleanup in some configurations*/ ; | 
|  | 285 | } | 
|  | 286 | /* END_CASE */ | 
|  | 287 |  | 
|  | 288 | /* BEGIN_CASE */ | 
| Gilles Peskine | 27d806f | 2019-03-01 18:02:53 +0100 | [diff] [blame] | 289 | void get_integer( const data_t *input, | 
|  | 290 | const char *expected_hex, int expected_result ) | 
|  | 291 | { | 
|  | 292 | unsigned char *p; | 
|  | 293 | #if defined(MBEDTLS_BIGNUM_C) | 
|  | 294 | mbedtls_mpi expected_mpi; | 
|  | 295 | mbedtls_mpi actual_mpi; | 
| Gilles Peskine | 970dcbf | 2019-10-10 19:21:12 +0200 | [diff] [blame] | 296 | mbedtls_mpi complement; | 
| Gilles Peskine | 03c165e | 2019-10-10 19:15:18 +0200 | [diff] [blame] | 297 | int expected_result_for_mpi = expected_result; | 
| Gilles Peskine | 27d806f | 2019-03-01 18:02:53 +0100 | [diff] [blame] | 298 | #endif | 
|  | 299 | long expected_value; | 
|  | 300 | int expected_result_for_int = expected_result; | 
| Gilles Peskine | 27d806f | 2019-03-01 18:02:53 +0100 | [diff] [blame] | 301 | int val; | 
|  | 302 | int ret; | 
|  | 303 |  | 
|  | 304 | #if defined(MBEDTLS_BIGNUM_C) | 
|  | 305 | mbedtls_mpi_init( &expected_mpi ); | 
|  | 306 | mbedtls_mpi_init( &actual_mpi ); | 
| Gilles Peskine | 970dcbf | 2019-10-10 19:21:12 +0200 | [diff] [blame] | 307 | mbedtls_mpi_init( &complement ); | 
| Gilles Peskine | 27d806f | 2019-03-01 18:02:53 +0100 | [diff] [blame] | 308 | #endif | 
|  | 309 |  | 
|  | 310 | errno = 0; | 
|  | 311 | expected_value = strtol( expected_hex, NULL, 16 ); | 
|  | 312 | if( expected_result == 0 && | 
|  | 313 | ( errno == ERANGE | 
|  | 314 | #if LONG_MAX > INT_MAX | 
|  | 315 | || expected_value > INT_MAX || expected_value < INT_MIN | 
|  | 316 | #endif | 
|  | 317 | ) ) | 
|  | 318 | { | 
| Gilles Peskine | 970dcbf | 2019-10-10 19:21:12 +0200 | [diff] [blame] | 319 | /* The library returns the dubious error code INVALID_LENGTH | 
|  | 320 | * for integers that are out of range. */ | 
|  | 321 | expected_result_for_int = MBEDTLS_ERR_ASN1_INVALID_LENGTH; | 
|  | 322 | } | 
|  | 323 | if( expected_result == 0 && expected_value < 0 ) | 
|  | 324 | { | 
|  | 325 | /* The library does not support negative INTEGERs and | 
|  | 326 | * returns the dubious error code INVALID_LENGTH. | 
|  | 327 | * Test that we preserve the historical behavior. If we | 
|  | 328 | * decide to change the behavior, we'll also change this test. */ | 
| Gilles Peskine | 27d806f | 2019-03-01 18:02:53 +0100 | [diff] [blame] | 329 | expected_result_for_int = MBEDTLS_ERR_ASN1_INVALID_LENGTH; | 
|  | 330 | } | 
|  | 331 |  | 
|  | 332 | p = input->x; | 
|  | 333 | ret = mbedtls_asn1_get_int( &p, input->x + input->len, &val ); | 
|  | 334 | TEST_EQUAL( ret, expected_result_for_int ); | 
|  | 335 | if( ret == 0 ) | 
|  | 336 | { | 
|  | 337 | TEST_EQUAL( val, expected_value ); | 
|  | 338 | TEST_ASSERT( p == input->x + input->len ); | 
|  | 339 | } | 
|  | 340 |  | 
|  | 341 | #if defined(MBEDTLS_BIGNUM_C) | 
|  | 342 | ret = mbedtls_mpi_read_string( &expected_mpi, 16, expected_hex ); | 
|  | 343 | TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); | 
|  | 344 | if( ret == MBEDTLS_ERR_MPI_BAD_INPUT_DATA ) | 
|  | 345 | { | 
|  | 346 | /* The data overflows the maximum MPI size. */ | 
|  | 347 | expected_result_for_mpi = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; | 
|  | 348 | } | 
|  | 349 | p = input->x; | 
|  | 350 | ret = mbedtls_asn1_get_mpi( &p, input->x + input->len, &actual_mpi ); | 
|  | 351 | TEST_EQUAL( ret, expected_result_for_mpi ); | 
|  | 352 | if( ret == 0 ) | 
|  | 353 | { | 
| Gilles Peskine | 970dcbf | 2019-10-10 19:21:12 +0200 | [diff] [blame] | 354 | if( expected_value >= 0 ) | 
|  | 355 | { | 
|  | 356 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &actual_mpi, | 
|  | 357 | &expected_mpi ) == 0 ); | 
|  | 358 | } | 
|  | 359 | else | 
|  | 360 | { | 
|  | 361 | /* The library ignores the sign bit in ASN.1 INTEGERs | 
|  | 362 | * (which makes sense insofar as INTEGERs are sometimes | 
|  | 363 | * abused as bit strings), so the result of parsing them | 
|  | 364 | * is a positive integer such that expected_mpi + | 
|  | 365 | * actual_mpi = 2^n where n is the length of the content | 
|  | 366 | * of the INTEGER. (Leading ff octets don't matter for the | 
|  | 367 | * expected value, but they matter for the actual value.) | 
|  | 368 | * Test that we don't change from this behavior. If we | 
|  | 369 | * decide to fix the library to change the behavior on | 
|  | 370 | * negative INTEGERs, we'll fix this test code. */ | 
|  | 371 | unsigned char *q = input->x + 1; | 
|  | 372 | size_t len; | 
|  | 373 | TEST_ASSERT( mbedtls_asn1_get_len( &q, input->x + input->len, | 
|  | 374 | &len ) == 0 ); | 
|  | 375 | TEST_ASSERT( mbedtls_mpi_lset( &complement, 1 ) == 0 ); | 
|  | 376 | TEST_ASSERT( mbedtls_mpi_shift_l( &complement, len * 8 ) == 0 ); | 
|  | 377 | TEST_ASSERT( mbedtls_mpi_add_mpi( &complement, &complement, | 
|  | 378 | &expected_mpi ) == 0 ); | 
|  | 379 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &complement, | 
|  | 380 | &actual_mpi ) == 0 ); | 
|  | 381 | } | 
| Gilles Peskine | 27d806f | 2019-03-01 18:02:53 +0100 | [diff] [blame] | 382 | TEST_ASSERT( p == input->x + input->len ); | 
|  | 383 | } | 
|  | 384 | #endif | 
|  | 385 |  | 
|  | 386 | exit: | 
|  | 387 | #if defined(MBEDTLS_BIGNUM_C) | 
|  | 388 | mbedtls_mpi_free( &expected_mpi ); | 
|  | 389 | mbedtls_mpi_free( &actual_mpi ); | 
| Gilles Peskine | 970dcbf | 2019-10-10 19:21:12 +0200 | [diff] [blame] | 390 | mbedtls_mpi_free( &complement ); | 
| Gilles Peskine | 27d806f | 2019-03-01 18:02:53 +0100 | [diff] [blame] | 391 | #endif | 
| Gilles Peskine | 03c165e | 2019-10-10 19:15:18 +0200 | [diff] [blame] | 392 | /*empty cleanup in some configurations*/ ; | 
| Gilles Peskine | 27d806f | 2019-03-01 18:02:53 +0100 | [diff] [blame] | 393 | } | 
|  | 394 | /* END_CASE */ | 
|  | 395 |  | 
|  | 396 | /* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */ | 
|  | 397 | void get_mpi_too_large( ) | 
|  | 398 | { | 
|  | 399 | unsigned char *buf = NULL; | 
|  | 400 | unsigned char *p; | 
|  | 401 | mbedtls_mpi actual_mpi; | 
|  | 402 | size_t too_many_octets = | 
|  | 403 | MBEDTLS_MPI_MAX_LIMBS * sizeof(mbedtls_mpi_uint) + 1; | 
|  | 404 | size_t size = too_many_octets + 6; | 
|  | 405 |  | 
|  | 406 | mbedtls_mpi_init( &actual_mpi ); | 
|  | 407 |  | 
|  | 408 | ASSERT_ALLOC( buf, size ); | 
|  | 409 | buf[0] = 0x02; /* tag: INTEGER */ | 
|  | 410 | buf[1] = 0x84; /* 4-octet length */ | 
|  | 411 | buf[2] = ( too_many_octets >> 24 ) & 0xff; | 
|  | 412 | buf[3] = ( too_many_octets >> 16 ) & 0xff; | 
|  | 413 | buf[4] = ( too_many_octets >> 8 ) & 0xff; | 
|  | 414 | buf[5] = too_many_octets & 0xff; | 
|  | 415 | buf[6] = 0x01; /* most significant octet */ | 
|  | 416 |  | 
|  | 417 | p = buf; | 
|  | 418 | TEST_EQUAL( mbedtls_asn1_get_mpi( &p, buf + size, &actual_mpi ), | 
|  | 419 | MBEDTLS_ERR_MPI_ALLOC_FAILED ); | 
|  | 420 |  | 
|  | 421 | exit: | 
|  | 422 | mbedtls_mpi_free( &actual_mpi ); | 
|  | 423 | mbedtls_free( buf ); | 
|  | 424 | } | 
|  | 425 | /* END_CASE */ | 
|  | 426 |  | 
|  | 427 | /* BEGIN_CASE */ | 
|  | 428 | void get_bitstring( const data_t *input, | 
|  | 429 | int expected_length, int expected_unused_bits, | 
|  | 430 | int expected_result, int expected_result_null ) | 
|  | 431 | { | 
|  | 432 | mbedtls_asn1_bitstring bs = { 0xdead, 0x21, NULL }; | 
|  | 433 | unsigned char *p = input->x; | 
|  | 434 |  | 
|  | 435 | TEST_EQUAL( mbedtls_asn1_get_bitstring( &p, input->x + input->len, &bs ), | 
|  | 436 | expected_result ); | 
|  | 437 | if( expected_result == 0 ) | 
|  | 438 | { | 
|  | 439 | TEST_EQUAL( bs.len, (size_t) expected_length ); | 
|  | 440 | TEST_EQUAL( bs.unused_bits, expected_unused_bits ); | 
|  | 441 | TEST_ASSERT( bs.p != NULL ); | 
|  | 442 | TEST_EQUAL( bs.p - input->x + bs.len, input->len ); | 
|  | 443 | TEST_ASSERT( p == input->x + input->len ); | 
|  | 444 | } | 
|  | 445 |  | 
|  | 446 | p = input->x; | 
|  | 447 | TEST_EQUAL( mbedtls_asn1_get_bitstring_null( &p, input->x + input->len, | 
|  | 448 | &bs.len ), | 
|  | 449 | expected_result_null ); | 
|  | 450 | if( expected_result_null == 0 ) | 
|  | 451 | { | 
|  | 452 | TEST_EQUAL( bs.len, (size_t) expected_length ); | 
|  | 453 | if( expected_result == 0 ) | 
|  | 454 | TEST_ASSERT( p == input->x + input->len - bs.len ); | 
|  | 455 | } | 
|  | 456 | } | 
|  | 457 | /* END_CASE */ | 
|  | 458 |  | 
|  | 459 | /* BEGIN_CASE */ | 
|  | 460 | void get_sequence_of( const data_t *input, int tag, | 
|  | 461 | const char *description, | 
|  | 462 | int expected_result ) | 
|  | 463 | { | 
|  | 464 | mbedtls_asn1_sequence head = { { 0, 0, NULL }, NULL }; | 
|  | 465 | mbedtls_asn1_sequence *cur, *next; | 
|  | 466 | unsigned char *p = input->x; | 
|  | 467 | const char *rest = description; | 
|  | 468 | unsigned long n; | 
|  | 469 |  | 
|  | 470 | TEST_EQUAL( mbedtls_asn1_get_sequence_of( &p, input->x + input->len, | 
|  | 471 | &head, tag ), | 
|  | 472 | expected_result ); | 
|  | 473 | if( expected_result == 0 ) | 
|  | 474 | { | 
|  | 475 | TEST_ASSERT( p == input->x + input->len ); | 
|  | 476 |  | 
|  | 477 | if( ! *rest ) | 
|  | 478 | { | 
|  | 479 | TEST_EQUAL( head.buf.tag, 0 ); | 
|  | 480 | TEST_ASSERT( head.buf.p == NULL ); | 
|  | 481 | TEST_EQUAL( head.buf.len, 0 ); | 
|  | 482 | TEST_ASSERT( head.next == NULL ); | 
|  | 483 | } | 
|  | 484 | else | 
|  | 485 | { | 
|  | 486 | cur = &head; | 
|  | 487 | while( *rest ) | 
|  | 488 | { | 
|  | 489 | ++test_info.step; | 
|  | 490 | TEST_ASSERT( cur != NULL ); | 
|  | 491 | TEST_EQUAL( cur->buf.tag, tag ); | 
|  | 492 | n = strtoul( rest, (char **) &rest, 0 ); | 
|  | 493 | TEST_EQUAL( n, (size_t)( cur->buf.p - input->x ) ); | 
|  | 494 | ++rest; | 
|  | 495 | n = strtoul( rest, (char **) &rest, 0 ); | 
|  | 496 | TEST_EQUAL( n, cur->buf.len ); | 
|  | 497 | if( *rest ) | 
|  | 498 | ++rest; | 
|  | 499 | cur = cur->next; | 
|  | 500 | } | 
|  | 501 | TEST_ASSERT( cur == NULL ); | 
|  | 502 | } | 
|  | 503 | } | 
|  | 504 |  | 
|  | 505 | exit: | 
|  | 506 | cur = head.next; | 
|  | 507 | while( cur != NULL ) | 
|  | 508 | { | 
|  | 509 | next = cur->next; | 
|  | 510 | mbedtls_free( cur ); | 
|  | 511 | cur = next; | 
|  | 512 | } | 
|  | 513 | } | 
|  | 514 | /* END_CASE */ | 
|  | 515 |  | 
|  | 516 | /* BEGIN_CASE */ | 
|  | 517 | void get_alg( const data_t *input, | 
|  | 518 | int oid_offset, int oid_length, | 
|  | 519 | int params_tag, int params_offset, int params_length, | 
|  | 520 | int total_length, | 
|  | 521 | int expected_result ) | 
|  | 522 | { | 
|  | 523 | mbedtls_asn1_buf oid = { -1, 0, NULL }; | 
|  | 524 | mbedtls_asn1_buf params = { -1, 0, NULL }; | 
|  | 525 | unsigned char *p = input->x; | 
|  | 526 | int ret; | 
|  | 527 |  | 
|  | 528 | TEST_EQUAL( mbedtls_asn1_get_alg( &p, input->x + input->len, | 
|  | 529 | &oid, ¶ms ), | 
|  | 530 | expected_result ); | 
|  | 531 | if( expected_result == 0 ) | 
|  | 532 | { | 
|  | 533 | TEST_EQUAL( oid.tag, MBEDTLS_ASN1_OID ); | 
|  | 534 | TEST_EQUAL( oid.p - input->x, oid_offset ); | 
|  | 535 | TEST_EQUAL( oid.len, (size_t) oid_length ); | 
|  | 536 | TEST_EQUAL( params.tag, params_tag ); | 
|  | 537 | if( params_offset != 0 ) | 
|  | 538 | TEST_EQUAL( params.p - input->x, params_offset ); | 
|  | 539 | else | 
|  | 540 | TEST_ASSERT( params.p == NULL ); | 
|  | 541 | TEST_EQUAL( params.len, (size_t) params_length ); | 
|  | 542 | TEST_EQUAL( p - input->x, total_length ); | 
|  | 543 | } | 
|  | 544 |  | 
|  | 545 | ret = mbedtls_asn1_get_alg_null( &p, input->x + input->len, &oid ); | 
|  | 546 | if( expected_result == 0 && params_offset == 0 ) | 
|  | 547 | { | 
|  | 548 | TEST_EQUAL( oid.tag, MBEDTLS_ASN1_OID ); | 
|  | 549 | TEST_EQUAL( oid.p - input->x, oid_offset ); | 
|  | 550 | TEST_EQUAL( oid.len, (size_t) oid_length ); | 
|  | 551 | TEST_EQUAL( p - input->x, total_length ); | 
|  | 552 | } | 
|  | 553 | else | 
|  | 554 | TEST_ASSERT( ret != 0 ); | 
|  | 555 | } | 
|  | 556 | /* END_CASE */ | 
|  | 557 |  | 
|  | 558 | /* BEGIN_CASE */ | 
|  | 559 | void find_named_data( data_t *oid0, data_t *oid1, data_t *oid2, data_t *oid3, | 
|  | 560 | data_t *needle, int from, int position ) | 
|  | 561 | { | 
|  | 562 | mbedtls_asn1_named_data nd[] ={ | 
|  | 563 | { {0x06, oid0->len, oid0->x}, {0, 0, NULL}, NULL, 0 }, | 
|  | 564 | { {0x06, oid1->len, oid1->x}, {0, 0, NULL}, NULL, 0 }, | 
|  | 565 | { {0x06, oid2->len, oid2->x}, {0, 0, NULL}, NULL, 0 }, | 
|  | 566 | { {0x06, oid3->len, oid3->x}, {0, 0, NULL}, NULL, 0 }, | 
|  | 567 | }; | 
|  | 568 | mbedtls_asn1_named_data *pointers[ARRAY_LENGTH( nd ) + 1]; | 
|  | 569 | size_t i; | 
|  | 570 | mbedtls_asn1_named_data *found; | 
|  | 571 |  | 
|  | 572 | for( i = 0; i < ARRAY_LENGTH( nd ); i++ ) | 
|  | 573 | pointers[i] = &nd[i]; | 
|  | 574 | pointers[ARRAY_LENGTH( nd )] = NULL; | 
|  | 575 | for( i = 0; i < ARRAY_LENGTH( nd ); i++ ) | 
|  | 576 | nd[i].next = pointers[i+1]; | 
|  | 577 |  | 
|  | 578 | found = mbedtls_asn1_find_named_data( pointers[from], | 
|  | 579 | (const char *) needle->x, | 
|  | 580 | needle->len ); | 
|  | 581 | TEST_ASSERT( found == pointers[position] ); | 
|  | 582 | } | 
|  | 583 | /* END_CASE */ | 
|  | 584 |  | 
|  | 585 | /* BEGIN_CASE */ | 
|  | 586 | void free_named_data_null( ) | 
|  | 587 | { | 
|  | 588 | mbedtls_asn1_free_named_data( NULL ); | 
|  | 589 | goto exit; /* Silence unused label warning */ | 
|  | 590 | } | 
|  | 591 | /* END_CASE */ | 
|  | 592 |  | 
|  | 593 | /* BEGIN_CASE */ | 
|  | 594 | void free_named_data( int with_oid, int with_val, int with_next ) | 
|  | 595 | { | 
|  | 596 | mbedtls_asn1_named_data next = | 
|  | 597 | { {0x06, 0, NULL}, {0, 0xcafe, NULL}, NULL, 0 }; | 
|  | 598 | mbedtls_asn1_named_data head = | 
|  | 599 | { {0x06, 0, NULL}, {0, 0, NULL}, NULL, 0 }; | 
|  | 600 |  | 
|  | 601 | if( with_oid ) | 
|  | 602 | ASSERT_ALLOC( head.oid.p, 1 ); | 
|  | 603 | if( with_val ) | 
|  | 604 | ASSERT_ALLOC( head.val.p, 1 ); | 
|  | 605 | if( with_next ) | 
|  | 606 | head.next = &next; | 
|  | 607 |  | 
|  | 608 | mbedtls_asn1_free_named_data( &head ); | 
|  | 609 | TEST_ASSERT( head.oid.p == NULL ); | 
|  | 610 | TEST_ASSERT( head.val.p == NULL ); | 
|  | 611 | TEST_ASSERT( head.next == NULL ); | 
|  | 612 | TEST_ASSERT( next.val.len == 0xcafe ); | 
|  | 613 |  | 
|  | 614 | exit: | 
|  | 615 | mbedtls_free( head.oid.p ); | 
|  | 616 | mbedtls_free( head.val.p ); | 
|  | 617 | } | 
|  | 618 | /* END_CASE */ | 
|  | 619 |  | 
|  | 620 | /* BEGIN_CASE */ | 
|  | 621 | void free_named_data_list( int length ) | 
|  | 622 | { | 
|  | 623 | mbedtls_asn1_named_data *head = NULL; | 
|  | 624 | int i; | 
|  | 625 |  | 
|  | 626 | for( i = 0; i < length; i++ ) | 
|  | 627 | { | 
|  | 628 | mbedtls_asn1_named_data *new = NULL; | 
|  | 629 | ASSERT_ALLOC( new, sizeof( mbedtls_asn1_named_data ) ); | 
| Gilles Peskine | 88f136f | 2019-09-20 21:06:27 +0200 | [diff] [blame] | 630 | new->next = head; | 
| Gilles Peskine | 27d806f | 2019-03-01 18:02:53 +0100 | [diff] [blame] | 631 | head = new; | 
|  | 632 | } | 
|  | 633 |  | 
|  | 634 | mbedtls_asn1_free_named_data_list( &head ); | 
|  | 635 | TEST_ASSERT( head == NULL ); | 
|  | 636 | /* Most of the point of the test is that it doesn't leak memory. | 
|  | 637 | * So this test is only really useful under a memory leak detection | 
|  | 638 | * framework. */ | 
|  | 639 | exit: | 
|  | 640 | mbedtls_asn1_free_named_data_list( &head ); | 
|  | 641 | } | 
|  | 642 | /* END_CASE */ |