]>
Commit | Line | Data |
---|---|---|
596d6b7e | 1 | #! /usr/bin/env perl |
b6461792 | 2 | # Copyright 2015-2024 The OpenSSL Project Authors. All Rights Reserved. |
596d6b7e | 3 | # |
909f1a2e | 4 | # Licensed under the Apache License 2.0 (the "License"). You may not use |
596d6b7e RS |
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 | |
8 | ||
4650de3e RL |
9 | |
10 | use strict; | |
11 | use warnings; | |
12 | ||
13 | use File::Spec; | |
1f019cd0 | 14 | use OpenSSL::Test::Utils; |
42e0ccdf | 15 | use OpenSSL::Test qw/:DEFAULT srctop_file/; |
4650de3e RL |
16 | |
17 | setup("test_x509"); | |
18 | ||
81202237 | 19 | plan tests => 51; |
4650de3e | 20 | |
53d0d01f MC |
21 | # Prevent MSys2 filename munging for arguments that look like file paths but |
22 | # aren't | |
23 | $ENV{MSYS2_ARG_CONV_EXCL} = "/CN="; | |
24 | ||
abc4439c | 25 | require_ok(srctop_file("test", "recipes", "tconversion.pl")); |
4650de3e | 26 | |
f0a057dd | 27 | my @certs = qw(test certs); |
abc4439c | 28 | my $pem = srctop_file(@certs, "cyrillic.pem"); |
42f7a489 RL |
29 | my $out_msb = "out-cyrillic.msb"; |
30 | my $out_utf8 = "out-cyrillic.utf8"; | |
d105a24c TM |
31 | my $der = "cyrillic.der"; |
32 | my $der2 = "cyrillic.der"; | |
abc4439c DDO |
33 | my $msb = srctop_file(@certs, "cyrillic.msb"); |
34 | my $utf = srctop_file(@certs, "cyrillic.utf8"); | |
4772610c | 35 | |
42f7a489 | 36 | ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_msb, |
4772610c | 37 | "-nameopt", "esc_msb"]))); |
2c8a740a | 38 | is(cmp_text($out_msb, $msb), |
05458fdb | 39 | 0, 'Comparing esc_msb output with cyrillic.msb'); |
42f7a489 | 40 | ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_utf8, |
4772610c | 41 | "-nameopt", "utf8"]))); |
2c8a740a | 42 | is(cmp_text($out_utf8, $utf), |
05458fdb | 43 | 0, 'Comparing utf8 output with cyrillic.utf8'); |
4772610c | 44 | |
d105a24c | 45 | SKIP: { |
2a33470b | 46 | skip "DES disabled", 1 if disabled("des"); |
53d0d01f | 47 | skip "Platform doesn't support command line UTF-8", 1 if $^O =~ /^(VMS|msys)$/; |
2a33470b DDO |
48 | |
49 | my $p12 = srctop_file("test", "shibboleth.pfx"); | |
50 | my $p12pass = "σύνθημα γνώρισμα"; | |
51 | my $out_pem = "out.pem"; | |
52 | ok(run(app(["openssl", "x509", "-text", "-in", $p12, "-out", $out_pem, | |
53 | "-passin", "pass:$p12pass"]))); | |
8cadc517 | 54 | # not unlinking $out_pem |
2a33470b DDO |
55 | } |
56 | ||
d105a24c TM |
57 | ok(!run(app(["openssl", "x509", "-in", $pem, "-inform", "DER", |
58 | "-out", $der, "-outform", "DER"])), | |
59 | "Checking failure of mismatching -inform DER"); | |
60 | ok(run(app(["openssl", "x509", "-in", $pem, "-inform", "PEM", | |
61 | "-out", $der, "-outform", "DER"])), | |
62 | "Conversion to DER"); | |
63 | ok(!run(app(["openssl", "x509", "-in", $der, "-inform", "PEM", | |
64 | "-out", $der2, "-outform", "DER"])), | |
65 | "Checking failure of mismatching -inform PEM"); | |
66 | ||
49b36afb DDO |
67 | # producing and checking self-issued (but not self-signed) cert |
68 | my $subj = "/CN=CA"; # using same DN as in issuer of ee-cert.pem | |
69 | my $extfile = srctop_file("test", "v3_ca_exts.cnf"); | |
70 | my $pkey = srctop_file(@certs, "ca-key.pem"); # issuer private key | |
71 | my $pubkey = "ca-pubkey.pem"; # the corresponding issuer public key | |
72 | # use any (different) key for signing our self-issued cert: | |
0e89b396 | 73 | my $key = srctop_file(@certs, "serverkey.pem"); |
49b36afb DDO |
74 | my $selfout = "self-issued.out"; |
75 | my $testcert = srctop_file(@certs, "ee-cert.pem"); | |
76 | ok(run(app(["openssl", "pkey", "-in", $pkey, "-pubout", "-out", $pubkey])) | |
0e89b396 DDO |
77 | && run(app(["openssl", "x509", "-new", "-force_pubkey", $pubkey, "-subj", $subj, |
78 | "-extfile", $extfile, "-key", $key, "-out", $selfout])) | |
49b36afb DDO |
79 | && run(app(["openssl", "verify", "-no_check_time", |
80 | "-trusted", $selfout, "-partial_chain", $testcert]))); | |
81 | # not unlinking $pubkey | |
82 | # not unlinking $selfout | |
52958608 | 83 | |
4e5bf933 JS |
84 | # test -set_issuer option |
85 | my $ca_issu = srctop_file(@certs, "ca-cert.pem"); # issuer cert | |
86 | my $caout_issu = "ca-issu.out"; | |
87 | ok(run(app(["openssl", "x509", "-new", "-force_pubkey", $key, "-subj", "/CN=EE", | |
88 | "-set_issuer", "/CN=TEST-CA", "-extfile", $extfile, "-CA", $ca_issu, | |
89 | "-CAkey", $pkey, "-text", "-out", $caout_issu]))); | |
90 | ok(get_issuer($caout_issu) =~ /CN=TEST-CA/); | |
91 | # not unlinking $caout | |
92 | ||
0e89b396 DDO |
93 | # simple way of directly producing a CA-signed cert with private/pubkey input |
94 | my $ca = srctop_file(@certs, "ca-cert.pem"); # issuer cert | |
95 | my $caout = "ca-issued.out"; | |
96 | ok(run(app(["openssl", "x509", "-new", "-force_pubkey", $key, "-subj", "/CN=EE", | |
97 | "-extfile", $extfile, "-CA", $ca, "-CAkey", $pkey, "-out", $caout])) | |
98 | && run(app(["openssl", "verify", "-no_check_time", | |
99 | "-trusted", $ca, "-partial_chain", $caout]))); | |
100 | ||
4650de3e | 101 | subtest 'x509 -- x.509 v1 certificate' => sub { |
b40498c6 | 102 | tconversion( -type => 'x509', -prefix => 'x509v1', |
abc4439c | 103 | -in => srctop_file("test", "testx509.pem") ); |
4650de3e RL |
104 | }; |
105 | subtest 'x509 -- first x.509 v3 certificate' => sub { | |
b40498c6 | 106 | tconversion( -type => 'x509', -prefix => 'x509v3-1', |
abc4439c | 107 | -in => srctop_file("test", "v3-cert1.pem") ); |
4650de3e RL |
108 | }; |
109 | subtest 'x509 -- second x.509 v3 certificate' => sub { | |
b40498c6 | 110 | tconversion( -type => 'x509', -prefix => 'x509v3-2', |
abc4439c | 111 | -in => srctop_file("test", "v3-cert2.pem") ); |
4650de3e | 112 | }; |
e417070c RS |
113 | |
114 | subtest 'x509 -- pathlen' => sub { | |
abc4439c | 115 | ok(run(test(["v3ext", srctop_file(@certs, "pathlen.pem")]))); |
47f387e9 DWG |
116 | }; |
117 | ||
f0a057dd DDO |
118 | cert_contains(srctop_file(@certs, "fake-gp.pem"), |
119 | "2.16.528.1.1003.1.3.5.5.2-1-0000006666-Z-12345678-01.015-12345678", | |
120 | 1, 'x500 -- subjectAltName'); | |
29844ea5 | 121 | |
91bc783a JW |
122 | cert_contains(srctop_file(@certs, "ext-noAssertion.pem"), |
123 | "No Assertion", | |
124 | 1, 'X.509 Not Assertion Extension'); | |
125 | ||
126 | cert_contains(srctop_file(@certs, "ext-groupAC.pem"), | |
127 | "Group Attribute Certificate", | |
128 | 1, 'X.509 Group Attribute Certificate Extension'); | |
129 | ||
130 | cert_contains(srctop_file(@certs, "ext-sOAIdentifier.pem"), | |
131 | "Source of Authority", | |
132 | 1, 'X.509 Source of Authority Extension'); | |
133 | ||
134 | cert_contains(srctop_file(@certs, "ext-noRevAvail.pem"), | |
135 | "No Revocation Available", | |
136 | 1, 'X.509 No Revocation Available'); | |
137 | ||
138 | cert_contains(srctop_file(@certs, "ext-singleUse.pem"), | |
139 | "Single Use", | |
140 | 1, 'X509v3 Single Use'); | |
141 | ||
142 | cert_contains(srctop_file(@certs, "ext-indirectIssuer.pem"), | |
143 | "Indirect Issuer", | |
144 | 1, 'X.509 Indirect Issuer'); | |
145 | ||
29844ea5 DDO |
146 | sub test_errors { # actually tests diagnostics of OSSL_STORE |
147 | my ($expected, $cert, @opts) = @_; | |
abc4439c | 148 | my $infile = srctop_file(@certs, $cert); |
29844ea5 | 149 | my @args = qw(openssl x509 -in); |
9032c2c1 | 150 | push(@args, $infile, @opts); |
29844ea5 | 151 | my $tmpfile = 'out.txt'; |
9032c2c1 DDO |
152 | my $res = grep(/-text/, @opts) ? run(app([@args], stdout => $tmpfile)) |
153 | : !run(app([@args], stderr => $tmpfile)); | |
29844ea5 DDO |
154 | my $found = 0; |
155 | open(my $in, '<', $tmpfile) or die "Could not open file $tmpfile"; | |
156 | while(<$in>) { | |
157 | print; # this may help debugging | |
158 | $res &&= !m/asn1 encoding/; # output must not include ASN.1 parse errors | |
159 | $found = 1 if m/$expected/; # output must include $expected | |
160 | } | |
161 | close $in; | |
9032c2c1 | 162 | # $tmpfile is kept to help with investigation in case of failure |
29844ea5 DDO |
163 | return $res && $found; |
164 | } | |
165 | ||
f7626d0b DDO |
166 | # 3 tests for non-existence of spurious OSSL_STORE ASN.1 parse error output. |
167 | # This requires provoking a failure exit of the app after reading input files. | |
1a683b80 | 168 | ok(test_errors("Bad output format", "root-cert.pem", '-outform', 'http'), |
f7626d0b | 169 | "load root-cert errors"); |
29844ea5 | 170 | ok(test_errors("RC2-40-CBC", "v3-certs-RC2.p12", '-passin', 'pass:v3-certs'), |
f7626d0b | 171 | "load v3-certs-RC2 no asn1 errors"); # error msg should mention "RC2-40-CBC" |
9032c2c1 DDO |
172 | SKIP: { |
173 | skip "sm2 not disabled", 1 if !disabled("sm2"); | |
174 | ||
237cb05d | 175 | ok(test_errors("Unable to load Public Key", "sm2.pem", '-text'), |
9032c2c1 DDO |
176 | "error loading unsupported sm2 cert"); |
177 | } | |
55b7fa26 HH |
178 | |
179 | # 3 tests for -dateopts formats | |
180 | ok(run(app(["openssl", "x509", "-noout", "-dates", "-dateopt", "rfc_822", | |
181 | "-in", srctop_file("test/certs", "ca-cert.pem")])), | |
182 | "Run with rfc_8222 -dateopt format"); | |
183 | ok(run(app(["openssl", "x509", "-noout", "-dates", "-dateopt", "iso_8601", | |
184 | "-in", srctop_file("test/certs", "ca-cert.pem")])), | |
185 | "Run with iso_8601 -dateopt format"); | |
186 | ok(!run(app(["openssl", "x509", "-noout", "-dates", "-dateopt", "invalid_format", | |
187 | "-in", srctop_file("test/certs", "ca-cert.pem")])), | |
188 | "Run with invalid -dateopt format"); | |
29fcd2e7 | 189 | |
29fcd2e7 TS |
190 | # Tests for signing certs (broken in 1.1.1o) |
191 | my $a_key = "a-key.pem"; | |
192 | my $a_cert = "a-cert.pem"; | |
193 | my $a2_cert = "a2-cert.pem"; | |
194 | my $ca_key = "ca-key.pem"; | |
195 | my $ca_cert = "ca-cert.pem"; | |
196 | my $cnf = srctop_file('apps', 'openssl.cnf'); | |
197 | ||
198 | # Create cert A | |
199 | ok(run(app(["openssl", "req", "-x509", "-newkey", "rsa:2048", | |
200 | "-config", $cnf, | |
201 | "-keyout", $a_key, "-out", $a_cert, "-days", "365", | |
202 | "-nodes", "-subj", "/CN=test.example.com"]))); | |
203 | # Create cert CA - note key size | |
204 | ok(run(app(["openssl", "req", "-x509", "-newkey", "rsa:4096", | |
205 | "-config", $cnf, | |
206 | "-keyout", $ca_key, "-out", $ca_cert, "-days", "3650", | |
207 | "-nodes", "-subj", "/CN=ca.example.com"]))); | |
208 | # Sign cert A with CA (errors on 1.1.1o) | |
209 | ok(run(app(["openssl", "x509", "-in", $a_cert, "-CA", $ca_cert, | |
210 | "-CAkey", $ca_key, "-set_serial", "1234567890", | |
211 | "-preserve_dates", "-sha256", "-text", "-out", $a2_cert]))); | |
212 | # verify issuer is CA | |
81202237 | 213 | ok(get_issuer($a2_cert) =~ /CN=ca.example.com/); |
29fcd2e7 | 214 | |
bac7e687 NH |
215 | my $in_csr = srctop_file('test', 'certs', 'x509-check.csr'); |
216 | my $in_key = srctop_file('test', 'certs', 'x509-check-key.pem'); | |
217 | my $invextfile = srctop_file('test', 'invalid-x509.cnf'); | |
218 | # Test that invalid extensions settings fail | |
219 | ok(!run(app(["openssl", "x509", "-req", "-in", $in_csr, "-signkey", $in_key, | |
220 | "-out", "/dev/null", "-days", "3650" , "-extensions", "ext", | |
221 | "-extfile", $invextfile]))); | |
222 | ||
29fcd2e7 TS |
223 | # Tests for issue #16080 (fixed in 1.1.1o) |
224 | my $b_key = "b-key.pem"; | |
225 | my $b_csr = "b-cert.csr"; | |
226 | my $b_cert = "b-cert.pem"; | |
227 | # Create the CSR | |
228 | ok(run(app(["openssl", "req", "-new", "-newkey", "rsa:4096", | |
229 | "-keyout", $b_key, "-out", $b_csr, "-nodes", | |
230 | "-config", $cnf, | |
231 | "-subj", "/CN=b.example.com"]))); | |
232 | # Sign it - position of "-text" matters! | |
233 | ok(run(app(["openssl", "x509", "-req", "-text", "-CAcreateserial", | |
234 | "-CA", $ca_cert, "-CAkey", $ca_key, | |
235 | "-in", $b_csr, "-out", $b_cert]))); | |
236 | # Verify issuer is CA | |
237 | ok(get_issuer($b_cert) =~ /CN=ca.example.com/); | |
29d4d8e8 | 238 | |
342e3652 DDO |
239 | # although no explicit extensions given: |
240 | has_version($b_cert, 3); | |
241 | has_SKID($b_cert, 1); | |
242 | has_AKID($b_cert, 1); | |
243 | ||
42a6a25b AK |
244 | # Tests for https://github.com/openssl/openssl/issues/10442 (fixed in 1.1.1a) |
245 | # (incorrect default `-CAcreateserial` if `-CA` path has a dot in it) | |
246 | my $folder_with_dot = "test_x509.folder"; | |
247 | ok(mkdir $folder_with_dot); | |
248 | my $ca_cert_dot_in_dir = File::Spec->catfile($folder_with_dot, "ca-cert.pem"); | |
249 | ok(copy($ca_cert,$ca_cert_dot_in_dir)); | |
250 | my $ca_serial_dot_in_dir = File::Spec->catfile($folder_with_dot, "ca-cert.srl"); | |
251 | ||
252 | ok(run(app(["openssl", "x509", "-req", "-text", "-CAcreateserial", | |
253 | "-CA", $ca_cert_dot_in_dir, "-CAkey", $ca_key, | |
254 | "-in", $b_csr]))); | |
255 | ok(-e $ca_serial_dot_in_dir); | |
256 | ||
81202237 SW |
257 | # Tests for explict start and end dates of certificates |
258 | my $today; | |
259 | my $enddate; | |
260 | $today = strftime("%Y-%m-%d", localtime); | |
261 | ok(run(app(["openssl", "x509", "-req", "-text", | |
262 | "-key", $b_key, | |
263 | "-not_before", "20231031000000Z", | |
264 | "-not_after", "today", | |
265 | "-in", $b_csr, "-out", $b_cert])) | |
266 | && get_not_before($b_cert) =~ /Oct 31 00:00:00 2023 GMT/ | |
267 | && get_not_after_date($b_cert) eq $today); | |
268 | # explicit start and end dates | |
269 | ok(run(app(["openssl", "x509", "-req", "-text", | |
270 | "-key", $b_key, | |
271 | "-not_before", "20231031000000Z", | |
272 | "-not_after", "20231231000000Z", | |
273 | "-days", "99", | |
274 | "-in", $b_csr, "-out", $b_cert])) | |
275 | && get_not_before($b_cert) =~ /Oct 31 00:00:00 2023 GMT/ | |
276 | && get_not_after($b_cert) =~ /Dec 31 00:00:00 2023 GMT/); | |
277 | # start date today and days | |
278 | $today = strftime("%Y-%m-%d", localtime); | |
279 | $enddate = strftime("%Y-%m-%d", localtime(time + 99 * 24 * 60 * 60)); | |
280 | ok(run(app(["openssl", "x509", "-req", "-text", | |
281 | "-key", $b_key, | |
282 | "-not_before", "today", | |
283 | "-days", "99", | |
284 | "-in", $b_csr, "-out", $b_cert])) | |
285 | && get_not_before_date($b_cert) eq $today | |
286 | && get_not_after_date($b_cert) eq $enddate); | |
287 | # end date before start date | |
288 | ok(!run(app(["openssl", "x509", "-req", "-text", | |
289 | "-key", $b_key, | |
290 | "-not_before", "today", | |
291 | "-not_after", "20231031000000Z", | |
292 | "-in", $b_csr, "-out", $b_cert]))); | |
293 | # default days option | |
294 | $today = strftime("%Y-%m-%d", localtime); | |
295 | $enddate = strftime("%Y-%m-%d", localtime(time + 30 * 24 * 60 * 60)); | |
296 | ok(run(app(["openssl", "x509", "-req", "-text", | |
297 | "-key", $b_key, | |
298 | "-in", $b_csr, "-out", $b_cert])) | |
299 | && get_not_before_date($b_cert) eq $today | |
300 | && get_not_after_date($b_cert) eq $enddate); | |
301 | ||
29d4d8e8 | 302 | SKIP: { |
303 | skip "EC is not supported by this OpenSSL build", 1 | |
304 | if disabled("ec"); | |
305 | ok(run(test(["x509_test"])), "running x509_test"); | |
306 | } |