OpenVPN
ssl_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-2024 OpenVPN Inc <sales@openvpn.net>
9  * Copyright (C) 2010-2021 Fox Crypto B.V. <openvpn@foxcrypto.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 #endif
32 
33 #include "syshead.h"
34 
35 #if defined(ENABLE_CRYPTO_OPENSSL)
36 
37 #include "errlevel.h"
38 #include "buffer.h"
39 #include "misc.h"
40 #include "manage.h"
41 #include "memdbg.h"
42 #include "ssl_backend.h"
43 #include "ssl_common.h"
44 #include "base64.h"
45 #include "openssl_compat.h"
46 #include "xkey_common.h"
47 
48 #ifdef ENABLE_CRYPTOAPI
49 #include "cryptoapi.h"
50 #endif
51 
52 #include "ssl_verify_openssl.h"
53 #include "ssl_util.h"
54 
55 #include <openssl/bn.h>
56 #include <openssl/crypto.h>
57 #include <openssl/dh.h>
58 #include <openssl/dsa.h>
59 #include <openssl/err.h>
60 #include <openssl/pkcs12.h>
61 #include <openssl/rsa.h>
62 #include <openssl/x509.h>
63 #include <openssl/ssl.h>
64 #ifndef OPENSSL_NO_EC
65 #include <openssl/ec.h>
66 #endif
67 
68 #if defined(_MSC_VER) && !defined(_M_ARM64)
69 #include <openssl/applink.c>
70 #endif
71 
72 OSSL_LIB_CTX *tls_libctx; /* Global */
73 
74 static void unload_xkey_provider(void);
75 
76 /*
77  * Allocate space in SSL objects in which to store a struct tls_session
78  * pointer back to parent.
79  *
80  */
81 
82 int mydata_index; /* GLOBAL */
83 
84 void
86 {
87  mydata_index = SSL_get_ex_new_index(0, "struct session *", NULL, NULL, NULL);
88  ASSERT(mydata_index >= 0);
89 }
90 
91 void
93 {
94 }
95 
96 void
98 {
99  ASSERT(NULL != ctx);
100 
101  ctx->ctx = SSL_CTX_new_ex(tls_libctx, NULL, SSLv23_server_method());
102 
103  if (ctx->ctx == NULL)
104  {
105  crypto_msg(M_FATAL, "SSL_CTX_new SSLv23_server_method");
106  }
107  if (ERR_peek_error() != 0)
108  {
109  crypto_msg(M_WARN, "Warning: TLS server context initialisation "
110  "has warnings.");
111  }
112 }
113 
114 void
116 {
117  ASSERT(NULL != ctx);
118 
119  ctx->ctx = SSL_CTX_new_ex(tls_libctx, NULL, SSLv23_client_method());
120 
121  if (ctx->ctx == NULL)
122  {
123  crypto_msg(M_FATAL, "SSL_CTX_new SSLv23_client_method");
124  }
125  if (ERR_peek_error() != 0)
126  {
127  crypto_msg(M_WARN, "Warning: TLS client context initialisation "
128  "has warnings.");
129  }
130 }
131 
132 void
134 {
135  ASSERT(NULL != ctx);
136  SSL_CTX_free(ctx->ctx);
137  ctx->ctx = NULL;
138  unload_xkey_provider(); /* in case it is loaded */
139 }
140 
141 bool
143 {
144  ASSERT(NULL != ctx);
145  return NULL != ctx->ctx;
146 }
147 
148 bool
150  const char *label, size_t label_size,
151  void *ekm, size_t ekm_size)
152 
153 {
154  SSL *ssl = session->key[KS_PRIMARY].ks_ssl.ssl;
155 
156  if (SSL_export_keying_material(ssl, ekm, ekm_size, label,
157  label_size, NULL, 0, 0) == 1)
158  {
159  return true;
160  }
161  else
162  {
163  secure_memzero(ekm, ekm_size);
164  return false;
165  }
166 }
167 
168 /*
169  * Print debugging information on SSL/TLS session negotiation.
170  */
171 
172 #ifndef INFO_CALLBACK_SSL_CONST
173 #define INFO_CALLBACK_SSL_CONST const
174 #endif
175 static void
176 info_callback(INFO_CALLBACK_SSL_CONST SSL *s, int where, int ret)
177 {
178  if (where & SSL_CB_LOOP)
179  {
180  dmsg(D_HANDSHAKE_VERBOSE, "SSL state (%s): %s",
181  where & SSL_ST_CONNECT ? "connect" :
182  where &SSL_ST_ACCEPT ? "accept" :
183  "undefined", SSL_state_string_long(s));
184  }
185  else if (where & SSL_CB_ALERT)
186  {
187  dmsg(D_TLS_DEBUG_LOW, "%s %s SSL alert: %s",
188  where & SSL_CB_READ ? "Received" : "Sent",
189  SSL_alert_type_string_long(ret),
190  SSL_alert_desc_string_long(ret));
191  }
192 }
193 
194 /*
195  * Return maximum TLS version supported by local OpenSSL library.
196  * Assume that presence of SSL_OP_NO_TLSvX macro indicates that
197  * TLSvX is supported.
198  */
199 int
201 {
202 #if defined(TLS1_3_VERSION)
203  /* If this is defined we can safely assume TLS 1.3 support */
204  return TLS_VER_1_3;
205 #elif OPENSSL_VERSION_NUMBER >= 0x10100000L
206  /*
207  * If TLS_VER_1_3 is not defined, we were compiled against a version that
208  * did not support TLS 1.3.
209  *
210  * However, the library we are *linked* against might be OpenSSL 1.1.1
211  * and therefore supports TLS 1.3. This needs to be checked at runtime
212  * since we can be compiled against 1.1.0 and then the library can be
213  * upgraded to 1.1.1.
214  * We only need to check this for OpenSSL versions that can be
215  * upgraded to 1.1.1 without recompile (>= 1.1.0)
216  */
217  if (OpenSSL_version_num() >= 0x1010100fL)
218  {
219  return TLS_VER_1_3;
220  }
221  else
222  {
223  return TLS_VER_1_2;
224  }
225 #elif defined(TLS1_2_VERSION) || defined(SSL_OP_NO_TLSv1_2)
226  return TLS_VER_1_2;
227 #elif defined(TLS1_1_VERSION) || defined(SSL_OP_NO_TLSv1_1)
228  return TLS_VER_1_1;
229 #else /* if defined(TLS1_3_VERSION) */
230  return TLS_VER_1_0;
231 #endif
232 }
233 
235 static int
237 {
238  if (ver == TLS_VER_1_0)
239  {
240  return TLS1_VERSION;
241  }
242  else if (ver == TLS_VER_1_1)
243  {
244  return TLS1_1_VERSION;
245  }
246  else if (ver == TLS_VER_1_2)
247  {
248  return TLS1_2_VERSION;
249  }
250  else if (ver == TLS_VER_1_3)
251  {
252  /*
253  * Supporting the library upgraded to TLS1.3 without recompile
254  * is enough to support here with a simple constant that the same
255  * as in the TLS 1.3, so spec it is very unlikely that OpenSSL
256  * will change this constant
257  */
258 #ifndef TLS1_3_VERSION
259  /*
260  * We do not want to define TLS_VER_1_3 if not defined
261  * since other parts of the code use the existance of this macro
262  * as proxy for TLS 1.3 support
263  */
264  return 0x0304;
265 #else
266  return TLS1_3_VERSION;
267 #endif
268  }
269  return 0;
270 }
271 
272 static bool
273 tls_ctx_set_tls_versions(struct tls_root_ctx *ctx, unsigned int ssl_flags)
274 {
275  int tls_ver_min = openssl_tls_version(
277  int tls_ver_max = openssl_tls_version(
279 
280  if (!tls_ver_min)
281  {
282  /* Enforce at least TLS 1.0 */
283  int cur_min = SSL_CTX_get_min_proto_version(ctx->ctx);
284  tls_ver_min = cur_min < TLS1_VERSION ? TLS1_VERSION : cur_min;
285  }
286 
287  if (!SSL_CTX_set_min_proto_version(ctx->ctx, tls_ver_min))
288  {
289  msg(D_TLS_ERRORS, "%s: failed to set minimum TLS version", __func__);
290  return false;
291  }
292 
293  if (tls_ver_max && !SSL_CTX_set_max_proto_version(ctx->ctx, tls_ver_max))
294  {
295  msg(D_TLS_ERRORS, "%s: failed to set maximum TLS version", __func__);
296  return false;
297  }
298 
299  return true;
300 }
301 
302 bool
303 tls_ctx_set_options(struct tls_root_ctx *ctx, unsigned int ssl_flags)
304 {
305  ASSERT(NULL != ctx);
306 
307  /* process SSL options */
308  long sslopt = SSL_OP_SINGLE_DH_USE | SSL_OP_NO_TICKET;
309 #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE
310  sslopt |= SSL_OP_CIPHER_SERVER_PREFERENCE;
311 #endif
312  sslopt |= SSL_OP_NO_COMPRESSION;
313  /* Disable TLS renegotiations. OpenVPN's renegotiation creates new SSL
314  * session and does not depend on this feature. And TLS renegotiations have
315  * been problematic in the past */
316 #ifdef SSL_OP_NO_RENEGOTIATION
317  sslopt |= SSL_OP_NO_RENEGOTIATION;
318 #endif
319 
320  SSL_CTX_set_options(ctx->ctx, sslopt);
321 
322  if (!tls_ctx_set_tls_versions(ctx, ssl_flags))
323  {
324  return false;
325  }
326 
327 #ifdef SSL_MODE_RELEASE_BUFFERS
328  SSL_CTX_set_mode(ctx->ctx, SSL_MODE_RELEASE_BUFFERS);
329 #endif
330  SSL_CTX_set_session_cache_mode(ctx->ctx, SSL_SESS_CACHE_OFF);
331  SSL_CTX_set_default_passwd_cb(ctx->ctx, pem_password_callback);
332 
333  /* Require peer certificate verification */
334  int verify_flags = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
335  if (ssl_flags & SSLF_CLIENT_CERT_NOT_REQUIRED)
336  {
337  verify_flags = 0;
338  }
339  else if (ssl_flags & SSLF_CLIENT_CERT_OPTIONAL)
340  {
341  verify_flags = SSL_VERIFY_PEER;
342  }
343  SSL_CTX_set_verify(ctx->ctx, verify_flags, verify_callback);
344 
345  SSL_CTX_set_info_callback(ctx->ctx, info_callback);
346 
347  return true;
348 }
349 
350 void
351 convert_tls_list_to_openssl(char *openssl_ciphers, size_t len, const char *ciphers)
352 {
353  /* Parse supplied cipher list and pass on to OpenSSL */
354  size_t begin_of_cipher, end_of_cipher;
355 
356  const char *current_cipher;
357  size_t current_cipher_len;
358 
359  const tls_cipher_name_pair *cipher_pair;
360 
361  size_t openssl_ciphers_len = 0;
362  openssl_ciphers[0] = '\0';
363 
364  /* Translate IANA cipher suite names to OpenSSL names */
365  begin_of_cipher = end_of_cipher = 0;
366  for (; begin_of_cipher < strlen(ciphers); begin_of_cipher = end_of_cipher)
367  {
368  end_of_cipher += strcspn(&ciphers[begin_of_cipher], ":");
369  cipher_pair = tls_get_cipher_name_pair(&ciphers[begin_of_cipher], end_of_cipher - begin_of_cipher);
370 
371  if (NULL == cipher_pair)
372  {
373  /* No translation found, use original */
374  current_cipher = &ciphers[begin_of_cipher];
375  current_cipher_len = end_of_cipher - begin_of_cipher;
376 
377  /* Issue warning on missing translation */
378  /* %.*s format specifier expects length of type int, so guarantee */
379  /* that length is small enough and cast to int. */
380  msg(D_LOW, "No valid translation found for TLS cipher '%.*s'",
381  constrain_int(current_cipher_len, 0, 256), current_cipher);
382  }
383  else
384  {
385  /* Use OpenSSL name */
386  current_cipher = cipher_pair->openssl_name;
387  current_cipher_len = strlen(current_cipher);
388 
389  if (end_of_cipher - begin_of_cipher == current_cipher_len
390  && 0 != memcmp(&ciphers[begin_of_cipher], cipher_pair->iana_name,
391  end_of_cipher - begin_of_cipher))
392  {
393  /* Non-IANA name used, show warning */
394  msg(M_WARN, "Deprecated TLS cipher name '%s', please use IANA name '%s'", cipher_pair->openssl_name, cipher_pair->iana_name);
395  }
396  }
397 
398  /* Make sure new cipher name fits in cipher string */
399  if ((SIZE_MAX - openssl_ciphers_len) < current_cipher_len
400  || (len - 1) < (openssl_ciphers_len + current_cipher_len))
401  {
402  msg(M_FATAL,
403  "Failed to set restricted TLS cipher list, too long (>%d).",
404  (int)(len - 1));
405  }
406 
407  /* Concatenate cipher name to OpenSSL cipher string */
408  memcpy(&openssl_ciphers[openssl_ciphers_len], current_cipher, current_cipher_len);
409  openssl_ciphers_len += current_cipher_len;
410  openssl_ciphers[openssl_ciphers_len] = ':';
411  openssl_ciphers_len++;
412 
413  end_of_cipher++;
414  }
415 
416  if (openssl_ciphers_len > 0)
417  {
418  openssl_ciphers[openssl_ciphers_len-1] = '\0';
419  }
420 }
421 
422 void
423 tls_ctx_restrict_ciphers(struct tls_root_ctx *ctx, const char *ciphers)
424 {
425  if (ciphers == NULL)
426  {
427  /* Use sane default TLS cipher list */
428  if (!SSL_CTX_set_cipher_list(ctx->ctx,
429  /* Use openssl's default list as a basis */
430  "DEFAULT"
431  /* Disable export ciphers and openssl's 'low' and 'medium' ciphers */
432  ":!EXP:!LOW:!MEDIUM"
433  /* Disable static (EC)DH keys (no forward secrecy) */
434  ":!kDH:!kECDH"
435  /* Disable DSA private keys */
436  ":!DSS"
437  /* Disable unsupported TLS modes */
438  ":!PSK:!SRP:!kRSA"))
439  {
440  crypto_msg(M_FATAL, "Failed to set default TLS cipher list.");
441  }
442  return;
443  }
444 
445  char openssl_ciphers[4096];
446  convert_tls_list_to_openssl(openssl_ciphers, sizeof(openssl_ciphers), ciphers);
447 
448  ASSERT(NULL != ctx);
449 
450  /* Set OpenSSL cipher list */
451  if (!SSL_CTX_set_cipher_list(ctx->ctx, openssl_ciphers))
452  {
453  crypto_msg(M_FATAL, "Failed to set restricted TLS cipher list: %s", openssl_ciphers);
454  }
455 }
456 
457 void
458 convert_tls13_list_to_openssl(char *openssl_ciphers, size_t len,
459  const char *ciphers)
460 {
461  /*
462  * OpenSSL (and official IANA) cipher names have _ in them. We
463  * historically used names with - in them. Silently convert names
464  * with - to names with _ to support both
465  */
466  if (strlen(ciphers) >= (len - 1))
467  {
468  msg(M_FATAL,
469  "Failed to set restricted TLS 1.3 cipher list, too long (>%d).",
470  (int) (len - 1));
471  }
472 
473  strncpy(openssl_ciphers, ciphers, len);
474 
475  for (size_t i = 0; i < strlen(openssl_ciphers); i++)
476  {
477  if (openssl_ciphers[i] == '-')
478  {
479  openssl_ciphers[i] = '_';
480  }
481  }
482 }
483 
484 void
485 tls_ctx_restrict_ciphers_tls13(struct tls_root_ctx *ctx, const char *ciphers)
486 {
487  if (ciphers == NULL)
488  {
489  /* default cipher list of OpenSSL 1.1.1 is sane, do not set own
490  * default as we do with tls-cipher */
491  return;
492  }
493 
494 #if !defined(TLS1_3_VERSION)
495  crypto_msg(M_WARN, "Not compiled with OpenSSL 1.1.1 or higher. "
496  "Ignoring TLS 1.3 only tls-ciphersuites '%s' setting.",
497  ciphers);
498 #else
499  ASSERT(NULL != ctx);
500 
501  char openssl_ciphers[4096];
502  convert_tls13_list_to_openssl(openssl_ciphers, sizeof(openssl_ciphers),
503  ciphers);
504 
505  if (!SSL_CTX_set_ciphersuites(ctx->ctx, openssl_ciphers))
506  {
507  crypto_msg(M_FATAL, "Failed to set restricted TLS 1.3 cipher list: %s",
508  openssl_ciphers);
509  }
510 #endif
511 }
512 
513 void
514 tls_ctx_set_cert_profile(struct tls_root_ctx *ctx, const char *profile)
515 {
516 #if OPENSSL_VERSION_NUMBER > 0x10100000L \
517  && (!defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER > 0x3060000fL)
518  /* OpenSSL does not have certificate profiles, but a complex set of
519  * callbacks that we could try to implement to achieve something similar.
520  * For now, use OpenSSL's security levels to achieve similar (but not equal)
521  * behaviour. */
522  if (!profile || 0 == strcmp(profile, "legacy"))
523  {
524  SSL_CTX_set_security_level(ctx->ctx, 1);
525  }
526  else if (0 == strcmp(profile, "insecure"))
527  {
528  SSL_CTX_set_security_level(ctx->ctx, 0);
529  }
530  else if (0 == strcmp(profile, "preferred"))
531  {
532  SSL_CTX_set_security_level(ctx->ctx, 2);
533  }
534  else if (0 == strcmp(profile, "suiteb"))
535  {
536  SSL_CTX_set_security_level(ctx->ctx, 3);
537  SSL_CTX_set_cipher_list(ctx->ctx, "SUITEB128");
538  }
539  else
540  {
541  msg(M_FATAL, "ERROR: Invalid cert profile: %s", profile);
542  }
543 #else /* if OPENSSL_VERSION_NUMBER > 0x10100000L */
544  if (profile)
545  {
546  msg(M_WARN, "WARNING: OpenSSL 1.1.0 and LibreSSL do not support "
547  "--tls-cert-profile, ignoring user-set profile: '%s'", profile);
548  }
549 #endif /* if OPENSSL_VERSION_NUMBER > 0x10100000L */
550 }
551 
552 void
553 tls_ctx_set_tls_groups(struct tls_root_ctx *ctx, const char *groups)
554 {
555  ASSERT(ctx);
556 #if OPENSSL_VERSION_NUMBER < 0x30000000L
557  struct gc_arena gc = gc_new();
558  /* This method could be as easy as
559  * SSL_CTX_set1_groups_list(ctx->ctx, groups)
560  * but OpenSSL (< 3.0) does not like the name secp256r1 for prime256v1
561  * This is one of the important curves.
562  * To support the same name for OpenSSL and mbedTLS, we do
563  * this dance.
564  * Also note that the code is wrong in the presence of OpenSSL3 providers.
565  */
566 
567  int groups_count = get_num_elements(groups, ':');
568 
569  int *glist;
570  /* Allocate an array for them */
571  ALLOC_ARRAY_CLEAR_GC(glist, int, groups_count, &gc);
572 
573  /* Parse allowed ciphers, getting IDs */
574  int glistlen = 0;
575  char *tmp_groups = string_alloc(groups, &gc);
576 
577  const char *token;
578  while ((token = strsep(&tmp_groups, ":")))
579  {
580  if (streq(token, "secp256r1"))
581  {
582  token = "prime256v1";
583  }
584  int nid = OBJ_sn2nid(token);
585 
586  if (nid == 0)
587  {
588  msg(M_WARN, "Warning unknown curve/group specified: %s", token);
589  }
590  else
591  {
592  glist[glistlen] = nid;
593  glistlen++;
594  }
595  }
596 
597  if (!SSL_CTX_set1_groups(ctx->ctx, glist, glistlen))
598  {
599  crypto_msg(M_FATAL, "Failed to set allowed TLS group list: %s",
600  groups);
601  }
602  gc_free(&gc);
603 #else /* if OPENSSL_VERSION_NUMBER < 0x30000000L */
604  if (!SSL_CTX_set1_groups_list(ctx->ctx, groups))
605  {
606  crypto_msg(M_FATAL, "Failed to set allowed TLS group list: %s",
607  groups);
608  }
609 #endif /* if OPENSSL_VERSION_NUMBER < 0x30000000L */
610 }
611 
612 void
614 {
615  int ret;
616  const X509 *cert;
617 
618  ASSERT(ctx);
619 
620  cert = SSL_CTX_get0_certificate(ctx->ctx);
621 
622  if (cert == NULL)
623  {
624  return; /* Nothing to check if there is no certificate */
625  }
626 
627  ret = X509_cmp_time(X509_get0_notBefore(cert), NULL);
628  if (ret == 0)
629  {
630  msg(D_TLS_DEBUG_MED, "Failed to read certificate notBefore field.");
631  }
632  if (ret > 0)
633  {
634  msg(M_WARN, "WARNING: Your certificate is not yet valid!");
635  }
636 
637  ret = X509_cmp_time(X509_get0_notAfter(cert), NULL);
638  if (ret == 0)
639  {
640  msg(D_TLS_DEBUG_MED, "Failed to read certificate notAfter field.");
641  }
642  if (ret < 0)
643  {
644  msg(M_WARN, "WARNING: Your certificate has expired!");
645  }
646 }
647 
648 void
649 tls_ctx_load_dh_params(struct tls_root_ctx *ctx, const char *dh_file,
650  bool dh_file_inline)
651 {
652  BIO *bio;
653 
654  ASSERT(NULL != ctx);
655 
656  if (dh_file_inline)
657  {
658  if (!(bio = BIO_new_mem_buf((char *)dh_file, -1)))
659  {
660  crypto_msg(M_FATAL, "Cannot open memory BIO for inline DH parameters");
661  }
662  }
663  else
664  {
665  /* Get Diffie Hellman Parameters */
666  if (!(bio = BIO_new_file(dh_file, "r")))
667  {
668  crypto_msg(M_FATAL, "Cannot open %s for DH parameters", dh_file);
669  }
670  }
671 
672 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
673  EVP_PKEY *dh = PEM_read_bio_Parameters(bio, NULL);
674  BIO_free(bio);
675 
676  if (!dh)
677  {
678  crypto_msg(M_FATAL, "Cannot load DH parameters from %s",
679  print_key_filename(dh_file, dh_file_inline));
680  }
681  if (!SSL_CTX_set0_tmp_dh_pkey(ctx->ctx, dh))
682  {
683  crypto_msg(M_FATAL, "SSL_CTX_set0_tmp_dh_pkey");
684  }
685 
686  msg(D_TLS_DEBUG_LOW, "Diffie-Hellman initialized with %d bit key",
687  8 * EVP_PKEY_get_size(dh));
688 #else /* if OPENSSL_VERSION_NUMBER >= 0x30000000L */
689  DH *dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
690  BIO_free(bio);
691 
692  if (!dh)
693  {
694  crypto_msg(M_FATAL, "Cannot load DH parameters from %s",
695  print_key_filename(dh_file, dh_file_inline));
696  }
697  if (!SSL_CTX_set_tmp_dh(ctx->ctx, dh))
698  {
699  crypto_msg(M_FATAL, "SSL_CTX_set_tmp_dh");
700  }
701 
702  msg(D_TLS_DEBUG_LOW, "Diffie-Hellman initialized with %d bit key",
703  8 * DH_size(dh));
704 
705  DH_free(dh);
706 #endif /* if OPENSSL_VERSION_NUMBER >= 0x30000000L */
707 }
708 
709 void
710 tls_ctx_load_ecdh_params(struct tls_root_ctx *ctx, const char *curve_name)
711 {
712 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
713  if (curve_name != NULL)
714  {
715  msg(M_WARN, "WARNING: OpenSSL 3.0+ builds do not support specifying an "
716  "ECDH curve with --ecdh-curve, using default curves. Use "
717  "--tls-groups to specify groups.");
718  }
719 #elif !defined(OPENSSL_NO_EC)
720  int nid = NID_undef;
721  EC_KEY *ecdh = NULL;
722  const char *sname = NULL;
723 
724  /* Generate a new ECDH key for each SSL session (for non-ephemeral ECDH) */
725  SSL_CTX_set_options(ctx->ctx, SSL_OP_SINGLE_ECDH_USE);
726 
727  if (curve_name != NULL)
728  {
729  /* Use user supplied curve if given */
730  msg(D_TLS_DEBUG, "Using user specified ECDH curve (%s)", curve_name);
731  nid = OBJ_sn2nid(curve_name);
732  }
733  else
734  {
735  return;
736  }
737 
738  /* Translate NID back to name , just for kicks */
739  sname = OBJ_nid2sn(nid);
740  if (sname == NULL)
741  {
742  sname = "(Unknown)";
743  }
744 
745  /* Create new EC key and set as ECDH key */
746  if (NID_undef == nid || NULL == (ecdh = EC_KEY_new_by_curve_name(nid)))
747  {
748  /* Creating key failed, fall back on sane default */
749  ecdh = EC_KEY_new_by_curve_name(NID_secp384r1);
750  const char *source = (NULL == curve_name) ?
751  "extract curve from certificate" : "use supplied curve";
753  "Failed to %s (%s), using secp384r1 instead.", source, sname);
754  sname = OBJ_nid2sn(NID_secp384r1);
755  }
756 
757  if (!SSL_CTX_set_tmp_ecdh(ctx->ctx, ecdh))
758  {
759  crypto_msg(M_FATAL, "SSL_CTX_set_tmp_ecdh: cannot add curve");
760  }
761 
762  msg(D_TLS_DEBUG_LOW, "ECDH curve %s added", sname);
763 
764  EC_KEY_free(ecdh);
765 #else /* ifndef OPENSSL_NO_EC */
766  msg(D_LOW, "Your OpenSSL library was built without elliptic curve support."
767  " Skipping ECDH parameter loading.");
768 #endif /* OPENSSL_NO_EC */
769 }
770 
771 int
772 tls_ctx_load_pkcs12(struct tls_root_ctx *ctx, const char *pkcs12_file,
773  bool pkcs12_file_inline, bool load_ca_file)
774 {
775  FILE *fp;
776  EVP_PKEY *pkey;
777  X509 *cert;
778  STACK_OF(X509) *ca = NULL;
779  PKCS12 *p12;
780  int i;
781  char password[256];
782 
783  ASSERT(NULL != ctx);
784 
785  if (pkcs12_file_inline)
786  {
787  BIO *b64 = BIO_new(BIO_f_base64());
788  BIO *bio = BIO_new_mem_buf((void *) pkcs12_file,
789  (int) strlen(pkcs12_file));
790  ASSERT(b64 && bio);
791  BIO_push(b64, bio);
792  p12 = d2i_PKCS12_bio(b64, NULL);
793  if (!p12)
794  {
795  crypto_msg(M_FATAL, "Error reading inline PKCS#12 file");
796  }
797  BIO_free(b64);
798  BIO_free(bio);
799  }
800  else
801  {
802  /* Load the PKCS #12 file */
803  if (!(fp = platform_fopen(pkcs12_file, "rb")))
804  {
805  crypto_msg(M_FATAL, "Error opening file %s", pkcs12_file);
806  }
807  p12 = d2i_PKCS12_fp(fp, NULL);
808  fclose(fp);
809  if (!p12)
810  {
811  crypto_msg(M_FATAL, "Error reading PKCS#12 file %s", pkcs12_file);
812  }
813  }
814 
815  /* Parse the PKCS #12 file */
816  if (!PKCS12_parse(p12, "", &pkey, &cert, &ca))
817  {
818  pem_password_callback(password, sizeof(password) - 1, 0, NULL);
819  /* Reparse the PKCS #12 file with password */
820  ca = NULL;
821  if (!PKCS12_parse(p12, password, &pkey, &cert, &ca))
822  {
823  crypto_msg(M_WARN, "Decoding PKCS12 failed. Probably wrong password "
824  "or unsupported/legacy encryption");
825 #ifdef ENABLE_MANAGEMENT
826  if (management && (ERR_GET_REASON(ERR_peek_error()) == PKCS12_R_MAC_VERIFY_FAILURE))
827  {
829  }
830 #endif
831  PKCS12_free(p12);
832  return 1;
833  }
834  }
835  PKCS12_free(p12);
836 
837  /* Load Certificate */
838  if (!SSL_CTX_use_certificate(ctx->ctx, cert))
839  {
841  crypto_msg(M_FATAL, "Cannot use certificate");
842  }
843 
844  /* Load Private Key */
845  if (!SSL_CTX_use_PrivateKey(ctx->ctx, pkey))
846  {
847  crypto_msg(M_FATAL, "Cannot use private key");
848  }
849 
850  /* Check Private Key */
851  if (!SSL_CTX_check_private_key(ctx->ctx))
852  {
853  crypto_msg(M_FATAL, "Private key does not match the certificate");
854  }
855 
856  /* Set Certificate Verification chain */
857  if (load_ca_file)
858  {
859  /* Add CAs from PKCS12 to the cert store and mark them as trusted.
860  * They're also used to fill in the chain of intermediate certs as
861  * necessary.
862  */
863  if (ca && sk_X509_num(ca))
864  {
865  for (i = 0; i < sk_X509_num(ca); i++)
866  {
867  X509_STORE *cert_store = SSL_CTX_get_cert_store(ctx->ctx);
868  if (!X509_STORE_add_cert(cert_store, sk_X509_value(ca, i)))
869  {
870  crypto_msg(M_FATAL, "Cannot add certificate to certificate chain (X509_STORE_add_cert)");
871  }
872  if (!SSL_CTX_add_client_CA(ctx->ctx, sk_X509_value(ca, i)))
873  {
874  crypto_msg(M_FATAL, "Cannot add certificate to client CA list (SSL_CTX_add_client_CA)");
875  }
876  }
877  }
878  }
879  else
880  {
881  /* If trusted CA certs were loaded from a PEM file, and we ignore the
882  * ones in PKCS12, do load PKCS12-provided certs to the client extra
883  * certs chain just in case they include intermediate CAs needed to
884  * prove my identity to the other end. This does not make them trusted.
885  */
886  if (ca && sk_X509_num(ca))
887  {
888  for (i = 0; i < sk_X509_num(ca); i++)
889  {
890  if (!SSL_CTX_add_extra_chain_cert(ctx->ctx, sk_X509_value(ca, i)))
891  {
892  crypto_msg(M_FATAL, "Cannot add extra certificate to chain (SSL_CTX_add_extra_chain_cert)");
893  }
894  }
895  }
896  }
897  return 0;
898 }
899 
900 #ifdef ENABLE_CRYPTOAPI
901 void
902 tls_ctx_load_cryptoapi(struct tls_root_ctx *ctx, const char *cryptoapi_cert)
903 {
904  ASSERT(NULL != ctx);
905 
906  /* Load Certificate and Private Key */
907  if (!SSL_CTX_use_CryptoAPI_certificate(ctx->ctx, cryptoapi_cert))
908  {
909  crypto_msg(M_FATAL, "Cannot load certificate \"%s\" from Microsoft Certificate Store", cryptoapi_cert);
910  }
911 }
912 #endif /* ENABLE_CRYPTOAPI */
913 
914 static void
915 tls_ctx_add_extra_certs(struct tls_root_ctx *ctx, BIO *bio, bool optional)
916 {
917  X509 *cert;
918  while (true)
919  {
920  cert = NULL;
921  if (!PEM_read_bio_X509(bio, &cert, NULL, NULL))
922  {
923  /* a PEM_R_NO_START_LINE "Error" indicates that no certificate
924  * is found in the buffer. If loading more certificates is
925  * optional, break without raising an error
926  */
927  if (optional
928  && ERR_GET_REASON(ERR_peek_error()) == PEM_R_NO_START_LINE)
929  {
930  /* remove that error from error stack */
931  (void)ERR_get_error();
932  break;
933  }
934 
935  /* Otherwise, bail out with error */
936  crypto_msg(M_FATAL, "Error reading extra certificate");
937  }
938  /* takes ownership of cert like a set1 method */
939  if (SSL_CTX_add_extra_chain_cert(ctx->ctx, cert) != 1)
940  {
941  crypto_msg(M_FATAL, "Error adding extra certificate");
942  }
943  /* We loaded at least one certificate, so loading more is optional */
944  optional = true;
945  }
946 }
947 
948 void
949 tls_ctx_load_cert_file(struct tls_root_ctx *ctx, const char *cert_file,
950  bool cert_file_inline)
951 {
952  BIO *in = NULL;
953  X509 *x = NULL;
954  int ret = 0;
955 
956  ASSERT(NULL != ctx);
957 
958  if (cert_file_inline)
959  {
960  in = BIO_new_mem_buf((char *) cert_file, -1);
961  }
962  else
963  {
964  in = BIO_new_file(cert_file, "r");
965  }
966 
967  if (in == NULL)
968  {
969  SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
970  goto end;
971  }
972 
973  x = PEM_read_bio_X509(in, NULL,
974  SSL_CTX_get_default_passwd_cb(ctx->ctx),
975  SSL_CTX_get_default_passwd_cb_userdata(ctx->ctx));
976  if (x == NULL)
977  {
978  SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_PEM_LIB);
979  goto end;
980  }
981 
982  ret = SSL_CTX_use_certificate(ctx->ctx, x);
983  if (ret)
984  {
985  tls_ctx_add_extra_certs(ctx, in, true);
986  }
987 
988 end:
989  if (!ret)
990  {
992  if (cert_file_inline)
993  {
994  crypto_msg(M_FATAL, "Cannot load inline certificate file");
995  }
996  else
997  {
998  crypto_msg(M_FATAL, "Cannot load certificate file %s", cert_file);
999  }
1000  }
1001  else
1002  {
1004  }
1005 
1006  BIO_free(in);
1007  X509_free(x);
1008 }
1009 
1010 int
1011 tls_ctx_load_priv_file(struct tls_root_ctx *ctx, const char *priv_key_file,
1012  bool priv_key_file_inline)
1013 {
1014  SSL_CTX *ssl_ctx = NULL;
1015  BIO *in = NULL;
1016  EVP_PKEY *pkey = NULL;
1017  int ret = 1;
1018 
1019  ASSERT(NULL != ctx);
1020 
1021  ssl_ctx = ctx->ctx;
1022 
1023  if (priv_key_file_inline)
1024  {
1025  in = BIO_new_mem_buf((char *) priv_key_file, -1);
1026  }
1027  else
1028  {
1029  in = BIO_new_file(priv_key_file, "r");
1030  }
1031 
1032  if (!in)
1033  {
1034  goto end;
1035  }
1036 
1037  pkey = PEM_read_bio_PrivateKey(in, NULL,
1038  SSL_CTX_get_default_passwd_cb(ctx->ctx),
1039  SSL_CTX_get_default_passwd_cb_userdata(ctx->ctx));
1040 
1041  if (!pkey || !SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
1042  {
1043 #ifdef ENABLE_MANAGEMENT
1044  if (management && (ERR_GET_REASON(ERR_peek_error()) == EVP_R_BAD_DECRYPT))
1045  {
1047  }
1048 #endif
1049  crypto_msg(M_WARN, "Cannot load private key file %s",
1050  print_key_filename(priv_key_file, priv_key_file_inline));
1051  goto end;
1052  }
1053 
1054  /* Check Private Key */
1055  if (!SSL_CTX_check_private_key(ssl_ctx))
1056  {
1057  crypto_msg(M_FATAL, "Private key does not match the certificate");
1058  }
1059  ret = 0;
1060 
1061 end:
1062  EVP_PKEY_free(pkey);
1063  BIO_free(in);
1064  return ret;
1065 }
1066 
1067 void
1068 backend_tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx, const char *crl_file,
1069  bool crl_inline)
1070 {
1071  BIO *in = NULL;
1072 
1073  X509_STORE *store = SSL_CTX_get_cert_store(ssl_ctx->ctx);
1074  if (!store)
1075  {
1076  crypto_msg(M_FATAL, "Cannot get certificate store");
1077  }
1078 
1079  /* Always start with a cleared CRL list, for that we
1080  * we need to manually find the CRL object from the stack
1081  * and remove it */
1082  STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store);
1083  for (int i = 0; i < sk_X509_OBJECT_num(objs); i++)
1084  {
1085  X509_OBJECT *obj = sk_X509_OBJECT_value(objs, i);
1086  ASSERT(obj);
1087  if (X509_OBJECT_get_type(obj) == X509_LU_CRL)
1088  {
1089  sk_X509_OBJECT_delete(objs, i);
1090  X509_OBJECT_free(obj);
1091  }
1092  }
1093 
1094  X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
1095 
1096  if (crl_inline)
1097  {
1098  in = BIO_new_mem_buf((char *) crl_file, -1);
1099  }
1100  else
1101  {
1102  in = BIO_new_file(crl_file, "r");
1103  }
1104 
1105  if (in == NULL)
1106  {
1107  msg(M_WARN, "CRL: cannot read: %s",
1108  print_key_filename(crl_file, crl_inline));
1109  goto end;
1110  }
1111 
1112  int num_crls_loaded = 0;
1113  while (true)
1114  {
1115  X509_CRL *crl = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL);
1116  if (crl == NULL)
1117  {
1118  /*
1119  * PEM_R_NO_START_LINE can be considered equivalent to EOF.
1120  */
1121  bool eof = ERR_GET_REASON(ERR_peek_error()) == PEM_R_NO_START_LINE;
1122  /* but warn if no CRLs have been loaded */
1123  if (num_crls_loaded > 0 && eof)
1124  {
1125  /* remove that error from error stack */
1126  (void)ERR_get_error();
1127  break;
1128  }
1129 
1130  crypto_msg(M_WARN, "CRL: cannot read CRL from file %s",
1131  print_key_filename(crl_file, crl_inline));
1132  break;
1133  }
1134 
1135  if (!X509_STORE_add_crl(store, crl))
1136  {
1137  X509_CRL_free(crl);
1138  crypto_msg(M_WARN, "CRL: cannot add %s to store",
1139  print_key_filename(crl_file, crl_inline));
1140  break;
1141  }
1142  X509_CRL_free(crl);
1143  num_crls_loaded++;
1144  }
1145  msg(M_INFO, "CRL: loaded %d CRLs from file %s", num_crls_loaded, crl_file);
1146 end:
1147  BIO_free(in);
1148 }
1149 
1150 
1151 #if defined(ENABLE_MANAGEMENT) && !defined(HAVE_XKEY_PROVIDER)
1152 
1153 /* encrypt */
1154 static int
1155 rsa_pub_enc(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
1156 {
1157  ASSERT(0);
1158  return -1;
1159 }
1160 
1161 /* verify arbitrary data */
1162 static int
1163 rsa_pub_dec(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
1164 {
1165  ASSERT(0);
1166  return -1;
1167 }
1168 
1169 /* decrypt */
1170 static int
1171 rsa_priv_dec(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
1172 {
1173  ASSERT(0);
1174  return -1;
1175 }
1176 
1177 /* called at RSA_free */
1178 static int
1180 {
1181  /* meth was allocated in tls_ctx_use_management_external_key() ; since
1182  * this function is called when the parent RSA object is destroyed,
1183  * it is no longer used after this point so kill it. */
1184  const RSA_METHOD *meth = RSA_get_method(rsa);
1185  RSA_meth_free((RSA_METHOD *)meth);
1186  return 1;
1187 }
1188 
1189 /*
1190  * Convert OpenSSL's constant to the strings used in the management
1191  * interface query
1192  */
1193 const char *
1194 get_rsa_padding_name(const int padding)
1195 {
1196  switch (padding)
1197  {
1198  case RSA_PKCS1_PADDING:
1199  return "RSA_PKCS1_PADDING";
1200 
1201  case RSA_NO_PADDING:
1202  return "RSA_NO_PADDING";
1203 
1204  default:
1205  return "UNKNOWN";
1206  }
1207 }
1208 
1220 static int
1221 get_sig_from_man(const unsigned char *dgst, unsigned int dgstlen,
1222  unsigned char *sig, unsigned int siglen,
1223  const char *algorithm)
1224 {
1225  char *in_b64 = NULL;
1226  char *out_b64 = NULL;
1227  int len = -1;
1228 
1229  int bencret = openvpn_base64_encode(dgst, dgstlen, &in_b64);
1230 
1231  if (management && bencret > 0)
1232  {
1233  out_b64 = management_query_pk_sig(management, in_b64, algorithm);
1234 
1235  }
1236  if (out_b64)
1237  {
1238  len = openvpn_base64_decode(out_b64, sig, siglen);
1239  }
1240 
1241  free(in_b64);
1242  free(out_b64);
1243  return len;
1244 }
1245 
1246 /* sign arbitrary data */
1247 static int
1248 rsa_priv_enc(int flen, const unsigned char *from, unsigned char *to, RSA *rsa,
1249  int padding)
1250 {
1251  unsigned int len = RSA_size(rsa);
1252  int ret = -1;
1253 
1254  if (padding != RSA_PKCS1_PADDING && padding != RSA_NO_PADDING)
1255  {
1256  RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
1257  return -1;
1258  }
1259 
1260  ret = get_sig_from_man(from, flen, to, len, get_rsa_padding_name(padding));
1261 
1262  return (ret == len) ? ret : -1;
1263 }
1264 
1265 static int
1266 tls_ctx_use_external_rsa_key(struct tls_root_ctx *ctx, EVP_PKEY *pkey)
1267 {
1268  RSA *rsa = NULL;
1269  RSA_METHOD *rsa_meth;
1270 
1271  ASSERT(NULL != ctx);
1272 
1273  const RSA *pub_rsa = EVP_PKEY_get0_RSA(pkey);
1274  ASSERT(NULL != pub_rsa);
1275 
1276  /* allocate custom RSA method object */
1277  rsa_meth = RSA_meth_new("OpenVPN external private key RSA Method",
1278  RSA_METHOD_FLAG_NO_CHECK);
1279  check_malloc_return(rsa_meth);
1280  RSA_meth_set_pub_enc(rsa_meth, rsa_pub_enc);
1281  RSA_meth_set_pub_dec(rsa_meth, rsa_pub_dec);
1282  RSA_meth_set_priv_enc(rsa_meth, rsa_priv_enc);
1283  RSA_meth_set_priv_dec(rsa_meth, rsa_priv_dec);
1284  RSA_meth_set_init(rsa_meth, NULL);
1285  RSA_meth_set_finish(rsa_meth, openvpn_extkey_rsa_finish);
1286  RSA_meth_set0_app_data(rsa_meth, NULL);
1287 
1288  /* allocate RSA object */
1289  rsa = RSA_new();
1290  if (rsa == NULL)
1291  {
1292  SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_MALLOC_FAILURE);
1293  goto err;
1294  }
1295 
1296  /* initialize RSA object */
1297  const BIGNUM *n = NULL;
1298  const BIGNUM *e = NULL;
1299  RSA_get0_key(pub_rsa, &n, &e, NULL);
1300  RSA_set0_key(rsa, BN_dup(n), BN_dup(e), NULL);
1301  RSA_set_flags(rsa, RSA_flags(rsa) | RSA_FLAG_EXT_PKEY);
1302  if (!RSA_set_method(rsa, rsa_meth))
1303  {
1304  RSA_meth_free(rsa_meth);
1305  goto err;
1306  }
1307  /* from this point rsa_meth will get freed with rsa */
1308 
1309  /* bind our custom RSA object to ssl_ctx */
1310  if (!SSL_CTX_use_RSAPrivateKey(ctx->ctx, rsa))
1311  {
1312  goto err;
1313  }
1314 
1315  RSA_free(rsa); /* doesn't necessarily free, just decrements refcount */
1316  return 1;
1317 
1318 err:
1319  if (rsa)
1320  {
1321  RSA_free(rsa);
1322  }
1323  else if (rsa_meth)
1324  {
1325  RSA_meth_free(rsa_meth);
1326  }
1327  return 0;
1328 }
1329 
1330 #if !defined(OPENSSL_NO_EC)
1331 
1332 /* called when EC_KEY is destroyed */
1333 static void
1335 {
1336  /* release the method structure */
1337  const EC_KEY_METHOD *ec_meth = EC_KEY_get_method(ec);
1338  EC_KEY_METHOD_free((EC_KEY_METHOD *) ec_meth);
1339 }
1340 
1341 /* EC_KEY_METHOD callback: sign().
1342  * Sign the hash using EC key and return DER encoded signature in sig,
1343  * its length in siglen. Return value is 1 on success, 0 on error.
1344  */
1345 static int
1346 ecdsa_sign(int type, const unsigned char *dgst, int dgstlen, unsigned char *sig,
1347  unsigned int *siglen, const BIGNUM *kinv, const BIGNUM *r, EC_KEY *ec)
1348 {
1349  int capacity = ECDSA_size(ec);
1350  /*
1351  * ECDSA does not seem to have proper constants for paddings since
1352  * there are only signatures without padding at the moment, use
1353  * a generic ECDSA for the moment
1354  */
1355  int len = get_sig_from_man(dgst, dgstlen, sig, capacity, "ECDSA");
1356 
1357  if (len > 0)
1358  {
1359  *siglen = len;
1360  return 1;
1361  }
1362  return 0;
1363 }
1364 
1365 /* EC_KEY_METHOD callback: sign_setup(). We do no precomputations */
1366 static int
1367 ecdsa_sign_setup(EC_KEY *ec, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
1368 {
1369  return 1;
1370 }
1371 
1372 /* EC_KEY_METHOD callback: sign_sig().
1373  * Sign the hash and return the result as a newly allocated ECDS_SIG
1374  * struct or NULL on error.
1375  */
1376 static ECDSA_SIG *
1377 ecdsa_sign_sig(const unsigned char *dgst, int dgstlen, const BIGNUM *in_kinv,
1378  const BIGNUM *in_r, EC_KEY *ec)
1379 {
1380  ECDSA_SIG *ecsig = NULL;
1381  unsigned int len = ECDSA_size(ec);
1382  struct gc_arena gc = gc_new();
1383 
1384  unsigned char *buf = gc_malloc(len, false, &gc);
1385  if (ecdsa_sign(0, dgst, dgstlen, buf, &len, NULL, NULL, ec) != 1)
1386  {
1387  goto out;
1388  }
1389  /* const char ** should be avoided: not up to us, so we cast our way through */
1390  ecsig = d2i_ECDSA_SIG(NULL, (const unsigned char **)&buf, len);
1391 
1392 out:
1393  gc_free(&gc);
1394  return ecsig;
1395 }
1396 
1397 static int
1398 tls_ctx_use_external_ec_key(struct tls_root_ctx *ctx, EVP_PKEY *pkey)
1399 {
1400  EC_KEY *ec = NULL;
1401  EVP_PKEY *privkey = NULL;
1402  EC_KEY_METHOD *ec_method;
1403 
1404  ASSERT(ctx);
1405 
1406  ec_method = EC_KEY_METHOD_new(EC_KEY_OpenSSL());
1407  if (!ec_method)
1408  {
1409  goto err;
1410  }
1411 
1412  /* Among init methods, we only need the finish method */
1413  EC_KEY_METHOD_set_init(ec_method, NULL, openvpn_extkey_ec_finish, NULL, NULL, NULL, NULL);
1414  EC_KEY_METHOD_set_sign(ec_method, ecdsa_sign, ecdsa_sign_setup, ecdsa_sign_sig);
1415 
1416  ec = EC_KEY_dup(EVP_PKEY_get0_EC_KEY(pkey));
1417  if (!ec)
1418  {
1419  EC_KEY_METHOD_free(ec_method);
1420  goto err;
1421  }
1422  if (!EC_KEY_set_method(ec, ec_method))
1423  {
1424  EC_KEY_METHOD_free(ec_method);
1425  goto err;
1426  }
1427  /* from this point ec_method will get freed when ec is freed */
1428 
1429  privkey = EVP_PKEY_new();
1430  if (!EVP_PKEY_assign_EC_KEY(privkey, ec))
1431  {
1432  goto err;
1433  }
1434  /* from this point ec will get freed when privkey is freed */
1435 
1436  if (!SSL_CTX_use_PrivateKey(ctx->ctx, privkey))
1437  {
1438  ec = NULL; /* avoid double freeing it below */
1439  goto err;
1440  }
1441 
1442  EVP_PKEY_free(privkey); /* this will down ref privkey and ec */
1443  return 1;
1444 
1445 err:
1446  /* Reach here only when ec and privkey can be independenly freed */
1447  EVP_PKEY_free(privkey);
1448  EC_KEY_free(ec);
1449  return 0;
1450 }
1451 #endif /* !defined(OPENSSL_NO_EC) */
1452 #endif /* ENABLE_MANAGEMENT && !HAVE_XKEY_PROVIDER */
1453 
1454 #ifdef ENABLE_MANAGEMENT
1455 int
1457 {
1458  int ret = 1;
1459 
1460  ASSERT(NULL != ctx);
1461 
1462  X509 *cert = SSL_CTX_get0_certificate(ctx->ctx);
1463 
1464  ASSERT(NULL != cert);
1465 
1466  /* get the public key */
1467  EVP_PKEY *pkey = X509_get0_pubkey(cert);
1468  ASSERT(pkey); /* NULL before SSL_CTX_use_certificate() is called */
1469 
1470 #ifdef HAVE_XKEY_PROVIDER
1471  EVP_PKEY *privkey = xkey_load_management_key(tls_libctx, pkey);
1472  if (!privkey
1473  || !SSL_CTX_use_PrivateKey(ctx->ctx, privkey))
1474  {
1475  EVP_PKEY_free(privkey);
1476  goto cleanup;
1477  }
1478  EVP_PKEY_free(privkey);
1479 #else /* ifdef HAVE_XKEY_PROVIDER */
1480 #if OPENSSL_VERSION_NUMBER < 0x30000000L
1481  if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA)
1482 #else /* OPENSSL_VERSION_NUMBER < 0x30000000L */
1483  if (EVP_PKEY_is_a(pkey, "RSA"))
1484 #endif /* OPENSSL_VERSION_NUMBER < 0x30000000L */
1485  {
1486  if (!tls_ctx_use_external_rsa_key(ctx, pkey))
1487  {
1488  goto cleanup;
1489  }
1490  }
1491 #if !defined(OPENSSL_NO_EC)
1492 #if OPENSSL_VERSION_NUMBER < 0x30000000L
1493  else if (EVP_PKEY_id(pkey) == EVP_PKEY_EC)
1494 #else /* OPENSSL_VERSION_NUMBER < 0x30000000L */
1495  else if (EVP_PKEY_is_a(pkey, "EC"))
1496 #endif /* OPENSSL_VERSION_NUMBER < 0x30000000L */
1497  {
1498  if (!tls_ctx_use_external_ec_key(ctx, pkey))
1499  {
1500  goto cleanup;
1501  }
1502  }
1503  else
1504  {
1505  crypto_msg(M_WARN, "management-external-key requires an RSA or EC certificate");
1506  goto cleanup;
1507  }
1508 #else /* !defined(OPENSSL_NO_EC) */
1509  else
1510  {
1511  crypto_msg(M_WARN, "management-external-key requires an RSA certificate");
1512  goto cleanup;
1513  }
1514 #endif /* !defined(OPENSSL_NO_EC) */
1515 
1516 #endif /* HAVE_XKEY_PROVIDER */
1517 
1518  ret = 0;
1519 cleanup:
1520  if (ret)
1521  {
1522  crypto_msg(M_FATAL, "Cannot enable SSL external private key capability");
1523  }
1524  return ret;
1525 }
1526 
1527 #endif /* ifdef ENABLE_MANAGEMENT */
1528 
1529 static int
1530 sk_x509_name_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
1531 {
1532  return X509_NAME_cmp(*a, *b);
1533 }
1534 
1535 void
1536 tls_ctx_load_ca(struct tls_root_ctx *ctx, const char *ca_file,
1537  bool ca_file_inline, const char *ca_path, bool tls_server)
1538 {
1539  STACK_OF(X509_INFO) *info_stack = NULL;
1540  STACK_OF(X509_NAME) *cert_names = NULL;
1541  X509_LOOKUP *lookup = NULL;
1542  X509_STORE *store = NULL;
1543  X509_NAME *xn = NULL;
1544  BIO *in = NULL;
1545  int i, added = 0, prev = 0;
1546 
1547  ASSERT(NULL != ctx);
1548 
1549  store = SSL_CTX_get_cert_store(ctx->ctx);
1550  if (!store)
1551  {
1552  crypto_msg(M_FATAL, "Cannot get certificate store");
1553  }
1554 
1555  /* Try to add certificates and CRLs from ca_file */
1556  if (ca_file)
1557  {
1558  if (ca_file_inline)
1559  {
1560  in = BIO_new_mem_buf((char *)ca_file, -1);
1561  }
1562  else
1563  {
1564  in = BIO_new_file(ca_file, "r");
1565  }
1566 
1567  if (in)
1568  {
1569  info_stack = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL);
1570  }
1571 
1572  if (info_stack)
1573  {
1574  for (i = 0; i < sk_X509_INFO_num(info_stack); i++)
1575  {
1576  X509_INFO *info = sk_X509_INFO_value(info_stack, i);
1577  if (info->crl)
1578  {
1579  X509_STORE_add_crl(store, info->crl);
1580  }
1581 
1582  if (tls_server && !info->x509)
1583  {
1584  crypto_msg(M_FATAL, "X509 name was missing in TLS mode");
1585  }
1586 
1587  if (info->x509)
1588  {
1589  X509_STORE_add_cert(store, info->x509);
1590  added++;
1591 
1592  if (!tls_server)
1593  {
1594  continue;
1595  }
1596 
1597  /* Use names of CAs as a client CA list */
1598  if (cert_names == NULL)
1599  {
1600  cert_names = sk_X509_NAME_new(sk_x509_name_cmp);
1601  if (!cert_names)
1602  {
1603  continue;
1604  }
1605  }
1606 
1607  xn = X509_get_subject_name(info->x509);
1608  if (!xn)
1609  {
1610  continue;
1611  }
1612 
1613  /* Don't add duplicate CA names */
1614  if (sk_X509_NAME_find(cert_names, xn) == -1)
1615  {
1616  xn = X509_NAME_dup(xn);
1617  if (!xn)
1618  {
1619  continue;
1620  }
1621  sk_X509_NAME_push(cert_names, xn);
1622  }
1623  }
1624 
1625  if (tls_server)
1626  {
1627  int cnum = sk_X509_NAME_num(cert_names);
1628  if (cnum != (prev + 1))
1629  {
1631  "Cannot load CA certificate file %s (entry %d did not validate)",
1632  print_key_filename(ca_file, ca_file_inline),
1633  added);
1634  }
1635  prev = cnum;
1636  }
1637  }
1638  sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
1639  }
1640 
1641  if (tls_server)
1642  {
1643  SSL_CTX_set_client_CA_list(ctx->ctx, cert_names);
1644  }
1645 
1646  if (!added)
1647  {
1649  "Cannot load CA certificate file %s (no entries were read)",
1650  print_key_filename(ca_file, ca_file_inline));
1651  }
1652 
1653  if (tls_server)
1654  {
1655  int cnum = sk_X509_NAME_num(cert_names);
1656  if (cnum != added)
1657  {
1658  crypto_msg(M_FATAL, "Cannot load CA certificate file %s (only %d "
1659  "of %d entries were valid X509 names)",
1660  print_key_filename(ca_file, ca_file_inline), cnum,
1661  added);
1662  }
1663  }
1664 
1665  BIO_free(in);
1666  }
1667 
1668  /* Set a store for certs (CA & CRL) with a lookup on the "capath" hash directory */
1669  if (ca_path)
1670  {
1671  lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
1672  if (lookup && X509_LOOKUP_add_dir(lookup, ca_path, X509_FILETYPE_PEM))
1673  {
1674  msg(M_WARN, "WARNING: experimental option --capath %s", ca_path);
1675  }
1676  else
1677  {
1678  crypto_msg(M_FATAL, "Cannot add lookup at --capath %s", ca_path);
1679  }
1680  X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
1681  }
1682 }
1683 
1684 void
1685 tls_ctx_load_extra_certs(struct tls_root_ctx *ctx, const char *extra_certs_file,
1686  bool extra_certs_file_inline)
1687 {
1688  BIO *in;
1689  if (extra_certs_file_inline)
1690  {
1691  in = BIO_new_mem_buf((char *)extra_certs_file, -1);
1692  }
1693  else
1694  {
1695  in = BIO_new_file(extra_certs_file, "r");
1696  }
1697 
1698  if (in == NULL)
1699  {
1700  crypto_msg(M_FATAL, "Cannot load extra-certs file: %s",
1701  print_key_filename(extra_certs_file,
1702  extra_certs_file_inline));
1703 
1704  }
1705  else
1706  {
1707  tls_ctx_add_extra_certs(ctx, in, false);
1708  }
1709 
1710  BIO_free(in);
1711 }
1712 
1713 /* **************************************
1714  *
1715  * Key-state specific functions
1716  *
1717  ***************************************/
1718 /*
1719  *
1720  * BIO functions
1721  *
1722  */
1723 
1724 #ifdef BIO_DEBUG
1725 
1726 #warning BIO_DEBUG defined
1727 
1728 static FILE *biofp; /* GLOBAL */
1729 static bool biofp_toggle; /* GLOBAL */
1730 static time_t biofp_last_open; /* GLOBAL */
1731 static const int biofp_reopen_interval = 600; /* GLOBAL */
1732 
1733 static void
1734 close_biofp(void)
1735 {
1736  if (biofp)
1737  {
1738  ASSERT(!fclose(biofp));
1739  biofp = NULL;
1740  }
1741 }
1742 
1743 static void
1744 open_biofp(void)
1745 {
1746  const time_t current = time(NULL);
1747  const pid_t pid = getpid();
1748 
1749  if (biofp_last_open + biofp_reopen_interval < current)
1750  {
1751  close_biofp();
1752  }
1753  if (!biofp)
1754  {
1755  char fn[256];
1756  snprintf(fn, sizeof(fn), "bio/%d-%d.log", pid, biofp_toggle);
1757  biofp = fopen(fn, "w");
1758  ASSERT(biofp);
1759  biofp_last_open = time(NULL);
1760  biofp_toggle ^= 1;
1761  }
1762 }
1763 
1764 static void
1765 bio_debug_data(const char *mode, BIO *bio, const uint8_t *buf, int len, const char *desc)
1766 {
1767  struct gc_arena gc = gc_new();
1768  if (len > 0)
1769  {
1770  open_biofp();
1771  fprintf(biofp, "BIO_%s %s time=%" PRIi64 " bio=" ptr_format " len=%d data=%s\n",
1772  mode, desc, (int64_t)time(NULL), (ptr_type)bio, len, format_hex(buf, len, 0, &gc));
1773  fflush(biofp);
1774  }
1775  gc_free(&gc);
1776 }
1777 
1778 static void
1779 bio_debug_oc(const char *mode, BIO *bio)
1780 {
1781  open_biofp();
1782  fprintf(biofp, "BIO %s time=%" PRIi64 " bio=" ptr_format "\n",
1783  mode, (int64_t)time(NULL), (ptr_type)bio);
1784  fflush(biofp);
1785 }
1786 
1787 #endif /* ifdef BIO_DEBUG */
1788 
1789 /*
1790  * Write to an OpenSSL BIO in non-blocking mode.
1791  */
1792 static int
1793 bio_write(BIO *bio, const uint8_t *data, int size, const char *desc)
1794 {
1795  int i;
1796  int ret = 0;
1797  ASSERT(size >= 0);
1798  if (size)
1799  {
1800  /*
1801  * Free the L_TLS lock prior to calling BIO routines
1802  * so that foreground thread can still call
1803  * tls_pre_decrypt or tls_pre_encrypt,
1804  * allowing tunnel packet forwarding to continue.
1805  */
1806 #ifdef BIO_DEBUG
1807  bio_debug_data("write", bio, data, size, desc);
1808 #endif
1809  i = BIO_write(bio, data, size);
1810 
1811  if (i < 0)
1812  {
1813  if (!BIO_should_retry(bio))
1814  {
1815  crypto_msg(D_TLS_ERRORS, "TLS ERROR: BIO write %s error", desc);
1816  ret = -1;
1817  ERR_clear_error();
1818  }
1819  }
1820  else if (i != size)
1821  {
1822  crypto_msg(D_TLS_ERRORS, "TLS ERROR: BIO write %s incomplete %d/%d",
1823  desc, i, size);
1824  ret = -1;
1825  ERR_clear_error();
1826  }
1827  else
1828  { /* successful write */
1829  dmsg(D_HANDSHAKE_VERBOSE, "BIO write %s %d bytes", desc, i);
1830  ret = 1;
1831  }
1832  }
1833  return ret;
1834 }
1835 
1836 /*
1837  * Inline functions for reading from and writing
1838  * to BIOs.
1839  */
1840 
1841 static void
1842 bio_write_post(const int status, struct buffer *buf)
1843 {
1844  if (status == 1) /* success status return from bio_write? */
1845  {
1846  memset(BPTR(buf), 0, BLEN(buf)); /* erase data just written */
1847  buf->len = 0;
1848  }
1849 }
1850 
1851 /*
1852  * Read from an OpenSSL BIO in non-blocking mode.
1853  */
1854 static int
1855 bio_read(BIO *bio, struct buffer *buf, const char *desc)
1856 {
1857  ASSERT(buf->len >= 0);
1858  if (buf->len)
1859  {
1860  /* we only want to write empty buffers, ignore read request
1861  * if the buffer is not empty */
1862  return 0;
1863  }
1864  int len = buf_forward_capacity(buf);
1865 
1866  /*
1867  * BIO_read brackets most of the serious RSA
1868  * key negotiation number crunching.
1869  */
1870  int i = BIO_read(bio, BPTR(buf), len);
1871 
1872  VALGRIND_MAKE_READABLE((void *) &i, sizeof(i));
1873 
1874 #ifdef BIO_DEBUG
1875  bio_debug_data("read", bio, BPTR(buf), i, desc);
1876 #endif
1877 
1878  int ret = 0;
1879  if (i < 0)
1880  {
1881  if (!BIO_should_retry(bio))
1882  {
1883  crypto_msg(D_TLS_ERRORS, "TLS_ERROR: BIO read %s error", desc);
1884  buf->len = 0;
1885  ret = -1;
1886  ERR_clear_error();
1887  }
1888  }
1889  else if (!i)
1890  {
1891  buf->len = 0;
1892  }
1893  else
1894  { /* successful read */
1895  dmsg(D_HANDSHAKE_VERBOSE, "BIO read %s %d bytes", desc, i);
1896  buf->len = i;
1897  ret = 1;
1898  VALGRIND_MAKE_READABLE((void *) BPTR(buf), BLEN(buf));
1899  }
1900  return ret;
1901 }
1902 
1903 void
1904 key_state_ssl_init(struct key_state_ssl *ks_ssl, const struct tls_root_ctx *ssl_ctx, bool is_server, struct tls_session *session)
1905 {
1906  ASSERT(NULL != ssl_ctx);
1907  ASSERT(ks_ssl);
1908  CLEAR(*ks_ssl);
1909 
1910  ks_ssl->ssl = SSL_new(ssl_ctx->ctx);
1911  if (!ks_ssl->ssl)
1912  {
1913  crypto_msg(M_FATAL, "SSL_new failed");
1914  }
1915 
1916  /* put session * in ssl object so we can access it
1917  * from verify callback*/
1918  SSL_set_ex_data(ks_ssl->ssl, mydata_index, session);
1919 
1920  ASSERT((ks_ssl->ssl_bio = BIO_new(BIO_f_ssl())));
1921  ASSERT((ks_ssl->ct_in = BIO_new(BIO_s_mem())));
1922  ASSERT((ks_ssl->ct_out = BIO_new(BIO_s_mem())));
1923 
1924 #ifdef BIO_DEBUG
1925  bio_debug_oc("open ssl_bio", ks_ssl->ssl_bio);
1926  bio_debug_oc("open ct_in", ks_ssl->ct_in);
1927  bio_debug_oc("open ct_out", ks_ssl->ct_out);
1928 #endif
1929 
1930  if (is_server)
1931  {
1932  SSL_set_accept_state(ks_ssl->ssl);
1933  }
1934  else
1935  {
1936  SSL_set_connect_state(ks_ssl->ssl);
1937  }
1938 
1939  SSL_set_bio(ks_ssl->ssl, ks_ssl->ct_in, ks_ssl->ct_out);
1940  BIO_set_ssl(ks_ssl->ssl_bio, ks_ssl->ssl, BIO_NOCLOSE);
1941 }
1942 
1943 void
1945 {
1946  SSL_set_shutdown(ks_ssl->ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
1947 }
1948 
1949 void
1951 {
1952  if (ks_ssl->ssl)
1953  {
1954 #ifdef BIO_DEBUG
1955  bio_debug_oc("close ssl_bio", ks_ssl->ssl_bio);
1956  bio_debug_oc("close ct_in", ks_ssl->ct_in);
1957  bio_debug_oc("close ct_out", ks_ssl->ct_out);
1958 #endif
1959  BIO_free_all(ks_ssl->ssl_bio);
1960  SSL_free(ks_ssl->ssl);
1961  }
1962 }
1963 
1964 int
1965 key_state_write_plaintext(struct key_state_ssl *ks_ssl, struct buffer *buf)
1966 {
1967  int ret = 0;
1969 
1970  ASSERT(NULL != ks_ssl);
1971 
1972  ret = bio_write(ks_ssl->ssl_bio, BPTR(buf), BLEN(buf),
1973  "tls_write_plaintext");
1974  bio_write_post(ret, buf);
1975 
1976  perf_pop();
1977  return ret;
1978 }
1979 
1980 int
1981 key_state_write_plaintext_const(struct key_state_ssl *ks_ssl, const uint8_t *data, int len)
1982 {
1983  int ret = 0;
1985 
1986  ASSERT(NULL != ks_ssl);
1987 
1988  ret = bio_write(ks_ssl->ssl_bio, data, len, "tls_write_plaintext_const");
1989 
1990  perf_pop();
1991  return ret;
1992 }
1993 
1994 int
1995 key_state_read_ciphertext(struct key_state_ssl *ks_ssl, struct buffer *buf)
1996 {
1997  int ret = 0;
1999 
2000  ASSERT(NULL != ks_ssl);
2001 
2002  ret = bio_read(ks_ssl->ct_out, buf, "tls_read_ciphertext");
2003 
2004  perf_pop();
2005  return ret;
2006 }
2007 
2008 int
2010 {
2011  int ret = 0;
2013 
2014  ASSERT(NULL != ks_ssl);
2015 
2016  ret = bio_write(ks_ssl->ct_in, BPTR(buf), BLEN(buf), "tls_write_ciphertext");
2017  bio_write_post(ret, buf);
2018 
2019  perf_pop();
2020  return ret;
2021 }
2022 
2023 int
2024 key_state_read_plaintext(struct key_state_ssl *ks_ssl, struct buffer *buf)
2025 {
2026  int ret = 0;
2028 
2029  ASSERT(NULL != ks_ssl);
2030 
2031  ret = bio_read(ks_ssl->ssl_bio, buf, "tls_read_plaintext");
2032 
2033  perf_pop();
2034  return ret;
2035 }
2036 
2037 static void
2038 print_pkey_details(EVP_PKEY *pkey, char *buf, size_t buflen)
2039 {
2040  const char *curve = "";
2041  const char *type = "(error getting type)";
2042 
2043  if (pkey == NULL)
2044  {
2045  buf[0] = 0;
2046  return;
2047  }
2048 
2049  int typeid = EVP_PKEY_id(pkey);
2050 #if OPENSSL_VERSION_NUMBER < 0x30000000L
2051  bool is_ec = typeid == EVP_PKEY_EC;
2052 #else
2053  bool is_ec = EVP_PKEY_is_a(pkey, "EC");
2054 #endif
2055 
2056 #ifndef OPENSSL_NO_EC
2057  char groupname[64];
2058  if (is_ec)
2059  {
2060  size_t len;
2061  if (EVP_PKEY_get_group_name(pkey, groupname, sizeof(groupname), &len))
2062  {
2063  curve = groupname;
2064  }
2065  else
2066  {
2067  curve = "(error getting curve name)";
2068  }
2069  }
2070 #endif
2071  if (typeid != 0)
2072  {
2073 #if OPENSSL_VERSION_NUMBER < 0x30000000L
2074  type = OBJ_nid2sn(typeid);
2075 
2076  /* OpenSSL reports rsaEncryption, dsaEncryption and
2077  * id-ecPublicKey, map these values to nicer ones */
2078  if (typeid == EVP_PKEY_RSA)
2079  {
2080  type = "RSA";
2081  }
2082  else if (typeid == EVP_PKEY_DSA)
2083  {
2084  type = "DSA";
2085  }
2086  else if (typeid == EVP_PKEY_EC)
2087  {
2088  /* EC gets the curve appended after the type */
2089  type = "EC, curve ";
2090  }
2091  else if (type == NULL)
2092  {
2093  type = "unknown type";
2094  }
2095 #else /* OpenSSL >= 3 */
2096  type = EVP_PKEY_get0_type_name(pkey);
2097  if (type == NULL)
2098  {
2099  type = "(error getting public key type)";
2100  }
2101 #endif /* if OPENSSL_VERSION_NUMBER < 0x30000000L */
2102  }
2103 
2104  snprintf(buf, buflen, "%d bits %s%s",
2105  EVP_PKEY_bits(pkey), type, curve);
2106 }
2107 
2114 static void
2115 print_cert_details(X509 *cert, char *buf, size_t buflen)
2116 {
2117  EVP_PKEY *pkey = X509_get_pubkey(cert);
2118  char pkeybuf[64] = { 0 };
2119  print_pkey_details(pkey, pkeybuf, sizeof(pkeybuf));
2120 
2121  char sig[128] = { 0 };
2122  int signature_nid = X509_get_signature_nid(cert);
2123  if (signature_nid != 0)
2124  {
2125  snprintf(sig, sizeof(sig), ", signature: %s",
2126  OBJ_nid2sn(signature_nid));
2127  }
2128 
2129  snprintf(buf, buflen, ", peer certificate: %s%s",
2130  pkeybuf, sig);
2131 
2132  EVP_PKEY_free(pkey);
2133 }
2134 
2135 static void
2136 print_server_tempkey(SSL *ssl, char *buf, size_t buflen)
2137 {
2138  EVP_PKEY *pkey = NULL;
2139  SSL_get_peer_tmp_key(ssl, &pkey);
2140  if (!pkey)
2141  {
2142  return;
2143  }
2144 
2145  char pkeybuf[128] = { 0 };
2146  print_pkey_details(pkey, pkeybuf, sizeof(pkeybuf));
2147 
2148  snprintf(buf, buflen, ", peer temporary key: %s",
2149  pkeybuf);
2150 
2151  EVP_PKEY_free(pkey);
2152 }
2153 
2154 #if !defined(LIBRESSL_VERSION_NUMBER) \
2155  || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x3090000fL)
2156 
2161 static const char *
2162 get_sigtype(int nid)
2163 {
2164  /* Fix a few OpenSSL names to be better understandable */
2165  switch (nid)
2166  {
2167  case EVP_PKEY_RSA:
2168  /* will otherwise say rsaEncryption */
2169  return "RSA";
2170 
2171  case EVP_PKEY_DSA:
2172  /* dsaEncryption otherwise */
2173  return "DSA";
2174 
2175  case EVP_PKEY_EC:
2176  /* will say id-ecPublicKey */
2177  return "ECDSA";
2178 
2179  case -1:
2180  return "(error getting name)";
2181 
2182  default:
2183  return OBJ_nid2sn(nid);
2184  }
2185 }
2186 #endif /* ifndef LIBRESSL_VERSION_NUMBER */
2187 
2192 static void
2193 print_peer_signature(SSL *ssl, char *buf, size_t buflen)
2194 {
2195  int peer_sig_nid = NID_undef, peer_sig_type_nid = NID_undef;
2196  const char *peer_sig = "unknown";
2197  const char *peer_sig_type = "unknown type";
2198 
2199  /* Even though these methods use the deprecated NIDs instead of using
2200  * string as new OpenSSL APIs do, there seem to be no API that replaces
2201  * it yet */
2202 #if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER > 0x3050400fL
2203  if (SSL_get_peer_signature_nid(ssl, &peer_sig_nid)
2204  && peer_sig_nid != NID_undef)
2205  {
2206  peer_sig = OBJ_nid2sn(peer_sig_nid);
2207  }
2208 #endif
2209 
2210 #if !defined(LIBRESSL_VERSION_NUMBER) \
2211  || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x3090000fL)
2212  /* LibreSSL 3.7.x and 3.8.x implement this function but do not export it
2213  * and fail linking with an unresolved symbol */
2214  if (SSL_get_peer_signature_type_nid(ssl, &peer_sig_type_nid)
2215  && peer_sig_type_nid != NID_undef)
2216  {
2217  peer_sig_type = get_sigtype(peer_sig_type_nid);
2218  }
2219 #endif
2220 
2221  if (peer_sig_nid == NID_undef && peer_sig_type_nid == NID_undef)
2222  {
2223  return;
2224  }
2225 
2226  snprintf(buf, buflen, ", peer signing digest/type: %s %s",
2227  peer_sig, peer_sig_type);
2228 }
2229 
2230 
2231 
2232 /* **************************************
2233  *
2234  * Information functions
2235  *
2236  * Print information for the end user.
2237  *
2238  ***************************************/
2239 void
2240 print_details(struct key_state_ssl *ks_ssl, const char *prefix)
2241 {
2242  const SSL_CIPHER *ciph;
2243  char s1[256];
2244  char s2[256];
2245  char s3[256];
2246  char s4[256];
2247 
2248  s1[0] = s2[0] = s3[0] = s4[0] = 0;
2249  ciph = SSL_get_current_cipher(ks_ssl->ssl);
2250  snprintf(s1, sizeof(s1), "%s %s, cipher %s %s",
2251  prefix,
2252  SSL_get_version(ks_ssl->ssl),
2253  SSL_CIPHER_get_version(ciph),
2254  SSL_CIPHER_get_name(ciph));
2255  X509 *cert = SSL_get_peer_certificate(ks_ssl->ssl);
2256 
2257  if (cert)
2258  {
2259  print_cert_details(cert, s2, sizeof(s2));
2260  X509_free(cert);
2261  }
2262  print_server_tempkey(ks_ssl->ssl, s3, sizeof(s3));
2263  print_peer_signature(ks_ssl->ssl, s4, sizeof(s4));
2264 
2265  msg(D_HANDSHAKE, "%s%s%s%s", s1, s2, s3, s4);
2266 }
2267 
2268 void
2269 show_available_tls_ciphers_list(const char *cipher_list,
2270  const char *tls_cert_profile,
2271  bool tls13)
2272 {
2273  struct tls_root_ctx tls_ctx;
2274 
2275  tls_ctx.ctx = SSL_CTX_new(SSLv23_method());
2276  if (!tls_ctx.ctx)
2277  {
2278  crypto_msg(M_FATAL, "Cannot create SSL_CTX object");
2279  }
2280 
2281 #if defined(TLS1_3_VERSION)
2282  if (tls13)
2283  {
2284  SSL_CTX_set_min_proto_version(tls_ctx.ctx,
2286  tls_ctx_restrict_ciphers_tls13(&tls_ctx, cipher_list);
2287  }
2288  else
2289 #endif
2290  {
2291  SSL_CTX_set_max_proto_version(tls_ctx.ctx, TLS1_2_VERSION);
2292  tls_ctx_restrict_ciphers(&tls_ctx, cipher_list);
2293  }
2294 
2295  tls_ctx_set_cert_profile(&tls_ctx, tls_cert_profile);
2296 
2297  SSL *ssl = SSL_new(tls_ctx.ctx);
2298  if (!ssl)
2299  {
2300  crypto_msg(M_FATAL, "Cannot create SSL object");
2301  }
2302 
2303 #if OPENSSL_VERSION_NUMBER < 0x1010000fL
2304  STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl);
2305 #else
2306  STACK_OF(SSL_CIPHER) *sk = SSL_get1_supported_ciphers(ssl);
2307 #endif
2308  for (int i = 0; i < sk_SSL_CIPHER_num(sk); i++)
2309  {
2310  const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, i);
2311 
2312  const char *cipher_name = SSL_CIPHER_get_name(c);
2313 
2314  const tls_cipher_name_pair *pair =
2315  tls_get_cipher_name_pair(cipher_name, strlen(cipher_name));
2316 
2317  if (tls13)
2318  {
2319  printf("%s\n", cipher_name);
2320  }
2321  else if (NULL == pair)
2322  {
2323  /* No translation found, print warning */
2324  printf("%s (No IANA name known to OpenVPN, use OpenSSL name.)\n",
2325  cipher_name);
2326  }
2327  else
2328  {
2329  printf("%s\n", pair->iana_name);
2330  }
2331  }
2332 #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL)
2333  sk_SSL_CIPHER_free(sk);
2334 #endif
2335  SSL_free(ssl);
2336  SSL_CTX_free(tls_ctx.ctx);
2337 }
2338 
2339 /*
2340  * Show the Elliptic curves that are available for us to use
2341  * in the OpenSSL library.
2342  */
2343 void
2345 {
2346  printf("Consider using 'openssl ecparam -list_curves' as alternative to running\n"
2347  "this command.\n"
2348  "Note this output does only list curves/groups that OpenSSL considers as\n"
2349  "builtin EC curves. It does not list additional curves nor X448 or X25519\n");
2350 #ifndef OPENSSL_NO_EC
2351  EC_builtin_curve *curves = NULL;
2352  size_t crv_len = 0;
2353  size_t n = 0;
2354 
2355  crv_len = EC_get_builtin_curves(NULL, 0);
2356  ALLOC_ARRAY(curves, EC_builtin_curve, crv_len);
2357  if (EC_get_builtin_curves(curves, crv_len))
2358  {
2359  printf("\nAvailable Elliptic curves/groups:\n");
2360  for (n = 0; n < crv_len; n++)
2361  {
2362  const char *sname;
2363  sname = OBJ_nid2sn(curves[n].nid);
2364  if (sname == NULL)
2365  {
2366  sname = "";
2367  }
2368 
2369  printf("%s\n", sname);
2370  }
2371  }
2372  else
2373  {
2374  crypto_msg(M_FATAL, "Cannot get list of builtin curves");
2375  }
2376  free(curves);
2377 #else /* ifndef OPENSSL_NO_EC */
2378  msg(M_WARN, "Your OpenSSL library was built without elliptic curve support. "
2379  "No curves available.");
2380 #endif /* ifndef OPENSSL_NO_EC */
2381 }
2382 
2383 void
2385 {
2386  SSL_CTX *ctx;
2387  SSL *ssl;
2388  const char *cipher_name;
2389 
2390  ctx = SSL_CTX_new(SSLv23_method());
2391  if (!ctx)
2392  {
2393  crypto_msg(M_FATAL, "Cannot create SSL_CTX object");
2394  }
2395  ssl = SSL_new(ctx);
2396  if (!ssl)
2397  {
2398  crypto_msg(M_FATAL, "Cannot create SSL object");
2399  }
2400 
2401  cipher_name = SSL_get_cipher_list(ssl, 0);
2402  strncpynt(buf, cipher_name, size);
2403 
2404  SSL_free(ssl);
2405  SSL_CTX_free(ctx);
2406 }
2407 
2408 const char *
2410 {
2411  return OpenSSL_version(OPENSSL_VERSION);
2412 }
2413 
2414 
2416 #ifdef HAVE_XKEY_PROVIDER
2417 static int
2418 provider_load(OSSL_PROVIDER *prov, void *dest_libctx)
2419 {
2420  const char *name = OSSL_PROVIDER_get0_name(prov);
2421  OSSL_PROVIDER_load(dest_libctx, name);
2422  return 1;
2423 }
2424 
2425 static int
2426 provider_unload(OSSL_PROVIDER *prov, void *unused)
2427 {
2428  (void) unused;
2429  OSSL_PROVIDER_unload(prov);
2430  return 1;
2431 }
2432 #endif /* HAVE_XKEY_PROVIDER */
2433 
2441 void
2443 {
2444 #ifdef HAVE_XKEY_PROVIDER
2445 
2446  /* Make a new library context for use in TLS context */
2447  if (!tls_libctx)
2448  {
2449  tls_libctx = OSSL_LIB_CTX_new();
2451 
2452  /* Load all providers in default LIBCTX into this libctx.
2453  * OpenSSL has a child libctx functionality to automate this,
2454  * but currently that is usable only from within providers.
2455  * So we do something close to it manually here.
2456  */
2457  OSSL_PROVIDER_do_all(NULL, provider_load, tls_libctx);
2458  }
2459 
2460  if (!OSSL_PROVIDER_available(tls_libctx, "ovpn.xkey"))
2461  {
2462  OSSL_PROVIDER_add_builtin(tls_libctx, "ovpn.xkey", xkey_provider_init);
2463  if (!OSSL_PROVIDER_load(tls_libctx, "ovpn.xkey"))
2464  {
2465  msg(M_NONFATAL, "ERROR: failed loading external key provider: "
2466  "Signing with external keys will not work.");
2467  }
2468  }
2469 
2470  /* We only implement minimal functionality in ovpn.xkey, so we do not want
2471  * methods in xkey to be picked unless absolutely required (i.e, when the key
2472  * is external). Ensure this by setting a default propquery for the custom
2473  * libctx that unprefers, but does not forbid, ovpn.xkey. See also man page
2474  * of "property" in OpenSSL 3.0.
2475  */
2476  EVP_set_default_properties(tls_libctx, "?provider!=ovpn.xkey");
2477 
2478 #endif /* HAVE_XKEY_PROVIDER */
2479 }
2480 
2484 static void
2486 {
2487 #ifdef HAVE_XKEY_PROVIDER
2488  if (tls_libctx)
2489  {
2490  OSSL_PROVIDER_do_all(tls_libctx, provider_unload, NULL);
2491  OSSL_LIB_CTX_free(tls_libctx);
2492  }
2493 #endif /* HAVE_XKEY_PROVIDER */
2494  tls_libctx = NULL;
2495 }
2496 
2497 #endif /* defined(ENABLE_CRYPTO_OPENSSL) */
SSLF_TLS_VERSION_MIN_SHIFT
#define SSLF_TLS_VERSION_MIN_SHIFT
Definition: ssl_common.h:413
tls_ctx_add_extra_certs
static void tls_ctx_add_extra_certs(struct tls_root_ctx *ctx, BIO *bio, bool optional)
Definition: ssl_openssl.c:915
openvpn_base64_decode
int openvpn_base64_decode(const char *str, void *data, int size)
Definition: base64.c:158
D_TLS_DEBUG
#define D_TLS_DEBUG
Definition: errlevel.h:165
tls_ctx_set_options
bool tls_ctx_set_options(struct tls_root_ctx *ctx, unsigned int ssl_flags)
Set any library specific options.
Definition: ssl_openssl.c:303
key_state_export_keying_material
bool key_state_export_keying_material(struct tls_session *session, const char *label, size_t label_size, void *ekm, size_t ekm_size)
Keying Material Exporters [RFC 5705] allows additional keying material to be derived from existing TL...
Definition: ssl_openssl.c:149
get_rsa_padding_name
const char * get_rsa_padding_name(const int padding)
Definition: ssl_openssl.c:1194
M_INFO
#define M_INFO
Definition: errlevel.h:55
key_state_ssl
Definition: ssl_mbedtls.h:127
rsa_priv_dec
static int rsa_priv_dec(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
Definition: ssl_openssl.c:1171
tls_ctx_client_new
void tls_ctx_client_new(struct tls_root_ctx *ctx)
Initialises a library-specific TLS context for a client.
Definition: ssl_openssl.c:115
key_state_ssl::ct_in
BIO * ct_in
Definition: ssl_openssl.h:48
sk_x509_name_cmp
static int sk_x509_name_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
Definition: ssl_openssl.c:1530
ssl_backend.h
gc_new
static struct gc_arena gc_new(void)
Definition: buffer.h:1030
PERF_BIO_READ_PLAINTEXT
#define PERF_BIO_READ_PLAINTEXT
Definition: perf.h:38
tls_ctx_restrict_ciphers_tls13
void tls_ctx_restrict_ciphers_tls13(struct tls_root_ctx *ctx, const char *ciphers)
Restrict the list of ciphers that can be used within the TLS context for TLS 1.3 and higher.
Definition: ssl_openssl.c:485
SSLF_TLS_VERSION_MAX_MASK
#define SSLF_TLS_VERSION_MAX_MASK
Definition: ssl_common.h:416
key_state_ssl_free
void key_state_ssl_free(struct key_state_ssl *ks_ssl)
Free the SSL channel part of the given key state.
Definition: ssl_openssl.c:1950
buffer::len
int len
Length in bytes of the actual content within the allocated memory.
Definition: buffer.h:66
M_FATAL
#define M_FATAL
Definition: error.h:89
convert_tls13_list_to_openssl
void convert_tls13_list_to_openssl(char *openssl_ciphers, size_t len, const char *ciphers)
Definition: ssl_openssl.c:458
info_callback
static void info_callback(INFO_CALLBACK_SSL_CONST SSL *s, int where, int ret)
Definition: ssl_openssl.c:176
mydata_index
int mydata_index
Allocate space in SSL objects in which to store a struct tls_session pointer back to parent.
Definition: ssl_openssl.c:82
streq
#define streq(x, y)
Definition: options.h:707
management_auth_failure
void management_auth_failure(struct management *man, const char *type, const char *reason)
Definition: manage.c:3080
M_NONFATAL
#define M_NONFATAL
Definition: error.h:90
manage.h
D_HANDSHAKE_VERBOSE
#define D_HANDSHAKE_VERBOSE
Definition: errlevel.h:156
KS_PRIMARY
#define KS_PRIMARY
Primary key state index.
Definition: ssl_common.h:448
D_TLS_DEBUG_LOW
#define D_TLS_DEBUG_LOW
Definition: errlevel.h:77
print_details
void print_details(struct key_state_ssl *ks_ssl, const char *prefix)
Definition: ssl_openssl.c:2240
ecdsa_sign
static int ecdsa_sign(int type, const unsigned char *dgst, int dgstlen, unsigned char *sig, unsigned int *siglen, const BIGNUM *kinv, const BIGNUM *r, EC_KEY *ec)
Definition: ssl_openssl.c:1346
tls_ctx_load_ca
void tls_ctx_load_ca(struct tls_root_ctx *ctx, const char *ca_file, bool ca_file_inline, const char *ca_path, bool tls_server)
Load certificate authority certificates from the given file or path.
Definition: ssl_openssl.c:1536
key_state_ssl::ssl_bio
BIO * ssl_bio
Definition: ssl_openssl.h:47
D_HANDSHAKE
#define D_HANDSHAKE
Definition: errlevel.h:72
key_state_ssl::ct_out
BIO * ct_out
Definition: ssl_openssl.h:49
TLS_VER_1_1
#define TLS_VER_1_1
Definition: ssl_backend.h:106
print_cert_details
static void print_cert_details(X509 *cert, char *buf, size_t buflen)
Print human readable information about the certificate into buf.
Definition: ssl_openssl.c:2115
get_num_elements
int get_num_elements(const char *string, char delimiter)
Returns the occurrences of 'delimiter' in a string +1 This is typically used to find out the number e...
Definition: ssl_util.c:284
session::key
char key[48]
Definition: keyingmaterialexporter.c:58
get_ssl_library_version
const char * get_ssl_library_version(void)
return a pointer to a static memory area containing the name and version number of the SSL library in...
Definition: ssl_openssl.c:2409
tls_init_lib
void tls_init_lib(void)
Perform any static initialisation necessary by the library.
Definition: ssl_openssl.c:85
xkey_common.h
openvpn_base64_encode
int openvpn_base64_encode(const void *data, int size, char **str)
Definition: base64.c:52
get_sig_from_man
static int get_sig_from_man(const unsigned char *dgst, unsigned int dgstlen, unsigned char *sig, unsigned int siglen, const char *algorithm)
Pass the input hash in 'dgst' to management and get the signature back.
Definition: ssl_openssl.c:1221
ssl_verify_openssl.h
VALGRIND_MAKE_READABLE
#define VALGRIND_MAKE_READABLE(addr, len)
Definition: memdbg.h:53
print_key_filename
const char * print_key_filename(const char *str, bool is_inline)
To be used when printing a string that may contain inline data.
Definition: crypto.c:1083
dmsg
#define dmsg(flags,...)
Definition: error.h:148
PERF_BIO_WRITE_PLAINTEXT
#define PERF_BIO_WRITE_PLAINTEXT
Definition: perf.h:39
D_LOW
#define D_LOW
Definition: errlevel.h:97
tls_ctx_load_dh_params
void tls_ctx_load_dh_params(struct tls_root_ctx *ctx, const char *dh_file, bool dh_file_inline)
Load Diffie Hellman Parameters, and load them into the library-specific TLS context.
Definition: ssl_openssl.c:649
key_state_ssl::ssl
SSL * ssl
Definition: ssl_openssl.h:46
load_xkey_provider
void load_xkey_provider(void)
Some helper routines for provider load/unload.
Definition: ssl_openssl.c:2442
TLS_VER_1_0
#define TLS_VER_1_0
Definition: ssl_backend.h:105
get_sigtype
static const char * get_sigtype(int nid)
Translate an OpenSSL NID into a more human readable name.
Definition: ssl_openssl.c:2162
key_state_write_ciphertext
int key_state_write_ciphertext(struct key_state_ssl *ks_ssl, struct buffer *buf)
Insert a ciphertext buffer into the TLS module.
Definition: ssl_openssl.c:2009
openvpn_extkey_rsa_finish
static int openvpn_extkey_rsa_finish(RSA *rsa)
Definition: ssl_openssl.c:1179
tls_ctx_set_tls_versions
static bool tls_ctx_set_tls_versions(struct tls_root_ctx *ctx, unsigned int ssl_flags)
Definition: ssl_openssl.c:273
convert_tls_list_to_openssl
void convert_tls_list_to_openssl(char *openssl_ciphers, size_t len, const char *ciphers)
Definition: ssl_openssl.c:351
tls_ctx_use_external_rsa_key
static int tls_ctx_use_external_rsa_key(struct tls_root_ctx *ctx, EVP_PKEY *pkey)
Definition: ssl_openssl.c:1266
CLEAR
#define CLEAR(x)
Definition: basic.h:33
tls_ctx_load_cert_file
void tls_ctx_load_cert_file(struct tls_root_ctx *ctx, const char *cert_file, bool cert_file_inline)
Load certificate file into the given TLS context.
Definition: ssl_openssl.c:949
ssl_util.h
print_peer_signature
static void print_peer_signature(SSL *ssl, char *buf, size_t buflen)
Get the type of the signature that is used by the peer during the TLS handshake.
Definition: ssl_openssl.c:2193
key_state_read_plaintext
int key_state_read_plaintext(struct key_state_ssl *ks_ssl, struct buffer *buf)
Extract plaintext data from the TLS module.
Definition: ssl_openssl.c:2024
string_alloc
char * string_alloc(const char *str, struct gc_arena *gc)
Definition: buffer.c:667
secure_memzero
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition: buffer.h:414
ASSERT
#define ASSERT(x)
Definition: error.h:195
tls_get_cipher_name_pair
const tls_cipher_name_pair * tls_get_cipher_name_pair(const char *cipher_name, size_t len)
Definition: ssl_util.c:265
get_highest_preference_tls_cipher
void get_highest_preference_tls_cipher(char *buf, int size)
Definition: ssl_openssl.c:2384
SSLF_TLS_VERSION_MIN_MASK
#define SSLF_TLS_VERSION_MIN_MASK
Definition: ssl_common.h:414
key_state_ssl_init
void key_state_ssl_init(struct key_state_ssl *ks_ssl, const struct tls_root_ctx *ssl_ctx, bool is_server, struct tls_session *session)
Initialise the SSL channel part of the given key state.
Definition: ssl_openssl.c:1904
ecdsa_sign_setup
static int ecdsa_sign_setup(EC_KEY *ec, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
Definition: ssl_openssl.c:1367
tls_ctx_server_new
void tls_ctx_server_new(struct tls_root_ctx *ctx)
Initialise a library-specific TLS context for a server.
Definition: ssl_openssl.c:97
BLEN
#define BLEN(buf)
Definition: buffer.h:127
SSL_CTX_new_ex
#define SSL_CTX_new_ex(libctx, propq, method)
Reduce SSL_CTX_new_ex() to SSL_CTX_new() for OpenSSL < 3.
Definition: openssl_compat.h:125
tls_version_max
int tls_version_max(void)
Return the maximum TLS version (as a TLS_VER_x constant) supported by current SSL implementation.
Definition: ssl_openssl.c:200
cryptoapi.h
PERF_BIO_WRITE_CIPHERTEXT
#define PERF_BIO_WRITE_CIPHERTEXT
Definition: perf.h:41
tls_ctx_use_management_external_key
int tls_ctx_use_management_external_key(struct tls_root_ctx *ctx)
Tell the management interface to load the given certificate and the external private key matching the...
Definition: ssl_openssl.c:1456
tls_ctx_load_priv_file
int tls_ctx_load_priv_file(struct tls_root_ctx *ctx, const char *priv_key_file, bool priv_key_file_inline)
Load private key file into the given TLS context.
Definition: ssl_openssl.c:1011
ALLOC_ARRAY_CLEAR_GC
#define ALLOC_ARRAY_CLEAR_GC(dptr, type, n, gc)
Definition: buffer.h:1087
misc.h
unload_xkey_provider
static void unload_xkey_provider(void)
Undo steps in load_xkey_provider.
Definition: ssl_openssl.c:2485
SSL_CTX_set1_groups
#define SSL_CTX_set1_groups
Definition: openssl_compat.h:50
M_WARN
#define M_WARN
Definition: error.h:91
perf_pop
static void perf_pop(void)
Definition: perf.h:82
tls_ctx_load_extra_certs
void tls_ctx_load_extra_certs(struct tls_root_ctx *ctx, const char *extra_certs_file, bool extra_certs_file_inline)
Load extra certificate authority certificates from the given file or path.
Definition: ssl_openssl.c:1685
base64.h
errlevel.h
SSLF_CLIENT_CERT_NOT_REQUIRED
#define SSLF_CLIENT_CERT_NOT_REQUIRED
Definition: ssl_common.h:407
management_query_pk_sig
char * management_query_pk_sig(struct management *man, const char *b64_data, const char *algorithm)
Definition: manage.c:3747
bio_write
static int bio_write(BIO *bio, const uint8_t *data, int size, const char *desc)
Definition: ssl_openssl.c:1793
INFO_CALLBACK_SSL_CONST
#define INFO_CALLBACK_SSL_CONST
Definition: ssl_openssl.c:173
buffer
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
print_pkey_details
static void print_pkey_details(EVP_PKEY *pkey, char *buf, size_t buflen)
Definition: ssl_openssl.c:2038
show_available_curves
void show_available_curves(void)
Definition: ssl_openssl.c:2344
key_state_write_plaintext
int key_state_write_plaintext(struct key_state_ssl *ks_ssl, struct buffer *buf)
Insert a plaintext buffer into the TLS module.
Definition: ssl_openssl.c:1965
tls_ctx_load_pkcs12
int tls_ctx_load_pkcs12(struct tls_root_ctx *ctx, const char *pkcs12_file, bool pkcs12_file_inline, bool load_ca_file)
Load PKCS #12 file for key, cert and (optionally) CA certs, and add to library-specific TLS context.
Definition: ssl_openssl.c:772
show_available_tls_ciphers_list
void show_available_tls_ciphers_list(const char *cipher_list, const char *tls_cert_profile, bool tls13)
Definition: ssl_openssl.c:2269
D_TLS_ERRORS
#define D_TLS_ERRORS
Definition: errlevel.h:59
tls_ctx_load_cryptoapi
void tls_ctx_load_cryptoapi(struct tls_root_ctx *ctx, const char *cryptoapi_cert)
Use Windows cryptoapi for key and cert, and add to library-specific TLS context.
Definition: ssl_openssl.c:902
TLS_VER_1_2
#define TLS_VER_1_2
Definition: ssl_backend.h:107
rsa_pub_dec
static int rsa_pub_dec(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
Definition: ssl_openssl.c:1163
tls_free_lib
void tls_free_lib(void)
Free any global SSL library-specific data structures.
Definition: ssl_openssl.c:92
openssl_compat.h
tls_session
Security parameter state of a single session within a VPN tunnel.
Definition: ssl_common.h:471
openssl_tls_version
static int openssl_tls_version(int ver)
Convert internal version number to openssl version number.
Definition: ssl_openssl.c:236
buffer.h
syshead.h
tls_ctx_initialised
bool tls_ctx_initialised(struct tls_root_ctx *ctx)
Checks whether the given TLS context is initialised.
Definition: ssl_openssl.c:142
platform_fopen
FILE * platform_fopen(const char *path, const char *mode)
Definition: platform.c:514
BPTR
#define BPTR(buf)
Definition: buffer.h:124
tls_ctx_check_cert_time
void tls_ctx_check_cert_time(const struct tls_root_ctx *ctx)
Check our certificate notBefore and notAfter fields, and warn if the cert is either not yet valid or ...
Definition: ssl_openssl.c:613
SSLF_TLS_VERSION_MAX_SHIFT
#define SSLF_TLS_VERSION_MAX_SHIFT
Definition: ssl_common.h:415
gc_arena
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
bio_read
static int bio_read(BIO *bio, struct buffer *buf, const char *desc)
Definition: ssl_openssl.c:1855
rsa_priv_enc
static int rsa_priv_enc(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
Definition: ssl_openssl.c:1248
crypto_print_openssl_errors
void crypto_print_openssl_errors(const unsigned int flags)
Retrieve any occurred OpenSSL errors and print those errors.
Definition: crypto_openssl.c:235
tls_ctx_restrict_ciphers
void tls_ctx_restrict_ciphers(struct tls_root_ctx *ctx, const char *ciphers)
Restrict the list of ciphers that can be used within the TLS context for TLS 1.2 and below.
Definition: ssl_openssl.c:423
EVP_PKEY_get_group_name
static int EVP_PKEY_get_group_name(EVP_PKEY *pkey, char *gname, size_t gname_sz, size_t *gname_len)
Definition: openssl_compat.h:90
tls_ctx_free
void tls_ctx_free(struct tls_root_ctx *ctx)
Frees the library-specific TLSv1 context.
Definition: ssl_openssl.c:133
strncpynt
static void strncpynt(char *dest, const char *src, size_t maxlen)
Definition: buffer.h:361
M_DEBUG
#define M_DEBUG
Definition: error.h:92
PERF_BIO_READ_CIPHERTEXT
#define PERF_BIO_READ_CIPHERTEXT
Definition: perf.h:40
backend_tls_ctx_reload_crl
void backend_tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx, const char *crl_file, bool crl_inline)
Reload the Certificate Revocation List for the SSL channel.
Definition: ssl_openssl.c:1068
tls_cipher_name_pair::openssl_name
const char * openssl_name
Definition: ssl_util.h:75
tls_ctx_set_tls_groups
void tls_ctx_set_tls_groups(struct tls_root_ctx *ctx, const char *groups)
Set the (elliptic curve) group allowed for signatures and key exchange.
Definition: ssl_openssl.c:553
crypto_msg
#define crypto_msg(flags,...)
Retrieve any OpenSSL errors, then print the supplied error message.
Definition: crypto_openssl.h:117
rsa_pub_enc
static int rsa_pub_enc(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
Definition: ssl_openssl.c:1155
gc_malloc
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Definition: buffer.c:354
verify_callback
int verify_callback(void *session_obj, mbedtls_x509_crt *cert, int cert_depth, uint32_t *flags)
Verify that the remote OpenVPN peer's certificate allows setting up a VPN tunnel.
D_TLS_DEBUG_MED
#define D_TLS_DEBUG_MED
Definition: errlevel.h:157
tls_root_ctx
Structure that wraps the TLS context.
Definition: ssl_mbedtls.h:106
key_state_ssl_shutdown
void key_state_ssl_shutdown(struct key_state_ssl *ks_ssl)
Sets a TLS session to be shutdown state, so the TLS library will generate a shutdown alert.
Definition: ssl_openssl.c:1944
status
static SERVICE_STATUS status
Definition: interactive.c:53
SSL_CTX_use_CryptoAPI_certificate
int SSL_CTX_use_CryptoAPI_certificate(SSL_CTX *ssl_ctx, const char *cert_prop)
Definition: cryptoapi.c:59
tls_ctx_set_cert_profile
void tls_ctx_set_cert_profile(struct tls_root_ctx *ctx, const char *profile)
Set the TLS certificate profile.
Definition: ssl_openssl.c:514
ptr_type
unsigned long ptr_type
Definition: common.h:58
management
Definition: manage.h:335
gc_free
static void gc_free(struct gc_arena *a)
Definition: buffer.h:1038
tls_ctx_load_ecdh_params
void tls_ctx_load_ecdh_params(struct tls_root_ctx *ctx, const char *curve_name)
Load Elliptic Curve Parameters, and load them into the library-specific TLS context.
Definition: ssl_openssl.c:710
print_server_tempkey
static void print_server_tempkey(SSL *ssl, char *buf, size_t buflen)
Definition: ssl_openssl.c:2136
TLS_VER_1_3
#define TLS_VER_1_3
Definition: ssl_backend.h:108
strsep
char * strsep(char **stringp, const char *delim)
Definition: compat-strsep.c:36
OSSL_PROVIDER
void OSSL_PROVIDER
Definition: openssl_compat.h:130
config.h
pem_password_callback
int pem_password_callback(char *buf, int size, int rwflag, void *u)
Callback to retrieve the user's password.
Definition: ssl.c:253
ssl_common.h
tls_root_ctx::ctx
SSL_CTX * ctx
Definition: ssl_openssl.h:40
tls_libctx
OSSL_LIB_CTX * tls_libctx
Definition: ssl_openssl.c:72
format_hex
static char * format_hex(const uint8_t *data, int size, int maxoutput, struct gc_arena *gc)
Definition: buffer.h:510
bio_write_post
static void bio_write_post(const int status, struct buffer *buf)
Definition: ssl_openssl.c:1842
check_malloc_return
static void check_malloc_return(void *p)
Definition: buffer.h:1108
session
Definition: keyingmaterialexporter.c:56
constrain_int
static int constrain_int(int x, int min, int max)
Definition: integer.h:102
buf_forward_capacity
static int buf_forward_capacity(const struct buffer *buf)
Definition: buffer.h:546
key_state_write_plaintext_const
int key_state_write_plaintext_const(struct key_state_ssl *ks_ssl, const uint8_t *data, int len)
Insert plaintext data into the TLS module.
Definition: ssl_openssl.c:1981
tls_ctx_use_external_ec_key
static int tls_ctx_use_external_ec_key(struct tls_root_ctx *ctx, EVP_PKEY *pkey)
Definition: ssl_openssl.c:1398
memdbg.h
ALLOC_ARRAY
#define ALLOC_ARRAY(dptr, type, n)
Definition: buffer.h:1071
tls_cipher_name_pair::iana_name
const char * iana_name
Definition: ssl_util.h:75
UP_TYPE_PRIVATE_KEY
#define UP_TYPE_PRIVATE_KEY
Definition: ssl_common.h:42
msg
#define msg(flags,...)
Definition: error.h:144
openvpn_extkey_ec_finish
static void openvpn_extkey_ec_finish(EC_KEY *ec)
Definition: ssl_openssl.c:1334
OSSL_LIB_CTX
void OSSL_LIB_CTX
Definition: openssl_compat.h:129
SSLF_CLIENT_CERT_OPTIONAL
#define SSLF_CLIENT_CERT_OPTIONAL
Definition: ssl_common.h:408
perf_push
static void perf_push(int type)
Definition: perf.h:78
ptr_format
#define ptr_format
Definition: common.h:49
key_state_read_ciphertext
int key_state_read_ciphertext(struct key_state_ssl *ks_ssl, struct buffer *buf)
Extract ciphertext data from the TLS module.
Definition: ssl_openssl.c:1995
ecdsa_sign_sig
static ECDSA_SIG * ecdsa_sign_sig(const unsigned char *dgst, int dgstlen, const BIGNUM *in_kinv, const BIGNUM *in_r, EC_KEY *ec)
Definition: ssl_openssl.c:1377
tls_cipher_name_pair
Get a tls_cipher_name_pair containing OpenSSL and IANA names for supplied TLS cipher name.
Definition: ssl_util.h:75
cleanup
static int cleanup(void **state)
Definition: test_pkcs11.c:280