2016-04-22 19:21:51 +08:00
|
|
|
#! /usr/bin/env perl
|
2021-01-28 20:54:57 +08:00
|
|
|
# Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2016-04-22 19:21:51 +08:00
|
|
|
#
|
2018-12-06 20:05:25 +08:00
|
|
|
# Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-04-22 19:21:51 +08:00
|
|
|
# this file except in compliance with the License. You can obtain a copy
|
|
|
|
# in the file LICENSE in the source distribution or at
|
|
|
|
# https://www.openssl.org/source/license.html
|
|
|
|
|
2015-04-18 02:15:22 +08:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use File::Spec;
|
2019-03-27 00:07:02 +08:00
|
|
|
use OpenSSL::Test::Utils;
|
2016-01-30 08:05:33 +08:00
|
|
|
use OpenSSL::Test qw/:DEFAULT srctop_file/;
|
2015-04-18 02:15:22 +08:00
|
|
|
|
|
|
|
setup("test_x509");
|
|
|
|
|
2021-05-03 20:40:06 +08:00
|
|
|
plan tests => 18;
|
2015-04-18 02:15:22 +08:00
|
|
|
|
2020-12-10 23:41:03 +08:00
|
|
|
require_ok(srctop_file("test", "recipes", "tconversion.pl"));
|
2015-04-18 02:15:22 +08:00
|
|
|
|
2020-12-20 02:49:25 +08:00
|
|
|
my @certs = qw(test certs);
|
2020-12-10 23:41:03 +08:00
|
|
|
my $pem = srctop_file(@certs, "cyrillic.pem");
|
2020-02-13 07:28:47 +08:00
|
|
|
my $out_msb = "out-cyrillic.msb";
|
|
|
|
my $out_utf8 = "out-cyrillic.utf8";
|
2021-05-03 20:40:06 +08:00
|
|
|
my $der = "cyrillic.der";
|
|
|
|
my $der2 = "cyrillic.der";
|
2020-12-10 23:41:03 +08:00
|
|
|
my $msb = srctop_file(@certs, "cyrillic.msb");
|
|
|
|
my $utf = srctop_file(@certs, "cyrillic.utf8");
|
2017-03-15 02:53:35 +08:00
|
|
|
|
2020-02-13 07:28:47 +08:00
|
|
|
ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_msb,
|
2017-03-15 02:53:35 +08:00
|
|
|
"-nameopt", "esc_msb"])));
|
2021-03-03 01:55:35 +08:00
|
|
|
is(cmp_text($out_msb, $msb),
|
2021-01-09 00:43:13 +08:00
|
|
|
0, 'Comparing esc_msb output with cyrillic.msb');
|
2020-02-13 07:28:47 +08:00
|
|
|
ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_utf8,
|
2017-03-15 02:53:35 +08:00
|
|
|
"-nameopt", "utf8"])));
|
2021-03-03 01:55:35 +08:00
|
|
|
is(cmp_text($out_utf8, $utf),
|
2021-01-09 00:43:13 +08:00
|
|
|
0, 'Comparing utf8 output with cyrillic.utf8');
|
2017-03-15 02:53:35 +08:00
|
|
|
|
2021-05-03 20:40:06 +08:00
|
|
|
SKIP: {
|
2020-08-16 21:25:27 +08:00
|
|
|
skip "DES disabled", 1 if disabled("des");
|
|
|
|
|
|
|
|
my $p12 = srctop_file("test", "shibboleth.pfx");
|
|
|
|
my $p12pass = "σύνθημα γνώρισμα";
|
|
|
|
my $out_pem = "out.pem";
|
|
|
|
ok(run(app(["openssl", "x509", "-text", "-in", $p12, "-out", $out_pem,
|
|
|
|
"-passin", "pass:$p12pass"])));
|
2020-12-10 23:32:13 +08:00
|
|
|
# not unlinking $out_pem
|
2020-08-16 21:25:27 +08:00
|
|
|
}
|
|
|
|
|
2021-05-03 20:40:06 +08:00
|
|
|
ok(!run(app(["openssl", "x509", "-in", $pem, "-inform", "DER",
|
|
|
|
"-out", $der, "-outform", "DER"])),
|
|
|
|
"Checking failure of mismatching -inform DER");
|
|
|
|
ok(run(app(["openssl", "x509", "-in", $pem, "-inform", "PEM",
|
|
|
|
"-out", $der, "-outform", "DER"])),
|
|
|
|
"Conversion to DER");
|
|
|
|
ok(!run(app(["openssl", "x509", "-in", $der, "-inform", "PEM",
|
|
|
|
"-out", $der2, "-outform", "DER"])),
|
|
|
|
"Checking failure of mismatching -inform PEM");
|
|
|
|
|
2020-12-11 00:01:45 +08:00
|
|
|
# producing and checking self-issued (but not self-signed) cert
|
|
|
|
my $subj = "/CN=CA"; # using same DN as in issuer of ee-cert.pem
|
|
|
|
my $extfile = srctop_file("test", "v3_ca_exts.cnf");
|
|
|
|
my $pkey = srctop_file(@certs, "ca-key.pem"); # issuer private key
|
|
|
|
my $pubkey = "ca-pubkey.pem"; # the corresponding issuer public key
|
|
|
|
# use any (different) key for signing our self-issued cert:
|
|
|
|
my $signkey = srctop_file(@certs, "serverkey.pem");
|
|
|
|
my $selfout = "self-issued.out";
|
|
|
|
my $testcert = srctop_file(@certs, "ee-cert.pem");
|
|
|
|
ok(run(app(["openssl", "pkey", "-in", $pkey, "-pubout", "-out", $pubkey]))
|
|
|
|
&& run(app(["openssl", "x509", "-new", "-force_pubkey", $pubkey,
|
|
|
|
"-subj", $subj, "-extfile", $extfile,
|
|
|
|
"-signkey", $signkey, "-out", $selfout]))
|
|
|
|
&& run(app(["openssl", "verify", "-no_check_time",
|
|
|
|
"-trusted", $selfout, "-partial_chain", $testcert])));
|
|
|
|
# not unlinking $pubkey
|
|
|
|
# not unlinking $selfout
|
2019-03-19 07:35:03 +08:00
|
|
|
|
2015-04-18 02:15:22 +08:00
|
|
|
subtest 'x509 -- x.509 v1 certificate' => sub {
|
2020-10-15 22:53:29 +08:00
|
|
|
tconversion( -type => 'x509', -prefix => 'x509v1',
|
2020-12-10 23:41:03 +08:00
|
|
|
-in => srctop_file("test", "testx509.pem") );
|
2015-04-18 02:15:22 +08:00
|
|
|
};
|
|
|
|
subtest 'x509 -- first x.509 v3 certificate' => sub {
|
2020-10-15 22:53:29 +08:00
|
|
|
tconversion( -type => 'x509', -prefix => 'x509v3-1',
|
2020-12-10 23:41:03 +08:00
|
|
|
-in => srctop_file("test", "v3-cert1.pem") );
|
2015-04-18 02:15:22 +08:00
|
|
|
};
|
|
|
|
subtest 'x509 -- second x.509 v3 certificate' => sub {
|
2020-10-15 22:53:29 +08:00
|
|
|
tconversion( -type => 'x509', -prefix => 'x509v3-2',
|
2020-12-10 23:41:03 +08:00
|
|
|
-in => srctop_file("test", "v3-cert2.pem") );
|
2015-04-18 02:15:22 +08:00
|
|
|
};
|
2016-06-08 23:37:06 +08:00
|
|
|
|
|
|
|
subtest 'x509 -- pathlen' => sub {
|
2020-12-10 23:41:03 +08:00
|
|
|
ok(run(test(["v3ext", srctop_file(@certs, "pathlen.pem")])));
|
2020-04-22 05:06:38 +08:00
|
|
|
};
|
|
|
|
|
2020-12-20 02:49:25 +08:00
|
|
|
cert_contains(srctop_file(@certs, "fake-gp.pem"),
|
|
|
|
"2.16.528.1.1003.1.3.5.5.2-1-0000006666-Z-12345678-01.015-12345678",
|
|
|
|
1, 'x500 -- subjectAltName');
|
2020-09-16 18:52:09 +08:00
|
|
|
|
|
|
|
sub test_errors { # actually tests diagnostics of OSSL_STORE
|
|
|
|
my ($expected, $cert, @opts) = @_;
|
2020-12-10 23:41:03 +08:00
|
|
|
my $infile = srctop_file(@certs, $cert);
|
2020-09-16 18:52:09 +08:00
|
|
|
my @args = qw(openssl x509 -in);
|
2020-09-29 01:44:49 +08:00
|
|
|
push(@args, $infile, @opts);
|
2020-09-16 18:52:09 +08:00
|
|
|
my $tmpfile = 'out.txt';
|
2020-09-29 01:44:49 +08:00
|
|
|
my $res = grep(/-text/, @opts) ? run(app([@args], stdout => $tmpfile))
|
|
|
|
: !run(app([@args], stderr => $tmpfile));
|
2020-09-16 18:52:09 +08:00
|
|
|
my $found = 0;
|
|
|
|
open(my $in, '<', $tmpfile) or die "Could not open file $tmpfile";
|
|
|
|
while(<$in>) {
|
|
|
|
print; # this may help debugging
|
|
|
|
$res &&= !m/asn1 encoding/; # output must not include ASN.1 parse errors
|
|
|
|
$found = 1 if m/$expected/; # output must include $expected
|
|
|
|
}
|
|
|
|
close $in;
|
2020-09-29 01:44:49 +08:00
|
|
|
# $tmpfile is kept to help with investigation in case of failure
|
2020-09-16 18:52:09 +08:00
|
|
|
return $res && $found;
|
|
|
|
}
|
|
|
|
|
2020-11-09 17:08:23 +08:00
|
|
|
# 3 tests for non-existence of spurious OSSL_STORE ASN.1 parse error output.
|
|
|
|
# This requires provoking a failure exit of the app after reading input files.
|
2020-12-08 02:37:46 +08:00
|
|
|
ok(test_errors("Bad output format", "root-cert.pem", '-outform', 'http'),
|
2020-11-09 17:08:23 +08:00
|
|
|
"load root-cert errors");
|
2020-09-16 18:52:09 +08:00
|
|
|
ok(test_errors("RC2-40-CBC", "v3-certs-RC2.p12", '-passin', 'pass:v3-certs'),
|
2020-11-09 17:08:23 +08:00
|
|
|
"load v3-certs-RC2 no asn1 errors"); # error msg should mention "RC2-40-CBC"
|
2020-09-29 01:44:49 +08:00
|
|
|
SKIP: {
|
|
|
|
skip "sm2 not disabled", 1 if !disabled("sm2");
|
|
|
|
|
|
|
|
ok(test_errors("unknown group|unsupported algorithm", "sm2.pem", '-text'),
|
|
|
|
"error loading unsupported sm2 cert");
|
|
|
|
}
|