70-test_tls13hrr.t 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #! /usr/bin/env perl
  2. # Copyright 2017-2024 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. use strict;
  9. use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
  10. use OpenSSL::Test::Utils;
  11. use TLSProxy::Proxy;
  12. use TLSProxy::Message;
  13. my $test_name = "test_tls13hrr";
  14. setup($test_name);
  15. plan skip_all => "TLSProxy isn't usable on $^O"
  16. if $^O =~ /^(VMS)$/;
  17. plan skip_all => "$test_name needs the dynamic engine feature enabled"
  18. if disabled("engine") || disabled("dynamic-engine");
  19. plan skip_all => "$test_name needs the sock feature enabled"
  20. if disabled("sock");
  21. plan skip_all => "$test_name needs TLS1.3 enabled"
  22. if disabled("tls1_3") || (disabled("ec") && disabled("dh"));
  23. my $proxy = TLSProxy::Proxy->new(
  24. undef,
  25. cmdstr(app(["openssl"]), display => 1),
  26. srctop_file("apps", "server.pem"),
  27. (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
  28. );
  29. use constant {
  30. CHANGE_HRR_CIPHERSUITE => 0,
  31. CHANGE_CH1_CIPHERSUITE => 1,
  32. DUPLICATE_HRR => 2,
  33. INVALID_GROUP => 3
  34. };
  35. #Test 1: A client should fail if the server changes the ciphersuite between the
  36. # HRR and the SH
  37. $proxy->filter(\&hrr_filter);
  38. if (disabled("ec")) {
  39. $proxy->serverflags("-curves ffdhe3072");
  40. } else {
  41. $proxy->serverflags("-curves P-256");
  42. }
  43. my $testtype = CHANGE_HRR_CIPHERSUITE;
  44. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  45. plan tests => 4;
  46. ok(TLSProxy::Message->fail(), "Server ciphersuite changes");
  47. #Test 2: It is an error if the client changes the offered ciphersuites so that
  48. # we end up selecting a different ciphersuite between HRR and the SH
  49. $proxy->clear();
  50. if (disabled("ec")) {
  51. $proxy->serverflags("-curves ffdhe3072");
  52. } else {
  53. $proxy->serverflags("-curves P-384");
  54. }
  55. $proxy->ciphersuitess("TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384");
  56. $testtype = CHANGE_CH1_CIPHERSUITE;
  57. $proxy->start();
  58. ok(TLSProxy::Message->fail(), "Client ciphersuite changes");
  59. #Test 3: A client should fail with unexpected_message alert if the server
  60. # sends more than 1 HRR
  61. my $fatal_alert = 0;
  62. $proxy->clear();
  63. if (disabled("ec")) {
  64. $proxy->serverflags("-curves ffdhe3072");
  65. } else {
  66. $proxy->serverflags("-curves P-384");
  67. }
  68. $testtype = DUPLICATE_HRR;
  69. $proxy->start();
  70. ok($fatal_alert, "Server duplicated HRR");
  71. #Test 4: If the client sends a group that is in the supported_groups list but
  72. # otherwise not valid (e.g. not suitable for TLSv1.3) we should reject it
  73. # and not consider it when sending the HRR. We send brainpoolP512r1 in
  74. # the ClientHello, which is acceptable to the server but is not valid in
  75. # TLSv1.3. We expect the server to select P-521 in the HRR and the
  76. # handshake to complete successfully
  77. SKIP: {
  78. skip "EC/TLSv1.2 is disabled in this build", 1
  79. if disabled("ec") || disabled("tls1_2");
  80. $proxy->clear();
  81. $proxy->clientflags("-groups P-256:brainpoolP512r1:P-521");
  82. $proxy->serverflags("-groups brainpoolP512r1:P-521");
  83. $testtype = INVALID_GROUP;
  84. $proxy->start();
  85. ok(TLSProxy::Message->success(), "Invalid group with HRR");
  86. }
  87. sub hrr_filter
  88. {
  89. my $proxy = shift;
  90. if ($testtype == CHANGE_HRR_CIPHERSUITE) {
  91. # We're only interested in the HRR
  92. if ($proxy->flight != 1) {
  93. return;
  94. }
  95. my $hrr = ${$proxy->message_list}[1];
  96. # We will normally only ever select CIPHER_TLS13_AES_128_GCM_SHA256
  97. # because that's what Proxy tells s_server to do. Setting as below means
  98. # the ciphersuite will change will we get the ServerHello
  99. $hrr->ciphersuite(TLSProxy::Message::CIPHER_TLS13_AES_256_GCM_SHA384);
  100. $hrr->repack();
  101. return;
  102. }
  103. if ($testtype == DUPLICATE_HRR) {
  104. # We're only interested in the HRR
  105. # and the unexpected_message alert from client
  106. if ($proxy->flight == 4) {
  107. $fatal_alert = 1
  108. if @{$proxy->record_list}[-1]->is_fatal_alert(0) == TLSProxy::Message::AL_DESC_UNEXPECTED_MESSAGE;
  109. return;
  110. }
  111. if ($proxy->flight != 3) {
  112. return;
  113. }
  114. # Find ServerHello record (HRR actually) and insert after that
  115. my $i;
  116. for ($i = 0; ${$proxy->record_list}[$i]->flight() < 1; $i++) {
  117. next;
  118. }
  119. my $hrr_record = ${$proxy->record_list}[$i];
  120. my $dup_hrr = TLSProxy::Record->new(3,
  121. $hrr_record->content_type(),
  122. $hrr_record->version(),
  123. $hrr_record->len(),
  124. $hrr_record->sslv2(),
  125. $hrr_record->len_real(),
  126. $hrr_record->decrypt_len(),
  127. $hrr_record->data(),
  128. $hrr_record->decrypt_data());
  129. $i++;
  130. splice @{$proxy->record_list}, $i, 0, $dup_hrr;
  131. return;
  132. }
  133. if ($proxy->flight != 0) {
  134. return;
  135. }
  136. my $ch1 = ${$proxy->message_list}[0];
  137. if ($testtype == CHANGE_CH1_CIPHERSUITE) {
  138. # The server will always pick TLS_AES_256_GCM_SHA384
  139. my @ciphersuites = (TLSProxy::Message::CIPHER_TLS13_AES_128_GCM_SHA256);
  140. $ch1->ciphersuite_len(2 * scalar @ciphersuites);
  141. $ch1->ciphersuites(\@ciphersuites);
  142. } elsif ($testtype == INVALID_GROUP) {
  143. # INVALID_GROUP
  144. my $ext = pack "C7",
  145. 0x00, 0x05, #List Length
  146. 0x00, 0x1c, #brainpoolP512r1 (not compatible with TLSv1.3)
  147. 0x00, 0x01, 0xff; #key_exchange data
  148. $ch1->set_extension(
  149. TLSProxy::Message::EXT_KEY_SHARE, $ext);
  150. }
  151. $ch1->repack();
  152. }