OpenVPN 3 Core Library
Loading...
Searching...
No Matches
compat.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12
13#pragma once
14
15#include <openssl/opensslv.h>
16
17#if OPENSSL_VERSION_NUMBER < 0x30000000L
18#include <cassert>
19#include <cstring>
20#include <openssl/evp.h>
21#include <openssl/ec.h>
22#include <openssl/objects.h>
23
24
25/* Note that this is not a perfect emulation of the new function but
26 * is good enough for our case of printing certificate details during
27 * handshake */
28static inline int EVP_PKEY_get_group_name(EVP_PKEY *pkey,
29 char *gname,
30 size_t gname_sz,
31 size_t *gname_len)
32{
33 if (EVP_PKEY_get0_EC_KEY(pkey) == nullptr)
34 {
35 return 0;
36 }
37 const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
38 const EC_GROUP *group = EC_KEY_get0_group(ec);
39
40 int nid = EC_GROUP_get_curve_name(group);
41
42 if (nid == NID_undef)
43 {
44 return 0;
45 }
46 const char *curve = OBJ_nid2sn(nid);
47
48 std::strncpy(gname, curve, gname_sz - 1);
49 *gname_len = std::strlen(curve);
50 return 1;
51}
52
53/* Mimics the function but only when the default context without
54 * options is chosen */
55static inline const EVP_CIPHER *
56EVP_CIPHER_fetch(void *ctx, const char *algorithm, const char *properties)
57{
58 assert(!ctx);
59 assert(!properties);
60 const EVP_CIPHER *cipher = EVP_get_cipherbyname(algorithm);
61#ifdef OPENSSL_FIPS
62 /* Rhel 8/CentOS 8 have a patched OpenSSL version that return a cipher
63 * here that is actually not usable if in FIPS mode */
64
65 if (FIPS_mode() && !(EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_FIPS))
66 {
67 return nullptr;
68 }
69#endif
70 return cipher;
71}
72
73static inline EVP_PKEY *
75 EVP_PKEY **x,
76 pem_password_cb *cb,
77 void *u,
78 void *libctx,
79 const char *propq)
80{
81 return PEM_read_bio_PrivateKey(bp, x, cb, u);
82}
83
84static inline void
85EVP_CIPHER_free(const EVP_CIPHER *cipher)
86{
87 /* OpenSSL 1.1.1 and lower have no concept of dynamic EVP_CIPHER, so this is
88 * a noop */
89}
90
91static inline SSL_CTX *
92SSL_CTX_new_ex(void *libctx, const char *propq, const SSL_METHOD *meth)
93{
94 return SSL_CTX_new(meth);
95}
96
97static inline void
98OSSL_LIB_CTX_free(void *libctx)
99{
100}
101#define EVP_PKEY_get_bits EVP_PKEY_bits
102
103static inline const EVP_MD *
104EVP_MD_fetch(void *, const char *algorithm, const char *)
105{
106 return EVP_get_digestbyname(algorithm);
107}
108
109static inline void
110EVP_MD_free(const EVP_MD *md)
111{
112 /* OpenSSL 1.1.1 and lower use only const EVP_CIPHER, nothing to free */
113}
114
115#endif
116#if OPENSSL_VERSION_NUMBER < 0x30200000L && OPENSSL_VERSION_NUMBER >= 0x30000000L
117static inline const char *
118SSL_get0_group_name(SSL *s)
119{
120 /* int is the return type according the manual page but gcc complains that
121 * this a long to int conversion. So explicitly cast to int */
122 int nid = static_cast<int>(SSL_get_negotiated_group(s));
123 return SSL_group_to_name(s, nid);
124}
125#endif
static void EVP_CIPHER_free(const EVP_CIPHER *cipher)
Definition compat.hpp:85
static SSL_CTX * SSL_CTX_new_ex(void *libctx, const char *propq, const SSL_METHOD *meth)
Definition compat.hpp:92
static const EVP_CIPHER * EVP_CIPHER_fetch(void *ctx, const char *algorithm, const char *properties)
Definition compat.hpp:56
static EVP_PKEY * PEM_read_bio_PrivateKey_ex(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u, void *libctx, const char *propq)
Definition compat.hpp:74
static void OSSL_LIB_CTX_free(void *libctx)
Definition compat.hpp:98
static void EVP_MD_free(const EVP_MD *md)
Definition compat.hpp:110
static int EVP_PKEY_get_group_name(EVP_PKEY *pkey, char *gname, size_t gname_sz, size_t *gname_len)
Definition compat.hpp:28
static const EVP_MD * EVP_MD_fetch(void *, const char *algorithm, const char *)
Definition compat.hpp:104