70-test_tls13hrr.t 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #! /usr/bin/env perl
  2. # Copyright 2017-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (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");
  22. $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
  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. };
  34. #Test 1: A client should fail if the server changes the ciphersuite between the
  35. # HRR and the SH
  36. $proxy->filter(\&hrr_filter);
  37. $proxy->serverflags("-curves P-256");
  38. my $testtype = CHANGE_HRR_CIPHERSUITE;
  39. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  40. plan tests => 3;
  41. ok(TLSProxy::Message->fail(), "Server ciphersuite changes");
  42. #Test 2: It is an error if the client changes the offered ciphersuites so that
  43. # we end up selecting a different ciphersuite between HRR and the SH
  44. $proxy->clear();
  45. $proxy->serverflags("-curves P-256");
  46. $proxy->ciphersuitess("TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384");
  47. $testtype = CHANGE_CH1_CIPHERSUITE;
  48. $proxy->start();
  49. ok(TLSProxy::Message->fail(), "Client ciphersuite changes");
  50. #Test 3: A client should fail with unexpected_message alert if the server
  51. # sends more than 1 HRR
  52. my $fatal_alert = 0;
  53. $proxy->clear();
  54. if (disabled("ec")) {
  55. $proxy->serverflags("-curves ffdhe3072");
  56. } else {
  57. $proxy->serverflags("-curves P-256");
  58. }
  59. $testtype = DUPLICATE_HRR;
  60. $proxy->start();
  61. ok($fatal_alert, "Server duplicated HRR");
  62. sub hrr_filter
  63. {
  64. my $proxy = shift;
  65. if ($testtype == CHANGE_HRR_CIPHERSUITE) {
  66. # We're only interested in the HRR
  67. if ($proxy->flight != 1) {
  68. return;
  69. }
  70. my $hrr = ${$proxy->message_list}[1];
  71. # We will normally only ever select CIPHER_TLS13_AES_128_GCM_SHA256
  72. # because that's what Proxy tells s_server to do. Setting as below means
  73. # the ciphersuite will change will we get the ServerHello
  74. $hrr->ciphersuite(TLSProxy::Message::CIPHER_TLS13_AES_256_GCM_SHA384);
  75. $hrr->repack();
  76. return;
  77. }
  78. if ($testtype == DUPLICATE_HRR) {
  79. # We're only interested in the HRR
  80. # and the unexpected_message alert from client
  81. if ($proxy->flight == 4) {
  82. $fatal_alert = 1
  83. if @{$proxy->record_list}[-1]->is_fatal_alert(0) == 10;
  84. return;
  85. }
  86. if ($proxy->flight != 3) {
  87. return;
  88. }
  89. # Find ServerHello record (HRR actually) and insert after that
  90. my $i;
  91. for ($i = 0; ${$proxy->record_list}[$i]->flight() < 1; $i++) {
  92. next;
  93. }
  94. my $hrr_record = ${$proxy->record_list}[$i];
  95. my $dup_hrr = TLSProxy::Record->new(3,
  96. $hrr_record->content_type(),
  97. $hrr_record->version(),
  98. $hrr_record->len(),
  99. $hrr_record->sslv2(),
  100. $hrr_record->len_real(),
  101. $hrr_record->decrypt_len(),
  102. $hrr_record->data(),
  103. $hrr_record->decrypt_data());
  104. $i++;
  105. splice @{$proxy->record_list}, $i, 0, $dup_hrr;
  106. return;
  107. }
  108. # CHANGE_CH1_CIPHERSUITE
  109. if ($proxy->flight != 0) {
  110. return;
  111. }
  112. my $ch1 = ${$proxy->message_list}[0];
  113. # The server will always pick TLS_AES_256_GCM_SHA384
  114. my @ciphersuites = (TLSProxy::Message::CIPHER_TLS13_AES_128_GCM_SHA256);
  115. $ch1->ciphersuite_len(2 * scalar @ciphersuites);
  116. $ch1->ciphersuites(\@ciphersuites);
  117. $ch1->repack();
  118. }