70-test_sslversions.t 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2020 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.cnf 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. $proxy->clientflags("-cipher DEFAULT:\@SECLEVEL=0");
  82. $proxy->ciphers("AES128-SHA:\@SECLEVEL=0");
  83. $testtype = TLS1_1_AND_1_0_ONLY;
  84. $proxy->start();
  85. $record = pop @{$proxy->record_list};
  86. ok(TLSProxy::Message->success()
  87. && $record->version() == TLSProxy::Record::VERS_TLS_1_1,
  88. "TLS1.1 and TLS1.0 in supported versions extension only");
  89. #Test 7: TLS1.4 and TLS1.3 in supported versions. Should succeed and use TLS1.3
  90. $proxy->clear();
  91. $testtype = WITH_TLS1_4;
  92. $proxy->start();
  93. $record = pop @{$proxy->record_list};
  94. ok(TLSProxy::Message->success()
  95. && $record->version() == TLSProxy::Record::VERS_TLS_1_2
  96. && TLSProxy::Proxy->is_tls13(),
  97. "TLS1.4 in supported versions extension");
  98. #Test 8: Set the legacy version to SSLv3 with supported versions. Should fail
  99. $proxy->clear();
  100. $testtype = BAD_LEGACY_VERSION;
  101. $proxy->start();
  102. ok(TLSProxy::Message->fail(), "Legacy version is SSLv3 with supported versions");
  103. sub modify_supported_versions_filter
  104. {
  105. my $proxy = shift;
  106. if ($proxy->flight == 1) {
  107. # Change the ServerRandom so that the downgrade sentinel doesn't cause
  108. # the connection to fail
  109. my $message = ${$proxy->message_list}[1];
  110. return if (!defined $message);
  111. $message->random("\0"x32);
  112. $message->repack();
  113. return;
  114. }
  115. # We're only interested in the initial ClientHello
  116. if ($proxy->flight != 0) {
  117. return;
  118. }
  119. foreach my $message (@{$proxy->message_list}) {
  120. if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
  121. my $ext;
  122. if ($testtype == REVERSE_ORDER_VERSIONS) {
  123. $ext = pack "C5",
  124. 0x04, # Length
  125. 0x03, 0x03, #TLSv1.2
  126. 0x03, 0x04; #TLSv1.3
  127. } elsif ($testtype == UNRECOGNISED_VERSIONS) {
  128. $ext = pack "C5",
  129. 0x04, # Length
  130. 0x04, 0x04, #Some unrecognised version
  131. 0x04, 0x03; #Another unrecognised version
  132. } elsif ($testtype == TLS1_1_AND_1_0_ONLY) {
  133. $ext = pack "C5",
  134. 0x04, # Length
  135. 0x03, 0x02, #TLSv1.1
  136. 0x03, 0x01; #TLSv1.0
  137. } elsif ($testtype == WITH_TLS1_4) {
  138. $ext = pack "C5",
  139. 0x04, # Length
  140. 0x03, 0x05, #TLSv1.4
  141. 0x03, 0x04; #TLSv1.3
  142. }
  143. if ($testtype == REVERSE_ORDER_VERSIONS
  144. || $testtype == UNRECOGNISED_VERSIONS
  145. || $testtype == TLS1_1_AND_1_0_ONLY
  146. || $testtype == WITH_TLS1_4) {
  147. $message->set_extension(
  148. TLSProxy::Message::EXT_SUPPORTED_VERSIONS, $ext);
  149. } elsif ($testtype == EMPTY_EXTENSION) {
  150. $message->set_extension(
  151. TLSProxy::Message::EXT_SUPPORTED_VERSIONS, "");
  152. } elsif ($testtype == NO_EXTENSION) {
  153. $message->delete_extension(
  154. TLSProxy::Message::EXT_SUPPORTED_VERSIONS);
  155. } else {
  156. # BAD_LEGACY_VERSION
  157. $message->client_version(TLSProxy::Record::VERS_SSL_3_0);
  158. }
  159. $message->repack();
  160. }
  161. }
  162. }