Home | History | Annotate | Line # | Download | only in recipes
      1  1.1  christos #! /usr/bin/env perl
      2  1.1  christos # Copyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved.
      3  1.1  christos #
      4  1.1  christos # Licensed under the Apache License 2.0 (the "License").  You may not use
      5  1.1  christos # this file except in compliance with the License.  You can obtain a copy
      6  1.1  christos # in the file LICENSE in the source distribution or at
      7  1.1  christos # https://www.openssl.org/source/license.html
      8  1.1  christos 
      9  1.1  christos use strict;
     10  1.1  christos use List::Util 'first';
     11  1.1  christos use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
     12  1.1  christos use OpenSSL::Test::Utils;
     13  1.1  christos use TLSProxy::Proxy;
     14  1.1  christos 
     15  1.1  christos my $test_name = "test_renegotiation";
     16  1.1  christos setup($test_name);
     17  1.1  christos 
     18  1.1  christos plan skip_all => "TLSProxy isn't usable on $^O"
     19  1.1  christos     if $^O =~ /^(VMS)$/;
     20  1.1  christos 
     21  1.1  christos plan skip_all => "$test_name needs the dynamic engine feature enabled"
     22  1.1  christos     if disabled("engine") || disabled("dynamic-engine");
     23  1.1  christos 
     24  1.1  christos plan skip_all => "$test_name needs the sock feature enabled"
     25  1.1  christos     if disabled("sock");
     26  1.1  christos 
     27  1.1  christos plan skip_all => "$test_name needs TLS <= 1.2 enabled"
     28  1.1  christos     if alldisabled(("ssl3", "tls1", "tls1_1", "tls1_2"));
     29  1.1  christos 
     30  1.1  christos plan tests => 9;
     31  1.1  christos 
     32  1.1  christos my $proxy = TLSProxy::Proxy->new(
     33  1.1  christos     undef,
     34  1.1  christos     cmdstr(app(["openssl"]), display => 1),
     35  1.1  christos     srctop_file("apps", "server.pem"),
     36  1.1  christos     (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
     37  1.1  christos );
     38  1.1  christos 
     39  1.1  christos #Test 1: A basic renegotiation test
     40  1.1  christos $proxy->clientflags("-no_tls1_3");
     41  1.1  christos $proxy->serverflags("-client_renegotiation");
     42  1.1  christos $proxy->reneg(1);
     43  1.1  christos $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
     44  1.1  christos ok(TLSProxy::Message->success(), "Basic renegotiation");
     45  1.1  christos 
     46  1.1  christos #Test 2: Seclevel 0 client does not send the Reneg SCSV. Reneg should fail
     47  1.1  christos $proxy->clear();
     48  1.1  christos $proxy->filter(\&reneg_scsv_filter);
     49  1.1  christos $proxy->cipherc("DEFAULT:\@SECLEVEL=0");
     50  1.1  christos $proxy->clientflags("-no_tls1_3");
     51  1.1  christos $proxy->serverflags("-client_renegotiation");
     52  1.1  christos $proxy->reneg(1);
     53  1.1  christos $proxy->start();
     54  1.1  christos ok(TLSProxy::Message->fail(), "No client SCSV");
     55  1.1  christos 
     56  1.1  christos SKIP: {
     57  1.1  christos     skip "TLSv1.2 disabled", 1
     58  1.1  christos         if disabled("tls1_2");
     59  1.1  christos 
     60  1.1  christos     #Test 3: TLS 1.2 client does not send the Reneg extension. Reneg should fail
     61  1.1  christos 
     62  1.1  christos     $proxy->clear();
     63  1.1  christos     $proxy->cipherc("DEFAULT:\@SECLEVEL=2");
     64  1.1  christos     $proxy->filter(\&reneg_ext_filter);
     65  1.1  christos     $proxy->clientflags("-no_tls1_3");
     66  1.1  christos     $proxy->serverflags("-client_renegotiation");
     67  1.1  christos     $proxy->reneg(1);
     68  1.1  christos     $proxy->start();
     69  1.1  christos     ok(TLSProxy::Message->fail(), "No client extension");
     70  1.1  christos }
     71  1.1  christos 
     72  1.1  christos SKIP: {
     73  1.1  christos     skip "TLSv1.2 or TLSv1.1 disabled", 1
     74  1.1  christos         if disabled("tls1_2") || disabled("tls1_1");
     75  1.1  christos     #Test 4: Check that the ClientHello version remains the same in the reneg
     76  1.1  christos     #        handshake
     77  1.1  christos     $proxy->clear();
     78  1.1  christos     $proxy->filter(undef);
     79  1.1  christos     $proxy->ciphers("DEFAULT:\@SECLEVEL=0");
     80  1.1  christos     $proxy->clientflags("-no_tls1_3 -cipher AES128-SHA:\@SECLEVEL=0");
     81  1.1  christos     $proxy->serverflags("-no_tls1_3 -no_tls1_2 -client_renegotiation");
     82  1.1  christos     $proxy->reneg(1);
     83  1.1  christos     $proxy->start();
     84  1.1  christos     my $chversion;
     85  1.1  christos     my $chmatch = 0;
     86  1.1  christos     foreach my $message (@{$proxy->message_list}) {
     87  1.1  christos         if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
     88  1.1  christos             if (!defined $chversion) {
     89  1.1  christos                 $chversion = $message->client_version;
     90  1.1  christos             } else {
     91  1.1  christos                 if ($chversion == $message->client_version) {
     92  1.1  christos                     $chmatch = 1;
     93  1.1  christos                 }
     94  1.1  christos             }
     95  1.1  christos         }
     96  1.1  christos     }
     97  1.1  christos     ok(TLSProxy::Message->success() && $chmatch,
     98  1.1  christos        "Check ClientHello version is the same");
     99  1.1  christos }
    100  1.1  christos 
    101  1.1  christos SKIP: {
    102  1.1  christos     skip "TLSv1.2 disabled", 1
    103  1.1  christos         if disabled("tls1_2");
    104  1.1  christos 
    105  1.1  christos     #Test 5: Test for CVE-2021-3449. client_sig_algs instead of sig_algs in
    106  1.1  christos     #        resumption ClientHello
    107  1.1  christos     $proxy->clear();
    108  1.1  christos     $proxy->filter(\&sigalgs_filter);
    109  1.1  christos     $proxy->clientflags("-tls1_2");
    110  1.1  christos     $proxy->serverflags("-client_renegotiation");
    111  1.1  christos     $proxy->reneg(1);
    112  1.1  christos     $proxy->start();
    113  1.1  christos     ok(TLSProxy::Message->fail(), "client_sig_algs instead of sig_algs");
    114  1.1  christos }
    115  1.1  christos 
    116  1.1  christos SKIP: {
    117  1.1  christos     skip "TLSv1.2 and TLSv1.1 disabled", 1
    118  1.1  christos         if disabled("tls1_2") && disabled("tls1_1");
    119  1.1  christos     #Test 6: Client fails to do renegotiation
    120  1.1  christos     $proxy->clear();
    121  1.1  christos     $proxy->filter(undef);
    122  1.1  christos     $proxy->serverflags("-no_tls1_3");
    123  1.1  christos     $proxy->clientflags("-no_tls1_3");
    124  1.1  christos     $proxy->reneg(1);
    125  1.1  christos     $proxy->start();
    126  1.1  christos     ok(TLSProxy::Message->fail(),
    127  1.1  christos         "Check client renegotiation failed");
    128  1.1  christos }
    129  1.1  christos 
    130  1.1  christos SKIP: {
    131  1.1  christos     skip "TLSv1 disabled", 1
    132  1.1  christos         if disabled("tls1");
    133  1.1  christos 
    134  1.1  christos     #Test 7: Check that SECLEVEL 0 sends SCSV not RI extension
    135  1.1  christos     $proxy->clear();
    136  1.1  christos     $proxy->filter(undef);
    137  1.1  christos     $proxy->cipherc("DEFAULT:\@SECLEVEL=0");
    138  1.1  christos     $proxy->start();
    139  1.1  christos 
    140  1.1  christos     my $clientHello = first { $_->mt == TLSProxy::Message::MT_CLIENT_HELLO } @{$proxy->message_list};
    141  1.1  christos     my $has_scsv = 255 ~~ @{$clientHello->ciphersuites};
    142  1.1  christos     my $has_ri_extension = exists $clientHello->extension_data()->{TLSProxy::Message::EXT_RENEGOTIATE};
    143  1.1  christos 
    144  1.1  christos     ok($has_scsv && !$has_ri_extension, "SECLEVEL=0 should use SCSV not RI extension by default");
    145  1.1  christos }
    146  1.1  christos 
    147  1.1  christos SKIP: {
    148  1.1  christos     skip "TLSv1.2 disabled", 1
    149  1.1  christos         if disabled("tls1_2");
    150  1.1  christos 
    151  1.1  christos     #Test 8: Check that SECLEVEL0 + TLS 1.2 sends RI extension not SCSV
    152  1.1  christos     $proxy->clear();
    153  1.1  christos     $proxy->filter(undef);
    154  1.1  christos     $proxy->cipherc("DEFAULT:\@SECLEVEL=0");
    155  1.1  christos     $proxy->clientflags("-tls1_2");
    156  1.1  christos     $proxy->start();
    157  1.1  christos 
    158  1.1  christos     my $clientHello = first { $_->mt == TLSProxy::Message::MT_CLIENT_HELLO } @{$proxy->message_list};
    159  1.1  christos     my $has_scsv = 255 ~~ @{$clientHello->ciphersuites};
    160  1.1  christos     my $has_ri_extension = exists $clientHello->extension_data()->{TLSProxy::Message::EXT_RENEGOTIATE};
    161  1.1  christos 
    162  1.1  christos     ok(!$has_scsv && $has_ri_extension, "TLS1.2 should use RI extension despite SECLEVEL=0");
    163  1.1  christos }
    164  1.1  christos 
    165  1.1  christos 
    166  1.1  christos SKIP: {
    167  1.1  christos     skip "TLSv1.3 disabled", 1
    168  1.1  christos         if disabled("tls1_3");
    169  1.1  christos 
    170  1.1  christos     #Test 9: Check that TLS 1.3 sends neither RI extension nor SCSV
    171  1.1  christos     $proxy->clear();
    172  1.1  christos     $proxy->filter(undef);
    173  1.1  christos     $proxy->clientflags("-tls1_3");
    174  1.1  christos     $proxy->start();
    175  1.1  christos 
    176  1.1  christos     my $clientHello = first { $_->mt == TLSProxy::Message::MT_CLIENT_HELLO } @{$proxy->message_list};
    177  1.1  christos     my $has_scsv = 255 ~~ @{$clientHello->ciphersuites};
    178  1.1  christos     my $has_ri_extension = exists $clientHello->extension_data()->{TLSProxy::Message::EXT_RENEGOTIATE};
    179  1.1  christos 
    180  1.1  christos     ok(!$has_scsv && !$has_ri_extension, "TLS1.3 should not use RI extension or SCSV");
    181  1.1  christos }
    182  1.1  christos 
    183  1.1  christos sub reneg_scsv_filter
    184  1.1  christos {
    185  1.1  christos     my $proxy = shift;
    186  1.1  christos 
    187  1.1  christos     # We're only interested in the initial ClientHello message
    188  1.1  christos     if ($proxy->flight != 0) {
    189  1.1  christos         return;
    190  1.1  christos     }
    191  1.1  christos 
    192  1.1  christos     foreach my $message (@{$proxy->message_list}) {
    193  1.1  christos         if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
    194  1.1  christos             #Remove any SCSV ciphersuites - just leave AES128-SHA (0x002f)
    195  1.1  christos             my @ciphersuite = (0x002f);
    196  1.1  christos             $message->ciphersuites(\@ciphersuite);
    197  1.1  christos             $message->ciphersuite_len(2);
    198  1.1  christos             $message->repack();
    199  1.1  christos         }
    200  1.1  christos     }
    201  1.1  christos }
    202  1.1  christos 
    203  1.1  christos sub reneg_ext_filter
    204  1.1  christos {
    205  1.1  christos     my $proxy = shift;
    206  1.1  christos 
    207  1.1  christos     # We're only interested in the initial ClientHello message
    208  1.1  christos     if ($proxy->flight != 0) {
    209  1.1  christos         return;
    210  1.1  christos     }
    211  1.1  christos 
    212  1.1  christos     foreach my $message (@{$proxy->message_list}) {
    213  1.1  christos         if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
    214  1.1  christos             $message->delete_extension(TLSProxy::Message::EXT_RENEGOTIATE);
    215  1.1  christos             $message->repack();
    216  1.1  christos         }
    217  1.1  christos     }
    218  1.1  christos }
    219  1.1  christos 
    220  1.1  christos sub sigalgs_filter
    221  1.1  christos {
    222  1.1  christos     my $proxy = shift;
    223  1.1  christos     my $cnt = 0;
    224  1.1  christos 
    225  1.1  christos     # We're only interested in the second ClientHello message
    226  1.1  christos     foreach my $message (@{$proxy->message_list}) {
    227  1.1  christos         if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
    228  1.1  christos             next if ($cnt++ == 0);
    229  1.1  christos 
    230  1.1  christos             my $sigs = pack "C10", 0x00, 0x08,
    231  1.1  christos                             # rsa_pkcs_sha{256,384,512,1}
    232  1.1  christos                             0x04, 0x01,  0x05, 0x01,  0x06, 0x01,  0x02, 0x01;
    233  1.1  christos             $message->set_extension(TLSProxy::Message::EXT_SIG_ALGS_CERT, $sigs);
    234  1.1  christos             $message->delete_extension(TLSProxy::Message::EXT_SIG_ALGS);
    235  1.1  christos             $message->repack();
    236  1.1  christos         }
    237  1.1  christos     }
    238  1.1  christos }
    239