70-test_tls13cookie.t 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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");
  39. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  40. plan tests => 2;
  41. ok(TLSProxy::Message->success() && $cookieseen == 1, "Cookie seen");
  42. #Test 2: Same as test 1 but should also work where a new key_share is also
  43. # required
  44. $testtype = COOKIE_AND_KEY_SHARE;
  45. $proxy->clear();
  46. $proxy->clientflags("-curves P-256:X25519");
  47. $proxy->serverflags("-curves X25519");
  48. $proxy->start();
  49. ok(TLSProxy::Message->success() && $cookieseen == 1, "Cookie seen");
  50. sub cookie_filter
  51. {
  52. my $proxy = shift;
  53. # We're only interested in the HRR and both ClientHellos
  54. return if ($proxy->flight > 2);
  55. my $ext = pack "C8",
  56. 0x00, 0x06, #Cookie Length
  57. 0x00, 0x01, #Dummy cookie data (6 bytes)
  58. 0x02, 0x03,
  59. 0x04, 0x05;
  60. foreach my $message (@{$proxy->message_list}) {
  61. if ($message->mt == TLSProxy::Message::MT_SERVER_HELLO
  62. && ${$message->records}[0]->flight == 1) {
  63. $message->delete_extension(TLSProxy::Message::EXT_KEY_SHARE)
  64. if ($testtype == COOKIE_ONLY);
  65. $message->set_extension(TLSProxy::Message::EXT_COOKIE, $ext);
  66. $message->repack();
  67. } elsif ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
  68. if (${$message->records}[0]->flight == 0) {
  69. if ($testtype == COOKIE_ONLY) {
  70. my $ext = pack "C7",
  71. 0x00, 0x05, #List Length
  72. 0x00, 0x17, #P-256
  73. 0x00, 0x01, #key_exchange data length
  74. 0xff; #Dummy key_share data
  75. # Trick the server into thinking we got an unacceptable
  76. # key_share
  77. $message->set_extension(
  78. TLSProxy::Message::EXT_KEY_SHARE, $ext);
  79. $message->repack();
  80. }
  81. } else {
  82. #cmp can behave differently dependent on locale
  83. no locale;
  84. my $cookie =
  85. $message->extension_data->{TLSProxy::Message::EXT_COOKIE};
  86. return if !defined($cookie);
  87. return if ($cookie cmp $ext) != 0;
  88. $cookieseen = 1;
  89. }
  90. }
  91. }
  92. }