diff options
| author | doug <> | 2015-02-16 06:48:17 +0000 |
|---|---|---|
| committer | doug <> | 2015-02-16 06:48:17 +0000 |
| commit | 7b9469d42e3e55b355bdc3e71d8893d3ca583be1 (patch) | |
| tree | 1309b4b368b0b263a103ed534dd2574da085d416 | |
| parent | 191c62e76a1c0617acb040a77924d270b58dcd9b (diff) | |
| download | openbsd-7b9469d42e3e55b355bdc3e71d8893d3ca583be1.tar.gz openbsd-7b9469d42e3e55b355bdc3e71d8893d3ca583be1.tar.bz2 openbsd-7b9469d42e3e55b355bdc3e71d8893d3ca583be1.zip | |
Add more error checking and free resources in bytestringtest.
Diffstat (limited to '')
| -rw-r--r-- | src/regress/lib/libssl/bytestring/bytestringtest.c | 73 |
1 files changed, 47 insertions, 26 deletions
diff --git a/src/regress/lib/libssl/bytestring/bytestringtest.c b/src/regress/lib/libssl/bytestring/bytestringtest.c index ba768e4bb0..8269151127 100644 --- a/src/regress/lib/libssl/bytestring/bytestringtest.c +++ b/src/regress/lib/libssl/bytestring/bytestringtest.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: bytestringtest.c,v 1.2 2015/02/06 22:22:33 doug Exp $ */ | 1 | /* $OpenBSD: bytestringtest.c,v 1.3 2015/02/16 06:48:17 doug Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2014, Google Inc. | 3 | * Copyright (c) 2014, Google Inc. |
| 4 | * | 4 | * |
| @@ -314,8 +314,9 @@ static int | |||
| 314 | test_cbb_finish_child(void) | 314 | test_cbb_finish_child(void) |
| 315 | { | 315 | { |
| 316 | CBB cbb, child; | 316 | CBB cbb, child; |
| 317 | uint8_t *out_buf; | 317 | uint8_t *out_buf = NULL; |
| 318 | size_t out_size; | 318 | size_t out_size; |
| 319 | int ret = 0; | ||
| 319 | 320 | ||
| 320 | if (!CBB_init(&cbb, 16) || | 321 | if (!CBB_init(&cbb, 16) || |
| 321 | !CBB_add_u8_length_prefixed(&cbb, &child) || | 322 | !CBB_add_u8_length_prefixed(&cbb, &child) || |
| @@ -323,10 +324,13 @@ test_cbb_finish_child(void) | |||
| 323 | !CBB_finish(&cbb, &out_buf, &out_size) || | 324 | !CBB_finish(&cbb, &out_buf, &out_size) || |
| 324 | out_size != 1 || | 325 | out_size != 1 || |
| 325 | out_buf[0] != 0) | 326 | out_buf[0] != 0) |
| 326 | return 0; | 327 | goto err; |
| 327 | 328 | ||
| 329 | ret = 1; | ||
| 330 | |||
| 331 | err: | ||
| 328 | free(out_buf); | 332 | free(out_buf); |
| 329 | return 1; | 333 | return ret; |
| 330 | } | 334 | } |
| 331 | 335 | ||
| 332 | static int | 336 | static int |
| @@ -334,10 +338,10 @@ test_cbb_prefixed(void) | |||
| 334 | { | 338 | { |
| 335 | static const uint8_t kExpected[] = {0, 1, 1, 0, 2, 2, 3, 0, 0, 3, | 339 | static const uint8_t kExpected[] = {0, 1, 1, 0, 2, 2, 3, 0, 0, 3, |
| 336 | 4, 5, 6, 5, 4, 1, 0, 1, 2}; | 340 | 4, 5, 6, 5, 4, 1, 0, 1, 2}; |
| 337 | uint8_t *buf; | 341 | uint8_t *buf = NULL; |
| 338 | size_t buf_len; | 342 | size_t buf_len; |
| 339 | CBB cbb, contents, inner_contents, inner_inner_contents; | 343 | CBB cbb, contents, inner_contents, inner_inner_contents; |
| 340 | int ok; | 344 | int ok = 0; |
| 341 | 345 | ||
| 342 | if (!CBB_init(&cbb, 0) || | 346 | if (!CBB_init(&cbb, 0) || |
| 343 | !CBB_add_u8_length_prefixed(&cbb, &contents) || | 347 | !CBB_add_u8_length_prefixed(&cbb, &contents) || |
| @@ -354,10 +358,12 @@ test_cbb_prefixed(void) | |||
| 354 | &inner_inner_contents) || | 358 | &inner_inner_contents) || |
| 355 | !CBB_add_u8(&inner_inner_contents, 2) || | 359 | !CBB_add_u8(&inner_inner_contents, 2) || |
| 356 | !CBB_finish(&cbb, &buf, &buf_len)) | 360 | !CBB_finish(&cbb, &buf, &buf_len)) |
| 357 | return 0; | 361 | goto err; |
| 358 | 362 | ||
| 359 | ok = buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len) | 363 | ok = buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len) |
| 360 | == 0; | 364 | == 0; |
| 365 | |||
| 366 | err: | ||
| 361 | free(buf); | 367 | free(buf); |
| 362 | return ok; | 368 | return ok; |
| 363 | } | 369 | } |
| @@ -366,8 +372,9 @@ static int | |||
| 366 | test_cbb_misuse(void) | 372 | test_cbb_misuse(void) |
| 367 | { | 373 | { |
| 368 | CBB cbb, child, contents; | 374 | CBB cbb, child, contents; |
| 369 | uint8_t *buf; | 375 | uint8_t *buf = NULL; |
| 370 | size_t buf_len; | 376 | size_t buf_len; |
| 377 | int ret = 0; | ||
| 371 | 378 | ||
| 372 | if (!CBB_init(&cbb, 0) || | 379 | if (!CBB_init(&cbb, 0) || |
| 373 | !CBB_add_u8_length_prefixed(&cbb, &child) || | 380 | !CBB_add_u8_length_prefixed(&cbb, &child) || |
| @@ -392,78 +399,88 @@ test_cbb_misuse(void) | |||
| 392 | 399 | ||
| 393 | if (!CBB_finish(&cbb, &buf, &buf_len) || buf_len != 3 || | 400 | if (!CBB_finish(&cbb, &buf, &buf_len) || buf_len != 3 || |
| 394 | memcmp(buf, "\x01\x01\x02", 3) != 0) | 401 | memcmp(buf, "\x01\x01\x02", 3) != 0) |
| 395 | return 0; | 402 | goto err; |
| 396 | 403 | ||
| 397 | free(buf); | 404 | ret = 1; |
| 398 | 405 | ||
| 399 | return 1; | 406 | err: |
| 407 | free(buf); | ||
| 408 | return ret; | ||
| 400 | } | 409 | } |
| 401 | 410 | ||
| 402 | static int | 411 | static int |
| 403 | test_cbb_asn1(void) | 412 | test_cbb_asn1(void) |
| 404 | { | 413 | { |
| 405 | static const uint8_t kExpected[] = {0x30, 3, 1, 2, 3}; | 414 | static const uint8_t kExpected[] = {0x30, 3, 1, 2, 3}; |
| 406 | uint8_t *buf, *test_data; | 415 | uint8_t *buf = NULL, *test_data = NULL; |
| 407 | size_t buf_len; | 416 | size_t buf_len; |
| 408 | CBB cbb, contents, inner_contents; | 417 | CBB cbb, contents, inner_contents; |
| 418 | int ret = 0; | ||
| 409 | 419 | ||
| 410 | if (!CBB_init(&cbb, 0) || | 420 | if (!CBB_init(&cbb, 0) || |
| 411 | !CBB_add_asn1(&cbb, &contents, 0x30) || | 421 | !CBB_add_asn1(&cbb, &contents, 0x30) || |
| 412 | !CBB_add_bytes(&contents, (const uint8_t*) "\x01\x02\x03", 3) || | 422 | !CBB_add_bytes(&contents, (const uint8_t*) "\x01\x02\x03", 3) || |
| 413 | !CBB_finish(&cbb, &buf, &buf_len)) | 423 | !CBB_finish(&cbb, &buf, &buf_len)) |
| 414 | return 0; | 424 | goto err; |
| 415 | 425 | ||
| 416 | if (buf_len != sizeof(kExpected) || memcmp(buf, kExpected, buf_len) | 426 | if (buf_len != sizeof(kExpected) || memcmp(buf, kExpected, buf_len) |
| 417 | != 0) | 427 | != 0) |
| 418 | return 0; | 428 | goto err; |
| 419 | 429 | ||
| 420 | free(buf); | 430 | free(buf); |
| 431 | buf = NULL; | ||
| 421 | 432 | ||
| 422 | test_data = malloc(100000); | 433 | if ((test_data = malloc(100000)) == NULL) |
| 434 | goto err; | ||
| 423 | memset(test_data, 0x42, 100000); | 435 | memset(test_data, 0x42, 100000); |
| 424 | 436 | ||
| 425 | if (!CBB_init(&cbb, 0) || | 437 | if (!CBB_init(&cbb, 0) || |
| 426 | !CBB_add_asn1(&cbb, &contents, 0x30) || | 438 | !CBB_add_asn1(&cbb, &contents, 0x30) || |
| 427 | !CBB_add_bytes(&contents, test_data, 130) || | 439 | !CBB_add_bytes(&contents, test_data, 130) || |
| 428 | !CBB_finish(&cbb, &buf, &buf_len)) | 440 | !CBB_finish(&cbb, &buf, &buf_len)) |
| 429 | return 0; | 441 | goto err; |
| 430 | 442 | ||
| 431 | if (buf_len != 3 + 130 || | 443 | if (buf_len != 3 + 130 || |
| 432 | memcmp(buf, "\x30\x81\x82", 3) != 0 || | 444 | memcmp(buf, "\x30\x81\x82", 3) != 0 || |
| 433 | memcmp(buf + 3, test_data, 130) != 0) { | 445 | memcmp(buf + 3, test_data, 130) != 0) { |
| 434 | return 0; | 446 | goto err; |
| 435 | } | 447 | } |
| 436 | free(buf); | 448 | free(buf); |
| 449 | buf = NULL; | ||
| 437 | 450 | ||
| 438 | if (!CBB_init(&cbb, 0) || | 451 | if (!CBB_init(&cbb, 0) || |
| 439 | !CBB_add_asn1(&cbb, &contents, 0x30) || | 452 | !CBB_add_asn1(&cbb, &contents, 0x30) || |
| 440 | !CBB_add_bytes(&contents, test_data, 1000) || | 453 | !CBB_add_bytes(&contents, test_data, 1000) || |
| 441 | !CBB_finish(&cbb, &buf, &buf_len)) | 454 | !CBB_finish(&cbb, &buf, &buf_len)) |
| 442 | return 0; | 455 | goto err; |
| 443 | 456 | ||
| 444 | if (buf_len != 4 + 1000 || | 457 | if (buf_len != 4 + 1000 || |
| 445 | memcmp(buf, "\x30\x82\x03\xe8", 4) != 0 || | 458 | memcmp(buf, "\x30\x82\x03\xe8", 4) != 0 || |
| 446 | memcmp(buf + 4, test_data, 1000)) { | 459 | memcmp(buf + 4, test_data, 1000)) { |
| 447 | return 0; | 460 | goto err; |
| 448 | } | 461 | } |
| 449 | free(buf); | 462 | free(buf); |
| 463 | buf = NULL; | ||
| 450 | 464 | ||
| 451 | if (!CBB_init(&cbb, 0) || | 465 | if (!CBB_init(&cbb, 0) || |
| 452 | !CBB_add_asn1(&cbb, &contents, 0x30) || | 466 | !CBB_add_asn1(&cbb, &contents, 0x30) || |
| 453 | !CBB_add_asn1(&contents, &inner_contents, 0x30) || | 467 | !CBB_add_asn1(&contents, &inner_contents, 0x30) || |
| 454 | !CBB_add_bytes(&inner_contents, test_data, 100000) || | 468 | !CBB_add_bytes(&inner_contents, test_data, 100000) || |
| 455 | !CBB_finish(&cbb, &buf, &buf_len)) | 469 | !CBB_finish(&cbb, &buf, &buf_len)) |
| 456 | return 0; | 470 | goto err; |
| 457 | 471 | ||
| 458 | if (buf_len != 5 + 5 + 100000 || | 472 | if (buf_len != 5 + 5 + 100000 || |
| 459 | memcmp(buf, "\x30\x83\x01\x86\xa5\x30\x83\x01\x86\xa0", 10) != 0 || | 473 | memcmp(buf, "\x30\x83\x01\x86\xa5\x30\x83\x01\x86\xa0", 10) != 0 || |
| 460 | memcmp(buf + 10, test_data, 100000)) | 474 | memcmp(buf + 10, test_data, 100000)) |
| 461 | return 0; | 475 | goto err; |
| 462 | 476 | ||
| 477 | ret = 1; | ||
| 478 | |||
| 479 | err: | ||
| 463 | free(buf); | 480 | free(buf); |
| 464 | free(test_data); | 481 | free(test_data); |
| 465 | 482 | ||
| 466 | return 1; | 483 | return ret; |
| 467 | } | 484 | } |
| 468 | 485 | ||
| 469 | static int | 486 | static int |
| @@ -471,13 +488,14 @@ do_ber_convert(const char *name, const uint8_t *der_expected, size_t der_len, | |||
| 471 | const uint8_t *ber, size_t ber_len) | 488 | const uint8_t *ber, size_t ber_len) |
| 472 | { | 489 | { |
| 473 | CBS in; | 490 | CBS in; |
| 474 | uint8_t *out; | 491 | uint8_t *out = NULL; |
| 475 | size_t out_len; | 492 | size_t out_len; |
| 493 | int ret = 0; | ||
| 476 | 494 | ||
| 477 | CBS_init(&in, ber, ber_len); | 495 | CBS_init(&in, ber, ber_len); |
| 478 | if (!CBS_asn1_ber_to_der(&in, &out, &out_len)) { | 496 | if (!CBS_asn1_ber_to_der(&in, &out, &out_len)) { |
| 479 | fprintf(stderr, "%s: CBS_asn1_ber_to_der failed.\n", name); | 497 | fprintf(stderr, "%s: CBS_asn1_ber_to_der failed.\n", name); |
| 480 | return 0; | 498 | goto end; |
| 481 | } | 499 | } |
| 482 | 500 | ||
| 483 | if (out == NULL) { | 501 | if (out == NULL) { |
| @@ -493,11 +511,14 @@ do_ber_convert(const char *name, const uint8_t *der_expected, size_t der_len, | |||
| 493 | 511 | ||
| 494 | if (out_len != der_len || memcmp(out, der_expected, der_len) != 0) { | 512 | if (out_len != der_len || memcmp(out, der_expected, der_len) != 0) { |
| 495 | fprintf(stderr, "%s: incorrect converted result.\n", name); | 513 | fprintf(stderr, "%s: incorrect converted result.\n", name); |
| 496 | return 0; | 514 | goto end; |
| 497 | } | 515 | } |
| 498 | 516 | ||
| 517 | ret = 1; | ||
| 518 | |||
| 519 | end: | ||
| 499 | free(out); | 520 | free(out); |
| 500 | return 1; | 521 | return ret; |
| 501 | } | 522 | } |
| 502 | 523 | ||
| 503 | static int | 524 | static int |
