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