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.

622 lines
20KB

  1. /*
  2. * Copyright (c) 2015 Hendrik Leppkes
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /** Based on the CURL SChannel module */
  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. #define SECURITY_WIN32
  28. #include <windows.h>
  29. #include <security.h>
  30. #include <schnlsp.h>
  31. #define SCHANNEL_INITIAL_BUFFER_SIZE 4096
  32. #define SCHANNEL_FREE_BUFFER_SIZE 1024
  33. /* mingw does not define this symbol */
  34. #ifndef SECBUFFER_ALERT
  35. #define SECBUFFER_ALERT 17
  36. #endif
  37. typedef struct TLSContext {
  38. const AVClass *class;
  39. TLSShared tls_shared;
  40. CredHandle cred_handle;
  41. TimeStamp cred_timestamp;
  42. CtxtHandle ctxt_handle;
  43. TimeStamp ctxt_timestamp;
  44. ULONG request_flags;
  45. ULONG context_flags;
  46. uint8_t *enc_buf;
  47. int enc_buf_size;
  48. int enc_buf_offset;
  49. uint8_t *dec_buf;
  50. int dec_buf_size;
  51. int dec_buf_offset;
  52. SecPkgContext_StreamSizes sizes;
  53. int connected;
  54. int connection_closed;
  55. int sspi_close_notify;
  56. } TLSContext;
  57. static void init_sec_buffer(SecBuffer *buffer, unsigned long type,
  58. void *data, unsigned long size)
  59. {
  60. buffer->cbBuffer = size;
  61. buffer->BufferType = type;
  62. buffer->pvBuffer = data;
  63. }
  64. static void init_sec_buffer_desc(SecBufferDesc *desc, SecBuffer *buffers,
  65. unsigned long buffer_count)
  66. {
  67. desc->ulVersion = SECBUFFER_VERSION;
  68. desc->pBuffers = buffers;
  69. desc->cBuffers = buffer_count;
  70. }
  71. static int tls_shutdown_client(URLContext *h)
  72. {
  73. TLSContext *c = h->priv_data;
  74. TLSShared *s = &c->tls_shared;
  75. int ret;
  76. if (c->connected) {
  77. SecBufferDesc BuffDesc;
  78. SecBuffer Buffer;
  79. SECURITY_STATUS sspi_ret;
  80. SecBuffer outbuf;
  81. SecBufferDesc outbuf_desc;
  82. DWORD dwshut = SCHANNEL_SHUTDOWN;
  83. init_sec_buffer(&Buffer, SECBUFFER_TOKEN, &dwshut, sizeof(dwshut));
  84. init_sec_buffer_desc(&BuffDesc, &Buffer, 1);
  85. sspi_ret = ApplyControlToken(&c->ctxt_handle, &BuffDesc);
  86. if (sspi_ret != SEC_E_OK)
  87. av_log(h, AV_LOG_ERROR, "ApplyControlToken failed\n");
  88. init_sec_buffer(&outbuf, SECBUFFER_EMPTY, NULL, 0);
  89. init_sec_buffer_desc(&outbuf_desc, &outbuf, 1);
  90. sspi_ret = InitializeSecurityContext(&c->cred_handle, &c->ctxt_handle, s->host,
  91. c->request_flags, 0, 0, NULL, 0, &c->ctxt_handle,
  92. &outbuf_desc, &c->context_flags, &c->ctxt_timestamp);
  93. if (sspi_ret == SEC_E_OK || sspi_ret == SEC_I_CONTEXT_EXPIRED) {
  94. ret = ffurl_write(s->tcp, outbuf.pvBuffer, outbuf.cbBuffer);
  95. FreeContextBuffer(outbuf.pvBuffer);
  96. if (ret < 0 || ret != outbuf.cbBuffer)
  97. av_log(h, AV_LOG_ERROR, "Failed to send close message\n");
  98. }
  99. c->connected = 0;
  100. }
  101. return 0;
  102. }
  103. static int tls_close(URLContext *h)
  104. {
  105. TLSContext *c = h->priv_data;
  106. tls_shutdown_client(h);
  107. DeleteSecurityContext(&c->ctxt_handle);
  108. FreeCredentialsHandle(&c->cred_handle);
  109. av_freep(&c->enc_buf);
  110. c->enc_buf_size = c->enc_buf_offset = 0;
  111. av_freep(&c->dec_buf);
  112. c->dec_buf_size = c->dec_buf_offset = 0;
  113. ffurl_closep(&c->tls_shared.tcp);
  114. return 0;
  115. }
  116. static int tls_client_handshake_loop(URLContext *h, int initial)
  117. {
  118. TLSContext *c = h->priv_data;
  119. TLSShared *s = &c->tls_shared;
  120. SECURITY_STATUS sspi_ret;
  121. SecBuffer outbuf[3] = { 0 };
  122. SecBufferDesc outbuf_desc;
  123. SecBuffer inbuf[2];
  124. SecBufferDesc inbuf_desc;
  125. int i, ret = 0, read_data = initial;
  126. if (c->enc_buf == NULL) {
  127. c->enc_buf_offset = 0;
  128. ret = av_reallocp(&c->enc_buf, SCHANNEL_INITIAL_BUFFER_SIZE);
  129. if (ret < 0)
  130. goto fail;
  131. c->enc_buf_size = SCHANNEL_INITIAL_BUFFER_SIZE;
  132. }
  133. if (c->dec_buf == NULL) {
  134. c->dec_buf_offset = 0;
  135. ret = av_reallocp(&c->dec_buf, SCHANNEL_INITIAL_BUFFER_SIZE);
  136. if (ret < 0)
  137. goto fail;
  138. c->dec_buf_size = SCHANNEL_INITIAL_BUFFER_SIZE;
  139. }
  140. while (1) {
  141. if (c->enc_buf_size - c->enc_buf_offset < SCHANNEL_FREE_BUFFER_SIZE) {
  142. c->enc_buf_size = c->enc_buf_offset + SCHANNEL_FREE_BUFFER_SIZE;
  143. ret = av_reallocp(&c->enc_buf, c->enc_buf_size);
  144. if (ret < 0) {
  145. c->enc_buf_size = c->enc_buf_offset = 0;
  146. goto fail;
  147. }
  148. }
  149. if (read_data) {
  150. ret = ffurl_read(c->tls_shared.tcp, c->enc_buf + c->enc_buf_offset,
  151. c->enc_buf_size - c->enc_buf_offset);
  152. if (ret < 0) {
  153. av_log(h, AV_LOG_ERROR, "Failed to read handshake response\n");
  154. goto fail;
  155. }
  156. c->enc_buf_offset += ret;
  157. }
  158. /* input buffers */
  159. init_sec_buffer(&inbuf[0], SECBUFFER_TOKEN, av_malloc(c->enc_buf_offset), c->enc_buf_offset);
  160. init_sec_buffer(&inbuf[1], SECBUFFER_EMPTY, NULL, 0);
  161. init_sec_buffer_desc(&inbuf_desc, inbuf, 2);
  162. if (inbuf[0].pvBuffer == NULL) {
  163. av_log(h, AV_LOG_ERROR, "Failed to allocate input buffer\n");
  164. ret = AVERROR(ENOMEM);
  165. goto fail;
  166. }
  167. memcpy(inbuf[0].pvBuffer, c->enc_buf, c->enc_buf_offset);
  168. /* output buffers */
  169. init_sec_buffer(&outbuf[0], SECBUFFER_TOKEN, NULL, 0);
  170. init_sec_buffer(&outbuf[1], SECBUFFER_ALERT, NULL, 0);
  171. init_sec_buffer(&outbuf[2], SECBUFFER_EMPTY, NULL, 0);
  172. init_sec_buffer_desc(&outbuf_desc, outbuf, 3);
  173. sspi_ret = InitializeSecurityContext(&c->cred_handle, &c->ctxt_handle, s->host, c->request_flags,
  174. 0, 0, &inbuf_desc, 0, NULL, &outbuf_desc, &c->context_flags,
  175. &c->ctxt_timestamp);
  176. av_freep(&inbuf[0].pvBuffer);
  177. if (sspi_ret == SEC_E_INCOMPLETE_MESSAGE) {
  178. av_log(h, AV_LOG_DEBUG, "Received incomplete handshake, need more data\n");
  179. read_data = 1;
  180. continue;
  181. }
  182. /* remote requests a client certificate - attempt to continue without one anyway */
  183. if (sspi_ret == SEC_I_INCOMPLETE_CREDENTIALS &&
  184. !(c->request_flags & ISC_REQ_USE_SUPPLIED_CREDS)) {
  185. av_log(h, AV_LOG_VERBOSE, "Client certificate has been requested, ignoring\n");
  186. c->request_flags |= ISC_REQ_USE_SUPPLIED_CREDS;
  187. read_data = 0;
  188. continue;
  189. }
  190. /* continue handshake */
  191. if (sspi_ret == SEC_I_CONTINUE_NEEDED || sspi_ret == SEC_E_OK) {
  192. for (i = 0; i < 3; i++) {
  193. if (outbuf[i].BufferType == SECBUFFER_TOKEN && outbuf[i].cbBuffer > 0) {
  194. ret = ffurl_write(c->tls_shared.tcp, outbuf[i].pvBuffer, outbuf[i].cbBuffer);
  195. if (ret < 0 || ret != outbuf[i].cbBuffer) {
  196. av_log(h, AV_LOG_VERBOSE, "Failed to send handshake data\n");
  197. ret = AVERROR(EIO);
  198. goto fail;
  199. }
  200. }
  201. if (outbuf[i].pvBuffer != NULL) {
  202. FreeContextBuffer(outbuf[i].pvBuffer);
  203. outbuf[i].pvBuffer = NULL;
  204. }
  205. }
  206. } else {
  207. if (sspi_ret == SEC_E_WRONG_PRINCIPAL)
  208. av_log(h, AV_LOG_ERROR, "SNI or certificate check failed\n");
  209. else
  210. av_log(h, AV_LOG_ERROR, "Creating security context failed (0x%lx)\n", sspi_ret);
  211. ret = AVERROR_UNKNOWN;
  212. goto fail;
  213. }
  214. if (inbuf[1].BufferType == SECBUFFER_EXTRA && inbuf[1].cbBuffer > 0) {
  215. if (c->enc_buf_offset > inbuf[1].cbBuffer) {
  216. memmove(c->enc_buf, (c->enc_buf + c->enc_buf_offset) - inbuf[1].cbBuffer,
  217. inbuf[1].cbBuffer);
  218. c->enc_buf_offset = inbuf[1].cbBuffer;
  219. if (sspi_ret == SEC_I_CONTINUE_NEEDED) {
  220. read_data = 0;
  221. continue;
  222. }
  223. }
  224. } else {
  225. c->enc_buf_offset = 0;
  226. }
  227. if (sspi_ret == SEC_I_CONTINUE_NEEDED) {
  228. read_data = 1;
  229. continue;
  230. }
  231. break;
  232. }
  233. return 0;
  234. fail:
  235. /* free any remaining output data */
  236. for (i = 0; i < 3; i++) {
  237. if (outbuf[i].pvBuffer != NULL) {
  238. FreeContextBuffer(outbuf[i].pvBuffer);
  239. outbuf[i].pvBuffer = NULL;
  240. }
  241. }
  242. return ret;
  243. }
  244. static int tls_client_handshake(URLContext *h)
  245. {
  246. TLSContext *c = h->priv_data;
  247. TLSShared *s = &c->tls_shared;
  248. SecBuffer outbuf;
  249. SecBufferDesc outbuf_desc;
  250. SECURITY_STATUS sspi_ret;
  251. int ret;
  252. init_sec_buffer(&outbuf, SECBUFFER_EMPTY, NULL, 0);
  253. init_sec_buffer_desc(&outbuf_desc, &outbuf, 1);
  254. c->request_flags = ISC_REQ_SEQUENCE_DETECT | ISC_REQ_REPLAY_DETECT |
  255. ISC_REQ_CONFIDENTIALITY | ISC_REQ_ALLOCATE_MEMORY |
  256. ISC_REQ_STREAM;
  257. sspi_ret = InitializeSecurityContext(&c->cred_handle, NULL, s->host, c->request_flags, 0, 0,
  258. NULL, 0, &c->ctxt_handle, &outbuf_desc, &c->context_flags,
  259. &c->ctxt_timestamp);
  260. if (sspi_ret != SEC_I_CONTINUE_NEEDED) {
  261. av_log(h, AV_LOG_ERROR, "Unable to create initial security context (0x%lx)\n", sspi_ret);
  262. ret = AVERROR_UNKNOWN;
  263. goto fail;
  264. }
  265. ret = ffurl_write(s->tcp, outbuf.pvBuffer, outbuf.cbBuffer);
  266. FreeContextBuffer(outbuf.pvBuffer);
  267. if (ret < 0 || ret != outbuf.cbBuffer) {
  268. av_log(h, AV_LOG_ERROR, "Failed to send initial handshake data\n");
  269. ret = AVERROR(EIO);
  270. goto fail;
  271. }
  272. return tls_client_handshake_loop(h, 1);
  273. fail:
  274. DeleteSecurityContext(&c->ctxt_handle);
  275. return ret;
  276. }
  277. static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
  278. {
  279. TLSContext *c = h->priv_data;
  280. TLSShared *s = &c->tls_shared;
  281. SECURITY_STATUS sspi_ret;
  282. SCHANNEL_CRED schannel_cred = { 0 };
  283. int ret;
  284. if ((ret = ff_tls_open_underlying(s, h, uri, options)) < 0)
  285. goto fail;
  286. if (s->listen) {
  287. av_log(h, AV_LOG_ERROR, "TLS Listen Sockets with SChannel is not implemented.\n");
  288. ret = AVERROR(EINVAL);
  289. goto fail;
  290. }
  291. /* SChannel Options */
  292. schannel_cred.dwVersion = SCHANNEL_CRED_VERSION;
  293. if (s->verify)
  294. schannel_cred.dwFlags = SCH_CRED_AUTO_CRED_VALIDATION |
  295. SCH_CRED_REVOCATION_CHECK_CHAIN;
  296. else
  297. schannel_cred.dwFlags = SCH_CRED_MANUAL_CRED_VALIDATION |
  298. SCH_CRED_IGNORE_NO_REVOCATION_CHECK |
  299. SCH_CRED_IGNORE_REVOCATION_OFFLINE;
  300. /* Get credential handle */
  301. sspi_ret = AcquireCredentialsHandle(NULL, (TCHAR *)UNISP_NAME, SECPKG_CRED_OUTBOUND,
  302. NULL, &schannel_cred, NULL, NULL, &c->cred_handle,
  303. &c->cred_timestamp);
  304. if (sspi_ret != SEC_E_OK) {
  305. av_log(h, AV_LOG_ERROR, "Unable to acquire security credentials (0x%lx)\n", sspi_ret);
  306. ret = AVERROR_UNKNOWN;
  307. goto fail;
  308. }
  309. ret = tls_client_handshake(h);
  310. if (ret < 0)
  311. goto fail;
  312. c->connected = 1;
  313. return 0;
  314. fail:
  315. tls_close(h);
  316. return ret;
  317. }
  318. static int tls_read(URLContext *h, uint8_t *buf, int len)
  319. {
  320. TLSContext *c = h->priv_data;
  321. TLSShared *s = &c->tls_shared;
  322. SECURITY_STATUS sspi_ret = SEC_E_OK;
  323. SecBuffer inbuf[4];
  324. SecBufferDesc inbuf_desc;
  325. int size, ret;
  326. int min_enc_buf_size = len + SCHANNEL_FREE_BUFFER_SIZE;
  327. /* If we have some left-over data from previous network activity,
  328. * return it first in case it is enough. It may contain
  329. * data that is required to know whether this connection
  330. * is still required or not, esp. in case of HTTP keep-alive
  331. * connections. */
  332. if (c->dec_buf_offset > 0)
  333. goto cleanup;
  334. if (c->sspi_close_notify)
  335. goto cleanup;
  336. if (!c->connection_closed) {
  337. size = c->enc_buf_size - c->enc_buf_offset;
  338. if (size < SCHANNEL_FREE_BUFFER_SIZE || c->enc_buf_size < min_enc_buf_size) {
  339. c->enc_buf_size = c->enc_buf_offset + SCHANNEL_FREE_BUFFER_SIZE;
  340. if (c->enc_buf_size < min_enc_buf_size)
  341. c->enc_buf_size = min_enc_buf_size;
  342. ret = av_reallocp(&c->enc_buf, c->enc_buf_size);
  343. if (ret < 0) {
  344. c->enc_buf_size = c->enc_buf_offset = 0;
  345. return ret;
  346. }
  347. }
  348. ret = ffurl_read(s->tcp, c->enc_buf + c->enc_buf_offset,
  349. c->enc_buf_size - c->enc_buf_offset);
  350. if (ret == AVERROR_EOF) {
  351. c->connection_closed = 1;
  352. ret = 0;
  353. } else if (ret < 0) {
  354. av_log(h, AV_LOG_ERROR, "Unable to read from socket\n");
  355. return ret;
  356. }
  357. c->enc_buf_offset += ret;
  358. }
  359. while (c->enc_buf_offset > 0 && sspi_ret == SEC_E_OK) {
  360. /* input buffer */
  361. init_sec_buffer(&inbuf[0], SECBUFFER_DATA, c->enc_buf, c->enc_buf_offset);
  362. /* additional buffers for possible output */
  363. init_sec_buffer(&inbuf[1], SECBUFFER_EMPTY, NULL, 0);
  364. init_sec_buffer(&inbuf[2], SECBUFFER_EMPTY, NULL, 0);
  365. init_sec_buffer(&inbuf[3], SECBUFFER_EMPTY, NULL, 0);
  366. init_sec_buffer_desc(&inbuf_desc, inbuf, 4);
  367. sspi_ret = DecryptMessage(&c->ctxt_handle, &inbuf_desc, 0, NULL);
  368. if (sspi_ret == SEC_E_OK || sspi_ret == SEC_I_RENEGOTIATE ||
  369. sspi_ret == SEC_I_CONTEXT_EXPIRED) {
  370. /* handle decrypted data */
  371. if (inbuf[1].BufferType == SECBUFFER_DATA) {
  372. /* grow buffer if needed */
  373. size = inbuf[1].cbBuffer > SCHANNEL_FREE_BUFFER_SIZE ?
  374. inbuf[1].cbBuffer : SCHANNEL_FREE_BUFFER_SIZE;
  375. if (c->dec_buf_size - c->dec_buf_offset < size || c->dec_buf_size < len) {
  376. c->dec_buf_size = c->dec_buf_offset + size;
  377. if (c->dec_buf_size < len)
  378. c->dec_buf_size = len;
  379. ret = av_reallocp(&c->dec_buf, c->dec_buf_size);
  380. if (ret < 0) {
  381. c->dec_buf_size = c->dec_buf_offset = 0;
  382. return ret;
  383. }
  384. }
  385. /* copy decrypted data to buffer */
  386. size = inbuf[1].cbBuffer;
  387. if (size) {
  388. memcpy(c->dec_buf + c->dec_buf_offset, inbuf[1].pvBuffer, size);
  389. c->dec_buf_offset += size;
  390. }
  391. }
  392. if (inbuf[3].BufferType == SECBUFFER_EXTRA && inbuf[3].cbBuffer > 0) {
  393. if (c->enc_buf_offset > inbuf[3].cbBuffer) {
  394. memmove(c->enc_buf, (c->enc_buf + c->enc_buf_offset) - inbuf[3].cbBuffer,
  395. inbuf[3].cbBuffer);
  396. c->enc_buf_offset = inbuf[3].cbBuffer;
  397. }
  398. } else
  399. c->enc_buf_offset = 0;
  400. if (sspi_ret == SEC_I_RENEGOTIATE) {
  401. if (c->enc_buf_offset) {
  402. av_log(h, AV_LOG_ERROR, "Cannot renegotiate, encrypted data buffer not empty\n");
  403. ret = AVERROR_UNKNOWN;
  404. goto cleanup;
  405. }
  406. av_log(h, AV_LOG_VERBOSE, "Re-negotiating security context\n");
  407. ret = tls_client_handshake_loop(h, 0);
  408. if (ret < 0) {
  409. goto cleanup;
  410. }
  411. sspi_ret = SEC_E_OK;
  412. continue;
  413. } else if (sspi_ret == SEC_I_CONTEXT_EXPIRED) {
  414. c->sspi_close_notify = 1;
  415. if (!c->connection_closed) {
  416. c->connection_closed = 1;
  417. av_log(h, AV_LOG_VERBOSE, "Server closed the connection\n");
  418. }
  419. ret = 0;
  420. goto cleanup;
  421. }
  422. } else if (sspi_ret == SEC_E_INCOMPLETE_MESSAGE) {
  423. ret = AVERROR(EAGAIN);
  424. goto cleanup;
  425. } else {
  426. av_log(h, AV_LOG_ERROR, "Unable to decrypt message (error 0x%x)\n", (unsigned)sspi_ret);
  427. ret = AVERROR(EIO);
  428. goto cleanup;
  429. }
  430. }
  431. ret = 0;
  432. cleanup:
  433. size = FFMIN(len, c->dec_buf_offset);
  434. if (size) {
  435. memcpy(buf, c->dec_buf, size);
  436. memmove(c->dec_buf, c->dec_buf + size, c->dec_buf_offset - size);
  437. c->dec_buf_offset -= size;
  438. return size;
  439. }
  440. if (ret == 0 && !c->connection_closed)
  441. ret = AVERROR(EAGAIN);
  442. return ret < 0 ? ret : AVERROR_EOF;
  443. }
  444. static int tls_write(URLContext *h, const uint8_t *buf, int len)
  445. {
  446. TLSContext *c = h->priv_data;
  447. TLSShared *s = &c->tls_shared;
  448. SECURITY_STATUS sspi_ret;
  449. int ret = 0, data_size;
  450. uint8_t *data = NULL;
  451. SecBuffer outbuf[4];
  452. SecBufferDesc outbuf_desc;
  453. if (c->sizes.cbMaximumMessage == 0) {
  454. sspi_ret = QueryContextAttributes(&c->ctxt_handle, SECPKG_ATTR_STREAM_SIZES, &c->sizes);
  455. if (sspi_ret != SEC_E_OK)
  456. return AVERROR_UNKNOWN;
  457. }
  458. /* limit how much data we can consume */
  459. len = FFMIN(len, c->sizes.cbMaximumMessage);
  460. data_size = c->sizes.cbHeader + len + c->sizes.cbTrailer;
  461. data = av_malloc(data_size);
  462. if (data == NULL)
  463. return AVERROR(ENOMEM);
  464. init_sec_buffer(&outbuf[0], SECBUFFER_STREAM_HEADER,
  465. data, c->sizes.cbHeader);
  466. init_sec_buffer(&outbuf[1], SECBUFFER_DATA,
  467. data + c->sizes.cbHeader, len);
  468. init_sec_buffer(&outbuf[2], SECBUFFER_STREAM_TRAILER,
  469. data + c->sizes.cbHeader + len,
  470. c->sizes.cbTrailer);
  471. init_sec_buffer(&outbuf[3], SECBUFFER_EMPTY, NULL, 0);
  472. init_sec_buffer_desc(&outbuf_desc, outbuf, 4);
  473. memcpy(outbuf[1].pvBuffer, buf, len);
  474. sspi_ret = EncryptMessage(&c->ctxt_handle, 0, &outbuf_desc, 0);
  475. if (sspi_ret == SEC_E_OK) {
  476. len = outbuf[0].cbBuffer + outbuf[1].cbBuffer + outbuf[2].cbBuffer;
  477. ret = ffurl_write(s->tcp, data, len);
  478. if (ret < 0 || ret != len) {
  479. ret = AVERROR(EIO);
  480. av_log(h, AV_LOG_ERROR, "Writing encrypted data to socket failed\n");
  481. goto done;
  482. }
  483. } else {
  484. av_log(h, AV_LOG_ERROR, "Encrypting data failed\n");
  485. if (sspi_ret == SEC_E_INSUFFICIENT_MEMORY)
  486. ret = AVERROR(ENOMEM);
  487. else
  488. ret = AVERROR(EIO);
  489. goto done;
  490. }
  491. done:
  492. av_freep(&data);
  493. return ret < 0 ? ret : outbuf[1].cbBuffer;
  494. }
  495. static int tls_get_file_handle(URLContext *h)
  496. {
  497. TLSContext *c = h->priv_data;
  498. return ffurl_get_file_handle(c->tls_shared.tcp);
  499. }
  500. static int tls_get_short_seek(URLContext *h)
  501. {
  502. TLSContext *s = h->priv_data;
  503. return ffurl_get_short_seek(s->tls_shared.tcp);
  504. }
  505. static const AVOption options[] = {
  506. TLS_COMMON_OPTIONS(TLSContext, tls_shared),
  507. { NULL }
  508. };
  509. static const AVClass tls_class = {
  510. .class_name = "tls",
  511. .item_name = av_default_item_name,
  512. .option = options,
  513. .version = LIBAVUTIL_VERSION_INT,
  514. };
  515. const URLProtocol ff_tls_protocol = {
  516. .name = "tls",
  517. .url_open2 = tls_open,
  518. .url_read = tls_read,
  519. .url_write = tls_write,
  520. .url_close = tls_close,
  521. .url_get_file_handle = tls_get_file_handle,
  522. .url_get_short_seek = tls_get_short_seek,
  523. .priv_data_size = sizeof(TLSContext),
  524. .flags = URL_PROTOCOL_FLAG_NETWORK,
  525. .priv_data_class = &tls_class,
  526. };