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