70-test_key_share.t 15 KB

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