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