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.

317 lines
9.6KB

  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 "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. switch (ret) {
  74. case GNUTLS_E_AGAIN:
  75. case GNUTLS_E_INTERRUPTED:
  76. break;
  77. case GNUTLS_E_WARNING_ALERT_RECEIVED:
  78. av_log(h, AV_LOG_WARNING, "%s\n", gnutls_strerror(ret));
  79. break;
  80. default:
  81. av_log(h, AV_LOG_ERROR, "%s\n", gnutls_strerror(ret));
  82. return AVERROR(EIO);
  83. }
  84. if (gnutls_record_get_direction(c->session))
  85. p.events = POLLOUT;
  86. else
  87. p.events = POLLIN;
  88. #elif CONFIG_OPENSSL
  89. ret = SSL_get_error(c->ssl, ret);
  90. if (ret == SSL_ERROR_WANT_READ) {
  91. p.events = POLLIN;
  92. } else if (ret == SSL_ERROR_WANT_WRITE) {
  93. p.events = POLLOUT;
  94. } else {
  95. av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  96. return AVERROR(EIO);
  97. }
  98. #endif
  99. if (h->flags & AVIO_FLAG_NONBLOCK)
  100. return AVERROR(EAGAIN);
  101. while (1) {
  102. int n = poll(&p, 1, 100);
  103. if (n > 0)
  104. break;
  105. if (ff_check_interrupt(&h->interrupt_callback))
  106. return AVERROR(EINTR);
  107. }
  108. return 0;
  109. }
  110. static void set_options(URLContext *h, const char *uri)
  111. {
  112. TLSContext *c = h->priv_data;
  113. char buf[1024], key[1024];
  114. int has_cert, has_key, verify = 0;
  115. #if CONFIG_GNUTLS
  116. int ret;
  117. #endif
  118. const char *p = strchr(uri, '?');
  119. if (!p)
  120. return;
  121. if (av_find_info_tag(buf, sizeof(buf), "cafile", p)) {
  122. #if CONFIG_GNUTLS
  123. ret = gnutls_certificate_set_x509_trust_file(c->cred, buf, GNUTLS_X509_FMT_PEM);
  124. if (ret < 0)
  125. av_log(h, AV_LOG_ERROR, "%s\n", gnutls_strerror(ret));
  126. #elif CONFIG_OPENSSL
  127. if (!SSL_CTX_load_verify_locations(c->ctx, buf, NULL))
  128. av_log(h, AV_LOG_ERROR, "SSL_CTX_load_verify_locations %s\n", ERR_error_string(ERR_get_error(), NULL));
  129. #endif
  130. }
  131. if (av_find_info_tag(buf, sizeof(buf), "verify", p)) {
  132. char *endptr = NULL;
  133. verify = strtol(buf, &endptr, 10);
  134. if (buf == endptr)
  135. verify = 1;
  136. }
  137. has_cert = av_find_info_tag(buf, sizeof(buf), "cert", p);
  138. has_key = av_find_info_tag(key, sizeof(key), "key", p);
  139. #if CONFIG_GNUTLS
  140. if (has_cert && has_key) {
  141. ret = gnutls_certificate_set_x509_key_file(c->cred, buf, key, GNUTLS_X509_FMT_PEM);
  142. if (ret < 0)
  143. av_log(h, AV_LOG_ERROR, "%s\n", gnutls_strerror(ret));
  144. } else if (has_cert ^ has_key) {
  145. av_log(h, AV_LOG_ERROR, "cert and key required\n");
  146. }
  147. gnutls_certificate_set_verify_flags(c->cred, verify);
  148. #elif CONFIG_OPENSSL
  149. if (has_cert && !SSL_CTX_use_certificate_chain_file(c->ctx, buf))
  150. av_log(h, AV_LOG_ERROR, "SSL_CTX_use_certificate_chain_file %s\n", ERR_error_string(ERR_get_error(), NULL));
  151. if (has_key && !SSL_CTX_use_PrivateKey_file(c->ctx, key, SSL_FILETYPE_PEM))
  152. av_log(h, AV_LOG_ERROR, "SSL_CTX_use_PrivateKey_file %s\n", ERR_error_string(ERR_get_error(), NULL));
  153. if (verify)
  154. SSL_CTX_set_verify(c->ctx, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  155. #endif
  156. }
  157. static int tls_open(URLContext *h, const char *uri, int flags)
  158. {
  159. TLSContext *c = h->priv_data;
  160. int ret;
  161. int port;
  162. char buf[200], host[200], path[1024];
  163. int numerichost = 0;
  164. struct addrinfo hints = { 0 }, *ai = NULL;
  165. const char *proxy_path;
  166. int use_proxy;
  167. int server = 0;
  168. const char *p = strchr(uri, '?');
  169. if (p && av_find_info_tag(buf, sizeof(buf), "listen", p))
  170. server = 1;
  171. ff_tls_init();
  172. av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, path, sizeof(path), uri);
  173. ff_url_join(buf, sizeof(buf), "tcp", NULL, host, port, "%s", path);
  174. hints.ai_flags = AI_NUMERICHOST;
  175. if (!getaddrinfo(host, NULL, &hints, &ai)) {
  176. numerichost = 1;
  177. freeaddrinfo(ai);
  178. }
  179. proxy_path = getenv("http_proxy");
  180. use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), host) &&
  181. proxy_path != NULL && av_strstart(proxy_path, "http://", NULL);
  182. if (use_proxy) {
  183. char proxy_host[200], proxy_auth[200], dest[200];
  184. int proxy_port;
  185. av_url_split(NULL, 0, proxy_auth, sizeof(proxy_auth),
  186. proxy_host, sizeof(proxy_host), &proxy_port, NULL, 0,
  187. proxy_path);
  188. ff_url_join(dest, sizeof(dest), NULL, NULL, host, port, NULL);
  189. ff_url_join(buf, sizeof(buf), "httpproxy", proxy_auth, proxy_host,
  190. proxy_port, "/%s", dest);
  191. }
  192. ret = ffurl_open(&c->tcp, buf, AVIO_FLAG_READ_WRITE,
  193. &h->interrupt_callback, NULL);
  194. if (ret)
  195. goto fail;
  196. c->fd = ffurl_get_file_handle(c->tcp);
  197. #if CONFIG_GNUTLS
  198. gnutls_init(&c->session, server ? GNUTLS_SERVER : GNUTLS_CLIENT);
  199. if (!numerichost)
  200. gnutls_server_name_set(c->session, GNUTLS_NAME_DNS, host, strlen(host));
  201. gnutls_certificate_allocate_credentials(&c->cred);
  202. set_options(h, uri);
  203. gnutls_credentials_set(c->session, GNUTLS_CRD_CERTIFICATE, c->cred);
  204. gnutls_transport_set_ptr(c->session, (gnutls_transport_ptr_t)
  205. (intptr_t) c->fd);
  206. gnutls_priority_set_direct(c->session, "NORMAL", NULL);
  207. while (1) {
  208. ret = gnutls_handshake(c->session);
  209. if (ret == 0)
  210. break;
  211. if ((ret = do_tls_poll(h, ret)) < 0)
  212. goto fail;
  213. }
  214. #elif CONFIG_OPENSSL
  215. c->ctx = SSL_CTX_new(server ? TLSv1_server_method() : TLSv1_client_method());
  216. if (!c->ctx) {
  217. av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  218. ret = AVERROR(EIO);
  219. goto fail;
  220. }
  221. set_options(h, uri);
  222. c->ssl = SSL_new(c->ctx);
  223. if (!c->ssl) {
  224. av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  225. ret = AVERROR(EIO);
  226. goto fail;
  227. }
  228. SSL_set_fd(c->ssl, c->fd);
  229. if (!server && !numerichost)
  230. SSL_set_tlsext_host_name(c->ssl, host);
  231. while (1) {
  232. ret = server ? SSL_accept(c->ssl) : SSL_connect(c->ssl);
  233. if (ret > 0)
  234. break;
  235. if (ret == 0) {
  236. av_log(h, AV_LOG_ERROR, "Unable to negotiate TLS/SSL session\n");
  237. ret = AVERROR(EIO);
  238. goto fail;
  239. }
  240. if ((ret = do_tls_poll(h, ret)) < 0)
  241. goto fail;
  242. }
  243. #endif
  244. return 0;
  245. fail:
  246. TLS_free(c);
  247. if (c->tcp)
  248. ffurl_close(c->tcp);
  249. ff_tls_deinit();
  250. return ret;
  251. }
  252. static int tls_read(URLContext *h, uint8_t *buf, int size)
  253. {
  254. TLSContext *c = h->priv_data;
  255. while (1) {
  256. int ret = TLS_read(c, buf, size);
  257. if (ret > 0)
  258. return ret;
  259. if (ret == 0)
  260. return AVERROR_EOF;
  261. if ((ret = do_tls_poll(h, ret)) < 0)
  262. return ret;
  263. }
  264. return 0;
  265. }
  266. static int tls_write(URLContext *h, const uint8_t *buf, int size)
  267. {
  268. TLSContext *c = h->priv_data;
  269. while (1) {
  270. int ret = TLS_write(c, buf, size);
  271. if (ret > 0)
  272. return ret;
  273. if (ret == 0)
  274. return AVERROR_EOF;
  275. if ((ret = do_tls_poll(h, ret)) < 0)
  276. return ret;
  277. }
  278. return 0;
  279. }
  280. static int tls_close(URLContext *h)
  281. {
  282. TLSContext *c = h->priv_data;
  283. TLS_shutdown(c);
  284. TLS_free(c);
  285. ffurl_close(c->tcp);
  286. ff_tls_deinit();
  287. return 0;
  288. }
  289. URLProtocol ff_tls_protocol = {
  290. .name = "tls",
  291. .url_open = tls_open,
  292. .url_read = tls_read,
  293. .url_write = tls_write,
  294. .url_close = tls_close,
  295. .priv_data_size = sizeof(TLSContext),
  296. .flags = URL_PROTOCOL_FLAG_NETWORK,
  297. };