OpenVPN
mbedtls_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) 2023 Fox Crypto B.V. <openvpn@foxcrypto.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
33#ifndef MBEDTLS_COMPAT_H_
34#define MBEDTLS_COMPAT_H_
35
36#include "syshead.h"
37
38#include "errlevel.h"
39
40#include <mbedtls/cipher.h>
41#include <mbedtls/ctr_drbg.h>
42#include <mbedtls/dhm.h>
43#include <mbedtls/ecp.h>
44#include <mbedtls/md.h>
45#include <mbedtls/pem.h>
46#include <mbedtls/pk.h>
47#include <mbedtls/ssl.h>
48#include <mbedtls/version.h>
49#include <mbedtls/x509_crt.h>
50
51#if HAVE_MBEDTLS_PSA_CRYPTO_H
52 #include <psa/crypto.h>
53#endif
54
55#if MBEDTLS_VERSION_NUMBER >= 0x03000000
56typedef uint16_t mbedtls_compat_group_id;
57#else
58typedef mbedtls_ecp_group_id mbedtls_compat_group_id;
59#endif
60
61static inline void
63{
64#if HAVE_MBEDTLS_PSA_CRYPTO_H && defined(MBEDTLS_PSA_CRYPTO_C)
65 if (psa_crypto_init() != PSA_SUCCESS)
66 {
67 msg(M_FATAL, "mbedtls: psa_crypto_init() failed");
68 }
69#else
70 return;
71#endif /* HAVE_MBEDTLS_PSA_CRYPTO_H && defined(MBEDTLS_PSA_CRYPTO_C) */
72}
73
74static inline mbedtls_compat_group_id
75mbedtls_compat_get_group_id(const mbedtls_ecp_curve_info *curve_info)
76{
77#if MBEDTLS_VERSION_NUMBER >= 0x03000000
78 return curve_info->tls_id;
79#else
80 return curve_info->grp_id;
81#endif
82}
83
84/*
85 * In older versions of mbedtls, mbedtls_ctr_drbg_update() did not return an
86 * error code, and it was deprecated in favor of mbedtls_ctr_drbg_update_ret()
87 * which does.
88 *
89 * In mbedtls 3, this function was removed and mbedtls_ctr_drbg_update() returns
90 * an error code.
91 */
92static inline int
93mbedtls_compat_ctr_drbg_update(mbedtls_ctr_drbg_context *ctx,
94 const unsigned char *additional,
95 size_t add_len)
96{
97#if MBEDTLS_VERSION_NUMBER > 0x03000000
98 return mbedtls_ctr_drbg_update(ctx, additional, add_len);
99#elif HAVE_MBEDTLS_CTR_DRBG_UPDATE_RET
100 return mbedtls_ctr_drbg_update_ret(ctx, additional, add_len);
101#else
102 mbedtls_ctr_drbg_update(ctx, additional, add_len);
103 return 0;
104#endif /* HAVE_MBEDTLS_CTR_DRBG_UPDATE_RET */
105}
106
107static inline int
108mbedtls_compat_pk_check_pair(const mbedtls_pk_context *pub, const mbedtls_pk_context *prv,
109 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
110{
111#if MBEDTLS_VERSION_NUMBER < 0x03020100
112 return mbedtls_pk_check_pair(pub, prv);
113#else
114 return mbedtls_pk_check_pair(pub, prv, f_rng, p_rng);
115#endif /* MBEDTLS_VERSION_NUMBER < 0x03020100 */
116}
117
118static inline int
119mbedtls_compat_pk_parse_key(mbedtls_pk_context *ctx,
120 const unsigned char *key, size_t keylen,
121 const unsigned char *pwd, size_t pwdlen,
122 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
123{
124#if MBEDTLS_VERSION_NUMBER < 0x03020100
125 return mbedtls_pk_parse_key(ctx, key, keylen, pwd, pwdlen);
126#else
127 return mbedtls_pk_parse_key(ctx, key, keylen, pwd, pwdlen, f_rng, p_rng);
128#endif
129}
130
131static inline int
132mbedtls_compat_pk_parse_keyfile(mbedtls_pk_context *ctx,
133 const char *path, const char *password,
134 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
135{
136#if MBEDTLS_VERSION_NUMBER < 0x03020100
137 return mbedtls_pk_parse_keyfile(ctx, path, password);
138#else
139 return mbedtls_pk_parse_keyfile(ctx, path, password, f_rng, p_rng);
140#endif
141}
142
143#if MBEDTLS_VERSION_NUMBER < 0x03020100
149
150static inline void
152{
153 int major = (tls_version >> 8) & 0xff;
154 int minor = tls_version & 0xff;
155 mbedtls_ssl_conf_min_version(conf, major, minor);
156}
157
158static inline void
160{
161 int major = (tls_version >> 8) & 0xff;
162 int minor = tls_version & 0xff;
163 mbedtls_ssl_conf_max_version(conf, major, minor);
164}
165
166static inline void
167mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf, mbedtls_compat_group_id *groups)
168{
169 mbedtls_ssl_conf_curves(conf, groups);
170}
171
172static inline size_t
173mbedtls_cipher_info_get_block_size(const mbedtls_cipher_info_t *cipher)
174{
175 return (size_t)cipher->block_size;
176}
177
178static inline size_t
179mbedtls_cipher_info_get_iv_size(const mbedtls_cipher_info_t *cipher)
180{
181 return (size_t)cipher->iv_size;
182}
183
184static inline size_t
185mbedtls_cipher_info_get_key_bitlen(const mbedtls_cipher_info_t *cipher)
186{
187 return (size_t)cipher->key_bitlen;
188}
189
190static inline mbedtls_cipher_mode_t
191mbedtls_cipher_info_get_mode(const mbedtls_cipher_info_t *cipher)
192{
193 return cipher->mode;
194}
195
196static inline const char *
197mbedtls_cipher_info_get_name(const mbedtls_cipher_info_t *cipher)
198{
199 return cipher->name;
200}
201
202static inline mbedtls_cipher_type_t
203mbedtls_cipher_info_get_type(const mbedtls_cipher_info_t *cipher)
204{
205 return cipher->type;
206}
207
208static inline size_t
209mbedtls_dhm_get_bitlen(const mbedtls_dhm_context *ctx)
210{
211 return 8 * ctx->len;
212}
213
214static inline const mbedtls_md_info_t *
215mbedtls_md_info_from_ctx(const mbedtls_md_context_t *ctx)
216{
217 return ctx->md_info;
218}
219
220static inline const unsigned char *
221mbedtls_pem_get_buffer(const mbedtls_pem_context *ctx, size_t *buf_size)
222{
223 *buf_size = ctx->buflen;
224 return ctx->buf;
225}
226
227static inline int
228mbedtls_x509_crt_has_ext_type(const mbedtls_x509_crt *ctx, int ext_type)
229{
230 return ctx->ext_types & ext_type;
231}
232#endif /* MBEDTLS_VERSION_NUMBER < 0x03020100 */
233
234#endif /* MBEDTLS_COMPAT_H_ */
static int mbedtls_compat_pk_parse_key(mbedtls_pk_context *ctx, const unsigned char *key, size_t keylen, const unsigned char *pwd, size_t pwdlen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
static const char * mbedtls_cipher_info_get_name(const mbedtls_cipher_info_t *cipher)
static size_t mbedtls_cipher_info_get_key_bitlen(const mbedtls_cipher_info_t *cipher)
static const mbedtls_md_info_t * mbedtls_md_info_from_ctx(const mbedtls_md_context_t *ctx)
mbedtls_ssl_protocol_version
@ MBEDTLS_SSL_VERSION_TLS1_2
@ MBEDTLS_SSL_VERSION_TLS1_3
@ MBEDTLS_SSL_VERSION_UNKNOWN
static int mbedtls_x509_crt_has_ext_type(const mbedtls_x509_crt *ctx, int ext_type)
static size_t mbedtls_cipher_info_get_iv_size(const mbedtls_cipher_info_t *cipher)
static void mbedtls_compat_psa_crypto_init(void)
static mbedtls_compat_group_id mbedtls_compat_get_group_id(const mbedtls_ecp_curve_info *curve_info)
static int mbedtls_compat_ctr_drbg_update(mbedtls_ctr_drbg_context *ctx, const unsigned char *additional, size_t add_len)
static int mbedtls_compat_pk_check_pair(const mbedtls_pk_context *pub, const mbedtls_pk_context *prv, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
static void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf, mbedtls_compat_group_id *groups)
static mbedtls_cipher_type_t mbedtls_cipher_info_get_type(const mbedtls_cipher_info_t *cipher)
static size_t mbedtls_dhm_get_bitlen(const mbedtls_dhm_context *ctx)
static void mbedtls_ssl_conf_max_tls_version(mbedtls_ssl_config *conf, mbedtls_ssl_protocol_version tls_version)
mbedtls_ecp_group_id mbedtls_compat_group_id
static size_t mbedtls_cipher_info_get_block_size(const mbedtls_cipher_info_t *cipher)
static mbedtls_cipher_mode_t mbedtls_cipher_info_get_mode(const mbedtls_cipher_info_t *cipher)
static const unsigned char * mbedtls_pem_get_buffer(const mbedtls_pem_context *ctx, size_t *buf_size)
static void mbedtls_ssl_conf_min_tls_version(mbedtls_ssl_config *conf, mbedtls_ssl_protocol_version tls_version)
static int mbedtls_compat_pk_parse_keyfile(mbedtls_pk_context *ctx, const char *path, const char *password, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
#define M_FATAL
Definition error.h:89
#define msg(flags,...)
Definition error.h:144
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152