70-test_sslversions.t 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #! /usr/bin/env perl
  2. # Copyright 2015-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. use File::Temp qw(tempfile);
  13. use constant {
  14. REVERSE_ORDER_VERSIONS => 1,
  15. UNRECOGNISED_VERSIONS => 2,
  16. NO_EXTENSION => 3,
  17. EMPTY_EXTENSION => 4,
  18. TLS1_1_AND_1_0_ONLY => 5,
  19. WITH_TLS1_4 => 6,
  20. BAD_LEGACY_VERSION => 7
  21. };
  22. my $testtype;
  23. my $test_name = "test_sslversions";
  24. setup($test_name);
  25. plan skip_all => "TLSProxy isn't usable on $^O"
  26. if $^O =~ /^(VMS)$/;
  27. plan skip_all => "$test_name needs the dynamic engine feature enabled"
  28. if disabled("engine") || disabled("dynamic-engine");
  29. plan skip_all => "$test_name needs the sock feature enabled"
  30. if disabled("sock");
  31. plan skip_all => "$test_name needs TLS1.3, TLS1.2 and TLS1.1 enabled"
  32. if disabled("tls1_3") || disabled("tls1_2") || disabled("tls1_1");
  33. $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
  34. my $proxy = TLSProxy::Proxy->new(
  35. undef,
  36. cmdstr(app(["openssl"]), display => 1),
  37. srctop_file("apps", "server.pem"),
  38. (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
  39. );
  40. #We're just testing various negative and unusual scenarios here. ssltest with
  41. #02-protocol-version.conf should check all the various combinations of normal
  42. #version neg
  43. #Test 1: An empty supported_versions extension should not succeed
  44. $testtype = EMPTY_EXTENSION;
  45. $proxy->filter(\&modify_supported_versions_filter);
  46. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  47. plan tests => 8;
  48. ok(TLSProxy::Message->fail(), "Empty supported versions");
  49. #Test 2: supported_versions extension with no recognised versions should not
  50. #succeed
  51. $proxy->clear();
  52. $testtype = UNRECOGNISED_VERSIONS;
  53. $proxy->start();
  54. ok(TLSProxy::Message->fail(), "No recognised versions");
  55. #Test 3: No supported versions extensions should succeed and select TLSv1.2
  56. $proxy->clear();
  57. $testtype = NO_EXTENSION;
  58. $proxy->start();
  59. my $record = pop @{$proxy->record_list};
  60. ok(TLSProxy::Message->success()
  61. && $record->version() == TLSProxy::Record::VERS_TLS_1_2,
  62. "No supported versions extension");
  63. #Test 4: No supported versions extensions should fail if only TLS1.3 available
  64. $proxy->clear();
  65. $proxy->serverflags("-tls1_3");
  66. $proxy->start();
  67. ok(TLSProxy::Message->fail(), "No supported versions extension (only TLS1.3)");
  68. #Test 5: supported versions extension with best version last should succeed
  69. #and select TLSv1.3
  70. $proxy->clear();
  71. $testtype = REVERSE_ORDER_VERSIONS;
  72. $proxy->start();
  73. $record = pop @{$proxy->record_list};
  74. ok(TLSProxy::Message->success()
  75. && $record->version() == TLSProxy::Record::VERS_TLS_1_2
  76. && TLSProxy::Proxy->is_tls13(),
  77. "Reverse order versions");
  78. #Test 6: no TLSv1.3 or TLSv1.2 version in supported versions extension, but
  79. #TLSv1.1 and TLSv1.0 are present. Should just use TLSv1.1 and succeed
  80. $proxy->clear();
  81. $testtype = TLS1_1_AND_1_0_ONLY;
  82. $proxy->start();
  83. $record = pop @{$proxy->record_list};
  84. ok(TLSProxy::Message->success()
  85. && $record->version() == TLSProxy::Record::VERS_TLS_1_1,
  86. "TLS1.1 and TLS1.0 in supported versions extension only");
  87. #Test 7: TLS1.4 and TLS1.3 in supported versions. Should succeed and use TLS1.3
  88. $proxy->clear();
  89. $testtype = WITH_TLS1_4;
  90. $proxy->start();
  91. $record = pop @{$proxy->record_list};
  92. ok(TLSProxy::Message->success()
  93. && $record->version() == TLSProxy::Record::VERS_TLS_1_2
  94. && TLSProxy::Proxy->is_tls13(),
  95. "TLS1.4 in supported versions extension");
  96. #Test 8: Set the legacy version to SSLv3 with supported versions. Should fail
  97. $proxy->clear();
  98. $testtype = BAD_LEGACY_VERSION;
  99. $proxy->start();
  100. ok(TLSProxy::Message->fail(), "Legacy version is SSLv3 with supported versions");
  101. sub modify_supported_versions_filter
  102. {
  103. my $proxy = shift;
  104. if ($proxy->flight == 1) {
  105. # Change the ServerRandom so that the downgrade sentinel doesn't cause
  106. # the connection to fail
  107. my $message = ${$proxy->message_list}[1];
  108. return if (!defined $message);
  109. $message->random("\0"x32);
  110. $message->repack();
  111. return;
  112. }
  113. # We're only interested in the initial ClientHello
  114. if ($proxy->flight != 0) {
  115. return;
  116. }
  117. foreach my $message (@{$proxy->message_list}) {
  118. if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
  119. my $ext;
  120. if ($testtype == REVERSE_ORDER_VERSIONS) {
  121. $ext = pack "C5",
  122. 0x04, # Length
  123. 0x03, 0x03, #TLSv1.2
  124. 0x03, 0x04; #TLSv1.3
  125. } elsif ($testtype == UNRECOGNISED_VERSIONS) {
  126. $ext = pack "C5",
  127. 0x04, # Length
  128. 0x04, 0x04, #Some unrecognised version
  129. 0x04, 0x03; #Another unrecognised version
  130. } elsif ($testtype == TLS1_1_AND_1_0_ONLY) {
  131. $ext = pack "C5",
  132. 0x04, # Length
  133. 0x03, 0x02, #TLSv1.1
  134. 0x03, 0x01; #TLSv1.0
  135. } elsif ($testtype == WITH_TLS1_4) {
  136. $ext = pack "C5",
  137. 0x04, # Length
  138. 0x03, 0x05, #TLSv1.4
  139. 0x03, 0x04; #TLSv1.3
  140. }
  141. if ($testtype == REVERSE_ORDER_VERSIONS
  142. || $testtype == UNRECOGNISED_VERSIONS
  143. || $testtype == TLS1_1_AND_1_0_ONLY
  144. || $testtype == WITH_TLS1_4) {
  145. $message->set_extension(
  146. TLSProxy::Message::EXT_SUPPORTED_VERSIONS, $ext);
  147. } elsif ($testtype == EMPTY_EXTENSION) {
  148. $message->set_extension(
  149. TLSProxy::Message::EXT_SUPPORTED_VERSIONS, "");
  150. } elsif ($testtype == NO_EXTENSION) {
  151. $message->delete_extension(
  152. TLSProxy::Message::EXT_SUPPORTED_VERSIONS);
  153. } else {
  154. # BAD_LEGACY_VERSION
  155. $message->client_version(TLSProxy::Record::VERS_SSL_3_0);
  156. }
  157. $message->repack();
  158. }
  159. }
  160. }