xref: /freebsd/crypto/openssl/test/recipes/70-test_npn.t (revision 96190b4fef3b4a0cc3ca0606b0c4e3e69a5e6717)
1#! /usr/bin/env perl
2# Copyright 2024 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/;
11use OpenSSL::Test::Utils;
12
13use TLSProxy::Proxy;
14
15my $test_name = "test_npn";
16setup($test_name);
17
18plan skip_all => "TLSProxy isn't usable on $^O"
19    if $^O =~ /^(VMS)$/;
20
21plan skip_all => "$test_name needs the dynamic engine feature enabled"
22    if disabled("engine") || disabled("dynamic-engine");
23
24plan skip_all => "$test_name needs the sock feature enabled"
25    if disabled("sock");
26
27plan skip_all => "$test_name needs NPN enabled"
28    if disabled("nextprotoneg");
29
30plan skip_all => "$test_name needs TLSv1.2 enabled"
31    if disabled("tls1_2");
32
33my $proxy = TLSProxy::Proxy->new(
34    undef,
35    cmdstr(app(["openssl"]), display => 1),
36    srctop_file("apps", "server.pem"),
37    (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
38);
39
40$proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
41plan tests => 1;
42
43my $npnseen = 0;
44
45# Test 1: Check sending an empty NextProto message from the client works. This is
46#         valid as per the spec, but OpenSSL does not allow you to send it.
47#         Therefore we must be prepared to receive such a message but we cannot
48#         generate it except via TLSProxy
49$proxy->clear();
50$proxy->filter(\&npn_filter);
51$proxy->clientflags("-nextprotoneg foo -no_tls1_3");
52$proxy->serverflags("-nextprotoneg foo");
53$proxy->start();
54ok($npnseen && TLSProxy::Message->success(), "Empty NPN message");
55
56sub npn_filter
57{
58    my $proxy = shift;
59    my $message;
60
61    # The NextProto message always appears in flight 2
62    return if $proxy->flight != 2;
63
64    foreach my $message (@{$proxy->message_list}) {
65        if ($message->mt == TLSProxy::Message::MT_NEXT_PROTO) {
66            # Our TLSproxy NextProto message support doesn't support parsing of
67            # the message. If we repack it just creates an empty NextProto
68            # message - which is exactly the scenario we want to test here.
69            $message->repack();
70            $npnseen = 1;
71        }
72    }
73}
74