70-test_tls13cookie.t 3.8 KB

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