70-test_sslvertol.t 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #! /usr/bin/env perl
  2. # Copyright 2015-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_sslvertol";
  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 TLS enabled"
  21. if alldisabled(available_protocols("tls"));
  22. my $proxy = TLSProxy::Proxy->new(
  23. \&vers_tolerance_filter,
  24. cmdstr(app(["openssl"]), display => 1),
  25. srctop_file("apps", "server.pem"),
  26. (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
  27. );
  28. my @available_tls_versions = ();
  29. foreach (available_protocols("tls")) {
  30. unless (disabled($_)) {
  31. note("Checking enabled protocol $_");
  32. m|^([a-z]+)(\d)(_\d)?|;
  33. my $versionname;
  34. if (defined $3) {
  35. $versionname = 'TLSProxy::Record::VERS_'.uc($1).'_'.$2.$3;
  36. note("'$1', '$2', '$3' => $versionname");
  37. } else {
  38. $versionname = 'TLSProxy::Record::VERS_'.uc($1).'_'.$2.'_0';
  39. note("'$1', '$2' => $versionname");
  40. }
  41. push @available_tls_versions, eval $versionname;
  42. }
  43. }
  44. note("TLS versions we can expect: ", join(", ", @available_tls_versions));
  45. #This file does tests without the supported_versions extension.
  46. #See 70-test_sslversions.t for tests with supported versions.
  47. #Test 1: Asking for TLS1.4 should pass and negotiate the maximum
  48. #available TLS version according to configuration below TLS1.3
  49. my $client_version = TLSProxy::Record::VERS_TLS_1_4;
  50. my $previous_version = tls_version_below(TLSProxy::Record::VERS_TLS_1_3);
  51. $proxy->clientflags("-no_tls1_3");
  52. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  53. plan tests => 3;
  54. SKIP: {
  55. skip "There are too few protocols enabled for test 1", 1
  56. unless defined $previous_version;
  57. my $record = pop @{$proxy->record_list};
  58. ok((note("Record version received: ".$record->version()),
  59. TLSProxy::Message->success())
  60. && $record->version() == $previous_version,
  61. "Version tolerance test, below TLS 1.4 and not TLS 1.3");
  62. }
  63. #Test 2: Asking for TLS1.3 with that disabled should succeed and negotiate
  64. #the highest configured TLS version below that.
  65. $client_version = TLSProxy::Record::VERS_TLS_1_3;
  66. $previous_version = tls_version_below($client_version);
  67. SKIP: {
  68. skip "There are too few protocols enabled for test 2", 1
  69. unless defined $previous_version;
  70. $proxy->clear();
  71. $proxy->clientflags("-no_tls1_3");
  72. $proxy->start();
  73. my $record = pop @{$proxy->record_list};
  74. ok((note("Record version received: ".$record->version()),
  75. TLSProxy::Message->success())
  76. && $record->version() == $previous_version,
  77. "Version tolerance test, max version but not TLS 1.3");
  78. }
  79. #Test 3: Testing something below SSLv3 should fail. We must disable TLS 1.3
  80. #to avoid having the 'supported_versions' extension kick in and override our
  81. #desires.
  82. $client_version = TLSProxy::Record::VERS_SSL_3_0 - 1;
  83. $proxy->clear();
  84. $proxy->clientflags("-no_tls1_3");
  85. $proxy->start();
  86. my $record = pop @{$proxy->record_list};
  87. ok((note("Record version received: ".
  88. (defined $record ? $record->version() : "none")),
  89. TLSProxy::Message->fail()),
  90. "Version tolerance test, SSL < 3.0");
  91. sub vers_tolerance_filter
  92. {
  93. my $proxy = shift;
  94. # We're only interested in the initial ClientHello
  95. if ($proxy->flight != 0) {
  96. return;
  97. }
  98. foreach my $message (@{$proxy->message_list}) {
  99. if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
  100. #Set the client version
  101. #Anything above the max supported version should succeed
  102. #Anything below SSLv3 should fail
  103. $message->client_version($client_version);
  104. $message->repack();
  105. }
  106. }
  107. }
  108. sub tls_version_below {
  109. if (@_) {
  110. my $term = shift;
  111. my $res = undef;
  112. foreach (@available_tls_versions) {
  113. $res = $_ if $_ < $term;
  114. }
  115. return $res;
  116. }
  117. return $available_tls_versions[-1];
  118. }