70-test_tls13kexmodes.t 14 KB

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