2 # Copyright 2015-2023 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
14 use OpenSSL::Test::Utils;
15 use OpenSSL::Test qw/:DEFAULT srctop_file/;
21 # Prevent MSys2 filename munging for arguments that look like file paths but
23 $ENV{MSYS2_ARG_CONV_EXCL} = "/CN=";
25 require_ok(srctop_file("test", "recipes", "tconversion.pl"));
27 my @certs = qw(test certs);
28 my $pem = srctop_file(@certs, "cyrillic.pem");
29 my $out_msb = "out-cyrillic.msb";
30 my $out_utf8 = "out-cyrillic.utf8";
31 my $der = "cyrillic.der";
32 my $der2 = "cyrillic.der";
33 my $msb = srctop_file(@certs, "cyrillic.msb");
34 my $utf = srctop_file(@certs, "cyrillic.utf8");
36 ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_msb,
37 "-nameopt", "esc_msb"])));
38 is(cmp_text($out_msb, $msb),
39 0, 'Comparing esc_msb output with cyrillic.msb');
40 ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_utf8,
41 "-nameopt", "utf8"])));
42 is(cmp_text($out_utf8, $utf),
43 0, 'Comparing utf8 output with cyrillic.utf8');
46 skip "DES disabled", 1 if disabled("des");
47 skip "Platform doesn't support command line UTF-8", 1 if $^O =~ /^(VMS|msys)$/;
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"])));
54 # not unlinking $out_pem
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"])),
63 ok(!run(app(["openssl", "x509", "-in", $der, "-inform", "PEM",
64 "-out", $der2, "-outform", "DER"])),
65 "Checking failure of mismatching -inform PEM");
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:
73 my $key = srctop_file(@certs, "serverkey.pem");
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]))
77 && run(app(["openssl", "x509", "-new", "-force_pubkey", $pubkey, "-subj", $subj,
78 "-extfile", $extfile, "-key", $key, "-out", $selfout]))
79 && run(app(["openssl", "verify", "-no_check_time",
80 "-trusted", $selfout, "-partial_chain", $testcert])));
81 # not unlinking $pubkey
82 # not unlinking $selfout
84 # simple way of directly producing a CA-signed cert with private/pubkey input
85 my $ca = srctop_file(@certs, "ca-cert.pem"); # issuer cert
86 my $caout = "ca-issued.out";
87 ok(run(app(["openssl", "x509", "-new", "-force_pubkey", $key, "-subj", "/CN=EE",
88 "-extfile", $extfile, "-CA", $ca, "-CAkey", $pkey, "-out", $caout]))
89 && run(app(["openssl", "verify", "-no_check_time",
90 "-trusted", $ca, "-partial_chain", $caout])));
92 subtest 'x509 -- x.509 v1 certificate' => sub {
93 tconversion( -type => 'x509', -prefix => 'x509v1',
94 -in => srctop_file("test", "testx509.pem") );
96 subtest 'x509 -- first x.509 v3 certificate' => sub {
97 tconversion( -type => 'x509', -prefix => 'x509v3-1',
98 -in => srctop_file("test", "v3-cert1.pem") );
100 subtest 'x509 -- second x.509 v3 certificate' => sub {
101 tconversion( -type => 'x509', -prefix => 'x509v3-2',
102 -in => srctop_file("test", "v3-cert2.pem") );
105 subtest 'x509 -- pathlen' => sub {
106 ok(run(test(["v3ext", srctop_file(@certs, "pathlen.pem")])));
109 cert_contains(srctop_file(@certs, "fake-gp.pem"),
110 "2.16.528.1.1003.1.3.5.5.2-1-0000006666-Z-12345678-01.015-12345678",
111 1, 'x500 -- subjectAltName');
113 cert_contains(srctop_file(@certs, "ext-noAssertion.pem"),
115 1, 'X.509 Not Assertion Extension');
117 cert_contains(srctop_file(@certs, "ext-groupAC.pem"),
118 "Group Attribute Certificate",
119 1, 'X.509 Group Attribute Certificate Extension');
121 cert_contains(srctop_file(@certs, "ext-sOAIdentifier.pem"),
122 "Source of Authority",
123 1, 'X.509 Source of Authority Extension');
125 cert_contains(srctop_file(@certs, "ext-noRevAvail.pem"),
126 "No Revocation Available",
127 1, 'X.509 No Revocation Available');
129 cert_contains(srctop_file(@certs, "ext-singleUse.pem"),
131 1, 'X509v3 Single Use');
133 cert_contains(srctop_file(@certs, "ext-indirectIssuer.pem"),
135 1, 'X.509 Indirect Issuer');
137 sub test_errors { # actually tests diagnostics of OSSL_STORE
138 my ($expected, $cert, @opts) = @_;
139 my $infile = srctop_file(@certs, $cert);
140 my @args = qw(openssl x509 -in);
141 push(@args, $infile, @opts);
142 my $tmpfile = 'out.txt';
143 my $res = grep(/-text/, @opts) ? run(app([@args], stdout => $tmpfile))
144 : !run(app([@args], stderr => $tmpfile));
146 open(my $in, '<', $tmpfile) or die "Could not open file $tmpfile";
148 print; # this may help debugging
149 $res &&= !m/asn1 encoding/; # output must not include ASN.1 parse errors
150 $found = 1 if m/$expected/; # output must include $expected
153 # $tmpfile is kept to help with investigation in case of failure
154 return $res && $found;
157 # 3 tests for non-existence of spurious OSSL_STORE ASN.1 parse error output.
158 # This requires provoking a failure exit of the app after reading input files.
159 ok(test_errors("Bad output format", "root-cert.pem", '-outform', 'http'),
160 "load root-cert errors");
161 ok(test_errors("RC2-40-CBC", "v3-certs-RC2.p12", '-passin', 'pass:v3-certs'),
162 "load v3-certs-RC2 no asn1 errors"); # error msg should mention "RC2-40-CBC"
164 skip "sm2 not disabled", 1 if !disabled("sm2");
166 ok(test_errors("Unable to load Public Key", "sm2.pem", '-text'),
167 "error loading unsupported sm2 cert");
170 # 3 tests for -dateopts formats
171 ok(run(app(["openssl", "x509", "-noout", "-dates", "-dateopt", "rfc_822",
172 "-in", srctop_file("test/certs", "ca-cert.pem")])),
173 "Run with rfc_8222 -dateopt format");
174 ok(run(app(["openssl", "x509", "-noout", "-dates", "-dateopt", "iso_8601",
175 "-in", srctop_file("test/certs", "ca-cert.pem")])),
176 "Run with iso_8601 -dateopt format");
177 ok(!run(app(["openssl", "x509", "-noout", "-dates", "-dateopt", "invalid_format",
178 "-in", srctop_file("test/certs", "ca-cert.pem")])),
179 "Run with invalid -dateopt format");
181 # extracts issuer from a -text formatted-output
185 open my $fh, $f or die;
186 while (my $line = <$fh>) {
187 if ($line =~ /Issuer:/) {
195 # Tests for signing certs (broken in 1.1.1o)
196 my $a_key = "a-key.pem";
197 my $a_cert = "a-cert.pem";
198 my $a2_cert = "a2-cert.pem";
199 my $ca_key = "ca-key.pem";
200 my $ca_cert = "ca-cert.pem";
201 my $cnf = srctop_file('apps', 'openssl.cnf');
204 ok(run(app(["openssl", "req", "-x509", "-newkey", "rsa:2048",
206 "-keyout", $a_key, "-out", $a_cert, "-days", "365",
207 "-nodes", "-subj", "/CN=test.example.com"])));
208 # Create cert CA - note key size
209 ok(run(app(["openssl", "req", "-x509", "-newkey", "rsa:4096",
211 "-keyout", $ca_key, "-out", $ca_cert, "-days", "3650",
212 "-nodes", "-subj", "/CN=ca.example.com"])));
213 # Sign cert A with CA (errors on 1.1.1o)
214 ok(run(app(["openssl", "x509", "-in", $a_cert, "-CA", $ca_cert,
215 "-CAkey", $ca_key, "-set_serial", "1234567890",
216 "-preserve_dates", "-sha256", "-text", "-out", $a2_cert])));
217 # verify issuer is CA
218 ok (get_issuer($a2_cert) =~ /CN=ca.example.com/);
220 # Tests for issue #16080 (fixed in 1.1.1o)
221 my $b_key = "b-key.pem";
222 my $b_csr = "b-cert.csr";
223 my $b_cert = "b-cert.pem";
225 ok(run(app(["openssl", "req", "-new", "-newkey", "rsa:4096",
226 "-keyout", $b_key, "-out", $b_csr, "-nodes",
228 "-subj", "/CN=b.example.com"])));
229 # Sign it - position of "-text" matters!
230 ok(run(app(["openssl", "x509", "-req", "-text", "-CAcreateserial",
231 "-CA", $ca_cert, "-CAkey", $ca_key,
232 "-in", $b_csr, "-out", $b_cert])));
233 # Verify issuer is CA
234 ok(get_issuer($b_cert) =~ /CN=ca.example.com/);
236 # although no explicit extensions given:
237 has_version($b_cert, 3);
238 has_SKID($b_cert, 1);
239 has_AKID($b_cert, 1);
241 # Tests for https://github.com/openssl/openssl/issues/10442 (fixed in 1.1.1a)
242 # (incorrect default `-CAcreateserial` if `-CA` path has a dot in it)
243 my $folder_with_dot = "test_x509.folder";
244 ok(mkdir $folder_with_dot);
245 my $ca_cert_dot_in_dir = File::Spec->catfile($folder_with_dot, "ca-cert.pem");
246 ok(copy($ca_cert,$ca_cert_dot_in_dir));
247 my $ca_serial_dot_in_dir = File::Spec->catfile($folder_with_dot, "ca-cert.srl");
249 ok(run(app(["openssl", "x509", "-req", "-text", "-CAcreateserial",
250 "-CA", $ca_cert_dot_in_dir, "-CAkey", $ca_key,
252 ok(-e $ca_serial_dot_in_dir);
255 skip "EC is not supported by this OpenSSL build", 1
257 ok(run(test(["x509_test"])), "running x509_test");