Prune low-level ASN.1 parse errors from error queue in decoder_process()
[openssl.git] / test / recipes / 25-test_x509.t
1 #! /usr/bin/env perl
2 # Copyright 2015-2020 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 => 14;
20
21 require_ok(srctop_file('test','recipes','tconversion.pl'));
22
23 my $pem = srctop_file("test/certs", "cyrillic.pem");
24 my $out_msb = "out-cyrillic.msb";
25 my $out_utf8 = "out-cyrillic.utf8";
26 my $msb = srctop_file("test/certs", "cyrillic.msb");
27 my $utf = srctop_file("test/certs", "cyrillic.utf8");
28
29 ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_msb,
30             "-nameopt", "esc_msb"])));
31 is(cmp_text($out_msb, srctop_file("test/certs", "cyrillic.msb")),
32    0, 'Comparing esc_msb output');
33 ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_utf8,
34             "-nameopt", "utf8"])));
35 is(cmp_text($out_utf8, srctop_file("test/certs", "cyrillic.utf8")),
36    0, 'Comparing utf8 output');
37
38  SKIP: {
39     skip "DES disabled", 1 if disabled("des");
40
41     my $p12 = srctop_file("test", "shibboleth.pfx");
42     my $p12pass = "σύνθημα γνώρισμα";
43     my $out_pem = "out.pem";
44     ok(run(app(["openssl", "x509", "-text", "-in", $p12, "-out", $out_pem,
45                 "-passin", "pass:$p12pass"])));
46     unlink $out_pem;
47 }
48
49 SKIP: {
50     skip "EC disabled", 1 if disabled("ec");
51
52     # producing and checking self-issued (but not self-signed) cert
53     my @path = qw(test certs);
54     my $subj = "/CN=CA"; # using same DN as in issuer of ee-cert.pem
55     my $extfile = srctop_file("test", "v3_ca_exts.cnf");
56     my $pkey = srctop_file(@path, "ca-key.pem"); #  issuer private key
57     my $pubkey = "ca-pubkey.pem"; # the corresponding issuer public key
58     # use any (different) key for signing our self-issued cert:
59     my $signkey = srctop_file(@path, "ee-ecdsa-key.pem");
60     my $selfout = "self-issued.out";
61     my $testcert = srctop_file(@path, "ee-cert.pem");
62     ok(run(app(["openssl", "pkey", "-in", $pkey, "-pubout", "-out", $pubkey]))
63        &&
64        run(app(["openssl", "x509", "-new", "-force_pubkey", $pubkey,
65                 "-subj", $subj, "-extfile", $extfile,
66                 "-signkey", $signkey, "-out", $selfout]))
67        &&
68        run(app(["openssl", "verify", "-no_check_time",
69                 "-trusted", $selfout, "-partial_chain", $testcert])));
70     unlink $pubkey;
71     unlink $selfout;
72 }
73
74 subtest 'x509 -- x.509 v1 certificate' => sub {
75     tconversion("x509", srctop_file("test","testx509.pem"));
76 };
77 subtest 'x509 -- first x.509 v3 certificate' => sub {
78     tconversion("x509", srctop_file("test","v3-cert1.pem"));
79 };
80 subtest 'x509 -- second x.509 v3 certificate' => sub {
81     tconversion("x509", srctop_file("test","v3-cert2.pem"));
82 };
83
84 subtest 'x509 -- pathlen' => sub {
85     ok(run(test(["v3ext", srctop_file("test/certs", "pathlen.pem")])));
86 };
87
88 subtest 'x500 -- subjectAltName' => sub {
89      my $fp = srctop_file("test/certs", "fake-gp.pem");
90      my $out = "ext.out";
91      ok(run(app(["openssl", "x509", "-text", "-in", $fp, "-out", $out])));
92      ok(has_doctor_id($out));
93      unlink $out;
94 };
95
96 sub has_doctor_id { 
97     $_ = shift @_;
98     open(DATA,$_) or return 0;
99     $_= join('',<DATA>); 
100     close(DATA);
101     return m/2.16.528.1.1003.1.3.5.5.2-1-0000006666-Z-12345678-01.015-12345678/;
102 }
103
104 sub test_errors { # actually tests diagnostics of OSSL_STORE
105     my ($expected, $cert, @opts) = @_;
106     my $infile = srctop_file('test', 'certs', $cert);
107     my @args = qw(openssl x509 -in);
108     push(@args, "$infile", @opts);
109     my $tmpfile = 'out.txt';
110     my $res = !run(app([@args], stderr => $tmpfile));
111     my $found = 0;
112     open(my $in, '<', $tmpfile) or die "Could not open file $tmpfile";
113     while(<$in>) {
114         print; # this may help debugging
115         $res &&= !m/asn1 encoding/; # output must not include ASN.1 parse errors
116         $found = 1 if m/$expected/; # output must include $expected
117     }
118     close $in;
119     unlink $tmpfile;
120     return $res && $found;
121 }
122
123 ok(test_errors("Can't open any-dir/", "root-cert.pem", '-out', 'any-dir/'),
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");