FFmpeg  4.4.9
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 #ifdef MBEDTLS_PSA_CRYPTO_C
30 #include <psa/crypto.h>
31 #endif
32 
33 #include "avformat.h"
34 #include "internal.h"
35 #include "url.h"
36 #include "tls.h"
37 #include "libavutil/parseutils.h"
38 
39 typedef struct TLSContext {
40  const AVClass *class;
42  mbedtls_ssl_context ssl_context;
43  mbedtls_ssl_config ssl_config;
44  mbedtls_entropy_context entropy_context;
45  mbedtls_ctr_drbg_context ctr_drbg_context;
46  mbedtls_x509_crt ca_cert;
47  mbedtls_x509_crt own_cert;
48  mbedtls_pk_context priv_key;
49  char *priv_key_pw;
50 } TLSContext;
51 
52 #define OFFSET(x) offsetof(TLSContext, x)
53 
54 static int tls_close(URLContext *h)
55 {
56  TLSContext *tls_ctx = h->priv_data;
57 
58  mbedtls_ssl_close_notify(&tls_ctx->ssl_context);
59  mbedtls_pk_free(&tls_ctx->priv_key);
60  mbedtls_x509_crt_free(&tls_ctx->ca_cert);
61  mbedtls_x509_crt_free(&tls_ctx->own_cert);
62  mbedtls_ssl_free(&tls_ctx->ssl_context);
63  mbedtls_ssl_config_free(&tls_ctx->ssl_config);
64  mbedtls_ctr_drbg_free(&tls_ctx->ctr_drbg_context);
65  mbedtls_entropy_free(&tls_ctx->entropy_context);
66 
67  ffurl_closep(&tls_ctx->tls_shared.tcp);
68  return 0;
69 }
70 
71 static int handle_transport_error(URLContext *h, const char* func_name, int react_on_eagain, int ret)
72 {
73  switch (ret) {
74  case AVERROR(EAGAIN):
75  return react_on_eagain;
76  case AVERROR_EXIT:
77  return 0;
78  case AVERROR(EPIPE):
79  case AVERROR(ECONNRESET):
80  return MBEDTLS_ERR_NET_CONN_RESET;
81  default:
82  av_log(h, AV_LOG_ERROR, "%s returned 0x%x\n", func_name, ret);
83  errno = EIO;
84  return MBEDTLS_ERR_NET_SEND_FAILED;
85  }
86 }
87 
88 static int mbedtls_send(void *ctx, const unsigned char *buf, size_t len)
89 {
90  URLContext *h = (URLContext*) ctx;
91  int ret = ffurl_write(h, buf, len);
92  if (ret >= 0)
93  return ret;
94 
95  if (h->max_packet_size && len > h->max_packet_size)
96  return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
97 
98  return handle_transport_error(h, "ffurl_write", MBEDTLS_ERR_SSL_WANT_WRITE, ret);
99 }
100 
101 static int mbedtls_recv(void *ctx, unsigned char *buf, size_t len)
102 {
103  URLContext *h = (URLContext*) ctx;
104  int ret = ffurl_read(h, buf, len);
105  if (ret >= 0)
106  return ret;
107 
108  if (h->max_packet_size && len > h->max_packet_size)
109  return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
110 
111  return handle_transport_error(h, "ffurl_read", MBEDTLS_ERR_SSL_WANT_READ, ret);
112 }
113 
114 static void handle_pk_parse_error(URLContext *h, int ret)
115 {
116  switch (ret) {
117  case MBEDTLS_ERR_PK_FILE_IO_ERROR:
118  av_log(h, AV_LOG_ERROR, "Read of key file failed. Is it actually there, are the access permissions correct?\n");
119  break;
120  case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
121  av_log(h, AV_LOG_ERROR, "A password for the private key is missing.\n");
122  break;
123  case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
124  av_log(h, AV_LOG_ERROR, "The given password for the private key is wrong.\n");
125  break;
126  default:
127  av_log(h, AV_LOG_ERROR, "mbedtls_pk_parse_key returned -0x%x\n", -ret);
128  break;
129  }
130 }
131 
132 static void handle_handshake_error(URLContext *h, int ret)
133 {
134  switch (ret) {
135 #if MBEDTLS_VERSION_MAJOR < 3
136  case MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE:
137  av_log(h, AV_LOG_ERROR, "None of the common ciphersuites is usable. Was the local certificate correctly set?\n");
138  break;
139 #else
140  case MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE:
141  av_log(h, AV_LOG_ERROR, "TLS handshake failed.\n");
142  break;
143 #endif
144  case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
145  av_log(h, AV_LOG_ERROR, "A fatal alert message was received from the peer, has the peer a correct certificate?\n");
146  break;
147  case MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED:
148  av_log(h, AV_LOG_ERROR, "No CA chain is set, but required to operate. Was the CA correctly set?\n");
149  break;
150  case MBEDTLS_ERR_NET_CONN_RESET:
151  av_log(h, AV_LOG_ERROR, "TLS handshake was aborted by peer.\n");
152  break;
153  default:
154  av_log(h, AV_LOG_ERROR, "mbedtls_ssl_handshake returned -0x%x\n", -ret);
155  break;
156  }
157 }
158 
159 static void parse_options(TLSContext *tls_ctxc, const char *uri)
160 {
161  char buf[1024];
162  const char *p = strchr(uri, '?');
163  if (!p)
164  return;
165 
166  if (!tls_ctxc->priv_key_pw && av_find_info_tag(buf, sizeof(buf), "key_password", p))
167  tls_ctxc->priv_key_pw = av_strdup(buf);
168 }
169 
170 static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
171 {
172  TLSContext *tls_ctx = h->priv_data;
173  TLSShared *shr = &tls_ctx->tls_shared;
174  uint32_t verify_res_flags;
175  int ret;
176 
177  // parse additional options
178  parse_options(tls_ctx, uri);
179 
180  if ((ret = ff_tls_open_underlying(shr, h, uri, options)) < 0)
181  goto fail;
182 
183 #ifdef MBEDTLS_PSA_CRYPTO_C
184  if ((ret = psa_crypto_init()) != PSA_SUCCESS) {
185  av_log(h, AV_LOG_ERROR, "psa_crypto_init returned %d\n", ret);
186  goto fail;
187  }
188 #endif
189 
190  mbedtls_ssl_init(&tls_ctx->ssl_context);
191  mbedtls_ssl_config_init(&tls_ctx->ssl_config);
192  mbedtls_entropy_init(&tls_ctx->entropy_context);
193  mbedtls_ctr_drbg_init(&tls_ctx->ctr_drbg_context);
194  mbedtls_x509_crt_init(&tls_ctx->ca_cert);
195  mbedtls_pk_init(&tls_ctx->priv_key);
196 
197  // load trusted CA
198  if (shr->ca_file) {
199  if ((ret = mbedtls_x509_crt_parse_file(&tls_ctx->ca_cert, shr->ca_file)) != 0) {
200  av_log(h, AV_LOG_ERROR, "mbedtls_x509_crt_parse_file for CA cert returned %d\n", ret);
201  goto fail;
202  }
203  }
204 
205  // load own certificate
206  if (shr->cert_file) {
207  if ((ret = mbedtls_x509_crt_parse_file(&tls_ctx->own_cert, shr->cert_file)) != 0) {
208  av_log(h, AV_LOG_ERROR, "mbedtls_x509_crt_parse_file for own cert returned %d\n", ret);
209  goto fail;
210  }
211  }
212 
213  // seed the random number generator
214  if ((ret = mbedtls_ctr_drbg_seed(&tls_ctx->ctr_drbg_context,
215  mbedtls_entropy_func,
216  &tls_ctx->entropy_context,
217  NULL, 0)) != 0) {
218  av_log(h, AV_LOG_ERROR, "mbedtls_ctr_drbg_seed returned %d\n", ret);
219  goto fail;
220  }
221 
222  // load key file
223  if (shr->key_file) {
224  if ((ret = mbedtls_pk_parse_keyfile(&tls_ctx->priv_key,
225  shr->key_file,
226  tls_ctx->priv_key_pw
227 #if MBEDTLS_VERSION_MAJOR >= 3
228  , mbedtls_ctr_drbg_random,
229  &tls_ctx->ctr_drbg_context
230 #endif
231  )) != 0) {
232  handle_pk_parse_error(h, ret);
233  goto fail;
234  }
235  }
236 
237  if ((ret = mbedtls_ssl_config_defaults(&tls_ctx->ssl_config,
238  shr->listen ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT,
239  MBEDTLS_SSL_TRANSPORT_STREAM,
240  MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
241  av_log(h, AV_LOG_ERROR, "mbedtls_ssl_config_defaults returned %d\n", ret);
242  goto fail;
243  }
244 
245  mbedtls_ssl_conf_authmode(&tls_ctx->ssl_config,
246  shr->verify ? MBEDTLS_SSL_VERIFY_REQUIRED : MBEDTLS_SSL_VERIFY_NONE);
247  mbedtls_ssl_conf_rng(&tls_ctx->ssl_config, mbedtls_ctr_drbg_random, &tls_ctx->ctr_drbg_context);
248  mbedtls_ssl_conf_ca_chain(&tls_ctx->ssl_config, &tls_ctx->ca_cert, NULL);
249 
250  // set own certificate and private key
251  if ((ret = mbedtls_ssl_conf_own_cert(&tls_ctx->ssl_config, &tls_ctx->own_cert, &tls_ctx->priv_key)) != 0) {
252  av_log(h, AV_LOG_ERROR, "mbedtls_ssl_conf_own_cert returned %d\n", ret);
253  goto fail;
254  }
255 
256  if ((ret = mbedtls_ssl_setup(&tls_ctx->ssl_context, &tls_ctx->ssl_config)) != 0) {
257  av_log(h, AV_LOG_ERROR, "mbedtls_ssl_setup returned %d\n", ret);
258  goto fail;
259  }
260 
261  if (!shr->listen) {
262  if ((ret = mbedtls_ssl_set_hostname(&tls_ctx->ssl_context, shr->host)) != 0) {
263  av_log(h, AV_LOG_ERROR, "mbedtls_ssl_set_hostname returned %d\n", ret);
264  goto fail;
265  }
266  }
267 
268  // set I/O functions to use FFmpeg internal code for transport layer
269  mbedtls_ssl_set_bio(&tls_ctx->ssl_context, shr->tcp, mbedtls_send, mbedtls_recv, NULL);
270 
271  // ssl handshake
272  while ((ret = mbedtls_ssl_handshake(&tls_ctx->ssl_context)) != 0) {
273  if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
275  goto fail;
276  }
277  }
278 
279  if (shr->verify) {
280  // check the result of the certificate verification
281  if ((verify_res_flags = mbedtls_ssl_get_verify_result(&tls_ctx->ssl_context)) != 0) {
282  av_log(h, AV_LOG_ERROR, "mbedtls_ssl_get_verify_result reported problems "\
283  "with the certificate verification, returned flags: %u\n",
284  verify_res_flags);
285  if (verify_res_flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED)
286  av_log(h, AV_LOG_ERROR, "The certificate is not correctly signed by the trusted CA.\n");
287  goto fail;
288  }
289  }
290 
291  return 0;
292 
293 fail:
294  tls_close(h);
295  return AVERROR(EIO);
296 }
297 
298 static int handle_tls_error(URLContext *h, const char* func_name, int ret)
299 {
300  switch (ret) {
301  case MBEDTLS_ERR_SSL_WANT_READ:
302  case MBEDTLS_ERR_SSL_WANT_WRITE:
303  return AVERROR(EAGAIN);
304  case MBEDTLS_ERR_NET_SEND_FAILED:
305  case MBEDTLS_ERR_NET_RECV_FAILED:
306  return AVERROR(EIO);
307  case MBEDTLS_ERR_NET_CONN_RESET:
308  case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
309  av_log(h, AV_LOG_WARNING, "%s reported connection reset by peer\n", func_name);
310  return AVERROR_EOF;
311  default:
312  av_log(h, AV_LOG_ERROR, "%s returned -0x%x\n", func_name, -ret);
313  return AVERROR(EIO);
314  }
315 }
316 
317 static int tls_read(URLContext *h, uint8_t *buf, int size)
318 {
319  TLSContext *tls_ctx = h->priv_data;
320  int ret;
321 
322  if ((ret = mbedtls_ssl_read(&tls_ctx->ssl_context, buf, size)) > 0) {
323  // return read length
324  return ret;
325  }
326 
327  return handle_tls_error(h, "mbedtls_ssl_read", ret);
328 }
329 
330 static int tls_write(URLContext *h, const uint8_t *buf, int size)
331 {
332  TLSContext *tls_ctx = h->priv_data;
333  int ret;
334 
335  if ((ret = mbedtls_ssl_write(&tls_ctx->ssl_context, buf, size)) > 0) {
336  // return written length
337  return ret;
338  }
339 
340  return handle_tls_error(h, "mbedtls_ssl_write", ret);
341 }
342 
344 {
345  TLSContext *c = h->priv_data;
346  return ffurl_get_file_handle(c->tls_shared.tcp);
347 }
348 
350 {
351  TLSContext *s = h->priv_data;
352  return ffurl_get_short_seek(s->tls_shared.tcp);
353 }
354 
355 static const AVOption options[] = {
356  TLS_COMMON_OPTIONS(TLSContext, tls_shared), \
357  {"key_password", "Password for the private key file", OFFSET(priv_key_pw), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \
358  { NULL }
359 };
360 
361 static const AVClass tls_class = {
362  .class_name = "tls",
363  .item_name = av_default_item_name,
364  .option = options,
365  .version = LIBAVUTIL_VERSION_INT,
366 };
367 
369  .name = "tls",
370  .url_open2 = tls_open,
371  .url_read = tls_read,
372  .url_write = tls_write,
373  .url_close = tls_close,
374  .url_get_file_handle = tls_get_file_handle,
375  .url_get_short_seek = tls_get_short_seek,
376  .priv_data_size = sizeof(TLSContext),
378  .priv_data_class = &tls_class,
379 };
uint8_t
Main libavformat public API header.
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:404
int ffurl_get_short_seek(URLContext *h)
Return the current short seek threshold value for this URL.
Definition: avio.c:647
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition: avio.c:441
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:418
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
Definition: avio.c:623
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
#define fail()
Definition: checkasm.h:133
#define NULL
Definition: coverity.c:32
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:253
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
common internal API header
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
misc parsing utilities
Describe the class of an AVClass context structure.
Definition: log.h:67
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
AVOption.
Definition: opt.h:248
char * priv_key_pw
Definition: tls_mbedtls.c:49
mbedtls_ctr_drbg_context ctr_drbg_context
Definition: tls_mbedtls.c:45
mbedtls_ssl_context ssl_context
Definition: tls_mbedtls.c:42
mbedtls_ssl_config ssl_config
Definition: tls_mbedtls.c:43
mbedtls_x509_crt ca_cert
Definition: tls_mbedtls.c:46
mbedtls_pk_context priv_key
Definition: tls_mbedtls.c:48
mbedtls_x509_crt own_cert
Definition: tls_mbedtls.c:47
mbedtls_entropy_context entropy_context
Definition: tls_mbedtls.c:44
TLSShared tls_shared
Definition: tls_gnutls.c:50
Definition: tls.h:29
int listen
Definition: tls.h:34
int verify
Definition: tls.h:31
char * ca_file
Definition: tls.h:30
URLContext * tcp
Definition: tls.h:42
char * key_file
Definition: tls.h:33
char * host
Definition: tls.h:36
char * cert_file
Definition: tls.h:32
Definition: url.h:38
const char * name
Definition: url.h:55
#define av_log(a,...)
AVFormatContext * ctx
Definition: movenc.c:48
int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AVDictionary **options)
Definition: tls.c:67
#define TLS_COMMON_OPTIONS(pstruct, options_field)
Definition: tls.h:46
#define TLS_OPTFL
Definition: tls.h:45
static int tls_close(URLContext *h)
Definition: tls_mbedtls.c:54
static const AVClass tls_class
Definition: tls_mbedtls.c:361
static void handle_pk_parse_error(URLContext *h, int ret)
Definition: tls_mbedtls.c:114
const URLProtocol ff_tls_protocol
Definition: tls_mbedtls.c:368
static const AVOption options[]
Definition: tls_mbedtls.c:355
static void handle_handshake_error(URLContext *h, int ret)
Definition: tls_mbedtls.c:132
static int tls_read(URLContext *h, uint8_t *buf, int size)
Definition: tls_mbedtls.c:317
static int handle_tls_error(URLContext *h, const char *func_name, int ret)
Definition: tls_mbedtls.c:298
static int handle_transport_error(URLContext *h, const char *func_name, int react_on_eagain, int ret)
Definition: tls_mbedtls.c:71
static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
Definition: tls_mbedtls.c:170
static int tls_get_short_seek(URLContext *h)
Definition: tls_mbedtls.c:349
static int tls_write(URLContext *h, const uint8_t *buf, int size)
Definition: tls_mbedtls.c:330
static int mbedtls_send(void *ctx, const unsigned char *buf, size_t len)
Definition: tls_mbedtls.c:88
static int mbedtls_recv(void *ctx, unsigned char *buf, size_t len)
Definition: tls_mbedtls.c:101
static void parse_options(TLSContext *tls_ctxc, const char *uri)
Definition: tls_mbedtls.c:159
#define OFFSET(x)
Definition: tls_mbedtls.c:52
static int tls_get_file_handle(URLContext *h)
Definition: tls_mbedtls.c:343
int size
unbuffered private I/O API
#define URL_PROTOCOL_FLAG_NETWORK
Definition: url.h:34
int len
static double c[64]