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.

246 lines
7.2KB

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