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.

384 lines
11KB

  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 "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 && OPENSSL_VERSION_NUMBER < 0x10100000L
  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. int ff_openssl_init(void)
  64. {
  65. ff_lock_avformat();
  66. if (!openssl_init) {
  67. /* OpenSSL 1.0.2 or below, then you would use SSL_library_init. If you are
  68. * using OpenSSL 1.1.0 or above, then the library will initialize
  69. * itself automatically.
  70. * https://wiki.openssl.org/index.php/Library_Initialization
  71. */
  72. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  73. SSL_library_init();
  74. SSL_load_error_strings();
  75. #endif
  76. #if HAVE_THREADS && OPENSSL_VERSION_NUMBER < 0x10100000L
  77. if (!CRYPTO_get_locking_callback()) {
  78. int i;
  79. openssl_mutexes = av_malloc_array(sizeof(pthread_mutex_t), CRYPTO_num_locks());
  80. if (!openssl_mutexes) {
  81. ff_unlock_avformat();
  82. return AVERROR(ENOMEM);
  83. }
  84. for (i = 0; i < CRYPTO_num_locks(); i++)
  85. pthread_mutex_init(&openssl_mutexes[i], NULL);
  86. CRYPTO_set_locking_callback(openssl_lock);
  87. #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
  88. CRYPTO_set_id_callback(openssl_thread_id);
  89. #endif
  90. }
  91. #endif
  92. }
  93. openssl_init++;
  94. ff_unlock_avformat();
  95. return 0;
  96. }
  97. void ff_openssl_deinit(void)
  98. {
  99. ff_lock_avformat();
  100. openssl_init--;
  101. if (!openssl_init) {
  102. #if HAVE_THREADS && OPENSSL_VERSION_NUMBER < 0x10100000L
  103. if (CRYPTO_get_locking_callback() == openssl_lock) {
  104. int i;
  105. CRYPTO_set_locking_callback(NULL);
  106. for (i = 0; i < CRYPTO_num_locks(); i++)
  107. pthread_mutex_destroy(&openssl_mutexes[i]);
  108. av_free(openssl_mutexes);
  109. }
  110. #endif
  111. }
  112. ff_unlock_avformat();
  113. }
  114. static int print_tls_error(URLContext *h, int ret)
  115. {
  116. TLSContext *c = h->priv_data;
  117. if (h->flags & AVIO_FLAG_NONBLOCK) {
  118. int err = SSL_get_error(c->ssl, ret);
  119. if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE)
  120. return AVERROR(EAGAIN);
  121. }
  122. av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  123. return AVERROR(EIO);
  124. }
  125. static int tls_close(URLContext *h)
  126. {
  127. TLSContext *c = h->priv_data;
  128. if (c->ssl) {
  129. SSL_shutdown(c->ssl);
  130. SSL_free(c->ssl);
  131. }
  132. if (c->ctx)
  133. SSL_CTX_free(c->ctx);
  134. ffurl_closep(&c->tls_shared.tcp);
  135. #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
  136. if (c->url_bio_method)
  137. BIO_meth_free(c->url_bio_method);
  138. #endif
  139. ff_openssl_deinit();
  140. return 0;
  141. }
  142. static int url_bio_create(BIO *b)
  143. {
  144. #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
  145. BIO_set_init(b, 1);
  146. BIO_set_data(b, NULL);
  147. BIO_set_flags(b, 0);
  148. #else
  149. b->init = 1;
  150. b->ptr = NULL;
  151. b->flags = 0;
  152. #endif
  153. return 1;
  154. }
  155. static int url_bio_destroy(BIO *b)
  156. {
  157. return 1;
  158. }
  159. #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
  160. #define GET_BIO_DATA(x) BIO_get_data(x)
  161. #else
  162. #define GET_BIO_DATA(x) (x)->ptr
  163. #endif
  164. static int url_bio_bread(BIO *b, char *buf, int len)
  165. {
  166. URLContext *h = GET_BIO_DATA(b);
  167. int ret = ffurl_read(h, buf, len);
  168. if (ret >= 0)
  169. return ret;
  170. BIO_clear_retry_flags(b);
  171. if (ret == AVERROR(EAGAIN))
  172. BIO_set_retry_read(b);
  173. if (ret == AVERROR_EXIT)
  174. return 0;
  175. return -1;
  176. }
  177. static int url_bio_bwrite(BIO *b, const char *buf, int len)
  178. {
  179. URLContext *h = GET_BIO_DATA(b);
  180. int ret = ffurl_write(h, buf, len);
  181. if (ret >= 0)
  182. return ret;
  183. BIO_clear_retry_flags(b);
  184. if (ret == AVERROR(EAGAIN))
  185. BIO_set_retry_write(b);
  186. if (ret == AVERROR_EXIT)
  187. return 0;
  188. return -1;
  189. }
  190. static long url_bio_ctrl(BIO *b, int cmd, long num, void *ptr)
  191. {
  192. if (cmd == BIO_CTRL_FLUSH) {
  193. BIO_clear_retry_flags(b);
  194. return 1;
  195. }
  196. return 0;
  197. }
  198. static int url_bio_bputs(BIO *b, const char *str)
  199. {
  200. return url_bio_bwrite(b, str, strlen(str));
  201. }
  202. #if OPENSSL_VERSION_NUMBER < 0x1010000fL
  203. static BIO_METHOD url_bio_method = {
  204. .type = BIO_TYPE_SOURCE_SINK,
  205. .name = "urlprotocol bio",
  206. .bwrite = url_bio_bwrite,
  207. .bread = url_bio_bread,
  208. .bputs = url_bio_bputs,
  209. .bgets = NULL,
  210. .ctrl = url_bio_ctrl,
  211. .create = url_bio_create,
  212. .destroy = url_bio_destroy,
  213. };
  214. #endif
  215. static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
  216. {
  217. TLSContext *p = h->priv_data;
  218. TLSShared *c = &p->tls_shared;
  219. BIO *bio;
  220. int ret;
  221. if ((ret = ff_openssl_init()) < 0)
  222. return ret;
  223. if ((ret = ff_tls_open_underlying(c, h, uri, options)) < 0)
  224. goto fail;
  225. // We want to support all versions of TLS >= 1.0, but not the deprecated
  226. // and insecure SSLv2 and SSLv3. Despite the name, SSLv23_*_method()
  227. // enables support for all versions of SSL and TLS, and we then disable
  228. // support for the old protocols immediately after creating the context.
  229. p->ctx = SSL_CTX_new(c->listen ? SSLv23_server_method() : SSLv23_client_method());
  230. if (!p->ctx) {
  231. av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  232. ret = AVERROR(EIO);
  233. goto fail;
  234. }
  235. SSL_CTX_set_options(p->ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
  236. if (c->ca_file) {
  237. if (!SSL_CTX_load_verify_locations(p->ctx, c->ca_file, NULL))
  238. av_log(h, AV_LOG_ERROR, "SSL_CTX_load_verify_locations %s\n", ERR_error_string(ERR_get_error(), NULL));
  239. }
  240. if (c->cert_file && !SSL_CTX_use_certificate_chain_file(p->ctx, c->cert_file)) {
  241. av_log(h, AV_LOG_ERROR, "Unable to load cert file %s: %s\n",
  242. c->cert_file, ERR_error_string(ERR_get_error(), NULL));
  243. ret = AVERROR(EIO);
  244. goto fail;
  245. }
  246. if (c->key_file && !SSL_CTX_use_PrivateKey_file(p->ctx, c->key_file, SSL_FILETYPE_PEM)) {
  247. av_log(h, AV_LOG_ERROR, "Unable to load key file %s: %s\n",
  248. c->key_file, ERR_error_string(ERR_get_error(), NULL));
  249. ret = AVERROR(EIO);
  250. goto fail;
  251. }
  252. // Note, this doesn't check that the peer certificate actually matches
  253. // the requested hostname.
  254. if (c->verify)
  255. SSL_CTX_set_verify(p->ctx, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
  256. p->ssl = SSL_new(p->ctx);
  257. if (!p->ssl) {
  258. av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  259. ret = AVERROR(EIO);
  260. goto fail;
  261. }
  262. #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
  263. p->url_bio_method = BIO_meth_new(BIO_TYPE_SOURCE_SINK, "urlprotocol bio");
  264. BIO_meth_set_write(p->url_bio_method, url_bio_bwrite);
  265. BIO_meth_set_read(p->url_bio_method, url_bio_bread);
  266. BIO_meth_set_puts(p->url_bio_method, url_bio_bputs);
  267. BIO_meth_set_ctrl(p->url_bio_method, url_bio_ctrl);
  268. BIO_meth_set_create(p->url_bio_method, url_bio_create);
  269. BIO_meth_set_destroy(p->url_bio_method, url_bio_destroy);
  270. bio = BIO_new(p->url_bio_method);
  271. BIO_set_data(bio, c->tcp);
  272. #else
  273. bio = BIO_new(&url_bio_method);
  274. bio->ptr = c->tcp;
  275. #endif
  276. SSL_set_bio(p->ssl, bio, bio);
  277. if (!c->listen && !c->numerichost)
  278. SSL_set_tlsext_host_name(p->ssl, c->host);
  279. ret = c->listen ? SSL_accept(p->ssl) : SSL_connect(p->ssl);
  280. if (ret == 0) {
  281. av_log(h, AV_LOG_ERROR, "Unable to negotiate TLS/SSL session\n");
  282. ret = AVERROR(EIO);
  283. goto fail;
  284. } else if (ret < 0) {
  285. ret = print_tls_error(h, ret);
  286. goto fail;
  287. }
  288. return 0;
  289. fail:
  290. tls_close(h);
  291. return ret;
  292. }
  293. static int tls_read(URLContext *h, 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_read(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 int tls_write(URLContext *h, const uint8_t *buf, int size)
  308. {
  309. TLSContext *c = h->priv_data;
  310. int ret;
  311. // Set or clear the AVIO_FLAG_NONBLOCK on c->tls_shared.tcp
  312. c->tls_shared.tcp->flags &= ~AVIO_FLAG_NONBLOCK;
  313. c->tls_shared.tcp->flags |= h->flags & AVIO_FLAG_NONBLOCK;
  314. ret = SSL_write(c->ssl, buf, size);
  315. if (ret > 0)
  316. return ret;
  317. if (ret == 0)
  318. return AVERROR_EOF;
  319. return print_tls_error(h, ret);
  320. }
  321. static int tls_get_file_handle(URLContext *h)
  322. {
  323. TLSContext *c = h->priv_data;
  324. return ffurl_get_file_handle(c->tls_shared.tcp);
  325. }
  326. static int tls_get_short_seek(URLContext *h)
  327. {
  328. TLSContext *s = h->priv_data;
  329. return ffurl_get_short_seek(s->tls_shared.tcp);
  330. }
  331. static const AVOption options[] = {
  332. TLS_COMMON_OPTIONS(TLSContext, tls_shared),
  333. { NULL }
  334. };
  335. static const AVClass tls_class = {
  336. .class_name = "tls",
  337. .item_name = av_default_item_name,
  338. .option = options,
  339. .version = LIBAVUTIL_VERSION_INT,
  340. };
  341. const URLProtocol ff_tls_protocol = {
  342. .name = "tls",
  343. .url_open2 = tls_open,
  344. .url_read = tls_read,
  345. .url_write = tls_write,
  346. .url_close = tls_close,
  347. .url_get_file_handle = tls_get_file_handle,
  348. .url_get_short_seek = tls_get_short_seek,
  349. .priv_data_size = sizeof(TLSContext),
  350. .flags = URL_PROTOCOL_FLAG_NETWORK,
  351. .priv_data_class = &tls_class,
  352. };