1 /*
2  * SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "utils/includes.h"
8 #include "utils/common.h"
9 
10 #include "crypto/tls.h"
11 #include "crypto/sha1.h"
12 #include "crypto/md5.h"
13 #include "crypto/sha256.h"
14 #include "crypto/sha384.h"
15 
16 /* TODO: Remove this once the appropriate solution is found
17  *
18  * ssl_misc.h header uses private elements from
19  * mbedtls, which become undefined if the following flag
20  * is not defined
21  */
22 #define MBEDTLS_ALLOW_PRIVATE_ACCESS
23 
24 // located at mbedtls/library/ssl_misc.h
25 #include "ssl_misc.h"
26 
27 #include "mbedtls/ctr_drbg.h"
28 #include "mbedtls/entropy.h"
29 #include "mbedtls/debug.h"
30 #include "mbedtls/oid.h"
31 #ifdef ESPRESSIF_USE
32 #include "mbedtls/esp_debug.h"
33 #include "mbedtls/esp_config.h"
34 #else
35 #if defined(CONFIG_MBEDTLS)
36 #if !defined(CONFIG_MBEDTLS_CFG_FILE)
37 #include "mbedtls/config.h"
38 #else
39 #include CONFIG_MBEDTLS_CFG_FILE
40 #endif /* CONFIG_MBEDTLS_CFG_FILE */
41 #endif
42 #endif
43 #include "mbedtls/platform.h"
44 #include "eap_peer/eap.h"
45 
46 
47 #define TLS_RANDOM_LEN 32
48 #define TLS_MASTER_SECRET_LEN 48
49 #define MAX_CIPHERSUITE 32
50 
51 
52 
53 /* Throw a compilation error if basic requirements in mbedtls are not enabled */
54 #if !defined(MBEDTLS_SSL_TLS_C)
55 #error "TLS not enabled in mbedtls config"
56 #endif
57 
58 #if !defined(MBEDTLS_SHA256_C)
59 #error "SHA256 is disabled in mbedtls config"
60 #endif
61 
62 #if !defined(MBEDTLS_AES_C)
63 #error "AES support is disabled in mbedtls config"
64 #endif
65 
66 uint32_t tls_instance_count;
67 struct tls_data {
68 	/* Data for mbedlts */
69 	struct wpabuf *in_data;
70 	/* Data from mbedtls */
71 	struct wpabuf *out_data;
72 };
73 
74 typedef struct tls_context {
75 	mbedtls_ssl_context ssl;            /*!< TLS/SSL context */
76 	mbedtls_entropy_context entropy;    /*!< mbedTLS entropy context structure */
77 	mbedtls_ctr_drbg_context ctr_drbg;  /*!< mbedTLS ctr drbg context structure */
78 	mbedtls_ssl_config conf;            /*!< TLS/SSL config to be shared structures */
79 	mbedtls_x509_crt cacert;            /*!< Container for X.509 CA certificate */
80 	mbedtls_x509_crt *cacert_ptr;       /*!< Pointer to the cacert being used. */
81 	mbedtls_x509_crt clientcert;        /*!< Container for X.509 client certificate */
82 	mbedtls_pk_context clientkey;       /*!< Private key of client certificate */
83 	int ciphersuite[MAX_CIPHERSUITE];
84 } tls_context_t;
85 
86 struct tls_connection {
87 	tls_context_t *tls;
88 	struct tls_data tls_io_data;
89 	unsigned char master_secret[TLS_MASTER_SECRET_LEN];
90 	unsigned char randbytes[2 * TLS_RANDOM_LEN];
91         mbedtls_tls_prf_types tls_prf_type;
92 	mbedtls_md_type_t mac;
93 };
94 
tls_mbedtls_cleanup(tls_context_t * tls)95 static void tls_mbedtls_cleanup(tls_context_t *tls)
96 {
97 	if (!tls) {
98 		return;
99 	}
100 	tls->cacert_ptr = NULL;
101 	mbedtls_x509_crt_free(&tls->cacert);
102 	mbedtls_x509_crt_free(&tls->clientcert);
103 	mbedtls_pk_free(&tls->clientkey);
104 	mbedtls_entropy_free(&tls->entropy);
105 	mbedtls_ssl_config_free(&tls->conf);
106 	mbedtls_ctr_drbg_free(&tls->ctr_drbg);
107 	mbedtls_ssl_free(&tls->ssl);
108 }
109 
tls_mbedtls_conn_delete(tls_context_t * tls)110 static void tls_mbedtls_conn_delete(tls_context_t *tls)
111 {
112 	if (tls != NULL) {
113 		tls_mbedtls_cleanup(tls);
114 	}
115 }
116 
tls_mbedtls_write(void * ctx,const unsigned char * buf,size_t len)117 static int tls_mbedtls_write(void *ctx, const unsigned char *buf, size_t len)
118 {
119 	struct tls_connection *conn = (struct tls_connection *)ctx;
120 	struct tls_data *data = &conn->tls_io_data;
121 
122 	if (wpabuf_resize(&data->out_data, len) < 0)
123 		return 0;
124 
125 	wpabuf_put_data(data->out_data, buf, len);
126 
127 	return len;
128 }
129 
tls_mbedtls_read(void * ctx,unsigned char * buf,size_t len)130 static int tls_mbedtls_read(void *ctx, unsigned char *buf, size_t len)
131 {
132 	struct tls_connection *conn = (struct tls_connection *)ctx;
133 	struct tls_data *data = &conn->tls_io_data;
134 	struct wpabuf *local_buf;
135 
136 	if (data->in_data == NULL || len > wpabuf_len(data->in_data)) {
137 		/* We don't have suffient buffer available for read */
138 		wpa_printf(MSG_INFO, "len=%zu not available in input", len);
139 		return MBEDTLS_ERR_SSL_WANT_READ;
140 	}
141 
142 	os_memcpy(buf, wpabuf_head(data->in_data), len);
143 	/* adjust buffer */
144 	if (len < wpabuf_len(data->in_data)) {
145 		/* TODO optimize this operation */
146 		local_buf = wpabuf_alloc_copy(wpabuf_mhead_u8(data->in_data) + len,
147 					      wpabuf_len(data->in_data) - len);
148 		wpabuf_free(data->in_data);
149 		data->in_data = local_buf;
150 	} else {
151 		wpabuf_free(data->in_data);
152 		data->in_data = NULL;
153 	}
154 
155 	return len;
156 }
157 
set_pki_context(tls_context_t * tls,const struct tls_connection_params * cfg)158 static int set_pki_context(tls_context_t *tls, const struct tls_connection_params *cfg)
159 {
160 	int ret;
161 
162 	if (cfg->client_cert_blob == NULL || cfg->private_key_blob  == NULL) {
163 		wpa_printf(MSG_ERROR, "%s: config not correct", __func__);
164 		return -1;
165 	}
166 
167 	mbedtls_x509_crt_init(&tls->clientcert);
168 	mbedtls_pk_init(&tls->clientkey);
169 
170 	ret = mbedtls_x509_crt_parse(&tls->clientcert,
171 				     cfg->client_cert_blob, cfg->client_cert_blob_len);
172 	if (ret < 0) {
173 		wpa_printf(MSG_ERROR, "mbedtls_x509_crt_parse returned -0x%x", -ret);
174 		return ret;
175 	}
176 
177 	ret = mbedtls_pk_parse_key(&tls->clientkey, cfg->private_key_blob, cfg->private_key_blob_len,
178 				   (const unsigned char *)cfg->private_key_passwd,
179 				   cfg->private_key_passwd ? os_strlen(cfg->private_key_passwd) : 0, mbedtls_ctr_drbg_random, &tls->ctr_drbg);
180 	if (ret < 0) {
181 		wpa_printf(MSG_ERROR, "mbedtls_pk_parse_keyfile returned -0x%x", -ret);
182 		return ret;
183 	}
184 
185 	ret = mbedtls_ssl_conf_own_cert(&tls->conf, &tls->clientcert, &tls->clientkey);
186 	if (ret < 0) {
187 		wpa_printf(MSG_ERROR, "mbedtls_ssl_conf_own_cert returned -0x%x", -ret);
188 		return ret;
189 	}
190 
191 	return 0;
192 }
193 
set_ca_cert(tls_context_t * tls,const unsigned char * cacert,size_t cacert_len)194 static int set_ca_cert(tls_context_t *tls, const unsigned char *cacert, size_t cacert_len)
195 {
196 	tls->cacert_ptr = &tls->cacert;
197 	mbedtls_x509_crt_init(tls->cacert_ptr);
198 	int ret = mbedtls_x509_crt_parse(tls->cacert_ptr, cacert, cacert_len);
199 	if (ret < 0) {
200 		wpa_printf(MSG_ERROR, "mbedtls_x509_crt_parse returned -0x%x", -ret);
201 		return ret;
202 	}
203 	mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
204 	mbedtls_ssl_conf_ca_chain(&tls->conf, tls->cacert_ptr, NULL);
205 	return 0;
206 }
207 
208 #ifdef CONFIG_SUITEB192
209 static uint16_t tls_sig_algs_for_suiteb[] = {
210 #if defined(MBEDTLS_SHA512_C)
211 #if defined(MBEDTLS_ECDSA_C)
212     MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
213     MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
214 #endif
215 #if defined(MBEDTLS_RSA_C)
216     MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
217     MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
218 #endif
219 #endif /* MBEDTLS_SHA512_C */
220     MBEDTLS_TLS_SIG_NONE
221 };
222 
223 const mbedtls_x509_crt_profile suiteb_mbedtls_x509_crt_profile =
224 {
225 #if defined(MBEDTLS_SHA512_C)
226 	MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
227 	MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ) |
228 #endif
229 	0,
230 	0xFFFFFFF, /* Any PK alg    */
231 	MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
232 	1024,
233 };
234 
tls_set_suiteb_config(tls_context_t * tls)235 static void tls_set_suiteb_config(tls_context_t *tls)
236 {
237 	const mbedtls_x509_crt_profile *crt_profile = &suiteb_mbedtls_x509_crt_profile;
238 	mbedtls_ssl_conf_cert_profile(&tls->conf, crt_profile);
239 	mbedtls_ssl_conf_sig_algs(&tls->conf, tls_sig_algs_for_suiteb);
240 }
241 #endif
242 
243 static uint16_t tls_sig_algs_for_eap[] = {
244 #if defined(MBEDTLS_SHA512_C)
245 #if defined(MBEDTLS_ECDSA_C)
246     MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
247     MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
248 #endif
249 #if defined(MBEDTLS_RSA_C)
250     MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
251     MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
252 #endif
253 #endif /* MBEDTLS_SHA512_C */
254 #if defined(MBEDTLS_SHA256_C)
255 #if defined(MBEDTLS_ECDSA_C)
256     MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
257     MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA224 ),
258 #endif
259 #if defined(MBEDTLS_RSA_C)
260     MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
261     MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA224 ),
262 #endif
263 #endif /* MBEDTLS_SHA256_C */
264 #if defined(MBEDTLS_SHA1_C)
265 #if defined(MBEDTLS_ECDSA_C)
266     MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA1 ),
267 #endif
268 #if defined(MBEDTLS_RSA_C)
269     MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA1 ),
270 #endif
271 #endif /* MBEDTLS_SHA1_C */
272     MBEDTLS_TLS_SIG_NONE
273 };
274 
275 const mbedtls_x509_crt_profile eap_mbedtls_x509_crt_profile =
276 {
277 #if defined(MBEDTLS_SHA1_C)
278 	MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
279 #endif
280 #if defined(MBEDTLS_SHA256_C)
281 	MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
282 	MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
283 #endif
284 #if defined(MBEDTLS_SHA512_C)
285 	MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
286 	MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ) |
287 #endif
288 	0,
289 	0xFFFFFFF, /* Any PK alg    */
290 	0xFFFFFFF, /* Any curve     */
291 	1024,
292 };
293 
tls_enable_sha1_config(tls_context_t * tls)294 static void tls_enable_sha1_config(tls_context_t *tls)
295 {
296 	const mbedtls_x509_crt_profile *crt_profile = &eap_mbedtls_x509_crt_profile;
297 	mbedtls_ssl_conf_cert_profile(&tls->conf, crt_profile);
298 	mbedtls_ssl_conf_sig_algs(&tls->conf, tls_sig_algs_for_eap);
299 }
300 #ifdef CONFIG_ESP_WIFI_DISABLE_KEY_USAGE_CHECK
tls_disable_key_usages(void * data,mbedtls_x509_crt * cert,int depth,uint32_t * flags)301 static int tls_disable_key_usages(void *data, mbedtls_x509_crt *cert, int depth, uint32_t *flags)
302 {
303 	cert->MBEDTLS_PRIVATE(ext_types) &= ~MBEDTLS_X509_EXT_KEY_USAGE;
304 	cert->MBEDTLS_PRIVATE(ext_types) &= ~MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE;
305 	return 0;
306 }
307 #endif /*CONFIG_ESP_WIFI_DISABLE_KEY_USAGE_CHECK*/
308 
309 static const int eap_ciphersuite_preference[] =
310 {
311 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
312 #if defined(MBEDTLS_CCM_C)
313 	MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CCM,
314 #endif
315 #if defined(MBEDTLS_CIPHER_MODE_CBC)
316 	MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,
317 	MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA,
318 #endif
319 #if defined(MBEDTLS_CIPHER_MODE_CBC)
320 	MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256,
321 	MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA,
322 #endif
323 
324 #if defined(MBEDTLS_GCM_C)
325 	MBEDTLS_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
326 #endif
327 #if defined(MBEDTLS_CCM_C)
328 	MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CCM,
329 #endif
330 #if defined(MBEDTLS_CIPHER_MODE_CBC)
331 	MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,
332 	MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
333 #endif
334 #if defined(MBEDTLS_CCM_C)
335 	MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CCM_8,
336 #endif
337 #endif
338 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
339 	MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CCM,
340 	MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CBC_SHA,
341 	MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CCM_8,
342 
343 	MBEDTLS_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256,
344 	MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CCM,
345 	MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256,
346 	MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CCM_8,
347 #endif
348 #if defined(MBEDTLS_CCM_C)
349 	MBEDTLS_TLS_RSA_WITH_AES_256_CCM,
350 #endif
351 #if defined(MBEDTLS_CIPHER_MODE_CBC)
352 	MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA256,
353 	MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA,
354 #endif
355 #if defined(MBEDTLS_CCM_C)
356 	MBEDTLS_TLS_RSA_WITH_AES_256_CCM_8,
357 #endif
358 
359 #if defined(MBEDTLS_GCM_C)
360 	MBEDTLS_TLS_RSA_WITH_AES_128_GCM_SHA256,
361 #endif
362 #if defined(MBEDTLS_CCM_C)
363 	MBEDTLS_TLS_RSA_WITH_AES_128_CCM,
364 #endif
365 #if defined(MBEDTLS_CIPHER_MODE_CBC)
366 	MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA256,
367 	MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA,
368 #endif
369 #if defined(MBEDTLS_GCM_C)
370 	MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256,
371 #endif
372 #if defined(MBEDTLS_CIPHER_MODE_CBC)
373 	MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256,
374 	MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA,
375 #endif
376 #if defined(MBEDTLS_GCM_C)
377 	MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256,
378 #endif
379 #if defined(MBEDTLS_CIPHER_MODE_CBC)
380 	MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256,
381 	MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA,
382 #endif
383 #if defined(MBEDTLS_CCM_C)
384 	MBEDTLS_TLS_RSA_WITH_AES_128_CCM_8,
385 #endif
386 
387 #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
388 #if defined(MBEDTLS_GCM_C)
389 	MBEDTLS_TLS_RSA_PSK_WITH_AES_128_GCM_SHA256,
390 #endif
391 #if defined(MBEDTLS_CIPHER_MODE_CBC)
392 	MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA256,
393 	MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA,
394 #endif
395 	/* The PSK suites */
396 #if defined(MBEDTLS_CCM_C)
397 	MBEDTLS_TLS_PSK_WITH_AES_256_CCM,
398 #endif
399 #if defined(MBEDTLS_CIPHER_MODE_CBC)
400 	MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA,
401 #endif
402 #if defined(MBEDTLS_CCM_C)
403 	MBEDTLS_TLS_PSK_WITH_AES_256_CCM_8,
404 #endif
405 
406 #if defined(MBEDTLS_GCM_C)
407 	MBEDTLS_TLS_PSK_WITH_AES_128_GCM_SHA256,
408 #endif
409 #if defined(MBEDTLS_CCM_C)
410 	MBEDTLS_TLS_PSK_WITH_AES_128_CCM,
411 #endif
412 #if defined(MBEDTLS_CIPHER_MODE_CBC)
413 	MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA256,
414 	MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA,
415 #endif
416 #if defined(MBEDTLS_CCM_C)
417 	MBEDTLS_TLS_PSK_WITH_AES_128_CCM_8,
418 #endif
419 #endif
420 	0
421 };
422 
423 #ifdef CONFIG_SUITEB192
424 static const int suiteb_rsa_ciphersuite_preference[] =
425 {
426 #if defined(MBEDTLS_GCM_C)
427 #if defined(MBEDTLS_SHA512_C)
428 	MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
429 	MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
430 #endif
431 #endif
432 	0
433 };
434 
435 static const int suiteb_ecc_ciphersuite_preference[] =
436 {
437 #if defined(MBEDTLS_GCM_C)
438 #if defined(MBEDTLS_SHA512_C)
439 	MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
440 #endif
441 #endif
442 	0
443 };
444 static const int suiteb_ciphersuite_preference[] =
445 {
446 #if defined(MBEDTLS_GCM_C)
447 #if defined(MBEDTLS_SHA512_C)
448 	MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
449 	MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
450 	MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
451 #endif
452 #endif
453 	0
454 };
455 #endif
456 
tls_set_ciphersuite(const struct tls_connection_params * cfg,tls_context_t * tls)457 static void tls_set_ciphersuite(const struct tls_connection_params *cfg, tls_context_t *tls)
458 {
459 	/* Only set ciphersuite if cert's key length is high or ciphersuites are set by user */
460 #ifdef CONFIG_SUITEB192
461 	if (cfg->flags & TLS_CONN_SUITEB) {
462 		/* cipher suites will be set based on certificate */
463 		mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(&tls->clientkey);
464 		if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
465 			mbedtls_ssl_conf_ciphersuites(&tls->conf,
466 						      suiteb_rsa_ciphersuite_preference);
467 		} else if (pk_alg == MBEDTLS_PK_ECDSA ||
468 			   pk_alg == MBEDTLS_PK_ECKEY ||
469 			   pk_alg == MBEDTLS_PK_ECKEY_DH) {
470 			mbedtls_ssl_conf_ciphersuites(&tls->conf,
471 						      suiteb_ecc_ciphersuite_preference);
472 		} else {
473 			mbedtls_ssl_conf_ciphersuites(&tls->conf,
474 						      suiteb_ciphersuite_preference);
475 		}
476 	} else
477 #endif
478 	if (tls->ciphersuite[0]) {
479 		mbedtls_ssl_conf_ciphersuites(&tls->conf, tls->ciphersuite);
480 	} else if (mbedtls_pk_get_bitlen(&tls->clientkey) > 2048 ||
481 		(tls->cacert_ptr && mbedtls_pk_get_bitlen(&tls->cacert_ptr->pk) > 2048)) {
482 		mbedtls_ssl_conf_ciphersuites(&tls->conf, eap_ciphersuite_preference);
483 	}
484 }
485 
set_client_config(const struct tls_connection_params * cfg,tls_context_t * tls)486 static int set_client_config(const struct tls_connection_params *cfg, tls_context_t *tls)
487 {
488 	int ret;
489 	int preset = MBEDTLS_SSL_PRESET_DEFAULT;
490 	assert(cfg != NULL);
491 	assert(tls != NULL);
492 
493 #ifdef CONFIG_SUITEB192
494 	if (cfg->flags & TLS_CONN_SUITEB)
495 		preset = MBEDTLS_SSL_PRESET_SUITEB;
496 #endif
497 	ret = mbedtls_ssl_config_defaults(&tls->conf,
498 					MBEDTLS_SSL_IS_CLIENT,
499 					MBEDTLS_SSL_TRANSPORT_STREAM,
500 					preset);
501 	if (ret != 0) {
502 		wpa_printf(MSG_ERROR, "mbedtls_ssl_config_defaults returned -0x%x", -ret);
503 		return ret;
504 	}
505 
506 	if (preset != MBEDTLS_SSL_PRESET_SUITEB) {
507 		/* Enable SHA1 support since it's not enabled by default in mbedtls */
508 		tls_enable_sha1_config(tls);
509 #ifdef CONFIG_SUITEB192
510 	} else {
511 		tls_set_suiteb_config(tls);
512 #endif
513 	}
514 
515 	if (cfg->ca_cert_blob != NULL) {
516 		ret = set_ca_cert(tls, cfg->ca_cert_blob, cfg->ca_cert_blob_len);
517 		if (ret != 0) {
518 			return ret;
519 		}
520 	} else {
521 		mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_NONE);
522 	}
523 
524 	if (cfg->client_cert_blob != NULL && cfg->private_key_blob != NULL) {
525 		ret = set_pki_context(tls, cfg);
526 		if (ret != 0) {
527 			wpa_printf(MSG_ERROR, "Failed to set client pki context");
528 			return ret;
529 		}
530 	}
531 
532 	/* Usages of default ciphersuites can take a lot of time on low end device
533 	 * and can cause watchdog. Enabling the ciphers which are secured enough
534 	 * but doesn't take that much processing power */
535 	tls_set_ciphersuite(cfg, tls);
536 
537 #ifdef CONFIG_ESP_WIFI_DISABLE_KEY_USAGE_CHECK
538 	mbedtls_ssl_set_verify( &tls->ssl, tls_disable_key_usages, NULL );
539 #endif /*CONFIG_ESP_WIFI_DISABLE_KEY_USAGE_CHECK*/
540 
541 #ifdef CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
542 	if (cfg->flags & TLS_CONN_USE_DEFAULT_CERT_BUNDLE) {
543 		wpa_printf(MSG_INFO, "Using default cert bundle");
544 		if (esp_crt_bundle_attach_fn) {
545 			ret = (*esp_crt_bundle_attach_fn)(&tls->conf);
546 		}
547 		if (ret != 0) {
548 			wpa_printf(MSG_ERROR, "Failed to set default cert bundle");
549 			return ret;
550 		}
551 	}
552 #endif
553 
554 	return 0;
555 }
556 
tls_key_derivation(void * ctx,mbedtls_ssl_key_export_type secret_type,const unsigned char * secret,size_t secret_len,const unsigned char client_random[TLS_RANDOM_LEN],const unsigned char server_random[TLS_RANDOM_LEN],mbedtls_tls_prf_types tls_prf_type)557 static void  tls_key_derivation(void *ctx,
558 				mbedtls_ssl_key_export_type secret_type,
559 				const unsigned char *secret,
560 				size_t secret_len,
561 				const unsigned char client_random[TLS_RANDOM_LEN],
562 				const unsigned char server_random[TLS_RANDOM_LEN],
563 				mbedtls_tls_prf_types tls_prf_type)
564 {
565 	struct tls_connection *conn = (struct tls_connection *)ctx;
566 
567 	os_memcpy(conn->master_secret, secret, sizeof(conn->master_secret));
568 	os_memcpy(conn->randbytes, client_random, TLS_RANDOM_LEN);
569 	os_memcpy(conn->randbytes + 32, server_random, TLS_RANDOM_LEN);
570 	conn->tls_prf_type = tls_prf_type;
571 }
572 
tls_create_mbedtls_handle(struct tls_connection * conn,const struct tls_connection_params * params,tls_context_t * tls)573 static int tls_create_mbedtls_handle(struct tls_connection *conn,
574 				     const struct tls_connection_params *params,
575 				     tls_context_t *tls)
576 {
577 	int ret;
578 
579 	assert(params != NULL);
580 	assert(tls != NULL);
581 
582 	mbedtls_ssl_init(&tls->ssl);
583 	mbedtls_ctr_drbg_init(&tls->ctr_drbg);
584 	mbedtls_ssl_config_init(&tls->conf);
585 	mbedtls_entropy_init(&tls->entropy);
586 
587 	ret = set_client_config(params, tls);
588 	if (ret != 0) {
589 		wpa_printf(MSG_ERROR, "Failed to set client configurations");
590 		goto exit;
591 	}
592 
593 	ret = mbedtls_ctr_drbg_seed(&tls->ctr_drbg, mbedtls_entropy_func,
594 				    &tls->entropy, NULL, 0);
595 	if (ret != 0) {
596 		wpa_printf(MSG_ERROR, "mbedtls_ctr_drbg_seed returned -0x%x", -ret);
597 		goto exit;
598 	}
599 
600 	mbedtls_ssl_conf_rng(&tls->conf, mbedtls_ctr_drbg_random, &tls->ctr_drbg);
601 
602 	ret = mbedtls_ssl_setup(&tls->ssl, &tls->conf);
603 	if (ret != 0) {
604 		wpa_printf(MSG_ERROR, "mbedtls_ssl_setup returned -0x%x", -ret);
605 		goto exit;
606 	}
607 	mbedtls_ssl_set_export_keys_cb(&tls->ssl, tls_key_derivation, conn);
608 #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
609 	/* Disable BEAST attack countermeasures for Windows 2008 interoperability */
610 	mbedtls_ssl_conf_cbc_record_splitting(&tls->conf, MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED);
611 #endif
612 
613 	/* Enable debug prints in case supplicant's prints are enabled */
614 #if defined(CONFIG_MBEDTLS_DEBUG) && defined(ESPRESSIF_USE)
615 	mbedtls_esp_enable_debug_log(&tls->conf, 2);
616 #endif
617 	return 0;
618 
619 exit:
620 	tls_mbedtls_cleanup(tls);
621 	return ret;
622 }
623 
tls_init(const struct tls_config * conf)624 void *tls_init(const struct tls_config *conf)
625 {
626 	tls_instance_count++;
627 	return &tls_instance_count;
628 }
629 
tls_deinit(void * tls_ctx)630 void tls_deinit(void *tls_ctx)
631 {
632 	tls_instance_count--;
633 }
634 
tls_connection_init(void * tls_ctx)635 struct tls_connection * tls_connection_init(void *tls_ctx)
636 {
637 	struct tls_connection *conn = os_zalloc(sizeof(*conn));
638 	if (!conn) {
639 		wpa_printf(MSG_ERROR, "TLS: Failed to allocate connection memory");
640 		return NULL;
641 	}
642 	return conn;
643 }
644 
645 
tls_connection_deinit(void * tls_ctx,struct tls_connection * conn)646 void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn)
647 {
648 	/* case: tls init failed */
649 	if (!conn) {
650 		return;
651 	}
652 	/* Free ssl ctx and data */
653 	tls_mbedtls_conn_delete((tls_context_t *) conn->tls);
654 	os_free(conn->tls);
655 	conn->tls = NULL;
656 	/* Data in in ssl ctx, free connection */
657 	os_free(conn);
658 }
659 
tls_get_errors(void * tls_ctx)660 int tls_get_errors(void *tls_ctx)
661 {
662 	return 0;
663 }
664 
tls_connection_established(void * tls_ctx,struct tls_connection * conn)665 int tls_connection_established(void *tls_ctx, struct tls_connection *conn)
666 {
667 	mbedtls_ssl_context *ssl = &conn->tls->ssl;
668 
669 	return mbedtls_ssl_is_handshake_over(ssl);
670 }
671 
tls_global_set_verify(void * tls_ctx,int check_crl,int strict)672 int tls_global_set_verify(void *tls_ctx, int check_crl, int strict)
673 {
674 	wpa_printf(MSG_INFO, "TLS: global settings are not supported");
675 	return -1;
676 }
677 
tls_connection_set_verify(void * tls_ctx,struct tls_connection * conn,int verify_peer,unsigned int flags,const u8 * session_ctx,size_t session_ctx_len)678 int tls_connection_set_verify(void *tls_ctx, struct tls_connection *conn,
679 			      int verify_peer, unsigned int flags,
680 			      const u8 *session_ctx, size_t session_ctx_len)
681 {
682 	wpa_printf(MSG_INFO, "TLS: tls_connection_set_verify not supported");
683 	return -1;
684 }
685 
686 #ifdef CONFIG_ESP_WIFI_ENT_FREE_DYNAMIC_BUFFER
esp_mbedtls_free_dhm(mbedtls_ssl_context * ssl)687 static void esp_mbedtls_free_dhm(mbedtls_ssl_context *ssl)
688 {
689 #ifdef CONFIG_MBEDTLS_DHM_C
690 	const mbedtls_ssl_config *conf = mbedtls_ssl_context_get_config(ssl);
691 	mbedtls_mpi_free((mbedtls_mpi *)&conf->MBEDTLS_PRIVATE(dhm_P));
692 	mbedtls_mpi_free((mbedtls_mpi *)&conf->MBEDTLS_PRIVATE(dhm_G));
693 #endif /* CONFIG_MBEDTLS_DHM_C */
694 }
695 
esp_mbedtls_free_keycert(mbedtls_ssl_context * ssl)696 static void esp_mbedtls_free_keycert(mbedtls_ssl_context *ssl)
697 {
698 	mbedtls_ssl_config *conf = (mbedtls_ssl_config * )mbedtls_ssl_context_get_config(ssl);
699 	mbedtls_ssl_key_cert *keycert = conf->MBEDTLS_PRIVATE(key_cert), *next;
700 
701 	while (keycert) {
702 		next = keycert->next;
703 
704 		if (keycert) {
705 			mbedtls_free(keycert);
706 		}
707 
708 		keycert = next;
709 	}
710 
711 	conf->MBEDTLS_PRIVATE(key_cert) = NULL;
712 }
713 
esp_mbedtls_free_keycert_key(mbedtls_ssl_context * ssl)714 static void esp_mbedtls_free_keycert_key(mbedtls_ssl_context *ssl)
715 {
716 	const mbedtls_ssl_config *conf = mbedtls_ssl_context_get_config(ssl);
717 	mbedtls_ssl_key_cert *keycert = conf->MBEDTLS_PRIVATE(key_cert);
718 
719 	while (keycert) {
720 		if (keycert->key) {
721 			mbedtls_pk_free(keycert->key);
722 			keycert->key = NULL;
723 		}
724 		keycert = keycert->next;
725 	}
726 }
727 
esp_mbedtls_free_cacert(mbedtls_ssl_context * ssl)728 static void esp_mbedtls_free_cacert(mbedtls_ssl_context *ssl)
729 {
730 	if (ssl->MBEDTLS_PRIVATE(conf)->MBEDTLS_PRIVATE(ca_chain)) {
731 		mbedtls_ssl_config *conf = (mbedtls_ssl_config * )mbedtls_ssl_context_get_config(ssl);
732 
733 		mbedtls_x509_crt_free(conf->MBEDTLS_PRIVATE(ca_chain));
734 		conf->MBEDTLS_PRIVATE(ca_chain) = NULL;
735 	}
736 }
737 #endif
738 
tls_connection_handshake(void * tls_ctx,struct tls_connection * conn,const struct wpabuf * in_data,struct wpabuf ** appl_data)739 struct wpabuf * tls_connection_handshake(void *tls_ctx,
740 					 struct tls_connection *conn,
741 					 const struct wpabuf *in_data,
742 					 struct wpabuf **appl_data)
743 {
744 	tls_context_t *tls = conn->tls;
745 	int ret = 0;
746 	struct wpabuf *resp;
747 	int cli_state;
748 
749 	/* data freed by sender */
750 	conn->tls_io_data.out_data = NULL;
751 	if (wpabuf_len(in_data)) {
752 		conn->tls_io_data.in_data = wpabuf_dup(in_data);
753 	}
754 
755 	/* Multiple reads */
756 	while (!mbedtls_ssl_is_handshake_over(&tls->ssl)) {
757 		cli_state = tls->ssl.MBEDTLS_PRIVATE(state);
758 		if (cli_state == MBEDTLS_SSL_CLIENT_CERTIFICATE) {
759 			/* Read random data before session completes, not present after handshake */
760 			if (tls->ssl.MBEDTLS_PRIVATE(handshake)) {
761 				os_memcpy(conn->randbytes, tls->ssl.MBEDTLS_PRIVATE(handshake)->randbytes,
762 					  TLS_RANDOM_LEN * 2);
763 				conn->mac = tls->ssl.MBEDTLS_PRIVATE(handshake)->ciphersuite_info->mac;
764 			}
765 		}
766 		ret = mbedtls_ssl_handshake_step(&tls->ssl);
767 
768 		if (ret < 0) {
769 			break;
770 		}
771 #ifdef CONFIG_ESP_WIFI_ENT_FREE_DYNAMIC_BUFFER
772 		if (mbedtls_ssl_get_version_number(&tls->ssl) == MBEDTLS_SSL_VERSION_TLS1_2) {
773 			if (cli_state == MBEDTLS_SSL_SERVER_CERTIFICATE) {
774 				esp_mbedtls_free_cacert(&tls->ssl);
775 			} else if (cli_state == MBEDTLS_SSL_CERTIFICATE_VERIFY) {
776 				esp_mbedtls_free_dhm(&tls->ssl);
777 				esp_mbedtls_free_keycert_key(&tls->ssl);
778 				esp_mbedtls_free_keycert(&tls->ssl);
779 			}
780 		}
781 #endif
782 	}
783 	if (ret < 0 && ret != MBEDTLS_ERR_SSL_WANT_READ) {
784 		wpa_printf(MSG_INFO, "%s: ret is %d line:%d", __func__, ret, __LINE__);
785 		goto end;
786 	}
787 
788 	if (!conn->tls_io_data.out_data) {
789 		wpa_printf(MSG_INFO, "application data is null, adding one byte for ack");
790 		u8 *dummy = os_zalloc(1);
791 		conn->tls_io_data.out_data = wpabuf_alloc_ext_data(dummy, 0);
792 	}
793 
794 end:
795 	resp = conn->tls_io_data.out_data;
796 	conn->tls_io_data.out_data = NULL;
797 	return resp;
798 }
799 
tls_connection_server_handshake(void * tls_ctx,struct tls_connection * conn,const struct wpabuf * in_data,struct wpabuf ** appl_data)800 struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
801 						struct tls_connection *conn,
802 						const struct wpabuf *in_data,
803 						struct wpabuf **appl_data)
804 {
805 	wpa_printf(MSG_ERROR, "%s: not supported %d", __func__, __LINE__);
806 	return NULL;
807 }
808 
809 
tls_connection_encrypt(void * tls_ctx,struct tls_connection * conn,const struct wpabuf * in_data)810 struct wpabuf * tls_connection_encrypt(void *tls_ctx,
811 				       struct tls_connection *conn,
812 				       const struct wpabuf *in_data)
813 {
814 	struct wpabuf *resp;
815 	size_t ret;
816 
817 	/* Reset dangling pointer */
818 	conn->tls_io_data.out_data = NULL;
819 	ret = mbedtls_ssl_write(&conn->tls->ssl,
820 			(unsigned char*) wpabuf_head(in_data),  wpabuf_len(in_data));
821 
822 	if (ret < wpabuf_len(in_data)) {
823 		wpa_printf(MSG_ERROR, "%s:%d, not able to write whole data",
824 			   __func__, __LINE__);
825 	}
826 
827 	resp = conn->tls_io_data.out_data;
828 	conn->tls_io_data.out_data = NULL;
829 	return resp;
830 }
831 
832 
tls_connection_decrypt(void * tls_ctx,struct tls_connection * conn,const struct wpabuf * in_data)833 struct wpabuf *tls_connection_decrypt(void *tls_ctx,
834 				      struct tls_connection *conn,
835 				      const struct wpabuf *in_data)
836 {
837 #define MAX_PHASE2_BUFFER 1536
838 	struct wpabuf *out = NULL;
839 	int ret;
840 	unsigned char *buf = os_malloc(MAX_PHASE2_BUFFER);
841 
842 	if (!buf) {
843 		return NULL;
844 	}
845 	/* Reset dangling output buffer before setting data, data was freed by caller */
846 	conn->tls_io_data.out_data = NULL;
847 
848 	conn->tls_io_data.in_data = wpabuf_dup(in_data);
849 
850 	if (!conn->tls_io_data.in_data) {
851 		goto cleanup;
852 	}
853 	ret = mbedtls_ssl_read(&conn->tls->ssl, buf, MAX_PHASE2_BUFFER);
854 	if (ret < 0) {
855 		wpa_printf(MSG_ERROR, "%s:%d, not able to read data",
856 				__func__, __LINE__);
857 		goto cleanup;
858 	}
859 	out = wpabuf_alloc_copy(buf, ret);
860 cleanup:
861 	/* there may be some error written in output buffer */
862 	if (conn->tls_io_data.out_data) {
863 		os_free(conn->tls_io_data.out_data);
864 		conn->tls_io_data.out_data = NULL;
865 	}
866 
867 	os_free(buf);
868 
869 	return out;
870 #undef MAX_PHASE2_BUFFER
871 }
872 
873 
tls_connection_resumed(void * tls_ctx,struct tls_connection * conn)874 int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn)
875 {
876 	if (conn && conn->tls && conn->tls->ssl.MBEDTLS_PRIVATE(handshake)) {
877 		return conn->tls->ssl.MBEDTLS_PRIVATE(handshake)->resume;
878 	}
879 
880 	return 0;
881 }
882 
883 /* cipher array should contain cipher number in mbedtls num as per IANA
884  * Please see cipherlist is u8, therefore only initial ones are supported */
tls_connection_set_cipher_list(void * tls_ctx,struct tls_connection * conn,u8 * ciphers)885 int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
886 				   u8 *ciphers)
887 {
888 	int i = 0;
889 
890 	while (*ciphers != 0 && i < MAX_CIPHERSUITE) {
891 		conn->tls->ciphersuite[i] = ciphers[i];
892 		i++;
893 	}
894 	return 0;
895 }
896 
tls_get_version(void * tls_ctx,struct tls_connection * conn,char * buf,size_t buflen)897 int tls_get_version(void *tls_ctx, struct tls_connection *conn,
898 		    char *buf, size_t buflen)
899 {
900 	const char *name;
901 
902 	if (conn == NULL) {
903 		return -1;
904 	}
905 
906 	name = mbedtls_ssl_get_version(&conn->tls->ssl);
907 	if (name == NULL) {
908 		return -1;
909 	}
910 
911 	os_strlcpy(buf, name, buflen);
912 
913 	return 0;
914 }
915 
tls_get_cipher(void * tls_ctx,struct tls_connection * conn,char * buf,size_t buflen)916 int tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
917 		   char *buf, size_t buflen)
918 {
919 	const char *name;
920 	if (conn == NULL) {
921 		return -1;
922 	}
923 
924 	name = mbedtls_ssl_get_ciphersuite(&conn->tls->ssl);
925 	if (name == NULL) {
926 		return -1;
927 	}
928 
929 	os_strlcpy(buf, name, buflen);
930 
931 	return 0;
932 }
933 
934 
tls_connection_enable_workaround(void * tls_ctx,struct tls_connection * conn)935 int tls_connection_enable_workaround(void *tls_ctx,
936 				     struct tls_connection *conn)
937 {
938 	wpa_printf(MSG_ERROR, "%s: not supported %d", __func__, __LINE__);
939 	return -1;
940 }
941 
tls_connection_get_failed(void * tls_ctx,struct tls_connection * conn)942 int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn)
943 {
944 	return 0;
945 }
946 
tls_connection_get_read_alerts(void * tls_ctx,struct tls_connection * conn)947 int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn)
948 {
949 	wpa_printf(MSG_ERROR, "%s: not supported %d", __func__, __LINE__);
950 	return 0;
951 }
952 
tls_connection_get_write_alerts(void * tls_ctx,struct tls_connection * conn)953 int tls_connection_get_write_alerts(void *tls_ctx, struct tls_connection *conn)
954 {
955 	wpa_printf(MSG_ERROR, "%s: not supported %d", __func__, __LINE__);
956 	return 0;
957 }
958 
tls_connection_set_params(void * tls_ctx,struct tls_connection * conn,const struct tls_connection_params * params)959 int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
960 			      const struct tls_connection_params *params)
961 {
962 	int ret = 0;
963 	tls_context_t *tls = (tls_context_t *)os_zalloc(sizeof(tls_context_t));
964 
965 	if (!tls) {
966 		wpa_printf(MSG_ERROR, "failed to allocate tls context");
967 		return -1;
968 	}
969 	if (!params) {
970 		wpa_printf(MSG_ERROR, "configuration is null");
971 		ret = -1;
972 		goto err;
973 	}
974 
975 	ret = tls_create_mbedtls_handle(conn, params, tls);
976 	if (ret < 0) {
977 		wpa_printf(MSG_ERROR, "failed to create ssl handle");
978 		goto err;
979 	}
980 	mbedtls_ssl_set_bio(&tls->ssl, conn, tls_mbedtls_write, tls_mbedtls_read, NULL);
981 	conn->tls = (tls_context_t *)tls;
982 
983 	return ret;
984 err:
985 	os_free(tls);
986 	return ret;
987 }
988 
tls_global_set_params(void * tls_ctx,const struct tls_connection_params * params)989 int tls_global_set_params(void *tls_ctx,
990 			  const struct tls_connection_params *params)
991 {
992 	wpa_printf(MSG_INFO, "TLS: Global parameters not supported");
993 	return -1;
994 }
995 
tls_connection_set_session_ticket_cb(void * tls_ctx,struct tls_connection * conn,tls_session_ticket_cb cb,void * ctx)996 int tls_connection_set_session_ticket_cb(void *tls_ctx,
997 					 struct tls_connection *conn,
998 					 tls_session_ticket_cb cb,
999 					 void *ctx)
1000 {
1001 	wpa_printf(MSG_ERROR, "TLS: %s not supported", __func__);
1002 	return -1;
1003 }
1004 
tls_connection_prf(void * tls_ctx,struct tls_connection * conn,const char * label,int server_random_first,u8 * out,size_t out_len)1005 static int tls_connection_prf(void *tls_ctx, struct tls_connection *conn,
1006 		       const char *label, int server_random_first,
1007 		       u8 *out, size_t out_len)
1008 {
1009 	int ret;
1010 	u8 seed[2 * TLS_RANDOM_LEN];
1011 	mbedtls_ssl_context *ssl = &conn->tls->ssl;
1012 
1013 	if (!ssl) {
1014 		wpa_printf(MSG_ERROR, "TLS: %s, session ingo is null", __func__);
1015 		return -1;
1016 	}
1017 	if (!mbedtls_ssl_is_handshake_over(ssl)) {
1018 		wpa_printf(MSG_ERROR, "TLS: %s, incorrect tls state=%d", __func__, ssl->MBEDTLS_PRIVATE(state));
1019 		return -1;
1020 	}
1021 
1022 	if (server_random_first) {
1023 		os_memcpy(seed, conn->randbytes + TLS_RANDOM_LEN, TLS_RANDOM_LEN);
1024 		os_memcpy(seed + TLS_RANDOM_LEN, conn->randbytes, TLS_RANDOM_LEN);
1025 	} else {
1026 		os_memcpy(seed, conn->randbytes, 2 * TLS_RANDOM_LEN);
1027 	}
1028 
1029 	wpa_hexdump_key(MSG_MSGDUMP, "random", seed, 2 * TLS_RANDOM_LEN);
1030 	wpa_hexdump_key(MSG_MSGDUMP, "master",  ssl->MBEDTLS_PRIVATE(session)->MBEDTLS_PRIVATE(master), TLS_MASTER_SECRET_LEN);
1031 
1032 	ret = mbedtls_ssl_tls_prf(conn->tls_prf_type, conn->master_secret, TLS_MASTER_SECRET_LEN,
1033 				label, seed, 2 * TLS_RANDOM_LEN, out, out_len);
1034 
1035 	if (ret < 0) {
1036 		wpa_printf(MSG_ERROR, "prf failed, ret=%d", ret);
1037 	}
1038 	wpa_hexdump_key(MSG_MSGDUMP, "key", out, out_len);
1039 
1040 	return ret;
1041 }
1042 
tls_connection_export_key(void * tls_ctx,struct tls_connection * conn,const char * label,const u8 * context,size_t context_len,u8 * out,size_t out_len)1043 int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn,
1044 			      const char *label, const u8 *context,
1045 			      size_t context_len, u8 *out, size_t out_len)
1046 {
1047 	return tls_connection_prf(tls_ctx, conn, label, 0, out, out_len);
1048 }
1049 
tls_connection_get_eap_fast_key(void * tls_ctx,struct tls_connection * conn,u8 * out,size_t out_len)1050 int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn,
1051 				    u8 *out, size_t out_len)
1052 {
1053 	wpa_printf(MSG_INFO, "TLS: tls_connection_get_eap_fast_key not supported, please unset mbedtls crypto and try again");
1054 	return -1;
1055 }
1056 
tls_connection_client_hello_ext(void * tls_ctx,struct tls_connection * conn,int ext_type,const u8 * data,size_t data_len)1057 int tls_connection_client_hello_ext(void *tls_ctx, struct tls_connection *conn,
1058 				    int ext_type, const u8 *data,
1059 				    size_t data_len)
1060 {
1061 	wpa_printf(MSG_INFO, "TLS: tls_connection_client_hello_ext not supported, please unset mbedtls crypto and try again");
1062 	return -1;
1063 }
1064 
tls_connection_shutdown(void * tls_ctx,struct tls_connection * conn)1065 int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn)
1066 {
1067 	if (conn->tls_io_data.in_data) {
1068 		wpabuf_free(conn->tls_io_data.in_data);
1069 	}
1070 	conn->tls_io_data.in_data = NULL;
1071 
1072 	/* outdata may have dangling pointer */
1073 	conn->tls_io_data.out_data = NULL;
1074 
1075 	return mbedtls_ssl_session_reset(&conn->tls->ssl);
1076 }
1077 
tls_connection_get_random(void * tls_ctx,struct tls_connection * conn,struct tls_random * data)1078 int tls_connection_get_random(void *tls_ctx, struct tls_connection *conn,
1079 			    struct tls_random *data)
1080 {
1081 	mbedtls_ssl_context *ssl = &conn->tls->ssl;
1082 
1083 	os_memset(data, 0, sizeof(*data));
1084 	if (ssl->MBEDTLS_PRIVATE(state) == MBEDTLS_SSL_CLIENT_HELLO) {
1085 		return -1;
1086 	}
1087 
1088 	data->client_random = conn->randbytes;
1089 	data->client_random_len = TLS_RANDOM_LEN;
1090 
1091 	if (ssl->MBEDTLS_PRIVATE(state) != MBEDTLS_SSL_SERVER_HELLO) {
1092 		data->server_random = conn->randbytes + TLS_RANDOM_LEN;
1093 		data->server_random_len = TLS_RANDOM_LEN;
1094 	}
1095 
1096 	return 0;
1097 }
1098