70-test_sslmessages.t 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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_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. $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
  26. my $proxy = TLSProxy::Proxy->new(
  27. undef,
  28. cmdstr(app(["openssl"]), display => 1),
  29. srctop_file("apps", "server.pem"),
  30. (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
  31. );
  32. @handmessages = (
  33. [TLSProxy::Message::MT_CLIENT_HELLO,
  34. checkhandshake::ALL_HANDSHAKES],
  35. [TLSProxy::Message::MT_SERVER_HELLO,
  36. checkhandshake::ALL_HANDSHAKES],
  37. [TLSProxy::Message::MT_CERTIFICATE,
  38. checkhandshake::ALL_HANDSHAKES
  39. & ~checkhandshake::RESUME_HANDSHAKE],
  40. (disabled("ec") ? () :
  41. [TLSProxy::Message::MT_SERVER_KEY_EXCHANGE,
  42. checkhandshake::EC_HANDSHAKE]),
  43. [TLSProxy::Message::MT_CERTIFICATE_STATUS,
  44. checkhandshake::OCSP_HANDSHAKE],
  45. #ServerKeyExchange handshakes not currently supported by TLSProxy
  46. [TLSProxy::Message::MT_CERTIFICATE_REQUEST,
  47. checkhandshake::CLIENT_AUTH_HANDSHAKE],
  48. [TLSProxy::Message::MT_SERVER_HELLO_DONE,
  49. checkhandshake::ALL_HANDSHAKES
  50. & ~checkhandshake::RESUME_HANDSHAKE],
  51. [TLSProxy::Message::MT_CERTIFICATE,
  52. checkhandshake::CLIENT_AUTH_HANDSHAKE],
  53. [TLSProxy::Message::MT_CLIENT_KEY_EXCHANGE,
  54. checkhandshake::ALL_HANDSHAKES
  55. & ~checkhandshake::RESUME_HANDSHAKE],
  56. [TLSProxy::Message::MT_CERTIFICATE_VERIFY,
  57. checkhandshake::CLIENT_AUTH_HANDSHAKE],
  58. [TLSProxy::Message::MT_NEXT_PROTO,
  59. checkhandshake::NPN_HANDSHAKE],
  60. [TLSProxy::Message::MT_FINISHED,
  61. checkhandshake::ALL_HANDSHAKES],
  62. [TLSProxy::Message::MT_NEW_SESSION_TICKET,
  63. checkhandshake::ALL_HANDSHAKES
  64. & ~checkhandshake::RESUME_HANDSHAKE],
  65. [TLSProxy::Message::MT_FINISHED,
  66. checkhandshake::ALL_HANDSHAKES],
  67. [TLSProxy::Message::MT_CLIENT_HELLO,
  68. checkhandshake::RENEG_HANDSHAKE],
  69. [TLSProxy::Message::MT_SERVER_HELLO,
  70. checkhandshake::RENEG_HANDSHAKE],
  71. [TLSProxy::Message::MT_CERTIFICATE,
  72. checkhandshake::RENEG_HANDSHAKE],
  73. [TLSProxy::Message::MT_SERVER_HELLO_DONE,
  74. checkhandshake::RENEG_HANDSHAKE],
  75. [TLSProxy::Message::MT_CLIENT_KEY_EXCHANGE,
  76. checkhandshake::RENEG_HANDSHAKE],
  77. [TLSProxy::Message::MT_FINISHED,
  78. checkhandshake::RENEG_HANDSHAKE],
  79. [TLSProxy::Message::MT_NEW_SESSION_TICKET,
  80. checkhandshake::RENEG_HANDSHAKE],
  81. [TLSProxy::Message::MT_FINISHED,
  82. checkhandshake::RENEG_HANDSHAKE],
  83. [0, 0]
  84. );
  85. @extensions = (
  86. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SERVER_NAME,
  87. TLSProxy::Message::CLIENT,
  88. checkhandshake::SERVER_NAME_CLI_EXTENSION],
  89. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_STATUS_REQUEST,
  90. TLSProxy::Message::CLIENT,
  91. checkhandshake::STATUS_REQUEST_CLI_EXTENSION],
  92. (disabled("ec") ? () :
  93. [TLSProxy::Message::MT_CLIENT_HELLO,
  94. TLSProxy::Message::EXT_SUPPORTED_GROUPS,
  95. TLSProxy::Message::CLIENT,
  96. checkhandshake::DEFAULT_EXTENSIONS]),
  97. (disabled("ec") ? () :
  98. [TLSProxy::Message::MT_CLIENT_HELLO,
  99. TLSProxy::Message::EXT_EC_POINT_FORMATS,
  100. TLSProxy::Message::CLIENT,
  101. checkhandshake::DEFAULT_EXTENSIONS]),
  102. (disabled("tls1_2") ? () :
  103. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SIG_ALGS,
  104. TLSProxy::Message::CLIENT,
  105. checkhandshake::DEFAULT_EXTENSIONS]),
  106. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ALPN,
  107. TLSProxy::Message::CLIENT,
  108. checkhandshake::ALPN_CLI_EXTENSION],
  109. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SCT,
  110. TLSProxy::Message::CLIENT,
  111. checkhandshake::SCT_CLI_EXTENSION],
  112. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ENCRYPT_THEN_MAC,
  113. TLSProxy::Message::CLIENT,
  114. checkhandshake::DEFAULT_EXTENSIONS],
  115. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EXTENDED_MASTER_SECRET,
  116. TLSProxy::Message::CLIENT,
  117. checkhandshake::DEFAULT_EXTENSIONS],
  118. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SESSION_TICKET,
  119. TLSProxy::Message::CLIENT,
  120. checkhandshake::DEFAULT_EXTENSIONS],
  121. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_RENEGOTIATE,
  122. TLSProxy::Message::CLIENT,
  123. checkhandshake::RENEGOTIATE_CLI_EXTENSION],
  124. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_NPN,
  125. TLSProxy::Message::CLIENT,
  126. checkhandshake::NPN_CLI_EXTENSION],
  127. [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SRP,
  128. TLSProxy::Message::CLIENT,
  129. checkhandshake::SRP_CLI_EXTENSION],
  130. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_RENEGOTIATE,
  131. TLSProxy::Message::SERVER,
  132. checkhandshake::DEFAULT_EXTENSIONS],
  133. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_ENCRYPT_THEN_MAC,
  134. TLSProxy::Message::SERVER,
  135. checkhandshake::DEFAULT_EXTENSIONS],
  136. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_EXTENDED_MASTER_SECRET,
  137. TLSProxy::Message::SERVER,
  138. checkhandshake::DEFAULT_EXTENSIONS],
  139. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_SESSION_TICKET,
  140. TLSProxy::Message::SERVER,
  141. checkhandshake::SESSION_TICKET_SRV_EXTENSION],
  142. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_SERVER_NAME,
  143. TLSProxy::Message::SERVER,
  144. checkhandshake::SERVER_NAME_SRV_EXTENSION],
  145. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_STATUS_REQUEST,
  146. TLSProxy::Message::SERVER,
  147. checkhandshake::STATUS_REQUEST_SRV_EXTENSION],
  148. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_ALPN,
  149. TLSProxy::Message::SERVER,
  150. checkhandshake::ALPN_SRV_EXTENSION],
  151. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_SCT,
  152. TLSProxy::Message::SERVER,
  153. checkhandshake::SCT_SRV_EXTENSION],
  154. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_NPN,
  155. TLSProxy::Message::SERVER,
  156. checkhandshake::NPN_SRV_EXTENSION],
  157. [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_EC_POINT_FORMATS,
  158. TLSProxy::Message::SERVER,
  159. checkhandshake::EC_POINT_FORMAT_SRV_EXTENSION],
  160. [0,0,0,0]
  161. );
  162. #Test 1: Check we get all the right messages for a default handshake
  163. (undef, my $session) = tempfile();
  164. $proxy->serverconnects(2);
  165. $proxy->clientflags("-no_tls1_3 -sess_out ".$session);
  166. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  167. plan tests => 21;
  168. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  169. checkhandshake::DEFAULT_EXTENSIONS,
  170. "Default handshake test");
  171. #Test 2: Resumption handshake
  172. $proxy->clearClient();
  173. $proxy->clientflags("-no_tls1_3 -sess_in ".$session);
  174. $proxy->clientstart();
  175. checkhandshake($proxy, checkhandshake::RESUME_HANDSHAKE,
  176. checkhandshake::DEFAULT_EXTENSIONS
  177. & ~checkhandshake::SESSION_TICKET_SRV_EXTENSION,
  178. "Resumption handshake test");
  179. unlink $session;
  180. SKIP: {
  181. skip "No OCSP support in this OpenSSL build", 3
  182. if disabled("ocsp");
  183. #Test 3: A status_request handshake (client request only)
  184. $proxy->clear();
  185. $proxy->clientflags("-no_tls1_3 -status");
  186. $proxy->start();
  187. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  188. checkhandshake::DEFAULT_EXTENSIONS
  189. | checkhandshake::STATUS_REQUEST_CLI_EXTENSION,
  190. "status_request handshake test (client)");
  191. #Test 4: A status_request handshake (server support only)
  192. $proxy->clear();
  193. $proxy->clientflags("-no_tls1_3");
  194. $proxy->serverflags("-status_file "
  195. .srctop_file("test", "recipes", "ocsp-response.der"));
  196. $proxy->start();
  197. checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
  198. checkhandshake::DEFAULT_EXTENSIONS,
  199. "status_request handshake test (server)");
  200. #Test 5: A status_request handshake (client and server)
  201. $proxy->clear();
  202. $proxy->clientflags("-no_tls1_3 -status");
  203. $proxy->serverflags("-status_file "
  204. .srctop_file("test", "recipes", "ocsp-response.der"));
  205. $proxy->start();
  206. checkhandshake($proxy, checkhandshake::OCSP_HANDSHAKE,
  207. checkhandshake::DEFAULT_EXTENSIONS
  208. | checkhandshake::STATUS_REQUEST_CLI_EXTENSION
  209. | checkhandshake::STATUS_REQUEST_SRV_EXTENSION,
  210. "status_request handshake test");
  211. }
  212. #Test 6: A client auth handshake
  213. $proxy->clear();
  214. $proxy->clientflags("-no_tls1_3 -cert ".srctop_file("apps", "server.pem"));
  215. $proxy->serverflags("-Verify 5");
  216. $proxy->start();
  217. checkhandshake($proxy, checkhandshake::CLIENT_AUTH_HANDSHAKE,
  218. checkhandshake::DEFAULT_EXTENSIONS,
  219. "Client auth handshake test");
  220. #Test 7: A handshake with a renegotiation
  221. $proxy->clear();
  222. $proxy->clientflags("-no_tls1_3");
  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. }