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