5 X509_STORE_CTX_get_verify_cb,
6 X509_STORE_CTX_set_verify_cb - get and set verification callback
10 #include <openssl/x509_vfy.h>
12 typedef int (*X509_STORE_CTX_verify_cb)(int, X509_STORE_CTX *);
14 X509_STORE_CTX_verify_cb X509_STORE_CTX_get_verify_cb(X509_STORE_CTX *ctx);
16 void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
17 X509_STORE_CTX_verify_cb verify_cb);
21 X509_STORE_CTX_set_verify_cb() sets the verification callback of B<ctx> to
22 B<verify_cb> overwriting any existing callback.
24 The verification callback can be used to customise the operation of certificate
25 verification, either by overriding error conditions or logging errors for
28 However a verification callback is B<not> essential and the default operation
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.
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.
43 X509_STORE_CTX_get_verify_cb() returns the value of the current callback
44 for the specific B<ctx>.
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
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>.
64 X509_STORE_CTX_set_verify_cb() does not return a value.
68 Default callback operation:
70 int verify_callback(int ok, X509_STORE_CTX *ctx)
75 Simple example, suppose a certificate in the chain is expired and we wish
76 to continue after this error:
78 int verify_callback(int ok, X509_STORE_CTX *ctx)
80 /* Tolerate certificate expiration */
81 if (X509_STORE_CTX_get_error(ctx) == X509_V_ERR_CERT_HAS_EXPIRED)
83 /* Otherwise don't override */
87 More complex example, we don't wish to continue after B<any> certificate has
88 expired just one specific case:
90 int verify_callback(int ok, X509_STORE_CTX *ctx)
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)
96 if (check_is_acceptable_expired_cert(err_cert)
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
106 int verify_callback(int ok, X509_STORE_CTX *ctx)
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);
115 BIO_printf(bio_err,"depth=%d ",depth);
118 X509_NAME_print_ex(bio_err, X509_get_subject_name(err_cert),
120 BIO_puts(bio_err, "\n");
123 BIO_puts(bio_err, "<no cert>\n");
125 BIO_printf(bio_err,"verify error:num=%d:%s\n",err,
126 X509_verify_cert_error_string(err));
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),
133 BIO_puts(bio_err, "\n");
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");
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");
147 case X509_V_ERR_NO_EXPLICIT_POLICY:
148 policies_print(bio_err, ctx);
151 if (err == X509_V_OK && ok == 2)
152 /* print out policies */
154 BIO_printf(bio_err,"verify return:%d\n",ok);
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)>
168 Copyright 2009-2016 The OpenSSL Project Authors. All Rights Reserved.
170 Licensed under the OpenSSL license (the "License"). You may not use
171 this file except in compliance with the License. You can obtain a copy
172 in the file LICENSE in the source distribution or at
173 L<https://www.openssl.org/source/license.html>.