Doc nits: callback function typedefs
[openssl.git] / doc / man3 / SSL_CTX_set_verify.pod
1 =pod
2
3 =head1 NAME
4
5 SSL_CTX_set_verify, SSL_set_verify,
6 SSL_CTX_set_verify_depth, SSL_set_verify_depth,
7 verify_cb
8 - set peer certificate verification parameters
9
10 =head1 SYNOPSIS
11
12  #include <openssl/ssl.h>
13
14  void SSL_CTX_set_verify(SSL_CTX *ctx, int mode, verify_cb verify_callback);
15  void SSL_set_verify(SSL *s, int mode, verify_cb verify_callback);
16  void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth);
17  void SSL_set_verify_depth(SSL *s, int depth);
18
19  typedef int (*verify_cb)(int preverify_ok, X509_STORE_CTX *x509_ctx);
20
21 =head1 DESCRIPTION
22
23 SSL_CTX_set_verify() sets the verification flags for B<ctx> to be B<mode> and
24 specifies the B<verify_callback> function to be used. If no callback function
25 shall be specified, the NULL pointer can be used for B<verify_callback>.
26
27 SSL_set_verify() sets the verification flags for B<ssl> to be B<mode> and
28 specifies the B<verify_callback> function to be used. If no callback function
29 shall be specified, the NULL pointer can be used for B<verify_callback>. In
30 this case last B<verify_callback> set specifically for this B<ssl> remains. If
31 no special B<callback> was set before, the default callback for the underlying
32 B<ctx> is used, that was valid at the time B<ssl> was created with
33 L<SSL_new(3)>.
34
35 SSL_CTX_set_verify_depth() sets the maximum B<depth> for the certificate chain
36 verification that shall be allowed for B<ctx>. (See the BUGS section.)
37
38 SSL_set_verify_depth() sets the maximum B<depth> for the certificate chain
39 verification that shall be allowed for B<ssl>. (See the BUGS section.)
40
41 =head1 NOTES
42
43 The verification of certificates can be controlled by a set of logically
44 or'ed B<mode> flags:
45
46 =over 4
47
48 =item SSL_VERIFY_NONE
49
50 B<Server mode:> the server will not send a client certificate request to the
51 client, so the client will not send a certificate.
52
53 B<Client mode:> if not using an anonymous cipher (by default disabled), the
54 server will send a certificate which will be checked. The result of the
55 certificate verification process can be checked after the TLS/SSL handshake
56 using the L<SSL_get_verify_result(3)> function.
57 The handshake will be continued regardless of the verification result.
58
59 =item SSL_VERIFY_PEER
60
61 B<Server mode:> the server sends a client certificate request to the client.
62 The certificate returned (if any) is checked. If the verification process
63 fails, the TLS/SSL handshake is
64 immediately terminated with an alert message containing the reason for
65 the verification failure.
66 The behaviour can be controlled by the additional
67 SSL_VERIFY_FAIL_IF_NO_PEER_CERT and SSL_VERIFY_CLIENT_ONCE flags.
68
69 B<Client mode:> the server certificate is verified. If the verification process
70 fails, the TLS/SSL handshake is
71 immediately terminated with an alert message containing the reason for
72 the verification failure. If no server certificate is sent, because an
73 anonymous cipher is used, SSL_VERIFY_PEER is ignored.
74
75 =item SSL_VERIFY_FAIL_IF_NO_PEER_CERT
76
77 B<Server mode:> if the client did not return a certificate, the TLS/SSL
78 handshake is immediately terminated with a "handshake failure" alert.
79 This flag must be used together with SSL_VERIFY_PEER.
80
81 B<Client mode:> ignored
82
83 =item SSL_VERIFY_CLIENT_ONCE
84
85 B<Server mode:> only request a client certificate on the initial TLS/SSL
86 handshake. Do not ask for a client certificate again in case of a
87 renegotiation. This flag must be used together with SSL_VERIFY_PEER.
88
89 B<Client mode:> ignored
90
91 =back
92
93 If the B<mode> is SSL_VERIFY_NONE none of the other flags may be set.
94
95 The actual verification procedure is performed either using the built-in
96 verification procedure or using another application provided verification
97 function set with
98 L<SSL_CTX_set_cert_verify_callback(3)>.
99 The following descriptions apply in the case of the built-in procedure. An
100 application provided procedure also has access to the verify depth information
101 and the verify_callback() function, but the way this information is used
102 may be different.
103
104 SSL_CTX_set_verify_depth() and SSL_set_verify_depth() set the limit up
105 to which depth certificates in a chain are used during the verification
106 procedure. If the certificate chain is longer than allowed, the certificates
107 above the limit are ignored. Error messages are generated as if these
108 certificates would not be present, most likely a
109 X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY will be issued.
110 The depth count is "level 0:peer certificate", "level 1: CA certificate",
111 "level 2: higher level CA certificate", and so on. Setting the maximum
112 depth to 2 allows the levels 0, 1, and 2. The default depth limit is 100,
113 allowing for the peer certificate and additional 100 CA certificates.
114
115 The B<verify_callback> function is used to control the behaviour when the
116 SSL_VERIFY_PEER flag is set. It must be supplied by the application and
117 receives two arguments: B<preverify_ok> indicates, whether the verification of
118 the certificate in question was passed (preverify_ok=1) or not
119 (preverify_ok=0). B<x509_ctx> is a pointer to the complete context used
120 for the certificate chain verification.
121
122 The certificate chain is checked starting with the deepest nesting level
123 (the root CA certificate) and worked upward to the peer's certificate.
124 At each level signatures and issuer attributes are checked. Whenever
125 a verification error is found, the error number is stored in B<x509_ctx>
126 and B<verify_callback> is called with B<preverify_ok>=0. By applying
127 X509_CTX_store_* functions B<verify_callback> can locate the certificate
128 in question and perform additional steps (see EXAMPLES). If no error is
129 found for a certificate, B<verify_callback> is called with B<preverify_ok>=1
130 before advancing to the next level.
131
132 The return value of B<verify_callback> controls the strategy of the further
133 verification process. If B<verify_callback> returns 0, the verification
134 process is immediately stopped with "verification failed" state. If
135 SSL_VERIFY_PEER is set, a verification failure alert is sent to the peer and
136 the TLS/SSL handshake is terminated. If B<verify_callback> returns 1,
137 the verification process is continued. If B<verify_callback> always returns
138 1, the TLS/SSL handshake will not be terminated with respect to verification
139 failures and the connection will be established. The calling process can
140 however retrieve the error code of the last verification error using
141 L<SSL_get_verify_result(3)> or by maintaining its
142 own error storage managed by B<verify_callback>.
143
144 If no B<verify_callback> is specified, the default callback will be used.
145 Its return value is identical to B<preverify_ok>, so that any verification
146 failure will lead to a termination of the TLS/SSL handshake with an
147 alert message, if SSL_VERIFY_PEER is set.
148
149 =head1 BUGS
150
151 In client mode, it is not checked whether the SSL_VERIFY_PEER flag
152 is set, but whether any flags are set. This can lead to
153 unexpected behaviour if SSL_VERIFY_PEER and other flags are not used as
154 required.
155
156 =head1 RETURN VALUES
157
158 The SSL*_set_verify*() functions do not provide diagnostic information.
159
160 =head1 EXAMPLES
161
162 The following code sequence realizes an example B<verify_callback> function
163 that will always continue the TLS/SSL handshake regardless of verification
164 failure, if wished. The callback realizes a verification depth limit with
165 more informational output.
166
167 All verification errors are printed; information about the certificate chain
168 is printed on request.
169 The example is realized for a server that does allow but not require client
170 certificates.
171
172 The example makes use of the ex_data technique to store application data
173 into/retrieve application data from the SSL structure
174 (see L<SSL_get_ex_new_index(3)>,
175 L<SSL_get_ex_data_X509_STORE_CTX_idx(3)>).
176
177  ...
178  typedef struct {
179    int verbose_mode;
180    int verify_depth;
181    int always_continue;
182  } mydata_t;
183  int mydata_index;
184  ...
185  static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
186  {
187     char    buf[256];
188     X509   *err_cert;
189     int     err, depth;
190     SSL    *ssl;
191     mydata_t *mydata;
192
193     err_cert = X509_STORE_CTX_get_current_cert(ctx);
194     err = X509_STORE_CTX_get_error(ctx);
195     depth = X509_STORE_CTX_get_error_depth(ctx);
196
197     /*
198      * Retrieve the pointer to the SSL of the connection currently treated
199      * and the application specific data stored into the SSL object.
200      */
201     ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
202     mydata = SSL_get_ex_data(ssl, mydata_index);
203
204     X509_NAME_oneline(X509_get_subject_name(err_cert), buf, 256);
205
206     /*
207      * Catch a too long certificate chain. The depth limit set using
208      * SSL_CTX_set_verify_depth() is by purpose set to "limit+1" so
209      * that whenever the "depth>verify_depth" condition is met, we
210      * have violated the limit and want to log this error condition.
211      * We must do it here, because the CHAIN_TOO_LONG error would not
212      * be found explicitly; only errors introduced by cutting off the
213      * additional certificates would be logged.
214      */
215     if (depth > mydata->verify_depth) {
216         preverify_ok = 0;
217         err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
218         X509_STORE_CTX_set_error(ctx, err);
219     }
220     if (!preverify_ok) {
221         printf("verify error:num=%d:%s:depth=%d:%s\n", err,
222                  X509_verify_cert_error_string(err), depth, buf);
223     }
224     else if (mydata->verbose_mode)
225     {
226         printf("depth=%d:%s\n", depth, buf);
227     }
228
229     /*
230      * At this point, err contains the last verification error. We can use
231      * it for something special
232      */
233     if (!preverify_ok && (err == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT))
234     {
235       X509_NAME_oneline(X509_get_issuer_name(err_cert), buf, 256);
236       printf("issuer= %s\n", buf);
237     }
238
239     if (mydata->always_continue)
240       return 1;
241     else
242       return preverify_ok;
243  }
244  ...
245
246  mydata_t mydata;
247
248  ...
249  mydata_index = SSL_get_ex_new_index(0, "mydata index", NULL, NULL, NULL);
250
251  ...
252  SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
253                     verify_callback);
254
255  /*
256   * Let the verify_callback catch the verify_depth error so that we get
257   * an appropriate error in the logfile.
258   */
259  SSL_CTX_set_verify_depth(verify_depth + 1);
260
261  /*
262   * Set up the SSL specific data into "mydata" and store it into th SSL
263   * structure.
264   */
265  mydata.verify_depth = verify_depth; ...
266  SSL_set_ex_data(ssl, mydata_index, &mydata);
267
268  ...
269  SSL_accept(ssl);       /* check of success left out for clarity */
270  if (peer = SSL_get_peer_certificate(ssl))
271  {
272    if (SSL_get_verify_result(ssl) == X509_V_OK)
273    {
274      /* The client sent a certificate which verified OK */
275    }
276  }
277
278 =head1 SEE ALSO
279
280 L<ssl(7)>, L<SSL_new(3)>,
281 L<SSL_CTX_get_verify_mode(3)>,
282 L<SSL_get_verify_result(3)>,
283 L<SSL_CTX_load_verify_locations(3)>,
284 L<SSL_get_peer_certificate(3)>,
285 L<SSL_CTX_set_cert_verify_callback(3)>,
286 L<SSL_get_ex_data_X509_STORE_CTX_idx(3)>,
287 L<SSL_get_ex_new_index(3)>
288
289 =head1 COPYRIGHT
290
291 Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
292
293 Licensed under the OpenSSL license (the "License").  You may not use
294 this file except in compliance with the License.  You can obtain a copy
295 in the file LICENSE in the source distribution or at
296 L<https://www.openssl.org/source/license.html>.
297
298 =cut