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