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_tls13cookie"; 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 29use constant { 30 COOKIE_ONLY => 0, 31 COOKIE_AND_KEY_SHARE => 1 32}; 33 34my $proxy = TLSProxy::Proxy->new( 35 undef, 36 cmdstr(app(["openssl"]), display => 1), 37 srctop_file("apps", "server.pem"), 38 (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE}) 39); 40 41my $cookieseen = 0; 42my $testtype; 43 44#Test 1: Inserting a cookie into an HRR should see it echoed in the ClientHello 45$testtype = COOKIE_ONLY; 46$proxy->filter(\&cookie_filter); 47$proxy->serverflags("-curves X25519") if !disabled("ec"); 48$proxy->start() or plan skip_all => "Unable to start up Proxy for tests"; 49plan tests => 2; 50SKIP: { 51 skip "EC disabled", 1, if disabled("ec"); 52 ok(TLSProxy::Message->success() && $cookieseen == 1, "Cookie seen"); 53} 54 55 56 57#Test 2: Same as test 1 but should also work where a new key_share is also 58# required 59$testtype = COOKIE_AND_KEY_SHARE; 60$proxy->clear(); 61if (disabled("ec")) { 62 $proxy->clientflags("-curves ffdhe3072:ffdhe2048"); 63 $proxy->serverflags("-curves ffdhe2048"); 64} else { 65 $proxy->clientflags("-curves P-256:X25519"); 66 $proxy->serverflags("-curves X25519"); 67} 68$proxy->start(); 69ok(TLSProxy::Message->success() && $cookieseen == 1, "Cookie seen"); 70 71sub cookie_filter 72{ 73 my $proxy = shift; 74 75 # We're only interested in the HRR and both ClientHellos 76 return if ($proxy->flight > 2); 77 78 my $ext = pack "C8", 79 0x00, 0x06, #Cookie Length 80 0x00, 0x01, #Dummy cookie data (6 bytes) 81 0x02, 0x03, 82 0x04, 0x05; 83 84 foreach my $message (@{$proxy->message_list}) { 85 if ($message->mt == TLSProxy::Message::MT_SERVER_HELLO 86 && ${$message->records}[0]->flight == 1) { 87 $message->delete_extension(TLSProxy::Message::EXT_KEY_SHARE) 88 if ($testtype == COOKIE_ONLY); 89 $message->set_extension(TLSProxy::Message::EXT_COOKIE, $ext); 90 $message->repack(); 91 } elsif ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) { 92 if (${$message->records}[0]->flight == 0) { 93 if ($testtype == COOKIE_ONLY) { 94 my $ext = pack "C7", 95 0x00, 0x05, #List Length 96 0x00, 0x17, #P-256 97 0x00, 0x01, #key_exchange data length 98 0xff; #Dummy key_share data 99 # Trick the server into thinking we got an unacceptable 100 # key_share 101 $message->set_extension( 102 TLSProxy::Message::EXT_KEY_SHARE, $ext); 103 $message->repack(); 104 } 105 } else { 106 #cmp can behave differently dependent on locale 107 no locale; 108 my $cookie = 109 $message->extension_data->{TLSProxy::Message::EXT_COOKIE}; 110 111 return if !defined($cookie); 112 113 return if ($cookie cmp $ext) != 0; 114 115 $cookieseen = 1; 116 } 117 } 118 } 119} 120