tls.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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, verify = 0;
  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. if (av_find_info_tag(buf, sizeof(buf), "verify", p)) {
  125. char *endptr = NULL;
  126. verify = strtol(buf, &endptr, 10);
  127. if (buf == endptr)
  128. verify = 1;
  129. }
  130. has_cert = av_find_info_tag(buf, sizeof(buf), "cert", p);
  131. has_key = av_find_info_tag(key, sizeof(key), "key", p);
  132. #if CONFIG_GNUTLS
  133. if (has_cert && has_key) {
  134. ret = gnutls_certificate_set_x509_key_file(c->cred, buf, key, GNUTLS_X509_FMT_PEM);
  135. if (ret < 0)
  136. av_log(h, AV_LOG_ERROR, "%s\n", gnutls_strerror(ret));
  137. } else if (has_cert ^ has_key) {
  138. av_log(h, AV_LOG_ERROR, "cert and key required\n");
  139. }
  140. gnutls_certificate_set_verify_flags(c->cred, verify);
  141. #elif CONFIG_OPENSSL
  142. if (has_cert && !SSL_CTX_use_certificate_chain_file(c->ctx, buf))
  143. av_log(h, AV_LOG_ERROR, "SSL_CTX_use_certificate_chain_file %s\n", ERR_error_string(ERR_get_error(), NULL));
  144. if (has_key && !SSL_CTX_use_PrivateKey_file(c->ctx, key, SSL_FILETYPE_PEM))
  145. av_log(h, AV_LOG_ERROR, "SSL_CTX_use_PrivateKey_file %s\n", ERR_error_string(ERR_get_error(), NULL));
  146. if (verify)
  147. SSL_CTX_set_verify(c->ctx, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  148. #endif
  149. }
  150. static int tls_open(URLContext *h, const char *uri, int flags)
  151. {
  152. TLSContext *c = h->priv_data;
  153. int ret;
  154. int port;
  155. char buf[200], host[200], path[1024];
  156. int numerichost = 0;
  157. struct addrinfo hints = { 0 }, *ai = NULL;
  158. const char *proxy_path;
  159. int use_proxy;
  160. int server = 0;
  161. const char *p = strchr(uri, '?');
  162. if (p && av_find_info_tag(buf, sizeof(buf), "listen", p))
  163. server = 1;
  164. ff_tls_init();
  165. proxy_path = getenv("http_proxy");
  166. use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
  167. av_strstart(proxy_path, "http://", NULL);
  168. av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, path, sizeof(path), uri);
  169. ff_url_join(buf, sizeof(buf), "tcp", NULL, host, port, "%s", path);
  170. hints.ai_flags = AI_NUMERICHOST;
  171. if (!getaddrinfo(host, NULL, &hints, &ai)) {
  172. numerichost = 1;
  173. freeaddrinfo(ai);
  174. }
  175. if (use_proxy) {
  176. char proxy_host[200], proxy_auth[200], dest[200];
  177. int proxy_port;
  178. av_url_split(NULL, 0, proxy_auth, sizeof(proxy_auth),
  179. proxy_host, sizeof(proxy_host), &proxy_port, NULL, 0,
  180. proxy_path);
  181. ff_url_join(dest, sizeof(dest), NULL, NULL, host, port, NULL);
  182. ff_url_join(buf, sizeof(buf), "httpproxy", proxy_auth, proxy_host,
  183. proxy_port, "/%s", dest);
  184. }
  185. ret = ffurl_open(&c->tcp, buf, AVIO_FLAG_READ_WRITE,
  186. &h->interrupt_callback, NULL);
  187. if (ret)
  188. goto fail;
  189. c->fd = ffurl_get_file_handle(c->tcp);
  190. #if CONFIG_GNUTLS
  191. gnutls_init(&c->session, server ? GNUTLS_SERVER : GNUTLS_CLIENT);
  192. if (!numerichost)
  193. gnutls_server_name_set(c->session, GNUTLS_NAME_DNS, host, strlen(host));
  194. gnutls_certificate_allocate_credentials(&c->cred);
  195. set_options(h, uri);
  196. gnutls_credentials_set(c->session, GNUTLS_CRD_CERTIFICATE, c->cred);
  197. gnutls_transport_set_ptr(c->session, (gnutls_transport_ptr_t)
  198. (intptr_t) c->fd);
  199. gnutls_priority_set_direct(c->session, "NORMAL", NULL);
  200. while (1) {
  201. ret = gnutls_handshake(c->session);
  202. if (ret == 0)
  203. break;
  204. if ((ret = do_tls_poll(h, ret)) < 0)
  205. goto fail;
  206. }
  207. #elif CONFIG_OPENSSL
  208. c->ctx = SSL_CTX_new(server ? TLSv1_server_method() : TLSv1_client_method());
  209. if (!c->ctx) {
  210. av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  211. ret = AVERROR(EIO);
  212. goto fail;
  213. }
  214. set_options(h, uri);
  215. c->ssl = SSL_new(c->ctx);
  216. if (!c->ssl) {
  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. SSL_set_fd(c->ssl, c->fd);
  222. if (!server && !numerichost)
  223. SSL_set_tlsext_host_name(c->ssl, host);
  224. while (1) {
  225. ret = server ? SSL_accept(c->ssl) : SSL_connect(c->ssl);
  226. if (ret > 0)
  227. break;
  228. if (ret == 0) {
  229. av_log(h, AV_LOG_ERROR, "Unable to negotiate TLS/SSL session\n");
  230. ret = AVERROR(EIO);
  231. goto fail;
  232. }
  233. if ((ret = do_tls_poll(h, ret)) < 0)
  234. goto fail;
  235. }
  236. #endif
  237. return 0;
  238. fail:
  239. TLS_free(c);
  240. if (c->tcp)
  241. ffurl_close(c->tcp);
  242. ff_tls_deinit();
  243. return ret;
  244. }
  245. static int tls_read(URLContext *h, uint8_t *buf, int size)
  246. {
  247. TLSContext *c = h->priv_data;
  248. while (1) {
  249. int ret = TLS_read(c, buf, size);
  250. if (ret > 0)
  251. return ret;
  252. if (ret == 0)
  253. return AVERROR_EOF;
  254. if ((ret = do_tls_poll(h, ret)) < 0)
  255. return ret;
  256. }
  257. return 0;
  258. }
  259. static int tls_write(URLContext *h, const uint8_t *buf, int size)
  260. {
  261. TLSContext *c = h->priv_data;
  262. while (1) {
  263. int ret = TLS_write(c, buf, size);
  264. if (ret > 0)
  265. return ret;
  266. if (ret == 0)
  267. return AVERROR_EOF;
  268. if ((ret = do_tls_poll(h, ret)) < 0)
  269. return ret;
  270. }
  271. return 0;
  272. }
  273. static int tls_close(URLContext *h)
  274. {
  275. TLSContext *c = h->priv_data;
  276. TLS_shutdown(c);
  277. TLS_free(c);
  278. ffurl_close(c->tcp);
  279. ff_tls_deinit();
  280. return 0;
  281. }
  282. URLProtocol ff_tls_protocol = {
  283. .name = "tls",
  284. .url_open = tls_open,
  285. .url_read = tls_read,
  286. .url_write = tls_write,
  287. .url_close = tls_close,
  288. .priv_data_size = sizeof(TLSContext),
  289. .flags = URL_PROTOCOL_FLAG_NETWORK,
  290. };