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 bldtop_dir/;
18 use OpenSSL::Test::Utils;
19 use Data::Dumper; # for debugging purposes only
21 setup("test_cmp_cli");
23 plan skip_all => "These tests are not supported in a no-cmp build"
25 plan skip_all => "These tests are not supported in a no-ec build"
27 plan skip_all => "These tests are not supported in a fuzz build"
28 if !disabled("fuzz-libfuzzer") || !disabled("fuzz-afl");
29 plan skip_all => "Tests involving server not available on Windows or VMS"
30 if $^O =~ /^(VMS|MSWin32)$/;
32 sub chop_dblquot { # chop any leading & trailing '"' (needed for Windows)
34 $str =~ s/^\"(.*?)\"$/$1/;
38 my $proxy = "<EMPTY>";
39 $proxy = chop_dblquot($ENV{http_proxy} // $ENV{HTTP_PROXY} // $proxy);
40 $proxy =~ s{^https?://}{}i;
41 my $no_proxy = $ENV{no_proxy} // $ENV{NO_PROXY};
43 my $app = "apps/openssl cmp";
45 my @cmp_basic_tests = (
46 [ "show help", [ "-config", '""', "-help" ], 0 ],
47 [ "CLI option not starting with '-'", [ "-config", '""', "days", "1" ], 1 ],
48 [ "unknown CLI option", [ "-config", '""', "-dayss" ], 1 ],
49 [ "bad int syntax: non-digit", [ "-config", '""', "-days", "a/" ], 1 ],
50 [ "bad int syntax: float", [ "-config", '""', "-days", "3.14" ], 1 ],
51 [ "bad int syntax: trailing garbage", [ "-config", '""', "-days", "314_+" ], 1 ],
52 [ "bad int: out of range", [ "-config", '""', "-days", "2147483648" ], 1 ],
55 my $rsp_cert = "signer_only.crt";
56 my $outfile = "test.cert.pem";
57 my $secret = "pass:test";
60 # this uses the mock server directly in the cmp app, without TCP
61 sub use_mock_srv_internally
63 ok(run(cmd([bldtop_dir($app),
65 "-use_mock_srv", "-srv_ref", "mock server",
66 "-srv_cert", "server.crt", # used for setting sender
67 "-srv_secret", $secret,
69 "-rsp_cert", $rsp_cert,
71 "-subject", "/CN=any",
72 "-newkey", "signer.key",
73 "-recipient", "/O=openssl_cmp", # if given must be consistent with sender
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");
82 # the CMP server configuration consists of:
83 my $ca_dn; # The CA's Distinguished Name
84 my $server_dn; # The server's Distinguished Name
85 my $server_host;# The server's host name or IP address
86 my $server_port;# The server's port
87 my $server_tls; # The server's TLS port, if any, or 0
88 my $server_path;# The server's CMP alias
89 my $server_cert;# The server's cert
90 my $kur_port; # The server's port for kur (cert update)
91 my $pbm_port; # The server port to be used for PBM
92 my $pbm_ref; # The reference for PBM
93 my $pbm_secret; # The secret for PBM
94 my $column; # The column number of the expected result
95 my $sleep = 0; # The time to sleep between two requests
97 # The local $server_name variables below are among others taken as the name of a
98 # sub-directory with server-specific certs etc. and CA-specific config section.
101 my $server_name = shift;
103 my $test_config = $ENV{OPENSSL_CMP_CONFIG} // "$server_name/test.cnf";
104 open (CH, $test_config) or die "Cannot open $test_config: $!";
107 if (m/\[\s*$section\s*\]/) {
109 } elsif (m/\[\s*.*?\s*\]/) {
112 $ca_dn = $1 eq "" ? '""""' : $1 if m/^\s*ca_dn\s*=\s*(.*)?\s*$/;
113 $server_dn = $1 eq "" ? '""""' : $1 if m/^\s*server_dn\s*=\s*(.*)?\s*$/;
114 $server_host = $1 eq "" ? '""""' : $1 if m/^\s*server_host\s*=\s*(\S*)?\s*(\#.*)?$/;
115 $server_port = $1 eq "" ? '""""' : $1 if m/^\s*server_port\s*=\s*(.*)?\s*$/;
116 $server_tls = $1 eq "" ? '""""' : $1 if m/^\s*server_tls\s*=\s*(.*)?\s*$/;
117 $server_path = $1 eq "" ? '""""' : $1 if m/^\s*server_path\s*=\s*(.*)?\s*$/;
118 $server_cert = $1 eq "" ? '""""' : $1 if m/^\s*server_cert\s*=\s*(.*)?\s*$/;
119 $kur_port = $1 eq "" ? '""""' : $1 if m/^\s*kur_port\s*=\s*(.*)?\s*$/;
120 $pbm_port = $1 eq "" ? '""""' : $1 if m/^\s*pbm_port\s*=\s*(.*)?\s*$/;
121 $pbm_ref = $1 eq "" ? '""""' : $1 if m/^\s*pbm_ref\s*=\s*(.*)?\s*$/;
122 $pbm_secret = $1 eq "" ? '""""' : $1 if m/^\s*pbm_secret\s*=\s*(.*)?\s*$/;
123 $column = $1 eq "" ? '""""' : $1 if m/^\s*column\s*=\s*(.*)?\s*$/;
124 $sleep = $1 eq "" ? '""""' : $1 if m/^\s*sleep\s*=\s*(.*)?\s*$/;
128 die "Cannot find all CMP server config values in $test_config section [$section]\n"
130 || !defined $server_dn || !defined $server_host
131 || !defined $server_port || !defined $server_tls
132 || !defined $server_path || !defined $server_cert
133 || !defined $kur_port || !defined $pbm_port
134 || !defined $pbm_ref || !defined $pbm_secret
135 || !defined $column || !defined $sleep;
136 $server_dn = $server_dn // $ca_dn;
139 my @server_configurations = ("Mock");
140 @server_configurations = split /\s+/, $ENV{OPENSSL_CMP_SERVER} if $ENV{OPENSSL_CMP_SERVER};
141 # set env variable, e.g., OPENSSL_CMP_SERVER="Mock Insta" to include further CMP servers
143 my @all_aspects = ("connection", "verification", "credentials", "commands", "enrollment");
144 @all_aspects = split /\s+/, $ENV{OPENSSL_CMP_ASPECTS} if $ENV{OPENSSL_CMP_ASPECTS};
145 # set env variable, e.g., OPENSSL_CMP_ASPECTS="commands enrollment" to select specific aspects
148 if ($ENV{HARNESS_FAILLOG}) {
149 my $file = $ENV{HARNESS_FAILLOG};
150 open($faillog, ">", $file) or die "Cannot open $file for writing: $!";
154 my $server_name = shift;
160 my $expected_exit = shift;
161 my $path_app = bldtop_dir($app);
162 with({ exit_checker => sub {
163 my $actual_exit = shift;
164 my $OK = $actual_exit == $expected_exit;
165 if ($faillog && !$OK) {
166 sub quote_spc_empty(_) { $_ eq "" ? '""' : $_ =~ m/ / ? '"'.$_.'"' : $_ };
167 my $invocation = ("$path_app ").join(' ', map quote_spc_empty @$params);
168 print $faillog "$server_name $aspect \"$title\" ($i/$n)".
169 " expected=$expected_exit actual=$actual_exit\n";
170 print $faillog "$invocation\n\n";
173 sub { ok(run(cmd([$path_app, @$params,])),
177 sub test_cmp_cli_aspect {
178 my $server_name = shift;
181 subtest "CMP app CLI $server_name $aspect\n" => sub {
182 my $n = scalar @$tests;
187 test_cmp_cli($server_name, $aspect, $n, $i++, $$_[0], $$_[1], $$_[2]);
192 unlink "test.cert.pem", "test.cacerts.pem", "test.extracerts.pem";
195 indir data_dir() => sub {
196 plan tests => 1 + @server_configurations * @all_aspects
197 + (grep(/^Mock$/, @server_configurations)
198 && grep(/^certstatus$/, @all_aspects) ? 0 : 1);
200 test_cmp_cli_aspect("basic", "options", \@cmp_basic_tests);
202 indir "Mock" => sub {
203 use_mock_srv_internally();
206 foreach my $server_name (@server_configurations) {
207 $server_name = chop_dblquot($server_name);
208 load_config($server_name, $server_name);
209 my $launch_mock = $server_name eq "Mock" && !$ENV{OPENSSL_CMP_CONFIG};
211 indir "Mock" => sub {
212 stop_mock_server(); # in case a previous run did not exit properly
213 start_mock_server("") || die "Cannot start CMP mock server";
216 foreach my $aspect (@all_aspects) {
217 $aspect = chop_dblquot($aspect);
218 next if $server_name eq "Mock" && $aspect eq "certstatus";
219 load_config($server_name, $aspect); # update with any aspect-specific settings
220 indir $server_name => sub {
221 my $tests = load_tests($server_name, $aspect);
222 test_cmp_cli_aspect($server_name, $aspect, $tests);
225 stop_mock_server() if $launch_mock;
229 close($faillog) if $faillog;
232 my $server_name = shift;
234 my $test_config = $ENV{OPENSSL_CMP_CONFIG} // "$server_name/test.cnf";
235 my $file = data_file("test_$aspect.csv");
238 open(my $data, '<', $file) || die "Cannot open $file for reading: $!";
240 while (my $line = <$data>) {
242 $line =~ s{\r\n}{\n}g; # adjust line endings
243 $line =~ s{_CA_DN}{$ca_dn}g;
244 $line =~ s{_SERVER_DN}{$server_dn}g;
245 $line =~ s{_SERVER_HOST}{$server_host}g;
246 $line =~ s{_SERVER_PORT}{$server_port}g;
247 $line =~ s{_SERVER_TLS}{$server_tls}g;
248 $line =~ s{_SERVER_PATH}{$server_path}g;
249 $line =~ s{_SERVER_CERT}{$server_cert}g;
250 $line =~ s{_KUR_PORT}{$kur_port}g;
251 $line =~ s{_PBM_PORT}{$pbm_port}g;
252 $line =~ s{_PBM_REF}{$pbm_ref}g;
253 $line =~ s{_PBM_SECRET}{$pbm_secret}g;
255 next LOOP if $server_tls == 0 && $line =~ m/,\s*-tls_used\s*,/;
256 my $noproxy = $no_proxy;
257 if ($line =~ m/,\s*-no_proxy\s*,(.*?)(,|$)/) {
259 } elsif ($server_host eq "127.0.0.1") {
260 # do connections to localhost (e.g., Mock server) without proxy
261 $line =~ s{-section,,}{-section,,-no_proxy,127.0.0.1,} ;
263 if ($line =~ m/,\s*-proxy\s*,/) {
264 next LOOP if $no_proxy && ($noproxy =~ $server_host);
266 $line =~ s{-section,,}{-section,,-proxy,$proxy,};
268 $line =~ s{-section,,}{-config,../$test_config,-section,$server_name $aspect,};
270 my @fields = grep /\S/, split ",", $line;
271 s/^<EMPTY>$// for (@fields); # used for proxy=""
272 s/^\s+// for (@fields); # remove leading whitespace from elements
273 s/\s+$// for (@fields); # remove trailing whitespace from elements
274 s/^\"(\".*?\")\"$/$1/ for (@fields); # remove escaping from quotation marks from elements
275 my $expected_exit = $fields[$column];
277 my $title = $fields[$description];
278 next LOOP if (!defined($expected_exit)
279 || ($expected_exit ne 0 && $expected_exit ne 1));
280 @fields = grep {$_ ne 'BLANK'} @fields[$description + 1 .. @fields - 1];
281 push @result, [$title, \@fields, $expected_exit];
287 sub mock_server_pid {
288 return `lsof -iTCP:$localport -sTCP:LISTEN | tail -n 1 | awk '{ print \$2 }'`;
291 sub start_mock_server {
292 return 0 if mock_server_pid(); # already running
293 my $args = $_[0]; # optional further CLI arguments
294 my $dir = bldtop_dir("");
295 return system("LD_LIBRARY_PATH=$dir DYLD_LIBRARY_PATH=$dir " .
296 bldtop_dir($app) . " -config server.cnf " .
297 "$args &") == 0; # start in background, check for success
300 sub stop_mock_server {
301 my $pid = mock_server_pid();
302 system("kill $pid") if $pid;