70-test_tls13downgrade.t 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #! /usr/bin/env perl
  2. # Copyright 2017-2021 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_tls13downgrade";
  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 and TLS1.2 enabled"
  21. if disabled("tls1_3")
  22. || (disabled("ec") && disabled("dh"))
  23. || disabled("tls1_2");
  24. my $proxy = TLSProxy::Proxy->new(
  25. undef,
  26. cmdstr(app(["openssl"]), display => 1),
  27. srctop_file("apps", "server.pem"),
  28. (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
  29. );
  30. use constant {
  31. DOWNGRADE_TO_TLS_1_2 => 0,
  32. DOWNGRADE_TO_TLS_1_1 => 1,
  33. FALLBACK_FROM_TLS_1_3 => 2,
  34. };
  35. #Test 1: Downgrade from TLSv1.3 to TLSv1.2
  36. $proxy->filter(\&downgrade_filter);
  37. my $testtype = DOWNGRADE_TO_TLS_1_2;
  38. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  39. plan tests => 6;
  40. ok(TLSProxy::Message->fail(), "Downgrade TLSv1.3 to TLSv1.2");
  41. #Test 2: Downgrade from TLSv1.3 to TLSv1.1
  42. $proxy->clear();
  43. $testtype = DOWNGRADE_TO_TLS_1_1;
  44. $proxy->start();
  45. ok(TLSProxy::Message->fail(), "Downgrade TLSv1.3 to TLSv1.1");
  46. #Test 3: Downgrade from TLSv1.2 to TLSv1.1
  47. $proxy->clear();
  48. $proxy->clientflags("-no_tls1_3");
  49. $proxy->serverflags("-no_tls1_3");
  50. $proxy->start();
  51. ok(TLSProxy::Message->fail(), "Downgrade TLSv1.2 to TLSv1.1");
  52. #Test 4: Client falls back from TLSv1.3 (server does not support the fallback
  53. # SCSV)
  54. $proxy->clear();
  55. $testtype = FALLBACK_FROM_TLS_1_3;
  56. $proxy->clientflags("-fallback_scsv -no_tls1_3");
  57. $proxy->start();
  58. my $alert = TLSProxy::Message->alert();
  59. ok(TLSProxy::Message->fail()
  60. && !$alert->server()
  61. && $alert->description() == TLSProxy::Message::AL_DESC_ILLEGAL_PARAMETER,
  62. "Fallback from TLSv1.3");
  63. SKIP: {
  64. skip "TLSv1.1 disabled", 2 if disabled("tls1_1");
  65. #Test 5: A client side protocol "hole" should not be detected as a downgrade
  66. $proxy->clear();
  67. $proxy->filter(undef);
  68. $proxy->clientflags("-no_tls1_2");
  69. $proxy->ciphers("AES128-SHA:\@SECLEVEL=0");
  70. $proxy->start();
  71. ok(TLSProxy::Message->success(), "TLSv1.2 client-side protocol hole");
  72. #Test 6: A server side protocol "hole" should not be detected as a downgrade
  73. $proxy->clear();
  74. $proxy->filter(undef);
  75. $proxy->serverflags("-no_tls1_2");
  76. $proxy->start();
  77. ok(TLSProxy::Message->success(), "TLSv1.2 server-side protocol hole");
  78. }
  79. sub downgrade_filter
  80. {
  81. my $proxy = shift;
  82. # We're only interested in the initial ClientHello
  83. if ($proxy->flight != 0) {
  84. return;
  85. }
  86. my $message = ${$proxy->message_list}[0];
  87. my $ext;
  88. if ($testtype == FALLBACK_FROM_TLS_1_3) {
  89. #The default ciphersuite we use for TLSv1.2 without any SCSV
  90. my @ciphersuites = (TLSProxy::Message::CIPHER_RSA_WITH_AES_128_CBC_SHA);
  91. $message->ciphersuite_len(2 * scalar @ciphersuites);
  92. $message->ciphersuites(\@ciphersuites);
  93. } else {
  94. if ($testtype == DOWNGRADE_TO_TLS_1_2) {
  95. $ext = pack "C3",
  96. 0x02, # Length
  97. 0x03, 0x03; #TLSv1.2
  98. } else {
  99. $ext = pack "C3",
  100. 0x02, # Length
  101. 0x03, 0x02; #TLSv1.1
  102. }
  103. $message->set_extension(TLSProxy::Message::EXT_SUPPORTED_VERSIONS, $ext);
  104. }
  105. $message->repack();
  106. }