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