xref: /openbsd/lib/libssl/man/SSL_CTX_set_verify.3 (revision 750c98ff)
1*750c98ffSjmc.\" $OpenBSD: SSL_CTX_set_verify.3,v 1.9 2021/06/12 16:59:53 jmc Exp $
2384b127bSschwarze.\" full merge up to: OpenSSL 9b86974e Aug 17 15:21:33 2015 -0400
31bfa9420Sschwarze.\" selective merge up to: OpenSSL 1cb7eff4 Sep 10 13:56:40 2019 +0100
4f1a3c524Sschwarze.\"
5bd2ca0bbSschwarze.\" This file was written by Lutz Jaenicke <jaenicke@openssl.org>.
6bd2ca0bbSschwarze.\" Copyright (c) 2000, 2001, 2002, 2003, 2014 The OpenSSL Project.
7bd2ca0bbSschwarze.\" All rights reserved.
8f1a3c524Sschwarze.\"
9bd2ca0bbSschwarze.\" Redistribution and use in source and binary forms, with or without
10bd2ca0bbSschwarze.\" modification, are permitted provided that the following conditions
11bd2ca0bbSschwarze.\" are met:
12bd2ca0bbSschwarze.\"
13bd2ca0bbSschwarze.\" 1. Redistributions of source code must retain the above copyright
14bd2ca0bbSschwarze.\"    notice, this list of conditions and the following disclaimer.
15bd2ca0bbSschwarze.\"
16bd2ca0bbSschwarze.\" 2. Redistributions in binary form must reproduce the above copyright
17bd2ca0bbSschwarze.\"    notice, this list of conditions and the following disclaimer in
18bd2ca0bbSschwarze.\"    the documentation and/or other materials provided with the
19bd2ca0bbSschwarze.\"    distribution.
20bd2ca0bbSschwarze.\"
21bd2ca0bbSschwarze.\" 3. All advertising materials mentioning features or use of this
22bd2ca0bbSschwarze.\"    software must display the following acknowledgment:
23bd2ca0bbSschwarze.\"    "This product includes software developed by the OpenSSL Project
24bd2ca0bbSschwarze.\"    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
25bd2ca0bbSschwarze.\"
26bd2ca0bbSschwarze.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27bd2ca0bbSschwarze.\"    endorse or promote products derived from this software without
28bd2ca0bbSschwarze.\"    prior written permission. For written permission, please contact
29bd2ca0bbSschwarze.\"    openssl-core@openssl.org.
30bd2ca0bbSschwarze.\"
31bd2ca0bbSschwarze.\" 5. Products derived from this software may not be called "OpenSSL"
32bd2ca0bbSschwarze.\"    nor may "OpenSSL" appear in their names without prior written
33bd2ca0bbSschwarze.\"    permission of the OpenSSL Project.
34bd2ca0bbSschwarze.\"
35bd2ca0bbSschwarze.\" 6. Redistributions of any form whatsoever must retain the following
36bd2ca0bbSschwarze.\"    acknowledgment:
37bd2ca0bbSschwarze.\"    "This product includes software developed by the OpenSSL Project
38bd2ca0bbSschwarze.\"    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
39bd2ca0bbSschwarze.\"
40bd2ca0bbSschwarze.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41bd2ca0bbSschwarze.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42bd2ca0bbSschwarze.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43bd2ca0bbSschwarze.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44bd2ca0bbSschwarze.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45bd2ca0bbSschwarze.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46bd2ca0bbSschwarze.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47bd2ca0bbSschwarze.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48bd2ca0bbSschwarze.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49bd2ca0bbSschwarze.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50bd2ca0bbSschwarze.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51bd2ca0bbSschwarze.\" OF THE POSSIBILITY OF SUCH DAMAGE.
52bd2ca0bbSschwarze.\"
53*750c98ffSjmc.Dd $Mdocdate: June 12 2021 $
54f1a3c524Sschwarze.Dt SSL_CTX_SET_VERIFY 3
55f1a3c524Sschwarze.Os
56f1a3c524Sschwarze.Sh NAME
57f1a3c524Sschwarze.Nm SSL_CTX_set_verify ,
58f1a3c524Sschwarze.Nm SSL_set_verify ,
59f1a3c524Sschwarze.Nm SSL_CTX_set_verify_depth ,
60f1a3c524Sschwarze.Nm SSL_set_verify_depth
61f1a3c524Sschwarze.Nd set peer certificate verification parameters
62f1a3c524Sschwarze.Sh SYNOPSIS
63f1a3c524Sschwarze.In openssl/ssl.h
64f1a3c524Sschwarze.Ft void
65f1a3c524Sschwarze.Fo SSL_CTX_set_verify
66f1a3c524Sschwarze.Fa "SSL_CTX *ctx"
67f1a3c524Sschwarze.Fa "int mode"
68f1a3c524Sschwarze.Fa "int (*verify_callback)(int, X509_STORE_CTX *)"
69f1a3c524Sschwarze.Fc
70f1a3c524Sschwarze.Ft void
71f1a3c524Sschwarze.Fo SSL_set_verify
72f1a3c524Sschwarze.Fa "SSL *s"
73f1a3c524Sschwarze.Fa "int mode"
74f1a3c524Sschwarze.Fa "int (*verify_callback)(int, X509_STORE_CTX *)"
75f1a3c524Sschwarze.Fc
76f1a3c524Sschwarze.Ft void
77f1a3c524Sschwarze.Fn SSL_CTX_set_verify_depth "SSL_CTX *ctx" "int depth"
78f1a3c524Sschwarze.Ft void
79f1a3c524Sschwarze.Fn SSL_set_verify_depth "SSL *s" "int depth"
80f1a3c524Sschwarze.Ft int
81f1a3c524Sschwarze.Fn verify_callback "int preverify_ok" "X509_STORE_CTX *x509_ctx"
82f1a3c524Sschwarze.Sh DESCRIPTION
83f1a3c524Sschwarze.Fn SSL_CTX_set_verify
84f1a3c524Sschwarzesets the verification flags for
85f1a3c524Sschwarze.Fa ctx
86f1a3c524Sschwarzeto be
87f1a3c524Sschwarze.Fa mode
88f1a3c524Sschwarzeand
89f1a3c524Sschwarzespecifies the
90f1a3c524Sschwarze.Fa verify_callback
91f1a3c524Sschwarzefunction to be used.
92f1a3c524SschwarzeIf no callback function shall be specified, the
93f1a3c524Sschwarze.Dv NULL
94f1a3c524Sschwarzepointer can be used for
95f1a3c524Sschwarze.Fa verify_callback .
96f1a3c524Sschwarze.Pp
97f1a3c524Sschwarze.Fn SSL_set_verify
98f1a3c524Sschwarzesets the verification flags for
99f1a3c524Sschwarze.Fa ssl
100f1a3c524Sschwarzeto be
101f1a3c524Sschwarze.Fa mode
102f1a3c524Sschwarzeand specifies the
103f1a3c524Sschwarze.Fa verify_callback
104f1a3c524Sschwarzefunction to be used.
105f1a3c524SschwarzeIf no callback function shall be specified, the
106f1a3c524Sschwarze.Dv NULL
107f1a3c524Sschwarzepointer can be used for
108f1a3c524Sschwarze.Fa verify_callback .
109f1a3c524SschwarzeIn this case last
110f1a3c524Sschwarze.Fa verify_callback
111f1a3c524Sschwarzeset specifically for this
112f1a3c524Sschwarze.Fa ssl
113f1a3c524Sschwarzeremains.
114f1a3c524SschwarzeIf no special callback was set before, the default callback for the underlying
115f1a3c524Sschwarze.Fa ctx
116f1a3c524Sschwarzeis used, that was valid at the time
117f1a3c524Sschwarze.Fa ssl
118f1a3c524Sschwarzewas created with
119f1a3c524Sschwarze.Xr SSL_new 3 .
120384b127bSschwarzeWithin the callback function,
121384b127bSschwarze.Xr SSL_get_ex_data_X509_STORE_CTX_idx 3
122384b127bSschwarzecan be called to get the data index of the current
123384b127bSschwarze.Vt SSL
124384b127bSschwarzeobject that is doing the verification.
125f1a3c524Sschwarze.Pp
126f1a3c524Sschwarze.Fn SSL_CTX_set_verify_depth
127f1a3c524Sschwarzesets the maximum
128f1a3c524Sschwarze.Fa depth
129f1a3c524Sschwarzefor the certificate chain verification that shall be allowed for
130f1a3c524Sschwarze.Fa ctx .
131f1a3c524Sschwarze(See the
132f1a3c524Sschwarze.Sx BUGS
133f1a3c524Sschwarzesection.)
134f1a3c524Sschwarze.Pp
135f1a3c524Sschwarze.Fn SSL_set_verify_depth
136f1a3c524Sschwarzesets the maximum
137f1a3c524Sschwarze.Fa depth
138f1a3c524Sschwarzefor the certificate chain verification that shall be allowed for
139f1a3c524Sschwarze.Fa ssl .
140f1a3c524Sschwarze(See the
141f1a3c524Sschwarze.Sx BUGS
142f1a3c524Sschwarzesection.)
143bd2ca0bbSschwarze.Pp
144f1a3c524SschwarzeThe verification of certificates can be controlled by a set of bitwise ORed
145f1a3c524Sschwarze.Fa mode
146f1a3c524Sschwarzeflags:
147f1a3c524Sschwarze.Bl -tag -width Ds
148f1a3c524Sschwarze.It Dv SSL_VERIFY_NONE
149f1a3c524Sschwarze.Em Server mode :
150f1a3c524Sschwarzethe server will not send a client certificate request to the client,
151f1a3c524Sschwarzeso the client will not send a certificate.
152f1a3c524Sschwarze.Pp
153f1a3c524Sschwarze.Em Client mode :
154f1a3c524Sschwarzeif not using an anonymous cipher (by default disabled),
155f1a3c524Sschwarzethe server will send a certificate which will be checked.
156f1a3c524SschwarzeThe result of the certificate verification process can be checked after the
157f1a3c524SschwarzeTLS/SSL handshake using the
158f1a3c524Sschwarze.Xr SSL_get_verify_result 3
159f1a3c524Sschwarzefunction.
160f1a3c524SschwarzeThe handshake will be continued regardless of the verification result.
161f1a3c524Sschwarze.It Dv SSL_VERIFY_PEER
162f1a3c524Sschwarze.Em Server mode :
163f1a3c524Sschwarzethe server sends a client certificate request to the client.
164f1a3c524SschwarzeThe certificate returned (if any) is checked.
165f1a3c524SschwarzeIf the verification process fails,
166f1a3c524Sschwarzethe TLS/SSL handshake is immediately terminated with an alert message
167f1a3c524Sschwarzecontaining the reason for the verification failure.
168f1a3c524SschwarzeThe behaviour can be controlled by the additional
169f1a3c524Sschwarze.Dv SSL_VERIFY_FAIL_IF_NO_PEER_CERT
170f1a3c524Sschwarzeand
171f1a3c524Sschwarze.Dv SSL_VERIFY_CLIENT_ONCE
172f1a3c524Sschwarzeflags.
173f1a3c524Sschwarze.Pp
174f1a3c524Sschwarze.Em Client mode :
175f1a3c524Sschwarzethe server certificate is verified.
176f1a3c524SschwarzeIf the verification process fails,
177f1a3c524Sschwarzethe TLS/SSL handshake is immediately terminated with an alert message
178f1a3c524Sschwarzecontaining the reason for the verification failure.
179f1a3c524SschwarzeIf no server certificate is sent, because an anonymous cipher is used,
180f1a3c524Sschwarze.Dv SSL_VERIFY_PEER
181f1a3c524Sschwarzeis ignored.
182f1a3c524Sschwarze.It Dv SSL_VERIFY_FAIL_IF_NO_PEER_CERT
183f1a3c524Sschwarze.Em Server mode :
184f1a3c524Sschwarzeif the client did not return a certificate, the TLS/SSL
185f1a3c524Sschwarzehandshake is immediately terminated with a
186f1a3c524Sschwarze.Dq handshake failure
187f1a3c524Sschwarzealert.
188f1a3c524SschwarzeThis flag must be used together with
189f1a3c524Sschwarze.Dv SSL_VERIFY_PEER .
190f1a3c524Sschwarze.Pp
191f1a3c524Sschwarze.Em Client mode :
192f1a3c524Sschwarzeignored
193f1a3c524Sschwarze.It Dv SSL_VERIFY_CLIENT_ONCE
194f1a3c524Sschwarze.Em Server mode :
195f1a3c524Sschwarzeonly request a client certificate on the initial TLS/SSL handshake.
196f1a3c524SschwarzeDo not ask for a client certificate again in case of a renegotiation.
197f1a3c524SschwarzeThis flag must be used together with
198f1a3c524Sschwarze.Dv SSL_VERIFY_PEER .
199f1a3c524Sschwarze.Pp
200f1a3c524Sschwarze.Em Client mode :
201f1a3c524Sschwarzeignored
202f1a3c524Sschwarze.El
203f1a3c524Sschwarze.Pp
204f1a3c524SschwarzeExactly one of the
205f1a3c524Sschwarze.Fa mode
206f1a3c524Sschwarzeflags
207f1a3c524Sschwarze.Dv SSL_VERIFY_NONE
208f1a3c524Sschwarzeand
209f1a3c524Sschwarze.Dv SSL_VERIFY_PEER
210f1a3c524Sschwarzemust be set at any time.
211f1a3c524Sschwarze.Pp
212f1a3c524SschwarzeThe actual verification procedure is performed either using the built-in
213f1a3c524Sschwarzeverification procedure or using another application provided verification
214f1a3c524Sschwarzefunction set with
215f1a3c524Sschwarze.Xr SSL_CTX_set_cert_verify_callback 3 .
216f1a3c524SschwarzeThe following descriptions apply in the case of the built-in procedure.
217f1a3c524SschwarzeAn application provided procedure also has access to the verify depth
218f1a3c524Sschwarzeinformation and the
219f1a3c524Sschwarze.Fa verify_callback Ns ()
220f1a3c524Sschwarzefunction, but the way this information is used may be different.
221f1a3c524Sschwarze.Pp
222f1a3c524Sschwarze.Fn SSL_CTX_set_verify_depth
223f1a3c524Sschwarzeand
224f1a3c524Sschwarze.Fn SSL_set_verify_depth
225f1a3c524Sschwarzeset the limit up to which depth certificates in a chain are used during the
226f1a3c524Sschwarzeverification procedure.
227f1a3c524SschwarzeIf the certificate chain is longer than allowed,
228f1a3c524Sschwarzethe certificates above the limit are ignored.
229f1a3c524SschwarzeError messages are generated as if these certificates would not be present,
230f1a3c524Sschwarzemost likely a
231f1a3c524Sschwarze.Dv X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
232f1a3c524Sschwarzewill be issued.
233f1a3c524SschwarzeThe depth count is
234f1a3c524Sschwarze.Dq level 0: peer certificate ,
235f1a3c524Sschwarze.Dq level 1: CA certificate ,
236f1a3c524Sschwarze.Dq level 2: higher level CA certificate ,
237f1a3c524Sschwarzeand so on.
238f1a3c524SschwarzeSetting the maximum depth to 2 allows the levels 0, 1, and 2.
239f1a3c524SschwarzeThe default depth limit is 100,
240f1a3c524Sschwarzeallowing for the peer certificate and an additional 100 CA certificates.
241f1a3c524Sschwarze.Pp
242f1a3c524SschwarzeThe
243f1a3c524Sschwarze.Fa verify_callback
244f1a3c524Sschwarzefunction is used to control the behaviour when the
245f1a3c524Sschwarze.Dv SSL_VERIFY_PEER
246f1a3c524Sschwarzeflag is set.
247f1a3c524SschwarzeIt must be supplied by the application and receives two arguments:
248f1a3c524Sschwarze.Fa preverify_ok
249f1a3c524Sschwarzeindicates whether the verification of the certificate in question was passed
250f1a3c524Sschwarze(preverify_ok=1) or not (preverify_ok=0).
251f1a3c524Sschwarze.Fa x509_ctx
252f1a3c524Sschwarzeis a pointer to the complete context used
253f1a3c524Sschwarzefor the certificate chain verification.
254f1a3c524Sschwarze.Pp
255f1a3c524SschwarzeThe certificate chain is checked starting with the deepest nesting level
256f1a3c524Sschwarze(the root CA certificate) and worked upward to the peer's certificate.
257f1a3c524SschwarzeAt each level signatures and issuer attributes are checked.
258f1a3c524SschwarzeWhenever a verification error is found, the error number is stored in
259f1a3c524Sschwarze.Fa x509_ctx
260f1a3c524Sschwarzeand
261f1a3c524Sschwarze.Fa verify_callback
262f1a3c524Sschwarzeis called with
263f1a3c524Sschwarze.Fa preverify_ok
264f1a3c524Sschwarzeequal to 0.
265f1a3c524SschwarzeBy applying
266f1a3c524Sschwarze.Fn X509_CTX_store_*
267f1a3c524Sschwarzefunctions
268f1a3c524Sschwarze.Fa verify_callback
269f1a3c524Sschwarzecan locate the certificate in question and perform additional steps (see
270f1a3c524Sschwarze.Sx EXAMPLES ) .
271f1a3c524SschwarzeIf no error is found for a certificate,
272f1a3c524Sschwarze.Fa verify_callback
273f1a3c524Sschwarzeis called with
274f1a3c524Sschwarze.Fa preverify_ok
275f1a3c524Sschwarzeequal to 1 before advancing to the next level.
276f1a3c524Sschwarze.Pp
277f1a3c524SschwarzeThe return value of
278f1a3c524Sschwarze.Fa verify_callback
279f1a3c524Sschwarzecontrols the strategy of the further verification process.
280f1a3c524SschwarzeIf
281f1a3c524Sschwarze.Fa verify_callback
282f1a3c524Sschwarzereturns 0, the verification process is immediately stopped with
283f1a3c524Sschwarze.Dq verification failed
284f1a3c524Sschwarzestate.
285f1a3c524SschwarzeIf
286f1a3c524Sschwarze.Dv SSL_VERIFY_PEER
287f1a3c524Sschwarzeis set, a verification failure alert is sent to the peer and the TLS/SSL
288f1a3c524Sschwarzehandshake is terminated.
289f1a3c524SschwarzeIf
290f1a3c524Sschwarze.Fa verify_callback
291f1a3c524Sschwarzereturns 1, the verification process is continued.
292f1a3c524SschwarzeIf
293f1a3c524Sschwarze.Fa verify_callback
294f1a3c524Sschwarzealways returns 1,
295f1a3c524Sschwarzethe TLS/SSL handshake will not be terminated with respect to verification
296f1a3c524Sschwarzefailures and the connection will be established.
297f1a3c524SschwarzeThe calling process can however retrieve the error code of the last
298f1a3c524Sschwarzeverification error using
299f1a3c524Sschwarze.Xr SSL_get_verify_result 3
300f1a3c524Sschwarzeor by maintaining its own error storage managed by
301f1a3c524Sschwarze.Fa verify_callback .
302f1a3c524Sschwarze.Pp
303f1a3c524SschwarzeIf no
304f1a3c524Sschwarze.Fa verify_callback
305f1a3c524Sschwarzeis specified, the default callback will be used.
306f1a3c524SschwarzeIts return value is identical to
307f1a3c524Sschwarze.Fa preverify_ok ,
308f1a3c524Sschwarzeso that any verification
309f1a3c524Sschwarzefailure will lead to a termination of the TLS/SSL handshake with an
310f1a3c524Sschwarzealert message, if
311f1a3c524Sschwarze.Dv SSL_VERIFY_PEER
312f1a3c524Sschwarzeis set.
313f1a3c524Sschwarze.Sh EXAMPLES
314f1a3c524SschwarzeThe following code sequence realizes an example
315f1a3c524Sschwarze.Fa verify_callback
316f1a3c524Sschwarzefunction that will always continue the TLS/SSL handshake regardless of
317f1a3c524Sschwarzeverification failure, if wished.
318f1a3c524SschwarzeThe callback realizes a verification depth limit with more informational output.
319f1a3c524Sschwarze.Pp
320f1a3c524SschwarzeAll verification errors are printed;
321f1a3c524Sschwarzeinformation about the certificate chain is printed on request.
322f1a3c524SschwarzeThe example is realized for a server that does allow but not require client
323f1a3c524Sschwarzecertificates.
324f1a3c524Sschwarze.Pp
325f1a3c524SschwarzeThe example makes use of the ex_data technique to store application data
326f1a3c524Sschwarzeinto/retrieve application data from the
327f1a3c524Sschwarze.Vt SSL
328f1a3c524Sschwarzestructure (see
329f1a3c524Sschwarze.Xr SSL_get_ex_new_index 3 ,
330f1a3c524Sschwarze.Xr SSL_get_ex_data_X509_STORE_CTX_idx 3 ) .
331f1a3c524Sschwarze.Bd -literal
332f1a3c524Sschwarze\&...
333f1a3c524Sschwarze
334f1a3c524Sschwarzetypedef struct {
335f1a3c524Sschwarze	int	verbose_mode;
336f1a3c524Sschwarze	int	verify_depth;
337f1a3c524Sschwarze	int	always_continue;
338f1a3c524Sschwarze} mydata_t;
339f1a3c524Sschwarzeint mydata_index;
340f1a3c524Sschwarze\&...
341f1a3c524Sschwarzestatic int
342f1a3c524Sschwarzeverify_callback(int preverify_ok, X509_STORE_CTX *ctx)
343f1a3c524Sschwarze{
344f1a3c524Sschwarze	char buf[256];
345f1a3c524Sschwarze	X509 *err_cert;
346f1a3c524Sschwarze	int err, depth;
347f1a3c524Sschwarze	SSL *ssl;
348f1a3c524Sschwarze	mydata_t *mydata;
349f1a3c524Sschwarze
350f1a3c524Sschwarze	err_cert = X509_STORE_CTX_get_current_cert(ctx);
351f1a3c524Sschwarze	err = X509_STORE_CTX_get_error(ctx);
352f1a3c524Sschwarze	depth = X509_STORE_CTX_get_error_depth(ctx);
353f1a3c524Sschwarze
354f1a3c524Sschwarze	/*
355f1a3c524Sschwarze	 * Retrieve the pointer to the SSL of the connection currently
356f1a3c524Sschwarze	 * treated * and the application specific data stored into the
357f1a3c524Sschwarze	 * SSL object.
358f1a3c524Sschwarze	 */
359f1a3c524Sschwarze	ssl = X509_STORE_CTX_get_ex_data(ctx,
360f1a3c524Sschwarze	    SSL_get_ex_data_X509_STORE_CTX_idx());
361f1a3c524Sschwarze	mydata = SSL_get_ex_data(ssl, mydata_index);
362f1a3c524Sschwarze
363f1a3c524Sschwarze	X509_NAME_oneline(X509_get_subject_name(err_cert), buf, 256);
364f1a3c524Sschwarze
365f1a3c524Sschwarze	/*
366f1a3c524Sschwarze	 * Catch a too long certificate chain. The depth limit set using
367f1a3c524Sschwarze	 * SSL_CTX_set_verify_depth() is by purpose set to "limit+1" so
368f1a3c524Sschwarze	 * that whenever the "depth>verify_depth" condition is met, we
369f1a3c524Sschwarze	 * have violated the limit and want to log this error condition.
370f1a3c524Sschwarze	 * We must do it here, because the CHAIN_TOO_LONG error would not
371f1a3c524Sschwarze	 * be found explicitly; only errors introduced by cutting off the
372f1a3c524Sschwarze	 * additional certificates would be logged.
373f1a3c524Sschwarze	 */
374f1a3c524Sschwarze	if (depth > mydata->verify_depth) {
375f1a3c524Sschwarze		preverify_ok = 0;
376f1a3c524Sschwarze		err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
377f1a3c524Sschwarze		X509_STORE_CTX_set_error(ctx, err);
378f1a3c524Sschwarze	}
379f1a3c524Sschwarze	if (!preverify_ok) {
380f1a3c524Sschwarze		printf("verify error:num=%d:%s:depth=%d:%s\en", err,
381f1a3c524Sschwarze		    X509_verify_cert_error_string(err), depth, buf);
382f1a3c524Sschwarze	} else if (mydata->verbose_mode) {
383f1a3c524Sschwarze		printf("depth=%d:%s\en", depth, buf);
384f1a3c524Sschwarze	}
385f1a3c524Sschwarze
386f1a3c524Sschwarze	/*
387f1a3c524Sschwarze	 * At this point, err contains the last verification error.
388f1a3c524Sschwarze	 * We can use it for something special
389f1a3c524Sschwarze	 */
390f1a3c524Sschwarze	if (!preverify_ok && (err ==
391f1a3c524Sschwarze	    X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT)) {
392f1a3c524Sschwarze		X509_NAME_oneline(X509_get_issuer_name(ctx->current_cert),
393f1a3c524Sschwarze		    buf, 256);
394f1a3c524Sschwarze		printf("issuer= %s\en", buf);
395f1a3c524Sschwarze	}
396f1a3c524Sschwarze
397f1a3c524Sschwarze	if (mydata->always_continue)
398f1a3c524Sschwarze		return 1;
399f1a3c524Sschwarze	else
400f1a3c524Sschwarze		return preverify_ok;
401f1a3c524Sschwarze}
402f1a3c524Sschwarze\&...
403f1a3c524Sschwarze
404f1a3c524Sschwarzemydata_t mydata;
405f1a3c524Sschwarze
406f1a3c524Sschwarze\&...
407f1a3c524Sschwarze
408f1a3c524Sschwarzemydata_index = SSL_get_ex_new_index(0, "mydata index", NULL, NULL, NULL);
409f1a3c524Sschwarze
410f1a3c524Sschwarze\&...
411f1a3c524Sschwarze
412f1a3c524SschwarzeSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
413f1a3c524Sschwarze    verify_callback);
414f1a3c524Sschwarze
415f1a3c524Sschwarze/*
416f1a3c524Sschwarze * Let the verify_callback catch the verify_depth error so that we get
417f1a3c524Sschwarze * an appropriate error in the logfile.
418f1a3c524Sschwarze */
419f1a3c524SschwarzeSSL_CTX_set_verify_depth(verify_depth + 1);
420f1a3c524Sschwarze
421f1a3c524Sschwarze/*
422f1a3c524Sschwarze * Set up the SSL specific data into "mydata" and store it into the SSL
423f1a3c524Sschwarze * structure.
424f1a3c524Sschwarze */
425f1a3c524Sschwarzemydata.verify_depth = verify_depth; ...
426f1a3c524SschwarzeSSL_set_ex_data(ssl, mydata_index, &mydata);
427f1a3c524Sschwarze
428f1a3c524Sschwarze\&...
429f1a3c524Sschwarze
430f1a3c524SschwarzeSSL_accept(ssl); /* check of success left out for clarity */
431f1a3c524Sschwarzeif (peer = SSL_get_peer_certificate(ssl)) {
432f1a3c524Sschwarze	if (SSL_get_verify_result(ssl) == X509_V_OK) {
433f1a3c524Sschwarze		/* The client sent a certificate which verified OK */
434f1a3c524Sschwarze	}
435f1a3c524Sschwarze}
436f1a3c524Sschwarze.Ed
437f1a3c524Sschwarze.Sh SEE ALSO
438f1a3c524Sschwarze.Xr ssl 3 ,
439f1a3c524Sschwarze.Xr SSL_CTX_get_verify_mode 3 ,
440f1a3c524Sschwarze.Xr SSL_CTX_load_verify_locations 3 ,
441f1a3c524Sschwarze.Xr SSL_CTX_set_cert_verify_callback 3 ,
442f1a3c524Sschwarze.Xr SSL_get_ex_data_X509_STORE_CTX_idx 3 ,
443f1a3c524Sschwarze.Xr SSL_get_ex_new_index 3 ,
444f1a3c524Sschwarze.Xr SSL_get_peer_certificate 3 ,
445f1a3c524Sschwarze.Xr SSL_get_verify_result 3 ,
4461bfa9420Sschwarze.Xr SSL_new 3 ,
4471bfa9420Sschwarze.Xr SSL_set1_host 3
4488fba1ec8Sschwarze.Sh HISTORY
4498fba1ec8Sschwarze.Fn SSL_set_verify
45010e00d17Sschwarzeappeared in SSLeay 0.4 or earlier.
45110e00d17Sschwarze.Fn SSL_CTX_set_verify
45210e00d17Sschwarzefirst appeared in SSLeay 0.6.4.
45310e00d17SschwarzeBoth functions have been available since
4548fba1ec8Sschwarze.Ox 2.4 .
455684e8488Sschwarze.Pp
456684e8488Sschwarze.Fn SSL_CTX_set_verify_depth
457684e8488Sschwarzeand
458684e8488Sschwarze.Fn SSL_set_verify_depth
459684e8488Sschwarzefirst appeared in OpenSSL 0.9.3 and have been available since
460684e8488Sschwarze.Ox 2.6 .
461f1a3c524Sschwarze.Sh BUGS
462f1a3c524SschwarzeIn client mode, it is not checked whether the
463f1a3c524Sschwarze.Dv SSL_VERIFY_PEER
464f1a3c524Sschwarzeflag is set, but whether
465f1a3c524Sschwarze.Dv SSL_VERIFY_NONE
466f1a3c524Sschwarzeis not set.
467f1a3c524SschwarzeThis can lead to unexpected behaviour, if the
468f1a3c524Sschwarze.Dv SSL_VERIFY_PEER
469f1a3c524Sschwarzeand
470f1a3c524Sschwarze.Dv SSL_VERIFY_NONE
471f1a3c524Sschwarzeare not used as required (exactly one must be set at any time).
472f1a3c524Sschwarze.Pp
473f1a3c524SschwarzeThe certificate verification depth set with
474f1a3c524Sschwarze.Fn SSL[_CTX]_verify_depth
475f1a3c524Sschwarzestops the verification at a certain depth.
476f1a3c524SschwarzeThe error message produced will be that of an incomplete certificate chain and
477f1a3c524Sschwarzenot
478f1a3c524Sschwarze.Dv X509_V_ERR_CERT_CHAIN_TOO_LONG
479f1a3c524Sschwarzeas may be expected.
480