70-test_tls13hrr.t 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #! /usr/bin/env perl
  2. # Copyright 2017-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. use strict;
  9. use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
  10. use OpenSSL::Test::Utils;
  11. use TLSProxy::Proxy;
  12. my $test_name = "test_tls13hrr";
  13. setup($test_name);
  14. plan skip_all => "TLSProxy isn't usable on $^O"
  15. if $^O =~ /^(VMS)$/;
  16. plan skip_all => "$test_name needs the dynamic engine feature enabled"
  17. if disabled("engine") || disabled("dynamic-engine");
  18. plan skip_all => "$test_name needs the sock feature enabled"
  19. if disabled("sock");
  20. plan skip_all => "$test_name needs TLS1.3 enabled"
  21. if disabled("tls1_3") || (disabled("ec") && disabled("dh"));
  22. my $proxy = TLSProxy::Proxy->new(
  23. undef,
  24. cmdstr(app(["openssl"]), display => 1),
  25. srctop_file("apps", "server.pem"),
  26. (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
  27. );
  28. use constant {
  29. CHANGE_HRR_CIPHERSUITE => 0,
  30. CHANGE_CH1_CIPHERSUITE => 1,
  31. DUPLICATE_HRR => 2
  32. };
  33. #Test 1: A client should fail if the server changes the ciphersuite between the
  34. # HRR and the SH
  35. $proxy->filter(\&hrr_filter);
  36. if (disabled("ec")) {
  37. $proxy->serverflags("-curves ffdhe3072");
  38. } else {
  39. $proxy->serverflags("-curves P-256");
  40. }
  41. my $testtype = CHANGE_HRR_CIPHERSUITE;
  42. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  43. plan tests => 3;
  44. ok(TLSProxy::Message->fail(), "Server ciphersuite changes");
  45. #Test 2: It is an error if the client changes the offered ciphersuites so that
  46. # we end up selecting a different ciphersuite between HRR and the SH
  47. $proxy->clear();
  48. if (disabled("ec")) {
  49. $proxy->serverflags("-curves ffdhe3072");
  50. } else {
  51. $proxy->serverflags("-curves P-256");
  52. }
  53. $proxy->ciphersuitess("TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384");
  54. $testtype = CHANGE_CH1_CIPHERSUITE;
  55. $proxy->start();
  56. ok(TLSProxy::Message->fail(), "Client ciphersuite changes");
  57. #Test 3: A client should fail with unexpected_message alert if the server
  58. # sends more than 1 HRR
  59. my $fatal_alert = 0;
  60. $proxy->clear();
  61. if (disabled("ec")) {
  62. $proxy->serverflags("-curves ffdhe3072");
  63. } else {
  64. $proxy->serverflags("-curves P-256");
  65. }
  66. $testtype = DUPLICATE_HRR;
  67. $proxy->start();
  68. ok($fatal_alert, "Server duplicated HRR");
  69. sub hrr_filter
  70. {
  71. my $proxy = shift;
  72. if ($testtype == CHANGE_HRR_CIPHERSUITE) {
  73. # We're only interested in the HRR
  74. if ($proxy->flight != 1) {
  75. return;
  76. }
  77. my $hrr = ${$proxy->message_list}[1];
  78. # We will normally only ever select CIPHER_TLS13_AES_128_GCM_SHA256
  79. # because that's what Proxy tells s_server to do. Setting as below means
  80. # the ciphersuite will change will we get the ServerHello
  81. $hrr->ciphersuite(TLSProxy::Message::CIPHER_TLS13_AES_256_GCM_SHA384);
  82. $hrr->repack();
  83. return;
  84. }
  85. if ($testtype == DUPLICATE_HRR) {
  86. # We're only interested in the HRR
  87. # and the unexpected_message alert from client
  88. if ($proxy->flight == 4) {
  89. $fatal_alert = 1
  90. if @{$proxy->record_list}[-1]->is_fatal_alert(0) == 10;
  91. return;
  92. }
  93. if ($proxy->flight != 3) {
  94. return;
  95. }
  96. # Find ServerHello record (HRR actually) and insert after that
  97. my $i;
  98. for ($i = 0; ${$proxy->record_list}[$i]->flight() < 1; $i++) {
  99. next;
  100. }
  101. my $hrr_record = ${$proxy->record_list}[$i];
  102. my $dup_hrr = TLSProxy::Record->new(3,
  103. $hrr_record->content_type(),
  104. $hrr_record->version(),
  105. $hrr_record->len(),
  106. $hrr_record->sslv2(),
  107. $hrr_record->len_real(),
  108. $hrr_record->decrypt_len(),
  109. $hrr_record->data(),
  110. $hrr_record->decrypt_data());
  111. $i++;
  112. splice @{$proxy->record_list}, $i, 0, $dup_hrr;
  113. return;
  114. }
  115. # CHANGE_CH1_CIPHERSUITE
  116. if ($proxy->flight != 0) {
  117. return;
  118. }
  119. my $ch1 = ${$proxy->message_list}[0];
  120. # The server will always pick TLS_AES_256_GCM_SHA384
  121. my @ciphersuites = (TLSProxy::Message::CIPHER_TLS13_AES_128_GCM_SHA256);
  122. $ch1->ciphersuite_len(2 * scalar @ciphersuites);
  123. $ch1->ciphersuites(\@ciphersuites);
  124. $ch1->repack();
  125. }