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.

270 lines
8.0KB

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