70-test_sslextension.t 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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->cipherc("DEFAULT:\@SECLEVEL=0");
  172. $proxy->ciphers("AES128-SHA:\@SECLEVEL=0");
  173. $proxy->clientflags("-no_tls1_3");
  174. $proxy->start();
  175. ok(TLSProxy::Message->success, "Zero extension length test");
  176. #Test 4: Inject an unsolicited extension (<= TLSv1.2)
  177. $fatal_alert = 0;
  178. $proxy->clear();
  179. $proxy->filter(\&inject_unsolicited_extension);
  180. $testtype = UNSOLICITED_SERVER_NAME;
  181. $proxy->clientflags("-no_tls1_3 -noservername");
  182. $proxy->start();
  183. ok($fatal_alert, "Unsolicited server name extension");
  184. #Test 5: Send the cryptopro extension in a ClientHello. Normally this is an
  185. # unsolicited extension only ever seen in the ServerHello. We should
  186. # ignore it in a ClientHello
  187. $proxy->clear();
  188. $proxy->filter(\&inject_cryptopro_extension);
  189. $proxy->clientflags("-no_tls1_3");
  190. $proxy->start();
  191. ok(TLSProxy::Message->success(), "Cryptopro extension in ClientHello");
  192. }
  193. SKIP: {
  194. skip "TLS <= 1.2 disabled or EC disabled", 1
  195. if $no_below_tls13 || disabled("ec");
  196. #Test 6: Inject a noncompliant supported_groups extension (<= TLSv1.2)
  197. $proxy->clear();
  198. $proxy->filter(\&inject_unsolicited_extension);
  199. $testtype = NONCOMPLIANT_SUPPORTED_GROUPS;
  200. $proxy->clientflags("-no_tls1_3");
  201. $proxy->start();
  202. ok(TLSProxy::Message->success(), "Noncompliant supported_groups extension");
  203. }
  204. SKIP: {
  205. skip "TLS <= 1.2 or CT disabled", 1
  206. if $no_below_tls13 || disabled("ct");
  207. #Test 7: Same as above for the SCT extension which has special handling
  208. $fatal_alert = 0;
  209. $proxy->clear();
  210. $proxy->filter(\&inject_unsolicited_extension);
  211. $testtype = UNSOLICITED_SCT;
  212. $proxy->clientflags("-no_tls1_3");
  213. $proxy->start();
  214. ok($fatal_alert, "Unsolicited sct extension");
  215. }
  216. SKIP: {
  217. skip "TLS 1.3 disabled", 1
  218. if disabled("tls1_3") || (disabled("ec") && disabled("dh"));
  219. #Test 8: Inject an unsolicited extension (TLSv1.3)
  220. $fatal_alert = 0;
  221. $proxy->clear();
  222. $proxy->filter(\&inject_unsolicited_extension);
  223. $testtype = UNSOLICITED_SERVER_NAME_TLS13;
  224. $proxy->clientflags("-noservername");
  225. $proxy->start();
  226. ok($fatal_alert, "Unsolicited server name extension (TLSv1.3)");
  227. }