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.

298 lines
7.7KB

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