summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authormiod <>2014-05-19 20:09:15 +0000
committermiod <>2014-05-19 20:09:15 +0000
commit71e22b3fafe9cd4167c81a927e41cd9a06077d02 (patch)
tree93b42c350012f1dd3f4b0a0d0352e2be8c1dc9cd /src/lib
parentc31c3892883088d706d5f4d261636c1b85f051a5 (diff)
downloadopenbsd-71e22b3fafe9cd4167c81a927e41cd9a06077d02.tar.gz
openbsd-71e22b3fafe9cd4167c81a927e41cd9a06077d02.tar.bz2
openbsd-71e22b3fafe9cd4167c81a927e41cd9a06077d02.zip
Fix several bounds checks in ssl_add_clienthello_tlsext() and
ssl_add_serverhello_tlsext(), and convert all of them to the same idiom, for easier review. Math is hard, let's go webshopping. Help and ok guenther@
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libssl/src/ssl/t1_lib.c73
-rw-r--r--src/lib/libssl/t1_lib.c73
2 files changed, 78 insertions, 68 deletions
diff --git a/src/lib/libssl/src/ssl/t1_lib.c b/src/lib/libssl/src/ssl/t1_lib.c
index 2e183bb233..54f536917e 100644
--- a/src/lib/libssl/src/ssl/t1_lib.c
+++ b/src/lib/libssl/src/ssl/t1_lib.c
@@ -361,20 +361,22 @@ ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
361 361
362 if (s->tlsext_hostname != NULL) { 362 if (s->tlsext_hostname != NULL) {
363 /* Add TLS extension servername to the Client Hello message */ 363 /* Add TLS extension servername to the Client Hello message */
364 unsigned long size_str; 364 size_t size_str, lenmax;
365 long lenmax;
366 365
367 366
368 /* check for enough space. 367 /* check for enough space.
369 4 for the servername type and entension length 368 4 for the servername type and extension length
370 2 for servernamelist length 369 2 for servernamelist length
371 1 for the hostname type 370 1 for the hostname type
372 2 for hostname length 371 2 for hostname length
373 + hostname length 372 + hostname length
374 */ 373 */
375 374
376 if ((lenmax = limit - ret - 9) < 0 || 375 if ((size_t)(limit - ret) < 9)
377 (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax) 376 return NULL;
377
378 lenmax = limit - ret - 9;
379 if ((size_str = strlen(s->tlsext_hostname)) > lenmax)
378 return NULL; 380 return NULL;
379 381
380 /* extension type and length */ 382 /* extension type and length */
@@ -401,7 +403,7 @@ ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
401 return NULL; 403 return NULL;
402 } 404 }
403 405
404 if ((limit - p - 4 - el) < 0) 406 if ((size_t)(limit - ret) < 4 + el)
405 return NULL; 407 return NULL;
406 408
407 s2n(TLSEXT_TYPE_renegotiate, ret); 409 s2n(TLSEXT_TYPE_renegotiate, ret);
@@ -420,12 +422,13 @@ ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
420 if (s->tlsext_ecpointformatlist != NULL && 422 if (s->tlsext_ecpointformatlist != NULL &&
421 s->version != DTLS1_VERSION) { 423 s->version != DTLS1_VERSION) {
422 /* Add TLS extension ECPointFormats to the ClientHello message */ 424 /* Add TLS extension ECPointFormats to the ClientHello message */
423 long lenmax; 425 size_t lenmax;
424 426
425 if ((lenmax = limit - ret - 5) < 0) 427 if ((size_t)(limit - ret) < 5)
426 return NULL; 428 return NULL;
427 429
428 if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) 430 lenmax = limit - ret - 5;
431 if (s->tlsext_ecpointformatlist_length > lenmax)
429 return NULL; 432 return NULL;
430 if (s->tlsext_ecpointformatlist_length > 255) { 433 if (s->tlsext_ecpointformatlist_length > 255) {
431 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); 434 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
@@ -441,13 +444,15 @@ ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
441 if (s->tlsext_ellipticcurvelist != NULL && 444 if (s->tlsext_ellipticcurvelist != NULL &&
442 s->version != DTLS1_VERSION) { 445 s->version != DTLS1_VERSION) {
443 /* Add TLS extension EllipticCurves to the ClientHello message */ 446 /* Add TLS extension EllipticCurves to the ClientHello message */
444 long lenmax; 447 size_t lenmax;
445 448
446 if ((lenmax = limit - ret - 6) 449 if ((size_t)(limit - ret) < 6)
447 < 0) return NULL; 450 return NULL;
448 451
449 if (s->tlsext_ellipticcurvelist_length > (unsigned long)lenmax) return NULL; 452 lenmax = limit - ret - 6;
450 if (s->tlsext_ellipticcurvelist_length > 65532) { 453 if (s->tlsext_ellipticcurvelist_length > lenmax)
454 return NULL;
455 if (s->tlsext_ellipticcurvelist_length > 65532) {
451 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); 456 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
452 return NULL; 457 return NULL;
453 } 458 }
@@ -487,7 +492,7 @@ ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
487 /* Check for enough room 2 for extension type, 2 for len 492 /* Check for enough room 2 for extension type, 2 for len
488 * rest for ticket 493 * rest for ticket
489 */ 494 */
490 if ((long)(limit - ret - 4 - ticklen) < 0) 495 if ((size_t)(limit - ret) < 4 + ticklen)
491 return NULL; 496 return NULL;
492 s2n(TLSEXT_TYPE_session_ticket, ret); 497 s2n(TLSEXT_TYPE_session_ticket, ret);
493 498
@@ -512,10 +517,10 @@ skip_ext:
512 517
513#ifdef TLSEXT_TYPE_opaque_prf_input 518#ifdef TLSEXT_TYPE_opaque_prf_input
514 if (s->s3->client_opaque_prf_input != NULL && 519 if (s->s3->client_opaque_prf_input != NULL &&
515 s->version != DTLS1_VERSION) { 520 s->version != DTLS1_VERSION) {
516 size_t col = s->s3->client_opaque_prf_input_len; 521 size_t col = s->s3->client_opaque_prf_input_len;
517 522
518 if ((long)(limit - ret - 6 - col < 0)) 523 if ((size_t)(limit - ret) < 6 + col)
519 return NULL; 524 return NULL;
520 if (col > 0xFFFD) /* can't happen */ 525 if (col > 0xFFFD) /* can't happen */
521 return NULL; 526 return NULL;
@@ -551,7 +556,7 @@ skip_ext:
551 } else 556 } else
552 extlen = 0; 557 extlen = 0;
553 558
554 if ((long)(limit - ret - 7 - extlen - idlen) < 0) 559 if ((size_t)(limit - ret) < 7 + extlen + idlen)
555 return NULL; 560 return NULL;
556 s2n(TLSEXT_TYPE_status_request, ret); 561 s2n(TLSEXT_TYPE_status_request, ret);
557 if (extlen + idlen > 0xFFF0) 562 if (extlen + idlen > 0xFFF0)
@@ -578,7 +583,7 @@ skip_ext:
578 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) { 583 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {
579 /* The client advertises an emtpy extension to indicate its 584 /* The client advertises an emtpy extension to indicate its
580 * support for Next Protocol Negotiation */ 585 * support for Next Protocol Negotiation */
581 if (limit - ret - 4 < 0) 586 if ((size_t)(limit - ret) < 4)
582 return NULL; 587 return NULL;
583 s2n(TLSEXT_TYPE_next_proto_neg, ret); 588 s2n(TLSEXT_TYPE_next_proto_neg, ret);
584 s2n(0, ret); 589 s2n(0, ret);
@@ -591,7 +596,7 @@ skip_ext:
591 596
592 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0); 597 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
593 598
594 if ((limit - p - 4 - el) < 0) 599 if ((size_t)(limit - ret) < 4 + el)
595 return NULL; 600 return NULL;
596 601
597 s2n(TLSEXT_TYPE_use_srtp, ret); 602 s2n(TLSEXT_TYPE_use_srtp, ret);
@@ -659,7 +664,7 @@ ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
659 return NULL; /* this really never occurs, but ... */ 664 return NULL; /* this really never occurs, but ... */
660 665
661 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL) { 666 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL) {
662 if ((long)(limit - ret - 4) < 0) 667 if ((size_t)(limit - ret) < 4)
663 return NULL; 668 return NULL;
664 669
665 s2n(TLSEXT_TYPE_server_name, ret); 670 s2n(TLSEXT_TYPE_server_name, ret);
@@ -674,8 +679,8 @@ ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
674 return NULL; 679 return NULL;
675 } 680 }
676 681
677 if ((limit - p - 4 - el) 682 if ((size_t)(limit - ret) < 4 + el)
678 < 0) return NULL; 683 return NULL;
679 684
680 s2n(TLSEXT_TYPE_renegotiate, ret); 685 s2n(TLSEXT_TYPE_renegotiate, ret);
681 s2n(el, ret); 686 s2n(el, ret);
@@ -692,13 +697,13 @@ ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
692 if (s->tlsext_ecpointformatlist != NULL && 697 if (s->tlsext_ecpointformatlist != NULL &&
693 s->version != DTLS1_VERSION) { 698 s->version != DTLS1_VERSION) {
694 /* Add TLS extension ECPointFormats to the ServerHello message */ 699 /* Add TLS extension ECPointFormats to the ServerHello message */
695 long lenmax; 700 size_t lenmax;
696 701
702 if ((size_t)(limit - ret) < 5)
703 return NULL;
697 704
698 if ((lenmax = limit - ret - 5) 705 lenmax = limit - ret - 5;
699 < 0) return NULL; 706 if (s->tlsext_ecpointformatlist_length > lenmax)
700
701 if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax)
702 return NULL; 707 return NULL;
703 if (s->tlsext_ecpointformatlist_length > 255) { 708 if (s->tlsext_ecpointformatlist_length > 255) {
704 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); 709 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
@@ -716,7 +721,7 @@ ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
716#endif /* OPENSSL_NO_EC */ 721#endif /* OPENSSL_NO_EC */
717 722
718 if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) { 723 if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
719 if ((long)(limit - ret - 4) < 0) 724 if ((size_t)(limit - ret) < 4)
720 return NULL; 725 return NULL;
721 726
722 s2n(TLSEXT_TYPE_session_ticket, ret); 727 s2n(TLSEXT_TYPE_session_ticket, ret);
@@ -724,7 +729,7 @@ ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
724 } 729 }
725 730
726 if (s->tlsext_status_expected) { 731 if (s->tlsext_status_expected) {
727 if ((long)(limit - ret - 4) < 0) 732 if ((size_t)(limit - ret) < 4)
728 return NULL; 733 return NULL;
729 734
730 s2n(TLSEXT_TYPE_status_request, ret); 735 s2n(TLSEXT_TYPE_status_request, ret);
@@ -735,7 +740,7 @@ ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
735 if (s->s3->server_opaque_prf_input != NULL && s->version != DTLS1_VERSION) { 740 if (s->s3->server_opaque_prf_input != NULL && s->version != DTLS1_VERSION) {
736 size_t sol = s->s3->server_opaque_prf_input_len; 741 size_t sol = s->s3->server_opaque_prf_input_len;
737 742
738 if ((long)(limit - ret - 6 - sol) < 0) 743 if ((size_t)(limit - ret) < 6 + sol)
739 return NULL; 744 return NULL;
740 if (sol > 0xFFFD) /* can't happen */ 745 if (sol > 0xFFFD) /* can't happen */
741 return NULL; 746 return NULL;
@@ -755,7 +760,7 @@ ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
755 760
756 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0); 761 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
757 762
758 if ((limit - p - 4 - el) < 0) 763 if ((size_t)(limit - ret) < 4 + el)
759 return NULL; 764 return NULL;
760 765
761 s2n(TLSEXT_TYPE_use_srtp, ret); 766 s2n(TLSEXT_TYPE_use_srtp, ret);
@@ -780,7 +785,7 @@ ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
780 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08, 785 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
781 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17 786 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
782 }; 787 };
783 if (limit - ret < 36) 788 if ((size_t)(limit - ret) < 36)
784 return NULL; 789 return NULL;
785 memcpy(ret, cryptopro_ext, 36); 790 memcpy(ret, cryptopro_ext, 36);
786 ret += 36; 791 ret += 36;
@@ -796,7 +801,7 @@ ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
796 801
797 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg); 802 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
798 if (r == SSL_TLSEXT_ERR_OK) { 803 if (r == SSL_TLSEXT_ERR_OK) {
799 if ((long)(limit - ret - 4 - npalen) < 0) 804 if ((size_t)(limit - ret) < 4 + npalen)
800 return NULL; 805 return NULL;
801 s2n(TLSEXT_TYPE_next_proto_neg, ret); 806 s2n(TLSEXT_TYPE_next_proto_neg, ret);
802 s2n(npalen, ret); 807 s2n(npalen, ret);
diff --git a/src/lib/libssl/t1_lib.c b/src/lib/libssl/t1_lib.c
index 2e183bb233..54f536917e 100644
--- a/src/lib/libssl/t1_lib.c
+++ b/src/lib/libssl/t1_lib.c
@@ -361,20 +361,22 @@ ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
361 361
362 if (s->tlsext_hostname != NULL) { 362 if (s->tlsext_hostname != NULL) {
363 /* Add TLS extension servername to the Client Hello message */ 363 /* Add TLS extension servername to the Client Hello message */
364 unsigned long size_str; 364 size_t size_str, lenmax;
365 long lenmax;
366 365
367 366
368 /* check for enough space. 367 /* check for enough space.
369 4 for the servername type and entension length 368 4 for the servername type and extension length
370 2 for servernamelist length 369 2 for servernamelist length
371 1 for the hostname type 370 1 for the hostname type
372 2 for hostname length 371 2 for hostname length
373 + hostname length 372 + hostname length
374 */ 373 */
375 374
376 if ((lenmax = limit - ret - 9) < 0 || 375 if ((size_t)(limit - ret) < 9)
377 (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax) 376 return NULL;
377
378 lenmax = limit - ret - 9;
379 if ((size_str = strlen(s->tlsext_hostname)) > lenmax)
378 return NULL; 380 return NULL;
379 381
380 /* extension type and length */ 382 /* extension type and length */
@@ -401,7 +403,7 @@ ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
401 return NULL; 403 return NULL;
402 } 404 }
403 405
404 if ((limit - p - 4 - el) < 0) 406 if ((size_t)(limit - ret) < 4 + el)
405 return NULL; 407 return NULL;
406 408
407 s2n(TLSEXT_TYPE_renegotiate, ret); 409 s2n(TLSEXT_TYPE_renegotiate, ret);
@@ -420,12 +422,13 @@ ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
420 if (s->tlsext_ecpointformatlist != NULL && 422 if (s->tlsext_ecpointformatlist != NULL &&
421 s->version != DTLS1_VERSION) { 423 s->version != DTLS1_VERSION) {
422 /* Add TLS extension ECPointFormats to the ClientHello message */ 424 /* Add TLS extension ECPointFormats to the ClientHello message */
423 long lenmax; 425 size_t lenmax;
424 426
425 if ((lenmax = limit - ret - 5) < 0) 427 if ((size_t)(limit - ret) < 5)
426 return NULL; 428 return NULL;
427 429
428 if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) 430 lenmax = limit - ret - 5;
431 if (s->tlsext_ecpointformatlist_length > lenmax)
429 return NULL; 432 return NULL;
430 if (s->tlsext_ecpointformatlist_length > 255) { 433 if (s->tlsext_ecpointformatlist_length > 255) {
431 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); 434 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
@@ -441,13 +444,15 @@ ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
441 if (s->tlsext_ellipticcurvelist != NULL && 444 if (s->tlsext_ellipticcurvelist != NULL &&
442 s->version != DTLS1_VERSION) { 445 s->version != DTLS1_VERSION) {
443 /* Add TLS extension EllipticCurves to the ClientHello message */ 446 /* Add TLS extension EllipticCurves to the ClientHello message */
444 long lenmax; 447 size_t lenmax;
445 448
446 if ((lenmax = limit - ret - 6) 449 if ((size_t)(limit - ret) < 6)
447 < 0) return NULL; 450 return NULL;
448 451
449 if (s->tlsext_ellipticcurvelist_length > (unsigned long)lenmax) return NULL; 452 lenmax = limit - ret - 6;
450 if (s->tlsext_ellipticcurvelist_length > 65532) { 453 if (s->tlsext_ellipticcurvelist_length > lenmax)
454 return NULL;
455 if (s->tlsext_ellipticcurvelist_length > 65532) {
451 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); 456 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
452 return NULL; 457 return NULL;
453 } 458 }
@@ -487,7 +492,7 @@ ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
487 /* Check for enough room 2 for extension type, 2 for len 492 /* Check for enough room 2 for extension type, 2 for len
488 * rest for ticket 493 * rest for ticket
489 */ 494 */
490 if ((long)(limit - ret - 4 - ticklen) < 0) 495 if ((size_t)(limit - ret) < 4 + ticklen)
491 return NULL; 496 return NULL;
492 s2n(TLSEXT_TYPE_session_ticket, ret); 497 s2n(TLSEXT_TYPE_session_ticket, ret);
493 498
@@ -512,10 +517,10 @@ skip_ext:
512 517
513#ifdef TLSEXT_TYPE_opaque_prf_input 518#ifdef TLSEXT_TYPE_opaque_prf_input
514 if (s->s3->client_opaque_prf_input != NULL && 519 if (s->s3->client_opaque_prf_input != NULL &&
515 s->version != DTLS1_VERSION) { 520 s->version != DTLS1_VERSION) {
516 size_t col = s->s3->client_opaque_prf_input_len; 521 size_t col = s->s3->client_opaque_prf_input_len;
517 522
518 if ((long)(limit - ret - 6 - col < 0)) 523 if ((size_t)(limit - ret) < 6 + col)
519 return NULL; 524 return NULL;
520 if (col > 0xFFFD) /* can't happen */ 525 if (col > 0xFFFD) /* can't happen */
521 return NULL; 526 return NULL;
@@ -551,7 +556,7 @@ skip_ext:
551 } else 556 } else
552 extlen = 0; 557 extlen = 0;
553 558
554 if ((long)(limit - ret - 7 - extlen - idlen) < 0) 559 if ((size_t)(limit - ret) < 7 + extlen + idlen)
555 return NULL; 560 return NULL;
556 s2n(TLSEXT_TYPE_status_request, ret); 561 s2n(TLSEXT_TYPE_status_request, ret);
557 if (extlen + idlen > 0xFFF0) 562 if (extlen + idlen > 0xFFF0)
@@ -578,7 +583,7 @@ skip_ext:
578 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) { 583 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {
579 /* The client advertises an emtpy extension to indicate its 584 /* The client advertises an emtpy extension to indicate its
580 * support for Next Protocol Negotiation */ 585 * support for Next Protocol Negotiation */
581 if (limit - ret - 4 < 0) 586 if ((size_t)(limit - ret) < 4)
582 return NULL; 587 return NULL;
583 s2n(TLSEXT_TYPE_next_proto_neg, ret); 588 s2n(TLSEXT_TYPE_next_proto_neg, ret);
584 s2n(0, ret); 589 s2n(0, ret);
@@ -591,7 +596,7 @@ skip_ext:
591 596
592 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0); 597 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
593 598
594 if ((limit - p - 4 - el) < 0) 599 if ((size_t)(limit - ret) < 4 + el)
595 return NULL; 600 return NULL;
596 601
597 s2n(TLSEXT_TYPE_use_srtp, ret); 602 s2n(TLSEXT_TYPE_use_srtp, ret);
@@ -659,7 +664,7 @@ ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
659 return NULL; /* this really never occurs, but ... */ 664 return NULL; /* this really never occurs, but ... */
660 665
661 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL) { 666 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL) {
662 if ((long)(limit - ret - 4) < 0) 667 if ((size_t)(limit - ret) < 4)
663 return NULL; 668 return NULL;
664 669
665 s2n(TLSEXT_TYPE_server_name, ret); 670 s2n(TLSEXT_TYPE_server_name, ret);
@@ -674,8 +679,8 @@ ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
674 return NULL; 679 return NULL;
675 } 680 }
676 681
677 if ((limit - p - 4 - el) 682 if ((size_t)(limit - ret) < 4 + el)
678 < 0) return NULL; 683 return NULL;
679 684
680 s2n(TLSEXT_TYPE_renegotiate, ret); 685 s2n(TLSEXT_TYPE_renegotiate, ret);
681 s2n(el, ret); 686 s2n(el, ret);
@@ -692,13 +697,13 @@ ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
692 if (s->tlsext_ecpointformatlist != NULL && 697 if (s->tlsext_ecpointformatlist != NULL &&
693 s->version != DTLS1_VERSION) { 698 s->version != DTLS1_VERSION) {
694 /* Add TLS extension ECPointFormats to the ServerHello message */ 699 /* Add TLS extension ECPointFormats to the ServerHello message */
695 long lenmax; 700 size_t lenmax;
696 701
702 if ((size_t)(limit - ret) < 5)
703 return NULL;
697 704
698 if ((lenmax = limit - ret - 5) 705 lenmax = limit - ret - 5;
699 < 0) return NULL; 706 if (s->tlsext_ecpointformatlist_length > lenmax)
700
701 if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax)
702 return NULL; 707 return NULL;
703 if (s->tlsext_ecpointformatlist_length > 255) { 708 if (s->tlsext_ecpointformatlist_length > 255) {
704 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); 709 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
@@ -716,7 +721,7 @@ ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
716#endif /* OPENSSL_NO_EC */ 721#endif /* OPENSSL_NO_EC */
717 722
718 if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) { 723 if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
719 if ((long)(limit - ret - 4) < 0) 724 if ((size_t)(limit - ret) < 4)
720 return NULL; 725 return NULL;
721 726
722 s2n(TLSEXT_TYPE_session_ticket, ret); 727 s2n(TLSEXT_TYPE_session_ticket, ret);
@@ -724,7 +729,7 @@ ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
724 } 729 }
725 730
726 if (s->tlsext_status_expected) { 731 if (s->tlsext_status_expected) {
727 if ((long)(limit - ret - 4) < 0) 732 if ((size_t)(limit - ret) < 4)
728 return NULL; 733 return NULL;
729 734
730 s2n(TLSEXT_TYPE_status_request, ret); 735 s2n(TLSEXT_TYPE_status_request, ret);
@@ -735,7 +740,7 @@ ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
735 if (s->s3->server_opaque_prf_input != NULL && s->version != DTLS1_VERSION) { 740 if (s->s3->server_opaque_prf_input != NULL && s->version != DTLS1_VERSION) {
736 size_t sol = s->s3->server_opaque_prf_input_len; 741 size_t sol = s->s3->server_opaque_prf_input_len;
737 742
738 if ((long)(limit - ret - 6 - sol) < 0) 743 if ((size_t)(limit - ret) < 6 + sol)
739 return NULL; 744 return NULL;
740 if (sol > 0xFFFD) /* can't happen */ 745 if (sol > 0xFFFD) /* can't happen */
741 return NULL; 746 return NULL;
@@ -755,7 +760,7 @@ ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
755 760
756 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0); 761 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
757 762
758 if ((limit - p - 4 - el) < 0) 763 if ((size_t)(limit - ret) < 4 + el)
759 return NULL; 764 return NULL;
760 765
761 s2n(TLSEXT_TYPE_use_srtp, ret); 766 s2n(TLSEXT_TYPE_use_srtp, ret);
@@ -780,7 +785,7 @@ ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
780 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08, 785 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
781 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17 786 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
782 }; 787 };
783 if (limit - ret < 36) 788 if ((size_t)(limit - ret) < 36)
784 return NULL; 789 return NULL;
785 memcpy(ret, cryptopro_ext, 36); 790 memcpy(ret, cryptopro_ext, 36);
786 ret += 36; 791 ret += 36;
@@ -796,7 +801,7 @@ ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
796 801
797 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg); 802 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
798 if (r == SSL_TLSEXT_ERR_OK) { 803 if (r == SSL_TLSEXT_ERR_OK) {
799 if ((long)(limit - ret - 4 - npalen) < 0) 804 if ((size_t)(limit - ret) < 4 + npalen)
800 return NULL; 805 return NULL;
801 s2n(TLSEXT_TYPE_next_proto_neg, ret); 806 s2n(TLSEXT_TYPE_next_proto_neg, ret);
802 s2n(npalen, ret); 807 s2n(npalen, ret);