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#include <mbedtls/timing.h>
31#ifdef MBEDTLS_PSA_CRYPTO_C
32#include <psa/crypto.h>
35#include "config_components.h"
50 size_t n =
sizeof(
md);
55 mbedtls_x509_crt_init(&crt);
57 if ((ret = mbedtls_x509_crt_parse(&crt, cert_buf, cert_sz)) != 0) {
58 mbedtls_x509_crt_free(&crt);
62 if ((ret = mbedtls_sha256(crt.raw.p, crt.raw.len,
md, 0)) != 0) {
63 mbedtls_x509_crt_free(&crt);
69 for (
int i = 0;
i < n - 1;
i++)
76int ff_ssl_read_key_cert(
char *key_url,
char *cert_url,
char *key_buf,
size_t key_sz,
char *cert_buf,
size_t cert_sz,
char **fingerprint)
79 AVBPrint key_bp, cert_bp;
95 if (key_sz < key_bp.size || cert_sz < cert_bp.size) {
101 key_buf = key_bp.str;
102 cert_buf = cert_bp.str;
116 mbedtls_entropy_context entropy;
117 mbedtls_ctr_drbg_context ctr_drbg;
119 mbedtls_entropy_init(&entropy);
120 mbedtls_ctr_drbg_init(&ctr_drbg);
122 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
123 &entropy,
NULL, 0)) != 0) {
128 if ((ret = mbedtls_pk_setup(
key,
129 mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY))) != 0) {
139 if ((ret = mbedtls_ecp_gen_key(MBEDTLS_ECP_DP_SECP256R1,
141 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
146 mbedtls_entropy_free(&entropy);
147 mbedtls_ctr_drbg_free(&ctr_drbg);
154 const char *
name =
"CN=lavf";
157 char not_before[16], not_after[16];
158 unsigned char serial[20];
159 mbedtls_entropy_context entropy;
160 mbedtls_ctr_drbg_context ctr_drbg;
161 mbedtls_x509write_cert crt;
163 mbedtls_entropy_init(&entropy);
164 mbedtls_ctr_drbg_init(&ctr_drbg);
165 mbedtls_x509write_crt_init(&crt);
167 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
NULL, 0)) != 0) {
172 mbedtls_x509write_crt_set_subject_key(&crt,
key);
173 mbedtls_x509write_crt_set_issuer_key(&crt,
key);
174 if ((ret = mbedtls_x509write_crt_set_subject_name(&crt,
name)) != 0) {
179 if ((ret = mbedtls_x509write_crt_set_issuer_name(&crt,
name)) != 0) {
183 mbedtls_x509write_crt_set_version(&crt, MBEDTLS_X509_CRT_VERSION_3);
184 mbedtls_x509write_crt_set_md_alg(&crt, MBEDTLS_MD_SHA256);
192 if ((ret = mbedtls_x509write_crt_set_serial_raw(&crt, serial,
sizeof(serial))) != 0) {
199 strftime(not_before,
sizeof(not_before),
"%Y%m%d%H%M%S", &tm);
201 strftime(not_after,
sizeof(not_after),
"%Y%m%d%H%M%S", &tm);
203 if ((ret = mbedtls_x509write_crt_set_validity(&crt, not_before, not_after)) != 0) {
208 if ((ret = mbedtls_x509write_crt_pem(&crt, cert_buf, cert_sz,
209 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
215 mbedtls_entropy_free(&entropy);
216 mbedtls_ctr_drbg_free(&ctr_drbg);
217 mbedtls_x509write_crt_free(&crt);
224 mbedtls_pk_context
key;
226 mbedtls_pk_init(&
key);
231 if ((ret = mbedtls_pk_write_key_pem(&
key, key_buf, key_sz)) != 0)
242 mbedtls_pk_free(&
key);
281#if defined(MBEDTLS_SSL_DTLS_SRTP)
282static void dtls_srtp_key_derivation(
void *p_expkey,
283 mbedtls_ssl_key_export_type secret_type,
284 const unsigned char *secret,
286 const unsigned char client_random[32],
287 const unsigned char server_random[32],
288 mbedtls_tls_prf_types tls_prf_type)
296 memcpy(keys->
randbytes, client_random, 32);
297 memcpy(keys->
randbytes + 32, server_random, 32);
306#if defined(MBEDTLS_SSL_DTLS_SRTP)
307 const char*
dst =
"EXTRACTOR-dtls_srtp";
308 mbedtls_dtls_srtp_info dtls_srtp_negotiation_result;
309 mbedtls_ssl_get_dtls_srtp_negotiation_result(&tls_ctx->
ssl_context, &dtls_srtp_negotiation_result);
318 materials_sz)) != 0) {
329#define OFFSET(x) offsetof(TLSContext, x)
337 mbedtls_pk_free(&tls_ctx->
priv_key);
338 mbedtls_x509_crt_free(&tls_ctx->
ca_cert);
339 mbedtls_x509_crt_free(&tls_ctx->
own_cert);
341 mbedtls_ssl_config_free(&tls_ctx->
ssl_config);
353 return react_on_eagain;
358 return MBEDTLS_ERR_NET_CONN_RESET;
362 return MBEDTLS_ERR_NET_SEND_FAILED;
375 if (
h->max_packet_size &&
len >
h->max_packet_size)
376 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
388#if CONFIG_UDP_PROTOCOL
398 av_log(tls_ctx,
AV_LOG_TRACE,
"Set UDP remote addr on UDP socket, now 'connected'\n");
403 return MBEDTLS_ERR_SSL_WANT_READ;
406 if (
h->max_packet_size &&
len >
h->max_packet_size)
407 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
422 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
423 av_log(
h,
AV_LOG_ERROR,
"Read of key file failed. Is it actually there, are the access permissions correct?\n");
425 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
428 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
440#if MBEDTLS_VERSION_MAJOR < 3
441 case MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE:
442 av_log(
h,
AV_LOG_ERROR,
"None of the common ciphersuites is usable. Was the local certificate correctly set?\n");
445 case MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE:
448 case MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION:
452 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
453 av_log(
h,
AV_LOG_ERROR,
"A fatal alert message was received from the peer, has the peer a correct certificate?\n");
455 case MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED:
456 av_log(
h,
AV_LOG_ERROR,
"No CA chain is set, but required to operate. Was the CA correctly set?\n");
458 case MBEDTLS_ERR_SSL_INTERNAL_ERROR:
461 case MBEDTLS_ERR_NET_CONN_RESET:
464 case MBEDTLS_ERR_X509_CERT_VERIFY_FAILED:
478 uint32_t verify_res_flags;
484 ret = mbedtls_ssl_handshake(&tls_ctx->
ssl_context);
488 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
496 if ((verify_res_flags = mbedtls_ssl_get_verify_result(&tls_ctx->
ssl_context)) != 0) {
498 "with the certificate verification, returned flags: %"PRIu32
"\n",
500 if (verify_res_flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED)
513 uint32_t verify_res_flags;
515#if defined(MBEDTLS_SSL_DTLS_SRTP)
516 const mbedtls_ssl_srtp_profile
profiles[] = {
517 MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80,
518 MBEDTLS_TLS_SRTP_UNSET
525 }
else if (!shr->
host) {
530#ifdef MBEDTLS_PSA_CRYPTO_C
531 if ((ret = psa_crypto_init()) != PSA_SUCCESS) {
538 mbedtls_ssl_config_init(&tls_ctx->
ssl_config);
541 mbedtls_x509_crt_init(&tls_ctx->
ca_cert);
542 mbedtls_pk_init(&tls_ctx->
priv_key);
554 if ((ret = mbedtls_x509_crt_parse_file(&tls_ctx->
ca_cert, shr->
ca_file)) != 0) {
562 if ((ret = mbedtls_x509_crt_parse_file(&tls_ctx->
own_cert, shr->
cert_file)) != 0) {
575 mbedtls_entropy_func,
584 if ((ret = mbedtls_pk_parse_keyfile(&tls_ctx->
priv_key,
587#
if MBEDTLS_VERSION_MAJOR >= 3
588 , mbedtls_ctr_drbg_random,
596 if ((ret = mbedtls_pk_parse_key(&tls_ctx->
priv_key,
601#
if MBEDTLS_VERSION_MAJOR >= 3
602 , mbedtls_ctr_drbg_random,
621 if ((ret = mbedtls_x509_crt_parse(&tls_ctx->
own_cert, buf,
sizeof(buf))) != 0) {
627 if ((ret = mbedtls_ssl_config_defaults(&tls_ctx->
ssl_config,
628 shr->
listen ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT,
629 shr->
is_dtls ? MBEDTLS_SSL_TRANSPORT_DATAGRAM : MBEDTLS_SSL_TRANSPORT_STREAM,
630 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
635#ifdef MBEDTLS_SSL_PROTO_TLS1_3
637 if (mbedtls_version_get_number() == 0x03060000 && !shr->
verify) {
638 av_log(
h,
AV_LOG_INFO,
"Forcing TLSv1.2 because certificate verification is disabled\n");
639 mbedtls_ssl_conf_max_tls_version(&tls_ctx->
ssl_config, MBEDTLS_SSL_VERSION_TLS1_2);
644 mbedtls_ssl_conf_authmode(&tls_ctx->
ssl_config,
645 shr->
verify ? MBEDTLS_SSL_VERIFY_OPTIONAL : MBEDTLS_SSL_VERIFY_NONE);
657#if defined(MBEDTLS_SSL_DTLS_SRTP)
658 if ((ret = mbedtls_ssl_conf_dtls_srtp_protection_profiles(&tls_ctx->
ssl_config,
profiles)) != 0) {
659 av_log(
h,
AV_LOG_ERROR,
"mbedtls_ssl_conf_dtls_srtp_protection_profiles returned %d\n", ret);
662 mbedtls_ssl_set_export_keys_cb(&tls_ctx->
ssl_context, dtls_srtp_key_derivation, &tls_ctx->
srtp_key);
677 if ((ret = mbedtls_ssl_set_hostname(&tls_ctx->
ssl_context, shr->
host)) != 0) {
687 mbedtls_ssl_set_timer_cb(&tls_ctx->
ssl_context, &tls_ctx->
timer, mbedtls_timing_set_delay, mbedtls_timing_get_delay);
699 if ((verify_res_flags = mbedtls_ssl_get_verify_result(&tls_ctx->
ssl_context)) != 0) {
701 "with the certificate verification, returned flags: %"PRIu32
"\n",
703 if (verify_res_flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED)
727 case MBEDTLS_ERR_SSL_WANT_READ:
728 case MBEDTLS_ERR_SSL_WANT_WRITE:
729#ifdef MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET
730 case MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET:
733 case MBEDTLS_ERR_NET_SEND_FAILED:
734 case MBEDTLS_ERR_NET_RECV_FAILED:
736 case MBEDTLS_ERR_NET_CONN_RESET:
737 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
819 .class_name =
"dtls",
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
int ffurl_get_short_seek(void *urlcontext)
Return the current short seek threshold value for this URL.
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
#define AVIO_FLAG_NONBLOCK
Use non-blocking mode.
void av_bprintf(AVBPrint *buf, const char *fmt,...)
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
int av_random_bytes(uint8_t *buf, size_t len)
Generate cryptographically secure random data, i.e.
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
#define AVERROR_EOF
End of file.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
int av_log_get_level(void)
Get the current log level.
const char * av_basename(const char *path)
Thread safe basename.
#define LIBAVUTIL_VERSION_INT
static const AVProfile profiles[]
Memory handling functions.
int ff_udp_set_remote_addr(URLContext *h, const struct sockaddr *dest_addr, socklen_t dest_addr_len, int do_connect)
This function is identical to ff_udp_set_remote_url, except that it takes a sockaddr directly.
void ff_udp_get_last_recv_addr(URLContext *h, struct sockaddr_storage *addr, socklen_t *addr_len)
const URLProtocol ff_dtls_protocol
const URLProtocol ff_tls_protocol
Describe the class of an AVClass context structure.
mbedtls_ctr_drbg_context ctr_drbg_context
mbedtls_ssl_context ssl_context
mbedtls_ssl_config ssl_config
struct sockaddr_storage dest_addr
mbedtls_timing_delay_context timer
mbedtls_pk_context priv_key
mbedtls_x509_crt own_cert
mbedtls_entropy_context entropy_context
char underlying_host[200]
int mtu
The size of RTP packet, should generally be set to MTU.
mbedtls_tls_prf_types tls_prf_type
unsigned char master_secret[48]
unsigned char randbytes[64]
static AVFormatContext * ctx
int ff_url_read_all(const char *url, AVBPrint *bp)
Read all data from the given URL url and store it in the given buffer bp.
int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AVDictionary **options)
int ff_tls_parse_host(TLSShared *s, char *hostname, int hostname_size, int *port_ptr, const char *uri)
int ff_is_dtls_packet(const uint8_t *buf, int size)
Whether the packet is a DTLS packet, as defined by RFC 5764 Section 5.1.2.
#define MAX_CERTIFICATE_SIZE
Maximum size limit of a certificate and private key size.
#define TLS_COMMON_OPTIONS(pstruct, options_field)
static int tls_handshake(URLContext *h)
static int tls_close(URLContext *h)
static const AVClass tls_class
static int tls_read(URLContext *h, uint8_t *buf, int size)
static int dtls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
static int tls_get_short_seek(URLContext *h)
static int tls_write(URLContext *h, const uint8_t *buf, int size)
static const AVClass dtls_class
static int tls_get_file_handle(URLContext *h)
static int mbedtls_gen_x509_cert(mbedtls_pk_context *key, char *cert_buf, size_t cert_sz)
static int tls_handshake(URLContext *h)
static int tls_close(URLContext *h)
static int mbedtls_x509_fingerprint(char *cert_buf, size_t cert_sz, char **fingerprint)
static void handle_pk_parse_error(URLContext *h, int ret)
int ff_ssl_gen_key_cert(char *key_buf, size_t key_sz, char *cert_buf, size_t cert_sz, char **fingerprint)
static void handle_handshake_error(URLContext *h, int ret)
int ff_ssl_read_key_cert(char *key_url, char *cert_url, char *key_buf, size_t key_sz, char *cert_buf, size_t cert_sz, char **fingerprint)
static int tls_read(URLContext *h, uint8_t *buf, int size)
static int handle_tls_error(URLContext *h, const char *func_name, int ret)
static int dtls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
int ff_dtls_export_materials(URLContext *h, char *dtls_srtp_materials, size_t materials_sz)
static int handle_transport_error(URLContext *h, const char *func_name, int react_on_eagain, int ret)
static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
static int mbedtls_gen_pkey(mbedtls_pk_context *key)
static int tls_get_short_seek(URLContext *h)
static void mbedtls_debug(void *ctx, int lvl, const char *file, int line, const char *msg)
static int tls_write(URLContext *h, const uint8_t *buf, int size)
static int mbedtls_send(void *ctx, const unsigned char *buf, size_t len)
static int mbedtls_recv(void *ctx, unsigned char *buf, size_t len)
static int tls_get_file_handle(URLContext *h)
int ff_tls_set_external_socket(URLContext *h, URLContext *sock)
unbuffered private I/O API
static int ffurl_write(URLContext *h, const uint8_t *buf, int size)
Write size bytes from buf to the resource accessed by h.
static int ffurl_read(URLContext *h, uint8_t *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf.
#define URL_PROTOCOL_FLAG_NETWORK