81-test_cmp_cli.t 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #! /usr/bin/env perl
  2. # Copyright 2007-2020 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 File::Spec::Functions qw/catfile/;
  14. use File::Compare qw/compare_text/;
  15. use OpenSSL::Test qw/:DEFAULT with data_file data_dir srctop_dir bldtop_dir result_dir result_file/;
  16. use OpenSSL::Test::Utils;
  17. use Data::Dumper; # for debugging purposes only
  18. BEGIN {
  19. setup("test_cmp_cli");
  20. }
  21. use lib srctop_dir('Configurations');
  22. use lib bldtop_dir('.');
  23. use platform;
  24. plan skip_all => "These tests are not supported in a fuzz build"
  25. if config('options') =~ /-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION/;
  26. plan skip_all => "These tests are not supported in a no-cmp build"
  27. if disabled("cmp");
  28. plan skip_all => "These tests are not supported in a no-ec build"
  29. if disabled("ec");
  30. plan skip_all => "Tests involving CMP server not available on Windows or VMS"
  31. if $^O =~ /^(VMS|MSWin32)$/;
  32. plan skip_all => "Tests involving CMP server not available in cross-compile builds"
  33. if defined $ENV{EXE_SHELL};
  34. plan skip_all => "Tests involving CMP server require 'kill' command"
  35. if system("which kill");
  36. plan skip_all => "Tests involving CMP server require 'lsof' command"
  37. if system("which lsof"); # this typically excludes Solaris
  38. sub chop_dblquot { # chop any leading & trailing '"' (needed for Windows)
  39. my $str = shift;
  40. $str =~ s/^\"(.*?)\"$/$1/;
  41. return $str;
  42. }
  43. my $proxy = "<EMPTY>";
  44. $proxy = chop_dblquot($ENV{http_proxy} // $ENV{HTTP_PROXY} // $proxy);
  45. $proxy =~ s{^https?://}{}i;
  46. my $no_proxy = $ENV{no_proxy} // $ENV{NO_PROXY};
  47. my $app = "apps/openssl cmp";
  48. my @cmp_basic_tests = (
  49. [ "show help", [ "-config", '""', "-help" ], 0 ],
  50. [ "CLI option not starting with '-'", [ "-config", '""', "days", "1" ], 1 ],
  51. [ "unknown CLI option", [ "-config", '""', "-dayss" ], 1 ],
  52. [ "bad int syntax: non-digit", [ "-config", '""', "-days", "a/" ], 1 ],
  53. [ "bad int syntax: float", [ "-config", '""', "-days", "3.14" ], 1 ],
  54. [ "bad int syntax: trailing garbage", [ "-config", '""', "-days", "314_+" ], 1 ],
  55. [ "bad int: out of range", [ "-config", '""', "-days", "2147483648" ], 1 ],
  56. );
  57. # this uses the mock server directly in the cmp app, without TCP
  58. sub use_mock_srv_internally
  59. {
  60. my $secret = "pass:test";
  61. my $rsp_cert = "signer_only.crt";
  62. my $outfile = result_file("test.certout.pem");
  63. ok(run(cmd([bldtop_dir($app),
  64. "-config", '""',
  65. "-use_mock_srv", "-srv_ref", "mock server",
  66. "-srv_cert", "server.crt", # used for setting sender
  67. "-srv_secret", $secret,
  68. "-poll_count", "1",
  69. "-rsp_cert", $rsp_cert,
  70. "-cmd", "cr",
  71. "-subject", "/CN=any",
  72. "-newkey", "signer.key",
  73. "-recipient", "/O=openssl_cmp", # if given must be consistent with sender
  74. "-secret", $secret,
  75. "-ref", "client under test",
  76. "-certout", $outfile]))
  77. && compare_text($outfile, $rsp_cert) == 0,
  78. "CMP app with -use_mock_srv and -poll_count 1");
  79. # not unlinking $outfile
  80. }
  81. # the CMP server configuration consists of:
  82. my $ca_dn; # The CA's Distinguished Name
  83. my $server_dn; # The server's Distinguished Name
  84. my $server_host;# The server's host name or IP address
  85. my $server_port;# The server's port
  86. my $server_tls; # The server's TLS port, if any, or 0
  87. my $server_path;# The server's CMP alias
  88. my $server_cert;# The server's cert
  89. my $kur_port; # The server's port for kur (cert update)
  90. my $pbm_port; # The server port to be used for PBM
  91. my $pbm_ref; # The reference for PBM
  92. my $pbm_secret; # The secret for PBM
  93. my $column; # The column number of the expected result
  94. my $sleep = 0; # The time to sleep between two requests
  95. # The local $server_name variables below are among others taken as the name of a
  96. # sub-directory with server-specific certs etc. and CA-specific config section.
  97. sub load_config {
  98. my $server_name = shift;
  99. my $section = shift;
  100. my $test_config = $ENV{OPENSSL_CMP_CONFIG} // "$server_name/test.cnf";
  101. open (CH, $test_config) or die "Cannot open $test_config: $!";
  102. my $active = 0;
  103. while (<CH>) {
  104. if (m/\[\s*$section\s*\]/) {
  105. $active = 1;
  106. } elsif (m/\[\s*.*?\s*\]/) {
  107. $active = 0;
  108. } elsif ($active) {
  109. $ca_dn = $1 eq "" ? '""""' : $1 if m/^\s*ca_dn\s*=\s*(.*)?\s*$/;
  110. $server_dn = $1 eq "" ? '""""' : $1 if m/^\s*server_dn\s*=\s*(.*)?\s*$/;
  111. $server_host = $1 eq "" ? '""""' : $1 if m/^\s*server_host\s*=\s*(\S*)?\s*(\#.*)?$/;
  112. $server_port = $1 eq "" ? '""""' : $1 if m/^\s*server_port\s*=\s*(.*)?\s*$/;
  113. $server_tls = $1 eq "" ? '""""' : $1 if m/^\s*server_tls\s*=\s*(.*)?\s*$/;
  114. $server_path = $1 eq "" ? '""""' : $1 if m/^\s*server_path\s*=\s*(.*)?\s*$/;
  115. $server_cert = $1 eq "" ? '""""' : $1 if m/^\s*server_cert\s*=\s*(.*)?\s*$/;
  116. $kur_port = $1 eq "" ? '""""' : $1 if m/^\s*kur_port\s*=\s*(.*)?\s*$/;
  117. $pbm_port = $1 eq "" ? '""""' : $1 if m/^\s*pbm_port\s*=\s*(.*)?\s*$/;
  118. $pbm_ref = $1 eq "" ? '""""' : $1 if m/^\s*pbm_ref\s*=\s*(.*)?\s*$/;
  119. $pbm_secret = $1 eq "" ? '""""' : $1 if m/^\s*pbm_secret\s*=\s*(.*)?\s*$/;
  120. $column = $1 eq "" ? '""""' : $1 if m/^\s*column\s*=\s*(.*)?\s*$/;
  121. $sleep = $1 eq "" ? '""""' : $1 if m/^\s*sleep\s*=\s*(.*)?\s*$/;
  122. }
  123. }
  124. close CH;
  125. die "Cannot find all CMP server config values in $test_config section [$section]\n"
  126. if !defined $ca_dn
  127. || !defined $server_dn || !defined $server_host
  128. || !defined $server_port || !defined $server_tls
  129. || !defined $server_path || !defined $server_cert
  130. || !defined $kur_port || !defined $pbm_port
  131. || !defined $pbm_ref || !defined $pbm_secret
  132. || !defined $column || !defined $sleep;
  133. $server_dn = $server_dn // $ca_dn;
  134. }
  135. my @server_configurations = ("Mock");
  136. @server_configurations = split /\s+/, $ENV{OPENSSL_CMP_SERVER} if $ENV{OPENSSL_CMP_SERVER};
  137. # set env variable, e.g., OPENSSL_CMP_SERVER="Mock Insta" to include further CMP servers
  138. my @all_aspects = ("connection", "verification", "credentials", "commands", "enrollment");
  139. @all_aspects = split /\s+/, $ENV{OPENSSL_CMP_ASPECTS} if $ENV{OPENSSL_CMP_ASPECTS};
  140. # set env variable, e.g., OPENSSL_CMP_ASPECTS="commands enrollment" to select specific aspects
  141. my $faillog;
  142. if ($ENV{HARNESS_FAILLOG}) {
  143. my $file = $ENV{HARNESS_FAILLOG};
  144. open($faillog, ">", $file) or die "Cannot open $file for writing: $!";
  145. }
  146. sub test_cmp_cli {
  147. my $server_name = shift;
  148. my $aspect = shift;
  149. my $n = shift;
  150. my $i = shift;
  151. my $title = shift;
  152. my $params = shift;
  153. my $expected_exit = shift;
  154. my $path_app = bldtop_dir($app);
  155. with({ exit_checker => sub {
  156. my $actual_exit = shift;
  157. my $OK = $actual_exit == $expected_exit;
  158. if ($faillog && !$OK) {
  159. my $quote_spc_empty = sub { $_ eq "" ? '""' : $_ =~ m/ / ? '"'.$_.'"' : $_ };
  160. my $invocation = "$path_app ".join(' ', map $quote_spc_empty->($_), @$params);
  161. print $faillog "$server_name $aspect \"$title\" ($i/$n)".
  162. " expected=$expected_exit actual=$actual_exit\n";
  163. print $faillog "$invocation\n\n";
  164. }
  165. return $OK; } },
  166. sub { ok(run(cmd([$path_app, @$params,])),
  167. $title); });
  168. }
  169. sub test_cmp_cli_aspect {
  170. my $server_name = shift;
  171. my $aspect = shift;
  172. my $tests = shift;
  173. subtest "CMP app CLI $server_name $aspect\n" => sub {
  174. my $n = scalar @$tests;
  175. plan tests => $n;
  176. my $i = 1;
  177. foreach (@$tests) {
  178. SKIP: {
  179. test_cmp_cli($server_name, $aspect, $n, $i++, $$_[0], $$_[1], $$_[2]);
  180. sleep($sleep);
  181. }
  182. }
  183. };
  184. # not unlinking test.certout*.pem, test.cacerts.pem, and test.extracerts.pem
  185. }
  186. # The input files for the tests done here dynamically depend on the test server
  187. # selected (where the Mock server used by default is just one possibility).
  188. # On the other hand the main test configuration file test.cnf, which references
  189. # several server-dependent input files by relative file names, is static.
  190. # Moreover the tests use much greater variety of input files than output files.
  191. # Therefore we chose the current directory as a subdirectory of $SRCTOP and it
  192. # was simpler to prepend the output file names by BLDTOP than doing the tests
  193. # from $BLDTOP/test-runs/test_cmp_cli and prepending the input files by SRCTOP.
  194. indir data_dir() => sub {
  195. plan tests => 1 + @server_configurations * @all_aspects
  196. + (grep(/^Mock$/, @server_configurations)
  197. && grep(/^certstatus$/, @all_aspects) ? 0 : 1);
  198. test_cmp_cli_aspect("basic", "options", \@cmp_basic_tests);
  199. indir "Mock" => sub {
  200. use_mock_srv_internally();
  201. };
  202. foreach my $server_name (@server_configurations) {
  203. $server_name = chop_dblquot($server_name);
  204. load_config($server_name, $server_name);
  205. SKIP:
  206. {
  207. my $pid;
  208. if ($server_name eq "Mock") {
  209. indir "Mock" => sub {
  210. $pid = start_mock_server("");
  211. skip "Cannot start or find the started CMP mock server",
  212. scalar @all_aspects unless $pid;
  213. }
  214. }
  215. foreach my $aspect (@all_aspects) {
  216. $aspect = chop_dblquot($aspect);
  217. next if $server_name eq "Mock" && $aspect eq "certstatus";
  218. load_config($server_name, $aspect); # update with any aspect-specific settings
  219. indir $server_name => sub {
  220. my $tests = load_tests($server_name, $aspect);
  221. test_cmp_cli_aspect($server_name, $aspect, $tests);
  222. };
  223. };
  224. stop_mock_server($pid) if $pid;
  225. }
  226. };
  227. };
  228. close($faillog) if $faillog;
  229. sub load_tests {
  230. my $server_name = shift;
  231. my $aspect = shift;
  232. my $test_config = $ENV{OPENSSL_CMP_CONFIG} // "$server_name/test.cnf";
  233. my $file = data_file("test_$aspect.csv");
  234. my $result_dir = result_dir();
  235. my @result;
  236. open(my $data, '<', $file) || die "Cannot open $file for reading: $!";
  237. LOOP:
  238. while (my $line = <$data>) {
  239. chomp $line;
  240. $line =~ s{\r\n}{\n}g; # adjust line endings
  241. $line =~ s{_CA_DN}{$ca_dn}g;
  242. $line =~ s{_SERVER_DN}{$server_dn}g;
  243. $line =~ s{_SERVER_HOST}{$server_host}g;
  244. $line =~ s{_SERVER_PORT}{$server_port}g;
  245. $line =~ s{_SERVER_TLS}{$server_tls}g;
  246. $line =~ s{_SERVER_PATH}{$server_path}g;
  247. $line =~ s{_SERVER_CERT}{$server_cert}g;
  248. $line =~ s{_KUR_PORT}{$kur_port}g;
  249. $line =~ s{_PBM_PORT}{$pbm_port}g;
  250. $line =~ s{_PBM_REF}{$pbm_ref}g;
  251. $line =~ s{_PBM_SECRET}{$pbm_secret}g;
  252. $line =~ s{_RESULT_DIR}{$result_dir}g;
  253. next LOOP if $server_tls == 0 && $line =~ m/,\s*-tls_used\s*,/;
  254. my $noproxy = $no_proxy;
  255. if ($line =~ m/,\s*-no_proxy\s*,(.*?)(,|$)/) {
  256. $noproxy = $1;
  257. } elsif ($server_host eq "127.0.0.1") {
  258. # do connections to localhost (e.g., Mock server) without proxy
  259. $line =~ s{-section,,}{-section,,-no_proxy,127.0.0.1,} ;
  260. }
  261. if ($line =~ m/,\s*-proxy\s*,/) {
  262. next LOOP if $no_proxy && ($noproxy =~ $server_host);
  263. } else {
  264. $line =~ s{-section,,}{-section,,-proxy,$proxy,};
  265. }
  266. $line =~ s{-section,,}{-section,,-certout,$result_dir/test.cert.pem,};
  267. $line =~ s{-section,,}{-config,../$test_config,-section,$server_name $aspect,};
  268. my @fields = grep /\S/, split ",", $line;
  269. s/^<EMPTY>$// for (@fields); # used for proxy=""
  270. s/^\s+// for (@fields); # remove leading whitespace from elements
  271. s/\s+$// for (@fields); # remove trailing whitespace from elements
  272. s/^\"(\".*?\")\"$/$1/ for (@fields); # remove escaping from quotation marks from elements
  273. my $expected_exit = $fields[$column];
  274. my $description = 1;
  275. my $title = $fields[$description];
  276. next LOOP if (!defined($expected_exit)
  277. || ($expected_exit ne 0 && $expected_exit ne 1));
  278. @fields = grep {$_ ne 'BLANK'} @fields[$description + 1 .. @fields - 1];
  279. push @result, [$title, \@fields, $expected_exit];
  280. }
  281. close($data);
  282. return \@result;
  283. }
  284. sub mock_server_pid {
  285. return `lsof -iTCP:$server_port` =~ m/\n\S+\s+(\d+)\s+[^\n]+LISTEN/s ? $1 : 0;
  286. }
  287. sub start_mock_server {
  288. my $args = $_[0]; # optional further CLI arguments
  289. my $dir = bldtop_dir("");
  290. my $cmd = "LD_LIBRARY_PATH=$dir DYLD_LIBRARY_PATH=$dir " .
  291. bldtop_dir($app) . " -config server.cnf $args";
  292. my $pid = mock_server_pid();
  293. return $pid if $pid; # already running
  294. return system("$cmd &") == 0 # start in background, check for success
  295. ? (sleep 1, mock_server_pid()) : 0;
  296. }
  297. sub stop_mock_server {
  298. my $pid = $_[0];
  299. system("kill $pid") if $pid;
  300. }