25-test_x509.t 3.1 KB
Newer Older
R
Rich Salz 已提交
1
#! /usr/bin/env perl
M
Matt Caswell 已提交
2
# Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
R
Rich Salz 已提交
3
#
4
# Licensed under the Apache License 2.0 (the "License").  You may not use
R
Rich Salz 已提交
5 6 7 8
# 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

9 10 11 12 13

use strict;
use warnings;

use File::Spec;
M
Matt Caswell 已提交
14
use OpenSSL::Test::Utils;
15
use OpenSSL::Test qw/:DEFAULT srctop_file/;
16 17 18

setup("test_x509");

19
plan tests => 11;
20

21
require_ok(srctop_file('test','recipes','tconversion.pl'));
22

R
Rich Salz 已提交
23
my $pem = srctop_file("test/certs", "cyrillic.pem");
24 25
my $out_msb = "out-cyrillic.msb";
my $out_utf8 = "out-cyrillic.utf8";
R
Rich Salz 已提交
26 27 28
my $msb = srctop_file("test/certs", "cyrillic.msb");
my $utf = srctop_file("test/certs", "cyrillic.utf8");

29
ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_msb,
R
Rich Salz 已提交
30
            "-nameopt", "esc_msb"])));
31
is(cmp_text($out_msb, srctop_file("test/certs", "cyrillic.msb")),
R
Rich Salz 已提交
32
   0, 'Comparing esc_msb output');
33
ok(run(app(["openssl", "x509", "-text", "-in", $pem, "-out", $out_utf8,
R
Rich Salz 已提交
34
            "-nameopt", "utf8"])));
35
is(cmp_text($out_utf8, srctop_file("test/certs", "cyrillic.utf8")),
R
Rich Salz 已提交
36 37
   0, 'Comparing utf8 output');

M
Matt Caswell 已提交
38 39 40 41 42 43
SKIP: {
    skip "EC disabled", 1 if disabled("ec");

    # producing and checking self-issued (but not self-signed) cert
    my @path = qw(test certs);
    my $subj = "/CN=CA"; # using same DN as in issuer of ee-cert.pem
44
    my $extfile = srctop_file("test", "v3_ca_exts.cnf");
M
Matt Caswell 已提交
45 46 47 48 49 50 51 52 53
    my $pkey = srctop_file(@path, "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(@path, "ee-ecdsa-key.pem");
    my $selfout = "self-issued.out";
    my $testcert = srctop_file(@path, "ee-cert.pem");
    ok(run(app(["openssl", "pkey", "-in", $pkey, "-pubout", "-out", $pubkey]))
       &&
       run(app(["openssl", "x509", "-new", "-force_pubkey", $pubkey,
54 55
                "-subj", $subj, "-extfile", $extfile,
                "-signkey", $signkey, "-out", $selfout]))
M
Matt Caswell 已提交
56 57
       &&
       run(app(["openssl", "verify", "-no_check_time",
58 59 60
                "-trusted", $selfout, "-partial_chain", $testcert])));
    unlink $pubkey;
    unlink $selfout;
M
Matt Caswell 已提交
61
}
62

63
subtest 'x509 -- x.509 v1 certificate' => sub {
64
    tconversion("x509", srctop_file("test","testx509.pem"));
65 66
};
subtest 'x509 -- first x.509 v3 certificate' => sub {
67
    tconversion("x509", srctop_file("test","v3-cert1.pem"));
68 69
};
subtest 'x509 -- second x.509 v3 certificate' => sub {
70
    tconversion("x509", srctop_file("test","v3-cert2.pem"));
71
};
R
Rich Salz 已提交
72 73 74

subtest 'x509 -- pathlen' => sub {
    ok(run(test(["v3ext", srctop_file("test/certs", "pathlen.pem")])));
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
};

subtest 'x500 -- subjectAltName' => sub {
     my $fp = srctop_file("test/certs", "fake-gp.pem");
     my $out = "ext.out";
     ok(run(app(["openssl", "x509", "-text", "-in", $fp, "-out", $out])));
     ok(has_doctor_id($out));
     unlink $out;
};

sub has_doctor_id { 
    $_ = shift @_;
    open(DATA,$_) or return 0;
    $_= join('',<DATA>); 
    close(DATA);
    return m/2.16.528.1.1003.1.3.5.5.2-1-0000006666-Z-12345678-01.015-12345678/;
R
Rich Salz 已提交
91
}