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