70-test_sslextension.t 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 feature 'state';
  10. use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
  11. use OpenSSL::Test::Utils;
  12. use TLSProxy::Proxy;
  13. my $test_name = "test_sslextension";
  14. setup($test_name);
  15. plan skip_all => "TLSProxy isn't usable on $^O"
  16. if $^O =~ /^(VMS)$/;
  17. plan skip_all => "$test_name needs the dynamic engine feature enabled"
  18. if disabled("engine") || disabled("dynamic-engine");
  19. plan skip_all => "$test_name needs the sock feature enabled"
  20. if disabled("sock");
  21. plan skip_all => "$test_name needs TLS enabled"
  22. if alldisabled(available_protocols("tls"));
  23. my $no_below_tls13 = alldisabled(("tls1", "tls1_1", "tls1_2"))
  24. || (!disabled("tls1_3") && disabled("tls1_2"));
  25. use constant {
  26. UNSOLICITED_SERVER_NAME => 0,
  27. UNSOLICITED_SERVER_NAME_TLS13 => 1,
  28. UNSOLICITED_SCT => 2,
  29. NONCOMPLIANT_SUPPORTED_GROUPS => 3
  30. };
  31. my $testtype;
  32. my $fatal_alert = 0; # set by filter on fatal alert
  33. $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
  34. my $proxy = TLSProxy::Proxy->new(
  35. \&inject_duplicate_extension_clienthello,
  36. cmdstr(app(["openssl"]), display => 1),
  37. srctop_file("apps", "server.pem"),
  38. (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
  39. );
  40. sub extension_filter
  41. {
  42. my $proxy = shift;
  43. if ($proxy->flight == 1) {
  44. # Change the ServerRandom so that the downgrade sentinel doesn't cause
  45. # the connection to fail
  46. my $message = ${$proxy->message_list}[1];
  47. $message->random("\0"x32);
  48. $message->repack();
  49. return;
  50. }
  51. # We're only interested in the initial ClientHello
  52. if ($proxy->flight != 0) {
  53. return;
  54. }
  55. foreach my $message (@{$proxy->message_list}) {
  56. if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
  57. # Remove all extensions and set the extension len to zero
  58. $message->extension_data({});
  59. $message->extensions_len(0);
  60. # Extensions have been removed so make sure we don't try to use them
  61. $message->process_extensions();
  62. $message->repack();
  63. }
  64. }
  65. }
  66. sub inject_duplicate_extension
  67. {
  68. my ($proxy, $message_type) = @_;
  69. foreach my $message (@{$proxy->message_list}) {
  70. if ($message->mt == $message_type) {
  71. my %extensions = %{$message->extension_data};
  72. # Add a duplicate (unknown) extension.
  73. $message->set_extension(TLSProxy::Message::EXT_DUPLICATE_EXTENSION, "");
  74. $message->set_extension(TLSProxy::Message::EXT_DUPLICATE_EXTENSION, "");
  75. $message->repack();
  76. }
  77. }
  78. }
  79. sub inject_duplicate_extension_clienthello
  80. {
  81. my $proxy = shift;
  82. # We're only interested in the initial ClientHello
  83. if ($proxy->flight == 0) {
  84. inject_duplicate_extension($proxy, TLSProxy::Message::MT_CLIENT_HELLO);
  85. return;
  86. }
  87. my $last_record = @{$proxy->{record_list}}[-1];
  88. $fatal_alert = 1 if $last_record->is_fatal_alert(1);
  89. }
  90. sub inject_duplicate_extension_serverhello
  91. {
  92. my $proxy = shift;
  93. # We're only interested in the initial ServerHello
  94. if ($proxy->flight == 0) {
  95. return;
  96. } elsif ($proxy->flight == 1) {
  97. inject_duplicate_extension($proxy, TLSProxy::Message::MT_SERVER_HELLO);
  98. return;
  99. }
  100. my $last_record = @{$proxy->{record_list}}[-1];
  101. $fatal_alert = 1 if $last_record->is_fatal_alert(0);
  102. }
  103. sub inject_unsolicited_extension
  104. {
  105. my $proxy = shift;
  106. my $message;
  107. state $sent_unsolisited_extension;
  108. if ($proxy->flight == 0) {
  109. $sent_unsolisited_extension = 0;
  110. return;
  111. }
  112. # We're only interested in the initial ServerHello/EncryptedExtensions
  113. if ($proxy->flight != 1) {
  114. if ($sent_unsolisited_extension) {
  115. my $last_record = @{$proxy->record_list}[-1];
  116. $fatal_alert = 1 if $last_record->is_fatal_alert(0);
  117. }
  118. return;
  119. }
  120. if ($testtype == UNSOLICITED_SERVER_NAME_TLS13) {
  121. return if (!defined($message = ${$proxy->message_list}[2]));
  122. die "Expecting EE message ".($message->mt).","
  123. .${$proxy->message_list}[1]->mt.", "
  124. .${$proxy->message_list}[3]->mt
  125. if $message->mt != TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS;
  126. } else {
  127. $message = ${$proxy->message_list}[1];
  128. }
  129. my $ext = pack "C2",
  130. 0x00, 0x00; #Extension length
  131. my $type;
  132. if ($testtype == UNSOLICITED_SERVER_NAME
  133. || $testtype == UNSOLICITED_SERVER_NAME_TLS13) {
  134. $type = TLSProxy::Message::EXT_SERVER_NAME;
  135. } elsif ($testtype == UNSOLICITED_SCT) {
  136. $type = TLSProxy::Message::EXT_SCT;
  137. } elsif ($testtype == NONCOMPLIANT_SUPPORTED_GROUPS) {
  138. $type = TLSProxy::Message::EXT_SUPPORTED_GROUPS;
  139. }
  140. $message->set_extension($type, $ext);
  141. $message->repack();
  142. $sent_unsolisited_extension = 1;
  143. }
  144. # Test 1-2: Sending a duplicate extension should fail.
  145. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  146. plan tests => 7;
  147. ok($fatal_alert, "Duplicate ClientHello extension");
  148. $fatal_alert = 0;
  149. $proxy->clear();
  150. $proxy->filter(\&inject_duplicate_extension_serverhello);
  151. $proxy->start();
  152. ok($fatal_alert, "Duplicate ServerHello extension");
  153. SKIP: {
  154. skip "TLS <= 1.2 disabled", 3 if $no_below_tls13;
  155. #Test 3: Sending a zero length extension block should pass
  156. $proxy->clear();
  157. $proxy->filter(\&extension_filter);
  158. $proxy->start();
  159. ok(TLSProxy::Message->success, "Zero extension length test");
  160. #Test 4: Inject an unsolicited extension (<= TLSv1.2)
  161. $fatal_alert = 0;
  162. $proxy->clear();
  163. $proxy->filter(\&inject_unsolicited_extension);
  164. $testtype = UNSOLICITED_SERVER_NAME;
  165. $proxy->clientflags("-no_tls1_3 -noservername");
  166. $proxy->start();
  167. ok($fatal_alert, "Unsolicited server name extension");
  168. #Test 5: Inject a noncompliant supported_groups extension (<= TLSv1.2)
  169. $proxy->clear();
  170. $proxy->filter(\&inject_unsolicited_extension);
  171. $testtype = NONCOMPLIANT_SUPPORTED_GROUPS;
  172. $proxy->clientflags("-no_tls1_3");
  173. $proxy->start();
  174. ok(TLSProxy::Message->success(), "Noncompliant supported_groups extension");
  175. }
  176. SKIP: {
  177. skip "TLS <= 1.2 or CT disabled", 1
  178. if $no_below_tls13 || disabled("ct");
  179. #Test 6: Same as above for the SCT extension which has special handling
  180. $fatal_alert = 0;
  181. $proxy->clear();
  182. $testtype = UNSOLICITED_SCT;
  183. $proxy->clientflags("-no_tls1_3");
  184. $proxy->start();
  185. ok($fatal_alert, "Unsolicited sct extension");
  186. }
  187. SKIP: {
  188. skip "TLS 1.3 disabled", 1 if disabled("tls1_3");
  189. #Test 7: Inject an unsolicited extension (TLSv1.3)
  190. $fatal_alert = 0;
  191. $proxy->clear();
  192. $proxy->filter(\&inject_unsolicited_extension);
  193. $testtype = UNSOLICITED_SERVER_NAME_TLS13;
  194. $proxy->clientflags("-noservername");
  195. $proxy->start();
  196. ok($fatal_alert, "Unsolicited server name extension (TLSv1.3)");
  197. }