Browse code

Add support for mbedtls 3.X.Y

Most struct fields in mbedtls 3 are private and now need accessor
functions. Most of it was straightforward to adapt, but for two things
there were no accessor functions yet:

* Netscape certificate type
* key usage (you can check key usage, but not get the raw bytes)

I decided to remove Netscape certificate type checks when using OpenVPN
with mbedtls. The key usage bytes were printed in an error message, and
I removed that part from it.

Adding the random number functions to the load private key function may
look weird, but the purpose is to make side channels for elliptic curve
operations harder to exploit.

Change-Id: I445a93e84dc54b865b757038d22318ac427fce96
Signed-off-by: Max Fillinger <max@max-fillinger.net>
Acked-by: Frank Lichtenheld <frank@lichtenheld.com>
Message-Id: <20231025121830.1030959-1-frank@lichtenheld.com>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg27295.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>
(cherry picked from commit ace7a4f1c271550bb8ad276663e045ab97a46f16)

Max Fillinger authored on 2023/10/25 21:18:30
Showing 6 changed files
... ...
@@ -1029,15 +1029,21 @@ elif test "${with_crypto_library}" = "mbedtls"; then
1029 1029
 #include <mbedtls/version.h>
1030 1030
 			]],
1031 1031
 			[[
1032
-#if MBEDTLS_VERSION_NUMBER < 0x02000000 || MBEDTLS_VERSION_NUMBER >= 0x03000000
1032
+#if MBEDTLS_VERSION_NUMBER < 0x02000000 || (MBEDTLS_VERSION_NUMBER >= 0x03000000 && MBEDTLS_VERSION_NUMBER < 0x03020100)
1033 1033
 #error invalid version
1034 1034
 #endif
1035 1035
 			]]
1036 1036
 		)],
1037 1037
 		[AC_MSG_RESULT([ok])],
1038
-		[AC_MSG_ERROR([mbed TLS 2.y.z required])]
1038
+		[AC_MSG_ERROR([mbed TLS version >= 2.0.0 or >= 3.2.1 required])]
1039 1039
 	)
1040 1040
 
1041
+    AC_CHECK_HEADER(
1042
+        psa/crypto.h,
1043
+        [AC_DEFINE([MBEDTLS_HAVE_PSA_CRYPTO_H], [1], [yes])],
1044
+        [AC_DEFINE([MBEDTLS_HAVE_PSA_CRYPTO_H], [0], [no])]
1045
+    )
1046
+
1041 1047
 	AC_CHECK_FUNCS(
1042 1048
 		[ \
1043 1049
 			mbedtls_cipher_write_tag \
... ...
@@ -41,6 +41,7 @@
41 41
 #include "integer.h"
42 42
 #include "crypto_backend.h"
43 43
 #include "otime.h"
44
+#include "mbedtls_compat.h"
44 45
 #include "misc.h"
45 46
 
46 47
 #include <mbedtls/base64.h>
... ...
@@ -170,10 +171,11 @@ show_available_ciphers(void)
170 170
     while (*ciphers != 0)
171 171
     {
172 172
         const mbedtls_cipher_info_t *info = mbedtls_cipher_info_from_type(*ciphers);
173
-        if (info && !cipher_kt_insecure(info->name)
174
-            && (cipher_kt_mode_aead(info->name) || cipher_kt_mode_cbc(info->name)))
173
+        const char *name = mbedtls_cipher_info_get_name(info);
174
+        if (info && name && !cipher_kt_insecure(name)
175
+            && (cipher_kt_mode_aead(name) || cipher_kt_mode_cbc(name)))
175 176
         {
176
-            print_cipher(info->name);
177
+            print_cipher(name);
177 178
         }
178 179
         ciphers++;
179 180
     }
... ...
@@ -184,10 +186,11 @@ show_available_ciphers(void)
184 184
     while (*ciphers != 0)
185 185
     {
186 186
         const mbedtls_cipher_info_t *info = mbedtls_cipher_info_from_type(*ciphers);
187
-        if (info && cipher_kt_insecure(info->name)
188
-            && (cipher_kt_mode_aead(info->name) || cipher_kt_mode_cbc(info->name)))
187
+        const char *name = mbedtls_cipher_info_get_name(info);
188
+        if (info && name && cipher_kt_insecure(name)
189
+            && (cipher_kt_mode_aead(name) || cipher_kt_mode_cbc(name)))
189 190
         {
190
-            print_cipher(info->name);
191
+            print_cipher(name);
191 192
         }
192 193
         ciphers++;
193 194
     }
... ...
@@ -295,7 +298,9 @@ crypto_pem_decode(const char *name, struct buffer *dst,
295 295
     mbedtls_pem_context ctx = { 0 };
296 296
     bool ret = mbed_ok(mbedtls_pem_read_buffer(&ctx, header, footer, BPTR(&input),
297 297
                                                NULL, 0, &use_len));
298
-    if (ret && !buf_write(dst, ctx.buf, ctx.buflen))
298
+    size_t buf_size = 0;
299
+    const unsigned char *buf = mbedtls_pem_get_buffer(&ctx, &buf_size);
300
+    if (ret && !buf_write(dst, buf, buf_size))
299 301
     {
300 302
         ret = false;
301 303
         msg(M_WARN, "PEM decode error: destination buffer too small");
... ...
@@ -416,11 +421,12 @@ cipher_valid_reason(const char *ciphername, const char **reason)
416 416
         return false;
417 417
     }
418 418
 
419
-    if (cipher->key_bitlen/8 > MAX_CIPHER_KEY_LENGTH)
419
+    const size_t key_bytelen = mbedtls_cipher_info_get_key_bitlen(cipher)/8;
420
+    if (key_bytelen > MAX_CIPHER_KEY_LENGTH)
420 421
     {
421
-        msg(D_LOW, "Cipher algorithm '%s' uses a default key size (%d bytes) "
422
+        msg(D_LOW, "Cipher algorithm '%s' uses a default key size (%zu bytes) "
422 423
             "which is larger than " PACKAGE_NAME "'s current maximum key size "
423
-            "(%d bytes)", ciphername, cipher->key_bitlen/8, MAX_CIPHER_KEY_LENGTH);
424
+            "(%d bytes)", ciphername, key_bytelen, MAX_CIPHER_KEY_LENGTH);
424 425
         *reason = "disabled due to key size too large";
425 426
         return false;
426 427
     }
... ...
@@ -438,7 +444,7 @@ cipher_kt_name(const char *ciphername)
438 438
         return "[null-cipher]";
439 439
     }
440 440
 
441
-    return translate_cipher_name_to_openvpn(cipher_kt->name);
441
+    return translate_cipher_name_to_openvpn(mbedtls_cipher_info_get_name(cipher_kt));
442 442
 }
443 443
 
444 444
 int
... ...
@@ -451,7 +457,7 @@ cipher_kt_key_size(const char *ciphername)
451 451
         return 0;
452 452
     }
453 453
 
454
-    return cipher_kt->key_bitlen/8;
454
+    return (int)mbedtls_cipher_info_get_key_bitlen(cipher_kt)/8;
455 455
 }
456 456
 
457 457
 int
... ...
@@ -463,7 +469,7 @@ cipher_kt_iv_size(const char *ciphername)
463 463
     {
464 464
         return 0;
465 465
     }
466
-    return cipher_kt->iv_size;
466
+    return (int)mbedtls_cipher_info_get_iv_size(cipher_kt);
467 467
 }
468 468
 
469 469
 int
... ...
@@ -474,7 +480,7 @@ cipher_kt_block_size(const char *ciphername)
474 474
     {
475 475
         return 0;
476 476
     }
477
-    return cipher_kt->block_size;
477
+    return (int)mbedtls_cipher_info_get_block_size(cipher_kt);
478 478
 }
479 479
 
480 480
 int
... ...
@@ -498,16 +504,16 @@ cipher_kt_insecure(const char *ciphername)
498 498
 
499 499
     return !(cipher_kt_block_size(ciphername) >= 128 / 8
500 500
 #ifdef MBEDTLS_CHACHAPOLY_C
501
-             || cipher_kt->type == MBEDTLS_CIPHER_CHACHA20_POLY1305
501
+             || mbedtls_cipher_info_get_type(cipher_kt) == MBEDTLS_CIPHER_CHACHA20_POLY1305
502 502
 #endif
503 503
              );
504 504
 }
505 505
 
506
-static int
506
+static mbedtls_cipher_mode_t
507 507
 cipher_kt_mode(const mbedtls_cipher_info_t *cipher_kt)
508 508
 {
509 509
     ASSERT(NULL != cipher_kt);
510
-    return cipher_kt->mode;
510
+    return mbedtls_cipher_info_get_mode(cipher_kt);
511 511
 }
512 512
 
513 513
 bool
... ...
@@ -566,22 +572,29 @@ cipher_ctx_init(mbedtls_cipher_context_t *ctx, const uint8_t *key,
566 566
     CLEAR(*ctx);
567 567
 
568 568
     const mbedtls_cipher_info_t *kt = cipher_get(ciphername);
569
-    int key_len = kt->key_bitlen/8;
570
-
571 569
     ASSERT(kt);
570
+    size_t key_bitlen = mbedtls_cipher_info_get_key_bitlen(kt);
572 571
 
573 572
     if (!mbed_ok(mbedtls_cipher_setup(ctx, kt)))
574 573
     {
575 574
         msg(M_FATAL, "mbed TLS cipher context init #1");
576 575
     }
577 576
 
578
-    if (!mbed_ok(mbedtls_cipher_setkey(ctx, key, key_len*8, operation)))
577
+    if (!mbed_ok(mbedtls_cipher_setkey(ctx, key, (int)key_bitlen, operation)))
579 578
     {
580 579
         msg(M_FATAL, "mbed TLS cipher set key");
581 580
     }
582 581
 
582
+    if (mbedtls_cipher_info_get_mode(kt) == MBEDTLS_MODE_CBC)
583
+    {
584
+        if (!mbed_ok(mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_PKCS7)))
585
+        {
586
+            msg(M_FATAL, "mbed TLS cipher set padding mode");
587
+        }
588
+    }
589
+
583 590
     /* make sure we used a big enough key */
584
-    ASSERT(ctx->key_bitlen <= key_len*8);
591
+    ASSERT(mbedtls_cipher_get_key_bitlen(ctx) <= key_bitlen);
585 592
 }
586 593
 
587 594
 int
... ...
@@ -609,7 +622,7 @@ cipher_ctx_get_tag(cipher_ctx_t *ctx, uint8_t *tag, int tag_len)
609 609
 int
610 610
 cipher_ctx_block_size(const mbedtls_cipher_context_t *ctx)
611 611
 {
612
-    return mbedtls_cipher_get_block_size(ctx);
612
+    return (int)mbedtls_cipher_get_block_size(ctx);
613 613
 }
614 614
 
615 615
 int
... ...
@@ -617,7 +630,7 @@ cipher_ctx_mode(const mbedtls_cipher_context_t *ctx)
617 617
 {
618 618
     ASSERT(NULL != ctx);
619 619
 
620
-    return cipher_kt_mode(ctx->cipher_info);
620
+    return mbedtls_cipher_get_cipher_mode(ctx);
621 621
 }
622 622
 
623 623
 bool
... ...
@@ -652,7 +665,7 @@ cipher_ctx_reset(mbedtls_cipher_context_t *ctx, const uint8_t *iv_buf)
652 652
         return 0;
653 653
     }
654 654
 
655
-    if (!mbed_ok(mbedtls_cipher_set_iv(ctx, iv_buf, ctx->cipher_info->iv_size)))
655
+    if (!mbed_ok(mbedtls_cipher_set_iv(ctx, iv_buf, (size_t)mbedtls_cipher_get_iv_size(ctx))))
656 656
     {
657 657
         return 0;
658 658
     }
... ...
@@ -714,7 +727,7 @@ cipher_ctx_final_check_tag(mbedtls_cipher_context_t *ctx, uint8_t *dst,
714 714
 {
715 715
     size_t olen = 0;
716 716
 
717
-    if (MBEDTLS_DECRYPT != ctx->operation)
717
+    if (MBEDTLS_DECRYPT != mbedtls_cipher_get_operation(ctx))
718 718
     {
719 719
         return 0;
720 720
     }
... ...
@@ -866,7 +879,7 @@ md_ctx_size(const mbedtls_md_context_t *ctx)
866 866
     {
867 867
         return 0;
868 868
     }
869
-    return mbedtls_md_get_size(ctx->md_info);
869
+    return (int)mbedtls_md_get_size(mbedtls_md_info_from_ctx(ctx));
870 870
 }
871 871
 
872 872
 void
... ...
@@ -936,7 +949,7 @@ hmac_ctx_size(mbedtls_md_context_t *ctx)
936 936
     {
937 937
         return 0;
938 938
     }
939
-    return mbedtls_md_get_size(ctx->md_info);
939
+    return mbedtls_md_get_size(mbedtls_md_info_from_ctx(ctx));
940 940
 }
941 941
 
942 942
 void
943 943
new file mode 100644
... ...
@@ -0,0 +1,186 @@
0
+/*
1
+ *  OpenVPN -- An application to securely tunnel IP networks
2
+ *             over a single TCP/UDP 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) 2023 Fox Crypto B.V. <openvpn@foxcrypto.com>
8
+ *
9
+ *  This program is free software; you can redistribute it and/or modify
10
+ *  it under the terms of the GNU General Public License version 2
11
+ *  as published by the Free Software Foundation.
12
+ *
13
+ *  This program is distributed in the hope that it will be useful,
14
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ *  GNU General Public License for more details.
17
+ *
18
+ *  You should have received a copy of the GNU General Public License along
19
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
20
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
+ */
22
+
23
+/**
24
+ * @file mbedtls compatibility stub
25
+ *
26
+ * This file provide compatibility stubs for the mbedtls libraries
27
+ * prior to version 3. This version made most fields in structs private
28
+ * and requires accessor functions to be used. For earlier versions, we
29
+ * implement the accessor functions here.
30
+ */
31
+
32
+#ifndef MBEDTLS_COMPAT_H_
33
+#define MBEDTLS_COMPAT_H_
34
+
35
+#include "errlevel.h"
36
+
37
+#include <mbedtls/cipher.h>
38
+#include <mbedtls/ctr_drbg.h>
39
+#include <mbedtls/dhm.h>
40
+#include <mbedtls/md.h>
41
+#include <mbedtls/pem.h>
42
+#include <mbedtls/pk.h>
43
+#include <mbedtls/version.h>
44
+#include <mbedtls/x509_crt.h>
45
+
46
+#if MBEDTLS_HAVE_PSA_CRYPTO_H
47
+    #include <psa/crypto.h>
48
+#endif
49
+
50
+static inline void
51
+mbedtls_compat_psa_crypto_init(void)
52
+{
53
+#if MBEDTLS_HAVE_PSA_CRYPTO_H && defined(MBEDTLS_PSA_CRYPTO_C)
54
+    if (psa_crypto_init() != PSA_SUCCESS)
55
+    {
56
+        msg(M_FATAL, "mbedtls: psa_crypto_init() failed");
57
+    }
58
+#else
59
+    return;
60
+#endif /* MBEDTLS_HAVE_PSA_CRYPTO_H && defined(MBEDTLS_PSA_CRYPTO_C) */
61
+}
62
+
63
+/*
64
+ * In older versions of mbedtls, mbedtls_ctr_drbg_update() did not return an
65
+ * error code, and it was deprecated in favor of mbedtls_ctr_drbg_update_ret()
66
+ * which does.
67
+ *
68
+ * In mbedtls 3, this function was removed and mbedtls_ctr_drbg_update() returns
69
+ * an error code.
70
+ */
71
+static inline int
72
+mbedtls_compat_ctr_drbg_update(mbedtls_ctr_drbg_context *ctx,
73
+                               const unsigned char *additional,
74
+                               size_t add_len)
75
+{
76
+#if HAVE_CTR_DRBG_UPDATE_RET
77
+    return mbedtls_ctr_drbg_update_ret(ctx, additional, add_len);
78
+#elif MBEDTLS_VERSION_NUMBER < 0x03020100
79
+    mbedtls_ctr_drbg_update(ctx, additional, add_len);
80
+    return 0;
81
+#else
82
+    return mbedtls_ctr_drbg_update(ctx, additional, add_len);
83
+#endif /* HAVE_CTR_DRBG_UPDATE_RET */
84
+}
85
+
86
+static inline int
87
+mbedtls_compat_pk_check_pair(const mbedtls_pk_context *pub, const mbedtls_pk_context *prv,
88
+                             int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
89
+{
90
+#if MBEDTLS_VERSION_NUMBER < 0x03020100
91
+    return mbedtls_pk_check_pair(pub, prv);
92
+#else
93
+    return mbedtls_pk_check_pair(pub, prv, f_rng, p_rng);
94
+#endif /* MBEDTLS_VERSION_NUMBER < 0x03020100 */
95
+}
96
+
97
+static inline int
98
+mbedtls_compat_pk_parse_key(mbedtls_pk_context *ctx,
99
+                            const unsigned char *key, size_t keylen,
100
+                            const unsigned char *pwd, size_t pwdlen,
101
+                            int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
102
+{
103
+#if MBEDTLS_VERSION_NUMBER < 0x03020100
104
+    return mbedtls_pk_parse_key(ctx, key, keylen, pwd, pwdlen);
105
+#else
106
+    return mbedtls_pk_parse_key(ctx, key, keylen, pwd, pwdlen, f_rng, p_rng);
107
+#endif
108
+}
109
+
110
+static inline int
111
+mbedtls_compat_pk_parse_keyfile(mbedtls_pk_context *ctx,
112
+                                const char *path, const char *password,
113
+                                int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
114
+{
115
+#if MBEDTLS_VERSION_NUMBER < 0x03020100
116
+    return mbedtls_pk_parse_keyfile(ctx, path, password);
117
+#else
118
+    return mbedtls_pk_parse_keyfile(ctx, path, password, f_rng, p_rng);
119
+#endif
120
+}
121
+
122
+#if MBEDTLS_VERSION_NUMBER < 0x03020100
123
+static inline size_t
124
+mbedtls_cipher_info_get_block_size(const mbedtls_cipher_info_t *cipher)
125
+{
126
+    return (size_t)cipher->block_size;
127
+}
128
+
129
+static inline size_t
130
+mbedtls_cipher_info_get_iv_size(const mbedtls_cipher_info_t *cipher)
131
+{
132
+    return (size_t)cipher->iv_size;
133
+}
134
+
135
+static inline size_t
136
+mbedtls_cipher_info_get_key_bitlen(const mbedtls_cipher_info_t *cipher)
137
+{
138
+    return (size_t)cipher->key_bitlen;
139
+}
140
+
141
+static inline mbedtls_cipher_mode_t
142
+mbedtls_cipher_info_get_mode(const mbedtls_cipher_info_t *cipher)
143
+{
144
+    return cipher->mode;
145
+}
146
+
147
+static inline const char *
148
+mbedtls_cipher_info_get_name(const mbedtls_cipher_info_t *cipher)
149
+{
150
+    return cipher->name;
151
+}
152
+
153
+static inline mbedtls_cipher_type_t
154
+mbedtls_cipher_info_get_type(const mbedtls_cipher_info_t *cipher)
155
+{
156
+    return cipher->type;
157
+}
158
+
159
+static inline size_t
160
+mbedtls_dhm_get_bitlen(const mbedtls_dhm_context *ctx)
161
+{
162
+    return 8 * ctx->len;
163
+}
164
+
165
+static inline const mbedtls_md_info_t *
166
+mbedtls_md_info_from_ctx(const mbedtls_md_context_t *ctx)
167
+{
168
+    return ctx->md_info;
169
+}
170
+
171
+static inline const unsigned char *
172
+mbedtls_pem_get_buffer(const mbedtls_pem_context *ctx, size_t *buf_size)
173
+{
174
+    *buf_size = ctx->buflen;
175
+    return ctx->buf;
176
+}
177
+
178
+static inline int
179
+mbedtls_x509_crt_has_ext_type(const mbedtls_x509_crt *ctx, int ext_type)
180
+{
181
+    return ctx->ext_types & ext_type;
182
+}
183
+#endif /* MBEDTLS_VERSION_NUMBER < 0x03020100 */
184
+
185
+#endif /* MBEDTLS_COMPAT_H_ */
... ...
@@ -650,8 +650,10 @@ static const char usage_message[] =
650 650
     "--verify-x509-name name: Accept connections only from a host with X509 subject\n"
651 651
     "                  DN name. The remote host must also pass all other tests\n"
652 652
     "                  of verification.\n"
653
+#ifndef ENABLE_CRYPTO_MBEDTLS
653 654
     "--ns-cert-type t: (DEPRECATED) Require that peer certificate was signed with \n"
654 655
     "                  an explicit nsCertType designation t = 'client' | 'server'.\n"
656
+#endif
655 657
     "--x509-track x  : Save peer X509 attribute x in environment for use by\n"
656 658
     "                  plugins and management interface.\n"
657 659
 #ifdef HAVE_EXPORT_KEYING_MATERIAL
... ...
@@ -9087,6 +9089,10 @@ add_option(struct options *options,
9087 9087
     }
9088 9088
     else if (streq(p[0], "ns-cert-type") && p[1] && !p[2])
9089 9089
     {
9090
+#ifdef ENABLE_CRYPTO_MBEDTLS
9091
+        msg(msglevel, "--ns-cert-type is not available with mbedtls.");
9092
+        goto err;
9093
+#else
9090 9094
         VERIFY_PERMISSION(OPT_P_GENERAL);
9091 9095
         if (streq(p[1], "server"))
9092 9096
         {
... ...
@@ -9101,6 +9107,7 @@ add_option(struct options *options,
9101 9101
             msg(msglevel, "--ns-cert-type must be 'client' or 'server'");
9102 9102
             goto err;
9103 9103
         }
9104
+#endif /* ENABLE_CRYPTO_MBEDTLS */
9104 9105
     }
9105 9106
     else if (streq(p[0], "remote-cert-ku"))
9106 9107
     {
... ...
@@ -41,6 +41,7 @@
41 41
 #include "buffer.h"
42 42
 #include "misc.h"
43 43
 #include "manage.h"
44
+#include "mbedtls_compat.h"
44 45
 #include "pkcs11_backend.h"
45 46
 #include "ssl_common.h"
46 47
 
... ...
@@ -58,25 +59,6 @@
58 58
 #include <mbedtls/oid.h>
59 59
 #include <mbedtls/pem.h>
60 60
 
61
-/**
62
- * Compatibility: mbedtls_ctr_drbg_update was deprecated in mbedtls 2.16 and
63
- * replaced with mbedtls_ctr_drbg_update_ret, which returns an error code.
64
- * For older versions, we call mbedtls_ctr_drbg_update and return 0 (success).
65
- *
66
- * Note: this change was backported to other mbedTLS branches, therefore we
67
- * rely on function detection at configure time.
68
- */
69
-#ifndef HAVE_CTR_DRBG_UPDATE_RET
70
-static int
71
-mbedtls_ctr_drbg_update_ret(mbedtls_ctr_drbg_context *ctx,
72
-                            const unsigned char *additional,
73
-                            size_t add_len)
74
-{
75
-    mbedtls_ctr_drbg_update(ctx, additional, add_len);
76
-    return 0;
77
-}
78
-#endif
79
-
80 61
 static const mbedtls_x509_crt_profile openvpn_x509_crt_profile_legacy =
81 62
 {
82 63
     /* Hashes from SHA-1 and above */
... ...
@@ -108,6 +90,7 @@ static const mbedtls_x509_crt_profile openvpn_x509_crt_profile_preferred =
108 108
 void
109 109
 tls_init_lib(void)
110 110
 {
111
+    mbedtls_compat_psa_crypto_init();
111 112
 }
112 113
 
113 114
 void
... ...
@@ -430,7 +413,7 @@ tls_ctx_load_dh_params(struct tls_root_ctx *ctx, const char *dh_file,
430 430
     }
431 431
 
432 432
     msg(D_TLS_DEBUG_LOW, "Diffie-Hellman initialized with " counter_format " bit key",
433
-        (counter_type) 8 * mbedtls_mpi_size(&ctx->dhm_ctx->P));
433
+        (counter_type) mbedtls_dhm_get_bitlen(ctx->dhm_ctx));
434 434
 }
435 435
 
436 436
 void
... ...
@@ -504,29 +487,40 @@ tls_ctx_load_priv_file(struct tls_root_ctx *ctx, const char *priv_key_file,
504 504
 
505 505
     if (priv_key_inline)
506 506
     {
507
-        status = mbedtls_pk_parse_key(ctx->priv_key,
508
-                                      (const unsigned char *) priv_key_file,
509
-                                      strlen(priv_key_file) + 1, NULL, 0);
507
+        status = mbedtls_compat_pk_parse_key(ctx->priv_key,
508
+                                             (const unsigned char *) priv_key_file,
509
+                                             strlen(priv_key_file) + 1, NULL, 0,
510
+                                             mbedtls_ctr_drbg_random,
511
+                                             rand_ctx_get());
510 512
 
511 513
         if (MBEDTLS_ERR_PK_PASSWORD_REQUIRED == status)
512 514
         {
513 515
             char passbuf[512] = {0};
514 516
             pem_password_callback(passbuf, 512, 0, NULL);
515
-            status = mbedtls_pk_parse_key(ctx->priv_key,
516
-                                          (const unsigned char *) priv_key_file,
517
-                                          strlen(priv_key_file) + 1,
518
-                                          (unsigned char *) passbuf,
519
-                                          strlen(passbuf));
517
+            status = mbedtls_compat_pk_parse_key(ctx->priv_key,
518
+                                                 (const unsigned char *) priv_key_file,
519
+                                                 strlen(priv_key_file) + 1,
520
+                                                 (unsigned char *) passbuf,
521
+                                                 strlen(passbuf),
522
+                                                 mbedtls_ctr_drbg_random,
523
+                                                 rand_ctx_get());
520 524
         }
521 525
     }
522 526
     else
523 527
     {
524
-        status = mbedtls_pk_parse_keyfile(ctx->priv_key, priv_key_file, NULL);
528
+        status = mbedtls_compat_pk_parse_keyfile(ctx->priv_key,
529
+                                                 priv_key_file,
530
+                                                 NULL,
531
+                                                 mbedtls_ctr_drbg_random,
532
+                                                 rand_ctx_get());
525 533
         if (MBEDTLS_ERR_PK_PASSWORD_REQUIRED == status)
526 534
         {
527 535
             char passbuf[512] = {0};
528 536
             pem_password_callback(passbuf, 512, 0, NULL);
529
-            status = mbedtls_pk_parse_keyfile(ctx->priv_key, priv_key_file, passbuf);
537
+            status = mbedtls_compat_pk_parse_keyfile(ctx->priv_key,
538
+                                                     priv_key_file, passbuf,
539
+                                                     mbedtls_ctr_drbg_random,
540
+                                                     rand_ctx_get());
530 541
         }
531 542
     }
532 543
     if (!mbed_ok(status))
... ...
@@ -542,7 +536,10 @@ tls_ctx_load_priv_file(struct tls_root_ctx *ctx, const char *priv_key_file,
542 542
         return 1;
543 543
     }
544 544
 
545
-    if (!mbed_ok(mbedtls_pk_check_pair(&ctx->crt_chain->pk, ctx->priv_key)))
545
+    if (!mbed_ok(mbedtls_compat_pk_check_pair(&ctx->crt_chain->pk,
546
+                                              ctx->priv_key,
547
+                                              mbedtls_ctr_drbg_random,
548
+                                              rand_ctx_get())))
546 549
     {
547 550
         msg(M_WARN, "Private key does not match the certificate");
548 551
         return 1;
... ...
@@ -558,7 +555,6 @@ tls_ctx_load_priv_file(struct tls_root_ctx *ctx, const char *priv_key_file,
558 558
  * @param ctx_voidptr   Management external key context.
559 559
  * @param f_rng         (Unused)
560 560
  * @param p_rng         (Unused)
561
- * @param mode          RSA mode (should be RSA_PRIVATE).
562 561
  * @param md_alg        Message digest ('hash') algorithm type.
563 562
  * @param hashlen       Length of hash (overridden by length specified by md_alg
564 563
  *                      if md_alg != MBEDTLS_MD_NONE).
... ...
@@ -572,7 +568,10 @@ tls_ctx_load_priv_file(struct tls_root_ctx *ctx, const char *priv_key_file,
572 572
  */
573 573
 static inline int
574 574
 external_pkcs1_sign( void *ctx_voidptr,
575
-                     int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode,
575
+                     int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
576
+#if MBEDTLS_VERSION_NUMBER < 0x03020100
577
+                     int mode,
578
+#endif
576 579
                      mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash,
577 580
                      unsigned char *sig )
578 581
 {
... ...
@@ -587,10 +586,12 @@ external_pkcs1_sign( void *ctx_voidptr,
587 587
         return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
588 588
     }
589 589
 
590
+#if MBEDTLS_VERSION_NUMBER < 0x03020100
590 591
     if (MBEDTLS_RSA_PRIVATE != mode)
591 592
     {
592 593
         return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
593 594
     }
595
+#endif
594 596
 
595 597
     /*
596 598
      * Support a wide range of hashes. TLSv1.1 and before only need SIG_RSA_RAW,
... ...
@@ -967,7 +968,7 @@ tls_ctx_personalise_random(struct tls_root_ctx *ctx)
967 967
 
968 968
         if (0 != memcmp(old_sha256_hash, sha256_hash, sizeof(sha256_hash)))
969 969
         {
970
-            if (!mbed_ok(mbedtls_ctr_drbg_update_ret(cd_ctx, sha256_hash, 32)))
970
+            if (!mbed_ok(mbedtls_compat_ctr_drbg_update(cd_ctx, sha256_hash, 32)))
971 971
             {
972 972
                 msg(M_WARN, "WARNING: failed to personalise random, could not update CTR_DRBG");
973 973
             }
... ...
@@ -979,12 +980,16 @@ tls_ctx_personalise_random(struct tls_root_ctx *ctx)
979 979
 int
980 980
 tls_version_max(void)
981 981
 {
982
-#if defined(MBEDTLS_SSL_MAJOR_VERSION_3) && defined(MBEDTLS_SSL_MINOR_VERSION_3)
982
+#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
983
+    return TLS_VER_1_3;
984
+#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
983 985
     return TLS_VER_1_2;
984
-#elif defined(MBEDTLS_SSL_MAJOR_VERSION_3) && defined(MBEDTLS_SSL_MINOR_VERSION_2)
986
+#elif defined(MBEDTLS_SSL_PROTO_TLS1_1)
985 987
     return TLS_VER_1_1;
986
-#else
988
+#elif defined(MBEDTLS_SSL_PROTO_TLS1)
987 989
     return TLS_VER_1_0;
990
+#else /* if defined(MBEDTLS_SSL_PROTO_TLS1_3) */
991
+    #error "mbedtls is compiled without support for any version of TLS."
988 992
 #endif
989 993
 }
990 994
 
... ...
@@ -1006,23 +1011,36 @@ tls_version_to_major_minor(int tls_ver, int *major, int *minor)
1006 1006
 
1007 1007
     switch (tls_ver)
1008 1008
     {
1009
+#if defined(MBEDTLS_SSL_PROTO_TLS1)
1009 1010
         case TLS_VER_1_0:
1010 1011
             *major = MBEDTLS_SSL_MAJOR_VERSION_3;
1011 1012
             *minor = MBEDTLS_SSL_MINOR_VERSION_1;
1012 1013
             break;
1014
+#endif
1013 1015
 
1016
+#if defined(MBEDTLS_SSL_PROTO_TLS1_1)
1014 1017
         case TLS_VER_1_1:
1015 1018
             *major = MBEDTLS_SSL_MAJOR_VERSION_3;
1016 1019
             *minor = MBEDTLS_SSL_MINOR_VERSION_2;
1017 1020
             break;
1021
+#endif
1018 1022
 
1023
+#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1019 1024
         case TLS_VER_1_2:
1020 1025
             *major = MBEDTLS_SSL_MAJOR_VERSION_3;
1021 1026
             *minor = MBEDTLS_SSL_MINOR_VERSION_3;
1022 1027
             break;
1028
+#endif
1029
+
1030
+#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1031
+        case TLS_VER_1_3:
1032
+            *major = MBEDTLS_SSL_MAJOR_VERSION_3;
1033
+            *minor = MBEDTLS_SSL_MINOR_VERSION_4;
1034
+            break;
1035
+#endif
1023 1036
 
1024 1037
         default:
1025
-            msg(M_FATAL, "%s: invalid TLS version %d", __func__, tls_ver);
1038
+            msg(M_FATAL, "%s: invalid or unsupported TLS version %d", __func__, tls_ver);
1026 1039
             break;
1027 1040
     }
1028 1041
 }
... ...
@@ -1149,17 +1167,17 @@ key_state_ssl_init(struct key_state_ssl *ks_ssl,
1149 1149
 
1150 1150
     /* Initialize minimum TLS version */
1151 1151
     {
1152
-        const int tls_version_min =
1152
+        const int configured_tls_version_min =
1153 1153
             (session->opt->ssl_flags >> SSLF_TLS_VERSION_MIN_SHIFT)
1154 1154
             &SSLF_TLS_VERSION_MIN_MASK;
1155 1155
 
1156
-        /* default to TLS 1.0 */
1156
+        /* default to TLS 1.2 */
1157 1157
         int major = MBEDTLS_SSL_MAJOR_VERSION_3;
1158
-        int minor = MBEDTLS_SSL_MINOR_VERSION_1;
1158
+        int minor = MBEDTLS_SSL_MINOR_VERSION_3;
1159 1159
 
1160
-        if (tls_version_min > TLS_VER_UNSPEC)
1160
+        if (configured_tls_version_min > TLS_VER_UNSPEC)
1161 1161
         {
1162
-            tls_version_to_major_minor(tls_version_min, &major, &minor);
1162
+            tls_version_to_major_minor(configured_tls_version_min, &major, &minor);
1163 1163
         }
1164 1164
 
1165 1165
         mbedtls_ssl_conf_min_version(ks_ssl->ssl_config, major, minor);
... ...
@@ -1167,16 +1185,24 @@ key_state_ssl_init(struct key_state_ssl *ks_ssl,
1167 1167
 
1168 1168
     /* Initialize maximum TLS version */
1169 1169
     {
1170
-        const int tls_version_max =
1170
+        const int configured_tls_version_max =
1171 1171
             (session->opt->ssl_flags >> SSLF_TLS_VERSION_MAX_SHIFT)
1172 1172
             &SSLF_TLS_VERSION_MAX_MASK;
1173 1173
 
1174
-        if (tls_version_max > TLS_VER_UNSPEC)
1174
+        int major = 0;
1175
+        int minor = 0;
1176
+
1177
+        if (configured_tls_version_max > TLS_VER_UNSPEC)
1178
+        {
1179
+            tls_version_to_major_minor(configured_tls_version_max, &major, &minor);
1180
+        }
1181
+        else
1175 1182
         {
1176
-            int major, minor;
1177
-            tls_version_to_major_minor(tls_version_max, &major, &minor);
1178
-            mbedtls_ssl_conf_max_version(ks_ssl->ssl_config, major, minor);
1183
+            /* Default to tls_version_max(). */
1184
+            tls_version_to_major_minor(tls_version_max(), &major, &minor);
1179 1185
         }
1186
+
1187
+        mbedtls_ssl_conf_max_version(ks_ssl->ssl_config, major, minor);
1180 1188
     }
1181 1189
 
1182 1190
 #ifdef HAVE_EXPORT_KEYING_MATERIAL
... ...
@@ -1188,7 +1214,7 @@ key_state_ssl_init(struct key_state_ssl *ks_ssl,
1188 1188
     /* Initialise SSL context */
1189 1189
     ALLOC_OBJ_CLEAR(ks_ssl->ctx, mbedtls_ssl_context);
1190 1190
     mbedtls_ssl_init(ks_ssl->ctx);
1191
-    mbedtls_ssl_setup(ks_ssl->ctx, ks_ssl->ssl_config);
1191
+    mbed_ok(mbedtls_ssl_setup(ks_ssl->ctx, ks_ssl->ssl_config));
1192 1192
 
1193 1193
     /* Initialise BIOs */
1194 1194
     ALLOC_OBJ_CLEAR(ks_ssl->bio_ctx, bio_ctx);
... ...
@@ -35,6 +35,7 @@
35 35
 #if defined(ENABLE_CRYPTO_MBEDTLS)
36 36
 
37 37
 #include "crypto_mbedtls.h"
38
+#include "mbedtls_compat.h"
38 39
 #include "ssl_verify.h"
39 40
 #include <mbedtls/asn1.h>
40 41
 #include <mbedtls/error.h>
... ...
@@ -467,6 +468,8 @@ x509_setenv(struct env_set *es, int cert_depth, mbedtls_x509_crt *cert)
467 467
     }
468 468
 }
469 469
 
470
+/* Dummy function because Netscape certificate types are not supported in OpenVPN with mbedtls.
471
+ * Returns SUCCESS if usage is NS_CERT_CHECK_NONE, FAILURE otherwise. */
470 472
 result_t
471 473
 x509_verify_ns_cert_type(mbedtls_x509_crt *cert, const int usage)
472 474
 {
... ...
@@ -474,18 +477,6 @@ x509_verify_ns_cert_type(mbedtls_x509_crt *cert, const int usage)
474 474
     {
475 475
         return SUCCESS;
476 476
     }
477
-    if (usage == NS_CERT_CHECK_CLIENT)
478
-    {
479
-        return ((cert->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE)
480
-                && (cert->ns_cert_type & MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT)) ?
481
-               SUCCESS : FAILURE;
482
-    }
483
-    if (usage == NS_CERT_CHECK_SERVER)
484
-    {
485
-        return ((cert->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE)
486
-                && (cert->ns_cert_type & MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER)) ?
487
-               SUCCESS : FAILURE;
488
-    }
489 477
 
490 478
     return FAILURE;
491 479
 }
... ...
@@ -496,7 +487,7 @@ x509_verify_cert_ku(mbedtls_x509_crt *cert, const unsigned *const expected_ku,
496 496
 {
497 497
     msg(D_HANDSHAKE, "Validating certificate key usage");
498 498
 
499
-    if (!(cert->ext_types & MBEDTLS_X509_EXT_KEY_USAGE))
499
+    if (!mbedtls_x509_crt_has_ext_type(cert, MBEDTLS_X509_EXT_KEY_USAGE))
500 500
     {
501 501
         msg(D_TLS_ERRORS,
502 502
             "ERROR: Certificate does not have key usage extension");
... ...
@@ -521,9 +512,7 @@ x509_verify_cert_ku(mbedtls_x509_crt *cert, const unsigned *const expected_ku,
521 521
 
522 522
     if (fFound != SUCCESS)
523 523
     {
524
-        msg(D_TLS_ERRORS,
525
-            "ERROR: Certificate has key usage %04x, expected one of:",
526
-            cert->key_usage);
524
+        msg(D_TLS_ERRORS, "ERROR: Certificate has invalid key usage, expected one of:");
527 525
         for (size_t i = 0; i < expected_len && expected_ku[i]; i++)
528 526
         {
529 527
             msg(D_TLS_ERRORS, " * %04x", expected_ku[i]);
... ...
@@ -538,7 +527,7 @@ x509_verify_cert_eku(mbedtls_x509_crt *cert, const char *const expected_oid)
538 538
 {
539 539
     result_t fFound = FAILURE;
540 540
 
541
-    if (!(cert->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE))
541
+    if (!mbedtls_x509_crt_has_ext_type(cert, MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE))
542 542
     {
543 543
         msg(D_HANDSHAKE, "Certificate does not have extended key usage extension");
544 544
     }