X509_STORE_CTX_set_verify_cb.pod 8.8 KB

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