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