22 #include <mbedtls/version.h>
23 #include <mbedtls/ctr_drbg.h>
24 #include <mbedtls/entropy.h>
25 #include <mbedtls/net_sockets.h>
26 #include <mbedtls/platform.h>
27 #include <mbedtls/ssl.h>
28 #include <mbedtls/x509_crt.h>
29 #include <mbedtls/debug.h>
30 #ifdef MBEDTLS_PSA_CRYPTO_C
31 #include <psa/crypto.h>
55 #define OFFSET(x) offsetof(TLSContext, x)
63 mbedtls_x509_crt_free(&tls_ctx->
ca_cert);
64 mbedtls_x509_crt_free(&tls_ctx->
own_cert);
78 return react_on_eagain;
83 return MBEDTLS_ERR_NET_CONN_RESET;
87 return MBEDTLS_ERR_NET_SEND_FAILED;
98 if (
h->max_packet_size &&
len >
h->max_packet_size)
99 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
111 if (
h->max_packet_size &&
len >
h->max_packet_size)
112 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
127 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
128 av_log(
h,
AV_LOG_ERROR,
"Read of key file failed. Is it actually there, are the access permissions correct?\n");
130 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
133 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
145 #if MBEDTLS_VERSION_MAJOR < 3
146 case MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE:
147 av_log(
h,
AV_LOG_ERROR,
"None of the common ciphersuites is usable. Was the local certificate correctly set?\n");
150 case MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE:
153 case MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION:
157 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
158 av_log(
h,
AV_LOG_ERROR,
"A fatal alert message was received from the peer, has the peer a correct certificate?\n");
160 case MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED:
161 av_log(
h,
AV_LOG_ERROR,
"No CA chain is set, but required to operate. Was the CA correctly set?\n");
163 case MBEDTLS_ERR_SSL_INTERNAL_ERROR:
166 case MBEDTLS_ERR_NET_CONN_RESET:
169 case MBEDTLS_ERR_X509_CERT_VERIFY_FAILED:
181 const char *p = strchr(uri,
'?');
193 uint32_t verify_res_flags;
202 #ifdef MBEDTLS_PSA_CRYPTO_C
203 if ((
ret = psa_crypto_init()) != PSA_SUCCESS) {
210 mbedtls_ssl_config_init(&tls_ctx->
ssl_config);
213 mbedtls_x509_crt_init(&tls_ctx->
ca_cert);
214 mbedtls_pk_init(&tls_ctx->
priv_key);
242 mbedtls_entropy_func,
251 if ((
ret = mbedtls_pk_parse_keyfile(&tls_ctx->
priv_key,
254 #
if MBEDTLS_VERSION_MAJOR >= 3
255 , mbedtls_ctr_drbg_random,
265 shr->
listen ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT,
266 MBEDTLS_SSL_TRANSPORT_STREAM,
267 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
272 #ifdef MBEDTLS_SSL_PROTO_TLS1_3
274 if (mbedtls_version_get_number() == 0x03060000 && !shr->
verify) {
275 av_log(
h,
AV_LOG_INFO,
"Forcing TLSv1.2 because certificate verification is disabled\n");
276 mbedtls_ssl_conf_max_tls_version(&tls_ctx->
ssl_config, MBEDTLS_SSL_VERSION_TLS1_2);
281 mbedtls_ssl_conf_authmode(&tls_ctx->
ssl_config,
282 shr->
verify ? MBEDTLS_SSL_VERIFY_OPTIONAL : MBEDTLS_SSL_VERIFY_NONE);
308 while ((
ret = mbedtls_ssl_handshake(&tls_ctx->
ssl_context)) != 0) {
309 if (
ret != MBEDTLS_ERR_SSL_WANT_READ &&
ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
317 if ((verify_res_flags = mbedtls_ssl_get_verify_result(&tls_ctx->
ssl_context)) != 0) {
319 "with the certificate verification, returned flags: %u\n",
321 if (verify_res_flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED)
337 case MBEDTLS_ERR_SSL_WANT_READ:
338 case MBEDTLS_ERR_SSL_WANT_WRITE:
339 #ifdef MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET
340 case MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET:
343 case MBEDTLS_ERR_NET_SEND_FAILED:
344 case MBEDTLS_ERR_NET_RECV_FAILED:
346 case MBEDTLS_ERR_NET_CONN_RESET:
347 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: