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.

260 lines
7.7KB

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