FFmpeg
tls_gnutls.c
Go to the documentation of this file.
1 /*
2  * TLS/SSL Protocol
3  * Copyright (c) 2011 Martin Storsjo
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 <errno.h>
23 
24 #include <gnutls/gnutls.h>
25 #include <gnutls/dtls.h>
26 #include <gnutls/x509.h>
27 
28 #include "avformat.h"
29 #include "network.h"
30 #include "os_support.h"
31 #include "url.h"
32 #include "tls.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/mem.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/thread.h"
37 #include "libavutil/random_seed.h"
38 
39 #ifndef GNUTLS_VERSION_NUMBER
40 #define GNUTLS_VERSION_NUMBER LIBGNUTLS_VERSION_NUMBER
41 #endif
42 
43 #if HAVE_THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
44 #include <gcrypt.h>
45 GCRY_THREAD_OPTION_PTHREAD_IMPL;
46 #endif
47 
48 #define MAX_MD_SIZE 64
49 
50 static int pkey_to_pem_string(gnutls_x509_privkey_t key, char *out, size_t out_sz)
51 {
52  size_t required_sz = out_sz - 1;
53  int ret = 0;
54 
55  if (!out || !out_sz)
56  return AVERROR(EINVAL);
57 
58  ret = gnutls_x509_privkey_export(key, GNUTLS_X509_FMT_PEM, out, &required_sz);
59  if (ret < 0) {
60  if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER)
62  "TLS: Buffer size %zu is not enough to store private key PEM (need %zu)\n",
63  out_sz, required_sz + 1);
64  return AVERROR(EINVAL);
65  }
66  out[required_sz] = '\0';
67  return required_sz;
68 }
69 
70 static int crt_to_pem_string(gnutls_x509_crt_t crt, char *out, size_t out_sz)
71 {
72  size_t required_sz = out_sz - 1;
73  int ret = 0;
74 
75  if (!out || !out_sz)
76  return AVERROR(EINVAL);
77 
78  ret = gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, out, &required_sz);
79  if (ret < 0) {
80  if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER)
82  "TLS: Buffer size %zu is not enough to store certificate PEM (need %zu)\n",
83  out_sz, required_sz + 1);
84  return AVERROR(EINVAL);
85  }
86  out[required_sz] = '\0';
87  return required_sz;
88 }
89 
90 static int gnutls_x509_fingerprint(gnutls_x509_crt_t cert, char **fingerprint)
91 {
92  unsigned char md[MAX_MD_SIZE];
93  size_t n = sizeof(md);
94  AVBPrint buf;
95  int ret;
96 
97  ret = gnutls_x509_crt_get_fingerprint(cert, GNUTLS_DIG_SHA256, md, &n);
98  if (ret < 0) {
99  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to generate fingerprint, %s\n",
100  gnutls_strerror(ret));
101  return AVERROR(EINVAL);
102  }
103 
104  av_bprint_init(&buf, n*3, n*3);
105 
106  for (int i = 0; i < n - 1; i++)
107  av_bprintf(&buf, "%02X:", md[i]);
108  av_bprintf(&buf, "%02X", md[n - 1]);
109 
110  return av_bprint_finalize(&buf, fingerprint);
111 }
112 
113 int ff_ssl_read_key_cert(char *key_url, char *crt_url, char *key_buf, size_t key_sz, char *crt_buf, size_t crt_sz, char **fingerprint)
114 {
115  int ret = 0;
116  AVBPrint key_bp, crt_bp;
117  gnutls_x509_crt_t crt = NULL;
118  gnutls_x509_privkey_t key = NULL;
119  gnutls_datum_t tmp;
120 
123 
124  ret = ff_url_read_all(key_url, &key_bp);
125  if (ret < 0) {
126  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to open key file %s\n", key_url);
127  goto end;
128  }
129 
130  ret = ff_url_read_all(crt_url, &crt_bp);
131  if (ret < 0) {
132  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to open certificate file %s\n", crt_url);
133  goto end;
134  }
135 
136  ret = gnutls_x509_privkey_init(&key);
137  if (ret < 0) {
138  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to init private key: %s\n", gnutls_strerror(ret));
139  goto end;
140  }
141 
142  ret = gnutls_x509_crt_init(&crt);
143  if (ret < 0) {
144  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to init certificate: %s\n", gnutls_strerror(ret));
145  goto end;
146  }
147 
148  tmp.data = key_bp.str;
149  tmp.size = key_bp.len;
150  ret = gnutls_x509_privkey_import(key, &tmp, GNUTLS_X509_FMT_PEM);
151  if (ret < 0) {
152  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to import private key: %s\n", gnutls_strerror(ret));
153  goto end;
154  }
155 
156  tmp.data = crt_bp.str;
157  tmp.size = crt_bp.len;
158  ret = gnutls_x509_crt_import(crt, &tmp, GNUTLS_X509_FMT_PEM);
159  if (ret < 0) {
160  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to import certificate: %s\n", gnutls_strerror(ret));
161  goto end;
162  }
163 
164  ret = pkey_to_pem_string(key, key_buf, key_sz);
165  if (ret < 0) {
166  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to converter private key to PEM string\n");
167  goto end;
168  }
169 
170  ret = crt_to_pem_string(crt, crt_buf, crt_sz);
171  if (ret < 0) {
172  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to converter certificate to PEM string\n");
173  goto end;
174  }
175 
176  ret = gnutls_x509_fingerprint(crt, fingerprint);
177  if (ret < 0)
178  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to generate fingerprint\n");
179 
180 end:
181  av_bprint_finalize(&key_bp, NULL);
182  av_bprint_finalize(&crt_bp, NULL);
183  if (crt)
184  gnutls_x509_crt_deinit(crt);
185  if (key)
186  gnutls_x509_privkey_deinit(key);
187  return ret;
188 }
189 
190 static int gnutls_gen_private_key(gnutls_x509_privkey_t *key)
191 {
192  int ret = 0;
193 
194  ret = gnutls_x509_privkey_init(key);
195  if (ret < 0) {
196  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to init private key: %s\n", gnutls_strerror(ret));
197  goto einval_end;
198  }
199 
200  ret = gnutls_x509_privkey_generate(*key, GNUTLS_PK_ECDSA,
201  gnutls_sec_param_to_pk_bits(GNUTLS_PK_ECDSA, GNUTLS_SEC_PARAM_MEDIUM), 0);
202  if (ret < 0) {
203  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to generate private key: %s\n", gnutls_strerror(ret));
204  goto einval_end;
205  }
206 
207  goto end;
208 einval_end:
209  ret = AVERROR(EINVAL);
210  gnutls_x509_privkey_deinit(*key);
211  *key = NULL;
212 end:
213  return ret;
214 }
215 
216 static int gnutls_gen_certificate(gnutls_x509_privkey_t key, gnutls_x509_crt_t *crt, char **fingerprint)
217 {
218  int ret = 0;
219  uint64_t serial;
220  unsigned char buf[8];
221  const char *dn = "CN=lavf";
222 
223  ret = gnutls_x509_crt_init(crt);
224  if (ret < 0) {
225  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to init certificate: %s\n", gnutls_strerror(ret));
226  goto einval_end;
227  }
228 
229  ret = gnutls_x509_crt_set_version(*crt, 3);
230  if (ret < 0) {
231  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to set certificate version: %s\n", gnutls_strerror(ret));
232  goto einval_end;
233  }
234 
235  /**
236  * See https://gnutls.org/manual/gnutls.html#gnutls_005fx509_005fcrt_005fset_005fserial-1
237  * The provided serial should be a big-endian positive number (i.e. its leftmost bit should be zero).
238  */
239  serial = av_get_random_seed();
240  AV_WB64(buf, serial);
241  buf[0] &= 0x7F;
242  ret = gnutls_x509_crt_set_serial(*crt, buf, sizeof(buf));
243  if (ret < 0) {
244  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to set certificate serial: %s\n", gnutls_strerror(ret));
245  goto einval_end;
246  }
247 
248  ret = gnutls_x509_crt_set_activation_time(*crt, time(NULL));
249  if (ret < 0) {
250  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to set certificate activation time: %s\n", gnutls_strerror(ret));
251  goto einval_end;
252  }
253 
254  ret = gnutls_x509_crt_set_expiration_time(*crt, time(NULL) + 365 * 24 * 60 * 60);
255  if (ret < 0) {
256  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to set certificate expiration time: %s\n", gnutls_strerror(ret));
257  goto einval_end;
258  }
259 
260  ret = gnutls_x509_crt_set_dn(*crt, dn, NULL);
261  if (ret < 0) {
262  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to set certificate dn: %s\n", gnutls_strerror(ret));
263  goto einval_end;
264  }
265 
266  ret = gnutls_x509_crt_set_issuer_dn(*crt, dn, NULL);
267  if (ret < 0) {
268  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to set certificate issuer dn: %s\n", gnutls_strerror(ret));
269  goto einval_end;
270  }
271 
272  ret = gnutls_x509_crt_set_key(*crt, key);
273  if (ret < 0) {
274  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to set key: %s\n", gnutls_strerror(ret));
275  goto einval_end;
276  }
277 
278  ret = gnutls_x509_crt_sign2(*crt, *crt, key, GNUTLS_DIG_SHA256, 0);
279  if (ret < 0) {
280  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to sign certificate: %s\n", gnutls_strerror(ret));
281  goto einval_end;
282  }
283 
284  ret = gnutls_x509_fingerprint(*crt, fingerprint);
285  if (ret < 0)
286  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to generate fingerprint\n");
287 
288  goto end;
289 einval_end:
290  ret = AVERROR(EINVAL);
291  gnutls_x509_crt_deinit(*crt);
292  *crt = NULL;
293 end:
294  return ret;
295 }
296 
297 int ff_ssl_gen_key_cert(char *key_buf, size_t key_sz, char *cert_buf, size_t cert_sz, char **fingerprint)
298 {
299  int ret;
300  gnutls_x509_crt_t crt = NULL;
301  gnutls_x509_privkey_t key = NULL;
302 
304  if (ret < 0) {
305  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to generate private key\n");
306  goto end;
307  }
308 
309  ret = gnutls_gen_certificate(key, &crt, fingerprint);
310  if (ret < 0) {
311  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to generate certificate\n");
312  goto end;
313  }
314 
315  ret = pkey_to_pem_string(key, key_buf, key_sz);
316  if (ret < 0) {
317  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to convert private key to PEM string\n");
318  goto end;
319  }
320 
321  ret = crt_to_pem_string(crt, cert_buf, cert_sz);
322  if (ret < 0) {
323  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to convert certificate to PEM string\n");
324  goto end;
325  }
326 end:
327  if (crt)
328  gnutls_x509_crt_deinit(crt);
329  if (key)
330  gnutls_x509_privkey_deinit(key);
331  return ret;
332 }
333 
334 typedef struct TLSContext {
336  gnutls_session_t session;
337  gnutls_certificate_credentials_t cred;
339  int io_err;
341  socklen_t dest_addr_len;
342 } TLSContext;
343 
345 
346 void ff_gnutls_init(void)
347 {
349 #if HAVE_THREADS && GNUTLS_VERSION_NUMBER < 0x020b00
350  if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
351  gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
352 #endif
353  gnutls_global_init();
355 }
356 
358 {
360  gnutls_global_deinit();
362 }
363 
365 {
366  TLSContext *c = h->priv_data;
367  TLSShared *s = &c->tls_shared;
368 
369  if (s->is_dtls)
370  s->udp = sock;
371  else
372  s->tcp = sock;
373 
374  return 0;
375 }
376 
377 int ff_dtls_export_materials(URLContext *h, char *dtls_srtp_materials, size_t materials_sz)
378 {
379  int ret = 0;
380  TLSContext *c = h->priv_data;
381 
382  ret = gnutls_srtp_get_keys(c->session, dtls_srtp_materials, materials_sz, NULL, NULL, NULL, NULL);
383  if (ret < 0) {
384  av_log(c, AV_LOG_ERROR, "Failed to export SRTP material: %s\n", gnutls_strerror(ret));
385  return -1;
386  }
387  return 0;
388 }
389 
390 static int print_tls_error(URLContext *h, int ret)
391 {
392  TLSContext *c = h->priv_data;
393  switch (ret) {
394  case GNUTLS_E_AGAIN:
395  return AVERROR(EAGAIN);
396  case GNUTLS_E_INTERRUPTED:
397 #ifdef GNUTLS_E_PREMATURE_TERMINATION
398  case GNUTLS_E_PREMATURE_TERMINATION:
399 #endif
400  break;
401  case GNUTLS_E_WARNING_ALERT_RECEIVED:
402  av_log(h, AV_LOG_WARNING, "%s\n", gnutls_strerror(ret));
403  break;
404  default:
405  av_log(h, AV_LOG_ERROR, "%s\n", gnutls_strerror(ret));
406  break;
407  }
408  if (c->io_err) {
409  av_log(h, AV_LOG_ERROR, "IO error: %s\n", av_err2str(c->io_err));
410  ret = c->io_err;
411  c->io_err = 0;
412  return ret;
413  }
414  return AVERROR(EIO);
415 }
416 
417 static int tls_close(URLContext *h)
418 {
419  TLSContext *c = h->priv_data;
420  TLSShared *s = &c->tls_shared;
421  if (c->need_shutdown)
422  gnutls_bye(c->session, GNUTLS_SHUT_WR);
423  if (c->session)
424  gnutls_deinit(c->session);
425  if (c->cred)
426  gnutls_certificate_free_credentials(c->cred);
427  if (!s->external_sock)
428  ffurl_closep(s->is_dtls ? &s->udp : &s->tcp);
430  return 0;
431 }
432 
433 static ssize_t gnutls_url_pull(gnutls_transport_ptr_t transport,
434  void *buf, size_t len)
435 {
436  TLSContext *c = (TLSContext*) transport;
437  TLSShared *s = &c->tls_shared;
438  URLContext *uc = s->is_dtls ? s->udp : s->tcp;
439  int ret = ffurl_read(uc, buf, len);
440  if (ret >= 0) {
441  if (s->is_dtls && s->listen && !c->dest_addr_len) {
442  int err_ret;
443 
444  ff_udp_get_last_recv_addr(s->udp, &c->dest_addr, &c->dest_addr_len);
445  err_ret = ff_udp_set_remote_addr(s->udp, (struct sockaddr *)&c->dest_addr, c->dest_addr_len, 1);
446  if (err_ret < 0) {
447  av_log(c, AV_LOG_ERROR, "Failed connecting udp context\n");
448  return err_ret;
449  }
450  av_log(c, AV_LOG_TRACE, "Set UDP remote addr on UDP socket, now 'connected'\n");
451  }
452  return ret;
453  }
454  if (ret == AVERROR_EXIT)
455  return 0;
456  if (ret == AVERROR(EAGAIN)) {
457  errno = EAGAIN;
458  } else {
459  errno = EIO;
460  c->io_err = ret;
461  }
462  return -1;
463 }
464 
465 static ssize_t gnutls_url_push(gnutls_transport_ptr_t transport,
466  const void *buf, size_t len)
467 {
468  TLSContext *c = (TLSContext*) transport;
469  TLSShared *s = &c->tls_shared;
470  URLContext *uc = s->is_dtls ? s->udp : s->tcp;
471  int ret = ffurl_write(uc, buf, len);
472  if (ret >= 0)
473  return ret;
474  if (ret == AVERROR_EXIT)
475  return 0;
476  if (ret == AVERROR(EAGAIN)) {
477  errno = EAGAIN;
478  } else {
479  errno = EIO;
480  c->io_err = ret;
481  }
482  return -1;
483 }
484 
485 static int gnutls_pull_timeout(gnutls_transport_ptr_t ptr, unsigned int ms)
486 {
487  TLSContext *c = (TLSContext*) ptr;
488  TLSShared *s = &c->tls_shared;
489  int ret;
490  int sockfd = ffurl_get_file_handle(s->udp);
491  struct pollfd pfd = { .fd = sockfd, .events = POLLIN, .revents = 0 };
492 
493  if (sockfd < 0)
494  return 0;
495 
496  ret = poll(&pfd, 1, ms);
497  if (ret <= 0)
498  return ret;
499  return 1;
500 }
501 
503 {
504  TLSContext *c = h->priv_data;
505  TLSShared *s = &c->tls_shared;
506  URLContext *uc = s->is_dtls ? s->udp : s->tcp;
507  int ret;
508 
509  uc->flags &= ~AVIO_FLAG_NONBLOCK;
510 
511  do {
512  if (ff_check_interrupt(&h->interrupt_callback)) {
513  ret = AVERROR_EXIT;
514  goto end;
515  }
516 
517  ret = gnutls_handshake(c->session);
518  if (gnutls_error_is_fatal(ret)) {
519  ret = print_tls_error(h, ret);
520  goto end;
521  }
522  } while (ret);
523 
524 end:
525  return ret;
526 }
527 
528 static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
529 {
530  TLSContext *c = h->priv_data;
531  TLSShared *s = &c->tls_shared;
532  uint16_t gnutls_flags = 0;
533  gnutls_x509_crt_t cert = NULL;
534  gnutls_x509_privkey_t pkey = NULL;
535  int ret;
536 
537  ff_gnutls_init();
538 
539  if (!s->external_sock) {
540  if ((ret = ff_tls_open_underlying(s, h, uri, options)) < 0)
541  goto fail;
542  } else if (!s->host) {
543  /* With an external socket ff_tls_open_underlying() is skipped, so the
544  * host (used for SNI below) is never parsed. Parse it here to avoid
545  * passing a NULL s->host to gnutls_server_name_set(), which crashes. */
546  struct addrinfo hints = { .ai_flags = AI_NUMERICHOST }, *ai = NULL;
547  av_url_split(NULL, 0, NULL, 0, s->underlying_host, sizeof(s->underlying_host),
548  NULL, NULL, 0, uri);
549  if (!getaddrinfo(s->underlying_host, NULL, &hints, &ai)) {
550  s->numerichost = 1;
551  freeaddrinfo(ai);
552  }
553  if (!(s->host = av_strdup(s->underlying_host))) {
554  ret = AVERROR(ENOMEM);
555  goto fail;
556  }
557  }
558 
559  if (s->is_dtls)
560  gnutls_flags |= GNUTLS_DATAGRAM;
561 
562  if (s->listen)
563  gnutls_flags |= GNUTLS_SERVER;
564  else
565  gnutls_flags |= GNUTLS_CLIENT;
566  gnutls_init(&c->session, gnutls_flags);
567  if (!s->listen && !s->numerichost)
568  gnutls_server_name_set(c->session, GNUTLS_NAME_DNS, s->host, strlen(s->host));
569  gnutls_certificate_allocate_credentials(&c->cred);
570  if (s->ca_file) {
571  ret = gnutls_certificate_set_x509_trust_file(c->cred, s->ca_file, GNUTLS_X509_FMT_PEM);
572  if (ret < 0)
573  av_log(h, AV_LOG_ERROR, "%s\n", gnutls_strerror(ret));
574  }
575 #if GNUTLS_VERSION_NUMBER >= 0x030020
576  else
577  gnutls_certificate_set_x509_system_trust(c->cred);
578 #endif
579  gnutls_certificate_set_verify_flags(c->cred, s->verify ?
580  GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT : 0);
581  if (s->cert_file && s->key_file) {
582  ret = gnutls_certificate_set_x509_key_file(c->cred,
583  s->cert_file, s->key_file,
584  GNUTLS_X509_FMT_PEM);
585  if (ret < 0) {
587  "Unable to set cert/key files %s and %s: %s\n",
588  s->cert_file, s->key_file, gnutls_strerror(ret));
589  ret = AVERROR(EIO);
590  goto fail;
591  }
592  } else if (s->cert_file || s->key_file) {
593  av_log(h, AV_LOG_ERROR, "cert and key required\n");
594  } else if (s->cert_buf && s->key_buf) {
595  gnutls_datum_t cert_data = { .data = s->cert_buf, .size = strlen(s->cert_buf)};
596  gnutls_datum_t pkey_data = { .data = s->key_buf, .size = strlen(s->key_buf)};
597  ret = gnutls_certificate_set_x509_key_mem(c->cred, &cert_data, &pkey_data, GNUTLS_X509_FMT_PEM);
598  if (ret < 0) {
599  av_log(h, AV_LOG_ERROR, "Unable to set cert/key memory: %s\n", gnutls_strerror(ret));
600  ret = AVERROR(EINVAL);
601  goto fail;
602  }
603  } else if (s->cert_buf || s->key_buf) {
604  av_log(h, AV_LOG_ERROR, "cert and key required\n");
605  }
606 
607  if (s->listen && !s->cert_file && !s->cert_buf && !s->key_file && !s->key_buf) {
608  av_log(h, AV_LOG_VERBOSE, "No server certificate provided, using self-signed\n");
609 
610  ret = gnutls_gen_private_key(&pkey);
611  if (ret < 0)
612  goto fail;
613 
614  ret = gnutls_gen_certificate(pkey, &cert, NULL);
615  if (ret < 0)
616  goto fail;
617 
618  ret = gnutls_certificate_set_x509_key(c->cred, &cert, 1, pkey);
619  if (ret < 0) {
620  av_log(h, AV_LOG_ERROR, "Unable to set self-signed certificate: %s\n", gnutls_strerror(ret));
621  ret = AVERROR(EINVAL);
622  goto fail;
623  }
624  }
625  gnutls_credentials_set(c->session, GNUTLS_CRD_CERTIFICATE, c->cred);
626  gnutls_transport_set_pull_function(c->session, gnutls_url_pull);
627  gnutls_transport_set_push_function(c->session, gnutls_url_push);
628  gnutls_transport_set_ptr(c->session, c);
629  if (s->is_dtls) {
630  gnutls_transport_set_pull_timeout_function(c->session, gnutls_pull_timeout);
631  if (s->mtu)
632  gnutls_dtls_set_mtu(c->session, s->mtu);
633  }
634  gnutls_set_default_priority(c->session);
635 
636  if (s->use_srtp) {
637  ret = gnutls_srtp_set_profile(c->session, GNUTLS_SRTP_AES128_CM_HMAC_SHA1_80);
638  if (ret < 0) {
639  av_log(c, AV_LOG_ERROR, "Unable to set SRTP profile: %s\n", gnutls_strerror(ret));
640  ret = AVERROR(EINVAL);
641  goto fail;
642  }
643  }
644 
645  if (!s->external_sock) {
646  ret = tls_handshake(h);
647  if (ret < 0)
648  goto fail;
649  }
650  c->need_shutdown = 1;
651  if (s->verify) {
652  unsigned int status, cert_list_size;
653  gnutls_x509_crt_t cert;
654  const gnutls_datum_t *cert_list;
655  if ((ret = gnutls_certificate_verify_peers2(c->session, &status)) < 0) {
656  av_log(h, AV_LOG_ERROR, "Unable to verify peer certificate: %s\n",
657  gnutls_strerror(ret));
658  ret = AVERROR(EIO);
659  goto fail;
660  }
661  if (status & GNUTLS_CERT_INVALID) {
662  av_log(h, AV_LOG_ERROR, "Peer certificate failed verification\n");
663  ret = AVERROR(EIO);
664  goto fail;
665  }
666  if (gnutls_certificate_type_get(c->session) != GNUTLS_CRT_X509) {
667  av_log(h, AV_LOG_ERROR, "Unsupported certificate type\n");
668  ret = AVERROR(EIO);
669  goto fail;
670  }
671  gnutls_x509_crt_init(&cert);
672  cert_list = gnutls_certificate_get_peers(c->session, &cert_list_size);
673  gnutls_x509_crt_import(cert, cert_list, GNUTLS_X509_FMT_DER);
674  ret = gnutls_x509_crt_check_hostname(cert, s->host);
675  gnutls_x509_crt_deinit(cert);
676  if (!ret) {
678  "The certificate's owner does not match hostname %s\n", s->host);
679  ret = AVERROR(EIO);
680  goto fail;
681  }
682  }
683 
684  return 0;
685 fail:
686  if (cert)
687  gnutls_x509_crt_deinit(cert);
688  if (pkey)
689  gnutls_x509_privkey_deinit(pkey);
690  tls_close(h);
691  return ret;
692 }
693 
694 static int dtls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
695 {
696  TLSContext *c = h->priv_data;
697  TLSShared *s = &c->tls_shared;
698  s->is_dtls = 1;
699  return tls_open(h, uri, flags, options);
700 }
701 
702 static int tls_read(URLContext *h, uint8_t *buf, int size)
703 {
704  TLSContext *c = h->priv_data;
705  TLSShared *s = &c->tls_shared;
706  URLContext *uc = s->is_dtls ? s->udp : s->tcp;
707  int ret;
708  // Set or clear the AVIO_FLAG_NONBLOCK on the underlying socket
709  uc->flags &= ~AVIO_FLAG_NONBLOCK;
710  uc->flags |= h->flags & AVIO_FLAG_NONBLOCK;
711  ret = gnutls_record_recv(c->session, buf, size);
712  if (ret > 0)
713  return ret;
714  if (ret == 0)
715  return AVERROR_EOF;
716  return print_tls_error(h, ret);
717 }
718 
719 static int tls_write(URLContext *h, const uint8_t *buf, int size)
720 {
721  TLSContext *c = h->priv_data;
722  TLSShared *s = &c->tls_shared;
723  URLContext *uc = s->is_dtls ? s->udp : s->tcp;
724  int ret;
725  // Set or clear the AVIO_FLAG_NONBLOCK on the underlying socket
726  uc->flags &= ~AVIO_FLAG_NONBLOCK;
727  uc->flags |= h->flags & AVIO_FLAG_NONBLOCK;
728 
729  if (s->is_dtls) {
730  const size_t mtu_size = gnutls_dtls_get_data_mtu(c->session);
731  size = FFMIN(size, mtu_size);
732  }
733 
734  ret = gnutls_record_send(c->session, buf, size);
735  if (ret > 0)
736  return ret;
737  if (ret == 0)
738  return AVERROR_EOF;
739  return print_tls_error(h, ret);
740 }
741 
743 {
744  TLSContext *c = h->priv_data;
745  return ffurl_get_file_handle(c->tls_shared.tcp);
746 }
747 
749 {
750  TLSContext *s = h->priv_data;
751  return ffurl_get_short_seek(s->tls_shared.tcp);
752 }
753 
754 static const AVOption options[] = {
755  TLS_COMMON_OPTIONS(TLSContext, tls_shared),
756  { NULL }
757 };
758 
759 static const AVClass tls_class = {
760  .class_name = "tls",
761  .item_name = av_default_item_name,
762  .option = options,
763  .version = LIBAVUTIL_VERSION_INT,
764 };
765 
767  .name = "tls",
768  .url_open2 = tls_open,
769  .url_read = tls_read,
770  .url_write = tls_write,
771  .url_close = tls_close,
772  .url_get_file_handle = tls_get_file_handle,
773  .url_get_short_seek = tls_get_short_seek,
774  .priv_data_size = sizeof(TLSContext),
776  .priv_data_class = &tls_class,
777 };
778 
779 static const AVClass dtls_class = {
780  .class_name = "dtls",
781  .item_name = av_default_item_name,
782  .option = options,
783  .version = LIBAVUTIL_VERSION_INT,
784 };
785 
787  .name = "dtls",
788  .url_open2 = dtls_open,
789  .url_handshake = tls_handshake,
790  .url_read = tls_read,
791  .url_write = tls_write,
792  .url_close = tls_close,
793  .url_get_file_handle = tls_get_file_handle,
794  .url_get_short_seek = tls_get_short_seek,
795  .priv_data_size = sizeof(TLSContext),
797  .priv_data_class = &dtls_class,
798 };
ff_gnutls_init
void ff_gnutls_init(void)
Definition: tls_gnutls.c:346
flags
const SwsFlags flags[]
Definition: swscale.c:61
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
TLSContext
Definition: tls_gnutls.c:334
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
gnutls_x509_fingerprint
static int gnutls_x509_fingerprint(gnutls_x509_crt_t cert, char **fingerprint)
Definition: tls_gnutls.c:90
ff_ssl_gen_key_cert
int ff_ssl_gen_key_cert(char *key_buf, size_t key_sz, char *cert_buf, size_t cert_sz, char **fingerprint)
Definition: tls_gnutls.c:297
URL_PROTOCOL_FLAG_NETWORK
#define URL_PROTOCOL_FLAG_NETWORK
Definition: url.h:33
out
static FILE * out
Definition: movenc.c:55
gnutls_url_pull
static ssize_t gnutls_url_pull(gnutls_transport_ptr_t transport, void *buf, size_t len)
Definition: tls_gnutls.c:433
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
thread.h
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
md
#define md
Definition: vf_colormatrix.c:101
ff_ssl_read_key_cert
int ff_ssl_read_key_cert(char *key_url, char *crt_url, char *key_buf, size_t key_sz, char *crt_buf, size_t crt_sz, char **fingerprint)
Definition: tls_gnutls.c:113
print_tls_error
static int print_tls_error(URLContext *h, int ret)
Definition: tls_gnutls.c:390
AVOption
AVOption.
Definition: opt.h:429
ff_dtls_export_materials
int ff_dtls_export_materials(URLContext *h, char *dtls_srtp_materials, size_t materials_sz)
Definition: tls_gnutls.c:377
tls_class
static const AVClass tls_class
Definition: tls_gnutls.c:759
ff_tls_set_external_socket
int ff_tls_set_external_socket(URLContext *h, URLContext *sock)
Definition: tls_gnutls.c:364
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:226
tls_write
static int tls_write(URLContext *h, const uint8_t *buf, int size)
Definition: tls_gnutls.c:719
AVDictionary
Definition: dict.c:32
URLProtocol
Definition: url.h:51
os_support.h
AV_WB64
#define AV_WB64(p, v)
Definition: intreadwrite.h:429
sockaddr_storage
Definition: network.h:111
av_get_random_seed
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:196
ff_mutex_unlock
static int ff_mutex_unlock(AVMutex *mutex)
Definition: thread.h:189
TLSContext::cred
gnutls_certificate_credentials_t cred
Definition: tls_gnutls.c:337
freeaddrinfo
#define freeaddrinfo
Definition: network.h:218
TLS_COMMON_OPTIONS
#define TLS_COMMON_OPTIONS(pstruct, options_field)
Definition: tls.h:88
gnutls_gen_certificate
static int gnutls_gen_certificate(gnutls_x509_privkey_t key, gnutls_x509_crt_t *crt, char **fingerprint)
Definition: tls_gnutls.c:216
fail
#define fail()
Definition: checkasm.h:219
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
gnutls_mutex
static AVMutex gnutls_mutex
Definition: tls_gnutls.c:344
ff_check_interrupt
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrupt a blocking function associated with cb.
Definition: avio.c:855
crt_to_pem_string
static int crt_to_pem_string(gnutls_x509_crt_t crt, char *out, size_t out_sz)
Definition: tls_gnutls.c:70
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:236
tls_open
static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
Definition: tls_gnutls.c:528
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
AVMutex
#define AVMutex
Definition: thread.h:184
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AI_NUMERICHOST
#define AI_NUMERICHOST
Definition: network.h:187
URLContext::flags
int flags
Definition: url.h:40
key
const char * key
Definition: hwcontext_opencl.c:189
tmp
static uint8_t tmp[40]
Definition: aes_ctr.c:52
dtls_class
static const AVClass dtls_class
Definition: tls_gnutls.c:779
tls_close
static int tls_close(URLContext *h)
Definition: tls_gnutls.c:417
ff_udp_set_remote_addr
int ff_udp_set_remote_addr(URLContext *h, const struct sockaddr *dest_addr, socklen_t dest_addr_len, int do_connect)
This function is identical to ff_udp_set_remote_url, except that it takes a sockaddr directly.
Definition: udp.c:472
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:242
ff_udp_get_last_recv_addr
void ff_udp_get_last_recv_addr(URLContext *h, struct sockaddr_storage *addr, socklen_t *addr_len)
Definition: udp.c:510
options
Definition: swscale.c:43
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
pkey_to_pem_string
static int pkey_to_pem_string(gnutls_x509_privkey_t key, char *out, size_t out_sz)
Definition: tls_gnutls.c:50
ff_url_read_all
int ff_url_read_all(const char *url, AVBPrint *bp)
Read all data from the given URL url and store it in the given buffer bp.
Definition: tls.c:116
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:122
AV_MUTEX_INITIALIZER
#define AV_MUTEX_INITIALIZER
Definition: thread.h:185
size
int size
Definition: twinvq_data.h:10344
TLSContext::tls_shared
TLSShared tls_shared
Definition: tls_gnutls.c:335
URLProtocol::name
const char * name
Definition: url.h:52
gnutls_gen_private_key
static int gnutls_gen_private_key(gnutls_x509_privkey_t *key)
Definition: tls_gnutls.c:190
TLSContext::io_err
int io_err
Definition: tls_gnutls.c:339
tls_get_file_handle
static int tls_get_file_handle(URLContext *h)
Definition: tls_gnutls.c:742
gnutls_url_push
static ssize_t gnutls_url_push(gnutls_transport_ptr_t transport, const void *buf, size_t len)
Definition: tls_gnutls.c:465
ff_mutex_lock
static int ff_mutex_lock(AVMutex *mutex)
Definition: thread.h:188
getaddrinfo
#define getaddrinfo
Definition: network.h:217
TLSContext::dest_addr
struct sockaddr_storage dest_addr
Definition: tls_gnutls.c:340
tls_handshake
static int tls_handshake(URLContext *h)
Definition: tls_gnutls.c:502
tls_get_short_seek
static int tls_get_short_seek(URLContext *h)
Definition: tls_gnutls.c:748
URLContext
Definition: url.h:35
dtls_open
static int dtls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
Definition: tls_gnutls.c:694
TLSContext::dest_addr_len
socklen_t dest_addr_len
Definition: tls_gnutls.c:341
av_url_split
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
Definition: utils.c:361
ff_tls_protocol
const URLProtocol ff_tls_protocol
Definition: tls_gnutls.c:766
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
url.h
len
int len
Definition: vorbis_enc_data.h:426
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
avformat.h
av_bprintf
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:122
network.h
tls.h
status
ov_status_e status
Definition: dnn_backend_openvino.c:100
random_seed.h
addrinfo::ai_flags
int ai_flags
Definition: network.h:138
ff_dtls_protocol
const URLProtocol ff_dtls_protocol
Definition: tls_gnutls.c:786
TLSContext::need_shutdown
int need_shutdown
Definition: tls_gnutls.c:338
options
static const AVOption options[]
Definition: tls_gnutls.c:754
gnutls_pull_timeout
static int gnutls_pull_timeout(gnutls_transport_ptr_t ptr, unsigned int ms)
Definition: tls_gnutls.c:485
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:272
mem.h
MAX_CERTIFICATE_SIZE
#define MAX_CERTIFICATE_SIZE
Maximum size limit of a certificate and private key size.
Definition: tls.h:35
tls_read
static int tls_read(URLContext *h, uint8_t *buf, int size)
Definition: tls_gnutls.c:702
TLSContext::session
gnutls_session_t session
Definition: tls_gnutls.c:336
TLSShared
Definition: tls.h:37
AVIO_FLAG_NONBLOCK
#define AVIO_FLAG_NONBLOCK
Use non-blocking mode.
Definition: avio.h:636
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
MAX_MD_SIZE
#define MAX_MD_SIZE
Definition: tls_gnutls.c:48
addrinfo
Definition: network.h:137
ff_gnutls_deinit
void ff_gnutls_deinit(void)
Definition: tls_gnutls.c:357
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