summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjsing <>2019-04-25 04:54:35 +0000
committerjsing <>2019-04-25 04:54:35 +0000
commit36f65c71f38fa570364b27285824fff86fcf0460 (patch)
tree52a011e96dbe86ef0b262e7acd453be600ab47a4
parentbd76db5d5244e8cc6d3c5422caae5ebdbc7f971b (diff)
downloadopenbsd-36f65c71f38fa570364b27285824fff86fcf0460.tar.gz
openbsd-36f65c71f38fa570364b27285824fff86fcf0460.tar.bz2
openbsd-36f65c71f38fa570364b27285824fff86fcf0460.zip
Use EVP_CIPHER_CTX_{new,free}() and HMAC_CTX_{new,free}() instead of
allocating on stack. While here also check the return values from EVP_DecryptInit_ex() and HMAC_Init_ex(). ok tb@
-rw-r--r--src/lib/libssl/t1_lib.c53
1 files changed, 29 insertions, 24 deletions
diff --git a/src/lib/libssl/t1_lib.c b/src/lib/libssl/t1_lib.c
index 2bae50f5b3..3cde388d6d 100644
--- a/src/lib/libssl/t1_lib.c
+++ b/src/lib/libssl/t1_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: t1_lib.c,v 1.162 2019/04/25 04:48:56 jsing Exp $ */ 1/* $OpenBSD: t1_lib.c,v 1.163 2019/04/25 04:54:35 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -875,19 +875,17 @@ tls_decrypt_ticket(SSL *s, CBS *session_id, CBS *ticket, SSL_SESSION **psess)
875{ 875{
876 CBS ticket_name, ticket_iv, ticket_encdata, ticket_hmac; 876 CBS ticket_name, ticket_iv, ticket_encdata, ticket_hmac;
877 SSL_SESSION *sess = NULL; 877 SSL_SESSION *sess = NULL;
878 size_t session_id_len = 0; 878 size_t session_id_len;
879 unsigned char *sdec = NULL; 879 unsigned char *sdec = NULL;
880 const unsigned char *p; 880 const unsigned char *p;
881 int slen, hlen, renew_ticket = 0;
882 unsigned char hmac[EVP_MAX_MD_SIZE]; 881 unsigned char hmac[EVP_MAX_MD_SIZE];
883 HMAC_CTX hctx; 882 HMAC_CTX *hctx = NULL;
884 EVP_CIPHER_CTX cctx; 883 EVP_CIPHER_CTX *cctx = NULL;
885 SSL_CTX *tctx = s->initial_ctx; 884 SSL_CTX *tctx = s->initial_ctx;
885 int slen, hlen;
886 int renew_ticket = 0;
886 int ret = -1; 887 int ret = -1;
887 888
888 HMAC_CTX_init(&hctx);
889 EVP_CIPHER_CTX_init(&cctx);
890
891 *psess = NULL; 889 *psess = NULL;
892 890
893 if (!CBS_get_bytes(ticket, &ticket_name, 16)) 891 if (!CBS_get_bytes(ticket, &ticket_name, 16))
@@ -896,6 +894,11 @@ tls_decrypt_ticket(SSL *s, CBS *session_id, CBS *ticket, SSL_SESSION **psess)
896 /* 894 /*
897 * Initialize session ticket encryption and HMAC contexts. 895 * Initialize session ticket encryption and HMAC contexts.
898 */ 896 */
897 if ((cctx = EVP_CIPHER_CTX_new()) == NULL)
898 goto err;
899 if ((hctx = HMAC_CTX_new()) == NULL)
900 goto err;
901
899 if (tctx->internal->tlsext_ticket_key_cb != NULL) { 902 if (tctx->internal->tlsext_ticket_key_cb != NULL) {
900 int rv; 903 int rv;
901 904
@@ -910,7 +913,7 @@ tls_decrypt_ticket(SSL *s, CBS *session_id, CBS *ticket, SSL_SESSION **psess)
910 913
911 if ((rv = tctx->internal->tlsext_ticket_key_cb(s, 914 if ((rv = tctx->internal->tlsext_ticket_key_cb(s,
912 (unsigned char *)CBS_data(&ticket_name), 915 (unsigned char *)CBS_data(&ticket_name),
913 (unsigned char *)CBS_data(ticket), &cctx, &hctx, 0)) < 0) 916 (unsigned char *)CBS_data(ticket), cctx, hctx, 0)) < 0)
914 goto err; 917 goto err;
915 if (rv == 0) 918 if (rv == 0)
916 goto derr; 919 goto derr;
@@ -922,7 +925,7 @@ tls_decrypt_ticket(SSL *s, CBS *session_id, CBS *ticket, SSL_SESSION **psess)
922 * the IV since its length is known. 925 * the IV since its length is known.
923 */ 926 */
924 if (!CBS_get_bytes(ticket, &ticket_iv, 927 if (!CBS_get_bytes(ticket, &ticket_iv,
925 EVP_CIPHER_CTX_iv_length(&cctx))) 928 EVP_CIPHER_CTX_iv_length(cctx)))
926 goto derr; 929 goto derr;
927 } else { 930 } else {
928 /* Check that the key name matches. */ 931 /* Check that the key name matches. */
@@ -930,21 +933,23 @@ tls_decrypt_ticket(SSL *s, CBS *session_id, CBS *ticket, SSL_SESSION **psess)
930 tctx->internal->tlsext_tick_key_name, 933 tctx->internal->tlsext_tick_key_name,
931 sizeof(tctx->internal->tlsext_tick_key_name))) 934 sizeof(tctx->internal->tlsext_tick_key_name)))
932 goto derr; 935 goto derr;
933 HMAC_Init_ex(&hctx, tctx->internal->tlsext_tick_hmac_key,
934 sizeof(tctx->internal->tlsext_tick_hmac_key), EVP_sha256(),
935 NULL);
936 if (!CBS_get_bytes(ticket, &ticket_iv, 936 if (!CBS_get_bytes(ticket, &ticket_iv,
937 EVP_CIPHER_iv_length(EVP_aes_128_cbc()))) 937 EVP_CIPHER_iv_length(EVP_aes_128_cbc())))
938 goto derr; 938 goto derr;
939 EVP_DecryptInit_ex(&cctx, EVP_aes_128_cbc(), NULL, 939 if (!EVP_DecryptInit_ex(cctx, EVP_aes_128_cbc(), NULL,
940 tctx->internal->tlsext_tick_aes_key, CBS_data(&ticket_iv)); 940 tctx->internal->tlsext_tick_aes_key, CBS_data(&ticket_iv)))
941 goto err;
942 if (!HMAC_Init_ex(hctx, tctx->internal->tlsext_tick_hmac_key,
943 sizeof(tctx->internal->tlsext_tick_hmac_key), EVP_sha256(),
944 NULL))
945 goto err;
941 } 946 }
942 947
943 /* 948 /*
944 * Attempt to process session ticket. 949 * Attempt to process session ticket.
945 */ 950 */
946 951
947 if ((hlen = HMAC_size(&hctx)) < 0) 952 if ((hlen = HMAC_size(hctx)) < 0)
948 goto err; 953 goto err;
949 954
950 if (hlen > CBS_len(ticket)) 955 if (hlen > CBS_len(ticket))
@@ -957,16 +962,16 @@ tls_decrypt_ticket(SSL *s, CBS *session_id, CBS *ticket, SSL_SESSION **psess)
957 goto err; 962 goto err;
958 963
959 /* Check HMAC of encrypted ticket. */ 964 /* Check HMAC of encrypted ticket. */
960 if (HMAC_Update(&hctx, CBS_data(&ticket_name), 965 if (HMAC_Update(hctx, CBS_data(&ticket_name),
961 CBS_len(&ticket_name)) <= 0) 966 CBS_len(&ticket_name)) <= 0)
962 goto err; 967 goto err;
963 if (HMAC_Update(&hctx, CBS_data(&ticket_iv), 968 if (HMAC_Update(hctx, CBS_data(&ticket_iv),
964 CBS_len(&ticket_iv)) <= 0) 969 CBS_len(&ticket_iv)) <= 0)
965 goto err; 970 goto err;
966 if (HMAC_Update(&hctx, CBS_data(&ticket_encdata), 971 if (HMAC_Update(hctx, CBS_data(&ticket_encdata),
967 CBS_len(&ticket_encdata)) <= 0) 972 CBS_len(&ticket_encdata)) <= 0)
968 goto err; 973 goto err;
969 if (HMAC_Final(&hctx, hmac, &hlen) <= 0) 974 if (HMAC_Final(hctx, hmac, &hlen) <= 0)
970 goto err; 975 goto err;
971 976
972 if (!CBS_mem_equal(&ticket_hmac, hmac, hlen)) 977 if (!CBS_mem_equal(&ticket_hmac, hmac, hlen))
@@ -975,10 +980,10 @@ tls_decrypt_ticket(SSL *s, CBS *session_id, CBS *ticket, SSL_SESSION **psess)
975 /* Attempt to decrypt session data. */ 980 /* Attempt to decrypt session data. */
976 if ((sdec = malloc(CBS_len(&ticket_encdata))) == NULL) 981 if ((sdec = malloc(CBS_len(&ticket_encdata))) == NULL)
977 goto err; 982 goto err;
978 if (EVP_DecryptUpdate(&cctx, sdec, &slen, CBS_data(&ticket_encdata), 983 if (EVP_DecryptUpdate(cctx, sdec, &slen, CBS_data(&ticket_encdata),
979 CBS_len(&ticket_encdata)) <= 0) 984 CBS_len(&ticket_encdata)) <= 0)
980 goto derr; 985 goto derr;
981 if (EVP_DecryptFinal_ex(&cctx, sdec + slen, &hlen) <= 0) 986 if (EVP_DecryptFinal_ex(cctx, sdec + slen, &hlen) <= 0)
982 goto derr; 987 goto derr;
983 988
984 slen += hlen; 989 slen += hlen;
@@ -1021,8 +1026,8 @@ tls_decrypt_ticket(SSL *s, CBS *session_id, CBS *ticket, SSL_SESSION **psess)
1021 1026
1022 done: 1027 done:
1023 free(sdec); 1028 free(sdec);
1024 HMAC_CTX_cleanup(&hctx); 1029 EVP_CIPHER_CTX_free(cctx);
1025 EVP_CIPHER_CTX_cleanup(&cctx); 1030 HMAC_CTX_free(hctx);
1026 SSL_SESSION_free(sess); 1031 SSL_SESSION_free(sess);
1027 1032
1028 if (ret == 2) 1033 if (ret == 2)