/src/FreeRDP/winpr/libwinpr/sspi/Schannel/schannel_openssl.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * Schannel Security Package (OpenSSL) |
4 | | * |
5 | | * Copyright 2012-2014 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <winpr/config.h> |
21 | | |
22 | | #include "schannel_openssl.h" |
23 | | |
24 | | #ifdef WITH_OPENSSL |
25 | | |
26 | | #include <winpr/crt.h> |
27 | | #include <winpr/sspi.h> |
28 | | #include <winpr/ssl.h> |
29 | | #include <winpr/print.h> |
30 | | #include <winpr/crypto.h> |
31 | | |
32 | | #include <openssl/ssl.h> |
33 | | #include <openssl/err.h> |
34 | | #include <openssl/bio.h> |
35 | | |
36 | | struct S_SCHANNEL_OPENSSL |
37 | | { |
38 | | SSL* ssl; |
39 | | SSL_CTX* ctx; |
40 | | BOOL connected; |
41 | | BIO* bioRead; |
42 | | BIO* bioWrite; |
43 | | BYTE* ReadBuffer; |
44 | | BYTE* WriteBuffer; |
45 | | }; |
46 | | |
47 | | #include "../../log.h" |
48 | | #define TAG WINPR_TAG("sspi.schannel") |
49 | | |
50 | | static char* openssl_get_ssl_error_string(int ssl_error) |
51 | 0 | { |
52 | 0 | switch (ssl_error) |
53 | 0 | { |
54 | 0 | case SSL_ERROR_ZERO_RETURN: |
55 | 0 | return "SSL_ERROR_ZERO_RETURN"; |
56 | | |
57 | 0 | case SSL_ERROR_WANT_READ: |
58 | 0 | return "SSL_ERROR_WANT_READ"; |
59 | | |
60 | 0 | case SSL_ERROR_WANT_WRITE: |
61 | 0 | return "SSL_ERROR_WANT_WRITE"; |
62 | | |
63 | 0 | case SSL_ERROR_SYSCALL: |
64 | 0 | return "SSL_ERROR_SYSCALL"; |
65 | | |
66 | 0 | case SSL_ERROR_SSL: |
67 | 0 | return "SSL_ERROR_SSL"; |
68 | 0 | } |
69 | | |
70 | 0 | return "SSL_ERROR_UNKNOWN"; |
71 | 0 | } |
72 | | |
73 | | static void schannel_context_cleanup(SCHANNEL_OPENSSL* context) |
74 | 0 | { |
75 | 0 | WINPR_ASSERT(context); |
76 | | |
77 | 0 | free(context->ReadBuffer); |
78 | 0 | context->ReadBuffer = NULL; |
79 | |
|
80 | 0 | if (context->bioWrite) |
81 | 0 | BIO_free_all(context->bioWrite); |
82 | 0 | context->bioWrite = NULL; |
83 | |
|
84 | 0 | if (context->bioRead) |
85 | 0 | BIO_free_all(context->bioRead); |
86 | 0 | context->bioRead = NULL; |
87 | |
|
88 | 0 | if (context->ssl) |
89 | 0 | SSL_free(context->ssl); |
90 | 0 | context->ssl = NULL; |
91 | |
|
92 | 0 | if (context->ctx) |
93 | 0 | SSL_CTX_free(context->ctx); |
94 | 0 | context->ctx = NULL; |
95 | 0 | } |
96 | | |
97 | | static const SSL_METHOD* get_method(BOOL server) |
98 | 0 | { |
99 | 0 | if (server) |
100 | 0 | { |
101 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
102 | | return SSLv23_server_method(); |
103 | | #else |
104 | 0 | return TLS_server_method(); |
105 | 0 | #endif |
106 | 0 | } |
107 | 0 | else |
108 | 0 | { |
109 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
110 | | return SSLv23_client_method(); |
111 | | #else |
112 | 0 | return TLS_client_method(); |
113 | 0 | #endif |
114 | 0 | } |
115 | 0 | } |
116 | | int schannel_openssl_client_init(SCHANNEL_OPENSSL* context) |
117 | 0 | { |
118 | 0 | int status = 0; |
119 | 0 | long options = 0; |
120 | 0 | context->ctx = SSL_CTX_new(get_method(FALSE)); |
121 | |
|
122 | 0 | if (!context->ctx) |
123 | 0 | { |
124 | 0 | WLog_ERR(TAG, "SSL_CTX_new failed"); |
125 | 0 | return -1; |
126 | 0 | } |
127 | | |
128 | | /** |
129 | | * SSL_OP_NO_COMPRESSION: |
130 | | * |
131 | | * The Microsoft RDP server does not advertise support |
132 | | * for TLS compression, but alternative servers may support it. |
133 | | * This was observed between early versions of the FreeRDP server |
134 | | * and the FreeRDP client, and caused major performance issues, |
135 | | * which is why we're disabling it. |
136 | | */ |
137 | 0 | #ifdef SSL_OP_NO_COMPRESSION |
138 | 0 | options |= SSL_OP_NO_COMPRESSION; |
139 | 0 | #endif |
140 | | /** |
141 | | * SSL_OP_TLS_BLOCK_PADDING_BUG: |
142 | | * |
143 | | * The Microsoft RDP server does *not* support TLS padding. |
144 | | * It absolutely needs to be disabled otherwise it won't work. |
145 | | */ |
146 | 0 | options |= SSL_OP_TLS_BLOCK_PADDING_BUG; |
147 | | /** |
148 | | * SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS: |
149 | | * |
150 | | * Just like TLS padding, the Microsoft RDP server does not |
151 | | * support empty fragments. This needs to be disabled. |
152 | | */ |
153 | 0 | options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; |
154 | 0 | SSL_CTX_set_options(context->ctx, options); |
155 | 0 | context->ssl = SSL_new(context->ctx); |
156 | |
|
157 | 0 | if (!context->ssl) |
158 | 0 | { |
159 | 0 | WLog_ERR(TAG, "SSL_new failed"); |
160 | 0 | goto fail; |
161 | 0 | } |
162 | | |
163 | 0 | context->bioRead = BIO_new(BIO_s_mem()); |
164 | |
|
165 | 0 | if (!context->bioRead) |
166 | 0 | { |
167 | 0 | WLog_ERR(TAG, "BIO_new failed"); |
168 | 0 | goto fail; |
169 | 0 | } |
170 | | |
171 | 0 | status = BIO_set_write_buf_size(context->bioRead, SCHANNEL_CB_MAX_TOKEN); |
172 | |
|
173 | 0 | if (status != 1) |
174 | 0 | { |
175 | 0 | WLog_ERR(TAG, "BIO_set_write_buf_size on bioRead failed"); |
176 | 0 | goto fail; |
177 | 0 | } |
178 | | |
179 | 0 | context->bioWrite = BIO_new(BIO_s_mem()); |
180 | |
|
181 | 0 | if (!context->bioWrite) |
182 | 0 | { |
183 | 0 | WLog_ERR(TAG, "BIO_new failed"); |
184 | 0 | goto fail; |
185 | 0 | } |
186 | | |
187 | 0 | status = BIO_set_write_buf_size(context->bioWrite, SCHANNEL_CB_MAX_TOKEN); |
188 | |
|
189 | 0 | if (status != 1) |
190 | 0 | { |
191 | 0 | WLog_ERR(TAG, "BIO_set_write_buf_size on bioWrite failed"); |
192 | 0 | goto fail; |
193 | 0 | } |
194 | | |
195 | 0 | status = BIO_make_bio_pair(context->bioRead, context->bioWrite); |
196 | |
|
197 | 0 | if (status != 1) |
198 | 0 | { |
199 | 0 | WLog_ERR(TAG, "BIO_make_bio_pair failed"); |
200 | 0 | goto fail; |
201 | 0 | } |
202 | | |
203 | 0 | SSL_set_bio(context->ssl, context->bioRead, context->bioWrite); |
204 | 0 | context->ReadBuffer = (BYTE*)malloc(SCHANNEL_CB_MAX_TOKEN); |
205 | |
|
206 | 0 | if (!context->ReadBuffer) |
207 | 0 | { |
208 | 0 | WLog_ERR(TAG, "Failed to allocate ReadBuffer"); |
209 | 0 | goto fail; |
210 | 0 | } |
211 | | |
212 | 0 | context->WriteBuffer = (BYTE*)malloc(SCHANNEL_CB_MAX_TOKEN); |
213 | |
|
214 | 0 | if (!context->WriteBuffer) |
215 | 0 | { |
216 | 0 | WLog_ERR(TAG, "Failed to allocate ReadBuffer"); |
217 | 0 | goto fail; |
218 | 0 | } |
219 | | |
220 | 0 | return 0; |
221 | 0 | fail: |
222 | 0 | schannel_context_cleanup(context); |
223 | 0 | return -1; |
224 | 0 | } |
225 | | |
226 | | int schannel_openssl_server_init(SCHANNEL_OPENSSL* context) |
227 | 0 | { |
228 | 0 | int status = 0; |
229 | 0 | unsigned long options = 0; |
230 | |
|
231 | 0 | context->ctx = SSL_CTX_new(get_method(TRUE)); |
232 | |
|
233 | 0 | if (!context->ctx) |
234 | 0 | { |
235 | 0 | WLog_ERR(TAG, "SSL_CTX_new failed"); |
236 | 0 | return -1; |
237 | 0 | } |
238 | | |
239 | | /* |
240 | | * SSL_OP_NO_SSLv2: |
241 | | * |
242 | | * We only want SSLv3 and TLSv1, so disable SSLv2. |
243 | | * SSLv3 is used by, eg. Microsoft RDC for Mac OS X. |
244 | | */ |
245 | 0 | options |= SSL_OP_NO_SSLv2; |
246 | | /** |
247 | | * SSL_OP_NO_COMPRESSION: |
248 | | * |
249 | | * The Microsoft RDP server does not advertise support |
250 | | * for TLS compression, but alternative servers may support it. |
251 | | * This was observed between early versions of the FreeRDP server |
252 | | * and the FreeRDP client, and caused major performance issues, |
253 | | * which is why we're disabling it. |
254 | | */ |
255 | 0 | #ifdef SSL_OP_NO_COMPRESSION |
256 | 0 | options |= SSL_OP_NO_COMPRESSION; |
257 | 0 | #endif |
258 | | /** |
259 | | * SSL_OP_TLS_BLOCK_PADDING_BUG: |
260 | | * |
261 | | * The Microsoft RDP server does *not* support TLS padding. |
262 | | * It absolutely needs to be disabled otherwise it won't work. |
263 | | */ |
264 | 0 | options |= SSL_OP_TLS_BLOCK_PADDING_BUG; |
265 | | /** |
266 | | * SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS: |
267 | | * |
268 | | * Just like TLS padding, the Microsoft RDP server does not |
269 | | * support empty fragments. This needs to be disabled. |
270 | | */ |
271 | 0 | options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; |
272 | 0 | SSL_CTX_set_options(context->ctx, options); |
273 | |
|
274 | | #if defined(WITH_DEBUG_SCHANNEL) |
275 | | if (SSL_CTX_use_RSAPrivateKey_file(context->ctx, "/tmp/localhost.key", SSL_FILETYPE_PEM) <= 0) |
276 | | { |
277 | | WLog_ERR(TAG, "SSL_CTX_use_RSAPrivateKey_file failed"); |
278 | | goto fail; |
279 | | } |
280 | | #endif |
281 | |
|
282 | 0 | context->ssl = SSL_new(context->ctx); |
283 | |
|
284 | 0 | if (!context->ssl) |
285 | 0 | { |
286 | 0 | WLog_ERR(TAG, "SSL_new failed"); |
287 | 0 | goto fail; |
288 | 0 | } |
289 | | |
290 | 0 | if (SSL_use_certificate_file(context->ssl, "/tmp/localhost.crt", SSL_FILETYPE_PEM) <= 0) |
291 | 0 | { |
292 | 0 | WLog_ERR(TAG, "SSL_use_certificate_file failed"); |
293 | 0 | goto fail; |
294 | 0 | } |
295 | | |
296 | 0 | context->bioRead = BIO_new(BIO_s_mem()); |
297 | |
|
298 | 0 | if (!context->bioRead) |
299 | 0 | { |
300 | 0 | WLog_ERR(TAG, "BIO_new failed"); |
301 | 0 | goto fail; |
302 | 0 | } |
303 | | |
304 | 0 | status = BIO_set_write_buf_size(context->bioRead, SCHANNEL_CB_MAX_TOKEN); |
305 | |
|
306 | 0 | if (status != 1) |
307 | 0 | { |
308 | 0 | WLog_ERR(TAG, "BIO_set_write_buf_size failed for bioRead"); |
309 | 0 | goto fail; |
310 | 0 | } |
311 | | |
312 | 0 | context->bioWrite = BIO_new(BIO_s_mem()); |
313 | |
|
314 | 0 | if (!context->bioWrite) |
315 | 0 | { |
316 | 0 | WLog_ERR(TAG, "BIO_new failed"); |
317 | 0 | goto fail; |
318 | 0 | } |
319 | | |
320 | 0 | status = BIO_set_write_buf_size(context->bioWrite, SCHANNEL_CB_MAX_TOKEN); |
321 | |
|
322 | 0 | if (status != 1) |
323 | 0 | { |
324 | 0 | WLog_ERR(TAG, "BIO_set_write_buf_size failed for bioWrite"); |
325 | 0 | goto fail; |
326 | 0 | } |
327 | | |
328 | 0 | status = BIO_make_bio_pair(context->bioRead, context->bioWrite); |
329 | |
|
330 | 0 | if (status != 1) |
331 | 0 | { |
332 | 0 | WLog_ERR(TAG, "BIO_make_bio_pair failed"); |
333 | 0 | goto fail; |
334 | 0 | } |
335 | | |
336 | 0 | SSL_set_bio(context->ssl, context->bioRead, context->bioWrite); |
337 | 0 | context->ReadBuffer = (BYTE*)malloc(SCHANNEL_CB_MAX_TOKEN); |
338 | |
|
339 | 0 | if (!context->ReadBuffer) |
340 | 0 | { |
341 | 0 | WLog_ERR(TAG, "Failed to allocate memory for ReadBuffer"); |
342 | 0 | goto fail; |
343 | 0 | } |
344 | | |
345 | 0 | context->WriteBuffer = (BYTE*)malloc(SCHANNEL_CB_MAX_TOKEN); |
346 | |
|
347 | 0 | if (!context->WriteBuffer) |
348 | 0 | { |
349 | 0 | WLog_ERR(TAG, "Failed to allocate memory for WriteBuffer"); |
350 | 0 | goto fail; |
351 | 0 | } |
352 | | |
353 | 0 | return 0; |
354 | 0 | fail: |
355 | 0 | schannel_context_cleanup(context); |
356 | 0 | return -1; |
357 | 0 | } |
358 | | |
359 | | SECURITY_STATUS schannel_openssl_client_process_tokens(SCHANNEL_OPENSSL* context, |
360 | | PSecBufferDesc pInput, |
361 | | PSecBufferDesc pOutput) |
362 | 0 | { |
363 | 0 | int status = 0; |
364 | 0 | int ssl_error = 0; |
365 | 0 | PSecBuffer pBuffer = NULL; |
366 | |
|
367 | 0 | if (!context->connected) |
368 | 0 | { |
369 | 0 | if (pInput) |
370 | 0 | { |
371 | 0 | if (pInput->cBuffers < 1) |
372 | 0 | return SEC_E_INVALID_TOKEN; |
373 | | |
374 | 0 | pBuffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN); |
375 | |
|
376 | 0 | if (!pBuffer) |
377 | 0 | return SEC_E_INVALID_TOKEN; |
378 | | |
379 | 0 | ERR_clear_error(); |
380 | 0 | status = BIO_write(context->bioRead, pBuffer->pvBuffer, pBuffer->cbBuffer); |
381 | 0 | if (status < 0) |
382 | 0 | return SEC_E_INVALID_TOKEN; |
383 | 0 | } |
384 | | |
385 | 0 | status = SSL_connect(context->ssl); |
386 | |
|
387 | 0 | if (status < 0) |
388 | 0 | { |
389 | 0 | ssl_error = SSL_get_error(context->ssl, status); |
390 | 0 | WLog_ERR(TAG, "SSL_connect error: %s", openssl_get_ssl_error_string(ssl_error)); |
391 | 0 | } |
392 | |
|
393 | 0 | if (status == 1) |
394 | 0 | context->connected = TRUE; |
395 | |
|
396 | 0 | ERR_clear_error(); |
397 | 0 | status = BIO_read(context->bioWrite, context->ReadBuffer, SCHANNEL_CB_MAX_TOKEN); |
398 | |
|
399 | 0 | if (pOutput->cBuffers < 1) |
400 | 0 | return SEC_E_INVALID_TOKEN; |
401 | | |
402 | 0 | pBuffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN); |
403 | |
|
404 | 0 | if (!pBuffer) |
405 | 0 | return SEC_E_INVALID_TOKEN; |
406 | | |
407 | 0 | if (status > 0) |
408 | 0 | { |
409 | 0 | if (pBuffer->cbBuffer < (unsigned long)status) |
410 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
411 | | |
412 | 0 | CopyMemory(pBuffer->pvBuffer, context->ReadBuffer, status); |
413 | 0 | pBuffer->cbBuffer = status; |
414 | 0 | return (context->connected) ? SEC_E_OK : SEC_I_CONTINUE_NEEDED; |
415 | 0 | } |
416 | 0 | else |
417 | 0 | { |
418 | 0 | pBuffer->cbBuffer = 0; |
419 | 0 | return (context->connected) ? SEC_E_OK : SEC_I_CONTINUE_NEEDED; |
420 | 0 | } |
421 | 0 | } |
422 | | |
423 | 0 | return SEC_E_OK; |
424 | 0 | } |
425 | | |
426 | | SECURITY_STATUS schannel_openssl_server_process_tokens(SCHANNEL_OPENSSL* context, |
427 | | PSecBufferDesc pInput, |
428 | | PSecBufferDesc pOutput) |
429 | 0 | { |
430 | 0 | int status = 0; |
431 | 0 | int ssl_error = 0; |
432 | 0 | PSecBuffer pBuffer = NULL; |
433 | |
|
434 | 0 | if (!context->connected) |
435 | 0 | { |
436 | 0 | if (pInput->cBuffers < 1) |
437 | 0 | return SEC_E_INVALID_TOKEN; |
438 | | |
439 | 0 | pBuffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN); |
440 | |
|
441 | 0 | if (!pBuffer) |
442 | 0 | return SEC_E_INVALID_TOKEN; |
443 | | |
444 | 0 | ERR_clear_error(); |
445 | 0 | status = BIO_write(context->bioRead, pBuffer->pvBuffer, pBuffer->cbBuffer); |
446 | 0 | if (status >= 0) |
447 | 0 | status = SSL_accept(context->ssl); |
448 | |
|
449 | 0 | if (status < 0) |
450 | 0 | { |
451 | 0 | ssl_error = SSL_get_error(context->ssl, status); |
452 | 0 | WLog_ERR(TAG, "SSL_accept error: %s", openssl_get_ssl_error_string(ssl_error)); |
453 | 0 | return SEC_E_INVALID_TOKEN; |
454 | 0 | } |
455 | | |
456 | 0 | if (status == 1) |
457 | 0 | context->connected = TRUE; |
458 | |
|
459 | 0 | ERR_clear_error(); |
460 | 0 | status = BIO_read(context->bioWrite, context->ReadBuffer, SCHANNEL_CB_MAX_TOKEN); |
461 | 0 | if (status < 0) |
462 | 0 | { |
463 | 0 | ssl_error = SSL_get_error(context->ssl, status); |
464 | 0 | WLog_ERR(TAG, "BIO_read: %s", openssl_get_ssl_error_string(ssl_error)); |
465 | 0 | return SEC_E_INVALID_TOKEN; |
466 | 0 | } |
467 | | |
468 | 0 | if (pOutput->cBuffers < 1) |
469 | 0 | return SEC_E_INVALID_TOKEN; |
470 | | |
471 | 0 | pBuffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN); |
472 | |
|
473 | 0 | if (!pBuffer) |
474 | 0 | return SEC_E_INVALID_TOKEN; |
475 | | |
476 | 0 | if (status > 0) |
477 | 0 | { |
478 | 0 | if (pBuffer->cbBuffer < (unsigned long)status) |
479 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
480 | | |
481 | 0 | CopyMemory(pBuffer->pvBuffer, context->ReadBuffer, status); |
482 | 0 | pBuffer->cbBuffer = status; |
483 | 0 | return (context->connected) ? SEC_E_OK : SEC_I_CONTINUE_NEEDED; |
484 | 0 | } |
485 | 0 | else |
486 | 0 | { |
487 | 0 | pBuffer->cbBuffer = 0; |
488 | 0 | return (context->connected) ? SEC_E_OK : SEC_I_CONTINUE_NEEDED; |
489 | 0 | } |
490 | 0 | } |
491 | | |
492 | 0 | return SEC_E_OK; |
493 | 0 | } |
494 | | |
495 | | SECURITY_STATUS schannel_openssl_encrypt_message(SCHANNEL_OPENSSL* context, PSecBufferDesc pMessage) |
496 | 0 | { |
497 | 0 | int status = 0; |
498 | 0 | int ssl_error = 0; |
499 | 0 | PSecBuffer pStreamBodyBuffer = NULL; |
500 | 0 | PSecBuffer pStreamHeaderBuffer = NULL; |
501 | 0 | PSecBuffer pStreamTrailerBuffer = NULL; |
502 | 0 | pStreamHeaderBuffer = sspi_FindSecBuffer(pMessage, SECBUFFER_STREAM_HEADER); |
503 | 0 | pStreamBodyBuffer = sspi_FindSecBuffer(pMessage, SECBUFFER_DATA); |
504 | 0 | pStreamTrailerBuffer = sspi_FindSecBuffer(pMessage, SECBUFFER_STREAM_TRAILER); |
505 | |
|
506 | 0 | if ((!pStreamHeaderBuffer) || (!pStreamBodyBuffer) || (!pStreamTrailerBuffer)) |
507 | 0 | return SEC_E_INVALID_TOKEN; |
508 | | |
509 | 0 | status = SSL_write(context->ssl, pStreamBodyBuffer->pvBuffer, pStreamBodyBuffer->cbBuffer); |
510 | |
|
511 | 0 | if (status < 0) |
512 | 0 | { |
513 | 0 | ssl_error = SSL_get_error(context->ssl, status); |
514 | 0 | WLog_ERR(TAG, "SSL_write: %s", openssl_get_ssl_error_string(ssl_error)); |
515 | 0 | } |
516 | |
|
517 | 0 | ERR_clear_error(); |
518 | 0 | status = BIO_read(context->bioWrite, context->ReadBuffer, SCHANNEL_CB_MAX_TOKEN); |
519 | |
|
520 | 0 | if (status > 0) |
521 | 0 | { |
522 | 0 | size_t ustatus = (size_t)status; |
523 | 0 | size_t length = 0; |
524 | 0 | size_t offset = 0; |
525 | |
|
526 | 0 | length = |
527 | 0 | (pStreamHeaderBuffer->cbBuffer > ustatus) ? ustatus : pStreamHeaderBuffer->cbBuffer; |
528 | 0 | CopyMemory(pStreamHeaderBuffer->pvBuffer, &context->ReadBuffer[offset], length); |
529 | 0 | ustatus -= length; |
530 | 0 | offset += length; |
531 | 0 | length = (pStreamBodyBuffer->cbBuffer > ustatus) ? ustatus : pStreamBodyBuffer->cbBuffer; |
532 | 0 | CopyMemory(pStreamBodyBuffer->pvBuffer, &context->ReadBuffer[offset], length); |
533 | 0 | ustatus -= length; |
534 | 0 | offset += length; |
535 | 0 | length = |
536 | 0 | (pStreamTrailerBuffer->cbBuffer > ustatus) ? ustatus : pStreamTrailerBuffer->cbBuffer; |
537 | 0 | CopyMemory(pStreamTrailerBuffer->pvBuffer, &context->ReadBuffer[offset], length); |
538 | 0 | } |
539 | |
|
540 | 0 | return SEC_E_OK; |
541 | 0 | } |
542 | | |
543 | | SECURITY_STATUS schannel_openssl_decrypt_message(SCHANNEL_OPENSSL* context, PSecBufferDesc pMessage) |
544 | 0 | { |
545 | 0 | int status = 0; |
546 | 0 | int length = 0; |
547 | 0 | BYTE* buffer = NULL; |
548 | 0 | int ssl_error = 0; |
549 | 0 | PSecBuffer pBuffer = NULL; |
550 | 0 | pBuffer = sspi_FindSecBuffer(pMessage, SECBUFFER_DATA); |
551 | |
|
552 | 0 | if (!pBuffer) |
553 | 0 | return SEC_E_INVALID_TOKEN; |
554 | | |
555 | 0 | ERR_clear_error(); |
556 | 0 | status = BIO_write(context->bioRead, pBuffer->pvBuffer, pBuffer->cbBuffer); |
557 | 0 | if (status > 0) |
558 | 0 | status = SSL_read(context->ssl, pBuffer->pvBuffer, pBuffer->cbBuffer); |
559 | |
|
560 | 0 | if (status < 0) |
561 | 0 | { |
562 | 0 | ssl_error = SSL_get_error(context->ssl, status); |
563 | 0 | WLog_ERR(TAG, "SSL_read: %s", openssl_get_ssl_error_string(ssl_error)); |
564 | 0 | } |
565 | |
|
566 | 0 | length = status; |
567 | 0 | buffer = pBuffer->pvBuffer; |
568 | 0 | pMessage->pBuffers[0].BufferType = SECBUFFER_STREAM_HEADER; |
569 | 0 | pMessage->pBuffers[0].cbBuffer = 5; |
570 | 0 | pMessage->pBuffers[1].BufferType = SECBUFFER_DATA; |
571 | 0 | pMessage->pBuffers[1].pvBuffer = buffer; |
572 | 0 | pMessage->pBuffers[1].cbBuffer = length; |
573 | 0 | pMessage->pBuffers[2].BufferType = SECBUFFER_STREAM_TRAILER; |
574 | 0 | pMessage->pBuffers[2].cbBuffer = 36; |
575 | 0 | pMessage->pBuffers[3].BufferType = SECBUFFER_EMPTY; |
576 | 0 | pMessage->pBuffers[3].cbBuffer = 0; |
577 | 0 | return SEC_E_OK; |
578 | 0 | } |
579 | | |
580 | | SCHANNEL_OPENSSL* schannel_openssl_new(void) |
581 | 0 | { |
582 | 0 | SCHANNEL_OPENSSL* context = NULL; |
583 | 0 | context = (SCHANNEL_OPENSSL*)calloc(1, sizeof(SCHANNEL_OPENSSL)); |
584 | |
|
585 | 0 | if (context != NULL) |
586 | 0 | { |
587 | 0 | winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT); |
588 | 0 | context->connected = FALSE; |
589 | 0 | } |
590 | |
|
591 | 0 | return context; |
592 | 0 | } |
593 | | |
594 | | void schannel_openssl_free(SCHANNEL_OPENSSL* context) |
595 | 0 | { |
596 | 0 | if (context) |
597 | 0 | { |
598 | 0 | free(context->ReadBuffer); |
599 | 0 | free(context->WriteBuffer); |
600 | 0 | free(context); |
601 | 0 | } |
602 | 0 | } |
603 | | |
604 | | #else |
605 | | |
606 | | int schannel_openssl_client_init(SCHANNEL_OPENSSL* context) |
607 | | { |
608 | | return 0; |
609 | | } |
610 | | |
611 | | int schannel_openssl_server_init(SCHANNEL_OPENSSL* context) |
612 | | { |
613 | | return 0; |
614 | | } |
615 | | |
616 | | SECURITY_STATUS schannel_openssl_client_process_tokens(SCHANNEL_OPENSSL* context, |
617 | | PSecBufferDesc pInput, |
618 | | PSecBufferDesc pOutput) |
619 | | { |
620 | | return SEC_E_OK; |
621 | | } |
622 | | |
623 | | SECURITY_STATUS schannel_openssl_server_process_tokens(SCHANNEL_OPENSSL* context, |
624 | | PSecBufferDesc pInput, |
625 | | PSecBufferDesc pOutput) |
626 | | { |
627 | | return SEC_E_OK; |
628 | | } |
629 | | |
630 | | SECURITY_STATUS schannel_openssl_encrypt_message(SCHANNEL_OPENSSL* context, PSecBufferDesc pMessage) |
631 | | { |
632 | | return SEC_E_OK; |
633 | | } |
634 | | |
635 | | SECURITY_STATUS schannel_openssl_decrypt_message(SCHANNEL_OPENSSL* context, PSecBufferDesc pMessage) |
636 | | { |
637 | | return SEC_E_OK; |
638 | | } |
639 | | |
640 | | SCHANNEL_OPENSSL* schannel_openssl_new(void) |
641 | | { |
642 | | return NULL; |
643 | | } |
644 | | |
645 | | void schannel_openssl_free(SCHANNEL_OPENSSL* context) |
646 | | { |
647 | | } |
648 | | |
649 | | #endif |