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