2
0

70-test_tls13downgrade.t 3.8 KB

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