2 * TLS/DTLS/SSL Protocol
3 * Copyright (c) 2011 Martin Storsjo
4 * Copyright (c) 2025 Jack Lau
6 * This file is part of FFmpeg.
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.
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.
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
23 #ifndef AVFORMAT_TLS_H
24 #define AVFORMAT_TLS_H
26 #include "libavutil/bprint.h"
27 #include "libavutil/opt.h"
33 * Maximum size limit of a certificate and private key size.
35 #define MAX_CERTIFICATE_SIZE 8192
37 typedef struct TLSShared
{
48 char underlying_host
[200];
58 /* The certificate and private key content used for DTLS handshake */
63 * The size of RTP packet, should generally be set to MTU.
64 * Note that pion requires a smaller value, for example, 1200.
69 #define TLS_OPTFL (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
71 #if FF_API_NO_DEFAULT_TLS_VERIFY
72 #define TLS_VERIFY_DEFAULT 0
74 #define TLS_VERIFY_DEFAULT 1
77 #define FF_TLS_CLIENT_OPTIONS(pstruct, options_field) \
78 {"ca_file", "Certificate Authority database file", offsetof(pstruct, options_field . ca_file), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \
79 {"cafile", "Certificate Authority database file", offsetof(pstruct, options_field . ca_file), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \
80 {"tls_verify", "Verify the peer certificate", offsetof(pstruct, options_field . verify), AV_OPT_TYPE_BOOL, { .i64 = TLS_VERIFY_DEFAULT }, 0, 1, .flags = TLS_OPTFL }, \
81 {"verify", "Verify the peer certificate", offsetof(pstruct, options_field . verify), AV_OPT_TYPE_BOOL, { .i64 = TLS_VERIFY_DEFAULT }, 0, 1, .flags = TLS_OPTFL }, \
82 {"cert_file", "Certificate file", offsetof(pstruct, options_field . cert_file), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \
83 {"cert", "Certificate file", offsetof(pstruct, options_field . cert_file), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \
84 {"key_file", "Private key file", offsetof(pstruct, options_field . key_file), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \
85 {"key", "Private key file", offsetof(pstruct, options_field . key_file), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \
86 {"verifyhost", "Verify against a specific hostname", offsetof(pstruct, options_field . host), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }
88 #define TLS_COMMON_OPTIONS(pstruct, options_field) \
89 {"listen", "Listen for incoming connections", offsetof(pstruct, options_field . listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = TLS_OPTFL }, \
90 {"http_proxy", "Set proxy to tunnel through", offsetof(pstruct, options_field . http_proxy), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \
91 {"external_sock", "Use external socket", offsetof(pstruct, options_field . external_sock), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = TLS_OPTFL }, \
92 {"use_srtp", "Enable use_srtp DTLS extension", offsetof(pstruct, options_field . use_srtp), AV_OPT_TYPE_BOOL, { .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 {"cert_pem", "Certificate PEM string", offsetof(pstruct, options_field . cert_buf), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \
95 {"key_pem", "Private key PEM string", offsetof(pstruct, options_field . key_buf), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \
96 FF_TLS_CLIENT_OPTIONS(pstruct, options_field)
98 int ff_tls_open_underlying(TLSShared
*c
, URLContext
*parent
, const char *uri
, AVDictionary
**options
);
100 int ff_url_read_all(const char *url
, AVBPrint
*bp
);
102 int ff_tls_set_external_socket(URLContext
*h
, URLContext
*sock
);
104 int ff_dtls_export_materials(URLContext
*h
, char *dtls_srtp_materials
, size_t materials_sz
);
106 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
);
108 int ff_ssl_gen_key_cert(char *key_buf
, size_t key_sz
, char *cert_buf
, size_t cert_sz
, char **fingerprint
);
110 void ff_gnutls_init(void);
111 void ff_gnutls_deinit(void);
113 int ff_openssl_init(void);
114 void ff_openssl_deinit(void);
116 #endif /* AVFORMAT_TLS_H */