5 SSL_CTX_set_verify, SSL_set_verify, SSL_CTX_set_verify_depth, SSL_set_verify_depth - set peer certificate verification parameters
9 #include <openssl/ssl.h>
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);
18 int verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx);
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>.
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 the time B<ssl> was created with
32 L<SSL_new(3)|SSL_new(3)>.
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.)
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.)
42 The verification of certificates can be controlled by a set of logically
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.
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)|SSL_get_verify_result(3)> function.
56 The handshake will be continued regardless of the verification result.
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 as indicated by B<verify_callback>, 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.
68 B<Client mode:> the server certificate is verified. If the verification process
69 fails as indicated by B<verify_callback>, 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.
74 =item SSL_VERIFY_FAIL_IF_NO_PEER_CERT
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.
80 B<Client mode:> ignored
82 =item SSL_VERIFY_CLIENT_ONCE
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.
88 B<Client mode:> ignored
92 Exactly one of the B<mode> flags SSL_VERIFY_NONE and SSL_VERIFY_PEER must be
95 SSL_CTX_set_verify_depth() and SSL_set_verify_depth() set the limit up
96 to which depth certificates in a chain are used during the verification
97 procedure. If the certificate chain is longer than allowed, the certificates
98 above the limit are ignored. Error messages are generated as if these
99 certificates would not be present, most likely a
100 X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY will be issued.
101 The depth count is "level 0:peer certificate", "level 1: CA certificate",
102 "level 2: higher level CA certificate", and so on. Setting the maximum
103 depth to 2 allows the levels 0, 1, and 2. The default depth limit is 9,
104 allowing for the peer certificate and additional 9 CA certificates.
106 The B<verify_callback> function is used to control the behaviour when the
107 SSL_VERIFY_PEER flag is set. It must be supplied by the application and
108 receives two arguments: B<preverify_ok> indicates, whether the verification of
109 the certificate in question was passed (preverify_ok=1) or not
110 (preverify_ok=0). B<x509_ctx> is a pointer to the complete context used
111 for the certificate chain verification.
113 The certificate chain is checked starting with the deepest nesting level
114 (the root CA certificate) and worked upward to the peer's certificate.
115 At each level signatures and issuer attributes are checked. Whenever
116 a verification error is found, the error number is stored in B<x509_ctx>
117 and B<verify_callback> is called with B<preverify_ok>=0. By applying
118 X509_CTX_store_* functions B<verify_callback> can locate the certificate
119 in question and perform additional steps (see EXAMPLES). If no error is
120 found for a certificate, B<verify_callback> is called with B<preverify_ok>=1
121 before advancing to the next level.
123 The return value of B<verify_callback> controls the strategy of the further
124 verification process. If B<verify_callback> returns 0, the verification
125 process is immediately stopped with "verification failed" state. If
126 SSL_VERIFY_PEER is set, a verification failure alert is sent to the peer and
127 the TLS/SSL handshake is terminated. If B<verify_callback> returns 1,
128 the verification process is continued. If B<verify_callback> always returns
129 1, the TLS/SSL handshake will never be terminated because of this application
130 experiencing a verification failure. The calling process can however
131 retrieve the error code of the last verification error using
132 L<SSL_get_verify_result(3)|SSL_get_verify_result(3)> or by maintaining its
133 own error storage managed by B<verify_callback>.
135 If no B<verify_callback> is specified, the default callback will be used.
136 Its return value is identical to B<preverify_ok>, so that any verification
137 failure will lead to a termination of the TLS/SSL handshake with an
138 alert message, if SSL_VERIFY_PEER is set.
142 In client mode, it is not checked whether the SSL_VERIFY_PEER flag
143 is set, but whether SSL_VERIFY_NONE is not set. This can lead to
144 unexpected behaviour, if the SSL_VERIFY_PEER and SSL_VERIFY_NONE are not
145 used as required (exactly one must be set at any time).
147 The certificate verification depth set with SSL[_CTX]_verify_depth()
148 stops the verification at a certain depth. The error message produced
149 will be that of an incomplete certificate chain and not
150 X509_V_ERR_CERT_CHAIN_TOO_LONG as may be expected.
154 The SSL*_set_verify*() functions do not provide diagnostic information.
158 The following code sequence realizes an example B<verify_callback> function
159 that will always continue the TLS/SSL handshake regardless of verification
160 failure, if wished. The callback realizes a verification depth limit with
161 more informational output.
163 All verification errors are printed, informations about the certificate chain
164 are printed on request.
165 The example is realized for a server that does allow but not require client
173 static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
179 err_cert = X509_STORE_CTX_get_current_cert(ctx);
180 err = X509_STORE_CTX_get_error(ctx);
181 depth = X509_STORE_CTX_get_error_depth(ctx);
183 X509_NAME_oneline(X509_get_subject_name(err_cert), buf, 256);
186 * Catch a too long certificate chain. The depth limit set using
187 * SSL_CTX_set_verify_depth() is by purpose set to "limit+1" so
188 * that whenever the "depth>verify_depth" condition is met, we
189 * have violated the limit and want to log this error condition.
190 * We must do it here, because the CHAIN_TOO_LONG error would not
191 * be found explicitly; only errors introduced by cutting off the
192 * additional certificates would be logged.
194 if (depth > verify_depth) {
196 err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
197 X509_STORE_CTX_set_error(ctx, err);
200 printf("verify error:num=%d:%s:depth=%d:%s\n", err,
201 X509_verify_cert_error_string(err), depth, buf);
203 else if (verbose_mode)
205 printf("depth=%d:%s\n", depth, buf);
209 * At this point, err contains the last verification error. We can use
210 * it for something special
212 if (!preverify_ok && (err == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT)
214 X509_NAME_oneline(X509_get_issuer_name(ctx->current_cert), buf, 256);
215 printf("issuer= %s\n", buf);
224 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
228 * Let the verify_callback catch the verify_depth error so that we get
229 * an appropriate error in the logfile.
231 SSL_CTX_set_verify_depth(verify_depth + 1);
234 SSL_accept(ssl); /* check of success left out for clarity */
235 if (peer = SSL_get_peer_certificate(ssl))
237 if (SSL_get_verify_result(ssl) == X509_V_OK)
239 /* The client sent a certificate which verified OK */
245 L<ssl(3)|ssl(3)>, L<SSL_new(3)|SSL_new(3)>,
246 L<SSL_CTX_get_verify_mode(3)|SSL_CTX_get_verify_mode(3)>,
247 L<SSL_get_verify_result(3)|SSL_get_verify_result(3)>,
248 L<SSL_CTX_load_verify_locations(3)|SSL_CTX_load_verify_locations(3)>,
249 L<SSL_get_peer_certificate(3)|SSL_get_peer_certificate(3)>