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.

428 lines
13KB

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