blob: 065dccb020a3512c68eff8be00f3221653e23fec
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 | |
21 | /** Based on the CURL SChannel module */ |
22 | |
23 | #include "avformat.h" |
24 | #include "internal.h" |
25 | #include "network.h" |
26 | #include "os_support.h" |
27 | #include "url.h" |
28 | #include "tls.h" |
29 | |
30 | #define SECURITY_WIN32 |
31 | #include <windows.h> |
32 | #include <security.h> |
33 | #include <schnlsp.h> |
34 | |
35 | #define SCHANNEL_INITIAL_BUFFER_SIZE 4096 |
36 | #define SCHANNEL_FREE_BUFFER_SIZE 1024 |
37 | |
38 | /* mingw does not define this symbol */ |
39 | #ifndef SECBUFFER_ALERT |
40 | #define SECBUFFER_ALERT 17 |
41 | #endif |
42 | |
43 | typedef struct TLSContext { |
44 | const AVClass *class; |
45 | TLSShared tls_shared; |
46 | |
47 | CredHandle cred_handle; |
48 | TimeStamp cred_timestamp; |
49 | |
50 | CtxtHandle ctxt_handle; |
51 | TimeStamp ctxt_timestamp; |
52 | |
53 | ULONG request_flags; |
54 | ULONG context_flags; |
55 | |
56 | uint8_t *enc_buf; |
57 | int enc_buf_size; |
58 | int enc_buf_offset; |
59 | |
60 | uint8_t *dec_buf; |
61 | int dec_buf_size; |
62 | int dec_buf_offset; |
63 | |
64 | SecPkgContext_StreamSizes sizes; |
65 | |
66 | int connected; |
67 | int connection_closed; |
68 | int sspi_close_notify; |
69 | } TLSContext; |
70 | |
71 | static void init_sec_buffer(SecBuffer *buffer, unsigned long type, |
72 | void *data, unsigned long size) |
73 | { |
74 | buffer->cbBuffer = size; |
75 | buffer->BufferType = type; |
76 | buffer->pvBuffer = data; |
77 | } |
78 | |
79 | static void init_sec_buffer_desc(SecBufferDesc *desc, SecBuffer *buffers, |
80 | unsigned long buffer_count) |
81 | { |
82 | desc->ulVersion = SECBUFFER_VERSION; |
83 | desc->pBuffers = buffers; |
84 | desc->cBuffers = buffer_count; |
85 | } |
86 | |
87 | static int tls_shutdown_client(URLContext *h) |
88 | { |
89 | TLSContext *c = h->priv_data; |
90 | TLSShared *s = &c->tls_shared; |
91 | int ret; |
92 | |
93 | if (c->connected) { |
94 | SecBufferDesc BuffDesc; |
95 | SecBuffer Buffer; |
96 | SECURITY_STATUS sspi_ret; |
97 | SecBuffer outbuf; |
98 | SecBufferDesc outbuf_desc; |
99 | |
100 | DWORD dwshut = SCHANNEL_SHUTDOWN; |
101 | init_sec_buffer(&Buffer, SECBUFFER_TOKEN, &dwshut, sizeof(dwshut)); |
102 | init_sec_buffer_desc(&BuffDesc, &Buffer, 1); |
103 | |
104 | sspi_ret = ApplyControlToken(&c->ctxt_handle, &BuffDesc); |
105 | if (sspi_ret != SEC_E_OK) |
106 | av_log(h, AV_LOG_ERROR, "ApplyControlToken failed\n"); |
107 | |
108 | init_sec_buffer(&outbuf, SECBUFFER_EMPTY, NULL, 0); |
109 | init_sec_buffer_desc(&outbuf_desc, &outbuf, 1); |
110 | |
111 | sspi_ret = InitializeSecurityContext(&c->cred_handle, &c->ctxt_handle, s->host, |
112 | c->request_flags, 0, 0, NULL, 0, &c->ctxt_handle, |
113 | &outbuf_desc, &c->context_flags, &c->ctxt_timestamp); |
114 | if (sspi_ret == SEC_E_OK || sspi_ret == SEC_I_CONTEXT_EXPIRED) { |
115 | ret = ffurl_write(s->tcp, outbuf.pvBuffer, outbuf.cbBuffer); |
116 | FreeContextBuffer(outbuf.pvBuffer); |
117 | if (ret < 0 || ret != outbuf.cbBuffer) |
118 | av_log(h, AV_LOG_ERROR, "Failed to send close message\n"); |
119 | } |
120 | |
121 | c->connected = 0; |
122 | } |
123 | return 0; |
124 | } |
125 | |
126 | static int tls_close(URLContext *h) |
127 | { |
128 | TLSContext *c = h->priv_data; |
129 | |
130 | tls_shutdown_client(h); |
131 | |
132 | DeleteSecurityContext(&c->ctxt_handle); |
133 | FreeCredentialsHandle(&c->cred_handle); |
134 | |
135 | av_freep(&c->enc_buf); |
136 | c->enc_buf_size = c->enc_buf_offset = 0; |
137 | |
138 | av_freep(&c->dec_buf); |
139 | c->dec_buf_size = c->dec_buf_offset = 0; |
140 | |
141 | if (c->tls_shared.tcp) |
142 | ffurl_close(c->tls_shared.tcp); |
143 | return 0; |
144 | } |
145 | |
146 | static int tls_client_handshake_loop(URLContext *h, int initial) |
147 | { |
148 | TLSContext *c = h->priv_data; |
149 | TLSShared *s = &c->tls_shared; |
150 | SECURITY_STATUS sspi_ret; |
151 | SecBuffer outbuf[3]; |
152 | SecBufferDesc outbuf_desc; |
153 | SecBuffer inbuf[2]; |
154 | SecBufferDesc inbuf_desc; |
155 | int i, ret = 0, read_data = initial; |
156 | |
157 | if (c->enc_buf == NULL) { |
158 | c->enc_buf_offset = 0; |
159 | ret = av_reallocp(&c->enc_buf, SCHANNEL_INITIAL_BUFFER_SIZE); |
160 | if (ret < 0) |
161 | goto fail; |
162 | c->enc_buf_size = SCHANNEL_INITIAL_BUFFER_SIZE; |
163 | } |
164 | |
165 | if (c->dec_buf == NULL) { |
166 | c->dec_buf_offset = 0; |
167 | ret = av_reallocp(&c->dec_buf, SCHANNEL_INITIAL_BUFFER_SIZE); |
168 | if (ret < 0) |
169 | goto fail; |
170 | c->dec_buf_size = SCHANNEL_INITIAL_BUFFER_SIZE; |
171 | } |
172 | |
173 | while (1) { |
174 | if (c->enc_buf_size - c->enc_buf_offset < SCHANNEL_FREE_BUFFER_SIZE) { |
175 | c->enc_buf_size = c->enc_buf_offset + SCHANNEL_FREE_BUFFER_SIZE; |
176 | ret = av_reallocp(&c->enc_buf, c->enc_buf_size); |
177 | if (ret < 0) { |
178 | c->enc_buf_size = c->enc_buf_offset = 0; |
179 | goto fail; |
180 | } |
181 | } |
182 | |
183 | if (read_data) { |
184 | ret = ffurl_read(c->tls_shared.tcp, c->enc_buf + c->enc_buf_offset, |
185 | c->enc_buf_size - c->enc_buf_offset); |
186 | if (ret < 0) { |
187 | av_log(h, AV_LOG_ERROR, "Failed to read handshake response\n"); |
188 | goto fail; |
189 | } |
190 | c->enc_buf_offset += ret; |
191 | } |
192 | |
193 | /* input buffers */ |
194 | init_sec_buffer(&inbuf[0], SECBUFFER_TOKEN, av_malloc(c->enc_buf_offset), c->enc_buf_offset); |
195 | init_sec_buffer(&inbuf[1], SECBUFFER_EMPTY, NULL, 0); |
196 | init_sec_buffer_desc(&inbuf_desc, inbuf, 2); |
197 | |
198 | if (inbuf[0].pvBuffer == NULL) { |
199 | av_log(h, AV_LOG_ERROR, "Failed to allocate input buffer\n"); |
200 | ret = AVERROR(ENOMEM); |
201 | goto fail; |
202 | } |
203 | |
204 | memcpy(inbuf[0].pvBuffer, c->enc_buf, c->enc_buf_offset); |
205 | |
206 | /* output buffers */ |
207 | init_sec_buffer(&outbuf[0], SECBUFFER_TOKEN, NULL, 0); |
208 | init_sec_buffer(&outbuf[1], SECBUFFER_ALERT, NULL, 0); |
209 | init_sec_buffer(&outbuf[2], SECBUFFER_EMPTY, NULL, 0); |
210 | init_sec_buffer_desc(&outbuf_desc, outbuf, 3); |
211 | |
212 | sspi_ret = InitializeSecurityContext(&c->cred_handle, &c->ctxt_handle, s->host, c->request_flags, |
213 | 0, 0, &inbuf_desc, 0, NULL, &outbuf_desc, &c->context_flags, |
214 | &c->ctxt_timestamp); |
215 | av_freep(&inbuf[0].pvBuffer); |
216 | |
217 | if (sspi_ret == SEC_E_INCOMPLETE_MESSAGE) { |
218 | av_log(h, AV_LOG_DEBUG, "Received incomplete handshake, need more data\n"); |
219 | read_data = 1; |
220 | continue; |
221 | } |
222 | |
223 | /* remote requests a client certificate - attempt to continue without one anyway */ |
224 | if (sspi_ret == SEC_I_INCOMPLETE_CREDENTIALS && |
225 | !(c->request_flags & ISC_REQ_USE_SUPPLIED_CREDS)) { |
226 | av_log(h, AV_LOG_VERBOSE, "Client certificate has been requested, ignoring\n"); |
227 | c->request_flags |= ISC_REQ_USE_SUPPLIED_CREDS; |
228 | read_data = 0; |
229 | continue; |
230 | } |
231 | |
232 | /* continue handshake */ |
233 | if (sspi_ret == SEC_I_CONTINUE_NEEDED || sspi_ret == SEC_E_OK) { |
234 | for (i = 0; i < 3; i++) { |
235 | if (outbuf[i].BufferType == SECBUFFER_TOKEN && outbuf[i].cbBuffer > 0) { |
236 | ret = ffurl_write(c->tls_shared.tcp, outbuf[i].pvBuffer, outbuf[i].cbBuffer); |
237 | if (ret < 0 || ret != outbuf[i].cbBuffer) { |
238 | av_log(h, AV_LOG_VERBOSE, "Failed to send handshake data\n"); |
239 | ret = AVERROR(EIO); |
240 | goto fail; |
241 | } |
242 | } |
243 | |
244 | if (outbuf[i].pvBuffer != NULL) { |
245 | FreeContextBuffer(outbuf[i].pvBuffer); |
246 | outbuf[i].pvBuffer = NULL; |
247 | } |
248 | } |
249 | } else { |
250 | if (sspi_ret == SEC_E_WRONG_PRINCIPAL) |
251 | av_log(h, AV_LOG_ERROR, "SNI or certificate check failed\n"); |
252 | else |
253 | av_log(h, AV_LOG_ERROR, "Creating security context failed (0x%lx)\n", sspi_ret); |
254 | ret = AVERROR_UNKNOWN; |
255 | goto fail; |
256 | } |
257 | |
258 | if (inbuf[1].BufferType == SECBUFFER_EXTRA && inbuf[1].cbBuffer > 0) { |
259 | if (c->enc_buf_offset > inbuf[1].cbBuffer) { |
260 | memmove(c->enc_buf, (c->enc_buf + c->enc_buf_offset) - inbuf[1].cbBuffer, |
261 | inbuf[1].cbBuffer); |
262 | c->enc_buf_offset = inbuf[1].cbBuffer; |
263 | if (sspi_ret == SEC_I_CONTINUE_NEEDED) { |
264 | read_data = 0; |
265 | continue; |
266 | } |
267 | } |
268 | } else { |
269 | c->enc_buf_offset = 0; |
270 | } |
271 | |
272 | if (sspi_ret == SEC_I_CONTINUE_NEEDED) { |
273 | read_data = 1; |
274 | continue; |
275 | } |
276 | |
277 | break; |
278 | } |
279 | |
280 | return 0; |
281 | |
282 | fail: |
283 | /* free any remaining output data */ |
284 | for (i = 0; i < 3; i++) { |
285 | if (outbuf[i].pvBuffer != NULL) { |
286 | FreeContextBuffer(outbuf[i].pvBuffer); |
287 | outbuf[i].pvBuffer = NULL; |
288 | } |
289 | } |
290 | |
291 | return ret; |
292 | } |
293 | |
294 | static int tls_client_handshake(URLContext *h) |
295 | { |
296 | TLSContext *c = h->priv_data; |
297 | TLSShared *s = &c->tls_shared; |
298 | SecBuffer outbuf; |
299 | SecBufferDesc outbuf_desc; |
300 | SECURITY_STATUS sspi_ret; |
301 | int ret; |
302 | |
303 | init_sec_buffer(&outbuf, SECBUFFER_EMPTY, NULL, 0); |
304 | init_sec_buffer_desc(&outbuf_desc, &outbuf, 1); |
305 | |
306 | c->request_flags = ISC_REQ_SEQUENCE_DETECT | ISC_REQ_REPLAY_DETECT | |
307 | ISC_REQ_CONFIDENTIALITY | ISC_REQ_ALLOCATE_MEMORY | |
308 | ISC_REQ_STREAM; |
309 | |
310 | sspi_ret = InitializeSecurityContext(&c->cred_handle, NULL, s->host, c->request_flags, 0, 0, |
311 | NULL, 0, &c->ctxt_handle, &outbuf_desc, &c->context_flags, |
312 | &c->ctxt_timestamp); |
313 | if (sspi_ret != SEC_I_CONTINUE_NEEDED) { |
314 | av_log(h, AV_LOG_ERROR, "Unable to create initial security context (0x%lx)\n", sspi_ret); |
315 | ret = AVERROR_UNKNOWN; |
316 | goto fail; |
317 | } |
318 | |
319 | ret = ffurl_write(s->tcp, outbuf.pvBuffer, outbuf.cbBuffer); |
320 | FreeContextBuffer(outbuf.pvBuffer); |
321 | if (ret < 0 || ret != outbuf.cbBuffer) { |
322 | av_log(h, AV_LOG_ERROR, "Failed to send initial handshake data\n"); |
323 | ret = AVERROR(EIO); |
324 | goto fail; |
325 | } |
326 | |
327 | return tls_client_handshake_loop(h, 1); |
328 | |
329 | fail: |
330 | DeleteSecurityContext(&c->ctxt_handle); |
331 | return ret; |
332 | } |
333 | |
334 | static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options) |
335 | { |
336 | TLSContext *c = h->priv_data; |
337 | TLSShared *s = &c->tls_shared; |
338 | SECURITY_STATUS sspi_ret; |
339 | SCHANNEL_CRED schannel_cred = { 0 }; |
340 | int ret; |
341 | |
342 | if ((ret = ff_tls_open_underlying(s, h, uri, options)) < 0) |
343 | goto fail; |
344 | |
345 | if (s->listen) { |
346 | av_log(h, AV_LOG_ERROR, "TLS Listen Sockets with SChannel is not implemented.\n"); |
347 | ret = AVERROR(EINVAL); |
348 | goto fail; |
349 | } |
350 | |
351 | /* SChannel Options */ |
352 | schannel_cred.dwVersion = SCHANNEL_CRED_VERSION; |
353 | |
354 | if (s->verify) |
355 | schannel_cred.dwFlags = SCH_CRED_AUTO_CRED_VALIDATION | |
356 | SCH_CRED_REVOCATION_CHECK_CHAIN; |
357 | else |
358 | schannel_cred.dwFlags = SCH_CRED_MANUAL_CRED_VALIDATION | |
359 | SCH_CRED_IGNORE_NO_REVOCATION_CHECK | |
360 | SCH_CRED_IGNORE_REVOCATION_OFFLINE; |
361 | |
362 | /* Get credential handle */ |
363 | sspi_ret = AcquireCredentialsHandle(NULL, (TCHAR *)UNISP_NAME, SECPKG_CRED_OUTBOUND, |
364 | NULL, &schannel_cred, NULL, NULL, &c->cred_handle, |
365 | &c->cred_timestamp); |
366 | if (sspi_ret != SEC_E_OK) { |
367 | av_log(h, AV_LOG_ERROR, "Unable to acquire security credentials (0x%lx)\n", sspi_ret); |
368 | ret = AVERROR_UNKNOWN; |
369 | goto fail; |
370 | } |
371 | |
372 | ret = tls_client_handshake(h); |
373 | if (ret < 0) |
374 | goto fail; |
375 | |
376 | c->connected = 1; |
377 | |
378 | return 0; |
379 | |
380 | fail: |
381 | tls_close(h); |
382 | return ret; |
383 | } |
384 | |
385 | static int tls_read(URLContext *h, uint8_t *buf, int len) |
386 | { |
387 | TLSContext *c = h->priv_data; |
388 | TLSShared *s = &c->tls_shared; |
389 | SECURITY_STATUS sspi_ret = SEC_E_OK; |
390 | SecBuffer inbuf[4]; |
391 | SecBufferDesc inbuf_desc; |
392 | int size, ret; |
393 | int min_enc_buf_size = len + SCHANNEL_FREE_BUFFER_SIZE; |
394 | |
395 | if (len <= c->dec_buf_offset) |
396 | goto cleanup; |
397 | |
398 | if (c->sspi_close_notify) |
399 | goto cleanup; |
400 | |
401 | if (!c->connection_closed) { |
402 | size = c->enc_buf_size - c->enc_buf_offset; |
403 | if (size < SCHANNEL_FREE_BUFFER_SIZE || c->enc_buf_size < min_enc_buf_size) { |
404 | c->enc_buf_size = c->enc_buf_offset + SCHANNEL_FREE_BUFFER_SIZE; |
405 | if (c->enc_buf_size < min_enc_buf_size) |
406 | c->enc_buf_size = min_enc_buf_size; |
407 | ret = av_reallocp(&c->enc_buf, c->enc_buf_size); |
408 | if (ret < 0) { |
409 | c->enc_buf_size = c->enc_buf_offset = 0; |
410 | return ret; |
411 | } |
412 | } |
413 | |
414 | ret = ffurl_read(s->tcp, c->enc_buf + c->enc_buf_offset, |
415 | c->enc_buf_size - c->enc_buf_offset); |
416 | if (ret < 0) { |
417 | av_log(h, AV_LOG_ERROR, "Unable to read from socket\n"); |
418 | return ret; |
419 | } else if (ret == 0) |
420 | c->connection_closed = 1; |
421 | |
422 | c->enc_buf_offset += ret; |
423 | } |
424 | |
425 | while (c->enc_buf_offset > 0 && sspi_ret == SEC_E_OK && c->dec_buf_offset < len) { |
426 | /* input buffer */ |
427 | init_sec_buffer(&inbuf[0], SECBUFFER_DATA, c->enc_buf, c->enc_buf_offset); |
428 | |
429 | /* additional buffers for possible output */ |
430 | init_sec_buffer(&inbuf[1], SECBUFFER_EMPTY, NULL, 0); |
431 | init_sec_buffer(&inbuf[2], SECBUFFER_EMPTY, NULL, 0); |
432 | init_sec_buffer(&inbuf[3], SECBUFFER_EMPTY, NULL, 0); |
433 | init_sec_buffer_desc(&inbuf_desc, inbuf, 4); |
434 | |
435 | sspi_ret = DecryptMessage(&c->ctxt_handle, &inbuf_desc, 0, NULL); |
436 | if (sspi_ret == SEC_E_OK || sspi_ret == SEC_I_RENEGOTIATE || |
437 | sspi_ret == SEC_I_CONTEXT_EXPIRED) { |
438 | /* handle decrypted data */ |
439 | if (inbuf[1].BufferType == SECBUFFER_DATA) { |
440 | /* grow buffer if needed */ |
441 | size = inbuf[1].cbBuffer > SCHANNEL_FREE_BUFFER_SIZE ? |
442 | inbuf[1].cbBuffer : SCHANNEL_FREE_BUFFER_SIZE; |
443 | if (c->dec_buf_size - c->dec_buf_offset < size || c->dec_buf_size < len) { |
444 | c->dec_buf_size = c->dec_buf_offset + size; |
445 | if (c->dec_buf_size < len) |
446 | c->dec_buf_size = len; |
447 | ret = av_reallocp(&c->dec_buf, c->dec_buf_size); |
448 | if (ret < 0) { |
449 | c->dec_buf_size = c->dec_buf_offset = 0; |
450 | return ret; |
451 | } |
452 | } |
453 | |
454 | /* copy decrypted data to buffer */ |
455 | size = inbuf[1].cbBuffer; |
456 | if (size) { |
457 | memcpy(c->dec_buf + c->dec_buf_offset, inbuf[1].pvBuffer, size); |
458 | c->dec_buf_offset += size; |
459 | } |
460 | } |
461 | if (inbuf[3].BufferType == SECBUFFER_EXTRA && inbuf[3].cbBuffer > 0) { |
462 | if (c->enc_buf_offset > inbuf[3].cbBuffer) { |
463 | memmove(c->enc_buf, (c->enc_buf + c->enc_buf_offset) - inbuf[3].cbBuffer, |
464 | inbuf[3].cbBuffer); |
465 | c->enc_buf_offset = inbuf[3].cbBuffer; |
466 | } |
467 | } else |
468 | c->enc_buf_offset = 0; |
469 | |
470 | if (sspi_ret == SEC_I_RENEGOTIATE) { |
471 | if (c->enc_buf_offset) { |
472 | av_log(h, AV_LOG_ERROR, "Cannot renegotiate, encrypted data buffer not empty\n"); |
473 | ret = AVERROR_UNKNOWN; |
474 | goto cleanup; |
475 | } |
476 | |
477 | av_log(h, AV_LOG_VERBOSE, "Re-negotiating security context\n"); |
478 | ret = tls_client_handshake_loop(h, 0); |
479 | if (ret < 0) { |
480 | goto cleanup; |
481 | } |
482 | sspi_ret = SEC_E_OK; |
483 | continue; |
484 | } else if (sspi_ret == SEC_I_CONTEXT_EXPIRED) { |
485 | c->sspi_close_notify = 1; |
486 | if (!c->connection_closed) { |
487 | c->connection_closed = 1; |
488 | av_log(h, AV_LOG_VERBOSE, "Server closed the connection\n"); |
489 | } |
490 | ret = 0; |
491 | goto cleanup; |
492 | } |
493 | } else if (sspi_ret == SEC_E_INCOMPLETE_MESSAGE) { |
494 | ret = AVERROR(EAGAIN); |
495 | goto cleanup; |
496 | } else { |
497 | av_log(h, AV_LOG_ERROR, "Unable to decrypt message\n"); |
498 | ret = AVERROR(EIO); |
499 | goto cleanup; |
500 | } |
501 | } |
502 | |
503 | ret = 0; |
504 | |
505 | cleanup: |
506 | size = FFMIN(len, c->dec_buf_offset); |
507 | if (size) { |
508 | memcpy(buf, c->dec_buf, size); |
509 | memmove(c->dec_buf, c->dec_buf + size, c->dec_buf_offset - size); |
510 | c->dec_buf_offset -= size; |
511 | |
512 | return size; |
513 | } |
514 | |
515 | if (ret == 0 && !c->connection_closed) |
516 | ret = AVERROR(EAGAIN); |
517 | |
518 | return ret < 0 ? ret : 0; |
519 | } |
520 | |
521 | static int tls_write(URLContext *h, const uint8_t *buf, int len) |
522 | { |
523 | TLSContext *c = h->priv_data; |
524 | TLSShared *s = &c->tls_shared; |
525 | SECURITY_STATUS sspi_ret; |
526 | int ret = 0, data_size; |
527 | uint8_t *data = NULL; |
528 | SecBuffer outbuf[4]; |
529 | SecBufferDesc outbuf_desc; |
530 | |
531 | if (c->sizes.cbMaximumMessage == 0) { |
532 | sspi_ret = QueryContextAttributes(&c->ctxt_handle, SECPKG_ATTR_STREAM_SIZES, &c->sizes); |
533 | if (sspi_ret != SEC_E_OK) |
534 | return AVERROR_UNKNOWN; |
535 | } |
536 | |
537 | /* limit how much data we can consume */ |
538 | len = FFMIN(len, c->sizes.cbMaximumMessage); |
539 | |
540 | data_size = c->sizes.cbHeader + len + c->sizes.cbTrailer; |
541 | data = av_malloc(data_size); |
542 | if (data == NULL) |
543 | return AVERROR(ENOMEM); |
544 | |
545 | init_sec_buffer(&outbuf[0], SECBUFFER_STREAM_HEADER, |
546 | data, c->sizes.cbHeader); |
547 | init_sec_buffer(&outbuf[1], SECBUFFER_DATA, |
548 | data + c->sizes.cbHeader, len); |
549 | init_sec_buffer(&outbuf[2], SECBUFFER_STREAM_TRAILER, |
550 | data + c->sizes.cbHeader + len, |
551 | c->sizes.cbTrailer); |
552 | init_sec_buffer(&outbuf[3], SECBUFFER_EMPTY, NULL, 0); |
553 | init_sec_buffer_desc(&outbuf_desc, outbuf, 4); |
554 | |
555 | memcpy(outbuf[1].pvBuffer, buf, len); |
556 | |
557 | sspi_ret = EncryptMessage(&c->ctxt_handle, 0, &outbuf_desc, 0); |
558 | if (sspi_ret == SEC_E_OK) { |
559 | len = outbuf[0].cbBuffer + outbuf[1].cbBuffer + outbuf[2].cbBuffer; |
560 | ret = ffurl_write(s->tcp, data, len); |
561 | if (ret < 0 || ret != len) { |
562 | ret = AVERROR(EIO); |
563 | av_log(h, AV_LOG_ERROR, "Writing encrypted data to socket failed\n"); |
564 | goto done; |
565 | } |
566 | } else { |
567 | av_log(h, AV_LOG_ERROR, "Encrypting data failed\n"); |
568 | if (sspi_ret == SEC_E_INSUFFICIENT_MEMORY) |
569 | ret = AVERROR(ENOMEM); |
570 | else |
571 | ret = AVERROR(EIO); |
572 | goto done; |
573 | } |
574 | |
575 | done: |
576 | av_freep(&data); |
577 | return ret < 0 ? ret : outbuf[1].cbBuffer; |
578 | } |
579 | |
580 | static int tls_get_file_handle(URLContext *h) |
581 | { |
582 | TLSContext *c = h->priv_data; |
583 | return ffurl_get_file_handle(c->tls_shared.tcp); |
584 | } |
585 | |
586 | static const AVOption options[] = { |
587 | TLS_COMMON_OPTIONS(TLSContext, tls_shared), |
588 | { NULL } |
589 | }; |
590 | |
591 | static const AVClass tls_class = { |
592 | .class_name = "tls", |
593 | .item_name = av_default_item_name, |
594 | .option = options, |
595 | .version = LIBAVUTIL_VERSION_INT, |
596 | }; |
597 | |
598 | const URLProtocol ff_tls_schannel_protocol = { |
599 | .name = "tls", |
600 | .url_open2 = tls_open, |
601 | .url_read = tls_read, |
602 | .url_write = tls_write, |
603 | .url_close = tls_close, |
604 | .url_get_file_handle = tls_get_file_handle, |
605 | .priv_data_size = sizeof(TLSContext), |
606 | .flags = URL_PROTOCOL_FLAG_NETWORK, |
607 | .priv_data_class = &tls_class, |
608 | }; |
609 |