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