FFmpeg
tls_libtls.c
Go to the documentation of this file.
1 /*
2  * TLS/SSL Protocol
3  * Copyright (c) 2011 Martin Storsjo
4  * Copyright (c) 2017 sfan5 <sfan5@live.de>
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 #include "avformat.h"
24 #include "internal.h"
25 #include "network.h"
26 #include "url.h"
27 #include "tls.h"
28 #include "libavcodec/internal.h"
29 #include "libavutil/avutil.h"
30 #include "libavutil/opt.h"
31 
32 #include <tls.h>
33 
34 typedef struct TLSContext {
36  struct tls *ctx;
37 } TLSContext;
38 
40 {
41  TLSContext *p = h->priv_data;
42  if (p->ctx) {
43  tls_close(p->ctx);
44  tls_free(p->ctx);
45  }
46  ffurl_closep(&p->tls_shared.tcp);
47  return 0;
48 }
49 
50 static ssize_t tls_read_callback(struct tls *ctx, void *buf, size_t buflen, void *cb_arg)
51 {
52  URLContext *h = (URLContext*) cb_arg;
53  int ret = ffurl_read(h, buf, buflen);
54  if (ret == AVERROR(EAGAIN))
55  return TLS_WANT_POLLIN;
56  else if (ret == AVERROR_EXIT)
57  return 0;
58  return ret >= 0 ? ret : -1;
59 }
60 
61 static ssize_t tls_write_callback(struct tls *ctx, const void *buf, size_t buflen, void *cb_arg)
62 {
63  URLContext *h = (URLContext*) cb_arg;
64  int ret = ffurl_write(h, buf, buflen);
65  if (ret == AVERROR(EAGAIN))
66  return TLS_WANT_POLLOUT;
67  else if (ret == AVERROR_EXIT)
68  return 0;
69  return ret >= 0 ? ret : -1;
70 }
71 
72 static int ff_tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
73 {
74  TLSContext *p = h->priv_data;
75  TLSShared *c = &p->tls_shared;
76  struct tls_config *cfg = NULL;
77  int ret;
78 
79  if (tls_init() == -1) {
80  ret = AVERROR(EIO);
81  goto fail;
82  }
83 
84  if ((ret = ff_tls_open_underlying(c, h, uri, options)) < 0)
85  goto fail;
86 
87  p->ctx = !c->listen ? tls_client() : tls_server();
88  if (!p->ctx) {
89  ret = AVERROR(EIO);
90  goto fail;
91  }
92 
93  cfg = tls_config_new();
94  if (!p->ctx) {
95  ret = AVERROR(EIO);
96  goto fail;
97  }
98  if (tls_config_set_protocols(cfg, TLS_PROTOCOLS_ALL) == -1)
99  goto err_config;
100  // While TLSv1.0 and TLSv1.1 are already enabled by the above,
101  // we need to be less strict with ciphers so it works in practice.
102  if (tls_config_set_ciphers(cfg, "compat") == -1)
103  goto err_config;
104  if (c->ca_file && tls_config_set_ca_file(cfg, c->ca_file) == -1)
105  goto err_config;
106  if (c->cert_file && tls_config_set_cert_file(cfg, c->cert_file) == -1)
107  goto err_config;
108  if (c->key_file && tls_config_set_key_file(cfg, c->key_file) == -1)
109  goto err_config;
110  if (!c->verify) {
111  tls_config_insecure_noverifycert(cfg);
112  tls_config_insecure_noverifyname(cfg);
113  tls_config_insecure_noverifytime(cfg);
114  }
115  if (tls_configure(p->ctx, cfg) == -1)
116  goto err_ctx;
117 
118  if (!c->listen) {
119  ret = tls_connect_cbs(p->ctx, tls_read_callback, tls_write_callback,
120  c->tcp, c->host);
121  } else {
122  struct tls *ctx_new;
123  ret = tls_accept_cbs(p->ctx, &ctx_new, tls_read_callback,
124  tls_write_callback, c->tcp);
125  if (ret == 0) {
126  // free "server" context and replace by "connection" context
127  tls_free(p->ctx);
128  p->ctx = ctx_new;
129  }
130  }
131  if (ret == -1)
132  goto err_ctx;
133 
134  tls_config_free(cfg);
135  return 0;
136 err_config:
137  av_log(h, AV_LOG_ERROR, "%s\n", tls_config_error(cfg));
138  ret = AVERROR(EIO);
139  goto fail;
140 err_ctx:
141  av_log(h, AV_LOG_ERROR, "%s\n", tls_error(p->ctx));
142  ret = AVERROR(EIO);
143  /* fallthrough */
144 fail:
145  if (cfg)
146  tls_config_free(cfg);
147  ff_tls_close(h);
148  return ret;
149 }
150 
151 static int ff_tls_read(URLContext *h, uint8_t *buf, int size)
152 {
153  TLSContext *p = h->priv_data;
154  ssize_t ret;
155  ret = tls_read(p->ctx, buf, size);
156  if (ret > 0)
157  return ret;
158  else if (ret == 0)
159  return AVERROR_EOF;
160  else if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT)
161  return AVERROR(EAGAIN);
162  av_log(h, AV_LOG_ERROR, "%s\n", tls_error(p->ctx));
163  return AVERROR(EIO);
164 }
165 
166 static int ff_tls_write(URLContext *h, const uint8_t *buf, int size)
167 {
168  TLSContext *p = h->priv_data;
169  ssize_t ret;
170  ret = tls_write(p->ctx, buf, size);
171  if (ret > 0)
172  return ret;
173  else if (ret == 0)
174  return AVERROR_EOF;
175  else if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT)
176  return AVERROR(EAGAIN);
177  av_log(h, AV_LOG_ERROR, "%s\n", tls_error(p->ctx));
178  return AVERROR(EIO);
179 }
180 
182 {
183  TLSContext *c = h->priv_data;
184  return ffurl_get_file_handle(c->tls_shared.tcp);
185 }
186 
188 {
189  TLSContext *s = h->priv_data;
190  return ffurl_get_short_seek(s->tls_shared.tcp);
191 }
192 
193 static const AVOption options[] = {
194  TLS_COMMON_OPTIONS(TLSContext, tls_shared),
195  { NULL }
196 };
197 
198 static const AVClass tls_class = {
199  .class_name = "tls",
200  .item_name = av_default_item_name,
201  .option = options,
202  .version = LIBAVUTIL_VERSION_INT,
203 };
204 
206  .name = "tls",
207  .url_open2 = ff_tls_open,
208  .url_read = ff_tls_read,
209  .url_write = ff_tls_write,
210  .url_close = ff_tls_close,
211  .url_get_file_handle = tls_get_file_handle,
212  .url_get_short_seek = tls_get_short_seek,
213  .priv_data_size = sizeof(TLSContext),
215  .priv_data_class = &tls_class,
216 };
flags
const SwsFlags flags[]
Definition: swscale.c:61
TLSContext
Definition: tls_gnutls.c:45
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
opt.h
URL_PROTOCOL_FLAG_NETWORK
#define URL_PROTOCOL_FLAG_NETWORK
Definition: url.h:33
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
ffurl_write
static int ffurl_write(URLContext *h, const uint8_t *buf, int size)
Write size bytes from buf to the resource accessed by h.
Definition: url.h:202
ff_tls_close
static int ff_tls_close(URLContext *h)
Definition: tls_libtls.c:39
internal.h
AVOption
AVOption.
Definition: opt.h:429
tls_write
static int tls_write(URLContext *h, const uint8_t *buf, int size)
Definition: tls_gnutls.c:284
AVDictionary
Definition: dict.c:32
URLProtocol
Definition: url.h:51
ff_tls_read
static int ff_tls_read(URLContext *h, uint8_t *buf, int size)
Definition: tls_libtls.c:151
TLS_COMMON_OPTIONS
#define TLS_COMMON_OPTIONS(pstruct, options_field)
Definition: tls.h:88
fail
#define fail()
Definition: checkasm.h:203
ffurl_get_short_seek
int ffurl_get_short_seek(void *urlcontext)
Return the current short seek threshold value for this URL.
Definition: avio.c:839
ff_tls_open
static int ff_tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
Definition: tls_libtls.c:72
tls_class
static const AVClass tls_class
Definition: tls_libtls.c:198
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
s
#define s(width, name)
Definition: cbs_vp9.c:198
ctx
AVFormatContext * ctx
Definition: movenc.c:49
tls_get_file_handle
static int tls_get_file_handle(URLContext *h)
Definition: tls_libtls.c:181
tls_close
static int tls_close(URLContext *h)
Definition: tls_gnutls.c:100
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
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:241
options
Definition: swscale.c:43
options
static const AVOption options[]
Definition: tls_libtls.c:193
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
size
int size
Definition: twinvq_data.h:10344
TLSContext::tls_shared
TLSShared tls_shared
Definition: tls_gnutls.c:46
URLProtocol::name
const char * name
Definition: url.h:52
tls_get_short_seek
static int tls_get_short_seek(URLContext *h)
Definition: tls_libtls.c:187
ff_tls_protocol
const URLProtocol ff_tls_protocol
Definition: tls_libtls.c:205
URLContext
Definition: url.h:35
url.h
ffurl_closep
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition: avio.c:589
ff_tls_open_underlying
int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AVDictionary **options)
Definition: tls.c:34
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:81
tls_write_callback
static ssize_t tls_write_callback(struct tls *ctx, const void *buf, size_t buflen, void *cb_arg)
Definition: tls_libtls.c:61
avformat.h
network.h
tls.h
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
avutil.h
tls_read
static int tls_read(URLContext *h, uint8_t *buf, int size)
Definition: tls_gnutls.c:267
TLSShared
Definition: tls.h:37
TLSContext::ctx
struct tls * ctx
Definition: tls_libtls.c:36
ff_tls_write
static int ff_tls_write(URLContext *h, const uint8_t *buf, int size)
Definition: tls_libtls.c:166
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
h
h
Definition: vp9dsp_template.c:2070
AVERROR_EXIT
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:58
ffurl_get_file_handle
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
Definition: avio.c:815
ffurl_read
static int ffurl_read(URLContext *h, uint8_t *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf.
Definition: url.h:181
tls_read_callback
static ssize_t tls_read_callback(struct tls *ctx, void *buf, size_t buflen, void *cb_arg)
Definition: tls_libtls.c:50