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