70-test_sslversions.t 6.3 KB

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