2 # Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the Apache License 2.0 (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
13 use OpenSSL
::Test
::Utils
;
14 use OpenSSL
::Test qw
/:DEFAULT srctop_file/;
20 require_ok
(srctop_file
('test', 'recipes', 'tconversion.pl'));
22 my @certs = qw(test certs);
24 # What type of key to generate?
26 if (disabled
("rsa")) {
27 @req_new = ("-newkey", "dsa:".srctop_file
("apps", "dsa512.pem"));
30 note
("There should be a 2 sequences of .'s and some +'s.");
31 note
("There should not be more that at most 80 per line");
34 # Check for duplicate -addext parameters, and one "working" case.
35 my @addext_args = ( "openssl", "req", "-new", "-out", "testreq.pem",
36 "-config", srctop_file
("test", "test.cnf"), @req_new );
37 my $val = "subjectAltName=DNS:example.com";
38 my $val2 = " " . $val;
41 ok
( run
(app
([@addext_args, "-addext", $val])));
42 ok
(!run
(app
([@addext_args, "-addext", $val, "-addext", $val])));
43 ok
(!run
(app
([@addext_args, "-addext", $val, "-addext", $val2])));
44 ok
(!run
(app
([@addext_args, "-addext", $val, "-addext", $val3])));
45 ok
(!run
(app
([@addext_args, "-addext", $val2, "-addext", $val3])));
47 subtest
"generating alt certificate requests with RSA" => sub {
51 skip
"RSA is not supported by this OpenSSL build", 2
54 ok
(run
(app
(["openssl", "req",
55 "-config", srctop_file
("test", "test.cnf"),
57 "-new", "-out", "testreq-rsa.pem", "-utf8",
58 "-key", srctop_file
("test", "testrsa.pem")])),
59 "Generating request");
61 ok
(run
(app
(["openssl", "req",
62 "-config", srctop_file
("test", "test.cnf"),
63 "-verify", "-in", "testreq-rsa.pem", "-noout"])),
64 "Verifying signature on request");
66 ok
(run
(app
(["openssl", "req",
67 "-config", srctop_file
("test", "test.cnf"),
69 "-verify", "-in", "testreq-rsa.pem", "-noout"])),
70 "Verifying signature on request");
75 subtest
"generating certificate requests with RSA" => sub {
79 skip
"RSA is not supported by this OpenSSL build", 2
82 ok
(run
(app
(["openssl", "req",
83 "-config", srctop_file
("test", "test.cnf"),
84 "-new", "-out", "testreq-rsa.pem", "-utf8",
85 "-key", srctop_file
("test", "testrsa.pem")])),
86 "Generating request");
88 ok
(run
(app
(["openssl", "req",
89 "-config", srctop_file
("test", "test.cnf"),
90 "-verify", "-in", "testreq-rsa.pem", "-noout"])),
91 "Verifying signature on request");
95 subtest
"generating certificate requests with RSA-PSS" => sub {
99 skip
"RSA is not supported by this OpenSSL build", 2
102 ok
(run
(app
(["openssl", "req",
103 "-config", srctop_file
("test", "test.cnf"),
104 "-new", "-out", "testreq-rsapss.pem", "-utf8",
105 "-key", srctop_file
("test", "testrsapss.pem")])),
106 "Generating request");
108 ok
(run
(app
(["openssl", "req",
109 "-config", srctop_file
("test", "test.cnf"),
110 "-verify", "-in", "testreq-rsapss.pem", "-noout"])),
111 "Verifying signature on request");
113 ok
(run
(app
(["openssl", "req",
114 "-config", srctop_file
("test", "test.cnf"),
115 "-new", "-out", "testreq-rsapss2.pem", "-utf8",
116 "-sigopt", "rsa_padding_mode:pss",
117 "-sigopt", "rsa_pss_saltlen:-1",
118 "-key", srctop_file
("test", "testrsapss.pem")])),
119 "Generating request");
121 ok
(run
(app
(["openssl", "req",
122 "-config", srctop_file
("test", "test.cnf"),
123 "-verify", "-in", "testreq-rsapss2.pem", "-noout"])),
124 "Verifying signature on request");
128 subtest
"generating certificate requests with DSA" => sub {
132 skip
"DSA is not supported by this OpenSSL build", 2
135 ok
(run
(app
(["openssl", "req",
136 "-config", srctop_file
("test", "test.cnf"),
137 "-new", "-out", "testreq-dsa.pem", "-utf8",
138 "-key", srctop_file
("test", "testdsa.pem")])),
139 "Generating request");
141 ok
(run
(app
(["openssl", "req",
142 "-config", srctop_file
("test", "test.cnf"),
143 "-verify", "-in", "testreq-dsa.pem", "-noout"])),
144 "Verifying signature on request");
148 subtest
"generating certificate requests with ECDSA" => sub {
152 skip
"ECDSA is not supported by this OpenSSL build", 2
155 ok
(run
(app
(["openssl", "req",
156 "-config", srctop_file
("test", "test.cnf"),
157 "-new", "-out", "testreq-ec.pem", "-utf8",
158 "-key", srctop_file
("test", "testec-p256.pem")])),
159 "Generating request");
161 ok
(run
(app
(["openssl", "req",
162 "-config", srctop_file
("test", "test.cnf"),
163 "-verify", "-in", "testreq-ec.pem", "-noout"])),
164 "Verifying signature on request");
168 subtest
"generating certificate requests with Ed25519" => sub {
172 skip
"Ed25519 is not supported by this OpenSSL build", 2
175 ok
(run
(app
(["openssl", "req",
176 "-config", srctop_file
("test", "test.cnf"),
177 "-new", "-out", "testreq-ed25519.pem", "-utf8",
178 "-key", srctop_file
("test", "tested25519.pem")])),
179 "Generating request");
181 ok
(run
(app
(["openssl", "req",
182 "-config", srctop_file
("test", "test.cnf"),
183 "-verify", "-in", "testreq-ed25519.pem", "-noout"])),
184 "Verifying signature on request");
188 subtest
"generating certificate requests with Ed448" => sub {
192 skip
"Ed448 is not supported by this OpenSSL build", 2
195 ok
(run
(app
(["openssl", "req",
196 "-config", srctop_file
("test", "test.cnf"),
197 "-new", "-out", "testreq-ed448.pem", "-utf8",
198 "-key", srctop_file
("test", "tested448.pem")])),
199 "Generating request");
201 ok
(run
(app
(["openssl", "req",
202 "-config", srctop_file
("test", "test.cnf"),
203 "-verify", "-in", "testreq-ed448.pem", "-noout"])),
204 "Verifying signature on request");
208 subtest
"generating certificate requests" => sub {
211 ok
(run
(app
(["openssl", "req", "-config", srctop_file
("test", "test.cnf"),
212 @req_new, "-out", "testreq.pem"])),
213 "Generating request");
215 ok
(run
(app
(["openssl", "req", "-config", srctop_file
("test", "test.cnf"),
216 "-verify", "-in", "testreq.pem", "-noout"])),
217 "Verifying signature on request");
220 subtest
"generating SM2 certificate requests" => sub {
224 skip
"SM2 is not supported by this OpenSSL build", 4
226 ok
(run
(app
(["openssl", "req",
227 "-config", srctop_file
("test", "test.cnf"),
228 "-new", "-key", srctop_file
(@certs, "sm2.key"),
229 "-sigopt", "distid:1234567812345678",
230 "-out", "testreq-sm2.pem", "-sm3"])),
231 "Generating SM2 certificate request");
233 ok
(run
(app
(["openssl", "req",
234 "-config", srctop_file
("test", "test.cnf"),
235 "-verify", "-in", "testreq-sm2.pem", "-noout",
236 "-vfyopt", "distid:1234567812345678", "-sm3"])),
237 "Verifying signature on SM2 certificate request");
239 ok
(run
(app
(["openssl", "req",
240 "-config", srctop_file
("test", "test.cnf"),
241 "-new", "-key", srctop_file
(@certs, "sm2.key"),
242 "-sigopt", "hexdistid:DEADBEEF",
243 "-out", "testreq-sm2.pem", "-sm3"])),
244 "Generating SM2 certificate request with hex id");
246 ok
(run
(app
(["openssl", "req",
247 "-config", srctop_file
("test", "test.cnf"),
248 "-verify", "-in", "testreq-sm2.pem", "-noout",
249 "-vfyopt", "hexdistid:DEADBEEF", "-sm3"])),
250 "Verifying signature on SM2 certificate request");
254 my @openssl_args = ("req", "-config", srctop_file
("apps", "openssl.cnf"));
256 run_conversion
('req conversions',
258 run_conversion
('req conversions -- testreq2',
259 srctop_file
("test", "testreq2.pem"));
265 subtest
$title => sub {
266 run
(app
(["openssl", @openssl_args,
267 "-in", $reqfile, "-inform", "p",
269 stderr
=> "req-check.err", stdout
=> undef));
270 open DATA
, "req-check.err";
272 plan skip_all
=> "skipping req conversion test for $reqfile"
273 if grep /Unknown Public Key/, map { s/\R//; } <DATA
>;
275 tconversion
( -type
=> 'req', -in => $reqfile,
276 -args
=> [ @openssl_args ] );
279 unlink "req-check.err";
285 # Test both generation and verification of certs w.r.t. RFC 5280 requirements
287 my $ca_cert; # will be set below
290 my $ss = $cert =~ m/self-signed/;
291 my $is_ca = $cert =~ m/CA/;
292 my $cn = $is_ca ?
"CA" : "EE";
293 my $ca_key = srctop_file
(@certs, "ca-key.pem");
294 my $key = $is_ca ?
$ca_key : srctop_file
(@certs, "ee-key.pem");
295 my @cmd = ("openssl", "req", "-config", "\"\"", "-x509",
296 "-key", $key, "-subj", "/CN=$cn", @_, "-out", $cert);
297 push(@cmd, ("-CA", $ca_cert, "-CAkey", $ca_key)) unless $ss;
298 ok
(run
(app
([@cmd])), "generate $cert");
302 my $expect = shift @_;
303 cert_contains
($cert, "Subject Key Identifier", $expect);
307 my $expect = shift @_;
308 cert_contains
($cert, "Authority Key Identifier", $expect);
312 my $expect = shift @_;
313 cert_contains
($cert, "Key Usage", $expect);
317 my $expect = shift @_;
318 my $trusted = shift @_;
319 $trusted = $cert unless $trusted;
320 ok
(run
(app
(["openssl", "verify", "-x509_strict", "-trusted", $trusted,
321 "-partial_chain", $cert])) == $expect,
322 "strict verify allow $cert");
325 my @v3_ca = ("-addext", "basicConstraints = critical,CA:true",
326 "-addext", "keyUsage = keyCertSign");
327 my $SKID_AKID = "subjectKeyIdentifier,authorityKeyIdentifier";
328 my $cert = "self-signed_v1_CA_no_KIDs.pem";
329 generate_cert
($cert);
330 cert_ext_has_n_different_lines
($cert, 0, $SKID_AKID); # no SKID and no AKID
331 #TODO strict_verify($cert, 1); # self-signed v1 root cert should be accepted as CA
333 $ca_cert = "self-signed_v3_CA_default_SKID.pem";
334 generate_cert
($ca_cert, @v3_ca);
335 has_SKID
($ca_cert, 1);
336 has_AKID
($ca_cert, 0);
337 strict_verify
($ca_cert, 1);
339 $cert = "self-signed_v3_CA_no_SKID.pem";
340 generate_cert
($cert, @v3_ca, "-addext", "subjectKeyIdentifier = none");
341 cert_ext_has_n_different_lines
($cert, 0, $SKID_AKID); # no SKID and no AKID
342 #TODO strict_verify($cert, 0);
344 $cert = "self-signed_v3_CA_both_KIDs.pem";
345 generate_cert
($cert, @v3_ca, "-addext", "subjectKeyIdentifier = hash",
346 "-addext", "authorityKeyIdentifier = keyid");
347 cert_ext_has_n_different_lines
($cert, 3, $SKID_AKID); # SKID == AKID
348 strict_verify
($cert, 1);
350 $cert = "self-signed_v3_EE_wrong_keyUsage.pem";
351 generate_cert
($cert, "-addext", "keyUsage = keyCertSign");
352 #TODO strict_verify($cert, 1); # should be accepted because RFC 5280 does not apply
354 $cert = "v3_EE_default_KIDs.pem";
355 generate_cert
($cert, "-addext", "keyUsage = dataEncipherment");
356 cert_ext_has_n_different_lines
($cert, 4, $SKID_AKID); # SKID != AKID
357 strict_verify
($cert, 1, $ca_cert);
359 $cert = "v3_EE_no_AKID.pem";
360 generate_cert
($cert, "-addext", "authorityKeyIdentifier = none");
363 strict_verify
($cert, 0, $ca_cert);
365 $cert = "self-issued_v3_EE_default_KIDs.pem";
366 generate_cert
($cert, "-addext", "keyUsage = dataEncipherment",
367 "-in", srctop_file
(@certs, "x509-check.csr"));
368 cert_ext_has_n_different_lines
($cert, 4, $SKID_AKID); # SKID != AKID
369 strict_verify
($cert, 1);
371 my $cert = "self-signed_CA_no_keyUsage.pem";
372 generate_cert
($cert, "-in", srctop_file
(@certs, "ext-check.csr"));
373 has_keyUsage
($cert, 0);
374 my $cert = "self-signed_CA_with_keyUsages.pem";
375 generate_cert
($cert, "-in", srctop_file
(@certs, "ext-check.csr"),
376 "-copy_extensions", "copy");
377 has_keyUsage
($cert, 1);