xref: /freebsd/crypto/openssl/test/recipes/25-test_x509.t (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert#! /usr/bin/env perl
2*e0c4386eSCy Schubert# Copyright 2015-2024 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert#
4*e0c4386eSCy Schubert# Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert# this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert# in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert# https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubertuse strict;
11*e0c4386eSCy Schubertuse warnings;
12*e0c4386eSCy Schubert
13*e0c4386eSCy Schubertuse File::Spec;
14*e0c4386eSCy Schubertuse OpenSSL::Test::Utils;
15*e0c4386eSCy Schubertuse OpenSSL::Test qw/:DEFAULT srctop_file/;
16*e0c4386eSCy Schubert
17*e0c4386eSCy Schubertsetup("test_x509");
18*e0c4386eSCy Schubert
19*e0c4386eSCy Schubertplan tests => 29;
20*e0c4386eSCy Schubert
21*e0c4386eSCy Schubert# Prevent MSys2 filename munging for arguments that look like file paths but
22*e0c4386eSCy Schubert# aren't
23*e0c4386eSCy Schubert$ENV{MSYS2_ARG_CONV_EXCL} = "/CN=";
24*e0c4386eSCy Schubert
25*e0c4386eSCy Schubertrequire_ok(srctop_file("test", "recipes", "tconversion.pl"));
26*e0c4386eSCy Schubert
27*e0c4386eSCy Schubertmy @certs = qw(test certs);
28*e0c4386eSCy Schubertmy $pem = srctop_file(@certs, "cyrillic.pem");
29*e0c4386eSCy Schubertmy $out_msb = "out-cyrillic.msb";
30*e0c4386eSCy Schubertmy $out_utf8 = "out-cyrillic.utf8";
31*e0c4386eSCy Schubertmy $der = "cyrillic.der";
32*e0c4386eSCy Schubertmy $der2 = "cyrillic.der";
33*e0c4386eSCy Schubertmy $msb = srctop_file(@certs, "cyrillic.msb");
34*e0c4386eSCy Schubertmy $utf = srctop_file(@certs, "cyrillic.utf8");
35*e0c4386eSCy Schubert
36*e0c4386eSCy Schubertok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_msb,
37*e0c4386eSCy Schubert            "-nameopt", "esc_msb"])));
38*e0c4386eSCy Schubertis(cmp_text($out_msb, $msb),
39*e0c4386eSCy Schubert   0, 'Comparing esc_msb output with cyrillic.msb');
40*e0c4386eSCy Schubertok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_utf8,
41*e0c4386eSCy Schubert            "-nameopt", "utf8"])));
42*e0c4386eSCy Schubertis(cmp_text($out_utf8, $utf),
43*e0c4386eSCy Schubert   0, 'Comparing utf8 output with cyrillic.utf8');
44*e0c4386eSCy Schubert
45*e0c4386eSCy SchubertSKIP: {
46*e0c4386eSCy Schubert    skip "DES disabled", 1 if disabled("des");
47*e0c4386eSCy Schubert    skip "Platform doesn't support command line UTF-8", 1 if $^O =~ /^(VMS|msys)$/;
48*e0c4386eSCy Schubert
49*e0c4386eSCy Schubert    my $p12 = srctop_file("test", "shibboleth.pfx");
50*e0c4386eSCy Schubert    my $p12pass = "σύνθημα γνώρισμα";
51*e0c4386eSCy Schubert    my $out_pem = "out.pem";
52*e0c4386eSCy Schubert    ok(run(app(["openssl", "x509", "-text", "-in", $p12, "-out", $out_pem,
53*e0c4386eSCy Schubert                "-passin", "pass:$p12pass"])));
54*e0c4386eSCy Schubert    # not unlinking $out_pem
55*e0c4386eSCy Schubert}
56*e0c4386eSCy Schubert
57*e0c4386eSCy Schubertok(!run(app(["openssl", "x509", "-in", $pem, "-inform", "DER",
58*e0c4386eSCy Schubert             "-out", $der, "-outform", "DER"])),
59*e0c4386eSCy Schubert   "Checking failure of mismatching -inform DER");
60*e0c4386eSCy Schubertok(run(app(["openssl", "x509", "-in", $pem, "-inform", "PEM",
61*e0c4386eSCy Schubert            "-out", $der, "-outform", "DER"])),
62*e0c4386eSCy Schubert   "Conversion to DER");
63*e0c4386eSCy Schubertok(!run(app(["openssl", "x509", "-in", $der, "-inform", "PEM",
64*e0c4386eSCy Schubert             "-out", $der2, "-outform", "DER"])),
65*e0c4386eSCy Schubert   "Checking failure of mismatching -inform PEM");
66*e0c4386eSCy Schubert
67*e0c4386eSCy Schubert# producing and checking self-issued (but not self-signed) cert
68*e0c4386eSCy Schubertmy $subj = "/CN=CA"; # using same DN as in issuer of ee-cert.pem
69*e0c4386eSCy Schubertmy $extfile = srctop_file("test", "v3_ca_exts.cnf");
70*e0c4386eSCy Schubertmy $pkey = srctop_file(@certs, "ca-key.pem"); # issuer private key
71*e0c4386eSCy Schubertmy $pubkey = "ca-pubkey.pem"; # the corresponding issuer public key
72*e0c4386eSCy Schubert# use any (different) key for signing our self-issued cert:
73*e0c4386eSCy Schubertmy $signkey = srctop_file(@certs, "serverkey.pem");
74*e0c4386eSCy Schubertmy $selfout = "self-issued.out";
75*e0c4386eSCy Schubertmy $testcert = srctop_file(@certs, "ee-cert.pem");
76*e0c4386eSCy Schubertok(run(app(["openssl", "pkey", "-in", $pkey, "-pubout", "-out", $pubkey]))
77*e0c4386eSCy Schubert&& run(app(["openssl", "x509", "-new", "-force_pubkey", $pubkey,
78*e0c4386eSCy Schubert            "-subj", $subj, "-extfile", $extfile,
79*e0c4386eSCy Schubert            "-signkey", $signkey, "-out", $selfout]))
80*e0c4386eSCy Schubert&& run(app(["openssl", "verify", "-no_check_time",
81*e0c4386eSCy Schubert            "-trusted", $selfout, "-partial_chain", $testcert])));
82*e0c4386eSCy Schubert# not unlinking $pubkey
83*e0c4386eSCy Schubert# not unlinking $selfout
84*e0c4386eSCy Schubert
85*e0c4386eSCy Schubertsubtest 'x509 -- x.509 v1 certificate' => sub {
86*e0c4386eSCy Schubert    tconversion( -type => 'x509', -prefix => 'x509v1',
87*e0c4386eSCy Schubert                 -in => srctop_file("test", "testx509.pem") );
88*e0c4386eSCy Schubert};
89*e0c4386eSCy Schubertsubtest 'x509 -- first x.509 v3 certificate' => sub {
90*e0c4386eSCy Schubert    tconversion( -type => 'x509', -prefix => 'x509v3-1',
91*e0c4386eSCy Schubert                 -in => srctop_file("test", "v3-cert1.pem") );
92*e0c4386eSCy Schubert};
93*e0c4386eSCy Schubertsubtest 'x509 -- second x.509 v3 certificate' => sub {
94*e0c4386eSCy Schubert    tconversion( -type => 'x509', -prefix => 'x509v3-2',
95*e0c4386eSCy Schubert                 -in => srctop_file("test", "v3-cert2.pem") );
96*e0c4386eSCy Schubert};
97*e0c4386eSCy Schubert
98*e0c4386eSCy Schubertsubtest 'x509 -- pathlen' => sub {
99*e0c4386eSCy Schubert    ok(run(test(["v3ext", srctop_file(@certs, "pathlen.pem")])));
100*e0c4386eSCy Schubert};
101*e0c4386eSCy Schubert
102*e0c4386eSCy Schubertcert_contains(srctop_file(@certs, "fake-gp.pem"),
103*e0c4386eSCy Schubert              "2.16.528.1.1003.1.3.5.5.2-1-0000006666-Z-12345678-01.015-12345678",
104*e0c4386eSCy Schubert              1, 'x500 -- subjectAltName');
105*e0c4386eSCy Schubert
106*e0c4386eSCy Schubertsub test_errors { # actually tests diagnostics of OSSL_STORE
107*e0c4386eSCy Schubert    my ($expected, $cert, @opts) = @_;
108*e0c4386eSCy Schubert    my $infile = srctop_file(@certs, $cert);
109*e0c4386eSCy Schubert    my @args = qw(openssl x509 -in);
110*e0c4386eSCy Schubert    push(@args, $infile, @opts);
111*e0c4386eSCy Schubert    my $tmpfile = 'out.txt';
112*e0c4386eSCy Schubert    my $res =  grep(/-text/, @opts) ? run(app([@args], stdout => $tmpfile))
113*e0c4386eSCy Schubert                                    : !run(app([@args], stderr => $tmpfile));
114*e0c4386eSCy Schubert    my $found = 0;
115*e0c4386eSCy Schubert    open(my $in, '<', $tmpfile) or die "Could not open file $tmpfile";
116*e0c4386eSCy Schubert    while(<$in>) {
117*e0c4386eSCy Schubert        print; # this may help debugging
118*e0c4386eSCy Schubert        $res &&= !m/asn1 encoding/; # output must not include ASN.1 parse errors
119*e0c4386eSCy Schubert        $found = 1 if m/$expected/; # output must include $expected
120*e0c4386eSCy Schubert    }
121*e0c4386eSCy Schubert    close $in;
122*e0c4386eSCy Schubert    # $tmpfile is kept to help with investigation in case of failure
123*e0c4386eSCy Schubert    return $res && $found;
124*e0c4386eSCy Schubert}
125*e0c4386eSCy Schubert
126*e0c4386eSCy Schubert# 3 tests for non-existence of spurious OSSL_STORE ASN.1 parse error output.
127*e0c4386eSCy Schubert# This requires provoking a failure exit of the app after reading input files.
128*e0c4386eSCy Schubertok(test_errors("Bad output format", "root-cert.pem", '-outform', 'http'),
129*e0c4386eSCy Schubert   "load root-cert errors");
130*e0c4386eSCy Schubertok(test_errors("RC2-40-CBC", "v3-certs-RC2.p12", '-passin', 'pass:v3-certs'),
131*e0c4386eSCy Schubert   "load v3-certs-RC2 no asn1 errors"); # error msg should mention "RC2-40-CBC"
132*e0c4386eSCy SchubertSKIP: {
133*e0c4386eSCy Schubert    skip "sm2 not disabled", 1 if !disabled("sm2");
134*e0c4386eSCy Schubert
135*e0c4386eSCy Schubert    ok(test_errors("Unable to load Public Key", "sm2.pem", '-text'),
136*e0c4386eSCy Schubert       "error loading unsupported sm2 cert");
137*e0c4386eSCy Schubert}
138*e0c4386eSCy Schubert
139*e0c4386eSCy Schubert# 3 tests for -dateopts formats
140*e0c4386eSCy Schubertok(run(app(["openssl", "x509", "-noout", "-dates", "-dateopt", "rfc_822",
141*e0c4386eSCy Schubert	     "-in", srctop_file("test/certs", "ca-cert.pem")])),
142*e0c4386eSCy Schubert   "Run with rfc_8222 -dateopt format");
143*e0c4386eSCy Schubertok(run(app(["openssl", "x509", "-noout", "-dates", "-dateopt", "iso_8601",
144*e0c4386eSCy Schubert	     "-in", srctop_file("test/certs", "ca-cert.pem")])),
145*e0c4386eSCy Schubert   "Run with iso_8601 -dateopt format");
146*e0c4386eSCy Schubertok(!run(app(["openssl", "x509", "-noout", "-dates", "-dateopt", "invalid_format",
147*e0c4386eSCy Schubert	     "-in", srctop_file("test/certs", "ca-cert.pem")])),
148*e0c4386eSCy Schubert   "Run with invalid -dateopt format");
149*e0c4386eSCy Schubert
150*e0c4386eSCy Schubert# extracts issuer from a -text formatted-output
151*e0c4386eSCy Schubertsub get_issuer {
152*e0c4386eSCy Schubert    my $f = shift(@_);
153*e0c4386eSCy Schubert    my $issuer = "";
154*e0c4386eSCy Schubert    open my $fh, $f or die;
155*e0c4386eSCy Schubert    while (my $line = <$fh>) {
156*e0c4386eSCy Schubert        if ($line =~ /Issuer:/) {
157*e0c4386eSCy Schubert            $issuer = $line;
158*e0c4386eSCy Schubert        }
159*e0c4386eSCy Schubert    }
160*e0c4386eSCy Schubert    close $fh;
161*e0c4386eSCy Schubert    return $issuer;
162*e0c4386eSCy Schubert}
163*e0c4386eSCy Schubert
164*e0c4386eSCy Schubert# Tests for signing certs (broken in 1.1.1o)
165*e0c4386eSCy Schubertmy $a_key = "a-key.pem";
166*e0c4386eSCy Schubertmy $a_cert = "a-cert.pem";
167*e0c4386eSCy Schubertmy $a2_cert = "a2-cert.pem";
168*e0c4386eSCy Schubertmy $ca_key = "ca-key.pem";
169*e0c4386eSCy Schubertmy $ca_cert = "ca-cert.pem";
170*e0c4386eSCy Schubertmy $cnf = srctop_file('apps', 'openssl.cnf');
171*e0c4386eSCy Schubert
172*e0c4386eSCy Schubert# Create cert A
173*e0c4386eSCy Schubertok(run(app(["openssl", "req", "-x509", "-newkey", "rsa:2048",
174*e0c4386eSCy Schubert            "-config", $cnf,
175*e0c4386eSCy Schubert            "-keyout", $a_key, "-out", $a_cert, "-days", "365",
176*e0c4386eSCy Schubert            "-nodes", "-subj", "/CN=test.example.com"])));
177*e0c4386eSCy Schubert# Create cert CA - note key size
178*e0c4386eSCy Schubertok(run(app(["openssl", "req", "-x509", "-newkey", "rsa:4096",
179*e0c4386eSCy Schubert            "-config", $cnf,
180*e0c4386eSCy Schubert            "-keyout", $ca_key, "-out", $ca_cert, "-days", "3650",
181*e0c4386eSCy Schubert            "-nodes", "-subj", "/CN=ca.example.com"])));
182*e0c4386eSCy Schubert# Sign cert A with CA (errors on 1.1.1o)
183*e0c4386eSCy Schubertok(run(app(["openssl", "x509", "-in", $a_cert, "-CA", $ca_cert,
184*e0c4386eSCy Schubert            "-CAkey", $ca_key, "-set_serial", "1234567890",
185*e0c4386eSCy Schubert            "-preserve_dates", "-sha256", "-text", "-out", $a2_cert])));
186*e0c4386eSCy Schubert# verify issuer is CA
187*e0c4386eSCy Schubertok (get_issuer($a2_cert) =~ /CN = ca.example.com/);
188*e0c4386eSCy Schubert
189*e0c4386eSCy Schubertmy $in_csr = srctop_file('test', 'certs', 'x509-check.csr');
190*e0c4386eSCy Schubertmy $in_key = srctop_file('test', 'certs', 'x509-check-key.pem');
191*e0c4386eSCy Schubertmy $invextfile = srctop_file('test', 'invalid-x509.cnf');
192*e0c4386eSCy Schubert# Test that invalid extensions settings fail
193*e0c4386eSCy Schubertok(!run(app(["openssl", "x509", "-req", "-in", $in_csr, "-signkey", $in_key,
194*e0c4386eSCy Schubert            "-out", "/dev/null", "-days", "3650" , "-extensions", "ext",
195*e0c4386eSCy Schubert            "-extfile", $invextfile])));
196*e0c4386eSCy Schubert
197*e0c4386eSCy Schubert# Tests for issue #16080 (fixed in 1.1.1o)
198*e0c4386eSCy Schubertmy $b_key = "b-key.pem";
199*e0c4386eSCy Schubertmy $b_csr = "b-cert.csr";
200*e0c4386eSCy Schubertmy $b_cert = "b-cert.pem";
201*e0c4386eSCy Schubert# Create the CSR
202*e0c4386eSCy Schubertok(run(app(["openssl", "req", "-new", "-newkey", "rsa:4096",
203*e0c4386eSCy Schubert            "-keyout", $b_key, "-out", $b_csr, "-nodes",
204*e0c4386eSCy Schubert            "-config", $cnf,
205*e0c4386eSCy Schubert            "-subj", "/CN=b.example.com"])));
206*e0c4386eSCy Schubert# Sign it - position of "-text" matters!
207*e0c4386eSCy Schubertok(run(app(["openssl", "x509", "-req", "-text", "-CAcreateserial",
208*e0c4386eSCy Schubert            "-CA", $ca_cert, "-CAkey", $ca_key,
209*e0c4386eSCy Schubert            "-in", $b_csr, "-out", $b_cert])));
210*e0c4386eSCy Schubert# Verify issuer is CA
211*e0c4386eSCy Schubertok(get_issuer($b_cert) =~ /CN = ca.example.com/);
212