70-test_key_share.t 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (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. };
  27. use constant {
  28. CLIENT_TO_SERVER => 1,
  29. SERVER_TO_CLIENT => 2
  30. };
  31. use constant {
  32. X25519 => 0x1d,
  33. P_256 => 0x17
  34. };
  35. my $testtype;
  36. my $direction;
  37. my $selectedgroupid;
  38. my $test_name = "test_key_share";
  39. setup($test_name);
  40. plan skip_all => "TLSProxy isn't usable on $^O"
  41. if $^O =~ /^(VMS|MSWin32)$/;
  42. plan skip_all => "$test_name needs the dynamic engine feature enabled"
  43. if disabled("engine") || disabled("dynamic-engine");
  44. plan skip_all => "$test_name needs the sock feature enabled"
  45. if disabled("sock");
  46. plan skip_all => "$test_name needs TLS1.3 enabled"
  47. if disabled("tls1_3");
  48. $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
  49. my $proxy = TLSProxy::Proxy->new(
  50. undef,
  51. cmdstr(app(["openssl"]), display => 1),
  52. srctop_file("apps", "server.pem"),
  53. (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
  54. );
  55. #We assume that test_ssl_new and friends will test the happy path for this,
  56. #so we concentrate on the less common scenarios
  57. #Test 1: An empty key_shares extension should succeed after a HelloRetryRequest
  58. $testtype = EMPTY_EXTENSION;
  59. $direction = CLIENT_TO_SERVER;
  60. $proxy->filter(\&modify_key_shares_filter);
  61. $proxy->serverflags("-curves P-256");
  62. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  63. plan tests => 21;
  64. ok(TLSProxy::Message->success(), "Success after HRR");
  65. #Test 2: The server sending an HRR requesting a group the client already sent
  66. # should fail
  67. $proxy->clear();
  68. $proxy->start();
  69. ok(TLSProxy::Message->fail(), "Server asks for group already provided");
  70. #Test 3: A missing key_shares extension should not succeed
  71. $proxy->clear();
  72. $testtype = MISSING_EXTENSION;
  73. $proxy->start();
  74. ok(TLSProxy::Message->fail(), "Missing key_shares extension");
  75. #Test 4: No initial acceptable key_shares should succeed after a
  76. # HelloRetryRequest
  77. $proxy->clear();
  78. $proxy->filter(undef);
  79. $proxy->serverflags("-curves P-256");
  80. $proxy->start();
  81. ok(TLSProxy::Message->success(), "No initial acceptable key_shares");
  82. #Test 5: No acceptable key_shares and no shared groups should fail
  83. $proxy->clear();
  84. $proxy->filter(undef);
  85. $proxy->serverflags("-curves P-256");
  86. $proxy->clientflags("-curves P-384");
  87. $proxy->start();
  88. ok(TLSProxy::Message->fail(), "No acceptable key_shares");
  89. #Test 6: A non preferred but acceptable key_share should succeed
  90. $proxy->clear();
  91. $proxy->clientflags("-curves P-256");
  92. $proxy->start();
  93. ok(TLSProxy::Message->success(), "Non preferred key_share");
  94. $proxy->filter(\&modify_key_shares_filter);
  95. #Test 7: An acceptable key_share after a list of non-acceptable ones should
  96. #succeed
  97. $proxy->clear();
  98. $testtype = ACCEPTABLE_AT_END;
  99. $proxy->start();
  100. ok(TLSProxy::Message->success(), "Acceptable key_share at end of list");
  101. #Test 8: An acceptable key_share but for a group not in supported_groups should
  102. #fail
  103. $proxy->clear();
  104. $testtype = NOT_IN_SUPPORTED_GROUPS;
  105. $proxy->start();
  106. ok(TLSProxy::Message->fail(), "Acceptable key_share not in supported_groups");
  107. #Test 9: Too short group_id should fail
  108. $proxy->clear();
  109. $testtype = GROUP_ID_TOO_SHORT;
  110. $proxy->start();
  111. ok(TLSProxy::Message->fail(), "Group id too short");
  112. #Test 10: key_exchange length mismatch should fail
  113. $proxy->clear();
  114. $testtype = KEX_LEN_MISMATCH;
  115. $proxy->start();
  116. ok(TLSProxy::Message->fail(), "key_exchange length mismatch");
  117. #Test 11: Zero length key_exchange should fail
  118. $proxy->clear();
  119. $testtype = ZERO_LEN_KEX_DATA;
  120. $proxy->start();
  121. ok(TLSProxy::Message->fail(), "zero length key_exchange data");
  122. #Test 12: Trailing data on key_share list should fail
  123. $proxy->clear();
  124. $testtype = TRAILING_DATA;
  125. $proxy->start();
  126. ok(TLSProxy::Message->fail(), "key_share list trailing data");
  127. #Test 13: Multiple acceptable key_shares - we choose the first one
  128. $proxy->clear();
  129. $direction = SERVER_TO_CLIENT;
  130. $testtype = LOOK_ONLY;
  131. $proxy->clientflags("-curves P-256:X25519");
  132. $proxy->start();
  133. ok(TLSProxy::Message->success() && ($selectedgroupid == P_256),
  134. "Multiple acceptable key_shares");
  135. #Test 14: Multiple acceptable key_shares - we choose the first one (part 2)
  136. $proxy->clear();
  137. $proxy->clientflags("-curves X25519:P-256");
  138. $proxy->start();
  139. ok(TLSProxy::Message->success() && ($selectedgroupid == X25519),
  140. "Multiple acceptable key_shares (part 2)");
  141. #Test 15: Server sends key_share that wasn't offerred should fail
  142. $proxy->clear();
  143. $testtype = SELECT_X25519;
  144. $proxy->clientflags("-curves P-256");
  145. $proxy->start();
  146. ok(TLSProxy::Message->fail(), "Non offered key_share");
  147. #Test 16: Too short group_id in ServerHello should fail
  148. $proxy->clear();
  149. $testtype = GROUP_ID_TOO_SHORT;
  150. $proxy->start();
  151. ok(TLSProxy::Message->fail(), "Group id too short in ServerHello");
  152. #Test 17: key_exchange length mismatch in ServerHello should fail
  153. $proxy->clear();
  154. $testtype = KEX_LEN_MISMATCH;
  155. $proxy->start();
  156. ok(TLSProxy::Message->fail(), "key_exchange length mismatch in ServerHello");
  157. #Test 18: Zero length key_exchange in ServerHello should fail
  158. $proxy->clear();
  159. $testtype = ZERO_LEN_KEX_DATA;
  160. $proxy->start();
  161. ok(TLSProxy::Message->fail(), "zero length key_exchange data in ServerHello");
  162. #Test 19: Trailing data on key_share in ServerHello should fail
  163. $proxy->clear();
  164. $testtype = TRAILING_DATA;
  165. $proxy->start();
  166. ok(TLSProxy::Message->fail(), "key_share trailing data in ServerHello");
  167. #Test 20: key_share should not be sent if the client is not capable of
  168. # negotiating TLSv1.3
  169. $proxy->clear();
  170. $proxy->filter(undef);
  171. $proxy->clientflags("-no_tls1_3");
  172. $proxy->start();
  173. my $clienthello = $proxy->message_list->[0];
  174. ok(TLSProxy::Message->success()
  175. && !defined $clienthello->extension_data->{TLSProxy::Message::EXT_KEY_SHARE},
  176. "No key_share for TLS<=1.2 client");
  177. $proxy->filter(\&modify_key_shares_filter);
  178. #Test 21: A server not capable of negotiating TLSv1.3 should not attempt to
  179. # process a key_share
  180. $proxy->clear();
  181. $direction = CLIENT_TO_SERVER;
  182. $testtype = NO_ACCEPTABLE_KEY_SHARES;
  183. $proxy->serverflags("-no_tls1_3");
  184. $proxy->start();
  185. ok(TLSProxy::Message->success(), "Ignore key_share for TLS<=1.2 server");
  186. sub modify_key_shares_filter
  187. {
  188. my $proxy = shift;
  189. # We're only interested in the initial ClientHello
  190. if (($direction == CLIENT_TO_SERVER && $proxy->flight != 0)
  191. || ($direction == SERVER_TO_CLIENT && $proxy->flight != 1)) {
  192. return;
  193. }
  194. foreach my $message (@{$proxy->message_list}) {
  195. if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO
  196. && $direction == CLIENT_TO_SERVER) {
  197. my $ext;
  198. my $suppgroups;
  199. #Setup supported groups to include some unrecognised groups
  200. $suppgroups = pack "C8",
  201. 0x00, 0x06, #List Length
  202. 0xff, 0xfe, #Non existing group 1
  203. 0xff, 0xff, #Non existing group 2
  204. 0x00, 0x1d; #x25519
  205. if ($testtype == EMPTY_EXTENSION) {
  206. $ext = pack "C2",
  207. 0x00, 0x00;
  208. } elsif ($testtype == NO_ACCEPTABLE_KEY_SHARES) {
  209. $ext = pack "C12",
  210. 0x00, 0x0a, #List Length
  211. 0xff, 0xfe, #Non existing group 1
  212. 0x00, 0x01, 0xff, #key_exchange data
  213. 0xff, 0xff, #Non existing group 2
  214. 0x00, 0x01, 0xff; #key_exchange data
  215. } elsif ($testtype == ACCEPTABLE_AT_END) {
  216. $ext = pack "C11H64",
  217. 0x00, 0x29, #List Length
  218. 0xff, 0xfe, #Non existing group 1
  219. 0x00, 0x01, 0xff, #key_exchange data
  220. 0x00, 0x1d, #x25519
  221. 0x00, 0x20, #key_exchange data length
  222. "155155B95269ED5C87EAA99C2EF5A593".
  223. "EDF83495E80380089F831B94D14B1421"; #key_exchange data
  224. } elsif ($testtype == NOT_IN_SUPPORTED_GROUPS) {
  225. $suppgroups = pack "C4",
  226. 0x00, 0x02, #List Length
  227. 0x00, 0xfe; #Non existing group 1
  228. } elsif ($testtype == GROUP_ID_TOO_SHORT) {
  229. $ext = pack "C6H64C1",
  230. 0x00, 0x25, #List Length
  231. 0x00, 0x1d, #x25519
  232. 0x00, 0x20, #key_exchange data length
  233. "155155B95269ED5C87EAA99C2EF5A593".
  234. "EDF83495E80380089F831B94D14B1421"; #key_exchange data
  235. 0x00; #Group id too short
  236. } elsif ($testtype == KEX_LEN_MISMATCH) {
  237. $ext = pack "C8",
  238. 0x00, 0x06, #List Length
  239. 0x00, 0x1d, #x25519
  240. 0x00, 0x20, #key_exchange data length
  241. 0x15, 0x51; #Only two bytes of data, but length should be 32
  242. } elsif ($testtype == ZERO_LEN_KEX_DATA) {
  243. $ext = pack "C10H64",
  244. 0x00, 0x28, #List Length
  245. 0xff, 0xfe, #Non existing group 1
  246. 0x00, 0x00, #zero length key_exchange data is invalid
  247. 0x00, 0x1d, #x25519
  248. 0x00, 0x20, #key_exchange data length
  249. "155155B95269ED5C87EAA99C2EF5A593".
  250. "EDF83495E80380089F831B94D14B1421"; #key_exchange data
  251. } elsif ($testtype == TRAILING_DATA) {
  252. $ext = pack "C6H64C1",
  253. 0x00, 0x24, #List Length
  254. 0x00, 0x1d, #x25519
  255. 0x00, 0x20, #key_exchange data length
  256. "155155B95269ED5C87EAA99C2EF5A593".
  257. "EDF83495E80380089F831B94D14B1421", #key_exchange data
  258. 0x00; #Trailing garbage
  259. }
  260. if ($testtype != EMPTY_EXTENSION) {
  261. $message->set_extension(
  262. TLSProxy::Message::EXT_SUPPORTED_GROUPS, $suppgroups);
  263. }
  264. if ($testtype == MISSING_EXTENSION) {
  265. $message->delete_extension(
  266. TLSProxy::Message::EXT_KEY_SHARE);
  267. } elsif ($testtype != NOT_IN_SUPPORTED_GROUPS) {
  268. $message->set_extension(
  269. TLSProxy::Message::EXT_KEY_SHARE, $ext);
  270. }
  271. $message->repack();
  272. } elsif ($message->mt == TLSProxy::Message::MT_SERVER_HELLO
  273. && $direction == SERVER_TO_CLIENT) {
  274. my $ext;
  275. my $key_share =
  276. $message->extension_data->{TLSProxy::Message::EXT_KEY_SHARE};
  277. $selectedgroupid = unpack("n", $key_share);
  278. if ($testtype == LOOK_ONLY) {
  279. return;
  280. }
  281. if ($testtype == SELECT_X25519) {
  282. $ext = pack "C4H64",
  283. 0x00, 0x1d, #x25519
  284. 0x00, 0x20, #key_exchange data length
  285. "155155B95269ED5C87EAA99C2EF5A593".
  286. "EDF83495E80380089F831B94D14B1421"; #key_exchange data
  287. } elsif ($testtype == GROUP_ID_TOO_SHORT) {
  288. $ext = pack "C1",
  289. 0x00;
  290. } elsif ($testtype == KEX_LEN_MISMATCH) {
  291. $ext = pack "C6",
  292. 0x00, 0x1d, #x25519
  293. 0x00, 0x20, #key_exchange data length
  294. 0x15, 0x51; #Only two bytes of data, but length should be 32
  295. } elsif ($testtype == ZERO_LEN_KEX_DATA) {
  296. $ext = pack "C4",
  297. 0x00, 0x1d, #x25519
  298. 0x00, 0x00, #zero length key_exchange data is invalid
  299. } elsif ($testtype == TRAILING_DATA) {
  300. $ext = pack "C4H64C1",
  301. 0x00, 0x1d, #x25519
  302. 0x00, 0x20, #key_exchange data length
  303. "155155B95269ED5C87EAA99C2EF5A593".
  304. "EDF83495E80380089F831B94D14B1421", #key_exchange data
  305. 0x00; #Trailing garbage
  306. }
  307. $message->set_extension(TLSProxy::Message::EXT_KEY_SHARE, $ext);
  308. $message->repack();
  309. }
  310. }
  311. }