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.

371 lines
12KB

  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/opt.h"
  25. #include "libavutil/parseutils.h"
  26. #if CONFIG_GNUTLS
  27. #include <gnutls/gnutls.h>
  28. #include <gnutls/x509.h>
  29. #define TLS_read(c, buf, size) gnutls_record_recv(c->session, buf, size)
  30. #define TLS_write(c, buf, size) gnutls_record_send(c->session, buf, size)
  31. #define TLS_shutdown(c) gnutls_bye(c->session, GNUTLS_SHUT_RDWR)
  32. #define TLS_free(c) do { \
  33. if (c->session) \
  34. gnutls_deinit(c->session); \
  35. if (c->cred) \
  36. gnutls_certificate_free_credentials(c->cred); \
  37. } while (0)
  38. #elif CONFIG_OPENSSL
  39. #include <openssl/bio.h>
  40. #include <openssl/ssl.h>
  41. #include <openssl/err.h>
  42. #define TLS_read(c, buf, size) SSL_read(c->ssl, buf, size)
  43. #define TLS_write(c, buf, size) SSL_write(c->ssl, buf, size)
  44. #define TLS_shutdown(c) SSL_shutdown(c->ssl)
  45. #define TLS_free(c) do { \
  46. if (c->ssl) \
  47. SSL_free(c->ssl); \
  48. if (c->ctx) \
  49. SSL_CTX_free(c->ctx); \
  50. } while (0)
  51. #endif
  52. #include "network.h"
  53. #include "os_support.h"
  54. #include "internal.h"
  55. #if HAVE_POLL_H
  56. #include <poll.h>
  57. #endif
  58. typedef struct TLSContext {
  59. const AVClass *class;
  60. URLContext *tcp;
  61. #if CONFIG_GNUTLS
  62. gnutls_session_t session;
  63. gnutls_certificate_credentials_t cred;
  64. #elif CONFIG_OPENSSL
  65. SSL_CTX *ctx;
  66. SSL *ssl;
  67. #endif
  68. int fd;
  69. char *ca_file;
  70. int verify;
  71. char *cert_file;
  72. char *key_file;
  73. int listen;
  74. } TLSContext;
  75. #define OFFSET(x) offsetof(TLSContext, x)
  76. #define D AV_OPT_FLAG_DECODING_PARAM
  77. #define E AV_OPT_FLAG_ENCODING_PARAM
  78. static const AVOption options[] = {
  79. {"ca_file", "Certificate Authority database file", OFFSET(ca_file), AV_OPT_TYPE_STRING, .flags = D|E },
  80. {"tls_verify", "Verify the peer certificate", OFFSET(verify), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = D|E },
  81. {"cert_file", "Certificate file", OFFSET(cert_file), AV_OPT_TYPE_STRING, .flags = D|E },
  82. {"key_file", "Private key file", OFFSET(key_file), AV_OPT_TYPE_STRING, .flags = D|E },
  83. {"listen", "Listen for incoming connections", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = D|E },
  84. { NULL }
  85. };
  86. static const AVClass tls_class = {
  87. .class_name = "tls",
  88. .item_name = av_default_item_name,
  89. .option = options,
  90. .version = LIBAVUTIL_VERSION_INT,
  91. };
  92. static int do_tls_poll(URLContext *h, int ret)
  93. {
  94. TLSContext *c = h->priv_data;
  95. struct pollfd p = { c->fd, 0, 0 };
  96. #if CONFIG_GNUTLS
  97. switch (ret) {
  98. case GNUTLS_E_AGAIN:
  99. case GNUTLS_E_INTERRUPTED:
  100. break;
  101. case GNUTLS_E_WARNING_ALERT_RECEIVED:
  102. av_log(h, AV_LOG_WARNING, "%s\n", gnutls_strerror(ret));
  103. break;
  104. default:
  105. av_log(h, AV_LOG_ERROR, "%s\n", gnutls_strerror(ret));
  106. return AVERROR(EIO);
  107. }
  108. if (gnutls_record_get_direction(c->session))
  109. p.events = POLLOUT;
  110. else
  111. p.events = POLLIN;
  112. #elif CONFIG_OPENSSL
  113. ret = SSL_get_error(c->ssl, ret);
  114. if (ret == SSL_ERROR_WANT_READ) {
  115. p.events = POLLIN;
  116. } else if (ret == SSL_ERROR_WANT_WRITE) {
  117. p.events = POLLOUT;
  118. } else {
  119. av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  120. return AVERROR(EIO);
  121. }
  122. #endif
  123. if (h->flags & AVIO_FLAG_NONBLOCK)
  124. return AVERROR(EAGAIN);
  125. while (1) {
  126. int n = poll(&p, 1, 100);
  127. if (n > 0)
  128. break;
  129. if (ff_check_interrupt(&h->interrupt_callback))
  130. return AVERROR(EINTR);
  131. }
  132. return 0;
  133. }
  134. static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
  135. {
  136. TLSContext *c = h->priv_data;
  137. int ret;
  138. int port;
  139. const char *p;
  140. char buf[200], host[200], opts[50] = "";
  141. int numerichost = 0;
  142. struct addrinfo hints = { 0 }, *ai = NULL;
  143. const char *proxy_path;
  144. int use_proxy;
  145. ff_tls_init();
  146. if (c->listen)
  147. snprintf(opts, sizeof(opts), "?listen=1");
  148. av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0, uri);
  149. p = strchr(uri, '?');
  150. if (!p) {
  151. p = opts;
  152. } else {
  153. if (av_find_info_tag(opts, sizeof(opts), "listen", p))
  154. c->listen = 1;
  155. }
  156. ff_url_join(buf, sizeof(buf), "tcp", NULL, host, port, "%s", p);
  157. hints.ai_flags = AI_NUMERICHOST;
  158. if (!getaddrinfo(host, NULL, &hints, &ai)) {
  159. numerichost = 1;
  160. freeaddrinfo(ai);
  161. }
  162. proxy_path = getenv("http_proxy");
  163. use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), host) &&
  164. proxy_path && av_strstart(proxy_path, "http://", NULL);
  165. if (use_proxy) {
  166. char proxy_host[200], proxy_auth[200], dest[200];
  167. int proxy_port;
  168. av_url_split(NULL, 0, proxy_auth, sizeof(proxy_auth),
  169. proxy_host, sizeof(proxy_host), &proxy_port, NULL, 0,
  170. proxy_path);
  171. ff_url_join(dest, sizeof(dest), NULL, NULL, host, port, NULL);
  172. ff_url_join(buf, sizeof(buf), "httpproxy", proxy_auth, proxy_host,
  173. proxy_port, "/%s", dest);
  174. }
  175. ret = ffurl_open(&c->tcp, buf, AVIO_FLAG_READ_WRITE,
  176. &h->interrupt_callback, options);
  177. if (ret)
  178. goto fail;
  179. c->fd = ffurl_get_file_handle(c->tcp);
  180. #if CONFIG_GNUTLS
  181. gnutls_init(&c->session, c->listen ? GNUTLS_SERVER : GNUTLS_CLIENT);
  182. if (!c->listen && !numerichost)
  183. gnutls_server_name_set(c->session, GNUTLS_NAME_DNS, host, strlen(host));
  184. gnutls_certificate_allocate_credentials(&c->cred);
  185. if (c->ca_file)
  186. gnutls_certificate_set_x509_trust_file(c->cred, c->ca_file, GNUTLS_X509_FMT_PEM);
  187. #if GNUTLS_VERSION_MAJOR >= 3
  188. else
  189. gnutls_certificate_set_x509_system_trust(c->cred);
  190. #endif
  191. gnutls_certificate_set_verify_flags(c->cred, c->verify ?
  192. GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT : 0);
  193. if (c->cert_file && c->key_file) {
  194. ret = gnutls_certificate_set_x509_key_file(c->cred,
  195. c->cert_file, c->key_file,
  196. GNUTLS_X509_FMT_PEM);
  197. if (ret < 0) {
  198. av_log(h, AV_LOG_ERROR,
  199. "Unable to set cert/key files %s and %s: %s\n",
  200. c->cert_file, c->key_file, gnutls_strerror(ret));
  201. ret = AVERROR(EIO);
  202. goto fail;
  203. }
  204. }
  205. gnutls_credentials_set(c->session, GNUTLS_CRD_CERTIFICATE, c->cred);
  206. gnutls_transport_set_ptr(c->session, (gnutls_transport_ptr_t)
  207. (intptr_t) c->fd);
  208. gnutls_priority_set_direct(c->session, "NORMAL", NULL);
  209. while (1) {
  210. ret = gnutls_handshake(c->session);
  211. if (ret == 0)
  212. break;
  213. if ((ret = do_tls_poll(h, ret)) < 0)
  214. goto fail;
  215. }
  216. if (c->verify) {
  217. unsigned int status, cert_list_size;
  218. gnutls_x509_crt_t cert;
  219. const gnutls_datum_t *cert_list;
  220. if ((ret = gnutls_certificate_verify_peers2(c->session, &status)) < 0) {
  221. av_log(h, AV_LOG_ERROR, "Unable to verify peer certificate: %s\n",
  222. gnutls_strerror(ret));
  223. ret = AVERROR(EIO);
  224. goto fail;
  225. }
  226. if (status & GNUTLS_CERT_INVALID) {
  227. av_log(h, AV_LOG_ERROR, "Peer certificate failed verification\n");
  228. ret = AVERROR(EIO);
  229. goto fail;
  230. }
  231. if (gnutls_certificate_type_get(c->session) != GNUTLS_CRT_X509) {
  232. av_log(h, AV_LOG_ERROR, "Unsupported certificate type\n");
  233. ret = AVERROR(EIO);
  234. goto fail;
  235. }
  236. gnutls_x509_crt_init(&cert);
  237. cert_list = gnutls_certificate_get_peers(c->session, &cert_list_size);
  238. gnutls_x509_crt_import(cert, cert_list, GNUTLS_X509_FMT_DER);
  239. ret = gnutls_x509_crt_check_hostname(cert, host);
  240. gnutls_x509_crt_deinit(cert);
  241. if (!ret) {
  242. av_log(h, AV_LOG_ERROR,
  243. "The certificate's owner does not match hostname %s\n", host);
  244. ret = AVERROR(EIO);
  245. goto fail;
  246. }
  247. }
  248. #elif CONFIG_OPENSSL
  249. c->ctx = SSL_CTX_new(c->listen ? TLSv1_server_method() : TLSv1_client_method());
  250. if (!c->ctx) {
  251. av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  252. ret = AVERROR(EIO);
  253. goto fail;
  254. }
  255. if (c->ca_file)
  256. SSL_CTX_load_verify_locations(c->ctx, c->ca_file, NULL);
  257. if (c->cert_file && !SSL_CTX_use_certificate_chain_file(c->ctx, c->cert_file)) {
  258. av_log(h, AV_LOG_ERROR, "Unable to load cert file %s: %s\n",
  259. c->cert_file, ERR_error_string(ERR_get_error(), NULL));
  260. ret = AVERROR(EIO);
  261. goto fail;
  262. }
  263. if (c->key_file && !SSL_CTX_use_PrivateKey_file(c->ctx, c->key_file, SSL_FILETYPE_PEM)) {
  264. av_log(h, AV_LOG_ERROR, "Unable to load key file %s: %s\n",
  265. c->key_file, ERR_error_string(ERR_get_error(), NULL));
  266. ret = AVERROR(EIO);
  267. goto fail;
  268. }
  269. // Note, this doesn't check that the peer certificate actually matches
  270. // the requested hostname.
  271. if (c->verify)
  272. SSL_CTX_set_verify(c->ctx, SSL_VERIFY_PEER, NULL);
  273. c->ssl = SSL_new(c->ctx);
  274. if (!c->ssl) {
  275. av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  276. ret = AVERROR(EIO);
  277. goto fail;
  278. }
  279. SSL_set_fd(c->ssl, c->fd);
  280. if (!c->listen && !numerichost)
  281. SSL_set_tlsext_host_name(c->ssl, host);
  282. while (1) {
  283. ret = c->listen ? SSL_accept(c->ssl) : SSL_connect(c->ssl);
  284. if (ret > 0)
  285. break;
  286. if (ret == 0) {
  287. av_log(h, AV_LOG_ERROR, "Unable to negotiate TLS/SSL session\n");
  288. ret = AVERROR(EIO);
  289. goto fail;
  290. }
  291. if ((ret = do_tls_poll(h, ret)) < 0)
  292. goto fail;
  293. }
  294. #endif
  295. return 0;
  296. fail:
  297. TLS_free(c);
  298. if (c->tcp)
  299. ffurl_close(c->tcp);
  300. ff_tls_deinit();
  301. return ret;
  302. }
  303. static int tls_read(URLContext *h, uint8_t *buf, int size)
  304. {
  305. TLSContext *c = h->priv_data;
  306. while (1) {
  307. int ret = TLS_read(c, buf, size);
  308. if (ret > 0)
  309. return ret;
  310. if (ret == 0)
  311. return AVERROR_EOF;
  312. if ((ret = do_tls_poll(h, ret)) < 0)
  313. return ret;
  314. }
  315. return 0;
  316. }
  317. static int tls_write(URLContext *h, const uint8_t *buf, int size)
  318. {
  319. TLSContext *c = h->priv_data;
  320. while (1) {
  321. int ret = TLS_write(c, buf, size);
  322. if (ret > 0)
  323. return ret;
  324. if (ret == 0)
  325. return AVERROR_EOF;
  326. if ((ret = do_tls_poll(h, ret)) < 0)
  327. return ret;
  328. }
  329. return 0;
  330. }
  331. static int tls_close(URLContext *h)
  332. {
  333. TLSContext *c = h->priv_data;
  334. TLS_shutdown(c);
  335. TLS_free(c);
  336. ffurl_close(c->tcp);
  337. ff_tls_deinit();
  338. return 0;
  339. }
  340. URLProtocol ff_tls_protocol = {
  341. .name = "tls",
  342. .url_open2 = tls_open,
  343. .url_read = tls_read,
  344. .url_write = tls_write,
  345. .url_close = tls_close,
  346. .priv_data_size = sizeof(TLSContext),
  347. .flags = URL_PROTOCOL_FLAG_NETWORK,
  348. .priv_data_class = &tls_class,
  349. };