2
0

80-test_cmp_http.t 13 KB

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