70-test_tls13psk.t 4.9 KB

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