1e1051a39Sopenharmony_ci#! /usr/bin/env perl 2e1051a39Sopenharmony_ci# Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci# 4e1051a39Sopenharmony_ci# Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci# this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci# in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci# https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci 9e1051a39Sopenharmony_ciuse strict; 10e1051a39Sopenharmony_ciuse OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/; 11e1051a39Sopenharmony_ciuse OpenSSL::Test::Utils; 12e1051a39Sopenharmony_ciuse TLSProxy::Proxy; 13e1051a39Sopenharmony_ci 14e1051a39Sopenharmony_cimy $test_name = "test_tls13cookie"; 15e1051a39Sopenharmony_cisetup($test_name); 16e1051a39Sopenharmony_ci 17e1051a39Sopenharmony_ciplan skip_all => "TLSProxy isn't usable on $^O" 18e1051a39Sopenharmony_ci if $^O =~ /^(VMS)$/; 19e1051a39Sopenharmony_ci 20e1051a39Sopenharmony_ciplan skip_all => "$test_name needs the dynamic engine feature enabled" 21e1051a39Sopenharmony_ci if disabled("engine") || disabled("dynamic-engine"); 22e1051a39Sopenharmony_ci 23e1051a39Sopenharmony_ciplan skip_all => "$test_name needs the sock feature enabled" 24e1051a39Sopenharmony_ci if disabled("sock"); 25e1051a39Sopenharmony_ci 26e1051a39Sopenharmony_ciplan skip_all => "$test_name needs TLS1.3 enabled" 27e1051a39Sopenharmony_ci if disabled("tls1_3") || (disabled("ec") && disabled("dh")); 28e1051a39Sopenharmony_ci 29e1051a39Sopenharmony_ci$ENV{OPENSSL_ia32cap} = '~0x200000200000000'; 30e1051a39Sopenharmony_ci 31e1051a39Sopenharmony_ciuse constant { 32e1051a39Sopenharmony_ci COOKIE_ONLY => 0, 33e1051a39Sopenharmony_ci COOKIE_AND_KEY_SHARE => 1 34e1051a39Sopenharmony_ci}; 35e1051a39Sopenharmony_ci 36e1051a39Sopenharmony_cimy $proxy = TLSProxy::Proxy->new( 37e1051a39Sopenharmony_ci undef, 38e1051a39Sopenharmony_ci cmdstr(app(["openssl"]), display => 1), 39e1051a39Sopenharmony_ci srctop_file("apps", "server.pem"), 40e1051a39Sopenharmony_ci (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE}) 41e1051a39Sopenharmony_ci); 42e1051a39Sopenharmony_ci 43e1051a39Sopenharmony_cimy $cookieseen = 0; 44e1051a39Sopenharmony_cimy $testtype; 45e1051a39Sopenharmony_ci 46e1051a39Sopenharmony_ci#Test 1: Inserting a cookie into an HRR should see it echoed in the ClientHello 47e1051a39Sopenharmony_ci$testtype = COOKIE_ONLY; 48e1051a39Sopenharmony_ci$proxy->filter(\&cookie_filter); 49e1051a39Sopenharmony_ci$proxy->serverflags("-curves X25519") if !disabled("ec"); 50e1051a39Sopenharmony_ci$proxy->start() or plan skip_all => "Unable to start up Proxy for tests"; 51e1051a39Sopenharmony_ciplan tests => 2; 52e1051a39Sopenharmony_ciSKIP: { 53e1051a39Sopenharmony_ci skip "EC disabled", 1, if disabled("ec"); 54e1051a39Sopenharmony_ci ok(TLSProxy::Message->success() && $cookieseen == 1, "Cookie seen"); 55e1051a39Sopenharmony_ci} 56e1051a39Sopenharmony_ci 57e1051a39Sopenharmony_ci 58e1051a39Sopenharmony_ci 59e1051a39Sopenharmony_ci#Test 2: Same as test 1 but should also work where a new key_share is also 60e1051a39Sopenharmony_ci# required 61e1051a39Sopenharmony_ci$testtype = COOKIE_AND_KEY_SHARE; 62e1051a39Sopenharmony_ci$proxy->clear(); 63e1051a39Sopenharmony_ciif (disabled("ec")) { 64e1051a39Sopenharmony_ci $proxy->clientflags("-curves ffdhe3072:ffdhe2048"); 65e1051a39Sopenharmony_ci $proxy->serverflags("-curves ffdhe2048"); 66e1051a39Sopenharmony_ci} else { 67e1051a39Sopenharmony_ci $proxy->clientflags("-curves P-256:X25519"); 68e1051a39Sopenharmony_ci $proxy->serverflags("-curves X25519"); 69e1051a39Sopenharmony_ci} 70e1051a39Sopenharmony_ci$proxy->start(); 71e1051a39Sopenharmony_ciok(TLSProxy::Message->success() && $cookieseen == 1, "Cookie seen"); 72e1051a39Sopenharmony_ci 73e1051a39Sopenharmony_cisub cookie_filter 74e1051a39Sopenharmony_ci{ 75e1051a39Sopenharmony_ci my $proxy = shift; 76e1051a39Sopenharmony_ci 77e1051a39Sopenharmony_ci # We're only interested in the HRR and both ClientHellos 78e1051a39Sopenharmony_ci return if ($proxy->flight > 2); 79e1051a39Sopenharmony_ci 80e1051a39Sopenharmony_ci my $ext = pack "C8", 81e1051a39Sopenharmony_ci 0x00, 0x06, #Cookie Length 82e1051a39Sopenharmony_ci 0x00, 0x01, #Dummy cookie data (6 bytes) 83e1051a39Sopenharmony_ci 0x02, 0x03, 84e1051a39Sopenharmony_ci 0x04, 0x05; 85e1051a39Sopenharmony_ci 86e1051a39Sopenharmony_ci foreach my $message (@{$proxy->message_list}) { 87e1051a39Sopenharmony_ci if ($message->mt == TLSProxy::Message::MT_SERVER_HELLO 88e1051a39Sopenharmony_ci && ${$message->records}[0]->flight == 1) { 89e1051a39Sopenharmony_ci $message->delete_extension(TLSProxy::Message::EXT_KEY_SHARE) 90e1051a39Sopenharmony_ci if ($testtype == COOKIE_ONLY); 91e1051a39Sopenharmony_ci $message->set_extension(TLSProxy::Message::EXT_COOKIE, $ext); 92e1051a39Sopenharmony_ci $message->repack(); 93e1051a39Sopenharmony_ci } elsif ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) { 94e1051a39Sopenharmony_ci if (${$message->records}[0]->flight == 0) { 95e1051a39Sopenharmony_ci if ($testtype == COOKIE_ONLY) { 96e1051a39Sopenharmony_ci my $ext = pack "C7", 97e1051a39Sopenharmony_ci 0x00, 0x05, #List Length 98e1051a39Sopenharmony_ci 0x00, 0x17, #P-256 99e1051a39Sopenharmony_ci 0x00, 0x01, #key_exchange data length 100e1051a39Sopenharmony_ci 0xff; #Dummy key_share data 101e1051a39Sopenharmony_ci # Trick the server into thinking we got an unacceptable 102e1051a39Sopenharmony_ci # key_share 103e1051a39Sopenharmony_ci $message->set_extension( 104e1051a39Sopenharmony_ci TLSProxy::Message::EXT_KEY_SHARE, $ext); 105e1051a39Sopenharmony_ci $message->repack(); 106e1051a39Sopenharmony_ci } 107e1051a39Sopenharmony_ci } else { 108e1051a39Sopenharmony_ci #cmp can behave differently dependent on locale 109e1051a39Sopenharmony_ci no locale; 110e1051a39Sopenharmony_ci my $cookie = 111e1051a39Sopenharmony_ci $message->extension_data->{TLSProxy::Message::EXT_COOKIE}; 112e1051a39Sopenharmony_ci 113e1051a39Sopenharmony_ci return if !defined($cookie); 114e1051a39Sopenharmony_ci 115e1051a39Sopenharmony_ci return if ($cookie cmp $ext) != 0; 116e1051a39Sopenharmony_ci 117e1051a39Sopenharmony_ci $cookieseen = 1; 118e1051a39Sopenharmony_ci } 119e1051a39Sopenharmony_ci } 120e1051a39Sopenharmony_ci } 121e1051a39Sopenharmony_ci} 122