2 # Copyright 2007-2019 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 => "This test is not supported in a no-cmp build"
25 plan skip_all => "This test is not supported in a no-ec build"
27 plan skip_all => "Tests involving server not available on Windows or VMS"
28 if $^O =~ /^(VMS|MSWin32)$/;
30 sub chop_dblquot { # chop any leading & trailing '"' (needed for Windows)
32 $str =~ s/^\"(.*?)\"$/$1/;
36 my $proxy = "<EMPTY>";
37 $proxy = chop_dblquot($ENV{http_proxy} // $ENV{HTTP_PROXY} // $proxy);
38 $proxy =~ s{^https?://}{}i;
39 my $no_proxy = $ENV{no_proxy} // $ENV{NO_PROXY};
41 my $app = "apps/openssl cmp";
43 my @cmp_basic_tests = (
44 [ "show help", [ "-config", '""', "-help" ], 0 ],
45 [ "CLI option not starting with '-'", [ "-config", '""', "days", "1" ], 1 ],
46 [ "unknown CLI option", [ "-config", '""', "-dayss" ], 1 ],
47 [ "bad int syntax: non-digit", [ "-config", '""', "-days", "a/" ], 1 ],
48 [ "bad int syntax: float", [ "-config", '""', "-days", "3.14" ], 1 ],
49 [ "bad int syntax: trailing garbage", [ "-config", '""', "-days", "314_+" ], 1 ],
50 [ "bad int: out of range", [ "-config", '""', "-days", "2147483648" ], 1 ],
53 my $rsp_cert = "signer_only.crt";
54 my $outfile = "test.cert.pem";
55 my $secret = "pass:test";
58 # this uses the mock server directly in the cmp app, without TCP
59 sub use_mock_srv_internally
61 ok(run(cmd([bldtop_dir($app),
63 "-use_mock_srv", "-srv_ref", "mock server",
64 "-srv_cert", "server.crt", # used for setting sender
65 "-srv_secret", $secret,
67 "-rsp_cert", $rsp_cert,
69 "-subject", "/CN=any",
70 "-newkey", "signer.key",
71 "-recipient", "/O=openssl_cmp", # if given must be consistent with sender
73 "-ref", "client under test",
74 "-certout" , $outfile]))
75 && compare_text($outfile, $rsp_cert) == 0,
76 "CMP app with -use_mock_srv and -poll_count 1");
80 # the CMP server configuration consists of:
81 my $ca_dn; # The CA's Distinguished Name
82 my $server_dn; # The server's Distinguished Name
83 my $server_host;# The server's host name or IP address
84 my $server_port;# The server's port
85 my $server_tls; # The server's TLS port, if any, or 0
86 my $server_path;# The server's CMP alias
87 my $server_cert;# The server's cert
88 my $kur_port; # The server's port for kur (cert update)
89 my $pbm_port; # The server port to be used for PBM
90 my $pbm_ref; # The reference for PBM
91 my $pbm_secret; # The secret for PBM
92 my $column; # The column number of the expected result
93 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.
99 my $server_name = shift;
101 my $test_config = $ENV{OPENSSL_CMP_CONFIG} // "$server_name/test.cnf";
102 open (CH, $test_config) or die "Cannot open $test_config: $!";
105 if (m/\[\s*$section\s*\]/) {
107 } elsif (m/\[\s*.*?\s*\]/) {
110 $ca_dn = $1 eq "" ? '""""' : $1 if m/^\s*ca_dn\s*=\s*(.*)?\s*$/;
111 $server_dn = $1 eq "" ? '""""' : $1 if m/^\s*server_dn\s*=\s*(.*)?\s*$/;
112 $server_host = $1 eq "" ? '""""' : $1 if m/^\s*server_host\s*=\s*(\S*)?\s*(\#.*)?$/;
113 $server_port = $1 eq "" ? '""""' : $1 if m/^\s*server_port\s*=\s*(.*)?\s*$/;
114 $server_tls = $1 eq "" ? '""""' : $1 if m/^\s*server_tls\s*=\s*(.*)?\s*$/;
115 $server_path = $1 eq "" ? '""""' : $1 if m/^\s*server_path\s*=\s*(.*)?\s*$/;
116 $server_cert = $1 eq "" ? '""""' : $1 if m/^\s*server_cert\s*=\s*(.*)?\s*$/;
117 $kur_port = $1 eq "" ? '""""' : $1 if m/^\s*kur_port\s*=\s*(.*)?\s*$/;
118 $pbm_port = $1 eq "" ? '""""' : $1 if m/^\s*pbm_port\s*=\s*(.*)?\s*$/;
119 $pbm_ref = $1 eq "" ? '""""' : $1 if m/^\s*pbm_ref\s*=\s*(.*)?\s*$/;
120 $pbm_secret = $1 eq "" ? '""""' : $1 if m/^\s*pbm_secret\s*=\s*(.*)?\s*$/;
121 $column = $1 eq "" ? '""""' : $1 if m/^\s*column\s*=\s*(.*)?\s*$/;
122 $sleep = $1 eq "" ? '""""' : $1 if m/^\s*sleep\s*=\s*(.*)?\s*$/;
126 die "Cannot find all CMP server config values in $test_config section [$section]\n"
128 || !defined $server_dn || !defined $server_host
129 || !defined $server_port || !defined $server_tls
130 || !defined $server_path || !defined $server_cert
131 || !defined $kur_port || !defined $pbm_port
132 || !defined $pbm_ref || !defined $pbm_secret
133 || !defined $column || !defined $sleep;
134 $server_dn = $server_dn // $ca_dn;
137 my @server_configurations = ("Mock");
138 @server_configurations = split /\s+/, $ENV{OPENSSL_CMP_SERVER} if $ENV{OPENSSL_CMP_SERVER};
139 # set env variable, e.g., OPENSSL_CMP_SERVER="Mock Insta" to include further CMP servers
141 my @all_aspects = ("connection", "verification", "credentials", "commands", "enrollment");
142 @all_aspects = split /\s+/, $ENV{OPENSSL_CMP_ASPECTS} if $ENV{OPENSSL_CMP_ASPECTS};
143 # set env variable, e.g., OPENSSL_CMP_ASPECTS="commands enrollment" to select specific aspects
146 if ($ENV{HARNESS_FAILLOG}) {
147 my $file = $ENV{HARNESS_FAILLOG};
148 open($faillog, ">", $file) or die "Cannot open $file for writing: $!";
152 my $server_name = shift;
158 my $expected_exit = shift;
159 my $path_app = bldtop_dir($app);
160 with({ exit_checker => sub {
161 my $actual_exit = shift;
162 my $OK = $actual_exit == $expected_exit;
163 if ($faillog && !$OK) {
164 sub quote_spc_empty(_) { $_ eq "" ? '""' : $_ =~ m/ / ? '"'.$_.'"' : $_ };
165 my $invocation = ("$path_app ").join(' ', map quote_spc_empty @$params);
166 print $faillog "$server_name $aspect \"$title\" ($i/$n)".
167 " expected=$expected_exit actual=$actual_exit\n";
168 print $faillog "$invocation\n\n";
171 sub { ok(run(cmd([$path_app, @$params,])),
175 sub test_cmp_cli_aspect {
176 my $server_name = shift;
179 subtest "CMP app CLI $server_name $aspect\n" => sub {
180 my $n = scalar @$tests;
185 test_cmp_cli($server_name, $aspect, $n, $i++, $$_[0], $$_[1], $$_[2]);
190 unlink "test.cert.pem", "test.cacerts.pem", "test.extracerts.pem";
193 indir data_dir() => sub {
194 plan tests => 1 + @server_configurations * @all_aspects
195 + (grep(/^Mock$/, @server_configurations)
196 && grep(/^certstatus$/, @all_aspects) ? 0 : 1);
198 test_cmp_cli_aspect("basic", "options", \@cmp_basic_tests);
200 indir "Mock" => sub {
201 use_mock_srv_internally();
204 foreach my $server_name (@server_configurations) {
205 $server_name = chop_dblquot($server_name);
206 load_config($server_name, $server_name);
207 my $launch_mock = $server_name eq "Mock" && !$ENV{OPENSSL_CMP_CONFIG};
209 indir "Mock" => sub {
210 stop_mock_server(); # in case a previous run did not exit properly
211 start_mock_server("") || die "Cannot start CMP mock server";
214 foreach my $aspect (@all_aspects) {
215 $aspect = chop_dblquot($aspect);
216 next if $server_name eq "Mock" && $aspect eq "certstatus";
217 load_config($server_name, $aspect); # update with any aspect-specific settings
218 indir $server_name => sub {
219 my $tests = load_tests($server_name, $aspect);
220 test_cmp_cli_aspect($server_name, $aspect, $tests);
223 stop_mock_server() if $launch_mock;
227 close($faillog) if $faillog;
230 my $server_name = shift;
232 my $test_config = $ENV{OPENSSL_CMP_CONFIG} // "$server_name/test.cnf";
233 my $file = data_file("test_$aspect.csv");
236 open(my $data, '<', $file) || die "Cannot open $file for reading: $!";
238 while (my $line = <$data>) {
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;
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*,(.*?)(,|$)/) {
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,} ;
261 if ($line =~ m/,\s*-proxy\s*,/) {
262 next LOOP if $no_proxy && ($noproxy =~ $server_host);
264 $line =~ s{-section,,}{-section,,-proxy,$proxy,};
266 $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];
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];
285 sub mock_server_pid {
286 return `lsof -iTCP:$localport -sTCP:LISTEN | tail -n 1 | awk '{ print \$2 }'`;
289 sub start_mock_server {
290 return 0 if mock_server_pid(); # already running
291 my $args = $_[0]; # optional further CLI arguments
292 my $dir = bldtop_dir("");
293 return system("LD_LIBRARY_PATH=$dir DYLD_LIBRARY_PATH=$dir " .
294 bldtop_dir($app) . " -config server.cnf " .
295 "$args &") == 0; # start in background, check for success
298 sub stop_mock_server {
299 my $pid = mock_server_pid();
300 system("kill $pid") if $pid;