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