FFmpeg
tls_mbedtls.c
Go to the documentation of this file.
1 /*
2  * TLS/SSL Protocol
3  * Copyright (c) 2018 Thomas Volkert
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
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 
30 #include "avformat.h"
31 #include "internal.h"
32 #include "url.h"
33 #include "tls.h"
34 #include "libavutil/parseutils.h"
35 
36 typedef struct TLSContext {
37  const AVClass *class;
39  mbedtls_ssl_context ssl_context;
40  mbedtls_ssl_config ssl_config;
41  mbedtls_entropy_context entropy_context;
42  mbedtls_ctr_drbg_context ctr_drbg_context;
43  mbedtls_x509_crt ca_cert;
44  mbedtls_x509_crt own_cert;
45  mbedtls_pk_context priv_key;
46  char *priv_key_pw;
47 } TLSContext;
48 
49 #define OFFSET(x) offsetof(TLSContext, x)
50 
51 static int tls_close(URLContext *h)
52 {
53  TLSContext *tls_ctx = h->priv_data;
54 
55  mbedtls_ssl_close_notify(&tls_ctx->ssl_context);
56  mbedtls_pk_free(&tls_ctx->priv_key);
57  mbedtls_x509_crt_free(&tls_ctx->ca_cert);
58  mbedtls_x509_crt_free(&tls_ctx->own_cert);
59  mbedtls_ssl_free(&tls_ctx->ssl_context);
60  mbedtls_ssl_config_free(&tls_ctx->ssl_config);
61  mbedtls_ctr_drbg_free(&tls_ctx->ctr_drbg_context);
62  mbedtls_entropy_free(&tls_ctx->entropy_context);
63 
64  return 0;
65 }
66 
67 static int handle_transport_error(URLContext *h, const char* func_name, int react_on_eagain, int ret)
68 {
69  switch (ret) {
70  case AVERROR(EAGAIN):
71  return react_on_eagain;
72  case AVERROR_EXIT:
73  return 0;
74  case AVERROR(EPIPE):
75  case AVERROR(ECONNRESET):
76  return MBEDTLS_ERR_NET_CONN_RESET;
77  default:
78  av_log(h, AV_LOG_ERROR, "%s returned 0x%x\n", func_name, ret);
79  errno = EIO;
80  return MBEDTLS_ERR_NET_SEND_FAILED;
81  }
82 }
83 
84 static int mbedtls_send(void *ctx, const unsigned char *buf, size_t len)
85 {
86  URLContext *h = (URLContext*) ctx;
87  int ret = ffurl_write(h, buf, len);
88  if (ret >= 0)
89  return ret;
90 
91  if (h->max_packet_size && len > h->max_packet_size)
92  return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
93 
94  return handle_transport_error(h, "ffurl_write", MBEDTLS_ERR_SSL_WANT_WRITE, ret);
95 }
96 
97 static int mbedtls_recv(void *ctx, unsigned char *buf, size_t len)
98 {
99  URLContext *h = (URLContext*) ctx;
100  int ret = ffurl_read(h, buf, len);
101  if (ret >= 0)
102  return ret;
103 
104  if (h->max_packet_size && len > h->max_packet_size)
105  return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
106 
107  return handle_transport_error(h, "ffurl_read", MBEDTLS_ERR_SSL_WANT_READ, ret);
108 }
109 
111 {
112  switch (ret) {
113  case MBEDTLS_ERR_PK_FILE_IO_ERROR:
114  av_log(h, AV_LOG_ERROR, "Read of key file failed. Is it actually there, are the access permissions correct?\n");
115  break;
116  case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
117  av_log(h, AV_LOG_ERROR, "A password for the private key is missing.\n");
118  break;
119  case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
120  av_log(h, AV_LOG_ERROR, "The given password for the private key is wrong.\n");
121  break;
122  default:
123  av_log(h, AV_LOG_ERROR, "mbedtls_pk_parse_key returned -0x%x\n", -ret);
124  break;
125  }
126 }
127 
129 {
130  switch (ret) {
131 #if MBEDTLS_VERSION_MAJOR < 3
132  case MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE:
133  av_log(h, AV_LOG_ERROR, "None of the common ciphersuites is usable. Was the local certificate correctly set?\n");
134  break;
135 #else
136  case MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE:
137  av_log(h, AV_LOG_ERROR, "TLS handshake failed.\n");
138  break;
139 #endif
140  case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
141  av_log(h, AV_LOG_ERROR, "A fatal alert message was received from the peer, has the peer a correct certificate?\n");
142  break;
143  case MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED:
144  av_log(h, AV_LOG_ERROR, "No CA chain is set, but required to operate. Was the CA correctly set?\n");
145  break;
146  case MBEDTLS_ERR_NET_CONN_RESET:
147  av_log(h, AV_LOG_ERROR, "TLS handshake was aborted by peer.\n");
148  break;
149  default:
150  av_log(h, AV_LOG_ERROR, "mbedtls_ssl_handshake returned -0x%x\n", -ret);
151  break;
152  }
153 }
154 
155 static void parse_options(TLSContext *tls_ctxc, const char *uri)
156 {
157  char buf[1024];
158  const char *p = strchr(uri, '?');
159  if (!p)
160  return;
161 
162  if (!tls_ctxc->priv_key_pw && av_find_info_tag(buf, sizeof(buf), "key_password", p))
163  tls_ctxc->priv_key_pw = av_strdup(buf);
164 }
165 
166 static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
167 {
168  TLSContext *tls_ctx = h->priv_data;
169  TLSShared *shr = &tls_ctx->tls_shared;
170  uint32_t verify_res_flags;
171  int ret;
172 
173  // parse additional options
174  parse_options(tls_ctx, uri);
175 
176  if ((ret = ff_tls_open_underlying(shr, h, uri, options)) < 0)
177  goto fail;
178 
179  mbedtls_ssl_init(&tls_ctx->ssl_context);
180  mbedtls_ssl_config_init(&tls_ctx->ssl_config);
181  mbedtls_entropy_init(&tls_ctx->entropy_context);
182  mbedtls_ctr_drbg_init(&tls_ctx->ctr_drbg_context);
183  mbedtls_x509_crt_init(&tls_ctx->ca_cert);
184  mbedtls_pk_init(&tls_ctx->priv_key);
185 
186  // load trusted CA
187  if (shr->ca_file) {
188  if ((ret = mbedtls_x509_crt_parse_file(&tls_ctx->ca_cert, shr->ca_file)) != 0) {
189  av_log(h, AV_LOG_ERROR, "mbedtls_x509_crt_parse_file for CA cert returned %d\n", ret);
190  goto fail;
191  }
192  }
193 
194  // load own certificate
195  if (shr->cert_file) {
196  if ((ret = mbedtls_x509_crt_parse_file(&tls_ctx->own_cert, shr->cert_file)) != 0) {
197  av_log(h, AV_LOG_ERROR, "mbedtls_x509_crt_parse_file for own cert returned %d\n", ret);
198  goto fail;
199  }
200  }
201 
202  // seed the random number generator
203  if ((ret = mbedtls_ctr_drbg_seed(&tls_ctx->ctr_drbg_context,
204  mbedtls_entropy_func,
205  &tls_ctx->entropy_context,
206  NULL, 0)) != 0) {
207  av_log(h, AV_LOG_ERROR, "mbedtls_ctr_drbg_seed returned %d\n", ret);
208  goto fail;
209  }
210 
211  // load key file
212  if (shr->key_file) {
213  if ((ret = mbedtls_pk_parse_keyfile(&tls_ctx->priv_key,
214  shr->key_file,
215  tls_ctx->priv_key_pw
216 #if MBEDTLS_VERSION_MAJOR >= 3
217  , mbedtls_ctr_drbg_random,
218  &tls_ctx->ctr_drbg_context
219 #endif
220  )) != 0) {
222  goto fail;
223  }
224  }
225 
226  if ((ret = mbedtls_ssl_config_defaults(&tls_ctx->ssl_config,
227  shr->listen ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT,
228  MBEDTLS_SSL_TRANSPORT_STREAM,
229  MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
230  av_log(h, AV_LOG_ERROR, "mbedtls_ssl_config_defaults returned %d\n", ret);
231  goto fail;
232  }
233 
234  mbedtls_ssl_conf_authmode(&tls_ctx->ssl_config,
235  shr->ca_file ? MBEDTLS_SSL_VERIFY_REQUIRED : MBEDTLS_SSL_VERIFY_NONE);
236  mbedtls_ssl_conf_rng(&tls_ctx->ssl_config, mbedtls_ctr_drbg_random, &tls_ctx->ctr_drbg_context);
237  mbedtls_ssl_conf_ca_chain(&tls_ctx->ssl_config, &tls_ctx->ca_cert, NULL);
238 
239  // set own certificate and private key
240  if ((ret = mbedtls_ssl_conf_own_cert(&tls_ctx->ssl_config, &tls_ctx->own_cert, &tls_ctx->priv_key)) != 0) {
241  av_log(h, AV_LOG_ERROR, "mbedtls_ssl_conf_own_cert returned %d\n", ret);
242  goto fail;
243  }
244 
245  if ((ret = mbedtls_ssl_setup(&tls_ctx->ssl_context, &tls_ctx->ssl_config)) != 0) {
246  av_log(h, AV_LOG_ERROR, "mbedtls_ssl_setup returned %d\n", ret);
247  goto fail;
248  }
249 
250  if (!shr->listen && !shr->numerichost) {
251  if ((ret = mbedtls_ssl_set_hostname(&tls_ctx->ssl_context, shr->host)) != 0) {
252  av_log(h, AV_LOG_ERROR, "mbedtls_ssl_set_hostname returned %d\n", ret);
253  goto fail;
254  }
255  }
256 
257  // set I/O functions to use FFmpeg internal code for transport layer
258  mbedtls_ssl_set_bio(&tls_ctx->ssl_context, shr->tcp, mbedtls_send, mbedtls_recv, NULL);
259 
260  // ssl handshake
261  while ((ret = mbedtls_ssl_handshake(&tls_ctx->ssl_context)) != 0) {
262  if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
264  goto fail;
265  }
266  }
267 
268  if (shr->verify) {
269  // check the result of the certificate verification
270  if ((verify_res_flags = mbedtls_ssl_get_verify_result(&tls_ctx->ssl_context)) != 0) {
271  av_log(h, AV_LOG_ERROR, "mbedtls_ssl_get_verify_result reported problems "\
272  "with the certificate verification, returned flags: %u\n",
273  verify_res_flags);
274  if (verify_res_flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED)
275  av_log(h, AV_LOG_ERROR, "The certificate is not correctly signed by the trusted CA.\n");
276  goto fail;
277  }
278  }
279 
280  return 0;
281 
282 fail:
283  tls_close(h);
284  return AVERROR(EIO);
285 }
286 
287 static int handle_tls_error(URLContext *h, const char* func_name, int ret)
288 {
289  switch (ret) {
290  case MBEDTLS_ERR_SSL_WANT_READ:
291  case MBEDTLS_ERR_SSL_WANT_WRITE:
292  return AVERROR(EAGAIN);
293  case MBEDTLS_ERR_NET_SEND_FAILED:
294  case MBEDTLS_ERR_NET_RECV_FAILED:
295  return AVERROR(EIO);
296  case MBEDTLS_ERR_NET_CONN_RESET:
297  case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
298  av_log(h, AV_LOG_WARNING, "%s reported connection reset by peer\n", func_name);
299  return AVERROR_EOF;
300  default:
301  av_log(h, AV_LOG_ERROR, "%s returned -0x%x\n", func_name, -ret);
302  return AVERROR(EIO);
303  }
304 }
305 
306 static int tls_read(URLContext *h, uint8_t *buf, int size)
307 {
308  TLSContext *tls_ctx = h->priv_data;
309  int ret;
310 
311  if ((ret = mbedtls_ssl_read(&tls_ctx->ssl_context, buf, size)) > 0) {
312  // return read length
313  return ret;
314  }
315 
316  return handle_tls_error(h, "mbedtls_ssl_read", ret);
317 }
318 
319 static int tls_write(URLContext *h, const uint8_t *buf, int size)
320 {
321  TLSContext *tls_ctx = h->priv_data;
322  int ret;
323 
324  if ((ret = mbedtls_ssl_write(&tls_ctx->ssl_context, buf, size)) > 0) {
325  // return written length
326  return ret;
327  }
328 
329  return handle_tls_error(h, "mbedtls_ssl_write", ret);
330 }
331 
333 {
334  TLSContext *c = h->priv_data;
335  return ffurl_get_file_handle(c->tls_shared.tcp);
336 }
337 
338 static const AVOption options[] = {
339  TLS_COMMON_OPTIONS(TLSContext, tls_shared), \
340  {"key_password", "Password for the private key file", OFFSET(priv_key_pw), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \
341  { NULL }
342 };
343 
344 static const AVClass tls_class = {
345  .class_name = "tls",
346  .item_name = av_default_item_name,
347  .option = options,
348  .version = LIBAVUTIL_VERSION_INT,
349 };
350 
352  .name = "tls",
353  .url_open2 = tls_open,
354  .url_read = tls_read,
355  .url_write = tls_write,
356  .url_close = tls_close,
357  .url_get_file_handle = tls_get_file_handle,
358  .priv_data_size = sizeof(TLSContext),
360  .priv_data_class = &tls_class,
361 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
TLSContext
Definition: tls_gnutls.c:48
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
av_find_info_tag
int av_find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
Attempt to find a specific tag in a URL.
Definition: parseutils.c:751
URL_PROTOCOL_FLAG_NETWORK
#define URL_PROTOCOL_FLAG_NETWORK
Definition: url.h:34
TLSContext::entropy_context
mbedtls_entropy_context entropy_context
Definition: tls_mbedtls.c:41
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
handle_pk_parse_error
static void handle_pk_parse_error(URLContext *h, int ret)
Definition: tls_mbedtls.c:110
AVOption
AVOption.
Definition: opt.h:246
AVDictionary
Definition: dict.c:30
URLProtocol
Definition: url.h:54
TLSShared::verify
int verify
Definition: tls.h:31
TLSShared::listen
int listen
Definition: tls.h:34
TLSContext::ctr_drbg_context
mbedtls_ctr_drbg_context ctr_drbg_context
Definition: tls_mbedtls.c:42
TLS_COMMON_OPTIONS
#define TLS_COMMON_OPTIONS(pstruct, options_field)
Definition: tls.h:45
fail
#define fail()
Definition: checkasm.h:120
TLSContext::ca_cert
mbedtls_x509_crt ca_cert
Definition: tls_mbedtls.c:43
tls_close
static int tls_close(URLContext *h)
Definition: tls_mbedtls.c:51
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
mbedtls_recv
static int mbedtls_recv(void *ctx, unsigned char *buf, size_t len)
Definition: tls_mbedtls.c:97
buf
void * buf
Definition: avisynth_c.h:766
TLS_OPTFL
#define TLS_OPTFL
Definition: tls.h:44
TLSContext::priv_key
mbedtls_pk_context priv_key
Definition: tls_mbedtls.c:45
ctx
AVFormatContext * ctx
Definition: movenc.c:48
TLSContext::ssl_context
mbedtls_ssl_context ssl_context
Definition: tls_mbedtls.c:39
internal.h
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
parse_options
static void parse_options(TLSContext *tls_ctxc, const char *uri)
Definition: tls_mbedtls.c:155
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
parseutils.h
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
TLSShared::host
char * host
Definition: tls.h:36
OFFSET
#define OFFSET(x)
Definition: tls_mbedtls.c:49
ff_tls_protocol
const URLProtocol ff_tls_protocol
Definition: tls_mbedtls.c:351
TLSContext::ssl_config
mbedtls_ssl_config ssl_config
Definition: tls_mbedtls.c:40
mbedtls_send
static int mbedtls_send(void *ctx, const unsigned char *buf, size_t len)
Definition: tls_mbedtls.c:84
size
int size
Definition: twinvq_data.h:11134
handle_handshake_error
static void handle_handshake_error(URLContext *h, int ret)
Definition: tls_mbedtls.c:128
TLSContext::tls_shared
TLSShared tls_shared
Definition: tls_gnutls.c:50
URLProtocol::name
const char * name
Definition: url.h:55
tls_read
static int tls_read(URLContext *h, uint8_t *buf, int size)
Definition: tls_mbedtls.c:306
handle_transport_error
static int handle_transport_error(URLContext *h, const char *func_name, int react_on_eagain, int ret)
Definition: tls_mbedtls.c:67
options
static const AVOption options[]
Definition: tls_mbedtls.c:338
URLContext
Definition: url.h:38
url.h
uint8_t
uint8_t
Definition: audio_convert.c:194
len
int len
Definition: vorbis_enc_data.h:452
TLSShared::cert_file
char * cert_file
Definition: tls.h:32
ff_tls_open_underlying
int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AVDictionary **options)
Definition: tls.c:56
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
TLSShared::ca_file
char * ca_file
Definition: tls.h:30
avformat.h
tls_class
static const AVClass tls_class
Definition: tls_mbedtls.c:344
tls.h
TLSContext::own_cert
mbedtls_x509_crt own_cert
Definition: tls_mbedtls.c:44
TLSShared::key_file
char * key_file
Definition: tls.h:33
ffurl_read
int ffurl_read(URLContext *h, unsigned char *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf.
Definition: avio.c:410
ffurl_write
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Write size bytes from buf to the resource accessed by h.
Definition: avio.c:424
tls_open
static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
Definition: tls_mbedtls.c:166
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:251
tls_write
static int tls_write(URLContext *h, const uint8_t *buf, int size)
Definition: tls_mbedtls.c:319
TLSShared
Definition: tls.h:29
handle_tls_error
static int handle_tls_error(URLContext *h, const char *func_name, int ret)
Definition: tls_mbedtls.c:287
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:565
tls_get_file_handle
static int tls_get_file_handle(URLContext *h)
Definition: tls_mbedtls.c:332
TLSContext::priv_key_pw
char * priv_key_pw
Definition: tls_mbedtls.c:46
TLSShared::numerichost
int numerichost
Definition: tls.h:39
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
h
h
Definition: vp9dsp_template.c:2038
AVERROR_EXIT
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:227
ffurl_get_file_handle
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
Definition: avio.c:629
TLSShared::tcp
URLContext * tcp
Definition: tls.h:41