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.

602 lines
19KB

  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. if (c->tls_shared.tcp)
  114. ffurl_close(c->tls_shared.tcp);
  115. return 0;
  116. }
  117. static int tls_client_handshake_loop(URLContext *h, int initial)
  118. {
  119. TLSContext *c = h->priv_data;
  120. TLSShared *s = &c->tls_shared;
  121. SECURITY_STATUS sspi_ret;
  122. SecBuffer outbuf[3];
  123. SecBufferDesc outbuf_desc;
  124. SecBuffer inbuf[2];
  125. SecBufferDesc inbuf_desc;
  126. int i, ret = 0, read_data = initial;
  127. if (c->enc_buf == NULL) {
  128. c->enc_buf_offset = 0;
  129. ret = av_reallocp(&c->enc_buf, SCHANNEL_INITIAL_BUFFER_SIZE);
  130. if (ret < 0)
  131. goto fail;
  132. c->enc_buf_size = SCHANNEL_INITIAL_BUFFER_SIZE;
  133. }
  134. if (c->dec_buf == NULL) {
  135. c->dec_buf_offset = 0;
  136. ret = av_reallocp(&c->dec_buf, SCHANNEL_INITIAL_BUFFER_SIZE);
  137. if (ret < 0)
  138. goto fail;
  139. c->dec_buf_size = SCHANNEL_INITIAL_BUFFER_SIZE;
  140. }
  141. while (1) {
  142. if (c->enc_buf_size - c->enc_buf_offset < SCHANNEL_FREE_BUFFER_SIZE) {
  143. c->enc_buf_size = c->enc_buf_offset + SCHANNEL_FREE_BUFFER_SIZE;
  144. ret = av_reallocp(&c->enc_buf, c->enc_buf_size);
  145. if (ret < 0) {
  146. c->enc_buf_size = c->enc_buf_offset = 0;
  147. goto fail;
  148. }
  149. }
  150. if (read_data) {
  151. ret = ffurl_read(c->tls_shared.tcp, c->enc_buf + c->enc_buf_offset,
  152. c->enc_buf_size - c->enc_buf_offset);
  153. if (ret < 0) {
  154. av_log(h, AV_LOG_ERROR, "Failed to read handshake response\n");
  155. goto fail;
  156. }
  157. c->enc_buf_offset += ret;
  158. }
  159. /* input buffers */
  160. init_sec_buffer(&inbuf[0], SECBUFFER_TOKEN, av_malloc(c->enc_buf_offset), c->enc_buf_offset);
  161. init_sec_buffer(&inbuf[1], SECBUFFER_EMPTY, NULL, 0);
  162. init_sec_buffer_desc(&inbuf_desc, inbuf, 2);
  163. if (inbuf[0].pvBuffer == NULL) {
  164. av_log(h, AV_LOG_ERROR, "Failed to allocate input buffer\n");
  165. ret = AVERROR(ENOMEM);
  166. goto fail;
  167. }
  168. memcpy(inbuf[0].pvBuffer, c->enc_buf, c->enc_buf_offset);
  169. /* output buffers */
  170. init_sec_buffer(&outbuf[0], SECBUFFER_TOKEN, NULL, 0);
  171. init_sec_buffer(&outbuf[1], SECBUFFER_ALERT, NULL, 0);
  172. init_sec_buffer(&outbuf[2], SECBUFFER_EMPTY, NULL, 0);
  173. init_sec_buffer_desc(&outbuf_desc, outbuf, 3);
  174. sspi_ret = InitializeSecurityContext(&c->cred_handle, &c->ctxt_handle, s->host, c->request_flags,
  175. 0, 0, &inbuf_desc, 0, NULL, &outbuf_desc, &c->context_flags,
  176. &c->ctxt_timestamp);
  177. av_freep(&inbuf[0].pvBuffer);
  178. if (sspi_ret == SEC_E_INCOMPLETE_MESSAGE) {
  179. av_log(h, AV_LOG_DEBUG, "Received incomplete handshake, need more data\n");
  180. read_data = 1;
  181. continue;
  182. }
  183. /* remote requests a client certificate - attempt to continue without one anyway */
  184. if (sspi_ret == SEC_I_INCOMPLETE_CREDENTIALS &&
  185. !(c->request_flags & ISC_REQ_USE_SUPPLIED_CREDS)) {
  186. av_log(h, AV_LOG_VERBOSE, "Client certificate has been requested, ignoring\n");
  187. c->request_flags |= ISC_REQ_USE_SUPPLIED_CREDS;
  188. read_data = 0;
  189. continue;
  190. }
  191. /* continue handshake */
  192. if (sspi_ret == SEC_I_CONTINUE_NEEDED || sspi_ret == SEC_E_OK) {
  193. for (i = 0; i < 3; i++) {
  194. if (outbuf[i].BufferType == SECBUFFER_TOKEN && outbuf[i].cbBuffer > 0) {
  195. ret = ffurl_write(c->tls_shared.tcp, outbuf[i].pvBuffer, outbuf[i].cbBuffer);
  196. if (ret < 0 || ret != outbuf[i].cbBuffer) {
  197. av_log(h, AV_LOG_VERBOSE, "Failed to send handshake data\n");
  198. ret = AVERROR(EIO);
  199. goto fail;
  200. }
  201. }
  202. if (outbuf[i].pvBuffer != NULL) {
  203. FreeContextBuffer(outbuf[i].pvBuffer);
  204. outbuf[i].pvBuffer = NULL;
  205. }
  206. }
  207. } else {
  208. if (sspi_ret == SEC_E_WRONG_PRINCIPAL)
  209. av_log(h, AV_LOG_ERROR, "SNI or certificate check failed\n");
  210. else
  211. av_log(h, AV_LOG_ERROR, "Creating security context failed (0x%lx)\n", sspi_ret);
  212. ret = AVERROR_UNKNOWN;
  213. goto fail;
  214. }
  215. if (inbuf[1].BufferType == SECBUFFER_EXTRA && inbuf[1].cbBuffer > 0) {
  216. if (c->enc_buf_offset > inbuf[1].cbBuffer) {
  217. memmove(c->enc_buf, (c->enc_buf + c->enc_buf_offset) - inbuf[1].cbBuffer,
  218. inbuf[1].cbBuffer);
  219. c->enc_buf_offset = inbuf[1].cbBuffer;
  220. if (sspi_ret == SEC_I_CONTINUE_NEEDED) {
  221. read_data = 0;
  222. continue;
  223. }
  224. }
  225. } else {
  226. c->enc_buf_offset = 0;
  227. }
  228. if (sspi_ret == SEC_I_CONTINUE_NEEDED) {
  229. read_data = 1;
  230. continue;
  231. }
  232. break;
  233. }
  234. return 0;
  235. fail:
  236. /* free any remaining output data */
  237. for (i = 0; i < 3; i++) {
  238. if (outbuf[i].pvBuffer != NULL) {
  239. FreeContextBuffer(outbuf[i].pvBuffer);
  240. outbuf[i].pvBuffer = NULL;
  241. }
  242. }
  243. return ret;
  244. }
  245. static int tls_client_handshake(URLContext *h)
  246. {
  247. TLSContext *c = h->priv_data;
  248. TLSShared *s = &c->tls_shared;
  249. SecBuffer outbuf;
  250. SecBufferDesc outbuf_desc;
  251. SECURITY_STATUS sspi_ret;
  252. int ret;
  253. init_sec_buffer(&outbuf, SECBUFFER_EMPTY, NULL, 0);
  254. init_sec_buffer_desc(&outbuf_desc, &outbuf, 1);
  255. c->request_flags = ISC_REQ_SEQUENCE_DETECT | ISC_REQ_REPLAY_DETECT |
  256. ISC_REQ_CONFIDENTIALITY | ISC_REQ_ALLOCATE_MEMORY |
  257. ISC_REQ_STREAM;
  258. sspi_ret = InitializeSecurityContext(&c->cred_handle, NULL, s->host, c->request_flags, 0, 0,
  259. NULL, 0, &c->ctxt_handle, &outbuf_desc, &c->context_flags,
  260. &c->ctxt_timestamp);
  261. if (sspi_ret != SEC_I_CONTINUE_NEEDED) {
  262. av_log(h, AV_LOG_ERROR, "Unable to create initial security context (0x%lx)\n", sspi_ret);
  263. ret = AVERROR_UNKNOWN;
  264. goto fail;
  265. }
  266. ret = ffurl_write(s->tcp, outbuf.pvBuffer, outbuf.cbBuffer);
  267. FreeContextBuffer(outbuf.pvBuffer);
  268. if (ret < 0 || ret != outbuf.cbBuffer) {
  269. av_log(h, AV_LOG_ERROR, "Failed to send initial handshake data\n");
  270. ret = AVERROR(EIO);
  271. goto fail;
  272. }
  273. return tls_client_handshake_loop(h, 1);
  274. fail:
  275. DeleteSecurityContext(&c->ctxt_handle);
  276. return ret;
  277. }
  278. static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
  279. {
  280. TLSContext *c = h->priv_data;
  281. TLSShared *s = &c->tls_shared;
  282. SECURITY_STATUS sspi_ret;
  283. SCHANNEL_CRED schannel_cred = { 0 };
  284. int ret;
  285. if ((ret = ff_tls_open_underlying(s, h, uri, options)) < 0)
  286. goto fail;
  287. if (s->listen) {
  288. av_log(h, AV_LOG_ERROR, "TLS Listen Sockets with SChannel is not implemented.\n");
  289. ret = AVERROR(EINVAL);
  290. goto fail;
  291. }
  292. /* SChannel Options */
  293. schannel_cred.dwVersion = SCHANNEL_CRED_VERSION;
  294. if (s->verify)
  295. schannel_cred.dwFlags = SCH_CRED_AUTO_CRED_VALIDATION |
  296. SCH_CRED_REVOCATION_CHECK_CHAIN;
  297. else
  298. schannel_cred.dwFlags = SCH_CRED_MANUAL_CRED_VALIDATION |
  299. SCH_CRED_IGNORE_NO_REVOCATION_CHECK |
  300. SCH_CRED_IGNORE_REVOCATION_OFFLINE;
  301. /* Get credential handle */
  302. sspi_ret = AcquireCredentialsHandle(NULL, (TCHAR *)UNISP_NAME, SECPKG_CRED_OUTBOUND,
  303. NULL, &schannel_cred, NULL, NULL, &c->cred_handle,
  304. &c->cred_timestamp);
  305. if (sspi_ret != SEC_E_OK) {
  306. av_log(h, AV_LOG_ERROR, "Unable to acquire security credentials (0x%lx)\n", sspi_ret);
  307. ret = AVERROR_UNKNOWN;
  308. goto fail;
  309. }
  310. ret = tls_client_handshake(h);
  311. if (ret < 0)
  312. goto fail;
  313. c->connected = 1;
  314. return 0;
  315. fail:
  316. tls_close(h);
  317. return ret;
  318. }
  319. static int tls_read(URLContext *h, uint8_t *buf, int len)
  320. {
  321. TLSContext *c = h->priv_data;
  322. TLSShared *s = &c->tls_shared;
  323. SECURITY_STATUS sspi_ret = SEC_E_OK;
  324. SecBuffer inbuf[4];
  325. SecBufferDesc inbuf_desc;
  326. int size, ret;
  327. int min_enc_buf_size = len + SCHANNEL_FREE_BUFFER_SIZE;
  328. if (len <= c->dec_buf_offset)
  329. goto cleanup;
  330. if (c->sspi_close_notify)
  331. goto cleanup;
  332. if (!c->connection_closed) {
  333. size = c->enc_buf_size - c->enc_buf_offset;
  334. if (size < SCHANNEL_FREE_BUFFER_SIZE || c->enc_buf_size < min_enc_buf_size) {
  335. c->enc_buf_size = c->enc_buf_offset + SCHANNEL_FREE_BUFFER_SIZE;
  336. if (c->enc_buf_size < min_enc_buf_size)
  337. c->enc_buf_size = min_enc_buf_size;
  338. ret = av_reallocp(&c->enc_buf, c->enc_buf_size);
  339. if (ret < 0) {
  340. c->enc_buf_size = c->enc_buf_offset = 0;
  341. return ret;
  342. }
  343. }
  344. ret = ffurl_read(s->tcp, c->enc_buf + c->enc_buf_offset,
  345. c->enc_buf_size - c->enc_buf_offset);
  346. if (ret < 0) {
  347. av_log(h, AV_LOG_ERROR, "Unable to read from socket\n");
  348. return ret;
  349. } else if (ret == 0)
  350. c->connection_closed = 1;
  351. c->enc_buf_offset += ret;
  352. }
  353. while (c->enc_buf_offset > 0 && sspi_ret == SEC_E_OK && c->dec_buf_offset < len) {
  354. /* input buffer */
  355. init_sec_buffer(&inbuf[0], SECBUFFER_DATA, c->enc_buf, c->enc_buf_offset);
  356. /* additional buffers for possible output */
  357. init_sec_buffer(&inbuf[1], SECBUFFER_EMPTY, NULL, 0);
  358. init_sec_buffer(&inbuf[2], SECBUFFER_EMPTY, NULL, 0);
  359. init_sec_buffer(&inbuf[3], SECBUFFER_EMPTY, NULL, 0);
  360. init_sec_buffer_desc(&inbuf_desc, inbuf, 4);
  361. sspi_ret = DecryptMessage(&c->ctxt_handle, &inbuf_desc, 0, NULL);
  362. if (sspi_ret == SEC_E_OK || sspi_ret == SEC_I_RENEGOTIATE ||
  363. sspi_ret == SEC_I_CONTEXT_EXPIRED) {
  364. /* handle decrypted data */
  365. if (inbuf[1].BufferType == SECBUFFER_DATA) {
  366. /* grow buffer if needed */
  367. size = inbuf[1].cbBuffer > SCHANNEL_FREE_BUFFER_SIZE ?
  368. inbuf[1].cbBuffer : SCHANNEL_FREE_BUFFER_SIZE;
  369. if (c->dec_buf_size - c->dec_buf_offset < size || c->dec_buf_size < len) {
  370. c->dec_buf_size = c->dec_buf_offset + size;
  371. if (c->dec_buf_size < len)
  372. c->dec_buf_size = len;
  373. ret = av_reallocp(&c->dec_buf, c->dec_buf_size);
  374. if (ret < 0) {
  375. c->dec_buf_size = c->dec_buf_offset = 0;
  376. return ret;
  377. }
  378. }
  379. /* copy decrypted data to buffer */
  380. size = inbuf[1].cbBuffer;
  381. if (size) {
  382. memcpy(c->dec_buf + c->dec_buf_offset, inbuf[1].pvBuffer, size);
  383. c->dec_buf_offset += size;
  384. }
  385. }
  386. if (inbuf[3].BufferType == SECBUFFER_EXTRA && inbuf[3].cbBuffer > 0) {
  387. if (c->enc_buf_offset > inbuf[3].cbBuffer) {
  388. memmove(c->enc_buf, (c->enc_buf + c->enc_buf_offset) - inbuf[3].cbBuffer,
  389. inbuf[3].cbBuffer);
  390. c->enc_buf_offset = inbuf[3].cbBuffer;
  391. }
  392. } else
  393. c->enc_buf_offset = 0;
  394. if (sspi_ret == SEC_I_RENEGOTIATE) {
  395. if (c->enc_buf_offset) {
  396. av_log(h, AV_LOG_ERROR, "Cannot renegotiate, encrypted data buffer not empty\n");
  397. ret = AVERROR_UNKNOWN;
  398. goto cleanup;
  399. }
  400. av_log(h, AV_LOG_VERBOSE, "Re-negotiating security context\n");
  401. ret = tls_client_handshake_loop(h, 0);
  402. if (ret < 0) {
  403. goto cleanup;
  404. }
  405. sspi_ret = SEC_E_OK;
  406. continue;
  407. } else if (sspi_ret == SEC_I_CONTEXT_EXPIRED) {
  408. c->sspi_close_notify = 1;
  409. if (!c->connection_closed) {
  410. c->connection_closed = 1;
  411. av_log(h, AV_LOG_VERBOSE, "Server closed the connection\n");
  412. }
  413. ret = 0;
  414. goto cleanup;
  415. }
  416. } else if (sspi_ret == SEC_E_INCOMPLETE_MESSAGE) {
  417. ret = AVERROR(EAGAIN);
  418. goto cleanup;
  419. } else {
  420. av_log(h, AV_LOG_ERROR, "Unable to decrypt message\n");
  421. ret = AVERROR(EIO);
  422. goto cleanup;
  423. }
  424. }
  425. ret = 0;
  426. cleanup:
  427. size = FFMIN(len, c->dec_buf_offset);
  428. if (size) {
  429. memcpy(buf, c->dec_buf, size);
  430. memmove(c->dec_buf, c->dec_buf + size, c->dec_buf_offset - size);
  431. c->dec_buf_offset -= size;
  432. return size;
  433. }
  434. if (ret == 0 && !c->connection_closed)
  435. ret = AVERROR(EAGAIN);
  436. return ret < 0 ? ret : 0;
  437. }
  438. static int tls_write(URLContext *h, const uint8_t *buf, int len)
  439. {
  440. TLSContext *c = h->priv_data;
  441. TLSShared *s = &c->tls_shared;
  442. SECURITY_STATUS sspi_ret;
  443. int ret = 0, data_size;
  444. uint8_t *data = NULL;
  445. SecBuffer outbuf[4];
  446. SecBufferDesc outbuf_desc;
  447. if (c->sizes.cbMaximumMessage == 0) {
  448. sspi_ret = QueryContextAttributes(&c->ctxt_handle, SECPKG_ATTR_STREAM_SIZES, &c->sizes);
  449. if (sspi_ret != SEC_E_OK)
  450. return AVERROR_UNKNOWN;
  451. }
  452. /* limit how much data we can consume */
  453. len = FFMIN(len, c->sizes.cbMaximumMessage);
  454. data_size = c->sizes.cbHeader + len + c->sizes.cbTrailer;
  455. data = av_malloc(data_size);
  456. if (data == NULL)
  457. return AVERROR(ENOMEM);
  458. init_sec_buffer(&outbuf[0], SECBUFFER_STREAM_HEADER,
  459. data, c->sizes.cbHeader);
  460. init_sec_buffer(&outbuf[1], SECBUFFER_DATA,
  461. data + c->sizes.cbHeader, len);
  462. init_sec_buffer(&outbuf[2], SECBUFFER_STREAM_TRAILER,
  463. data + c->sizes.cbHeader + len,
  464. c->sizes.cbTrailer);
  465. init_sec_buffer(&outbuf[3], SECBUFFER_EMPTY, NULL, 0);
  466. init_sec_buffer_desc(&outbuf_desc, outbuf, 4);
  467. memcpy(outbuf[1].pvBuffer, buf, len);
  468. sspi_ret = EncryptMessage(&c->ctxt_handle, 0, &outbuf_desc, 0);
  469. if (sspi_ret == SEC_E_OK) {
  470. len = outbuf[0].cbBuffer + outbuf[1].cbBuffer + outbuf[2].cbBuffer;
  471. ret = ffurl_write(s->tcp, data, len);
  472. if (ret < 0 || ret != len) {
  473. ret = AVERROR(EIO);
  474. av_log(h, AV_LOG_ERROR, "Writing encrypted data to socket failed\n");
  475. goto done;
  476. }
  477. } else {
  478. av_log(h, AV_LOG_ERROR, "Encrypting data failed\n");
  479. if (sspi_ret == SEC_E_INSUFFICIENT_MEMORY)
  480. ret = AVERROR(ENOMEM);
  481. else
  482. ret = AVERROR(EIO);
  483. goto done;
  484. }
  485. done:
  486. av_freep(&data);
  487. return ret < 0 ? ret : outbuf[1].cbBuffer;
  488. }
  489. static const AVOption options[] = {
  490. TLS_COMMON_OPTIONS(TLSContext, tls_shared),
  491. { NULL }
  492. };
  493. static const AVClass tls_class = {
  494. .class_name = "tls",
  495. .item_name = av_default_item_name,
  496. .option = options,
  497. .version = LIBAVUTIL_VERSION_INT,
  498. };
  499. const URLProtocol ff_tls_schannel_protocol = {
  500. .name = "tls",
  501. .url_open2 = tls_open,
  502. .url_read = tls_read,
  503. .url_write = tls_write,
  504. .url_close = tls_close,
  505. .priv_data_size = sizeof(TLSContext),
  506. .flags = URL_PROTOCOL_FLAG_NETWORK,
  507. .priv_data_class = &tls_class,
  508. };