70-test_tls13kexmodes.t 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #! /usr/bin/env perl
  2. # Copyright 2017-2021 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)$/;
  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_SERVER_HELLO,
  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. TLSProxy::Message::CLIENT,
  56. checkhandshake::SERVER_NAME_CLI_EXTENSION],
  57. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_STATUS_REQUEST,
  58. TLSProxy::Message::CLIENT,
  59. checkhandshake::STATUS_REQUEST_CLI_EXTENSION],
  60. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_GROUPS,
  61. TLSProxy::Message::CLIENT,
  62. checkhandshake::DEFAULT_EXTENSIONS],
  63. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EC_POINT_FORMATS,
  64. TLSProxy::Message::CLIENT,
  65. checkhandshake::DEFAULT_EXTENSIONS],
  66. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SIG_ALGS,
  67. TLSProxy::Message::CLIENT,
  68. checkhandshake::DEFAULT_EXTENSIONS],
  69. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ALPN,
  70. TLSProxy::Message::CLIENT,
  71. checkhandshake::ALPN_CLI_EXTENSION],
  72. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SCT,
  73. TLSProxy::Message::CLIENT,
  74. checkhandshake::SCT_CLI_EXTENSION],
  75. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ENCRYPT_THEN_MAC,
  76. TLSProxy::Message::CLIENT,
  77. checkhandshake::DEFAULT_EXTENSIONS],
  78. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EXTENDED_MASTER_SECRET,
  79. TLSProxy::Message::CLIENT,
  80. checkhandshake::DEFAULT_EXTENSIONS],
  81. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SESSION_TICKET,
  82. TLSProxy::Message::CLIENT,
  83. checkhandshake::DEFAULT_EXTENSIONS],
  84. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_KEY_SHARE,
  85. TLSProxy::Message::CLIENT,
  86. checkhandshake::DEFAULT_EXTENSIONS],
  87. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_VERSIONS,
  88. TLSProxy::Message::CLIENT,
  89. checkhandshake::DEFAULT_EXTENSIONS],
  90. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK_KEX_MODES,
  91. TLSProxy::Message::CLIENT,
  92. checkhandshake::PSK_KEX_MODES_EXTENSION],
  93. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK,
  94. TLSProxy::Message::CLIENT,
  95. checkhandshake::PSK_CLI_EXTENSION],
  96. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_SUPPORTED_VERSIONS,
  97. TLSProxy::Message::SERVER,
  98. checkhandshake::DEFAULT_EXTENSIONS],
  99. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_KEY_SHARE,
  100. TLSProxy::Message::SERVER,
  101. checkhandshake::KEY_SHARE_HRR_EXTENSION],
  102. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SERVER_NAME,
  103. TLSProxy::Message::CLIENT,
  104. checkhandshake::SERVER_NAME_CLI_EXTENSION],
  105. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_STATUS_REQUEST,
  106. TLSProxy::Message::CLIENT,
  107. checkhandshake::STATUS_REQUEST_CLI_EXTENSION],
  108. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_GROUPS,
  109. TLSProxy::Message::CLIENT,
  110. checkhandshake::DEFAULT_EXTENSIONS],
  111. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EC_POINT_FORMATS,
  112. TLSProxy::Message::CLIENT,
  113. checkhandshake::DEFAULT_EXTENSIONS],
  114. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SIG_ALGS,
  115. TLSProxy::Message::CLIENT,
  116. checkhandshake::DEFAULT_EXTENSIONS],
  117. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ALPN,
  118. TLSProxy::Message::CLIENT,
  119. checkhandshake::ALPN_CLI_EXTENSION],
  120. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SCT,
  121. TLSProxy::Message::CLIENT,
  122. checkhandshake::SCT_CLI_EXTENSION],
  123. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ENCRYPT_THEN_MAC,
  124. TLSProxy::Message::CLIENT,
  125. checkhandshake::DEFAULT_EXTENSIONS],
  126. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EXTENDED_MASTER_SECRET,
  127. TLSProxy::Message::CLIENT,
  128. checkhandshake::DEFAULT_EXTENSIONS],
  129. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SESSION_TICKET,
  130. TLSProxy::Message::CLIENT,
  131. checkhandshake::DEFAULT_EXTENSIONS],
  132. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_KEY_SHARE,
  133. TLSProxy::Message::CLIENT,
  134. checkhandshake::DEFAULT_EXTENSIONS],
  135. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_VERSIONS,
  136. TLSProxy::Message::CLIENT,
  137. checkhandshake::DEFAULT_EXTENSIONS],
  138. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK_KEX_MODES,
  139. TLSProxy::Message::CLIENT,
  140. checkhandshake::PSK_KEX_MODES_EXTENSION],
  141. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK,
  142. TLSProxy::Message::CLIENT,
  143. checkhandshake::PSK_CLI_EXTENSION],
  144. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_SUPPORTED_VERSIONS,
  145. TLSProxy::Message::SERVER,
  146. checkhandshake::DEFAULT_EXTENSIONS],
  147. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_KEY_SHARE,
  148. TLSProxy::Message::SERVER,
  149. checkhandshake::KEY_SHARE_SRV_EXTENSION],
  150. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_PSK,
  151. TLSProxy::Message::SERVER,
  152. checkhandshake::PSK_SRV_EXTENSION],
  153. [TLSProxy::Message::MT_CERTIFICATE, TLSProxy::Message::EXT_STATUS_REQUEST,
  154. TLSProxy::Message::SERVER,
  155. checkhandshake::STATUS_REQUEST_SRV_EXTENSION],
  156. [0,0,0,0]
  157. );
  158. use constant {
  159. DELETE_EXTENSION => 0,
  160. EMPTY_EXTENSION => 1,
  161. NON_DHE_KEX_MODE_ONLY => 2,
  162. DHE_KEX_MODE_ONLY => 3,
  163. UNKNOWN_KEX_MODES => 4,
  164. BOTH_KEX_MODES => 5
  165. };
  166. my $proxy = TLSProxy::Proxy->new(
  167. undef,
  168. cmdstr(app(["openssl"]), display => 1),
  169. srctop_file("apps", "server.pem"),
  170. (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
  171. );
  172. #Test 1: First get a session
  173. (undef, my $session) = tempfile();
  174. $proxy->clientflags("-sess_out ".$session);
  175. $proxy->serverflags("-servername localhost");
  176. $proxy->sessionfile($session);
  177. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  178. plan tests => 11;
  179. ok(TLSProxy::Message->success(), "Initial connection");
  180. #Test 2: Attempt a resume with no kex modes extension. Should fail (server
  181. # MUST abort handshake with pre_shared key and no psk_kex_modes)
  182. $proxy->clear();
  183. $proxy->clientflags("-sess_in ".$session);
  184. my $testtype = DELETE_EXTENSION;
  185. $proxy->filter(\&modify_kex_modes_filter);
  186. $proxy->start();
  187. ok(TLSProxy::Message->fail(), "Resume with no kex modes");
  188. #Test 3: Attempt a resume with empty kex modes extension. Should fail (empty
  189. # extension is invalid)
  190. $proxy->clear();
  191. $proxy->clientflags("-sess_in ".$session);
  192. $testtype = EMPTY_EXTENSION;
  193. $proxy->start();
  194. ok(TLSProxy::Message->fail(), "Resume with empty kex modes");
  195. #Test 4: Attempt a resume with non-dhe kex mode only. Should resume without a
  196. # key_share
  197. $proxy->clear();
  198. $proxy->clientflags("-allow_no_dhe_kex -sess_in ".$session);
  199. $proxy->serverflags("-allow_no_dhe_kex");
  200. $testtype = NON_DHE_KEX_MODE_ONLY;
  201. $proxy->start();
  202. checkhandshake($proxy, checkhandshake::RESUME_HANDSHAKE,
  203. checkhandshake::DEFAULT_EXTENSIONS
  204. | checkhandshake::PSK_KEX_MODES_EXTENSION
  205. | checkhandshake::PSK_CLI_EXTENSION
  206. | checkhandshake::PSK_SRV_EXTENSION,
  207. "Resume with non-dhe kex mode");
  208. #Test 5: Attempt a resume with dhe kex mode only. Should resume with a key_share
  209. $proxy->clear();
  210. $proxy->clientflags("-sess_in ".$session);
  211. $testtype = DHE_KEX_MODE_ONLY;
  212. $proxy->start();
  213. checkhandshake($proxy, checkhandshake::RESUME_HANDSHAKE,
  214. checkhandshake::DEFAULT_EXTENSIONS
  215. | checkhandshake::PSK_KEX_MODES_EXTENSION
  216. | checkhandshake::KEY_SHARE_SRV_EXTENSION
  217. | checkhandshake::PSK_CLI_EXTENSION
  218. | checkhandshake::PSK_SRV_EXTENSION,
  219. "Resume with non-dhe kex mode");
  220. #Test 6: Attempt a resume with only unrecognised kex modes. Should not resume
  221. # but rather fall back to full handshake
  222. $proxy->clear();
  223. $proxy->clientflags("-sess_in ".$session);
  224. $testtype = UNKNOWN_KEX_MODES;
  225. $proxy->start();
  226. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  227. checkhandshake::DEFAULT_EXTENSIONS
  228. | checkhandshake::PSK_KEX_MODES_EXTENSION
  229. | checkhandshake::KEY_SHARE_SRV_EXTENSION
  230. | checkhandshake::PSK_CLI_EXTENSION,
  231. "Resume with unrecognized kex mode");
  232. #Test 7: Attempt a resume with both non-dhe and dhe kex mode. Should resume with
  233. # a key_share
  234. $proxy->clear();
  235. $proxy->clientflags("-sess_in ".$session);
  236. $testtype = BOTH_KEX_MODES;
  237. $proxy->start();
  238. checkhandshake($proxy, checkhandshake::RESUME_HANDSHAKE,
  239. checkhandshake::DEFAULT_EXTENSIONS
  240. | checkhandshake::PSK_KEX_MODES_EXTENSION
  241. | checkhandshake::KEY_SHARE_SRV_EXTENSION
  242. | checkhandshake::PSK_CLI_EXTENSION
  243. | checkhandshake::PSK_SRV_EXTENSION,
  244. "Resume with non-dhe kex mode");
  245. #Test 8: Attempt a resume with both non-dhe and dhe kex mode, but unacceptable
  246. # initial key_share. Should resume with a key_share following an HRR
  247. $proxy->clear();
  248. $proxy->clientflags("-sess_in ".$session);
  249. $proxy->serverflags("-curves P-256");
  250. $testtype = BOTH_KEX_MODES;
  251. $proxy->start();
  252. checkhandshake($proxy, checkhandshake::HRR_RESUME_HANDSHAKE,
  253. checkhandshake::DEFAULT_EXTENSIONS
  254. | checkhandshake::PSK_KEX_MODES_EXTENSION
  255. | checkhandshake::KEY_SHARE_SRV_EXTENSION
  256. | checkhandshake::KEY_SHARE_HRR_EXTENSION
  257. | checkhandshake::PSK_CLI_EXTENSION
  258. | checkhandshake::PSK_SRV_EXTENSION,
  259. "Resume with both kex modes and HRR");
  260. #Test 9: Attempt a resume with dhe kex mode only and an unacceptable initial
  261. # key_share. Should resume with a key_share following an HRR
  262. $proxy->clear();
  263. $proxy->clientflags("-sess_in ".$session);
  264. $proxy->serverflags("-curves P-256");
  265. $testtype = DHE_KEX_MODE_ONLY;
  266. $proxy->start();
  267. checkhandshake($proxy, checkhandshake::HRR_RESUME_HANDSHAKE,
  268. checkhandshake::DEFAULT_EXTENSIONS
  269. | checkhandshake::PSK_KEX_MODES_EXTENSION
  270. | checkhandshake::KEY_SHARE_SRV_EXTENSION
  271. | checkhandshake::KEY_SHARE_HRR_EXTENSION
  272. | checkhandshake::PSK_CLI_EXTENSION
  273. | checkhandshake::PSK_SRV_EXTENSION,
  274. "Resume with dhe kex mode and HRR");
  275. #Test 10: Attempt a resume with both non-dhe and dhe kex mode, unacceptable
  276. # initial key_share and no overlapping groups. Should resume without a
  277. # key_share
  278. $proxy->clear();
  279. $proxy->clientflags("-allow_no_dhe_kex -curves P-384 -sess_in ".$session);
  280. $proxy->serverflags("-allow_no_dhe_kex -curves P-256");
  281. $testtype = BOTH_KEX_MODES;
  282. $proxy->start();
  283. checkhandshake($proxy, checkhandshake::RESUME_HANDSHAKE,
  284. checkhandshake::DEFAULT_EXTENSIONS
  285. | checkhandshake::PSK_KEX_MODES_EXTENSION
  286. | checkhandshake::PSK_CLI_EXTENSION
  287. | checkhandshake::PSK_SRV_EXTENSION,
  288. "Resume with both kex modes, no overlapping groups");
  289. #Test 11: Attempt a resume with dhe kex mode only, unacceptable
  290. # initial key_share and no overlapping groups. Should fail
  291. $proxy->clear();
  292. $proxy->clientflags("-curves P-384 -sess_in ".$session);
  293. $proxy->serverflags("-curves P-256");
  294. $testtype = DHE_KEX_MODE_ONLY;
  295. $proxy->start();
  296. ok(TLSProxy::Message->fail(), "Resume with dhe kex mode, no overlapping groups");
  297. unlink $session;
  298. sub modify_kex_modes_filter
  299. {
  300. my $proxy = shift;
  301. # We're only interested in the initial ClientHello
  302. return if ($proxy->flight != 0);
  303. foreach my $message (@{$proxy->message_list}) {
  304. if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
  305. my $ext;
  306. if ($testtype == EMPTY_EXTENSION) {
  307. $ext = pack "C",
  308. 0x00; #List length
  309. } elsif ($testtype == NON_DHE_KEX_MODE_ONLY) {
  310. $ext = pack "C2",
  311. 0x01, #List length
  312. 0x00; #psk_ke
  313. } elsif ($testtype == DHE_KEX_MODE_ONLY) {
  314. $ext = pack "C2",
  315. 0x01, #List length
  316. 0x01; #psk_dhe_ke
  317. } elsif ($testtype == UNKNOWN_KEX_MODES) {
  318. $ext = pack "C3",
  319. 0x02, #List length
  320. 0xfe, #unknown
  321. 0xff; #unknown
  322. } elsif ($testtype == BOTH_KEX_MODES) {
  323. #We deliberately list psk_ke first...should still use psk_dhe_ke
  324. $ext = pack "C3",
  325. 0x02, #List length
  326. 0x00, #psk_ke
  327. 0x01; #psk_dhe_ke
  328. }
  329. if ($testtype == DELETE_EXTENSION) {
  330. $message->delete_extension(
  331. TLSProxy::Message::EXT_PSK_KEX_MODES);
  332. } else {
  333. $message->set_extension(
  334. TLSProxy::Message::EXT_PSK_KEX_MODES, $ext);
  335. }
  336. $message->repack();
  337. }
  338. }
  339. }