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>
48 size_t n =
sizeof(
md);
53 mbedtls_x509_crt_init(&crt);
55 if ((
ret = mbedtls_x509_crt_parse(&crt, cert_buf, cert_sz)) != 0) {
56 mbedtls_x509_crt_free(&crt);
60 if ((
ret = mbedtls_sha256(crt.raw.p, crt.raw.len,
md, 0)) != 0) {
61 mbedtls_x509_crt_free(&crt);
67 for (
int i = 0;
i < n - 1;
i++)
74 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)
77 AVBPrint key_bp, cert_bp;
93 if (key_sz < key_bp.size || cert_sz < cert_bp.size) {
100 cert_buf = cert_bp.str;
114 mbedtls_entropy_context entropy;
115 mbedtls_ctr_drbg_context ctr_drbg;
117 mbedtls_entropy_init(&entropy);
118 mbedtls_ctr_drbg_init(&ctr_drbg);
120 if ((
ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
121 &entropy,
NULL, 0)) != 0) {
126 if ((
ret = mbedtls_pk_setup(
key,
127 mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY))) != 0) {
137 if ((
ret = mbedtls_ecp_gen_key(MBEDTLS_ECP_DP_SECP256R1,
139 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
144 mbedtls_entropy_free(&entropy);
145 mbedtls_ctr_drbg_free(&ctr_drbg);
152 const char *
name =
"CN=lavf";
155 char not_before[16], not_after[16];
156 unsigned char serial[20];
157 mbedtls_entropy_context entropy;
158 mbedtls_ctr_drbg_context ctr_drbg;
159 mbedtls_x509write_cert crt;
161 mbedtls_entropy_init(&entropy);
162 mbedtls_ctr_drbg_init(&ctr_drbg);
163 mbedtls_x509write_crt_init(&crt);
165 if ((
ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
NULL, 0)) != 0) {
170 mbedtls_x509write_crt_set_subject_key(&crt,
key);
171 mbedtls_x509write_crt_set_issuer_key(&crt,
key);
172 if ((
ret = mbedtls_x509write_crt_set_subject_name(&crt,
name)) != 0) {
177 if ((
ret = mbedtls_x509write_crt_set_issuer_name(&crt,
name)) != 0) {
181 mbedtls_x509write_crt_set_version(&crt, MBEDTLS_X509_CRT_VERSION_3);
182 mbedtls_x509write_crt_set_md_alg(&crt, MBEDTLS_MD_SHA256);
190 if ((
ret = mbedtls_x509write_crt_set_serial_raw(&crt, serial,
sizeof(serial))) != 0) {
197 strftime(not_before,
sizeof(not_before),
"%Y%m%d%H%M%S", &tm);
199 strftime(not_after,
sizeof(not_after),
"%Y%m%d%H%M%S", &tm);
201 if ((
ret = mbedtls_x509write_crt_set_validity(&crt, not_before, not_after)) != 0) {
206 if ((
ret = mbedtls_x509write_crt_pem(&crt, cert_buf, cert_sz,
207 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
213 mbedtls_entropy_free(&entropy);
214 mbedtls_ctr_drbg_free(&ctr_drbg);
215 mbedtls_x509write_crt_free(&crt);
222 mbedtls_pk_context
key;
224 mbedtls_pk_init(&
key);
229 if ((
ret = mbedtls_pk_write_key_pem(&
key, key_buf, key_sz)) != 0)
240 mbedtls_pk_free(&
key);
279 #if defined(MBEDTLS_SSL_DTLS_SRTP)
280 static void dtls_srtp_key_derivation(
void *p_expkey,
281 mbedtls_ssl_key_export_type secret_type,
282 const unsigned char *secret,
284 const unsigned char client_random[32],
285 const unsigned char server_random[32],
286 mbedtls_tls_prf_types tls_prf_type)
294 memcpy(keys->
randbytes, client_random, 32);
295 memcpy(keys->
randbytes + 32, server_random, 32);
304 #if defined(MBEDTLS_SSL_DTLS_SRTP)
305 const char*
dst =
"EXTRACTOR-dtls_srtp";
306 mbedtls_dtls_srtp_info dtls_srtp_negotiation_result;
307 mbedtls_ssl_get_dtls_srtp_negotiation_result(&tls_ctx->
ssl_context, &dtls_srtp_negotiation_result);
316 materials_sz)) != 0) {
327 #define OFFSET(x) offsetof(TLSContext, x)
335 mbedtls_pk_free(&tls_ctx->
priv_key);
336 mbedtls_x509_crt_free(&tls_ctx->
ca_cert);
337 mbedtls_x509_crt_free(&tls_ctx->
own_cert);
339 mbedtls_ssl_config_free(&tls_ctx->
ssl_config);
351 return react_on_eagain;
356 return MBEDTLS_ERR_NET_CONN_RESET;
360 return MBEDTLS_ERR_NET_SEND_FAILED;
373 if (
h->max_packet_size &&
len >
h->max_packet_size)
374 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
395 av_log(tls_ctx,
AV_LOG_TRACE,
"Set UDP remote addr on UDP socket, now 'connected'\n");
399 if (
h->max_packet_size &&
len >
h->max_packet_size)
400 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
415 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
416 av_log(
h,
AV_LOG_ERROR,
"Read of key file failed. Is it actually there, are the access permissions correct?\n");
418 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
421 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
433 #if MBEDTLS_VERSION_MAJOR < 3
434 case MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE:
435 av_log(
h,
AV_LOG_ERROR,
"None of the common ciphersuites is usable. Was the local certificate correctly set?\n");
438 case MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE:
441 case MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION:
445 case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
446 av_log(
h,
AV_LOG_ERROR,
"A fatal alert message was received from the peer, has the peer a correct certificate?\n");
448 case MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED:
449 av_log(
h,
AV_LOG_ERROR,
"No CA chain is set, but required to operate. Was the CA correctly set?\n");
451 case MBEDTLS_ERR_SSL_INTERNAL_ERROR:
454 case MBEDTLS_ERR_NET_CONN_RESET:
457 case MBEDTLS_ERR_X509_CERT_VERIFY_FAILED:
480 if (
ret != MBEDTLS_ERR_SSL_WANT_READ &&
ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
493 uint32_t verify_res_flags;
495 #if defined(MBEDTLS_SSL_DTLS_SRTP)
496 const mbedtls_ssl_srtp_profile
profiles[] = {
497 MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80,
498 MBEDTLS_TLS_SRTP_UNSET
507 #ifdef MBEDTLS_PSA_CRYPTO_C
508 if ((
ret = psa_crypto_init()) != PSA_SUCCESS) {
515 mbedtls_ssl_config_init(&tls_ctx->
ssl_config);
518 mbedtls_x509_crt_init(&tls_ctx->
ca_cert);
519 mbedtls_pk_init(&tls_ctx->
priv_key);
552 mbedtls_entropy_func,
561 if ((
ret = mbedtls_pk_parse_keyfile(&tls_ctx->
priv_key,
564 #
if MBEDTLS_VERSION_MAJOR >= 3
565 , mbedtls_ctr_drbg_random,
573 if ((
ret = mbedtls_pk_parse_key(&tls_ctx->
priv_key,
578 #
if MBEDTLS_VERSION_MAJOR >= 3
579 , mbedtls_ctr_drbg_random,
598 if ((
ret = mbedtls_x509_crt_parse(&tls_ctx->
own_cert, buf,
sizeof(buf))) != 0) {
605 shr->
listen ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT,
606 shr->
is_dtls ? MBEDTLS_SSL_TRANSPORT_DATAGRAM : MBEDTLS_SSL_TRANSPORT_STREAM,
607 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
612 #ifdef MBEDTLS_SSL_PROTO_TLS1_3
614 if (mbedtls_version_get_number() == 0x03060000 && !shr->
verify) {
615 av_log(
h,
AV_LOG_INFO,
"Forcing TLSv1.2 because certificate verification is disabled\n");
616 mbedtls_ssl_conf_max_tls_version(&tls_ctx->
ssl_config, MBEDTLS_SSL_VERSION_TLS1_2);
621 mbedtls_ssl_conf_authmode(&tls_ctx->
ssl_config,
622 shr->
verify ? MBEDTLS_SSL_VERIFY_OPTIONAL : MBEDTLS_SSL_VERIFY_NONE);
634 #if defined(MBEDTLS_SSL_DTLS_SRTP)
639 mbedtls_ssl_set_export_keys_cb(&tls_ctx->
ssl_context, dtls_srtp_key_derivation, &tls_ctx->
srtp_key);
664 mbedtls_ssl_set_timer_cb(&tls_ctx->
ssl_context, &tls_ctx->
timer, mbedtls_timing_set_delay, mbedtls_timing_get_delay);
676 if ((verify_res_flags = mbedtls_ssl_get_verify_result(&tls_ctx->
ssl_context)) != 0) {
678 "with the certificate verification, returned flags: %"PRIu32
"\n",
680 if (verify_res_flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED)
704 case MBEDTLS_ERR_SSL_WANT_READ:
705 case MBEDTLS_ERR_SSL_WANT_WRITE:
706 #ifdef MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET
707 case MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET:
710 case MBEDTLS_ERR_NET_SEND_FAILED:
711 case MBEDTLS_ERR_NET_RECV_FAILED:
713 case MBEDTLS_ERR_NET_CONN_RESET:
714 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: