/src/FreeRDP/libfreerdp/crypto/tls.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Transport Layer Security |
4 | | * |
5 | | * Copyright 2011-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * Copyright 2023 Armin Novak <anovak@thincast.com> |
7 | | * Copyright 2023 Thincast Technologies GmbH |
8 | | * |
9 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | * you may not use this file except in compliance with the License. |
11 | | * You may obtain a copy of the License at |
12 | | * |
13 | | * http://www.apache.org/licenses/LICENSE-2.0 |
14 | | * |
15 | | * Unless required by applicable law or agreed to in writing, software |
16 | | * distributed under the License is distributed on an "AS IS" BASIS, |
17 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | * See the License for the specific language governing permissions and |
19 | | * limitations under the License. |
20 | | */ |
21 | | |
22 | | #include <freerdp/config.h> |
23 | | |
24 | | #include "../core/settings.h" |
25 | | |
26 | | #include <winpr/assert.h> |
27 | | #include <string.h> |
28 | | #include <errno.h> |
29 | | |
30 | | #include <winpr/crt.h> |
31 | | #include <winpr/string.h> |
32 | | #include <winpr/sspi.h> |
33 | | #include <winpr/ssl.h> |
34 | | |
35 | | #include <winpr/stream.h> |
36 | | #include <freerdp/utils/ringbuffer.h> |
37 | | |
38 | | #include <freerdp/crypto/certificate.h> |
39 | | #include <freerdp/crypto/certificate_data.h> |
40 | | |
41 | | #include <freerdp/log.h> |
42 | | #include "../crypto/tls.h" |
43 | | #include "../core/tcp.h" |
44 | | |
45 | | #include "opensslcompat.h" |
46 | | #include "certificate.h" |
47 | | #include "privatekey.h" |
48 | | |
49 | | #ifdef WINPR_HAVE_POLL_H |
50 | | #include <poll.h> |
51 | | #endif |
52 | | |
53 | | #ifdef FREERDP_HAVE_VALGRIND_MEMCHECK_H |
54 | | #include <valgrind/memcheck.h> |
55 | | #endif |
56 | | |
57 | | #define TAG FREERDP_TAG("crypto") |
58 | | |
59 | | /** |
60 | | * Earlier Microsoft iOS RDP clients have sent a null or even double null |
61 | | * terminated hostname in the SNI TLS extension. |
62 | | * If the length indicator does not equal the hostname strlen OpenSSL |
63 | | * will abort (see openssl:ssl/t1_lib.c). |
64 | | * Here is a tcpdump segment of Microsoft Remote Desktop Client Version |
65 | | * 8.1.7 running on an iPhone 4 with iOS 7.1.2 showing the transmitted |
66 | | * SNI hostname TLV blob when connection to server "abcd": |
67 | | * 00 name_type 0x00 (host_name) |
68 | | * 00 06 length_in_bytes 0x0006 |
69 | | * 61 62 63 64 00 00 host_name "abcd\0\0" |
70 | | * |
71 | | * Currently the only (runtime) workaround is setting an openssl tls |
72 | | * extension debug callback that sets the SSL context's servername_done |
73 | | * to 1 which effectively disables the parsing of that extension type. |
74 | | * |
75 | | * Nowadays this workaround is not required anymore but still can be |
76 | | * activated by adding the following define: |
77 | | * |
78 | | * #define MICROSOFT_IOS_SNI_BUG |
79 | | */ |
80 | | |
81 | | typedef struct |
82 | | { |
83 | | SSL* ssl; |
84 | | CRITICAL_SECTION lock; |
85 | | } BIO_RDP_TLS; |
86 | | |
87 | | static int tls_verify_certificate(rdpTls* tls, const rdpCertificate* cert, const char* hostname, |
88 | | UINT16 port); |
89 | | static void tls_print_certificate_name_mismatch_error(const char* hostname, UINT16 port, |
90 | | const char* common_name, char** alt_names, |
91 | | size_t alt_names_count); |
92 | | static void tls_print_new_certificate_warn(rdpCertificateStore* store, const char* hostname, |
93 | | UINT16 port, const char* fingerprint); |
94 | | static void tls_print_certificate_error(rdpCertificateStore* store, rdpCertificateData* stored_data, |
95 | | const char* hostname, UINT16 port, const char* fingerprint); |
96 | | |
97 | | static void free_tls_public_key(rdpTls* tls) |
98 | 0 | { |
99 | 0 | WINPR_ASSERT(tls); |
100 | 0 | free(tls->PublicKey); |
101 | 0 | tls->PublicKey = NULL; |
102 | 0 | tls->PublicKeyLength = 0; |
103 | 0 | } |
104 | | |
105 | | static void free_tls_bindings(rdpTls* tls) |
106 | 0 | { |
107 | 0 | WINPR_ASSERT(tls); |
108 | | |
109 | 0 | if (tls->Bindings) |
110 | 0 | free(tls->Bindings->Bindings); |
111 | |
|
112 | 0 | free(tls->Bindings); |
113 | 0 | tls->Bindings = NULL; |
114 | 0 | } |
115 | | |
116 | | static int bio_rdp_tls_write(BIO* bio, const char* buf, int size) |
117 | 0 | { |
118 | 0 | int error = 0; |
119 | 0 | int status = 0; |
120 | 0 | BIO_RDP_TLS* tls = (BIO_RDP_TLS*)BIO_get_data(bio); |
121 | |
|
122 | 0 | if (!buf || !tls) |
123 | 0 | return 0; |
124 | | |
125 | 0 | BIO_clear_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_READ | BIO_FLAGS_IO_SPECIAL); |
126 | 0 | EnterCriticalSection(&tls->lock); |
127 | 0 | status = SSL_write(tls->ssl, buf, size); |
128 | 0 | error = SSL_get_error(tls->ssl, status); |
129 | 0 | LeaveCriticalSection(&tls->lock); |
130 | |
|
131 | 0 | if (status <= 0) |
132 | 0 | { |
133 | 0 | switch (error) |
134 | 0 | { |
135 | 0 | case SSL_ERROR_NONE: |
136 | 0 | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
137 | 0 | break; |
138 | | |
139 | 0 | case SSL_ERROR_WANT_WRITE: |
140 | 0 | BIO_set_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY); |
141 | 0 | break; |
142 | | |
143 | 0 | case SSL_ERROR_WANT_READ: |
144 | 0 | BIO_set_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY); |
145 | 0 | break; |
146 | | |
147 | 0 | case SSL_ERROR_WANT_X509_LOOKUP: |
148 | 0 | BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL); |
149 | 0 | BIO_set_retry_reason(bio, BIO_RR_SSL_X509_LOOKUP); |
150 | 0 | break; |
151 | | |
152 | 0 | case SSL_ERROR_WANT_CONNECT: |
153 | 0 | BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL); |
154 | 0 | BIO_set_retry_reason(bio, BIO_RR_CONNECT); |
155 | 0 | break; |
156 | | |
157 | 0 | case SSL_ERROR_SYSCALL: |
158 | 0 | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
159 | 0 | break; |
160 | | |
161 | 0 | case SSL_ERROR_SSL: |
162 | 0 | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
163 | 0 | break; |
164 | 0 | } |
165 | 0 | } |
166 | | |
167 | 0 | return status; |
168 | 0 | } |
169 | | |
170 | | static int bio_rdp_tls_read(BIO* bio, char* buf, int size) |
171 | 0 | { |
172 | 0 | int error = 0; |
173 | 0 | int status = 0; |
174 | 0 | BIO_RDP_TLS* tls = (BIO_RDP_TLS*)BIO_get_data(bio); |
175 | |
|
176 | 0 | if (!buf || !tls) |
177 | 0 | return 0; |
178 | | |
179 | 0 | BIO_clear_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_READ | BIO_FLAGS_IO_SPECIAL); |
180 | 0 | EnterCriticalSection(&tls->lock); |
181 | 0 | status = SSL_read(tls->ssl, buf, size); |
182 | 0 | error = SSL_get_error(tls->ssl, status); |
183 | 0 | LeaveCriticalSection(&tls->lock); |
184 | |
|
185 | 0 | if (status <= 0) |
186 | 0 | { |
187 | 0 | switch (error) |
188 | 0 | { |
189 | 0 | case SSL_ERROR_NONE: |
190 | 0 | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
191 | 0 | break; |
192 | | |
193 | 0 | case SSL_ERROR_WANT_READ: |
194 | 0 | BIO_set_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY); |
195 | 0 | break; |
196 | | |
197 | 0 | case SSL_ERROR_WANT_WRITE: |
198 | 0 | BIO_set_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY); |
199 | 0 | break; |
200 | | |
201 | 0 | case SSL_ERROR_WANT_X509_LOOKUP: |
202 | 0 | BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL); |
203 | 0 | BIO_set_retry_reason(bio, BIO_RR_SSL_X509_LOOKUP); |
204 | 0 | break; |
205 | | |
206 | 0 | case SSL_ERROR_WANT_ACCEPT: |
207 | 0 | BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL); |
208 | 0 | BIO_set_retry_reason(bio, BIO_RR_ACCEPT); |
209 | 0 | break; |
210 | | |
211 | 0 | case SSL_ERROR_WANT_CONNECT: |
212 | 0 | BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL); |
213 | 0 | BIO_set_retry_reason(bio, BIO_RR_CONNECT); |
214 | 0 | break; |
215 | | |
216 | 0 | case SSL_ERROR_SSL: |
217 | 0 | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
218 | 0 | break; |
219 | | |
220 | 0 | case SSL_ERROR_ZERO_RETURN: |
221 | 0 | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
222 | 0 | break; |
223 | | |
224 | 0 | case SSL_ERROR_SYSCALL: |
225 | 0 | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
226 | 0 | break; |
227 | 0 | } |
228 | 0 | } |
229 | | |
230 | | #ifdef FREERDP_HAVE_VALGRIND_MEMCHECK_H |
231 | | |
232 | | if (status > 0) |
233 | | { |
234 | | VALGRIND_MAKE_MEM_DEFINED(buf, status); |
235 | | } |
236 | | |
237 | | #endif |
238 | 0 | return status; |
239 | 0 | } |
240 | | |
241 | | static int bio_rdp_tls_puts(BIO* bio, const char* str) |
242 | 0 | { |
243 | 0 | size_t size = 0; |
244 | 0 | int status = 0; |
245 | |
|
246 | 0 | if (!str) |
247 | 0 | return 0; |
248 | | |
249 | 0 | size = strlen(str); |
250 | 0 | ERR_clear_error(); |
251 | 0 | status = BIO_write(bio, str, size); |
252 | 0 | return status; |
253 | 0 | } |
254 | | |
255 | | static int bio_rdp_tls_gets(BIO* bio, char* str, int size) |
256 | 0 | { |
257 | 0 | return 1; |
258 | 0 | } |
259 | | |
260 | | static long bio_rdp_tls_ctrl(BIO* bio, int cmd, long num, void* ptr) |
261 | 0 | { |
262 | 0 | BIO* ssl_rbio = NULL; |
263 | 0 | BIO* ssl_wbio = NULL; |
264 | 0 | BIO* next_bio = NULL; |
265 | 0 | int status = -1; |
266 | 0 | BIO_RDP_TLS* tls = (BIO_RDP_TLS*)BIO_get_data(bio); |
267 | |
|
268 | 0 | if (!tls) |
269 | 0 | return 0; |
270 | | |
271 | 0 | if (!tls->ssl && (cmd != BIO_C_SET_SSL)) |
272 | 0 | return 0; |
273 | | |
274 | 0 | next_bio = BIO_next(bio); |
275 | 0 | ssl_rbio = tls->ssl ? SSL_get_rbio(tls->ssl) : NULL; |
276 | 0 | ssl_wbio = tls->ssl ? SSL_get_wbio(tls->ssl) : NULL; |
277 | |
|
278 | 0 | switch (cmd) |
279 | 0 | { |
280 | 0 | case BIO_CTRL_RESET: |
281 | 0 | SSL_shutdown(tls->ssl); |
282 | |
|
283 | 0 | if (SSL_in_connect_init(tls->ssl)) |
284 | 0 | SSL_set_connect_state(tls->ssl); |
285 | 0 | else if (SSL_in_accept_init(tls->ssl)) |
286 | 0 | SSL_set_accept_state(tls->ssl); |
287 | |
|
288 | 0 | SSL_clear(tls->ssl); |
289 | |
|
290 | 0 | if (next_bio) |
291 | 0 | status = BIO_ctrl(next_bio, cmd, num, ptr); |
292 | 0 | else if (ssl_rbio) |
293 | 0 | status = BIO_ctrl(ssl_rbio, cmd, num, ptr); |
294 | 0 | else |
295 | 0 | status = 1; |
296 | |
|
297 | 0 | break; |
298 | | |
299 | 0 | case BIO_C_GET_FD: |
300 | 0 | status = BIO_ctrl(ssl_rbio, cmd, num, ptr); |
301 | 0 | break; |
302 | | |
303 | 0 | case BIO_CTRL_INFO: |
304 | 0 | status = 0; |
305 | 0 | break; |
306 | | |
307 | 0 | case BIO_CTRL_SET_CALLBACK: |
308 | 0 | status = 0; |
309 | 0 | break; |
310 | | |
311 | 0 | case BIO_CTRL_GET_CALLBACK: |
312 | 0 | *((ULONG_PTR*)ptr) = (ULONG_PTR)SSL_get_info_callback(tls->ssl); |
313 | 0 | status = 1; |
314 | 0 | break; |
315 | | |
316 | 0 | case BIO_C_SSL_MODE: |
317 | 0 | if (num) |
318 | 0 | SSL_set_connect_state(tls->ssl); |
319 | 0 | else |
320 | 0 | SSL_set_accept_state(tls->ssl); |
321 | |
|
322 | 0 | status = 1; |
323 | 0 | break; |
324 | | |
325 | 0 | case BIO_CTRL_GET_CLOSE: |
326 | 0 | status = BIO_get_shutdown(bio); |
327 | 0 | break; |
328 | | |
329 | 0 | case BIO_CTRL_SET_CLOSE: |
330 | 0 | BIO_set_shutdown(bio, (int)num); |
331 | 0 | status = 1; |
332 | 0 | break; |
333 | | |
334 | 0 | case BIO_CTRL_WPENDING: |
335 | 0 | status = BIO_ctrl(ssl_wbio, cmd, num, ptr); |
336 | 0 | break; |
337 | | |
338 | 0 | case BIO_CTRL_PENDING: |
339 | 0 | status = SSL_pending(tls->ssl); |
340 | |
|
341 | 0 | if (status == 0) |
342 | 0 | status = BIO_pending(ssl_rbio); |
343 | |
|
344 | 0 | break; |
345 | | |
346 | 0 | case BIO_CTRL_FLUSH: |
347 | 0 | BIO_clear_retry_flags(bio); |
348 | 0 | status = BIO_ctrl(ssl_wbio, cmd, num, ptr); |
349 | 0 | if (status != 1) |
350 | 0 | WLog_DBG(TAG, "BIO_ctrl returned %d", status); |
351 | 0 | BIO_copy_next_retry(bio); |
352 | 0 | status = 1; |
353 | 0 | break; |
354 | | |
355 | 0 | case BIO_CTRL_PUSH: |
356 | 0 | if (next_bio && (next_bio != ssl_rbio)) |
357 | 0 | { |
358 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
359 | | (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL) |
360 | | SSL_set_bio(tls->ssl, next_bio, next_bio); |
361 | | CRYPTO_add(&(bio->next_bio->references), 1, CRYPTO_LOCK_BIO); |
362 | | #else |
363 | | /* |
364 | | * We are going to pass ownership of next to the SSL object...but |
365 | | * we don't own a reference to pass yet - so up ref |
366 | | */ |
367 | 0 | BIO_up_ref(next_bio); |
368 | 0 | SSL_set_bio(tls->ssl, next_bio, next_bio); |
369 | 0 | #endif |
370 | 0 | } |
371 | |
|
372 | 0 | status = 1; |
373 | 0 | break; |
374 | | |
375 | 0 | case BIO_CTRL_POP: |
376 | | |
377 | | /* Only detach if we are the BIO explicitly being popped */ |
378 | 0 | if (bio == ptr) |
379 | 0 | { |
380 | 0 | if (ssl_rbio != ssl_wbio) |
381 | 0 | BIO_free_all(ssl_wbio); |
382 | |
|
383 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
384 | | (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL) |
385 | | |
386 | | if (next_bio) |
387 | | CRYPTO_add(&(bio->next_bio->references), -1, CRYPTO_LOCK_BIO); |
388 | | |
389 | | tls->ssl->wbio = tls->ssl->rbio = NULL; |
390 | | #else |
391 | | /* OpenSSL 1.1: This will also clear the reference we obtained during push */ |
392 | 0 | SSL_set_bio(tls->ssl, NULL, NULL); |
393 | 0 | #endif |
394 | 0 | } |
395 | |
|
396 | 0 | status = 1; |
397 | 0 | break; |
398 | | |
399 | 0 | case BIO_C_GET_SSL: |
400 | 0 | if (ptr) |
401 | 0 | { |
402 | 0 | *((SSL**)ptr) = tls->ssl; |
403 | 0 | status = 1; |
404 | 0 | } |
405 | |
|
406 | 0 | break; |
407 | | |
408 | 0 | case BIO_C_SET_SSL: |
409 | 0 | BIO_set_shutdown(bio, (int)num); |
410 | |
|
411 | 0 | if (ptr) |
412 | 0 | { |
413 | 0 | tls->ssl = (SSL*)ptr; |
414 | 0 | ssl_rbio = SSL_get_rbio(tls->ssl); |
415 | 0 | } |
416 | |
|
417 | 0 | if (ssl_rbio) |
418 | 0 | { |
419 | 0 | if (next_bio) |
420 | 0 | BIO_push(ssl_rbio, next_bio); |
421 | |
|
422 | 0 | BIO_set_next(bio, ssl_rbio); |
423 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
424 | | (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL) |
425 | | CRYPTO_add(&(ssl_rbio->references), 1, CRYPTO_LOCK_BIO); |
426 | | #else |
427 | 0 | BIO_up_ref(ssl_rbio); |
428 | 0 | #endif |
429 | 0 | } |
430 | |
|
431 | 0 | BIO_set_init(bio, 1); |
432 | 0 | status = 1; |
433 | 0 | break; |
434 | | |
435 | 0 | case BIO_C_DO_STATE_MACHINE: |
436 | 0 | BIO_clear_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_WRITE | BIO_FLAGS_IO_SPECIAL); |
437 | 0 | BIO_set_retry_reason(bio, 0); |
438 | 0 | status = SSL_do_handshake(tls->ssl); |
439 | |
|
440 | 0 | if (status <= 0) |
441 | 0 | { |
442 | 0 | switch (SSL_get_error(tls->ssl, status)) |
443 | 0 | { |
444 | 0 | case SSL_ERROR_WANT_READ: |
445 | 0 | BIO_set_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY); |
446 | 0 | break; |
447 | | |
448 | 0 | case SSL_ERROR_WANT_WRITE: |
449 | 0 | BIO_set_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY); |
450 | 0 | break; |
451 | | |
452 | 0 | case SSL_ERROR_WANT_CONNECT: |
453 | 0 | BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY); |
454 | 0 | BIO_set_retry_reason(bio, BIO_get_retry_reason(next_bio)); |
455 | 0 | break; |
456 | | |
457 | 0 | default: |
458 | 0 | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
459 | 0 | break; |
460 | 0 | } |
461 | 0 | } |
462 | | |
463 | 0 | break; |
464 | | |
465 | 0 | default: |
466 | 0 | status = BIO_ctrl(ssl_rbio, cmd, num, ptr); |
467 | 0 | break; |
468 | 0 | } |
469 | | |
470 | 0 | return status; |
471 | 0 | } |
472 | | |
473 | | static int bio_rdp_tls_new(BIO* bio) |
474 | 0 | { |
475 | 0 | BIO_RDP_TLS* tls = NULL; |
476 | 0 | BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
477 | |
|
478 | 0 | if (!(tls = calloc(1, sizeof(BIO_RDP_TLS)))) |
479 | 0 | return 0; |
480 | | |
481 | 0 | InitializeCriticalSectionAndSpinCount(&tls->lock, 4000); |
482 | 0 | BIO_set_data(bio, (void*)tls); |
483 | 0 | return 1; |
484 | 0 | } |
485 | | |
486 | | static int bio_rdp_tls_free(BIO* bio) |
487 | 0 | { |
488 | 0 | BIO_RDP_TLS* tls = NULL; |
489 | |
|
490 | 0 | if (!bio) |
491 | 0 | return 0; |
492 | | |
493 | 0 | tls = (BIO_RDP_TLS*)BIO_get_data(bio); |
494 | |
|
495 | 0 | if (!tls) |
496 | 0 | return 0; |
497 | | |
498 | 0 | BIO_set_data(bio, NULL); |
499 | 0 | if (BIO_get_shutdown(bio)) |
500 | 0 | { |
501 | 0 | if (BIO_get_init(bio) && tls->ssl) |
502 | 0 | { |
503 | 0 | SSL_shutdown(tls->ssl); |
504 | 0 | SSL_free(tls->ssl); |
505 | 0 | } |
506 | |
|
507 | 0 | BIO_set_init(bio, 0); |
508 | 0 | BIO_set_flags(bio, 0); |
509 | 0 | } |
510 | |
|
511 | 0 | DeleteCriticalSection(&tls->lock); |
512 | 0 | free(tls); |
513 | |
|
514 | 0 | return 1; |
515 | 0 | } |
516 | | |
517 | | static long bio_rdp_tls_callback_ctrl(BIO* bio, int cmd, bio_info_cb* fp) |
518 | 0 | { |
519 | 0 | long status = 0; |
520 | 0 | BIO_RDP_TLS* tls = NULL; |
521 | |
|
522 | 0 | if (!bio) |
523 | 0 | return 0; |
524 | | |
525 | 0 | tls = (BIO_RDP_TLS*)BIO_get_data(bio); |
526 | |
|
527 | 0 | if (!tls) |
528 | 0 | return 0; |
529 | | |
530 | 0 | switch (cmd) |
531 | 0 | { |
532 | 0 | case BIO_CTRL_SET_CALLBACK: |
533 | 0 | { |
534 | 0 | typedef void (*fkt_t)(const SSL*, int, int); |
535 | | /* Documented since https://www.openssl.org/docs/man1.1.1/man3/BIO_set_callback.html |
536 | | * the argument is not really of type bio_info_cb* and must be cast |
537 | | * to the required type */ |
538 | 0 | fkt_t fkt = (fkt_t)(void*)fp; |
539 | 0 | SSL_set_info_callback(tls->ssl, fkt); |
540 | 0 | status = 1; |
541 | 0 | } |
542 | 0 | break; |
543 | | |
544 | 0 | default: |
545 | 0 | status = BIO_callback_ctrl(SSL_get_rbio(tls->ssl), cmd, fp); |
546 | 0 | break; |
547 | 0 | } |
548 | | |
549 | 0 | return status; |
550 | 0 | } |
551 | | |
552 | 0 | #define BIO_TYPE_RDP_TLS 68 |
553 | | |
554 | | static BIO_METHOD* BIO_s_rdp_tls(void) |
555 | 0 | { |
556 | 0 | static BIO_METHOD* bio_methods = NULL; |
557 | |
|
558 | 0 | if (bio_methods == NULL) |
559 | 0 | { |
560 | 0 | if (!(bio_methods = BIO_meth_new(BIO_TYPE_RDP_TLS, "RdpTls"))) |
561 | 0 | return NULL; |
562 | | |
563 | 0 | BIO_meth_set_write(bio_methods, bio_rdp_tls_write); |
564 | 0 | BIO_meth_set_read(bio_methods, bio_rdp_tls_read); |
565 | 0 | BIO_meth_set_puts(bio_methods, bio_rdp_tls_puts); |
566 | 0 | BIO_meth_set_gets(bio_methods, bio_rdp_tls_gets); |
567 | 0 | BIO_meth_set_ctrl(bio_methods, bio_rdp_tls_ctrl); |
568 | 0 | BIO_meth_set_create(bio_methods, bio_rdp_tls_new); |
569 | 0 | BIO_meth_set_destroy(bio_methods, bio_rdp_tls_free); |
570 | 0 | BIO_meth_set_callback_ctrl(bio_methods, bio_rdp_tls_callback_ctrl); |
571 | 0 | } |
572 | | |
573 | 0 | return bio_methods; |
574 | 0 | } |
575 | | |
576 | | static BIO* BIO_new_rdp_tls(SSL_CTX* ctx, int client) |
577 | 0 | { |
578 | 0 | BIO* bio = NULL; |
579 | 0 | SSL* ssl = NULL; |
580 | 0 | bio = BIO_new(BIO_s_rdp_tls()); |
581 | |
|
582 | 0 | if (!bio) |
583 | 0 | return NULL; |
584 | | |
585 | 0 | ssl = SSL_new(ctx); |
586 | |
|
587 | 0 | if (!ssl) |
588 | 0 | { |
589 | 0 | BIO_free_all(bio); |
590 | 0 | return NULL; |
591 | 0 | } |
592 | | |
593 | 0 | if (client) |
594 | 0 | SSL_set_connect_state(ssl); |
595 | 0 | else |
596 | 0 | SSL_set_accept_state(ssl); |
597 | |
|
598 | 0 | BIO_set_ssl(bio, ssl, BIO_CLOSE); |
599 | 0 | return bio; |
600 | 0 | } |
601 | | |
602 | | static rdpCertificate* tls_get_certificate(rdpTls* tls, BOOL peer) |
603 | 0 | { |
604 | 0 | X509* remote_cert = NULL; |
605 | |
|
606 | 0 | if (peer) |
607 | 0 | remote_cert = SSL_get_peer_certificate(tls->ssl); |
608 | 0 | else |
609 | 0 | remote_cert = X509_dup(SSL_get_certificate(tls->ssl)); |
610 | |
|
611 | 0 | if (!remote_cert) |
612 | 0 | { |
613 | 0 | WLog_ERR(TAG, "failed to get the server TLS certificate"); |
614 | 0 | return NULL; |
615 | 0 | } |
616 | | |
617 | | /* Get the peer's chain. If it does not exist, we're setting NULL (clean data either way) */ |
618 | 0 | STACK_OF(X509)* chain = SSL_get_peer_cert_chain(tls->ssl); |
619 | 0 | rdpCertificate* cert = freerdp_certificate_new_from_x509(remote_cert, chain); |
620 | 0 | X509_free(remote_cert); |
621 | |
|
622 | 0 | return cert; |
623 | 0 | } |
624 | | |
625 | | static const char* tls_get_server_name(rdpTls* tls) |
626 | 0 | { |
627 | 0 | return tls->serverName ? tls->serverName : tls->hostname; |
628 | 0 | } |
629 | | |
630 | 0 | #define TLS_SERVER_END_POINT "tls-server-end-point:" |
631 | | |
632 | | static SecPkgContext_Bindings* tls_get_channel_bindings(const rdpCertificate* cert) |
633 | 0 | { |
634 | 0 | size_t CertificateHashLength = 0; |
635 | 0 | BYTE* ChannelBindingToken = NULL; |
636 | 0 | UINT32 ChannelBindingTokenLength = 0; |
637 | 0 | SEC_CHANNEL_BINDINGS* ChannelBindings = NULL; |
638 | 0 | SecPkgContext_Bindings* ContextBindings = NULL; |
639 | 0 | const size_t PrefixLength = strnlen(TLS_SERVER_END_POINT, ARRAYSIZE(TLS_SERVER_END_POINT)); |
640 | |
|
641 | 0 | WINPR_ASSERT(cert); |
642 | | |
643 | | /* See https://www.rfc-editor.org/rfc/rfc5929 for details about hashes */ |
644 | 0 | WINPR_MD_TYPE alg = freerdp_certificate_get_signature_alg(cert); |
645 | 0 | const char* hash = NULL; |
646 | 0 | switch (alg) |
647 | 0 | { |
648 | | |
649 | 0 | case WINPR_MD_MD5: |
650 | 0 | case WINPR_MD_SHA1: |
651 | 0 | hash = winpr_md_type_to_string(WINPR_MD_SHA256); |
652 | 0 | break; |
653 | 0 | default: |
654 | 0 | hash = winpr_md_type_to_string(alg); |
655 | 0 | break; |
656 | 0 | } |
657 | 0 | if (!hash) |
658 | 0 | return NULL; |
659 | | |
660 | 0 | char* CertificateHash = freerdp_certificate_get_hash(cert, hash, &CertificateHashLength); |
661 | 0 | if (!CertificateHash) |
662 | 0 | return NULL; |
663 | | |
664 | 0 | ChannelBindingTokenLength = PrefixLength + CertificateHashLength; |
665 | 0 | ContextBindings = (SecPkgContext_Bindings*)calloc(1, sizeof(SecPkgContext_Bindings)); |
666 | |
|
667 | 0 | if (!ContextBindings) |
668 | 0 | goto out_free; |
669 | | |
670 | 0 | ContextBindings->BindingsLength = sizeof(SEC_CHANNEL_BINDINGS) + ChannelBindingTokenLength; |
671 | 0 | ChannelBindings = (SEC_CHANNEL_BINDINGS*)calloc(1, ContextBindings->BindingsLength); |
672 | |
|
673 | 0 | if (!ChannelBindings) |
674 | 0 | goto out_free; |
675 | | |
676 | 0 | ContextBindings->Bindings = ChannelBindings; |
677 | 0 | ChannelBindings->cbApplicationDataLength = ChannelBindingTokenLength; |
678 | 0 | ChannelBindings->dwApplicationDataOffset = sizeof(SEC_CHANNEL_BINDINGS); |
679 | 0 | ChannelBindingToken = &((BYTE*)ChannelBindings)[ChannelBindings->dwApplicationDataOffset]; |
680 | 0 | memcpy(ChannelBindingToken, TLS_SERVER_END_POINT, PrefixLength); |
681 | 0 | memcpy(ChannelBindingToken + PrefixLength, CertificateHash, CertificateHashLength); |
682 | 0 | free(CertificateHash); |
683 | 0 | return ContextBindings; |
684 | 0 | out_free: |
685 | 0 | free(CertificateHash); |
686 | 0 | free(ContextBindings); |
687 | 0 | return NULL; |
688 | 0 | } |
689 | | |
690 | | static INIT_ONCE secrets_file_idx_once = INIT_ONCE_STATIC_INIT; |
691 | | static int secrets_file_idx = -1; |
692 | | |
693 | | static BOOL CALLBACK secrets_file_init_cb(PINIT_ONCE once, PVOID param, PVOID* context) |
694 | 0 | { |
695 | 0 | secrets_file_idx = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
696 | |
|
697 | 0 | return (secrets_file_idx != -1); |
698 | 0 | } |
699 | | |
700 | | static void SSLCTX_keylog_cb(const SSL* ssl, const char* line) |
701 | 0 | { |
702 | 0 | char* dfile = NULL; |
703 | |
|
704 | 0 | if (secrets_file_idx == -1) |
705 | 0 | return; |
706 | | |
707 | 0 | dfile = SSL_get_ex_data(ssl, secrets_file_idx); |
708 | 0 | if (dfile) |
709 | 0 | { |
710 | 0 | FILE* f = winpr_fopen(dfile, "a+"); |
711 | 0 | if (f) |
712 | 0 | { |
713 | 0 | fwrite(line, strlen(line), 1, f); |
714 | 0 | fwrite("\n", 1, 1, f); |
715 | 0 | fclose(f); |
716 | 0 | } |
717 | 0 | } |
718 | 0 | } |
719 | | |
720 | | static void tls_reset(rdpTls* tls) |
721 | 0 | { |
722 | 0 | WINPR_ASSERT(tls); |
723 | | |
724 | 0 | if (tls->ctx) |
725 | 0 | { |
726 | 0 | SSL_CTX_free(tls->ctx); |
727 | 0 | tls->ctx = NULL; |
728 | 0 | } |
729 | | |
730 | | /* tls->underlying is a stacked BIO under tls->bio. |
731 | | * BIO_free_all will free recursivly. */ |
732 | 0 | if (tls->bio) |
733 | 0 | BIO_free_all(tls->bio); |
734 | 0 | else if (tls->underlying) |
735 | 0 | BIO_free_all(tls->underlying); |
736 | 0 | tls->bio = NULL; |
737 | 0 | tls->underlying = NULL; |
738 | |
|
739 | 0 | free_tls_public_key(tls); |
740 | 0 | free_tls_bindings(tls); |
741 | 0 | } |
742 | | |
743 | | #if OPENSSL_VERSION_NUMBER >= 0x010000000L |
744 | | static BOOL tls_prepare(rdpTls* tls, BIO* underlying, const SSL_METHOD* method, int options, |
745 | | BOOL clientMode) |
746 | | #else |
747 | | static BOOL tls_prepare(rdpTls* tls, BIO* underlying, SSL_METHOD* method, int options, |
748 | | BOOL clientMode) |
749 | | #endif |
750 | 0 | { |
751 | 0 | WINPR_ASSERT(tls); |
752 | | |
753 | 0 | rdpSettings* settings = tls->settings; |
754 | 0 | WINPR_ASSERT(settings); |
755 | | |
756 | 0 | tls_reset(tls); |
757 | 0 | tls->ctx = SSL_CTX_new(method); |
758 | |
|
759 | 0 | tls->underlying = underlying; |
760 | |
|
761 | 0 | if (!tls->ctx) |
762 | 0 | { |
763 | 0 | WLog_ERR(TAG, "SSL_CTX_new failed"); |
764 | 0 | return FALSE; |
765 | 0 | } |
766 | | |
767 | 0 | SSL_CTX_set_mode(tls->ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | SSL_MODE_ENABLE_PARTIAL_WRITE); |
768 | 0 | SSL_CTX_set_options(tls->ctx, options); |
769 | 0 | SSL_CTX_set_read_ahead(tls->ctx, 1); |
770 | 0 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
771 | 0 | UINT16 version = freerdp_settings_get_uint16(settings, FreeRDP_TLSMinVersion); |
772 | 0 | if (!SSL_CTX_set_min_proto_version(tls->ctx, version)) |
773 | 0 | { |
774 | 0 | WLog_ERR(TAG, "SSL_CTX_set_min_proto_version %s failed", version); |
775 | 0 | return FALSE; |
776 | 0 | } |
777 | 0 | version = freerdp_settings_get_uint16(settings, FreeRDP_TLSMaxVersion); |
778 | 0 | if (!SSL_CTX_set_max_proto_version(tls->ctx, version)) |
779 | 0 | { |
780 | 0 | WLog_ERR(TAG, "SSL_CTX_set_max_proto_version %s failed", version); |
781 | 0 | return FALSE; |
782 | 0 | } |
783 | 0 | #endif |
784 | 0 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER) |
785 | 0 | SSL_CTX_set_security_level(tls->ctx, settings->TlsSecLevel); |
786 | 0 | #endif |
787 | |
|
788 | 0 | if (settings->AllowedTlsCiphers) |
789 | 0 | { |
790 | 0 | if (!SSL_CTX_set_cipher_list(tls->ctx, settings->AllowedTlsCiphers)) |
791 | 0 | { |
792 | 0 | WLog_ERR(TAG, "SSL_CTX_set_cipher_list %s failed", settings->AllowedTlsCiphers); |
793 | 0 | return FALSE; |
794 | 0 | } |
795 | 0 | } |
796 | | |
797 | 0 | tls->bio = BIO_new_rdp_tls(tls->ctx, clientMode); |
798 | |
|
799 | 0 | if (BIO_get_ssl(tls->bio, &tls->ssl) < 0) |
800 | 0 | { |
801 | 0 | WLog_ERR(TAG, "unable to retrieve the SSL of the connection"); |
802 | 0 | return FALSE; |
803 | 0 | } |
804 | | |
805 | 0 | if (settings->TlsSecretsFile) |
806 | 0 | { |
807 | 0 | #if OPENSSL_VERSION_NUMBER >= 0x10101000L |
808 | 0 | InitOnceExecuteOnce(&secrets_file_idx_once, secrets_file_init_cb, NULL, NULL); |
809 | |
|
810 | 0 | if (secrets_file_idx != -1) |
811 | 0 | { |
812 | 0 | SSL_set_ex_data(tls->ssl, secrets_file_idx, settings->TlsSecretsFile); |
813 | 0 | SSL_CTX_set_keylog_callback(tls->ctx, SSLCTX_keylog_cb); |
814 | 0 | } |
815 | | #else |
816 | | WLog_WARN(TAG, "Key-Logging not available - requires OpenSSL 1.1.1 or higher"); |
817 | | #endif |
818 | 0 | } |
819 | |
|
820 | 0 | BIO_push(tls->bio, underlying); |
821 | 0 | return TRUE; |
822 | 0 | } |
823 | | |
824 | | static void adjustSslOptions(int* options) |
825 | 0 | { |
826 | 0 | WINPR_ASSERT(options); |
827 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
828 | | *options |= SSL_OP_NO_SSLv2; |
829 | | *options |= SSL_OP_NO_SSLv3; |
830 | | #endif |
831 | 0 | } |
832 | | |
833 | | const SSL_METHOD* freerdp_tls_get_ssl_method(BOOL isDtls, BOOL isClient) |
834 | 0 | { |
835 | 0 | if (isClient) |
836 | 0 | { |
837 | 0 | if (isDtls) |
838 | 0 | return DTLS_client_method(); |
839 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
840 | | return SSLv23_client_method(); |
841 | | #else |
842 | 0 | return TLS_client_method(); |
843 | 0 | #endif |
844 | 0 | } |
845 | | |
846 | 0 | if (isDtls) |
847 | 0 | return DTLS_server_method(); |
848 | | |
849 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
850 | | return SSLv23_server_method(); |
851 | | #else |
852 | 0 | return TLS_server_method(); |
853 | 0 | #endif |
854 | 0 | } |
855 | | |
856 | | TlsHandshakeResult freerdp_tls_connect_ex(rdpTls* tls, BIO* underlying, const SSL_METHOD* methods) |
857 | 0 | { |
858 | 0 | WINPR_ASSERT(tls); |
859 | | |
860 | 0 | int options = 0; |
861 | | /** |
862 | | * SSL_OP_NO_COMPRESSION: |
863 | | * |
864 | | * The Microsoft RDP server does not advertise support |
865 | | * for TLS compression, but alternative servers may support it. |
866 | | * This was observed between early versions of the FreeRDP server |
867 | | * and the FreeRDP client, and caused major performance issues, |
868 | | * which is why we're disabling it. |
869 | | */ |
870 | 0 | #ifdef SSL_OP_NO_COMPRESSION |
871 | 0 | options |= SSL_OP_NO_COMPRESSION; |
872 | 0 | #endif |
873 | | /** |
874 | | * SSL_OP_TLS_BLOCK_PADDING_BUG: |
875 | | * |
876 | | * The Microsoft RDP server does *not* support TLS padding. |
877 | | * It absolutely needs to be disabled otherwise it won't work. |
878 | | */ |
879 | 0 | options |= SSL_OP_TLS_BLOCK_PADDING_BUG; |
880 | | /** |
881 | | * SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS: |
882 | | * |
883 | | * Just like TLS padding, the Microsoft RDP server does not |
884 | | * support empty fragments. This needs to be disabled. |
885 | | */ |
886 | 0 | options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; |
887 | |
|
888 | 0 | tls->isClientMode = TRUE; |
889 | 0 | adjustSslOptions(&options); |
890 | |
|
891 | 0 | if (!tls_prepare(tls, underlying, methods, options, TRUE)) |
892 | 0 | return 0; |
893 | | |
894 | 0 | #if !defined(OPENSSL_NO_TLSEXT) && !defined(LIBRESSL_VERSION_NUMBER) |
895 | 0 | SSL_set_tlsext_host_name(tls->ssl, tls_get_server_name(tls)); |
896 | 0 | #endif |
897 | |
|
898 | 0 | return freerdp_tls_handshake(tls); |
899 | 0 | } |
900 | | |
901 | | TlsHandshakeResult freerdp_tls_handshake(rdpTls* tls) |
902 | 0 | { |
903 | 0 | TlsHandshakeResult ret = TLS_HANDSHAKE_ERROR; |
904 | |
|
905 | 0 | WINPR_ASSERT(tls); |
906 | 0 | int status = BIO_do_handshake(tls->bio); |
907 | 0 | if (status != 1) |
908 | 0 | { |
909 | 0 | if (!BIO_should_retry(tls->bio)) |
910 | 0 | return TLS_HANDSHAKE_ERROR; |
911 | | |
912 | 0 | return TLS_HANDSHAKE_CONTINUE; |
913 | 0 | } |
914 | | |
915 | 0 | int verify_status = 0; |
916 | 0 | rdpCertificate* cert = tls_get_certificate(tls, tls->isClientMode); |
917 | |
|
918 | 0 | if (!cert) |
919 | 0 | { |
920 | 0 | WLog_ERR(TAG, "tls_get_certificate failed to return the server certificate."); |
921 | 0 | return TLS_HANDSHAKE_ERROR; |
922 | 0 | } |
923 | | |
924 | 0 | do |
925 | 0 | { |
926 | 0 | free_tls_bindings(tls); |
927 | 0 | tls->Bindings = tls_get_channel_bindings(cert); |
928 | 0 | if (!tls->Bindings) |
929 | 0 | { |
930 | 0 | WLog_ERR(TAG, "unable to retrieve bindings"); |
931 | 0 | break; |
932 | 0 | } |
933 | | |
934 | 0 | free_tls_public_key(tls); |
935 | 0 | if (!freerdp_certificate_get_public_key(cert, &tls->PublicKey, &tls->PublicKeyLength)) |
936 | 0 | { |
937 | 0 | WLog_ERR(TAG, |
938 | 0 | "freerdp_certificate_get_public_key failed to return the server public key."); |
939 | 0 | break; |
940 | 0 | } |
941 | | |
942 | | /* server-side NLA needs public keys (keys from us, the server) but no certificate verify */ |
943 | 0 | ret = TLS_HANDSHAKE_SUCCESS; |
944 | |
|
945 | 0 | if (tls->isClientMode) |
946 | 0 | { |
947 | 0 | verify_status = tls_verify_certificate(tls, cert, tls_get_server_name(tls), tls->port); |
948 | |
|
949 | 0 | if (verify_status < 1) |
950 | 0 | { |
951 | 0 | WLog_ERR(TAG, "certificate not trusted, aborting."); |
952 | 0 | freerdp_tls_send_alert(tls); |
953 | 0 | ret = TLS_HANDSHAKE_VERIFY_ERROR; |
954 | 0 | } |
955 | 0 | } |
956 | 0 | } while (0); |
957 | | |
958 | 0 | freerdp_certificate_free(cert); |
959 | 0 | return ret; |
960 | 0 | } |
961 | | |
962 | | static int pollAndHandshake(rdpTls* tls) |
963 | 0 | { |
964 | 0 | WINPR_ASSERT(tls); |
965 | | |
966 | 0 | do |
967 | 0 | { |
968 | 0 | HANDLE event = NULL; |
969 | 0 | DWORD status = 0; |
970 | 0 | if (BIO_get_event(tls->bio, &event) < 0) |
971 | 0 | { |
972 | 0 | WLog_ERR(TAG, "unable to retrieve BIO associated event"); |
973 | 0 | return -1; |
974 | 0 | } |
975 | | |
976 | 0 | if (!event) |
977 | 0 | { |
978 | 0 | WLog_ERR(TAG, "unable to retrieve BIO event"); |
979 | 0 | return -1; |
980 | 0 | } |
981 | | |
982 | 0 | status = WaitForSingleObjectEx(event, 50, TRUE); |
983 | 0 | switch (status) |
984 | 0 | { |
985 | 0 | case WAIT_OBJECT_0: |
986 | 0 | break; |
987 | 0 | case WAIT_TIMEOUT: |
988 | 0 | case WAIT_IO_COMPLETION: |
989 | 0 | continue; |
990 | 0 | default: |
991 | 0 | WLog_ERR(TAG, "error during WaitForSingleObject(): 0x%08" PRIX32 "", status); |
992 | 0 | return -1; |
993 | 0 | } |
994 | | |
995 | 0 | TlsHandshakeResult result = freerdp_tls_handshake(tls); |
996 | 0 | switch (result) |
997 | 0 | { |
998 | 0 | case TLS_HANDSHAKE_CONTINUE: |
999 | 0 | break; |
1000 | 0 | case TLS_HANDSHAKE_SUCCESS: |
1001 | 0 | return 1; |
1002 | 0 | case TLS_HANDSHAKE_ERROR: |
1003 | 0 | case TLS_HANDSHAKE_VERIFY_ERROR: |
1004 | 0 | default: |
1005 | 0 | return -1; |
1006 | 0 | } |
1007 | 0 | } while (TRUE); |
1008 | 0 | } |
1009 | | |
1010 | | int freerdp_tls_connect(rdpTls* tls, BIO* underlying) |
1011 | 0 | { |
1012 | 0 | const SSL_METHOD* method = freerdp_tls_get_ssl_method(FALSE, TRUE); |
1013 | |
|
1014 | 0 | WINPR_ASSERT(tls); |
1015 | 0 | TlsHandshakeResult result = freerdp_tls_connect_ex(tls, underlying, method); |
1016 | 0 | switch (result) |
1017 | 0 | { |
1018 | 0 | case TLS_HANDSHAKE_SUCCESS: |
1019 | 0 | return 1; |
1020 | 0 | case TLS_HANDSHAKE_CONTINUE: |
1021 | 0 | break; |
1022 | 0 | case TLS_HANDSHAKE_ERROR: |
1023 | 0 | case TLS_HANDSHAKE_VERIFY_ERROR: |
1024 | 0 | return -1; |
1025 | 0 | } |
1026 | | |
1027 | 0 | return pollAndHandshake(tls); |
1028 | 0 | } |
1029 | | |
1030 | | #if defined(MICROSOFT_IOS_SNI_BUG) && !defined(OPENSSL_NO_TLSEXT) && \ |
1031 | | !defined(LIBRESSL_VERSION_NUMBER) |
1032 | | static void tls_openssl_tlsext_debug_callback(SSL* s, int client_server, int type, |
1033 | | unsigned char* data, int len, void* arg) |
1034 | | { |
1035 | | if (type == TLSEXT_TYPE_server_name) |
1036 | | { |
1037 | | WLog_DBG(TAG, "Client uses SNI (extension disabled)"); |
1038 | | s->servername_done = 2; |
1039 | | } |
1040 | | } |
1041 | | #endif |
1042 | | |
1043 | | BOOL freerdp_tls_accept(rdpTls* tls, BIO* underlying, rdpSettings* settings) |
1044 | 0 | { |
1045 | 0 | WINPR_ASSERT(tls); |
1046 | 0 | TlsHandshakeResult res = |
1047 | 0 | freerdp_tls_accept_ex(tls, underlying, settings, freerdp_tls_get_ssl_method(FALSE, FALSE)); |
1048 | 0 | switch (res) |
1049 | 0 | { |
1050 | 0 | case TLS_HANDSHAKE_SUCCESS: |
1051 | 0 | return TRUE; |
1052 | 0 | case TLS_HANDSHAKE_CONTINUE: |
1053 | 0 | break; |
1054 | 0 | case TLS_HANDSHAKE_ERROR: |
1055 | 0 | case TLS_HANDSHAKE_VERIFY_ERROR: |
1056 | 0 | default: |
1057 | 0 | return FALSE; |
1058 | 0 | } |
1059 | | |
1060 | 0 | return pollAndHandshake(tls) > 0; |
1061 | 0 | } |
1062 | | |
1063 | | TlsHandshakeResult freerdp_tls_accept_ex(rdpTls* tls, BIO* underlying, rdpSettings* settings, |
1064 | | const SSL_METHOD* methods) |
1065 | 0 | { |
1066 | 0 | WINPR_ASSERT(tls); |
1067 | | |
1068 | 0 | long options = 0; |
1069 | 0 | int status = 0; |
1070 | | |
1071 | | /** |
1072 | | * SSL_OP_NO_SSLv2: |
1073 | | * |
1074 | | * We only want SSLv3 and TLSv1, so disable SSLv2. |
1075 | | * SSLv3 is used by, eg. Microsoft RDC for Mac OS X. |
1076 | | */ |
1077 | 0 | options |= SSL_OP_NO_SSLv2; |
1078 | | /** |
1079 | | * SSL_OP_NO_COMPRESSION: |
1080 | | * |
1081 | | * The Microsoft RDP server does not advertise support |
1082 | | * for TLS compression, but alternative servers may support it. |
1083 | | * This was observed between early versions of the FreeRDP server |
1084 | | * and the FreeRDP client, and caused major performance issues, |
1085 | | * which is why we're disabling it. |
1086 | | */ |
1087 | 0 | #ifdef SSL_OP_NO_COMPRESSION |
1088 | 0 | options |= SSL_OP_NO_COMPRESSION; |
1089 | 0 | #endif |
1090 | | /** |
1091 | | * SSL_OP_TLS_BLOCK_PADDING_BUG: |
1092 | | * |
1093 | | * The Microsoft RDP server does *not* support TLS padding. |
1094 | | * It absolutely needs to be disabled otherwise it won't work. |
1095 | | */ |
1096 | 0 | options |= SSL_OP_TLS_BLOCK_PADDING_BUG; |
1097 | | /** |
1098 | | * SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS: |
1099 | | * |
1100 | | * Just like TLS padding, the Microsoft RDP server does not |
1101 | | * support empty fragments. This needs to be disabled. |
1102 | | */ |
1103 | 0 | options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; |
1104 | | |
1105 | | /** |
1106 | | * SSL_OP_NO_RENEGOTIATION |
1107 | | * |
1108 | | * Disable SSL client site renegotiation. |
1109 | | */ |
1110 | |
|
1111 | 0 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) && (OPENSSL_VERSION_NUMBER < 0x30000000L) && \ |
1112 | 0 | !defined(LIBRESSL_VERSION_NUMBER) |
1113 | 0 | options |= SSL_OP_NO_RENEGOTIATION; |
1114 | 0 | #endif |
1115 | |
|
1116 | 0 | if (!tls_prepare(tls, underlying, methods, options, FALSE)) |
1117 | 0 | return TLS_HANDSHAKE_ERROR; |
1118 | | |
1119 | 0 | const rdpPrivateKey* key = freerdp_settings_get_pointer(settings, FreeRDP_RdpServerRsaKey); |
1120 | 0 | if (!key) |
1121 | 0 | { |
1122 | 0 | WLog_ERR(TAG, "invalid private key"); |
1123 | 0 | return TLS_HANDSHAKE_ERROR; |
1124 | 0 | } |
1125 | | |
1126 | 0 | EVP_PKEY* privkey = freerdp_key_get_evp_pkey(key); |
1127 | 0 | if (!privkey) |
1128 | 0 | { |
1129 | 0 | WLog_ERR(TAG, "invalid private key"); |
1130 | 0 | return TLS_HANDSHAKE_ERROR; |
1131 | 0 | } |
1132 | | |
1133 | 0 | status = SSL_use_PrivateKey(tls->ssl, privkey); |
1134 | | /* The local reference to the private key will anyway go out of |
1135 | | * scope; so the reference count should be decremented weither |
1136 | | * SSL_use_PrivateKey succeeds or fails. |
1137 | | */ |
1138 | 0 | EVP_PKEY_free(privkey); |
1139 | |
|
1140 | 0 | if (status <= 0) |
1141 | 0 | { |
1142 | 0 | WLog_ERR(TAG, "SSL_CTX_use_PrivateKey_file failed"); |
1143 | 0 | return TLS_HANDSHAKE_ERROR; |
1144 | 0 | } |
1145 | | |
1146 | 0 | rdpCertificate* cert = |
1147 | 0 | freerdp_settings_get_pointer_writable(settings, FreeRDP_RdpServerCertificate); |
1148 | 0 | if (!cert) |
1149 | 0 | { |
1150 | 0 | WLog_ERR(TAG, "invalid certificate"); |
1151 | 0 | return TLS_HANDSHAKE_ERROR; |
1152 | 0 | } |
1153 | | |
1154 | 0 | status = SSL_use_certificate(tls->ssl, freerdp_certificate_get_x509(cert)); |
1155 | |
|
1156 | 0 | if (status <= 0) |
1157 | 0 | { |
1158 | 0 | WLog_ERR(TAG, "SSL_use_certificate_file failed"); |
1159 | 0 | return TLS_HANDSHAKE_ERROR; |
1160 | 0 | } |
1161 | | |
1162 | | #if defined(MICROSOFT_IOS_SNI_BUG) && !defined(OPENSSL_NO_TLSEXT) && \ |
1163 | | !defined(LIBRESSL_VERSION_NUMBER) |
1164 | | SSL_set_tlsext_debug_callback(tls->ssl, tls_openssl_tlsext_debug_callback); |
1165 | | #endif |
1166 | | |
1167 | 0 | return freerdp_tls_handshake(tls); |
1168 | 0 | } |
1169 | | |
1170 | | BOOL freerdp_tls_send_alert(rdpTls* tls) |
1171 | 0 | { |
1172 | 0 | WINPR_ASSERT(tls); |
1173 | | |
1174 | 0 | if (!tls) |
1175 | 0 | return FALSE; |
1176 | | |
1177 | 0 | if (!tls->ssl) |
1178 | 0 | return TRUE; |
1179 | | |
1180 | | /** |
1181 | | * FIXME: The following code does not work on OpenSSL > 1.1.0 because the |
1182 | | * SSL struct is opaqe now |
1183 | | */ |
1184 | | #if (!defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER < 0x10100000L)) || \ |
1185 | | (defined(LIBRESSL_VERSION_NUMBER) && (LIBRESSL_VERSION_NUMBER <= 0x2080300fL)) |
1186 | | |
1187 | | if (tls->alertDescription != TLS_ALERT_DESCRIPTION_CLOSE_NOTIFY) |
1188 | | { |
1189 | | /** |
1190 | | * OpenSSL doesn't really expose an API for sending a TLS alert manually. |
1191 | | * |
1192 | | * The following code disables the sending of the default "close notify" |
1193 | | * and then proceeds to force sending a custom TLS alert before shutting down. |
1194 | | * |
1195 | | * Manually sending a TLS alert is necessary in certain cases, |
1196 | | * like when server-side NLA results in an authentication failure. |
1197 | | */ |
1198 | | SSL_SESSION* ssl_session = SSL_get_session(tls->ssl); |
1199 | | SSL_CTX* ssl_ctx = SSL_get_SSL_CTX(tls->ssl); |
1200 | | SSL_set_quiet_shutdown(tls->ssl, 1); |
1201 | | |
1202 | | if ((tls->alertLevel == TLS_ALERT_LEVEL_FATAL) && (ssl_session)) |
1203 | | SSL_CTX_remove_session(ssl_ctx, ssl_session); |
1204 | | |
1205 | | tls->ssl->s3->alert_dispatch = 1; |
1206 | | tls->ssl->s3->send_alert[0] = tls->alertLevel; |
1207 | | tls->ssl->s3->send_alert[1] = tls->alertDescription; |
1208 | | |
1209 | | if (tls->ssl->s3->wbuf.left == 0) |
1210 | | tls->ssl->method->ssl_dispatch_alert(tls->ssl); |
1211 | | } |
1212 | | |
1213 | | #endif |
1214 | 0 | return TRUE; |
1215 | 0 | } |
1216 | | |
1217 | | int freerdp_tls_write_all(rdpTls* tls, const BYTE* data, int length) |
1218 | 0 | { |
1219 | 0 | WINPR_ASSERT(tls); |
1220 | 0 | int status = 0; |
1221 | 0 | int offset = 0; |
1222 | 0 | BIO* bio = tls->bio; |
1223 | |
|
1224 | 0 | while (offset < length) |
1225 | 0 | { |
1226 | 0 | ERR_clear_error(); |
1227 | 0 | status = BIO_write(bio, &data[offset], length - offset); |
1228 | |
|
1229 | 0 | if (status > 0) |
1230 | 0 | { |
1231 | 0 | offset += status; |
1232 | 0 | } |
1233 | 0 | else |
1234 | 0 | { |
1235 | 0 | if (!BIO_should_retry(bio)) |
1236 | 0 | return -1; |
1237 | | |
1238 | 0 | if (BIO_write_blocked(bio)) |
1239 | 0 | status = BIO_wait_write(bio, 100); |
1240 | 0 | else if (BIO_read_blocked(bio)) |
1241 | 0 | return -2; /* Abort write, there is data that must be read */ |
1242 | 0 | else |
1243 | 0 | USleep(100); |
1244 | | |
1245 | 0 | if (status < 0) |
1246 | 0 | return -1; |
1247 | 0 | } |
1248 | 0 | } |
1249 | | |
1250 | 0 | return length; |
1251 | 0 | } |
1252 | | |
1253 | | int freerdp_tls_set_alert_code(rdpTls* tls, int level, int description) |
1254 | 0 | { |
1255 | 0 | WINPR_ASSERT(tls); |
1256 | 0 | tls->alertLevel = level; |
1257 | 0 | tls->alertDescription = description; |
1258 | 0 | return 0; |
1259 | 0 | } |
1260 | | |
1261 | | static BOOL tls_match_hostname(const char* pattern, const size_t pattern_length, |
1262 | | const char* hostname) |
1263 | 0 | { |
1264 | 0 | if (strlen(hostname) == pattern_length) |
1265 | 0 | { |
1266 | 0 | if (_strnicmp(hostname, pattern, pattern_length) == 0) |
1267 | 0 | return TRUE; |
1268 | 0 | } |
1269 | | |
1270 | 0 | if ((pattern_length > 2) && (pattern[0] == '*') && (pattern[1] == '.') && |
1271 | 0 | ((strlen(hostname)) >= pattern_length)) |
1272 | 0 | { |
1273 | 0 | const char* check_hostname = &hostname[strlen(hostname) - pattern_length + 1]; |
1274 | |
|
1275 | 0 | if (_strnicmp(check_hostname, &pattern[1], pattern_length - 1) == 0) |
1276 | 0 | { |
1277 | 0 | return TRUE; |
1278 | 0 | } |
1279 | 0 | } |
1280 | | |
1281 | 0 | return FALSE; |
1282 | 0 | } |
1283 | | |
1284 | | static BOOL is_redirected(rdpTls* tls) |
1285 | 0 | { |
1286 | 0 | rdpSettings* settings = tls->settings; |
1287 | |
|
1288 | 0 | if (LB_NOREDIRECT & settings->RedirectionFlags) |
1289 | 0 | return FALSE; |
1290 | | |
1291 | 0 | return settings->RedirectionFlags != 0; |
1292 | 0 | } |
1293 | | |
1294 | | static BOOL is_accepted(rdpTls* tls, const BYTE* pem, size_t length) |
1295 | 0 | { |
1296 | 0 | rdpSettings* settings = tls->settings; |
1297 | 0 | char* AccpetedKey = NULL; |
1298 | 0 | UINT32 AcceptedKeyLength = 0; |
1299 | |
|
1300 | 0 | if (tls->isGatewayTransport) |
1301 | 0 | { |
1302 | 0 | AccpetedKey = settings->GatewayAcceptedCert; |
1303 | 0 | AcceptedKeyLength = settings->GatewayAcceptedCertLength; |
1304 | 0 | } |
1305 | 0 | else if (is_redirected(tls)) |
1306 | 0 | { |
1307 | 0 | AccpetedKey = settings->RedirectionAcceptedCert; |
1308 | 0 | AcceptedKeyLength = settings->RedirectionAcceptedCertLength; |
1309 | 0 | } |
1310 | 0 | else |
1311 | 0 | { |
1312 | 0 | AccpetedKey = settings->AcceptedCert; |
1313 | 0 | AcceptedKeyLength = settings->AcceptedCertLength; |
1314 | 0 | } |
1315 | |
|
1316 | 0 | if (AcceptedKeyLength > 0) |
1317 | 0 | { |
1318 | 0 | if (AcceptedKeyLength == length) |
1319 | 0 | { |
1320 | 0 | if (memcmp(AccpetedKey, pem, AcceptedKeyLength) == 0) |
1321 | 0 | return TRUE; |
1322 | 0 | } |
1323 | 0 | } |
1324 | | |
1325 | 0 | if (tls->isGatewayTransport) |
1326 | 0 | { |
1327 | 0 | free(settings->GatewayAcceptedCert); |
1328 | 0 | settings->GatewayAcceptedCert = NULL; |
1329 | 0 | settings->GatewayAcceptedCertLength = 0; |
1330 | 0 | } |
1331 | 0 | else if (is_redirected(tls)) |
1332 | 0 | { |
1333 | 0 | free(settings->RedirectionAcceptedCert); |
1334 | 0 | settings->RedirectionAcceptedCert = NULL; |
1335 | 0 | settings->RedirectionAcceptedCertLength = 0; |
1336 | 0 | } |
1337 | 0 | else |
1338 | 0 | { |
1339 | 0 | free(settings->AcceptedCert); |
1340 | 0 | settings->AcceptedCert = NULL; |
1341 | 0 | settings->AcceptedCertLength = 0; |
1342 | 0 | } |
1343 | |
|
1344 | 0 | return FALSE; |
1345 | 0 | } |
1346 | | |
1347 | | static BOOL compare_fingerprint(const char* fp, const char* hash, const rdpCertificate* cert, |
1348 | | BOOL separator) |
1349 | 0 | { |
1350 | 0 | BOOL equal = 0; |
1351 | 0 | char* strhash = NULL; |
1352 | |
|
1353 | 0 | WINPR_ASSERT(fp); |
1354 | 0 | WINPR_ASSERT(hash); |
1355 | 0 | WINPR_ASSERT(cert); |
1356 | | |
1357 | 0 | strhash = freerdp_certificate_get_fingerprint_by_hash_ex(cert, hash, separator); |
1358 | 0 | if (!strhash) |
1359 | 0 | return FALSE; |
1360 | | |
1361 | 0 | equal = (_stricmp(strhash, fp) == 0); |
1362 | 0 | free(strhash); |
1363 | 0 | return equal; |
1364 | 0 | } |
1365 | | |
1366 | | static BOOL compare_fingerprint_all(const char* fp, const char* hash, const rdpCertificate* cert) |
1367 | 0 | { |
1368 | 0 | WINPR_ASSERT(fp); |
1369 | 0 | WINPR_ASSERT(hash); |
1370 | 0 | WINPR_ASSERT(cert); |
1371 | 0 | if (compare_fingerprint(fp, hash, cert, FALSE)) |
1372 | 0 | return TRUE; |
1373 | 0 | if (compare_fingerprint(fp, hash, cert, TRUE)) |
1374 | 0 | return TRUE; |
1375 | 0 | return FALSE; |
1376 | 0 | } |
1377 | | |
1378 | | static BOOL is_accepted_fingerprint(const rdpCertificate* cert, |
1379 | | const char* CertificateAcceptedFingerprints) |
1380 | 0 | { |
1381 | 0 | WINPR_ASSERT(cert); |
1382 | | |
1383 | 0 | BOOL rc = FALSE; |
1384 | 0 | if (CertificateAcceptedFingerprints) |
1385 | 0 | { |
1386 | 0 | char* context = NULL; |
1387 | 0 | char* copy = _strdup(CertificateAcceptedFingerprints); |
1388 | 0 | char* cur = strtok_s(copy, ",", &context); |
1389 | 0 | while (cur) |
1390 | 0 | { |
1391 | 0 | char* subcontext = NULL; |
1392 | 0 | const char* h = strtok_s(cur, ":", &subcontext); |
1393 | 0 | const char* fp = NULL; |
1394 | |
|
1395 | 0 | if (!h) |
1396 | 0 | goto next; |
1397 | | |
1398 | 0 | fp = h + strlen(h) + 1; |
1399 | 0 | if (!fp) |
1400 | 0 | goto next; |
1401 | | |
1402 | 0 | if (compare_fingerprint_all(fp, h, cert)) |
1403 | 0 | { |
1404 | 0 | rc = TRUE; |
1405 | 0 | break; |
1406 | 0 | } |
1407 | 0 | next: |
1408 | 0 | cur = strtok_s(NULL, ",", &context); |
1409 | 0 | } |
1410 | 0 | free(copy); |
1411 | 0 | } |
1412 | | |
1413 | 0 | return rc; |
1414 | 0 | } |
1415 | | |
1416 | | static BOOL accept_cert(rdpTls* tls, const BYTE* pem, UINT32 length) |
1417 | 0 | { |
1418 | 0 | WINPR_ASSERT(tls); |
1419 | 0 | FreeRDP_Settings_Keys_String id = FreeRDP_AcceptedCert; |
1420 | 0 | FreeRDP_Settings_Keys_UInt32 lid = FreeRDP_AcceptedCertLength; |
1421 | |
|
1422 | 0 | rdpSettings* settings = tls->settings; |
1423 | |
|
1424 | 0 | if (tls->isGatewayTransport) |
1425 | 0 | { |
1426 | 0 | id = FreeRDP_GatewayAcceptedCert; |
1427 | 0 | lid = FreeRDP_GatewayAcceptedCertLength; |
1428 | 0 | } |
1429 | 0 | else if (is_redirected(tls)) |
1430 | 0 | { |
1431 | 0 | id = FreeRDP_RedirectionAcceptedCert; |
1432 | 0 | lid = FreeRDP_RedirectionAcceptedCertLength; |
1433 | 0 | } |
1434 | |
|
1435 | 0 | if (!freerdp_settings_set_string_len(settings, id, (const char*)pem, length)) |
1436 | 0 | return FALSE; |
1437 | | |
1438 | 0 | return freerdp_settings_set_uint32(settings, lid, length); |
1439 | 0 | } |
1440 | | |
1441 | | static BOOL tls_extract_pem(const rdpCertificate* cert, BYTE** PublicKey, size_t* PublicKeyLength) |
1442 | 0 | { |
1443 | 0 | if (!cert || !PublicKey) |
1444 | 0 | return FALSE; |
1445 | 0 | *PublicKey = (BYTE*)freerdp_certificate_get_pem(cert, PublicKeyLength); |
1446 | 0 | return *PublicKey != NULL; |
1447 | 0 | } |
1448 | | |
1449 | | int tls_verify_certificate(rdpTls* tls, const rdpCertificate* cert, const char* hostname, |
1450 | | UINT16 port) |
1451 | 0 | { |
1452 | 0 | int match = 0; |
1453 | 0 | size_t length = 0; |
1454 | 0 | BOOL certificate_status = 0; |
1455 | 0 | char* common_name = NULL; |
1456 | 0 | size_t common_name_length = 0; |
1457 | 0 | char** dns_names = 0; |
1458 | 0 | size_t dns_names_count = 0; |
1459 | 0 | size_t* dns_names_lengths = NULL; |
1460 | 0 | int verification_status = -1; |
1461 | 0 | BOOL hostname_match = FALSE; |
1462 | 0 | rdpCertificateData* certificate_data = NULL; |
1463 | 0 | BYTE* pemCert = NULL; |
1464 | 0 | DWORD flags = VERIFY_CERT_FLAG_NONE; |
1465 | 0 | freerdp* instance = NULL; |
1466 | |
|
1467 | 0 | WINPR_ASSERT(tls); |
1468 | 0 | WINPR_ASSERT(tls->settings); |
1469 | | |
1470 | 0 | instance = (freerdp*)tls->settings->instance; |
1471 | 0 | WINPR_ASSERT(instance); |
1472 | | |
1473 | 0 | if (freerdp_shall_disconnect_context(instance->context)) |
1474 | 0 | return -1; |
1475 | | |
1476 | 0 | if (!tls_extract_pem(cert, &pemCert, &length)) |
1477 | 0 | goto end; |
1478 | | |
1479 | | /* Check, if we already accepted this key. */ |
1480 | 0 | if (is_accepted(tls, pemCert, length)) |
1481 | 0 | { |
1482 | 0 | verification_status = 1; |
1483 | 0 | goto end; |
1484 | 0 | } |
1485 | | |
1486 | 0 | if (is_accepted_fingerprint(cert, tls->settings->CertificateAcceptedFingerprints)) |
1487 | 0 | { |
1488 | 0 | verification_status = 1; |
1489 | 0 | goto end; |
1490 | 0 | } |
1491 | | |
1492 | 0 | if (tls->isGatewayTransport || is_redirected(tls)) |
1493 | 0 | flags |= VERIFY_CERT_FLAG_LEGACY; |
1494 | |
|
1495 | 0 | if (tls->isGatewayTransport) |
1496 | 0 | flags |= VERIFY_CERT_FLAG_GATEWAY; |
1497 | |
|
1498 | 0 | if (is_redirected(tls)) |
1499 | 0 | flags |= VERIFY_CERT_FLAG_REDIRECT; |
1500 | | |
1501 | | /* Certificate management is done by the application */ |
1502 | 0 | if (tls->settings->ExternalCertificateManagement) |
1503 | 0 | { |
1504 | 0 | if (instance->VerifyX509Certificate) |
1505 | 0 | verification_status = |
1506 | 0 | instance->VerifyX509Certificate(instance, pemCert, length, hostname, port, flags); |
1507 | 0 | else |
1508 | 0 | WLog_ERR(TAG, "No VerifyX509Certificate callback registered!"); |
1509 | |
|
1510 | 0 | if (verification_status > 0) |
1511 | 0 | accept_cert(tls, pemCert, length); |
1512 | 0 | else if (verification_status < 0) |
1513 | 0 | { |
1514 | 0 | WLog_ERR(TAG, "VerifyX509Certificate failed: (length = %" PRIuz ") status: [%d] %s", |
1515 | 0 | length, verification_status, pemCert); |
1516 | 0 | goto end; |
1517 | 0 | } |
1518 | 0 | } |
1519 | | /* ignore certificate verification if user explicitly required it (discouraged) */ |
1520 | 0 | else if (tls->settings->IgnoreCertificate) |
1521 | 0 | verification_status = 1; /* success! */ |
1522 | 0 | else if (!tls->isGatewayTransport && (tls->settings->AuthenticationLevel == 0)) |
1523 | 0 | verification_status = 1; /* success! */ |
1524 | 0 | else |
1525 | 0 | { |
1526 | | /* if user explicitly specified a certificate name, use it instead of the hostname */ |
1527 | 0 | if (!tls->isGatewayTransport && tls->settings->CertificateName) |
1528 | 0 | hostname = tls->settings->CertificateName; |
1529 | | |
1530 | | /* attempt verification using OpenSSL and the ~/.freerdp/certs certificate store */ |
1531 | 0 | certificate_status = freerdp_certificate_verify( |
1532 | 0 | cert, freerdp_certificate_store_get_certs_path(tls->certificate_store)); |
1533 | | /* verify certificate name match */ |
1534 | 0 | certificate_data = freerdp_certificate_data_new(hostname, port, cert); |
1535 | 0 | if (!certificate_data) |
1536 | 0 | goto end; |
1537 | | /* extra common name and alternative names */ |
1538 | 0 | common_name = freerdp_certificate_get_common_name(cert, &common_name_length); |
1539 | 0 | dns_names = freerdp_certificate_get_dns_names(cert, &dns_names_count, &dns_names_lengths); |
1540 | | |
1541 | | /* compare against common name */ |
1542 | |
|
1543 | 0 | if (common_name) |
1544 | 0 | { |
1545 | 0 | if (tls_match_hostname(common_name, common_name_length, hostname)) |
1546 | 0 | hostname_match = TRUE; |
1547 | 0 | } |
1548 | | |
1549 | | /* compare against alternative names */ |
1550 | |
|
1551 | 0 | if (dns_names) |
1552 | 0 | { |
1553 | 0 | for (size_t index = 0; index < dns_names_count; index++) |
1554 | 0 | { |
1555 | 0 | if (tls_match_hostname(dns_names[index], dns_names_lengths[index], hostname)) |
1556 | 0 | { |
1557 | 0 | hostname_match = TRUE; |
1558 | 0 | break; |
1559 | 0 | } |
1560 | 0 | } |
1561 | 0 | } |
1562 | | |
1563 | | /* if the certificate is valid and the certificate name matches, verification succeeds |
1564 | | */ |
1565 | 0 | if (certificate_status && hostname_match) |
1566 | 0 | verification_status = 1; /* success! */ |
1567 | |
|
1568 | 0 | if (!hostname_match) |
1569 | 0 | flags |= VERIFY_CERT_FLAG_MISMATCH; |
1570 | | |
1571 | | /* verification could not succeed with OpenSSL, use known_hosts file and prompt user for |
1572 | | * manual verification */ |
1573 | 0 | if (!certificate_status || !hostname_match) |
1574 | 0 | { |
1575 | 0 | DWORD accept_certificate = 0; |
1576 | 0 | size_t pem_length = 0; |
1577 | 0 | char* issuer = freerdp_certificate_get_issuer(cert); |
1578 | 0 | char* subject = freerdp_certificate_get_subject(cert); |
1579 | 0 | char* pem = freerdp_certificate_get_pem(cert, &pem_length); |
1580 | |
|
1581 | 0 | if (!pem) |
1582 | 0 | goto end; |
1583 | | |
1584 | | /* search for matching entry in known_hosts file */ |
1585 | 0 | match = |
1586 | 0 | freerdp_certificate_store_contains_data(tls->certificate_store, certificate_data); |
1587 | |
|
1588 | 0 | if (match == 1) |
1589 | 0 | { |
1590 | | /* no entry was found in known_hosts file, prompt user for manual verification |
1591 | | */ |
1592 | 0 | if (!hostname_match) |
1593 | 0 | tls_print_certificate_name_mismatch_error(hostname, port, common_name, |
1594 | 0 | dns_names, dns_names_count); |
1595 | |
|
1596 | 0 | { |
1597 | 0 | char* efp = freerdp_certificate_get_fingerprint(cert); |
1598 | 0 | tls_print_new_certificate_warn(tls->certificate_store, hostname, port, efp); |
1599 | 0 | free(efp); |
1600 | 0 | } |
1601 | | |
1602 | | /* Automatically accept certificate on first use */ |
1603 | 0 | if (tls->settings->AutoAcceptCertificate) |
1604 | 0 | { |
1605 | 0 | WLog_INFO(TAG, "No certificate stored, automatically accepting."); |
1606 | 0 | accept_certificate = 1; |
1607 | 0 | } |
1608 | 0 | else if (tls->settings->AutoDenyCertificate) |
1609 | 0 | { |
1610 | 0 | WLog_INFO(TAG, "No certificate stored, automatically denying."); |
1611 | 0 | accept_certificate = 0; |
1612 | 0 | } |
1613 | 0 | else if (instance->VerifyX509Certificate) |
1614 | 0 | { |
1615 | 0 | int rc = instance->VerifyX509Certificate(instance, pemCert, pem_length, |
1616 | 0 | hostname, port, flags); |
1617 | |
|
1618 | 0 | if (rc == 1) |
1619 | 0 | accept_certificate = 1; |
1620 | 0 | else if (rc > 1) |
1621 | 0 | accept_certificate = 2; |
1622 | 0 | else |
1623 | 0 | accept_certificate = 0; |
1624 | 0 | } |
1625 | 0 | else if (instance->VerifyCertificateEx) |
1626 | 0 | { |
1627 | 0 | const BOOL use_pem = freerdp_settings_get_bool( |
1628 | 0 | tls->settings, FreeRDP_CertificateCallbackPreferPEM); |
1629 | 0 | char* fp = NULL; |
1630 | 0 | DWORD cflags = flags; |
1631 | 0 | if (use_pem) |
1632 | 0 | { |
1633 | 0 | cflags |= VERIFY_CERT_FLAG_FP_IS_PEM; |
1634 | 0 | fp = pem; |
1635 | 0 | } |
1636 | 0 | else |
1637 | 0 | fp = freerdp_certificate_get_fingerprint(cert); |
1638 | 0 | accept_certificate = instance->VerifyCertificateEx( |
1639 | 0 | instance, hostname, port, common_name, subject, issuer, fp, cflags); |
1640 | 0 | if (!use_pem) |
1641 | 0 | free(fp); |
1642 | 0 | } |
1643 | | #if defined(WITH_FREERDP_DEPRECATED) |
1644 | | else if (instance->VerifyCertificate) |
1645 | | { |
1646 | | char* fp = freerdp_certificate_get_fingerprint(cert); |
1647 | | |
1648 | | WLog_WARN(TAG, "The VerifyCertificate callback is deprecated, migrate your " |
1649 | | "application to VerifyCertificateEx"); |
1650 | | accept_certificate = instance->VerifyCertificate(instance, common_name, subject, |
1651 | | issuer, fp, !hostname_match); |
1652 | | free(fp); |
1653 | | } |
1654 | | #endif |
1655 | 0 | } |
1656 | 0 | else if (match == -1) |
1657 | 0 | { |
1658 | 0 | rdpCertificateData* stored_data = |
1659 | 0 | freerdp_certificate_store_load_data(tls->certificate_store, hostname, port); |
1660 | | /* entry was found in known_hosts file, but fingerprint does not match. ask user |
1661 | | * to use it */ |
1662 | 0 | { |
1663 | 0 | char* efp = freerdp_certificate_get_fingerprint(cert); |
1664 | 0 | tls_print_certificate_error(tls->certificate_store, stored_data, hostname, port, |
1665 | 0 | efp); |
1666 | 0 | free(efp); |
1667 | 0 | } |
1668 | |
|
1669 | 0 | if (!stored_data) |
1670 | 0 | WLog_WARN(TAG, "Failed to get certificate entry for %s:%" PRIu16 "", hostname, |
1671 | 0 | port); |
1672 | |
|
1673 | 0 | if (tls->settings->AutoDenyCertificate) |
1674 | 0 | { |
1675 | 0 | WLog_INFO(TAG, "No certificate stored, automatically denying."); |
1676 | 0 | accept_certificate = 0; |
1677 | 0 | } |
1678 | 0 | else if (instance->VerifyX509Certificate) |
1679 | 0 | { |
1680 | 0 | const int rc = |
1681 | 0 | instance->VerifyX509Certificate(instance, pemCert, pem_length, hostname, |
1682 | 0 | port, flags | VERIFY_CERT_FLAG_CHANGED); |
1683 | |
|
1684 | 0 | if (rc == 1) |
1685 | 0 | accept_certificate = 1; |
1686 | 0 | else if (rc > 1) |
1687 | 0 | accept_certificate = 2; |
1688 | 0 | else |
1689 | 0 | accept_certificate = 0; |
1690 | 0 | } |
1691 | 0 | else if (instance->VerifyChangedCertificateEx) |
1692 | 0 | { |
1693 | 0 | DWORD cflags = flags | VERIFY_CERT_FLAG_CHANGED; |
1694 | 0 | const char* old_subject = freerdp_certificate_data_get_subject(stored_data); |
1695 | 0 | const char* old_issuer = freerdp_certificate_data_get_issuer(stored_data); |
1696 | 0 | const char* old_fp = freerdp_certificate_data_get_fingerprint(stored_data); |
1697 | 0 | const char* old_pem = freerdp_certificate_data_get_pem(stored_data); |
1698 | 0 | const BOOL fpIsAllocated = |
1699 | 0 | !old_pem || !freerdp_settings_get_bool( |
1700 | 0 | tls->settings, FreeRDP_CertificateCallbackPreferPEM); |
1701 | 0 | char* fp = NULL; |
1702 | 0 | if (!fpIsAllocated) |
1703 | 0 | { |
1704 | 0 | cflags |= VERIFY_CERT_FLAG_FP_IS_PEM; |
1705 | 0 | fp = pem; |
1706 | 0 | old_fp = old_pem; |
1707 | 0 | } |
1708 | 0 | else |
1709 | 0 | { |
1710 | 0 | fp = freerdp_certificate_get_fingerprint(cert); |
1711 | 0 | } |
1712 | 0 | accept_certificate = instance->VerifyChangedCertificateEx( |
1713 | 0 | instance, hostname, port, common_name, subject, issuer, fp, old_subject, |
1714 | 0 | old_issuer, old_fp, cflags); |
1715 | 0 | if (fpIsAllocated) |
1716 | 0 | free(fp); |
1717 | 0 | } |
1718 | | #if defined(WITH_FREERDP_DEPRECATED) |
1719 | | else if (instance->VerifyChangedCertificate) |
1720 | | { |
1721 | | char* fp = freerdp_certificate_get_fingerprint(cert); |
1722 | | const char* old_subject = freerdp_certificate_data_get_subject(stored_data); |
1723 | | const char* old_issuer = freerdp_certificate_data_get_issuer(stored_data); |
1724 | | const char* old_fingerprint = |
1725 | | freerdp_certificate_data_get_fingerprint(stored_data); |
1726 | | |
1727 | | WLog_WARN(TAG, "The VerifyChangedCertificate callback is deprecated, migrate " |
1728 | | "your application to VerifyChangedCertificateEx"); |
1729 | | accept_certificate = instance->VerifyChangedCertificate( |
1730 | | instance, common_name, subject, issuer, fp, old_subject, old_issuer, |
1731 | | old_fingerprint); |
1732 | | free(fp); |
1733 | | } |
1734 | | #endif |
1735 | |
|
1736 | 0 | freerdp_certificate_data_free(stored_data); |
1737 | 0 | } |
1738 | 0 | else if (match == 0) |
1739 | 0 | accept_certificate = 2; /* success! */ |
1740 | | |
1741 | | /* Save certificate or do a simple accept / reject */ |
1742 | 0 | switch (accept_certificate) |
1743 | 0 | { |
1744 | 0 | case 1: |
1745 | | |
1746 | | /* user accepted certificate, add entry in known_hosts file */ |
1747 | 0 | verification_status = freerdp_certificate_store_save_data( |
1748 | 0 | tls->certificate_store, certificate_data) |
1749 | 0 | ? 1 |
1750 | 0 | : -1; |
1751 | 0 | break; |
1752 | | |
1753 | 0 | case 2: |
1754 | | /* user did accept temporaty, do not add to known hosts file */ |
1755 | 0 | verification_status = 1; |
1756 | 0 | break; |
1757 | | |
1758 | 0 | default: |
1759 | | /* user did not accept, abort and do not add entry in known_hosts file */ |
1760 | 0 | verification_status = -1; /* failure! */ |
1761 | 0 | break; |
1762 | 0 | } |
1763 | | |
1764 | 0 | free(issuer); |
1765 | 0 | free(subject); |
1766 | 0 | free(pem); |
1767 | 0 | } |
1768 | | |
1769 | 0 | if (verification_status > 0) |
1770 | 0 | accept_cert(tls, pemCert, length); |
1771 | 0 | } |
1772 | | |
1773 | 0 | end: |
1774 | 0 | freerdp_certificate_data_free(certificate_data); |
1775 | 0 | free(common_name); |
1776 | 0 | freerdp_certificate_free_dns_names(dns_names_count, dns_names_lengths, dns_names); |
1777 | 0 | free(pemCert); |
1778 | 0 | return verification_status; |
1779 | 0 | } |
1780 | | |
1781 | | void tls_print_new_certificate_warn(rdpCertificateStore* store, const char* hostname, UINT16 port, |
1782 | | const char* fingerprint) |
1783 | 0 | { |
1784 | 0 | char* path = freerdp_certificate_store_get_cert_path(store, hostname, port); |
1785 | |
|
1786 | 0 | WLog_ERR(TAG, "The host key for %s:%" PRIu16 " has changed", hostname, port); |
1787 | 0 | WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
1788 | 0 | WLog_ERR(TAG, "@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @"); |
1789 | 0 | WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
1790 | 0 | WLog_ERR(TAG, "IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!"); |
1791 | 0 | WLog_ERR(TAG, "Someone could be eavesdropping on you right now (man-in-the-middle attack)!"); |
1792 | 0 | WLog_ERR(TAG, "It is also possible that a host key has just been changed."); |
1793 | 0 | WLog_ERR(TAG, "The fingerprint for the host key sent by the remote host is %s", fingerprint); |
1794 | 0 | WLog_ERR(TAG, "Please contact your system administrator."); |
1795 | 0 | WLog_ERR(TAG, "Add correct host key in %s to get rid of this message.", path); |
1796 | 0 | WLog_ERR(TAG, "Host key for %s has changed and you have requested strict checking.", hostname); |
1797 | 0 | WLog_ERR(TAG, "Host key verification failed."); |
1798 | |
|
1799 | 0 | free(path); |
1800 | 0 | } |
1801 | | |
1802 | | void tls_print_certificate_error(rdpCertificateStore* store, rdpCertificateData* stored_data, |
1803 | | const char* hostname, UINT16 port, const char* fingerprint) |
1804 | 0 | { |
1805 | 0 | char* path = freerdp_certificate_store_get_cert_path(store, hostname, port); |
1806 | |
|
1807 | 0 | WLog_ERR(TAG, "New host key for %s:%" PRIu16, hostname, port); |
1808 | 0 | WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
1809 | 0 | WLog_ERR(TAG, "@ WARNING: NEW HOST IDENTIFICATION! @"); |
1810 | 0 | WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
1811 | |
|
1812 | 0 | WLog_ERR(TAG, "The fingerprint for the host key sent by the remote host is %s", fingerprint); |
1813 | 0 | WLog_ERR(TAG, "Please contact your system administrator."); |
1814 | 0 | WLog_ERR(TAG, "Add correct host key in %s to get rid of this message.", path); |
1815 | |
|
1816 | 0 | free(path); |
1817 | 0 | } |
1818 | | |
1819 | | void tls_print_certificate_name_mismatch_error(const char* hostname, UINT16 port, |
1820 | | const char* common_name, char** alt_names, |
1821 | | size_t alt_names_count) |
1822 | 0 | { |
1823 | 0 | WINPR_ASSERT(NULL != hostname); |
1824 | 0 | WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
1825 | 0 | WLog_ERR(TAG, "@ WARNING: CERTIFICATE NAME MISMATCH! @"); |
1826 | 0 | WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
1827 | 0 | WLog_ERR(TAG, "The hostname used for this connection (%s:%" PRIu16 ") ", hostname, port); |
1828 | 0 | WLog_ERR(TAG, "does not match %s given in the certificate:", |
1829 | 0 | alt_names_count < 1 ? "the name" : "any of the names"); |
1830 | 0 | WLog_ERR(TAG, "Common Name (CN):"); |
1831 | 0 | WLog_ERR(TAG, "\t%s", common_name ? common_name : "no CN found in certificate"); |
1832 | |
|
1833 | 0 | if (alt_names_count > 0) |
1834 | 0 | { |
1835 | 0 | WINPR_ASSERT(NULL != alt_names); |
1836 | 0 | WLog_ERR(TAG, "Alternative names:"); |
1837 | |
|
1838 | 0 | for (size_t index = 0; index < alt_names_count; index++) |
1839 | 0 | { |
1840 | 0 | WINPR_ASSERT(alt_names[index]); |
1841 | 0 | WLog_ERR(TAG, "\t %s", alt_names[index]); |
1842 | 0 | } |
1843 | 0 | } |
1844 | | |
1845 | 0 | WLog_ERR(TAG, "A valid certificate for the wrong name should NOT be trusted!"); |
1846 | 0 | } |
1847 | | |
1848 | | rdpTls* freerdp_tls_new(rdpSettings* settings) |
1849 | 0 | { |
1850 | 0 | rdpTls* tls = NULL; |
1851 | 0 | tls = (rdpTls*)calloc(1, sizeof(rdpTls)); |
1852 | |
|
1853 | 0 | if (!tls) |
1854 | 0 | return NULL; |
1855 | | |
1856 | 0 | tls->settings = settings; |
1857 | |
|
1858 | 0 | if (!settings->ServerMode) |
1859 | 0 | { |
1860 | 0 | tls->certificate_store = freerdp_certificate_store_new(settings); |
1861 | |
|
1862 | 0 | if (!tls->certificate_store) |
1863 | 0 | goto out_free; |
1864 | 0 | } |
1865 | | |
1866 | 0 | tls->alertLevel = TLS_ALERT_LEVEL_WARNING; |
1867 | 0 | tls->alertDescription = TLS_ALERT_DESCRIPTION_CLOSE_NOTIFY; |
1868 | 0 | return tls; |
1869 | 0 | out_free: |
1870 | 0 | free(tls); |
1871 | 0 | return NULL; |
1872 | 0 | } |
1873 | | |
1874 | | void freerdp_tls_free(rdpTls* tls) |
1875 | 0 | { |
1876 | 0 | if (!tls) |
1877 | 0 | return; |
1878 | | |
1879 | 0 | tls_reset(tls); |
1880 | |
|
1881 | 0 | if (tls->certificate_store) |
1882 | 0 | { |
1883 | 0 | freerdp_certificate_store_free(tls->certificate_store); |
1884 | 0 | tls->certificate_store = NULL; |
1885 | 0 | } |
1886 | |
|
1887 | 0 | free(tls); |
1888 | 0 | } |