70-test_tls13kexmodes.t 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #! /usr/bin/env perl
  2. # Copyright 2017 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 srctop_dir bldtop_dir/;
  10. use OpenSSL::Test::Utils;
  11. use File::Temp qw(tempfile);
  12. use TLSProxy::Proxy;
  13. use checkhandshake qw(checkhandshake @handmessages @extensions);
  14. my $test_name = "test_tls13kexmodes";
  15. setup($test_name);
  16. plan skip_all => "TLSProxy isn't usable on $^O"
  17. if $^O =~ /^(VMS|MSWin32)$/;
  18. plan skip_all => "$test_name needs the dynamic engine feature enabled"
  19. if disabled("engine") || disabled("dynamic-engine");
  20. plan skip_all => "$test_name needs the sock feature enabled"
  21. if disabled("sock");
  22. plan skip_all => "$test_name needs TLSv1.3 enabled"
  23. if disabled("tls1_3");
  24. $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
  25. $ENV{CTLOG_FILE} = srctop_file("test", "ct", "log_list.conf");
  26. @handmessages = (
  27. [TLSProxy::Message::MT_CLIENT_HELLO,
  28. checkhandshake::ALL_HANDSHAKES],
  29. [TLSProxy::Message::MT_HELLO_RETRY_REQUEST,
  30. checkhandshake::HRR_HANDSHAKE | checkhandshake::HRR_RESUME_HANDSHAKE],
  31. [TLSProxy::Message::MT_CLIENT_HELLO,
  32. checkhandshake::HRR_HANDSHAKE | checkhandshake::HRR_RESUME_HANDSHAKE],
  33. [TLSProxy::Message::MT_SERVER_HELLO,
  34. checkhandshake::ALL_HANDSHAKES],
  35. [TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS,
  36. checkhandshake::ALL_HANDSHAKES],
  37. [TLSProxy::Message::MT_CERTIFICATE_REQUEST,
  38. checkhandshake::CLIENT_AUTH_HANDSHAKE],
  39. [TLSProxy::Message::MT_CERTIFICATE,
  40. checkhandshake::ALL_HANDSHAKES & ~(checkhandshake::RESUME_HANDSHAKE | checkhandshake::HRR_RESUME_HANDSHAKE)],
  41. [TLSProxy::Message::MT_CERTIFICATE_VERIFY,
  42. checkhandshake::ALL_HANDSHAKES & ~(checkhandshake::RESUME_HANDSHAKE | checkhandshake::HRR_RESUME_HANDSHAKE)],
  43. [TLSProxy::Message::MT_FINISHED,
  44. checkhandshake::ALL_HANDSHAKES],
  45. [TLSProxy::Message::MT_CERTIFICATE,
  46. checkhandshake::CLIENT_AUTH_HANDSHAKE],
  47. [TLSProxy::Message::MT_CERTIFICATE_VERIFY,
  48. checkhandshake::CLIENT_AUTH_HANDSHAKE],
  49. [TLSProxy::Message::MT_FINISHED,
  50. checkhandshake::ALL_HANDSHAKES],
  51. [0, 0]
  52. );
  53. @extensions = (
  54. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SERVER_NAME,
  55. checkhandshake::SERVER_NAME_CLI_EXTENSION],
  56. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_STATUS_REQUEST,
  57. checkhandshake::STATUS_REQUEST_CLI_EXTENSION],
  58. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_GROUPS,
  59. checkhandshake::DEFAULT_EXTENSIONS],
  60. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EC_POINT_FORMATS,
  61. checkhandshake::DEFAULT_EXTENSIONS],
  62. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SIG_ALGS,
  63. checkhandshake::DEFAULT_EXTENSIONS],
  64. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ALPN,
  65. checkhandshake::ALPN_CLI_EXTENSION],
  66. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SCT,
  67. checkhandshake::SCT_CLI_EXTENSION],
  68. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ENCRYPT_THEN_MAC,
  69. checkhandshake::DEFAULT_EXTENSIONS],
  70. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EXTENDED_MASTER_SECRET,
  71. checkhandshake::DEFAULT_EXTENSIONS],
  72. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SESSION_TICKET,
  73. checkhandshake::DEFAULT_EXTENSIONS],
  74. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_KEY_SHARE,
  75. checkhandshake::DEFAULT_EXTENSIONS],
  76. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_VERSIONS,
  77. checkhandshake::DEFAULT_EXTENSIONS],
  78. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK_KEX_MODES,
  79. checkhandshake::PSK_KEX_MODES_EXTENSION],
  80. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK,
  81. checkhandshake::PSK_CLI_EXTENSION],
  82. [TLSProxy::Message::MT_HELLO_RETRY_REQUEST, TLSProxy::Message::EXT_KEY_SHARE,
  83. checkhandshake::KEY_SHARE_HRR_EXTENSION],
  84. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SERVER_NAME,
  85. checkhandshake::SERVER_NAME_CLI_EXTENSION],
  86. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_STATUS_REQUEST,
  87. checkhandshake::STATUS_REQUEST_CLI_EXTENSION],
  88. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_GROUPS,
  89. checkhandshake::DEFAULT_EXTENSIONS],
  90. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SIG_ALGS,
  91. checkhandshake::DEFAULT_EXTENSIONS],
  92. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ALPN,
  93. checkhandshake::ALPN_CLI_EXTENSION],
  94. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SCT,
  95. checkhandshake::SCT_CLI_EXTENSION],
  96. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_KEY_SHARE,
  97. checkhandshake::DEFAULT_EXTENSIONS],
  98. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_VERSIONS,
  99. checkhandshake::DEFAULT_EXTENSIONS],
  100. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK_KEX_MODES,
  101. checkhandshake::PSK_KEX_MODES_EXTENSION],
  102. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK,
  103. checkhandshake::PSK_CLI_EXTENSION],
  104. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_KEY_SHARE,
  105. checkhandshake::KEY_SHARE_SRV_EXTENSION],
  106. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_PSK,
  107. checkhandshake::PSK_SRV_EXTENSION],
  108. [TLSProxy::Message::MT_CERTIFICATE, TLSProxy::Message::EXT_STATUS_REQUEST,
  109. checkhandshake::STATUS_REQUEST_SRV_EXTENSION],
  110. [0,0,0]
  111. );
  112. use constant {
  113. DELETE_EXTENSION => 0,
  114. EMPTY_EXTENSION => 1,
  115. NON_DHE_KEX_MODE_ONLY => 2,
  116. DHE_KEX_MODE_ONLY => 3,
  117. UNKNONW_KEX_MODES => 4,
  118. BOTH_KEX_MODES => 5
  119. };
  120. my $proxy = TLSProxy::Proxy->new(
  121. undef,
  122. cmdstr(app(["openssl"]), display => 1),
  123. srctop_file("apps", "server.pem"),
  124. (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
  125. );
  126. #Test 1: First get a session
  127. (undef, my $session) = tempfile();
  128. $proxy->clientflags("-sess_out ".$session);
  129. $proxy->sessionfile($session);
  130. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  131. plan tests => 11;
  132. ok(TLSProxy::Message->success(), "Initial connection");
  133. #Test 2: Attempt a resume with no kex modes extension. Should not resume
  134. $proxy->clear();
  135. $proxy->clientflags("-sess_in ".$session);
  136. my $testtype = DELETE_EXTENSION;
  137. $proxy->filter(\&modify_kex_modes_filter);
  138. $proxy->start();
  139. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  140. checkhandshake::DEFAULT_EXTENSIONS
  141. | checkhandshake::KEY_SHARE_SRV_EXTENSION
  142. | checkhandshake::PSK_CLI_EXTENSION,
  143. "Resume with no kex modes");
  144. #Test 3: Attempt a resume with empty kex modes extension. Should fail (empty
  145. # extension is invalid)
  146. $proxy->clear();
  147. $proxy->clientflags("-sess_in ".$session);
  148. $testtype = EMPTY_EXTENSION;
  149. $proxy->start();
  150. ok(TLSProxy::Message->fail(), "Resume with empty kex modes");
  151. #Test 4: Attempt a resume with non-dhe kex mode only. Should resume without a
  152. # key_share
  153. $proxy->clear();
  154. $proxy->clientflags("-sess_in ".$session);
  155. $testtype = NON_DHE_KEX_MODE_ONLY;
  156. $proxy->start();
  157. checkhandshake($proxy, checkhandshake::RESUME_HANDSHAKE,
  158. checkhandshake::DEFAULT_EXTENSIONS
  159. | checkhandshake::PSK_KEX_MODES_EXTENSION
  160. | checkhandshake::PSK_CLI_EXTENSION
  161. | checkhandshake::PSK_SRV_EXTENSION,
  162. "Resume with non-dhe kex mode");
  163. #Test 5: Attempt a resume with dhe kex mode only. Should resume with a key_share
  164. $proxy->clear();
  165. $proxy->clientflags("-sess_in ".$session);
  166. $testtype = DHE_KEX_MODE_ONLY;
  167. $proxy->start();
  168. checkhandshake($proxy, checkhandshake::RESUME_HANDSHAKE,
  169. checkhandshake::DEFAULT_EXTENSIONS
  170. | checkhandshake::PSK_KEX_MODES_EXTENSION
  171. | checkhandshake::KEY_SHARE_SRV_EXTENSION
  172. | checkhandshake::PSK_CLI_EXTENSION
  173. | checkhandshake::PSK_SRV_EXTENSION,
  174. "Resume with non-dhe kex mode");
  175. #Test 6: Attempt a resume with only unrecognised kex modes. Should not resume
  176. $proxy->clear();
  177. $proxy->clientflags("-sess_in ".$session);
  178. $testtype = UNKNONW_KEX_MODES;
  179. $proxy->start();
  180. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  181. checkhandshake::DEFAULT_EXTENSIONS
  182. | checkhandshake::PSK_KEX_MODES_EXTENSION
  183. | checkhandshake::KEY_SHARE_SRV_EXTENSION
  184. | checkhandshake::PSK_CLI_EXTENSION,
  185. "Resume with empty kex modes");
  186. #Test 7: Attempt a resume with both non-dhe and dhe kex mode. Should resume with
  187. # a key_share
  188. $proxy->clear();
  189. $proxy->clientflags("-sess_in ".$session);
  190. $testtype = BOTH_KEX_MODES;
  191. $proxy->start();
  192. checkhandshake($proxy, checkhandshake::RESUME_HANDSHAKE,
  193. checkhandshake::DEFAULT_EXTENSIONS
  194. | checkhandshake::PSK_KEX_MODES_EXTENSION
  195. | checkhandshake::KEY_SHARE_SRV_EXTENSION
  196. | checkhandshake::PSK_CLI_EXTENSION
  197. | checkhandshake::PSK_SRV_EXTENSION,
  198. "Resume with non-dhe kex mode");
  199. #Test 8: Attempt a resume with both non-dhe and dhe kex mode, but unacceptable
  200. # initial key_share. Should resume with a key_share following an HRR
  201. $proxy->clear();
  202. $proxy->clientflags("-sess_in ".$session);
  203. $proxy->serverflags("-curves P-256");
  204. $testtype = BOTH_KEX_MODES;
  205. $proxy->start();
  206. checkhandshake($proxy, checkhandshake::HRR_RESUME_HANDSHAKE,
  207. checkhandshake::DEFAULT_EXTENSIONS
  208. | checkhandshake::PSK_KEX_MODES_EXTENSION
  209. | checkhandshake::KEY_SHARE_SRV_EXTENSION
  210. | checkhandshake::KEY_SHARE_HRR_EXTENSION
  211. | checkhandshake::PSK_CLI_EXTENSION
  212. | checkhandshake::PSK_SRV_EXTENSION,
  213. "Resume with both kex modes and HRR");
  214. #Test 9: Attempt a resume with dhe kex mode only and an unnacceptable initial
  215. # key_share. Should resume with a key_share following an HRR
  216. $proxy->clear();
  217. $proxy->clientflags("-sess_in ".$session);
  218. $proxy->serverflags("-curves P-256");
  219. $testtype = DHE_KEX_MODE_ONLY;
  220. $proxy->start();
  221. checkhandshake($proxy, checkhandshake::HRR_RESUME_HANDSHAKE,
  222. checkhandshake::DEFAULT_EXTENSIONS
  223. | checkhandshake::PSK_KEX_MODES_EXTENSION
  224. | checkhandshake::KEY_SHARE_SRV_EXTENSION
  225. | checkhandshake::KEY_SHARE_HRR_EXTENSION
  226. | checkhandshake::PSK_CLI_EXTENSION
  227. | checkhandshake::PSK_SRV_EXTENSION,
  228. "Resume with dhe kex mode and HRR");
  229. #Test 10: Attempt a resume with both non-dhe and dhe kex mode, unacceptable
  230. # initial key_share and no overlapping groups. Should resume without a
  231. # key_share
  232. $proxy->clear();
  233. $proxy->clientflags("-curves P-384 -sess_in ".$session);
  234. $proxy->serverflags("-curves P-256");
  235. $testtype = BOTH_KEX_MODES;
  236. $proxy->start();
  237. checkhandshake($proxy, checkhandshake::RESUME_HANDSHAKE,
  238. checkhandshake::DEFAULT_EXTENSIONS
  239. | checkhandshake::PSK_KEX_MODES_EXTENSION
  240. | checkhandshake::PSK_CLI_EXTENSION
  241. | checkhandshake::PSK_SRV_EXTENSION,
  242. "Resume with both kex modes, no overlapping groups");
  243. #Test 11: Attempt a resume with dhe kex mode only, unacceptable
  244. # initial key_share and no overlapping groups. Should fail
  245. $proxy->clear();
  246. $proxy->clientflags("-curves P-384 -sess_in ".$session);
  247. $proxy->serverflags("-curves P-256");
  248. $testtype = DHE_KEX_MODE_ONLY;
  249. $proxy->start();
  250. ok(TLSProxy::Message->fail(), "Resume with dhe kex mode, no overlapping groups");
  251. unlink $session;
  252. sub modify_kex_modes_filter
  253. {
  254. my $proxy = shift;
  255. # We're only interested in the initial ClientHello
  256. return if ($proxy->flight != 0);
  257. foreach my $message (@{$proxy->message_list}) {
  258. if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
  259. my $ext;
  260. if ($testtype == EMPTY_EXTENSION) {
  261. $ext = pack "C",
  262. 0x00; #List length
  263. } elsif ($testtype == NON_DHE_KEX_MODE_ONLY) {
  264. $ext = pack "C2",
  265. 0x01, #List length
  266. 0x00; #psk_ke
  267. } elsif ($testtype == DHE_KEX_MODE_ONLY) {
  268. $ext = pack "C2",
  269. 0x01, #List length
  270. 0x01; #psk_dhe_ke
  271. } elsif ($testtype == UNKNONW_KEX_MODES) {
  272. $ext = pack "C3",
  273. 0x02, #List length
  274. 0xfe, #unknown
  275. 0xff; #unknown
  276. } elsif ($testtype == BOTH_KEX_MODES) {
  277. #We deliberately list psk_ke first...should still use psk_dhe_ke
  278. $ext = pack "C3",
  279. 0x02, #List length
  280. 0x00, #psk_ke
  281. 0x01; #psk_dhe_ke
  282. }
  283. if ($testtype == DELETE_EXTENSION) {
  284. $message->delete_extension(
  285. TLSProxy::Message::EXT_PSK_KEX_MODES);
  286. } else {
  287. $message->set_extension(
  288. TLSProxy::Message::EXT_PSK_KEX_MODES, $ext);
  289. }
  290. $message->repack();
  291. }
  292. }
  293. }