70-test_tls13psk.t 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #! /usr/bin/env perl
  2. # Copyright 2017-2020 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 srctop_dir bldtop_dir/;
  10. use OpenSSL::Test::Utils;
  11. use File::Temp qw(tempfile);
  12. use TLSProxy::Proxy;
  13. my $test_name = "test_tls13psk";
  14. setup($test_name);
  15. plan skip_all => "TLSProxy isn't usable on $^O"
  16. if $^O =~ /^(VMS)$/;
  17. plan skip_all => "$test_name needs the dynamic engine feature enabled"
  18. if disabled("engine") || disabled("dynamic-engine");
  19. plan skip_all => "$test_name needs the sock feature enabled"
  20. if disabled("sock");
  21. plan skip_all => "$test_name needs TLSv1.3 enabled"
  22. if disabled("tls1_3");
  23. $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
  24. my $proxy = TLSProxy::Proxy->new(
  25. undef,
  26. cmdstr(app(["openssl"]), display => 1),
  27. srctop_file("apps", "server.pem"),
  28. (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
  29. );
  30. use constant {
  31. PSK_LAST_FIRST_CH => 0,
  32. ILLEGAL_EXT_SECOND_CH => 1
  33. };
  34. #Most PSK tests are done in test_ssl_new. This tests various failure scenarios
  35. #around PSK
  36. #Test 1: First get a session
  37. (undef, my $session) = tempfile();
  38. $proxy->clientflags("-sess_out ".$session);
  39. $proxy->serverflags("-servername localhost");
  40. $proxy->sessionfile($session);
  41. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  42. plan tests => 5;
  43. ok(TLSProxy::Message->success(), "Initial connection");
  44. #Test 2: Attempt a resume with PSK not in last place. Should fail
  45. $proxy->clear();
  46. $proxy->clientflags("-sess_in ".$session);
  47. $proxy->filter(\&modify_psk_filter);
  48. my $testtype = PSK_LAST_FIRST_CH;
  49. $proxy->start();
  50. ok(TLSProxy::Message->fail(), "PSK not last");
  51. #Test 3: Attempt a resume after an HRR where PSK hash matches selected
  52. # ciphersuite. Should see PSK on second ClientHello
  53. $proxy->clear();
  54. $proxy->clientflags("-sess_in ".$session);
  55. if (disabled("ec")) {
  56. $proxy->serverflags("-curves ffdhe3072");
  57. } else {
  58. $proxy->serverflags("-curves P-256");
  59. }
  60. $proxy->filter(undef);
  61. $proxy->start();
  62. #Check if the PSK is present in the second ClientHello
  63. my $ch2 = ${$proxy->message_list}[2];
  64. my $ch2seen = defined $ch2 && $ch2->mt() == TLSProxy::Message::MT_CLIENT_HELLO;
  65. my $pskseen = $ch2seen
  66. && defined ${$ch2->{extension_data}}{TLSProxy::Message::EXT_PSK};
  67. ok($pskseen, "PSK hash matches");
  68. #Test 4: Attempt a resume after an HRR where PSK hash does not match selected
  69. # ciphersuite. Should not see PSK on second ClientHello
  70. $proxy->clear();
  71. $proxy->clientflags("-sess_in ".$session);
  72. $proxy->filter(\&modify_psk_filter);
  73. if (disabled("ec")) {
  74. $proxy->serverflags("-curves ffdhe3072");
  75. } else {
  76. $proxy->serverflags("-curves P-256");
  77. }
  78. $proxy->ciphersuitesc("TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384");
  79. $proxy->ciphersuitess("TLS_AES_256_GCM_SHA384");
  80. #We force an early failure because TLS Proxy doesn't actually support
  81. #TLS_AES_256_GCM_SHA384. That doesn't matter for this test though.
  82. $testtype = ILLEGAL_EXT_SECOND_CH;
  83. $proxy->start();
  84. #Check if the PSK is present in the second ClientHello
  85. $ch2 = ${$proxy->message_list}[2];
  86. $ch2seen = defined $ch2 && $ch2->mt() == TLSProxy::Message::MT_CLIENT_HELLO;
  87. $pskseen = $ch2seen
  88. && defined ${$ch2->extension_data}{TLSProxy::Message::EXT_PSK};
  89. ok($ch2seen && !$pskseen, "PSK hash does not match");
  90. #Test 5: Attempt a resume without a sig agls extension. Should succeed because
  91. # sig algs is not needed in a resumption.
  92. $proxy->clear();
  93. $proxy->clientflags("-sess_in ".$session);
  94. $proxy->filter(\&remove_sig_algs_filter);
  95. $proxy->start();
  96. ok(TLSProxy::Message->success(), "Remove sig algs");
  97. unlink $session;
  98. sub modify_psk_filter
  99. {
  100. my $proxy = shift;
  101. my $flight;
  102. my $message;
  103. if ($testtype == PSK_LAST_FIRST_CH) {
  104. $flight = 0;
  105. } else {
  106. $flight = 2;
  107. }
  108. # Only look at the first or second ClientHello
  109. return if $proxy->flight != $flight;
  110. if ($testtype == PSK_LAST_FIRST_CH) {
  111. $message = ${$proxy->message_list}[0];
  112. } else {
  113. $message = ${$proxy->message_list}[2];
  114. }
  115. return if (!defined $message
  116. || $message->mt != TLSProxy::Message::MT_CLIENT_HELLO);
  117. if ($testtype == PSK_LAST_FIRST_CH) {
  118. $message->set_extension(TLSProxy::Message::EXT_FORCE_LAST, "");
  119. } else {
  120. #Deliberately break the connection
  121. $message->set_extension(TLSProxy::Message::EXT_SUPPORTED_GROUPS, "");
  122. }
  123. $message->repack();
  124. }
  125. sub remove_sig_algs_filter
  126. {
  127. my $proxy = shift;
  128. my $message;
  129. # Only look at the first ClientHello
  130. return if $proxy->flight != 0;
  131. $message = ${$proxy->message_list}[0];
  132. $message->delete_extension(TLSProxy::Message::EXT_SIG_ALGS);
  133. $message->repack();
  134. }