70-test_sslmessages.t 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2021 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_sslmessages";
  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 TLS enabled"
  23. if alldisabled(available_protocols("tls"))
  24. || (!disabled("tls1_3") && disabled("tls1_2"));
  25. my $proxy = TLSProxy::Proxy->new(
  26. undef,
  27. cmdstr(app(["openssl"]), display => 1),
  28. srctop_file("apps", "server.pem"),
  29. (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
  30. );
  31. @handmessages = (
  32. [TLSProxy::Message::MT_CLIENT_HELLO,
  33. checkhandshake::ALL_HANDSHAKES],
  34. [TLSProxy::Message::MT_SERVER_HELLO,
  35. checkhandshake::ALL_HANDSHAKES],
  36. [TLSProxy::Message::MT_CERTIFICATE,
  37. checkhandshake::ALL_HANDSHAKES
  38. & ~checkhandshake::RESUME_HANDSHAKE],
  39. (disabled("ec") ? () :
  40. [TLSProxy::Message::MT_SERVER_KEY_EXCHANGE,
  41. checkhandshake::EC_HANDSHAKE]),
  42. [TLSProxy::Message::MT_CERTIFICATE_STATUS,
  43. checkhandshake::OCSP_HANDSHAKE],
  44. #ServerKeyExchange handshakes not currently supported by TLSProxy
  45. [TLSProxy::Message::MT_CERTIFICATE_REQUEST,
  46. checkhandshake::CLIENT_AUTH_HANDSHAKE],
  47. [TLSProxy::Message::MT_SERVER_HELLO_DONE,
  48. checkhandshake::ALL_HANDSHAKES
  49. & ~checkhandshake::RESUME_HANDSHAKE],
  50. [TLSProxy::Message::MT_CERTIFICATE,
  51. checkhandshake::CLIENT_AUTH_HANDSHAKE],
  52. [TLSProxy::Message::MT_CLIENT_KEY_EXCHANGE,
  53. checkhandshake::ALL_HANDSHAKES
  54. & ~checkhandshake::RESUME_HANDSHAKE],
  55. [TLSProxy::Message::MT_CERTIFICATE_VERIFY,
  56. checkhandshake::CLIENT_AUTH_HANDSHAKE],
  57. [TLSProxy::Message::MT_NEXT_PROTO,
  58. checkhandshake::NPN_HANDSHAKE],
  59. [TLSProxy::Message::MT_FINISHED,
  60. checkhandshake::ALL_HANDSHAKES],
  61. [TLSProxy::Message::MT_NEW_SESSION_TICKET,
  62. checkhandshake::ALL_HANDSHAKES
  63. & ~checkhandshake::RESUME_HANDSHAKE],
  64. [TLSProxy::Message::MT_FINISHED,
  65. checkhandshake::ALL_HANDSHAKES],
  66. [TLSProxy::Message::MT_CLIENT_HELLO,
  67. checkhandshake::RENEG_HANDSHAKE],
  68. [TLSProxy::Message::MT_SERVER_HELLO,
  69. checkhandshake::RENEG_HANDSHAKE],
  70. [TLSProxy::Message::MT_CERTIFICATE,
  71. checkhandshake::RENEG_HANDSHAKE],
  72. [TLSProxy::Message::MT_SERVER_HELLO_DONE,
  73. checkhandshake::RENEG_HANDSHAKE],
  74. [TLSProxy::Message::MT_CLIENT_KEY_EXCHANGE,
  75. checkhandshake::RENEG_HANDSHAKE],
  76. [TLSProxy::Message::MT_FINISHED,
  77. checkhandshake::RENEG_HANDSHAKE],
  78. [TLSProxy::Message::MT_NEW_SESSION_TICKET,
  79. checkhandshake::RENEG_HANDSHAKE],
  80. [TLSProxy::Message::MT_FINISHED,
  81. checkhandshake::RENEG_HANDSHAKE],
  82. [0, 0]
  83. );
  84. @extensions = (
  85. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SERVER_NAME,
  86. TLSProxy::Message::CLIENT,
  87. checkhandshake::SERVER_NAME_CLI_EXTENSION],
  88. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_STATUS_REQUEST,
  89. TLSProxy::Message::CLIENT,
  90. checkhandshake::STATUS_REQUEST_CLI_EXTENSION],
  91. (disabled("ec") ? () :
  92. [TLSProxy::Message::MT_CLIENT_HELLO,
  93. TLSProxy::Message::EXT_SUPPORTED_GROUPS,
  94. TLSProxy::Message::CLIENT,
  95. checkhandshake::DEFAULT_EXTENSIONS]),
  96. (disabled("ec") ? () :
  97. [TLSProxy::Message::MT_CLIENT_HELLO,
  98. TLSProxy::Message::EXT_EC_POINT_FORMATS,
  99. TLSProxy::Message::CLIENT,
  100. checkhandshake::DEFAULT_EXTENSIONS]),
  101. (disabled("tls1_2") ? () :
  102. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SIG_ALGS,
  103. TLSProxy::Message::CLIENT,
  104. checkhandshake::DEFAULT_EXTENSIONS]),
  105. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ALPN,
  106. TLSProxy::Message::CLIENT,
  107. checkhandshake::ALPN_CLI_EXTENSION],
  108. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SCT,
  109. TLSProxy::Message::CLIENT,
  110. checkhandshake::SCT_CLI_EXTENSION],
  111. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ENCRYPT_THEN_MAC,
  112. TLSProxy::Message::CLIENT,
  113. checkhandshake::DEFAULT_EXTENSIONS],
  114. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EXTENDED_MASTER_SECRET,
  115. TLSProxy::Message::CLIENT,
  116. checkhandshake::DEFAULT_EXTENSIONS],
  117. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SESSION_TICKET,
  118. TLSProxy::Message::CLIENT,
  119. checkhandshake::DEFAULT_EXTENSIONS],
  120. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_RENEGOTIATE,
  121. TLSProxy::Message::CLIENT,
  122. checkhandshake::RENEGOTIATE_CLI_EXTENSION],
  123. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_NPN,
  124. TLSProxy::Message::CLIENT,
  125. checkhandshake::NPN_CLI_EXTENSION],
  126. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SRP,
  127. TLSProxy::Message::CLIENT,
  128. checkhandshake::SRP_CLI_EXTENSION],
  129. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_RENEGOTIATE,
  130. TLSProxy::Message::SERVER,
  131. checkhandshake::DEFAULT_EXTENSIONS],
  132. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_ENCRYPT_THEN_MAC,
  133. TLSProxy::Message::SERVER,
  134. checkhandshake::DEFAULT_EXTENSIONS],
  135. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_EXTENDED_MASTER_SECRET,
  136. TLSProxy::Message::SERVER,
  137. checkhandshake::DEFAULT_EXTENSIONS],
  138. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_SESSION_TICKET,
  139. TLSProxy::Message::SERVER,
  140. checkhandshake::SESSION_TICKET_SRV_EXTENSION],
  141. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_SERVER_NAME,
  142. TLSProxy::Message::SERVER,
  143. checkhandshake::SERVER_NAME_SRV_EXTENSION],
  144. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_STATUS_REQUEST,
  145. TLSProxy::Message::SERVER,
  146. checkhandshake::STATUS_REQUEST_SRV_EXTENSION],
  147. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_ALPN,
  148. TLSProxy::Message::SERVER,
  149. checkhandshake::ALPN_SRV_EXTENSION],
  150. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_SCT,
  151. TLSProxy::Message::SERVER,
  152. checkhandshake::SCT_SRV_EXTENSION],
  153. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_NPN,
  154. TLSProxy::Message::SERVER,
  155. checkhandshake::NPN_SRV_EXTENSION],
  156. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_EC_POINT_FORMATS,
  157. TLSProxy::Message::SERVER,
  158. checkhandshake::EC_POINT_FORMAT_SRV_EXTENSION],
  159. [0,0,0,0]
  160. );
  161. #Test 1: Check we get all the right messages for a default handshake
  162. (undef, my $session) = tempfile();
  163. $proxy->serverconnects(2);
  164. $proxy->clientflags("-no_tls1_3 -sess_out ".$session);
  165. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  166. plan tests => 21;
  167. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  168. checkhandshake::DEFAULT_EXTENSIONS,
  169. "Default handshake test");
  170. #Test 2: Resumption handshake
  171. $proxy->clearClient();
  172. $proxy->clientflags("-no_tls1_3 -sess_in ".$session);
  173. $proxy->clientstart();
  174. checkhandshake($proxy, checkhandshake::RESUME_HANDSHAKE,
  175. checkhandshake::DEFAULT_EXTENSIONS
  176. & ~checkhandshake::SESSION_TICKET_SRV_EXTENSION,
  177. "Resumption handshake test");
  178. unlink $session;
  179. SKIP: {
  180. skip "No OCSP support in this OpenSSL build", 3
  181. if disabled("ocsp");
  182. #Test 3: A status_request handshake (client request only)
  183. $proxy->clear();
  184. $proxy->clientflags("-no_tls1_3 -status");
  185. $proxy->start();
  186. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  187. checkhandshake::DEFAULT_EXTENSIONS
  188. | checkhandshake::STATUS_REQUEST_CLI_EXTENSION,
  189. "status_request handshake test (client)");
  190. #Test 4: A status_request handshake (server support only)
  191. $proxy->clear();
  192. $proxy->clientflags("-no_tls1_3");
  193. $proxy->serverflags("-status_file "
  194. .srctop_file("test", "recipes", "ocsp-response.der"));
  195. $proxy->start();
  196. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  197. checkhandshake::DEFAULT_EXTENSIONS,
  198. "status_request handshake test (server)");
  199. #Test 5: A status_request handshake (client and server)
  200. $proxy->clear();
  201. $proxy->clientflags("-no_tls1_3 -status");
  202. $proxy->serverflags("-status_file "
  203. .srctop_file("test", "recipes", "ocsp-response.der"));
  204. $proxy->start();
  205. checkhandshake($proxy, checkhandshake::OCSP_HANDSHAKE,
  206. checkhandshake::DEFAULT_EXTENSIONS
  207. | checkhandshake::STATUS_REQUEST_CLI_EXTENSION
  208. | checkhandshake::STATUS_REQUEST_SRV_EXTENSION,
  209. "status_request handshake test");
  210. }
  211. #Test 6: A client auth handshake
  212. $proxy->clear();
  213. $proxy->clientflags("-no_tls1_3 -cert ".srctop_file("apps", "server.pem"));
  214. $proxy->serverflags("-Verify 5");
  215. $proxy->start();
  216. checkhandshake($proxy, checkhandshake::CLIENT_AUTH_HANDSHAKE,
  217. checkhandshake::DEFAULT_EXTENSIONS,
  218. "Client auth handshake test");
  219. #Test 7: A handshake with a renegotiation
  220. $proxy->clear();
  221. $proxy->clientflags("-no_tls1_3");
  222. $proxy->serverflags("-client_renegotiation");
  223. $proxy->reneg(1);
  224. $proxy->start();
  225. checkhandshake($proxy, checkhandshake::RENEG_HANDSHAKE,
  226. checkhandshake::DEFAULT_EXTENSIONS,
  227. "Renegotiation handshake test");
  228. #Test 8: Server name handshake (no client request)
  229. $proxy->clear();
  230. $proxy->clientflags("-no_tls1_3 -noservername");
  231. $proxy->start();
  232. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  233. checkhandshake::DEFAULT_EXTENSIONS
  234. & ~checkhandshake::SERVER_NAME_CLI_EXTENSION,
  235. "Server name handshake test (client)");
  236. #Test 9: Server name handshake (server support only)
  237. $proxy->clear();
  238. $proxy->clientflags("-no_tls1_3 -noservername");
  239. $proxy->serverflags("-servername testhost");
  240. $proxy->start();
  241. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  242. checkhandshake::DEFAULT_EXTENSIONS
  243. & ~checkhandshake::SERVER_NAME_CLI_EXTENSION,
  244. "Server name handshake test (server)");
  245. #Test 10: Server name handshake (client and server)
  246. $proxy->clear();
  247. $proxy->clientflags("-no_tls1_3 -servername testhost");
  248. $proxy->serverflags("-servername testhost");
  249. $proxy->start();
  250. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  251. checkhandshake::DEFAULT_EXTENSIONS
  252. | checkhandshake::SERVER_NAME_SRV_EXTENSION,
  253. "Server name handshake test");
  254. #Test 11: ALPN handshake (client request only)
  255. $proxy->clear();
  256. $proxy->clientflags("-no_tls1_3 -alpn test");
  257. $proxy->start();
  258. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  259. checkhandshake::DEFAULT_EXTENSIONS
  260. | checkhandshake::ALPN_CLI_EXTENSION,
  261. "ALPN handshake test (client)");
  262. #Test 12: ALPN handshake (server support only)
  263. $proxy->clear();
  264. $proxy->clientflags("-no_tls1_3");
  265. $proxy->serverflags("-alpn test");
  266. $proxy->start();
  267. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  268. checkhandshake::DEFAULT_EXTENSIONS,
  269. "ALPN handshake test (server)");
  270. #Test 13: ALPN handshake (client and server)
  271. $proxy->clear();
  272. $proxy->clientflags("-no_tls1_3 -alpn test");
  273. $proxy->serverflags("-alpn test");
  274. $proxy->start();
  275. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  276. checkhandshake::DEFAULT_EXTENSIONS
  277. | checkhandshake::ALPN_CLI_EXTENSION
  278. | checkhandshake::ALPN_SRV_EXTENSION,
  279. "ALPN handshake test");
  280. SKIP: {
  281. skip "No CT, EC or OCSP support in this OpenSSL build", 1
  282. if disabled("ct") || disabled("ec") || disabled("ocsp");
  283. #Test 14: SCT handshake (client request only)
  284. $proxy->clear();
  285. #Note: -ct also sends status_request
  286. $proxy->clientflags("-no_tls1_3 -ct");
  287. $proxy->serverflags("-status_file "
  288. .srctop_file("test", "recipes", "ocsp-response.der"));
  289. $proxy->start();
  290. checkhandshake($proxy, checkhandshake::OCSP_HANDSHAKE,
  291. checkhandshake::DEFAULT_EXTENSIONS
  292. | checkhandshake::SCT_CLI_EXTENSION
  293. | checkhandshake::STATUS_REQUEST_CLI_EXTENSION
  294. | checkhandshake::STATUS_REQUEST_SRV_EXTENSION,
  295. "SCT handshake test (client)");
  296. }
  297. SKIP: {
  298. skip "No OCSP support in this OpenSSL build", 1
  299. if disabled("ocsp");
  300. #Test 15: SCT handshake (server support only)
  301. $proxy->clear();
  302. #Note: -ct also sends status_request
  303. $proxy->clientflags("-no_tls1_3");
  304. $proxy->serverflags("-status_file "
  305. .srctop_file("test", "recipes", "ocsp-response.der"));
  306. $proxy->start();
  307. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  308. checkhandshake::DEFAULT_EXTENSIONS,
  309. "SCT handshake test (server)");
  310. }
  311. SKIP: {
  312. skip "No CT, EC or OCSP support in this OpenSSL build", 1
  313. if disabled("ct") || disabled("ec") || disabled("ocsp");
  314. #Test 16: SCT handshake (client and server)
  315. #There is no built-in server side support for this so we are actually also
  316. #testing custom extensions here
  317. $proxy->clear();
  318. #Note: -ct also sends status_request
  319. $proxy->clientflags("-no_tls1_3 -ct");
  320. $proxy->serverflags("-status_file "
  321. .srctop_file("test", "recipes", "ocsp-response.der")
  322. ." -serverinfo ".srctop_file("test", "serverinfo.pem"));
  323. $proxy->start();
  324. checkhandshake($proxy, checkhandshake::OCSP_HANDSHAKE,
  325. checkhandshake::DEFAULT_EXTENSIONS
  326. | checkhandshake::SCT_CLI_EXTENSION
  327. | checkhandshake::SCT_SRV_EXTENSION
  328. | checkhandshake::STATUS_REQUEST_CLI_EXTENSION
  329. | checkhandshake::STATUS_REQUEST_SRV_EXTENSION,
  330. "SCT handshake test");
  331. }
  332. SKIP: {
  333. skip "No NPN support in this OpenSSL build", 3
  334. if disabled("nextprotoneg");
  335. #Test 17: NPN handshake (client request only)
  336. $proxy->clear();
  337. $proxy->clientflags("-no_tls1_3 -nextprotoneg test");
  338. $proxy->start();
  339. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  340. checkhandshake::DEFAULT_EXTENSIONS
  341. | checkhandshake::NPN_CLI_EXTENSION,
  342. "NPN handshake test (client)");
  343. #Test 18: NPN handshake (server support only)
  344. $proxy->clear();
  345. $proxy->clientflags("-no_tls1_3");
  346. $proxy->serverflags("-nextprotoneg test");
  347. $proxy->start();
  348. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  349. checkhandshake::DEFAULT_EXTENSIONS,
  350. "NPN handshake test (server)");
  351. #Test 19: NPN handshake (client and server)
  352. $proxy->clear();
  353. $proxy->clientflags("-no_tls1_3 -nextprotoneg test");
  354. $proxy->serverflags("-nextprotoneg test");
  355. $proxy->start();
  356. checkhandshake($proxy, checkhandshake::NPN_HANDSHAKE,
  357. checkhandshake::DEFAULT_EXTENSIONS
  358. | checkhandshake::NPN_CLI_EXTENSION
  359. | checkhandshake::NPN_SRV_EXTENSION,
  360. "NPN handshake test");
  361. }
  362. SKIP: {
  363. skip "No SRP support in this OpenSSL build", 1
  364. if disabled("srp");
  365. #Test 20: SRP extension
  366. #Note: We are not actually going to perform an SRP handshake (TLSProxy
  367. #does not support it). However it is sufficient for us to check that the
  368. #SRP extension gets added on the client side. There is no SRP extension
  369. #generated on the server side anyway.
  370. $proxy->clear();
  371. $proxy->clientflags("-no_tls1_3 -srpuser user -srppass pass:pass");
  372. $proxy->start();
  373. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  374. checkhandshake::DEFAULT_EXTENSIONS
  375. | checkhandshake::SRP_CLI_EXTENSION,
  376. "SRP extension test");
  377. }
  378. #Test 21: EC handshake
  379. SKIP: {
  380. skip "No EC support in this OpenSSL build", 1 if disabled("ec");
  381. $proxy->clear();
  382. $proxy->clientflags("-no_tls1_3");
  383. $proxy->serverflags("-no_tls1_3");
  384. $proxy->ciphers("ECDHE-RSA-AES128-SHA");
  385. $proxy->start();
  386. checkhandshake($proxy, checkhandshake::EC_HANDSHAKE,
  387. checkhandshake::DEFAULT_EXTENSIONS
  388. | checkhandshake::EC_POINT_FORMAT_SRV_EXTENSION,
  389. "EC handshake test");
  390. }