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.

308 lines
8.0KB

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