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.

378 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
  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
  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
  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. if (c->tls_shared.tcp)
  135. ffurl_close(c->tls_shared.tcp);
  136. #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
  137. if (c->url_bio_method)
  138. BIO_meth_free(c->url_bio_method);
  139. #endif
  140. ff_openssl_deinit();
  141. return 0;
  142. }
  143. static int url_bio_create(BIO *b)
  144. {
  145. #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
  146. BIO_set_init(b, 1);
  147. BIO_set_data(b, NULL);
  148. BIO_set_flags(b, 0);
  149. #else
  150. b->init = 1;
  151. b->ptr = NULL;
  152. b->flags = 0;
  153. #endif
  154. return 1;
  155. }
  156. static int url_bio_destroy(BIO *b)
  157. {
  158. return 1;
  159. }
  160. #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
  161. #define GET_BIO_DATA(x) BIO_get_data(x)
  162. #else
  163. #define GET_BIO_DATA(x) (x)->ptr
  164. #endif
  165. static int url_bio_bread(BIO *b, char *buf, int len)
  166. {
  167. URLContext *h = GET_BIO_DATA(b);
  168. int ret = ffurl_read(h, buf, len);
  169. if (ret >= 0)
  170. return ret;
  171. BIO_clear_retry_flags(b);
  172. if (ret == AVERROR(EAGAIN))
  173. BIO_set_retry_read(b);
  174. if (ret == AVERROR_EXIT)
  175. return 0;
  176. return -1;
  177. }
  178. static int url_bio_bwrite(BIO *b, const char *buf, int len)
  179. {
  180. URLContext *h = GET_BIO_DATA(b);
  181. int ret = ffurl_write(h, buf, len);
  182. if (ret >= 0)
  183. return ret;
  184. BIO_clear_retry_flags(b);
  185. if (ret == AVERROR(EAGAIN))
  186. BIO_set_retry_write(b);
  187. if (ret == AVERROR_EXIT)
  188. return 0;
  189. return -1;
  190. }
  191. static long url_bio_ctrl(BIO *b, int cmd, long num, void *ptr)
  192. {
  193. if (cmd == BIO_CTRL_FLUSH) {
  194. BIO_clear_retry_flags(b);
  195. return 1;
  196. }
  197. return 0;
  198. }
  199. static int url_bio_bputs(BIO *b, const char *str)
  200. {
  201. return url_bio_bwrite(b, str, strlen(str));
  202. }
  203. #if OPENSSL_VERSION_NUMBER < 0x1010000fL
  204. static BIO_METHOD url_bio_method = {
  205. .type = BIO_TYPE_SOURCE_SINK,
  206. .name = "urlprotocol bio",
  207. .bwrite = url_bio_bwrite,
  208. .bread = url_bio_bread,
  209. .bputs = url_bio_bputs,
  210. .bgets = NULL,
  211. .ctrl = url_bio_ctrl,
  212. .create = url_bio_create,
  213. .destroy = url_bio_destroy,
  214. };
  215. #endif
  216. static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
  217. {
  218. TLSContext *p = h->priv_data;
  219. TLSShared *c = &p->tls_shared;
  220. BIO *bio;
  221. int ret;
  222. if ((ret = ff_openssl_init()) < 0)
  223. return ret;
  224. if ((ret = ff_tls_open_underlying(c, h, uri, options)) < 0)
  225. goto fail;
  226. // We want to support all versions of TLS >= 1.0, but not the deprecated
  227. // and insecure SSLv2 and SSLv3. Despite the name, SSLv23_*_method()
  228. // enables support for all versions of SSL and TLS, and we then disable
  229. // support for the old protocols immediately after creating the context.
  230. p->ctx = SSL_CTX_new(c->listen ? SSLv23_server_method() : SSLv23_client_method());
  231. if (!p->ctx) {
  232. av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  233. ret = AVERROR(EIO);
  234. goto fail;
  235. }
  236. SSL_CTX_set_options(p->ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
  237. if (c->ca_file) {
  238. if (!SSL_CTX_load_verify_locations(p->ctx, c->ca_file, NULL))
  239. av_log(h, AV_LOG_ERROR, "SSL_CTX_load_verify_locations %s\n", ERR_error_string(ERR_get_error(), NULL));
  240. }
  241. if (c->cert_file && !SSL_CTX_use_certificate_chain_file(p->ctx, c->cert_file)) {
  242. av_log(h, AV_LOG_ERROR, "Unable to load cert file %s: %s\n",
  243. c->cert_file, ERR_error_string(ERR_get_error(), NULL));
  244. ret = AVERROR(EIO);
  245. goto fail;
  246. }
  247. if (c->key_file && !SSL_CTX_use_PrivateKey_file(p->ctx, c->key_file, SSL_FILETYPE_PEM)) {
  248. av_log(h, AV_LOG_ERROR, "Unable to load key file %s: %s\n",
  249. c->key_file, ERR_error_string(ERR_get_error(), NULL));
  250. ret = AVERROR(EIO);
  251. goto fail;
  252. }
  253. // Note, this doesn't check that the peer certificate actually matches
  254. // the requested hostname.
  255. if (c->verify)
  256. SSL_CTX_set_verify(p->ctx, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
  257. p->ssl = SSL_new(p->ctx);
  258. if (!p->ssl) {
  259. av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  260. ret = AVERROR(EIO);
  261. goto fail;
  262. }
  263. #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
  264. p->url_bio_method = BIO_meth_new(BIO_TYPE_SOURCE_SINK, "urlprotocol bio");
  265. BIO_meth_set_write(p->url_bio_method, url_bio_bwrite);
  266. BIO_meth_set_read(p->url_bio_method, url_bio_bread);
  267. BIO_meth_set_puts(p->url_bio_method, url_bio_bputs);
  268. BIO_meth_set_ctrl(p->url_bio_method, url_bio_ctrl);
  269. BIO_meth_set_create(p->url_bio_method, url_bio_create);
  270. BIO_meth_set_destroy(p->url_bio_method, url_bio_destroy);
  271. bio = BIO_new(p->url_bio_method);
  272. BIO_set_data(bio, c->tcp);
  273. #else
  274. bio = BIO_new(&url_bio_method);
  275. bio->ptr = c->tcp;
  276. #endif
  277. SSL_set_bio(p->ssl, bio, bio);
  278. if (!c->listen && !c->numerichost)
  279. SSL_set_tlsext_host_name(p->ssl, c->host);
  280. ret = c->listen ? SSL_accept(p->ssl) : SSL_connect(p->ssl);
  281. if (ret == 0) {
  282. av_log(h, AV_LOG_ERROR, "Unable to negotiate TLS/SSL session\n");
  283. ret = AVERROR(EIO);
  284. goto fail;
  285. } else if (ret < 0) {
  286. ret = print_tls_error(h, ret);
  287. goto fail;
  288. }
  289. return 0;
  290. fail:
  291. tls_close(h);
  292. return ret;
  293. }
  294. static int tls_read(URLContext *h, uint8_t *buf, int size)
  295. {
  296. TLSContext *c = h->priv_data;
  297. int ret;
  298. // Set or clear the AVIO_FLAG_NONBLOCK on c->tls_shared.tcp
  299. c->tls_shared.tcp->flags &= ~AVIO_FLAG_NONBLOCK;
  300. c->tls_shared.tcp->flags |= h->flags & AVIO_FLAG_NONBLOCK;
  301. ret = SSL_read(c->ssl, buf, size);
  302. if (ret > 0)
  303. return ret;
  304. if (ret == 0)
  305. return AVERROR_EOF;
  306. return print_tls_error(h, ret);
  307. }
  308. static int tls_write(URLContext *h, const uint8_t *buf, int size)
  309. {
  310. TLSContext *c = h->priv_data;
  311. int ret;
  312. // Set or clear the AVIO_FLAG_NONBLOCK on c->tls_shared.tcp
  313. c->tls_shared.tcp->flags &= ~AVIO_FLAG_NONBLOCK;
  314. c->tls_shared.tcp->flags |= h->flags & AVIO_FLAG_NONBLOCK;
  315. ret = SSL_write(c->ssl, buf, size);
  316. if (ret > 0)
  317. return ret;
  318. if (ret == 0)
  319. return AVERROR_EOF;
  320. return print_tls_error(h, ret);
  321. }
  322. static int tls_get_file_handle(URLContext *h)
  323. {
  324. TLSContext *c = h->priv_data;
  325. return ffurl_get_file_handle(c->tls_shared.tcp);
  326. }
  327. static const AVOption options[] = {
  328. TLS_COMMON_OPTIONS(TLSContext, tls_shared),
  329. { NULL }
  330. };
  331. static const AVClass tls_class = {
  332. .class_name = "tls",
  333. .item_name = av_default_item_name,
  334. .option = options,
  335. .version = LIBAVUTIL_VERSION_INT,
  336. };
  337. const URLProtocol ff_tls_protocol = {
  338. .name = "tls",
  339. .url_open2 = tls_open,
  340. .url_read = tls_read,
  341. .url_write = tls_write,
  342. .url_close = tls_close,
  343. .url_get_file_handle = tls_get_file_handle,
  344. .priv_data_size = sizeof(TLSContext),
  345. .flags = URL_PROTOCOL_FLAG_NETWORK,
  346. .priv_data_class = &tls_class,
  347. };