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.

261 lines
7.8KB

  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 <errno.h>
  22. #include <gnutls/gnutls.h>
  23. #include <gnutls/x509.h>
  24. #include "avformat.h"
  25. #include "internal.h"
  26. #include "network.h"
  27. #include "os_support.h"
  28. #include "url.h"
  29. #include "tls.h"
  30. #include "libavcodec/internal.h"
  31. #include "libavutil/avstring.h"
  32. #include "libavutil/opt.h"
  33. #include "libavutil/parseutils.h"
  34. typedef struct TLSContext {
  35. const AVClass *class;
  36. TLSShared tls_shared;
  37. gnutls_session_t session;
  38. gnutls_certificate_credentials_t cred;
  39. int need_shutdown;
  40. } TLSContext;
  41. void ff_gnutls_init(void)
  42. {
  43. avpriv_lock_avformat();
  44. gnutls_global_init();
  45. avpriv_unlock_avformat();
  46. }
  47. void ff_gnutls_deinit(void)
  48. {
  49. avpriv_lock_avformat();
  50. gnutls_global_deinit();
  51. avpriv_unlock_avformat();
  52. }
  53. static int print_tls_error(URLContext *h, int ret)
  54. {
  55. switch (ret) {
  56. case GNUTLS_E_AGAIN:
  57. return AVERROR(EAGAIN);
  58. case GNUTLS_E_INTERRUPTED:
  59. break;
  60. case GNUTLS_E_WARNING_ALERT_RECEIVED:
  61. av_log(h, AV_LOG_WARNING, "%s\n", gnutls_strerror(ret));
  62. break;
  63. default:
  64. av_log(h, AV_LOG_ERROR, "%s\n", gnutls_strerror(ret));
  65. break;
  66. }
  67. return AVERROR(EIO);
  68. }
  69. static int tls_close(URLContext *h)
  70. {
  71. TLSContext *c = h->priv_data;
  72. if (c->need_shutdown)
  73. gnutls_bye(c->session, GNUTLS_SHUT_WR);
  74. if (c->session)
  75. gnutls_deinit(c->session);
  76. if (c->cred)
  77. gnutls_certificate_free_credentials(c->cred);
  78. if (c->tls_shared.tcp)
  79. ffurl_close(c->tls_shared.tcp);
  80. ff_gnutls_deinit();
  81. return 0;
  82. }
  83. static ssize_t gnutls_url_pull(gnutls_transport_ptr_t transport,
  84. void *buf, size_t len)
  85. {
  86. URLContext *h = (URLContext*) transport;
  87. int ret = ffurl_read(h, buf, len);
  88. if (ret >= 0)
  89. return ret;
  90. if (ret == AVERROR_EXIT)
  91. return 0;
  92. if (ret == AVERROR(EAGAIN))
  93. errno = EAGAIN;
  94. else
  95. errno = EIO;
  96. return -1;
  97. }
  98. static ssize_t gnutls_url_push(gnutls_transport_ptr_t transport,
  99. const void *buf, size_t len)
  100. {
  101. URLContext *h = (URLContext*) transport;
  102. int ret = ffurl_write(h, buf, len);
  103. if (ret >= 0)
  104. return ret;
  105. if (ret == AVERROR_EXIT)
  106. return 0;
  107. if (ret == AVERROR(EAGAIN))
  108. errno = EAGAIN;
  109. else
  110. errno = EIO;
  111. return -1;
  112. }
  113. static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
  114. {
  115. TLSContext *p = h->priv_data;
  116. TLSShared *c = &p->tls_shared;
  117. int ret;
  118. ff_gnutls_init();
  119. if ((ret = ff_tls_open_underlying(c, h, uri, options)) < 0)
  120. goto fail;
  121. gnutls_init(&p->session, c->listen ? GNUTLS_SERVER : GNUTLS_CLIENT);
  122. if (!c->listen && !c->numerichost)
  123. gnutls_server_name_set(p->session, GNUTLS_NAME_DNS, c->host, strlen(c->host));
  124. gnutls_certificate_allocate_credentials(&p->cred);
  125. if (c->ca_file)
  126. gnutls_certificate_set_x509_trust_file(p->cred, c->ca_file, GNUTLS_X509_FMT_PEM);
  127. #if GNUTLS_VERSION_MAJOR >= 3
  128. else
  129. gnutls_certificate_set_x509_system_trust(p->cred);
  130. #endif
  131. gnutls_certificate_set_verify_flags(p->cred, c->verify ?
  132. GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT : 0);
  133. if (c->cert_file && c->key_file) {
  134. ret = gnutls_certificate_set_x509_key_file(p->cred,
  135. c->cert_file, c->key_file,
  136. GNUTLS_X509_FMT_PEM);
  137. if (ret < 0) {
  138. av_log(h, AV_LOG_ERROR,
  139. "Unable to set cert/key files %s and %s: %s\n",
  140. c->cert_file, c->key_file, gnutls_strerror(ret));
  141. ret = AVERROR(EIO);
  142. goto fail;
  143. }
  144. }
  145. gnutls_credentials_set(p->session, GNUTLS_CRD_CERTIFICATE, p->cred);
  146. gnutls_transport_set_pull_function(p->session, gnutls_url_pull);
  147. gnutls_transport_set_push_function(p->session, gnutls_url_push);
  148. gnutls_transport_set_ptr(p->session, c->tcp);
  149. gnutls_priority_set_direct(p->session, "NORMAL", NULL);
  150. ret = gnutls_handshake(p->session);
  151. if (ret) {
  152. ret = print_tls_error(h, ret);
  153. goto fail;
  154. }
  155. p->need_shutdown = 1;
  156. if (c->verify) {
  157. unsigned int status, cert_list_size;
  158. gnutls_x509_crt_t cert;
  159. const gnutls_datum_t *cert_list;
  160. if ((ret = gnutls_certificate_verify_peers2(p->session, &status)) < 0) {
  161. av_log(h, AV_LOG_ERROR, "Unable to verify peer certificate: %s\n",
  162. gnutls_strerror(ret));
  163. ret = AVERROR(EIO);
  164. goto fail;
  165. }
  166. if (status & GNUTLS_CERT_INVALID) {
  167. av_log(h, AV_LOG_ERROR, "Peer certificate failed verification\n");
  168. ret = AVERROR(EIO);
  169. goto fail;
  170. }
  171. if (gnutls_certificate_type_get(p->session) != GNUTLS_CRT_X509) {
  172. av_log(h, AV_LOG_ERROR, "Unsupported certificate type\n");
  173. ret = AVERROR(EIO);
  174. goto fail;
  175. }
  176. gnutls_x509_crt_init(&cert);
  177. cert_list = gnutls_certificate_get_peers(p->session, &cert_list_size);
  178. gnutls_x509_crt_import(cert, cert_list, GNUTLS_X509_FMT_DER);
  179. ret = gnutls_x509_crt_check_hostname(cert, c->host);
  180. gnutls_x509_crt_deinit(cert);
  181. if (!ret) {
  182. av_log(h, AV_LOG_ERROR,
  183. "The certificate's owner does not match hostname %s\n", c->host);
  184. ret = AVERROR(EIO);
  185. goto fail;
  186. }
  187. }
  188. return 0;
  189. fail:
  190. tls_close(h);
  191. return ret;
  192. }
  193. static int tls_read(URLContext *h, uint8_t *buf, int size)
  194. {
  195. TLSContext *c = h->priv_data;
  196. int ret;
  197. // Set or clear the AVIO_FLAG_NONBLOCK on c->tls_shared.tcp
  198. c->tls_shared.tcp->flags &= ~AVIO_FLAG_NONBLOCK;
  199. c->tls_shared.tcp->flags |= h->flags & AVIO_FLAG_NONBLOCK;
  200. ret = gnutls_record_recv(c->session, buf, size);
  201. if (ret > 0)
  202. return ret;
  203. if (ret == 0)
  204. return AVERROR_EOF;
  205. return print_tls_error(h, ret);
  206. }
  207. static int tls_write(URLContext *h, const uint8_t *buf, int size)
  208. {
  209. TLSContext *c = h->priv_data;
  210. int ret;
  211. // Set or clear the AVIO_FLAG_NONBLOCK on c->tls_shared.tcp
  212. c->tls_shared.tcp->flags &= ~AVIO_FLAG_NONBLOCK;
  213. c->tls_shared.tcp->flags |= h->flags & AVIO_FLAG_NONBLOCK;
  214. ret = gnutls_record_send(c->session, buf, size);
  215. if (ret > 0)
  216. return ret;
  217. if (ret == 0)
  218. return AVERROR_EOF;
  219. return print_tls_error(h, ret);
  220. }
  221. static const AVOption options[] = {
  222. TLS_COMMON_OPTIONS(TLSContext, tls_shared),
  223. { NULL }
  224. };
  225. static const AVClass tls_class = {
  226. .class_name = "tls",
  227. .item_name = av_default_item_name,
  228. .option = options,
  229. .version = LIBAVUTIL_VERSION_INT,
  230. };
  231. const URLProtocol ff_tls_protocol = {
  232. .name = "tls",
  233. .url_open2 = tls_open,
  234. .url_read = tls_read,
  235. .url_write = tls_write,
  236. .url_close = tls_close,
  237. .priv_data_size = sizeof(TLSContext),
  238. .flags = URL_PROTOCOL_FLAG_NETWORK,
  239. .priv_data_class = &tls_class,
  240. };