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.

297 lines
8.8KB

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