/src/FreeRDP/libfreerdp/crypto/tls.c
Line | Count | Source |
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/winpr.h> |
32 | | #include <winpr/string.h> |
33 | | #include <winpr/sspi.h> |
34 | | #include <winpr/ssl.h> |
35 | | #include <winpr/json.h> |
36 | | |
37 | | #include <winpr/stream.h> |
38 | | #include <freerdp/utils/ringbuffer.h> |
39 | | |
40 | | #include <freerdp/crypto/certificate.h> |
41 | | #include <freerdp/crypto/certificate_data.h> |
42 | | #include <freerdp/utils/helpers.h> |
43 | | |
44 | | #include <freerdp/log.h> |
45 | | #include "../crypto/tls.h" |
46 | | #include "../core/tcp.h" |
47 | | |
48 | | #include "opensslcompat.h" |
49 | | #include "certificate.h" |
50 | | #include "privatekey.h" |
51 | | #include "../core/utils.h" |
52 | | |
53 | | #ifdef WINPR_HAVE_POLL_H |
54 | | #include <poll.h> |
55 | | #endif |
56 | | |
57 | | #ifdef FREERDP_HAVE_VALGRIND_MEMCHECK_H |
58 | | #include <valgrind/memcheck.h> |
59 | | #endif |
60 | | |
61 | 0 | #define TAG FREERDP_TAG("crypto") |
62 | | |
63 | | /** |
64 | | * Earlier Microsoft iOS RDP clients have sent a null or even double null |
65 | | * terminated hostname in the SNI TLS extension. |
66 | | * If the length indicator does not equal the hostname strlen OpenSSL |
67 | | * will abort (see openssl:ssl/t1_lib.c). |
68 | | * Here is a tcpdump segment of Microsoft Remote Desktop Client Version |
69 | | * 8.1.7 running on an iPhone 4 with iOS 7.1.2 showing the transmitted |
70 | | * SNI hostname TLV blob when connection to server "abcd": |
71 | | * 00 name_type 0x00 (host_name) |
72 | | * 00 06 length_in_bytes 0x0006 |
73 | | * 61 62 63 64 00 00 host_name "abcd\0\0" |
74 | | * |
75 | | * Currently the only (runtime) workaround is setting an openssl tls |
76 | | * extension debug callback that sets the SSL context's servername_done |
77 | | * to 1 which effectively disables the parsing of that extension type. |
78 | | * |
79 | | * Nowadays this workaround is not required anymore but still can be |
80 | | * activated by adding the following define: |
81 | | * |
82 | | * #define MICROSOFT_IOS_SNI_BUG |
83 | | */ |
84 | | |
85 | | typedef struct |
86 | | { |
87 | | SSL* ssl; |
88 | | CRITICAL_SECTION lock; |
89 | | #ifdef WITH_TLS_DATA_LIMIT |
90 | | UINT64 rxBytes; /* plaintext bytes read since the last rekey */ |
91 | | UINT64 txBytes; /* plaintext bytes written since the last rekey */ |
92 | | BOOL rekeyAvailable; /* FALSE once we learn the connection is not TLS 1.3 */ |
93 | | BOOL cutPending; /* TRUE once a TLS 1.3 rekey failed; tear the connection down */ |
94 | | #endif |
95 | | } BIO_RDP_TLS; |
96 | | |
97 | | #ifdef WITH_TLS_DATA_LIMIT |
98 | | /* Per-direction plaintext byte threshold before refreshing the TLS traffic keys, |
99 | | * to stay within AEAD cryptographic limits. RFC 8446 (TLS 1.3) ยง5.5 bounds |
100 | | * AES-GCM at 2^24.5 records (2^14 B) (~362 GiB). OpenSSL does not enforce this itself |
101 | | * (https://github.com/openssl/openssl/issues/23566). On TLS 1.3 the keys are |
102 | | * rekeyed in place (SSL_key_update); on other protocols a rekey is not possible. */ |
103 | | static const UINT64 TLS_DATA_LIMIT_BYTES = 388736063996ULL; |
104 | | #endif |
105 | | |
106 | | static INIT_ONCE g_BioMethodsOnce = INIT_ONCE_STATIC_INIT; |
107 | | static BIO_METHOD* g_BioMethods = nullptr; |
108 | | |
109 | | static INIT_ONCE secrets_file_idx_once = INIT_ONCE_STATIC_INIT; |
110 | | static int secrets_file_idx = -1; |
111 | | |
112 | | WINPR_ATTR_NODISCARD |
113 | | static int tls_verify_certificate(rdpTls* tls, const rdpCertificate* cert, const char* hostname, |
114 | | UINT16 port); |
115 | | |
116 | | static void tls_print_certificate_name_mismatch_error(const rdpCertificate* cert, |
117 | | const char* hostname, UINT16 port); |
118 | | |
119 | | static void tls_print_new_certificate_warn(rdpCertificateStore* store, const char* hostname, |
120 | | UINT16 port, const char* type, const char* fingerprint); |
121 | | |
122 | | static void tls_print_certificate_error(rdpCertificateStore* store, rdpCertificateData* stored_data, |
123 | | const char* hostname, UINT16 port, const char* fingerprint); |
124 | | |
125 | | static void free_tls_public_key(rdpTls* tls) |
126 | 0 | { |
127 | 0 | WINPR_ASSERT(tls); |
128 | 0 | free(tls->PublicKey); |
129 | 0 | tls->PublicKey = nullptr; |
130 | 0 | tls->PublicKeyLength = 0; |
131 | 0 | } |
132 | | |
133 | | static void free_tls_bindings(SecPkgContext_Bindings* bindings) |
134 | 0 | { |
135 | 0 | if (!bindings) |
136 | 0 | return; |
137 | | |
138 | 0 | free(bindings->Bindings); |
139 | 0 | free(bindings); |
140 | 0 | } |
141 | | |
142 | | #ifdef WITH_TLS_DATA_LIMIT |
143 | | /* Must be called with tls->lock held, after a successful SSL_read/SSL_write of |
144 | | * `count` bytes in the given direction. Once a per-direction byte threshold is |
145 | | * crossed, refreshes the TLS 1.3 traffic keys in place; on a non-TLS-1.3 |
146 | | * connection a rekey is not possible so it is disabled (the connection is kept). |
147 | | * If a TLS 1.3 rekey fails, flags the connection to be cut. */ |
148 | | static void bio_rdp_tls_account(BIO_RDP_TLS* tls, BOOL read, int count) |
149 | | { |
150 | | UINT64* counter = read ? &tls->rxBytes : &tls->txBytes; |
151 | | |
152 | | if (!tls->rekeyAvailable || tls->cutPending) |
153 | | return; |
154 | | |
155 | | *counter += (UINT64)count; |
156 | | if (*counter < TLS_DATA_LIMIT_BYTES) |
157 | | return; |
158 | | |
159 | | if (SSL_version(tls->ssl) != TLS1_3_VERSION) |
160 | | { |
161 | | WLog_DBG(TAG, "TLS data limit reached but connection is not TLS 1.3; " |
162 | | "rekey unavailable, keeping connection"); |
163 | | tls->rekeyAvailable = FALSE; |
164 | | return; |
165 | | } |
166 | | |
167 | | if (SSL_key_update(tls->ssl, SSL_KEY_UPDATE_REQUESTED) != 1) |
168 | | { |
169 | | WLog_ERR(TAG, "SSL_key_update failed at TLS data limit; cutting connection"); |
170 | | tls->cutPending = TRUE; |
171 | | return; |
172 | | } |
173 | | |
174 | | WLog_INFO(TAG, "TLS 1.3 key update requested at data limit (%" PRIu64 " bytes %s)", *counter, |
175 | | read ? "read" : "written"); |
176 | | *counter = 0; |
177 | | } |
178 | | |
179 | | static void bio_rdp_tls_read_account(BIO_RDP_TLS* tls, int count) |
180 | | { |
181 | | bio_rdp_tls_account(tls, TRUE, count); |
182 | | } |
183 | | |
184 | | static void bio_rdp_tls_write_account(BIO_RDP_TLS* tls, int count) |
185 | | { |
186 | | bio_rdp_tls_account(tls, FALSE, count); |
187 | | } |
188 | | #endif |
189 | | |
190 | | WINPR_ATTR_NODISCARD |
191 | | static int bio_rdp_tls_write(BIO* bio, const char* buf, int size) |
192 | 0 | { |
193 | 0 | int error = 0; |
194 | 0 | int status = 0; |
195 | 0 | BIO_RDP_TLS* tls = (BIO_RDP_TLS*)BIO_get_data(bio); |
196 | |
|
197 | 0 | if (!buf || !tls) |
198 | 0 | return 0; |
199 | | |
200 | | #ifdef WITH_TLS_DATA_LIMIT |
201 | | if (tls->cutPending) |
202 | | { |
203 | | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
204 | | return -1; |
205 | | } |
206 | | #endif |
207 | | |
208 | 0 | BIO_clear_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_READ | BIO_FLAGS_IO_SPECIAL); |
209 | 0 | EnterCriticalSection(&tls->lock); |
210 | 0 | status = SSL_write(tls->ssl, buf, size); |
211 | 0 | error = SSL_get_error(tls->ssl, status); |
212 | | #ifdef WITH_TLS_DATA_LIMIT |
213 | | if (status > 0) |
214 | | bio_rdp_tls_write_account(tls, status); |
215 | | #endif |
216 | 0 | LeaveCriticalSection(&tls->lock); |
217 | |
|
218 | 0 | if (status <= 0) |
219 | 0 | { |
220 | 0 | switch (error) |
221 | 0 | { |
222 | 0 | case SSL_ERROR_NONE: |
223 | 0 | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
224 | 0 | break; |
225 | | |
226 | 0 | case SSL_ERROR_WANT_WRITE: |
227 | 0 | BIO_set_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY); |
228 | 0 | break; |
229 | | |
230 | 0 | case SSL_ERROR_WANT_READ: |
231 | 0 | BIO_set_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY); |
232 | 0 | break; |
233 | | |
234 | 0 | case SSL_ERROR_WANT_X509_LOOKUP: |
235 | 0 | BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL); |
236 | 0 | BIO_set_retry_reason(bio, BIO_RR_SSL_X509_LOOKUP); |
237 | 0 | break; |
238 | | |
239 | 0 | case SSL_ERROR_WANT_CONNECT: |
240 | 0 | BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL); |
241 | 0 | BIO_set_retry_reason(bio, BIO_RR_CONNECT); |
242 | 0 | break; |
243 | | |
244 | 0 | case SSL_ERROR_SYSCALL: |
245 | 0 | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
246 | 0 | break; |
247 | | |
248 | 0 | case SSL_ERROR_SSL: |
249 | 0 | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
250 | 0 | break; |
251 | 0 | default: |
252 | 0 | break; |
253 | 0 | } |
254 | 0 | } |
255 | | |
256 | 0 | return status; |
257 | 0 | } |
258 | | |
259 | | WINPR_ATTR_NODISCARD |
260 | | static int bio_rdp_tls_read(BIO* bio, char* buf, int size) |
261 | 0 | { |
262 | 0 | int error = 0; |
263 | 0 | int status = 0; |
264 | 0 | BIO_RDP_TLS* tls = (BIO_RDP_TLS*)BIO_get_data(bio); |
265 | |
|
266 | 0 | if (!buf || !tls) |
267 | 0 | return 0; |
268 | | |
269 | | #ifdef WITH_TLS_DATA_LIMIT |
270 | | if (tls->cutPending) |
271 | | { |
272 | | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
273 | | return -1; |
274 | | } |
275 | | #endif |
276 | | |
277 | 0 | BIO_clear_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_READ | BIO_FLAGS_IO_SPECIAL); |
278 | 0 | EnterCriticalSection(&tls->lock); |
279 | 0 | status = SSL_read(tls->ssl, buf, size); |
280 | 0 | error = SSL_get_error(tls->ssl, status); |
281 | | #ifdef WITH_TLS_DATA_LIMIT |
282 | | if (status > 0) |
283 | | bio_rdp_tls_read_account(tls, status); |
284 | | #endif |
285 | 0 | LeaveCriticalSection(&tls->lock); |
286 | |
|
287 | 0 | if (status <= 0) |
288 | 0 | { |
289 | |
|
290 | 0 | switch (error) |
291 | 0 | { |
292 | 0 | case SSL_ERROR_NONE: |
293 | 0 | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
294 | 0 | break; |
295 | | |
296 | 0 | case SSL_ERROR_WANT_READ: |
297 | 0 | BIO_set_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY); |
298 | 0 | break; |
299 | | |
300 | 0 | case SSL_ERROR_WANT_WRITE: |
301 | 0 | BIO_set_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY); |
302 | 0 | break; |
303 | | |
304 | 0 | case SSL_ERROR_WANT_X509_LOOKUP: |
305 | 0 | BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL); |
306 | 0 | BIO_set_retry_reason(bio, BIO_RR_SSL_X509_LOOKUP); |
307 | 0 | break; |
308 | | |
309 | 0 | case SSL_ERROR_WANT_ACCEPT: |
310 | 0 | BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL); |
311 | 0 | BIO_set_retry_reason(bio, BIO_RR_ACCEPT); |
312 | 0 | break; |
313 | | |
314 | 0 | case SSL_ERROR_WANT_CONNECT: |
315 | 0 | BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL); |
316 | 0 | BIO_set_retry_reason(bio, BIO_RR_CONNECT); |
317 | 0 | break; |
318 | | |
319 | 0 | case SSL_ERROR_SSL: |
320 | 0 | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
321 | 0 | break; |
322 | | |
323 | 0 | case SSL_ERROR_ZERO_RETURN: |
324 | 0 | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
325 | 0 | break; |
326 | | |
327 | 0 | case SSL_ERROR_SYSCALL: |
328 | 0 | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
329 | 0 | break; |
330 | 0 | default: |
331 | 0 | break; |
332 | 0 | } |
333 | 0 | } |
334 | | |
335 | | #ifdef FREERDP_HAVE_VALGRIND_MEMCHECK_H |
336 | | |
337 | | if (status > 0) |
338 | | { |
339 | | VALGRIND_MAKE_MEM_DEFINED(buf, status); |
340 | | } |
341 | | |
342 | | #endif |
343 | 0 | return status; |
344 | 0 | } |
345 | | |
346 | | WINPR_ATTR_NODISCARD |
347 | | static int bio_rdp_tls_puts(BIO* bio, const char* str) |
348 | 0 | { |
349 | 0 | if (!str) |
350 | 0 | return 0; |
351 | | |
352 | 0 | const size_t max = (INT_MAX > SIZE_MAX) ? SIZE_MAX : INT_MAX; |
353 | 0 | const size_t size = strnlen(str, max); |
354 | 0 | if (size >= max) |
355 | 0 | return -1; |
356 | 0 | ERR_clear_error(); |
357 | 0 | return BIO_write(bio, str, (int)size); |
358 | 0 | } |
359 | | |
360 | | WINPR_ATTR_NODISCARD |
361 | | static int bio_rdp_tls_gets(WINPR_ATTR_UNUSED BIO* bio, WINPR_ATTR_UNUSED char* str, |
362 | | WINPR_ATTR_UNUSED int size) |
363 | 0 | { |
364 | 0 | return 1; |
365 | 0 | } |
366 | | |
367 | | WINPR_ATTR_NODISCARD |
368 | | static long bio_rdp_tls_ctrl(BIO* bio, int cmd, long num, void* ptr) |
369 | 0 | { |
370 | 0 | BIO* ssl_rbio = nullptr; |
371 | 0 | BIO* ssl_wbio = nullptr; |
372 | 0 | BIO* next_bio = nullptr; |
373 | 0 | long status = -1; |
374 | 0 | BIO_RDP_TLS* tls = (BIO_RDP_TLS*)BIO_get_data(bio); |
375 | |
|
376 | 0 | if (!tls) |
377 | 0 | return 0; |
378 | | |
379 | 0 | if (!tls->ssl && (cmd != BIO_C_SET_SSL)) |
380 | 0 | return 0; |
381 | | |
382 | 0 | next_bio = BIO_next(bio); |
383 | 0 | ssl_rbio = tls->ssl ? SSL_get_rbio(tls->ssl) : nullptr; |
384 | 0 | ssl_wbio = tls->ssl ? SSL_get_wbio(tls->ssl) : nullptr; |
385 | |
|
386 | 0 | switch (cmd) |
387 | 0 | { |
388 | 0 | case BIO_CTRL_RESET: |
389 | 0 | SSL_shutdown(tls->ssl); |
390 | |
|
391 | 0 | if (SSL_in_connect_init(tls->ssl)) |
392 | 0 | SSL_set_connect_state(tls->ssl); |
393 | 0 | else if (SSL_in_accept_init(tls->ssl)) |
394 | 0 | SSL_set_accept_state(tls->ssl); |
395 | |
|
396 | 0 | SSL_clear(tls->ssl); |
397 | |
|
398 | 0 | if (next_bio) |
399 | 0 | status = BIO_ctrl(next_bio, cmd, num, ptr); |
400 | 0 | else if (ssl_rbio) |
401 | 0 | status = BIO_ctrl(ssl_rbio, cmd, num, ptr); |
402 | 0 | else |
403 | 0 | status = 1; |
404 | |
|
405 | 0 | break; |
406 | | |
407 | 0 | case BIO_C_GET_FD: |
408 | 0 | status = BIO_ctrl(ssl_rbio, cmd, num, ptr); |
409 | 0 | break; |
410 | | |
411 | 0 | case BIO_CTRL_INFO: |
412 | 0 | status = 0; |
413 | 0 | break; |
414 | | |
415 | 0 | case BIO_CTRL_SET_CALLBACK: |
416 | 0 | status = 0; |
417 | 0 | break; |
418 | | |
419 | 0 | case BIO_CTRL_GET_CALLBACK: |
420 | | /* The OpenSSL API is horrible here: |
421 | | * we get a function pointer returned and have to cast it to ULONG_PTR |
422 | | * to return the value to the caller. |
423 | | * |
424 | | * This, of course, is something compilers warn about. So silence it by casting */ |
425 | 0 | { |
426 | 0 | void* vptr = WINPR_FUNC_PTR_CAST(SSL_get_info_callback(tls->ssl), void*); |
427 | 0 | *((void**)ptr) = vptr; |
428 | 0 | status = 1; |
429 | 0 | } |
430 | 0 | break; |
431 | | |
432 | 0 | case BIO_C_SSL_MODE: |
433 | 0 | if (num) |
434 | 0 | SSL_set_connect_state(tls->ssl); |
435 | 0 | else |
436 | 0 | SSL_set_accept_state(tls->ssl); |
437 | |
|
438 | 0 | status = 1; |
439 | 0 | break; |
440 | | |
441 | 0 | case BIO_CTRL_GET_CLOSE: |
442 | 0 | status = BIO_get_shutdown(bio); |
443 | 0 | break; |
444 | | |
445 | 0 | case BIO_CTRL_SET_CLOSE: |
446 | 0 | BIO_set_shutdown(bio, (int)num); |
447 | 0 | status = 1; |
448 | 0 | break; |
449 | | |
450 | 0 | case BIO_CTRL_WPENDING: |
451 | 0 | status = BIO_ctrl(ssl_wbio, cmd, num, ptr); |
452 | 0 | break; |
453 | | |
454 | 0 | case BIO_CTRL_PENDING: |
455 | 0 | status = SSL_pending(tls->ssl); |
456 | |
|
457 | 0 | if (status == 0) |
458 | 0 | status = BIO_pending(ssl_rbio); |
459 | |
|
460 | 0 | break; |
461 | | |
462 | 0 | case BIO_CTRL_FLUSH: |
463 | 0 | BIO_clear_retry_flags(bio); |
464 | 0 | status = BIO_ctrl(ssl_wbio, cmd, num, ptr); |
465 | 0 | if (status != 1) |
466 | 0 | WLog_DBG(TAG, "BIO_ctrl returned %ld", status); |
467 | 0 | BIO_copy_next_retry(bio); |
468 | 0 | status = 1; |
469 | 0 | break; |
470 | | |
471 | 0 | case BIO_CTRL_PUSH: |
472 | 0 | if (next_bio && (next_bio != ssl_rbio)) |
473 | 0 | { |
474 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
475 | | (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL) |
476 | | SSL_set_bio(tls->ssl, next_bio, next_bio); |
477 | | CRYPTO_add(&(bio->next_bio->references), 1, CRYPTO_LOCK_BIO); |
478 | | #else |
479 | | /* |
480 | | * We are going to pass ownership of next to the SSL object...but |
481 | | * we don't own a reference to pass yet - so up ref |
482 | | */ |
483 | 0 | BIO_up_ref(next_bio); |
484 | 0 | SSL_set_bio(tls->ssl, next_bio, next_bio); |
485 | 0 | #endif |
486 | 0 | } |
487 | |
|
488 | 0 | status = 1; |
489 | 0 | break; |
490 | | |
491 | 0 | case BIO_CTRL_POP: |
492 | | |
493 | | /* Only detach if we are the BIO explicitly being popped */ |
494 | 0 | if (bio == ptr) |
495 | 0 | { |
496 | 0 | if (ssl_rbio != ssl_wbio) |
497 | 0 | BIO_free_all(ssl_wbio); |
498 | |
|
499 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
500 | | (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL) |
501 | | |
502 | | if (next_bio) |
503 | | CRYPTO_add(&(bio->next_bio->references), -1, CRYPTO_LOCK_BIO); |
504 | | |
505 | | tls->ssl->wbio = tls->ssl->rbio = nullptr; |
506 | | #else |
507 | | /* OpenSSL 1.1: This will also clear the reference we obtained during push */ |
508 | 0 | SSL_set_bio(tls->ssl, nullptr, nullptr); |
509 | 0 | #endif |
510 | 0 | } |
511 | |
|
512 | 0 | status = 1; |
513 | 0 | break; |
514 | | |
515 | 0 | case BIO_C_GET_SSL: |
516 | 0 | if (ptr) |
517 | 0 | { |
518 | 0 | *((SSL**)ptr) = tls->ssl; |
519 | 0 | status = 1; |
520 | 0 | } |
521 | |
|
522 | 0 | break; |
523 | | |
524 | 0 | case BIO_C_SET_SSL: |
525 | 0 | BIO_set_shutdown(bio, (int)num); |
526 | |
|
527 | 0 | if (ptr) |
528 | 0 | { |
529 | 0 | tls->ssl = (SSL*)ptr; |
530 | 0 | ssl_rbio = SSL_get_rbio(tls->ssl); |
531 | 0 | } |
532 | |
|
533 | 0 | if (ssl_rbio) |
534 | 0 | { |
535 | 0 | if (next_bio) |
536 | 0 | BIO_push(ssl_rbio, next_bio); |
537 | |
|
538 | 0 | BIO_set_next(bio, ssl_rbio); |
539 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
540 | | (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL) |
541 | | CRYPTO_add(&(ssl_rbio->references), 1, CRYPTO_LOCK_BIO); |
542 | | #else |
543 | 0 | BIO_up_ref(ssl_rbio); |
544 | 0 | #endif |
545 | 0 | } |
546 | |
|
547 | 0 | BIO_set_init(bio, 1); |
548 | 0 | status = 1; |
549 | 0 | break; |
550 | | |
551 | 0 | case BIO_C_DO_STATE_MACHINE: |
552 | 0 | BIO_clear_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_WRITE | BIO_FLAGS_IO_SPECIAL); |
553 | 0 | BIO_set_retry_reason(bio, 0); |
554 | 0 | status = SSL_do_handshake(tls->ssl); |
555 | |
|
556 | 0 | if (status <= 0) |
557 | 0 | { |
558 | 0 | const int err = (status < INT32_MIN) ? INT32_MIN : (int)status; |
559 | 0 | switch (SSL_get_error(tls->ssl, err)) |
560 | 0 | { |
561 | 0 | case SSL_ERROR_WANT_READ: |
562 | 0 | BIO_set_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY); |
563 | 0 | break; |
564 | | |
565 | 0 | case SSL_ERROR_WANT_WRITE: |
566 | 0 | BIO_set_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY); |
567 | 0 | break; |
568 | | |
569 | 0 | case SSL_ERROR_WANT_CONNECT: |
570 | 0 | BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY); |
571 | 0 | BIO_set_retry_reason(bio, BIO_get_retry_reason(next_bio)); |
572 | 0 | break; |
573 | | |
574 | 0 | default: |
575 | 0 | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
576 | 0 | break; |
577 | 0 | } |
578 | 0 | } |
579 | | |
580 | 0 | break; |
581 | | |
582 | 0 | default: |
583 | 0 | status = BIO_ctrl(ssl_rbio, cmd, num, ptr); |
584 | 0 | break; |
585 | 0 | } |
586 | | |
587 | 0 | return status; |
588 | 0 | } |
589 | | |
590 | | WINPR_ATTR_NODISCARD |
591 | | static int bio_rdp_tls_new(BIO* bio) |
592 | 0 | { |
593 | 0 | BIO_RDP_TLS* tls = nullptr; |
594 | 0 | BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
595 | |
|
596 | 0 | if (!(tls = calloc(1, sizeof(BIO_RDP_TLS)))) |
597 | 0 | return 0; |
598 | | |
599 | 0 | if (!InitializeCriticalSectionAndSpinCount(&tls->lock, 4000)) |
600 | 0 | { |
601 | 0 | free(tls); |
602 | 0 | return -1; |
603 | 0 | } |
604 | | |
605 | | #ifdef WITH_TLS_DATA_LIMIT |
606 | | /* counters/cutPending zeroed by calloc; rekey is assumed possible until we |
607 | | * observe a non-TLS-1.3 connection at the data limit. */ |
608 | | tls->rekeyAvailable = TRUE; |
609 | | #endif |
610 | | |
611 | 0 | BIO_set_data(bio, (void*)tls); |
612 | 0 | return 1; |
613 | 0 | } |
614 | | |
615 | | WINPR_ATTR_NODISCARD |
616 | | static int bio_rdp_tls_free(BIO* bio) |
617 | 0 | { |
618 | 0 | BIO_RDP_TLS* tls = nullptr; |
619 | |
|
620 | 0 | if (!bio) |
621 | 0 | return 0; |
622 | | |
623 | 0 | tls = (BIO_RDP_TLS*)BIO_get_data(bio); |
624 | |
|
625 | 0 | if (!tls) |
626 | 0 | return 0; |
627 | | |
628 | 0 | BIO_set_data(bio, nullptr); |
629 | 0 | if (BIO_get_shutdown(bio)) |
630 | 0 | { |
631 | 0 | if (BIO_get_init(bio) && tls->ssl) |
632 | 0 | { |
633 | 0 | SSL_shutdown(tls->ssl); |
634 | 0 | SSL_free(tls->ssl); |
635 | 0 | } |
636 | |
|
637 | 0 | BIO_set_init(bio, 0); |
638 | 0 | BIO_set_flags(bio, 0); |
639 | 0 | } |
640 | |
|
641 | 0 | DeleteCriticalSection(&tls->lock); |
642 | 0 | free(tls); |
643 | |
|
644 | 0 | return 1; |
645 | 0 | } |
646 | | |
647 | | WINPR_ATTR_NODISCARD |
648 | | static long bio_rdp_tls_callback_ctrl(BIO* bio, int cmd, bio_info_cb* fp) |
649 | 0 | { |
650 | 0 | long status = 0; |
651 | |
|
652 | 0 | if (!bio) |
653 | 0 | return 0; |
654 | | |
655 | 0 | BIO_RDP_TLS* tls = (BIO_RDP_TLS*)BIO_get_data(bio); |
656 | |
|
657 | 0 | if (!tls) |
658 | 0 | return 0; |
659 | | |
660 | 0 | switch (cmd) |
661 | 0 | { |
662 | 0 | case BIO_CTRL_SET_CALLBACK: |
663 | 0 | { |
664 | 0 | typedef void (*fkt_t)(const SSL*, int, int); |
665 | | |
666 | | /* Documented since https://www.openssl.org/docs/man1.1.1/man3/BIO_set_callback.html |
667 | | * the argument is not really of type bio_info_cb* and must be cast |
668 | | * to the required type */ |
669 | |
|
670 | 0 | fkt_t fkt = WINPR_FUNC_PTR_CAST(fp, fkt_t); |
671 | 0 | SSL_set_info_callback(tls->ssl, fkt); |
672 | 0 | status = 1; |
673 | 0 | } |
674 | 0 | break; |
675 | | |
676 | 0 | default: |
677 | 0 | status = BIO_callback_ctrl(SSL_get_rbio(tls->ssl), cmd, fp); |
678 | 0 | break; |
679 | 0 | } |
680 | | |
681 | 0 | return status; |
682 | 0 | } |
683 | | |
684 | 0 | #define BIO_TYPE_RDP_TLS 68 |
685 | | |
686 | | WINPR_ATTR_NODISCARD |
687 | | static BOOL CALLBACK bio_rdp_tls_init_once(WINPR_ATTR_UNUSED PINIT_ONCE once, |
688 | | WINPR_ATTR_UNUSED PVOID param, |
689 | | WINPR_ATTR_UNUSED PVOID* context) |
690 | 0 | { |
691 | 0 | g_BioMethods = BIO_meth_new(BIO_TYPE_RDP_TLS, "RdpTls"); |
692 | 0 | if (!g_BioMethods) |
693 | 0 | return FALSE; |
694 | | |
695 | 0 | if (BIO_meth_set_write(g_BioMethods, bio_rdp_tls_write) != 1) |
696 | 0 | return FALSE; |
697 | 0 | if (BIO_meth_set_read(g_BioMethods, bio_rdp_tls_read) != 1) |
698 | 0 | return FALSE; |
699 | 0 | if (BIO_meth_set_puts(g_BioMethods, bio_rdp_tls_puts) != 1) |
700 | 0 | return FALSE; |
701 | 0 | if (BIO_meth_set_gets(g_BioMethods, bio_rdp_tls_gets) != 1) |
702 | 0 | return FALSE; |
703 | 0 | if (BIO_meth_set_ctrl(g_BioMethods, bio_rdp_tls_ctrl) != 1) |
704 | 0 | return FALSE; |
705 | 0 | if (BIO_meth_set_create(g_BioMethods, bio_rdp_tls_new) != 1) |
706 | 0 | return FALSE; |
707 | 0 | if (BIO_meth_set_destroy(g_BioMethods, bio_rdp_tls_free) != 1) |
708 | 0 | return FALSE; |
709 | 0 | if (BIO_meth_set_callback_ctrl(g_BioMethods, bio_rdp_tls_callback_ctrl) != 1) |
710 | 0 | return FALSE; |
711 | | |
712 | 0 | return TRUE; |
713 | 0 | } |
714 | | |
715 | | WINPR_ATTR_NODISCARD |
716 | | static BIO_METHOD* BIO_s_rdp_tls(void) |
717 | 0 | { |
718 | 0 | if (!InitOnceExecuteOnce(&g_BioMethodsOnce, bio_rdp_tls_init_once, nullptr, nullptr)) |
719 | 0 | return nullptr; |
720 | 0 | return g_BioMethods; |
721 | 0 | } |
722 | | |
723 | | WINPR_ATTR_MALLOC(BIO_free, 1) |
724 | | static BIO* BIO_new_rdp_tls(SSL_CTX* ctx, int client) |
725 | 0 | { |
726 | 0 | BIO* bio = BIO_new(BIO_s_rdp_tls()); |
727 | |
|
728 | 0 | if (!bio) |
729 | 0 | return nullptr; |
730 | | |
731 | 0 | SSL* ssl = SSL_new(ctx); |
732 | |
|
733 | 0 | if (!ssl) |
734 | 0 | { |
735 | 0 | BIO_free_all(bio); |
736 | 0 | return nullptr; |
737 | 0 | } |
738 | | |
739 | 0 | if (client) |
740 | 0 | SSL_set_connect_state(ssl); |
741 | 0 | else |
742 | 0 | SSL_set_accept_state(ssl); |
743 | |
|
744 | 0 | BIO_set_ssl(bio, ssl, BIO_CLOSE); |
745 | 0 | return bio; |
746 | 0 | } |
747 | | |
748 | | WINPR_ATTR_MALLOC(freerdp_certificate_free, 1) |
749 | | static rdpCertificate* tls_get_certificate(rdpTls* tls, BOOL peer) |
750 | 0 | { |
751 | 0 | X509* remote_cert = nullptr; |
752 | |
|
753 | 0 | if (peer) |
754 | 0 | remote_cert = SSL_get_peer_certificate(tls->ssl); |
755 | 0 | else |
756 | 0 | remote_cert = X509_dup(SSL_get_certificate(tls->ssl)); |
757 | |
|
758 | 0 | if (!remote_cert) |
759 | 0 | { |
760 | 0 | WLog_ERR(TAG, "failed to get the server TLS certificate"); |
761 | 0 | return nullptr; |
762 | 0 | } |
763 | | |
764 | | /* Get the peer's chain. If it does not exist, we're setting nullptr (clean data either way) */ |
765 | 0 | STACK_OF(X509)* chain = SSL_get_peer_cert_chain(tls->ssl); |
766 | 0 | rdpCertificate* cert = freerdp_certificate_new_from_x509(remote_cert, chain); |
767 | 0 | X509_free(remote_cert); |
768 | |
|
769 | 0 | return cert; |
770 | 0 | } |
771 | | |
772 | | WINPR_ATTR_NODISCARD |
773 | | static const char* tls_get_server_name(rdpTls* tls) |
774 | 0 | { |
775 | 0 | return tls->serverName ? tls->serverName : tls->hostname; |
776 | 0 | } |
777 | | |
778 | 0 | #define TLS_SERVER_END_POINT "tls-server-end-point:" |
779 | | |
780 | | WINPR_ATTR_MALLOC(free_tls_bindings, 1) |
781 | | static SecPkgContext_Bindings* tls_get_channel_bindings(const rdpCertificate* cert) |
782 | 0 | { |
783 | 0 | size_t CertificateHashLength = 0; |
784 | 0 | BYTE* ChannelBindingToken = nullptr; |
785 | 0 | SEC_CHANNEL_BINDINGS* ChannelBindings = nullptr; |
786 | 0 | const size_t PrefixLength = strnlen(TLS_SERVER_END_POINT, ARRAYSIZE(TLS_SERVER_END_POINT)); |
787 | |
|
788 | 0 | WINPR_ASSERT(cert); |
789 | | |
790 | | /* See https://www.rfc-editor.org/rfc/rfc5929 for details about hashes */ |
791 | 0 | WINPR_MD_TYPE alg = freerdp_certificate_get_signature_alg(cert); |
792 | 0 | const char* hash = nullptr; |
793 | 0 | switch (alg) |
794 | 0 | { |
795 | | |
796 | 0 | case WINPR_MD_MD5: |
797 | 0 | case WINPR_MD_SHA1: |
798 | 0 | hash = winpr_md_type_to_string(WINPR_MD_SHA256); |
799 | 0 | break; |
800 | 0 | default: |
801 | 0 | hash = winpr_md_type_to_string(alg); |
802 | 0 | break; |
803 | 0 | } |
804 | 0 | if (!hash) |
805 | 0 | return nullptr; |
806 | | |
807 | 0 | char* CertificateHash = freerdp_certificate_get_hash(cert, hash, &CertificateHashLength); |
808 | 0 | if (!CertificateHash) |
809 | 0 | return nullptr; |
810 | | |
811 | 0 | const size_t ChannelBindingTokenLength = PrefixLength + CertificateHashLength; |
812 | 0 | SecPkgContext_Bindings* ContextBindings = calloc(1, sizeof(SecPkgContext_Bindings)); |
813 | |
|
814 | 0 | if (!ContextBindings) |
815 | 0 | goto out_free; |
816 | | |
817 | 0 | { |
818 | 0 | const size_t slen = sizeof(SEC_CHANNEL_BINDINGS) + ChannelBindingTokenLength; |
819 | 0 | if (slen > UINT32_MAX) |
820 | 0 | goto out_free; |
821 | | |
822 | 0 | ContextBindings->BindingsLength = (UINT32)slen; |
823 | 0 | } |
824 | 0 | ChannelBindings = (SEC_CHANNEL_BINDINGS*)calloc(1, ContextBindings->BindingsLength); |
825 | |
|
826 | 0 | if (!ChannelBindings) |
827 | 0 | goto out_free; |
828 | | |
829 | 0 | ContextBindings->Bindings = ChannelBindings; |
830 | 0 | ChannelBindings->cbApplicationDataLength = (UINT32)ChannelBindingTokenLength; |
831 | 0 | ChannelBindings->dwApplicationDataOffset = sizeof(SEC_CHANNEL_BINDINGS); |
832 | 0 | ChannelBindingToken = &((BYTE*)ChannelBindings)[ChannelBindings->dwApplicationDataOffset]; |
833 | 0 | memcpy(ChannelBindingToken, TLS_SERVER_END_POINT, PrefixLength); |
834 | 0 | memcpy(ChannelBindingToken + PrefixLength, CertificateHash, CertificateHashLength); |
835 | 0 | free(CertificateHash); |
836 | 0 | return ContextBindings; |
837 | 0 | out_free: |
838 | 0 | free(CertificateHash); |
839 | 0 | free(ContextBindings); |
840 | 0 | return nullptr; |
841 | 0 | } |
842 | | |
843 | | WINPR_ATTR_NODISCARD |
844 | | static BOOL CALLBACK secrets_file_init_cb(WINPR_ATTR_UNUSED PINIT_ONCE once, |
845 | | WINPR_ATTR_UNUSED PVOID param, |
846 | | WINPR_ATTR_UNUSED PVOID* context) |
847 | 0 | { |
848 | 0 | secrets_file_idx = SSL_get_ex_new_index(0, nullptr, nullptr, nullptr, nullptr); |
849 | |
|
850 | 0 | return (secrets_file_idx != -1); |
851 | 0 | } |
852 | | |
853 | | static void SSLCTX_keylog_cb(const SSL* ssl, const char* line) |
854 | 0 | { |
855 | 0 | char* dfile = nullptr; |
856 | |
|
857 | 0 | if (secrets_file_idx == -1) |
858 | 0 | return; |
859 | | |
860 | 0 | dfile = SSL_get_ex_data(ssl, secrets_file_idx); |
861 | 0 | if (dfile) |
862 | 0 | { |
863 | 0 | FILE* f = winpr_fopen(dfile, "a+"); |
864 | 0 | if (f) |
865 | 0 | { |
866 | 0 | (void)fwrite(line, strlen(line), 1, f); |
867 | 0 | (void)fwrite("\n", 1, 1, f); |
868 | 0 | (void)fclose(f); |
869 | 0 | } |
870 | 0 | } |
871 | 0 | } |
872 | | |
873 | | static void tls_reset(rdpTls* tls) |
874 | 0 | { |
875 | 0 | WINPR_ASSERT(tls); |
876 | |
|
877 | 0 | if (tls->ctx) |
878 | 0 | { |
879 | 0 | SSL_CTX_free(tls->ctx); |
880 | 0 | tls->ctx = nullptr; |
881 | 0 | } |
882 | | |
883 | | /* tls->underlying is a stacked BIO under tls->bio. |
884 | | * BIO_free_all will free recursively. */ |
885 | 0 | if (tls->bio) |
886 | 0 | BIO_free_all(tls->bio); |
887 | 0 | else if (tls->underlying) |
888 | 0 | BIO_free_all(tls->underlying); |
889 | 0 | tls->bio = nullptr; |
890 | 0 | tls->underlying = nullptr; |
891 | |
|
892 | 0 | free_tls_public_key(tls); |
893 | 0 | free_tls_bindings(tls->Bindings); |
894 | 0 | tls->Bindings = nullptr; |
895 | 0 | } |
896 | | |
897 | | #if OPENSSL_VERSION_NUMBER >= 0x010000000L |
898 | | WINPR_ATTR_NODISCARD |
899 | | static BOOL tls_prepare(rdpTls* tls, BIO* underlying, const SSL_METHOD* method, int options, |
900 | | BOOL clientMode) |
901 | | #else |
902 | | WINPR_ATTR_NODISCARD |
903 | | static BOOL tls_prepare(rdpTls* tls, BIO* underlying, SSL_METHOD* method, int options, |
904 | | BOOL clientMode) |
905 | | #endif |
906 | 0 | { |
907 | 0 | WINPR_ASSERT(tls); |
908 | |
|
909 | 0 | rdpSettings* settings = tls->context->settings; |
910 | 0 | WINPR_ASSERT(settings); |
911 | |
|
912 | 0 | tls_reset(tls); |
913 | 0 | tls->ctx = SSL_CTX_new(method); |
914 | |
|
915 | 0 | tls->underlying = underlying; |
916 | |
|
917 | 0 | if (!tls->ctx) |
918 | 0 | { |
919 | 0 | WLog_ERR(TAG, "SSL_CTX_new failed"); |
920 | 0 | return FALSE; |
921 | 0 | } |
922 | | |
923 | 0 | SSL_CTX_set_mode(tls->ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | SSL_MODE_ENABLE_PARTIAL_WRITE); |
924 | 0 | SSL_CTX_set_options(tls->ctx, WINPR_ASSERTING_INT_CAST(uint64_t, options)); |
925 | 0 | SSL_CTX_set_read_ahead(tls->ctx, 1); |
926 | 0 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
927 | 0 | UINT16 version = freerdp_settings_get_uint16(settings, FreeRDP_TLSMinVersion); |
928 | 0 | if (!SSL_CTX_set_min_proto_version(tls->ctx, version)) |
929 | 0 | { |
930 | 0 | WLog_ERR(TAG, "SSL_CTX_set_min_proto_version %" PRIu16 " failed", version); |
931 | 0 | return FALSE; |
932 | 0 | } |
933 | 0 | version = freerdp_settings_get_uint16(settings, FreeRDP_TLSMaxVersion); |
934 | 0 | if (!SSL_CTX_set_max_proto_version(tls->ctx, version)) |
935 | 0 | { |
936 | 0 | WLog_ERR(TAG, "SSL_CTX_set_max_proto_version %" PRIu16 " failed", version); |
937 | 0 | return FALSE; |
938 | 0 | } |
939 | 0 | #endif |
940 | 0 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER) |
941 | 0 | SSL_CTX_set_security_level(tls->ctx, WINPR_ASSERTING_INT_CAST(int, settings->TlsSecLevel)); |
942 | 0 | #endif |
943 | |
|
944 | 0 | if (settings->AllowedTlsCiphers) |
945 | 0 | { |
946 | 0 | if (!SSL_CTX_set_cipher_list(tls->ctx, settings->AllowedTlsCiphers)) |
947 | 0 | { |
948 | 0 | WLog_ERR(TAG, "SSL_CTX_set_cipher_list %s failed", settings->AllowedTlsCiphers); |
949 | 0 | return FALSE; |
950 | 0 | } |
951 | 0 | } |
952 | | |
953 | 0 | tls->bio = BIO_new_rdp_tls(tls->ctx, clientMode); |
954 | |
|
955 | 0 | if (!tls->bio) |
956 | 0 | { |
957 | 0 | WLog_ERR(TAG, "unable to create TLS BIO"); |
958 | 0 | return FALSE; |
959 | 0 | } |
960 | | |
961 | 0 | if (BIO_get_ssl(tls->bio, &tls->ssl) < 0 || !tls->ssl) |
962 | 0 | { |
963 | 0 | WLog_ERR(TAG, "unable to retrieve the SSL of the connection"); |
964 | 0 | return FALSE; |
965 | 0 | } |
966 | | |
967 | 0 | if (settings->TlsSecretsFile) |
968 | 0 | { |
969 | 0 | #if OPENSSL_VERSION_NUMBER >= 0x10101000L |
970 | 0 | if (!InitOnceExecuteOnce(&secrets_file_idx_once, secrets_file_init_cb, nullptr, nullptr)) |
971 | 0 | return FALSE; |
972 | | |
973 | 0 | if (secrets_file_idx != -1) |
974 | 0 | { |
975 | 0 | SSL_set_ex_data(tls->ssl, secrets_file_idx, settings->TlsSecretsFile); |
976 | 0 | SSL_CTX_set_keylog_callback(tls->ctx, SSLCTX_keylog_cb); |
977 | 0 | } |
978 | | #else |
979 | | WLog_WARN(TAG, "Key-Logging not available - requires OpenSSL 1.1.1 or higher"); |
980 | | #endif |
981 | 0 | } |
982 | | |
983 | 0 | BIO_push(tls->bio, underlying); |
984 | 0 | return TRUE; |
985 | 0 | } |
986 | | |
987 | | static void |
988 | | adjustSslOptions(WINPR_ATTR_UNUSED int* options) // NOLINT(readability-non-const-parameter) |
989 | 0 | { |
990 | 0 | WINPR_ASSERT(options); |
991 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
992 | | *options |= SSL_OP_NO_SSLv2; |
993 | | *options |= SSL_OP_NO_SSLv3; |
994 | | #endif |
995 | 0 | } |
996 | | |
997 | | const SSL_METHOD* freerdp_tls_get_ssl_method(BOOL isDtls, BOOL isClient) |
998 | 0 | { |
999 | 0 | if (isClient) |
1000 | 0 | { |
1001 | 0 | if (isDtls) |
1002 | 0 | return DTLS_client_method(); |
1003 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
1004 | | return SSLv23_client_method(); |
1005 | | #else |
1006 | 0 | return TLS_client_method(); |
1007 | 0 | #endif |
1008 | 0 | } |
1009 | | |
1010 | 0 | if (isDtls) |
1011 | 0 | return DTLS_server_method(); |
1012 | | |
1013 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
1014 | | return SSLv23_server_method(); |
1015 | | #else |
1016 | 0 | return TLS_server_method(); |
1017 | 0 | #endif |
1018 | 0 | } |
1019 | | |
1020 | | TlsHandshakeResult freerdp_tls_connect_ex(rdpTls* tls, BIO* underlying, const SSL_METHOD* methods) |
1021 | 0 | { |
1022 | 0 | WINPR_ASSERT(tls); |
1023 | |
|
1024 | 0 | int options = 0; |
1025 | | /** |
1026 | | * SSL_OP_NO_COMPRESSION: |
1027 | | * |
1028 | | * The Microsoft RDP server does not advertise support |
1029 | | * for TLS compression, but alternative servers may support it. |
1030 | | * This was observed between early versions of the FreeRDP server |
1031 | | * and the FreeRDP client, and caused major performance issues, |
1032 | | * which is why we're disabling it. |
1033 | | */ |
1034 | 0 | #ifdef SSL_OP_NO_COMPRESSION |
1035 | 0 | options |= SSL_OP_NO_COMPRESSION; |
1036 | 0 | #endif |
1037 | | /** |
1038 | | * SSL_OP_TLS_BLOCK_PADDING_BUG: |
1039 | | * |
1040 | | * The Microsoft RDP server does *not* support TLS padding. |
1041 | | * It absolutely needs to be disabled otherwise it won't work. |
1042 | | */ |
1043 | 0 | options |= SSL_OP_TLS_BLOCK_PADDING_BUG; |
1044 | | /** |
1045 | | * SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS: |
1046 | | * |
1047 | | * Just like TLS padding, the Microsoft RDP server does not |
1048 | | * support empty fragments. This needs to be disabled. |
1049 | | */ |
1050 | 0 | options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; |
1051 | |
|
1052 | 0 | tls->isClientMode = TRUE; |
1053 | 0 | adjustSslOptions(&options); |
1054 | |
|
1055 | 0 | if (!tls_prepare(tls, underlying, methods, options, TRUE)) |
1056 | 0 | return TLS_HANDSHAKE_ERROR; |
1057 | | |
1058 | 0 | #if !defined(OPENSSL_NO_TLSEXT) |
1059 | 0 | const char* str = tls_get_server_name(tls); |
1060 | 0 | void* ptr = WINPR_CAST_CONST_PTR_AWAY(str, void*); |
1061 | 0 | SSL_set_tlsext_host_name(tls->ssl, ptr); |
1062 | 0 | #endif |
1063 | |
|
1064 | 0 | return freerdp_tls_handshake(tls); |
1065 | 0 | } |
1066 | | |
1067 | | static int bio_err_print(const char* str, size_t len, void* u) |
1068 | 0 | { |
1069 | 0 | wLog* log = u; |
1070 | 0 | WLog_Print(log, WLOG_ERROR, "[BIO_do_handshake] %s [%" PRIuz "]", str, len); |
1071 | 0 | return 0; |
1072 | 0 | } |
1073 | | |
1074 | | TlsHandshakeResult freerdp_tls_handshake(rdpTls* tls) |
1075 | 0 | { |
1076 | 0 | TlsHandshakeResult ret = TLS_HANDSHAKE_ERROR; |
1077 | |
|
1078 | 0 | WINPR_ASSERT(tls); |
1079 | 0 | const long status = BIO_do_handshake(tls->bio); |
1080 | 0 | if (status != 1) |
1081 | 0 | { |
1082 | 0 | if (!BIO_should_retry(tls->bio)) |
1083 | 0 | { |
1084 | 0 | wLog* log = WLog_Get(TAG); |
1085 | 0 | WLog_Print(log, WLOG_ERROR, "BIO_do_handshake failed"); |
1086 | 0 | ERR_print_errors_cb(bio_err_print, log); |
1087 | 0 | return TLS_HANDSHAKE_ERROR; |
1088 | 0 | } |
1089 | | |
1090 | 0 | return TLS_HANDSHAKE_CONTINUE; |
1091 | 0 | } |
1092 | | |
1093 | 0 | int verify_status = 0; |
1094 | 0 | rdpCertificate* cert = tls_get_certificate(tls, tls->isClientMode); |
1095 | |
|
1096 | 0 | if (!cert) |
1097 | 0 | { |
1098 | 0 | WLog_ERR(TAG, "tls_get_certificate failed to return the server certificate."); |
1099 | 0 | return TLS_HANDSHAKE_ERROR; |
1100 | 0 | } |
1101 | | |
1102 | 0 | do |
1103 | 0 | { |
1104 | 0 | free_tls_bindings(tls->Bindings); |
1105 | 0 | tls->Bindings = tls_get_channel_bindings(cert); |
1106 | 0 | if (!tls->Bindings) |
1107 | 0 | { |
1108 | 0 | WLog_ERR(TAG, "unable to retrieve bindings"); |
1109 | 0 | break; |
1110 | 0 | } |
1111 | | |
1112 | 0 | free_tls_public_key(tls); |
1113 | 0 | if (!freerdp_certificate_get_public_key(cert, &tls->PublicKey, &tls->PublicKeyLength)) |
1114 | 0 | { |
1115 | 0 | WLog_ERR(TAG, |
1116 | 0 | "freerdp_certificate_get_public_key failed to return the server public key."); |
1117 | 0 | break; |
1118 | 0 | } |
1119 | | |
1120 | | /* server-side NLA needs public keys (keys from us, the server) but no certificate verify */ |
1121 | 0 | ret = TLS_HANDSHAKE_SUCCESS; |
1122 | |
|
1123 | 0 | if (tls->isClientMode) |
1124 | 0 | { |
1125 | 0 | WINPR_ASSERT(tls->port <= UINT16_MAX); |
1126 | 0 | verify_status = |
1127 | 0 | tls_verify_certificate(tls, cert, tls_get_server_name(tls), (UINT16)tls->port); |
1128 | |
|
1129 | 0 | if (verify_status < 1) |
1130 | 0 | { |
1131 | 0 | WLog_ERR(TAG, "certificate not trusted, aborting."); |
1132 | 0 | freerdp_tls_send_alert(tls); |
1133 | 0 | ret = TLS_HANDSHAKE_VERIFY_ERROR; |
1134 | 0 | } |
1135 | 0 | } |
1136 | 0 | } while (0); |
1137 | |
|
1138 | 0 | freerdp_certificate_free(cert); |
1139 | 0 | return ret; |
1140 | 0 | } |
1141 | | |
1142 | | static int pollAndHandshake(rdpTls* tls) |
1143 | 0 | { |
1144 | 0 | WINPR_ASSERT(tls); |
1145 | |
|
1146 | 0 | do |
1147 | 0 | { |
1148 | 0 | HANDLE events[] = { freerdp_abort_event(tls->context), nullptr }; |
1149 | 0 | DWORD status = 0; |
1150 | 0 | if (BIO_get_event(tls->bio, &events[1]) < 0) |
1151 | 0 | { |
1152 | 0 | WLog_ERR(TAG, "unable to retrieve BIO associated event"); |
1153 | 0 | return -1; |
1154 | 0 | } |
1155 | | |
1156 | 0 | if (!events[1]) |
1157 | 0 | { |
1158 | 0 | WLog_ERR(TAG, "unable to retrieve BIO event"); |
1159 | 0 | return -1; |
1160 | 0 | } |
1161 | | |
1162 | 0 | status = WaitForMultipleObjectsEx(ARRAYSIZE(events), events, FALSE, INFINITE, TRUE); |
1163 | 0 | switch (status) |
1164 | 0 | { |
1165 | 0 | case WAIT_OBJECT_0 + 1: |
1166 | 0 | break; |
1167 | 0 | case WAIT_OBJECT_0: |
1168 | 0 | WLog_DBG(TAG, "Abort event set, cancel connect"); |
1169 | 0 | return -1; |
1170 | 0 | case WAIT_TIMEOUT: |
1171 | 0 | case WAIT_IO_COMPLETION: |
1172 | 0 | continue; |
1173 | 0 | default: |
1174 | 0 | WLog_ERR(TAG, "error during WaitForSingleObject(): 0x%08" PRIX32 "", status); |
1175 | 0 | return -1; |
1176 | 0 | } |
1177 | | |
1178 | 0 | TlsHandshakeResult result = freerdp_tls_handshake(tls); |
1179 | 0 | switch (result) |
1180 | 0 | { |
1181 | 0 | case TLS_HANDSHAKE_CONTINUE: |
1182 | 0 | break; |
1183 | 0 | case TLS_HANDSHAKE_SUCCESS: |
1184 | 0 | return 1; |
1185 | 0 | case TLS_HANDSHAKE_ERROR: |
1186 | 0 | case TLS_HANDSHAKE_VERIFY_ERROR: |
1187 | 0 | default: |
1188 | 0 | return -1; |
1189 | 0 | } |
1190 | 0 | } while (TRUE); |
1191 | 0 | } |
1192 | | |
1193 | | int freerdp_tls_connect(rdpTls* tls, BIO* underlying) |
1194 | 0 | { |
1195 | 0 | const SSL_METHOD* method = freerdp_tls_get_ssl_method(FALSE, TRUE); |
1196 | |
|
1197 | 0 | WINPR_ASSERT(tls); |
1198 | 0 | TlsHandshakeResult result = freerdp_tls_connect_ex(tls, underlying, method); |
1199 | 0 | switch (result) |
1200 | 0 | { |
1201 | 0 | case TLS_HANDSHAKE_SUCCESS: |
1202 | 0 | return 1; |
1203 | 0 | case TLS_HANDSHAKE_CONTINUE: |
1204 | 0 | break; |
1205 | 0 | case TLS_HANDSHAKE_ERROR: |
1206 | 0 | case TLS_HANDSHAKE_VERIFY_ERROR: |
1207 | 0 | return -1; |
1208 | 0 | default: |
1209 | 0 | return -1; |
1210 | 0 | } |
1211 | | |
1212 | 0 | return pollAndHandshake(tls); |
1213 | 0 | } |
1214 | | |
1215 | | #if defined(MICROSOFT_IOS_SNI_BUG) && !defined(OPENSSL_NO_TLSEXT) && \ |
1216 | | !defined(LIBRESSL_VERSION_NUMBER) |
1217 | | static void tls_openssl_tlsext_debug_callback(SSL* s, int client_server, int type, |
1218 | | unsigned char* data, int len, void* arg) |
1219 | | { |
1220 | | if (type == TLSEXT_TYPE_server_name) |
1221 | | { |
1222 | | WLog_DBG(TAG, "Client uses SNI (extension disabled)"); |
1223 | | s->servername_done = 2; |
1224 | | } |
1225 | | } |
1226 | | #endif |
1227 | | |
1228 | | BOOL freerdp_tls_accept(rdpTls* tls, BIO* underlying, rdpSettings* settings) |
1229 | 0 | { |
1230 | 0 | WINPR_ASSERT(tls); |
1231 | 0 | TlsHandshakeResult res = |
1232 | 0 | freerdp_tls_accept_ex(tls, underlying, settings, freerdp_tls_get_ssl_method(FALSE, FALSE)); |
1233 | 0 | switch (res) |
1234 | 0 | { |
1235 | 0 | case TLS_HANDSHAKE_SUCCESS: |
1236 | 0 | return TRUE; |
1237 | 0 | case TLS_HANDSHAKE_CONTINUE: |
1238 | 0 | break; |
1239 | 0 | case TLS_HANDSHAKE_ERROR: |
1240 | 0 | case TLS_HANDSHAKE_VERIFY_ERROR: |
1241 | 0 | default: |
1242 | 0 | return FALSE; |
1243 | 0 | } |
1244 | | |
1245 | 0 | return pollAndHandshake(tls) > 0; |
1246 | 0 | } |
1247 | | |
1248 | | TlsHandshakeResult freerdp_tls_accept_ex(rdpTls* tls, BIO* underlying, rdpSettings* settings, |
1249 | | const SSL_METHOD* methods) |
1250 | 0 | { |
1251 | 0 | WINPR_ASSERT(tls); |
1252 | |
|
1253 | 0 | int options = 0; |
1254 | 0 | int status = 0; |
1255 | | |
1256 | | /** |
1257 | | * SSL_OP_NO_SSLv2: |
1258 | | * |
1259 | | * We only want SSLv3 and TLSv1, so disable SSLv2. |
1260 | | * SSLv3 is used by, eg. Microsoft RDC for Mac OS X. |
1261 | | */ |
1262 | 0 | options |= SSL_OP_NO_SSLv2; |
1263 | | /** |
1264 | | * SSL_OP_NO_COMPRESSION: |
1265 | | * |
1266 | | * The Microsoft RDP server does not advertise support |
1267 | | * for TLS compression, but alternative servers may support it. |
1268 | | * This was observed between early versions of the FreeRDP server |
1269 | | * and the FreeRDP client, and caused major performance issues, |
1270 | | * which is why we're disabling it. |
1271 | | */ |
1272 | 0 | #ifdef SSL_OP_NO_COMPRESSION |
1273 | 0 | options |= SSL_OP_NO_COMPRESSION; |
1274 | 0 | #endif |
1275 | | /** |
1276 | | * SSL_OP_TLS_BLOCK_PADDING_BUG: |
1277 | | * |
1278 | | * The Microsoft RDP server does *not* support TLS padding. |
1279 | | * It absolutely needs to be disabled otherwise it won't work. |
1280 | | */ |
1281 | 0 | options |= SSL_OP_TLS_BLOCK_PADDING_BUG; |
1282 | | /** |
1283 | | * SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS: |
1284 | | * |
1285 | | * Just like TLS padding, the Microsoft RDP server does not |
1286 | | * support empty fragments. This needs to be disabled. |
1287 | | */ |
1288 | 0 | options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; |
1289 | | |
1290 | | /** |
1291 | | * SSL_OP_NO_RENEGOTIATION |
1292 | | * |
1293 | | * Disable SSL client site renegotiation. |
1294 | | */ |
1295 | |
|
1296 | 0 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) && (OPENSSL_VERSION_NUMBER < 0x30000000L) && \ |
1297 | 0 | !defined(LIBRESSL_VERSION_NUMBER) |
1298 | 0 | options |= SSL_OP_NO_RENEGOTIATION; |
1299 | 0 | #endif |
1300 | |
|
1301 | 0 | if (!tls_prepare(tls, underlying, methods, options, FALSE)) |
1302 | 0 | return TLS_HANDSHAKE_ERROR; |
1303 | | |
1304 | 0 | const rdpPrivateKey* key = freerdp_settings_get_pointer(settings, FreeRDP_RdpServerRsaKey); |
1305 | 0 | if (!key) |
1306 | 0 | { |
1307 | 0 | WLog_ERR(TAG, "invalid private key"); |
1308 | 0 | return TLS_HANDSHAKE_ERROR; |
1309 | 0 | } |
1310 | | |
1311 | 0 | EVP_PKEY* privkey = freerdp_key_get_evp_pkey(key); |
1312 | 0 | if (!privkey) |
1313 | 0 | { |
1314 | 0 | WLog_ERR(TAG, "invalid private key"); |
1315 | 0 | return TLS_HANDSHAKE_ERROR; |
1316 | 0 | } |
1317 | | |
1318 | 0 | status = SSL_use_PrivateKey(tls->ssl, privkey); |
1319 | | /* The local reference to the private key will anyway go out of |
1320 | | * scope; so the reference count should be decremented whether |
1321 | | * SSL_use_PrivateKey succeeds or fails. |
1322 | | */ |
1323 | 0 | EVP_PKEY_free(privkey); |
1324 | |
|
1325 | 0 | if (status <= 0) |
1326 | 0 | { |
1327 | 0 | WLog_ERR(TAG, "SSL_CTX_use_PrivateKey_file failed"); |
1328 | 0 | return TLS_HANDSHAKE_ERROR; |
1329 | 0 | } |
1330 | | |
1331 | 0 | rdpCertificate* cert = |
1332 | 0 | freerdp_settings_get_pointer_writable(settings, FreeRDP_RdpServerCertificate); |
1333 | 0 | if (!cert) |
1334 | 0 | { |
1335 | 0 | WLog_ERR(TAG, "invalid certificate"); |
1336 | 0 | return TLS_HANDSHAKE_ERROR; |
1337 | 0 | } |
1338 | | |
1339 | 0 | status = SSL_use_certificate(tls->ssl, freerdp_certificate_get_x509(cert)); |
1340 | |
|
1341 | 0 | if (status <= 0) |
1342 | 0 | { |
1343 | 0 | WLog_ERR(TAG, "SSL_use_certificate_file failed"); |
1344 | 0 | return TLS_HANDSHAKE_ERROR; |
1345 | 0 | } |
1346 | | |
1347 | 0 | const size_t cnt = freerdp_certificate_get_chain_len(cert); |
1348 | 0 | for (size_t x = 0; x < cnt; x++) |
1349 | 0 | { |
1350 | 0 | X509* xcert = freerdp_certificate_get_chain_at(cert, x); |
1351 | 0 | WINPR_ASSERT(xcert); |
1352 | 0 | const long rc = SSL_add1_chain_cert(tls->ssl, xcert); |
1353 | 0 | if (rc != 1) |
1354 | 0 | { |
1355 | 0 | WLog_ERR(TAG, "SSL_add1_chain_cert failed"); |
1356 | 0 | return TLS_HANDSHAKE_ERROR; |
1357 | 0 | } |
1358 | 0 | } |
1359 | | |
1360 | | #if defined(MICROSOFT_IOS_SNI_BUG) && !defined(OPENSSL_NO_TLSEXT) && \ |
1361 | | !defined(LIBRESSL_VERSION_NUMBER) |
1362 | | SSL_set_tlsext_debug_callback(tls->ssl, tls_openssl_tlsext_debug_callback); |
1363 | | #endif |
1364 | | |
1365 | 0 | return freerdp_tls_handshake(tls); |
1366 | 0 | } |
1367 | | |
1368 | | BOOL freerdp_tls_send_alert(rdpTls* tls) |
1369 | 0 | { |
1370 | 0 | WINPR_ASSERT(tls); |
1371 | |
|
1372 | 0 | if (!tls) |
1373 | 0 | return FALSE; |
1374 | | |
1375 | 0 | if (!tls->ssl) |
1376 | 0 | return TRUE; |
1377 | | |
1378 | | /** |
1379 | | * FIXME: The following code does not work on OpenSSL > 1.1.0 because the |
1380 | | * SSL struct is opaque now |
1381 | | */ |
1382 | | #if (!defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER < 0x10100000L)) || \ |
1383 | | (defined(LIBRESSL_VERSION_NUMBER) && (LIBRESSL_VERSION_NUMBER <= 0x2080300fL)) |
1384 | | |
1385 | | if (tls->alertDescription != TLS_ALERT_DESCRIPTION_CLOSE_NOTIFY) |
1386 | | { |
1387 | | /** |
1388 | | * OpenSSL doesn't really expose an API for sending a TLS alert manually. |
1389 | | * |
1390 | | * The following code disables the sending of the default "close notify" |
1391 | | * and then proceeds to force sending a custom TLS alert before shutting down. |
1392 | | * |
1393 | | * Manually sending a TLS alert is necessary in certain cases, |
1394 | | * like when server-side NLA results in an authentication failure. |
1395 | | */ |
1396 | | SSL_SESSION* ssl_session = SSL_get_session(tls->ssl); |
1397 | | SSL_CTX* ssl_ctx = SSL_get_SSL_CTX(tls->ssl); |
1398 | | SSL_set_quiet_shutdown(tls->ssl, 1); |
1399 | | |
1400 | | if ((tls->alertLevel == TLS_ALERT_LEVEL_FATAL) && (ssl_session)) |
1401 | | SSL_CTX_remove_session(ssl_ctx, ssl_session); |
1402 | | |
1403 | | tls->ssl->s3->alert_dispatch = 1; |
1404 | | tls->ssl->s3->send_alert[0] = tls->alertLevel; |
1405 | | tls->ssl->s3->send_alert[1] = tls->alertDescription; |
1406 | | |
1407 | | if (tls->ssl->s3->wbuf.left == 0) |
1408 | | tls->ssl->method->ssl_dispatch_alert(tls->ssl); |
1409 | | } |
1410 | | |
1411 | | #endif |
1412 | 0 | return TRUE; |
1413 | 0 | } |
1414 | | |
1415 | | int freerdp_tls_write_all(rdpTls* tls, const BYTE* data, size_t length) |
1416 | 0 | { |
1417 | 0 | WINPR_ASSERT(tls); |
1418 | 0 | size_t offset = 0; |
1419 | 0 | BIO* bio = tls->bio; |
1420 | |
|
1421 | 0 | if (length > INT32_MAX) |
1422 | 0 | return -1; |
1423 | | |
1424 | 0 | while (offset < length) |
1425 | 0 | { |
1426 | 0 | ERR_clear_error(); |
1427 | 0 | const int status = BIO_write(bio, &data[offset], (int)(length - offset)); |
1428 | |
|
1429 | 0 | if (status > 0) |
1430 | 0 | offset += (size_t)status; |
1431 | 0 | else |
1432 | 0 | { |
1433 | 0 | if (!BIO_should_retry(bio)) |
1434 | 0 | return -1; |
1435 | | |
1436 | 0 | if (BIO_write_blocked(bio)) |
1437 | 0 | { |
1438 | 0 | const long rc = BIO_wait_write(bio, 100); |
1439 | 0 | if (rc < 0) |
1440 | 0 | return -1; |
1441 | 0 | } |
1442 | 0 | else if (BIO_read_blocked(bio)) |
1443 | 0 | return -2; /* Abort write, there is data that must be read */ |
1444 | 0 | else |
1445 | 0 | USleep(100); |
1446 | 0 | } |
1447 | 0 | } |
1448 | | |
1449 | 0 | return (int)length; |
1450 | 0 | } |
1451 | | |
1452 | | int freerdp_tls_set_alert_code(rdpTls* tls, int level, int description) |
1453 | 0 | { |
1454 | 0 | WINPR_ASSERT(tls); |
1455 | 0 | tls->alertLevel = level; |
1456 | 0 | tls->alertDescription = description; |
1457 | 0 | return 0; |
1458 | 0 | } |
1459 | | |
1460 | | WINPR_ATTR_NODISCARD |
1461 | | static BOOL is_redirected(rdpTls* tls) |
1462 | 0 | { |
1463 | 0 | rdpSettings* settings = tls->context->settings; |
1464 | |
|
1465 | 0 | if (settings->GatewayArmTransport) |
1466 | 0 | return TRUE; |
1467 | | |
1468 | 0 | if (LB_NOREDIRECT & settings->RedirectionFlags) |
1469 | 0 | return FALSE; |
1470 | | |
1471 | 0 | return settings->RedirectionFlags != 0; |
1472 | 0 | } |
1473 | | |
1474 | | WINPR_ATTR_NODISCARD |
1475 | | static BOOL is_accepted(rdpTls* tls, const rdpCertificate* cert) |
1476 | 0 | { |
1477 | 0 | WINPR_ASSERT(tls); |
1478 | 0 | WINPR_ASSERT(tls->context); |
1479 | 0 | WINPR_ASSERT(cert); |
1480 | 0 | rdpSettings* settings = tls->context->settings; |
1481 | 0 | WINPR_ASSERT(settings); |
1482 | |
|
1483 | 0 | FreeRDP_Settings_Keys_String keyAccepted = FreeRDP_AcceptedCert; |
1484 | 0 | FreeRDP_Settings_Keys_UInt32 keyLength = FreeRDP_AcceptedCertLength; |
1485 | |
|
1486 | 0 | if (tls->isGatewayTransport) |
1487 | 0 | { |
1488 | 0 | keyAccepted = FreeRDP_GatewayAcceptedCert; |
1489 | 0 | keyLength = FreeRDP_GatewayAcceptedCertLength; |
1490 | 0 | } |
1491 | 0 | else if (is_redirected(tls)) |
1492 | 0 | { |
1493 | 0 | keyAccepted = FreeRDP_RedirectionAcceptedCert; |
1494 | 0 | keyLength = FreeRDP_RedirectionAcceptedCertLength; |
1495 | 0 | } |
1496 | |
|
1497 | 0 | const char* AcceptedKey = freerdp_settings_get_string(settings, keyAccepted); |
1498 | 0 | const UINT32 AcceptedKeyLength = freerdp_settings_get_uint32(settings, keyLength); |
1499 | |
|
1500 | 0 | if ((AcceptedKeyLength > 0) && AcceptedKey) |
1501 | 0 | { |
1502 | 0 | BOOL accepted = FALSE; |
1503 | 0 | size_t pemLength = 0; |
1504 | 0 | char* pem = freerdp_certificate_get_pem_ex(cert, &pemLength, FALSE); |
1505 | 0 | if (pem && (AcceptedKeyLength == pemLength)) |
1506 | 0 | { |
1507 | 0 | if (memcmp(AcceptedKey, pem, AcceptedKeyLength) == 0) |
1508 | 0 | accepted = TRUE; |
1509 | 0 | } |
1510 | 0 | free(pem); |
1511 | 0 | if (accepted) |
1512 | 0 | return TRUE; |
1513 | 0 | } |
1514 | | |
1515 | 0 | if (!freerdp_settings_set_string(settings, keyAccepted, nullptr)) |
1516 | 0 | WLog_WARN(TAG, "freerdp_settings_set_string(settings, keyAccepted=%d, nullptr) failed", |
1517 | 0 | keyAccepted); |
1518 | 0 | if (!freerdp_settings_set_uint32(settings, keyLength, 0)) |
1519 | 0 | WLog_WARN(TAG, "freerdp_settings_set_uint32(settings, keyLength=%d, 0) failed", keyLength); |
1520 | |
|
1521 | 0 | return FALSE; |
1522 | 0 | } |
1523 | | |
1524 | | WINPR_ATTR_NODISCARD |
1525 | | static BOOL compare_fingerprint(const char* fp, const char* hash, const rdpCertificate* cert, |
1526 | | BOOL separator) |
1527 | 0 | { |
1528 | 0 | BOOL equal = 0; |
1529 | 0 | char* strhash = nullptr; |
1530 | |
|
1531 | 0 | WINPR_ASSERT(fp); |
1532 | 0 | WINPR_ASSERT(hash); |
1533 | 0 | WINPR_ASSERT(cert); |
1534 | |
|
1535 | 0 | strhash = freerdp_certificate_get_fingerprint_by_hash_ex(cert, hash, separator); |
1536 | 0 | if (!strhash) |
1537 | 0 | return FALSE; |
1538 | | |
1539 | 0 | equal = (_stricmp(strhash, fp) == 0); |
1540 | 0 | free(strhash); |
1541 | 0 | return equal; |
1542 | 0 | } |
1543 | | |
1544 | | WINPR_ATTR_NODISCARD |
1545 | | static BOOL compare_fingerprint_all(const char* fp, const char* hash, const rdpCertificate* cert) |
1546 | 0 | { |
1547 | 0 | WINPR_ASSERT(fp); |
1548 | 0 | WINPR_ASSERT(hash); |
1549 | 0 | WINPR_ASSERT(cert); |
1550 | 0 | if (compare_fingerprint(fp, hash, cert, FALSE)) |
1551 | 0 | return TRUE; |
1552 | 0 | if (compare_fingerprint(fp, hash, cert, TRUE)) |
1553 | 0 | return TRUE; |
1554 | 0 | return FALSE; |
1555 | 0 | } |
1556 | | |
1557 | | WINPR_ATTR_NODISCARD |
1558 | | static BOOL is_accepted_fingerprint(const rdpCertificate* cert, |
1559 | | const char* CertificateAcceptedFingerprints) |
1560 | 0 | { |
1561 | 0 | WINPR_ASSERT(cert); |
1562 | |
|
1563 | 0 | BOOL rc = FALSE; |
1564 | 0 | if (CertificateAcceptedFingerprints) |
1565 | 0 | { |
1566 | 0 | char* context = nullptr; |
1567 | 0 | char* copy = _strdup(CertificateAcceptedFingerprints); |
1568 | 0 | char* cur = strtok_s(copy, ",", &context); |
1569 | 0 | while (cur) |
1570 | 0 | { |
1571 | 0 | char* subcontext = nullptr; |
1572 | 0 | const char* h = strtok_s(cur, ":", &subcontext); |
1573 | |
|
1574 | 0 | if (!h) |
1575 | 0 | goto next; |
1576 | | |
1577 | 0 | { |
1578 | 0 | const char* fp = h + strlen(h) + 1; |
1579 | 0 | if (compare_fingerprint_all(fp, h, cert)) |
1580 | 0 | { |
1581 | 0 | rc = TRUE; |
1582 | 0 | break; |
1583 | 0 | } |
1584 | 0 | } |
1585 | 0 | next: |
1586 | 0 | cur = strtok_s(nullptr, ",", &context); |
1587 | 0 | } |
1588 | 0 | free(copy); |
1589 | 0 | } |
1590 | | |
1591 | 0 | return rc; |
1592 | 0 | } |
1593 | | |
1594 | | WINPR_ATTR_NODISCARD |
1595 | | static BOOL accept_cert(rdpTls* tls, const rdpCertificate* cert) |
1596 | 0 | { |
1597 | 0 | WINPR_ASSERT(tls); |
1598 | 0 | WINPR_ASSERT(tls->context); |
1599 | 0 | WINPR_ASSERT(cert); |
1600 | |
|
1601 | 0 | FreeRDP_Settings_Keys_String id = FreeRDP_AcceptedCert; |
1602 | 0 | FreeRDP_Settings_Keys_UInt32 lid = FreeRDP_AcceptedCertLength; |
1603 | |
|
1604 | 0 | rdpSettings* settings = tls->context->settings; |
1605 | 0 | WINPR_ASSERT(settings); |
1606 | |
|
1607 | 0 | if (tls->isGatewayTransport) |
1608 | 0 | { |
1609 | 0 | id = FreeRDP_GatewayAcceptedCert; |
1610 | 0 | lid = FreeRDP_GatewayAcceptedCertLength; |
1611 | 0 | } |
1612 | 0 | else if (is_redirected(tls)) |
1613 | 0 | { |
1614 | 0 | id = FreeRDP_RedirectionAcceptedCert; |
1615 | 0 | lid = FreeRDP_RedirectionAcceptedCertLength; |
1616 | 0 | } |
1617 | |
|
1618 | 0 | size_t pemLength = 0; |
1619 | 0 | char* pem = freerdp_certificate_get_pem_ex(cert, &pemLength, FALSE); |
1620 | 0 | BOOL rc = FALSE; |
1621 | 0 | if (pemLength <= UINT32_MAX) |
1622 | 0 | { |
1623 | 0 | if (freerdp_settings_set_string_len(settings, id, pem, pemLength)) |
1624 | 0 | rc = freerdp_settings_set_uint32(settings, lid, (UINT32)pemLength); |
1625 | 0 | } |
1626 | 0 | free(pem); |
1627 | 0 | return rc; |
1628 | 0 | } |
1629 | | |
1630 | | WINPR_ATTR_NODISCARD |
1631 | | static BOOL tls_extract_full_pem(const rdpCertificate* cert, BYTE** PublicKey, |
1632 | | size_t* PublicKeyLength) |
1633 | 0 | { |
1634 | 0 | if (!cert || !PublicKey) |
1635 | 0 | return FALSE; |
1636 | 0 | *PublicKey = (BYTE*)freerdp_certificate_get_pem(cert, PublicKeyLength); |
1637 | 0 | return *PublicKey != nullptr; |
1638 | 0 | } |
1639 | | |
1640 | | WINPR_ATTR_NODISCARD |
1641 | | static int tls_config_parse_bool(WINPR_JSON* json, const char* opt) |
1642 | 0 | { |
1643 | 0 | WINPR_JSON* val = WINPR_JSON_GetObjectItemCaseSensitive(json, opt); |
1644 | 0 | if (!val || !WINPR_JSON_IsBool(val)) |
1645 | 0 | return -1; |
1646 | | |
1647 | 0 | if (WINPR_JSON_IsTrue(val)) |
1648 | 0 | return 1; |
1649 | 0 | return 0; |
1650 | 0 | } |
1651 | | |
1652 | | WINPR_ATTR_NODISCARD |
1653 | | static int tls_config_check_allowed_hashed(const char* configfile, const rdpCertificate* cert, |
1654 | | WINPR_JSON* json) |
1655 | 0 | { |
1656 | 0 | WINPR_ASSERT(configfile); |
1657 | 0 | WINPR_ASSERT(cert); |
1658 | 0 | WINPR_ASSERT(json); |
1659 | |
|
1660 | 0 | WINPR_JSON* db = WINPR_JSON_GetObjectItemCaseSensitive(json, "certificate-db"); |
1661 | 0 | if (!db || !WINPR_JSON_IsArray(db)) |
1662 | 0 | return 0; |
1663 | | |
1664 | 0 | for (size_t x = 0; x < WINPR_JSON_GetArraySize(db); x++) |
1665 | 0 | { |
1666 | 0 | WINPR_JSON* cur = WINPR_JSON_GetArrayItem(db, x); |
1667 | 0 | if (!cur || !WINPR_JSON_IsObject(cur)) |
1668 | 0 | { |
1669 | 0 | WLog_WARN(TAG, |
1670 | 0 | "[%s] invalid certificate-db entry at position %" PRIuz ": not a JSON object", |
1671 | 0 | configfile, x); |
1672 | 0 | continue; |
1673 | 0 | } |
1674 | | |
1675 | 0 | WINPR_JSON* key = WINPR_JSON_GetObjectItemCaseSensitive(cur, "type"); |
1676 | 0 | if (!key || !WINPR_JSON_IsString(key)) |
1677 | 0 | { |
1678 | 0 | WLog_WARN(TAG, |
1679 | 0 | "[%s] invalid certificate-db entry at position %" PRIuz |
1680 | 0 | ": invalid 'type' element, expected type string", |
1681 | 0 | configfile, x); |
1682 | 0 | continue; |
1683 | 0 | } |
1684 | 0 | WINPR_JSON* val = WINPR_JSON_GetObjectItemCaseSensitive(cur, "hash"); |
1685 | 0 | if (!val || !WINPR_JSON_IsString(val)) |
1686 | 0 | { |
1687 | 0 | WLog_WARN(TAG, |
1688 | 0 | "[%s] invalid certificate-db entry at position %" PRIuz |
1689 | 0 | ": invalid 'hash' element, expected type string", |
1690 | 0 | configfile, x); |
1691 | 0 | continue; |
1692 | 0 | } |
1693 | | |
1694 | 0 | const char* skey = WINPR_JSON_GetStringValue(key); |
1695 | 0 | const char* sval = WINPR_JSON_GetStringValue(val); |
1696 | |
|
1697 | 0 | char* hash = freerdp_certificate_get_fingerprint_by_hash_ex(cert, skey, FALSE); |
1698 | 0 | if (!hash) |
1699 | 0 | { |
1700 | 0 | WLog_WARN(TAG, |
1701 | 0 | "[%s] invalid certificate-db entry at position %" PRIuz |
1702 | 0 | ": hash type '%s' not supported by certificate", |
1703 | 0 | configfile, x, skey); |
1704 | 0 | continue; |
1705 | 0 | } |
1706 | | |
1707 | 0 | const int cmp = _stricmp(hash, sval); |
1708 | 0 | free(hash); |
1709 | |
|
1710 | 0 | if (cmp == 0) |
1711 | 0 | return 1; |
1712 | 0 | } |
1713 | | |
1714 | 0 | return 0; |
1715 | 0 | } |
1716 | | |
1717 | | WINPR_ATTR_NODISCARD |
1718 | | static int tls_config_check_certificate(const rdpCertificate* cert, BOOL* pAllowUserconfig) |
1719 | 0 | { |
1720 | 0 | WINPR_ASSERT(cert); |
1721 | 0 | WINPR_ASSERT(pAllowUserconfig); |
1722 | |
|
1723 | 0 | int rc = 0; |
1724 | 0 | const char configfile[] = "certificates.json"; |
1725 | 0 | WINPR_JSON* json = freerdp_GetJSONConfigFile(TRUE, configfile); |
1726 | |
|
1727 | 0 | if (!json) |
1728 | 0 | { |
1729 | 0 | WLog_DBG(TAG, "No or no valid configuration file for certificate handling, asking user"); |
1730 | 0 | goto fail; |
1731 | 0 | } |
1732 | | |
1733 | 0 | if (tls_config_parse_bool(json, "deny") > 0) |
1734 | 0 | { |
1735 | 0 | WLog_WARN(TAG, "[%s] certificate denied by configuration", configfile); |
1736 | 0 | rc = -1; |
1737 | 0 | goto fail; |
1738 | 0 | } |
1739 | | |
1740 | 0 | if (tls_config_parse_bool(json, "ignore") > 0) |
1741 | 0 | { |
1742 | 0 | WLog_WARN(TAG, "[%s] certificate ignored by configuration", configfile); |
1743 | 0 | rc = 1; |
1744 | 0 | goto fail; |
1745 | 0 | } |
1746 | | |
1747 | 0 | if (tls_config_check_allowed_hashed(configfile, cert, json) > 0) |
1748 | 0 | { |
1749 | 0 | WLog_WARN(TAG, "[%s] certificate manually accepted by configuration", configfile); |
1750 | 0 | rc = 1; |
1751 | 0 | goto fail; |
1752 | 0 | } |
1753 | | |
1754 | 0 | if (tls_config_parse_bool(json, "deny-userconfig") > 0) |
1755 | 0 | { |
1756 | 0 | WLog_WARN(TAG, "[%s] configuration denies user to accept certificates", configfile); |
1757 | 0 | rc = -1; |
1758 | 0 | goto fail; |
1759 | 0 | } |
1760 | | |
1761 | 0 | fail: |
1762 | |
|
1763 | 0 | *pAllowUserconfig = (rc == 0); |
1764 | 0 | WINPR_JSON_Delete(json); |
1765 | 0 | return rc; |
1766 | 0 | } |
1767 | | |
1768 | | int tls_verify_certificate(rdpTls* tls, const rdpCertificate* cert, const char* hostname, |
1769 | | UINT16 port) |
1770 | 0 | { |
1771 | 0 | int match = 0; |
1772 | 0 | size_t length = 0; |
1773 | 0 | BOOL certificate_status = 0; |
1774 | 0 | char* common_name = nullptr; |
1775 | 0 | size_t common_name_length = 0; |
1776 | 0 | int verification_status = -1; |
1777 | 0 | BOOL hostname_match = FALSE; |
1778 | 0 | rdpCertificateData* certificate_data = nullptr; |
1779 | 0 | BYTE* pemCert = nullptr; |
1780 | 0 | DWORD flags = VERIFY_CERT_FLAG_NONE; |
1781 | |
|
1782 | 0 | WINPR_ASSERT(tls); |
1783 | |
|
1784 | 0 | rdpContext* context = tls->context; |
1785 | 0 | WINPR_ASSERT(context); |
1786 | |
|
1787 | 0 | freerdp* instance = context->instance; |
1788 | 0 | WINPR_ASSERT(instance); |
1789 | |
|
1790 | 0 | const rdpSettings* settings = context->settings; |
1791 | 0 | WINPR_ASSERT(settings); |
1792 | |
|
1793 | 0 | if (freerdp_shall_disconnect_context(context)) |
1794 | 0 | return -1; |
1795 | | |
1796 | 0 | if (!tls_extract_full_pem(cert, &pemCert, &length)) |
1797 | 0 | goto end; |
1798 | | |
1799 | | /* Check, if we already accepted this key. */ |
1800 | 0 | if (is_accepted(tls, cert)) |
1801 | 0 | { |
1802 | 0 | verification_status = 1; |
1803 | 0 | goto end; |
1804 | 0 | } |
1805 | | |
1806 | 0 | if (is_accepted_fingerprint(cert, settings->CertificateAcceptedFingerprints)) |
1807 | 0 | { |
1808 | 0 | verification_status = 1; |
1809 | 0 | goto end; |
1810 | 0 | } |
1811 | | |
1812 | 0 | if (tls->isGatewayTransport || is_redirected(tls)) |
1813 | 0 | flags |= VERIFY_CERT_FLAG_LEGACY; |
1814 | |
|
1815 | 0 | if (tls->isGatewayTransport) |
1816 | 0 | flags |= VERIFY_CERT_FLAG_GATEWAY; |
1817 | |
|
1818 | 0 | if (is_redirected(tls)) |
1819 | 0 | flags |= VERIFY_CERT_FLAG_REDIRECT; |
1820 | | |
1821 | | /* Certificate management is done by the application */ |
1822 | 0 | if (settings->ExternalCertificateManagement) |
1823 | 0 | { |
1824 | 0 | if (instance->VerifyX509Certificate) |
1825 | 0 | verification_status = |
1826 | 0 | instance->VerifyX509Certificate(instance, pemCert, length, hostname, port, flags); |
1827 | 0 | else |
1828 | 0 | WLog_ERR(TAG, "No VerifyX509Certificate callback registered!"); |
1829 | |
|
1830 | 0 | if (verification_status > 0) |
1831 | 0 | { |
1832 | 0 | if (!accept_cert(tls, cert)) |
1833 | 0 | goto end; |
1834 | 0 | } |
1835 | 0 | else if (verification_status < 0) |
1836 | 0 | { |
1837 | 0 | WLog_ERR(TAG, "VerifyX509Certificate failed: (length = %" PRIuz ") status: [%d] %s", |
1838 | 0 | length, verification_status, pemCert); |
1839 | 0 | goto end; |
1840 | 0 | } |
1841 | 0 | } |
1842 | | /* ignore certificate verification if user explicitly required it (discouraged) */ |
1843 | 0 | else if (freerdp_settings_get_bool(settings, FreeRDP_IgnoreCertificate)) |
1844 | 0 | { |
1845 | 0 | WLog_WARN(TAG, "[DANGER] Certificate not checked, /cert:ignore in use."); |
1846 | 0 | WLog_WARN(TAG, "[DANGER] This prevents MITM attacks from being detected!"); |
1847 | 0 | WLog_WARN(TAG, |
1848 | 0 | "[DANGER] Avoid using this unless in a secure LAN (=no internet) environment"); |
1849 | 0 | verification_status = 1; /* success! */ |
1850 | 0 | } |
1851 | 0 | else if (!tls->isGatewayTransport && (settings->AuthenticationLevel == 0)) |
1852 | 0 | verification_status = 1; /* success! */ |
1853 | 0 | else |
1854 | 0 | { |
1855 | | /* if user explicitly specified a certificate name, use it instead of the hostname */ |
1856 | 0 | if (!tls->isGatewayTransport && settings->CertificateName) |
1857 | 0 | hostname = settings->CertificateName; |
1858 | | |
1859 | | /* attempt verification using OpenSSL and the ~/.freerdp/certs certificate store */ |
1860 | 0 | certificate_status = freerdp_certificate_verify( |
1861 | 0 | cert, freerdp_certificate_store_get_certs_path(tls->certificate_store)); |
1862 | | /* verify certificate name match */ |
1863 | 0 | certificate_data = freerdp_certificate_data_new(hostname, port, cert); |
1864 | 0 | if (!certificate_data) |
1865 | 0 | goto end; |
1866 | | /* extra common name and alternative names */ |
1867 | 0 | common_name = freerdp_certificate_get_common_name(cert, &common_name_length); |
1868 | |
|
1869 | 0 | const size_t hostlen = strlen(hostname); |
1870 | 0 | if (freerdp_certificate_matches_hostname(cert, hostname, hostlen)) |
1871 | 0 | hostname_match = TRUE; |
1872 | | |
1873 | | /* if the certificate is valid and the certificate name matches, verification succeeds |
1874 | | */ |
1875 | 0 | if (certificate_status && hostname_match) |
1876 | 0 | verification_status = 1; /* success! */ |
1877 | |
|
1878 | 0 | if (!hostname_match) |
1879 | 0 | flags |= VERIFY_CERT_FLAG_MISMATCH; |
1880 | |
|
1881 | 0 | BOOL allowUserconfig = TRUE; |
1882 | 0 | if (!certificate_status || !hostname_match) |
1883 | 0 | verification_status = tls_config_check_certificate(cert, &allowUserconfig); |
1884 | | |
1885 | | /* verification could not succeed with OpenSSL, use known_hosts file and prompt user for |
1886 | | * manual verification */ |
1887 | 0 | if (allowUserconfig && (!certificate_status || !hostname_match)) |
1888 | 0 | { |
1889 | 0 | DWORD accept_certificate = 0; |
1890 | 0 | size_t pem_length = 0; |
1891 | 0 | char* issuer = freerdp_certificate_get_issuer(cert); |
1892 | 0 | char* subject = freerdp_certificate_get_subject(cert); |
1893 | 0 | char* pem = freerdp_certificate_get_pem(cert, &pem_length); |
1894 | |
|
1895 | 0 | if (!pem) |
1896 | 0 | goto end; |
1897 | | |
1898 | | /* search for matching entry in known_hosts file */ |
1899 | 0 | match = |
1900 | 0 | freerdp_certificate_store_contains_data(tls->certificate_store, certificate_data); |
1901 | |
|
1902 | 0 | if (match == 1) |
1903 | 0 | { |
1904 | | /* no entry was found in known_hosts file, prompt user for manual verification |
1905 | | */ |
1906 | 0 | if (!hostname_match) |
1907 | 0 | tls_print_certificate_name_mismatch_error(cert, hostname, port); |
1908 | |
|
1909 | 0 | { |
1910 | 0 | const char* type = ""; |
1911 | 0 | if (freerdp_settings_get_bool(settings, FreeRDP_AutoAcceptCertificate)) |
1912 | 0 | type = "tofo (auto-accept)"; |
1913 | |
|
1914 | 0 | if (freerdp_settings_get_bool(settings, FreeRDP_AutoDenyCertificate)) |
1915 | 0 | type = "strict (auto-deny)"; |
1916 | |
|
1917 | 0 | if (freerdp_settings_get_bool(settings, FreeRDP_IgnoreCertificate)) |
1918 | 0 | type = "ignore (no check)"; |
1919 | |
|
1920 | 0 | char* efp = freerdp_certificate_get_fingerprint(cert); |
1921 | 0 | tls_print_new_certificate_warn(tls->certificate_store, hostname, port, type, |
1922 | 0 | efp); |
1923 | 0 | free(efp); |
1924 | 0 | } |
1925 | |
|
1926 | 0 | #if !defined(WITHOUT_FREERDP_3x_DEPRECATED) |
1927 | 0 | WINPR_PRAGMA_DIAG_PUSH |
1928 | 0 | WINPR_PRAGMA_DIAG_IGNORED_DEPRECATED_DECL |
1929 | 0 | #endif |
1930 | | |
1931 | | /* Automatically accept certificate on first use */ |
1932 | 0 | if (settings->AutoAcceptCertificate) |
1933 | 0 | { |
1934 | 0 | WLog_INFO(TAG, "No certificate stored, automatically accepting."); |
1935 | 0 | accept_certificate = 1; |
1936 | 0 | } |
1937 | 0 | else if (settings->AutoDenyCertificate) |
1938 | 0 | { |
1939 | 0 | WLog_INFO(TAG, "No certificate stored, automatically denying."); |
1940 | 0 | accept_certificate = 0; |
1941 | 0 | } |
1942 | 0 | else if (instance->VerifyX509Certificate) |
1943 | 0 | { |
1944 | 0 | int rc = instance->VerifyX509Certificate(instance, pemCert, pem_length, |
1945 | 0 | hostname, port, flags); |
1946 | |
|
1947 | 0 | if (rc == 1) |
1948 | 0 | accept_certificate = 1; |
1949 | 0 | else if (rc > 1) |
1950 | 0 | accept_certificate = 2; |
1951 | 0 | else |
1952 | 0 | accept_certificate = 0; |
1953 | 0 | } |
1954 | 0 | else if (instance->VerifyCertificateEx) |
1955 | 0 | { |
1956 | 0 | const BOOL use_pem = |
1957 | 0 | freerdp_settings_get_bool(settings, FreeRDP_CertificateCallbackPreferPEM); |
1958 | 0 | char* fp = nullptr; |
1959 | 0 | DWORD cflags = flags; |
1960 | 0 | if (use_pem) |
1961 | 0 | { |
1962 | 0 | cflags |= VERIFY_CERT_FLAG_FP_IS_PEM; |
1963 | 0 | fp = pem; |
1964 | 0 | } |
1965 | 0 | else |
1966 | 0 | fp = freerdp_certificate_get_fingerprint(cert); |
1967 | 0 | accept_certificate = instance->VerifyCertificateEx( |
1968 | 0 | instance, hostname, port, common_name, subject, issuer, fp, cflags); |
1969 | 0 | if (!use_pem) |
1970 | 0 | free(fp); |
1971 | 0 | } |
1972 | 0 | #if !defined(WITHOUT_FREERDP_3x_DEPRECATED) |
1973 | 0 | else if (instance->VerifyCertificate) |
1974 | 0 | { |
1975 | 0 | char* fp = freerdp_certificate_get_fingerprint(cert); |
1976 | |
|
1977 | 0 | WLog_WARN(TAG, "The VerifyCertificate callback is deprecated, migrate your " |
1978 | 0 | "application to VerifyCertificateEx"); |
1979 | 0 | accept_certificate = instance->VerifyCertificate(instance, common_name, subject, |
1980 | 0 | issuer, fp, !hostname_match); |
1981 | 0 | free(fp); |
1982 | 0 | } |
1983 | 0 | #endif |
1984 | 0 | } |
1985 | 0 | else if (match == -1) |
1986 | 0 | { |
1987 | 0 | rdpCertificateData* stored_data = |
1988 | 0 | freerdp_certificate_store_load_data(tls->certificate_store, hostname, port); |
1989 | | /* entry was found in known_hosts file, but fingerprint does not match. ask user |
1990 | | * to use it */ |
1991 | 0 | { |
1992 | 0 | char* efp = freerdp_certificate_get_fingerprint(cert); |
1993 | 0 | tls_print_certificate_error(tls->certificate_store, stored_data, hostname, port, |
1994 | 0 | efp); |
1995 | 0 | free(efp); |
1996 | 0 | } |
1997 | |
|
1998 | 0 | if (!stored_data) |
1999 | 0 | WLog_WARN(TAG, "Failed to get certificate entry for %s:%" PRIu16 "", hostname, |
2000 | 0 | port); |
2001 | |
|
2002 | 0 | if (settings->AutoDenyCertificate) |
2003 | 0 | { |
2004 | 0 | WLog_INFO(TAG, "No certificate stored, automatically denying."); |
2005 | 0 | accept_certificate = 0; |
2006 | 0 | } |
2007 | 0 | else if (instance->VerifyX509Certificate) |
2008 | 0 | { |
2009 | 0 | const int rc = |
2010 | 0 | instance->VerifyX509Certificate(instance, pemCert, pem_length, hostname, |
2011 | 0 | port, flags | VERIFY_CERT_FLAG_CHANGED); |
2012 | |
|
2013 | 0 | if (rc == 1) |
2014 | 0 | accept_certificate = 1; |
2015 | 0 | else if (rc > 1) |
2016 | 0 | accept_certificate = 2; |
2017 | 0 | else |
2018 | 0 | accept_certificate = 0; |
2019 | 0 | } |
2020 | 0 | else if (instance->VerifyChangedCertificateEx) |
2021 | 0 | { |
2022 | 0 | DWORD cflags = flags | VERIFY_CERT_FLAG_CHANGED; |
2023 | 0 | const char* old_subject = freerdp_certificate_data_get_subject(stored_data); |
2024 | 0 | const char* old_issuer = freerdp_certificate_data_get_issuer(stored_data); |
2025 | 0 | const char* old_fp = freerdp_certificate_data_get_fingerprint(stored_data); |
2026 | 0 | const char* old_pem = freerdp_certificate_data_get_pem(stored_data); |
2027 | 0 | const BOOL fpIsAllocated = |
2028 | 0 | !old_pem || |
2029 | 0 | !freerdp_settings_get_bool(settings, FreeRDP_CertificateCallbackPreferPEM); |
2030 | 0 | char* fp = nullptr; |
2031 | 0 | if (!fpIsAllocated) |
2032 | 0 | { |
2033 | 0 | cflags |= VERIFY_CERT_FLAG_FP_IS_PEM; |
2034 | 0 | fp = pem; |
2035 | 0 | old_fp = old_pem; |
2036 | 0 | } |
2037 | 0 | else |
2038 | 0 | { |
2039 | 0 | fp = freerdp_certificate_get_fingerprint(cert); |
2040 | 0 | } |
2041 | 0 | accept_certificate = instance->VerifyChangedCertificateEx( |
2042 | 0 | instance, hostname, port, common_name, subject, issuer, fp, old_subject, |
2043 | 0 | old_issuer, old_fp, cflags); |
2044 | 0 | if (fpIsAllocated) |
2045 | 0 | free(fp); |
2046 | 0 | } |
2047 | 0 | #if !defined(WITHOUT_FREERDP_3x_DEPRECATED) |
2048 | 0 | else if (instance->VerifyChangedCertificate) |
2049 | 0 | { |
2050 | 0 | char* fp = freerdp_certificate_get_fingerprint(cert); |
2051 | 0 | const char* old_subject = freerdp_certificate_data_get_subject(stored_data); |
2052 | 0 | const char* old_issuer = freerdp_certificate_data_get_issuer(stored_data); |
2053 | 0 | const char* old_fingerprint = |
2054 | 0 | freerdp_certificate_data_get_fingerprint(stored_data); |
2055 | |
|
2056 | 0 | WLog_WARN(TAG, "The VerifyChangedCertificate callback is deprecated, migrate " |
2057 | 0 | "your application to VerifyChangedCertificateEx"); |
2058 | 0 | accept_certificate = instance->VerifyChangedCertificate( |
2059 | 0 | instance, common_name, subject, issuer, fp, old_subject, old_issuer, |
2060 | 0 | old_fingerprint); |
2061 | 0 | free(fp); |
2062 | 0 | } |
2063 | 0 | #endif |
2064 | |
|
2065 | 0 | freerdp_certificate_data_free(stored_data); |
2066 | 0 | } |
2067 | 0 | else if (match == 0) |
2068 | 0 | accept_certificate = 2; /* success! */ |
2069 | |
|
2070 | 0 | #if !defined(WITHOUT_FREERDP_3x_DEPRECATED) |
2071 | 0 | WINPR_PRAGMA_DIAG_POP |
2072 | 0 | #endif |
2073 | | |
2074 | | /* Save certificate or do a simple accept / reject */ |
2075 | 0 | switch (accept_certificate) |
2076 | 0 | { |
2077 | 0 | case 1: |
2078 | | |
2079 | | /* user accepted certificate, add entry in known_hosts file */ |
2080 | 0 | verification_status = freerdp_certificate_store_save_data( |
2081 | 0 | tls->certificate_store, certificate_data) |
2082 | 0 | ? 1 |
2083 | 0 | : -1; |
2084 | 0 | break; |
2085 | | |
2086 | 0 | case 2: |
2087 | | /* user did accept temporary, do not add to known hosts file */ |
2088 | 0 | verification_status = 1; |
2089 | 0 | break; |
2090 | | |
2091 | 0 | default: |
2092 | | /* user did not accept, abort and do not add entry in known_hosts file */ |
2093 | 0 | verification_status = -1; /* failure! */ |
2094 | 0 | break; |
2095 | 0 | } |
2096 | | |
2097 | 0 | free(issuer); |
2098 | 0 | free(subject); |
2099 | 0 | free(pem); |
2100 | 0 | } |
2101 | | |
2102 | 0 | if (verification_status > 0) |
2103 | 0 | { |
2104 | 0 | if (!accept_cert(tls, cert)) |
2105 | 0 | goto end; |
2106 | 0 | } |
2107 | 0 | } |
2108 | | |
2109 | 0 | end: |
2110 | 0 | freerdp_certificate_data_free(certificate_data); |
2111 | 0 | free(common_name); |
2112 | 0 | free(pemCert); |
2113 | 0 | return verification_status; |
2114 | 0 | } |
2115 | | |
2116 | | void tls_print_new_certificate_warn(rdpCertificateStore* store, const char* hostname, UINT16 port, |
2117 | | const char* type, const char* fingerprint) |
2118 | 0 | { |
2119 | 0 | char* path = freerdp_certificate_store_get_cert_path(store, hostname, port); |
2120 | |
|
2121 | 0 | WLog_ERR(TAG, "The host key for %s:%" PRIu16 " has changed", hostname, port); |
2122 | 0 | WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
2123 | 0 | WLog_ERR(TAG, "@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @"); |
2124 | 0 | WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
2125 | 0 | WLog_ERR(TAG, "IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!"); |
2126 | 0 | WLog_ERR(TAG, "Someone could be eavesdropping on you right now (man-in-the-middle attack)!"); |
2127 | 0 | WLog_ERR(TAG, "It is also possible that a host key has just been changed."); |
2128 | 0 | WLog_ERR(TAG, "The fingerprint for the host key sent by the remote host is %s", fingerprint); |
2129 | 0 | WLog_ERR(TAG, "Please contact your system administrator."); |
2130 | 0 | WLog_ERR(TAG, "Add correct host key in %s to get rid of this message.", path); |
2131 | 0 | WLog_ERR(TAG, "Host key for %s has changed and you have requested %s checking.", hostname, |
2132 | 0 | type); |
2133 | 0 | WLog_ERR(TAG, "Host key verification failed."); |
2134 | |
|
2135 | 0 | free(path); |
2136 | 0 | } |
2137 | | |
2138 | | void tls_print_certificate_error(rdpCertificateStore* store, |
2139 | | WINPR_ATTR_UNUSED rdpCertificateData* stored_data, |
2140 | | const char* hostname, UINT16 port, const char* fingerprint) |
2141 | 0 | { |
2142 | 0 | char* path = freerdp_certificate_store_get_cert_path(store, hostname, port); |
2143 | |
|
2144 | 0 | WLog_ERR(TAG, "New host key for %s:%" PRIu16, hostname, port); |
2145 | 0 | WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
2146 | 0 | WLog_ERR(TAG, "@ WARNING: NEW HOST IDENTIFICATION! @"); |
2147 | 0 | WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
2148 | |
|
2149 | 0 | WLog_ERR(TAG, "The fingerprint for the host key sent by the remote host is %s", fingerprint); |
2150 | 0 | WLog_ERR(TAG, "Please contact your system administrator."); |
2151 | 0 | WLog_ERR(TAG, "Add correct host key in %s to get rid of this message.", path); |
2152 | |
|
2153 | 0 | free(path); |
2154 | 0 | } |
2155 | | |
2156 | | void tls_print_certificate_name_mismatch_error(const rdpCertificate* cert, const char* hostname, |
2157 | | UINT16 port) |
2158 | 0 | { |
2159 | 0 | WINPR_ASSERT(nullptr != hostname); |
2160 | 0 | WINPR_ASSERT(cert); |
2161 | |
|
2162 | 0 | size_t common_name_length = 0; |
2163 | 0 | char* common_name = freerdp_certificate_get_common_name(cert, &common_name_length); |
2164 | |
|
2165 | 0 | size_t dnscount = 0; |
2166 | 0 | size_t* dnslengths = nullptr; |
2167 | 0 | char** dnsnames = freerdp_certificate_get_dns_names(cert, &dnscount, &dnslengths); |
2168 | |
|
2169 | 0 | size_t ipcount = 0; |
2170 | 0 | size_t* iplengths = nullptr; |
2171 | 0 | char** ipnames = freerdp_certificate_get_ip_names(cert, &ipcount, &iplengths); |
2172 | |
|
2173 | 0 | size_t count = dnscount; |
2174 | 0 | char** names = dnsnames; |
2175 | 0 | const BOOL isIP = utils_is_valid_ip(hostname); |
2176 | 0 | if (isIP) |
2177 | 0 | { |
2178 | 0 | count = ipcount; |
2179 | 0 | names = ipnames; |
2180 | 0 | } |
2181 | |
|
2182 | 0 | WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
2183 | 0 | WLog_ERR(TAG, "@ WARNING: CERTIFICATE NAME MISMATCH! @"); |
2184 | 0 | WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
2185 | 0 | WLog_ERR(TAG, "The hostname used for this connection (%s:%" PRIu16 ") ", hostname, port); |
2186 | 0 | WLog_ERR(TAG, "does not match %s given in the certificate:", |
2187 | 0 | count < 1 ? "the name" : "any of the names"); |
2188 | 0 | WLog_ERR(TAG, "Common Name (CN):"); |
2189 | 0 | WLog_ERR(TAG, "\t%s", common_name ? common_name : "no CN found in certificate"); |
2190 | |
|
2191 | 0 | if (count > 0) |
2192 | 0 | { |
2193 | 0 | WINPR_ASSERT(nullptr != names); |
2194 | 0 | WLog_ERR(TAG, "Alternative names:"); |
2195 | |
|
2196 | 0 | for (size_t index = 0; index < count; index++) |
2197 | 0 | { |
2198 | 0 | WINPR_ASSERT(names[index]); |
2199 | 0 | WLog_ERR(TAG, "\t %s", names[index]); |
2200 | 0 | } |
2201 | 0 | } |
2202 | |
|
2203 | 0 | WLog_ERR(TAG, "A valid certificate for the wrong name should NOT be trusted!"); |
2204 | |
|
2205 | 0 | freerdp_certificate_free_dns_names(dnscount, dnslengths, dnsnames); |
2206 | 0 | freerdp_certificate_free_ip_names(ipcount, iplengths, ipnames); |
2207 | 0 | winpr_znfree(common_name, common_name_length); |
2208 | 0 | } |
2209 | | |
2210 | | rdpTls* freerdp_tls_new(rdpContext* context) |
2211 | 0 | { |
2212 | 0 | rdpTls* tls = nullptr; |
2213 | 0 | tls = (rdpTls*)calloc(1, sizeof(rdpTls)); |
2214 | |
|
2215 | 0 | if (!tls) |
2216 | 0 | return nullptr; |
2217 | | |
2218 | 0 | tls->context = context; |
2219 | |
|
2220 | 0 | if (!freerdp_settings_get_bool(tls->context->settings, FreeRDP_ServerMode)) |
2221 | 0 | { |
2222 | 0 | tls->certificate_store = freerdp_certificate_store_new(tls->context->settings); |
2223 | |
|
2224 | 0 | if (!tls->certificate_store) |
2225 | 0 | goto out_free; |
2226 | 0 | } |
2227 | | |
2228 | 0 | tls->alertLevel = TLS_ALERT_LEVEL_WARNING; |
2229 | 0 | tls->alertDescription = TLS_ALERT_DESCRIPTION_CLOSE_NOTIFY; |
2230 | 0 | return tls; |
2231 | 0 | out_free: |
2232 | 0 | free(tls); |
2233 | 0 | return nullptr; |
2234 | 0 | } |
2235 | | |
2236 | | void freerdp_tls_free(rdpTls* tls) |
2237 | 0 | { |
2238 | 0 | if (!tls) |
2239 | 0 | return; |
2240 | | |
2241 | 0 | tls_reset(tls); |
2242 | |
|
2243 | 0 | if (tls->certificate_store) |
2244 | 0 | { |
2245 | 0 | freerdp_certificate_store_free(tls->certificate_store); |
2246 | 0 | tls->certificate_store = nullptr; |
2247 | 0 | } |
2248 | |
|
2249 | 0 | free(tls); |
2250 | 0 | } |