70-test_sslcbcpadding.t 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #! /usr/bin/env perl
  2. # Copyright 2016 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 bldtop_dir/;
  10. use OpenSSL::Test::Utils;
  11. use TLSProxy::Proxy;
  12. my $test_name = "test_sslcbcpadding";
  13. setup($test_name);
  14. plan skip_all => "TLSProxy isn't usable on $^O"
  15. if $^O =~ /^(VMS|MSWin32)$/;
  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 TLSv1.2 enabled"
  21. if disabled("tls1_2");
  22. $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
  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->start() or plan skip_all => "Unable to start up Proxy for tests";
  36. plan tests => 1 + scalar(@test_offsets);
  37. ok(TLSProxy::Message->success(), "Maximally-padded record test");
  38. # Test that invalid padding is rejected.
  39. foreach my $offset (@test_offsets) {
  40. $proxy->clear();
  41. $proxy->serverflags("-tls1_2");
  42. $bad_padding_offset = $offset;
  43. $proxy->start();
  44. ok(TLSProxy::Message->fail(), "Invalid padding byte $bad_padding_offset");
  45. }
  46. sub add_maximal_padding_filter
  47. {
  48. my $proxy = shift;
  49. if ($proxy->flight == 0) {
  50. # Disable Encrypt-then-MAC.
  51. foreach my $message (@{$proxy->message_list}) {
  52. if ($message->mt != TLSProxy::Message::MT_CLIENT_HELLO) {
  53. next;
  54. }
  55. $message->delete_extension(TLSProxy::Message::EXT_ENCRYPT_THEN_MAC);
  56. $message->process_extensions();
  57. $message->repack();
  58. }
  59. }
  60. if ($proxy->flight == 3) {
  61. # Insert a maximally-padded record. Assume a block size of 16 (AES) and
  62. # a MAC length of 20 (SHA-1).
  63. my $block_size = 16;
  64. my $mac_len = 20;
  65. # Size the plaintext so that 256 is a valid padding.
  66. my $plaintext_len = $block_size - ($mac_len % $block_size);
  67. my $plaintext = "A" x $plaintext_len;
  68. my $data = "B" x $block_size; # Explicit IV.
  69. $data .= $plaintext;
  70. $data .= TLSProxy::Proxy::fill_known_data($mac_len); # MAC.
  71. # Add padding.
  72. for (my $i = 0; $i < 256; $i++) {
  73. if ($i == $bad_padding_offset) {
  74. $data .= "\xfe";
  75. } else {
  76. $data .= "\xff";
  77. }
  78. }
  79. my $record = TLSProxy::Record->new(
  80. $proxy->flight,
  81. TLSProxy::Record::RT_APPLICATION_DATA,
  82. TLSProxy::Record::VERS_TLS_1_2,
  83. length($data),
  84. 0,
  85. length($data),
  86. $plaintext_len,
  87. $data,
  88. $plaintext,
  89. );
  90. # Send the record immediately after the server Finished.
  91. push @{$proxy->record_list}, $record;
  92. }
  93. }