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