X509_STORE_CTX_set_verify_cb.pod 8.0 KB

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