OpenVPN
ssl_verify_openssl.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2026 OpenVPN Inc <sales@openvpn.net>
9 * Copyright (C) 2010-2026 Sentyron B.V. <openvpn@sentyron.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, see <https://www.gnu.org/licenses/>.
22 */
23
28
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33#include "syshead.h"
34
35#if defined(ENABLE_CRYPTO_OPENSSL)
36
37#include "ssl_verify_openssl.h"
38
39#include "error.h"
40#include "ssl_openssl.h"
41#include "ssl_verify.h"
42#include "ssl_verify_backend.h"
43#include "openssl_compat.h"
44
45#include <openssl/bn.h>
46#include <openssl/err.h>
47#include <openssl/x509v3.h>
48
49int
50verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
51{
52 int ret = 0;
53 struct tls_session *session;
54 SSL *ssl;
55 struct gc_arena gc = gc_new();
56
57 /* get the tls_session pointer */
58 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
59 ASSERT(ssl);
60 session = (struct tls_session *)SSL_get_ex_data(ssl, mydata_index);
62
63 X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx);
64 struct buffer cert_hash = x509_get_sha256_fingerprint(current_cert, &gc);
65 cert_hash_remember(session, X509_STORE_CTX_get_error_depth(ctx), &cert_hash);
66
67 /* did peer present cert which was signed by our root cert? */
68 if (!preverify_ok && !session->opt->verify_hash_no_ca)
69 {
70 /* get the X509 name */
71 char *subject = x509_get_subject(current_cert, &gc);
72 char *serial = backend_x509_get_serial(current_cert, &gc);
73
74 if (!subject)
75 {
76 subject = "(Failed to retrieve certificate subject)";
77 }
78
79 /* Log and ignore missing CRL errors */
80 if (X509_STORE_CTX_get_error(ctx) == X509_V_ERR_UNABLE_TO_GET_CRL)
81 {
82 msg(D_TLS_DEBUG_LOW, "VERIFY WARNING: depth=%d, %s: %s",
83 X509_STORE_CTX_get_error_depth(ctx),
84 X509_verify_cert_error_string(X509_STORE_CTX_get_error(ctx)), subject);
85 ret = 1;
86 goto cleanup;
87 }
88
89 /* Remote site specified a certificate, but it's not correct */
90 msg(D_TLS_ERRORS, "VERIFY ERROR: depth=%d, error=%s: %s, serial=%s",
91 X509_STORE_CTX_get_error_depth(ctx),
92 X509_verify_cert_error_string(X509_STORE_CTX_get_error(ctx)), subject,
93 serial ? serial : "<not available>");
94
95 ERR_clear_error();
96
97 session->verified = false;
98 goto cleanup;
99 }
100
101 if (SUCCESS != verify_cert(session, current_cert, X509_STORE_CTX_get_error_depth(ctx)))
102 {
103 goto cleanup;
104 }
105
106 ret = 1;
107
108cleanup:
109 gc_free(&gc);
110
111 return ret;
112}
113
114bool
116{
117 int nid = OBJ_txt2nid(fieldname);
118 return nid == NID_subject_alt_name || nid == NID_issuer_alt_name;
119}
120
121static bool
122extract_x509_extension(X509 *cert, char *fieldname, char *out, size_t size)
123{
124 bool retval = false;
125
126 if (!x509_username_field_ext_supported(fieldname))
127 {
128 msg(D_TLS_ERRORS, "ERROR: --x509-username-field 'ext:%s' not supported", fieldname);
129 return false;
130 }
131
132 int nid = OBJ_txt2nid(fieldname);
133 GENERAL_NAMES *extensions = X509_get_ext_d2i(cert, nid, NULL, NULL);
134 if (extensions)
135 {
136 /* get amount of alternatives,
137 * RFC2459 claims there MUST be at least
138 * one, but we don't depend on it...
139 */
140
141 openssl_stack_size_t numalts = sk_GENERAL_NAME_num(extensions);
142
143 /* loop through all alternatives */
144 for (openssl_stack_size_t i = 0; i < numalts; i++)
145 {
146 /* get a handle to alternative name number i */
147 const GENERAL_NAME *name = sk_GENERAL_NAME_value(extensions, i);
148 char *buf = NULL;
149
150 switch (name->type)
151 {
152 case GEN_EMAIL:
153 if (ASN1_STRING_to_UTF8((unsigned char **)&buf, name->d.rfc822Name) < 0)
154 {
155 continue;
156 }
157 if (strlen(buf) != ASN1_STRING_get_length(name->d.rfc822Name))
158 {
159 msg(D_TLS_ERRORS, "ASN1 ERROR: string contained terminating zero");
160 }
161 else if (strlcpy(out, buf, size) >= size)
162 {
163 msg(D_TLS_ERRORS, "ASN1 ERROR: string too long");
164 }
165 else
166 {
167 retval = true;
168 }
169 OPENSSL_free(buf);
170 break;
171
172 default:
173 msg(D_TLS_DEBUG, "%s: ignoring general name field type %d", __func__,
174 name->type);
175 break;
176 }
177 }
178 GENERAL_NAMES_free(extensions);
179 }
180 return retval;
181}
182
183/*
184 * Extract a field from an X509 subject name.
185 *
186 * Example:
187 *
188 * /C=US/ST=CO/L=Denver/O=ORG/CN=First-CN/CN=Test-CA/Email=jim@yonan.net
189 *
190 * The common name is 'Test-CA'
191 *
192 * Return true on success, false on error (insufficient buffer size in 'out'
193 * to contain result is grounds for error).
194 */
195static result_t
196extract_x509_field_ssl(const X509_NAME *x509, const char *field_name, char *out, size_t size)
197{
198 int lastpos = -1;
199 int tmp = -1;
200 unsigned char *buf = NULL;
201 result_t ret = FAILURE;
202
203 ASN1_OBJECT *field_name_obj = OBJ_txt2obj(field_name, 0);
204 if (field_name_obj == NULL)
205 {
206 msg(D_TLS_ERRORS, "Invalid X509 attribute name '%s'", field_name);
207 goto exit;
208 }
209
210 ASSERT(size > 0);
211 *out = '\0';
212 do
213 {
214 lastpos = tmp;
215#if OPENSSL_VERSION_NUMBER >= 0x30000000L
216 tmp = X509_NAME_get_index_by_OBJ(x509, field_name_obj, lastpos);
217#else
218 /* OpenSSL 1.1.x has the argument as non-const */
219 tmp = X509_NAME_get_index_by_OBJ((X509_NAME *)x509, field_name_obj, lastpos);
220#endif
221 } while (tmp > -1);
222
223 ASN1_OBJECT_free(field_name_obj);
224
225 /* Nothing found */
226 if (lastpos == -1)
227 {
228 goto exit;
229 }
230
231 const X509_NAME_ENTRY *x509ne = X509_NAME_get_entry(x509, lastpos);
232 if (!x509ne)
233 {
234 goto exit;
235 }
236
237 const ASN1_STRING *asn1 = X509_NAME_ENTRY_get_data(x509ne);
238 if (!asn1)
239 {
240 goto exit;
241 }
242 int length = ASN1_STRING_to_UTF8(&buf, asn1);
243 if (length < 0 || (size_t)length != strlen((char *)buf))
244 {
245 goto exit;
246 }
247
248 ret = (strlcpy(out, (char *)buf, size) < size) ? SUCCESS : FAILURE;
249
250exit:
251 OPENSSL_free(buf);
252 return ret;
253}
254
256backend_x509_get_username(char *common_name, size_t cn_len, char *x509_username_field, X509 *peer_cert)
257{
258 if (strncmp("ext:", x509_username_field, 4) == 0)
259 {
260 if (!extract_x509_extension(peer_cert, x509_username_field + 4, common_name, cn_len))
261 {
262 return FAILURE;
263 }
264 }
265 else if (strcmp(LN_serialNumber, x509_username_field) == 0)
266 {
267 const ASN1_INTEGER *asn1_i = X509_get_serialNumber(peer_cert);
268
269 BIGNUM *bn_serial = ASN1_INTEGER_to_BN(asn1_i, NULL);
270 char *serial = BN_bn2hex(bn_serial);
271 BN_free(bn_serial);
272
273 if (!serial || cn_len <= strlen(serial) + 2)
274 {
275 OPENSSL_free(serial);
276 return FAILURE;
277 }
278 snprintf(common_name, cn_len, "0x%s", serial);
279 OPENSSL_free(serial);
280 }
281 else
282 {
283 const X509_NAME *x509_subject_name = X509_get_subject_name(peer_cert);
284 if (x509_subject_name == NULL)
285 {
286 msg(D_TLS_ERRORS, "X509 subject name is NULL");
287 return FAILURE;
288 }
289
290 if (FAILURE
291 == extract_x509_field_ssl(x509_subject_name, x509_username_field,
292 common_name, cn_len))
293 {
294 return FAILURE;
295 }
296 }
297
298 return SUCCESS;
299}
300
301char *
303{
304 ASN1_INTEGER *asn1_i;
305 BIGNUM *bignum;
306 char *openssl_serial, *serial;
307
308 asn1_i = X509_get_serialNumber(cert);
309 bignum = ASN1_INTEGER_to_BN(asn1_i, NULL);
310 openssl_serial = BN_bn2dec(bignum);
311
312 serial = string_alloc(openssl_serial, gc);
313
314 BN_free(bignum);
315 OPENSSL_free(openssl_serial);
316
317 return serial;
318}
319
320#if defined(__GNUC__) || defined(__clang__)
321#pragma GCC diagnostic push
322#pragma GCC diagnostic ignored "-Wsign-conversion"
323#endif
324
325char *
327{
328 const ASN1_INTEGER *asn1_i = X509_get_serialNumber(cert);
329 BIGNUM *bn_serial = ASN1_INTEGER_to_BN(asn1_i, NULL);
330 int len_serial = BN_num_bytes(bn_serial);
331 unsigned char *buf = gc_malloc(len_serial, false, gc);
332 BN_bn2binpad(bn_serial, buf, len_serial);
333
334 char *ret = format_hex_ex(buf, len_serial, 0, 1, ":", gc);
335 BN_free(bn_serial);
336
337 return ret;
338}
339
341backend_x509_write_pem(openvpn_x509_cert_t *cert, const char *filename)
342{
343 BIO *out = BIO_new_file(filename, "w");
344 if (!out)
345 {
346 goto err;
347 }
348
349 if (!PEM_write_bio_X509(out, cert))
350 {
351 goto err;
352 }
353 BIO_free(out);
354
355 return SUCCESS;
356err:
357 BIO_free(out);
358 crypto_msg(D_TLS_DEBUG_LOW, "Error writing X509 certificate to file %s", filename);
359 return FAILURE;
360}
361
362struct buffer
364{
365 const EVP_MD *sha1 = EVP_sha1();
366 struct buffer hash = alloc_buf_gc((size_t)EVP_MD_size(sha1), gc);
367 X509_digest(cert, EVP_sha1(), BPTR(&hash), NULL);
368 ASSERT(buf_inc_len(&hash, (int)EVP_MD_size(sha1)));
369 return hash;
370}
371
372struct buffer
374{
375 const EVP_MD *sha256 = EVP_sha256();
376 struct buffer hash = alloc_buf_gc((size_t)EVP_MD_size(sha256), gc);
377 X509_digest(cert, EVP_sha256(), BPTR(&hash), NULL);
378 ASSERT(buf_inc_len(&hash, (int)EVP_MD_size(sha256)));
379 return hash;
380}
381
382char *
383x509_get_subject(X509 *cert, struct gc_arena *gc)
384{
385 BIO *subject_bio = NULL;
386 BUF_MEM *subject_mem;
387 char *subject = NULL;
388
389 subject_bio = BIO_new(BIO_s_mem());
390 if (subject_bio == NULL)
391 {
392 goto err;
393 }
394
395 X509_NAME_print_ex(subject_bio, X509_get_subject_name(cert), 0,
396 XN_FLAG_SEP_CPLUS_SPC | XN_FLAG_FN_SN | ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_UTF8_CONVERT);
397
398 if (BIO_eof(subject_bio))
399 {
400 goto err;
401 }
402
403 BIO_get_mem_ptr(subject_bio, &subject_mem);
404
405 /* Check subject for '\0' bytes. */
406 for (size_t i = 0; i < subject_mem->length; i++)
407 {
408 if (subject_mem->data[i] == 0)
409 {
410 msg(M_WARN, "ERROR: Certificate subject contains a '\\0' byte.");
411 goto err;
412 }
413 }
414
415 subject = gc_malloc(subject_mem->length + 1, false, gc);
416
417 memcpy(subject, subject_mem->data, subject_mem->length);
418 subject[subject_mem->length] = '\0';
419
420err:
421 BIO_free(subject_bio);
422 return subject;
423}
424
425
426/*
427 * x509-track implementation -- save X509 fields to environment,
428 * using the naming convention:
429 *
430 * X509_{cert_depth}_{name}={value}
431 *
432 * This function differs from x509_setenv below in the following ways:
433 *
434 * (1) Only explicitly named attributes in xt are saved, per usage
435 * of "x509-track" program options.
436 * (2) Only the level 0 cert info is saved unless the XT_FULL_CHAIN
437 * flag is set in xt->flags (corresponds with prepending a '+'
438 * to the name when specified by "x509-track" program option).
439 * (3) This function supports both X509 subject name fields as
440 * well as X509 V3 extensions.
441 * (4) This function can return the SHA1 fingerprint of a cert, e.g.
442 * x509-track "+SHA1"
443 * will return the SHA1 fingerprint for each certificate in the
444 * peer chain.
445 */
446
447void
448x509_track_add(const struct x509_track **ll_head, const char *name, msglvl_t msglevel,
449 struct gc_arena *gc)
450{
451 struct x509_track *xt;
452 ALLOC_OBJ_CLEAR_GC(xt, struct x509_track, gc);
453 if (*name == '+')
454 {
455 xt->flags |= XT_FULL_CHAIN;
456 ++name;
457 }
458 xt->name = name;
459 xt->nid = OBJ_txt2nid(name);
460 if (xt->nid != NID_undef)
461 {
462 xt->next = *ll_head;
463 *ll_head = xt;
464 }
465 else
466 {
467 msg(msglevel, "x509_track: no such attribute '%s'", name);
468 }
469}
470
471/* worker method for setenv_x509_track */
472static void
473do_setenv_x509(struct env_set *es, const char *name, char *value, int depth)
474{
475 char *name_expand;
476 size_t name_expand_size;
477
478 string_mod(value, CC_ANY, CC_CRLF, '?');
479 msg(D_X509_ATTR, "X509 ATTRIBUTE name='%s' value='%s' depth=%d", name, value, depth);
480 name_expand_size = 64 + strlen(name);
481 name_expand = (char *)malloc(name_expand_size);
482 check_malloc_return(name_expand);
483 snprintf(name_expand, name_expand_size, "X509_%d_%s", depth, name);
484 setenv_str(es, name_expand, value);
485 free(name_expand);
486}
487
488void
489x509_setenv_track(const struct x509_track *xt, struct env_set *es, const int depth, X509 *x509)
490{
491 struct gc_arena gc = gc_new();
492#if OPENSSL_VERSION_NUMBER < 0x30000000L
493 /* OpenSSL 1.1.x APIs all take non-const arguments */
494 X509_NAME *x509_name = X509_get_subject_name(x509);
495#else
496 const X509_NAME *x509_name = X509_get_subject_name(x509);
497#endif
498 const char nullc = '\0';
499
500 while (xt)
501 {
502 if (depth == 0 || (xt->flags & XT_FULL_CHAIN))
503 {
504 switch (xt->nid)
505 {
506 case NID_sha1:
507 case NID_sha256:
508 {
509 struct buffer fp_buf;
510 char *fp_str = NULL;
511
512 if (xt->nid == NID_sha1)
513 {
514 fp_buf = x509_get_sha1_fingerprint(x509, &gc);
515 }
516 else
517 {
518 fp_buf = x509_get_sha256_fingerprint(x509, &gc);
519 }
520
521 fp_str = format_hex_ex(BPTR(&fp_buf), BLEN(&fp_buf), 0, 1 | FHE_CAPS, ":", &gc);
522 do_setenv_x509(es, xt->name, fp_str, depth);
523 }
524 break;
525
526 default:
527 {
528 int i = X509_NAME_get_index_by_NID(x509_name, xt->nid, -1);
529 if (i >= 0)
530 {
531 const X509_NAME_ENTRY *ent = X509_NAME_get_entry(x509_name, i);
532 if (ent)
533 {
534 const ASN1_STRING *val = X509_NAME_ENTRY_get_data(ent);
535 unsigned char *buf = NULL;
536 if (ASN1_STRING_to_UTF8(&buf, val) >= 0)
537 {
538 do_setenv_x509(es, xt->name, (char *)buf, depth);
539 OPENSSL_free(buf);
540 }
541 }
542 }
543 else
544 {
545 i = X509_get_ext_by_NID(x509, xt->nid, -1);
546 if (i >= 0)
547 {
548#if OPENSSL_VERSION_NUMBER < 0x40000000L
549 X509_EXTENSION *ext = X509_get_ext(x509, i);
550#else
551 const X509_EXTENSION *ext = X509_get_ext(x509, i);
552#endif
553 if (ext)
554 {
555 BIO *bio = BIO_new(BIO_s_mem());
556 if (bio)
557 {
558 if (X509V3_EXT_print(bio, ext, 0, 0))
559 {
560 if (BIO_write(bio, &nullc, 1) == 1)
561 {
562 char *str;
563 BIO_get_mem_data(bio, &str);
564 do_setenv_x509(es, xt->name, str, depth);
565 }
566 }
567 BIO_free(bio);
568 }
569 }
570 }
571 }
572 }
573 }
574 }
575 xt = xt->next;
576 }
577 gc_free(&gc);
578}
579
580#if defined(__GNUC__) || defined(__clang__)
581#pragma GCC diagnostic pop
582#endif
583
584/*
585 * Save X509 fields to environment, using the naming convention:
586 *
587 * X509_{cert_depth}_{name}={value}
588 */
589void
590x509_setenv(struct env_set *es, int cert_depth, openvpn_x509_cert_t *peer_cert)
591{
592 const X509_NAME *x509 = X509_get_subject_name(peer_cert);
593
594 int n = X509_NAME_entry_count(x509);
595 for (int i = 0; i < n; ++i)
596 {
597 const X509_NAME_ENTRY *ent = X509_NAME_get_entry(x509, i);
598 if (!ent)
599 {
600 continue;
601 }
602 const ASN1_OBJECT *fn = X509_NAME_ENTRY_get_object(ent);
603 if (!fn)
604 {
605 continue;
606 }
607 const ASN1_STRING *val = X509_NAME_ENTRY_get_data(ent);
608 if (!val)
609 {
610 continue;
611 }
612 int fn_nid = OBJ_obj2nid(fn);
613 if (fn_nid == NID_undef)
614 {
615 continue;
616 }
617 const char *objbuf = OBJ_nid2sn(fn_nid);
618 if (!objbuf)
619 {
620 continue;
621 }
622 unsigned char *buf = NULL;
623 if (ASN1_STRING_to_UTF8(&buf, val) < 0)
624 {
625 continue;
626 }
627 size_t name_expand_size = 64 + strlen(objbuf);
628 char *name_expand = malloc(name_expand_size);
629 check_malloc_return(name_expand);
630 snprintf(name_expand, name_expand_size, "X509_%d_%s", cert_depth, objbuf);
631 string_mod(name_expand, CC_PRINT, CC_CRLF, '_');
632 string_mod((char *)buf, CC_PRINT, CC_CRLF, '_');
633 setenv_str_incr(es, name_expand, (char *)buf);
634 free(name_expand);
635 OPENSSL_free(buf);
636 }
637}
638
640x509_verify_ns_cert_type(openvpn_x509_cert_t *peer_cert, const int cert_type)
641{
642 if (cert_type == NS_CERT_CHECK_NONE)
643 {
644 return SUCCESS;
645 }
646 if (cert_type == NS_CERT_CHECK_CLIENT)
647 {
648 /*
649 * Unfortunately, X509_check_purpose() before OpenSSL 4.0 does some weird thing that
650 * prevent it to take a const argument
651 */
652 result_t result =
653 X509_check_purpose(peer_cert, X509_PURPOSE_SSL_CLIENT, 0) ? SUCCESS : FAILURE;
654 /*
655 * Note that we did not check for netscape certificate type here but
656 * instead a general SSL/TLS client purpose. These nscert attributes
657 * might stop being accepted by TLS libraries in the future.
658 * Currently, OpenSSL 4.0 and aws-lc 1.9.0 still consider nscert client
659 * as acceptable.
660 *
661 * So in case that this check failed, we now check if this is caused
662 * by the check above no longer recognising nscert attributes.
663 */
664 if (result == FAILURE)
665 {
666 ASN1_BIT_STRING *ns;
667 ns = X509_get_ext_d2i(peer_cert, NID_netscape_cert_type, NULL, NULL);
668 // bit 0 is to check if certificate is the client certificate
669 result = ASN1_BIT_STRING_get_bit(ns, 0) ? SUCCESS : FAILURE;
670 if (result == SUCCESS)
671 {
672 msg(M_WARN, "X509: Certificate is a client certificate yet it's purpose "
673 "cannot be verified (check may fail in the future)");
674 }
675 ASN1_BIT_STRING_free(ns);
676 }
677 return result;
678 }
679 if (cert_type == NS_CERT_CHECK_SERVER)
680 {
681 /*
682 * Unfortunately, X509_check_purpose() before OpenSSL 4.0 does some weird thing that
683 * prevent it to take a const argument
684 */
685 result_t result =
686 X509_check_purpose(peer_cert, X509_PURPOSE_SSL_SERVER, 0) ? SUCCESS : FAILURE;
687
688 /*
689 * Note that we did not check for netscape certificate type here but
690 * instead a general SSL/TLS server purpose. These nscert attributes
691 * might stop being accepted by TLS libraries in the future.
692 * Currently, OpenSSL 4.0 and aws-lc 1.9.0 still consider nscert server
693 * as acceptable.
694 *
695 * So in case that this check failed, we now check if this is caused
696 * by the check above no longer recognising nscert attributes.
697 */
698 if (result == FAILURE)
699 {
700 ASN1_BIT_STRING *ns = X509_get_ext_d2i(peer_cert, NID_netscape_cert_type, NULL, NULL);
701 // Server bit is 1 for ASN1_BIT_STRING_get_bit
702 result = ASN1_BIT_STRING_get_bit(ns, 1) ? SUCCESS : FAILURE;
703 if (result == SUCCESS)
704 {
705 msg(M_WARN, "X509: Certificate is a server certificate yet it's purpose "
706 "cannot be verified (check may fail in the future)");
707 }
708 ASN1_BIT_STRING_free(ns);
709 }
710 return result;
711 }
712
713 return FAILURE;
714}
715
717x509_verify_cert_ku(X509 *x509, const unsigned int *const expected_ku, size_t expected_len)
718{
719 ASN1_BIT_STRING *ku = X509_get_ext_d2i(x509, NID_key_usage, NULL, NULL);
720
721 if (ku == NULL)
722 {
723 msg(D_TLS_ERRORS, "Certificate does not have key usage extension");
724 return FAILURE;
725 }
726
727 if (expected_ku[0] == OPENVPN_KU_REQUIRED)
728 {
729 /* Extension required, value checked by TLS library */
730 ASN1_BIT_STRING_free(ku);
731 return SUCCESS;
732 }
733
734 unsigned int nku = 0;
735 for (int i = 0; i < 8; i++)
736 {
737 if (ASN1_BIT_STRING_get_bit(ku, i))
738 {
739 nku |= 1 << (7 - i);
740 }
741 }
742
743 /*
744 * Fixup if no LSB bits
745 */
746 if ((nku & 0xff) == 0)
747 {
748 nku >>= 8;
749 }
750
751 msg(D_HANDSHAKE, "Validating certificate key usage");
752 result_t fFound = FAILURE;
753 for (size_t i = 0; fFound != SUCCESS && i < expected_len; i++)
754 {
755 if (expected_ku[i] != 0 && (nku & expected_ku[i]) == expected_ku[i])
756 {
757 fFound = SUCCESS;
758 }
759 }
760
761 if (fFound != SUCCESS)
762 {
763 msg(D_TLS_ERRORS, "ERROR: Certificate has key usage %04x, expected one of:", nku);
764 for (size_t i = 0; i < expected_len && expected_ku[i]; i++)
765 {
766 msg(D_TLS_ERRORS, " * %04x", expected_ku[i]);
767 }
768 }
769
770 ASN1_BIT_STRING_free(ku);
771
772 return fFound;
773}
774
776x509_verify_cert_eku(X509 *x509, const char *const expected_oid)
777{
778 EXTENDED_KEY_USAGE *eku = NULL;
779 result_t fFound = FAILURE;
780
781 if ((eku = (EXTENDED_KEY_USAGE *)X509_get_ext_d2i(x509, NID_ext_key_usage, NULL, NULL)) == NULL)
782 {
783 msg(D_HANDSHAKE, "Certificate does not have extended key usage extension");
784 }
785 else
786 {
787 msg(D_HANDSHAKE, "Validating certificate extended key usage");
788 for (openssl_stack_size_t i = 0; SUCCESS != fFound && i < sk_ASN1_OBJECT_num(eku); i++)
789 {
790 ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(eku, i);
791 char szOid[1024];
792
793 if (SUCCESS != fFound && OBJ_obj2txt(szOid, sizeof(szOid), oid, 0) != -1)
794 {
795 msg(D_HANDSHAKE, "++ Certificate has EKU (str) %s, expects %s", szOid,
796 expected_oid);
797 if (!strcmp(expected_oid, szOid))
798 {
799 fFound = SUCCESS;
800 }
801 }
802 if (SUCCESS != fFound && OBJ_obj2txt(szOid, sizeof(szOid), oid, 1) != -1)
803 {
804 msg(D_HANDSHAKE, "++ Certificate has EKU (oid) %s, expects %s", szOid,
805 expected_oid);
806 if (!strcmp(expected_oid, szOid))
807 {
808 fFound = SUCCESS;
809 }
810 }
811 }
812 }
813
814 if (eku != NULL)
815 {
816 sk_ASN1_OBJECT_pop_free(eku, ASN1_OBJECT_free);
817 }
818
819 return fFound;
820}
821
822bool
824{
825 if (!opt->crl_file || (opt->ssl_flags & SSLF_CRL_VERIFY_DIR))
826 {
827 return false;
828 }
829
830 return opt->ssl_ctx->crls == NULL || sk_X509_CRL_num(opt->ssl_ctx->crls) == 0;
831}
832
833#endif /* defined(ENABLE_CRYPTO_OPENSSL) */
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Allocate memory and, optionally, zero it.
Definition buffer.c:321
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
Definition buffer.c:77
char * format_hex_ex(const uint8_t *data, size_t size, size_t maxoutput, unsigned int space_break_flags, const char *separator, struct gc_arena *gc)
Format a binary buffer as a hex string.
Definition buffer.c:460
bool string_mod(char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace)
Modifies a string in place by replacing certain classes of characters of it with a specified characte...
Definition buffer.c:1021
char * string_alloc(const char *str, struct gc_arena *gc)
Duplicate a string, allocating memory under garbage collection.
Definition buffer.c:618
#define CC_ANY
any character
Definition buffer.h:1686
#define BPTR(buf)
Return a pointer to the start of the buffer content.
Definition buffer.h:139
static bool buf_inc_len(struct buffer *buf, int inc)
Increase or decrease the length of a buffer.
Definition buffer.h:1119
#define CC_CRLF
carriage return or newline
Definition buffer.h:1723
#define ALLOC_OBJ_CLEAR_GC(dptr, type, gc)
Allocate and zero-initialise a garbage-collected object of the given type.
Definition buffer.h:2132
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:151
static void check_malloc_return(void *p)
Abort if a memory allocation returned NULL.
Definition buffer.h:2144
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1974
#define CC_PRINT
printable (>= 32, != 127)
Definition buffer.h:1694
#define FHE_CAPS
Flag for format_hex_ex(): output hex digits in upper case.
Definition buffer.h:927
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1958
size_t strlcpy(char *dest, const char *src, size_t maxlen)
#define crypto_msg(flags,...)
Retrieve any OpenSSL errors, then print the supplied error message.
void setenv_str(struct env_set *es, const char *name, const char *value)
Definition env_set.c:305
void setenv_str_incr(struct env_set *es, const char *name, const char *value)
Store the supplied name value pair in the env_set.
Definition env_set.c:327
#define D_TLS_DEBUG_LOW
Definition errlevel.h:75
#define D_X509_ATTR
Definition errlevel.h:100
#define D_HANDSHAKE
Definition errlevel.h:70
#define D_TLS_ERRORS
Definition errlevel.h:58
#define D_TLS_DEBUG
Definition errlevel.h:161
int verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
Verify that the remote OpenVPN peer's certificate allows setting up a VPN tunnel.
OpenSSL compatibility stub.
#define ASN1_STRING_get_length
int openssl_stack_size_t
#define msg(flags,...)
Definition error.h:152
unsigned int msglvl_t
Definition error.h:77
#define ASSERT(x)
Definition error.h:221
#define M_WARN
Definition error.h:92
#define SSLF_CRL_VERIFY_DIR
Definition ssl_common.h:433
int mydata_index
Allocate space in SSL objects in which to store a struct tls_session pointer back to parent.
Definition ssl_openssl.c:88
Control Channel OpenSSL Backend.
result_t verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_depth)
Definition ssl_verify.c:591
void cert_hash_remember(struct tls_session *session, const int error_depth, const struct buffer *cert_hash)
Definition ssl_verify.c:194
Control Channel Verification Module.
#define OPENVPN_KU_REQUIRED
Require keyUsage to be present in cert (0xFFFF is an invalid KU value).
Definition ssl_verify.h:257
#define XT_FULL_CHAIN
Definition ssl_verify.h:241
#define NS_CERT_CHECK_CLIENT
Do not perform Netscape certificate type verification.
Definition ssl_verify.h:254
#define NS_CERT_CHECK_NONE
Do not perform Netscape certificate type verification.
Definition ssl_verify.h:250
#define NS_CERT_CHECK_SERVER
Do not perform Netscape certificate type verification.
Definition ssl_verify.h:252
Control Channel Verification Module library-specific backend interface.
struct buffer x509_get_sha256_fingerprint(openvpn_x509_cert_t *cert, struct gc_arena *gc)
Retrieve the certificate's SHA256 fingerprint.
struct buffer x509_get_sha1_fingerprint(openvpn_x509_cert_t *cert, struct gc_arena *gc)
Retrieve the certificate's SHA1 fingerprint.
result_t
Result of verification function.
@ FAILURE
@ SUCCESS
mbedtls_x509_crt openvpn_x509_cert_t
char * x509_get_subject(X509 *cert, struct gc_arena *gc)
struct buffer x509_get_sha1_fingerprint(X509 *cert, struct gc_arena *gc)
result_t x509_verify_ns_cert_type(openvpn_x509_cert_t *peer_cert, const int cert_type)
result_t x509_verify_cert_ku(X509 *x509, const unsigned int *const expected_ku, size_t expected_len)
bool tls_verify_crl_missing(const struct tls_options *opt)
Return true iff a CRL is configured, but is not loaded.
static result_t extract_x509_field_ssl(const X509_NAME *x509, const char *field_name, char *out, size_t size)
result_t backend_x509_write_pem(openvpn_x509_cert_t *cert, const char *filename)
struct buffer x509_get_sha256_fingerprint(X509 *cert, struct gc_arena *gc)
result_t x509_verify_cert_eku(X509 *x509, const char *const expected_oid)
static void do_setenv_x509(struct env_set *es, const char *name, char *value, int depth)
static bool extract_x509_extension(X509 *cert, char *fieldname, char *out, size_t size)
bool x509_username_field_ext_supported(const char *fieldname)
Return true iff the supplied extension field is supported by the –x509-username-field option.
char * backend_x509_get_serial_hex(openvpn_x509_cert_t *cert, struct gc_arena *gc)
void x509_setenv_track(const struct x509_track *xt, struct env_set *es, const int depth, X509 *x509)
char * backend_x509_get_serial(openvpn_x509_cert_t *cert, struct gc_arena *gc)
void x509_track_add(const struct x509_track **ll_head, const char *name, msglvl_t msglevel, struct gc_arena *gc)
void x509_setenv(struct env_set *es, int cert_depth, openvpn_x509_cert_t *peer_cert)
result_t backend_x509_get_username(char *common_name, size_t cn_len, char *x509_username_field, X509 *peer_cert)
Control Channel Verification Module OpenSSL backend.
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
Structure containing the hash for a single certificate.
Definition ssl_verify.h:58
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
Definition list.h:56
unsigned int ssl_flags
Definition ssl_common.h:439
struct tls_root_ctx * ssl_ctx
Definition ssl_common.h:310
const char * crl_file
Definition ssl_common.h:360
Security parameter state of a single session within a VPN tunnel.
Definition ssl_common.h:497
unsigned int flags
Definition ssl_verify.h:242
const struct x509_track * next
Definition ssl_verify.h:239
const char * name
Definition ssl_verify.h:240
static int cleanup(void **state)
struct gc_arena gc
Definition test_ssl.c:138