80-test_ocsp.t 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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::Spec::Functions qw/devnull catfile/;
  12. use File::Basename;
  13. use File::Copy;
  14. use OpenSSL::Test qw/:DEFAULT with pipe srctop_dir data_file/;
  15. use OpenSSL::Test::Utils;
  16. setup("test_ocsp");
  17. plan skip_all => "OCSP is not supported by this OpenSSL build"
  18. if disabled("ocsp");
  19. my $ocspdir=srctop_dir("test", "ocsp-tests");
  20. # 17 December 2012 so we don't get certificate expiry errors.
  21. my @check_time=("-attime", "1355875200");
  22. sub test_ocsp {
  23. my $title = shift;
  24. my $inputfile = shift;
  25. my $CAfile = shift;
  26. my $untrusted = shift;
  27. if ($untrusted eq "") {
  28. $untrusted = $CAfile;
  29. }
  30. my $expected_exit = shift;
  31. my $outputfile = basename($inputfile, '.ors') . '.dat';
  32. run(app(["openssl", "base64", "-d",
  33. "-in", catfile($ocspdir,$inputfile),
  34. "-out", $outputfile]));
  35. with({ exit_checker => sub { return shift == $expected_exit; } },
  36. sub { ok(run(app(["openssl", "ocsp", "-respin", $outputfile,
  37. "-partial_chain", @check_time,
  38. "-CAfile", catfile($ocspdir, $CAfile),
  39. "-verify_other", catfile($ocspdir, $untrusted),
  40. "-no-CApath", "-no-CAstore"])),
  41. $title); });
  42. }
  43. plan tests => 11;
  44. subtest "=== VALID OCSP RESPONSES ===" => sub {
  45. plan tests => 7;
  46. test_ocsp("NON-DELEGATED; Intermediate CA -> EE",
  47. "ND1.ors", "ND1_Issuer_ICA.pem", "", 0);
  48. test_ocsp("NON-DELEGATED; Root CA -> Intermediate CA",
  49. "ND2.ors", "ND2_Issuer_Root.pem", "", 0);
  50. test_ocsp("NON-DELEGATED; Root CA -> EE",
  51. "ND3.ors", "ND3_Issuer_Root.pem", "", 0);
  52. test_ocsp("NON-DELEGATED; 3-level CA hierarchy",
  53. "ND1.ors", "ND1_Cross_Root.pem", "ND1_Issuer_ICA-Cross.pem", 0);
  54. test_ocsp("DELEGATED; Intermediate CA -> EE",
  55. "D1.ors", "D1_Issuer_ICA.pem", "", 0);
  56. test_ocsp("DELEGATED; Root CA -> Intermediate CA",
  57. "D2.ors", "D2_Issuer_Root.pem", "", 0);
  58. test_ocsp("DELEGATED; Root CA -> EE",
  59. "D3.ors", "D3_Issuer_Root.pem", "", 0);
  60. };
  61. subtest "=== INVALID SIGNATURE on the OCSP RESPONSE ===" => sub {
  62. plan tests => 6;
  63. test_ocsp("NON-DELEGATED; Intermediate CA -> EE",
  64. "ISOP_ND1.ors", "ND1_Issuer_ICA.pem", "", 1);
  65. test_ocsp("NON-DELEGATED; Root CA -> Intermediate CA",
  66. "ISOP_ND2.ors", "ND2_Issuer_Root.pem", "", 1);
  67. test_ocsp("NON-DELEGATED; Root CA -> EE",
  68. "ISOP_ND3.ors", "ND3_Issuer_Root.pem", "", 1);
  69. test_ocsp("DELEGATED; Intermediate CA -> EE",
  70. "ISOP_D1.ors", "D1_Issuer_ICA.pem", "", 1);
  71. test_ocsp("DELEGATED; Root CA -> Intermediate CA",
  72. "ISOP_D2.ors", "D2_Issuer_Root.pem", "", 1);
  73. test_ocsp("DELEGATED; Root CA -> EE",
  74. "ISOP_D3.ors", "D3_Issuer_Root.pem", "", 1);
  75. };
  76. subtest "=== WRONG RESPONDERID in the OCSP RESPONSE ===" => sub {
  77. plan tests => 6;
  78. test_ocsp("NON-DELEGATED; Intermediate CA -> EE",
  79. "WRID_ND1.ors", "ND1_Issuer_ICA.pem", "", 1);
  80. test_ocsp("NON-DELEGATED; Root CA -> Intermediate CA",
  81. "WRID_ND2.ors", "ND2_Issuer_Root.pem", "", 1);
  82. test_ocsp("NON-DELEGATED; Root CA -> EE",
  83. "WRID_ND3.ors", "ND3_Issuer_Root.pem", "", 1);
  84. test_ocsp("DELEGATED; Intermediate CA -> EE",
  85. "WRID_D1.ors", "D1_Issuer_ICA.pem", "", 1);
  86. test_ocsp("DELEGATED; Root CA -> Intermediate CA",
  87. "WRID_D2.ors", "D2_Issuer_Root.pem", "", 1);
  88. test_ocsp("DELEGATED; Root CA -> EE",
  89. "WRID_D3.ors", "D3_Issuer_Root.pem", "", 1);
  90. };
  91. subtest "=== WRONG ISSUERNAMEHASH in the OCSP RESPONSE ===" => sub {
  92. plan tests => 6;
  93. test_ocsp("NON-DELEGATED; Intermediate CA -> EE",
  94. "WINH_ND1.ors", "ND1_Issuer_ICA.pem", "", 1);
  95. test_ocsp("NON-DELEGATED; Root CA -> Intermediate CA",
  96. "WINH_ND2.ors", "ND2_Issuer_Root.pem", "", 1);
  97. test_ocsp("NON-DELEGATED; Root CA -> EE",
  98. "WINH_ND3.ors", "ND3_Issuer_Root.pem", "", 1);
  99. test_ocsp("DELEGATED; Intermediate CA -> EE",
  100. "WINH_D1.ors", "D1_Issuer_ICA.pem", "", 1);
  101. test_ocsp("DELEGATED; Root CA -> Intermediate CA",
  102. "WINH_D2.ors", "D2_Issuer_Root.pem", "", 1);
  103. test_ocsp("DELEGATED; Root CA -> EE",
  104. "WINH_D3.ors", "D3_Issuer_Root.pem", "", 1);
  105. };
  106. subtest "=== WRONG ISSUERKEYHASH in the OCSP RESPONSE ===" => sub {
  107. plan tests => 6;
  108. test_ocsp("NON-DELEGATED; Intermediate CA -> EE",
  109. "WIKH_ND1.ors", "ND1_Issuer_ICA.pem", "", 1);
  110. test_ocsp("NON-DELEGATED; Root CA -> Intermediate CA",
  111. "WIKH_ND2.ors", "ND2_Issuer_Root.pem", "", 1);
  112. test_ocsp("NON-DELEGATED; Root CA -> EE",
  113. "WIKH_ND3.ors", "ND3_Issuer_Root.pem", "", 1);
  114. test_ocsp("DELEGATED; Intermediate CA -> EE",
  115. "WIKH_D1.ors", "D1_Issuer_ICA.pem", "", 1);
  116. test_ocsp("DELEGATED; Root CA -> Intermediate CA",
  117. "WIKH_D2.ors", "D2_Issuer_Root.pem", "", 1);
  118. test_ocsp("DELEGATED; Root CA -> EE",
  119. "WIKH_D3.ors", "D3_Issuer_Root.pem", "", 1);
  120. };
  121. subtest "=== WRONG KEY in the DELEGATED OCSP SIGNING CERTIFICATE ===" => sub {
  122. plan tests => 3;
  123. test_ocsp("DELEGATED; Intermediate CA -> EE",
  124. "WKDOSC_D1.ors", "D1_Issuer_ICA.pem", "", 1);
  125. test_ocsp("DELEGATED; Root CA -> Intermediate CA",
  126. "WKDOSC_D2.ors", "D2_Issuer_Root.pem", "", 1);
  127. test_ocsp("DELEGATED; Root CA -> EE",
  128. "WKDOSC_D3.ors", "D3_Issuer_Root.pem", "", 1);
  129. };
  130. subtest "=== INVALID SIGNATURE on the DELEGATED OCSP SIGNING CERTIFICATE ===" => sub {
  131. plan tests => 3;
  132. test_ocsp("DELEGATED; Intermediate CA -> EE",
  133. "ISDOSC_D1.ors", "D1_Issuer_ICA.pem", "", 1);
  134. test_ocsp("DELEGATED; Root CA -> Intermediate CA",
  135. "ISDOSC_D2.ors", "D2_Issuer_Root.pem", "", 1);
  136. test_ocsp("DELEGATED; Root CA -> EE",
  137. "ISDOSC_D3.ors", "D3_Issuer_Root.pem", "", 1);
  138. };
  139. subtest "=== WRONG SUBJECT NAME in the ISSUER CERTIFICATE ===" => sub {
  140. plan tests => 6;
  141. test_ocsp("NON-DELEGATED; Intermediate CA -> EE",
  142. "ND1.ors", "WSNIC_ND1_Issuer_ICA.pem", "", 1);
  143. test_ocsp("NON-DELEGATED; Root CA -> Intermediate CA",
  144. "ND2.ors", "WSNIC_ND2_Issuer_Root.pem", "", 1);
  145. test_ocsp("NON-DELEGATED; Root CA -> EE",
  146. "ND3.ors", "WSNIC_ND3_Issuer_Root.pem", "", 1);
  147. test_ocsp("DELEGATED; Intermediate CA -> EE",
  148. "D1.ors", "WSNIC_D1_Issuer_ICA.pem", "", 1);
  149. test_ocsp("DELEGATED; Root CA -> Intermediate CA",
  150. "D2.ors", "WSNIC_D2_Issuer_Root.pem", "", 1);
  151. test_ocsp("DELEGATED; Root CA -> EE",
  152. "D3.ors", "WSNIC_D3_Issuer_Root.pem", "", 1);
  153. };
  154. subtest "=== WRONG KEY in the ISSUER CERTIFICATE ===" => sub {
  155. plan tests => 6;
  156. test_ocsp("NON-DELEGATED; Intermediate CA -> EE",
  157. "ND1.ors", "WKIC_ND1_Issuer_ICA.pem", "", 1);
  158. test_ocsp("NON-DELEGATED; Root CA -> Intermediate CA",
  159. "ND2.ors", "WKIC_ND2_Issuer_Root.pem", "", 1);
  160. test_ocsp("NON-DELEGATED; Root CA -> EE",
  161. "ND3.ors", "WKIC_ND3_Issuer_Root.pem", "", 1);
  162. test_ocsp("DELEGATED; Intermediate CA -> EE",
  163. "D1.ors", "WKIC_D1_Issuer_ICA.pem", "", 1);
  164. test_ocsp("DELEGATED; Root CA -> Intermediate CA",
  165. "D2.ors", "WKIC_D2_Issuer_Root.pem", "", 1);
  166. test_ocsp("DELEGATED; Root CA -> EE",
  167. "D3.ors", "WKIC_D3_Issuer_Root.pem", "", 1);
  168. };
  169. subtest "=== INVALID SIGNATURE on the ISSUER CERTIFICATE ===" => sub {
  170. plan tests => 6;
  171. # Expect success, because we're explicitly trusting the issuer certificate.
  172. test_ocsp("NON-DELEGATED; Intermediate CA -> EE",
  173. "ND1.ors", "ISIC_ND1_Issuer_ICA.pem", "", 0);
  174. test_ocsp("NON-DELEGATED; Root CA -> Intermediate CA",
  175. "ND2.ors", "ISIC_ND2_Issuer_Root.pem", "", 0);
  176. test_ocsp("NON-DELEGATED; Root CA -> EE",
  177. "ND3.ors", "ISIC_ND3_Issuer_Root.pem", "", 0);
  178. test_ocsp("DELEGATED; Intermediate CA -> EE",
  179. "D1.ors", "ISIC_D1_Issuer_ICA.pem", "", 0);
  180. test_ocsp("DELEGATED; Root CA -> Intermediate CA",
  181. "D2.ors", "ISIC_D2_Issuer_Root.pem", "", 0);
  182. test_ocsp("DELEGATED; Root CA -> EE",
  183. "D3.ors", "ISIC_D3_Issuer_Root.pem", "", 0);
  184. };
  185. subtest "=== OCSP API TESTS===" => sub {
  186. plan tests => 1;
  187. ok(run(test(["ocspapitest", data_file("cert.pem"), data_file("key.pem")])),
  188. "running ocspapitest");
  189. }