Tom Rini | 0344c60 | 2024-10-08 13:56:50 -0600 | [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 | /* Used internally to report an error that indicates a bug in a parsing function. */ |
| 13 | #define ERR_PARSE_INCONSISTENCY INT_MAX |
| 14 | |
| 15 | /* Use this magic value in some tests to indicate that the expected result |
| 16 | * should not be checked. */ |
| 17 | #define UNPREDICTABLE_RESULT 0x5552 |
| 18 | |
| 19 | static 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 | |
| 37 | /* Since we have a valid element start (tag and length), retrieve and |
| 38 | * check the tag. */ |
| 39 | tag = start[0]; |
| 40 | TEST_EQUAL(mbedtls_asn1_get_tag(p, end, &len2, tag ^ 1), |
| 41 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
| 42 | *p = start; |
| 43 | TEST_EQUAL(mbedtls_asn1_get_tag(p, end, &len2, tag), 0); |
| 44 | TEST_EQUAL(len, len2); |
| 45 | TEST_ASSERT(*p == content_start); |
| 46 | *p = content_start; |
| 47 | |
| 48 | switch (tag & 0x1f) { |
| 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 | } |
| 57 | break; |
| 58 | } |
| 59 | |
| 60 | case MBEDTLS_ASN1_INTEGER: |
| 61 | { |
| 62 | #if defined(MBEDTLS_BIGNUM_C) |
| 63 | mbedtls_mpi mpi; |
| 64 | mbedtls_mpi_init(&mpi); |
| 65 | *p = start; |
| 66 | ret = mbedtls_asn1_get_mpi(p, end, &mpi); |
| 67 | mbedtls_mpi_free(&mpi); |
| 68 | #else |
| 69 | *p = start + 1; |
| 70 | ret = mbedtls_asn1_get_len(p, end, &len); |
| 71 | *p += len; |
| 72 | #endif |
| 73 | /* If we're sure that the number fits in an int, also |
| 74 | * call mbedtls_asn1_get_int(). */ |
| 75 | if (ret == 0 && len < sizeof(int)) { |
| 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 | } |
| 97 | break; |
| 98 | } |
| 99 | |
| 100 | case MBEDTLS_ASN1_OCTET_STRING: |
| 101 | case MBEDTLS_ASN1_NULL: |
| 102 | case MBEDTLS_ASN1_OID: |
| 103 | case MBEDTLS_ASN1_UTF8_STRING: |
| 104 | case MBEDTLS_ASN1_SET: |
| 105 | case MBEDTLS_ASN1_PRINTABLE_STRING: |
| 106 | case MBEDTLS_ASN1_T61_STRING: |
| 107 | case MBEDTLS_ASN1_IA5_STRING: |
| 108 | case MBEDTLS_ASN1_UTC_TIME: |
| 109 | case MBEDTLS_ASN1_GENERALIZED_TIME: |
| 110 | case MBEDTLS_ASN1_UNIVERSAL_STRING: |
| 111 | case MBEDTLS_ASN1_BMP_STRING: |
| 112 | default: |
| 113 | /* No further testing implemented for this tag. */ |
| 114 | *p += len; |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | TEST_ASSERT(*p <= end); |
| 119 | return ret; |
| 120 | |
| 121 | exit: |
| 122 | return ERR_PARSE_INCONSISTENCY; |
| 123 | } |
| 124 | |
| 125 | int get_len_step(const data_t *input, size_t buffer_size, |
| 126 | size_t actual_length) |
| 127 | { |
| 128 | unsigned char *buf = NULL; |
| 129 | unsigned char *p = NULL; |
| 130 | unsigned char *end; |
| 131 | size_t parsed_length; |
| 132 | int ret; |
| 133 | |
| 134 | mbedtls_test_set_step(buffer_size); |
| 135 | /* Allocate a new buffer of exactly the length to parse each time. |
| 136 | * This gives memory sanitizers a chance to catch buffer overreads. */ |
| 137 | if (buffer_size == 0) { |
| 138 | TEST_CALLOC(buf, 1); |
| 139 | end = buf + 1; |
| 140 | p = end; |
| 141 | } else { |
| 142 | TEST_CALLOC_OR_SKIP(buf, buffer_size); |
| 143 | if (buffer_size > input->len) { |
| 144 | memcpy(buf, input->x, input->len); |
| 145 | memset(buf + input->len, 'A', buffer_size - input->len); |
| 146 | } else { |
| 147 | memcpy(buf, input->x, buffer_size); |
| 148 | } |
| 149 | p = buf; |
| 150 | end = buf + buffer_size; |
| 151 | } |
| 152 | |
| 153 | ret = mbedtls_asn1_get_len(&p, end, &parsed_length); |
| 154 | |
| 155 | if (buffer_size >= input->len + actual_length) { |
| 156 | TEST_EQUAL(ret, 0); |
| 157 | TEST_ASSERT(p == buf + input->len); |
| 158 | TEST_EQUAL(parsed_length, actual_length); |
| 159 | } else { |
| 160 | TEST_EQUAL(ret, MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
| 161 | } |
| 162 | mbedtls_free(buf); |
| 163 | return 1; |
| 164 | |
| 165 | exit: |
| 166 | mbedtls_free(buf); |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | typedef struct { |
| 171 | const unsigned char *input_start; |
| 172 | const char *description; |
| 173 | } traverse_state_t; |
| 174 | |
| 175 | /* Value returned by traverse_callback if description runs out. */ |
| 176 | #define RET_TRAVERSE_STOP 1 |
| 177 | /* Value returned by traverse_callback if description has an invalid format |
| 178 | * (see traverse_sequence_of). */ |
| 179 | #define RET_TRAVERSE_ERROR 2 |
| 180 | |
| 181 | |
| 182 | static int traverse_callback(void *ctx, int tag, |
| 183 | unsigned char *content, size_t len) |
| 184 | { |
| 185 | traverse_state_t *state = ctx; |
| 186 | size_t offset; |
| 187 | const char *rest = state->description; |
| 188 | unsigned long n; |
| 189 | |
| 190 | TEST_ASSERT(content > state->input_start); |
| 191 | offset = content - state->input_start; |
| 192 | mbedtls_test_set_step(offset); |
| 193 | |
| 194 | if (*rest == 0) { |
| 195 | return RET_TRAVERSE_STOP; |
| 196 | } |
| 197 | n = strtoul(rest, (char **) &rest, 0); |
| 198 | TEST_EQUAL(n, offset); |
| 199 | TEST_EQUAL(*rest, ','); |
| 200 | ++rest; |
| 201 | n = strtoul(rest, (char **) &rest, 0); |
| 202 | TEST_EQUAL(n, (unsigned) tag); |
| 203 | TEST_EQUAL(*rest, ','); |
| 204 | ++rest; |
| 205 | n = strtoul(rest, (char **) &rest, 0); |
| 206 | TEST_EQUAL(n, len); |
| 207 | if (*rest == ',') { |
| 208 | ++rest; |
| 209 | } |
| 210 | |
| 211 | state->description = rest; |
| 212 | return 0; |
| 213 | |
| 214 | exit: |
| 215 | return RET_TRAVERSE_ERROR; |
| 216 | } |
| 217 | |
| 218 | /* END_HEADER */ |
| 219 | |
| 220 | /* BEGIN_DEPENDENCIES |
| 221 | * depends_on:MBEDTLS_ASN1_PARSE_C |
| 222 | * END_DEPENDENCIES |
| 223 | */ |
| 224 | |
| 225 | /* BEGIN_CASE */ |
| 226 | void parse_prefixes(const data_t *input, |
| 227 | int full_result, |
| 228 | int overfull_result) |
| 229 | { |
| 230 | /* full_result: expected result from parsing the given string. */ |
| 231 | /* overfull_result: expected_result from parsing the given string plus |
| 232 | * some trailing garbage. This may be UNPREDICTABLE_RESULT to accept |
| 233 | * any result: use this for invalid inputs that may or may not become |
| 234 | * valid depending on what the trailing garbage is. */ |
| 235 | |
| 236 | unsigned char *buf = NULL; |
| 237 | unsigned char *p = NULL; |
| 238 | size_t buffer_size; |
| 239 | int ret; |
| 240 | |
| 241 | /* Test every prefix of the input, except the empty string. |
| 242 | * The first byte of the string is the tag. Without a tag byte, |
| 243 | * we wouldn't know what to parse the input as. |
| 244 | * Also test the input followed by an extra byte. |
| 245 | */ |
| 246 | for (buffer_size = 1; buffer_size <= input->len + 1; buffer_size++) { |
| 247 | mbedtls_test_set_step(buffer_size); |
| 248 | /* Allocate a new buffer of exactly the length to parse each time. |
| 249 | * This gives memory sanitizers a chance to catch buffer overreads. */ |
| 250 | TEST_CALLOC(buf, buffer_size); |
| 251 | memcpy(buf, input->x, buffer_size); |
| 252 | p = buf; |
| 253 | ret = nested_parse(&p, buf + buffer_size); |
| 254 | |
| 255 | if (ret == ERR_PARSE_INCONSISTENCY) { |
| 256 | goto exit; |
| 257 | } |
| 258 | if (buffer_size < input->len) { |
| 259 | TEST_EQUAL(ret, MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
| 260 | } else if (buffer_size == input->len) { |
| 261 | TEST_EQUAL(ret, full_result); |
| 262 | } else { /* ( buffer_size > input->len ) */ |
| 263 | if (overfull_result != UNPREDICTABLE_RESULT) { |
| 264 | TEST_EQUAL(ret, overfull_result); |
| 265 | } |
| 266 | } |
| 267 | if (ret == 0) { |
| 268 | TEST_ASSERT(p == buf + input->len); |
| 269 | } |
| 270 | |
| 271 | mbedtls_free(buf); |
| 272 | buf = NULL; |
| 273 | } |
| 274 | |
| 275 | exit: |
| 276 | mbedtls_free(buf); |
| 277 | } |
| 278 | /* END_CASE */ |
| 279 | |
| 280 | /* BEGIN_CASE */ |
| 281 | void get_len(const data_t *input, int actual_length_arg) |
| 282 | { |
| 283 | size_t actual_length = actual_length_arg; |
| 284 | size_t buffer_size; |
| 285 | |
| 286 | /* Test prefixes of a buffer containing the given length string |
| 287 | * followed by `actual_length` bytes of payload. To save a bit of |
| 288 | * time, we skip some "boring" prefixes: we don't test prefixes where |
| 289 | * the payload is truncated more than one byte away from either end, |
| 290 | * and we only test the empty string on a 1-byte input. |
| 291 | */ |
| 292 | for (buffer_size = 1; buffer_size <= input->len + 1; buffer_size++) { |
| 293 | if (!get_len_step(input, buffer_size, actual_length)) { |
| 294 | goto exit; |
| 295 | } |
| 296 | } |
| 297 | if (!get_len_step(input, input->len + actual_length - 1, actual_length)) { |
| 298 | goto exit; |
| 299 | } |
| 300 | if (!get_len_step(input, input->len + actual_length, actual_length)) { |
| 301 | goto exit; |
| 302 | } |
| 303 | } |
| 304 | /* END_CASE */ |
| 305 | |
| 306 | /* BEGIN_CASE */ |
| 307 | void get_boolean(const data_t *input, |
| 308 | int expected_value, int expected_result) |
| 309 | { |
| 310 | unsigned char *p = input->x; |
| 311 | int val; |
| 312 | int ret; |
| 313 | ret = mbedtls_asn1_get_bool(&p, input->x + input->len, &val); |
| 314 | TEST_EQUAL(ret, expected_result); |
| 315 | if (expected_result == 0) { |
| 316 | TEST_EQUAL(val, expected_value); |
| 317 | TEST_ASSERT(p == input->x + input->len); |
| 318 | } |
| 319 | } |
| 320 | /* END_CASE */ |
| 321 | |
| 322 | /* BEGIN_CASE */ |
| 323 | void empty_integer(const data_t *input) |
| 324 | { |
| 325 | unsigned char *p; |
| 326 | #if defined(MBEDTLS_BIGNUM_C) |
| 327 | mbedtls_mpi actual_mpi; |
| 328 | #endif |
| 329 | int val; |
| 330 | |
| 331 | #if defined(MBEDTLS_BIGNUM_C) |
| 332 | mbedtls_mpi_init(&actual_mpi); |
| 333 | #endif |
| 334 | |
| 335 | /* An INTEGER with no content is not valid. */ |
| 336 | p = input->x; |
| 337 | TEST_EQUAL(mbedtls_asn1_get_int(&p, input->x + input->len, &val), |
| 338 | MBEDTLS_ERR_ASN1_INVALID_LENGTH); |
| 339 | |
| 340 | #if defined(MBEDTLS_BIGNUM_C) |
| 341 | /* INTEGERs are sometimes abused as bitstrings, so the library accepts |
| 342 | * an INTEGER with empty content and gives it the value 0. */ |
| 343 | p = input->x; |
| 344 | TEST_EQUAL(mbedtls_asn1_get_mpi(&p, input->x + input->len, &actual_mpi), |
| 345 | 0); |
| 346 | TEST_EQUAL(mbedtls_mpi_cmp_int(&actual_mpi, 0), 0); |
| 347 | #endif |
| 348 | |
| 349 | exit: |
| 350 | #if defined(MBEDTLS_BIGNUM_C) |
| 351 | mbedtls_mpi_free(&actual_mpi); |
| 352 | #endif |
| 353 | /*empty cleanup in some configurations*/; |
| 354 | } |
| 355 | /* END_CASE */ |
| 356 | |
| 357 | /* BEGIN_CASE */ |
| 358 | void get_integer(const data_t *input, |
| 359 | const char *expected_hex, int expected_result) |
| 360 | { |
| 361 | unsigned char *p; |
| 362 | #if defined(MBEDTLS_BIGNUM_C) |
| 363 | mbedtls_mpi expected_mpi; |
| 364 | mbedtls_mpi actual_mpi; |
| 365 | mbedtls_mpi complement; |
| 366 | int expected_result_for_mpi = expected_result; |
| 367 | #endif |
| 368 | long expected_value; |
| 369 | int expected_result_for_int = expected_result; |
| 370 | int val; |
| 371 | int ret; |
| 372 | |
| 373 | #if defined(MBEDTLS_BIGNUM_C) |
| 374 | mbedtls_mpi_init(&expected_mpi); |
| 375 | mbedtls_mpi_init(&actual_mpi); |
| 376 | mbedtls_mpi_init(&complement); |
| 377 | #endif |
| 378 | |
| 379 | errno = 0; |
| 380 | expected_value = strtol(expected_hex, NULL, 16); |
| 381 | if (expected_result == 0 && |
| 382 | (errno == ERANGE |
| 383 | #if LONG_MAX > INT_MAX |
| 384 | || expected_value > INT_MAX || expected_value < INT_MIN |
| 385 | #endif |
| 386 | )) { |
| 387 | /* The library returns the dubious error code INVALID_LENGTH |
| 388 | * for integers that are out of range. */ |
| 389 | expected_result_for_int = MBEDTLS_ERR_ASN1_INVALID_LENGTH; |
| 390 | } |
| 391 | if (expected_result == 0 && expected_value < 0) { |
| 392 | /* The library does not support negative INTEGERs and |
| 393 | * returns the dubious error code INVALID_LENGTH. |
| 394 | * Test that we preserve the historical behavior. If we |
| 395 | * decide to change the behavior, we'll also change this test. */ |
| 396 | expected_result_for_int = MBEDTLS_ERR_ASN1_INVALID_LENGTH; |
| 397 | } |
| 398 | |
| 399 | p = input->x; |
| 400 | ret = mbedtls_asn1_get_int(&p, input->x + input->len, &val); |
| 401 | TEST_EQUAL(ret, expected_result_for_int); |
| 402 | if (ret == 0) { |
| 403 | TEST_EQUAL(val, expected_value); |
| 404 | TEST_ASSERT(p == input->x + input->len); |
| 405 | } |
| 406 | |
| 407 | #if defined(MBEDTLS_BIGNUM_C) |
| 408 | ret = mbedtls_test_read_mpi(&expected_mpi, expected_hex); |
| 409 | TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_MPI_BAD_INPUT_DATA); |
| 410 | if (ret == MBEDTLS_ERR_MPI_BAD_INPUT_DATA) { |
| 411 | /* The data overflows the maximum MPI size. */ |
| 412 | expected_result_for_mpi = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 413 | } |
| 414 | p = input->x; |
| 415 | ret = mbedtls_asn1_get_mpi(&p, input->x + input->len, &actual_mpi); |
| 416 | TEST_EQUAL(ret, expected_result_for_mpi); |
| 417 | if (ret == 0) { |
| 418 | if (expected_value >= 0) { |
| 419 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&actual_mpi, |
| 420 | &expected_mpi) == 0); |
| 421 | } else { |
| 422 | /* The library ignores the sign bit in ASN.1 INTEGERs |
| 423 | * (which makes sense insofar as INTEGERs are sometimes |
| 424 | * abused as bit strings), so the result of parsing them |
| 425 | * is a positive integer such that expected_mpi + |
| 426 | * actual_mpi = 2^n where n is the length of the content |
| 427 | * of the INTEGER. (Leading ff octets don't matter for the |
| 428 | * expected value, but they matter for the actual value.) |
| 429 | * Test that we don't change from this behavior. If we |
| 430 | * decide to fix the library to change the behavior on |
| 431 | * negative INTEGERs, we'll fix this test code. */ |
| 432 | unsigned char *q = input->x + 1; |
| 433 | size_t len; |
| 434 | TEST_ASSERT(mbedtls_asn1_get_len(&q, input->x + input->len, |
| 435 | &len) == 0); |
| 436 | TEST_ASSERT(mbedtls_mpi_lset(&complement, 1) == 0); |
| 437 | TEST_ASSERT(mbedtls_mpi_shift_l(&complement, len * 8) == 0); |
| 438 | TEST_ASSERT(mbedtls_mpi_add_mpi(&complement, &complement, |
| 439 | &expected_mpi) == 0); |
| 440 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&complement, |
| 441 | &actual_mpi) == 0); |
| 442 | } |
| 443 | TEST_ASSERT(p == input->x + input->len); |
| 444 | } |
| 445 | #endif |
| 446 | |
| 447 | exit: |
| 448 | #if defined(MBEDTLS_BIGNUM_C) |
| 449 | mbedtls_mpi_free(&expected_mpi); |
| 450 | mbedtls_mpi_free(&actual_mpi); |
| 451 | mbedtls_mpi_free(&complement); |
| 452 | #endif |
| 453 | /*empty cleanup in some configurations*/; |
| 454 | } |
| 455 | /* END_CASE */ |
| 456 | |
| 457 | /* BEGIN_CASE */ |
| 458 | void get_enum(const data_t *input, |
| 459 | const char *expected_hex, int expected_result) |
| 460 | { |
| 461 | unsigned char *p; |
| 462 | long expected_value; |
| 463 | int expected_result_for_enum = expected_result; |
| 464 | int val; |
| 465 | int ret; |
| 466 | |
| 467 | errno = 0; |
| 468 | expected_value = strtol(expected_hex, NULL, 16); |
| 469 | if (expected_result == 0 && |
| 470 | (errno == ERANGE |
| 471 | #if LONG_MAX > INT_MAX |
| 472 | || expected_value > INT_MAX || expected_value < INT_MIN |
| 473 | #endif |
| 474 | )) { |
| 475 | /* The library returns the dubious error code INVALID_LENGTH |
| 476 | * for integers that are out of range. */ |
| 477 | expected_result_for_enum = MBEDTLS_ERR_ASN1_INVALID_LENGTH; |
| 478 | } |
| 479 | if (expected_result == 0 && expected_value < 0) { |
| 480 | /* The library does not support negative INTEGERs and |
| 481 | * returns the dubious error code INVALID_LENGTH. |
| 482 | * Test that we preserve the historical behavior. If we |
| 483 | * decide to change the behavior, we'll also change this test. */ |
| 484 | expected_result_for_enum = MBEDTLS_ERR_ASN1_INVALID_LENGTH; |
| 485 | } |
| 486 | |
| 487 | p = input->x; |
| 488 | ret = mbedtls_asn1_get_enum(&p, input->x + input->len, &val); |
| 489 | TEST_EQUAL(ret, expected_result_for_enum); |
| 490 | if (ret == 0) { |
| 491 | TEST_EQUAL(val, expected_value); |
| 492 | TEST_ASSERT(p == input->x + input->len); |
| 493 | } |
| 494 | } |
| 495 | /* END_CASE */ |
| 496 | |
| 497 | /* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */ |
| 498 | void get_mpi_too_large() |
| 499 | { |
| 500 | unsigned char *buf = NULL; |
| 501 | unsigned char *p; |
| 502 | mbedtls_mpi actual_mpi; |
| 503 | size_t too_many_octets = |
| 504 | MBEDTLS_MPI_MAX_LIMBS * sizeof(mbedtls_mpi_uint) + 1; |
| 505 | size_t size = too_many_octets + 6; |
| 506 | |
| 507 | mbedtls_mpi_init(&actual_mpi); |
| 508 | |
| 509 | TEST_CALLOC(buf, size); |
| 510 | buf[0] = 0x02; /* tag: INTEGER */ |
| 511 | buf[1] = 0x84; /* 4-octet length */ |
| 512 | buf[2] = (too_many_octets >> 24) & 0xff; |
| 513 | buf[3] = (too_many_octets >> 16) & 0xff; |
| 514 | buf[4] = (too_many_octets >> 8) & 0xff; |
| 515 | buf[5] = too_many_octets & 0xff; |
| 516 | buf[6] = 0x01; /* most significant octet */ |
| 517 | |
| 518 | p = buf; |
| 519 | TEST_EQUAL(mbedtls_asn1_get_mpi(&p, buf + size, &actual_mpi), |
| 520 | MBEDTLS_ERR_MPI_ALLOC_FAILED); |
| 521 | |
| 522 | exit: |
| 523 | mbedtls_mpi_free(&actual_mpi); |
| 524 | mbedtls_free(buf); |
| 525 | } |
| 526 | /* END_CASE */ |
| 527 | |
| 528 | /* BEGIN_CASE */ |
| 529 | void get_bitstring(const data_t *input, |
| 530 | int expected_length, int expected_unused_bits, |
| 531 | int expected_result, int expected_result_null) |
| 532 | { |
| 533 | mbedtls_asn1_bitstring bs = { 0xdead, 0x21, NULL }; |
| 534 | unsigned char *p = input->x; |
| 535 | |
| 536 | TEST_EQUAL(mbedtls_asn1_get_bitstring(&p, input->x + input->len, &bs), |
| 537 | expected_result); |
| 538 | if (expected_result == 0) { |
| 539 | TEST_EQUAL(bs.len, (size_t) expected_length); |
| 540 | TEST_EQUAL(bs.unused_bits, expected_unused_bits); |
| 541 | TEST_ASSERT(bs.p != NULL); |
| 542 | TEST_EQUAL(bs.p - input->x + bs.len, input->len); |
| 543 | TEST_ASSERT(p == input->x + input->len); |
| 544 | } |
| 545 | |
| 546 | p = input->x; |
| 547 | TEST_EQUAL(mbedtls_asn1_get_bitstring_null(&p, input->x + input->len, |
| 548 | &bs.len), |
| 549 | expected_result_null); |
| 550 | if (expected_result_null == 0) { |
| 551 | TEST_EQUAL(bs.len, (size_t) expected_length); |
| 552 | if (expected_result == 0) { |
| 553 | TEST_ASSERT(p == input->x + input->len - bs.len); |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | /* END_CASE */ |
| 558 | |
| 559 | /* BEGIN_CASE */ |
| 560 | void get_sequence_of(const data_t *input, int tag, |
| 561 | const char *description, |
| 562 | int expected_result) |
| 563 | { |
| 564 | /* The description string is a comma-separated list of integers. |
| 565 | * For each element in the SEQUENCE in input, description contains |
| 566 | * two integers: the offset of the element (offset from the start |
| 567 | * of input to the tag of the element) and the length of the |
| 568 | * element's contents. |
| 569 | * "offset1,length1,..." */ |
| 570 | |
| 571 | mbedtls_asn1_sequence head = { { 0, 0, NULL }, NULL }; |
| 572 | mbedtls_asn1_sequence *cur; |
| 573 | unsigned char *p = input->x; |
| 574 | const char *rest = description; |
| 575 | unsigned long n; |
| 576 | unsigned int step = 0; |
| 577 | |
| 578 | TEST_EQUAL(mbedtls_asn1_get_sequence_of(&p, input->x + input->len, |
| 579 | &head, tag), |
| 580 | expected_result); |
| 581 | if (expected_result == 0) { |
| 582 | TEST_ASSERT(p == input->x + input->len); |
| 583 | |
| 584 | if (!*rest) { |
| 585 | TEST_EQUAL(head.buf.tag, 0); |
| 586 | TEST_ASSERT(head.buf.p == NULL); |
| 587 | TEST_EQUAL(head.buf.len, 0); |
| 588 | TEST_ASSERT(head.next == NULL); |
| 589 | } else { |
| 590 | cur = &head; |
| 591 | while (*rest) { |
| 592 | mbedtls_test_set_step(step); |
| 593 | TEST_ASSERT(cur != NULL); |
| 594 | TEST_EQUAL(cur->buf.tag, tag); |
| 595 | n = strtoul(rest, (char **) &rest, 0); |
| 596 | TEST_EQUAL(n, (size_t) (cur->buf.p - input->x)); |
| 597 | ++rest; |
| 598 | n = strtoul(rest, (char **) &rest, 0); |
| 599 | TEST_EQUAL(n, cur->buf.len); |
| 600 | if (*rest) { |
| 601 | ++rest; |
| 602 | } |
| 603 | cur = cur->next; |
| 604 | ++step; |
| 605 | } |
| 606 | TEST_ASSERT(cur == NULL); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | exit: |
| 611 | mbedtls_asn1_sequence_free(head.next); |
| 612 | } |
| 613 | /* END_CASE */ |
| 614 | |
| 615 | /* BEGIN_CASE */ |
| 616 | void traverse_sequence_of(const data_t *input, |
| 617 | int tag_must_mask, int tag_must_val, |
| 618 | int tag_may_mask, int tag_may_val, |
| 619 | const char *description, |
| 620 | int expected_result) |
| 621 | { |
| 622 | /* The description string is a comma-separated list of integers. |
| 623 | * For each element in the SEQUENCE in input, description contains |
| 624 | * three integers: the offset of the element's content (offset from |
| 625 | * the start of input to the content of the element), the element's tag, |
| 626 | * and the length of the element's contents. |
| 627 | * "offset1,tag1,length1,..." */ |
| 628 | |
| 629 | unsigned char *p = input->x; |
| 630 | traverse_state_t traverse_state = { input->x, description }; |
| 631 | int ret; |
| 632 | |
| 633 | ret = mbedtls_asn1_traverse_sequence_of(&p, input->x + input->len, |
| 634 | (uint8_t) tag_must_mask, (uint8_t) tag_must_val, |
| 635 | (uint8_t) tag_may_mask, (uint8_t) tag_may_val, |
| 636 | traverse_callback, &traverse_state); |
| 637 | if (ret == RET_TRAVERSE_ERROR) { |
| 638 | goto exit; |
| 639 | } |
| 640 | TEST_EQUAL(ret, expected_result); |
| 641 | TEST_EQUAL(*traverse_state.description, 0); |
| 642 | } |
| 643 | /* END_CASE */ |
| 644 | |
| 645 | /* BEGIN_CASE */ |
| 646 | void get_alg(const data_t *input, |
| 647 | int oid_offset, int oid_length, |
| 648 | int params_tag, int params_offset, int params_length, |
| 649 | int total_length, |
| 650 | int expected_result) |
| 651 | { |
| 652 | mbedtls_asn1_buf oid = { -1, 0, NULL }; |
| 653 | mbedtls_asn1_buf params = { -1, 0, NULL }; |
| 654 | unsigned char *p = input->x; |
| 655 | int ret; |
| 656 | |
| 657 | TEST_EQUAL(mbedtls_asn1_get_alg(&p, input->x + input->len, |
| 658 | &oid, ¶ms), |
| 659 | expected_result); |
| 660 | if (expected_result == 0) { |
| 661 | TEST_EQUAL(oid.tag, MBEDTLS_ASN1_OID); |
| 662 | TEST_EQUAL(oid.p - input->x, oid_offset); |
| 663 | TEST_EQUAL(oid.len, (size_t) oid_length); |
| 664 | TEST_EQUAL(params.tag, params_tag); |
| 665 | if (params_offset != 0) { |
| 666 | TEST_EQUAL(params.p - input->x, params_offset); |
| 667 | } else { |
| 668 | TEST_ASSERT(params.p == NULL); |
| 669 | } |
| 670 | TEST_EQUAL(params.len, (size_t) params_length); |
| 671 | TEST_EQUAL(p - input->x, total_length); |
| 672 | } |
| 673 | |
| 674 | ret = mbedtls_asn1_get_alg_null(&p, input->x + input->len, &oid); |
| 675 | if (expected_result == 0 && params_offset == 0) { |
| 676 | TEST_EQUAL(oid.tag, MBEDTLS_ASN1_OID); |
| 677 | TEST_EQUAL(oid.p - input->x, oid_offset); |
| 678 | TEST_EQUAL(oid.len, (size_t) oid_length); |
| 679 | TEST_EQUAL(p - input->x, total_length); |
| 680 | } else { |
| 681 | TEST_ASSERT(ret != 0); |
| 682 | } |
| 683 | } |
| 684 | /* END_CASE */ |
| 685 | |
| 686 | /* BEGIN_CASE */ |
| 687 | void find_named_data(data_t *oid0, data_t *oid1, data_t *oid2, data_t *oid3, |
| 688 | data_t *needle, int from, int position) |
| 689 | { |
| 690 | mbedtls_asn1_named_data nd[] = { |
| 691 | { { 0x06, oid0->len, oid0->x }, { 0, 0, NULL }, NULL, 0 }, |
| 692 | { { 0x06, oid1->len, oid1->x }, { 0, 0, NULL }, NULL, 0 }, |
| 693 | { { 0x06, oid2->len, oid2->x }, { 0, 0, NULL }, NULL, 0 }, |
| 694 | { { 0x06, oid3->len, oid3->x }, { 0, 0, NULL }, NULL, 0 }, |
| 695 | }; |
| 696 | mbedtls_asn1_named_data *pointers[ARRAY_LENGTH(nd) + 1]; |
| 697 | size_t i; |
| 698 | const mbedtls_asn1_named_data *found; |
| 699 | |
| 700 | for (i = 0; i < ARRAY_LENGTH(nd); i++) { |
| 701 | pointers[i] = &nd[i]; |
| 702 | } |
| 703 | pointers[ARRAY_LENGTH(nd)] = NULL; |
| 704 | for (i = 0; i < ARRAY_LENGTH(nd); i++) { |
| 705 | nd[i].next = pointers[i+1]; |
| 706 | } |
| 707 | |
| 708 | found = mbedtls_asn1_find_named_data((const mbedtls_asn1_named_data *) pointers[from], |
| 709 | (const char *) needle->x, |
| 710 | needle->len); |
| 711 | TEST_ASSERT(found == pointers[position]); |
| 712 | } |
| 713 | /* END_CASE */ |
| 714 | |
| 715 | /* BEGIN_CASE depends_on:!MBEDTLS_DEPRECATED_REMOVED:!MBEDTLS_DEPRECATED_WARNING */ |
| 716 | void free_named_data_null() |
| 717 | { |
| 718 | mbedtls_asn1_free_named_data(NULL); |
| 719 | goto exit; /* Silence unused label warning */ |
| 720 | } |
| 721 | /* END_CASE */ |
| 722 | |
| 723 | /* BEGIN_CASE depends_on:!MBEDTLS_DEPRECATED_REMOVED:!MBEDTLS_DEPRECATED_WARNING */ |
| 724 | void free_named_data(int with_oid, int with_val, int with_next) |
| 725 | { |
| 726 | mbedtls_asn1_named_data next = |
| 727 | { { 0x06, 0, NULL }, { 0, 0xcafe, NULL }, NULL, 0 }; |
| 728 | mbedtls_asn1_named_data head = |
| 729 | { { 0x06, 0, NULL }, { 0, 0, NULL }, NULL, 0 }; |
| 730 | |
| 731 | if (with_oid) { |
| 732 | TEST_CALLOC(head.oid.p, 1); |
| 733 | } |
| 734 | if (with_val) { |
| 735 | TEST_CALLOC(head.val.p, 1); |
| 736 | } |
| 737 | if (with_next) { |
| 738 | head.next = &next; |
| 739 | } |
| 740 | |
| 741 | mbedtls_asn1_free_named_data(&head); |
| 742 | TEST_ASSERT(head.oid.p == NULL); |
| 743 | TEST_ASSERT(head.val.p == NULL); |
| 744 | TEST_ASSERT(head.next == NULL); |
| 745 | TEST_ASSERT(next.val.len == 0xcafe); |
| 746 | |
| 747 | exit: |
| 748 | mbedtls_free(head.oid.p); |
| 749 | mbedtls_free(head.val.p); |
| 750 | } |
| 751 | /* END_CASE */ |
| 752 | |
| 753 | /* BEGIN_CASE */ |
| 754 | void free_named_data_list(int length) |
| 755 | { |
| 756 | mbedtls_asn1_named_data *head = NULL; |
| 757 | int i; |
| 758 | |
| 759 | for (i = 0; i < length; i++) { |
| 760 | mbedtls_asn1_named_data *new = NULL; |
| 761 | TEST_CALLOC(new, 1); |
| 762 | new->next = head; |
| 763 | head = new; |
| 764 | } |
| 765 | |
| 766 | mbedtls_asn1_free_named_data_list(&head); |
| 767 | TEST_ASSERT(head == NULL); |
| 768 | /* Most of the point of the test is that it doesn't leak memory. |
| 769 | * So this test is only really useful under a memory leak detection |
| 770 | * framework. */ |
| 771 | exit: |
| 772 | mbedtls_asn1_free_named_data_list(&head); |
| 773 | } |
| 774 | /* END_CASE */ |