2
0

70-test_sslvertol.t 4.6 KB

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