70-test_tls13messages.t 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2020 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_tls13messages";
  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. @handmessages = (
  28. [TLSProxy::Message::MT_CLIENT_HELLO,
  29. checkhandshake::ALL_HANDSHAKES],
  30. [TLSProxy::Message::MT_SERVER_HELLO,
  31. checkhandshake::HRR_HANDSHAKE | checkhandshake::HRR_RESUME_HANDSHAKE],
  32. [TLSProxy::Message::MT_CLIENT_HELLO,
  33. checkhandshake::HRR_HANDSHAKE | checkhandshake::HRR_RESUME_HANDSHAKE],
  34. [TLSProxy::Message::MT_SERVER_HELLO,
  35. checkhandshake::ALL_HANDSHAKES],
  36. [TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS,
  37. checkhandshake::ALL_HANDSHAKES],
  38. [TLSProxy::Message::MT_CERTIFICATE_REQUEST,
  39. checkhandshake::CLIENT_AUTH_HANDSHAKE],
  40. [TLSProxy::Message::MT_CERTIFICATE,
  41. checkhandshake::ALL_HANDSHAKES & ~(checkhandshake::RESUME_HANDSHAKE | checkhandshake::HRR_RESUME_HANDSHAKE)],
  42. [TLSProxy::Message::MT_CERTIFICATE_VERIFY,
  43. checkhandshake::ALL_HANDSHAKES & ~(checkhandshake::RESUME_HANDSHAKE | checkhandshake::HRR_RESUME_HANDSHAKE)],
  44. [TLSProxy::Message::MT_FINISHED,
  45. checkhandshake::ALL_HANDSHAKES],
  46. [TLSProxy::Message::MT_CERTIFICATE,
  47. checkhandshake::CLIENT_AUTH_HANDSHAKE],
  48. [TLSProxy::Message::MT_CERTIFICATE_VERIFY,
  49. checkhandshake::CLIENT_AUTH_HANDSHAKE],
  50. [TLSProxy::Message::MT_FINISHED,
  51. checkhandshake::ALL_HANDSHAKES],
  52. [0, 0]
  53. );
  54. @extensions = (
  55. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SERVER_NAME,
  56. TLSProxy::Message::CLIENT,
  57. checkhandshake::SERVER_NAME_CLI_EXTENSION],
  58. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_STATUS_REQUEST,
  59. TLSProxy::Message::CLIENT,
  60. checkhandshake::STATUS_REQUEST_CLI_EXTENSION],
  61. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_GROUPS,
  62. TLSProxy::Message::CLIENT,
  63. checkhandshake::DEFAULT_EXTENSIONS],
  64. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EC_POINT_FORMATS,
  65. TLSProxy::Message::CLIENT,
  66. checkhandshake::DEFAULT_EXTENSIONS],
  67. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SIG_ALGS,
  68. TLSProxy::Message::CLIENT,
  69. checkhandshake::DEFAULT_EXTENSIONS],
  70. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ALPN,
  71. TLSProxy::Message::CLIENT,
  72. checkhandshake::ALPN_CLI_EXTENSION],
  73. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SCT,
  74. TLSProxy::Message::CLIENT,
  75. checkhandshake::SCT_CLI_EXTENSION],
  76. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ENCRYPT_THEN_MAC,
  77. TLSProxy::Message::CLIENT,
  78. checkhandshake::DEFAULT_EXTENSIONS],
  79. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EXTENDED_MASTER_SECRET,
  80. TLSProxy::Message::CLIENT,
  81. checkhandshake::DEFAULT_EXTENSIONS],
  82. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SESSION_TICKET,
  83. TLSProxy::Message::CLIENT,
  84. checkhandshake::DEFAULT_EXTENSIONS],
  85. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_KEY_SHARE,
  86. TLSProxy::Message::CLIENT,
  87. checkhandshake::DEFAULT_EXTENSIONS],
  88. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_VERSIONS,
  89. TLSProxy::Message::CLIENT,
  90. checkhandshake::DEFAULT_EXTENSIONS],
  91. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK_KEX_MODES,
  92. TLSProxy::Message::CLIENT,
  93. checkhandshake::DEFAULT_EXTENSIONS],
  94. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK,
  95. TLSProxy::Message::CLIENT,
  96. checkhandshake::PSK_CLI_EXTENSION],
  97. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_POST_HANDSHAKE_AUTH,
  98. TLSProxy::Message::CLIENT,
  99. checkhandshake::POST_HANDSHAKE_AUTH_CLI_EXTENSION],
  100. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_SUPPORTED_VERSIONS,
  101. TLSProxy::Message::SERVER,
  102. checkhandshake::DEFAULT_EXTENSIONS],
  103. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_KEY_SHARE,
  104. TLSProxy::Message::SERVER,
  105. checkhandshake::KEY_SHARE_HRR_EXTENSION],
  106. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SERVER_NAME,
  107. TLSProxy::Message::CLIENT,
  108. checkhandshake::SERVER_NAME_CLI_EXTENSION],
  109. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_STATUS_REQUEST,
  110. TLSProxy::Message::CLIENT,
  111. checkhandshake::STATUS_REQUEST_CLI_EXTENSION],
  112. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_GROUPS,
  113. TLSProxy::Message::CLIENT,
  114. checkhandshake::DEFAULT_EXTENSIONS],
  115. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EC_POINT_FORMATS,
  116. TLSProxy::Message::CLIENT,
  117. checkhandshake::DEFAULT_EXTENSIONS],
  118. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SIG_ALGS,
  119. TLSProxy::Message::CLIENT,
  120. checkhandshake::DEFAULT_EXTENSIONS],
  121. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ALPN,
  122. TLSProxy::Message::CLIENT,
  123. checkhandshake::ALPN_CLI_EXTENSION],
  124. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SCT,
  125. TLSProxy::Message::CLIENT,
  126. checkhandshake::SCT_CLI_EXTENSION],
  127. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ENCRYPT_THEN_MAC,
  128. TLSProxy::Message::CLIENT,
  129. checkhandshake::DEFAULT_EXTENSIONS],
  130. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EXTENDED_MASTER_SECRET,
  131. TLSProxy::Message::CLIENT,
  132. checkhandshake::DEFAULT_EXTENSIONS],
  133. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SESSION_TICKET,
  134. TLSProxy::Message::CLIENT,
  135. checkhandshake::DEFAULT_EXTENSIONS],
  136. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_KEY_SHARE,
  137. TLSProxy::Message::CLIENT,
  138. checkhandshake::DEFAULT_EXTENSIONS],
  139. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_VERSIONS,
  140. TLSProxy::Message::CLIENT,
  141. checkhandshake::DEFAULT_EXTENSIONS],
  142. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK_KEX_MODES,
  143. TLSProxy::Message::CLIENT,
  144. checkhandshake::DEFAULT_EXTENSIONS],
  145. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK,
  146. TLSProxy::Message::CLIENT,
  147. checkhandshake::PSK_CLI_EXTENSION],
  148. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_POST_HANDSHAKE_AUTH,
  149. TLSProxy::Message::CLIENT,
  150. checkhandshake::POST_HANDSHAKE_AUTH_CLI_EXTENSION],
  151. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_SUPPORTED_VERSIONS,
  152. TLSProxy::Message::SERVER,
  153. checkhandshake::DEFAULT_EXTENSIONS],
  154. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_KEY_SHARE,
  155. TLSProxy::Message::SERVER,
  156. checkhandshake::DEFAULT_EXTENSIONS],
  157. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_PSK,
  158. TLSProxy::Message::SERVER,
  159. checkhandshake::PSK_SRV_EXTENSION],
  160. [TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS, TLSProxy::Message::EXT_SERVER_NAME,
  161. TLSProxy::Message::SERVER,
  162. checkhandshake::SERVER_NAME_SRV_EXTENSION],
  163. [TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS, TLSProxy::Message::EXT_ALPN,
  164. TLSProxy::Message::SERVER,
  165. checkhandshake::ALPN_SRV_EXTENSION],
  166. [TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS, TLSProxy::Message::EXT_SUPPORTED_GROUPS,
  167. TLSProxy::Message::SERVER,
  168. checkhandshake::SUPPORTED_GROUPS_SRV_EXTENSION],
  169. [TLSProxy::Message::MT_CERTIFICATE_REQUEST, TLSProxy::Message::EXT_SIG_ALGS,
  170. TLSProxy::Message::SERVER,
  171. checkhandshake::DEFAULT_EXTENSIONS],
  172. [TLSProxy::Message::MT_CERTIFICATE, TLSProxy::Message::EXT_STATUS_REQUEST,
  173. TLSProxy::Message::SERVER,
  174. checkhandshake::STATUS_REQUEST_SRV_EXTENSION],
  175. [TLSProxy::Message::MT_CERTIFICATE, TLSProxy::Message::EXT_SCT,
  176. TLSProxy::Message::SERVER,
  177. checkhandshake::SCT_SRV_EXTENSION],
  178. [0,0,0,0]
  179. );
  180. my $proxy = TLSProxy::Proxy->new(
  181. undef,
  182. cmdstr(app(["openssl"]), display => 1),
  183. srctop_file("apps", "server.pem"),
  184. (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
  185. );
  186. #Test 1: Check we get all the right messages for a default handshake
  187. (undef, my $session) = tempfile();
  188. $proxy->serverconnects(2);
  189. $proxy->clientflags("-sess_out ".$session);
  190. $proxy->sessionfile($session);
  191. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  192. plan tests => 17;
  193. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  194. checkhandshake::DEFAULT_EXTENSIONS,
  195. "Default handshake test");
  196. #Test 2: Resumption handshake
  197. $proxy->clearClient();
  198. $proxy->clientflags("-sess_in ".$session);
  199. $proxy->clientstart();
  200. checkhandshake($proxy, checkhandshake::RESUME_HANDSHAKE,
  201. (checkhandshake::DEFAULT_EXTENSIONS
  202. | checkhandshake::PSK_CLI_EXTENSION
  203. | checkhandshake::PSK_SRV_EXTENSION),
  204. "Resumption handshake test");
  205. SKIP: {
  206. skip "No OCSP support in this OpenSSL build", 4
  207. if disabled("ct") || disabled("ec") || disabled("ocsp");
  208. #Test 3: A status_request handshake (client request only)
  209. $proxy->clear();
  210. $proxy->clientflags("-status");
  211. $proxy->start();
  212. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  213. checkhandshake::DEFAULT_EXTENSIONS
  214. | checkhandshake::STATUS_REQUEST_CLI_EXTENSION,
  215. "status_request handshake test (client)");
  216. #Test 4: A status_request handshake (server support only)
  217. $proxy->clear();
  218. $proxy->serverflags("-status_file "
  219. .srctop_file("test", "recipes", "ocsp-response.der"));
  220. $proxy->start();
  221. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  222. checkhandshake::DEFAULT_EXTENSIONS,
  223. "status_request handshake test (server)");
  224. #Test 5: A status_request handshake (client and server)
  225. $proxy->clear();
  226. $proxy->clientflags("-status");
  227. $proxy->serverflags("-status_file "
  228. .srctop_file("test", "recipes", "ocsp-response.der"));
  229. $proxy->start();
  230. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  231. checkhandshake::DEFAULT_EXTENSIONS
  232. | checkhandshake::STATUS_REQUEST_CLI_EXTENSION
  233. | checkhandshake::STATUS_REQUEST_SRV_EXTENSION,
  234. "status_request handshake test");
  235. #Test 6: A status_request handshake (client and server) with client auth
  236. $proxy->clear();
  237. $proxy->clientflags("-status -enable_pha -cert "
  238. .srctop_file("apps", "server.pem"));
  239. $proxy->serverflags("-Verify 5 -status_file "
  240. .srctop_file("test", "recipes", "ocsp-response.der"));
  241. $proxy->start();
  242. checkhandshake($proxy, checkhandshake::CLIENT_AUTH_HANDSHAKE,
  243. checkhandshake::DEFAULT_EXTENSIONS
  244. | checkhandshake::STATUS_REQUEST_CLI_EXTENSION
  245. | checkhandshake::STATUS_REQUEST_SRV_EXTENSION
  246. | checkhandshake::POST_HANDSHAKE_AUTH_CLI_EXTENSION,
  247. "status_request handshake with client auth test");
  248. }
  249. #Test 7: A client auth handshake
  250. $proxy->clear();
  251. $proxy->clientflags("-enable_pha -cert ".srctop_file("apps", "server.pem"));
  252. $proxy->serverflags("-Verify 5");
  253. $proxy->start();
  254. checkhandshake($proxy, checkhandshake::CLIENT_AUTH_HANDSHAKE,
  255. checkhandshake::DEFAULT_EXTENSIONS |
  256. checkhandshake::POST_HANDSHAKE_AUTH_CLI_EXTENSION,
  257. "Client auth handshake test");
  258. #Test 8: Server name handshake (no client request)
  259. $proxy->clear();
  260. $proxy->clientflags("-noservername");
  261. $proxy->start();
  262. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  263. checkhandshake::DEFAULT_EXTENSIONS
  264. & ~checkhandshake::SERVER_NAME_CLI_EXTENSION,
  265. "Server name handshake test (client)");
  266. #Test 9: Server name handshake (server support only)
  267. $proxy->clear();
  268. $proxy->clientflags("-noservername");
  269. $proxy->serverflags("-servername testhost");
  270. $proxy->start();
  271. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  272. checkhandshake::DEFAULT_EXTENSIONS
  273. & ~checkhandshake::SERVER_NAME_CLI_EXTENSION,
  274. "Server name handshake test (server)");
  275. #Test 10: Server name handshake (client and server)
  276. $proxy->clear();
  277. $proxy->clientflags("-servername testhost");
  278. $proxy->serverflags("-servername testhost");
  279. $proxy->start();
  280. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  281. checkhandshake::DEFAULT_EXTENSIONS
  282. | checkhandshake::SERVER_NAME_SRV_EXTENSION,
  283. "Server name handshake test");
  284. #Test 11: ALPN handshake (client request only)
  285. $proxy->clear();
  286. $proxy->clientflags("-alpn test");
  287. $proxy->start();
  288. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  289. checkhandshake::DEFAULT_EXTENSIONS
  290. | checkhandshake::ALPN_CLI_EXTENSION,
  291. "ALPN handshake test (client)");
  292. #Test 12: ALPN handshake (server support only)
  293. $proxy->clear();
  294. $proxy->serverflags("-alpn test");
  295. $proxy->start();
  296. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  297. checkhandshake::DEFAULT_EXTENSIONS,
  298. "ALPN handshake test (server)");
  299. #Test 13: ALPN handshake (client and server)
  300. $proxy->clear();
  301. $proxy->clientflags("-alpn test");
  302. $proxy->serverflags("-alpn test");
  303. $proxy->start();
  304. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  305. checkhandshake::DEFAULT_EXTENSIONS
  306. | checkhandshake::ALPN_CLI_EXTENSION
  307. | checkhandshake::ALPN_SRV_EXTENSION,
  308. "ALPN handshake test");
  309. SKIP: {
  310. skip "No CT, EC or OCSP support in this OpenSSL build", 1
  311. if disabled("ct") || disabled("ec") || disabled("ocsp");
  312. #Test 14: SCT handshake (client request only)
  313. $proxy->clear();
  314. #Note: -ct also sends status_request
  315. $proxy->clientflags("-ct");
  316. $proxy->serverflags("-status_file "
  317. .srctop_file("test", "recipes", "ocsp-response.der")
  318. ." -serverinfo ".srctop_file("test", "serverinfo2.pem"));
  319. $proxy->start();
  320. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  321. checkhandshake::DEFAULT_EXTENSIONS
  322. | checkhandshake::SCT_CLI_EXTENSION
  323. | checkhandshake::SCT_SRV_EXTENSION
  324. | checkhandshake::STATUS_REQUEST_CLI_EXTENSION
  325. | checkhandshake::STATUS_REQUEST_SRV_EXTENSION,
  326. "SCT handshake test");
  327. }
  328. #Test 15: HRR Handshake
  329. $proxy->clear();
  330. $proxy->serverflags("-curves P-256");
  331. $proxy->start();
  332. checkhandshake($proxy, checkhandshake::HRR_HANDSHAKE,
  333. checkhandshake::DEFAULT_EXTENSIONS
  334. | checkhandshake::KEY_SHARE_HRR_EXTENSION,
  335. "HRR handshake test");
  336. #Test 16: Resumption handshake with HRR
  337. $proxy->clear();
  338. $proxy->clientflags("-sess_in ".$session);
  339. $proxy->serverflags("-curves P-256");
  340. $proxy->start();
  341. checkhandshake($proxy, checkhandshake::HRR_RESUME_HANDSHAKE,
  342. (checkhandshake::DEFAULT_EXTENSIONS
  343. | checkhandshake::KEY_SHARE_HRR_EXTENSION
  344. | checkhandshake::PSK_CLI_EXTENSION
  345. | checkhandshake::PSK_SRV_EXTENSION),
  346. "Resumption handshake with HRR test");
  347. #Test 17: Acceptable but non preferred key_share
  348. $proxy->clear();
  349. $proxy->clientflags("-curves P-256");
  350. $proxy->start();
  351. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  352. checkhandshake::DEFAULT_EXTENSIONS
  353. | checkhandshake::SUPPORTED_GROUPS_SRV_EXTENSION,
  354. "Acceptable but non preferred key_share");
  355. unlink $session;