70-test_tls13psk.t 4.8 KB

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