FFmpeg  4.1.11
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 
110 static void handle_pk_parse_error(URLContext *h, int ret)
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 
128 static void handle_handshake_error(URLContext *h, int ret)
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) {
221  handle_pk_parse_error(h, ret);
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) {
263  handle_handshake_error(h, ret);
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;
336 }
337 
338 static const AVOption options[] = {
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 };
#define NULL
Definition: coverity.c:32
int size
#define URL_PROTOCOL_FLAG_NETWORK
Definition: url.h:34
AVOption.
Definition: opt.h:246
int verify
Definition: tls.h:31
static int handle_tls_error(URLContext *h, const char *func_name, int ret)
Definition: tls_mbedtls.c:287
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
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
mbedtls_ssl_config ssl_config
Definition: tls_mbedtls.c:40
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
int listen
Definition: tls.h:34
#define TLS_OPTFL
Definition: tls.h:44
static void parse_options(TLSContext *tls_ctxc, const char *uri)
Definition: tls_mbedtls.c:155
static const AVOption options[]
Definition: tls_mbedtls.c:338
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
static int mbedtls_send(void *ctx, const unsigned char *buf, size_t len)
Definition: tls_mbedtls.c:84
static void handle_handshake_error(URLContext *h, int ret)
Definition: tls_mbedtls.c:128
uint8_t
static int tls_read(URLContext *h, uint8_t *buf, int size)
Definition: tls_mbedtls.c:306
static int tls_get_file_handle(URLContext *h)
Definition: tls_mbedtls.c:332
Definition: tls.h:29
#define AVERROR_EOF
End of file.
Definition: error.h:55
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
#define av_log(a,...)
static int tls_write(URLContext *h, const uint8_t *buf, int size)
Definition: tls_mbedtls.c:319
mbedtls_ctr_drbg_context ctr_drbg_context
Definition: tls_mbedtls.c:42
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define OFFSET(x)
Definition: tls_mbedtls.c:49
#define AVERROR(e)
Definition: error.h:43
static const AVClass tls_class
Definition: tls_mbedtls.c:344
#define fail()
Definition: checkasm.h:117
char * host
Definition: tls.h:36
static int handle_transport_error(URLContext *h, const char *func_name, int react_on_eagain, int ret)
Definition: tls_mbedtls.c:67
mbedtls_pk_context priv_key
Definition: tls_mbedtls.c:45
static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
Definition: tls_mbedtls.c:166
mbedtls_x509_crt ca_cert
Definition: tls_mbedtls.c:43
mbedtls_ssl_context ssl_context
Definition: tls_mbedtls.c:39
mbedtls_x509_crt own_cert
Definition: tls_mbedtls.c:44
#define TLS_COMMON_OPTIONS(pstruct, options_field)
Definition: tls.h:45
mbedtls_entropy_context entropy_context
Definition: tls_mbedtls.c:41
static int tls_close(URLContext *h)
Definition: tls_mbedtls.c:51
char * cert_file
Definition: tls.h:32
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
Definition: avio.c:629
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
char * ca_file
Definition: tls.h:30
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:251
TLSShared tls_shared
Definition: tls_gnutls.c:50
void * buf
Definition: avisynth_c.h:690
Definition: url.h:38
Describe the class of an AVClass context structure.
Definition: log.h:67
void * priv_data
Definition: url.h:41
char * priv_key_pw
Definition: tls_mbedtls.c:46
misc parsing utilities
const char * name
Definition: url.h:55
#define flags(name, subs,...)
Definition: cbs_av1.c:610
Main libavformat public API header.
const URLProtocol ff_tls_protocol
Definition: tls_mbedtls.c:351
int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AVDictionary **options)
Definition: tls.c:56
struct tls * ctx
Definition: tls_libtls.c:37
static double c[64]
static void handle_pk_parse_error(URLContext *h, int ret)
Definition: tls_mbedtls.c:110
URLContext * tcp
Definition: tls.h:41
int numerichost
Definition: tls.h:39
static int mbedtls_recv(void *ctx, unsigned char *buf, size_t len)
Definition: tls_mbedtls.c:97
int len
int max_packet_size
if non zero, the stream is packetized with this max packet size
Definition: url.h:44
unbuffered private I/O API
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
char * key_file
Definition: tls.h:33