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.

354 lines
9.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 "internal.h"
  23. #include "network.h"
  24. #include "os_support.h"
  25. #include "url.h"
  26. #include "tls.h"
  27. #include "libavcodec/internal.h"
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/avutil.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/parseutils.h"
  32. #include "libavutil/thread.h"
  33. #include <openssl/bio.h>
  34. #include <openssl/ssl.h>
  35. #include <openssl/err.h>
  36. static int openssl_init;
  37. typedef struct TLSContext {
  38. const AVClass *class;
  39. TLSShared tls_shared;
  40. SSL_CTX *ctx;
  41. SSL *ssl;
  42. #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
  43. BIO_METHOD* url_bio_method;
  44. #endif
  45. } TLSContext;
  46. #if HAVE_THREADS
  47. #include <openssl/crypto.h>
  48. pthread_mutex_t *openssl_mutexes;
  49. static void openssl_lock(int mode, int type, const char *file, int line)
  50. {
  51. if (mode & CRYPTO_LOCK)
  52. pthread_mutex_lock(&openssl_mutexes[type]);
  53. else
  54. pthread_mutex_unlock(&openssl_mutexes[type]);
  55. }
  56. #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
  57. static unsigned long openssl_thread_id(void)
  58. {
  59. return (intptr_t) pthread_self();
  60. }
  61. #endif
  62. #endif
  63. void ff_openssl_init(void)
  64. {
  65. avpriv_lock_avformat();
  66. if (!openssl_init) {
  67. SSL_library_init();
  68. SSL_load_error_strings();
  69. #if HAVE_THREADS
  70. if (!CRYPTO_get_locking_callback()) {
  71. int i;
  72. openssl_mutexes = av_malloc(sizeof(pthread_mutex_t) * CRYPTO_num_locks());
  73. for (i = 0; i < CRYPTO_num_locks(); i++)
  74. pthread_mutex_init(&openssl_mutexes[i], NULL);
  75. CRYPTO_set_locking_callback(openssl_lock);
  76. #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
  77. CRYPTO_set_id_callback(openssl_thread_id);
  78. #endif
  79. }
  80. #endif
  81. }
  82. openssl_init++;
  83. avpriv_unlock_avformat();
  84. }
  85. void ff_openssl_deinit(void)
  86. {
  87. avpriv_lock_avformat();
  88. openssl_init--;
  89. if (!openssl_init) {
  90. #if HAVE_THREADS
  91. if (CRYPTO_get_locking_callback() == openssl_lock) {
  92. int i;
  93. CRYPTO_set_locking_callback(NULL);
  94. for (i = 0; i < CRYPTO_num_locks(); i++)
  95. pthread_mutex_destroy(&openssl_mutexes[i]);
  96. av_free(openssl_mutexes);
  97. }
  98. #endif
  99. }
  100. avpriv_unlock_avformat();
  101. }
  102. static int print_tls_error(URLContext *h, int ret)
  103. {
  104. TLSContext *c = h->priv_data;
  105. if (h->flags & AVIO_FLAG_NONBLOCK) {
  106. int err = SSL_get_error(c->ssl, ret);
  107. if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_READ)
  108. return AVERROR(EAGAIN);
  109. }
  110. av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  111. return AVERROR(EIO);
  112. }
  113. static int tls_close(URLContext *h)
  114. {
  115. TLSContext *c = h->priv_data;
  116. if (c->ssl) {
  117. SSL_shutdown(c->ssl);
  118. SSL_free(c->ssl);
  119. }
  120. if (c->ctx)
  121. SSL_CTX_free(c->ctx);
  122. if (c->tls_shared.tcp)
  123. ffurl_close(c->tls_shared.tcp);
  124. #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
  125. if (c->url_bio_method)
  126. BIO_meth_free(c->url_bio_method);
  127. #endif
  128. ff_openssl_deinit();
  129. return 0;
  130. }
  131. static int url_bio_create(BIO *b)
  132. {
  133. #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
  134. BIO_set_init(b, 1);
  135. BIO_set_data(b, NULL);
  136. BIO_set_flags(b, 0);
  137. #else
  138. b->init = 1;
  139. b->ptr = NULL;
  140. b->flags = 0;
  141. #endif
  142. return 1;
  143. }
  144. static int url_bio_destroy(BIO *b)
  145. {
  146. return 1;
  147. }
  148. #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
  149. #define GET_BIO_DATA(x) BIO_get_data(x)
  150. #else
  151. #define GET_BIO_DATA(x) (x)->ptr
  152. #endif
  153. static int url_bio_bread(BIO *b, char *buf, int len)
  154. {
  155. URLContext *h = GET_BIO_DATA(b);
  156. int ret = ffurl_read(h, buf, len);
  157. if (ret >= 0)
  158. return ret;
  159. BIO_clear_retry_flags(b);
  160. if (ret == AVERROR(EAGAIN))
  161. BIO_set_retry_read(b);
  162. if (ret == AVERROR_EXIT)
  163. return 0;
  164. return -1;
  165. }
  166. static int url_bio_bwrite(BIO *b, const char *buf, int len)
  167. {
  168. URLContext *h = GET_BIO_DATA(b);
  169. int ret = ffurl_write(h, buf, len);
  170. if (ret >= 0)
  171. return ret;
  172. BIO_clear_retry_flags(b);
  173. if (ret == AVERROR(EAGAIN))
  174. BIO_set_retry_write(b);
  175. if (ret == AVERROR_EXIT)
  176. return 0;
  177. return -1;
  178. }
  179. static long url_bio_ctrl(BIO *b, int cmd, long num, void *ptr)
  180. {
  181. if (cmd == BIO_CTRL_FLUSH) {
  182. BIO_clear_retry_flags(b);
  183. return 1;
  184. }
  185. return 0;
  186. }
  187. static int url_bio_bputs(BIO *b, const char *str)
  188. {
  189. return url_bio_bwrite(b, str, strlen(str));
  190. }
  191. #if OPENSSL_VERSION_NUMBER < 0x1010000fL
  192. static BIO_METHOD url_bio_method = {
  193. .type = BIO_TYPE_SOURCE_SINK,
  194. .name = "urlprotocol bio",
  195. .bwrite = url_bio_bwrite,
  196. .bread = url_bio_bread,
  197. .bputs = url_bio_bputs,
  198. .bgets = NULL,
  199. .ctrl = url_bio_ctrl,
  200. .create = url_bio_create,
  201. .destroy = url_bio_destroy,
  202. };
  203. #endif
  204. static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
  205. {
  206. TLSContext *p = h->priv_data;
  207. TLSShared *c = &p->tls_shared;
  208. BIO *bio;
  209. int ret;
  210. ff_openssl_init();
  211. if ((ret = ff_tls_open_underlying(c, h, uri, options)) < 0)
  212. goto fail;
  213. // We want to support all versions of TLS >= 1.0, but not the deprecated
  214. // and insecure SSLv2 and SSLv3. Despite the name, SSLv23_*_method()
  215. // enables support for all versions of SSL and TLS, and we then disable
  216. // support for the old protocols immediately after creating the context.
  217. p->ctx = SSL_CTX_new(c->listen ? SSLv23_server_method() : SSLv23_client_method());
  218. if (!p->ctx) {
  219. av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  220. ret = AVERROR(EIO);
  221. goto fail;
  222. }
  223. SSL_CTX_set_options(p->ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
  224. if (c->ca_file)
  225. SSL_CTX_load_verify_locations(p->ctx, c->ca_file, NULL);
  226. if (c->cert_file && !SSL_CTX_use_certificate_chain_file(p->ctx, c->cert_file)) {
  227. av_log(h, AV_LOG_ERROR, "Unable to load cert file %s: %s\n",
  228. c->cert_file, ERR_error_string(ERR_get_error(), NULL));
  229. ret = AVERROR(EIO);
  230. goto fail;
  231. }
  232. if (c->key_file && !SSL_CTX_use_PrivateKey_file(p->ctx, c->key_file, SSL_FILETYPE_PEM)) {
  233. av_log(h, AV_LOG_ERROR, "Unable to load key file %s: %s\n",
  234. c->key_file, ERR_error_string(ERR_get_error(), NULL));
  235. ret = AVERROR(EIO);
  236. goto fail;
  237. }
  238. // Note, this doesn't check that the peer certificate actually matches
  239. // the requested hostname.
  240. if (c->verify)
  241. SSL_CTX_set_verify(p->ctx, SSL_VERIFY_PEER, NULL);
  242. p->ssl = SSL_new(p->ctx);
  243. if (!p->ssl) {
  244. av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  245. ret = AVERROR(EIO);
  246. goto fail;
  247. }
  248. #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
  249. p->url_bio_method = BIO_meth_new(BIO_TYPE_SOURCE_SINK, "urlprotocol bio");
  250. BIO_meth_set_write(p->url_bio_method, url_bio_bwrite);
  251. BIO_meth_set_read(p->url_bio_method, url_bio_bread);
  252. BIO_meth_set_puts(p->url_bio_method, url_bio_bputs);
  253. BIO_meth_set_ctrl(p->url_bio_method, url_bio_ctrl);
  254. BIO_meth_set_create(p->url_bio_method, url_bio_create);
  255. BIO_meth_set_destroy(p->url_bio_method, url_bio_destroy);
  256. bio = BIO_new(p->url_bio_method);
  257. BIO_set_data(bio, c->tcp);
  258. #else
  259. bio = BIO_new(&url_bio_method);
  260. bio->ptr = c->tcp;
  261. #endif
  262. SSL_set_bio(p->ssl, bio, bio);
  263. if (!c->listen && !c->numerichost)
  264. SSL_set_tlsext_host_name(p->ssl, c->host);
  265. ret = c->listen ? SSL_accept(p->ssl) : SSL_connect(p->ssl);
  266. if (ret == 0) {
  267. av_log(h, AV_LOG_ERROR, "Unable to negotiate TLS/SSL session\n");
  268. ret = AVERROR(EIO);
  269. goto fail;
  270. } else if (ret < 0) {
  271. ret = print_tls_error(h, ret);
  272. goto fail;
  273. }
  274. return 0;
  275. fail:
  276. tls_close(h);
  277. return ret;
  278. }
  279. static int tls_read(URLContext *h, uint8_t *buf, int size)
  280. {
  281. TLSContext *c = h->priv_data;
  282. int ret;
  283. // Set or clear the AVIO_FLAG_NONBLOCK on c->tls_shared.tcp
  284. c->tls_shared.tcp->flags &= ~AVIO_FLAG_NONBLOCK;
  285. c->tls_shared.tcp->flags |= h->flags & AVIO_FLAG_NONBLOCK;
  286. ret = SSL_read(c->ssl, buf, size);
  287. if (ret > 0)
  288. return ret;
  289. if (ret == 0)
  290. return AVERROR_EOF;
  291. return print_tls_error(h, ret);
  292. }
  293. static int tls_write(URLContext *h, const uint8_t *buf, int size)
  294. {
  295. TLSContext *c = h->priv_data;
  296. int ret;
  297. // Set or clear the AVIO_FLAG_NONBLOCK on c->tls_shared.tcp
  298. c->tls_shared.tcp->flags &= ~AVIO_FLAG_NONBLOCK;
  299. c->tls_shared.tcp->flags |= h->flags & AVIO_FLAG_NONBLOCK;
  300. ret = SSL_write(c->ssl, buf, size);
  301. if (ret > 0)
  302. return ret;
  303. if (ret == 0)
  304. return AVERROR_EOF;
  305. return print_tls_error(h, ret);
  306. }
  307. static const AVOption options[] = {
  308. TLS_COMMON_OPTIONS(TLSContext, tls_shared),
  309. { NULL }
  310. };
  311. static const AVClass tls_class = {
  312. .class_name = "tls",
  313. .item_name = av_default_item_name,
  314. .option = options,
  315. .version = LIBAVUTIL_VERSION_INT,
  316. };
  317. const URLProtocol ff_tls_protocol = {
  318. .name = "tls",
  319. .url_open2 = tls_open,
  320. .url_read = tls_read,
  321. .url_write = tls_write,
  322. .url_close = tls_close,
  323. .priv_data_size = sizeof(TLSContext),
  324. .flags = URL_PROTOCOL_FLAG_NETWORK,
  325. .priv_data_class = &tls_class,
  326. };