1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
|
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include "tls/s2n_signature_algorithms.h"
#include "api/s2n.h"
#include "crypto/s2n_fips.h"
#include "crypto/s2n_rsa_pss.h"
#include "s2n_test.h"
#include "testlib/s2n_testlib.h"
#include "tls/s2n_cipher_suites.h"
#include "tls/s2n_config.h"
#include "tls/s2n_connection.h"
#include "tls/s2n_security_policies.h"
#include "tls/s2n_signature_scheme.h"
#define LENGTH (s2n_array_len(test_signature_schemes))
#define STUFFER_SIZE (LENGTH * TLS_SIGNATURE_SCHEME_LEN + 10)
#define RSA_CIPHER_SUITE &s2n_ecdhe_rsa_with_aes_128_cbc_sha
#define ECDSA_CIPHER_SUITE &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha
#define TLS13_CIPHER_SUITE &s2n_tls13_aes_128_gcm_sha256
const struct s2n_signature_scheme *const test_signature_schemes[] = {
&s2n_ecdsa_secp384r1_sha384,
&s2n_rsa_pkcs1_sha256,
&s2n_rsa_pkcs1_sha224,
&s2n_rsa_pkcs1_sha1,
&s2n_ecdsa_sha1,
};
const struct s2n_signature_preferences test_preferences = {
.count = LENGTH,
.signature_schemes = test_signature_schemes,
};
struct s2n_local_sig_schemes_context {
struct s2n_security_policy policy;
struct s2n_signature_preferences signature_prefs;
};
static S2N_RESULT s2n_test_set_local_sig_schemes(struct s2n_connection *conn,
struct s2n_local_sig_schemes_context *context,
const struct s2n_signature_scheme **sig_schemes, size_t count)
{
RESULT_ENSURE_REF(context);
context->signature_prefs.signature_schemes = sig_schemes;
context->signature_prefs.count = count;
const struct s2n_security_policy *curr_policy = NULL;
RESULT_GUARD_POSIX(s2n_connection_get_security_policy(conn, &curr_policy));
context->policy = *curr_policy;
context->policy.signature_preferences = &context->signature_prefs;
conn->security_policy_override = &context->policy;
return S2N_RESULT_OK;
}
static S2N_RESULT s2n_test_set_peer_sig_schemes(struct s2n_sig_scheme_list *peer_list,
const struct s2n_signature_scheme **sig_schemes, size_t count)
{
for (size_t i = 0; i < count; i++) {
peer_list->iana_list[i] = sig_schemes[i]->iana_value;
}
peer_list->len = count;
return S2N_RESULT_OK;
}
int main(int argc, char **argv)
{
BEGIN_TEST();
EXPECT_SUCCESS(s2n_disable_tls13_in_test());
DEFER_CLEANUP(struct s2n_cert_chain_and_key *rsa_cert_chain = NULL,
s2n_cert_chain_and_key_ptr_free);
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&rsa_cert_chain,
S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
DEFER_CLEANUP(struct s2n_cert_chain_and_key *ecdsa_cert_chain = NULL,
s2n_cert_chain_and_key_ptr_free);
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&ecdsa_cert_chain,
S2N_ECDSA_P384_PKCS1_CERT_CHAIN, S2N_ECDSA_P384_PKCS1_KEY));
struct s2n_cert_chain_and_key *certs[] = { ecdsa_cert_chain, rsa_cert_chain };
/* s2n_signature_algorithms_supported_list_send */
{
struct s2n_security_policy test_security_policy = *s2n_fetch_default_config()->security_policy;
test_security_policy.signature_preferences = &test_preferences;
/* Test: if all signatures supported, send all signatures */
{
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
EXPECT_NOT_NULL(conn);
conn->security_policy_override = &test_security_policy;
conn->actual_protocol_version = S2N_TLS13;
DEFER_CLEANUP(struct s2n_stuffer result = { 0 }, s2n_stuffer_free);
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&result, 0));
EXPECT_OK(s2n_signature_algorithms_supported_list_send(conn, &result));
uint16_t size = 0;
EXPECT_SUCCESS(s2n_stuffer_read_uint16(&result, &size));
EXPECT_EQUAL(size, s2n_stuffer_data_available(&result));
for (size_t i = 0; i < s2n_array_len(test_signature_schemes); i++) {
uint16_t iana_value = 0;
EXPECT_SUCCESS(s2n_stuffer_read_uint16(&result, &iana_value));
EXPECT_EQUAL(iana_value, test_signature_schemes[i]->iana_value);
}
EXPECT_EQUAL(s2n_stuffer_data_available(&result), 0);
};
/* Test: do not send unsupported signatures */
{
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
EXPECT_NOT_NULL(conn);
conn->security_policy_override = &test_security_policy;
conn->actual_protocol_version = S2N_TLS12;
DEFER_CLEANUP(struct s2n_stuffer result = { 0 }, s2n_stuffer_free);
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&result, 0));
EXPECT_OK(s2n_signature_algorithms_supported_list_send(conn, &result));
uint16_t size = 0;
EXPECT_SUCCESS(s2n_stuffer_read_uint16(&result, &size));
EXPECT_EQUAL(size, s2n_stuffer_data_available(&result));
for (size_t i = 0; i < s2n_array_len(test_signature_schemes); i++) {
if (test_signature_schemes[i] != &s2n_ecdsa_secp384r1_sha384) {
uint16_t iana_value = 0;
EXPECT_SUCCESS(s2n_stuffer_read_uint16(&result, &iana_value));
EXPECT_EQUAL(iana_value, test_signature_schemes[i]->iana_value);
}
}
EXPECT_EQUAL(s2n_stuffer_data_available(&result), 0);
};
/* Test: written signatures readable */
{
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
EXPECT_NOT_NULL(conn);
conn->security_policy_override = &test_security_policy;
conn->actual_protocol_version = S2N_TLS13;
DEFER_CLEANUP(struct s2n_stuffer result = { 0 }, s2n_stuffer_free);
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&result, 0));
EXPECT_OK(s2n_signature_algorithms_supported_list_send(conn, &result));
struct s2n_sig_scheme_list signatures = { 0 };
EXPECT_SUCCESS(s2n_recv_supported_sig_scheme_list(&result, &signatures));
EXPECT_EQUAL(s2n_stuffer_data_available(&result), 0);
EXPECT_EQUAL(signatures.len, s2n_array_len(test_signature_schemes));
for (size_t i = 0; i < s2n_array_len(test_signature_schemes); i++) {
EXPECT_EQUAL(signatures.iana_list[i], test_signature_schemes[i]->iana_value);
}
};
/* Test: do not send TLS1.2 signature schemes if QUIC enabled */
{
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
EXPECT_NOT_NULL(conn);
conn->security_policy_override = &test_security_policy;
conn->actual_protocol_version = S2N_TLS13;
conn->quic_enabled = true;
DEFER_CLEANUP(struct s2n_stuffer result = { 0 }, s2n_stuffer_free);
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&result, 0));
EXPECT_OK(s2n_signature_algorithms_supported_list_send(conn, &result));
uint16_t size = 0;
EXPECT_SUCCESS(s2n_stuffer_read_uint16(&result, &size));
EXPECT_EQUAL(size, s2n_stuffer_data_available(&result));
uint16_t iana_value = 0;
EXPECT_SUCCESS(s2n_stuffer_read_uint16(&result, &iana_value));
EXPECT_EQUAL(iana_value, s2n_ecdsa_secp384r1_sha384.iana_value);
EXPECT_EQUAL(s2n_stuffer_data_available(&result), 0);
};
};
/* s2n_signature_algorithm_select */
{
DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, rsa_cert_chain));
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, ecdsa_cert_chain));
/* Clients can only configure one certificate */
DEFER_CLEANUP(struct s2n_config *client_ecdsa_config = s2n_config_new(), s2n_config_ptr_free);
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(client_ecdsa_config, ecdsa_cert_chain));
DEFER_CLEANUP(struct s2n_config *client_rsa_config = s2n_config_new(), s2n_config_ptr_free);
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(client_rsa_config, rsa_cert_chain));
/* TLS1.2 defaults defined by the RFC */
const struct s2n_signature_scheme *ecdsa_default = &s2n_ecdsa_sha1;
const struct s2n_signature_scheme *rsa_default = &s2n_rsa_pkcs1_sha1;
/* Test: choose legacy default for <TLS1.2 */
{
/* Both the client and server support other signature schemes-- we're
* just not going to choose them.
*/
const struct s2n_signature_scheme *test_schemes[] = {
&s2n_ecdsa_secp384r1_sha384,
&s2n_ecdsa_sha384,
&s2n_rsa_pss_rsae_sha256,
&s2n_rsa_pss_pss_sha256,
&s2n_rsa_pkcs1_sha256,
};
/* Test: Client chooses default based on configured cert type */
{
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
struct s2n_local_sig_schemes_context local_context = { 0 };
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context,
test_schemes, s2n_array_len(test_schemes)));
EXPECT_OK(s2n_test_set_peer_sig_schemes(&conn->handshake_params.server_sig_hash_algs,
test_schemes, s2n_array_len(test_schemes)));
/* Test: ECDSA */
{
const struct s2n_signature_scheme *expected = &s2n_ecdsa_sha1;
conn->handshake_params.client_cert_pkey_type = S2N_PKEY_TYPE_ECDSA;
EXPECT_SUCCESS(s2n_connection_set_config(conn, client_ecdsa_config));
/* TLS1.1 selects the default */
conn->actual_protocol_version = S2N_TLS11;
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.client_cert_sig_scheme, expected);
/* TLS1.2 doesn't select the default */
conn->actual_protocol_version = S2N_TLS12;
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_NOT_EQUAL(conn->handshake_params.client_cert_sig_scheme, expected);
};
/* Test: RSA */
{
const struct s2n_signature_scheme *expected = &s2n_rsa_pkcs1_md5_sha1;
conn->handshake_params.client_cert_pkey_type = S2N_PKEY_TYPE_RSA;
EXPECT_SUCCESS(s2n_connection_set_config(conn, client_rsa_config));
/* TLS1.1 selects the default */
conn->actual_protocol_version = S2N_TLS11;
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.client_cert_sig_scheme, expected);
/* TLS1.2 doesn't select the default */
conn->actual_protocol_version = S2N_TLS12;
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_NOT_EQUAL(conn->handshake_params.client_cert_sig_scheme, expected);
};
};
/* Test: Server chooses default based on cipher suite */
{
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
struct s2n_local_sig_schemes_context local_context = { 0 };
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context,
test_schemes, s2n_array_len(test_schemes)));
EXPECT_OK(s2n_test_set_peer_sig_schemes(&conn->handshake_params.client_sig_hash_algs,
test_schemes, s2n_array_len(test_schemes)));
/* Test: ECDSA */
{
const struct s2n_signature_scheme *expected = &s2n_ecdsa_sha1;
conn->secure->cipher_suite = ECDSA_CIPHER_SUITE;
/* TLS1.1 selects the default */
conn->actual_protocol_version = S2N_TLS11;
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, expected);
/* TLS1.2 doesn't select the default */
conn->actual_protocol_version = S2N_TLS12;
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_NOT_EQUAL(conn->handshake_params.server_cert_sig_scheme, expected);
};
/* Test: RSA */
{
const struct s2n_signature_scheme *expected = &s2n_rsa_pkcs1_md5_sha1;
conn->secure->cipher_suite = RSA_CIPHER_SUITE;
/* TLS1.1 selects the default */
conn->actual_protocol_version = S2N_TLS11;
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, expected);
/* TLS1.2 doesn't select the default */
conn->actual_protocol_version = S2N_TLS12;
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_NOT_EQUAL(conn->handshake_params.server_cert_sig_scheme, expected);
};
};
};
/* Test: successfully choose server signature scheme */
{
const struct s2n_signature_scheme *expected = &s2n_ecdsa_sha256;
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
EXPECT_SUCCESS(s2n_connection_set_config(conn, client_ecdsa_config));
struct s2n_local_sig_schemes_context local_context = { 0 };
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context, &expected, 1));
EXPECT_OK(s2n_test_set_peer_sig_schemes(&conn->handshake_params.server_sig_hash_algs,
&expected, 1));
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.client_cert_sig_scheme, expected);
};
/* Test: successfully choose client signature scheme */
{
const struct s2n_signature_scheme *expected = &s2n_ecdsa_sha256;
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
conn->actual_protocol_version = S2N_TLS12;
conn->secure->cipher_suite = ECDSA_CIPHER_SUITE;
EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
struct s2n_local_sig_schemes_context local_context = { 0 };
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context, &expected, 1));
EXPECT_OK(s2n_test_set_peer_sig_schemes(&conn->handshake_params.server_sig_hash_algs,
&expected, 1));
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, expected);
};
/* Test: choose local most preferred, not peer most preferred */
{
const struct s2n_signature_scheme *order[] = {
&s2n_ecdsa_sha256,
&s2n_ecdsa_sha384,
&s2n_ecdsa_sha1,
};
const struct s2n_signature_scheme *reversed_order[] = {
&s2n_ecdsa_sha1,
&s2n_ecdsa_sha384,
&s2n_ecdsa_sha256,
};
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
conn->actual_protocol_version = S2N_TLS12;
conn->secure->cipher_suite = ECDSA_CIPHER_SUITE;
EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
struct s2n_local_sig_schemes_context local_context = { 0 };
/* Local order preferred */
{
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context,
order, s2n_array_len(order)));
EXPECT_OK(s2n_test_set_peer_sig_schemes(&conn->handshake_params.client_sig_hash_algs,
reversed_order, s2n_array_len(reversed_order)));
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, order[0]);
}
/* Local order preferred, even when reversed */
{
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context,
reversed_order, s2n_array_len(reversed_order)));
EXPECT_OK(s2n_test_set_peer_sig_schemes(&conn->handshake_params.client_sig_hash_algs,
order, s2n_array_len(order)));
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, reversed_order[0]);
}
/* Local order matches peer order
* (to prove that we're not just choosing the peer's least preferred)
*/
{
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context,
order, s2n_array_len(order)));
EXPECT_OK(s2n_test_set_peer_sig_schemes(&conn->handshake_params.client_sig_hash_algs,
order, s2n_array_len(order)));
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, order[0]);
}
};
/* Test: do not choose invalid schemes */
{
/* Test: scheme not valid for higher protocol version */
{
/* Valid TLS1.3 ECDSA sig schemes include associated curves */
const struct s2n_signature_scheme *invalid = &s2n_ecdsa_sha256;
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
struct s2n_local_sig_schemes_context local_context = { 0 };
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context,
&invalid, 1));
EXPECT_OK(s2n_test_set_peer_sig_schemes(&conn->handshake_params.client_sig_hash_algs,
&invalid, 1));
/* Fails for TLS1.3 */
conn->actual_protocol_version = S2N_TLS13;
conn->secure->cipher_suite = TLS13_CIPHER_SUITE;
EXPECT_ERROR_WITH_ERRNO(
s2n_signature_algorithm_select(conn),
S2N_ERR_NO_VALID_SIGNATURE_SCHEME);
/* Succeeds for TLS1.2 */
conn->actual_protocol_version = S2N_TLS12;
conn->secure->cipher_suite = ECDSA_CIPHER_SUITE;
EXPECT_OK(s2n_signature_algorithm_select(conn));
};
/* Test: scheme not valid for lower protocol version */
{
/* Valid TLS1.2 ECDSA sig schemes do not include associated curves */
const struct s2n_signature_scheme *invalid = &s2n_ecdsa_secp384r1_sha384;
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
struct s2n_local_sig_schemes_context local_context = { 0 };
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context,
&invalid, 1));
EXPECT_OK(s2n_test_set_peer_sig_schemes(&conn->handshake_params.client_sig_hash_algs,
&invalid, 1));
/* Fails for TLS1.2 */
conn->actual_protocol_version = S2N_TLS12;
conn->secure->cipher_suite = ECDSA_CIPHER_SUITE;
EXPECT_ERROR_WITH_ERRNO(
s2n_signature_algorithm_select(conn),
S2N_ERR_NO_VALID_SIGNATURE_SCHEME);
/* Succeeds for TLS1.3 */
conn->actual_protocol_version = S2N_TLS13;
conn->secure->cipher_suite = TLS13_CIPHER_SUITE;
EXPECT_OK(s2n_signature_algorithm_select(conn));
};
/* Test: SHA1 not allowed in TLS1.3 */
{
/* No SHA1 signature schemes for TLS1.3 actually exist.
* Create one for testing.
*/
struct s2n_signature_scheme sha1_tls13_scheme = s2n_ecdsa_secp384r1_sha384;
sha1_tls13_scheme.hash_alg = s2n_ecdsa_sha1.hash_alg;
const struct s2n_signature_scheme *invalid = &sha1_tls13_scheme;
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
conn->actual_protocol_version = S2N_TLS13;
conn->secure->cipher_suite = TLS13_CIPHER_SUITE;
EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
struct s2n_local_sig_schemes_context local_context = { 0 };
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context,
&invalid, 1));
EXPECT_OK(s2n_test_set_peer_sig_schemes(&conn->handshake_params.client_sig_hash_algs,
&invalid, 1));
/* Fails with SHA1 */
EXPECT_ERROR_WITH_ERRNO(
s2n_signature_algorithm_select(conn),
S2N_ERR_NO_VALID_SIGNATURE_SCHEME);
/* Succeeds without SHA1 */
sha1_tls13_scheme.hash_alg = s2n_ecdsa_secp384r1_sha384.hash_alg;
EXPECT_OK(s2n_signature_algorithm_select(conn));
};
/* Test: rsa-pkcs1 not allowed in TLS1.3 */
{
/* No pkcs1 signature schemes for TLS1.3 actually exist.
* Create one for testing.
*/
struct s2n_signature_scheme pkcs1_tls13_scheme = s2n_rsa_pss_rsae_sha256;
pkcs1_tls13_scheme.sig_alg = s2n_rsa_pkcs1_sha256.sig_alg;
const struct s2n_signature_scheme *invalid = &pkcs1_tls13_scheme;
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
conn->actual_protocol_version = S2N_TLS13;
conn->secure->cipher_suite = TLS13_CIPHER_SUITE;
EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
struct s2n_local_sig_schemes_context local_context = { 0 };
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context,
&invalid, 1));
EXPECT_OK(s2n_test_set_peer_sig_schemes(&conn->handshake_params.client_sig_hash_algs,
&invalid, 1));
/* Fails for pkcs1 */
EXPECT_ERROR_WITH_ERRNO(
s2n_signature_algorithm_select(conn),
S2N_ERR_NO_VALID_SIGNATURE_SCHEME);
/* Succeeds for pss */
if (s2n_is_rsa_pss_signing_supported()) {
pkcs1_tls13_scheme.sig_alg = s2n_rsa_pss_rsae_sha256.sig_alg;
EXPECT_OK(s2n_signature_algorithm_select(conn));
}
};
/* Test: no rsa certs */
{
const struct s2n_signature_scheme *scheme = &s2n_rsa_pkcs1_sha256;
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
conn->actual_protocol_version = S2N_TLS12;
conn->secure->cipher_suite = RSA_CIPHER_SUITE;
struct s2n_local_sig_schemes_context local_context = { 0 };
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context,
&scheme, 1));
EXPECT_OK(s2n_test_set_peer_sig_schemes(&conn->handshake_params.server_sig_hash_algs,
&scheme, 1));
/* Fails for default config with no certs */
EXPECT_ERROR_WITH_ERRNO(
s2n_signature_algorithm_select(conn),
S2N_ERR_NO_VALID_SIGNATURE_SCHEME);
/* Fails for config with ecdsa cert */
EXPECT_SUCCESS(s2n_connection_set_config(conn, client_ecdsa_config));
EXPECT_ERROR_WITH_ERRNO(
s2n_signature_algorithm_select(conn),
S2N_ERR_NO_VALID_SIGNATURE_SCHEME);
/* Succeeds for config with rsa cert */
EXPECT_SUCCESS(s2n_connection_set_config(conn, client_rsa_config));
EXPECT_OK(s2n_signature_algorithm_select(conn));
};
/* Test: no ecdsa certs */
{
const struct s2n_signature_scheme *scheme = &s2n_ecdsa_sha384;
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
conn->actual_protocol_version = S2N_TLS12;
conn->secure->cipher_suite = ECDSA_CIPHER_SUITE;
struct s2n_local_sig_schemes_context local_context = { 0 };
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context,
&scheme, 1));
EXPECT_OK(s2n_test_set_peer_sig_schemes(&conn->handshake_params.server_sig_hash_algs,
&scheme, 1));
/* Fails for default config with no certs */
EXPECT_ERROR_WITH_ERRNO(
s2n_signature_algorithm_select(conn),
S2N_ERR_NO_VALID_SIGNATURE_SCHEME);
/* Fails for config with rsa cert */
EXPECT_SUCCESS(s2n_connection_set_config(conn, client_rsa_config));
EXPECT_ERROR_WITH_ERRNO(
s2n_signature_algorithm_select(conn),
S2N_ERR_NO_VALID_SIGNATURE_SCHEME);
/* Succeeds for config with ecdsa cert */
EXPECT_SUCCESS(s2n_connection_set_config(conn, client_ecdsa_config));
EXPECT_OK(s2n_signature_algorithm_select(conn));
};
/* Test: ecdsa cert exists, but for wrong curve.
*
* This is enforced for TLS1.3, where curves are specified by the
* signature schemes, but not for TLS1.2 where the supported_groups
* extension is used instead. See https://github.com/aws/s2n-tls/issues/4274
*/
{
const struct s2n_signature_scheme *ecdsa384 = &s2n_ecdsa_secp384r1_sha384;
const struct s2n_signature_scheme *ecdsa256 = &s2n_ecdsa_secp256r1_sha256;
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
conn->actual_protocol_version = S2N_TLS13;
conn->secure->cipher_suite = TLS13_CIPHER_SUITE;
EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
struct s2n_local_sig_schemes_context local_context = { 0 };
/* Fails with wrong curve (256) */
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context,
&ecdsa256, 1));
EXPECT_OK(s2n_test_set_peer_sig_schemes(&conn->handshake_params.client_sig_hash_algs,
&ecdsa256, 1));
EXPECT_ERROR_WITH_ERRNO(
s2n_signature_algorithm_select(conn),
S2N_ERR_NO_VALID_SIGNATURE_SCHEME);
/* Succeeds with right curve (384) */
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context,
&ecdsa384, 1));
EXPECT_OK(s2n_test_set_peer_sig_schemes(&conn->handshake_params.client_sig_hash_algs,
&ecdsa384, 1));
EXPECT_OK(s2n_signature_algorithm_select(conn));
};
};
/* Test: skip invalid schemes and choose a less preferred scheme */
{
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
conn->actual_protocol_version = S2N_TLS13;
conn->secure->cipher_suite = TLS13_CIPHER_SUITE;
EXPECT_SUCCESS(s2n_connection_set_config(conn, client_ecdsa_config));
const struct s2n_signature_scheme *expected = &s2n_ecdsa_secp384r1_sha384;
const struct s2n_signature_scheme *schemes[] = {
/* No RSA certificates */
&s2n_rsa_pss_rsae_sha256,
&s2n_rsa_pss_pss_sha256,
&s2n_rsa_pkcs1_sha256,
/* Only valid for TLS1.2 */
&s2n_ecdsa_sha384,
/* Wrong curve */
&s2n_ecdsa_secp256r1_sha256,
expected
};
struct s2n_local_sig_schemes_context local_context = { 0 };
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context,
schemes, s2n_array_len(schemes)));
EXPECT_OK(s2n_test_set_peer_sig_schemes(&conn->handshake_params.server_sig_hash_algs,
schemes, s2n_array_len(schemes)));
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.client_cert_sig_scheme, expected);
};
/* Test: skip schemes not offered by the peer and choose a less preferred scheme */
{
const struct s2n_signature_scheme *expected = &s2n_rsa_pkcs1_sha256;
const struct s2n_signature_scheme *peer_schemes[] = {
expected
};
const struct s2n_signature_scheme *local_schemes[] = {
&s2n_rsa_pss_rsae_sha256,
&s2n_rsa_pss_pss_sha256,
&s2n_ecdsa_sha384,
expected,
};
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
conn->actual_protocol_version = S2N_TLS12;
conn->secure->cipher_suite = RSA_CIPHER_SUITE;
EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
struct s2n_local_sig_schemes_context local_context = { 0 };
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context,
local_schemes, s2n_array_len(local_schemes)));
EXPECT_OK(s2n_test_set_peer_sig_schemes(&conn->handshake_params.client_sig_hash_algs,
peer_schemes, s2n_array_len(peer_schemes)));
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, expected);
};
/* Test: no schemes offered by the peer */
{
const struct s2n_signature_scheme *ecdsa_not_default_tls12 = &s2n_ecdsa_sha384;
const struct s2n_signature_scheme *ecdsa_not_default_tls13 = &s2n_ecdsa_secp384r1_sha384;
const struct s2n_signature_scheme *rsa_not_default = &s2n_rsa_pkcs1_sha256;
/* Test: defaults allowed by security policy */
{
/* We should need to skip valid non-default schemes to choose the defaults */
const struct s2n_signature_scheme *schemes_with_defaults[] = {
ecdsa_not_default_tls12,
ecdsa_not_default_tls13,
rsa_not_default,
rsa_default,
ecdsa_default
};
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
struct s2n_local_sig_schemes_context local_context = { 0 };
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context,
schemes_with_defaults, s2n_array_len(schemes_with_defaults)));
/* TLS1.2 with ECDSA chooses ECDSA default */
conn->actual_protocol_version = S2N_TLS12;
conn->secure->cipher_suite = ECDSA_CIPHER_SUITE;
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, ecdsa_default);
/* TLS1.2 with RSA chooses RSA default */
conn->actual_protocol_version = S2N_TLS12;
conn->secure->cipher_suite = RSA_CIPHER_SUITE;
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, rsa_default);
/* TLS1.3 never chooses the defaults */
conn->actual_protocol_version = S2N_TLS13;
conn->secure->cipher_suite = TLS13_CIPHER_SUITE;
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, ecdsa_not_default_tls13);
}
/* Test: defaults not allowed by security policy */
{
const struct s2n_signature_scheme *schemes_without_defaults[] = {
ecdsa_not_default_tls12,
ecdsa_not_default_tls13,
rsa_not_default,
/* Add some more, less preferred non-defaults.
* We only choose the most preferred though. */
&s2n_ecdsa_secp384r1_sha384,
&s2n_ecdsa_sha256,
&s2n_rsa_pss_rsae_sha384,
};
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
struct s2n_local_sig_schemes_context local_context = { 0 };
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context,
schemes_without_defaults, s2n_array_len(schemes_without_defaults)));
/* TLS1.2 with ECDSA does not choose default */
conn->actual_protocol_version = S2N_TLS12;
conn->secure->cipher_suite = ECDSA_CIPHER_SUITE;
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, ecdsa_not_default_tls12);
/* TLS1.2 with RSA does not choose default */
conn->actual_protocol_version = S2N_TLS12;
conn->secure->cipher_suite = RSA_CIPHER_SUITE;
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, rsa_not_default);
/* TLS1.3 never chooses the defaults */
conn->actual_protocol_version = S2N_TLS13;
conn->secure->cipher_suite = TLS13_CIPHER_SUITE;
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, ecdsa_not_default_tls13);
};
/* Test: skip invalid fallback candidates to choose valid one */
{
const struct s2n_signature_scheme *expected = &s2n_ecdsa_secp384r1_sha384;
const struct s2n_signature_scheme *schemes[] = {
/* No RSA certificates */
&s2n_rsa_pss_rsae_sha256,
&s2n_rsa_pss_pss_sha256,
&s2n_rsa_pkcs1_sha256,
/* Only valid for TLS1.2 */
&s2n_ecdsa_sha384,
/* Wrong curve */
&s2n_ecdsa_secp256r1_sha256,
expected
};
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
conn->actual_protocol_version = S2N_TLS13;
conn->secure->cipher_suite = TLS13_CIPHER_SUITE;
EXPECT_SUCCESS(s2n_connection_set_config(conn, client_ecdsa_config));
struct s2n_local_sig_schemes_context local_context = { 0 };
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context,
schemes, s2n_array_len(schemes)));
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.client_cert_sig_scheme, expected);
};
};
/* Test: No valid schemes offered by the peer.
*
* s2n-tls deviates from the RFC here by applying the same logic as if
* the peer offered no signature schemes at all.
*/
{
const struct s2n_signature_scheme *ecdsa_not_default_tls12 = &s2n_ecdsa_sha384;
const struct s2n_signature_scheme *ecdsa_not_default_tls13 = &s2n_ecdsa_secp384r1_sha384;
const struct s2n_signature_scheme *rsa_not_default = &s2n_rsa_pss_rsae_sha256;
/* Test: TLS1.2 chooses defaults */
{
/* TLS1.2 does not support rsa-pss certs */
const struct s2n_signature_scheme *invalid_scheme = &s2n_rsa_pss_pss_sha256;
/* We should need to skip valid non-default schemes to choose the defaults */
const struct s2n_signature_scheme *local_schemes[] = {
invalid_scheme,
ecdsa_not_default_tls12,
ecdsa_not_default_tls13,
rsa_not_default,
rsa_default,
ecdsa_default
};
const struct s2n_signature_scheme *peer_schemes[] = {
invalid_scheme
};
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
struct s2n_local_sig_schemes_context local_context = { 0 };
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context,
local_schemes, s2n_array_len(local_schemes)));
EXPECT_OK(s2n_test_set_peer_sig_schemes(&conn->handshake_params.client_sig_hash_algs,
peer_schemes, s2n_array_len(peer_schemes)));
/* ECDSA */
conn->actual_protocol_version = S2N_TLS12;
conn->secure->cipher_suite = ECDSA_CIPHER_SUITE;
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, ecdsa_default);
/* ECDSA */
conn->actual_protocol_version = S2N_TLS12;
conn->secure->cipher_suite = RSA_CIPHER_SUITE;
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, rsa_default);
};
/* Test: TLS1.3 chooses most preferred valid */
{
/* We should need to skip valid non-default schemes to choose the defaults */
const struct s2n_signature_scheme *local_schemes[] = {
ecdsa_not_default_tls12,
rsa_default,
ecdsa_default,
ecdsa_not_default_tls13
};
const struct s2n_signature_scheme *peer_schemes[] = {
ecdsa_not_default_tls12,
/* TLS1.3 does not support the TLS1.2 defaults */
rsa_default,
ecdsa_default
};
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
conn->actual_protocol_version = S2N_TLS13;
conn->secure->cipher_suite = TLS13_CIPHER_SUITE;
EXPECT_SUCCESS(s2n_connection_set_config(conn, server_config));
struct s2n_local_sig_schemes_context local_context = { 0 };
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context,
local_schemes, s2n_array_len(local_schemes)));
EXPECT_OK(s2n_test_set_peer_sig_schemes(&conn->handshake_params.client_sig_hash_algs,
peer_schemes, s2n_array_len(peer_schemes)));
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, ecdsa_not_default_tls13);
};
};
};
/* s2n_signature_algorithm_recv */
{
struct s2n_security_policy test_security_policy = *s2n_fetch_default_config()->security_policy;
test_security_policy.signature_preferences = &test_preferences;
/* Test: successfully choose valid server signature */
{
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
conn->security_policy_override = &test_security_policy;
conn->actual_protocol_version = S2N_TLS12;
DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
EXPECT_SUCCESS(s2n_stuffer_write_uint16(&input, s2n_rsa_pkcs1_sha256.iana_value));
EXPECT_OK(s2n_signature_algorithm_recv(conn, &input));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, &s2n_rsa_pkcs1_sha256);
};
/* Test: successfully choose valid client signature */
{
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
conn->security_policy_override = &test_security_policy;
conn->actual_protocol_version = S2N_TLS12;
DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
EXPECT_SUCCESS(s2n_stuffer_write_uint16(&input, s2n_rsa_pkcs1_sha256.iana_value));
EXPECT_OK(s2n_signature_algorithm_recv(conn, &input));
EXPECT_EQUAL(conn->handshake_params.client_cert_sig_scheme, &s2n_rsa_pkcs1_sha256);
};
/* Test: algorithm not included in message */
{
struct s2n_stuffer empty = { 0 };
/* Algorithm must be provided if >= TLS1.2 */
{
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
conn->security_policy_override = &test_security_policy;
conn->actual_protocol_version = S2N_TLS12;
conn->secure->cipher_suite = RSA_CIPHER_SUITE;
EXPECT_ERROR_WITH_ERRNO(s2n_signature_algorithm_recv(conn, &empty),
S2N_ERR_BAD_MESSAGE);
}
/* Client chooses default based on cipher suite */
{
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
conn->security_policy_override = &test_security_policy;
conn->actual_protocol_version = S2N_TLS11;
conn->secure->cipher_suite = RSA_CIPHER_SUITE;
EXPECT_OK(s2n_signature_algorithm_recv(conn, &empty));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, &s2n_rsa_pkcs1_md5_sha1);
conn->secure->cipher_suite = ECDSA_CIPHER_SUITE;
EXPECT_OK(s2n_signature_algorithm_recv(conn, &empty));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, &s2n_ecdsa_sha1);
};
/* Server chooses default based on client cert type */
{
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
conn->security_policy_override = &test_security_policy;
conn->actual_protocol_version = S2N_TLS11;
conn->handshake_params.client_cert_pkey_type = S2N_PKEY_TYPE_RSA;
EXPECT_OK(s2n_signature_algorithm_recv(conn, &empty));
EXPECT_EQUAL(conn->handshake_params.client_cert_sig_scheme, &s2n_rsa_pkcs1_md5_sha1);
conn->handshake_params.client_cert_pkey_type = S2N_PKEY_TYPE_ECDSA;
EXPECT_OK(s2n_signature_algorithm_recv(conn, &empty));
EXPECT_EQUAL(conn->handshake_params.client_cert_sig_scheme, &s2n_ecdsa_sha1);
};
};
/* Test: don't negotiate signature scheme not allowed by security policy */
{
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
const struct s2n_signature_scheme *const test_schemes[] = {
&s2n_rsa_pkcs1_sha256,
&s2n_ecdsa_sha256,
/* Include legacy defaults to ensure no exceptions made for defaults */
&s2n_rsa_pkcs1_md5_sha1,
&s2n_ecdsa_sha1,
};
const struct s2n_signature_scheme *const supported_schemes[] = {
&s2n_ecdsa_sha384,
};
struct s2n_security_policy test_policy = test_security_policy;
struct s2n_signature_preferences test_prefs = {
.signature_schemes = supported_schemes,
.count = s2n_array_len(supported_schemes),
};
test_policy.signature_preferences = &test_prefs;
struct s2n_security_policy control_policy = test_security_policy;
struct s2n_signature_preferences control_prefs = {
.signature_schemes = test_schemes,
.count = s2n_array_len(test_schemes),
};
control_policy.signature_preferences = &control_prefs;
/* Signature algorithms not allowed by policy rejected */
conn->security_policy_override = &test_policy;
for (size_t i = 0; i < s2n_array_len(test_schemes); i++) {
EXPECT_SUCCESS(s2n_stuffer_write_uint16(&input, test_schemes[i]->iana_value));
EXPECT_ERROR_WITH_ERRNO(s2n_signature_algorithm_recv(conn, &input),
S2N_ERR_INVALID_SIGNATURE_SCHEME);
}
/* Signature algorithms allowed by policy accepted */
conn->security_policy_override = &control_policy;
for (size_t i = 0; i < s2n_array_len(test_schemes); i++) {
EXPECT_SUCCESS(s2n_stuffer_write_uint16(&input, test_schemes[i]->iana_value));
EXPECT_OK(s2n_signature_algorithm_recv(conn, &input));
}
};
/* Test: don't negotiate invalid signatures (protocol too high) */
{
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
conn->security_policy_override = &test_security_policy;
DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
conn->actual_protocol_version = S2N_TLS12;
EXPECT_SUCCESS(s2n_stuffer_write_uint16(&input, s2n_rsa_pkcs1_sha224.iana_value));
EXPECT_OK(s2n_signature_algorithm_recv(conn, &input));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, &s2n_rsa_pkcs1_sha224);
conn->actual_protocol_version = S2N_TLS13;
EXPECT_SUCCESS(s2n_stuffer_write_uint16(&input, s2n_rsa_pkcs1_sha224.iana_value));
EXPECT_ERROR_WITH_ERRNO(s2n_signature_algorithm_recv(conn, &input),
S2N_ERR_INVALID_SIGNATURE_SCHEME);
};
};
/* Test: choose correct signature for duplicate iana values.
* Some signature schemes have the same iana, but are different for
* different protocol versions. */
{
const struct s2n_signature_scheme *const dup_test_signature_schemes[] = {
&s2n_ecdsa_secp384r1_sha384,
&s2n_ecdsa_sha384,
};
const struct s2n_signature_preferences dup_test_preferences = {
.count = 2,
.signature_schemes = dup_test_signature_schemes,
};
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
const struct s2n_security_policy *security_policy = NULL;
EXPECT_SUCCESS(s2n_connection_get_security_policy(conn, &security_policy));
EXPECT_NOT_NULL(security_policy);
struct s2n_security_policy test_security_policy = *security_policy;
test_security_policy.signature_preferences = &dup_test_preferences;
conn->security_policy_override = &test_security_policy;
DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
conn->actual_protocol_version = S2N_TLS13;
EXPECT_SUCCESS(s2n_stuffer_write_uint16(&input, s2n_ecdsa_sha384.iana_value));
EXPECT_OK(s2n_signature_algorithm_recv(conn, &input));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, &s2n_ecdsa_secp384r1_sha384);
conn->actual_protocol_version = S2N_TLS12;
EXPECT_SUCCESS(s2n_stuffer_write_uint16(&input, s2n_ecdsa_sha384.iana_value));
EXPECT_OK(s2n_signature_algorithm_recv(conn, &input));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, &s2n_ecdsa_sha384);
};
/* Test: send and receive default signature preferences */
for (size_t i = S2N_TLS10; i < S2N_TLS13; i++) {
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
EXPECT_NOT_NULL(conn);
conn->actual_protocol_version = i;
DEFER_CLEANUP(struct s2n_stuffer result = { 0 }, s2n_stuffer_free);
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&result, 0));
EXPECT_OK(s2n_signature_algorithms_supported_list_send(conn, &result));
struct s2n_sig_scheme_list signatures = { 0 };
EXPECT_SUCCESS(s2n_recv_supported_sig_scheme_list(&result, &signatures));
EXPECT_EQUAL(s2n_stuffer_data_available(&result), 0);
/* Verify no duplicates - some preferences contain duplicates, but only
* one should be valid at a time. */
uint16_t iana = 0, other_iana = 0;
for (size_t a = 0; a < signatures.len; a++) {
iana = signatures.iana_list[a];
for (int b = 0; b < signatures.len; b++) {
if (a == b) {
continue;
}
other_iana = signatures.iana_list[b];
EXPECT_NOT_EQUAL(iana, other_iana);
}
}
};
/* Test: libcrypto may not support PSS signatures */
{
const struct s2n_signature_scheme *const pss_test_signature_schemes[] = {
&s2n_rsa_pss_rsae_sha256,
&s2n_rsa_pss_pss_sha256,
};
const struct s2n_signature_preferences pss_test_preferences = {
.count = 2,
.signature_schemes = pss_test_signature_schemes,
};
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, rsa_cert_chain));
struct s2n_security_policy test_security_policy = *s2n_fetch_default_config()->security_policy;
test_security_policy.signature_preferences = &pss_test_preferences,
config->security_policy = &test_security_policy;
/* Do not offer PSS signatures schemes if unsupported:
* s2n_signature_algorithms_supported_list_send + PSS */
{
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
conn->secure->cipher_suite = TLS13_CIPHER_SUITE;
conn->actual_protocol_version = S2N_TLS13;
EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
DEFER_CLEANUP(struct s2n_stuffer result = { 0 }, s2n_stuffer_free);
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&result, 0));
EXPECT_OK(s2n_signature_algorithms_supported_list_send(conn, &result));
uint16_t size = 0;
EXPECT_SUCCESS(s2n_stuffer_read_uint16(&result, &size));
EXPECT_EQUAL(size, s2n_stuffer_data_available(&result));
if (s2n_is_rsa_pss_certs_supported()) {
EXPECT_EQUAL(size, 2 * sizeof(uint16_t));
} else if (s2n_is_rsa_pss_signing_supported()) {
EXPECT_EQUAL(size, 1 * sizeof(uint16_t));
} else {
EXPECT_EQUAL(size, 0);
}
};
/* Do not accept a PSS signature scheme if unsupported:
* s2n_signature_algorithm_recv + PSS */
{
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
conn->secure->cipher_suite = TLS13_CIPHER_SUITE;
conn->actual_protocol_version = S2N_TLS13;
EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
EXPECT_SUCCESS(s2n_stuffer_write_uint16(&input, s2n_rsa_pss_rsae_sha256.iana_value));
if (s2n_is_rsa_pss_signing_supported()) {
EXPECT_OK(s2n_signature_algorithm_recv(conn, &input));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme, &s2n_rsa_pss_rsae_sha256);
} else {
EXPECT_ERROR_WITH_ERRNO(s2n_signature_algorithm_recv(conn, &input),
S2N_ERR_INVALID_SIGNATURE_SCHEME);
}
};
/* Do not choose a PSS signature scheme if unsupported:
* s2n_signature_algorithm_select + PSS */
{
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
conn->secure->cipher_suite = TLS13_CIPHER_SUITE;
conn->actual_protocol_version = S2N_TLS13;
EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
const struct s2n_signature_scheme *schemes[] = { &s2n_rsa_pss_rsae_sha256 };
EXPECT_OK(s2n_test_set_peer_sig_schemes(&conn->handshake_params.server_sig_hash_algs,
schemes, s2n_array_len(schemes)));
if (s2n_is_rsa_pss_signing_supported()) {
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.client_cert_sig_scheme,
&s2n_rsa_pss_rsae_sha256);
} else {
EXPECT_ERROR_WITH_ERRNO(
s2n_signature_algorithm_select(conn),
S2N_ERR_NO_VALID_SIGNATURE_SCHEME);
}
};
/* Fallback to a PSS scheme if only PKCS1 offered:
* s2n_signature_algorithm_select + PSS
*/
{
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
conn->secure->cipher_suite = TLS13_CIPHER_SUITE;
conn->actual_protocol_version = S2N_TLS13;
/* Invalid (PKCS1 not allowed by TLS1.3) */
const struct s2n_signature_scheme *peer_schemes[] = { &s2n_rsa_pkcs1_sha224 };
EXPECT_OK(s2n_test_set_peer_sig_schemes(&conn->handshake_params.client_sig_hash_algs,
peer_schemes, s2n_array_len(peer_schemes)));
/* Both PKCS1 and PSS supported */
const struct s2n_signature_scheme *local_schemes[] = {
&s2n_rsa_pkcs1_sha224,
&s2n_rsa_pss_rsae_sha256,
&s2n_rsa_pss_rsae_sha384,
};
struct s2n_local_sig_schemes_context local_context = { 0 };
EXPECT_OK(s2n_test_set_local_sig_schemes(conn, &local_context,
local_schemes, s2n_array_len(local_schemes)));
/* No fallback if PSS not valid either (no RSA cert) */
EXPECT_ERROR_WITH_ERRNO(
s2n_signature_algorithm_select(conn),
S2N_ERR_NO_VALID_SIGNATURE_SCHEME);
/* Set the RSA cert, making our PSS option valid */
EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
/* Fallback to preferred PSS scheme if supported */
if (s2n_is_rsa_pss_signing_supported()) {
EXPECT_OK(s2n_signature_algorithm_select(conn));
EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme,
&s2n_rsa_pss_rsae_sha256);
} else {
EXPECT_ERROR_WITH_ERRNO(
s2n_signature_algorithm_select(conn),
S2N_ERR_NO_VALID_SIGNATURE_SCHEME);
}
};
};
/* Self-Talk tests: default signature schemes */
{
const struct s2n_signature_scheme *const default_schemes[] = {
&s2n_rsa_pkcs1_sha1,
&s2n_ecdsa_sha1
};
const struct s2n_signature_scheme *const sha256_schemes[] = {
&s2n_rsa_pkcs1_sha256,
&s2n_ecdsa_sha256
};
const struct s2n_signature_scheme *const sha384_schemes[] = {
&s2n_rsa_pkcs1_sha384,
&s2n_ecdsa_sha384
};
const struct s2n_signature_preferences defaults_preferences = {
.count = s2n_array_len(default_schemes),
.signature_schemes = default_schemes,
};
const struct s2n_signature_preferences sha256_preferences = {
.count = s2n_array_len(sha256_schemes),
.signature_schemes = sha256_schemes,
};
for (size_t i = 0; i < sha256_preferences.count; i++) {
for (size_t j = 0; j < defaults_preferences.count; j++) {
EXPECT_NOT_EQUAL(sha256_preferences.signature_schemes[i]->iana_value,
defaults_preferences.signature_schemes[j]->iana_value);
}
}
const struct s2n_signature_preferences sha384_preferences = {
.count = s2n_array_len(sha384_schemes),
.signature_schemes = sha384_schemes,
};
for (size_t i = 0; i < sha384_preferences.count; i++) {
for (size_t j = 0; j < defaults_preferences.count; j++) {
EXPECT_NOT_EQUAL(sha384_preferences.signature_schemes[i]->iana_value,
defaults_preferences.signature_schemes[j]->iana_value);
}
}
/* The policy needs to negotiate TLS1.2 and forward secret kex */
struct s2n_security_policy defaults_policy = security_policy_20190214;
defaults_policy.signature_preferences = &defaults_preferences;
struct s2n_security_policy sha256_policy = security_policy_20190214;
sha256_policy.signature_preferences = &sha256_preferences;
struct s2n_security_policy sha384_policy = security_policy_20190214;
sha384_policy.signature_preferences = &sha384_preferences;
/* Self-Talk test: client and server can negotiate without any defaults */
for (size_t cert_i = 0; cert_i < s2n_array_len(certs); cert_i++) {
/* Setup config */
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
EXPECT_NOT_NULL(config);
EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config));
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, certs[cert_i]));
/* Setup connections */
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
/* Create nonblocking pipes */
DEFER_CLEANUP(struct s2n_test_io_pair io_pair, s2n_io_pair_close);
EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
/* Client and server security policies should match but include no defaults */
client_conn->security_policy_override = &sha256_policy;
server_conn->security_policy_override = &sha256_policy;
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS12);
EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12);
};
/* Self-Talk test: server does not fallback to unsupported defaults */
for (size_t cert_i = 0; cert_i < s2n_array_len(certs); cert_i++) {
/* Setup config */
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
EXPECT_NOT_NULL(config);
EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config));
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, certs[cert_i]));
/* Setup connections */
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
/* Create nonblocking pipes */
DEFER_CLEANUP(struct s2n_test_io_pair io_pair, s2n_io_pair_close);
EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
/* Client and server security policies should have no signature schemes
* in common, and not include the default signature schemes.
*/
client_conn->security_policy_override = &sha256_policy;
server_conn->security_policy_override = &sha384_policy;
EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
S2N_ERR_INVALID_SIGNATURE_SCHEME);
EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS12);
EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12);
};
/* Self-Talk test: client does not accept unsupported defaults */
for (size_t cert_i = 0; cert_i < s2n_array_len(certs); cert_i++) {
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
EXPECT_NOT_NULL(config);
EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config));
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, certs[cert_i]));
/* Setup connections */
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
/* Create nonblocking pipes */
DEFER_CLEANUP(struct s2n_test_io_pair io_pair, s2n_io_pair_close);
EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
/* Client and server security policies should have no signature schemes in common.
* Server should include the default policies.
*/
client_conn->security_policy_override = &sha256_policy;
server_conn->security_policy_override = &defaults_policy;
EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
S2N_ERR_INVALID_SIGNATURE_SCHEME);
EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS12);
EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12);
};
};
END_TEST();
return 0;
}
|