]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/recipes/25-test_x509.t
Only use the legacy route to decode a public key if we have to
[thirdparty/openssl.git] / test / recipes / 25-test_x509.t
CommitLineData
596d6b7e 1#! /usr/bin/env perl
4333b89f 2# Copyright 2015-2021 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
10use strict;
11use warnings;
12
13use File::Spec;
1f019cd0 14use OpenSSL::Test::Utils;
42e0ccdf 15use OpenSSL::Test qw/:DEFAULT srctop_file/;
4650de3e
RL
16
17setup("test_x509");
18
d105a24c 19plan tests => 18;
4650de3e 20
abc4439c 21require_ok(srctop_file("test", "recipes", "tconversion.pl"));
4650de3e 22
f0a057dd 23my @certs = qw(test certs);
abc4439c 24my $pem = srctop_file(@certs, "cyrillic.pem");
42f7a489
RL
25my $out_msb = "out-cyrillic.msb";
26my $out_utf8 = "out-cyrillic.utf8";
d105a24c
TM
27my $der = "cyrillic.der";
28my $der2 = "cyrillic.der";
abc4439c
DDO
29my $msb = srctop_file(@certs, "cyrillic.msb");
30my $utf = srctop_file(@certs, "cyrillic.utf8");
4772610c 31
42f7a489 32ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_msb,
4772610c 33 "-nameopt", "esc_msb"])));
2c8a740a 34is(cmp_text($out_msb, $msb),
05458fdb 35 0, 'Comparing esc_msb output with cyrillic.msb');
42f7a489 36ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_utf8,
4772610c 37 "-nameopt", "utf8"])));
2c8a740a 38is(cmp_text($out_utf8, $utf),
05458fdb 39 0, 'Comparing utf8 output with cyrillic.utf8');
4772610c 40
d105a24c 41SKIP: {
2a33470b
DDO
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"])));
8cadc517 49 # not unlinking $out_pem
2a33470b
DDO
50}
51
d105a24c
TM
52ok(!run(app(["openssl", "x509", "-in", $pem, "-inform", "DER",
53 "-out", $der, "-outform", "DER"])),
54 "Checking failure of mismatching -inform DER");
55ok(run(app(["openssl", "x509", "-in", $pem, "-inform", "PEM",
56 "-out", $der, "-outform", "DER"])),
57 "Conversion to DER");
58ok(!run(app(["openssl", "x509", "-in", $der, "-inform", "PEM",
59 "-out", $der2, "-outform", "DER"])),
60 "Checking failure of mismatching -inform PEM");
61
49b36afb
DDO
62# producing and checking self-issued (but not self-signed) cert
63my $subj = "/CN=CA"; # using same DN as in issuer of ee-cert.pem
64my $extfile = srctop_file("test", "v3_ca_exts.cnf");
65my $pkey = srctop_file(@certs, "ca-key.pem"); # issuer private key
66my $pubkey = "ca-pubkey.pem"; # the corresponding issuer public key
67# use any (different) key for signing our self-issued cert:
68my $signkey = srctop_file(@certs, "serverkey.pem");
69my $selfout = "self-issued.out";
70my $testcert = srctop_file(@certs, "ee-cert.pem");
71ok(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
52958608 79
4650de3e 80subtest 'x509 -- x.509 v1 certificate' => sub {
b40498c6 81 tconversion( -type => 'x509', -prefix => 'x509v1',
abc4439c 82 -in => srctop_file("test", "testx509.pem") );
4650de3e
RL
83};
84subtest 'x509 -- first x.509 v3 certificate' => sub {
b40498c6 85 tconversion( -type => 'x509', -prefix => 'x509v3-1',
abc4439c 86 -in => srctop_file("test", "v3-cert1.pem") );
4650de3e
RL
87};
88subtest 'x509 -- second x.509 v3 certificate' => sub {
b40498c6 89 tconversion( -type => 'x509', -prefix => 'x509v3-2',
abc4439c 90 -in => srctop_file("test", "v3-cert2.pem") );
4650de3e 91};
e417070c
RS
92
93subtest 'x509 -- pathlen' => sub {
abc4439c 94 ok(run(test(["v3ext", srctop_file(@certs, "pathlen.pem")])));
47f387e9
DWG
95};
96
f0a057dd
DDO
97cert_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');
29844ea5
DDO
100
101sub test_errors { # actually tests diagnostics of OSSL_STORE
102 my ($expected, $cert, @opts) = @_;
abc4439c 103 my $infile = srctop_file(@certs, $cert);
29844ea5 104 my @args = qw(openssl x509 -in);
9032c2c1 105 push(@args, $infile, @opts);
29844ea5 106 my $tmpfile = 'out.txt';
9032c2c1
DDO
107 my $res = grep(/-text/, @opts) ? run(app([@args], stdout => $tmpfile))
108 : !run(app([@args], stderr => $tmpfile));
29844ea5
DDO
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;
9032c2c1 117 # $tmpfile is kept to help with investigation in case of failure
29844ea5
DDO
118 return $res && $found;
119}
120
f7626d0b
DDO
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.
1a683b80 123ok(test_errors("Bad output format", "root-cert.pem", '-outform', 'http'),
f7626d0b 124 "load root-cert errors");
29844ea5 125ok(test_errors("RC2-40-CBC", "v3-certs-RC2.p12", '-passin', 'pass:v3-certs'),
f7626d0b 126 "load v3-certs-RC2 no asn1 errors"); # error msg should mention "RC2-40-CBC"
9032c2c1
DDO
127SKIP: {
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}