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
9use strict;
10use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
11use OpenSSL::Test::Utils;
12use TLSProxy::Proxy;
13
14my $test_name = "test_tls13hrr";
15setup($test_name);
16
17plan skip_all => "TLSProxy isn't usable on $^O"
18    if $^O =~ /^(VMS)$/;
19
20plan skip_all => "$test_name needs the dynamic engine feature enabled"
21    if disabled("engine") || disabled("dynamic-engine");
22
23plan skip_all => "$test_name needs the sock feature enabled"
24    if disabled("sock");
25
26plan skip_all => "$test_name needs TLS1.3 enabled"
27    if disabled("tls1_3") || (disabled("ec") && disabled("dh"));
28
29my $proxy = TLSProxy::Proxy->new(
30    undef,
31    cmdstr(app(["openssl"]), display => 1),
32    srctop_file("apps", "server.pem"),
33    (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
34);
35
36use constant {
37    CHANGE_HRR_CIPHERSUITE => 0,
38    CHANGE_CH1_CIPHERSUITE => 1
39};
40
41#Test 1: A client should fail if the server changes the ciphersuite between the
42#        HRR and the SH
43$proxy->filter(\&hrr_filter);
44if (disabled("ec")) {
45    $proxy->serverflags("-curves ffdhe3072");
46} else {
47    $proxy->serverflags("-curves P-256");
48}
49my $testtype = CHANGE_HRR_CIPHERSUITE;
50$proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
51plan tests => 2;
52ok(TLSProxy::Message->fail(), "Server ciphersuite changes");
53
54#Test 2: It is an error if the client changes the offered ciphersuites so that
55#        we end up selecting a different ciphersuite between HRR and the SH
56$proxy->clear();
57if (disabled("ec")) {
58    $proxy->serverflags("-curves ffdhe3072");
59} else {
60    $proxy->serverflags("-curves P-256");
61}
62$proxy->ciphersuitess("TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384");
63$testtype = CHANGE_CH1_CIPHERSUITE;
64$proxy->start();
65ok(TLSProxy::Message->fail(), "Client ciphersuite changes");
66
67sub hrr_filter
68{
69    my $proxy = shift;
70
71    if ($testtype == CHANGE_HRR_CIPHERSUITE) {
72        # We're only interested in the HRR
73        if ($proxy->flight != 1) {
74            return;
75        }
76
77        my $hrr = ${$proxy->message_list}[1];
78
79        # We will normally only ever select CIPHER_TLS13_AES_128_GCM_SHA256
80        # because that's what Proxy tells s_server to do. Setting as below means
81        # the ciphersuite will change will we get the ServerHello
82        $hrr->ciphersuite(TLSProxy::Message::CIPHER_TLS13_AES_256_GCM_SHA384);
83        $hrr->repack();
84        return;
85    }
86
87    # CHANGE_CH1_CIPHERSUITE
88    if ($proxy->flight != 0) {
89        return;
90    }
91
92    my $ch1 = ${$proxy->message_list}[0];
93
94    # The server will always pick TLS_AES_256_GCM_SHA384
95    my @ciphersuites = (TLSProxy::Message::CIPHER_TLS13_AES_128_GCM_SHA256);
96    $ch1->ciphersuite_len(2 * scalar @ciphersuites);
97    $ch1->ciphersuites(\@ciphersuites);
98    $ch1->repack();
99}
100