X509_STORE_CTX_set_verify_cb.pod 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. =pod
  2. =head1 NAME
  3. X509_STORE_CTX_get_cleanup,
  4. X509_STORE_CTX_get_lookup_crls,
  5. X509_STORE_CTX_get_lookup_certs,
  6. X509_STORE_CTX_get_check_policy,
  7. X509_STORE_CTX_get_cert_crl,
  8. X509_STORE_CTX_get_check_crl,
  9. X509_STORE_CTX_get_get_crl,
  10. X509_STORE_CTX_get_check_revocation,
  11. X509_STORE_CTX_get_check_issued,
  12. X509_STORE_CTX_get_get_issuer,
  13. X509_STORE_CTX_get_verify_cb,
  14. X509_STORE_CTX_set_verify_cb,
  15. X509_STORE_CTX_verify_cb
  16. - get and set verification callback
  17. =head1 SYNOPSIS
  18. #include <openssl/x509_vfy.h>
  19. typedef int (*X509_STORE_CTX_verify_cb)(int, X509_STORE_CTX *);
  20. X509_STORE_CTX_verify_cb X509_STORE_CTX_get_verify_cb(X509_STORE_CTX *ctx);
  21. void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
  22. X509_STORE_CTX_verify_cb verify_cb);
  23. X509_STORE_CTX_get_issuer_fn X509_STORE_CTX_get_get_issuer(X509_STORE_CTX *ctx);
  24. X509_STORE_CTX_check_issued_fn X509_STORE_CTX_get_check_issued(X509_STORE_CTX *ctx);
  25. X509_STORE_CTX_check_revocation_fn X509_STORE_CTX_get_check_revocation(X509_STORE_CTX *ctx);
  26. X509_STORE_CTX_get_crl_fn X509_STORE_CTX_get_get_crl(X509_STORE_CTX *ctx);
  27. X509_STORE_CTX_check_crl_fn X509_STORE_CTX_get_check_crl(X509_STORE_CTX *ctx);
  28. X509_STORE_CTX_cert_crl_fn X509_STORE_CTX_get_cert_crl(X509_STORE_CTX *ctx);
  29. X509_STORE_CTX_check_policy_fn X509_STORE_CTX_get_check_policy(X509_STORE_CTX *ctx);
  30. X509_STORE_CTX_lookup_certs_fn X509_STORE_CTX_get_lookup_certs(X509_STORE_CTX *ctx);
  31. X509_STORE_CTX_lookup_crls_fn X509_STORE_CTX_get_lookup_crls(X509_STORE_CTX *ctx);
  32. X509_STORE_CTX_cleanup_fn X509_STORE_CTX_get_cleanup(X509_STORE_CTX *ctx);
  33. =head1 DESCRIPTION
  34. X509_STORE_CTX_set_verify_cb() sets the verification callback of B<ctx> to
  35. B<verify_cb> overwriting any existing callback.
  36. The verification callback can be used to customise the operation of certificate
  37. verification, either by overriding error conditions or logging errors for
  38. debugging purposes.
  39. However a verification callback is B<not> essential and the default operation
  40. is often sufficient.
  41. The B<ok> parameter to the callback indicates the value the callback should
  42. return to retain the default behaviour. If it is zero then an error condition
  43. is indicated. If it is 1 then no error occurred. If the flag
  44. B<X509_V_FLAG_NOTIFY_POLICY> is set then B<ok> is set to 2 to indicate the
  45. policy checking is complete.
  46. The B<ctx> parameter to the callback is the B<X509_STORE_CTX> structure that
  47. is performing the verification operation. A callback can examine this
  48. structure and receive additional information about the error, for example
  49. by calling X509_STORE_CTX_get_current_cert(). Additional application data can
  50. be passed to the callback via the B<ex_data> mechanism.
  51. X509_STORE_CTX_get_verify_cb() returns the value of the current callback
  52. for the specific B<ctx>.
  53. X509_STORE_CTX_get_get_issuer(),
  54. X509_STORE_CTX_get_check_issued(), X509_STORE_CTX_get_check_revocation(),
  55. X509_STORE_CTX_get_get_crl(), X509_STORE_CTX_get_check_crl(),
  56. X509_STORE_CTX_get_cert_crl(), X509_STORE_CTX_get_check_policy(),
  57. X509_STORE_CTX_get_lookup_certs(), X509_STORE_CTX_get_lookup_crls()
  58. and X509_STORE_CTX_get_cleanup() return the function pointers cached
  59. from the corresponding B<X509_STORE>, please see
  60. L<X509_STORE_set_verify(3)> for more information.
  61. =head1 WARNING
  62. In general a verification callback should B<NOT> unconditionally return 1 in
  63. all circumstances because this will allow verification to succeed no matter
  64. what the error. This effectively removes all security from the application
  65. because B<any> certificate (including untrusted generated ones) will be
  66. accepted.
  67. =head1 NOTES
  68. The verification callback can be set and inherited from the parent structure
  69. performing the operation. In some cases (such as S/MIME verification) the
  70. B<X509_STORE_CTX> structure is created and destroyed internally and the
  71. only way to set a custom verification callback is by inheriting it from the
  72. associated B<X509_STORE>.
  73. =head1 RETURN VALUES
  74. X509_STORE_CTX_set_verify_cb() does not return a value.
  75. =head1 EXAMPLES
  76. Default callback operation:
  77. int verify_callback(int ok, X509_STORE_CTX *ctx) {
  78. return ok;
  79. }
  80. Simple example, suppose a certificate in the chain is expired and we wish
  81. to continue after this error:
  82. int verify_callback(int ok, X509_STORE_CTX *ctx) {
  83. /* Tolerate certificate expiration */
  84. if (X509_STORE_CTX_get_error(ctx) == X509_V_ERR_CERT_HAS_EXPIRED)
  85. return 1;
  86. /* Otherwise don't override */
  87. return ok;
  88. }
  89. More complex example, we don't wish to continue after B<any> certificate has
  90. expired just one specific case:
  91. int verify_callback(int ok, X509_STORE_CTX *ctx)
  92. {
  93. int err = X509_STORE_CTX_get_error(ctx);
  94. X509 *err_cert = X509_STORE_CTX_get_current_cert(ctx);
  95. if (err == X509_V_ERR_CERT_HAS_EXPIRED) {
  96. if (check_is_acceptable_expired_cert(err_cert)
  97. return 1;
  98. }
  99. return ok;
  100. }
  101. Full featured logging callback. In this case the B<bio_err> is assumed to be
  102. a global logging B<BIO>, an alternative would to store a BIO in B<ctx> using
  103. B<ex_data>.
  104. int verify_callback(int ok, X509_STORE_CTX *ctx)
  105. {
  106. X509 *err_cert;
  107. int err, depth;
  108. err_cert = X509_STORE_CTX_get_current_cert(ctx);
  109. err = X509_STORE_CTX_get_error(ctx);
  110. depth = X509_STORE_CTX_get_error_depth(ctx);
  111. BIO_printf(bio_err, "depth=%d ", depth);
  112. if (err_cert) {
  113. X509_NAME_print_ex(bio_err, X509_get_subject_name(err_cert),
  114. 0, XN_FLAG_ONELINE);
  115. BIO_puts(bio_err, "\n");
  116. }
  117. else
  118. BIO_puts(bio_err, "<no cert>\n");
  119. if (!ok)
  120. BIO_printf(bio_err, "verify error:num=%d:%s\n", err,
  121. X509_verify_cert_error_string(err));
  122. switch (err) {
  123. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
  124. BIO_puts(bio_err, "issuer= ");
  125. X509_NAME_print_ex(bio_err, X509_get_issuer_name(err_cert),
  126. 0, XN_FLAG_ONELINE);
  127. BIO_puts(bio_err, "\n");
  128. break;
  129. case X509_V_ERR_CERT_NOT_YET_VALID:
  130. case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
  131. BIO_printf(bio_err, "notBefore=");
  132. ASN1_TIME_print(bio_err, X509_get_notBefore(err_cert));
  133. BIO_printf(bio_err, "\n");
  134. break;
  135. case X509_V_ERR_CERT_HAS_EXPIRED:
  136. case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
  137. BIO_printf(bio_err, "notAfter=");
  138. ASN1_TIME_print(bio_err, X509_get_notAfter(err_cert));
  139. BIO_printf(bio_err, "\n");
  140. break;
  141. case X509_V_ERR_NO_EXPLICIT_POLICY:
  142. policies_print(bio_err, ctx);
  143. break;
  144. }
  145. if (err == X509_V_OK && ok == 2)
  146. /* print out policies */
  147. BIO_printf(bio_err, "verify return:%d\n", ok);
  148. return(ok);
  149. }
  150. =head1 SEE ALSO
  151. L<X509_STORE_CTX_get_error(3)>
  152. L<X509_STORE_set_verify_cb_func(3)>
  153. L<X509_STORE_CTX_get_ex_new_index(3)>
  154. =head1 HISTORY
  155. X509_STORE_CTX_get_get_issuer(),
  156. X509_STORE_CTX_get_check_issued(), X509_STORE_CTX_get_check_revocation(),
  157. X509_STORE_CTX_get_get_crl(), X509_STORE_CTX_get_check_crl(),
  158. X509_STORE_CTX_get_cert_crl(), X509_STORE_CTX_get_check_policy(),
  159. X509_STORE_CTX_get_lookup_certs(), X509_STORE_CTX_get_lookup_crls()
  160. and X509_STORE_CTX_get_cleanup() were added in OpenSSL 1.1.0.
  161. =head1 COPYRIGHT
  162. Copyright 2009-2016 The OpenSSL Project Authors. All Rights Reserved.
  163. Licensed under the OpenSSL license (the "License"). You may not use
  164. this file except in compliance with the License. You can obtain a copy
  165. in the file LICENSE in the source distribution or at
  166. L<https://www.openssl.org/source/license.html>.
  167. =cut