Add some tests for -inform/keyform enforcement
[openssl.git] / test / recipes / 25-test_x509.t
1 #! /usr/bin/env perl
2 # Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
3 #
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
8
9
10 use strict;
11 use warnings;
12
13 use File::Spec;
14 use OpenSSL::Test::Utils;
15 use OpenSSL::Test qw/:DEFAULT srctop_file/;
16
17 setup("test_x509");
18
19 plan tests => 18;
20
21 require_ok(srctop_file("test", "recipes", "tconversion.pl"));
22
23 my @certs = qw(test certs);
24 my $pem = srctop_file(@certs, "cyrillic.pem");
25 my $out_msb = "out-cyrillic.msb";
26 my $out_utf8 = "out-cyrillic.utf8";
27 my $der = "cyrillic.der";
28 my $der2 = "cyrillic.der";
29 my $msb = srctop_file(@certs, "cyrillic.msb");
30 my $utf = srctop_file(@certs, "cyrillic.utf8");
31
32 ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_msb,
33             "-nameopt", "esc_msb"])));
34 is(cmp_text($out_msb, $msb),
35    0, 'Comparing esc_msb output with cyrillic.msb');
36 ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_utf8,
37             "-nameopt", "utf8"])));
38 is(cmp_text($out_utf8, $utf),
39    0, 'Comparing utf8 output with cyrillic.utf8');
40
41 SKIP: {
42     skip "DES disabled", 1 if disabled("des");
43
44     my $p12 = srctop_file("test", "shibboleth.pfx");
45     my $p12pass = "σύνθημα γνώρισμα";
46     my $out_pem = "out.pem";
47     ok(run(app(["openssl", "x509", "-text", "-in", $p12, "-out", $out_pem,
48                 "-passin", "pass:$p12pass"])));
49     # not unlinking $out_pem
50 }
51
52 ok(!run(app(["openssl", "x509", "-in", $pem, "-inform", "DER",
53              "-out", $der, "-outform", "DER"])),
54    "Checking failure of mismatching -inform DER");
55 ok(run(app(["openssl", "x509", "-in", $pem, "-inform", "PEM",
56             "-out", $der, "-outform", "DER"])),
57    "Conversion to DER");
58 ok(!run(app(["openssl", "x509", "-in", $der, "-inform", "PEM",
59              "-out", $der2, "-outform", "DER"])),
60    "Checking failure of mismatching -inform PEM");
61
62 # producing and checking self-issued (but not self-signed) cert
63 my $subj = "/CN=CA"; # using same DN as in issuer of ee-cert.pem
64 my $extfile = srctop_file("test", "v3_ca_exts.cnf");
65 my $pkey = srctop_file(@certs, "ca-key.pem"); # issuer private key
66 my $pubkey = "ca-pubkey.pem"; # the corresponding issuer public key
67 # use any (different) key for signing our self-issued cert:
68 my $signkey = srctop_file(@certs, "serverkey.pem");
69 my $selfout = "self-issued.out";
70 my $testcert = srctop_file(@certs, "ee-cert.pem");
71 ok(run(app(["openssl", "pkey", "-in", $pkey, "-pubout", "-out", $pubkey]))
72 && run(app(["openssl", "x509", "-new", "-force_pubkey", $pubkey,
73             "-subj", $subj, "-extfile", $extfile,
74             "-signkey", $signkey, "-out", $selfout]))
75 && run(app(["openssl", "verify", "-no_check_time",
76             "-trusted", $selfout, "-partial_chain", $testcert])));
77 # not unlinking $pubkey
78 # not unlinking $selfout
79
80 subtest 'x509 -- x.509 v1 certificate' => sub {
81     tconversion( -type => 'x509', -prefix => 'x509v1',
82                  -in => srctop_file("test", "testx509.pem") );
83 };
84 subtest 'x509 -- first x.509 v3 certificate' => sub {
85     tconversion( -type => 'x509', -prefix => 'x509v3-1',
86                  -in => srctop_file("test", "v3-cert1.pem") );
87 };
88 subtest 'x509 -- second x.509 v3 certificate' => sub {
89     tconversion( -type => 'x509', -prefix => 'x509v3-2',
90                  -in => srctop_file("test", "v3-cert2.pem") );
91 };
92
93 subtest 'x509 -- pathlen' => sub {
94     ok(run(test(["v3ext", srctop_file(@certs, "pathlen.pem")])));
95 };
96
97 cert_contains(srctop_file(@certs, "fake-gp.pem"),
98               "2.16.528.1.1003.1.3.5.5.2-1-0000006666-Z-12345678-01.015-12345678",
99               1, 'x500 -- subjectAltName');
100
101 sub test_errors { # actually tests diagnostics of OSSL_STORE
102     my ($expected, $cert, @opts) = @_;
103     my $infile = srctop_file(@certs, $cert);
104     my @args = qw(openssl x509 -in);
105     push(@args, $infile, @opts);
106     my $tmpfile = 'out.txt';
107     my $res =  grep(/-text/, @opts) ? run(app([@args], stdout => $tmpfile))
108                                     : !run(app([@args], stderr => $tmpfile));
109     my $found = 0;
110     open(my $in, '<', $tmpfile) or die "Could not open file $tmpfile";
111     while(<$in>) {
112         print; # this may help debugging
113         $res &&= !m/asn1 encoding/; # output must not include ASN.1 parse errors
114         $found = 1 if m/$expected/; # output must include $expected
115     }
116     close $in;
117     # $tmpfile is kept to help with investigation in case of failure
118     return $res && $found;
119 }
120
121 # 3 tests for non-existence of spurious OSSL_STORE ASN.1 parse error output.
122 # This requires provoking a failure exit of the app after reading input files.
123 ok(test_errors("Bad output format", "root-cert.pem", '-outform', 'http'),
124    "load root-cert errors");
125 ok(test_errors("RC2-40-CBC", "v3-certs-RC2.p12", '-passin', 'pass:v3-certs'),
126    "load v3-certs-RC2 no asn1 errors"); # error msg should mention "RC2-40-CBC"
127 SKIP: {
128     skip "sm2 not disabled", 1 if !disabled("sm2");
129
130     ok(test_errors("unknown group|unsupported algorithm", "sm2.pem", '-text'),
131        "error loading unsupported sm2 cert");
132 }