Debian Codegraph @

src/verify.c

raw
1 /*
2 * stunnel TLS offloading and load-balancing proxy
3 * Copyright (C) 1998-2026 Michal Trojnara <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses>.
17 *
18 * Linking stunnel statically or dynamically with other modules is making
19 * a combined work based on stunnel. Thus, the terms and conditions of
20 * the GNU General Public License cover the whole combination.
21 *
22 * In addition, as a special exception, the copyright holder of stunnel
23 * gives you permission to combine stunnel with free software programs or
24 * libraries that are released under the GNU LGPL and with code included
25 * in the standard release of OpenSSL under the OpenSSL License (or
26 * modified versions of such code, with unchanged license). You may copy
27 * and distribute such a system following the terms of the GNU GPL for
28 * stunnel and the licenses of the other code concerned.
29 *
30 * Note that people who make modified versions of stunnel are not obligated
31 * to grant this special exception for their modified versions; it is their
32 * choice whether to do so. The GNU General Public License gives permission
33 * to release a modified version without this exception; this exception
34 * also makes it possible to release a modified version which carries
35 * forward this exception.
36 */
37
38 #include "prototypes.h"
39
40 /**************************************** prototypes */
41
42 /* verify initialization */
43 NOEXPORT int init_ca(SERVICE_OPTIONS *section);
44 NOEXPORT int init_crl(SERVICE_OPTIONS *section);
45 NOEXPORT int load_file_lookup(X509_STORE *, char *);
46 NOEXPORT int add_dir_lookup(X509_STORE *, char *);
47 NOEXPORT void auth_warnings(SERVICE_OPTIONS *);
48
49 /* verify callback */
50 NOEXPORT int verify_callback(int, X509_STORE_CTX *);
51 NOEXPORT int verify_checks(CLI *, int, X509_STORE_CTX *);
52 NOEXPORT int cert_check(CLI *, X509_STORE_CTX *, int);
53 #if OPENSSL_VERSION_NUMBER>=0x10002000L
54 NOEXPORT int cert_check_subject(CLI *, X509_STORE_CTX *);
55 #endif /* OPENSSL_VERSION_NUMBER>=0x10002000L */
56 NOEXPORT int cert_check_local(X509_STORE_CTX *);
57 #if OPENSSL_VERSION_NUMBER>=0x40000000L
58 NOEXPORT int pubkey_eq(const X509 *, const X509 *);
59 #else /* OpenSSL 4.0.0 or later */
60 NOEXPORT int pubkey_eq(X509 *, X509 *);
61 #endif /* OpenSSL 4.0.0 or later */
62
63 /**************************************** verify initialization */
64
65 int verify_init(SERVICE_OPTIONS *section) {
66 int verify_mode=0;
67
68 if(init_ca(section)) {
69 if(section->option.verify_chain || section->option.verify_peer) {
70 s_log(LOG_ERR, "No trusted certificates found");
71 return 1; /* FAILED */
72 }
73 s_log(LOG_INFO, "No trusted certificates found");
74 }
75 if(init_crl(section))
76 return 1; /* FAILED */
77
78 /* verify callback setup */
79 if(section->option.request_cert) {
80 verify_mode|=SSL_VERIFY_PEER;
81 if(section->option.require_cert && !section->redirect_addr.names)
82 verify_mode|=SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
83 }
84 SSL_CTX_set_verify(section->ctx, verify_mode, verify_callback);
85
86 auth_warnings(section);
87
88 return 0; /* OK */
89 }
90
91 NOEXPORT int init_ca(SERVICE_OPTIONS *section) {
92 STACK_OF(X509_NAME) *ca_dn=NULL;
93 #ifndef OPENSSL_NO_ENGINE
94 NAME_LIST *ptr;
95 #endif
96 /* CA initialization with the file and/or directory */
97 if(section->ca_file || section->ca_dir) {
98 if(!SSL_CTX_load_verify_locations(section->ctx,
99 section->ca_file, section->ca_dir)) {
100 ssl_error(NULL, "SSL_CTX_load_verify_locations");
101 }
102 }
103 #if OPENSSL_VERSION_NUMBER>=0x30000000L
104 if(section->ca_store) {
105 if(!SSL_CTX_load_verify_store(section->ctx, section->ca_store)) {
106 ssl_error(NULL, "SSL_CTX_load_verify_store");
107 }
108 }
109 #endif /* OPENSSL_VERSION_NUMBER>=0x30000000L */
110
111 ca_dn=sk_X509_NAME_new_null();
112
113 #ifndef OPENSSL_NO_ENGINE
114 /* CA and client CA list initialization with the engine */
115 for(ptr=section->ca_engine; ptr; ptr=ptr->next) {
116 X509 *cert=engine_get_cert(section->engine, ptr->name);
117 if(cert) {
118 X509_STORE_add_cert(SSL_CTX_get_cert_store(section->ctx), cert);
119 sk_X509_NAME_push(ca_dn,
120 X509_NAME_dup(X509_get_subject_name(cert)));
121 X509_free(cert);
122 } else {
123 s_log(LOG_ERR, "CAengine failed to retrieve \"%s\"", ptr->name);
124 }
125 }
126 #endif
127
128 /* client CA list initialization with the file and/or directory */
129 if(section->ca_file)
130 SSL_add_file_cert_subjects_to_stack(ca_dn, section->ca_file);
131 if(section->ca_dir)
132 SSL_add_dir_cert_subjects_to_stack(ca_dn, section->ca_dir);
133 #if OPENSSL_VERSION_NUMBER>=0x30000000L
134 if(section->ca_store)
135 SSL_add_store_cert_subjects_to_stack(ca_dn, section->ca_store);
136 #endif /* OPENSSL_VERSION_NUMBER>=0x30000000L */
137
138 if(!sk_X509_NAME_num(ca_dn)) {
139 sk_X509_NAME_pop_free(ca_dn, X509_NAME_free);
140 return 1; /* FAILED */
141 }
142
143 if(section->option.client) {
144 print_CA_list("Configured trusted server CA", ca_dn);
145 sk_X509_NAME_pop_free(ca_dn, X509_NAME_free);
146 } else { /* only set the client CA list on the server */
147 print_CA_list("Configured trusted client CA", ca_dn);
148 SSL_CTX_set_client_CA_list(section->ctx, ca_dn);
149 }
150
151 return 0; /* OK */
152 }
153
154 NOEXPORT int init_crl(SERVICE_OPTIONS *section) {
155 X509_STORE *store;
156
157 if(!section->crl_file && !section->crl_dir)
158 return 0; /* OK (nothing to initialize) */
159
160 store=SSL_CTX_get_cert_store(section->ctx);
161 if(section->crl_file) {
162 if(load_file_lookup(store, section->crl_file))
163 return 1; /* FAILED */
164 }
165 if(section->crl_dir) {
166 #if OPENSSL_VERSION_NUMBER<0x10100000L
167 /* do not cache CRLs (only required with OpenSSL version < 1.0.0) */
168 store->cache=0;
169 #endif
170 if(add_dir_lookup(store, section->crl_dir))
171 return 1; /* FAILED */
172 }
173 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK);
174 return 0; /* OK */
175 }
176
177 NOEXPORT int load_file_lookup(X509_STORE *store, char *name) {
178 X509_LOOKUP *lookup;
179
180 lookup=X509_STORE_add_lookup(store, X509_LOOKUP_file());
181 if(!lookup) {
182 ssl_error(NULL, "X509_STORE_add_lookup(X509_LOOKUP_file)");
183 return 1; /* FAILED */
184 }
185 if(!X509_load_crl_file(lookup, name, X509_FILETYPE_PEM)) {
186 s_log(LOG_ERR, "Failed to load %s revocation lookup file", name);
187 ssl_error(NULL, "X509_load_crl_file");
188 return 1; /* FAILED */
189 }
190 s_log(LOG_DEBUG, "Loaded %s revocation lookup file", name);
191 return 0; /* OK */
192 }
193
194 NOEXPORT int add_dir_lookup(X509_STORE *store, char *name) {
195 X509_LOOKUP *lookup;
196
197 lookup=X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
198 if(!lookup) {
199 ssl_error(NULL, "X509_STORE_add_lookup(X509_LOOKUP_hash_dir)");
200 return 1; /* FAILED */
201 }
202 if(!X509_LOOKUP_add_dir(lookup, name, X509_FILETYPE_PEM)) {
203 s_log(LOG_ERR, "Failed to add %s revocation lookup directory", name);
204 ssl_error(NULL, "X509_LOOKUP_add_dir");
205 return 1; /* FAILED */
206 }
207 s_log(LOG_DEBUG, "Added %s revocation lookup directory", name);
208 return 0; /* OK */
209 }
210
211 /* issue warnings on insecure/missing authentication */
212 NOEXPORT void auth_warnings(SERVICE_OPTIONS *section) {
213 #ifndef OPENSSL_NO_PSK
214 if(section->psk_keys)
215 return;
216 #endif /* !defined(OPENSSL_NO_PSK) */
217 /* for servers it is usually okay to accept all client
218 certificates signed by a specified certificate authority */
219 if(!section->option.client)
220 return;
221 if(section->option.verify_peer) /* verify_peer does not depend on PKI */
222 return;
223 if(section->option.verify_chain) {
224 #if OPENSSL_VERSION_NUMBER>=0x10002000L
225 if(section->check_email || section->check_host || section->check_ip)
226 return;
227 #endif /* OPENSSL_VERSION_NUMBER>=0x10002000L */
228 s_log(LOG_WARNING,
229 "Service [%s] uses \"verifyChain\" without subject checks",
230 section->servname);
231 #if OPENSSL_VERSION_NUMBER<0x10002000L
232 s_log(LOG_WARNING,
233 "Rebuild your stunnel against OpenSSL version 1.0.2 or higher");
234 #endif /* OPENSSL_VERSION_NUMBER<0x10002000L */
235 s_log(LOG_WARNING,
236 "Use \"checkHost\" or \"checkIP\" to restrict trusted certificates");
237 return;
238 }
239 s_log(LOG_WARNING,
240 "Service [%s] needs authentication to prevent MITM attacks",
241 section->servname);
242 }
243
244 /**************************************** verify callback */
245
246 NOEXPORT int verify_callback(int preverify_ok, X509_STORE_CTX *callback_ctx) {
247 /* our verify callback function */
248 SSL *ssl;
249 CLI *c;
250
251 /* retrieve application specific data */
252 ssl=X509_STORE_CTX_get_ex_data(callback_ctx,
253 SSL_get_ex_data_X509_STORE_CTX_idx());
254 c=SSL_get_ex_data(ssl, index_ssl_cli);
255
256 if(!c->opt->option.verify_chain && !c->opt->option.verify_peer) {
257 s_log(LOG_INFO, "CERT: Certificate verification disabled");
258 return 1; /* accept */
259 }
260 if(verify_checks(c, preverify_ok, callback_ctx))
261 return 1; /* accept */
262 if(c->opt->option.connect_before_ssl)
263 return 0; /* reject */
264 if(c->opt->redirect_addr.names) {
265 SSL_SESSION *sess=SSL_get1_session(c->ssl);
266 if(!sess)
267 return 0; /* reject */
268 if(!SSL_SESSION_set_ex_data(sess,
269 index_session_authenticated, NULL)) {
270 ssl_error(c, "SSL_SESSION_set_ex_data");
271 SSL_SESSION_free(sess);
272 return 0; /* reject */
273 }
274 SSL_SESSION_free(sess);
275 return 1; /* accept */
276 }
277 return 0; /* reject */
278 }
279
280 NOEXPORT int verify_checks(CLI *c,
281 int preverify_ok, X509_STORE_CTX *callback_ctx) {
282 X509 *cert;
283 int depth;
284 char *subject;
285
286 cert=X509_STORE_CTX_get_current_cert(callback_ctx);
287 depth=X509_STORE_CTX_get_error_depth(callback_ctx);
288 subject=X509_NAME2text(X509_get_subject_name(cert));
289
290 s_log(LOG_DEBUG, "Verification started at depth=%d: %s", depth, subject);
291
292 if(!cert_check(c, callback_ctx, preverify_ok)) {
293 s_log(LOG_WARNING, "Rejected by CERT at depth=%d: %s", depth, subject);
294 str_free(subject);
295 return 0; /* reject */
296 }
297 #ifndef OPENSSL_NO_OCSP
298 if(!ocsp_check(c, callback_ctx)) {
299 s_log(LOG_WARNING, "Rejected by OCSP at depth=%d: %s", depth, subject);
300 str_free(subject);
301 return 0; /* reject */
302 }
303 #endif /* !defined(OPENSSL_NO_OCSP) */
304
305 s_log(depth ? LOG_INFO : LOG_NOTICE,
306 "Certificate accepted at depth=%d: %s", depth, subject);
307 str_free(subject);
308 return 1; /* accept */
309 }
310
311 /**************************************** certificate checking */
312
313 NOEXPORT int cert_check(CLI *c, X509_STORE_CTX *callback_ctx,
314 int preverify_ok) {
315 int err=X509_STORE_CTX_get_error(callback_ctx);
316 int depth=X509_STORE_CTX_get_error_depth(callback_ctx);
317
318 if(preverify_ok) {
319 s_log(LOG_DEBUG, "CERT: Pre-verification succeeded");
320 } else { /* remote site sent an invalid certificate */
321 if(c->opt->option.verify_chain || (depth==0 &&
322 err!=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY &&
323 err!=X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE &&
324 err!=X509_V_ERR_CERT_UNTRUSTED)) {
325 s_log(LOG_WARNING, "CERT: Pre-verification error: %s%s",
326 err == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN ||
327 err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT ?
328 "certificate not found in local repository: " : "",
329 X509_verify_cert_error_string(err));
330 /* retain the STORE_CTX error produced by pre-verification */
331 return 0; /* reject */
332 }
333 s_log(LOG_INFO, "CERT: Pre-verification error ignored: %s",
334 X509_verify_cert_error_string(err));
335 }
336
337 if(depth==0) { /* additional peer certificate checks */
338 #if OPENSSL_VERSION_NUMBER>=0x10002000L
339 if(!cert_check_subject(c, callback_ctx))
340 return 0; /* reject */
341 #endif /* OPENSSL_VERSION_NUMBER>=0x10002000L */
342 if(c->opt->option.verify_peer && !cert_check_local(callback_ctx))
343 return 0; /* reject */
344 }
345
346 return 1; /* accept */
347 }
348
349 #if OPENSSL_VERSION_NUMBER>=0x10002000L
350 NOEXPORT int cert_check_subject(CLI *c, X509_STORE_CTX *callback_ctx) {
351 X509 *cert=X509_STORE_CTX_get_current_cert(callback_ctx);
352 NAME_LIST *ptr;
353 char *peername=NULL;
354
355 if(!c->opt->check_host && !c->opt->check_email && !c->opt->check_ip) {
356 s_log(LOG_INFO, "CERT: No subject checks configured");
357 return 1; /* accept */
358 }
359
360 for(ptr=c->opt->check_host; ptr; ptr=ptr->next)
361 if(X509_check_host(cert, ptr->name, 0, 0, &peername)>0) {
362 s_log(LOG_INFO, "CERT: Host name \"%s\" matched with \"%s\"",
363 ptr->name, peername);
364 OPENSSL_free(peername);
365 return 1; /* accept */
366 }
367
368 for(ptr=c->opt->check_email; ptr; ptr=ptr->next)
369 if(X509_check_email(cert, ptr->name, 0, 0)>0) {
370 s_log(LOG_INFO, "CERT: Email address \"%s\" matched",
371 ptr->name);
372 return 1; /* accept */
373 }
374
375 for(ptr=c->opt->check_ip; ptr; ptr=ptr->next)
376 if(X509_check_ip_asc(cert, ptr->name, 0)>0) {
377 s_log(LOG_INFO, "CERT: IP address \"%s\" matched",
378 ptr->name);
379 return 1; /* accept */
380 }
381
382 s_log(LOG_WARNING, "CERT: Subject checks failed");
383 return 0; /* reject */
384 }
385 #endif /* OPENSSL_VERSION_NUMBER>=0x10002000L */
386
387 #if OPENSSL_VERSION_NUMBER>=0x10000000L
388 /* modern implementation for OpenSSL version >= 1.0.0 */
389
390 NOEXPORT int cert_check_local(X509_STORE_CTX *callback_ctx) {
391 X509 *cert;
392 #if OPENSSL_VERSION_NUMBER>=0x30000000L
393 const X509_NAME *subject;
394 #else /* OpenSSL 3.0.0 or later */
395 X509_NAME *subject;
396 #endif /* OpenSSL 3.0.0 or later */
397 STACK_OF(X509) *sk;
398 int i;
399
400 cert=X509_STORE_CTX_get_current_cert(callback_ctx);
401 subject=X509_get_subject_name(cert);
402
403 #if OPENSSL_VERSION_NUMBER<0x10100006L
404 #define X509_STORE_CTX_get1_certs X509_STORE_get1_certs
405 #endif
406 /* modern API allows retrieving multiple matching certificates */
407 sk=X509_STORE_CTX_get1_certs(callback_ctx, subject);
408 if(sk) {
409 for(i=0; i<sk_X509_num(sk); i++)
410 if(pubkey_eq(cert, sk_X509_value(sk, i))) {
411 sk_X509_pop_free(sk, X509_free);
412 return 1; /* accept */
413 }
414 sk_X509_pop_free(sk, X509_free);
415 }
416
417 s_log(LOG_WARNING, "CERT: Certificate not found in local repository");
418 X509_STORE_CTX_set_error(callback_ctx, X509_V_ERR_CERT_REJECTED);
419 return 0; /* reject */
420 }
421
422 #else /* OPENSSL_VERSION_NUMBER<0x10000000L */
423 /* legacy implementation for OpenSSL version < 1.0.0 */
424
425 NOEXPORT int cert_check_local(X509_STORE_CTX *callback_ctx) {
426 X509 *cert;
427 X509_NAME *subject;
428 X509_OBJECT obj;
429 int success;
430
431 cert=X509_STORE_CTX_get_current_cert(callback_ctx);
432 subject=X509_get_subject_name(cert);
433
434 /* pre-1.0.0 API only returns a single matching certificate */
435 memset((char *)&obj, 0, sizeof obj);
436 if(X509_STORE_get_by_subject(callback_ctx, X509_LU_X509,
437 subject, &obj)<=0) {
438 s_log(LOG_WARNING, "CERT: Certificate not found in local repository");
439 X509_STORE_CTX_set_error(callback_ctx, X509_V_ERR_CERT_REJECTED);
440 return 0; /* reject */
441 }
442 success=pubkey_eq(cert, obj.data.x509);
443 X509_OBJECT_free_contents(&obj);
444 if(success)
445 return 1; /* accept */
446
447 s_log(LOG_WARNING, "CERT: Public keys do not match");
448 X509_STORE_CTX_set_error(callback_ctx, X509_V_ERR_CERT_REJECTED);
449 return 0; /* reject */
450 }
451
452 #endif /* OPENSSL_VERSION_NUMBER>=0x10000000L */
453
454 #if OPENSSL_VERSION_NUMBER < 0x30000000L
455 #define EVP_PKEY_eq(a, b) EVP_PKEY_cmp((a), (b))
456 #endif /* OPENSSL_VERSION_NUMBER<0x30000000L */
457
458 #if OPENSSL_VERSION_NUMBER >= 0x40000000L
459 NOEXPORT int pubkey_eq(const X509 *c1, const X509 *c2) {
460 #else /* OpenSSL 4.0.0 or later */
461 NOEXPORT int pubkey_eq(X509 *c1, X509 *c2) {
462 #endif /* OpenSSL 4.0.0 or later */
463 EVP_PKEY *k1=X509_get_pubkey(c1);
464 EVP_PKEY *k2=X509_get_pubkey(c2);
465 int retval=k1 && k2 && EVP_PKEY_eq(k1, k2) == 1;
466
467 EVP_PKEY_free(k1);
468 EVP_PKEY_free(k2);
469 if(retval)
470 s_log(LOG_INFO, "CERT: Locally installed certificate public key matched");
471 return retval;
472 }
473
474 #ifndef OPENSSL_NO_ENGINE
475
476 X509 *engine_get_cert(ENGINE *engine, const char *id) {
477 struct {
478 const char *id;
479 X509 *cert;
480 } params;
481
482 params.id=id;
483 params.cert=NULL;
484 ENGINE_ctrl_cmd(engine, "LOAD_CERT_CTRL", 0, &params, NULL, 1);
485 if(!params.cert)
486 ssl_error(NULL, "ENGINE_ctrl_cmd");
487 return params.cert;
488 }
489
490 #endif
491
492 void print_CA_list(const char *type, const STACK_OF(X509_NAME) *ca_dn) {
493 char *ca_name;
494 int n, i;
495
496 if(!ca_dn) {
497 s_log(LOG_INFO, "%s list not found", type);
498 return;
499 }
500 n=sk_X509_NAME_num(ca_dn);
501 if(n==0) {
502 s_log(LOG_INFO, "%s list is empty", type);
503 return;
504 }
505 for(i=0; i<n; ++i) {
506 ca_name=X509_NAME2text(sk_X509_NAME_value(ca_dn, i));
507 s_log(LOG_INFO, "%s: %s", type, ca_name);
508 str_free(ca_name);
509 }
510 }
511
512 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
513 char *X509_NAME2text(const X509_NAME *name) {
514 #else /* OpenSSL 1.1.0 or newer */
515 char *X509_NAME2text(X509_NAME *name) {
516 #endif /* OpenSSL 1.1.0 or newer */
517 char *text;
518 BIO *bio;
519 int n;
520
521 bio=BIO_new(BIO_s_mem());
522 if(!bio)
523 return str_dup("BIO_new() failed");
524 X509_NAME_print_ex(bio, name, 0,
525 XN_FLAG_ONELINE & ~ASN1_STRFLGS_ESC_MSB & ~XN_FLAG_SPC_EQ);
526 n=BIO_pending(bio);
527 text=str_alloc((size_t)n+1);
528 n=BIO_read(bio, text, n);
529 if(n<0) {
530 BIO_free(bio);
531 str_free(text);
532 return str_dup("BIO_read() failed");
533 }
534 text[n]='\0';
535 BIO_free(bio);
536 return text;
537 }
538
539 /* end of verify.c */