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.

350 lines
9.5KB

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