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++)
76 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)
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)
282 static 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:
487 if (
ret != MBEDTLS_ERR_SSL_WANT_READ &&
ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
500 uint32_t verify_res_flags;
502 #if defined(MBEDTLS_SSL_DTLS_SRTP)
503 const mbedtls_ssl_srtp_profile
profiles[] = {
504 MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80,
505 MBEDTLS_TLS_SRTP_UNSET
514 #ifdef MBEDTLS_PSA_CRYPTO_C
515 if ((
ret = psa_crypto_init()) != PSA_SUCCESS) {
522 mbedtls_ssl_config_init(&tls_ctx->
ssl_config);
525 mbedtls_x509_crt_init(&tls_ctx->
ca_cert);
526 mbedtls_pk_init(&tls_ctx->
priv_key);
559 mbedtls_entropy_func,
568 if ((
ret = mbedtls_pk_parse_keyfile(&tls_ctx->
priv_key,
571 #
if MBEDTLS_VERSION_MAJOR >= 3
572 , mbedtls_ctr_drbg_random,
580 if ((
ret = mbedtls_pk_parse_key(&tls_ctx->
priv_key,
585 #
if MBEDTLS_VERSION_MAJOR >= 3
586 , mbedtls_ctr_drbg_random,
605 if ((
ret = mbedtls_x509_crt_parse(&tls_ctx->
own_cert, buf,
sizeof(buf))) != 0) {
612 shr->
listen ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT,
613 shr->
is_dtls ? MBEDTLS_SSL_TRANSPORT_DATAGRAM : MBEDTLS_SSL_TRANSPORT_STREAM,
614 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
619 #ifdef MBEDTLS_SSL_PROTO_TLS1_3
621 if (mbedtls_version_get_number() == 0x03060000 && !shr->
verify) {
622 av_log(
h,
AV_LOG_INFO,
"Forcing TLSv1.2 because certificate verification is disabled\n");
623 mbedtls_ssl_conf_max_tls_version(&tls_ctx->
ssl_config, MBEDTLS_SSL_VERSION_TLS1_2);
628 mbedtls_ssl_conf_authmode(&tls_ctx->
ssl_config,
629 shr->
verify ? MBEDTLS_SSL_VERIFY_OPTIONAL : MBEDTLS_SSL_VERIFY_NONE);
641 #if defined(MBEDTLS_SSL_DTLS_SRTP)
646 mbedtls_ssl_set_export_keys_cb(&tls_ctx->
ssl_context, dtls_srtp_key_derivation, &tls_ctx->
srtp_key);
671 mbedtls_ssl_set_timer_cb(&tls_ctx->
ssl_context, &tls_ctx->
timer, mbedtls_timing_set_delay, mbedtls_timing_get_delay);
683 if ((verify_res_flags = mbedtls_ssl_get_verify_result(&tls_ctx->
ssl_context)) != 0) {
685 "with the certificate verification, returned flags: %"PRIu32
"\n",
687 if (verify_res_flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED)
711 case MBEDTLS_ERR_SSL_WANT_READ:
712 case MBEDTLS_ERR_SSL_WANT_WRITE:
713 #ifdef MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET
714 case MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET:
717 case MBEDTLS_ERR_NET_SEND_FAILED:
718 case MBEDTLS_ERR_NET_RECV_FAILED:
720 case MBEDTLS_ERR_NET_CONN_RESET:
721 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: