70-test_tls13kexmodes.t 14 KB

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