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