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, see <https://www.gnu.org/licenses/>.
21 */
22
32#ifndef MBEDTLS_COMPAT_H_
33#define MBEDTLS_COMPAT_H_
34
35#include "syshead.h"
36
37#include "errlevel.h"
38
39#include <mbedtls/cipher.h>
40#include <mbedtls/ctr_drbg.h>
41#include <mbedtls/dhm.h>
42#include <mbedtls/ecp.h>
43#include <mbedtls/md.h>
44#include <mbedtls/pem.h>
45#include <mbedtls/pk.h>
46#include <mbedtls/ssl.h>
47#include <mbedtls/version.h>
48#include <mbedtls/x509_crt.h>
49
50#ifdef HAVE_PSA_CRYPTO_H
51#include <psa/crypto.h>
52#endif
53
54#if MBEDTLS_VERSION_NUMBER >= 0x03000000
55typedef uint16_t mbedtls_compat_group_id;
56#else
57typedef mbedtls_ecp_group_id mbedtls_compat_group_id;
58#endif
59
60static inline void
62{
63#if defined(HAVE_PSA_CRYPTO_H) && defined(MBEDTLS_PSA_CRYPTO_C)
64 if (psa_crypto_init() != PSA_SUCCESS)
65 {
66 msg(M_FATAL, "mbedtls: psa_crypto_init() failed");
67 }
68#else
69 return;
70#endif
71}
72
73static inline mbedtls_compat_group_id
74mbedtls_compat_get_group_id(const mbedtls_ecp_curve_info *curve_info)
75{
76#if MBEDTLS_VERSION_NUMBER >= 0x03000000
77 return curve_info->tls_id;
78#else
79 return curve_info->grp_id;
80#endif
81}
82
83/*
84 * In older versions of mbedtls, mbedtls_ctr_drbg_update() did not return an
85 * error code, and it was deprecated in favor of mbedtls_ctr_drbg_update_ret()
86 * which does.
87 *
88 * In mbedtls 3, this function was removed and mbedtls_ctr_drbg_update() returns
89 * an error code.
90 */
91static inline int
92mbedtls_compat_ctr_drbg_update(mbedtls_ctr_drbg_context *ctx, const unsigned char *additional,
93 size_t add_len)
94{
95#if MBEDTLS_VERSION_NUMBER > 0x03000000
96 return mbedtls_ctr_drbg_update(ctx, additional, add_len);
97#elif defined(HAVE_MBEDTLS_CTR_DRBG_UPDATE_RET)
98 return mbedtls_ctr_drbg_update_ret(ctx, additional, add_len);
99#else
100 mbedtls_ctr_drbg_update(ctx, additional, add_len);
101 return 0;
102#endif /* HAVE_MBEDTLS_CTR_DRBG_UPDATE_RET */
103}
104
105static inline int
106mbedtls_compat_pk_check_pair(const mbedtls_pk_context *pub, const mbedtls_pk_context *prv,
107 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
108{
109#if MBEDTLS_VERSION_NUMBER < 0x03020100
110 return mbedtls_pk_check_pair(pub, prv);
111#else
112 return mbedtls_pk_check_pair(pub, prv, f_rng, p_rng);
113#endif /* MBEDTLS_VERSION_NUMBER < 0x03020100 */
114}
115
116static inline int
117mbedtls_compat_pk_parse_key(mbedtls_pk_context *ctx, const unsigned char *key, size_t keylen,
118 const unsigned char *pwd, size_t pwdlen,
119 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
120{
121#if MBEDTLS_VERSION_NUMBER < 0x03020100
122 return mbedtls_pk_parse_key(ctx, key, keylen, pwd, pwdlen);
123#else
124 return mbedtls_pk_parse_key(ctx, key, keylen, pwd, pwdlen, f_rng, p_rng);
125#endif
126}
127
128static inline int
129mbedtls_compat_pk_parse_keyfile(mbedtls_pk_context *ctx, const char *path, const char *password,
130 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
131{
132#if MBEDTLS_VERSION_NUMBER < 0x03020100
133 return mbedtls_pk_parse_keyfile(ctx, path, password);
134#else
135 return mbedtls_pk_parse_keyfile(ctx, path, password, f_rng, p_rng);
136#endif
137}
138
139#if MBEDTLS_VERSION_NUMBER < 0x03020100
146
147static inline void
149{
150 int major = (tls_version >> 8) & 0xff;
151 int minor = tls_version & 0xff;
152 mbedtls_ssl_conf_min_version(conf, major, minor);
153}
154
155static inline void
157{
158 int major = (tls_version >> 8) & 0xff;
159 int minor = tls_version & 0xff;
160 mbedtls_ssl_conf_max_version(conf, major, minor);
161}
162
163static inline void
164mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf, mbedtls_compat_group_id *groups)
165{
166 mbedtls_ssl_conf_curves(conf, groups);
167}
168
169static inline size_t
170mbedtls_cipher_info_get_block_size(const mbedtls_cipher_info_t *cipher)
171{
172 return (size_t)cipher->block_size;
173}
174
175static inline size_t
176mbedtls_cipher_info_get_iv_size(const mbedtls_cipher_info_t *cipher)
177{
178 return (size_t)cipher->iv_size;
179}
180
181static inline size_t
182mbedtls_cipher_info_get_key_bitlen(const mbedtls_cipher_info_t *cipher)
183{
184 return (size_t)cipher->key_bitlen;
185}
186
187static inline mbedtls_cipher_mode_t
188mbedtls_cipher_info_get_mode(const mbedtls_cipher_info_t *cipher)
189{
190 return cipher->mode;
191}
192
193static inline const char *
194mbedtls_cipher_info_get_name(const mbedtls_cipher_info_t *cipher)
195{
196 return cipher->name;
197}
198
199static inline mbedtls_cipher_type_t
200mbedtls_cipher_info_get_type(const mbedtls_cipher_info_t *cipher)
201{
202 return cipher->type;
203}
204
205static inline size_t
206mbedtls_dhm_get_bitlen(const mbedtls_dhm_context *ctx)
207{
208 return 8 * ctx->len;
209}
210
211static inline const mbedtls_md_info_t *
212mbedtls_md_info_from_ctx(const mbedtls_md_context_t *ctx)
213{
214 return ctx->md_info;
215}
216
217static inline const unsigned char *
218mbedtls_pem_get_buffer(const mbedtls_pem_context *ctx, size_t *buf_size)
219{
220 *buf_size = ctx->buflen;
221 return ctx->buf;
222}
223
224static inline int
225mbedtls_x509_crt_has_ext_type(const mbedtls_x509_crt *ctx, int ext_type)
226{
227 return ctx->ext_types & ext_type;
228}
229#endif /* MBEDTLS_VERSION_NUMBER < 0x03020100 */
230
231#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:88
#define msg(flags,...)
Definition error.h:150
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152