hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
diff --git a/configs/config-thread.h b/configs/config-thread.h
index 25db16b..c2d8c88 100644
--- a/configs/config-thread.h
+++ b/configs/config-thread.h
@@ -21,6 +21,13 @@
  *
  *  This file is part of mbed TLS (https://tls.mbed.org)
  */
+/*
+ * Portions of this file are copyright (c) 2019 - 2020
+ * Amazon.com, Inc. or its affiliates.  All rights reserved.
+ *
+ * PORTIONS OF THIS FILE ARE AMAZON PROPRIETARY/CONFIDENTIAL.  USE IS SUBJECT TO
+ * LICENSE TERMS.
+ */
 
 /*
  * Minimal configuration for using TLS a part of Thread
@@ -51,6 +58,7 @@
 #define MBEDTLS_SSL_DTLS_ANTI_REPLAY
 #define MBEDTLS_SSL_DTLS_HELLO_VERIFY
 #define MBEDTLS_SSL_EXPORT_KEYS
+#define MBEDTLS_EAP_TLS_EXPORT_KEYS
 
 /* mbed TLS modules */
 #define MBEDTLS_AES_C
diff --git a/include/mbedtls/config.h b/include/mbedtls/config.h
index 834cced..d2e3878 100644
--- a/include/mbedtls/config.h
+++ b/include/mbedtls/config.h
@@ -1620,6 +1620,16 @@
 #define MBEDTLS_SSL_EXPORT_KEYS
 
 /**
+ * \def MBEDTLS_EAP_TLS_EXPORT_KEYS
+ *
+ * Enable support for exporting EAP_TLS keys as defined in rfc5216.
+ * This is required for 802.1X support.
+ *
+ * Comment this macro to disable support for EAP_TLS key export
+ */
+#define MBEDTLS_EAP_TLS_EXPORT_KEYS
+
+/**
  * \def MBEDTLS_SSL_SERVER_NAME_INDICATION
  *
  * Enable support for RFC 6066 server name indication (SNI) in SSL.
diff --git a/include/mbedtls/ssl.h b/include/mbedtls/ssl.h
index 1adf960..622a8b4 100644
--- a/include/mbedtls/ssl.h
+++ b/include/mbedtls/ssl.h
@@ -21,6 +21,13 @@
  *
  *  This file is part of mbed TLS (https://tls.mbed.org)
  */
+/*
+ * Portions of this file are copyright (c) 2019 - 2020
+ * Amazon.com, Inc. or its affiliates.  All rights reserved.
+ *
+ * PORTIONS OF THIS FILE ARE AMAZON PROPRIETARY/CONFIDENTIAL.  USE IS SUBJECT TO
+ * LICENSE TERMS.
+ */
 #ifndef MBEDTLS_SSL_H
 #define MBEDTLS_SSL_H
 
@@ -419,6 +426,18 @@ union mbedtls_ssl_premaster_secret
 extern "C" {
 #endif
 
+#if defined(MBEDTLS_EAP_TLS_EXPORT_KEYS)
+/*
+ * Exported key type
+ */
+typedef enum
+{
+    TLS_KEY,    /* rfc5246 - default*/
+    EAP_TLS_KEY /* rfc5216 */
+}
+mbedtls_tls_key_t;
+#endif
+
 /*
  * SSL state machine
  */
@@ -890,6 +909,10 @@ struct mbedtls_ssl_config
     int (*f_export_keys)( void *, const unsigned char *,
             const unsigned char *, size_t, size_t, size_t );
     void *p_export_keys;            /*!< context for key export callback    */
+#if defined(MBEDTLS_EAP_TLS_EXPORT_KEYS)
+    mbedtls_tls_key_t export_key_type;
+    unsigned char eap_tls_keyblk[128];
+#endif
 #endif
 
 #if defined(MBEDTLS_X509_CRT_PARSE_C)
@@ -1589,6 +1589,19 @@ void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
 #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_SRV_C */
 
 #if defined(MBEDTLS_SSL_EXPORT_KEYS)
+#if defined(MBEDTLS_EAP_TLS_EXPORT_KEYS)
+/**
+ * \brief           Set type of the TLS key to export.
+ *                  (Default: TLS_KEY as defined in rfc4346
+ *
+ * \note            See \c mbedtls_tls_key_t.
+ *
+ * \param conf      SSL configuration context
+ * \param export_key_type   Type of the key to be exported
+ */
+void mbedtls_ssl_conf_export_keys_type( mbedtls_ssl_config *conf,
+        mbedtls_tls_key_t export_key_type);
+#endif /* MBEDTLS_EAP_TLS_EXPORT_KEYS */
 /**
  * \brief           Configure key export callback.
  *                  (Default: none.)
diff --git a/include/mbedtls/ssl_internal.h b/include/mbedtls/ssl_internal.h
index bd5ad94..2db53a4 100644
--- a/include/mbedtls/ssl_internal.h
+++ b/include/mbedtls/ssl_internal.h
@@ -21,6 +21,13 @@
  *
  *  This file is part of mbed TLS (https://tls.mbed.org)
  */
+/*
+ * Portions of this file are copyright (c) 2019 - 2020
+ * Amazon.com, Inc. or its affiliates.  All rights reserved.
+ *
+ * PORTIONS OF THIS FILE ARE AMAZON PROPRIETARY/CONFIDENTIAL.  USE IS SUBJECT TO
+ * LICENSE TERMS.
+ */
 #ifndef MBEDTLS_SSL_INTERNAL_H
 #define MBEDTLS_SSL_INTERNAL_H
 
@@ -126,6 +133,15 @@
 #define MBEDTLS_SSL_RETRANS_FINISHED        3
 
 /*
+ * Move the definition from mbedtls/library/ssl_tls.c
+ *
+ * Note: Used by mbedtls_ssl_write_record function in
+ * mbedtls/library/ssl_tls.c
+ */
+#define SSL_DONT_FORCE_FLUSH 0
+#define SSL_FORCE_FLUSH      1
+
+/*
  * Allow extra bytes for record, authentication and encryption overhead:
  * counter (8) + header (5) + IV(16) + MAC (16-48) + padding (0-256)
  * and allow for a maximum of 1024 of compression expansion if
diff --git a/library/ssl_tls.c b/library/ssl_tls.c
index b8f35fe..48185fb 100644
--- a/library/ssl_tls.c
+++ b/library/ssl_tls.c
@@ -19,6 +19,13 @@
  *  This file is part of mbed TLS (https://tls.mbed.org)
  */
 /*
+ * Portions of this file are copyright (c) 2019 - 2020
+ * Amazon.com, Inc. or its affiliates.  All rights reserved.
+ *
+ * PORTIONS OF THIS FILE ARE AMAZON PROPRIETARY/CONFIDENTIAL.  USE IS SUBJECT TO
+ * LICENSE TERMS.
+ */
+/*
  *  The SSL 3.0 specification was drafted by Netscape in 1996,
  *  and became an IETF standard in 1999.
  *
@@ -104,9 +111,6 @@ static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
 static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
                                     mbedtls_ssl_transform *transform );
 
-#define SSL_DONT_FORCE_FLUSH 0
-#define SSL_FORCE_FLUSH      1
-
 #if defined(MBEDTLS_SSL_PROTO_DTLS)
 
 /* Forward declarations for functions related to message buffering. */
@@ -498,7 +502,16 @@ static int tls_prf_generic( mbedtls_md_type_t md_type,
 {
     size_t nb;
     size_t i, j, k, md_len;
+#if defined(MBEDTLS_EAP_TLS_EXPORT_KEYS)
+    /*
+     * EAP-TLS is using label: "client EAP encryption" (RFC 5216)
+     * while TLS is using "master secret" (RFC4346 and RFC5246)
+     * If EAP-TLS is supported, increase buffer by 8 bytes
+     */
+    unsigned char tmp[128+8];
+#else
     unsigned char tmp[128];
+#endif
     unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
     const mbedtls_md_info_t *md_info;
     mbedtls_md_context_t md_ctx;
@@ -612,6 +625,9 @@ int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
     int ret = 0;
     unsigned char tmp[64];
     unsigned char keyblk[256];
+#if defined(MBEDTLS_EAP_TLS_EXPORT_KEYS)
+    unsigned char eap_tls_keyblk[128];
+#endif
     unsigned char *key1;
     unsigned char *key2;
     unsigned char *mac_enc;
@@ -762,6 +778,17 @@ int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
     }
     else
         MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
+#if defined(MBEDTLS_EAP_TLS_EXPORT_KEYS)
+    if ( ssl->conf->export_key_type == EAP_TLS_KEY ) {
+        ret = handshake->tls_prf( session->master, 48, "client EAP encryption",
+                              handshake->randbytes, 64, eap_tls_keyblk, 128 );
+        if( ret != 0 )
+        {
+            MBEDTLS_SSL_DEBUG_RET( 1, "eap_tls_prf", ret );
+            return( ret );
+        }
+    }
+#endif
 
     /*
      * Swap the client and server random values.
@@ -1023,6 +1050,13 @@ int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
 #if defined(MBEDTLS_SSL_EXPORT_KEYS)
     if( ssl->conf->f_export_keys != NULL )
     {
+#if defined(MBEDTLS_EAP_TLS_EXPORT_KEYS)
+        if ( ssl->conf->export_key_type == EAP_TLS_KEY )
+            ssl->conf->f_export_keys( ssl->conf->p_export_keys,
+                                  session->master, eap_tls_keyblk,
+                                  0, 128, 0 );
+        else
+#endif /* MBEDTLS_EAP_TLS_EXPORT_KEYS */
         ssl->conf->f_export_keys( ssl->conf->p_export_keys,
                                   session->master, keyblk,
                                   mac_key_len, transform->keylen,
@@ -1079,6 +1113,10 @@ int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
     }
 #endif /* MBEDTLS_CIPHER_MODE_CBC */
 
+#if defined(MBEDTLS_EAP_TLS_EXPORT_KEYS)
+    mbedtls_platform_zeroize( eap_tls_keyblk, sizeof(eap_tls_keyblk) );
+#endif
+
     mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
 
 #if defined(MBEDTLS_ZLIB_SUPPORT)
@@ -7719,6 +7757,13 @@ void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
     conf->f_export_keys = f_export_keys;
     conf->p_export_keys = p_export_keys;
 }
+#if defined(MBEDTLS_EAP_TLS_EXPORT_KEYS)
+void mbedtls_ssl_conf_export_keys_type( mbedtls_ssl_config *conf,
+        mbedtls_tls_key_t export_key_type )
+{
+    conf->export_key_type=export_key_type;
+}
+#endif /* MBEDTLS_EAP_TLS_EXPORT_KEYS */
 #endif
 
 #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)