2016-04-22 19:21:51 +08:00
|
|
|
#! /usr/bin/env perl
|
2021-03-11 00:21:37 +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-20 04:24:17 +08:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use POSIX;
|
|
|
|
use File::Spec::Functions qw/splitdir curdir catfile/;
|
|
|
|
use File::Compare;
|
2021-03-11 00:21:37 +08:00
|
|
|
use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file data_file/;
|
2016-03-19 08:01:52 +08:00
|
|
|
use OpenSSL::Test::Utils;
|
2015-04-20 04:24:17 +08:00
|
|
|
|
|
|
|
setup("test_tsa");
|
|
|
|
|
2016-03-19 08:01:52 +08:00
|
|
|
plan skip_all => "TS is not supported by this OpenSSL build"
|
|
|
|
if disabled("ts");
|
|
|
|
|
2015-04-20 04:24:17 +08:00
|
|
|
# All these are modified inside indir further down. They need to exist
|
|
|
|
# here, however, to be available in all subroutines.
|
2017-07-23 05:37:06 +08:00
|
|
|
my $openssl_conf;
|
2015-04-20 04:24:17 +08:00
|
|
|
my $testtsa;
|
|
|
|
my $CAtsa;
|
2021-03-11 00:21:37 +08:00
|
|
|
my @QUERY = ("openssl", "ts", "-query");
|
|
|
|
my @REPLY;
|
|
|
|
my @VERIFY = ("openssl", "ts", "-verify");
|
2015-04-20 04:24:17 +08:00
|
|
|
|
|
|
|
sub create_tsa_cert {
|
|
|
|
my $INDEX = shift;
|
|
|
|
my $EXT = shift;
|
|
|
|
my $r = 1;
|
2015-09-06 22:03:30 +08:00
|
|
|
$ENV{TSDNSECT} = "ts_cert_dn";
|
2015-04-20 04:24:17 +08:00
|
|
|
|
2017-07-23 05:37:06 +08:00
|
|
|
ok(run(app(["openssl", "req", "-config", $openssl_conf, "-new",
|
2015-09-06 22:03:30 +08:00
|
|
|
"-out", "tsa_req${INDEX}.pem",
|
|
|
|
"-keyout", "tsa_key${INDEX}.pem"])));
|
2015-04-20 04:24:17 +08:00
|
|
|
note "using extension $EXT";
|
2015-09-06 22:03:30 +08:00
|
|
|
ok(run(app(["openssl", "x509", "-req",
|
|
|
|
"-in", "tsa_req${INDEX}.pem",
|
|
|
|
"-out", "tsa_cert${INDEX}.pem",
|
|
|
|
"-CA", "tsaca.pem", "-CAkey", "tsacakey.pem",
|
|
|
|
"-CAcreateserial",
|
2017-07-23 05:37:06 +08:00
|
|
|
"-extfile", $openssl_conf, "-extensions", $EXT])));
|
2015-04-20 04:24:17 +08:00
|
|
|
}
|
|
|
|
|
2021-03-11 00:21:37 +08:00
|
|
|
sub create_resp {
|
|
|
|
my $config = shift;
|
|
|
|
my $chain = shift;
|
2015-04-20 04:24:17 +08:00
|
|
|
my $queryfile = shift;
|
|
|
|
my $outputfile = shift;
|
|
|
|
|
2021-03-11 00:21:37 +08:00
|
|
|
ok(run(app([@REPLY, "-section", $config, "-queryfile", $queryfile,
|
|
|
|
"-chain", $chain, # this overrides "certs" entry in config
|
|
|
|
"-out", $outputfile])));
|
2015-04-20 04:24:17 +08:00
|
|
|
}
|
|
|
|
|
2021-03-11 00:21:37 +08:00
|
|
|
sub verify_ok {
|
|
|
|
my $datafile = shift;
|
2015-04-20 04:24:17 +08:00
|
|
|
my $queryfile = shift;
|
|
|
|
my $inputfile = shift;
|
2021-03-11 00:21:37 +08:00
|
|
|
my $untrustedfile = shift;
|
2015-04-20 04:24:17 +08:00
|
|
|
|
2021-03-11 00:21:37 +08:00
|
|
|
ok(run(app([@VERIFY, "-queryfile", $queryfile, "-in", $inputfile,
|
|
|
|
"-CAfile", "tsaca.pem", "-untrusted", $untrustedfile])));
|
|
|
|
ok(run(app([@VERIFY, "-data", $datafile, "-in", $inputfile,
|
|
|
|
"-CAfile", "tsaca.pem", "-untrusted", $untrustedfile])));
|
2015-04-20 04:24:17 +08:00
|
|
|
}
|
|
|
|
|
2021-03-11 00:21:37 +08:00
|
|
|
sub verify_fail {
|
2015-04-20 04:24:17 +08:00
|
|
|
my $queryfile = shift;
|
|
|
|
my $inputfile = shift;
|
2021-03-11 00:21:37 +08:00
|
|
|
my $untrustedfile = shift; # is needed for resp2, but not for resp1
|
|
|
|
my $cafile = shift;
|
2015-04-20 04:24:17 +08:00
|
|
|
|
2021-03-11 00:21:37 +08:00
|
|
|
ok(!run(app([@VERIFY, "-queryfile", $queryfile, "-in", $inputfile,
|
|
|
|
"-untrusted", $untrustedfile, "-CAfile", $cafile])));
|
2015-04-20 04:24:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# main functions
|
|
|
|
|
2021-03-11 00:21:37 +08:00
|
|
|
plan tests => 27;
|
2015-04-20 04:24:17 +08:00
|
|
|
|
2015-09-06 22:03:30 +08:00
|
|
|
note "setting up TSA test directory";
|
|
|
|
indir "tsa" => sub
|
|
|
|
{
|
2017-07-23 05:37:06 +08:00
|
|
|
$openssl_conf = srctop_file("test", "CAtsa.cnf");
|
2016-01-30 08:05:33 +08:00
|
|
|
$testtsa = srctop_file("test", "recipes", "80-test_tsa.t");
|
|
|
|
$CAtsa = srctop_file("test", "CAtsa.cnf");
|
2021-03-11 00:21:37 +08:00
|
|
|
@REPLY = ("openssl", "ts", "-config", $openssl_conf, "-reply");
|
2017-07-23 05:37:06 +08:00
|
|
|
|
|
|
|
# ../apps/CA.pl needs these
|
|
|
|
$ENV{OPENSSL_CONFIG} = "-config $openssl_conf";
|
|
|
|
$ENV{OPENSSL} = cmdstr(app(["openssl"]), display => 1);
|
2015-04-20 04:24:17 +08:00
|
|
|
|
2015-09-06 22:03:30 +08:00
|
|
|
SKIP: {
|
|
|
|
$ENV{TSDNSECT} = "ts_ca_dn";
|
|
|
|
skip "failed", 19
|
2017-07-23 05:37:06 +08:00
|
|
|
unless ok(run(app(["openssl", "req", "-config", $openssl_conf,
|
2020-05-11 21:41:08 +08:00
|
|
|
"-new", "-x509", "-noenc",
|
2015-09-06 22:03:30 +08:00
|
|
|
"-out", "tsaca.pem", "-keyout", "tsacakey.pem"])),
|
|
|
|
'creating a new CA for the TSA tests');
|
|
|
|
|
|
|
|
skip "failed", 18
|
|
|
|
unless subtest 'creating tsa_cert1.pem TSA server cert' => sub {
|
|
|
|
create_tsa_cert("1", "tsa_cert")
|
|
|
|
};
|
|
|
|
|
|
|
|
skip "failed", 17
|
|
|
|
unless subtest 'creating tsa_cert2.pem non-TSA server cert' => sub {
|
|
|
|
create_tsa_cert("2", "non_tsa_cert")
|
|
|
|
};
|
|
|
|
|
|
|
|
skip "failed", 16
|
2021-03-11 00:21:37 +08:00
|
|
|
unless ok(run(app([@QUERY, "-data", $testtsa,
|
2016-03-15 17:08:49 +08:00
|
|
|
"-tspolicy", "tsa_policy1", "-cert",
|
2015-09-06 22:03:30 +08:00
|
|
|
"-out", "req1.tsq"])),
|
|
|
|
'creating req1.req time stamp request for file testtsa');
|
|
|
|
|
2021-03-11 00:21:37 +08:00
|
|
|
ok(run(app([@QUERY, "-in", "req1.tsq", "-text"])),
|
2015-09-06 22:03:30 +08:00
|
|
|
'printing req1.req');
|
|
|
|
|
|
|
|
subtest 'generating valid response for req1.req' => sub {
|
2021-03-11 00:21:37 +08:00
|
|
|
create_resp("tsa_config1", "tsaca.pem", "req1.tsq", "resp1.tsr")
|
|
|
|
};
|
|
|
|
|
|
|
|
subtest 'generating response with wrong 2nd certid for req1.req' => sub {
|
|
|
|
create_resp("tsa_config1", "tsa_cert1.pem", "req1.tsq",
|
|
|
|
"resp1_invalid.tsr")
|
2015-09-06 22:03:30 +08:00
|
|
|
};
|
|
|
|
|
2021-03-11 00:21:37 +08:00
|
|
|
ok(run(app([@REPLY, "-in", "resp1.tsr", "-text"])),
|
2015-09-06 22:03:30 +08:00
|
|
|
'printing response');
|
|
|
|
|
|
|
|
subtest 'verifying valid response' => sub {
|
2021-03-11 00:21:37 +08:00
|
|
|
verify_ok($testtsa, "req1.tsq", "resp1.tsr", "tsa_cert1.pem")
|
2015-09-06 22:03:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
skip "failed", 11
|
|
|
|
unless subtest 'verifying valid token' => sub {
|
2021-03-11 00:21:37 +08:00
|
|
|
ok(run(app([@REPLY, "-in", "resp1.tsr",
|
2015-09-06 22:03:30 +08:00
|
|
|
"-out", "resp1.tsr.token", "-token_out"])));
|
2021-03-11 00:21:37 +08:00
|
|
|
ok(run(app([@VERIFY, "-queryfile", "req1.tsq",
|
2015-09-06 22:03:30 +08:00
|
|
|
"-in", "resp1.tsr.token", "-token_in",
|
2021-03-11 00:21:37 +08:00
|
|
|
"-CAfile", "tsaca.pem"])));
|
|
|
|
ok(run(app([@VERIFY, "-data", $testtsa,
|
2015-09-06 22:03:30 +08:00
|
|
|
"-in", "resp1.tsr.token", "-token_in",
|
2021-03-11 00:21:37 +08:00
|
|
|
"-CAfile", "tsaca.pem"])));
|
2015-09-06 22:03:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
skip "failed", 10
|
2021-03-11 00:21:37 +08:00
|
|
|
unless ok(run(app([@QUERY, "-data", $testtsa,
|
2016-03-15 17:08:49 +08:00
|
|
|
"-tspolicy", "tsa_policy2", "-no_nonce",
|
2015-09-06 22:03:30 +08:00
|
|
|
"-out", "req2.tsq"])),
|
|
|
|
'creating req2.req time stamp request for file testtsa');
|
|
|
|
|
2021-03-11 00:21:37 +08:00
|
|
|
ok(run(app([@QUERY, "-in", "req2.tsq", "-text"])),
|
2015-09-06 22:03:30 +08:00
|
|
|
'printing req2.req');
|
|
|
|
|
|
|
|
skip "failed", 8
|
|
|
|
unless subtest 'generating valid response for req2.req' => sub {
|
2021-03-11 00:21:37 +08:00
|
|
|
create_resp("tsa_config1", "tsaca.pem", "req2.tsq", "resp2.tsr")
|
2015-09-06 22:03:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
skip "failed", 7
|
|
|
|
unless subtest 'checking -token_in and -token_out options with -reply' => sub {
|
|
|
|
my $RESPONSE2="resp2.tsr.copy.tsr";
|
|
|
|
my $TOKEN_DER="resp2.tsr.token.der";
|
|
|
|
|
2021-03-11 00:21:37 +08:00
|
|
|
ok(run(app([@REPLY, "-in", "resp2.tsr",
|
2015-09-06 22:03:30 +08:00
|
|
|
"-out", "$TOKEN_DER", "-token_out"])));
|
2021-03-11 00:21:37 +08:00
|
|
|
ok(run(app([@REPLY, "-in", "$TOKEN_DER",
|
2015-09-06 22:03:30 +08:00
|
|
|
"-token_in", "-out", "$RESPONSE2"])));
|
|
|
|
is(compare($RESPONSE2, "resp2.tsr"), 0);
|
2021-03-11 00:21:37 +08:00
|
|
|
ok(run(app([@REPLY, "-in", "resp2.tsr",
|
2015-09-06 22:03:30 +08:00
|
|
|
"-text", "-token_out"])));
|
2021-03-11 00:21:37 +08:00
|
|
|
ok(run(app([@REPLY, "-in", "$TOKEN_DER",
|
2015-09-06 22:03:30 +08:00
|
|
|
"-token_in", "-text", "-token_out"])));
|
2021-03-11 00:21:37 +08:00
|
|
|
ok(run(app([@REPLY, "-queryfile", "req2.tsq",
|
2015-09-06 22:03:30 +08:00
|
|
|
"-text", "-token_out"])));
|
|
|
|
};
|
|
|
|
|
2021-03-11 00:21:37 +08:00
|
|
|
ok(run(app([@REPLY, "-in", "resp2.tsr", "-text"])),
|
2015-09-06 22:03:30 +08:00
|
|
|
'printing response');
|
|
|
|
|
2021-03-11 00:21:37 +08:00
|
|
|
subtest 'verifying valid resp1, wrong untrusted is not used' => sub {
|
|
|
|
verify_ok($testtsa, "req1.tsq", "resp1.tsr", "tsa_cert2.pem")
|
|
|
|
};
|
|
|
|
|
|
|
|
subtest 'verifying invalid resp1 with wrong 2nd certid' => sub {
|
|
|
|
verify_fail($testtsa, "req1.tsq", "resp1_invalid.tsr", "tsa_cert2.pem")
|
2015-09-06 22:03:30 +08:00
|
|
|
};
|
|
|
|
|
2021-03-11 00:21:37 +08:00
|
|
|
subtest 'verifying valid resp2, correct untrusted being used' => sub {
|
|
|
|
verify_ok($testtsa, "req2.tsq", "resp2.tsr", "tsa_cert1.pem")
|
2015-09-06 22:03:30 +08:00
|
|
|
};
|
|
|
|
|
2021-03-11 00:21:37 +08:00
|
|
|
subtest 'verifying resp2 against wrong req1 should fail' => sub {
|
|
|
|
verify_fail("req1.tsq", "resp2.tsr", "tsa_cert1.pem", "tsaca.pem")
|
|
|
|
};
|
|
|
|
|
|
|
|
subtest 'verifying resp1 against wrong req2 should fail' => sub {
|
|
|
|
verify_fail("req2.tsq", "resp1.tsr", "tsa_cert1.pem", "tsaca.pem")
|
|
|
|
};
|
|
|
|
|
|
|
|
subtest 'verifying resp1 using wrong untrusted should fail' => sub {
|
|
|
|
verify_fail("req2.tsq", "resp2.tsr", "tsa_cert2.pem", "tsaca.pem")
|
|
|
|
};
|
|
|
|
|
|
|
|
subtest 'verifying resp1 using wrong root should fail' => sub {
|
|
|
|
verify_fail("req1.tsq", "resp1.tsr", "tsa_cert1.pem", "tsa_cert1.pem")
|
2015-09-06 22:03:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
skip "failure", 2
|
2021-03-11 00:21:37 +08:00
|
|
|
unless ok(run(app([@QUERY, "-data", $CAtsa,
|
2015-09-06 22:03:30 +08:00
|
|
|
"-no_nonce", "-out", "req3.tsq"])),
|
|
|
|
"creating req3.req time stamp request for file CAtsa.cnf");
|
|
|
|
|
2021-03-11 00:21:37 +08:00
|
|
|
ok(run(app([@QUERY, "-in", "req3.tsq", "-text"])),
|
2015-09-06 22:03:30 +08:00
|
|
|
'printing req3.req');
|
|
|
|
|
2021-03-11 00:21:37 +08:00
|
|
|
subtest 'verifying resp1 against wrong req3 should fail' => sub {
|
|
|
|
verify_fail("req3.tsq", "resp1.tsr", "tsa_cert1.pem", "tsaca.pem")
|
2015-09-06 22:03:30 +08:00
|
|
|
};
|
2015-04-20 04:24:17 +08:00
|
|
|
}
|
2021-03-11 00:21:37 +08:00
|
|
|
|
|
|
|
# verifying response with two ESSCertIDs, referring to leaf cert
|
|
|
|
# "sectigo-signer.pem" and intermediate cert "sectigo-time-stamping-ca.pem"
|
|
|
|
# 1. validation chain contains these certs and root "user-trust-ca.pem"
|
|
|
|
ok(run(app([@VERIFY, "-no_check_time",
|
|
|
|
"-queryfile", data_file("all-zero.tsq"),
|
|
|
|
"-in", data_file("sectigo-all-zero.tsr"),
|
|
|
|
"-CAfile", data_file("user-trust-ca.pem")])),
|
|
|
|
"validation with two ESSCertIDs and 3-element chain");
|
|
|
|
# 2. validation chain contains these certs, a cross-cert, and different root
|
|
|
|
ok(run(app([@VERIFY, "-no_check_time",
|
|
|
|
"-queryfile", data_file("all-zero.tsq"),
|
|
|
|
"-in", data_file("sectigo-all-zero.tsr"),
|
|
|
|
"-untrusted", data_file("user-trust-ca-aaa.pem"),
|
|
|
|
"-CAfile", data_file("comodo-aaa.pem")])),
|
|
|
|
"validation with two ESSCertIDs and 4-element chain");
|
|
|
|
|
2015-09-06 22:03:30 +08:00
|
|
|
}, create => 1, cleanup => 1
|