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.4KB

  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. switch (ret) {
  73. case GNUTLS_E_AGAIN:
  74. case GNUTLS_E_INTERRUPTED:
  75. break;
  76. case GNUTLS_E_WARNING_ALERT_RECEIVED:
  77. av_log(h, AV_LOG_WARNING, "%s\n", gnutls_strerror(ret));
  78. break;
  79. default:
  80. av_log(h, AV_LOG_ERROR, "%s\n", gnutls_strerror(ret));
  81. return AVERROR(EIO);
  82. }
  83. if (gnutls_record_get_direction(c->session))
  84. p.events = POLLOUT;
  85. else
  86. p.events = POLLIN;
  87. #elif CONFIG_OPENSSL
  88. ret = SSL_get_error(c->ssl, ret);
  89. if (ret == SSL_ERROR_WANT_READ) {
  90. p.events = POLLIN;
  91. } else if (ret == SSL_ERROR_WANT_WRITE) {
  92. p.events = POLLOUT;
  93. } else {
  94. av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  95. return AVERROR(EIO);
  96. }
  97. #endif
  98. if (h->flags & AVIO_FLAG_NONBLOCK)
  99. return AVERROR(EAGAIN);
  100. while (1) {
  101. int n = poll(&p, 1, 100);
  102. if (n > 0)
  103. break;
  104. if (ff_check_interrupt(&h->interrupt_callback))
  105. return AVERROR(EINTR);
  106. }
  107. return 0;
  108. }
  109. static int tls_open(URLContext *h, const char *uri, int flags)
  110. {
  111. TLSContext *c = h->priv_data;
  112. int ret;
  113. int port;
  114. char buf[200], host[200];
  115. int numerichost = 0;
  116. struct addrinfo hints = { 0 }, *ai = NULL;
  117. const char *proxy_path;
  118. int use_proxy;
  119. ff_tls_init();
  120. av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0, uri);
  121. ff_url_join(buf, sizeof(buf), "tcp", NULL, host, port, NULL);
  122. hints.ai_flags = AI_NUMERICHOST;
  123. if (!getaddrinfo(host, NULL, &hints, &ai)) {
  124. numerichost = 1;
  125. freeaddrinfo(ai);
  126. }
  127. proxy_path = getenv("http_proxy");
  128. use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), host) &&
  129. proxy_path != NULL && av_strstart(proxy_path, "http://", NULL);
  130. if (use_proxy) {
  131. char proxy_host[200], proxy_auth[200], dest[200];
  132. int proxy_port;
  133. av_url_split(NULL, 0, proxy_auth, sizeof(proxy_auth),
  134. proxy_host, sizeof(proxy_host), &proxy_port, NULL, 0,
  135. proxy_path);
  136. ff_url_join(dest, sizeof(dest), NULL, NULL, host, port, NULL);
  137. ff_url_join(buf, sizeof(buf), "httpproxy", proxy_auth, proxy_host,
  138. proxy_port, "/%s", dest);
  139. }
  140. ret = ffurl_open(&c->tcp, buf, AVIO_FLAG_READ_WRITE,
  141. &h->interrupt_callback, NULL);
  142. if (ret)
  143. goto fail;
  144. c->fd = ffurl_get_file_handle(c->tcp);
  145. #if CONFIG_GNUTLS
  146. gnutls_init(&c->session, GNUTLS_CLIENT);
  147. if (!numerichost)
  148. gnutls_server_name_set(c->session, GNUTLS_NAME_DNS, host, strlen(host));
  149. gnutls_certificate_allocate_credentials(&c->cred);
  150. gnutls_certificate_set_verify_flags(c->cred, 0);
  151. gnutls_credentials_set(c->session, GNUTLS_CRD_CERTIFICATE, c->cred);
  152. gnutls_transport_set_ptr(c->session, (gnutls_transport_ptr_t)
  153. (intptr_t) c->fd);
  154. gnutls_priority_set_direct(c->session, "NORMAL", NULL);
  155. while (1) {
  156. ret = gnutls_handshake(c->session);
  157. if (ret == 0)
  158. break;
  159. if ((ret = do_tls_poll(h, ret)) < 0)
  160. goto fail;
  161. }
  162. #elif CONFIG_OPENSSL
  163. c->ctx = SSL_CTX_new(TLSv1_client_method());
  164. if (!c->ctx) {
  165. av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  166. ret = AVERROR(EIO);
  167. goto fail;
  168. }
  169. c->ssl = SSL_new(c->ctx);
  170. if (!c->ssl) {
  171. av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  172. ret = AVERROR(EIO);
  173. goto fail;
  174. }
  175. SSL_set_fd(c->ssl, c->fd);
  176. if (!numerichost)
  177. SSL_set_tlsext_host_name(c->ssl, host);
  178. while (1) {
  179. ret = SSL_connect(c->ssl);
  180. if (ret > 0)
  181. break;
  182. if (ret == 0) {
  183. av_log(h, AV_LOG_ERROR, "Unable to negotiate TLS/SSL session\n");
  184. ret = AVERROR(EIO);
  185. goto fail;
  186. }
  187. if ((ret = do_tls_poll(h, ret)) < 0)
  188. goto fail;
  189. }
  190. #endif
  191. return 0;
  192. fail:
  193. TLS_free(c);
  194. if (c->tcp)
  195. ffurl_close(c->tcp);
  196. ff_tls_deinit();
  197. return ret;
  198. }
  199. static int tls_read(URLContext *h, uint8_t *buf, int size)
  200. {
  201. TLSContext *c = h->priv_data;
  202. while (1) {
  203. int ret = TLS_read(c, buf, size);
  204. if (ret > 0)
  205. return ret;
  206. if (ret == 0)
  207. return AVERROR_EOF;
  208. if ((ret = do_tls_poll(h, ret)) < 0)
  209. return ret;
  210. }
  211. return 0;
  212. }
  213. static int tls_write(URLContext *h, const uint8_t *buf, int size)
  214. {
  215. TLSContext *c = h->priv_data;
  216. while (1) {
  217. int ret = TLS_write(c, buf, size);
  218. if (ret > 0)
  219. return ret;
  220. if (ret == 0)
  221. return AVERROR_EOF;
  222. if ((ret = do_tls_poll(h, ret)) < 0)
  223. return ret;
  224. }
  225. return 0;
  226. }
  227. static int tls_close(URLContext *h)
  228. {
  229. TLSContext *c = h->priv_data;
  230. TLS_shutdown(c);
  231. TLS_free(c);
  232. ffurl_close(c->tcp);
  233. ff_tls_deinit();
  234. return 0;
  235. }
  236. URLProtocol ff_tls_protocol = {
  237. .name = "tls",
  238. .url_open = tls_open,
  239. .url_read = tls_read,
  240. .url_write = tls_write,
  241. .url_close = tls_close,
  242. .priv_data_size = sizeof(TLSContext),
  243. .flags = URL_PROTOCOL_FLAG_NETWORK,
  244. };