1e1051a39Sopenharmony_ci#! /usr/bin/env perl 2e1051a39Sopenharmony_ci# Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci# 4e1051a39Sopenharmony_ci# Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci# this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci# in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci# https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ciuse strict; 11e1051a39Sopenharmony_ciuse warnings; 12e1051a39Sopenharmony_ci 13e1051a39Sopenharmony_ciuse POSIX; 14e1051a39Sopenharmony_ciuse File::Spec::Functions qw/splitdir curdir catfile/; 15e1051a39Sopenharmony_ciuse File::Compare; 16e1051a39Sopenharmony_ciuse OpenSSL::Test qw/:DEFAULT cmdstr srctop_file data_file/; 17e1051a39Sopenharmony_ciuse OpenSSL::Test::Utils; 18e1051a39Sopenharmony_ci 19e1051a39Sopenharmony_cisetup("test_tsa"); 20e1051a39Sopenharmony_ci 21e1051a39Sopenharmony_ciplan skip_all => "TS is not supported by this OpenSSL build" 22e1051a39Sopenharmony_ci if disabled("ts"); 23e1051a39Sopenharmony_ci 24e1051a39Sopenharmony_ci# All these are modified inside indir further down. They need to exist 25e1051a39Sopenharmony_ci# here, however, to be available in all subroutines. 26e1051a39Sopenharmony_cimy $openssl_conf; 27e1051a39Sopenharmony_cimy $testtsa; 28e1051a39Sopenharmony_cimy $tsacakey; 29e1051a39Sopenharmony_cimy $CAtsa; 30e1051a39Sopenharmony_cimy @QUERY = ("openssl", "ts", "-query"); 31e1051a39Sopenharmony_cimy @REPLY; 32e1051a39Sopenharmony_cimy @VERIFY = ("openssl", "ts", "-verify"); 33e1051a39Sopenharmony_ci 34e1051a39Sopenharmony_cisub create_tsa_cert { 35e1051a39Sopenharmony_ci my $INDEX = shift; 36e1051a39Sopenharmony_ci my $EXT = shift; 37e1051a39Sopenharmony_ci my $r = 1; 38e1051a39Sopenharmony_ci $ENV{TSDNSECT} = "ts_cert_dn"; 39e1051a39Sopenharmony_ci 40e1051a39Sopenharmony_ci ok(run(app(["openssl", "req", "-config", $openssl_conf, "-new", 41e1051a39Sopenharmony_ci "-out", "tsa_req${INDEX}.pem", 42e1051a39Sopenharmony_ci "-key", srctop_file("test", "certs", "alt${INDEX}-key.pem"), 43e1051a39Sopenharmony_ci "-keyout", "tsa_key${INDEX}.pem"]))); 44e1051a39Sopenharmony_ci note "using extension $EXT"; 45e1051a39Sopenharmony_ci ok(run(app(["openssl", "x509", "-req", 46e1051a39Sopenharmony_ci "-in", "tsa_req${INDEX}.pem", 47e1051a39Sopenharmony_ci "-out", "tsa_cert${INDEX}.pem", 48e1051a39Sopenharmony_ci "-CA", "tsaca.pem", "-CAkey", $tsacakey, 49e1051a39Sopenharmony_ci "-CAcreateserial", 50e1051a39Sopenharmony_ci "-extfile", $openssl_conf, "-extensions", $EXT]))); 51e1051a39Sopenharmony_ci} 52e1051a39Sopenharmony_ci 53e1051a39Sopenharmony_cisub create_resp { 54e1051a39Sopenharmony_ci my $config = shift; 55e1051a39Sopenharmony_ci my $chain = shift; 56e1051a39Sopenharmony_ci my $queryfile = shift; 57e1051a39Sopenharmony_ci my $outputfile = shift; 58e1051a39Sopenharmony_ci 59e1051a39Sopenharmony_ci ok(run(app([@REPLY, "-section", $config, "-queryfile", $queryfile, 60e1051a39Sopenharmony_ci "-chain", $chain, # this overrides "certs" entry in config 61e1051a39Sopenharmony_ci "-out", $outputfile]))); 62e1051a39Sopenharmony_ci} 63e1051a39Sopenharmony_ci 64e1051a39Sopenharmony_cisub verify_ok { 65e1051a39Sopenharmony_ci my $datafile = shift; 66e1051a39Sopenharmony_ci my $queryfile = shift; 67e1051a39Sopenharmony_ci my $inputfile = shift; 68e1051a39Sopenharmony_ci my $untrustedfile = shift; 69e1051a39Sopenharmony_ci 70e1051a39Sopenharmony_ci ok(run(app([@VERIFY, "-queryfile", $queryfile, "-in", $inputfile, 71e1051a39Sopenharmony_ci "-CAfile", "tsaca.pem", "-untrusted", $untrustedfile]))); 72e1051a39Sopenharmony_ci ok(run(app([@VERIFY, "-data", $datafile, "-in", $inputfile, 73e1051a39Sopenharmony_ci "-CAfile", "tsaca.pem", "-untrusted", $untrustedfile]))); 74e1051a39Sopenharmony_ci} 75e1051a39Sopenharmony_ci 76e1051a39Sopenharmony_cisub verify_fail { 77e1051a39Sopenharmony_ci my $queryfile = shift; 78e1051a39Sopenharmony_ci my $inputfile = shift; 79e1051a39Sopenharmony_ci my $untrustedfile = shift; # is needed for resp2, but not for resp1 80e1051a39Sopenharmony_ci my $cafile = shift; 81e1051a39Sopenharmony_ci 82e1051a39Sopenharmony_ci ok(!run(app([@VERIFY, "-queryfile", $queryfile, "-in", $inputfile, 83e1051a39Sopenharmony_ci "-untrusted", $untrustedfile, "-CAfile", $cafile]))); 84e1051a39Sopenharmony_ci} 85e1051a39Sopenharmony_ci 86e1051a39Sopenharmony_ci# main functions 87e1051a39Sopenharmony_ci 88e1051a39Sopenharmony_ciplan tests => 27; 89e1051a39Sopenharmony_ci 90e1051a39Sopenharmony_cinote "setting up TSA test directory"; 91e1051a39Sopenharmony_ciindir "tsa" => sub 92e1051a39Sopenharmony_ci{ 93e1051a39Sopenharmony_ci $openssl_conf = srctop_file("test", "CAtsa.cnf"); 94e1051a39Sopenharmony_ci $testtsa = srctop_file("test", "recipes", "80-test_tsa.t"); 95e1051a39Sopenharmony_ci $tsacakey = srctop_file("test", "certs", "ca-key.pem"); 96e1051a39Sopenharmony_ci $CAtsa = srctop_file("test", "CAtsa.cnf"); 97e1051a39Sopenharmony_ci @REPLY = ("openssl", "ts", "-config", $openssl_conf, "-reply"); 98e1051a39Sopenharmony_ci 99e1051a39Sopenharmony_ci # ../apps/CA.pl needs these 100e1051a39Sopenharmony_ci $ENV{OPENSSL_CONFIG} = "-config $openssl_conf"; 101e1051a39Sopenharmony_ci $ENV{OPENSSL} = cmdstr(app(["openssl"]), display => 1); 102e1051a39Sopenharmony_ci 103e1051a39Sopenharmony_ci SKIP: { 104e1051a39Sopenharmony_ci $ENV{TSDNSECT} = "ts_ca_dn"; 105e1051a39Sopenharmony_ci skip "failed", 19 106e1051a39Sopenharmony_ci unless ok(run(app(["openssl", "req", "-config", $openssl_conf, 107e1051a39Sopenharmony_ci "-new", "-x509", "-noenc", 108e1051a39Sopenharmony_ci "-out", "tsaca.pem", "-key", $tsacakey])), 109e1051a39Sopenharmony_ci 'creating a new CA for the TSA tests'); 110e1051a39Sopenharmony_ci 111e1051a39Sopenharmony_ci skip "failed", 18 112e1051a39Sopenharmony_ci unless subtest 'creating tsa_cert1.pem TSA server cert' => sub { 113e1051a39Sopenharmony_ci create_tsa_cert("1", "tsa_cert") 114e1051a39Sopenharmony_ci }; 115e1051a39Sopenharmony_ci 116e1051a39Sopenharmony_ci skip "failed", 17 117e1051a39Sopenharmony_ci unless subtest 'creating tsa_cert2.pem non-TSA server cert' => sub { 118e1051a39Sopenharmony_ci create_tsa_cert("2", "non_tsa_cert") 119e1051a39Sopenharmony_ci }; 120e1051a39Sopenharmony_ci 121e1051a39Sopenharmony_ci skip "failed", 16 122e1051a39Sopenharmony_ci unless ok(run(app([@QUERY, "-data", $testtsa, 123e1051a39Sopenharmony_ci "-tspolicy", "tsa_policy1", "-cert", 124e1051a39Sopenharmony_ci "-out", "req1.tsq"])), 125e1051a39Sopenharmony_ci 'creating req1.req time stamp request for file testtsa'); 126e1051a39Sopenharmony_ci 127e1051a39Sopenharmony_ci ok(run(app([@QUERY, "-in", "req1.tsq", "-text"])), 128e1051a39Sopenharmony_ci 'printing req1.req'); 129e1051a39Sopenharmony_ci 130e1051a39Sopenharmony_ci subtest 'generating valid response for req1.req' => sub { 131e1051a39Sopenharmony_ci create_resp("tsa_config1", "tsaca.pem", "req1.tsq", "resp1.tsr") 132e1051a39Sopenharmony_ci }; 133e1051a39Sopenharmony_ci 134e1051a39Sopenharmony_ci subtest 'generating response with wrong 2nd certid for req1.req' => sub { 135e1051a39Sopenharmony_ci create_resp("tsa_config1", "tsa_cert1.pem", "req1.tsq", 136e1051a39Sopenharmony_ci "resp1_invalid.tsr") 137e1051a39Sopenharmony_ci }; 138e1051a39Sopenharmony_ci 139e1051a39Sopenharmony_ci ok(run(app([@REPLY, "-in", "resp1.tsr", "-text"])), 140e1051a39Sopenharmony_ci 'printing response'); 141e1051a39Sopenharmony_ci 142e1051a39Sopenharmony_ci subtest 'verifying valid response' => sub { 143e1051a39Sopenharmony_ci verify_ok($testtsa, "req1.tsq", "resp1.tsr", "tsa_cert1.pem") 144e1051a39Sopenharmony_ci }; 145e1051a39Sopenharmony_ci 146e1051a39Sopenharmony_ci skip "failed", 11 147e1051a39Sopenharmony_ci unless subtest 'verifying valid token' => sub { 148e1051a39Sopenharmony_ci ok(run(app([@REPLY, "-in", "resp1.tsr", 149e1051a39Sopenharmony_ci "-out", "resp1.tsr.token", "-token_out"]))); 150e1051a39Sopenharmony_ci ok(run(app([@VERIFY, "-queryfile", "req1.tsq", 151e1051a39Sopenharmony_ci "-in", "resp1.tsr.token", "-token_in", 152e1051a39Sopenharmony_ci "-CAfile", "tsaca.pem"]))); 153e1051a39Sopenharmony_ci ok(run(app([@VERIFY, "-data", $testtsa, 154e1051a39Sopenharmony_ci "-in", "resp1.tsr.token", "-token_in", 155e1051a39Sopenharmony_ci "-CAfile", "tsaca.pem"]))); 156e1051a39Sopenharmony_ci }; 157e1051a39Sopenharmony_ci 158e1051a39Sopenharmony_ci skip "failed", 10 159e1051a39Sopenharmony_ci unless ok(run(app([@QUERY, "-data", $testtsa, 160e1051a39Sopenharmony_ci "-tspolicy", "tsa_policy2", "-no_nonce", 161e1051a39Sopenharmony_ci "-out", "req2.tsq"])), 162e1051a39Sopenharmony_ci 'creating req2.req time stamp request for file testtsa'); 163e1051a39Sopenharmony_ci 164e1051a39Sopenharmony_ci ok(run(app([@QUERY, "-in", "req2.tsq", "-text"])), 165e1051a39Sopenharmony_ci 'printing req2.req'); 166e1051a39Sopenharmony_ci 167e1051a39Sopenharmony_ci skip "failed", 8 168e1051a39Sopenharmony_ci unless subtest 'generating valid response for req2.req' => sub { 169e1051a39Sopenharmony_ci create_resp("tsa_config1", "tsaca.pem", "req2.tsq", "resp2.tsr") 170e1051a39Sopenharmony_ci }; 171e1051a39Sopenharmony_ci 172e1051a39Sopenharmony_ci skip "failed", 7 173e1051a39Sopenharmony_ci unless subtest 'checking -token_in and -token_out options with -reply' => sub { 174e1051a39Sopenharmony_ci my $RESPONSE2="resp2.tsr.copy.tsr"; 175e1051a39Sopenharmony_ci my $TOKEN_DER="resp2.tsr.token.der"; 176e1051a39Sopenharmony_ci 177e1051a39Sopenharmony_ci ok(run(app([@REPLY, "-in", "resp2.tsr", 178e1051a39Sopenharmony_ci "-out", "$TOKEN_DER", "-token_out"]))); 179e1051a39Sopenharmony_ci ok(run(app([@REPLY, "-in", "$TOKEN_DER", 180e1051a39Sopenharmony_ci "-token_in", "-out", "$RESPONSE2"]))); 181e1051a39Sopenharmony_ci is(compare($RESPONSE2, "resp2.tsr"), 0); 182e1051a39Sopenharmony_ci ok(run(app([@REPLY, "-in", "resp2.tsr", 183e1051a39Sopenharmony_ci "-text", "-token_out"]))); 184e1051a39Sopenharmony_ci ok(run(app([@REPLY, "-in", "$TOKEN_DER", 185e1051a39Sopenharmony_ci "-token_in", "-text", "-token_out"]))); 186e1051a39Sopenharmony_ci ok(run(app([@REPLY, "-queryfile", "req2.tsq", 187e1051a39Sopenharmony_ci "-text", "-token_out"]))); 188e1051a39Sopenharmony_ci }; 189e1051a39Sopenharmony_ci 190e1051a39Sopenharmony_ci ok(run(app([@REPLY, "-in", "resp2.tsr", "-text"])), 191e1051a39Sopenharmony_ci 'printing response'); 192e1051a39Sopenharmony_ci 193e1051a39Sopenharmony_ci subtest 'verifying valid resp1, wrong untrusted is not used' => sub { 194e1051a39Sopenharmony_ci verify_ok($testtsa, "req1.tsq", "resp1.tsr", "tsa_cert2.pem") 195e1051a39Sopenharmony_ci }; 196e1051a39Sopenharmony_ci 197e1051a39Sopenharmony_ci subtest 'verifying invalid resp1 with wrong 2nd certid' => sub { 198e1051a39Sopenharmony_ci verify_fail($testtsa, "req1.tsq", "resp1_invalid.tsr", "tsa_cert2.pem") 199e1051a39Sopenharmony_ci }; 200e1051a39Sopenharmony_ci 201e1051a39Sopenharmony_ci subtest 'verifying valid resp2, correct untrusted being used' => sub { 202e1051a39Sopenharmony_ci verify_ok($testtsa, "req2.tsq", "resp2.tsr", "tsa_cert1.pem") 203e1051a39Sopenharmony_ci }; 204e1051a39Sopenharmony_ci 205e1051a39Sopenharmony_ci subtest 'verifying resp2 against wrong req1 should fail' => sub { 206e1051a39Sopenharmony_ci verify_fail("req1.tsq", "resp2.tsr", "tsa_cert1.pem", "tsaca.pem") 207e1051a39Sopenharmony_ci }; 208e1051a39Sopenharmony_ci 209e1051a39Sopenharmony_ci subtest 'verifying resp1 against wrong req2 should fail' => sub { 210e1051a39Sopenharmony_ci verify_fail("req2.tsq", "resp1.tsr", "tsa_cert1.pem", "tsaca.pem") 211e1051a39Sopenharmony_ci }; 212e1051a39Sopenharmony_ci 213e1051a39Sopenharmony_ci subtest 'verifying resp1 using wrong untrusted should fail' => sub { 214e1051a39Sopenharmony_ci verify_fail("req2.tsq", "resp2.tsr", "tsa_cert2.pem", "tsaca.pem") 215e1051a39Sopenharmony_ci }; 216e1051a39Sopenharmony_ci 217e1051a39Sopenharmony_ci subtest 'verifying resp1 using wrong root should fail' => sub { 218e1051a39Sopenharmony_ci verify_fail("req1.tsq", "resp1.tsr", "tsa_cert1.pem", "tsa_cert1.pem") 219e1051a39Sopenharmony_ci }; 220e1051a39Sopenharmony_ci 221e1051a39Sopenharmony_ci skip "failure", 2 222e1051a39Sopenharmony_ci unless ok(run(app([@QUERY, "-data", $CAtsa, 223e1051a39Sopenharmony_ci "-no_nonce", "-out", "req3.tsq"])), 224e1051a39Sopenharmony_ci "creating req3.req time stamp request for file CAtsa.cnf"); 225e1051a39Sopenharmony_ci 226e1051a39Sopenharmony_ci ok(run(app([@QUERY, "-in", "req3.tsq", "-text"])), 227e1051a39Sopenharmony_ci 'printing req3.req'); 228e1051a39Sopenharmony_ci 229e1051a39Sopenharmony_ci subtest 'verifying resp1 against wrong req3 should fail' => sub { 230e1051a39Sopenharmony_ci verify_fail("req3.tsq", "resp1.tsr", "tsa_cert1.pem", "tsaca.pem") 231e1051a39Sopenharmony_ci }; 232e1051a39Sopenharmony_ci } 233e1051a39Sopenharmony_ci 234e1051a39Sopenharmony_ci # verifying response with two ESSCertIDs, referring to leaf cert 235e1051a39Sopenharmony_ci # "sectigo-signer.pem" and intermediate cert "sectigo-time-stamping-ca.pem" 236e1051a39Sopenharmony_ci # 1. validation chain contains these certs and root "user-trust-ca.pem" 237e1051a39Sopenharmony_ci ok(run(app([@VERIFY, "-no_check_time", 238e1051a39Sopenharmony_ci "-queryfile", data_file("all-zero.tsq"), 239e1051a39Sopenharmony_ci "-in", data_file("sectigo-all-zero.tsr"), 240e1051a39Sopenharmony_ci "-CAfile", data_file("user-trust-ca.pem")])), 241e1051a39Sopenharmony_ci "validation with two ESSCertIDs and 3-element chain"); 242e1051a39Sopenharmony_ci # 2. validation chain contains these certs, a cross-cert, and different root 243e1051a39Sopenharmony_ci ok(run(app([@VERIFY, "-no_check_time", 244e1051a39Sopenharmony_ci "-queryfile", data_file("all-zero.tsq"), 245e1051a39Sopenharmony_ci "-in", data_file("sectigo-all-zero.tsr"), 246e1051a39Sopenharmony_ci "-untrusted", data_file("user-trust-ca-aaa.pem"), 247e1051a39Sopenharmony_ci "-CAfile", data_file("comodo-aaa.pem")])), 248e1051a39Sopenharmony_ci "validation with two ESSCertIDs and 4-element chain"); 249e1051a39Sopenharmony_ci 250e1051a39Sopenharmony_ci}, create => 1, cleanup => 1 251