70-test_sslcbcpadding.t 4.1 KB

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