70-test_sslversions.t 5.8 KB

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