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