2
0

70-test_sslsignature.t 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #! /usr/bin/env perl
  2. # Copyright 2016-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 bldtop_dir/;
  10. use OpenSSL::Test::Utils;
  11. use TLSProxy::Proxy;
  12. my $test_name = "test_sslsignature";
  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 TLS enabled"
  21. if alldisabled(available_protocols("tls"));
  22. my $proxy = TLSProxy::Proxy->new(
  23. undef,
  24. cmdstr(app(["openssl"]), display => 1),
  25. srctop_file("apps", "server.pem"),
  26. (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
  27. );
  28. use constant {
  29. NO_CORRUPTION => 0,
  30. CORRUPT_SERVER_CERT_VERIFY => 1,
  31. CORRUPT_CLIENT_CERT_VERIFY => 2,
  32. CORRUPT_TLS1_2_SERVER_KEY_EXCHANGE => 3,
  33. };
  34. $proxy->filter(\&signature_filter);
  35. #Test 1: No corruption should succeed
  36. my $testtype = NO_CORRUPTION;
  37. $proxy->clientflags("-no_tls1_3") if disabled("ec") && disabled("dh");
  38. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  39. plan tests => 4;
  40. ok(TLSProxy::Message->success, "No corruption");
  41. SKIP: {
  42. skip "TLSv1.3 disabled", 1
  43. if disabled("tls1_3") || (disabled("ec") && disabled("dh"));
  44. #Test 2: Corrupting a server CertVerify signature in TLSv1.3 should fail
  45. $proxy->clear();
  46. $testtype = CORRUPT_SERVER_CERT_VERIFY;
  47. $proxy->start();
  48. ok(TLSProxy::Message->fail, "Corrupt server TLSv1.3 CertVerify");
  49. #Test x: Corrupting a client CertVerify signature in TLSv1.3 should fail
  50. #$proxy->clear();
  51. #$testtype = CORRUPT_CLIENT_CERT_VERIFY;
  52. #$proxy->serverflags("-Verify 5");
  53. #$proxy->clientflags("-cert ".srctop_file("apps", "server.pem"));
  54. #$proxy->start();
  55. #ok(TLSProxy::Message->fail, "Corrupt client TLSv1.3 CertVerify");
  56. #TODO(TLS1.3): This test fails due to a problem in s_server/TLSProxy.
  57. #Currently a connection is counted as "successful" if the client ends it
  58. #with a close_notify. In TLSProxy the client initiates the closure of the
  59. #connection so really we should not count it as successful until s_server
  60. #has also responded with a close_notify. However s_server never sends a
  61. #close_notify - it just closes the connection. Fixing this would be a
  62. #significant change to the long established behaviour of s_server.
  63. #Unfortunately in this test, it is the server that notices the incorrect
  64. #signature and responds with an appropriate alert. However s_client never
  65. #sees that because it occurs after the server Finished has been sent.
  66. #Therefore s_client just continues to send its application data and sends
  67. #its close_notify regardless. TLSProxy sees this and thinks that the
  68. #connection was successful when in fact it was not. There isn't an easy fix
  69. #for this, so leaving this test commented out for now.
  70. }
  71. SKIP: {
  72. skip "TLS <= 1.2 disabled", 2
  73. if alldisabled(("ssl3", "tls1", "tls1_1", "tls1_2"));
  74. #Test 3: Corrupting a CertVerify signature in <=TLSv1.2 should fail
  75. $proxy->clear();
  76. $testtype = CORRUPT_CLIENT_CERT_VERIFY;
  77. $proxy->serverflags("-Verify 5");
  78. $proxy->clientflags("-no_tls1_3 -cert ".srctop_file("apps", "server.pem"));
  79. $proxy->start();
  80. ok(TLSProxy::Message->fail, "Corrupt <=TLSv1.2 CertVerify");
  81. SKIP: {
  82. skip "DH disabled", 1 if disabled("dh");
  83. #Test 4: Corrupting a ServerKeyExchange signature in <=TLSv1.2 should
  84. #fail
  85. $proxy->clear();
  86. $testtype = CORRUPT_TLS1_2_SERVER_KEY_EXCHANGE;
  87. $proxy->clientflags("-no_tls1_3");
  88. $proxy->cipherc('DHE-RSA-AES128-SHA');
  89. $proxy->ciphers('DHE-RSA-AES128-SHA');
  90. $proxy->start();
  91. ok(TLSProxy::Message->fail, "Corrupt <=TLSv1.2 ServerKeyExchange");
  92. }
  93. }
  94. sub signature_filter
  95. {
  96. my $proxy = shift;
  97. my $flight;
  98. my $mt = TLSProxy::Message::MT_CERTIFICATE_VERIFY;
  99. if ($testtype == CORRUPT_SERVER_CERT_VERIFY
  100. || $testtype == CORRUPT_TLS1_2_SERVER_KEY_EXCHANGE
  101. || (!disabled("tls1_3") && $testtype == NO_CORRUPTION)) {
  102. $flight = 1;
  103. } else {
  104. $flight = 2;
  105. }
  106. # We're only interested in the initial server flight
  107. return if ($proxy->flight != $flight);
  108. $mt = TLSProxy::Message::MT_SERVER_KEY_EXCHANGE
  109. if ($testtype == CORRUPT_TLS1_2_SERVER_KEY_EXCHANGE);
  110. foreach my $message (@{$proxy->message_list}) {
  111. if ($message->mt == $mt) {
  112. my $sig = $message->signature();
  113. my $sigbase = substr($sig, 0, -1);
  114. my $sigend = unpack("C", substr($sig, -1));
  115. #Flip bits in final byte of signature to corrupt the sig
  116. $sigend ^= 0xff unless $testtype == NO_CORRUPTION;
  117. $message->signature($sigbase.pack("C", $sigend));
  118. $message->repack();
  119. }
  120. }
  121. }