81-test_cmp_cli.t 12 KB

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