1#! /usr/bin/env perl 2# Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved. 3# 4# Licensed under the Apache License 2.0 (the "License"). You may not use 5# this file except in compliance with the License. You can obtain a copy 6# in the file LICENSE in the source distribution or at 7# https://www.openssl.org/source/license.html 8 9use strict; 10use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/; 11use OpenSSL::Test::Utils; 12use TLSProxy::Proxy; 13use File::Temp qw(tempfile); 14 15use constant { 16 LOOK_ONLY => 0, 17 EMPTY_EXTENSION => 1, 18 MISSING_EXTENSION => 2, 19 NO_ACCEPTABLE_KEY_SHARES => 3, 20 NON_PREFERRED_KEY_SHARE => 4, 21 ACCEPTABLE_AT_END => 5, 22 NOT_IN_SUPPORTED_GROUPS => 6, 23 GROUP_ID_TOO_SHORT => 7, 24 KEX_LEN_MISMATCH => 8, 25 ZERO_LEN_KEX_DATA => 9, 26 TRAILING_DATA => 10, 27 SELECT_X25519 => 11, 28 NO_KEY_SHARES_IN_HRR => 12, 29 NON_TLS1_3_KEY_SHARE => 13 30}; 31 32use constant { 33 CLIENT_TO_SERVER => 1, 34 SERVER_TO_CLIENT => 2 35}; 36 37 38use constant { 39 X25519 => 0x1d, 40 P_256 => 0x17, 41 FFDHE2048 => 0x0100, 42 FFDHE3072 => 0x0101 43}; 44 45my $testtype; 46my $direction; 47my $selectedgroupid; 48 49my $test_name = "test_key_share"; 50setup($test_name); 51 52plan skip_all => "TLSProxy isn't usable on $^O" 53 if $^O =~ /^(VMS)$/; 54 55plan skip_all => "$test_name needs the dynamic engine feature enabled" 56 if disabled("engine") || disabled("dynamic-engine"); 57 58plan skip_all => "$test_name needs the sock feature enabled" 59 if disabled("sock"); 60 61plan skip_all => "$test_name needs TLS1.3 enabled" 62 if disabled("tls1_3"); 63 64plan skip_all => "$test_name needs EC or DH enabled" 65 if disabled("ec") && disabled("dh"); 66 67$ENV{OPENSSL_ia32cap} = '~0x200000200000000'; 68 69my $proxy = TLSProxy::Proxy->new( 70 undef, 71 cmdstr(app(["openssl"]), display => 1), 72 srctop_file("apps", "server.pem"), 73 (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE}) 74); 75 76#We assume that test_ssl_new and friends will test the happy path for this, 77#so we concentrate on the less common scenarios 78 79#Test 1: An empty key_shares extension should succeed after a HelloRetryRequest 80$testtype = EMPTY_EXTENSION; 81$direction = CLIENT_TO_SERVER; 82$proxy->filter(\&modify_key_shares_filter); 83if (disabled("ec")) { 84 $proxy->serverflags("-groups ffdhe3072"); 85} else { 86 $proxy->serverflags("-groups P-256"); 87} 88$proxy->start() or plan skip_all => "Unable to start up Proxy for tests"; 89plan tests => 23; 90ok(TLSProxy::Message->success(), "Success after HRR"); 91 92#Test 2: The server sending an HRR requesting a group the client already sent 93# should fail 94$proxy->clear(); 95$proxy->start(); 96ok(TLSProxy::Message->fail(), "Server asks for group already provided"); 97 98#Test 3: A missing key_shares extension should not succeed 99$proxy->clear(); 100$testtype = MISSING_EXTENSION; 101$proxy->start(); 102ok(TLSProxy::Message->fail(), "Missing key_shares extension"); 103 104#Test 4: No initial acceptable key_shares should succeed after a 105# HelloRetryRequest 106$proxy->clear(); 107$proxy->filter(undef); 108if (disabled("ec")) { 109 $proxy->serverflags("-groups ffdhe3072"); 110} else { 111 $proxy->serverflags("-groups P-256"); 112} 113$proxy->start(); 114ok(TLSProxy::Message->success(), "No initial acceptable key_shares"); 115 116#Test 5: No acceptable key_shares and no shared groups should fail 117$proxy->clear(); 118$proxy->filter(undef); 119if (disabled("ec")) { 120 $proxy->serverflags("-groups ffdhe2048"); 121} else { 122 $proxy->serverflags("-groups P-256"); 123} 124if (disabled("ec")) { 125 $proxy->clientflags("-groups ffdhe3072"); 126} else { 127 $proxy->clientflags("-groups P-384"); 128} 129$proxy->start(); 130ok(TLSProxy::Message->fail(), "No acceptable key_shares"); 131 132#Test 6: A non preferred but acceptable key_share should succeed 133$proxy->clear(); 134$proxy->clientflags("-curves P-256"); 135if (disabled("ec")) { 136 $proxy->clientflags("-groups ffdhe3072"); 137} else { 138 $proxy->clientflags("-groups P-256"); 139} 140$proxy->start(); 141ok(TLSProxy::Message->success(), "Non preferred key_share"); 142$proxy->filter(\&modify_key_shares_filter); 143 144SKIP: { 145 skip "No ec support in this OpenSSL build", 1 if disabled("ec"); 146 147 #Test 7: An acceptable key_share after a list of non-acceptable ones should 148 #succeed 149 $proxy->clear(); 150 $testtype = ACCEPTABLE_AT_END; 151 $proxy->start(); 152 ok(TLSProxy::Message->success(), "Acceptable key_share at end of list"); 153} 154 155#Test 8: An acceptable key_share but for a group not in supported_groups should 156#fail 157$proxy->clear(); 158$testtype = NOT_IN_SUPPORTED_GROUPS; 159$proxy->start(); 160ok(TLSProxy::Message->fail(), "Acceptable key_share not in supported_groups"); 161 162#Test 9: Too short group_id should fail 163$proxy->clear(); 164$testtype = GROUP_ID_TOO_SHORT; 165$proxy->start(); 166ok(TLSProxy::Message->fail(), "Group id too short"); 167 168#Test 10: key_exchange length mismatch should fail 169$proxy->clear(); 170$testtype = KEX_LEN_MISMATCH; 171$proxy->start(); 172ok(TLSProxy::Message->fail(), "key_exchange length mismatch"); 173 174#Test 11: Zero length key_exchange should fail 175$proxy->clear(); 176$testtype = ZERO_LEN_KEX_DATA; 177$proxy->start(); 178ok(TLSProxy::Message->fail(), "zero length key_exchange data"); 179 180#Test 12: Trailing data on key_share list should fail 181$proxy->clear(); 182$testtype = TRAILING_DATA; 183$proxy->start(); 184ok(TLSProxy::Message->fail(), "key_share list trailing data"); 185 186#Test 13: Multiple acceptable key_shares - we choose the first one 187$proxy->clear(); 188$direction = SERVER_TO_CLIENT; 189$testtype = LOOK_ONLY; 190$selectedgroupid = 0; 191if (disabled("ec")) { 192 $proxy->clientflags("-groups ffdhe3072:ffdhe2048"); 193} else { 194 $proxy->clientflags("-groups P-256:X25519"); 195} 196$proxy->start(); 197if (disabled("ec")) { 198 ok(TLSProxy::Message->success() && ($selectedgroupid == FFDHE3072), 199 "Multiple acceptable key_shares"); 200} else { 201 ok(TLSProxy::Message->success() && ($selectedgroupid == P_256), 202 "Multiple acceptable key_shares"); 203} 204 205#Test 14: Multiple acceptable key_shares - we choose the first one (part 2) 206$proxy->clear(); 207if (disabled("ec")) { 208 $proxy->clientflags("-curves ffdhe2048:ffdhe3072"); 209} else { 210 $proxy->clientflags("-curves X25519:P-256"); 211} 212$proxy->start(); 213if (disabled("ec")) { 214 ok(TLSProxy::Message->success() && ($selectedgroupid == FFDHE2048), 215 "Multiple acceptable key_shares (part 2)"); 216} else { 217 ok(TLSProxy::Message->success() && ($selectedgroupid == X25519), 218 "Multiple acceptable key_shares (part 2)"); 219} 220 221#Test 15: Server sends key_share that wasn't offered should fail 222$proxy->clear(); 223$testtype = SELECT_X25519; 224if (disabled("ec")) { 225 $proxy->clientflags("-groups ffdhe3072"); 226} else { 227 $proxy->clientflags("-groups P-256"); 228} 229$proxy->start(); 230ok(TLSProxy::Message->fail(), "Non offered key_share"); 231 232#Test 16: Too short group_id in ServerHello should fail 233$proxy->clear(); 234$testtype = GROUP_ID_TOO_SHORT; 235$proxy->start(); 236ok(TLSProxy::Message->fail(), "Group id too short in ServerHello"); 237 238#Test 17: key_exchange length mismatch in ServerHello should fail 239$proxy->clear(); 240$testtype = KEX_LEN_MISMATCH; 241$proxy->start(); 242ok(TLSProxy::Message->fail(), "key_exchange length mismatch in ServerHello"); 243 244#Test 18: Zero length key_exchange in ServerHello should fail 245$proxy->clear(); 246$testtype = ZERO_LEN_KEX_DATA; 247$proxy->start(); 248ok(TLSProxy::Message->fail(), "zero length key_exchange data in ServerHello"); 249 250#Test 19: Trailing data on key_share in ServerHello should fail 251$proxy->clear(); 252$testtype = TRAILING_DATA; 253$proxy->start(); 254ok(TLSProxy::Message->fail(), "key_share trailing data in ServerHello"); 255 256SKIP: { 257 skip "No TLSv1.2 support in this OpenSSL build", 2 if disabled("tls1_2"); 258 259 #Test 20: key_share should not be sent if the client is not capable of 260 # negotiating TLSv1.3 261 $proxy->clear(); 262 $proxy->filter(undef); 263 $proxy->clientflags("-no_tls1_3"); 264 $proxy->start(); 265 my $clienthello = $proxy->message_list->[0]; 266 ok(TLSProxy::Message->success() 267 && !defined $clienthello->extension_data->{TLSProxy::Message::EXT_KEY_SHARE}, 268 "No key_share for TLS<=1.2 client"); 269 $proxy->filter(\&modify_key_shares_filter); 270 271 #Test 21: A server not capable of negotiating TLSv1.3 should not attempt to 272 # process a key_share 273 $proxy->clear(); 274 $direction = CLIENT_TO_SERVER; 275 $testtype = NO_ACCEPTABLE_KEY_SHARES; 276 $proxy->serverflags("-no_tls1_3"); 277 $proxy->start(); 278 ok(TLSProxy::Message->success(), "Ignore key_share for TLS<=1.2 server"); 279} 280 281#Test 22: The server sending an HRR but not requesting a new key_share should 282# fail 283$proxy->clear(); 284$direction = SERVER_TO_CLIENT; 285$testtype = NO_KEY_SHARES_IN_HRR; 286if (disabled("ec")) { 287 $proxy->serverflags("-groups ffdhe2048"); 288} else { 289 $proxy->serverflags("-groups X25519"); 290} 291$proxy->start(); 292ok(TLSProxy::Message->fail(), "Server sends HRR with no key_shares"); 293 294SKIP: { 295 skip "No EC support in this OpenSSL build", 1 if disabled("ec"); 296 #Test 23: Trailing data on key_share in ServerHello should fail 297 $proxy->clear(); 298 $direction = CLIENT_TO_SERVER; 299 $proxy->clientflags("-groups secp192r1:P-256:X25519"); 300 $proxy->ciphers("AES128-SHA:\@SECLEVEL=0"); 301 $testtype = NON_TLS1_3_KEY_SHARE; 302 $proxy->start(); 303 my $ishrr = defined ${$proxy->message_list}[2] 304 &&(${$proxy->message_list}[0]->mt == TLSProxy::Message::MT_CLIENT_HELLO) 305 && (${$proxy->message_list}[2]->mt == TLSProxy::Message::MT_CLIENT_HELLO); 306 ok(TLSProxy::Message->success() && $ishrr, 307 "Client sends a key_share for a Non TLSv1.3 group"); 308} 309 310sub modify_key_shares_filter 311{ 312 my $proxy = shift; 313 314 # We're only interested in the initial ClientHello/SererHello/HRR 315 if (($direction == CLIENT_TO_SERVER && $proxy->flight != 0 316 && ($proxy->flight != 1 || $testtype != NO_KEY_SHARES_IN_HRR)) 317 || ($direction == SERVER_TO_CLIENT && $proxy->flight != 1)) { 318 return; 319 } 320 321 foreach my $message (@{$proxy->message_list}) { 322 if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO 323 && $direction == CLIENT_TO_SERVER) { 324 my $ext; 325 my $suppgroups; 326 327 if ($testtype != NON_TLS1_3_KEY_SHARE) { 328 #Setup supported groups to include some unrecognised groups 329 $suppgroups = pack "C8", 330 0x00, 0x06, #List Length 331 0xff, 0xfe, #Non existing group 1 332 0xff, 0xff, #Non existing group 2 333 0x00, 0x1d; #x25519 334 } else { 335 $suppgroups = pack "C6", 336 0x00, 0x04, #List Length 337 0x00, 0x13, 338 0x00, 0x1d; #x25519 339 } 340 341 if ($testtype == EMPTY_EXTENSION) { 342 $ext = pack "C2", 343 0x00, 0x00; 344 } elsif ($testtype == NO_ACCEPTABLE_KEY_SHARES) { 345 $ext = pack "C12", 346 0x00, 0x0a, #List Length 347 0xff, 0xfe, #Non existing group 1 348 0x00, 0x01, 0xff, #key_exchange data 349 0xff, 0xff, #Non existing group 2 350 0x00, 0x01, 0xff; #key_exchange data 351 } elsif ($testtype == ACCEPTABLE_AT_END) { 352 $ext = pack "C11H64", 353 0x00, 0x29, #List Length 354 0xff, 0xfe, #Non existing group 1 355 0x00, 0x01, 0xff, #key_exchange data 356 0x00, 0x1d, #x25519 357 0x00, 0x20, #key_exchange data length 358 "155155B95269ED5C87EAA99C2EF5A593". 359 "EDF83495E80380089F831B94D14B1421"; #key_exchange data 360 } elsif ($testtype == NOT_IN_SUPPORTED_GROUPS) { 361 $suppgroups = pack "C4", 362 0x00, 0x02, #List Length 363 0x00, 0xfe; #Non existing group 1 364 } elsif ($testtype == GROUP_ID_TOO_SHORT) { 365 $ext = pack "C6H64C1", 366 0x00, 0x25, #List Length 367 0x00, 0x1d, #x25519 368 0x00, 0x20, #key_exchange data length 369 "155155B95269ED5C87EAA99C2EF5A593". 370 "EDF83495E80380089F831B94D14B1421"; #key_exchange data 371 0x00; #Group id too short 372 } elsif ($testtype == KEX_LEN_MISMATCH) { 373 $ext = pack "C8", 374 0x00, 0x06, #List Length 375 0x00, 0x1d, #x25519 376 0x00, 0x20, #key_exchange data length 377 0x15, 0x51; #Only two bytes of data, but length should be 32 378 } elsif ($testtype == ZERO_LEN_KEX_DATA) { 379 $ext = pack "C10H64", 380 0x00, 0x28, #List Length 381 0xff, 0xfe, #Non existing group 1 382 0x00, 0x00, #zero length key_exchange data is invalid 383 0x00, 0x1d, #x25519 384 0x00, 0x20, #key_exchange data length 385 "155155B95269ED5C87EAA99C2EF5A593". 386 "EDF83495E80380089F831B94D14B1421"; #key_exchange data 387 } elsif ($testtype == TRAILING_DATA) { 388 $ext = pack "C6H64C1", 389 0x00, 0x24, #List Length 390 0x00, 0x1d, #x25519 391 0x00, 0x20, #key_exchange data length 392 "155155B95269ED5C87EAA99C2EF5A593". 393 "EDF83495E80380089F831B94D14B1421", #key_exchange data 394 0x00; #Trailing garbage 395 } elsif ($testtype == NO_KEY_SHARES_IN_HRR) { 396 #We trick the server into thinking we sent a P-256 key_share - 397 #but the client actually sent X25519 398 $ext = pack "C7", 399 0x00, 0x05, #List Length 400 0x00, 0x17, #P-256 401 0x00, 0x01, #key_exchange data length 402 0xff; #Dummy key_share data 403 } elsif ($testtype == NON_TLS1_3_KEY_SHARE) { 404 $ext = pack "C6H98", 405 0x00, 0x35, #List Length 406 0x00, 0x13, #P-192 407 0x00, 0x31, #key_exchange data length 408 "04EE3B38D1CB800A1A2B702FC8423599F2AC7161E175C865F8". 409 "3DAF78BCBAE561464E8144359BE70CB7989D28A2F43F8F2C"; #key_exchange data 410 } 411 412 if ($testtype != EMPTY_EXTENSION 413 && $testtype != NO_KEY_SHARES_IN_HRR) { 414 $message->set_extension( 415 TLSProxy::Message::EXT_SUPPORTED_GROUPS, $suppgroups); 416 } 417 if ($testtype == MISSING_EXTENSION) { 418 $message->delete_extension( 419 TLSProxy::Message::EXT_KEY_SHARE); 420 } elsif ($testtype != NOT_IN_SUPPORTED_GROUPS) { 421 $message->set_extension( 422 TLSProxy::Message::EXT_KEY_SHARE, $ext); 423 } 424 425 $message->repack(); 426 } elsif ($message->mt == TLSProxy::Message::MT_SERVER_HELLO 427 && $direction == SERVER_TO_CLIENT) { 428 my $ext; 429 my $key_share = 430 $message->extension_data->{TLSProxy::Message::EXT_KEY_SHARE}; 431 $selectedgroupid = unpack("n", $key_share); 432 433 if ($testtype == LOOK_ONLY) { 434 return; 435 } 436 if ($testtype == NO_KEY_SHARES_IN_HRR) { 437 $message->delete_extension(TLSProxy::Message::EXT_KEY_SHARE); 438 $message->set_extension(TLSProxy::Message::EXT_UNKNOWN, ""); 439 $message->repack(); 440 return; 441 } 442 if ($testtype == SELECT_X25519) { 443 $ext = pack "C4H64", 444 0x00, 0x1d, #x25519 445 0x00, 0x20, #key_exchange data length 446 "155155B95269ED5C87EAA99C2EF5A593". 447 "EDF83495E80380089F831B94D14B1421"; #key_exchange data 448 } elsif ($testtype == GROUP_ID_TOO_SHORT) { 449 $ext = pack "C1", 450 0x00; 451 } elsif ($testtype == KEX_LEN_MISMATCH) { 452 $ext = pack "C6", 453 0x00, 0x1d, #x25519 454 0x00, 0x20, #key_exchange data length 455 0x15, 0x51; #Only two bytes of data, but length should be 32 456 } elsif ($testtype == ZERO_LEN_KEX_DATA) { 457 $ext = pack "C4", 458 0x00, 0x1d, #x25519 459 0x00, 0x00, #zero length key_exchange data is invalid 460 } elsif ($testtype == TRAILING_DATA) { 461 $ext = pack "C4H64C1", 462 0x00, 0x1d, #x25519 463 0x00, 0x20, #key_exchange data length 464 "155155B95269ED5C87EAA99C2EF5A593". 465 "EDF83495E80380089F831B94D14B1421", #key_exchange data 466 0x00; #Trailing garbage 467 } 468 $message->set_extension(TLSProxy::Message::EXT_KEY_SHARE, $ext); 469 470 $message->repack(); 471 } 472 } 473} 474 475 476