82-test_ocsp_cert_chain.t 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #! /usr/bin/env perl
  2. # Copyright 2023-2024 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 IPC::Open3;
  11. use OpenSSL::Test qw/:DEFAULT srctop_file bldtop_file/;
  12. use OpenSSL::Test::Utils;
  13. use Symbol 'gensym';
  14. my $test_name = "test_ocsp_cert_chain";
  15. setup($test_name);
  16. plan skip_all => "$test_name requires OCSP support"
  17. if disabled("ocsp");
  18. plan skip_all => "$test_name requires EC cryptography"
  19. if disabled("ec");
  20. plan skip_all => "$test_name requires sock enabled"
  21. if disabled("sock");
  22. plan skip_all => "$test_name requires TLS enabled"
  23. if alldisabled(available_protocols("tls"));
  24. plan skip_all => "$test_name is not available Windows or VMS"
  25. if $^O =~ /^(VMS|MSWin32|msys)$/;
  26. plan tests => 3;
  27. my $shlib_wrap = bldtop_file("util", "shlib_wrap.sh");
  28. my $apps_openssl = bldtop_file("apps", "openssl");
  29. my $index_txt = srctop_file("test", "ocsp-tests", "index.txt");
  30. my $ocsp_pem = srctop_file("test", "ocsp-tests", "ocsp.pem");
  31. my $intermediate_cert_pem = srctop_file("test", "ocsp-tests", "intermediate-cert.pem");
  32. my $server_pem = srctop_file("test", "ocsp-tests", "server.pem");
  33. sub run_test {
  34. # this test starts two servers that listen on respective ports.
  35. # that can be problematic since the ports may not be available
  36. # (e.g. when multiple instances of the test are run on the same
  37. # machine).
  38. # to avoid this, we specify port 0 when staring each server, which
  39. # causes the OS to provide a random unused port.
  40. # using a random port with s_server is straightforward. doing so
  41. # with the ocsp responder required some investigation because the
  42. # url for the ocsp responder is usually included in the server's
  43. # cert (normally, in the authority-information-access extension,
  44. # and it would be complicated to change that when the test
  45. # executes). however, s_server has an option "-status_url" that
  46. # can be used to specify a fallback url when no url is specified
  47. # in the cert. that is what we do here.
  48. # openssl ocsp -port 0 -index index.txt -rsigner ocsp.pem -CA intermediate-cert.pem
  49. my @ocsp_cmd = ("ocsp", "-port", "0", "-index", $index_txt, "-rsigner", $ocsp_pem, "-CA", $intermediate_cert_pem);
  50. my $ocsp_pid = open3(my $ocsp_i, my $ocsp_o, my $ocsp_e = gensym, $shlib_wrap, $apps_openssl, @ocsp_cmd);
  51. ## ipv4
  52. # ACCEPT 0.0.0.0:19254 PID=620007
  53. ## ipv6
  54. # ACCEPT [::]:19254 PID=620007
  55. my $port = "0";
  56. while (<$ocsp_o>) {
  57. print($_);
  58. chomp;
  59. if (/^ACCEPT 0.0.0.0:(\d+)/) {
  60. $port = $1;
  61. last;
  62. } elsif (/^ACCEPT \[::\]:(\d+)/) {
  63. $port = $1;
  64. last;
  65. } else {
  66. last;
  67. }
  68. }
  69. ok($port ne "0", "ocsp server port check");
  70. my $ocsp_port = $port;
  71. print("ocsp server ready, listening on port $ocsp_port\n");
  72. # openssl s_server -accept 0 -cert server.pem -cert_chain intermediate-cert.pem \
  73. # -status_verbose -status_url http://localhost:19254/ocsp
  74. my @s_server_cmd = ("s_server", "-accept", "0", "-cert", $server_pem, "-cert_chain", $intermediate_cert_pem,
  75. "-status_verbose", "-status_url", "http://localhost:${ocsp_port}/ocsp");
  76. my $s_server_pid = open3(my $s_server_i, my $s_server_o, my $s_server_e = gensym, $shlib_wrap, $apps_openssl, @s_server_cmd);
  77. # ACCEPT 0.0.0.0:45921
  78. # ACCEPT [::]:45921
  79. $port = "0";
  80. while (<$s_server_o>) {
  81. print($_);
  82. chomp;
  83. if (/^ACCEPT 0.0.0.0:(\d+)/) {
  84. $port = $1;
  85. last;
  86. } elsif (/^ACCEPT \[::\]:(\d+)/) {
  87. $port = $1;
  88. last;
  89. } elsif (/^Using default/) {
  90. ;
  91. } else {
  92. last;
  93. }
  94. }
  95. ok($port ne "0", "s_server port check");
  96. my $server_port = $port;
  97. print("s_server ready, listening on port $server_port\n");
  98. # openssl s_client -connect localhost:45921 -status -verify_return_error
  99. my @s_client_cmd = ("s_client", "-connect", "localhost:$server_port", "-status", "-verify_return_error");
  100. my $s_client_pid = open3(my $s_client_i, my $s_client_o, my $s_client_e = gensym, $shlib_wrap, $apps_openssl, @s_client_cmd);
  101. ### the output from s_server that we want to check is written to its stderr
  102. ### cert_status: ocsp response sent:
  103. my $resp = 0;
  104. while (<$s_server_e>) {
  105. print($_);
  106. chomp;
  107. if (/^cert_status: ocsp response sent:/) {
  108. $resp = 1;
  109. last;
  110. }
  111. }
  112. ok($resp == 1, "check s_server sent ocsp response");
  113. waitpid($s_client_pid, 0);
  114. kill 'HUP', $s_server_pid, $ocsp_pid;
  115. }
  116. run_test();