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