1#! /usr/bin/env perl
2# Copyright 2023 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_certtypeext";
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 TLSv1.2 enabled"
27    if disabled("tls1_2");
28
29my $proxy = TLSProxy::Proxy->new(
30    \&certtype_filter,
31    cmdstr(app(["openssl"]), display => 1),
32    srctop_file("apps", "server.pem"),
33    (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
34);
35
36use constant {
37    SERVER_CERT_TYPE => 0,
38    CLIENT_CERT_TYPE => 1,
39    NO_CERT_TYPE => 2
40};
41my $testtype;
42
43# Test 1: Just do a verify without cert type
44$proxy->clear();
45$proxy->clientflags("-tls1_2 -cert ".srctop_file("apps", "server.pem"));
46$proxy->serverflags("-verify 4");
47$testtype = NO_CERT_TYPE;
48$proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
49plan tests => 4;
50ok(TLSProxy::Message->success, "Simple verify");
51
52# Test 2: Set a bogus server cert type
53$proxy->clear();
54$proxy->serverflags("-enable_server_rpk");
55$testtype = SERVER_CERT_TYPE;
56$proxy->start();
57ok(TLSProxy::Message->fail, "Unsupported server cert type");
58
59# Test 3: Set a bogus client cert type
60$proxy->clear();
61$proxy->serverflags("-enable_client_rpk");
62$testtype = CLIENT_CERT_TYPE;
63$proxy->start();
64ok(TLSProxy::Message->success, "Unsupported client cert type, no verify");
65
66# Test 4: Set a bogus server cert type with verify
67$proxy->clear();
68$testtype = CLIENT_CERT_TYPE;
69$proxy->clientflags("-tls1_2 -cert ".srctop_file("apps", "server.pem"));
70$proxy->serverflags("-verify 4 -enable_client_rpk");
71$proxy->start();
72ok(TLSProxy::Message->fail, "Unsupported client cert type with verify");
73
74sub certtype_filter
75{
76    my $proxy = shift;
77    my $message;
78
79    # We're only interested in the initial ClientHello
80    return if $proxy->flight != 0;
81
82    $message = ${$proxy->message_list}[0];
83
84    # Add unsupported and bogus client and server cert type to the client hello.
85    my $ct = pack "C5", 0x04, 0x01, 0x03, 0x55, 0x66;
86    if ($testtype == CLIENT_CERT_TYPE) {
87        print "SETTING CLIENT CERT TYPE\n";
88        $message->set_extension(TLSProxy::Message::EXT_CLIENT_CERT_TYPE, $ct);
89    }
90    if ($testtype == SERVER_CERT_TYPE) {
91        print "SETTING SERVER CERT TYPE\n";
92        $message->set_extension(TLSProxy::Message::EXT_SERVER_CERT_TYPE, $ct);
93    }
94
95    $message->repack();
96}
97