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.

336 lines
9.1KB

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