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
10KB

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