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