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.

256 lines
7.6KB

  1. /*
  2. * TLS/SSL Protocol
  3. * Copyright (c) 2011 Martin Storsjo
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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_WR);
  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. gnutls_certificate_set_x509_trust_file(p->cred, c->ca_file, GNUTLS_X509_FMT_PEM);
  129. #if GNUTLS_VERSION_MAJOR >= 3
  130. else
  131. gnutls_certificate_set_x509_system_trust(p->cred);
  132. #endif
  133. gnutls_certificate_set_verify_flags(p->cred, c->verify ?
  134. GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT : 0);
  135. if (c->cert_file && c->key_file) {
  136. ret = gnutls_certificate_set_x509_key_file(p->cred,
  137. c->cert_file, c->key_file,
  138. GNUTLS_X509_FMT_PEM);
  139. if (ret < 0) {
  140. av_log(h, AV_LOG_ERROR,
  141. "Unable to set cert/key files %s and %s: %s\n",
  142. c->cert_file, c->key_file, gnutls_strerror(ret));
  143. ret = AVERROR(EIO);
  144. goto fail;
  145. }
  146. }
  147. gnutls_credentials_set(p->session, GNUTLS_CRD_CERTIFICATE, p->cred);
  148. gnutls_transport_set_pull_function(p->session, gnutls_url_pull);
  149. gnutls_transport_set_push_function(p->session, gnutls_url_push);
  150. gnutls_transport_set_ptr(p->session, c->tcp);
  151. gnutls_priority_set_direct(p->session, "NORMAL", NULL);
  152. ret = gnutls_handshake(p->session);
  153. if (ret) {
  154. ret = print_tls_error(h, ret);
  155. goto fail;
  156. }
  157. p->need_shutdown = 1;
  158. if (c->verify) {
  159. unsigned int status, cert_list_size;
  160. gnutls_x509_crt_t cert;
  161. const gnutls_datum_t *cert_list;
  162. if ((ret = gnutls_certificate_verify_peers2(p->session, &status)) < 0) {
  163. av_log(h, AV_LOG_ERROR, "Unable to verify peer certificate: %s\n",
  164. gnutls_strerror(ret));
  165. ret = AVERROR(EIO);
  166. goto fail;
  167. }
  168. if (status & GNUTLS_CERT_INVALID) {
  169. av_log(h, AV_LOG_ERROR, "Peer certificate failed verification\n");
  170. ret = AVERROR(EIO);
  171. goto fail;
  172. }
  173. if (gnutls_certificate_type_get(p->session) != GNUTLS_CRT_X509) {
  174. av_log(h, AV_LOG_ERROR, "Unsupported certificate type\n");
  175. ret = AVERROR(EIO);
  176. goto fail;
  177. }
  178. gnutls_x509_crt_init(&cert);
  179. cert_list = gnutls_certificate_get_peers(p->session, &cert_list_size);
  180. gnutls_x509_crt_import(cert, cert_list, GNUTLS_X509_FMT_DER);
  181. ret = gnutls_x509_crt_check_hostname(cert, c->host);
  182. gnutls_x509_crt_deinit(cert);
  183. if (!ret) {
  184. av_log(h, AV_LOG_ERROR,
  185. "The certificate's owner does not match hostname %s\n", c->host);
  186. ret = AVERROR(EIO);
  187. goto fail;
  188. }
  189. }
  190. return 0;
  191. fail:
  192. tls_close(h);
  193. return ret;
  194. }
  195. static int tls_read(URLContext *h, uint8_t *buf, int size)
  196. {
  197. TLSContext *c = h->priv_data;
  198. int ret = gnutls_record_recv(c->session, buf, size);
  199. if (ret > 0)
  200. return ret;
  201. if (ret == 0)
  202. return AVERROR_EOF;
  203. return print_tls_error(h, ret);
  204. }
  205. static int tls_write(URLContext *h, const uint8_t *buf, int size)
  206. {
  207. TLSContext *c = h->priv_data;
  208. int ret = gnutls_record_send(c->session, buf, size);
  209. if (ret > 0)
  210. return ret;
  211. if (ret == 0)
  212. return AVERROR_EOF;
  213. return print_tls_error(h, ret);
  214. }
  215. static const AVOption options[] = {
  216. TLS_COMMON_OPTIONS(TLSContext, tls_shared),
  217. { NULL }
  218. };
  219. static const AVClass tls_class = {
  220. .class_name = "tls",
  221. .item_name = av_default_item_name,
  222. .option = options,
  223. .version = LIBAVUTIL_VERSION_INT,
  224. };
  225. const URLProtocol ff_tls_gnutls_protocol = {
  226. .name = "tls",
  227. .url_open2 = tls_open,
  228. .url_read = tls_read,
  229. .url_write = tls_write,
  230. .url_close = tls_close,
  231. .priv_data_size = sizeof(TLSContext),
  232. .flags = URL_PROTOCOL_FLAG_NETWORK,
  233. .priv_data_class = &tls_class,
  234. };