70-test_sslextension.t 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 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. my $proxy = TLSProxy::Proxy->new(
  34. \&inject_duplicate_extension_clienthello,
  35. cmdstr(app(["openssl"]), display => 1),
  36. srctop_file("apps", "server.pem"),
  37. (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
  38. );
  39. sub extension_filter
  40. {
  41. my $proxy = shift;
  42. if ($proxy->flight == 1) {
  43. # Change the ServerRandom so that the downgrade sentinel doesn't cause
  44. # the connection to fail
  45. my $message = ${$proxy->message_list}[1];
  46. $message->random("\0"x32);
  47. $message->repack();
  48. return;
  49. }
  50. # We're only interested in the initial ClientHello
  51. if ($proxy->flight != 0) {
  52. return;
  53. }
  54. foreach my $message (@{$proxy->message_list}) {
  55. if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
  56. # Remove all extensions and set the extension len to zero
  57. $message->extension_data({});
  58. $message->extensions_len(0);
  59. # Extensions have been removed so make sure we don't try to use them
  60. $message->process_extensions();
  61. $message->repack();
  62. }
  63. }
  64. }
  65. sub inject_duplicate_extension
  66. {
  67. my ($proxy, $message_type) = @_;
  68. foreach my $message (@{$proxy->message_list}) {
  69. if ($message->mt == $message_type) {
  70. my %extensions = %{$message->extension_data};
  71. # Add a duplicate extension. We use cryptopro_bug since we never
  72. # normally write that one, and it is allowed as unsolicited in the
  73. # ServerHello
  74. $message->set_extension(TLSProxy::Message::EXT_CRYPTOPRO_BUG_EXTENSION, "");
  75. $message->dupext(TLSProxy::Message::EXT_CRYPTOPRO_BUG_EXTENSION);
  76. $message->repack();
  77. }
  78. }
  79. }
  80. sub inject_duplicate_extension_clienthello
  81. {
  82. my $proxy = shift;
  83. # We're only interested in the initial ClientHello
  84. if ($proxy->flight == 0) {
  85. inject_duplicate_extension($proxy, TLSProxy::Message::MT_CLIENT_HELLO);
  86. return;
  87. }
  88. my $last_record = @{$proxy->{record_list}}[-1];
  89. $fatal_alert = 1 if $last_record->is_fatal_alert(1);
  90. }
  91. sub inject_duplicate_extension_serverhello
  92. {
  93. my $proxy = shift;
  94. # We're only interested in the initial ServerHello
  95. if ($proxy->flight == 0) {
  96. return;
  97. } elsif ($proxy->flight == 1) {
  98. inject_duplicate_extension($proxy, TLSProxy::Message::MT_SERVER_HELLO);
  99. return;
  100. }
  101. my $last_record = @{$proxy->{record_list}}[-1];
  102. $fatal_alert = 1 if $last_record->is_fatal_alert(0);
  103. }
  104. sub inject_unsolicited_extension
  105. {
  106. my $proxy = shift;
  107. my $message;
  108. state $sent_unsolisited_extension;
  109. if ($proxy->flight == 0) {
  110. $sent_unsolisited_extension = 0;
  111. return;
  112. }
  113. # We're only interested in the initial ServerHello/EncryptedExtensions
  114. if ($proxy->flight != 1) {
  115. if ($sent_unsolisited_extension) {
  116. my $last_record = @{$proxy->record_list}[-1];
  117. $fatal_alert = 1 if $last_record->is_fatal_alert(0);
  118. }
  119. return;
  120. }
  121. if ($testtype == UNSOLICITED_SERVER_NAME_TLS13) {
  122. return if (!defined($message = ${$proxy->message_list}[2]));
  123. die "Expecting EE message ".($message->mt).","
  124. .${$proxy->message_list}[1]->mt.", "
  125. .${$proxy->message_list}[3]->mt
  126. if $message->mt != TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS;
  127. } else {
  128. $message = ${$proxy->message_list}[1];
  129. }
  130. my $ext = pack "C2",
  131. 0x00, 0x00; #Extension length
  132. my $type;
  133. if ($testtype == UNSOLICITED_SERVER_NAME
  134. || $testtype == UNSOLICITED_SERVER_NAME_TLS13) {
  135. $type = TLSProxy::Message::EXT_SERVER_NAME;
  136. } elsif ($testtype == UNSOLICITED_SCT) {
  137. $type = TLSProxy::Message::EXT_SCT;
  138. } elsif ($testtype == NONCOMPLIANT_SUPPORTED_GROUPS) {
  139. $type = TLSProxy::Message::EXT_SUPPORTED_GROUPS;
  140. }
  141. $message->set_extension($type, $ext);
  142. $message->repack();
  143. $sent_unsolisited_extension = 1;
  144. }
  145. sub inject_cryptopro_extension
  146. {
  147. my $proxy = shift;
  148. # We're only interested in the initial ClientHello
  149. if ($proxy->flight != 0) {
  150. return;
  151. }
  152. my $message = ${$proxy->message_list}[0];
  153. $message->set_extension(TLSProxy::Message::EXT_CRYPTOPRO_BUG_EXTENSION, "");
  154. $message->repack();
  155. }
  156. # Test 1-2: Sending a duplicate extension should fail.
  157. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  158. plan tests => 8;
  159. ok($fatal_alert, "Duplicate ClientHello extension");
  160. SKIP: {
  161. skip "TLS <= 1.2 disabled", 4 if $no_below_tls13;
  162. $fatal_alert = 0;
  163. $proxy->clear();
  164. $proxy->filter(\&inject_duplicate_extension_serverhello);
  165. $proxy->clientflags("-no_tls1_3");
  166. $proxy->start();
  167. ok($fatal_alert, "Duplicate ServerHello extension");
  168. #Test 3: Sending a zero length extension block should pass
  169. $proxy->clear();
  170. $proxy->filter(\&extension_filter);
  171. $proxy->ciphers("AES128-SHA:\@SECLEVEL=0");
  172. $proxy->clientflags("-no_tls1_3");
  173. $proxy->start();
  174. ok(TLSProxy::Message->success, "Zero extension length test");
  175. #Test 4: Inject an unsolicited extension (<= TLSv1.2)
  176. $fatal_alert = 0;
  177. $proxy->clear();
  178. $proxy->filter(\&inject_unsolicited_extension);
  179. $testtype = UNSOLICITED_SERVER_NAME;
  180. $proxy->clientflags("-no_tls1_3 -noservername");
  181. $proxy->start();
  182. ok($fatal_alert, "Unsolicited server name extension");
  183. #Test 5: Send the cryptopro extension in a ClientHello. Normally this is an
  184. # unsolicited extension only ever seen in the ServerHello. We should
  185. # ignore it in a ClientHello
  186. $proxy->clear();
  187. $proxy->filter(\&inject_cryptopro_extension);
  188. $proxy->clientflags("-no_tls1_3");
  189. $proxy->start();
  190. ok(TLSProxy::Message->success(), "Cryptopro extension in ClientHello");
  191. }
  192. SKIP: {
  193. skip "TLS <= 1.2 disabled or EC disabled", 1
  194. if $no_below_tls13 || disabled("ec");
  195. #Test 6: Inject a noncompliant supported_groups extension (<= TLSv1.2)
  196. $proxy->clear();
  197. $proxy->filter(\&inject_unsolicited_extension);
  198. $testtype = NONCOMPLIANT_SUPPORTED_GROUPS;
  199. $proxy->clientflags("-no_tls1_3");
  200. $proxy->start();
  201. ok(TLSProxy::Message->success(), "Noncompliant supported_groups extension");
  202. }
  203. SKIP: {
  204. skip "TLS <= 1.2 or CT disabled", 1
  205. if $no_below_tls13 || disabled("ct");
  206. #Test 7: Same as above for the SCT extension which has special handling
  207. $fatal_alert = 0;
  208. $proxy->clear();
  209. $proxy->filter(\&inject_unsolicited_extension);
  210. $testtype = UNSOLICITED_SCT;
  211. $proxy->clientflags("-no_tls1_3");
  212. $proxy->start();
  213. ok($fatal_alert, "Unsolicited sct extension");
  214. }
  215. SKIP: {
  216. skip "TLS 1.3 disabled", 1
  217. if disabled("tls1_3") || (disabled("ec") && disabled("dh"));
  218. #Test 8: Inject an unsolicited extension (TLSv1.3)
  219. $fatal_alert = 0;
  220. $proxy->clear();
  221. $proxy->filter(\&inject_unsolicited_extension);
  222. $testtype = UNSOLICITED_SERVER_NAME_TLS13;
  223. $proxy->clientflags("-noservername");
  224. $proxy->start();
  225. ok($fatal_alert, "Unsolicited server name extension (TLSv1.3)");
  226. }