OpenVPN
openssl_compat.h
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2026 OpenVPN Inc <sales@openvpn.net>
9 * Copyright (C) 2010-2026 Sentyron B.V. <openvpn@sentyron.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, see <https://www.gnu.org/licenses/>.
22 */
23
33
34#ifndef OPENSSL_COMPAT_H_
35#define OPENSSL_COMPAT_H_
36
37#ifdef HAVE_CONFIG_H
38#include "config.h"
39#endif
40
41#include "buffer.h"
42
43#include <openssl/rsa.h>
44#include <openssl/ssl.h>
45#include <openssl/x509.h>
46#include <openssl/err.h>
47
48/* Define the type of error. This is something that is less
49 * intrusive than casts everywhere */
50#if defined(OPENSSL_IS_AWSLC)
51typedef uint32_t openssl_err_t;
52typedef size_t openssl_stack_size_t;
53#define PRI_OPENSSL_STACK "zu"
54typedef uint32_t openssl_opt_t;
55#else
56typedef unsigned long openssl_err_t;
58#define PRI_OPENSSL_STACK "d"
59/* OpenSSL 4.0 actually uses bits in the upper half of the uint64_t (e.g.
60 * SSL_OP_PREFER_NO_DHE_KEX), so we really should use an uint64_t here */
61typedef uint64_t openssl_opt_t;
62#endif
63
64/* SSL_CTX_set1_groups() length: size_t in LibreSSL and AWS-LC, passed to
65 * SSL_CTX_ctrl() as long by OpenSSL 1.1.1's macro */
66#if defined(LIBRESSL_VERSION_NUMBER) || defined(OPENSSL_IS_AWSLC)
67typedef size_t openssl_groups_len_t;
68#else
70#endif
71
72
73/* Functionality missing in LibreSSL before 3.5 */
74#if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x3050000fL
75#define EVP_CTRL_AEAD_SET_TAG EVP_CTRL_GCM_SET_TAG
76#define EVP_CTRL_AEAD_GET_TAG EVP_CTRL_GCM_GET_TAG
77#endif
78
79#if defined(LIBRESSL_VERSION_NUMBER)
80#define RSA_F_RSA_OSSL_PRIVATE_ENCRYPT RSA_F_RSA_EAY_PRIVATE_ENCRYPT
81#endif
82
83#if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x3050400fL \
84 || defined(OPENSSL_IS_AWSLC)
85#define SSL_get_peer_tmp_key SSL_get_server_tmp_key
86#endif
87
88/* Functionality missing in 1.1.1 */
89#if OPENSSL_VERSION_NUMBER < 0x30000000L && !defined(OPENSSL_NO_EC)
90
91/* Note that this is not a perfect emulation of the new function but
92 * is good enough for our case of printing certificate details during
93 * handshake */
94static inline int
95EVP_PKEY_get_group_name(EVP_PKEY *pkey, char *gname, size_t gname_sz, size_t *gname_len)
96{
97 const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
98 if (ec == NULL)
99 {
100 return 0;
101 }
102 const EC_GROUP *group = EC_KEY_get0_group(ec);
103 int nid = EC_GROUP_get_curve_name(group);
104
105 if (nid == 0)
106 {
107 return 0;
108 }
109 const char *curve = OBJ_nid2sn(nid);
110 if (!curve)
111 {
112 curve = "(error fetching curve name)";
113 }
114
115 strlcpy(gname, curve, gname_sz);
116
117 /* strlcpy ensures null termination so just strlen is fine here */
118 *gname_len = strlen(gname);
119 return 1;
120}
121#endif /* if OPENSSL_VERSION_NUMBER < 0x30000000L && !defined(OPENSSL_NO_EC) */
122
123#if OPENSSL_VERSION_NUMBER < 0x30000000L
124#define EVP_MD_get0_name EVP_MD_name
125#define EVP_CIPHER_get0_name EVP_CIPHER_name
126#define EVP_CIPHER_CTX_get_mode EVP_CIPHER_CTX_mode
127
129#define SSL_CTX_new_ex(libctx, propq, method) SSL_CTX_new((method))
130
131/* Some safe typedefs to avoid too many ifdefs */
132typedef void OSSL_LIB_CTX;
133typedef void OSSL_PROVIDER;
134
135/* Mimics the functions but only when the default context without
136 * options is chosen */
137static inline const EVP_CIPHER *
138EVP_CIPHER_fetch(const void *ctx, const char *algorithm, const char *properties)
139{
140 ASSERT(!ctx);
141 ASSERT(!properties);
142 return EVP_get_cipherbyname(algorithm);
143}
144
145static inline const EVP_MD *
146EVP_MD_fetch(const void *ctx, const char *algorithm, const char *properties)
147{
148 ASSERT(!ctx);
149 ASSERT(!properties);
150 return EVP_get_digestbyname(algorithm);
151}
152
153static inline void
154EVP_CIPHER_free(const EVP_CIPHER *cipher)
155{
156 /* OpenSSL 1.1.1 and lower use only const EVP_CIPHER, nothing to free */
157}
158
159static inline void
160EVP_MD_free(const EVP_MD *md)
161{
162 /* OpenSSL 1.1.1 and lower use only const EVP_MD, nothing to free */
163}
164
165static inline openssl_err_t
166ERR_get_error_all(const char **file, int *line, const char **func, const char **data, int *flags)
167{
168 static const char *empty = "";
169 *func = empty;
170 openssl_err_t err = ERR_get_error_line_data(file, line, data, flags);
171 return err;
172}
173
174#endif /* OPENSSL_VERSION_NUMBER < 0x30000000L */
175
176#if OPENSSL_VERSION_NUMBER < 0x30500000 \
177 && (!defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER > 0x3050400fL)
178static inline int
179SSL_get0_peer_signature_name(SSL *ssl, const char **sigalg)
180{
181 int peer_sig_nid;
182 if (SSL_get_peer_signature_nid(ssl, &peer_sig_nid) && peer_sig_nid != NID_undef)
183 {
184 *sigalg = OBJ_nid2sn(peer_sig_nid);
185 return 1;
186 }
187 return 0;
188}
189#elif defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER <= 0x3050400fL
190/* The older LibreSSL version do not implement any variant of getting the peer
191 * signature */
192static inline int
193SSL_get0_peer_signature_name(const SSL *ssl, const char **sigalg)
194{
195 *sigalg = NULL;
196 return 0;
197}
198#endif /* if OPENSSL_VERSION_NUMBER < 0x30500000 && (!defined(LIBRESSL_VERSION_NUMBER) || \
199 LIBRESSL_VERSION_NUMBER > 0x3050400fL) */
200
201#if OPENSSL_VERSION_NUMBER < 0x30200000L && OPENSSL_VERSION_NUMBER >= 0x30000000L
202static inline const char *
203SSL_get0_group_name(SSL *s)
204{
205 int nid = (int)SSL_get_negotiated_group(s);
206 return SSL_group_to_name(s, nid);
207}
208#endif
209
210/* Introduced in OpenSSL 3.6.0 */
211#ifndef EVP_CIPH_FLAG_ENC_THEN_MAC
212#define EVP_CIPH_FLAG_ENC_THEN_MAC 0x10000000
213#endif
214
215/* Introduced in OpenSSL 4.1.0 */
216#if OPENSSL_VERSION_NUMBER < 0x40100000
217#define ASN1_STRING_get_length (size_t)ASN1_STRING_length
218#endif
219
220#endif /* OPENSSL_COMPAT_H_ */
Buffer management functions and garbage collection.
size_t strlcpy(char *dest, const char *src, size_t maxlen)
static openssl_err_t ERR_get_error_all(const char **file, int *line, const char **func, const char **data, int *flags)
static const EVP_MD * EVP_MD_fetch(const void *ctx, const char *algorithm, const char *properties)
static void EVP_CIPHER_free(const EVP_CIPHER *cipher)
void OSSL_PROVIDER
long openssl_groups_len_t
static int SSL_get0_peer_signature_name(SSL *ssl, const char **sigalg)
int openssl_stack_size_t
void OSSL_LIB_CTX
static void EVP_MD_free(const EVP_MD *md)
unsigned long openssl_err_t
static const EVP_CIPHER * EVP_CIPHER_fetch(const void *ctx, const char *algorithm, const char *properties)
static int EVP_PKEY_get_group_name(EVP_PKEY *pkey, char *gname, size_t gname_sz, size_t *gname_len)
uint64_t openssl_opt_t
#define ASSERT(x)
Definition error.h:221