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