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-2018 OpenVPN Inc <sales@openvpn.net>
9  * Copyright (C) 2010-2018 Fox Crypto B.V. <openvpn@fox-it.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, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #elif defined(_MSC_VER)
32 #include "config-msvc.h"
33 #endif
34 
35 #include "syshead.h"
36 
37 #if defined(ENABLE_CRYPTO_OPENSSL)
38 
39 #include "ssl_verify_openssl.h"
40 
41 #include "error.h"
42 #include "ssl_openssl.h"
43 #include "ssl_verify.h"
44 #include "ssl_verify_backend.h"
45 #include "openssl_compat.h"
46 
47 #include <openssl/bn.h>
48 #include <openssl/err.h>
49 #include <openssl/x509v3.h>
50 
51 int
52 verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
53 {
54  int ret = 0;
55  struct tls_session *session;
56  SSL *ssl;
57  struct gc_arena gc = gc_new();
58 
59  /* get the tls_session pointer */
60  ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
61  ASSERT(ssl);
62  session = (struct tls_session *) SSL_get_ex_data(ssl, mydata_index);
63  ASSERT(session);
64 
65  X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx);
66  struct buffer cert_hash = x509_get_sha256_fingerprint(current_cert, &gc);
67  cert_hash_remember(session, X509_STORE_CTX_get_error_depth(ctx), &cert_hash);
68 
69  /* did peer present cert which was signed by our root cert? */
70  if (!preverify_ok)
71  {
72  /* get the X509 name */
73  char *subject = x509_get_subject(current_cert, &gc);
74 
75  if (!subject)
76  {
77  subject = "(Failed to retrieve certificate subject)";
78  }
79 
80  /* Log and ignore missing CRL errors */
81  if (X509_STORE_CTX_get_error(ctx) == X509_V_ERR_UNABLE_TO_GET_CRL)
82  {
83  msg(D_TLS_DEBUG_LOW, "VERIFY WARNING: depth=%d, %s: %s",
84  X509_STORE_CTX_get_error_depth(ctx),
85  X509_verify_cert_error_string(X509_STORE_CTX_get_error(ctx)),
86  subject);
87  ret = 1;
88  goto cleanup;
89  }
90 
91  /* Remote site specified a certificate, but it's not correct */
92  msg(D_TLS_ERRORS, "VERIFY ERROR: depth=%d, error=%s: %s",
93  X509_STORE_CTX_get_error_depth(ctx),
94  X509_verify_cert_error_string(X509_STORE_CTX_get_error(ctx)),
95  subject);
96 
97  ERR_clear_error();
98 
99  session->verified = false;
100  goto cleanup;
101  }
102 
103  if (SUCCESS != verify_cert(session, current_cert, X509_STORE_CTX_get_error_depth(ctx)))
104  {
105  goto cleanup;
106  }
107 
108  ret = 1;
109 
110 cleanup:
111  gc_free(&gc);
112 
113  return ret;
114 }
115 
116 #ifdef ENABLE_X509ALTUSERNAME
117 bool
118 x509_username_field_ext_supported(const char *fieldname)
119 {
120  int nid = OBJ_txt2nid(fieldname);
121  return nid == NID_subject_alt_name || nid == NID_issuer_alt_name;
122 }
123 
124 static
125 bool
126 extract_x509_extension(X509 *cert, char *fieldname, char *out, int size)
127 {
128  bool retval = false;
129  char *buf = 0;
130 
131  if (!x509_username_field_ext_supported(fieldname))
132  {
134  "ERROR: --x509-username-field 'ext:%s' not supported", fieldname);
135  return false;
136  }
137 
138  int nid = OBJ_txt2nid(fieldname);
139  GENERAL_NAMES *extensions = X509_get_ext_d2i(cert, nid, NULL, NULL);
140  if (extensions)
141  {
142  int numalts;
143  int i;
144  /* get amount of alternatives,
145  * RFC2459 claims there MUST be at least
146  * one, but we don't depend on it...
147  */
148 
149  numalts = sk_GENERAL_NAME_num(extensions);
150 
151  /* loop through all alternatives */
152  for (i = 0; i<numalts; i++)
153  {
154  /* get a handle to alternative name number i */
155  const GENERAL_NAME *name = sk_GENERAL_NAME_value(extensions, i );
156 
157  switch (name->type)
158  {
159  case GEN_EMAIL:
160  if (ASN1_STRING_to_UTF8((unsigned char **)&buf, name->d.ia5) < 0)
161  {
162  continue;
163  }
164  if (strlen(buf) != name->d.ia5->length)
165  {
166  msg(D_TLS_ERRORS, "ASN1 ERROR: string contained terminating zero");
167  OPENSSL_free(buf);
168  }
169  else
170  {
171  strncpynt(out, buf, size);
172  OPENSSL_free(buf);
173  retval = true;
174  }
175  break;
176 
177  default:
178  msg(D_TLS_DEBUG, "%s: ignoring general name field type %i",
179  __func__, name->type);
180  break;
181  }
182  }
183  GENERAL_NAMES_free(extensions);
184  }
185  return retval;
186 }
187 #endif /* ENABLE_X509ALTUSERNAME */
188 
189 /*
190  * Extract a field from an X509 subject name.
191  *
192  * Example:
193  *
194  * /C=US/ST=CO/L=Denver/O=ORG/CN=First-CN/CN=Test-CA/Email=jim@yonan.net
195  *
196  * The common name is 'Test-CA'
197  *
198  * Return true on success, false on error (insufficient buffer size in 'out'
199  * to contain result is grounds for error).
200  */
201 static result_t
202 extract_x509_field_ssl(X509_NAME *x509, const char *field_name, char *out,
203  int size)
204 {
205  int lastpos = -1;
206  int tmp = -1;
207  X509_NAME_ENTRY *x509ne = NULL;
208  ASN1_STRING *asn1 = NULL;
209  unsigned char *buf = NULL;
210  ASN1_OBJECT *field_name_obj = OBJ_txt2obj(field_name, 0);
211 
212  if (field_name_obj == NULL)
213  {
214  msg(D_TLS_ERRORS, "Invalid X509 attribute name '%s'", field_name);
215  return FAILURE;
216  }
217 
218  ASSERT(size > 0);
219  *out = '\0';
220  do
221  {
222  lastpos = tmp;
223  tmp = X509_NAME_get_index_by_OBJ(x509, field_name_obj, lastpos);
224  } while (tmp > -1);
225 
226  ASN1_OBJECT_free(field_name_obj);
227 
228  /* Nothing found */
229  if (lastpos == -1)
230  {
231  return FAILURE;
232  }
233 
234  x509ne = X509_NAME_get_entry(x509, lastpos);
235  if (!x509ne)
236  {
237  return FAILURE;
238  }
239 
240  asn1 = X509_NAME_ENTRY_get_data(x509ne);
241  if (!asn1)
242  {
243  return FAILURE;
244  }
245  if (ASN1_STRING_to_UTF8(&buf, asn1) < 0)
246  {
247  return FAILURE;
248  }
249 
250  strncpynt(out, (char *)buf, size);
251 
252  {
253  const result_t ret = (strlen((char *)buf) < size) ? SUCCESS : FAILURE;
254  OPENSSL_free(buf);
255  return ret;
256  }
257 }
258 
259 result_t
260 backend_x509_get_username(char *common_name, int cn_len,
261  char *x509_username_field, X509 *peer_cert)
262 {
263 #ifdef ENABLE_X509ALTUSERNAME
264  if (strncmp("ext:",x509_username_field,4) == 0)
265  {
266  if (!extract_x509_extension(peer_cert, x509_username_field+4, common_name, cn_len))
267  {
268  return FAILURE;
269  }
270  }
271  else
272 #endif
273  if (FAILURE == extract_x509_field_ssl(X509_get_subject_name(peer_cert),
274  x509_username_field, common_name, cn_len))
275  {
276  return FAILURE;
277  }
278 
279  return SUCCESS;
280 }
281 
282 char *
284 {
285  ASN1_INTEGER *asn1_i;
286  BIGNUM *bignum;
287  char *openssl_serial, *serial;
288 
289  asn1_i = X509_get_serialNumber(cert);
290  bignum = ASN1_INTEGER_to_BN(asn1_i, NULL);
291  openssl_serial = BN_bn2dec(bignum);
292 
293  serial = string_alloc(openssl_serial, gc);
294 
295  BN_free(bignum);
296  OPENSSL_free(openssl_serial);
297 
298  return serial;
299 }
300 
301 char *
303 {
304  const ASN1_INTEGER *asn1_i = X509_get_serialNumber(cert);
305 
306  return format_hex_ex(asn1_i->data, asn1_i->length, 0, 1, ":", gc);
307 }
308 
309 struct buffer
310 x509_get_sha1_fingerprint(X509 *cert, struct gc_arena *gc)
311 {
312  const EVP_MD *sha1 = EVP_sha1();
313  struct buffer hash = alloc_buf_gc(EVP_MD_size(sha1), gc);
314  X509_digest(cert, EVP_sha1(), BPTR(&hash), NULL);
315  ASSERT(buf_inc_len(&hash, EVP_MD_size(sha1)));
316  return hash;
317 }
318 
319 struct buffer
320 x509_get_sha256_fingerprint(X509 *cert, struct gc_arena *gc)
321 {
322  const EVP_MD *sha256 = EVP_sha256();
323  struct buffer hash = alloc_buf_gc(EVP_MD_size(sha256), gc);
324  X509_digest(cert, EVP_sha256(), BPTR(&hash), NULL);
325  ASSERT(buf_inc_len(&hash, EVP_MD_size(sha256)));
326  return hash;
327 }
328 
329 char *
330 x509_get_subject(X509 *cert, struct gc_arena *gc)
331 {
332  BIO *subject_bio = NULL;
333  BUF_MEM *subject_mem;
334  char *subject = NULL;
335 
336  subject_bio = BIO_new(BIO_s_mem());
337  if (subject_bio == NULL)
338  {
339  goto err;
340  }
341 
342  X509_NAME_print_ex(subject_bio, X509_get_subject_name(cert),
343  0, XN_FLAG_SEP_CPLUS_SPC | XN_FLAG_FN_SN
344  |ASN1_STRFLGS_UTF8_CONVERT | ASN1_STRFLGS_ESC_CTRL);
345 
346  if (BIO_eof(subject_bio))
347  {
348  goto err;
349  }
350 
351  BIO_get_mem_ptr(subject_bio, &subject_mem);
352 
353  subject = gc_malloc(subject_mem->length + 1, false, gc);
354 
355  memcpy(subject, subject_mem->data, subject_mem->length);
356  subject[subject_mem->length] = '\0';
357 
358 err:
359  if (subject_bio)
360  {
361  BIO_free(subject_bio);
362  }
363 
364  return subject;
365 }
366 
367 
368 /*
369  * x509-track implementation -- save X509 fields to environment,
370  * using the naming convention:
371  *
372  * X509_{cert_depth}_{name}={value}
373  *
374  * This function differs from x509_setenv below in the following ways:
375  *
376  * (1) Only explicitly named attributes in xt are saved, per usage
377  * of "x509-track" program options.
378  * (2) Only the level 0 cert info is saved unless the XT_FULL_CHAIN
379  * flag is set in xt->flags (corresponds with prepending a '+'
380  * to the name when specified by "x509-track" program option).
381  * (3) This function supports both X509 subject name fields as
382  * well as X509 V3 extensions.
383  * (4) This function can return the SHA1 fingerprint of a cert, e.g.
384  * x509-track "+SHA1"
385  * will return the SHA1 fingerprint for each certificate in the
386  * peer chain.
387  */
388 
389 void
390 x509_track_add(const struct x509_track **ll_head, const char *name, int msglevel, struct gc_arena *gc)
391 {
392  struct x509_track *xt;
393  ALLOC_OBJ_CLEAR_GC(xt, struct x509_track, gc);
394  if (*name == '+')
395  {
396  xt->flags |= XT_FULL_CHAIN;
397  ++name;
398  }
399  xt->name = name;
400  xt->nid = OBJ_txt2nid(name);
401  if (xt->nid != NID_undef)
402  {
403  xt->next = *ll_head;
404  *ll_head = xt;
405  }
406  else
407  {
408  msg(msglevel, "x509_track: no such attribute '%s'", name);
409  }
410 }
411 
412 /* worker method for setenv_x509_track */
413 static void
414 do_setenv_x509(struct env_set *es, const char *name, char *value, int depth)
415 {
416  char *name_expand;
417  size_t name_expand_size;
418 
419  string_mod(value, CC_ANY, CC_CRLF, '?');
420  msg(D_X509_ATTR, "X509 ATTRIBUTE name='%s' value='%s' depth=%d", name, value, depth);
421  name_expand_size = 64 + strlen(name);
422  name_expand = (char *) malloc(name_expand_size);
423  check_malloc_return(name_expand);
424  openvpn_snprintf(name_expand, name_expand_size, "X509_%d_%s", depth, name);
425  setenv_str(es, name_expand, value);
426  free(name_expand);
427 }
428 
429 void
430 x509_setenv_track(const struct x509_track *xt, struct env_set *es, const int depth, X509 *x509)
431 {
432  struct gc_arena gc = gc_new();
433  X509_NAME *x509_name = X509_get_subject_name(x509);
434  const char nullc = '\0';
435 
436  while (xt)
437  {
438  if (depth == 0 || (xt->flags & XT_FULL_CHAIN))
439  {
440  switch (xt->nid)
441  {
442  case NID_sha1:
443  case NID_sha256:
444  {
445  struct buffer fp_buf;
446  char *fp_str = NULL;
447 
448  if (xt->nid == NID_sha1)
449  {
450  fp_buf = x509_get_sha1_fingerprint(x509, &gc);
451  }
452  else
453  {
454  fp_buf = x509_get_sha256_fingerprint(x509, &gc);
455  }
456 
457  fp_str = format_hex_ex(BPTR(&fp_buf), BLEN(&fp_buf), 0,
458  1 | FHE_CAPS, ":", &gc);
459  do_setenv_x509(es, xt->name, fp_str, depth);
460  }
461  break;
462 
463  default:
464  {
465  int i = X509_NAME_get_index_by_NID(x509_name, xt->nid, -1);
466  if (i >= 0)
467  {
468  X509_NAME_ENTRY *ent = X509_NAME_get_entry(x509_name, i);
469  if (ent)
470  {
471  ASN1_STRING *val = X509_NAME_ENTRY_get_data(ent);
472  unsigned char *buf = NULL;
473  if (ASN1_STRING_to_UTF8(&buf, val) >= 0)
474  {
475  do_setenv_x509(es, xt->name, (char *)buf, depth);
476  OPENSSL_free(buf);
477  }
478  }
479  }
480  else
481  {
482  i = X509_get_ext_by_NID(x509, xt->nid, -1);
483  if (i >= 0)
484  {
485  X509_EXTENSION *ext = X509_get_ext(x509, i);
486  if (ext)
487  {
488  BIO *bio = BIO_new(BIO_s_mem());
489  if (bio)
490  {
491  if (X509V3_EXT_print(bio, ext, 0, 0))
492  {
493  if (BIO_write(bio, &nullc, 1) == 1)
494  {
495  char *str;
496  BIO_get_mem_data(bio, &str);
497  do_setenv_x509(es, xt->name, str, depth);
498  }
499  }
500  BIO_free(bio);
501  }
502  }
503  }
504  }
505  }
506  }
507  }
508  xt = xt->next;
509  }
510  gc_free(&gc);
511 }
512 
513 /*
514  * Save X509 fields to environment, using the naming convention:
515  *
516  * X509_{cert_depth}_{name}={value}
517  */
518 void
519 x509_setenv(struct env_set *es, int cert_depth, openvpn_x509_cert_t *peer_cert)
520 {
521  int i, n;
522  int fn_nid;
523  ASN1_OBJECT *fn;
524  ASN1_STRING *val;
525  X509_NAME_ENTRY *ent;
526  const char *objbuf;
527  unsigned char *buf = NULL;
528  char *name_expand;
529  size_t name_expand_size;
530  X509_NAME *x509 = X509_get_subject_name(peer_cert);
531 
532  n = X509_NAME_entry_count(x509);
533  for (i = 0; i < n; ++i)
534  {
535  ent = X509_NAME_get_entry(x509, i);
536  if (!ent)
537  {
538  continue;
539  }
540  fn = X509_NAME_ENTRY_get_object(ent);
541  if (!fn)
542  {
543  continue;
544  }
545  val = X509_NAME_ENTRY_get_data(ent);
546  if (!val)
547  {
548  continue;
549  }
550  fn_nid = OBJ_obj2nid(fn);
551  if (fn_nid == NID_undef)
552  {
553  continue;
554  }
555  objbuf = OBJ_nid2sn(fn_nid);
556  if (!objbuf)
557  {
558  continue;
559  }
560  if (ASN1_STRING_to_UTF8(&buf, val) < 0)
561  {
562  continue;
563  }
564  name_expand_size = 64 + strlen(objbuf);
565  name_expand = (char *) malloc(name_expand_size);
566  check_malloc_return(name_expand);
567  openvpn_snprintf(name_expand, name_expand_size, "X509_%d_%s", cert_depth,
568  objbuf);
569  string_mod(name_expand, CC_PRINT, CC_CRLF, '_');
570  string_mod((char *)buf, CC_PRINT, CC_CRLF, '_');
571  setenv_str_incr(es, name_expand, (char *)buf);
572  free(name_expand);
573  OPENSSL_free(buf);
574  }
575 }
576 
577 result_t
579 {
580  if (usage == NS_CERT_CHECK_NONE)
581  {
582  return SUCCESS;
583  }
584  if (usage == NS_CERT_CHECK_CLIENT)
585  {
586  /*
587  * Unfortunately, X509_check_purpose() does some weird thing that
588  * prevent it to take a const argument
589  */
590  result_t result = X509_check_purpose(peer_cert, X509_PURPOSE_SSL_CLIENT, 0) ?
591  SUCCESS : FAILURE;
592 
593  /*
594  * old versions of OpenSSL allow us to make the less strict check we used to
595  * do. If this less strict check pass, warn user that this might not be the
596  * case when its distribution will update to OpenSSL 1.1
597  */
598  if (result == FAILURE)
599  {
600  ASN1_BIT_STRING *ns;
601  ns = X509_get_ext_d2i(peer_cert, NID_netscape_cert_type, NULL, NULL);
602  result = (ns && ns->length > 0 && (ns->data[0] & NS_SSL_CLIENT)) ? SUCCESS : FAILURE;
603  if (result == SUCCESS)
604  {
605  msg(M_WARN, "X509: Certificate is a client certificate yet it's purpose "
606  "cannot be verified (check may fail in the future)");
607  }
608  ASN1_BIT_STRING_free(ns);
609  }
610  return result;
611  }
612  if (usage == NS_CERT_CHECK_SERVER)
613  {
614  /*
615  * Unfortunately, X509_check_purpose() does some weird thing that
616  * prevent it to take a const argument
617  */
618  result_t result = X509_check_purpose(peer_cert, X509_PURPOSE_SSL_SERVER, 0) ?
619  SUCCESS : FAILURE;
620 
621  /*
622  * old versions of OpenSSL allow us to make the less strict check we used to
623  * do. If this less strict check pass, warn user that this might not be the
624  * case when its distribution will update to OpenSSL 1.1
625  */
626  if (result == FAILURE)
627  {
628  ASN1_BIT_STRING *ns;
629  ns = X509_get_ext_d2i(peer_cert, NID_netscape_cert_type, NULL, NULL);
630  result = (ns && ns->length > 0 && (ns->data[0] & NS_SSL_SERVER)) ? SUCCESS : FAILURE;
631  if (result == SUCCESS)
632  {
633  msg(M_WARN, "X509: Certificate is a server certificate yet it's purpose "
634  "cannot be verified (check may fail in the future)");
635  }
636  ASN1_BIT_STRING_free(ns);
637  }
638  return result;
639  }
640 
641  return FAILURE;
642 }
643 
644 result_t
645 x509_verify_cert_ku(X509 *x509, const unsigned *const expected_ku,
646  int expected_len)
647 {
648  ASN1_BIT_STRING *ku = X509_get_ext_d2i(x509, NID_key_usage, NULL, NULL);
649 
650  if (ku == NULL)
651  {
652  msg(D_TLS_ERRORS, "Certificate does not have key usage extension");
653  return FAILURE;
654  }
655 
656  if (expected_ku[0] == OPENVPN_KU_REQUIRED)
657  {
658  /* Extension required, value checked by TLS library */
659  ASN1_BIT_STRING_free(ku);
660  return SUCCESS;
661  }
662 
663  unsigned nku = 0;
664  for (size_t i = 0; i < 8; i++)
665  {
666  if (ASN1_BIT_STRING_get_bit(ku, i))
667  {
668  nku |= 1 << (7 - i);
669  }
670  }
671 
672  /*
673  * Fixup if no LSB bits
674  */
675  if ((nku & 0xff) == 0)
676  {
677  nku >>= 8;
678  }
679 
680  msg(D_HANDSHAKE, "Validating certificate key usage");
681  result_t fFound = FAILURE;
682  for (size_t i = 0; fFound != SUCCESS && i < expected_len; i++)
683  {
684  if (expected_ku[i] != 0 && (nku & expected_ku[i]) == expected_ku[i])
685  {
686  fFound = SUCCESS;
687  }
688  }
689 
690  if (fFound != SUCCESS)
691  {
693  "ERROR: Certificate has key usage %04x, expected one of:", nku);
694  for (size_t i = 0; i < expected_len && expected_ku[i]; i++)
695  {
696  msg(D_TLS_ERRORS, " * %04x", expected_ku[i]);
697  }
698  }
699 
700  ASN1_BIT_STRING_free(ku);
701 
702  return fFound;
703 }
704 
705 result_t
706 x509_verify_cert_eku(X509 *x509, const char *const expected_oid)
707 {
708  EXTENDED_KEY_USAGE *eku = NULL;
709  result_t fFound = FAILURE;
710 
711  if ((eku = (EXTENDED_KEY_USAGE *) X509_get_ext_d2i(x509, NID_ext_key_usage,
712  NULL, NULL)) == NULL)
713  {
714  msg(D_HANDSHAKE, "Certificate does not have extended key usage extension");
715  }
716  else
717  {
718  int i;
719 
720  msg(D_HANDSHAKE, "Validating certificate extended key usage");
721  for (i = 0; SUCCESS != fFound && i < sk_ASN1_OBJECT_num(eku); i++)
722  {
723  ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(eku, i);
724  char szOid[1024];
725 
726  if (SUCCESS != fFound && OBJ_obj2txt(szOid, sizeof(szOid), oid, 0) != -1)
727  {
728  msg(D_HANDSHAKE, "++ Certificate has EKU (str) %s, expects %s",
729  szOid, expected_oid);
730  if (!strcmp(expected_oid, szOid))
731  {
732  fFound = SUCCESS;
733  }
734  }
735  if (SUCCESS != fFound && OBJ_obj2txt(szOid, sizeof(szOid), oid, 1) != -1)
736  {
737  msg(D_HANDSHAKE, "++ Certificate has EKU (oid) %s, expects %s",
738  szOid, expected_oid);
739  if (!strcmp(expected_oid, szOid))
740  {
741  fFound = SUCCESS;
742  }
743  }
744  }
745  }
746 
747  if (eku != NULL)
748  {
749  sk_ASN1_OBJECT_pop_free(eku, ASN1_OBJECT_free);
750  }
751 
752  return fFound;
753 }
754 
755 result_t
756 x509_write_pem(FILE *peercert_file, X509 *peercert)
757 {
758  if (PEM_write_X509(peercert_file, peercert) < 0)
759  {
760  msg(M_NONFATAL, "Failed to write peer certificate in PEM format");
761  return FAILURE;
762  }
763  return SUCCESS;
764 }
765 
766 bool
768 {
769  if (!opt->crl_file || (opt->ssl_flags & SSLF_CRL_VERIFY_DIR))
770  {
771  return false;
772  }
773 
774  X509_STORE *store = SSL_CTX_get_cert_store(opt->ssl_ctx.ctx);
775  if (!store)
776  {
777  crypto_msg(M_FATAL, "Cannot get certificate store");
778  }
779 
780  STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store);
781  for (int i = 0; i < sk_X509_OBJECT_num(objs); i++)
782  {
783  X509_OBJECT *obj = sk_X509_OBJECT_value(objs, i);
784  ASSERT(obj);
785  if (X509_OBJECT_get_type(obj) == X509_LU_CRL)
786  {
787  return false;
788  }
789  }
790  return true;
791 }
792 
793 #endif /* defined(ENABLE_CRYPTO_OPENSSL) */
static void strncpynt(char *dest, const char *src, size_t maxlen)
Definition: buffer.h:348
const char * crl_file
Definition: ssl_common.h:284
#define SSLF_CRL_VERIFY_DIR
Definition: ssl_common.h:344
#define D_TLS_DEBUG_LOW
Definition: errlevel.h:77
char * string_alloc(const char *str, struct gc_arena *gc)
Definition: buffer.c:688
#define D_TLS_DEBUG
Definition: errlevel.h:159
bool tls_verify_crl_missing(const struct tls_options *opt)
Return true iff a CRL is configured, but is not loaded.
static void gc_free(struct gc_arena *a)
Definition: buffer.h:1023
struct tls_root_ctx ssl_ctx
Definition: ssl_common.h:244
bool verified
Definition: ssl_common.h:440
#define NS_CERT_CHECK_CLIENT
Do not perform Netscape certificate type verification.
Definition: ssl_verify.h:216
#define D_TLS_ERRORS
Definition: errlevel.h:59
#define CC_PRINT
Definition: buffer.h:915
X509 openvpn_x509_cert_t
#define ASSERT(x)
Definition: error.h:221
#define M_FATAL
Definition: error.h:94
#define crypto_msg(flags,...)
Retrieve any OpenSSL errors, then print the supplied error message.
void cert_hash_remember(struct tls_session *session, const int error_depth, const struct buffer *cert_hash)
Definition: ssl_verify.c:233
void setenv_str(struct env_set *es, const char *name, const char *value)
Definition: env_set.c:285
void x509_setenv(struct env_set *es, int cert_depth, openvpn_x509_cert_t *peer_cert)
result_t verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_depth)
Definition: ssl_verify.c:656
result_t x509_verify_cert_ku(X509 *x509, const unsigned *const expected_ku, int expected_len)
char * x509_get_subject(X509 *cert, struct gc_arena *gc)
#define D_X509_ATTR
Definition: errlevel.h:104
#define BPTR(buf)
Definition: buffer.h:124
struct buffer x509_get_sha1_fingerprint(X509 *cert, struct gc_arena *gc)
Retrieve the certificate&#39;s SHA1 fingerprint.
bool openvpn_snprintf(char *str, size_t size, const char *format,...)
Definition: buffer.c:299
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:307
static struct gc_arena gc_new(void)
Definition: buffer.h:1015
result_t x509_verify_cert_eku(X509 *x509, const char *const expected_oid)
#define malloc
Definition: cmocka.c:1795
unsigned int flags
Definition: ssl_verify.h:204
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Definition: buffer.c:408
#define NS_CERT_CHECK_NONE
Do not perform Netscape certificate type verification.
Definition: ssl_verify.h:212
static STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *store)
Fetch the X509 object stack from the X509 store.
void x509_setenv_track(const struct x509_track *xt, struct env_set *es, const int depth, X509 *x509)
const struct x509_track * next
Definition: ssl_verify.h:201
static result_t extract_x509_field_ssl(X509_NAME *x509, const char *field_name, char *out, int size)
char * format_hex_ex(const uint8_t *data, int size, int maxoutput, unsigned int space_break_flags, const char *separator, struct gc_arena *gc)
Definition: buffer.c:522
struct buffer x509_get_sha256_fingerprint(X509 *cert, struct gc_arena *gc)
Retrieve the certificate&#39;s SHA256 fingerprint.
const char * name
Definition: ssl_verify.h:202
char * backend_x509_get_serial_hex(openvpn_x509_cert_t *cert, struct gc_arena *gc)
#define BLEN(buf)
Definition: buffer.h:127
int verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
Verify that the remote OpenVPN peer&#39;s certificate allows setting up a VPN tunnel. ...
char * backend_x509_get_serial(openvpn_x509_cert_t *cert, struct gc_arena *gc)
#define D_HANDSHAKE
Definition: errlevel.h:72
SSL_CTX * ctx
Definition: ssl_openssl.h:39
bool string_mod(char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace)
Definition: buffer.c:1081
#define ALLOC_OBJ_CLEAR_GC(dptr, type, gc)
Definition: buffer.h:1087
static void do_setenv_x509(struct env_set *es, const char *name, char *value, int depth)
static bool buf_inc_len(struct buffer *buf, int inc)
Definition: buffer.h:611
static void check_malloc_return(const void *p)
Definition: buffer.h:1093
static void usage(void)
Definition: options.c:4093
Security parameter state of a single session within a VPN tunnel.
Definition: ssl_common.h:407
#define msg
Definition: error.h:173
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
int mydata_index
Allocate space in SSL objects in which to store a struct tls_session pointer back to parent...
Definition: ssl_openssl.c:74
#define FHE_CAPS
Definition: buffer.h:519
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Definition: buffer.c:90
#define free
Definition: cmocka.c:1850
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
result_t backend_x509_get_username(char *common_name, int cn_len, char *x509_username_field, X509 *peer_cert)
result_t x509_write_pem(FILE *peercert_file, X509 *peercert)
void x509_track_add(const struct x509_track **ll_head, const char *name, int msglevel, struct gc_arena *gc)
#define XT_FULL_CHAIN
Definition: ssl_verify.h:203
#define CC_CRLF
Definition: buffer.h:944
#define M_NONFATAL
Definition: error.h:95
#define CC_ANY
Definition: buffer.h:907
unsigned int ssl_flags
Definition: ssl_common.h:349
#define M_WARN
Definition: error.h:96
result_t x509_verify_ns_cert_type(openvpn_x509_cert_t *peer_cert, const int usage)
static int X509_OBJECT_get_type(const X509_OBJECT *obj)
Get the type of an X509 object.
result_t
Result of verification function.
#define NS_CERT_CHECK_SERVER
Do not perform Netscape certificate type verification.
Definition: ssl_verify.h:214
#define OPENVPN_KU_REQUIRED
Require keyUsage to be present in cert (0xFFFF is an invalid KU value)
Definition: ssl_verify.h:219