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>
49 #define OFFSET(x) offsetof(TLSContext, x)
57 mbedtls_x509_crt_free(&tls_ctx->
ca_cert);
58 mbedtls_x509_crt_free(&tls_ctx->
own_cert);
71 return react_on_eagain;
76 return MBEDTLS_ERR_NET_CONN_RESET;
80 return MBEDTLS_ERR_NET_SEND_FAILED;
91 if (
h->max_packet_size &&
len >
h->max_packet_size)
92 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
104 if (
h->max_packet_size &&
len >
h->max_packet_size)
105 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
113 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
114 av_log(
h,
AV_LOG_ERROR,
"Read of key file failed. Is it actually there, are the access permissions correct?\n");
116 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
119 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
131 #if MBEDTLS_VERSION_MAJOR < 3
132 case MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE:
133 av_log(
h,
AV_LOG_ERROR,
"None of the common ciphersuites is usable. Was the local certificate correctly set?\n");
136 case MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE:
140 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
141 av_log(
h,
AV_LOG_ERROR,
"A fatal alert message was received from the peer, has the peer a correct certificate?\n");
143 case MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED:
144 av_log(
h,
AV_LOG_ERROR,
"No CA chain is set, but required to operate. Was the CA correctly set?\n");
146 case MBEDTLS_ERR_NET_CONN_RESET:
158 const char *p = strchr(uri,
'?');
170 uint32_t verify_res_flags;
180 mbedtls_ssl_config_init(&tls_ctx->
ssl_config);
183 mbedtls_x509_crt_init(&tls_ctx->
ca_cert);
184 mbedtls_pk_init(&tls_ctx->
priv_key);
204 mbedtls_entropy_func,
213 if ((
ret = mbedtls_pk_parse_keyfile(&tls_ctx->
priv_key,
216 #
if MBEDTLS_VERSION_MAJOR >= 3
217 , mbedtls_ctr_drbg_random,
227 shr->
listen ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT,
228 MBEDTLS_SSL_TRANSPORT_STREAM,
229 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
234 mbedtls_ssl_conf_authmode(&tls_ctx->
ssl_config,
235 shr->
ca_file ? MBEDTLS_SSL_VERIFY_REQUIRED : MBEDTLS_SSL_VERIFY_NONE);
261 while ((
ret = mbedtls_ssl_handshake(&tls_ctx->
ssl_context)) != 0) {
262 if (
ret != MBEDTLS_ERR_SSL_WANT_READ &&
ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
270 if ((verify_res_flags = mbedtls_ssl_get_verify_result(&tls_ctx->
ssl_context)) != 0) {
272 "with the certificate verification, returned flags: %u\n",
274 if (verify_res_flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED)
290 case MBEDTLS_ERR_SSL_WANT_READ:
291 case MBEDTLS_ERR_SSL_WANT_WRITE:
293 case MBEDTLS_ERR_NET_SEND_FAILED:
294 case MBEDTLS_ERR_NET_RECV_FAILED:
296 case MBEDTLS_ERR_NET_CONN_RESET:
297 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: