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.

235 lines
6.5KB

  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(NULL, 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(NULL, AV_LOG_ERROR, "%s\n", ERR_error_string(ret, NULL));
  88. return AVERROR(EIO);
  89. }
  90. #endif
  91. if (h->flags & URL_FLAG_NONBLOCK)
  92. return AVERROR(EAGAIN);
  93. while (1) {
  94. int n = poll(&p, 1, 100);
  95. if (n > 0)
  96. break;
  97. if (url_interrupt_cb())
  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. ff_tls_init();
  111. av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0, uri);
  112. ff_url_join(buf, sizeof(buf), "tcp", NULL, host, port, NULL);
  113. hints.ai_flags = AI_NUMERICHOST;
  114. if (!getaddrinfo(host, NULL, &hints, &ai)) {
  115. numerichost = 1;
  116. freeaddrinfo(ai);
  117. }
  118. ret = ffurl_open(&c->tcp, buf, AVIO_FLAG_READ_WRITE);
  119. if (ret)
  120. goto fail;
  121. c->fd = ffurl_get_file_handle(c->tcp);
  122. #if CONFIG_GNUTLS
  123. gnutls_init(&c->session, GNUTLS_CLIENT);
  124. if (!numerichost)
  125. gnutls_server_name_set(c->session, GNUTLS_NAME_DNS, host, strlen(host));
  126. gnutls_certificate_allocate_credentials(&c->cred);
  127. gnutls_certificate_set_verify_flags(c->cred, 0);
  128. gnutls_credentials_set(c->session, GNUTLS_CRD_CERTIFICATE, c->cred);
  129. gnutls_transport_set_ptr(c->session, (gnutls_transport_ptr_t)
  130. (intptr_t) c->fd);
  131. gnutls_priority_set_direct(c->session, "NORMAL", NULL);
  132. while (1) {
  133. ret = gnutls_handshake(c->session);
  134. if (ret == 0)
  135. break;
  136. if ((ret = do_tls_poll(h, ret)) < 0)
  137. goto fail;
  138. }
  139. #elif CONFIG_OPENSSL
  140. c->ctx = SSL_CTX_new(SSLv3_client_method());
  141. if (!c->ctx) {
  142. av_log(NULL, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  143. ret = AVERROR(EIO);
  144. goto fail;
  145. }
  146. c->ssl = SSL_new(c->ctx);
  147. if (!c->ssl) {
  148. av_log(NULL, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  149. ret = AVERROR(EIO);
  150. goto fail;
  151. }
  152. SSL_set_fd(c->ssl, c->fd);
  153. if (!numerichost)
  154. SSL_set_tlsext_host_name(c->ssl, host);
  155. while (1) {
  156. ret = SSL_connect(c->ssl);
  157. if (ret > 0)
  158. break;
  159. if (ret == 0) {
  160. av_log(NULL, AV_LOG_ERROR, "Unable to negotiate TLS/SSL session\n");
  161. ret = AVERROR(EIO);
  162. goto fail;
  163. }
  164. if ((ret = do_tls_poll(h, ret)) < 0)
  165. goto fail;
  166. }
  167. #endif
  168. return 0;
  169. fail:
  170. TLS_free(c);
  171. if (c->tcp)
  172. ffurl_close(c->tcp);
  173. ff_tls_deinit();
  174. return ret;
  175. }
  176. static int tls_read(URLContext *h, uint8_t *buf, int size)
  177. {
  178. TLSContext *c = h->priv_data;
  179. while (1) {
  180. int ret = TLS_read(c, buf, size);
  181. if (ret > 0)
  182. return ret;
  183. if (ret == 0)
  184. return AVERROR(EIO);
  185. if ((ret = do_tls_poll(h, ret)) < 0)
  186. return ret;
  187. }
  188. return 0;
  189. }
  190. static int tls_write(URLContext *h, const uint8_t *buf, int size)
  191. {
  192. TLSContext *c = h->priv_data;
  193. while (1) {
  194. int ret = TLS_write(c, buf, size);
  195. if (ret > 0)
  196. return ret;
  197. if (ret == 0)
  198. return AVERROR(EIO);
  199. if ((ret = do_tls_poll(h, ret)) < 0)
  200. return ret;
  201. }
  202. return 0;
  203. }
  204. static int tls_close(URLContext *h)
  205. {
  206. TLSContext *c = h->priv_data;
  207. TLS_shutdown(c);
  208. TLS_free(c);
  209. ffurl_close(c->tcp);
  210. ff_tls_deinit();
  211. return 0;
  212. }
  213. URLProtocol ff_tls_protocol = {
  214. .name = "tls",
  215. .url_open = tls_open,
  216. .url_read = tls_read,
  217. .url_write = tls_write,
  218. .url_seek = NULL,
  219. .url_close = tls_close,
  220. .priv_data_size = sizeof(TLSContext),
  221. };