80-test_ca.t 9.0 KB

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