X509_STORE_CTX_set_verify_cb.pod 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. =pod
  2. =head1 NAME
  3. X509_STORE_CTX_set_verify_cb - set verification callback
  4. =head1 SYNOPSIS
  5. #include <openssl/x509_vfy.h>
  6. void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
  7. int (*verify_cb)(int ok, X509_STORE_CTX *ctx));
  8. =head1 DESCRIPTION
  9. X509_STORE_CTX_set_verify_cb() sets the verification callback of B<ctx> to
  10. B<verify_cb> overwriting any existing callback.
  11. The verification callback can be used to customise the operation of certificate
  12. verification, either by overriding error conditions or logging errors for
  13. debugging purposes.
  14. However a verification callback is B<not> essential and the default operation
  15. is often sufficient.
  16. The B<ok> parameter to the callback indicates the value the callback should
  17. return to retain the default behaviour. If it is zero then and error condition
  18. is indicated. If it is 1 then no error occurred. If the flag
  19. B<X509_V_FLAG_NOTIFY_POLICY> is set then B<ok> is set to 2 to indicate the
  20. policy checking is complete.
  21. The B<ctx> parameter to the callback is the B<X509_STORE_CTX> structure that
  22. is performing the verification operation. A callback can examine this
  23. structure and receive additional information about the error, for example
  24. by calling X509_STORE_CTX_get_current_cert(). Additional application data can
  25. be passed to the callback via the B<ex_data> mechanism.
  26. =head1 WARNING
  27. In general a verification callback should B<NOT> unconditionally return 1 in
  28. all circumstances because this will allow verification to succeed no matter
  29. what the error. This effectively removes all security from the application
  30. because B<any> certificate (including untrusted generated ones) will be
  31. accepted.
  32. =head1 NOTES
  33. The verification callback can be set and inherited from the parent structure
  34. performing the operation. In some cases (such as S/MIME verification) the
  35. B<X509_STORE_CTX> structure is created and destroyed internally and the
  36. only way to set a custom verification callback is by inheriting it from the
  37. associated B<X509_STORE>.
  38. =head1 RETURN VALUES
  39. X509_STORE_CTX_set_verify_cb() does not return a value.
  40. =head1 EXAMPLES
  41. Default callback operation:
  42. int verify_callback(int ok, X509_STORE_CTX *ctx)
  43. {
  44. return ok;
  45. }
  46. Simple example, suppose a certificate in the chain is expired and we wish
  47. to continue after this error:
  48. int verify_callback(int ok, X509_STORE_CTX *ctx)
  49. {
  50. /* Tolerate certificate expiration */
  51. if (X509_STORE_CTX_get_error(ctx) == X509_V_ERR_CERT_HAS_EXPIRED)
  52. return 1;
  53. /* Otherwise don't override */
  54. return ok;
  55. }
  56. More complex example, we don't wish to continue after B<any> certificate has
  57. expired just one specific case:
  58. int verify_callback(int ok, X509_STORE_CTX *ctx)
  59. {
  60. int err = X509_STORE_CTX_get_error(ctx);
  61. X509 *err_cert = X509_STORE_CTX_get_current_cert(ctx);
  62. if (err == X509_V_ERR_CERT_HAS_EXPIRED)
  63. {
  64. if (check_is_acceptable_expired_cert(err_cert)
  65. return 1;
  66. }
  67. return ok;
  68. }
  69. Full featured logging callback. In this case the B<bio_err> is assumed to be
  70. a global logging B<BIO>, an alternative would to store a BIO in B<ctx> using
  71. B<ex_data>.
  72. int verify_callback(int ok, X509_STORE_CTX *ctx)
  73. {
  74. X509 *err_cert;
  75. int err,depth;
  76. err_cert = X509_STORE_CTX_get_current_cert(ctx);
  77. err = X509_STORE_CTX_get_error(ctx);
  78. depth = X509_STORE_CTX_get_error_depth(ctx);
  79. BIO_printf(bio_err,"depth=%d ",depth);
  80. if (err_cert)
  81. {
  82. X509_NAME_print_ex(bio_err, X509_get_subject_name(err_cert),
  83. 0, XN_FLAG_ONELINE);
  84. BIO_puts(bio_err, "\n");
  85. }
  86. else
  87. BIO_puts(bio_err, "<no cert>\n");
  88. if (!ok)
  89. BIO_printf(bio_err,"verify error:num=%d:%s\n",err,
  90. X509_verify_cert_error_string(err));
  91. switch (err)
  92. {
  93. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
  94. BIO_puts(bio_err,"issuer= ");
  95. X509_NAME_print_ex(bio_err, X509_get_issuer_name(err_cert),
  96. 0, XN_FLAG_ONELINE);
  97. BIO_puts(bio_err, "\n");
  98. break;
  99. case X509_V_ERR_CERT_NOT_YET_VALID:
  100. case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
  101. BIO_printf(bio_err,"notBefore=");
  102. ASN1_TIME_print(bio_err,X509_get_notBefore(err_cert));
  103. BIO_printf(bio_err,"\n");
  104. break;
  105. case X509_V_ERR_CERT_HAS_EXPIRED:
  106. case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
  107. BIO_printf(bio_err,"notAfter=");
  108. ASN1_TIME_print(bio_err,X509_get_notAfter(err_cert));
  109. BIO_printf(bio_err,"\n");
  110. break;
  111. case X509_V_ERR_NO_EXPLICIT_POLICY:
  112. policies_print(bio_err, ctx);
  113. break;
  114. }
  115. if (err == X509_V_OK && ok == 2)
  116. /* print out policies */
  117. BIO_printf(bio_err,"verify return:%d\n",ok);
  118. return(ok);
  119. }
  120. =head1 SEE ALSO
  121. L<X509_STORE_CTX_get_error(3)|X509_STORE_CTX_get_error(3)>
  122. L<X509_STORE_set_verify_cb_func(3)|X509_STORE_set_verify_cb_func(3)>
  123. L<X509_STORE_CTX_get_ex_new_index(3)|X509_STORE_CTX_get_ex_new_index(3)>
  124. =head1 HISTORY
  125. X509_STORE_CTX_set_verify_cb() is available in all versions of SSLeay and
  126. OpenSSL.
  127. =cut