70-test_sslcbcpadding.t 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #! /usr/bin/env perl
  2. # Copyright 2016-2024 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 feature 'state';
  10. use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
  11. use OpenSSL::Test::Utils;
  12. use TLSProxy::Proxy;
  13. my $test_name = "test_sslcbcpadding";
  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.2 enabled"
  22. if disabled("tls1_2");
  23. my $proxy = TLSProxy::Proxy->new(
  24. \&add_maximal_padding_filter,
  25. cmdstr(app(["openssl"]), display => 1),
  26. srctop_file("apps", "server.pem"),
  27. (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
  28. );
  29. # TODO: We could test all 256 values, but then the log file gets too large for
  30. # CI. See https://github.com/openssl/openssl/issues/1440.
  31. my @test_offsets = (0, 128, 254, 255);
  32. # Test that maximally-padded records are accepted.
  33. my $bad_padding_offset = -1;
  34. $proxy->serverflags("-tls1_2");
  35. $proxy->clientflags("-no_tls1_3");
  36. $proxy->serverconnects(1 + scalar(@test_offsets));
  37. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  38. plan tests => 1 + scalar(@test_offsets);
  39. ok(TLSProxy::Message->success(), "Maximally-padded record test");
  40. # Test that invalid padding is rejected.
  41. my $fatal_alert; # set by add_maximal_padding_filter on client's fatal alert
  42. foreach my $offset (@test_offsets) {
  43. $bad_padding_offset = $offset;
  44. $fatal_alert = 0;
  45. $proxy->clearClient();
  46. $proxy->clientflags("-no_tls1_3");
  47. $proxy->clientstart();
  48. ok($fatal_alert, "Invalid padding byte $bad_padding_offset");
  49. }
  50. sub add_maximal_padding_filter
  51. {
  52. my $proxy = shift;
  53. my $messages = $proxy->message_list;
  54. state $sent_corrupted_payload;
  55. if ($proxy->flight == 0) {
  56. # Disable Encrypt-then-MAC.
  57. foreach my $message (@{$messages}) {
  58. if ($message->mt != TLSProxy::Message::MT_CLIENT_HELLO) {
  59. next;
  60. }
  61. $message->delete_extension(TLSProxy::Message::EXT_ENCRYPT_THEN_MAC);
  62. $message->process_extensions();
  63. $message->repack();
  64. }
  65. $sent_corrupted_payload = 0;
  66. return;
  67. }
  68. my $last_message = @{$messages}[-1];
  69. if (defined($last_message)
  70. && $last_message->server
  71. && $last_message->mt == TLSProxy::Message::MT_FINISHED
  72. && !@{$last_message->records}[0]->{sent}) {
  73. # Insert a maximally-padded record. Assume a block size of 16 (AES) and
  74. # a MAC length of 20 (SHA-1).
  75. my $block_size = 16;
  76. my $mac_len = 20;
  77. # Size the plaintext so that 256 is a valid padding.
  78. my $plaintext_len = $block_size - ($mac_len % $block_size);
  79. my $plaintext = "A" x $plaintext_len;
  80. my $data = "B" x $block_size; # Explicit IV.
  81. $data .= $plaintext;
  82. $data .= TLSProxy::Proxy::fill_known_data($mac_len); # MAC.
  83. # Add padding.
  84. for (my $i = 0; $i < 256; $i++) {
  85. if ($i == $bad_padding_offset) {
  86. $sent_corrupted_payload = 1;
  87. $data .= "\xfe";
  88. } else {
  89. $data .= "\xff";
  90. }
  91. }
  92. my $record = TLSProxy::Record->new(
  93. $proxy->flight,
  94. TLSProxy::Record::RT_APPLICATION_DATA,
  95. TLSProxy::Record::VERS_TLS_1_2,
  96. length($data),
  97. 0,
  98. length($data),
  99. $plaintext_len,
  100. $data,
  101. $plaintext,
  102. );
  103. # Send the record immediately after the server Finished.
  104. push @{$proxy->record_list}, $record;
  105. } elsif ($sent_corrupted_payload) {
  106. # Check for bad_record_mac from client
  107. my $last_record = @{$proxy->record_list}[-1];
  108. $fatal_alert = 1 if $last_record->is_fatal_alert(0) == TLSProxy::Message::AL_DESC_BAD_RECORD_MAC;
  109. }
  110. }