tls.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 "avformat.h"
  22. #include "url.h"
  23. #include "libavutil/avstring.h"
  24. #if CONFIG_GNUTLS
  25. #include <gnutls/gnutls.h>
  26. #define TLS_read(c, buf, size) gnutls_record_recv(c->session, buf, size)
  27. #define TLS_write(c, buf, size) gnutls_record_send(c->session, buf, size)
  28. #define TLS_shutdown(c) gnutls_bye(c->session, GNUTLS_SHUT_RDWR)
  29. #define TLS_free(c) do { \
  30. if (c->session) \
  31. gnutls_deinit(c->session); \
  32. if (c->cred) \
  33. gnutls_certificate_free_credentials(c->cred); \
  34. } while (0)
  35. #elif CONFIG_OPENSSL
  36. #include <openssl/bio.h>
  37. #include <openssl/ssl.h>
  38. #include <openssl/err.h>
  39. #define TLS_read(c, buf, size) SSL_read(c->ssl, buf, size)
  40. #define TLS_write(c, buf, size) SSL_write(c->ssl, buf, size)
  41. #define TLS_shutdown(c) SSL_shutdown(c->ssl)
  42. #define TLS_free(c) do { \
  43. if (c->ssl) \
  44. SSL_free(c->ssl); \
  45. if (c->ctx) \
  46. SSL_CTX_free(c->ctx); \
  47. } while (0)
  48. #endif
  49. #include "network.h"
  50. #include "os_support.h"
  51. #include "internal.h"
  52. #if HAVE_POLL_H
  53. #include <poll.h>
  54. #endif
  55. typedef struct {
  56. const AVClass *class;
  57. URLContext *tcp;
  58. #if CONFIG_GNUTLS
  59. gnutls_session_t session;
  60. gnutls_certificate_credentials_t cred;
  61. #elif CONFIG_OPENSSL
  62. SSL_CTX *ctx;
  63. SSL *ssl;
  64. #endif
  65. int fd;
  66. } TLSContext;
  67. static int do_tls_poll(URLContext *h, int ret)
  68. {
  69. TLSContext *c = h->priv_data;
  70. struct pollfd p = { c->fd, 0, 0 };
  71. #if CONFIG_GNUTLS
  72. if (ret != GNUTLS_E_AGAIN && ret != GNUTLS_E_INTERRUPTED) {
  73. av_log(h, AV_LOG_ERROR, "%s\n", gnutls_strerror(ret));
  74. return AVERROR(EIO);
  75. }
  76. if (gnutls_record_get_direction(c->session))
  77. p.events = POLLOUT;
  78. else
  79. p.events = POLLIN;
  80. #elif CONFIG_OPENSSL
  81. ret = SSL_get_error(c->ssl, ret);
  82. if (ret == SSL_ERROR_WANT_READ) {
  83. p.events = POLLIN;
  84. } else if (ret == SSL_ERROR_WANT_WRITE) {
  85. p.events = POLLOUT;
  86. } else {
  87. av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  88. return AVERROR(EIO);
  89. }
  90. #endif
  91. if (h->flags & AVIO_FLAG_NONBLOCK)
  92. return AVERROR(EAGAIN);
  93. while (1) {
  94. int n = poll(&p, 1, 100);
  95. if (n > 0)
  96. break;
  97. if (ff_check_interrupt(&h->interrupt_callback))
  98. return AVERROR(EINTR);
  99. }
  100. return 0;
  101. }
  102. static int tls_open(URLContext *h, const char *uri, int flags)
  103. {
  104. TLSContext *c = h->priv_data;
  105. int ret;
  106. int port;
  107. char buf[200], host[200];
  108. int numerichost = 0;
  109. struct addrinfo hints = { 0 }, *ai = NULL;
  110. const char *proxy_path;
  111. int use_proxy;
  112. ff_tls_init();
  113. proxy_path = getenv("http_proxy");
  114. use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
  115. av_strstart(proxy_path, "http://", NULL);
  116. av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0, uri);
  117. ff_url_join(buf, sizeof(buf), "tcp", NULL, host, port, NULL);
  118. hints.ai_flags = AI_NUMERICHOST;
  119. if (!getaddrinfo(host, NULL, &hints, &ai)) {
  120. numerichost = 1;
  121. freeaddrinfo(ai);
  122. }
  123. if (use_proxy) {
  124. char proxy_host[200], proxy_auth[200], dest[200];
  125. int proxy_port;
  126. av_url_split(NULL, 0, proxy_auth, sizeof(proxy_auth),
  127. proxy_host, sizeof(proxy_host), &proxy_port, NULL, 0,
  128. proxy_path);
  129. ff_url_join(dest, sizeof(dest), NULL, NULL, host, port, NULL);
  130. ff_url_join(buf, sizeof(buf), "httpproxy", proxy_auth, proxy_host,
  131. proxy_port, "/%s", dest);
  132. }
  133. ret = ffurl_open(&c->tcp, buf, AVIO_FLAG_READ_WRITE,
  134. &h->interrupt_callback, NULL);
  135. if (ret)
  136. goto fail;
  137. c->fd = ffurl_get_file_handle(c->tcp);
  138. #if CONFIG_GNUTLS
  139. gnutls_init(&c->session, GNUTLS_CLIENT);
  140. if (!numerichost)
  141. gnutls_server_name_set(c->session, GNUTLS_NAME_DNS, host, strlen(host));
  142. gnutls_certificate_allocate_credentials(&c->cred);
  143. gnutls_certificate_set_verify_flags(c->cred, 0);
  144. gnutls_credentials_set(c->session, GNUTLS_CRD_CERTIFICATE, c->cred);
  145. gnutls_transport_set_ptr(c->session, (gnutls_transport_ptr_t)
  146. (intptr_t) c->fd);
  147. gnutls_priority_set_direct(c->session, "NORMAL", NULL);
  148. while (1) {
  149. ret = gnutls_handshake(c->session);
  150. if (ret == 0)
  151. break;
  152. if ((ret = do_tls_poll(h, ret)) < 0)
  153. goto fail;
  154. }
  155. #elif CONFIG_OPENSSL
  156. c->ctx = SSL_CTX_new(TLSv1_client_method());
  157. if (!c->ctx) {
  158. av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  159. ret = AVERROR(EIO);
  160. goto fail;
  161. }
  162. c->ssl = SSL_new(c->ctx);
  163. if (!c->ssl) {
  164. av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  165. ret = AVERROR(EIO);
  166. goto fail;
  167. }
  168. SSL_set_fd(c->ssl, c->fd);
  169. if (!numerichost)
  170. SSL_set_tlsext_host_name(c->ssl, host);
  171. while (1) {
  172. ret = SSL_connect(c->ssl);
  173. if (ret > 0)
  174. break;
  175. if (ret == 0) {
  176. av_log(h, AV_LOG_ERROR, "Unable to negotiate TLS/SSL session\n");
  177. ret = AVERROR(EIO);
  178. goto fail;
  179. }
  180. if ((ret = do_tls_poll(h, ret)) < 0)
  181. goto fail;
  182. }
  183. #endif
  184. return 0;
  185. fail:
  186. TLS_free(c);
  187. if (c->tcp)
  188. ffurl_close(c->tcp);
  189. ff_tls_deinit();
  190. return ret;
  191. }
  192. static int tls_read(URLContext *h, uint8_t *buf, int size)
  193. {
  194. TLSContext *c = h->priv_data;
  195. while (1) {
  196. int ret = TLS_read(c, buf, size);
  197. if (ret > 0)
  198. return ret;
  199. if (ret == 0)
  200. return AVERROR(EIO);
  201. if ((ret = do_tls_poll(h, ret)) < 0)
  202. return ret;
  203. }
  204. return 0;
  205. }
  206. static int tls_write(URLContext *h, const uint8_t *buf, int size)
  207. {
  208. TLSContext *c = h->priv_data;
  209. while (1) {
  210. int ret = TLS_write(c, buf, size);
  211. if (ret > 0)
  212. return ret;
  213. if (ret == 0)
  214. return AVERROR(EIO);
  215. if ((ret = do_tls_poll(h, ret)) < 0)
  216. return ret;
  217. }
  218. return 0;
  219. }
  220. static int tls_close(URLContext *h)
  221. {
  222. TLSContext *c = h->priv_data;
  223. TLS_shutdown(c);
  224. TLS_free(c);
  225. ffurl_close(c->tcp);
  226. ff_tls_deinit();
  227. return 0;
  228. }
  229. URLProtocol ff_tls_protocol = {
  230. .name = "tls",
  231. .url_open = 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. };