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);
72 return react_on_eagain;
77 return MBEDTLS_ERR_NET_CONN_RESET;
81 return MBEDTLS_ERR_NET_SEND_FAILED;
92 if (
h->max_packet_size &&
len >
h->max_packet_size)
93 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
105 if (
h->max_packet_size &&
len >
h->max_packet_size)
106 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
114 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
115 av_log(
h,
AV_LOG_ERROR,
"Read of key file failed. Is it actually there, are the access permissions correct?\n");
117 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
120 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
132 #if MBEDTLS_VERSION_MAJOR < 3
133 case MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE:
134 av_log(
h,
AV_LOG_ERROR,
"None of the common ciphersuites is usable. Was the local certificate correctly set?\n");
137 case MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE:
141 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
142 av_log(
h,
AV_LOG_ERROR,
"A fatal alert message was received from the peer, has the peer a correct certificate?\n");
144 case MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED:
145 av_log(
h,
AV_LOG_ERROR,
"No CA chain is set, but required to operate. Was the CA correctly set?\n");
147 case MBEDTLS_ERR_NET_CONN_RESET:
159 const char *p = strchr(uri,
'?');
171 uint32_t verify_res_flags;
181 mbedtls_ssl_config_init(&tls_ctx->
ssl_config);
184 mbedtls_x509_crt_init(&tls_ctx->
ca_cert);
185 mbedtls_pk_init(&tls_ctx->
priv_key);
205 mbedtls_entropy_func,
214 if ((
ret = mbedtls_pk_parse_keyfile(&tls_ctx->
priv_key,
217 #
if MBEDTLS_VERSION_MAJOR >= 3
218 , mbedtls_ctr_drbg_random,
228 shr->
listen ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT,
229 MBEDTLS_SSL_TRANSPORT_STREAM,
230 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
235 mbedtls_ssl_conf_authmode(&tls_ctx->
ssl_config,
236 shr->
ca_file ? MBEDTLS_SSL_VERIFY_REQUIRED : MBEDTLS_SSL_VERIFY_NONE);
262 while ((
ret = mbedtls_ssl_handshake(&tls_ctx->
ssl_context)) != 0) {
263 if (
ret != MBEDTLS_ERR_SSL_WANT_READ &&
ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
271 if ((verify_res_flags = mbedtls_ssl_get_verify_result(&tls_ctx->
ssl_context)) != 0) {
273 "with the certificate verification, returned flags: %u\n",
275 if (verify_res_flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED)
291 case MBEDTLS_ERR_SSL_WANT_READ:
292 case MBEDTLS_ERR_SSL_WANT_WRITE:
294 case MBEDTLS_ERR_NET_SEND_FAILED:
295 case MBEDTLS_ERR_NET_RECV_FAILED:
297 case MBEDTLS_ERR_NET_CONN_RESET:
298 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: