OCSP_check.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/bin/sh
  2. # Sample script to perform OCSP queries with OpenSSL
  3. # given a certificate serial number.
  4. # If you run your own CA, you can set up a very simple
  5. # OCSP server using the -port option to "openssl ocsp".
  6. # Full documentation and examples:
  7. # http://www.openssl.org/docs/apps/ocsp.html
  8. # Edit the following values to suit your needs
  9. # OCSP responder URL (mandatory)
  10. # YOU MUST UNCOMMENT ONE OF THESE AND SET IT TO A VALID SERVER
  11. #ocsp_url="http://ocsp.example.com/"
  12. #ocsp_url="https://ocsp.secure.example.com/"
  13. # Path to issuer certificate (mandatory)
  14. # YOU MUST SET THIS TO THE PATH TO THE CA CERTIFICATE
  15. issuer="/path/to/CAcert.crt"
  16. # use a nonce in the query, set to "-no_nonce" to not use it
  17. nonce="-nonce"
  18. # Verify the response
  19. # YOU MUST SET THIS TO THE PATH TO THE RESPONSE VERIFICATION CERT
  20. verify="/path/to/CAcert.crt"
  21. # Depth in the certificate chain where the cert to verify is.
  22. # Set to -1 to run the verification at every level (NOTE that
  23. # in that case you need a more complex script as the various
  24. # parameters for the query will likely be different at each level)
  25. # "0" is the usual value here, where the client certificate is
  26. check_depth=0
  27. cur_depth=$1 # this is the *CURRENT* depth
  28. common_name=$2 # CN in case you need it
  29. # minimal sanity checks
  30. err=0
  31. if [ -z "$issuer" ] || [ ! -e "$issuer" ]; then
  32. echo "Error: issuer certificate undefined or not found!" >&2
  33. err=1
  34. fi
  35. if [ -z "$verify" ] || [ ! -e "$verify" ]; then
  36. echo "Error: verification certificate undefined or not found!" >&2
  37. err=1
  38. fi
  39. if [ -z "$ocsp_url" ]; then
  40. echo "Error: OCSP server URL not defined!" >&2
  41. err=1
  42. fi
  43. if [ $err -eq 1 ]; then
  44. echo "Did you forget to customize the variables in the script?" >&2
  45. exit 1
  46. fi
  47. # begin
  48. if [ $check_depth -eq -1 ] || [ $cur_depth -eq $check_depth ]; then
  49. eval serial="\$tls_serial_${cur_depth}"
  50. # To successfully complete, the following must happen:
  51. #
  52. # - The serial number must not be empty
  53. # - The exit status of "openssl ocsp" must be zero
  54. # - The output of the above command must contain the line
  55. # "${serial}: good"
  56. #
  57. # Everything else fails with exit status 1.
  58. if [ -n "$serial" ]; then
  59. # This is only an example; you are encouraged to run this command (without
  60. # redirections) manually against your or your CA's OCSP server to see how
  61. # it responds, and adapt accordingly.
  62. # Sample output that is assumed here:
  63. #
  64. # Response verify OK
  65. # 4287405: good
  66. # This Update: Apr 24 19:38:49 2010 GMT
  67. # Next Update: May 2 14:23:42 2010 GMT
  68. #
  69. # NOTE: It is needed to check the exit code of OpenSSL explicitly. OpenSSL
  70. # can in some circumstances give a "good" result if it could not
  71. # reach the the OSCP server. In this case, the exit code will indicate
  72. # if OpenSSL itself failed or not. If OpenSSL's exit code is not 0,
  73. # don't trust the OpenSSL status.
  74. status=$(openssl ocsp -issuer "$issuer" \
  75. "$nonce" \
  76. -CAfile "$verify" \
  77. -url "$ocsp_url" \
  78. -serial "${serial}" 2>&1)
  79. if [ $? -eq 0 ]; then
  80. # check if ocsp didn't report any errors
  81. if echo "$status" | grep -Eq "(error|fail)"; then
  82. exit 1
  83. fi
  84. # check that the reported status of certificate is ok
  85. if echo "$status" | grep -Eq "^${serial}: good"; then
  86. # check if signature on the OCSP response verified correctly
  87. if echo "$status" | grep -Eq "^Response verify OK"; then
  88. exit 0
  89. fi
  90. fi
  91. fi
  92. fi
  93. # if we get here, something was wrong
  94. exit 1
  95. fi