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
|
/*
* Copyright 2025 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include "testutil.h"
#include "helpers/ssltestlib.h"
#include <openssl/objects.h>
#define TEST_true_or_end(a) if (!TEST_true(a)) \
goto end;
#define TEST_false_or_end(a) if (!TEST_false(a)) \
goto end;
#define SERVER_PREFERENCE 1
#define CLIENT_PREFERENCE 0
#define WORK_ON_SSL_OBJECT 1
#define WORK_ON_CONTEXT 0
#define SYNTAX_FAILURE "SYNTAX_FAILURE"
#define NEGOTIATION_FAILURE "NEGOTIATION_FAILURE"
typedef enum TEST_TYPE {
TEST_NEGOTIATION_FAILURE = 0,
TEST_NEGOTIATION_SUCCESS = 1,
TEST_SYNTAX_FAILURE = 2
} TEST_TYPE;
typedef enum SERVER_RESPONSE {
HRR = 0,
INIT = 1,
SH = 2
} SERVER_RESPONSE;
static char *cert = NULL;
static char *privkey = NULL;
struct tls13groupselection_test_st {
const char *client_groups;
const char *server_groups;
const int preference;
const char *expected_group;
const enum SERVER_RESPONSE expected_server_response;
};
static const struct tls13groupselection_test_st tls13groupselection_tests[] =
{
/*
* (A) Test with no explicit key share (backward compatibility)
* Key share is implicitly sent for first client group
* Test (implicitly) that the key share group is used
*/
{ "secp384r1:secp521r1:X25519:prime256v1:X448", /* test 0 */
"X25519:secp521r1:secp384r1:prime256v1:X448",
CLIENT_PREFERENCE,
"secp384r1", SH
},
{ "secp521r1:secp384r1:X25519:prime256v1:X448", /* test 1 */
"X25519:secp521r1:secp384r1:prime256v1:X448",
SERVER_PREFERENCE,
"secp521r1", SH
},
/*
* (B) No explicit key share test (backward compatibility)
* Key share is implicitly sent for first client group
* Check HRR if server does not support key share group
*/
{ "secp521r1:secp384r1:X25519:prime256v1:X448", /* test 2 */
"X25519:secp384r1:prime256v1",
CLIENT_PREFERENCE,
"secp384r1", HRR
},
{ "secp521r1:secp384r1:X25519:prime256v1:X448", /* test 3 */
"X25519:secp384r1:prime256v1",
SERVER_PREFERENCE,
"x25519", HRR
},
/*
* (C) Explicit key shares, SH tests
* Test key share selection as function of client-/server-preference
* Test (implicitly) that multiple key shares are generated
* Test (implicitly) that multiple tuples don't influence the client
* Test (implicitly) that key share prefix doesn't influence the server
*/
{ "secp521r1:secp384r1:*X25519/*prime256v1:X448", /* test 4 */
"secp521r1:*prime256v1:X25519:X448",
CLIENT_PREFERENCE,
"x25519", SH
},
{ "secp521r1:secp384r1:*X25519/*prime256v1:X448", /* test 5 */
"secp521r1:*prime256v1:X25519:X448",
SERVER_PREFERENCE,
"secp256r1", SH
},
/*
* (D) Explicit key shares, HRR tests
* Check that HRR is issued if group in first tuple
* is supported but no key share is available for the tuple
*/
{ "secp521r1:secp384r1:*X25519:prime256v1:*X448", /* test 6 */
"secp384r1:secp521r1:prime256v1/X25519:X448",
CLIENT_PREFERENCE,
"secp521r1", HRR
},
{ "secp521r1:secp384r1:*X25519:prime256v1:*X448", /* test 7 */
"secp384r1:secp521r1:prime256v1/X25519:X448",
SERVER_PREFERENCE,
"secp384r1", HRR
},
/*
* (E) Multiple tuples tests, client without tuple delimiters
* Check that second tuple is evaluated if there isn't any match
* first tuple
*/
{ "*X25519:prime256v1:*X448", /* test 8 */
"secp521r1:secp384r1/X448:X25519",
CLIENT_PREFERENCE,
"x25519", SH
},
{ "*X25519:prime256v1:*X448", /* test 9 */
"secp521r1:secp384r1/X448:X25519",
SERVER_PREFERENCE,
"x448", SH
},
/* (F) Check that '?' will ignore unknown group but use known group */
{ "*X25519:?unknown_group_123:prime256v1:*X448", /* test 10 */
"secp521r1:secp384r1/X448:?unknown_group_456:?X25519",
CLIENT_PREFERENCE,
"x25519", SH
},
{ "*X25519:prime256v1:*X448:?*unknown_group_789", /* test 11 */
"secp521r1:secp384r1/?X448:?unknown_group_456:X25519",
SERVER_PREFERENCE,
"x448", SH
},
/*
* (G) Check full backward compatibility (= don't explicitly set any groups)
*/
{ NULL, /* test 12 */
NULL,
CLIENT_PREFERENCE,
#ifndef OPENSSL_NO_ML_KEM
"X25519MLKEM768", SH
#else
"x25519", SH
#endif
},
{ NULL, /* test 13 */
NULL,
SERVER_PREFERENCE,
#ifndef OPENSSL_NO_ML_KEM
"X25519MLKEM768", SH
#else
"x25519", SH
#endif
},
/*
* (H) Check that removal of group is 'active'
*/
{ "*X25519:*X448", /* test 14 */
"secp521r1:X25519:prime256v1:-X25519:secp384r1/X448",
CLIENT_PREFERENCE,
"x448", SH
},
{ "*X25519:*X448", /* test 15 */
"secp521r1:X25519:prime256v1:-X25519:secp384r1/X448",
SERVER_PREFERENCE,
"x448", SH
},
{ "*X25519:prime256v1:*X448", /* test 16 */
"X25519:prime256v1/X448:-X25519",
CLIENT_PREFERENCE,
"secp256r1", HRR
},
{ "*X25519:prime256v1:*X448", /* test 17 */
"X25519:prime256v1/X448:-X25519",
SERVER_PREFERENCE,
"secp256r1", HRR
},
/*
* (I) Check handling of the "DEFAULT" 'pseudo group name'
*/
{ "*X25519:DEFAULT:-prime256v1:-X448", /* test 18 */
"DEFAULT:-X25519:-?X25519MLKEM768",
CLIENT_PREFERENCE,
"secp384r1", HRR
},
{ "*X25519:DEFAULT:-prime256v1:-X448", /* test 19 */
"DEFAULT:-X25519:-?X25519MLKEM768",
SERVER_PREFERENCE,
"secp384r1", HRR
},
/*
* (J) Deduplication check
*/
{ "secp521r1:X25519:prime256v1/X25519:prime256v1/X448", /* test 20 */
"secp521r1:X25519:prime256v1/X25519:prime256v1/X448",
CLIENT_PREFERENCE,
"secp521r1", SH
},
{ "secp521r1:X25519:prime256v1/X25519:prime256v1/X448", /* test 21 */
"secp521r1:X25519:prime256v1/X25519:prime256v1/X448",
SERVER_PREFERENCE,
"secp521r1", SH
},
/*
* (K) Check group removal when first entry requested a keyshare
*/
{ "*X25519:*prime256v1:-X25519", /* test 22 */
"X25519:prime256v1",
CLIENT_PREFERENCE,
"secp256r1", SH
},
/*
* (L) Syntax errors
*/
{ "*X25519:*prime256v1:NOTVALID", /* test 23 */
"",
CLIENT_PREFERENCE,
SYNTAX_FAILURE
},
{ "X25519//prime256v1", /* test 24 */
"",
CLIENT_PREFERENCE,
SYNTAX_FAILURE
},
{ "**X25519:*prime256v1", /* test 25 */
"",
CLIENT_PREFERENCE,
SYNTAX_FAILURE
},
{ "*X25519:*secp256r1:*X448:*secp521r1:*secp384r1", /* test 26 */
"",
CLIENT_PREFERENCE,
SYNTAX_FAILURE
},
{ "*X25519:*secp256r1:?:*secp521r1", /* test 27 */
"",
CLIENT_PREFERENCE,
SYNTAX_FAILURE
},
{ "*X25519:*secp256r1::secp521r1", /* test 28 */
"",
CLIENT_PREFERENCE,
SYNTAX_FAILURE
},
{ ":*secp256r1:secp521r1", /* test 29 */
"",
CLIENT_PREFERENCE,
SYNTAX_FAILURE
},
{ "*secp256r1:secp521r1:", /* test 30 */
"",
CLIENT_PREFERENCE,
SYNTAX_FAILURE
},
{ "/secp256r1/secp521r1", /* test 31 */
"",
CLIENT_PREFERENCE,
SYNTAX_FAILURE
},
{ "secp256r1/secp521r1/", /* test 32 */
"",
CLIENT_PREFERENCE,
SYNTAX_FAILURE
},
{ "X25519:??secp256r1:X448", /* test 33 */
"",
CLIENT_PREFERENCE,
SYNTAX_FAILURE
},
{ "X25519:secp256r1:**X448", /* test 34 */
"",
CLIENT_PREFERENCE,
SYNTAX_FAILURE
},
{ "--X25519:secp256r1:X448", /* test 35 */
"",
CLIENT_PREFERENCE,
SYNTAX_FAILURE
},
{ "-DEFAULT", /* test 36 */
"",
CLIENT_PREFERENCE,
SYNTAX_FAILURE
},
{ "?DEFAULT", /* test 37 */
"",
CLIENT_PREFERENCE,
SYNTAX_FAILURE
},
/*
* Negotiation Failures
* No overlapping groups between client and server
*/
/* test 38 remove all groups */
{ "X25519:secp256r1:X448:secp521r1:-X448:-secp256r1:-X25519:-secp521r1",
"",
CLIENT_PREFERENCE,
NEGOTIATION_FAILURE, INIT
},
{ "secp384r1:secp521r1:X25519", /* test 39 */
"prime256v1:X448",
CLIENT_PREFERENCE,
NEGOTIATION_FAILURE, INIT
},
{ "secp521r1:secp384r1:X25519", /* test 40 */
"prime256v1:X448",
SERVER_PREFERENCE,
NEGOTIATION_FAILURE, INIT
},
/*
* These are allowed
* "X25519/prime256v1:-X448", "X25519:-*X25519:*prime256v1, "*DEFAULT"
*/
/*
* Tests to show that spaces between tuples are allowed
*/
{ "secp521r1:X25519 / prime256v1/X25519 / prime256v1/X448", /* test 41 */
"secp521r1:X25519 / prime256v1/X25519 / prime256v1/X448",
CLIENT_PREFERENCE,
"secp521r1", SH
},
{ "secp521r1 / prime256v1:X25519 / prime256v1/X448", /* test 42 */
"secp521r1 / prime256v1:X25519 / prime256v1/X448",
SERVER_PREFERENCE,
"secp521r1", SH
},
/*
* Not a syntax error, but invalid because brainpoolP256r1 is the only
* key share and is not valid in TLSv1.3
*/
{ "*brainpoolP256r1:X25519", /* test 43 */
"X25519",
SERVER_PREFERENCE,
NEGOTIATION_FAILURE, INIT
}
};
static void server_response_check_cb(int write_p, int version,
int content_type, const void *buf,
size_t len, SSL *ssl, void *arg)
{
/* Cast arg to SERVER_RESPONSE */
enum SERVER_RESPONSE *server_response = (enum SERVER_RESPONSE *)arg;
/* Prepare check for HRR */
const uint8_t *incoming_random = (uint8_t *)buf + 6;
const uint8_t magic_HRR_random[32] = { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C };
/* Did a server hello arrive? */
if (write_p == 0 && /* Incoming data... */
content_type == SSL3_RT_HANDSHAKE && /* carrying a handshake record type ... */
version == TLS1_3_VERSION && /* for TLSv1.3 ... */
((uint8_t *)buf)[0] == SSL3_MT_SERVER_HELLO) { /* with message type "ServerHello" */
/* Check what it is: SH or HRR (compare the 'random' data field with HRR magic number) */
if (memcmp((void *)incoming_random, (void *)magic_HRR_random, 32) == 0)
*server_response *= HRR;
else
*server_response *= SH;
}
}
static int test_invalidsyntax(const struct tls13groupselection_test_st *current_test_vector,
int ssl_or_ctx)
{
int ok = 0;
SSL_CTX *client_ctx = NULL, *server_ctx = NULL;
SSL *clientssl = NULL, *serverssl = NULL;
if (!TEST_ptr(current_test_vector->client_groups)
|| !TEST_size_t_ne(strlen(current_test_vector->client_groups), 0))
goto end;
/* Creation of the contexts */
TEST_true_or_end(create_ssl_ctx_pair(NULL, TLS_server_method(),
TLS_client_method(),
TLS1_VERSION, 0,
&server_ctx, &client_ctx,
cert, privkey));
/* Customization of the contexts */
if (ssl_or_ctx == WORK_ON_CONTEXT)
TEST_false_or_end(SSL_CTX_set1_groups_list(client_ctx,
current_test_vector->client_groups));
/* Creation of the SSL objects */
TEST_true_or_end(create_ssl_objects(server_ctx, client_ctx,
&serverssl, &clientssl,
NULL, NULL));
/* Customization of the SSL objects */
if (ssl_or_ctx == WORK_ON_SSL_OBJECT)
TEST_false_or_end(SSL_set1_groups_list(clientssl, current_test_vector->client_groups));
ok = 1;
end:
SSL_free(serverssl);
SSL_free(clientssl);
SSL_CTX_free(server_ctx);
SSL_CTX_free(client_ctx);
return ok;
}
static int test_groupnegotiation(const struct tls13groupselection_test_st *current_test_vector,
int ssl_or_ctx, TEST_TYPE test_type)
{
int ok = 0;
int negotiated_group_client = 0;
int negotiated_group_server = 0;
const char *group_name_client;
SSL_CTX *client_ctx = NULL, *server_ctx = NULL;
SSL *clientssl = NULL, *serverssl = NULL;
enum SERVER_RESPONSE server_response;
/* Creation of the contexts */
TEST_true_or_end(create_ssl_ctx_pair(NULL, TLS_server_method(),
TLS_client_method(),
TLS1_VERSION, 0,
&server_ctx, &client_ctx,
cert, privkey));
/* Customization of the contexts */
if (ssl_or_ctx == WORK_ON_CONTEXT) {
if (current_test_vector->client_groups != NULL) {
TEST_true_or_end(SSL_CTX_set1_groups_list(client_ctx,
current_test_vector->client_groups));
}
if (current_test_vector->server_groups != NULL) {
TEST_true_or_end(SSL_CTX_set1_groups_list(server_ctx,
current_test_vector->server_groups));
}
TEST_true_or_end(SSL_CTX_set_min_proto_version(client_ctx, TLS1_3_VERSION));
TEST_true_or_end(SSL_CTX_set_min_proto_version(server_ctx, TLS1_3_VERSION));
if (current_test_vector->preference == SERVER_PREFERENCE)
SSL_CTX_set_options(server_ctx, SSL_OP_SERVER_PREFERENCE);
}
/* Creation of the SSL objects */
if (!TEST_true(create_ssl_objects(server_ctx, client_ctx,
&serverssl, &clientssl,
NULL, NULL)))
goto end;
/* Customization of the SSL objects */
if (ssl_or_ctx == WORK_ON_SSL_OBJECT) {
if (current_test_vector->client_groups != NULL)
TEST_true_or_end(SSL_set1_groups_list(clientssl, current_test_vector->client_groups));
if (current_test_vector->server_groups != NULL)
TEST_true_or_end(SSL_set1_groups_list(serverssl, current_test_vector->server_groups));
TEST_true_or_end(SSL_set_min_proto_version(clientssl, TLS1_3_VERSION));
TEST_true_or_end(SSL_set_min_proto_version(serverssl, TLS1_3_VERSION));
if (current_test_vector->preference == SERVER_PREFERENCE)
SSL_set_options(serverssl, SSL_OP_SERVER_PREFERENCE);
}
/* We set the message callback on the client side (which checks SH/HRR) */
server_response = INIT; /* Variable to hold server response info */
SSL_set_msg_callback_arg(clientssl, &server_response); /* add it to the callback */
SSL_set_msg_callback(clientssl, server_response_check_cb); /* and activate callback */
/* Creating a test connection */
if (test_type == TEST_NEGOTIATION_SUCCESS) {
TEST_true_or_end(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE));
/*
* Checking that the negotiated group matches our expectation
* and must be identical on server and client
* and must be expected SH or HRR
*/
negotiated_group_client = SSL_get_negotiated_group(clientssl);
negotiated_group_server = SSL_get_negotiated_group(serverssl);
group_name_client = SSL_group_to_name(clientssl, negotiated_group_client);
if (!TEST_int_eq(negotiated_group_client, negotiated_group_server))
goto end;
if (!TEST_int_eq((int)current_test_vector->expected_server_response, (int)server_response))
goto end;
if (TEST_str_eq(group_name_client, current_test_vector->expected_group))
ok = 1;
} else {
TEST_false_or_end(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE));
if (test_type == TEST_NEGOTIATION_FAILURE &&
!TEST_int_eq((int)current_test_vector->expected_server_response,
(int)server_response))
goto end;
ok = 1;
}
end:
SSL_free(serverssl);
SSL_free(clientssl);
SSL_CTX_free(server_ctx);
SSL_CTX_free(client_ctx);
return ok;
}
static int tls13groupselection_test(int i)
{
int testresult = 1; /* Assume the test will succeed */
int res = 0;
TEST_TYPE test_type = TEST_NEGOTIATION_SUCCESS;
/*
* Call the code under test, once such that the ssl object is used and
* once such that the ctx is used. If any of the tests fail (= return 0),
* the end result will be 0 thanks to multiplication
*/
TEST_info("==> Running TLSv1.3 test %d", i);
if (strncmp(tls13groupselection_tests[i].expected_group,
SYNTAX_FAILURE, sizeof(SYNTAX_FAILURE)) == 0)
test_type = TEST_SYNTAX_FAILURE;
else if (strncmp(tls13groupselection_tests[i].expected_group,
NEGOTIATION_FAILURE, sizeof(NEGOTIATION_FAILURE)) == 0)
test_type = TEST_NEGOTIATION_FAILURE;
if (test_type == TEST_SYNTAX_FAILURE)
res = test_invalidsyntax(&tls13groupselection_tests[i],
WORK_ON_SSL_OBJECT);
else
res = test_groupnegotiation(&tls13groupselection_tests[i],
WORK_ON_SSL_OBJECT, test_type);
if (!res)
TEST_error("====> [ERROR] TLSv1.3 test %d with WORK_ON_SSL_OBJECT failed", i);
testresult *= res;
if (test_type == TEST_SYNTAX_FAILURE)
res = test_invalidsyntax(&tls13groupselection_tests[i],
WORK_ON_CONTEXT);
else
res = test_groupnegotiation(&tls13groupselection_tests[i],
WORK_ON_CONTEXT, test_type);
if (!res)
TEST_error("====> [ERROR] TLSv1.3 test %d with WORK_ON_CONTEXT failed", i);
testresult *= res;
return testresult;
}
int setup_tests(void)
{
if (!TEST_ptr(cert = test_get_argument(0))
|| !TEST_ptr(privkey = test_get_argument(1)))
return 0;
ADD_ALL_TESTS(tls13groupselection_test, OSSL_NELEM(tls13groupselection_tests));
return 1;
}
|