80-test_cmp_http.t 12 KB

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