70-test_sslextension.t 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2018 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. $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 extension. We use cryptopro_bug since we never
  73. # normally write that one, and it is allowed as unsolicited in the
  74. # ServerHello
  75. $message->set_extension(TLSProxy::Message::EXT_CRYPTOPRO_BUG_EXTENSION, "");
  76. $message->dupext(TLSProxy::Message::EXT_CRYPTOPRO_BUG_EXTENSION);
  77. $message->repack();
  78. }
  79. }
  80. }
  81. sub inject_duplicate_extension_clienthello
  82. {
  83. my $proxy = shift;
  84. # We're only interested in the initial ClientHello
  85. if ($proxy->flight == 0) {
  86. inject_duplicate_extension($proxy, TLSProxy::Message::MT_CLIENT_HELLO);
  87. return;
  88. }
  89. my $last_record = @{$proxy->{record_list}}[-1];
  90. $fatal_alert = 1 if $last_record->is_fatal_alert(1);
  91. }
  92. sub inject_duplicate_extension_serverhello
  93. {
  94. my $proxy = shift;
  95. # We're only interested in the initial ServerHello
  96. if ($proxy->flight == 0) {
  97. return;
  98. } elsif ($proxy->flight == 1) {
  99. inject_duplicate_extension($proxy, TLSProxy::Message::MT_SERVER_HELLO);
  100. return;
  101. }
  102. my $last_record = @{$proxy->{record_list}}[-1];
  103. $fatal_alert = 1 if $last_record->is_fatal_alert(0);
  104. }
  105. sub inject_unsolicited_extension
  106. {
  107. my $proxy = shift;
  108. my $message;
  109. state $sent_unsolisited_extension;
  110. if ($proxy->flight == 0) {
  111. $sent_unsolisited_extension = 0;
  112. return;
  113. }
  114. # We're only interested in the initial ServerHello/EncryptedExtensions
  115. if ($proxy->flight != 1) {
  116. if ($sent_unsolisited_extension) {
  117. my $last_record = @{$proxy->record_list}[-1];
  118. $fatal_alert = 1 if $last_record->is_fatal_alert(0);
  119. }
  120. return;
  121. }
  122. if ($testtype == UNSOLICITED_SERVER_NAME_TLS13) {
  123. return if (!defined($message = ${$proxy->message_list}[2]));
  124. die "Expecting EE message ".($message->mt).","
  125. .${$proxy->message_list}[1]->mt.", "
  126. .${$proxy->message_list}[3]->mt
  127. if $message->mt != TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS;
  128. } else {
  129. $message = ${$proxy->message_list}[1];
  130. }
  131. my $ext = pack "C2",
  132. 0x00, 0x00; #Extension length
  133. my $type;
  134. if ($testtype == UNSOLICITED_SERVER_NAME
  135. || $testtype == UNSOLICITED_SERVER_NAME_TLS13) {
  136. $type = TLSProxy::Message::EXT_SERVER_NAME;
  137. } elsif ($testtype == UNSOLICITED_SCT) {
  138. $type = TLSProxy::Message::EXT_SCT;
  139. } elsif ($testtype == NONCOMPLIANT_SUPPORTED_GROUPS) {
  140. $type = TLSProxy::Message::EXT_SUPPORTED_GROUPS;
  141. }
  142. $message->set_extension($type, $ext);
  143. $message->repack();
  144. $sent_unsolisited_extension = 1;
  145. }
  146. sub inject_cryptopro_extension
  147. {
  148. my $proxy = shift;
  149. # We're only interested in the initial ClientHello
  150. if ($proxy->flight != 0) {
  151. return;
  152. }
  153. my $message = ${$proxy->message_list}[0];
  154. $message->set_extension(TLSProxy::Message::EXT_CRYPTOPRO_BUG_EXTENSION, "");
  155. $message->repack();
  156. }
  157. # Test 1-2: Sending a duplicate extension should fail.
  158. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  159. plan tests => 8;
  160. ok($fatal_alert, "Duplicate ClientHello extension");
  161. $fatal_alert = 0;
  162. $proxy->clear();
  163. $proxy->filter(\&inject_duplicate_extension_serverhello);
  164. $proxy->start();
  165. ok($fatal_alert, "Duplicate ServerHello extension");
  166. SKIP: {
  167. skip "TLS <= 1.2 disabled", 2 if $no_below_tls13;
  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->start();
  173. ok(TLSProxy::Message->success, "Zero extension length test");
  174. #Test 4: Inject an unsolicited extension (<= TLSv1.2)
  175. $fatal_alert = 0;
  176. $proxy->clear();
  177. $proxy->filter(\&inject_unsolicited_extension);
  178. $testtype = UNSOLICITED_SERVER_NAME;
  179. $proxy->clientflags("-no_tls1_3 -noservername");
  180. $proxy->start();
  181. ok($fatal_alert, "Unsolicited server name extension");
  182. }
  183. SKIP: {
  184. skip "TLS <= 1.2 disabled or EC disabled", 1
  185. if $no_below_tls13 || disabled("ec");
  186. #Test 5: Inject a noncompliant supported_groups extension (<= TLSv1.2)
  187. $proxy->clear();
  188. $proxy->filter(\&inject_unsolicited_extension);
  189. $testtype = NONCOMPLIANT_SUPPORTED_GROUPS;
  190. $proxy->clientflags("-no_tls1_3");
  191. $proxy->start();
  192. ok(TLSProxy::Message->success(), "Noncompliant supported_groups extension");
  193. }
  194. SKIP: {
  195. skip "TLS <= 1.2 or CT disabled", 1
  196. if $no_below_tls13 || disabled("ct");
  197. #Test 6: Same as above for the SCT extension which has special handling
  198. $fatal_alert = 0;
  199. $proxy->clear();
  200. $testtype = UNSOLICITED_SCT;
  201. $proxy->clientflags("-no_tls1_3");
  202. $proxy->start();
  203. ok($fatal_alert, "Unsolicited sct extension");
  204. }
  205. SKIP: {
  206. skip "TLS 1.3 disabled", 1 if disabled("tls1_3");
  207. #Test 7: Inject an unsolicited extension (TLSv1.3)
  208. $fatal_alert = 0;
  209. $proxy->clear();
  210. $proxy->filter(\&inject_unsolicited_extension);
  211. $testtype = UNSOLICITED_SERVER_NAME_TLS13;
  212. $proxy->clientflags("-noservername");
  213. $proxy->start();
  214. ok($fatal_alert, "Unsolicited server name extension (TLSv1.3)");
  215. }
  216. #Test 8: Send the cryptopro extension in a ClientHello. Normally this is an
  217. # unsolicited extension only ever seen in the ServerHello. We should
  218. # ignore it in a ClientHello
  219. $proxy->clear();
  220. $proxy->filter(\&inject_cryptopro_extension);
  221. $proxy->start();
  222. ok(TLSProxy::Message->success(), "Cryptopro extension in ClientHello");