70-test_tls13cookie.t 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #! /usr/bin/env perl
  2. # Copyright 2017-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. my $test_name = "test_tls13cookie";
  13. setup($test_name);
  14. plan skip_all => "TLSProxy isn't usable on $^O"
  15. if $^O =~ /^(VMS)$/;
  16. plan skip_all => "$test_name needs the dynamic engine feature enabled"
  17. if disabled("engine") || disabled("dynamic-engine");
  18. plan skip_all => "$test_name needs the sock feature enabled"
  19. if disabled("sock");
  20. plan skip_all => "$test_name needs TLS1.3 enabled"
  21. if disabled("tls1_3") || (disabled("ec") && disabled("dh"));
  22. use constant {
  23. COOKIE_ONLY => 0,
  24. COOKIE_AND_KEY_SHARE => 1
  25. };
  26. my $proxy = TLSProxy::Proxy->new(
  27. undef,
  28. cmdstr(app(["openssl"]), display => 1),
  29. srctop_file("apps", "server.pem"),
  30. (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
  31. );
  32. my $cookieseen = 0;
  33. my $testtype;
  34. #Test 1: Inserting a cookie into an HRR should see it echoed in the ClientHello
  35. $testtype = COOKIE_ONLY;
  36. $proxy->filter(\&cookie_filter);
  37. $proxy->serverflags("-curves X25519") if !disabled("ecx");
  38. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  39. plan tests => 2;
  40. SKIP: {
  41. skip "ECX disabled", 1, if (disabled("ecx"));
  42. ok(TLSProxy::Message->success() && $cookieseen == 1, "Cookie seen");
  43. }
  44. #Test 2: Same as test 1 but should also work where a new key_share is also
  45. # required
  46. $testtype = COOKIE_AND_KEY_SHARE;
  47. $proxy->clear();
  48. if (disabled("ecx")) {
  49. $proxy->clientflags("-curves ffdhe3072:ffdhe2048");
  50. $proxy->serverflags("-curves ffdhe2048");
  51. } else {
  52. $proxy->clientflags("-curves P-256:X25519");
  53. $proxy->serverflags("-curves X25519");
  54. }
  55. $proxy->start();
  56. ok(TLSProxy::Message->success() && $cookieseen == 1, "Cookie seen");
  57. sub cookie_filter
  58. {
  59. my $proxy = shift;
  60. # We're only interested in the HRR and both ClientHellos
  61. return if ($proxy->flight > 2);
  62. my $ext = pack "C8",
  63. 0x00, 0x06, #Cookie Length
  64. 0x00, 0x01, #Dummy cookie data (6 bytes)
  65. 0x02, 0x03,
  66. 0x04, 0x05;
  67. foreach my $message (@{$proxy->message_list}) {
  68. if ($message->mt == TLSProxy::Message::MT_SERVER_HELLO
  69. && ${$message->records}[0]->flight == 1) {
  70. $message->delete_extension(TLSProxy::Message::EXT_KEY_SHARE)
  71. if ($testtype == COOKIE_ONLY);
  72. $message->set_extension(TLSProxy::Message::EXT_COOKIE, $ext);
  73. $message->repack();
  74. } elsif ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
  75. if (${$message->records}[0]->flight == 0) {
  76. if ($testtype == COOKIE_ONLY) {
  77. my $ext = pack "C7",
  78. 0x00, 0x05, #List Length
  79. 0x00, 0x17, #P-256
  80. 0x00, 0x01, #key_exchange data length
  81. 0xff; #Dummy key_share data
  82. # Trick the server into thinking we got an unacceptable
  83. # key_share
  84. $message->set_extension(
  85. TLSProxy::Message::EXT_KEY_SHARE, $ext);
  86. $message->repack();
  87. }
  88. } else {
  89. #cmp can behave differently dependent on locale
  90. no locale;
  91. my $cookie =
  92. $message->extension_data->{TLSProxy::Message::EXT_COOKIE};
  93. return if !defined($cookie);
  94. return if ($cookie cmp $ext) != 0;
  95. $cookieseen = 1;
  96. }
  97. }
  98. }
  99. }