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