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.

352 lines
12KB

  1. /*
  2. * TLS/SSL Protocol
  3. * Copyright (c) 2018 Thomas Volkert
  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 <mbedtls/certs.h>
  22. #include <mbedtls/config.h>
  23. #include <mbedtls/ctr_drbg.h>
  24. #include <mbedtls/entropy.h>
  25. #include <mbedtls/net.h>
  26. #include <mbedtls/platform.h>
  27. #include <mbedtls/ssl.h>
  28. #include <mbedtls/x509_crt.h>
  29. #include "avformat.h"
  30. #include "internal.h"
  31. #include "url.h"
  32. #include "tls.h"
  33. #include "libavutil/parseutils.h"
  34. typedef struct TLSContext {
  35. const AVClass *class;
  36. TLSShared tls_shared;
  37. mbedtls_ssl_context ssl_context;
  38. mbedtls_ssl_config ssl_config;
  39. mbedtls_entropy_context entropy_context;
  40. mbedtls_ctr_drbg_context ctr_drbg_context;
  41. mbedtls_x509_crt ca_cert;
  42. mbedtls_x509_crt own_cert;
  43. mbedtls_pk_context priv_key;
  44. char *priv_key_pw;
  45. } TLSContext;
  46. #define OFFSET(x) offsetof(TLSContext, x)
  47. static int tls_close(URLContext *h)
  48. {
  49. TLSContext *tls_ctx = h->priv_data;
  50. mbedtls_ssl_close_notify(&tls_ctx->ssl_context);
  51. mbedtls_pk_free(&tls_ctx->priv_key);
  52. mbedtls_x509_crt_free(&tls_ctx->ca_cert);
  53. mbedtls_x509_crt_free(&tls_ctx->own_cert);
  54. mbedtls_ssl_free(&tls_ctx->ssl_context);
  55. mbedtls_ssl_config_free(&tls_ctx->ssl_config);
  56. mbedtls_ctr_drbg_free(&tls_ctx->ctr_drbg_context);
  57. mbedtls_entropy_free(&tls_ctx->entropy_context);
  58. return 0;
  59. }
  60. static int handle_transport_error(URLContext *h, const char* func_name, int react_on_eagain, int ret)
  61. {
  62. switch (ret) {
  63. case AVERROR(EAGAIN):
  64. return react_on_eagain;
  65. case AVERROR_EXIT:
  66. return 0;
  67. case AVERROR(EPIPE):
  68. case AVERROR(ECONNRESET):
  69. return MBEDTLS_ERR_NET_CONN_RESET;
  70. default:
  71. av_log(h, AV_LOG_ERROR, "%s returned 0x%x\n", func_name, ret);
  72. errno = EIO;
  73. return MBEDTLS_ERR_NET_SEND_FAILED;
  74. }
  75. }
  76. static int mbedtls_send(void *ctx, const unsigned char *buf, size_t len)
  77. {
  78. URLContext *h = (URLContext*) ctx;
  79. int ret = ffurl_write(h, buf, len);
  80. if (ret >= 0)
  81. return ret;
  82. if (h->max_packet_size && len > h->max_packet_size)
  83. return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
  84. return handle_transport_error(h, "ffurl_write", MBEDTLS_ERR_SSL_WANT_WRITE, ret);
  85. }
  86. static int mbedtls_recv(void *ctx, unsigned char *buf, size_t len)
  87. {
  88. URLContext *h = (URLContext*) ctx;
  89. int ret = ffurl_read(h, buf, len);
  90. if (ret >= 0)
  91. return ret;
  92. if (h->max_packet_size && len > h->max_packet_size)
  93. return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
  94. return handle_transport_error(h, "ffurl_read", MBEDTLS_ERR_SSL_WANT_READ, ret);
  95. }
  96. static void handle_pk_parse_error(URLContext *h, int ret)
  97. {
  98. switch (ret) {
  99. case MBEDTLS_ERR_PK_FILE_IO_ERROR:
  100. av_log(h, AV_LOG_ERROR, "Read of key file failed. Is it actually there, are the access permissions correct?\n");
  101. break;
  102. case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
  103. av_log(h, AV_LOG_ERROR, "A password for the private key is missing.\n");
  104. break;
  105. case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
  106. av_log(h, AV_LOG_ERROR, "The given password for the private key is wrong.\n");
  107. break;
  108. default:
  109. av_log(h, AV_LOG_ERROR, "mbedtls_pk_parse_key returned -0x%x\n", -ret);
  110. break;
  111. }
  112. }
  113. static void handle_handshake_error(URLContext *h, int ret)
  114. {
  115. switch (ret) {
  116. case MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE:
  117. av_log(h, AV_LOG_ERROR, "None of the common ciphersuites is usable. Was the local certificate correctly set?\n");
  118. break;
  119. case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
  120. av_log(h, AV_LOG_ERROR, "A fatal alert message was received from the peer, has the peer a correct certificate?\n");
  121. break;
  122. case MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED:
  123. av_log(h, AV_LOG_ERROR, "No CA chain is set, but required to operate. Was the CA correctly set?\n");
  124. break;
  125. case MBEDTLS_ERR_NET_CONN_RESET:
  126. av_log(h, AV_LOG_ERROR, "TLS handshake was aborted by peer.\n");
  127. break;
  128. default:
  129. av_log(h, AV_LOG_ERROR, "mbedtls_ssl_handshake returned -0x%x\n", -ret);
  130. break;
  131. }
  132. }
  133. static void parse_options(TLSContext *tls_ctxc, const char *uri)
  134. {
  135. char buf[1024];
  136. const char *p = strchr(uri, '?');
  137. if (!p)
  138. return;
  139. if (!tls_ctxc->priv_key_pw && av_find_info_tag(buf, sizeof(buf), "key_password", p))
  140. tls_ctxc->priv_key_pw = av_strdup(buf);
  141. }
  142. static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
  143. {
  144. TLSContext *tls_ctx = h->priv_data;
  145. TLSShared *shr = &tls_ctx->tls_shared;
  146. uint32_t verify_res_flags;
  147. int ret;
  148. // parse additional options
  149. parse_options(tls_ctx, uri);
  150. if ((ret = ff_tls_open_underlying(shr, h, uri, options)) < 0)
  151. goto fail;
  152. mbedtls_ssl_init(&tls_ctx->ssl_context);
  153. mbedtls_ssl_config_init(&tls_ctx->ssl_config);
  154. mbedtls_entropy_init(&tls_ctx->entropy_context);
  155. mbedtls_ctr_drbg_init(&tls_ctx->ctr_drbg_context);
  156. mbedtls_x509_crt_init(&tls_ctx->ca_cert);
  157. mbedtls_pk_init(&tls_ctx->priv_key);
  158. // load trusted CA
  159. if (shr->ca_file) {
  160. if ((ret = mbedtls_x509_crt_parse_file(&tls_ctx->ca_cert, shr->ca_file)) != 0) {
  161. av_log(h, AV_LOG_ERROR, "mbedtls_x509_crt_parse_file for CA cert returned %d\n", ret);
  162. goto fail;
  163. }
  164. }
  165. // load own certificate
  166. if (shr->cert_file) {
  167. if ((ret = mbedtls_x509_crt_parse_file(&tls_ctx->own_cert, shr->cert_file)) != 0) {
  168. av_log(h, AV_LOG_ERROR, "mbedtls_x509_crt_parse_file for own cert returned %d\n", ret);
  169. goto fail;
  170. }
  171. }
  172. // load key file
  173. if (shr->key_file) {
  174. if ((ret = mbedtls_pk_parse_keyfile(&tls_ctx->priv_key,
  175. shr->key_file,
  176. tls_ctx->priv_key_pw)) != 0) {
  177. handle_pk_parse_error(h, ret);
  178. goto fail;
  179. }
  180. }
  181. // seed the random number generator
  182. if ((ret = mbedtls_ctr_drbg_seed(&tls_ctx->ctr_drbg_context,
  183. mbedtls_entropy_func,
  184. &tls_ctx->entropy_context,
  185. NULL, 0)) != 0) {
  186. av_log(h, AV_LOG_ERROR, "mbedtls_ctr_drbg_seed returned %d\n", ret);
  187. goto fail;
  188. }
  189. if ((ret = mbedtls_ssl_config_defaults(&tls_ctx->ssl_config,
  190. shr->listen ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT,
  191. MBEDTLS_SSL_TRANSPORT_STREAM,
  192. MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
  193. av_log(h, AV_LOG_ERROR, "mbedtls_ssl_config_defaults returned %d\n", ret);
  194. goto fail;
  195. }
  196. mbedtls_ssl_conf_authmode(&tls_ctx->ssl_config,
  197. shr->ca_file ? MBEDTLS_SSL_VERIFY_REQUIRED : MBEDTLS_SSL_VERIFY_NONE);
  198. mbedtls_ssl_conf_rng(&tls_ctx->ssl_config, mbedtls_ctr_drbg_random, &tls_ctx->ctr_drbg_context);
  199. mbedtls_ssl_conf_ca_chain(&tls_ctx->ssl_config, &tls_ctx->ca_cert, NULL);
  200. // set own certificate and private key
  201. if ((ret = mbedtls_ssl_conf_own_cert(&tls_ctx->ssl_config, &tls_ctx->own_cert, &tls_ctx->priv_key)) != 0) {
  202. av_log(h, AV_LOG_ERROR, "mbedtls_ssl_conf_own_cert returned %d\n", ret);
  203. goto fail;
  204. }
  205. if ((ret = mbedtls_ssl_setup(&tls_ctx->ssl_context, &tls_ctx->ssl_config)) != 0) {
  206. av_log(h, AV_LOG_ERROR, "mbedtls_ssl_setup returned %d\n", ret);
  207. goto fail;
  208. }
  209. if (!shr->listen && !shr->numerichost) {
  210. if ((ret = mbedtls_ssl_set_hostname(&tls_ctx->ssl_context, shr->host)) != 0) {
  211. av_log(h, AV_LOG_ERROR, "mbedtls_ssl_set_hostname returned %d\n", ret);
  212. goto fail;
  213. }
  214. }
  215. // set I/O functions to use FFmpeg internal code for transport layer
  216. mbedtls_ssl_set_bio(&tls_ctx->ssl_context, shr->tcp, mbedtls_send, mbedtls_recv, NULL);
  217. // ssl handshake
  218. while ((ret = mbedtls_ssl_handshake(&tls_ctx->ssl_context)) != 0) {
  219. if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
  220. handle_handshake_error(h, ret);
  221. goto fail;
  222. }
  223. }
  224. if (shr->verify) {
  225. // check the result of the certificate verification
  226. if ((verify_res_flags = mbedtls_ssl_get_verify_result(&tls_ctx->ssl_context)) != 0) {
  227. av_log(h, AV_LOG_ERROR, "mbedtls_ssl_get_verify_result reported problems "\
  228. "with the certificate verification, returned flags: %u\n",
  229. verify_res_flags);
  230. if (verify_res_flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED)
  231. av_log(h, AV_LOG_ERROR, "The certificate is not correctly signed by the trusted CA.\n");
  232. goto fail;
  233. }
  234. }
  235. return 0;
  236. fail:
  237. tls_close(h);
  238. return AVERROR(EIO);
  239. }
  240. static int handle_tls_error(URLContext *h, const char* func_name, int ret)
  241. {
  242. switch (ret) {
  243. case MBEDTLS_ERR_SSL_WANT_READ:
  244. case MBEDTLS_ERR_SSL_WANT_WRITE:
  245. return AVERROR(EAGAIN);
  246. case MBEDTLS_ERR_NET_SEND_FAILED:
  247. case MBEDTLS_ERR_NET_RECV_FAILED:
  248. return AVERROR(EIO);
  249. case MBEDTLS_ERR_NET_CONN_RESET:
  250. case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
  251. av_log(h, AV_LOG_WARNING, "%s reported connection reset by peer\n", func_name);
  252. return AVERROR_EOF;
  253. default:
  254. av_log(h, AV_LOG_ERROR, "%s returned -0x%x\n", func_name, -ret);
  255. return AVERROR(EIO);
  256. }
  257. }
  258. static int tls_read(URLContext *h, uint8_t *buf, int size)
  259. {
  260. TLSContext *tls_ctx = h->priv_data;
  261. int ret;
  262. if ((ret = mbedtls_ssl_read(&tls_ctx->ssl_context, buf, size)) > 0) {
  263. // return read length
  264. return ret;
  265. }
  266. return handle_tls_error(h, "mbedtls_ssl_read", ret);
  267. }
  268. static int tls_write(URLContext *h, const uint8_t *buf, int size)
  269. {
  270. TLSContext *tls_ctx = h->priv_data;
  271. int ret;
  272. if ((ret = mbedtls_ssl_write(&tls_ctx->ssl_context, buf, size)) > 0) {
  273. // return written length
  274. return ret;
  275. }
  276. return handle_tls_error(h, "mbedtls_ssl_write", ret);
  277. }
  278. static int tls_get_file_handle(URLContext *h)
  279. {
  280. TLSContext *c = h->priv_data;
  281. return ffurl_get_file_handle(c->tls_shared.tcp);
  282. }
  283. static const AVOption options[] = {
  284. TLS_COMMON_OPTIONS(TLSContext, tls_shared), \
  285. {"key_password", "Password for the private key file", OFFSET(priv_key_pw), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \
  286. { NULL }
  287. };
  288. static const AVClass tls_class = {
  289. .class_name = "tls",
  290. .item_name = av_default_item_name,
  291. .option = options,
  292. .version = LIBAVUTIL_VERSION_INT,
  293. };
  294. const URLProtocol ff_tls_protocol = {
  295. .name = "tls",
  296. .url_open2 = tls_open,
  297. .url_read = tls_read,
  298. .url_write = tls_write,
  299. .url_close = tls_close,
  300. .url_get_file_handle = tls_get_file_handle,
  301. .priv_data_size = sizeof(TLSContext),
  302. .flags = URL_PROTOCOL_FLAG_NETWORK,
  303. .priv_data_class = &tls_class,
  304. };