1e0c4386eSCy Schubert#! /usr/bin/env perl 2*0d0c8621SEnji Cooper# Copyright 2007-2025 The OpenSSL Project Authors. All Rights Reserved. 3e0c4386eSCy Schubert# Copyright Nokia 2007-2019 4e0c4386eSCy Schubert# Copyright Siemens AG 2015-2019 5e0c4386eSCy Schubert# 6e0c4386eSCy Schubert# Licensed under the Apache License 2.0 (the "License"). You may not use 7e0c4386eSCy Schubert# this file except in compliance with the License. You can obtain a copy 8e0c4386eSCy Schubert# in the file LICENSE in the source distribution or at 9e0c4386eSCy Schubert# https://www.openssl.org/source/license.html 10e0c4386eSCy Schubert 11e0c4386eSCy Schubertuse strict; 12e0c4386eSCy Schubertuse warnings; 13e0c4386eSCy Schubert 14e0c4386eSCy Schubertuse POSIX; 15e0c4386eSCy Schubertuse OpenSSL::Test qw/:DEFAULT cmdstr data_file data_dir srctop_dir bldtop_dir result_dir/; 16e0c4386eSCy Schubertuse OpenSSL::Test::Utils; 17e0c4386eSCy Schubert 18e0c4386eSCy SchubertBEGIN { 19e0c4386eSCy Schubert setup("test_cmp_http"); 20e0c4386eSCy Schubert} 21e0c4386eSCy Schubertuse lib srctop_dir('Configurations'); 22e0c4386eSCy Schubertuse lib bldtop_dir('.'); 23e0c4386eSCy Schubert 24e0c4386eSCy Schubertplan skip_all => "These tests are not supported in a fuzz build" 25e0c4386eSCy Schubert if config('options') =~ /-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION|enable-fuzz-afl/; 26e0c4386eSCy Schubert 27e0c4386eSCy Schubertplan skip_all => "These tests are not supported in a no-cmp build" 28e0c4386eSCy Schubert if disabled("cmp"); 29e0c4386eSCy Schubertplan skip_all => "These tests are not supported in a no-ec build" 30e0c4386eSCy Schubert if disabled("ec"); 31e0c4386eSCy Schubertplan skip_all => "These tests are not supported in a no-sock build" 32e0c4386eSCy Schubert if disabled("sock"); 33e0c4386eSCy Schubert 34e0c4386eSCy Schubertplan skip_all => "Tests involving local HTTP server not available on Windows or VMS" 35e0c4386eSCy Schubert if $^O =~ /^(VMS|MSWin32|msys)$/; 36e0c4386eSCy Schubertplan skip_all => "Tests involving local HTTP server not available in cross-compile builds" 37e0c4386eSCy Schubert if defined $ENV{EXE_SHELL}; 38e0c4386eSCy Schubert 39e0c4386eSCy Schubertsub chop_dblquot { # chop any leading and trailing '"' (needed for Windows) 40e0c4386eSCy Schubert my $str = shift; 41e0c4386eSCy Schubert $str =~ s/^\"(.*?)\"$/$1/; 42e0c4386eSCy Schubert return $str; 43e0c4386eSCy Schubert} 44e0c4386eSCy Schubert 45e0c4386eSCy Schubertmy $proxy = chop_dblquot($ENV{http_proxy} // $ENV{HTTP_PROXY} // ""); 46e0c4386eSCy Schubert$proxy = "<EMPTY>" if $proxy eq ""; 47e0c4386eSCy Schubert$proxy =~ s{^https?://}{}i; 48e0c4386eSCy Schubertmy $no_proxy = $ENV{no_proxy} // $ENV{NO_PROXY}; 49e0c4386eSCy Schubert 50e0c4386eSCy Schubertmy @app = qw(openssl cmp); 51e0c4386eSCy Schubert 52e0c4386eSCy Schubert# the CMP server configuration consists of: 53e0c4386eSCy Schubertmy $ca_dn; # The CA's Distinguished Name 54e0c4386eSCy Schubertmy $server_dn; # The server's Distinguished Name 55e0c4386eSCy Schubertmy $server_host;# The server's host name or IP address 56e0c4386eSCy Schubertmy $server_port;# The server's port 57e0c4386eSCy Schubertmy $server_tls; # The server's TLS port, if any, or 0 58e0c4386eSCy Schubertmy $server_path;# The server's CMP alias 59e0c4386eSCy Schubertmy $server_cert;# The server's cert 60e0c4386eSCy Schubertmy $kur_port; # The server's port for kur (cert update) 61e0c4386eSCy Schubertmy $pbm_port; # The server port to be used for PBM 62e0c4386eSCy Schubertmy $pbm_ref; # The reference for PBM 63e0c4386eSCy Schubertmy $pbm_secret; # The secret for PBM 64e0c4386eSCy Schubertmy $column; # The column number of the expected result 65e0c4386eSCy Schubertmy $sleep = 0; # The time to sleep between two requests 66e0c4386eSCy Schubertmy $server_fh; # Server file handle 67e0c4386eSCy Schubert 68e0c4386eSCy Schubert# The local $server_name variables below are among others taken as the name of a 69e0c4386eSCy Schubert# sub-directory with server-specific certs etc. and CA-specific config section. 70e0c4386eSCy Schubert 71e0c4386eSCy Schubertsub load_config { 72e0c4386eSCy Schubert my $server_name = shift; 73e0c4386eSCy Schubert my $section = shift; 74e0c4386eSCy Schubert my $test_config = $ENV{OPENSSL_CMP_CONFIG} // "$server_name/test.cnf"; 75e0c4386eSCy Schubert open (CH, $test_config) or die "Cannot open $test_config: $!"; 76e0c4386eSCy Schubert my $active = 0; 77e0c4386eSCy Schubert while (<CH>) { 78e0c4386eSCy Schubert if (m/\[\s*$section\s*\]/) { 79e0c4386eSCy Schubert $active = 1; 80e0c4386eSCy Schubert } elsif (m/\[\s*.*?\s*\]/) { 81e0c4386eSCy Schubert $active = 0; 82e0c4386eSCy Schubert } elsif ($active) { 83e0c4386eSCy Schubert $ca_dn = $1 eq "" ? '""""' : $1 if m/^\s*ca_dn\s*=\s*(.*)?\s*$/; 84e0c4386eSCy Schubert $server_dn = $1 eq "" ? '""""' : $1 if m/^\s*server_dn\s*=\s*(.*)?\s*$/; 85e0c4386eSCy Schubert $server_host = $1 eq "" ? '""""' : $1 if m/^\s*server_host\s*=\s*(\S*)?\s*(\#.*)?$/; 86e0c4386eSCy Schubert $server_port = $1 eq "" ? '""""' : $1 if m/^\s*server_port\s*=\s*(.*)?\s*$/; 87e0c4386eSCy Schubert $server_tls = $1 eq "" ? '""""' : $1 if m/^\s*server_tls\s*=\s*(.*)?\s*$/; 88e0c4386eSCy Schubert $server_path = $1 eq "" ? '""""' : $1 if m/^\s*server_path\s*=\s*(.*)?\s*$/; 89e0c4386eSCy Schubert $server_cert = $1 eq "" ? '""""' : $1 if m/^\s*server_cert\s*=\s*(.*)?\s*$/; 90e0c4386eSCy Schubert $kur_port = $1 eq "" ? '""""' : $1 if m/^\s*kur_port\s*=\s*(.*)?\s*$/; 91e0c4386eSCy Schubert $pbm_port = $1 eq "" ? '""""' : $1 if m/^\s*pbm_port\s*=\s*(.*)?\s*$/; 92e0c4386eSCy Schubert $pbm_ref = $1 eq "" ? '""""' : $1 if m/^\s*pbm_ref\s*=\s*(.*)?\s*$/; 93e0c4386eSCy Schubert $pbm_secret = $1 eq "" ? '""""' : $1 if m/^\s*pbm_secret\s*=\s*(.*)?\s*$/; 94e0c4386eSCy Schubert $column = $1 eq "" ? '""""' : $1 if m/^\s*column\s*=\s*(.*)?\s*$/; 95e0c4386eSCy Schubert $sleep = $1 eq "" ? '""""' : $1 if m/^\s*sleep\s*=\s*(.*)?\s*$/; 96e0c4386eSCy Schubert } 97e0c4386eSCy Schubert } 98e0c4386eSCy Schubert close CH; 99e0c4386eSCy Schubert die "Cannot find all CMP server config values in $test_config section [$section]\n" 100e0c4386eSCy Schubert if !defined $ca_dn 101e0c4386eSCy Schubert || !defined $server_dn || !defined $server_host 102e0c4386eSCy Schubert || !defined $server_port || !defined $server_tls 103e0c4386eSCy Schubert || !defined $server_path || !defined $server_cert 104e0c4386eSCy Schubert || !defined $kur_port || !defined $pbm_port 105e0c4386eSCy Schubert || !defined $pbm_ref || !defined $pbm_secret 106e0c4386eSCy Schubert || !defined $column || !defined $sleep; 107e0c4386eSCy Schubert $server_dn = $server_dn // $ca_dn; 108e0c4386eSCy Schubert} 109e0c4386eSCy Schubert 110e0c4386eSCy Schubertmy @server_configurations = ("Mock"); 111e0c4386eSCy Schubert@server_configurations = split /\s+/, $ENV{OPENSSL_CMP_SERVER} if $ENV{OPENSSL_CMP_SERVER}; 112e0c4386eSCy Schubert# set env variable, e.g., OPENSSL_CMP_SERVER="Mock Insta" to include further CMP servers 113e0c4386eSCy Schubert 114e0c4386eSCy Schubertmy @all_aspects = ("connection", "verification", "credentials", "commands", "enrollment"); 115e0c4386eSCy Schubert@all_aspects = split /\s+/, $ENV{OPENSSL_CMP_ASPECTS} if $ENV{OPENSSL_CMP_ASPECTS}; 116e0c4386eSCy Schubert# set env variable, e.g., OPENSSL_CMP_ASPECTS="commands enrollment" to select specific aspects 117e0c4386eSCy Schubert 118e0c4386eSCy Schubertmy $faillog; 119e0c4386eSCy Schubertmy $file = $ENV{HARNESS_FAILLOG}; # pathname relative to result_dir 120e0c4386eSCy Schubertif ($file) { 121e0c4386eSCy Schubert open($faillog, ">", $file) or die "Cannot open $file for writing: $!"; 122e0c4386eSCy Schubert} 123e0c4386eSCy Schubert 124e0c4386eSCy Schubertsub test_cmp_http { 125e0c4386eSCy Schubert my $server_name = shift; 126e0c4386eSCy Schubert my $aspect = shift; 127e0c4386eSCy Schubert my $n = shift; 128e0c4386eSCy Schubert my $i = shift; 129e0c4386eSCy Schubert my $title = shift; 130e0c4386eSCy Schubert my $params = shift; 131e0c4386eSCy Schubert my $expected_result = shift; 132e0c4386eSCy Schubert $params = [ '-server', "127.0.0.1:$server_port", @$params ] 133e0c4386eSCy Schubert unless grep { $_ eq '-server' } @$params; 134e0c4386eSCy Schubert my $cmd = app([@app, @$params]); 135e0c4386eSCy Schubert 136e0c4386eSCy Schubert unless (is(my $actual_result = run($cmd), $expected_result, $title)) { 137e0c4386eSCy Schubert if ($faillog) { 138e0c4386eSCy Schubert my $quote_spc_empty = sub { $_ eq "" ? '""' : $_ =~ m/ / ? '"'.$_.'"' : $_ }; 139e0c4386eSCy Schubert my $invocation = cmdstr($cmd, display => 1); 140e0c4386eSCy Schubert print $faillog "$server_name $aspect \"$title\" ($i/$n)". 141e0c4386eSCy Schubert " expected=$expected_result actual=$actual_result\n"; 142e0c4386eSCy Schubert print $faillog "$invocation\n\n"; 143e0c4386eSCy Schubert } 144e0c4386eSCy Schubert } 145e0c4386eSCy Schubert} 146e0c4386eSCy Schubert 147e0c4386eSCy Schubertsub test_cmp_http_aspect { 148e0c4386eSCy Schubert my $server_name = shift; 149e0c4386eSCy Schubert my $aspect = shift; 150e0c4386eSCy Schubert my $tests = shift; 151e0c4386eSCy Schubert subtest "CMP app CLI $server_name $aspect\n" => sub { 152e0c4386eSCy Schubert my $n = scalar @$tests; 153e0c4386eSCy Schubert plan tests => $n; 154e0c4386eSCy Schubert my $i = 1; 155e0c4386eSCy Schubert foreach (@$tests) { 156e0c4386eSCy Schubert test_cmp_http($server_name, $aspect, $n, $i++, $$_[0], $$_[1], $$_[2]); 157e0c4386eSCy Schubert sleep($sleep); 158e0c4386eSCy Schubert } 159e0c4386eSCy Schubert }; 160e0c4386eSCy Schubert # not unlinking test.certout*.pem, test.cacerts.pem, and test.extracerts.pem 161e0c4386eSCy Schubert} 162e0c4386eSCy Schubert 163e0c4386eSCy Schubert# The input files for the tests done here dynamically depend on the test server 164e0c4386eSCy Schubert# selected (where the Mock server used by default is just one possibility). 165e0c4386eSCy Schubert# On the other hand the main test configuration file test.cnf, which references 166e0c4386eSCy Schubert# several server-dependent input files by relative file names, is static. 167e0c4386eSCy Schubert# Moreover the tests use much greater variety of input files than output files. 168e0c4386eSCy Schubert# Therefore we chose the current directory as a subdirectory of $SRCTOP and it 169e0c4386eSCy Schubert# was simpler to prepend the output file names by BLDTOP than doing the tests 170e0c4386eSCy Schubert# from $BLDTOP/test-runs/test_cmp_http and prepending the input files by SRCTOP. 171e0c4386eSCy Schubert 172e0c4386eSCy Schubertindir data_dir() => sub { 173e0c4386eSCy Schubert plan tests => 1 + @server_configurations * @all_aspects 174e0c4386eSCy Schubert - (grep(/^Mock$/, @server_configurations) 175e0c4386eSCy Schubert && grep(/^certstatus$/, @all_aspects)); 176e0c4386eSCy Schubert 177e0c4386eSCy Schubert foreach my $server_name (@server_configurations) { 178e0c4386eSCy Schubert $server_name = chop_dblquot($server_name); 179e0c4386eSCy Schubert load_config($server_name, $server_name); 180e0c4386eSCy Schubert { 181e0c4386eSCy Schubert SKIP: { 182e0c4386eSCy Schubert my $pid; 183e0c4386eSCy Schubert if ($server_name eq "Mock") { 184e0c4386eSCy Schubert indir "Mock" => sub { 185e0c4386eSCy Schubert $pid = start_mock_server(""); 186e0c4386eSCy Schubert die "Cannot start or find the started CMP mock server" unless $pid; 187e0c4386eSCy Schubert } 188e0c4386eSCy Schubert } 189e0c4386eSCy Schubert foreach my $aspect (@all_aspects) { 190e0c4386eSCy Schubert $aspect = chop_dblquot($aspect); 191e0c4386eSCy Schubert next if $server_name eq "Mock" && $aspect eq "certstatus"; 192e0c4386eSCy Schubert load_config($server_name, $aspect); # update with any aspect-specific settings 193e0c4386eSCy Schubert indir $server_name => sub { 194e0c4386eSCy Schubert my $tests = load_tests($server_name, $aspect); 195e0c4386eSCy Schubert test_cmp_http_aspect($server_name, $aspect, $tests); 196e0c4386eSCy Schubert }; 197e0c4386eSCy Schubert }; 198e0c4386eSCy Schubert stop_mock_server($pid) if $pid; 199e0c4386eSCy Schubert ok(1, "killing mock server"); 200e0c4386eSCy Schubert } 201e0c4386eSCy Schubert } 202e0c4386eSCy Schubert }; 203e0c4386eSCy Schubert}; 204e0c4386eSCy Schubert 205e0c4386eSCy Schubertclose($faillog) if $faillog; 206e0c4386eSCy Schubert 207e0c4386eSCy Schubertsub load_tests { 208e0c4386eSCy Schubert my $server_name = shift; 209e0c4386eSCy Schubert my $aspect = shift; 210e0c4386eSCy Schubert my $test_config = $ENV{OPENSSL_CMP_CONFIG} // "$server_name/test.cnf"; 211e0c4386eSCy Schubert my $file = data_file("test_$aspect.csv"); 212e0c4386eSCy Schubert my $result_dir = result_dir(); 213e0c4386eSCy Schubert my @result; 214e0c4386eSCy Schubert 215e0c4386eSCy Schubert open(my $data, '<', $file) || die "Cannot open $file for reading: $!"; 216e0c4386eSCy Schubert LOOP: 217e0c4386eSCy Schubert while (my $line = <$data>) { 218e0c4386eSCy Schubert chomp $line; 219e0c4386eSCy Schubert $line =~ s{\r\n}{\n}g; # adjust line endings 220e0c4386eSCy Schubert $line =~ s{_CA_DN}{$ca_dn}g; 221e0c4386eSCy Schubert $line =~ s{_SERVER_DN}{$server_dn}g; 222e0c4386eSCy Schubert $line =~ s{_SERVER_HOST}{$server_host}g; 223e0c4386eSCy Schubert $line =~ s{_SERVER_PORT}{$server_port}g; 224e0c4386eSCy Schubert $line =~ s{_SERVER_TLS}{$server_tls}g; 225e0c4386eSCy Schubert $line =~ s{_SERVER_PATH}{$server_path}g; 226e0c4386eSCy Schubert $line =~ s{_SERVER_CERT}{$server_cert}g; 227e0c4386eSCy Schubert $line =~ s{_KUR_PORT}{$kur_port}g; 228e0c4386eSCy Schubert $line =~ s{_PBM_PORT}{$pbm_port}g; 229e0c4386eSCy Schubert $line =~ s{_PBM_REF}{$pbm_ref}g; 230e0c4386eSCy Schubert $line =~ s{_PBM_SECRET}{$pbm_secret}g; 231e0c4386eSCy Schubert $line =~ s{_RESULT_DIR}{$result_dir}g; 232e0c4386eSCy Schubert 233e0c4386eSCy Schubert next LOOP if $server_tls == 0 && $line =~ m/,\s*-tls_used\s*,/; 234e0c4386eSCy Schubert my $noproxy = $no_proxy; 235e0c4386eSCy Schubert if ($line =~ m/,\s*-no_proxy\s*,(.*?)(,|$)/) { 236e0c4386eSCy Schubert $noproxy = $1; 237e0c4386eSCy Schubert } elsif ($server_host eq "127.0.0.1") { 238e0c4386eSCy Schubert # do connections to localhost (e.g., Mock server) without proxy 239e0c4386eSCy Schubert $line =~ s{-section,,}{-section,,-no_proxy,127.0.0.1,} ; 240e0c4386eSCy Schubert } 241e0c4386eSCy Schubert if ($line =~ m/,\s*-proxy\s*,/) { 242e0c4386eSCy Schubert next LOOP if $no_proxy && ($noproxy =~ $server_host); 243e0c4386eSCy Schubert } else { 244e0c4386eSCy Schubert $line =~ s{-section,,}{-section,,-proxy,$proxy,}; 245e0c4386eSCy Schubert } 246e0c4386eSCy Schubert $line =~ s{-section,,}{-section,,-certout,$result_dir/test.cert.pem,}; 247e0c4386eSCy Schubert $line =~ s{-section,,}{-config,../$test_config,-section,$server_name $aspect,}; 248e0c4386eSCy Schubert 249e0c4386eSCy Schubert my @fields = grep /\S/, split ",", $line; 250e0c4386eSCy Schubert s/^<EMPTY>$// for (@fields); # used for proxy="" 251e0c4386eSCy Schubert s/^\s+// for (@fields); # remove leading whitespace from elements 252e0c4386eSCy Schubert s/\s+$// for (@fields); # remove trailing whitespace from elements 253e0c4386eSCy Schubert s/^\"(\".*?\")\"$/$1/ for (@fields); # remove escaping from quotation marks from elements 254e0c4386eSCy Schubert my $expected_result = $fields[$column]; 255e0c4386eSCy Schubert my $description = 1; 256e0c4386eSCy Schubert my $title = $fields[$description]; 257e0c4386eSCy Schubert next LOOP if (!defined($expected_result) 258e0c4386eSCy Schubert || ($expected_result ne 0 && $expected_result ne 1)); 259e0c4386eSCy Schubert @fields = grep {$_ ne 'BLANK'} @fields[$description + 1 .. @fields - 1]; 260e0c4386eSCy Schubert push @result, [$title, \@fields, $expected_result]; 261e0c4386eSCy Schubert } 262e0c4386eSCy Schubert close($data); 263e0c4386eSCy Schubert return \@result; 264e0c4386eSCy Schubert} 265e0c4386eSCy Schubert 266e0c4386eSCy Schubertsub start_mock_server { 267e0c4386eSCy Schubert my $args = $_[0]; # optional further CLI arguments 268e0c4386eSCy Schubert my $cmd = cmdstr(app([@app, '-config', 'server.cnf', 269e0c4386eSCy Schubert $args ? $args : ()]), display => 1); 270e0c4386eSCy Schubert print "Current directory is ".getcwd()."\n"; 271e0c4386eSCy Schubert print "Launching mock server: $cmd\n"; 272e0c4386eSCy Schubert die "Invalid port: $server_port" unless $server_port =~ m/^\d+$/; 273*0d0c8621SEnji Cooper my $pid = open($server_fh, "$cmd 2>".result_dir()."/error.txt |") or die "Trying to $cmd"; 274e0c4386eSCy Schubert print "Pid is: $pid\n"; 275e0c4386eSCy Schubert if ($server_port == 0) { 276e0c4386eSCy Schubert # Find out the actual server port 277e0c4386eSCy Schubert while (<$server_fh>) { 278e0c4386eSCy Schubert print "Server output: $_"; 279e0c4386eSCy Schubert next if m/using section/; 280e0c4386eSCy Schubert s/\R$//; # Better chomp 281e0c4386eSCy Schubert ($server_port, $pid) = ($1, $2) if /^ACCEPT\s.*:(\d+) PID=(\d+)$/; 282e0c4386eSCy Schubert last; # Do not loop further to prevent hangs on server misbehavior 283e0c4386eSCy Schubert } 284e0c4386eSCy Schubert } 285e0c4386eSCy Schubert unless ($server_port > 0) { 286e0c4386eSCy Schubert stop_mock_server($pid); 287e0c4386eSCy Schubert return 0; 288e0c4386eSCy Schubert } 289e0c4386eSCy Schubert $server_tls = $kur_port = $pbm_port = $server_port; 290e0c4386eSCy Schubert return $pid; 291e0c4386eSCy Schubert} 292e0c4386eSCy Schubert 293e0c4386eSCy Schubertsub stop_mock_server { 294e0c4386eSCy Schubert my $pid = $_[0]; 295e0c4386eSCy Schubert print "Killing mock server with pid=$pid\n"; 296e0c4386eSCy Schubert kill('KILL', $pid); 297e0c4386eSCy Schubert waitpid($pid, 0); 298e0c4386eSCy Schubert} 299