80-test_tsa.t 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (the "License"). You may not use
  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. use strict;
  9. use warnings;
  10. use POSIX;
  11. use File::Spec::Functions qw/splitdir curdir catfile/;
  12. use File::Compare;
  13. use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file/;
  14. use OpenSSL::Test::Utils;
  15. setup("test_tsa");
  16. plan skip_all => "TS is not supported by this OpenSSL build"
  17. if disabled("ts");
  18. # All these are modified inside indir further down. They need to exist
  19. # here, however, to be available in all subroutines.
  20. my $testtsa;
  21. my $CAtsa;
  22. my @RUN = ("openssl", "ts");
  23. sub create_tsa_cert {
  24. my $INDEX = shift;
  25. my $EXT = shift;
  26. my $r = 1;
  27. $ENV{TSDNSECT} = "ts_cert_dn";
  28. ok(run(app(["openssl", "req", "-new",
  29. "-out", "tsa_req${INDEX}.pem",
  30. "-keyout", "tsa_key${INDEX}.pem"])));
  31. note "using extension $EXT";
  32. ok(run(app(["openssl", "x509", "-req",
  33. "-in", "tsa_req${INDEX}.pem",
  34. "-out", "tsa_cert${INDEX}.pem",
  35. "-CA", "tsaca.pem", "-CAkey", "tsacakey.pem",
  36. "-CAcreateserial",
  37. "-extfile", $ENV{OPENSSL_CONF}, "-extensions", $EXT])));
  38. }
  39. sub create_time_stamp_response {
  40. my $queryfile = shift;
  41. my $outputfile = shift;
  42. my $datafile = shift;
  43. ok(run(app([@RUN, "-reply", "-section", "$datafile",
  44. "-queryfile", "$queryfile", "-out", "$outputfile"])));
  45. }
  46. sub verify_time_stamp_response {
  47. my $queryfile = shift;
  48. my $inputfile = shift;
  49. my $datafile = shift;
  50. ok(run(app([@RUN, "-verify", "-queryfile", "$queryfile",
  51. "-in", "$inputfile", "-CAfile", "tsaca.pem",
  52. "-untrusted", "tsa_cert1.pem"])));
  53. ok(run(app([@RUN, "-verify", "-data", "$datafile",
  54. "-in", "$inputfile", "-CAfile", "tsaca.pem",
  55. "-untrusted", "tsa_cert1.pem"])));
  56. }
  57. sub verify_time_stamp_response_fail {
  58. my $queryfile = shift;
  59. my $inputfile = shift;
  60. ok(!run(app([@RUN, "-verify", "-queryfile", "$queryfile",
  61. "-in", "$inputfile", "-CAfile", "tsaca.pem",
  62. "-untrusted", "tsa_cert1.pem"])));
  63. }
  64. # main functions
  65. plan tests => 20;
  66. note "setting up TSA test directory";
  67. indir "tsa" => sub
  68. {
  69. $ENV{OPENSSL_CONF} = srctop_file("test", "CAtsa.cnf");
  70. # Because that's what ../apps/CA.pl really looks at
  71. $ENV{OPENSSL_CONFIG} = "-config ".$ENV{OPENSSL_CONF};
  72. $ENV{OPENSSL} = cmdstr(app(["openssl"]), display => 1);
  73. $testtsa = srctop_file("test", "recipes", "80-test_tsa.t");
  74. $CAtsa = srctop_file("test", "CAtsa.cnf");
  75. SKIP: {
  76. $ENV{TSDNSECT} = "ts_ca_dn";
  77. skip "failed", 19
  78. unless ok(run(app(["openssl", "req", "-new", "-x509", "-nodes",
  79. "-out", "tsaca.pem", "-keyout", "tsacakey.pem"])),
  80. 'creating a new CA for the TSA tests');
  81. skip "failed", 18
  82. unless subtest 'creating tsa_cert1.pem TSA server cert' => sub {
  83. create_tsa_cert("1", "tsa_cert")
  84. };
  85. skip "failed", 17
  86. unless subtest 'creating tsa_cert2.pem non-TSA server cert' => sub {
  87. create_tsa_cert("2", "non_tsa_cert")
  88. };
  89. skip "failed", 16
  90. unless ok(run(app([@RUN, "-query", "-data", $testtsa,
  91. "-tspolicy", "tsa_policy1", "-cert",
  92. "-out", "req1.tsq"])),
  93. 'creating req1.req time stamp request for file testtsa');
  94. ok(run(app([@RUN, "-query", "-in", "req1.tsq", "-text"])),
  95. 'printing req1.req');
  96. subtest 'generating valid response for req1.req' => sub {
  97. create_time_stamp_response("req1.tsq", "resp1.tsr", "tsa_config1")
  98. };
  99. ok(run(app([@RUN, "-reply", "-in", "resp1.tsr", "-text"])),
  100. 'printing response');
  101. subtest 'verifying valid response' => sub {
  102. verify_time_stamp_response("req1.tsq", "resp1.tsr", $testtsa)
  103. };
  104. skip "failed", 11
  105. unless subtest 'verifying valid token' => sub {
  106. ok(run(app([@RUN, "-reply", "-in", "resp1.tsr",
  107. "-out", "resp1.tsr.token", "-token_out"])));
  108. ok(run(app([@RUN, "-verify", "-queryfile", "req1.tsq",
  109. "-in", "resp1.tsr.token", "-token_in",
  110. "-CAfile", "tsaca.pem",
  111. "-untrusted", "tsa_cert1.pem"])));
  112. ok(run(app([@RUN, "-verify", "-data", $testtsa,
  113. "-in", "resp1.tsr.token", "-token_in",
  114. "-CAfile", "tsaca.pem",
  115. "-untrusted", "tsa_cert1.pem"])));
  116. };
  117. skip "failed", 10
  118. unless ok(run(app([@RUN, "-query", "-data", $testtsa,
  119. "-tspolicy", "tsa_policy2", "-no_nonce",
  120. "-out", "req2.tsq"])),
  121. 'creating req2.req time stamp request for file testtsa');
  122. ok(run(app([@RUN, "-query", "-in", "req2.tsq", "-text"])),
  123. 'printing req2.req');
  124. skip "failed", 8
  125. unless subtest 'generating valid response for req2.req' => sub {
  126. create_time_stamp_response("req2.tsq", "resp2.tsr", "tsa_config1")
  127. };
  128. skip "failed", 7
  129. unless subtest 'checking -token_in and -token_out options with -reply' => sub {
  130. my $RESPONSE2="resp2.tsr.copy.tsr";
  131. my $TOKEN_DER="resp2.tsr.token.der";
  132. ok(run(app([@RUN, "-reply", "-in", "resp2.tsr",
  133. "-out", "$TOKEN_DER", "-token_out"])));
  134. ok(run(app([@RUN, "-reply", "-in", "$TOKEN_DER",
  135. "-token_in", "-out", "$RESPONSE2"])));
  136. is(compare($RESPONSE2, "resp2.tsr"), 0);
  137. ok(run(app([@RUN, "-reply", "-in", "resp2.tsr",
  138. "-text", "-token_out"])));
  139. ok(run(app([@RUN, "-reply", "-in", "$TOKEN_DER",
  140. "-token_in", "-text", "-token_out"])));
  141. ok(run(app([@RUN, "-reply", "-queryfile", "req2.tsq",
  142. "-text", "-token_out"])));
  143. };
  144. ok(run(app([@RUN, "-reply", "-in", "resp2.tsr", "-text"])),
  145. 'printing response');
  146. subtest 'verifying valid response' => sub {
  147. verify_time_stamp_response("req2.tsq", "resp2.tsr", $testtsa)
  148. };
  149. subtest 'verifying response against wrong request, it should fail' => sub {
  150. verify_time_stamp_response_fail("req1.tsq", "resp2.tsr")
  151. };
  152. subtest 'verifying response against wrong request, it should fail' => sub {
  153. verify_time_stamp_response_fail("req2.tsq", "resp1.tsr")
  154. };
  155. skip "failure", 2
  156. unless ok(run(app([@RUN, "-query", "-data", $CAtsa,
  157. "-no_nonce", "-out", "req3.tsq"])),
  158. "creating req3.req time stamp request for file CAtsa.cnf");
  159. ok(run(app([@RUN, "-query", "-in", "req3.tsq", "-text"])),
  160. 'printing req3.req');
  161. subtest 'verifying response against wrong request, it should fail' => sub {
  162. verify_time_stamp_response_fail("req3.tsq", "resp1.tsr")
  163. };
  164. }
  165. }, create => 1, cleanup => 1