You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 lines
8.8KB

  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. #include <errno.h>
  22. #include <gnutls/gnutls.h>
  23. #include <gnutls/x509.h>
  24. #include "avformat.h"
  25. #include "internal.h"
  26. #include "network.h"
  27. #include "os_support.h"
  28. #include "url.h"
  29. #include "tls.h"
  30. #include "libavcodec/internal.h"
  31. #include "libavutil/avstring.h"
  32. #include "libavutil/opt.h"
  33. #include "libavutil/parseutils.h"
  34. #ifndef GNUTLS_VERSION_NUMBER
  35. #define GNUTLS_VERSION_NUMBER LIBGNUTLS_VERSION_NUMBER
  36. #endif
  37. #if HAVE_THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
  38. #include <gcrypt.h>
  39. #include "libavutil/thread.h"
  40. GCRY_THREAD_OPTION_PTHREAD_IMPL;
  41. #endif
  42. typedef struct TLSContext {
  43. const AVClass *class;
  44. TLSShared tls_shared;
  45. gnutls_session_t session;
  46. gnutls_certificate_credentials_t cred;
  47. int need_shutdown;
  48. } TLSContext;
  49. void ff_gnutls_init(void)
  50. {
  51. ff_lock_avformat();
  52. #if HAVE_THREADS && GNUTLS_VERSION_NUMBER < 0x020b00
  53. if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
  54. gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
  55. #endif
  56. gnutls_global_init();
  57. ff_unlock_avformat();
  58. }
  59. void ff_gnutls_deinit(void)
  60. {
  61. ff_lock_avformat();
  62. gnutls_global_deinit();
  63. ff_unlock_avformat();
  64. }
  65. static int print_tls_error(URLContext *h, int ret)
  66. {
  67. switch (ret) {
  68. case GNUTLS_E_AGAIN:
  69. return AVERROR(EAGAIN);
  70. case GNUTLS_E_INTERRUPTED:
  71. #ifdef GNUTLS_E_PREMATURE_TERMINATION
  72. case GNUTLS_E_PREMATURE_TERMINATION:
  73. #endif
  74. break;
  75. case GNUTLS_E_WARNING_ALERT_RECEIVED:
  76. av_log(h, AV_LOG_WARNING, "%s\n", gnutls_strerror(ret));
  77. break;
  78. default:
  79. av_log(h, AV_LOG_ERROR, "%s\n", gnutls_strerror(ret));
  80. break;
  81. }
  82. return AVERROR(EIO);
  83. }
  84. static int tls_close(URLContext *h)
  85. {
  86. TLSContext *c = h->priv_data;
  87. if (c->need_shutdown)
  88. gnutls_bye(c->session, GNUTLS_SHUT_WR);
  89. if (c->session)
  90. gnutls_deinit(c->session);
  91. if (c->cred)
  92. gnutls_certificate_free_credentials(c->cred);
  93. if (c->tls_shared.tcp)
  94. ffurl_close(c->tls_shared.tcp);
  95. ff_gnutls_deinit();
  96. return 0;
  97. }
  98. static ssize_t gnutls_url_pull(gnutls_transport_ptr_t transport,
  99. void *buf, size_t len)
  100. {
  101. URLContext *h = (URLContext*) transport;
  102. int ret = ffurl_read(h, buf, len);
  103. if (ret >= 0)
  104. return ret;
  105. if (ret == AVERROR_EXIT)
  106. return 0;
  107. if (ret == AVERROR(EAGAIN))
  108. errno = EAGAIN;
  109. else
  110. errno = EIO;
  111. return -1;
  112. }
  113. static ssize_t gnutls_url_push(gnutls_transport_ptr_t transport,
  114. const void *buf, size_t len)
  115. {
  116. URLContext *h = (URLContext*) transport;
  117. int ret = ffurl_write(h, buf, len);
  118. if (ret >= 0)
  119. return ret;
  120. if (ret == AVERROR_EXIT)
  121. return 0;
  122. if (ret == AVERROR(EAGAIN))
  123. errno = EAGAIN;
  124. else
  125. errno = EIO;
  126. return -1;
  127. }
  128. static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
  129. {
  130. TLSContext *p = h->priv_data;
  131. TLSShared *c = &p->tls_shared;
  132. int ret;
  133. ff_gnutls_init();
  134. if ((ret = ff_tls_open_underlying(c, h, uri, options)) < 0)
  135. goto fail;
  136. gnutls_init(&p->session, c->listen ? GNUTLS_SERVER : GNUTLS_CLIENT);
  137. if (!c->listen && !c->numerichost)
  138. gnutls_server_name_set(p->session, GNUTLS_NAME_DNS, c->host, strlen(c->host));
  139. gnutls_certificate_allocate_credentials(&p->cred);
  140. if (c->ca_file) {
  141. ret = gnutls_certificate_set_x509_trust_file(p->cred, c->ca_file, GNUTLS_X509_FMT_PEM);
  142. if (ret < 0)
  143. av_log(h, AV_LOG_ERROR, "%s\n", gnutls_strerror(ret));
  144. }
  145. #if GNUTLS_VERSION_NUMBER >= 0x030020
  146. else
  147. gnutls_certificate_set_x509_system_trust(p->cred);
  148. #endif
  149. gnutls_certificate_set_verify_flags(p->cred, c->verify ?
  150. GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT : 0);
  151. if (c->cert_file && c->key_file) {
  152. ret = gnutls_certificate_set_x509_key_file(p->cred,
  153. c->cert_file, c->key_file,
  154. GNUTLS_X509_FMT_PEM);
  155. if (ret < 0) {
  156. av_log(h, AV_LOG_ERROR,
  157. "Unable to set cert/key files %s and %s: %s\n",
  158. c->cert_file, c->key_file, gnutls_strerror(ret));
  159. ret = AVERROR(EIO);
  160. goto fail;
  161. }
  162. } else if (c->cert_file || c->key_file)
  163. av_log(h, AV_LOG_ERROR, "cert and key required\n");
  164. gnutls_credentials_set(p->session, GNUTLS_CRD_CERTIFICATE, p->cred);
  165. gnutls_transport_set_pull_function(p->session, gnutls_url_pull);
  166. gnutls_transport_set_push_function(p->session, gnutls_url_push);
  167. gnutls_transport_set_ptr(p->session, c->tcp);
  168. gnutls_priority_set_direct(p->session, "NORMAL", NULL);
  169. do {
  170. if (ff_check_interrupt(&h->interrupt_callback)) {
  171. ret = AVERROR_EXIT;
  172. goto fail;
  173. }
  174. ret = gnutls_handshake(p->session);
  175. if (gnutls_error_is_fatal(ret)) {
  176. ret = print_tls_error(h, ret);
  177. goto fail;
  178. }
  179. } while (ret);
  180. p->need_shutdown = 1;
  181. if (c->verify) {
  182. unsigned int status, cert_list_size;
  183. gnutls_x509_crt_t cert;
  184. const gnutls_datum_t *cert_list;
  185. if ((ret = gnutls_certificate_verify_peers2(p->session, &status)) < 0) {
  186. av_log(h, AV_LOG_ERROR, "Unable to verify peer certificate: %s\n",
  187. gnutls_strerror(ret));
  188. ret = AVERROR(EIO);
  189. goto fail;
  190. }
  191. if (status & GNUTLS_CERT_INVALID) {
  192. av_log(h, AV_LOG_ERROR, "Peer certificate failed verification\n");
  193. ret = AVERROR(EIO);
  194. goto fail;
  195. }
  196. if (gnutls_certificate_type_get(p->session) != GNUTLS_CRT_X509) {
  197. av_log(h, AV_LOG_ERROR, "Unsupported certificate type\n");
  198. ret = AVERROR(EIO);
  199. goto fail;
  200. }
  201. gnutls_x509_crt_init(&cert);
  202. cert_list = gnutls_certificate_get_peers(p->session, &cert_list_size);
  203. gnutls_x509_crt_import(cert, cert_list, GNUTLS_X509_FMT_DER);
  204. ret = gnutls_x509_crt_check_hostname(cert, c->host);
  205. gnutls_x509_crt_deinit(cert);
  206. if (!ret) {
  207. av_log(h, AV_LOG_ERROR,
  208. "The certificate's owner does not match hostname %s\n", c->host);
  209. ret = AVERROR(EIO);
  210. goto fail;
  211. }
  212. }
  213. return 0;
  214. fail:
  215. tls_close(h);
  216. return ret;
  217. }
  218. static int tls_read(URLContext *h, uint8_t *buf, int size)
  219. {
  220. TLSContext *c = h->priv_data;
  221. int ret;
  222. // Set or clear the AVIO_FLAG_NONBLOCK on c->tls_shared.tcp
  223. c->tls_shared.tcp->flags &= ~AVIO_FLAG_NONBLOCK;
  224. c->tls_shared.tcp->flags |= h->flags & AVIO_FLAG_NONBLOCK;
  225. ret = gnutls_record_recv(c->session, buf, size);
  226. if (ret > 0)
  227. return ret;
  228. if (ret == 0)
  229. return AVERROR_EOF;
  230. return print_tls_error(h, ret);
  231. }
  232. static int tls_write(URLContext *h, const uint8_t *buf, int size)
  233. {
  234. TLSContext *c = h->priv_data;
  235. int ret;
  236. // Set or clear the AVIO_FLAG_NONBLOCK on c->tls_shared.tcp
  237. c->tls_shared.tcp->flags &= ~AVIO_FLAG_NONBLOCK;
  238. c->tls_shared.tcp->flags |= h->flags & AVIO_FLAG_NONBLOCK;
  239. ret = gnutls_record_send(c->session, buf, size);
  240. if (ret > 0)
  241. return ret;
  242. if (ret == 0)
  243. return AVERROR_EOF;
  244. return print_tls_error(h, ret);
  245. }
  246. static int tls_get_file_handle(URLContext *h)
  247. {
  248. TLSContext *c = h->priv_data;
  249. return ffurl_get_file_handle(c->tls_shared.tcp);
  250. }
  251. static const AVOption options[] = {
  252. TLS_COMMON_OPTIONS(TLSContext, tls_shared),
  253. { NULL }
  254. };
  255. static const AVClass tls_class = {
  256. .class_name = "tls",
  257. .item_name = av_default_item_name,
  258. .option = options,
  259. .version = LIBAVUTIL_VERSION_INT,
  260. };
  261. const URLProtocol ff_tls_protocol = {
  262. .name = "tls",
  263. .url_open2 = tls_open,
  264. .url_read = tls_read,
  265. .url_write = tls_write,
  266. .url_close = tls_close,
  267. .url_get_file_handle = tls_get_file_handle,
  268. .priv_data_size = sizeof(TLSContext),
  269. .flags = URL_PROTOCOL_FLAG_NETWORK,
  270. .priv_data_class = &tls_class,
  271. };