Blame doc/man3/SSL_CTX_set_verify.pod

Packit Service 084de1
=pod
Packit Service 084de1
Packit Service 084de1
=head1 NAME
Packit Service 084de1
Packit Service 084de1
SSL_get_ex_data_X509_STORE_CTX_idx,
Packit Service 084de1
SSL_CTX_set_verify, SSL_set_verify,
Packit Service 084de1
SSL_CTX_set_verify_depth, SSL_set_verify_depth,
Packit Service 084de1
SSL_verify_cb,
Packit Service 084de1
SSL_verify_client_post_handshake,
Packit Service 084de1
SSL_set_post_handshake_auth,
Packit Service 084de1
SSL_CTX_set_post_handshake_auth
Packit Service 084de1
- set peer certificate verification parameters
Packit Service 084de1
Packit Service 084de1
=head1 SYNOPSIS
Packit Service 084de1
Packit Service 084de1
 #include <openssl/ssl.h>
Packit Service 084de1
Packit Service 084de1
 typedef int (*SSL_verify_cb)(int preverify_ok, X509_STORE_CTX *x509_ctx);
Packit Service 084de1
Packit Service 084de1
 void SSL_CTX_set_verify(SSL_CTX *ctx, int mode, SSL_verify_cb verify_callback);
Packit Service 084de1
 void SSL_set_verify(SSL *ssl, int mode, SSL_verify_cb verify_callback);
Packit Service 084de1
 SSL_get_ex_data_X509_STORE_CTX_idx(void);
Packit Service 084de1
Packit Service 084de1
 void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth);
Packit Service 084de1
 void SSL_set_verify_depth(SSL *ssl, int depth);
Packit Service 084de1
Packit Service 084de1
 int SSL_verify_client_post_handshake(SSL *ssl);
Packit Service 084de1
 void SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val);
Packit Service 084de1
 void SSL_set_post_handshake_auth(SSL *ssl, int val);
Packit Service 084de1
Packit Service 084de1
=head1 DESCRIPTION
Packit Service 084de1
Packit Service 084de1
SSL_CTX_set_verify() sets the verification flags for B<ctx> to be B<mode> and
Packit Service 084de1
specifies the B<verify_callback> function to be used. If no callback function
Packit Service 084de1
shall be specified, the NULL pointer can be used for B<verify_callback>.
Packit Service 084de1
Packit Service 084de1
SSL_set_verify() sets the verification flags for B<ssl> to be B<mode> and
Packit Service 084de1
specifies the B<verify_callback> function to be used. If no callback function
Packit Service 084de1
shall be specified, the NULL pointer can be used for B<verify_callback>. In
Packit Service 084de1
this case last B<verify_callback> set specifically for this B<ssl> remains. If
Packit Service 084de1
no special B<callback> was set before, the default callback for the underlying
Packit Service 084de1
B<ctx> is used, that was valid at the time B<ssl> was created with
Packit Service 084de1
L<SSL_new(3)>. Within the callback function,
Packit Service 084de1
B<SSL_get_ex_data_X509_STORE_CTX_idx> can be called to get the data index
Packit Service 084de1
of the current SSL object that is doing the verification.
Packit Service 084de1
Packit Service 084de1
SSL_CTX_set_verify_depth() sets the maximum B<depth> for the certificate chain
Packit Service 084de1
verification that shall be allowed for B<ctx>.
Packit Service 084de1
Packit Service 084de1
SSL_set_verify_depth() sets the maximum B<depth> for the certificate chain
Packit Service 084de1
verification that shall be allowed for B<ssl>.
Packit Service 084de1
Packit Service 084de1
SSL_CTX_set_post_handshake_auth() and SSL_set_post_handshake_auth() enable the
Packit Service 084de1
Post-Handshake Authentication extension to be added to the ClientHello such that
Packit Service 084de1
post-handshake authentication can be requested by the server. If B<val> is 0
Packit Service 084de1
then the extension is not sent, otherwise it is. By default the extension is not
Packit Service 084de1
sent. A certificate callback will need to be set via
Packit Service 084de1
SSL_CTX_set_client_cert_cb() if no certificate is provided at initialization.
Packit Service 084de1
Packit Service 084de1
SSL_verify_client_post_handshake() causes a CertificateRequest message to be
Packit Service 084de1
sent by a server on the given B<ssl> connection. The SSL_VERIFY_PEER flag must
Packit Service 084de1
be set; the SSL_VERIFY_POST_HANDSHAKE flag is optional.
Packit Service 084de1
Packit Service 084de1
=head1 NOTES
Packit Service 084de1
Packit Service 084de1
The verification of certificates can be controlled by a set of logically
Packit Service 084de1
or'ed B<mode> flags:
Packit Service 084de1
Packit Service 084de1
=over 4
Packit Service 084de1
Packit Service 084de1
=item SSL_VERIFY_NONE
Packit Service 084de1
Packit Service 084de1
B<Server mode:> the server will not send a client certificate request to the
Packit Service 084de1
client, so the client will not send a certificate.
Packit Service 084de1
Packit Service 084de1
B<Client mode:> if not using an anonymous cipher (by default disabled), the
Packit Service 084de1
server will send a certificate which will be checked. The result of the
Packit Service 084de1
certificate verification process can be checked after the TLS/SSL handshake
Packit Service 084de1
using the L<SSL_get_verify_result(3)> function.
Packit Service 084de1
The handshake will be continued regardless of the verification result.
Packit Service 084de1
Packit Service 084de1
=item SSL_VERIFY_PEER
Packit Service 084de1
Packit Service 084de1
B<Server mode:> the server sends a client certificate request to the client.
Packit Service 084de1
The certificate returned (if any) is checked. If the verification process
Packit Service 084de1
fails, the TLS/SSL handshake is
Packit Service 084de1
immediately terminated with an alert message containing the reason for
Packit Service 084de1
the verification failure.
Packit Service 084de1
The behaviour can be controlled by the additional
Packit Service 084de1
SSL_VERIFY_FAIL_IF_NO_PEER_CERT, SSL_VERIFY_CLIENT_ONCE and
Packit Service 084de1
SSL_VERIFY_POST_HANDSHAKE flags.
Packit Service 084de1
Packit Service 084de1
B<Client mode:> the server certificate is verified. If the verification process
Packit Service 084de1
fails, the TLS/SSL handshake is
Packit Service 084de1
immediately terminated with an alert message containing the reason for
Packit Service 084de1
the verification failure. If no server certificate is sent, because an
Packit Service 084de1
anonymous cipher is used, SSL_VERIFY_PEER is ignored.
Packit Service 084de1
Packit Service 084de1
=item SSL_VERIFY_FAIL_IF_NO_PEER_CERT
Packit Service 084de1
Packit Service 084de1
B<Server mode:> if the client did not return a certificate, the TLS/SSL
Packit Service 084de1
handshake is immediately terminated with a "handshake failure" alert.
Packit Service 084de1
This flag must be used together with SSL_VERIFY_PEER.
Packit Service 084de1
Packit Service 084de1
B<Client mode:> ignored (see BUGS)
Packit Service 084de1
Packit Service 084de1
=item SSL_VERIFY_CLIENT_ONCE
Packit Service 084de1
Packit Service 084de1
B<Server mode:> only request a client certificate once during the
Packit Service 084de1
connection. Do not ask for a client certificate again during
Packit Service 084de1
renegotiation or post-authentication if a certificate was requested
Packit Service 084de1
during the initial handshake. This flag must be used together with
Packit Service 084de1
SSL_VERIFY_PEER.
Packit Service 084de1
Packit Service 084de1
B<Client mode:> ignored (see BUGS)
Packit Service 084de1
Packit Service 084de1
=item SSL_VERIFY_POST_HANDSHAKE
Packit Service 084de1
Packit Service 084de1
B<Server mode:> the server will not send a client certificate request
Packit Service 084de1
during the initial handshake, but will send the request via
Packit Service 084de1
SSL_verify_client_post_handshake(). This allows the SSL_CTX or SSL
Packit Service 084de1
to be configured for post-handshake peer verification before the
Packit Service 084de1
handshake occurs. This flag must be used together with
Packit Service 084de1
SSL_VERIFY_PEER. TLSv1.3 only; no effect on pre-TLSv1.3 connections.
Packit Service 084de1
Packit Service 084de1
B<Client mode:> ignored (see BUGS)
Packit Service 084de1
Packit Service 084de1
=back
Packit Service 084de1
Packit Service 084de1
If the B<mode> is SSL_VERIFY_NONE none of the other flags may be set.
Packit Service 084de1
Packit Service 084de1
The actual verification procedure is performed either using the built-in
Packit Service 084de1
verification procedure or using another application provided verification
Packit Service 084de1
function set with
Packit Service 084de1
L<SSL_CTX_set_cert_verify_callback(3)>.
Packit Service 084de1
The following descriptions apply in the case of the built-in procedure. An
Packit Service 084de1
application provided procedure also has access to the verify depth information
Packit Service 084de1
and the verify_callback() function, but the way this information is used
Packit Service 084de1
may be different.
Packit Service 084de1
Packit Service 084de1
SSL_CTX_set_verify_depth() and SSL_set_verify_depth() set a limit on the
Packit Service 084de1
number of certificates between the end-entity and trust-anchor certificates.
Packit Service 084de1
Neither the
Packit Service 084de1
end-entity nor the trust-anchor certificates count against B<depth>. If the
Packit Service 084de1
certificate chain needed to reach a trusted issuer is longer than B<depth+2>,
Packit Service 084de1
X509_V_ERR_CERT_CHAIN_TOO_LONG will be issued.
Packit Service 084de1
The depth count is "level 0:peer certificate", "level 1: CA certificate",
Packit Service 084de1
"level 2: higher level CA certificate", and so on. Setting the maximum
Packit Service 084de1
depth to 2 allows the levels 0, 1, 2 and 3 (0 being the end-entity and 3 the
Packit Service 084de1
trust-anchor).
Packit Service 084de1
The default depth limit is 100,
Packit Service 084de1
allowing for the peer certificate, at most 100 intermediate CA certificates and
Packit Service 084de1
a final trust anchor certificate.
Packit Service 084de1
Packit Service 084de1
The B<verify_callback> function is used to control the behaviour when the
Packit Service 084de1
SSL_VERIFY_PEER flag is set. It must be supplied by the application and
Packit Service 084de1
receives two arguments: B<preverify_ok> indicates, whether the verification of
Packit Service 084de1
the certificate in question was passed (preverify_ok=1) or not
Packit Service 084de1
(preverify_ok=0). B<x509_ctx> is a pointer to the complete context used
Packit Service 084de1
for the certificate chain verification.
Packit Service 084de1
Packit Service 084de1
The certificate chain is checked starting with the deepest nesting level
Packit Service 084de1
(the root CA certificate) and worked upward to the peer's certificate.
Packit Service 084de1
At each level signatures and issuer attributes are checked. Whenever
Packit Service 084de1
a verification error is found, the error number is stored in B<x509_ctx>
Packit Service 084de1
and B<verify_callback> is called with B<preverify_ok>=0. By applying
Packit Service 084de1
X509_CTX_store_* functions B<verify_callback> can locate the certificate
Packit Service 084de1
in question and perform additional steps (see EXAMPLES). If no error is
Packit Service 084de1
found for a certificate, B<verify_callback> is called with B<preverify_ok>=1
Packit Service 084de1
before advancing to the next level.
Packit Service 084de1
Packit Service 084de1
The return value of B<verify_callback> controls the strategy of the further
Packit Service 084de1
verification process. If B<verify_callback> returns 0, the verification
Packit Service 084de1
process is immediately stopped with "verification failed" state. If
Packit Service 084de1
SSL_VERIFY_PEER is set, a verification failure alert is sent to the peer and
Packit Service 084de1
the TLS/SSL handshake is terminated. If B<verify_callback> returns 1,
Packit Service 084de1
the verification process is continued. If B<verify_callback> always returns
Packit Service 084de1
1, the TLS/SSL handshake will not be terminated with respect to verification
Packit Service 084de1
failures and the connection will be established. The calling process can
Packit Service 084de1
however retrieve the error code of the last verification error using
Packit Service 084de1
L<SSL_get_verify_result(3)> or by maintaining its
Packit Service 084de1
own error storage managed by B<verify_callback>.
Packit Service 084de1
Packit Service 084de1
If no B<verify_callback> is specified, the default callback will be used.
Packit Service 084de1
Its return value is identical to B<preverify_ok>, so that any verification
Packit Service 084de1
failure will lead to a termination of the TLS/SSL handshake with an
Packit Service 084de1
alert message, if SSL_VERIFY_PEER is set.
Packit Service 084de1
Packit Service 084de1
After calling SSL_set_post_handshake_auth(), the client will need to add a
Packit Service 084de1
certificate or certificate callback to its configuration before it can
Packit Service 084de1
successfully authenticate. This must be called before SSL_connect().
Packit Service 084de1
Packit Service 084de1
SSL_verify_client_post_handshake() requires that verify flags have been
Packit Service 084de1
previously set, and that a client sent the post-handshake authentication
Packit Service 084de1
extension. When the client returns a certificate the verify callback will be
Packit Service 084de1
invoked. A write operation must take place for the Certificate Request to be
Packit Service 084de1
sent to the client, this can be done with SSL_do_handshake() or SSL_write_ex().
Packit Service 084de1
Only one certificate request may be outstanding at any time.
Packit Service 084de1
Packit Service 084de1
When post-handshake authentication occurs, a refreshed NewSessionTicket
Packit Service 084de1
message is sent to the client.
Packit Service 084de1
Packit Service 084de1
=head1 BUGS
Packit Service 084de1
Packit Service 084de1
In client mode, it is not checked whether the SSL_VERIFY_PEER flag
Packit Service 084de1
is set, but whether any flags other than SSL_VERIFY_NONE are set. This can
Packit Service 084de1
lead to unexpected behaviour if SSL_VERIFY_PEER and other flags are not used as
Packit Service 084de1
required.
Packit Service 084de1
Packit Service 084de1
=head1 RETURN VALUES
Packit Service 084de1
Packit Service 084de1
The SSL*_set_verify*() functions do not provide diagnostic information.
Packit Service 084de1
Packit Service 084de1
The SSL_verify_client_post_handshake() function returns 1 if the request
Packit Service 084de1
succeeded, and 0 if the request failed. The error stack can be examined
Packit Service 084de1
to determine the failure reason.
Packit Service 084de1
Packit Service 084de1
=head1 EXAMPLES
Packit Service 084de1
Packit Service 084de1
The following code sequence realizes an example B<verify_callback> function
Packit Service 084de1
that will always continue the TLS/SSL handshake regardless of verification
Packit Service 084de1
failure, if wished. The callback realizes a verification depth limit with
Packit Service 084de1
more informational output.
Packit Service 084de1
Packit Service 084de1
All verification errors are printed; information about the certificate chain
Packit Service 084de1
is printed on request.
Packit Service 084de1
The example is realized for a server that does allow but not require client
Packit Service 084de1
certificates.
Packit Service 084de1
Packit Service 084de1
The example makes use of the ex_data technique to store application data
Packit Service 084de1
into/retrieve application data from the SSL structure
Packit Service 084de1
(see L<CRYPTO_get_ex_new_index(3)>,
Packit Service 084de1
L<SSL_get_ex_data_X509_STORE_CTX_idx(3)>).
Packit Service 084de1
Packit Service 084de1
 ...
Packit Service 084de1
 typedef struct {
Packit Service 084de1
   int verbose_mode;
Packit Service 084de1
   int verify_depth;
Packit Service 084de1
   int always_continue;
Packit Service 084de1
 } mydata_t;
Packit Service 084de1
 int mydata_index;
Packit Service 084de1
Packit Service 084de1
 ...
Packit Service 084de1
 static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
Packit Service 084de1
 {
Packit Service 084de1
     char    buf[256];
Packit Service 084de1
     X509   *err_cert;
Packit Service 084de1
     int     err, depth;
Packit Service 084de1
     SSL    *ssl;
Packit Service 084de1
     mydata_t *mydata;
Packit Service 084de1
Packit Service 084de1
     err_cert = X509_STORE_CTX_get_current_cert(ctx);
Packit Service 084de1
     err = X509_STORE_CTX_get_error(ctx);
Packit Service 084de1
     depth = X509_STORE_CTX_get_error_depth(ctx);
Packit Service 084de1
Packit Service 084de1
     /*
Packit Service 084de1
      * Retrieve the pointer to the SSL of the connection currently treated
Packit Service 084de1
      * and the application specific data stored into the SSL object.
Packit Service 084de1
      */
Packit Service 084de1
     ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Packit Service 084de1
     mydata = SSL_get_ex_data(ssl, mydata_index);
Packit Service 084de1
Packit Service 084de1
     X509_NAME_oneline(X509_get_subject_name(err_cert), buf, 256);
Packit Service 084de1
Packit Service 084de1
     /*
Packit Service 084de1
      * Catch a too long certificate chain. The depth limit set using
Packit Service 084de1
      * SSL_CTX_set_verify_depth() is by purpose set to "limit+1" so
Packit Service 084de1
      * that whenever the "depth>verify_depth" condition is met, we
Packit Service 084de1
      * have violated the limit and want to log this error condition.
Packit Service 084de1
      * We must do it here, because the CHAIN_TOO_LONG error would not
Packit Service 084de1
      * be found explicitly; only errors introduced by cutting off the
Packit Service 084de1
      * additional certificates would be logged.
Packit Service 084de1
      */
Packit Service 084de1
     if (depth > mydata->verify_depth) {
Packit Service 084de1
         preverify_ok = 0;
Packit Service 084de1
         err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
Packit Service 084de1
         X509_STORE_CTX_set_error(ctx, err);
Packit Service 084de1
     }
Packit Service 084de1
     if (!preverify_ok) {
Packit Service 084de1
         printf("verify error:num=%d:%s:depth=%d:%s\n", err,
Packit Service 084de1
                X509_verify_cert_error_string(err), depth, buf);
Packit Service 084de1
     } else if (mydata->verbose_mode) {
Packit Service 084de1
         printf("depth=%d:%s\n", depth, buf);
Packit Service 084de1
     }
Packit Service 084de1
Packit Service 084de1
     /*
Packit Service 084de1
      * At this point, err contains the last verification error. We can use
Packit Service 084de1
      * it for something special
Packit Service 084de1
      */
Packit Service 084de1
     if (!preverify_ok && (err == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT)) {
Packit Service 084de1
         X509_NAME_oneline(X509_get_issuer_name(err_cert), buf, 256);
Packit Service 084de1
         printf("issuer= %s\n", buf);
Packit Service 084de1
     }
Packit Service 084de1
Packit Service 084de1
     if (mydata->always_continue)
Packit Service 084de1
         return 1;
Packit Service 084de1
     else
Packit Service 084de1
         return preverify_ok;
Packit Service 084de1
 }
Packit Service 084de1
 ...
Packit Service 084de1
Packit Service 084de1
 mydata_t mydata;
Packit Service 084de1
Packit Service 084de1
 ...
Packit Service 084de1
 mydata_index = SSL_get_ex_new_index(0, "mydata index", NULL, NULL, NULL);
Packit Service 084de1
Packit Service 084de1
 ...
Packit Service 084de1
 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
Packit Service 084de1
                    verify_callback);
Packit Service 084de1
Packit Service 084de1
 /*
Packit Service 084de1
  * Let the verify_callback catch the verify_depth error so that we get
Packit Service 084de1
  * an appropriate error in the logfile.
Packit Service 084de1
  */
Packit Service 084de1
 SSL_CTX_set_verify_depth(verify_depth + 1);
Packit Service 084de1
Packit Service 084de1
 /*
Packit Service 084de1
  * Set up the SSL specific data into "mydata" and store it into th SSL
Packit Service 084de1
  * structure.
Packit Service 084de1
  */
Packit Service 084de1
 mydata.verify_depth = verify_depth; ...
Packit Service 084de1
 SSL_set_ex_data(ssl, mydata_index, &mydata);
Packit Service 084de1
Packit Service 084de1
 ...
Packit Service 084de1
 SSL_accept(ssl);       /* check of success left out for clarity */
Packit Service 084de1
 if (peer = SSL_get_peer_certificate(ssl)) {
Packit Service 084de1
     if (SSL_get_verify_result(ssl) == X509_V_OK) {
Packit Service 084de1
         /* The client sent a certificate which verified OK */
Packit Service 084de1
     }
Packit Service 084de1
 }
Packit Service 084de1
Packit Service 084de1
=head1 SEE ALSO
Packit Service 084de1
Packit Service 084de1
L<ssl(7)>, L<SSL_new(3)>,
Packit Service 084de1
L<SSL_CTX_get_verify_mode(3)>,
Packit Service 084de1
L<SSL_get_verify_result(3)>,
Packit Service 084de1
L<SSL_CTX_load_verify_locations(3)>,
Packit Service 084de1
L<SSL_get_peer_certificate(3)>,
Packit Service 084de1
L<SSL_CTX_set_cert_verify_callback(3)>,
Packit Service 084de1
L<SSL_get_ex_data_X509_STORE_CTX_idx(3)>,
Packit Service 084de1
L<SSL_CTX_set_client_cert_cb(3)>,
Packit Service 084de1
L<CRYPTO_get_ex_new_index(3)>
Packit Service 084de1
Packit Service 084de1
=head1 HISTORY
Packit Service 084de1
Packit Service 084de1
The SSL_VERIFY_POST_HANDSHAKE option, and the SSL_verify_client_post_handshake()
Packit Service 084de1
and SSL_set_post_handshake_auth() functions were added in OpenSSL 1.1.1.
Packit Service 084de1
Packit Service 084de1
=head1 COPYRIGHT
Packit Service 084de1
Packit Service 084de1
Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
Packit Service 084de1
Packit Service 084de1
Licensed under the OpenSSL license (the "License").  You may not use
Packit Service 084de1
this file except in compliance with the License.  You can obtain a copy
Packit Service 084de1
in the file LICENSE in the source distribution or at
Packit Service 084de1
L<https://www.openssl.org/source/license.html>.
Packit Service 084de1
Packit Service 084de1
=cut