70-test_key_share.t 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 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. };
  28. use constant {
  29. CLIENT_TO_SERVER => 1,
  30. SERVER_TO_CLIENT => 2
  31. };
  32. use constant {
  33. X25519 => 0x1d,
  34. P_256 => 0x17,
  35. FFDHE2048 => 0x0100,
  36. FFDHE3072 => 0x0101
  37. };
  38. my $testtype;
  39. my $direction;
  40. my $selectedgroupid;
  41. my $test_name = "test_key_share";
  42. setup($test_name);
  43. plan skip_all => "TLSProxy isn't usable on $^O"
  44. if $^O =~ /^(VMS)$/;
  45. plan skip_all => "$test_name needs the dynamic engine feature enabled"
  46. if disabled("engine") || disabled("dynamic-engine");
  47. plan skip_all => "$test_name needs the sock feature enabled"
  48. if disabled("sock");
  49. plan skip_all => "$test_name needs TLS1.3 enabled"
  50. if disabled("tls1_3");
  51. plan skip_all => "$test_name needs EC or DH enabled"
  52. if disabled("ec") && disabled("dh");
  53. $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
  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-256");
  70. }
  71. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  72. plan tests => 22;
  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:X25519");
  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("ec")) {
  177. $proxy->clientflags("-curves ffdhe2048:ffdhe3072");
  178. } else {
  179. $proxy->clientflags("-curves X25519:P-256");
  180. }
  181. $proxy->start();
  182. if (disabled("ec")) {
  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("ec")) {
  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("ec")) {
  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. sub modify_key_shares_filter
  254. {
  255. my $proxy = shift;
  256. # We're only interested in the initial ClientHello
  257. if (($direction == CLIENT_TO_SERVER && $proxy->flight != 0
  258. && ($proxy->flight != 1 || $testtype != NO_KEY_SHARES_IN_HRR))
  259. || ($direction == SERVER_TO_CLIENT && $proxy->flight != 1)) {
  260. return;
  261. }
  262. foreach my $message (@{$proxy->message_list}) {
  263. if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO
  264. && $direction == CLIENT_TO_SERVER) {
  265. my $ext;
  266. my $suppgroups;
  267. #Setup supported groups to include some unrecognised groups
  268. $suppgroups = pack "C8",
  269. 0x00, 0x06, #List Length
  270. 0xff, 0xfe, #Non existing group 1
  271. 0xff, 0xff, #Non existing group 2
  272. 0x00, 0x1d; #x25519
  273. if ($testtype == EMPTY_EXTENSION) {
  274. $ext = pack "C2",
  275. 0x00, 0x00;
  276. } elsif ($testtype == NO_ACCEPTABLE_KEY_SHARES) {
  277. $ext = pack "C12",
  278. 0x00, 0x0a, #List Length
  279. 0xff, 0xfe, #Non existing group 1
  280. 0x00, 0x01, 0xff, #key_exchange data
  281. 0xff, 0xff, #Non existing group 2
  282. 0x00, 0x01, 0xff; #key_exchange data
  283. } elsif ($testtype == ACCEPTABLE_AT_END) {
  284. $ext = pack "C11H64",
  285. 0x00, 0x29, #List Length
  286. 0xff, 0xfe, #Non existing group 1
  287. 0x00, 0x01, 0xff, #key_exchange data
  288. 0x00, 0x1d, #x25519
  289. 0x00, 0x20, #key_exchange data length
  290. "155155B95269ED5C87EAA99C2EF5A593".
  291. "EDF83495E80380089F831B94D14B1421"; #key_exchange data
  292. } elsif ($testtype == NOT_IN_SUPPORTED_GROUPS) {
  293. $suppgroups = pack "C4",
  294. 0x00, 0x02, #List Length
  295. 0x00, 0xfe; #Non existing group 1
  296. } elsif ($testtype == GROUP_ID_TOO_SHORT) {
  297. $ext = pack "C6H64C1",
  298. 0x00, 0x25, #List Length
  299. 0x00, 0x1d, #x25519
  300. 0x00, 0x20, #key_exchange data length
  301. "155155B95269ED5C87EAA99C2EF5A593".
  302. "EDF83495E80380089F831B94D14B1421"; #key_exchange data
  303. 0x00; #Group id too short
  304. } elsif ($testtype == KEX_LEN_MISMATCH) {
  305. $ext = pack "C8",
  306. 0x00, 0x06, #List Length
  307. 0x00, 0x1d, #x25519
  308. 0x00, 0x20, #key_exchange data length
  309. 0x15, 0x51; #Only two bytes of data, but length should be 32
  310. } elsif ($testtype == ZERO_LEN_KEX_DATA) {
  311. $ext = pack "C10H64",
  312. 0x00, 0x28, #List Length
  313. 0xff, 0xfe, #Non existing group 1
  314. 0x00, 0x00, #zero length key_exchange data is invalid
  315. 0x00, 0x1d, #x25519
  316. 0x00, 0x20, #key_exchange data length
  317. "155155B95269ED5C87EAA99C2EF5A593".
  318. "EDF83495E80380089F831B94D14B1421"; #key_exchange data
  319. } elsif ($testtype == TRAILING_DATA) {
  320. $ext = pack "C6H64C1",
  321. 0x00, 0x24, #List Length
  322. 0x00, 0x1d, #x25519
  323. 0x00, 0x20, #key_exchange data length
  324. "155155B95269ED5C87EAA99C2EF5A593".
  325. "EDF83495E80380089F831B94D14B1421", #key_exchange data
  326. 0x00; #Trailing garbage
  327. } elsif ($testtype == NO_KEY_SHARES_IN_HRR) {
  328. #We trick the server into thinking we sent a P-256 key_share -
  329. #but the client actually sent X25519
  330. $ext = pack "C7",
  331. 0x00, 0x05, #List Length
  332. 0x00, 0x17, #P-256
  333. 0x00, 0x01, #key_exchange data length
  334. 0xff; #Dummy key_share data
  335. }
  336. if ($testtype != EMPTY_EXTENSION
  337. && $testtype != NO_KEY_SHARES_IN_HRR) {
  338. $message->set_extension(
  339. TLSProxy::Message::EXT_SUPPORTED_GROUPS, $suppgroups);
  340. }
  341. if ($testtype == MISSING_EXTENSION) {
  342. $message->delete_extension(
  343. TLSProxy::Message::EXT_KEY_SHARE);
  344. } elsif ($testtype != NOT_IN_SUPPORTED_GROUPS) {
  345. $message->set_extension(
  346. TLSProxy::Message::EXT_KEY_SHARE, $ext);
  347. }
  348. $message->repack();
  349. } elsif ($message->mt == TLSProxy::Message::MT_SERVER_HELLO
  350. && $direction == SERVER_TO_CLIENT) {
  351. my $ext;
  352. my $key_share =
  353. $message->extension_data->{TLSProxy::Message::EXT_KEY_SHARE};
  354. $selectedgroupid = unpack("n", $key_share);
  355. if ($testtype == LOOK_ONLY) {
  356. return;
  357. }
  358. if ($testtype == NO_KEY_SHARES_IN_HRR) {
  359. $message->delete_extension(TLSProxy::Message::EXT_KEY_SHARE);
  360. $message->set_extension(TLSProxy::Message::EXT_UNKNOWN, "");
  361. $message->repack();
  362. return;
  363. }
  364. if ($testtype == SELECT_X25519) {
  365. $ext = pack "C4H64",
  366. 0x00, 0x1d, #x25519
  367. 0x00, 0x20, #key_exchange data length
  368. "155155B95269ED5C87EAA99C2EF5A593".
  369. "EDF83495E80380089F831B94D14B1421"; #key_exchange data
  370. } elsif ($testtype == GROUP_ID_TOO_SHORT) {
  371. $ext = pack "C1",
  372. 0x00;
  373. } elsif ($testtype == KEX_LEN_MISMATCH) {
  374. $ext = pack "C6",
  375. 0x00, 0x1d, #x25519
  376. 0x00, 0x20, #key_exchange data length
  377. 0x15, 0x51; #Only two bytes of data, but length should be 32
  378. } elsif ($testtype == ZERO_LEN_KEX_DATA) {
  379. $ext = pack "C4",
  380. 0x00, 0x1d, #x25519
  381. 0x00, 0x00, #zero length key_exchange data is invalid
  382. } elsif ($testtype == TRAILING_DATA) {
  383. $ext = pack "C4H64C1",
  384. 0x00, 0x1d, #x25519
  385. 0x00, 0x20, #key_exchange data length
  386. "155155B95269ED5C87EAA99C2EF5A593".
  387. "EDF83495E80380089F831B94D14B1421", #key_exchange data
  388. 0x00; #Trailing garbage
  389. }
  390. $message->set_extension(TLSProxy::Message::EXT_KEY_SHARE, $ext);
  391. $message->repack();
  392. }
  393. }
  394. }