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/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 #if HAVE_MBEDTLS_PSA_CRYPTO_H
51  #include <psa/crypto.h>
52 #endif
53 
54 static inline void
56 {
57 #if HAVE_MBEDTLS_PSA_CRYPTO_H && defined(MBEDTLS_PSA_CRYPTO_C)
58  if (psa_crypto_init() != PSA_SUCCESS)
59  {
60  msg(M_FATAL, "mbedtls: psa_crypto_init() failed");
61  }
62 #else
63  return;
64 #endif /* HAVE_MBEDTLS_PSA_CRYPTO_H && defined(MBEDTLS_PSA_CRYPTO_C) */
65 }
66 
67 /*
68  * In older versions of mbedtls, mbedtls_ctr_drbg_update() did not return an
69  * error code, and it was deprecated in favor of mbedtls_ctr_drbg_update_ret()
70  * which does.
71  *
72  * In mbedtls 3, this function was removed and mbedtls_ctr_drbg_update() returns
73  * an error code.
74  */
75 static inline int
76 mbedtls_compat_ctr_drbg_update(mbedtls_ctr_drbg_context *ctx,
77  const unsigned char *additional,
78  size_t add_len)
79 {
80 #if MBEDTLS_VERSION_NUMBER > 0x03000000
81  return mbedtls_ctr_drbg_update(ctx, additional, add_len);
82 #elif HAVE_MBEDTLS_CTR_DRBG_UPDATE_RET
83  return mbedtls_ctr_drbg_update_ret(ctx, additional, add_len);
84 #else
85  mbedtls_ctr_drbg_update(ctx, additional, add_len);
86  return 0;
87 #endif /* HAVE_MBEDTLS_CTR_DRBG_UPDATE_RET */
88 }
89 
90 static inline int
91 mbedtls_compat_pk_check_pair(const mbedtls_pk_context *pub, const mbedtls_pk_context *prv,
92  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
93 {
94 #if MBEDTLS_VERSION_NUMBER < 0x03020100
95  return mbedtls_pk_check_pair(pub, prv);
96 #else
97  return mbedtls_pk_check_pair(pub, prv, f_rng, p_rng);
98 #endif /* MBEDTLS_VERSION_NUMBER < 0x03020100 */
99 }
100 
101 static inline int
102 mbedtls_compat_pk_parse_key(mbedtls_pk_context *ctx,
103  const unsigned char *key, size_t keylen,
104  const unsigned char *pwd, size_t pwdlen,
105  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
106 {
107 #if MBEDTLS_VERSION_NUMBER < 0x03020100
108  return mbedtls_pk_parse_key(ctx, key, keylen, pwd, pwdlen);
109 #else
110  return mbedtls_pk_parse_key(ctx, key, keylen, pwd, pwdlen, f_rng, p_rng);
111 #endif
112 }
113 
114 static inline int
115 mbedtls_compat_pk_parse_keyfile(mbedtls_pk_context *ctx,
116  const char *path, const char *password,
117  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
118 {
119 #if MBEDTLS_VERSION_NUMBER < 0x03020100
120  return mbedtls_pk_parse_keyfile(ctx, path, password);
121 #else
122  return mbedtls_pk_parse_keyfile(ctx, path, password, f_rng, p_rng);
123 #endif
124 }
125 
126 #if MBEDTLS_VERSION_NUMBER < 0x03020100
127 static inline size_t
128 mbedtls_cipher_info_get_block_size(const mbedtls_cipher_info_t *cipher)
129 {
130  return (size_t)cipher->block_size;
131 }
132 
133 static inline size_t
134 mbedtls_cipher_info_get_iv_size(const mbedtls_cipher_info_t *cipher)
135 {
136  return (size_t)cipher->iv_size;
137 }
138 
139 static inline size_t
140 mbedtls_cipher_info_get_key_bitlen(const mbedtls_cipher_info_t *cipher)
141 {
142  return (size_t)cipher->key_bitlen;
143 }
144 
145 static inline mbedtls_cipher_mode_t
146 mbedtls_cipher_info_get_mode(const mbedtls_cipher_info_t *cipher)
147 {
148  return cipher->mode;
149 }
150 
151 static inline const char *
152 mbedtls_cipher_info_get_name(const mbedtls_cipher_info_t *cipher)
153 {
154  return cipher->name;
155 }
156 
157 static inline mbedtls_cipher_type_t
158 mbedtls_cipher_info_get_type(const mbedtls_cipher_info_t *cipher)
159 {
160  return cipher->type;
161 }
162 
163 static inline size_t
164 mbedtls_dhm_get_bitlen(const mbedtls_dhm_context *ctx)
165 {
166  return 8 * ctx->len;
167 }
168 
169 static inline const mbedtls_md_info_t *
170 mbedtls_md_info_from_ctx(const mbedtls_md_context_t *ctx)
171 {
172  return ctx->md_info;
173 }
174 
175 static inline const unsigned char *
176 mbedtls_pem_get_buffer(const mbedtls_pem_context *ctx, size_t *buf_size)
177 {
178  *buf_size = ctx->buflen;
179  return ctx->buf;
180 }
181 
182 static inline int
183 mbedtls_x509_crt_has_ext_type(const mbedtls_x509_crt *ctx, int ext_type)
184 {
185  return ctx->ext_types & ext_type;
186 }
187 #endif /* MBEDTLS_VERSION_NUMBER < 0x03020100 */
188 
189 #endif /* MBEDTLS_COMPAT_H_ */
mbedtls_compat_pk_parse_key
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)
Definition: mbedtls_compat.h:102
M_FATAL
#define M_FATAL
Definition: error.h:95
mbedtls_compat_ctr_drbg_update
static int mbedtls_compat_ctr_drbg_update(mbedtls_ctr_drbg_context *ctx, const unsigned char *additional, size_t add_len)
Definition: mbedtls_compat.h:76
mbedtls_compat_pk_check_pair
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)
Definition: mbedtls_compat.h:91
key
Container for unidirectional cipher and HMAC key material.
Definition: crypto.h:149
mbedtls_md_info_from_ctx
static const mbedtls_md_info_t * mbedtls_md_info_from_ctx(const mbedtls_md_context_t *ctx)
Definition: mbedtls_compat.h:170
mbedtls_cipher_info_get_key_bitlen
static size_t mbedtls_cipher_info_get_key_bitlen(const mbedtls_cipher_info_t *cipher)
Definition: mbedtls_compat.h:140
mbedtls_cipher_info_get_name
static const char * mbedtls_cipher_info_get_name(const mbedtls_cipher_info_t *cipher)
Definition: mbedtls_compat.h:152
mbedtls_cipher_info_get_type
static mbedtls_cipher_type_t mbedtls_cipher_info_get_type(const mbedtls_cipher_info_t *cipher)
Definition: mbedtls_compat.h:158
mbedtls_pem_get_buffer
static const unsigned char * mbedtls_pem_get_buffer(const mbedtls_pem_context *ctx, size_t *buf_size)
Definition: mbedtls_compat.h:176
errlevel.h
mbedtls_dhm_get_bitlen
static size_t mbedtls_dhm_get_bitlen(const mbedtls_dhm_context *ctx)
Definition: mbedtls_compat.h:164
mbedtls_compat_psa_crypto_init
static void mbedtls_compat_psa_crypto_init(void)
Definition: mbedtls_compat.h:55
mbedtls_cipher_info_get_block_size
static size_t mbedtls_cipher_info_get_block_size(const mbedtls_cipher_info_t *cipher)
Definition: mbedtls_compat.h:128
syshead.h
mbedtls_cipher_info_get_iv_size
static size_t mbedtls_cipher_info_get_iv_size(const mbedtls_cipher_info_t *cipher)
Definition: mbedtls_compat.h:134
mbedtls_x509_crt_has_ext_type
static int mbedtls_x509_crt_has_ext_type(const mbedtls_x509_crt *ctx, int ext_type)
Definition: mbedtls_compat.h:183
mbedtls_compat_pk_parse_keyfile
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)
Definition: mbedtls_compat.h:115
msg
#define msg(flags,...)
Definition: error.h:150
mbedtls_cipher_info_get_mode
static mbedtls_cipher_mode_t mbedtls_cipher_info_get_mode(const mbedtls_cipher_info_t *cipher)
Definition: mbedtls_compat.h:146