70-test_tls13psk.t 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #! /usr/bin/env perl
  2. # Copyright 2017 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|MSWin32)$/;
  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 just checks sending a PSK
  36. #extension when it isn't in the last place in a ClientHello
  37. #Test 1: First get a session
  38. (undef, my $session) = tempfile();
  39. $proxy->clientflags("-sess_out ".$session);
  40. $proxy->sessionfile($session);
  41. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  42. plan tests => 4;
  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. # ciperhsuite. Should see PSK on second ClientHello
  53. $proxy->clear();
  54. $proxy->clientflags("-sess_in ".$session);
  55. $proxy->serverflags("-curves P-256");
  56. $proxy->filter(undef);
  57. $proxy->start();
  58. #Check if the PSK is present in the second ClientHello
  59. my $ch2 = ${$proxy->message_list}[2];
  60. my $ch2seen = defined $ch2 && $ch2->mt() == TLSProxy::Message::MT_CLIENT_HELLO;
  61. my $pskseen = $ch2seen
  62. && defined ${$ch2->{extension_data}}{TLSProxy::Message::EXT_PSK};
  63. ok($pskseen, "PSK hash matches");
  64. #Test 4: Attempt a resume after an HRR where PSK hash does not match selected
  65. # ciphersuite. Should not see PSK on second ClientHello
  66. $proxy->clear();
  67. $proxy->clientflags("-sess_in ".$session);
  68. $proxy->filter(\&modify_psk_filter);
  69. $proxy->serverflags("-curves P-256");
  70. $proxy->cipherc("TLS13-AES-128-GCM-SHA256:TLS13-AES-256-GCM-SHA384");
  71. $proxy->ciphers("TLS13-AES-256-GCM-SHA384");
  72. #We force an early failure because TLS Proxy doesn't actually support
  73. #TLS13-AES-256-GCM-SHA384. That doesn't matter for this test though.
  74. $testtype = ILLEGAL_EXT_SECOND_CH;
  75. $proxy->start();
  76. #Check if the PSK is present in the second ClientHello
  77. $ch2 = ${$proxy->message_list}[2];
  78. $ch2seen = defined $ch2 && $ch2->mt() == TLSProxy::Message::MT_CLIENT_HELLO;
  79. $pskseen = $ch2seen
  80. && defined ${$ch2->extension_data}{TLSProxy::Message::EXT_PSK};
  81. ok($ch2seen && !$pskseen, "PSK hash does not match");
  82. unlink $session;
  83. sub modify_psk_filter
  84. {
  85. my $proxy = shift;
  86. my $flight;
  87. my $message;
  88. if ($testtype == PSK_LAST_FIRST_CH) {
  89. $flight = 0;
  90. } else {
  91. $flight = 2;
  92. }
  93. # Only look at the first or second ClientHello
  94. return if $proxy->flight != $flight;
  95. if ($testtype == PSK_LAST_FIRST_CH) {
  96. $message = ${$proxy->message_list}[0];
  97. } else {
  98. $message = ${$proxy->message_list}[2];
  99. }
  100. return if (!defined $message
  101. || $message->mt != TLSProxy::Message::MT_CLIENT_HELLO);
  102. if ($testtype == PSK_LAST_FIRST_CH) {
  103. $message->set_extension(TLSProxy::Message::EXT_FORCE_LAST, "");
  104. } else {
  105. #Deliberately break the connection
  106. $message->set_extension(TLSProxy::Message::EXT_SUPPORTED_GROUPS, "");
  107. }
  108. $message->repack();
  109. }