diff options
author | tb <> | 2022-09-04 07:55:32 +0000 |
---|---|---|
committer | tb <> | 2022-09-04 07:55:32 +0000 |
commit | 1012010906f7db47f5ef9a4b668073632813968a (patch) | |
tree | 60492298f7ea4952bf2e95d5d85a2673eaf9c457 /src/lib | |
parent | 837f40f79fcf594e0b81c0a317eb99a10b93a3bb (diff) | |
download | openbsd-1012010906f7db47f5ef9a4b668073632813968a.tar.gz openbsd-1012010906f7db47f5ef9a4b668073632813968a.tar.bz2 openbsd-1012010906f7db47f5ef9a4b668073632813968a.zip |
Make ssl_create_cipher_list() have a single exit
This simplifies memory management and makes it easier to see the leak
that were introduced in the previous commit. Sprinkle a few malloc
errors for consistency.
CID 278396
with/ok jsing
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/libssl/ssl_ciph.c | 46 |
1 files changed, 23 insertions, 23 deletions
diff --git a/src/lib/libssl/ssl_ciph.c b/src/lib/libssl/ssl_ciph.c index c42c3fd22d..d304cfe6ec 100644 --- a/src/lib/libssl/ssl_ciph.c +++ b/src/lib/libssl/ssl_ciph.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: ssl_ciph.c,v 1.131 2022/09/01 15:19:16 tb Exp $ */ | 1 | /* $OpenBSD: ssl_ciph.c,v 1.132 2022/09/04 07:55:32 tb 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 | * |
@@ -1209,7 +1209,7 @@ ssl_create_cipher_list(const SSL_METHOD *ssl_method, | |||
1209 | { | 1209 | { |
1210 | int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases; | 1210 | int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases; |
1211 | unsigned long disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl; | 1211 | unsigned long disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl; |
1212 | STACK_OF(SSL_CIPHER) *cipherstack; | 1212 | STACK_OF(SSL_CIPHER) *cipherstack = NULL, *ret = NULL; |
1213 | const char *rule_p; | 1213 | const char *rule_p; |
1214 | CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr; | 1214 | CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr; |
1215 | const SSL_CIPHER **ca_list = NULL; | 1215 | const SSL_CIPHER **ca_list = NULL; |
@@ -1222,7 +1222,7 @@ ssl_create_cipher_list(const SSL_METHOD *ssl_method, | |||
1222 | * Return with error if nothing to do. | 1222 | * Return with error if nothing to do. |
1223 | */ | 1223 | */ |
1224 | if (rule_str == NULL || cipher_list == NULL) | 1224 | if (rule_str == NULL || cipher_list == NULL) |
1225 | return NULL; | 1225 | goto err; |
1226 | 1226 | ||
1227 | /* | 1227 | /* |
1228 | * To reduce the work to do we only want to process the compiled | 1228 | * To reduce the work to do we only want to process the compiled |
@@ -1239,7 +1239,7 @@ ssl_create_cipher_list(const SSL_METHOD *ssl_method, | |||
1239 | co_list = reallocarray(NULL, num_of_ciphers, sizeof(CIPHER_ORDER)); | 1239 | co_list = reallocarray(NULL, num_of_ciphers, sizeof(CIPHER_ORDER)); |
1240 | if (co_list == NULL) { | 1240 | if (co_list == NULL) { |
1241 | SSLerrorx(ERR_R_MALLOC_FAILURE); | 1241 | SSLerrorx(ERR_R_MALLOC_FAILURE); |
1242 | return(NULL); /* Failure */ | 1242 | goto err; |
1243 | } | 1243 | } |
1244 | 1244 | ||
1245 | ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers, | 1245 | ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers, |
@@ -1292,10 +1292,8 @@ ssl_create_cipher_list(const SSL_METHOD *ssl_method, | |||
1292 | 1292 | ||
1293 | /* Now sort by symmetric encryption strength. The above ordering remains | 1293 | /* Now sort by symmetric encryption strength. The above ordering remains |
1294 | * in force within each class */ | 1294 | * in force within each class */ |
1295 | if (!ssl_cipher_strength_sort(&head, &tail)) { | 1295 | if (!ssl_cipher_strength_sort(&head, &tail)) |
1296 | free(co_list); | 1296 | goto err; |
1297 | return NULL; | ||
1298 | } | ||
1299 | 1297 | ||
1300 | /* Now disable everything (maintaining the ordering!) */ | 1298 | /* Now disable everything (maintaining the ordering!) */ |
1301 | ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail); | 1299 | ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail); |
@@ -1316,9 +1314,8 @@ ssl_create_cipher_list(const SSL_METHOD *ssl_method, | |||
1316 | num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1; | 1314 | num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1; |
1317 | ca_list = reallocarray(NULL, num_of_alias_max, sizeof(SSL_CIPHER *)); | 1315 | ca_list = reallocarray(NULL, num_of_alias_max, sizeof(SSL_CIPHER *)); |
1318 | if (ca_list == NULL) { | 1316 | if (ca_list == NULL) { |
1319 | free(co_list); | ||
1320 | SSLerrorx(ERR_R_MALLOC_FAILURE); | 1317 | SSLerrorx(ERR_R_MALLOC_FAILURE); |
1321 | return(NULL); /* Failure */ | 1318 | goto err; |
1322 | } | 1319 | } |
1323 | ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, disabled_mkey, | 1320 | ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, disabled_mkey, |
1324 | disabled_auth, disabled_enc, disabled_mac, disabled_ssl, head); | 1321 | disabled_auth, disabled_enc, disabled_mac, disabled_ssl, head); |
@@ -1341,12 +1338,9 @@ ssl_create_cipher_list(const SSL_METHOD *ssl_method, | |||
1341 | ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list, | 1338 | ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list, |
1342 | cert, &tls13_seen); | 1339 | cert, &tls13_seen); |
1343 | 1340 | ||
1344 | free((void *)ca_list); /* Not needed anymore */ | ||
1345 | |||
1346 | if (!ok) { | 1341 | if (!ok) { |
1347 | /* Rule processing failure */ | 1342 | /* Rule processing failure */ |
1348 | free(co_list); | 1343 | goto err; |
1349 | return (NULL); | ||
1350 | } | 1344 | } |
1351 | 1345 | ||
1352 | /* | 1346 | /* |
@@ -1354,8 +1348,8 @@ ssl_create_cipher_list(const SSL_METHOD *ssl_method, | |||
1354 | * if we cannot get one. | 1348 | * if we cannot get one. |
1355 | */ | 1349 | */ |
1356 | if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL) { | 1350 | if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL) { |
1357 | free(co_list); | 1351 | SSLerrorx(ERR_R_MALLOC_FAILURE); |
1358 | return (NULL); | 1352 | goto err; |
1359 | } | 1353 | } |
1360 | 1354 | ||
1361 | /* Prefer TLSv1.3 cipher suites. */ | 1355 | /* Prefer TLSv1.3 cipher suites. */ |
@@ -1363,8 +1357,8 @@ ssl_create_cipher_list(const SSL_METHOD *ssl_method, | |||
1363 | for (i = 0; i < sk_SSL_CIPHER_num(cipher_list_tls13); i++) { | 1357 | for (i = 0; i < sk_SSL_CIPHER_num(cipher_list_tls13); i++) { |
1364 | cipher = sk_SSL_CIPHER_value(cipher_list_tls13, i); | 1358 | cipher = sk_SSL_CIPHER_value(cipher_list_tls13, i); |
1365 | if (!sk_SSL_CIPHER_push(cipherstack, cipher)) { | 1359 | if (!sk_SSL_CIPHER_push(cipherstack, cipher)) { |
1366 | free(co_list); | 1360 | SSLerrorx(ERR_R_MALLOC_FAILURE); |
1367 | return (NULL); | 1361 | goto err; |
1368 | } | 1362 | } |
1369 | } | 1363 | } |
1370 | tls13_seen = 1; | 1364 | tls13_seen = 1; |
@@ -1386,8 +1380,8 @@ ssl_create_cipher_list(const SSL_METHOD *ssl_method, | |||
1386 | if (curr->active || | 1380 | if (curr->active || |
1387 | (!tls13_seen && curr->cipher->algorithm_ssl == SSL_TLSV1_3)) { | 1381 | (!tls13_seen && curr->cipher->algorithm_ssl == SSL_TLSV1_3)) { |
1388 | if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) { | 1382 | if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) { |
1389 | free(co_list); | 1383 | SSLerrorx(ERR_R_MALLOC_FAILURE); |
1390 | return (NULL); | 1384 | goto err; |
1391 | } | 1385 | } |
1392 | } | 1386 | } |
1393 | any_active |= curr->active; | 1387 | any_active |= curr->active; |
@@ -1395,12 +1389,18 @@ ssl_create_cipher_list(const SSL_METHOD *ssl_method, | |||
1395 | if (!any_active) | 1389 | if (!any_active) |
1396 | sk_SSL_CIPHER_zero(cipherstack); | 1390 | sk_SSL_CIPHER_zero(cipherstack); |
1397 | 1391 | ||
1398 | free(co_list); /* Not needed any longer */ | ||
1399 | |||
1400 | sk_SSL_CIPHER_free(*cipher_list); | 1392 | sk_SSL_CIPHER_free(*cipher_list); |
1401 | *cipher_list = cipherstack; | 1393 | *cipher_list = cipherstack; |
1394 | cipherstack = NULL; | ||
1395 | |||
1396 | ret = *cipher_list; | ||
1397 | |||
1398 | err: | ||
1399 | sk_SSL_CIPHER_free(cipherstack); | ||
1400 | free((void *)ca_list); | ||
1401 | free(co_list); | ||
1402 | 1402 | ||
1403 | return (cipherstack); | 1403 | return ret; |
1404 | } | 1404 | } |
1405 | 1405 | ||
1406 | const SSL_CIPHER * | 1406 | const SSL_CIPHER * |