2
0

70-test_key_share.t 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2023 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 bldtop_dir/;
  10. use OpenSSL::Test::Utils;
  11. use TLSProxy::Proxy;
  12. use File::Temp qw(tempfile);
  13. use constant {
  14. LOOK_ONLY => 0,
  15. EMPTY_EXTENSION => 1,
  16. MISSING_EXTENSION => 2,
  17. NO_ACCEPTABLE_KEY_SHARES => 3,
  18. NON_PREFERRED_KEY_SHARE => 4,
  19. ACCEPTABLE_AT_END => 5,
  20. NOT_IN_SUPPORTED_GROUPS => 6,
  21. GROUP_ID_TOO_SHORT => 7,
  22. KEX_LEN_MISMATCH => 8,
  23. ZERO_LEN_KEX_DATA => 9,
  24. TRAILING_DATA => 10,
  25. SELECT_X25519 => 11,
  26. NO_KEY_SHARES_IN_HRR => 12,
  27. NON_TLS1_3_KEY_SHARE => 13
  28. };
  29. use constant {
  30. CLIENT_TO_SERVER => 1,
  31. SERVER_TO_CLIENT => 2
  32. };
  33. use constant {
  34. X25519 => 0x1d,
  35. P_256 => 0x17,
  36. FFDHE2048 => 0x0100,
  37. FFDHE3072 => 0x0101
  38. };
  39. my $testtype;
  40. my $direction;
  41. my $selectedgroupid;
  42. my $test_name = "test_key_share";
  43. setup($test_name);
  44. plan skip_all => "TLSProxy isn't usable on $^O"
  45. if $^O =~ /^(VMS)$/;
  46. plan skip_all => "$test_name needs the dynamic engine feature enabled"
  47. if disabled("engine") || disabled("dynamic-engine");
  48. plan skip_all => "$test_name needs the sock feature enabled"
  49. if disabled("sock");
  50. plan skip_all => "$test_name needs TLS1.3 enabled"
  51. if disabled("tls1_3");
  52. plan skip_all => "$test_name needs EC or DH enabled"
  53. if disabled("ec") && disabled("dh");
  54. my $proxy = TLSProxy::Proxy->new(
  55. undef,
  56. cmdstr(app(["openssl"]), display => 1),
  57. srctop_file("apps", "server.pem"),
  58. (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
  59. );
  60. #We assume that test_ssl_new and friends will test the happy path for this,
  61. #so we concentrate on the less common scenarios
  62. #Test 1: An empty key_shares extension should succeed after a HelloRetryRequest
  63. $testtype = EMPTY_EXTENSION;
  64. $direction = CLIENT_TO_SERVER;
  65. $proxy->filter(\&modify_key_shares_filter);
  66. if (disabled("ec")) {
  67. $proxy->serverflags("-groups ffdhe3072");
  68. } else {
  69. $proxy->serverflags("-groups P-384");
  70. }
  71. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  72. plan tests => 23;
  73. ok(TLSProxy::Message->success(), "Success after HRR");
  74. #Test 2: The server sending an HRR requesting a group the client already sent
  75. # should fail
  76. $proxy->clear();
  77. $proxy->start();
  78. ok(TLSProxy::Message->fail(), "Server asks for group already provided");
  79. #Test 3: A missing key_shares extension should not succeed
  80. $proxy->clear();
  81. $testtype = MISSING_EXTENSION;
  82. $proxy->start();
  83. ok(TLSProxy::Message->fail(), "Missing key_shares extension");
  84. #Test 4: No initial acceptable key_shares should succeed after a
  85. # HelloRetryRequest
  86. $proxy->clear();
  87. $proxy->filter(undef);
  88. if (disabled("ec")) {
  89. $proxy->serverflags("-groups ffdhe3072");
  90. } else {
  91. $proxy->serverflags("-groups P-256");
  92. }
  93. $proxy->start();
  94. ok(TLSProxy::Message->success(), "No initial acceptable key_shares");
  95. #Test 5: No acceptable key_shares and no shared groups should fail
  96. $proxy->clear();
  97. $proxy->filter(undef);
  98. if (disabled("ec")) {
  99. $proxy->serverflags("-groups ffdhe2048");
  100. } else {
  101. $proxy->serverflags("-groups P-256");
  102. }
  103. if (disabled("ec")) {
  104. $proxy->clientflags("-groups ffdhe3072");
  105. } else {
  106. $proxy->clientflags("-groups P-384");
  107. }
  108. $proxy->start();
  109. ok(TLSProxy::Message->fail(), "No acceptable key_shares");
  110. #Test 6: A non preferred but acceptable key_share should succeed
  111. $proxy->clear();
  112. $proxy->clientflags("-curves P-256");
  113. if (disabled("ec")) {
  114. $proxy->clientflags("-groups ffdhe3072");
  115. } else {
  116. $proxy->clientflags("-groups P-256");
  117. }
  118. $proxy->start();
  119. ok(TLSProxy::Message->success(), "Non preferred key_share");
  120. $proxy->filter(\&modify_key_shares_filter);
  121. SKIP: {
  122. skip "No ec support in this OpenSSL build", 1 if disabled("ec");
  123. #Test 7: An acceptable key_share after a list of non-acceptable ones should
  124. #succeed
  125. $proxy->clear();
  126. $testtype = ACCEPTABLE_AT_END;
  127. $proxy->start();
  128. ok(TLSProxy::Message->success(), "Acceptable key_share at end of list");
  129. }
  130. #Test 8: An acceptable key_share but for a group not in supported_groups should
  131. #fail
  132. $proxy->clear();
  133. $testtype = NOT_IN_SUPPORTED_GROUPS;
  134. $proxy->start();
  135. ok(TLSProxy::Message->fail(), "Acceptable key_share not in supported_groups");
  136. #Test 9: Too short group_id should fail
  137. $proxy->clear();
  138. $testtype = GROUP_ID_TOO_SHORT;
  139. $proxy->start();
  140. ok(TLSProxy::Message->fail(), "Group id too short");
  141. #Test 10: key_exchange length mismatch should fail
  142. $proxy->clear();
  143. $testtype = KEX_LEN_MISMATCH;
  144. $proxy->start();
  145. ok(TLSProxy::Message->fail(), "key_exchange length mismatch");
  146. #Test 11: Zero length key_exchange should fail
  147. $proxy->clear();
  148. $testtype = ZERO_LEN_KEX_DATA;
  149. $proxy->start();
  150. ok(TLSProxy::Message->fail(), "zero length key_exchange data");
  151. #Test 12: Trailing data on key_share list should fail
  152. $proxy->clear();
  153. $testtype = TRAILING_DATA;
  154. $proxy->start();
  155. ok(TLSProxy::Message->fail(), "key_share list trailing data");
  156. #Test 13: Multiple acceptable key_shares - we choose the first one
  157. $proxy->clear();
  158. $direction = SERVER_TO_CLIENT;
  159. $testtype = LOOK_ONLY;
  160. $selectedgroupid = 0;
  161. if (disabled("ec")) {
  162. $proxy->clientflags("-groups ffdhe3072:ffdhe2048");
  163. } else {
  164. $proxy->clientflags("-groups P-256:P-384");
  165. }
  166. $proxy->start();
  167. if (disabled("ec")) {
  168. ok(TLSProxy::Message->success() && ($selectedgroupid == FFDHE3072),
  169. "Multiple acceptable key_shares");
  170. } else {
  171. ok(TLSProxy::Message->success() && ($selectedgroupid == P_256),
  172. "Multiple acceptable key_shares");
  173. }
  174. #Test 14: Multiple acceptable key_shares - we choose the first one (part 2)
  175. $proxy->clear();
  176. if (disabled("ecx")) {
  177. $proxy->clientflags("-curves ffdhe2048:ffdhe3072");
  178. } else {
  179. $proxy->clientflags("-curves X25519:P-256");
  180. }
  181. $proxy->start();
  182. if (disabled("ecx")) {
  183. ok(TLSProxy::Message->success() && ($selectedgroupid == FFDHE2048),
  184. "Multiple acceptable key_shares (part 2)");
  185. } else {
  186. ok(TLSProxy::Message->success() && ($selectedgroupid == X25519),
  187. "Multiple acceptable key_shares (part 2)");
  188. }
  189. #Test 15: Server sends key_share that wasn't offered should fail
  190. $proxy->clear();
  191. $testtype = SELECT_X25519;
  192. if (disabled("ecx")) {
  193. $proxy->clientflags("-groups ffdhe3072");
  194. } else {
  195. $proxy->clientflags("-groups P-256");
  196. }
  197. $proxy->start();
  198. ok(TLSProxy::Message->fail(), "Non offered key_share");
  199. #Test 16: Too short group_id in ServerHello should fail
  200. $proxy->clear();
  201. $testtype = GROUP_ID_TOO_SHORT;
  202. $proxy->start();
  203. ok(TLSProxy::Message->fail(), "Group id too short in ServerHello");
  204. #Test 17: key_exchange length mismatch in ServerHello should fail
  205. $proxy->clear();
  206. $testtype = KEX_LEN_MISMATCH;
  207. $proxy->start();
  208. ok(TLSProxy::Message->fail(), "key_exchange length mismatch in ServerHello");
  209. #Test 18: Zero length key_exchange in ServerHello should fail
  210. $proxy->clear();
  211. $testtype = ZERO_LEN_KEX_DATA;
  212. $proxy->start();
  213. ok(TLSProxy::Message->fail(), "zero length key_exchange data in ServerHello");
  214. #Test 19: Trailing data on key_share in ServerHello should fail
  215. $proxy->clear();
  216. $testtype = TRAILING_DATA;
  217. $proxy->start();
  218. ok(TLSProxy::Message->fail(), "key_share trailing data in ServerHello");
  219. SKIP: {
  220. skip "No TLSv1.2 support in this OpenSSL build", 2 if disabled("tls1_2");
  221. #Test 20: key_share should not be sent if the client is not capable of
  222. # negotiating TLSv1.3
  223. $proxy->clear();
  224. $proxy->filter(undef);
  225. $proxy->clientflags("-no_tls1_3");
  226. $proxy->start();
  227. my $clienthello = $proxy->message_list->[0];
  228. ok(TLSProxy::Message->success()
  229. && !defined $clienthello->extension_data->{TLSProxy::Message::EXT_KEY_SHARE},
  230. "No key_share for TLS<=1.2 client");
  231. $proxy->filter(\&modify_key_shares_filter);
  232. #Test 21: A server not capable of negotiating TLSv1.3 should not attempt to
  233. # process a key_share
  234. $proxy->clear();
  235. $direction = CLIENT_TO_SERVER;
  236. $testtype = NO_ACCEPTABLE_KEY_SHARES;
  237. $proxy->serverflags("-no_tls1_3");
  238. $proxy->start();
  239. ok(TLSProxy::Message->success(), "Ignore key_share for TLS<=1.2 server");
  240. }
  241. #Test 22: The server sending an HRR but not requesting a new key_share should
  242. # fail
  243. $proxy->clear();
  244. $direction = SERVER_TO_CLIENT;
  245. $testtype = NO_KEY_SHARES_IN_HRR;
  246. if (disabled("ecx")) {
  247. $proxy->serverflags("-groups ffdhe2048");
  248. } else {
  249. $proxy->serverflags("-groups X25519");
  250. }
  251. $proxy->start();
  252. ok(TLSProxy::Message->fail(), "Server sends HRR with no key_shares");
  253. SKIP: {
  254. skip "No EC support in this OpenSSL build", 1 if disabled("ec");
  255. #Test 23: Trailing data on key_share in ServerHello should fail
  256. $proxy->clear();
  257. $direction = CLIENT_TO_SERVER;
  258. if (disabled("ecx")) {
  259. $proxy->clientflags("-groups secp192r1:P-256:P-384");
  260. } else {
  261. $proxy->clientflags("-groups secp192r1:P-256:X25519");
  262. }
  263. $proxy->ciphers("AES128-SHA:\@SECLEVEL=0");
  264. $testtype = NON_TLS1_3_KEY_SHARE;
  265. $proxy->start();
  266. my $ishrr = defined ${$proxy->message_list}[2]
  267. &&(${$proxy->message_list}[0]->mt == TLSProxy::Message::MT_CLIENT_HELLO)
  268. && (${$proxy->message_list}[2]->mt == TLSProxy::Message::MT_CLIENT_HELLO);
  269. ok(TLSProxy::Message->success() && $ishrr,
  270. "Client sends a key_share for a Non TLSv1.3 group");
  271. }
  272. sub modify_key_shares_filter
  273. {
  274. my $proxy = shift;
  275. # We're only interested in the initial ClientHello/SererHello/HRR
  276. if (($direction == CLIENT_TO_SERVER && $proxy->flight != 0
  277. && ($proxy->flight != 1 || $testtype != NO_KEY_SHARES_IN_HRR))
  278. || ($direction == SERVER_TO_CLIENT && $proxy->flight != 1)) {
  279. return;
  280. }
  281. foreach my $message (@{$proxy->message_list}) {
  282. if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO
  283. && $direction == CLIENT_TO_SERVER) {
  284. my $ext;
  285. my $suppgroups;
  286. if ($testtype != NON_TLS1_3_KEY_SHARE) {
  287. #Setup supported groups to include some unrecognised groups
  288. if (disabled("ecx")) {
  289. $suppgroups = pack "C8",
  290. 0x00, 0x06, #List Length
  291. 0xff, 0xfe, #Non existing group 1
  292. 0xff, 0xff, #Non existing group 2
  293. 0x00, 0x17; #P-256
  294. } else {
  295. $suppgroups = pack "C8",
  296. 0x00, 0x06, #List Length
  297. 0xff, 0xfe, #Non existing group 1
  298. 0xff, 0xff, #Non existing group 2
  299. 0x00, 0x1d; #X25519
  300. }
  301. } else {
  302. if (disabled("ecx")) {
  303. $suppgroups = pack "C6",
  304. 0x00, 0x04, #List Length
  305. 0x00, 0x13,
  306. 0x00, 0x18; #P-384
  307. } else {
  308. $suppgroups = pack "C6",
  309. 0x00, 0x04, #List Length
  310. 0x00, 0x13,
  311. 0x00, 0x1d; #X25519
  312. }
  313. }
  314. if ($testtype == EMPTY_EXTENSION) {
  315. $ext = pack "C2",
  316. 0x00, 0x00;
  317. } elsif ($testtype == NO_ACCEPTABLE_KEY_SHARES) {
  318. $ext = pack "C12",
  319. 0x00, 0x0a, #List Length
  320. 0xff, 0xfe, #Non existing group 1
  321. 0x00, 0x01, 0xff, #key_exchange data
  322. 0xff, 0xff, #Non existing group 2
  323. 0x00, 0x01, 0xff; #key_exchange data
  324. } elsif ($testtype == ACCEPTABLE_AT_END) {
  325. if (disabled("ecx")) {
  326. $ext = pack "C11H130",
  327. 0x00, 0x4A, #List Length
  328. 0xff, 0xfe, #Non existing group 1
  329. 0x00, 0x01, 0xff, #key_exchange data
  330. 0x00, 0x17, #P-256
  331. 0x00, 0x41, #key_exchange data length
  332. "04A798ACF80B2991A0A53D084F4F649A46BE49D061EB5B8CFF9C8EC6AE792507B6".
  333. "F77FE6E446AF3645FD86BB7CFFD2644E45CC00183343C5CEAD67BB017B082007"; #key_exchange data
  334. } else {
  335. $ext = pack "C11H64",
  336. 0x00, 0x29, #List Length
  337. 0xff, 0xfe, #Non existing group 1
  338. 0x00, 0x01, 0xff, #key_exchange data
  339. 0x00, 0x1d, #x25519
  340. 0x00, 0x20, #key_exchange data length
  341. "155155B95269ED5C87EAA99C2EF5A593".
  342. "EDF83495E80380089F831B94D14B1421"; #key_exchange data
  343. }
  344. } elsif ($testtype == NOT_IN_SUPPORTED_GROUPS) {
  345. $suppgroups = pack "C4",
  346. 0x00, 0x02, #List Length
  347. 0x00, 0xfe; #Non existing group 1
  348. } elsif ($testtype == GROUP_ID_TOO_SHORT) {
  349. $ext = pack "C6H64C1",
  350. 0x00, 0x25, #List Length
  351. 0x00, 0x1d, #x25519
  352. 0x00, 0x20, #key_exchange data length
  353. "155155B95269ED5C87EAA99C2EF5A593".
  354. "EDF83495E80380089F831B94D14B1421"; #key_exchange data
  355. 0x00; #Group id too short
  356. } elsif ($testtype == KEX_LEN_MISMATCH) {
  357. $ext = pack "C8",
  358. 0x00, 0x06, #List Length
  359. 0x00, 0x1d, #x25519
  360. 0x00, 0x20, #key_exchange data length
  361. 0x15, 0x51; #Only two bytes of data, but length should be 32
  362. } elsif ($testtype == ZERO_LEN_KEX_DATA) {
  363. $ext = pack "C10H64",
  364. 0x00, 0x28, #List Length
  365. 0xff, 0xfe, #Non existing group 1
  366. 0x00, 0x00, #zero length key_exchange data is invalid
  367. 0x00, 0x1d, #x25519
  368. 0x00, 0x20, #key_exchange data length
  369. "155155B95269ED5C87EAA99C2EF5A593".
  370. "EDF83495E80380089F831B94D14B1421"; #key_exchange data
  371. } elsif ($testtype == TRAILING_DATA) {
  372. $ext = pack "C6H64C1",
  373. 0x00, 0x24, #List Length
  374. 0x00, 0x1d, #x25519
  375. 0x00, 0x20, #key_exchange data length
  376. "155155B95269ED5C87EAA99C2EF5A593".
  377. "EDF83495E80380089F831B94D14B1421", #key_exchange data
  378. 0x00; #Trailing garbage
  379. } elsif ($testtype == NO_KEY_SHARES_IN_HRR) {
  380. #We trick the server into thinking we sent a P-256 key_share -
  381. #but the client actually sent X25519
  382. $ext = pack "C7",
  383. 0x00, 0x05, #List Length
  384. 0x00, 0x17, #P-256
  385. 0x00, 0x01, #key_exchange data length
  386. 0xff; #Dummy key_share data
  387. } elsif ($testtype == NON_TLS1_3_KEY_SHARE) {
  388. $ext = pack "C6H98",
  389. 0x00, 0x35, #List Length
  390. 0x00, 0x13, #P-192
  391. 0x00, 0x31, #key_exchange data length
  392. "04EE3B38D1CB800A1A2B702FC8423599F2AC7161E175C865F8".
  393. "3DAF78BCBAE561464E8144359BE70CB7989D28A2F43F8F2C"; #key_exchange data
  394. }
  395. if ($testtype != EMPTY_EXTENSION
  396. && $testtype != NO_KEY_SHARES_IN_HRR) {
  397. $message->set_extension(
  398. TLSProxy::Message::EXT_SUPPORTED_GROUPS, $suppgroups);
  399. }
  400. if ($testtype == MISSING_EXTENSION) {
  401. $message->delete_extension(
  402. TLSProxy::Message::EXT_KEY_SHARE);
  403. } elsif ($testtype != NOT_IN_SUPPORTED_GROUPS) {
  404. $message->set_extension(
  405. TLSProxy::Message::EXT_KEY_SHARE, $ext);
  406. }
  407. $message->repack();
  408. } elsif ($message->mt == TLSProxy::Message::MT_SERVER_HELLO
  409. && $direction == SERVER_TO_CLIENT) {
  410. my $ext;
  411. my $key_share =
  412. $message->extension_data->{TLSProxy::Message::EXT_KEY_SHARE};
  413. $selectedgroupid = unpack("n", $key_share);
  414. if ($testtype == LOOK_ONLY) {
  415. return;
  416. }
  417. if ($testtype == NO_KEY_SHARES_IN_HRR) {
  418. $message->delete_extension(TLSProxy::Message::EXT_KEY_SHARE);
  419. $message->set_extension(TLSProxy::Message::EXT_UNKNOWN, "");
  420. $message->repack();
  421. return;
  422. }
  423. if ($testtype == SELECT_X25519) {
  424. $ext = pack "C4H64",
  425. 0x00, 0x1d, #x25519
  426. 0x00, 0x20, #key_exchange data length
  427. "155155B95269ED5C87EAA99C2EF5A593".
  428. "EDF83495E80380089F831B94D14B1421"; #key_exchange data
  429. } elsif ($testtype == GROUP_ID_TOO_SHORT) {
  430. $ext = pack "C1",
  431. 0x00;
  432. } elsif ($testtype == KEX_LEN_MISMATCH) {
  433. $ext = pack "C6",
  434. 0x00, 0x1d, #x25519
  435. 0x00, 0x20, #key_exchange data length
  436. 0x15, 0x51; #Only two bytes of data, but length should be 32
  437. } elsif ($testtype == ZERO_LEN_KEX_DATA) {
  438. $ext = pack "C4",
  439. 0x00, 0x1d, #x25519
  440. 0x00, 0x00, #zero length key_exchange data is invalid
  441. } elsif ($testtype == TRAILING_DATA) {
  442. $ext = pack "C4H64C1",
  443. 0x00, 0x1d, #x25519
  444. 0x00, 0x20, #key_exchange data length
  445. "155155B95269ED5C87EAA99C2EF5A593".
  446. "EDF83495E80380089F831B94D14B1421", #key_exchange data
  447. 0x00; #Trailing garbage
  448. }
  449. $message->set_extension(TLSProxy::Message::EXT_KEY_SHARE, $ext);
  450. $message->repack();
  451. }
  452. }
  453. }