80-test_ca.t 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (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::Path 2.00 qw/rmtree/;
  12. use OpenSSL::Test qw/:DEFAULT cmdstr data_file srctop_file/;
  13. use OpenSSL::Test::Utils;
  14. use Time::Local qw/timegm/;
  15. setup("test_ca");
  16. $ENV{OPENSSL} = cmdstr(app(["openssl"]), display => 1);
  17. my $cnf = '"' . srctop_file("test","ca-and-certs.cnf") . '"';;
  18. my $std_openssl_cnf = '"'
  19. . srctop_file("apps", $^O eq "VMS" ? "openssl-vms.cnf" : "openssl.cnf")
  20. . '"';
  21. rmtree("demoCA", { safe => 0 });
  22. plan tests => 15;
  23. SKIP: {
  24. $ENV{OPENSSL_CONFIG} = '-config ' . $cnf;
  25. skip "failed creating CA structure", 4
  26. if !ok(run(perlapp(["CA.pl","-newca"], stdin => undef)),
  27. 'creating CA structure');
  28. $ENV{OPENSSL_CONFIG} = '-config ' . $cnf;
  29. skip "failed creating new certificate request", 3
  30. if !ok(run(perlapp(["CA.pl","-newreq",
  31. '-extra-req', '-outform DER -section userreq'])),
  32. 'creating certificate request');
  33. $ENV{OPENSSL_CONFIG} = '-rand_serial -inform DER -config '.$std_openssl_cnf;
  34. skip "failed to sign certificate request", 2
  35. if !is(yes(cmdstr(perlapp(["CA.pl", "-sign"]))), 0,
  36. 'signing certificate request');
  37. ok(run(perlapp(["CA.pl", "-verify", "newcert.pem"])),
  38. 'verifying new certificate');
  39. skip "CT not configured, can't use -precert", 1
  40. if disabled("ct");
  41. $ENV{OPENSSL_CONFIG} = '-config ' . $cnf;
  42. ok(run(perlapp(["CA.pl", "-precert", '-extra-req', '-section userreq'], stderr => undef)),
  43. 'creating new pre-certificate');
  44. }
  45. SKIP: {
  46. skip "SM2 is not supported by this OpenSSL build", 1
  47. if disabled("sm2");
  48. is(yes(cmdstr(app(["openssl", "ca", "-config",
  49. $cnf,
  50. "-in", srctop_file("test", "certs", "sm2-csr.pem"),
  51. "-out", "sm2-test.crt",
  52. "-sigopt", "distid:1234567812345678",
  53. "-vfyopt", "distid:1234567812345678",
  54. "-md", "sm3",
  55. "-cert", srctop_file("test", "certs", "sm2-root.crt"),
  56. "-keyfile", srctop_file("test", "certs", "sm2-root.key")]))),
  57. 0,
  58. "Signing SM2 certificate request");
  59. }
  60. test_revoke('notimes', {
  61. should_succeed => 1,
  62. });
  63. test_revoke('lastupdate_invalid', {
  64. lastupdate => '1234567890',
  65. should_succeed => 0,
  66. });
  67. test_revoke('lastupdate_utctime', {
  68. lastupdate => '200901123456Z',
  69. should_succeed => 1,
  70. });
  71. test_revoke('lastupdate_generalizedtime', {
  72. lastupdate => '20990901123456Z',
  73. should_succeed => 1,
  74. });
  75. test_revoke('nextupdate_invalid', {
  76. nextupdate => '1234567890',
  77. should_succeed => 0,
  78. });
  79. test_revoke('nextupdate_utctime', {
  80. nextupdate => '200901123456Z',
  81. should_succeed => 1,
  82. });
  83. test_revoke('nextupdate_generalizedtime', {
  84. nextupdate => '20990901123456Z',
  85. should_succeed => 1,
  86. });
  87. test_revoke('both_utctime', {
  88. lastupdate => '200901123456Z',
  89. nextupdate => '200908123456Z',
  90. should_succeed => 1,
  91. });
  92. test_revoke('both_generalizedtime', {
  93. lastupdate => '20990901123456Z',
  94. nextupdate => '20990908123456Z',
  95. should_succeed => 1,
  96. });
  97. sub test_revoke {
  98. my ($filename, $opts) = @_;
  99. subtest "Revoke certificate and generate CRL: $filename" => sub {
  100. # Before Perl 5.12.0, the range of times Perl could represent was
  101. # limited by the size of time_t, so Time::Local was hamstrung by the
  102. # Y2038 problem
  103. # Perl 5.12.0 onwards use an internal time implementation with a
  104. # guaranteed >32-bit time range on all architectures, so the tests
  105. # involving post-2038 times won't fail provided we're running under
  106. # that version or newer
  107. plan skip_all =>
  108. 'Perl >= 5.12.0 required to run certificate revocation tests'
  109. if $] < 5.012000;
  110. $ENV{CN2} = $filename;
  111. ok(
  112. run(app(['openssl',
  113. 'req',
  114. '-config', $cnf,
  115. '-new',
  116. '-key', data_file('revoked.key'),
  117. '-out', "$filename-req.pem",
  118. '-section', 'userreq',
  119. ])),
  120. 'Generate CSR'
  121. );
  122. delete $ENV{CN2};
  123. ok(
  124. run(app(['openssl',
  125. 'ca',
  126. '-batch',
  127. '-config', $cnf,
  128. '-in', "$filename-req.pem",
  129. '-out', "$filename-cert.pem",
  130. ])),
  131. 'Sign CSR'
  132. );
  133. ok(
  134. run(app(['openssl',
  135. 'ca',
  136. '-config', $cnf,
  137. '-revoke', "$filename-cert.pem",
  138. ])),
  139. 'Revoke certificate'
  140. );
  141. my @gencrl_opts;
  142. if (exists $opts->{lastupdate}) {
  143. push @gencrl_opts, '-crl_lastupdate', $opts->{lastupdate};
  144. }
  145. if (exists $opts->{nextupdate}) {
  146. push @gencrl_opts, '-crl_nextupdate', $opts->{nextupdate};
  147. }
  148. is(
  149. run(app(['openssl',
  150. 'ca',
  151. '-config', $cnf,
  152. '-gencrl',
  153. '-out', "$filename-crl.pem",
  154. '-crlsec', '60',
  155. @gencrl_opts,
  156. ])),
  157. $opts->{should_succeed},
  158. 'Generate CRL'
  159. );
  160. my $crl_gentime = time;
  161. # The following tests only need to run if the CRL was supposed to be
  162. # generated:
  163. return unless $opts->{should_succeed};
  164. my $crl_lastupdate = crl_field("$filename-crl.pem", 'lastUpdate');
  165. if (exists $opts->{lastupdate}) {
  166. is(
  167. $crl_lastupdate,
  168. rfc5280_time($opts->{lastupdate}),
  169. 'CRL lastUpdate field has expected value'
  170. );
  171. } else {
  172. diag("CRL lastUpdate: $crl_lastupdate");
  173. diag("openssl run time: $crl_gentime");
  174. ok(
  175. # Is the CRL's lastUpdate time within a second of the time that
  176. # `openssl ca -gencrl` was executed?
  177. $crl_gentime - 1 <= $crl_lastupdate && $crl_lastupdate <= $crl_gentime + 1,
  178. 'CRL lastUpdate field has (roughly) expected value'
  179. );
  180. }
  181. my $crl_nextupdate = crl_field("$filename-crl.pem", 'nextUpdate');
  182. if (exists $opts->{nextupdate}) {
  183. is(
  184. $crl_nextupdate,
  185. rfc5280_time($opts->{nextupdate}),
  186. 'CRL nextUpdate field has expected value'
  187. );
  188. } else {
  189. diag("CRL nextUpdate: $crl_nextupdate");
  190. diag("openssl run time: $crl_gentime");
  191. ok(
  192. # Is the CRL's lastUpdate time within a second of the time that
  193. # `openssl ca -gencrl` was executed, taking into account the use
  194. # of '-crlsec 60'?
  195. $crl_gentime + 59 <= $crl_nextupdate && $crl_nextupdate <= $crl_gentime + 61,
  196. 'CRL nextUpdate field has (roughly) expected value'
  197. );
  198. }
  199. };
  200. }
  201. sub yes {
  202. my $cntr = 10;
  203. open(PIPE, "|-", join(" ",@_));
  204. local $SIG{PIPE} = "IGNORE";
  205. 1 while $cntr-- > 0 && print PIPE "y\n";
  206. close PIPE;
  207. return 0;
  208. }
  209. # Get the value of the lastUpdate or nextUpdate field from a CRL
  210. sub crl_field {
  211. my ($crl_path, $field_name) = @_;
  212. my @out = run(
  213. app(['openssl',
  214. 'crl',
  215. '-in', $crl_path,
  216. '-noout',
  217. '-' . lc($field_name),
  218. ]),
  219. capture => 1,
  220. statusvar => \my $exit,
  221. );
  222. ok($exit, "CRL $field_name field retrieved");
  223. diag("CRL $field_name: $out[0]");
  224. $out[0] =~ s/^\Q$field_name\E=//;
  225. $out[0] =~ s/\n?//;
  226. my $time = human_time($out[0]);
  227. return $time;
  228. }
  229. # Converts human-readable ASN1_TIME_print() output to Unix time
  230. sub human_time {
  231. my ($human) = @_;
  232. my ($mo, $d, $h, $m, $s, $y) = $human =~ /^([A-Za-z]{3})\s+(\d+) (\d{2}):(\d{2}):(\d{2}) (\d{4})/;
  233. my %months = (
  234. Jan => 0, Feb => 1, Mar => 2, Apr => 3, May => 4, Jun => 5,
  235. Jul => 6, Aug => 7, Sep => 8, Oct => 9, Nov => 10, Dec => 11,
  236. );
  237. return timegm($s, $m, $h, $d, $months{$mo}, $y);
  238. }
  239. # Converts an RFC 5280 timestamp to Unix time
  240. sub rfc5280_time {
  241. my ($asn1) = @_;
  242. my ($y, $mo, $d, $h, $m, $s) = $asn1 =~ /^(\d{2,4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z$/;
  243. return timegm($s, $m, $h, $d, $mo - 1, $y);
  244. }