70-test_comp.t 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #! /usr/bin/env perl
  2. # Copyright 2017-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 srctop_dir bldtop_dir/;
  10. use OpenSSL::Test::Utils;
  11. use File::Temp qw(tempfile);
  12. use TLSProxy::Proxy;
  13. my $test_name = "test_comp";
  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.3 or TLSv1.2 enabled"
  22. if disabled("tls1_3") && disabled("tls1_2");
  23. use constant {
  24. MULTIPLE_COMPRESSIONS => 0,
  25. NON_NULL_COMPRESSION => 1
  26. };
  27. my $testtype;
  28. my $proxy = TLSProxy::Proxy->new(
  29. undef,
  30. cmdstr(app(["openssl"]), display => 1),
  31. srctop_file("apps", "server.pem"),
  32. (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
  33. );
  34. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  35. plan tests => 4;
  36. SKIP: {
  37. skip "TLSv1.2 disabled", 2 if disabled("tls1_2");
  38. #Test 1: Check that sending multiple compression methods in a TLSv1.2
  39. # ClientHello succeeds
  40. $proxy->clear();
  41. $proxy->filter(\&add_comp_filter);
  42. $proxy->clientflags("-no_tls1_3");
  43. $testtype = MULTIPLE_COMPRESSIONS;
  44. $proxy->start();
  45. ok(TLSProxy::Message->success(), "Non null compression");
  46. #Test 2: NULL compression method must be present in TLSv1.2
  47. $proxy->clear();
  48. $proxy->clientflags("-no_tls1_3");
  49. $testtype = NON_NULL_COMPRESSION;
  50. $proxy->start();
  51. ok(TLSProxy::Message->fail(), "NULL compression missing");
  52. }
  53. SKIP: {
  54. skip "TLSv1.3 disabled", 2
  55. if disabled("tls1_3") || (disabled("ec") && disabled("dh"));
  56. #Test 3: Check that sending multiple compression methods in a TLSv1.3
  57. # ClientHello fails
  58. $proxy->clear();
  59. $proxy->filter(\&add_comp_filter);
  60. $testtype = MULTIPLE_COMPRESSIONS;
  61. $proxy->start();
  62. ok(TLSProxy::Message->fail(), "Non null compression (TLSv1.3)");
  63. #Test 4: NULL compression method must be present in TLSv1.3
  64. $proxy->clear();
  65. $testtype = NON_NULL_COMPRESSION;
  66. $proxy->start();
  67. ok(TLSProxy::Message->fail(), "NULL compression missing (TLSv1.3)");
  68. }
  69. sub add_comp_filter
  70. {
  71. my $proxy = shift;
  72. my $flight;
  73. my $message;
  74. my @comp;
  75. # Only look at the ClientHello
  76. return if $proxy->flight != 0;
  77. $message = ${$proxy->message_list}[0];
  78. return if (!defined $message
  79. || $message->mt != TLSProxy::Message::MT_CLIENT_HELLO);
  80. if ($testtype == MULTIPLE_COMPRESSIONS) {
  81. @comp = (
  82. 0x00, #Null compression method
  83. 0xff); #Unknown compression
  84. } elsif ($testtype == NON_NULL_COMPRESSION) {
  85. @comp = (0xff); #Unknown compression
  86. }
  87. $message->comp_meths(\@comp);
  88. $message->comp_meth_len(scalar @comp);
  89. $message->repack();
  90. }