70-test_key_share.t 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. 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-256");
  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: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. 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. $proxy->clientflags("-groups secp192r1:P-256:X25519");
  259. $proxy->ciphers("AES128-SHA:\@SECLEVEL=0");
  260. $testtype = NON_TLS1_3_KEY_SHARE;
  261. $proxy->start();
  262. my $ishrr = defined ${$proxy->message_list}[2]
  263. &&(${$proxy->message_list}[0]->mt == TLSProxy::Message::MT_CLIENT_HELLO)
  264. && (${$proxy->message_list}[2]->mt == TLSProxy::Message::MT_CLIENT_HELLO);
  265. ok(TLSProxy::Message->success() && $ishrr,
  266. "Client sends a key_share for a Non TLSv1.3 group");
  267. }
  268. sub modify_key_shares_filter
  269. {
  270. my $proxy = shift;
  271. # We're only interested in the initial ClientHello/SererHello/HRR
  272. if (($direction == CLIENT_TO_SERVER && $proxy->flight != 0
  273. && ($proxy->flight != 1 || $testtype != NO_KEY_SHARES_IN_HRR))
  274. || ($direction == SERVER_TO_CLIENT && $proxy->flight != 1)) {
  275. return;
  276. }
  277. foreach my $message (@{$proxy->message_list}) {
  278. if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO
  279. && $direction == CLIENT_TO_SERVER) {
  280. my $ext;
  281. my $suppgroups;
  282. if ($testtype != NON_TLS1_3_KEY_SHARE) {
  283. #Setup supported groups to include some unrecognised groups
  284. $suppgroups = pack "C8",
  285. 0x00, 0x06, #List Length
  286. 0xff, 0xfe, #Non existing group 1
  287. 0xff, 0xff, #Non existing group 2
  288. 0x00, 0x1d; #x25519
  289. } else {
  290. $suppgroups = pack "C6",
  291. 0x00, 0x04, #List Length
  292. 0x00, 0x13,
  293. 0x00, 0x1d; #x25519
  294. }
  295. if ($testtype == EMPTY_EXTENSION) {
  296. $ext = pack "C2",
  297. 0x00, 0x00;
  298. } elsif ($testtype == NO_ACCEPTABLE_KEY_SHARES) {
  299. $ext = pack "C12",
  300. 0x00, 0x0a, #List Length
  301. 0xff, 0xfe, #Non existing group 1
  302. 0x00, 0x01, 0xff, #key_exchange data
  303. 0xff, 0xff, #Non existing group 2
  304. 0x00, 0x01, 0xff; #key_exchange data
  305. } elsif ($testtype == ACCEPTABLE_AT_END) {
  306. $ext = pack "C11H64",
  307. 0x00, 0x29, #List Length
  308. 0xff, 0xfe, #Non existing group 1
  309. 0x00, 0x01, 0xff, #key_exchange data
  310. 0x00, 0x1d, #x25519
  311. 0x00, 0x20, #key_exchange data length
  312. "155155B95269ED5C87EAA99C2EF5A593".
  313. "EDF83495E80380089F831B94D14B1421"; #key_exchange data
  314. } elsif ($testtype == NOT_IN_SUPPORTED_GROUPS) {
  315. $suppgroups = pack "C4",
  316. 0x00, 0x02, #List Length
  317. 0x00, 0xfe; #Non existing group 1
  318. } elsif ($testtype == GROUP_ID_TOO_SHORT) {
  319. $ext = pack "C6H64C1",
  320. 0x00, 0x25, #List Length
  321. 0x00, 0x1d, #x25519
  322. 0x00, 0x20, #key_exchange data length
  323. "155155B95269ED5C87EAA99C2EF5A593".
  324. "EDF83495E80380089F831B94D14B1421"; #key_exchange data
  325. 0x00; #Group id too short
  326. } elsif ($testtype == KEX_LEN_MISMATCH) {
  327. $ext = pack "C8",
  328. 0x00, 0x06, #List Length
  329. 0x00, 0x1d, #x25519
  330. 0x00, 0x20, #key_exchange data length
  331. 0x15, 0x51; #Only two bytes of data, but length should be 32
  332. } elsif ($testtype == ZERO_LEN_KEX_DATA) {
  333. $ext = pack "C10H64",
  334. 0x00, 0x28, #List Length
  335. 0xff, 0xfe, #Non existing group 1
  336. 0x00, 0x00, #zero length key_exchange data is invalid
  337. 0x00, 0x1d, #x25519
  338. 0x00, 0x20, #key_exchange data length
  339. "155155B95269ED5C87EAA99C2EF5A593".
  340. "EDF83495E80380089F831B94D14B1421"; #key_exchange data
  341. } elsif ($testtype == TRAILING_DATA) {
  342. $ext = pack "C6H64C1",
  343. 0x00, 0x24, #List Length
  344. 0x00, 0x1d, #x25519
  345. 0x00, 0x20, #key_exchange data length
  346. "155155B95269ED5C87EAA99C2EF5A593".
  347. "EDF83495E80380089F831B94D14B1421", #key_exchange data
  348. 0x00; #Trailing garbage
  349. } elsif ($testtype == NO_KEY_SHARES_IN_HRR) {
  350. #We trick the server into thinking we sent a P-256 key_share -
  351. #but the client actually sent X25519
  352. $ext = pack "C7",
  353. 0x00, 0x05, #List Length
  354. 0x00, 0x17, #P-256
  355. 0x00, 0x01, #key_exchange data length
  356. 0xff; #Dummy key_share data
  357. } elsif ($testtype == NON_TLS1_3_KEY_SHARE) {
  358. $ext = pack "C6H98",
  359. 0x00, 0x35, #List Length
  360. 0x00, 0x13, #P-192
  361. 0x00, 0x31, #key_exchange data length
  362. "04EE3B38D1CB800A1A2B702FC8423599F2AC7161E175C865F8".
  363. "3DAF78BCBAE561464E8144359BE70CB7989D28A2F43F8F2C"; #key_exchange data
  364. }
  365. if ($testtype != EMPTY_EXTENSION
  366. && $testtype != NO_KEY_SHARES_IN_HRR) {
  367. $message->set_extension(
  368. TLSProxy::Message::EXT_SUPPORTED_GROUPS, $suppgroups);
  369. }
  370. if ($testtype == MISSING_EXTENSION) {
  371. $message->delete_extension(
  372. TLSProxy::Message::EXT_KEY_SHARE);
  373. } elsif ($testtype != NOT_IN_SUPPORTED_GROUPS) {
  374. $message->set_extension(
  375. TLSProxy::Message::EXT_KEY_SHARE, $ext);
  376. }
  377. $message->repack();
  378. } elsif ($message->mt == TLSProxy::Message::MT_SERVER_HELLO
  379. && $direction == SERVER_TO_CLIENT) {
  380. my $ext;
  381. my $key_share =
  382. $message->extension_data->{TLSProxy::Message::EXT_KEY_SHARE};
  383. $selectedgroupid = unpack("n", $key_share);
  384. if ($testtype == LOOK_ONLY) {
  385. return;
  386. }
  387. if ($testtype == NO_KEY_SHARES_IN_HRR) {
  388. $message->delete_extension(TLSProxy::Message::EXT_KEY_SHARE);
  389. $message->set_extension(TLSProxy::Message::EXT_UNKNOWN, "");
  390. $message->repack();
  391. return;
  392. }
  393. if ($testtype == SELECT_X25519) {
  394. $ext = pack "C4H64",
  395. 0x00, 0x1d, #x25519
  396. 0x00, 0x20, #key_exchange data length
  397. "155155B95269ED5C87EAA99C2EF5A593".
  398. "EDF83495E80380089F831B94D14B1421"; #key_exchange data
  399. } elsif ($testtype == GROUP_ID_TOO_SHORT) {
  400. $ext = pack "C1",
  401. 0x00;
  402. } elsif ($testtype == KEX_LEN_MISMATCH) {
  403. $ext = pack "C6",
  404. 0x00, 0x1d, #x25519
  405. 0x00, 0x20, #key_exchange data length
  406. 0x15, 0x51; #Only two bytes of data, but length should be 32
  407. } elsif ($testtype == ZERO_LEN_KEX_DATA) {
  408. $ext = pack "C4",
  409. 0x00, 0x1d, #x25519
  410. 0x00, 0x00, #zero length key_exchange data is invalid
  411. } elsif ($testtype == TRAILING_DATA) {
  412. $ext = pack "C4H64C1",
  413. 0x00, 0x1d, #x25519
  414. 0x00, 0x20, #key_exchange data length
  415. "155155B95269ED5C87EAA99C2EF5A593".
  416. "EDF83495E80380089F831B94D14B1421", #key_exchange data
  417. 0x00; #Trailing garbage
  418. }
  419. $message->set_extension(TLSProxy::Message::EXT_KEY_SHARE, $ext);
  420. $message->repack();
  421. }
  422. }
  423. }