FFmpeg
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
tls.h
Go to the documentation of this file.
1 /*
2  * TLS/DTLS/SSL Protocol
3  * Copyright (c) 2011 Martin Storsjo
4  * Copyright (c) 2025 Jack Lau
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #ifndef AVFORMAT_TLS_H
24 #define AVFORMAT_TLS_H
25 
26 #include "libavutil/bprint.h"
27 #include "libavutil/opt.h"
28 
29 #include "url.h"
30 
31 /**
32  * Maximum size limit of a certificate and private key size.
33  */
34 #define MAX_CERTIFICATE_SIZE 8192
35 
36 enum DTLSState {
38 
39  /* Whether DTLS handshake is finished. */
41  /* Whether DTLS session is closed. */
43  /* Whether DTLS handshake is failed. */
45 };
46 
47 typedef struct TLSShared {
48  char *ca_file;
49  int verify;
50  char *cert_file;
51  char *key_file;
52  int listen;
53 
54  char *host;
55  char *http_proxy;
56 
57  char underlying_host[200];
59 
61 
62  int is_dtls;
63 
65 
68 
69  /* The fingerprint of certificate, used in SDP offer. */
70  char *fingerprint;
71 
72  /* The certificate and private key content used for DTLS handshake */
73  char* cert_buf;
74  char* key_buf;
75  /**
76  * The size of RTP packet, should generally be set to MTU.
77  * Note that pion requires a smaller value, for example, 1200.
78  */
79  int mtu;
80 } TLSShared;
81 
82 #define TLS_OPTFL (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
83 #define TLS_COMMON_OPTIONS(pstruct, options_field) \
84  {"ca_file", "Certificate Authority database file", offsetof(pstruct, options_field . ca_file), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \
85  {"cafile", "Certificate Authority database file", offsetof(pstruct, options_field . ca_file), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \
86  {"tls_verify", "Verify the peer certificate", offsetof(pstruct, options_field . verify), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = TLS_OPTFL }, \
87  {"cert_file", "Certificate file", offsetof(pstruct, options_field . cert_file), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \
88  {"key_file", "Private key file", offsetof(pstruct, options_field . key_file), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \
89  {"listen", "Listen for incoming connections", offsetof(pstruct, options_field . listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = TLS_OPTFL }, \
90  {"verifyhost", "Verify against a specific hostname", offsetof(pstruct, options_field . host), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \
91  {"http_proxy", "Set proxy to tunnel through", offsetof(pstruct, options_field . http_proxy), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \
92  {"use_external_udp", "Use external UDP from muxer or demuxer", offsetof(pstruct, options_field . use_external_udp), AV_OPT_TYPE_INT, { .i64 = 0}, 0, 1, .flags = TLS_OPTFL }, \
93  {"mtu", "Maximum Transmission Unit", offsetof(pstruct, options_field . mtu), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, .flags = TLS_OPTFL}, \
94  {"fingerprint", "The optional fingerprint for DTLS", offsetof(pstruct, options_field . fingerprint), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL}, \
95  {"cert_buf", "The optional certificate buffer for DTLS", offsetof(pstruct, options_field . cert_buf), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL}, \
96  {"key_buf", "The optional private key buffer for DTLS", offsetof(pstruct, options_field . key_buf), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL}
97 
98 int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AVDictionary **options);
99 
100 int ff_url_read_all(const char *url, AVBPrint *bp);
101 
103 
104 int ff_dtls_export_materials(URLContext *h, char *dtls_srtp_materials, size_t materials_sz);
105 
107 
108 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);
109 
110 int ff_ssl_gen_key_cert(char *key_buf, size_t key_sz, char *cert_buf, size_t cert_sz, char **fingerprint);
111 
112 void ff_gnutls_init(void);
113 void ff_gnutls_deinit(void);
114 
115 int ff_openssl_init(void);
116 void ff_openssl_deinit(void);
117 
118 #endif /* AVFORMAT_TLS_H */
opt.h
ff_dtls_state
int ff_dtls_state(URLContext *h)
Definition: tls_openssl.c:527
ff_gnutls_init
void ff_gnutls_init(void)
Definition: tls_gnutls.c:55
AVDictionary
Definition: dict.c:32
TLSShared::state
enum DTLSState state
Definition: tls.h:64
TLSShared::fingerprint
char * fingerprint
Definition: tls.h:70
TLSShared::verify
int verify
Definition: tls.h:49
TLSShared::listen
int listen
Definition: tls.h:52
ff_gnutls_deinit
void ff_gnutls_deinit(void)
Definition: tls_gnutls.c:66
ff_openssl_deinit
void ff_openssl_deinit(void)
Definition: tls_openssl.c:595
DTLS_STATE_NONE
@ DTLS_STATE_NONE
Definition: tls.h:37
ff_openssl_init
int ff_openssl_init(void)
Definition: tls_openssl.c:565
DTLSState
DTLSState
Definition: tls.h:36
TLSShared::underlying_host
char underlying_host[200]
Definition: tls.h:57
DTLS_STATE_CLOSED
@ DTLS_STATE_CLOSED
Definition: tls.h:42
options
Definition: swscale.c:43
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
ff_dtls_set_udp
int ff_dtls_set_udp(URLContext *h, URLContext *udp)
Definition: tls_openssl.c:505
TLSShared::host
char * host
Definition: tls.h:54
TLSShared::cert_buf
char * cert_buf
Definition: tls.h:73
ff_url_read_all
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.
Definition: tls.c:153
TLSShared::http_proxy
char * http_proxy
Definition: tls.h:55
TLSShared::key_buf
char * key_buf
Definition: tls.h:74
TLSShared::use_external_udp
int use_external_udp
Definition: tls.h:66
bprint.h
URLContext
Definition: url.h:35
DTLS_STATE_FAILED
@ DTLS_STATE_FAILED
Definition: tls.h:44
url.h
TLSShared::cert_file
char * cert_file
Definition: tls.h:50
TLSShared::is_dtls
int is_dtls
Definition: tls.h:62
TLSShared::ca_file
char * ca_file
Definition: tls.h:48
ff_dtls_export_materials
int ff_dtls_export_materials(URLContext *h, char *dtls_srtp_materials, size_t materials_sz)
Definition: tls_openssl.c:512
TLSShared::key_file
char * key_file
Definition: tls.h:51
ff_ssl_read_key_cert
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)
Definition: tls_openssl.c:159
TLSShared::mtu
int mtu
The size of RTP packet, should generally be set to MTU.
Definition: tls.h:79
TLSShared
Definition: tls.h:47
ff_ssl_gen_key_cert
int ff_ssl_gen_key_cert(char *key_buf, size_t key_sz, char *cert_buf, size_t cert_sz, char **fingerprint)
Definition: tls_openssl.c:381
TLSShared::udp
URLContext * udp
Definition: tls.h:67
TLSShared::numerichost
int numerichost
Definition: tls.h:58
h
h
Definition: vp9dsp_template.c:2070
DTLS_STATE_FINISHED
@ DTLS_STATE_FINISHED
Definition: tls.h:40
TLSShared::tcp
URLContext * tcp
Definition: tls.h:60
ff_tls_open_underlying
int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AVDictionary **options)
Definition: tls.c:69