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