]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/recipes/25-test_x509.t
openssl_hexstr2buf_sep(): Prevent misleading 'malloc failure' errors on short input
[thirdparty/openssl.git] / test / recipes / 25-test_x509.t
CommitLineData
596d6b7e 1#! /usr/bin/env perl
33388b44 2# Copyright 2015-2020 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
f7626d0b 19plan tests => 15;
4650de3e 20
42e0ccdf 21require_ok(srctop_file('test','recipes','tconversion.pl'));
4650de3e 22
4772610c 23my $pem = srctop_file("test/certs", "cyrillic.pem");
42f7a489
RL
24my $out_msb = "out-cyrillic.msb";
25my $out_utf8 = "out-cyrillic.utf8";
4772610c
RS
26my $msb = srctop_file("test/certs", "cyrillic.msb");
27my $utf = srctop_file("test/certs", "cyrillic.utf8");
28
42f7a489 29ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_msb,
4772610c 30 "-nameopt", "esc_msb"])));
42f7a489 31is(cmp_text($out_msb, srctop_file("test/certs", "cyrillic.msb")),
4772610c 32 0, 'Comparing esc_msb output');
42f7a489 33ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_utf8,
4772610c 34 "-nameopt", "utf8"])));
42f7a489 35is(cmp_text($out_utf8, srctop_file("test/certs", "cyrillic.utf8")),
4772610c 36 0, 'Comparing utf8 output');
4772610c 37
2a33470b
DDO
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
1f019cd0
MC
49SKIP: {
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
4acd484d 55 my $extfile = srctop_file("test", "v3_ca_exts.cnf");
1f019cd0
MC
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,
4acd484d
DDO
65 "-subj", $subj, "-extfile", $extfile,
66 "-signkey", $signkey, "-out", $selfout]))
1f019cd0
MC
67 &&
68 run(app(["openssl", "verify", "-no_check_time",
4acd484d
DDO
69 "-trusted", $selfout, "-partial_chain", $testcert])));
70 unlink $pubkey;
71 unlink $selfout;
1f019cd0 72}
52958608 73
4650de3e 74subtest 'x509 -- x.509 v1 certificate' => sub {
b40498c6
RL
75 tconversion( -type => 'x509', -prefix => 'x509v1',
76 -in => srctop_file("test","testx509.pem") );
4650de3e
RL
77};
78subtest 'x509 -- first x.509 v3 certificate' => sub {
b40498c6
RL
79 tconversion( -type => 'x509', -prefix => 'x509v3-1',
80 -in => srctop_file("test","v3-cert1.pem") );
4650de3e
RL
81};
82subtest 'x509 -- second x.509 v3 certificate' => sub {
b40498c6
RL
83 tconversion( -type => 'x509', -prefix => 'x509v3-2',
84 -in => srctop_file("test","v3-cert2.pem") );
4650de3e 85};
e417070c
RS
86
87subtest 'x509 -- pathlen' => sub {
88 ok(run(test(["v3ext", srctop_file("test/certs", "pathlen.pem")])));
47f387e9
DWG
89};
90
91subtest 'x500 -- subjectAltName' => sub {
92 my $fp = srctop_file("test/certs", "fake-gp.pem");
93 my $out = "ext.out";
94 ok(run(app(["openssl", "x509", "-text", "-in", $fp, "-out", $out])));
95 ok(has_doctor_id($out));
96 unlink $out;
97};
98
99sub has_doctor_id {
100 $_ = shift @_;
101 open(DATA,$_) or return 0;
102 $_= join('',<DATA>);
103 close(DATA);
104 return m/2.16.528.1.1003.1.3.5.5.2-1-0000006666-Z-12345678-01.015-12345678/;
e417070c 105}
29844ea5
DDO
106
107sub test_errors { # actually tests diagnostics of OSSL_STORE
108 my ($expected, $cert, @opts) = @_;
109 my $infile = srctop_file('test', 'certs', $cert);
110 my @args = qw(openssl x509 -in);
9032c2c1 111 push(@args, $infile, @opts);
29844ea5 112 my $tmpfile = 'out.txt';
9032c2c1
DDO
113 my $res = grep(/-text/, @opts) ? run(app([@args], stdout => $tmpfile))
114 : !run(app([@args], stderr => $tmpfile));
29844ea5
DDO
115 my $found = 0;
116 open(my $in, '<', $tmpfile) or die "Could not open file $tmpfile";
117 while(<$in>) {
118 print; # this may help debugging
119 $res &&= !m/asn1 encoding/; # output must not include ASN.1 parse errors
120 $found = 1 if m/$expected/; # output must include $expected
121 }
122 close $in;
9032c2c1 123 # $tmpfile is kept to help with investigation in case of failure
29844ea5
DDO
124 return $res && $found;
125}
126
f7626d0b
DDO
127# 3 tests for non-existence of spurious OSSL_STORE ASN.1 parse error output.
128# This requires provoking a failure exit of the app after reading input files.
129ok(test_errors("bad output format", "root-cert.pem", '-outform', 'http'),
130 "load root-cert errors");
29844ea5 131ok(test_errors("RC2-40-CBC", "v3-certs-RC2.p12", '-passin', 'pass:v3-certs'),
f7626d0b 132 "load v3-certs-RC2 no asn1 errors"); # error msg should mention "RC2-40-CBC"
9032c2c1
DDO
133SKIP: {
134 skip "sm2 not disabled", 1 if !disabled("sm2");
135
136 ok(test_errors("unknown group|unsupported algorithm", "sm2.pem", '-text'),
137 "error loading unsupported sm2 cert");
138}