2 * FreeRDP: A Remote Desktop Protocol Implementation
3 * Transport Layer Security
5 * Copyright 2011-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
27 #include <winpr/crt.h>
28 #include <winpr/sspi.h>
29 #include <winpr/ssl.h>
31 #include <winpr/stream.h>
32 #include <freerdp/utils/ringbuffer.h>
34 #include <freerdp/log.h>
35 #include <freerdp/crypto/tls.h>
36 #include "../core/tcp.h"
42 #ifdef HAVE_VALGRIND_MEMCHECK_H
43 #include <valgrind/memcheck.h>
46 #define TAG FREERDP_TAG("crypto")
52 typedef struct _BIO_RDP_TLS BIO_RDP_TLS;
54 long bio_rdp_tls_callback(BIO* bio, int mode, const char* argp, int argi, long argl, long ret)
59 static int bio_rdp_tls_write(BIO* bio, const char* buf, int size)
62 BIO_RDP_TLS* tls = (BIO_RDP_TLS*) bio->ptr;
67 BIO_clear_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_READ | BIO_FLAGS_IO_SPECIAL);
69 status = SSL_write(tls->ssl, buf, size);
73 switch (SSL_get_error(tls->ssl, status))
76 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
79 case SSL_ERROR_WANT_WRITE:
80 BIO_set_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
83 case SSL_ERROR_WANT_READ:
84 BIO_set_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
87 case SSL_ERROR_WANT_X509_LOOKUP:
88 BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL);
89 bio->retry_reason = BIO_RR_SSL_X509_LOOKUP;
92 case SSL_ERROR_WANT_CONNECT:
93 BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL);
94 bio->retry_reason = BIO_RR_CONNECT;
97 case SSL_ERROR_SYSCALL:
98 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
102 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
110 static int bio_rdp_tls_read(BIO* bio, char* buf, int size)
113 BIO_RDP_TLS* tls = (BIO_RDP_TLS*) bio->ptr;
118 BIO_clear_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_READ | BIO_FLAGS_IO_SPECIAL);
120 status = SSL_read(tls->ssl, buf, size);
124 switch (SSL_get_error(tls->ssl, status))
127 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
130 case SSL_ERROR_WANT_READ:
131 BIO_set_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
134 case SSL_ERROR_WANT_WRITE:
135 BIO_set_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
138 case SSL_ERROR_WANT_X509_LOOKUP:
139 BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL);
140 bio->retry_reason = BIO_RR_SSL_X509_LOOKUP;
143 case SSL_ERROR_WANT_ACCEPT:
144 BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL);
145 bio->retry_reason = BIO_RR_ACCEPT;
148 case SSL_ERROR_WANT_CONNECT:
149 BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL);
150 bio->retry_reason = BIO_RR_CONNECT;
154 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
157 case SSL_ERROR_ZERO_RETURN:
158 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
161 case SSL_ERROR_SYSCALL:
162 if (WSAGetLastError() == WSAEWOULDBLOCK)
164 BIO_set_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
167 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
172 #ifdef HAVE_VALGRIND_MEMCHECK_H
175 VALGRIND_MAKE_MEM_DEFINED(buf, status);
182 static int bio_rdp_tls_puts(BIO* bio, const char* str)
191 status = BIO_write(bio, str, size);
196 static int bio_rdp_tls_gets(BIO* bio, char* str, int size)
201 static long bio_rdp_tls_ctrl(BIO* bio, int cmd, long num, void* ptr)
205 BIO_RDP_TLS* tls = (BIO_RDP_TLS*) bio->ptr;
210 if (!tls->ssl && (cmd != BIO_C_SET_SSL))
216 SSL_shutdown(tls->ssl);
218 if (tls->ssl->handshake_func == tls->ssl->method->ssl_connect)
219 SSL_set_connect_state(tls->ssl);
220 else if (tls->ssl->handshake_func == tls->ssl->method->ssl_accept)
221 SSL_set_accept_state(tls->ssl);
226 status = BIO_ctrl(bio->next_bio, cmd, num, ptr);
227 else if (tls->ssl->rbio)
228 status = BIO_ctrl(tls->ssl->rbio, cmd, num, ptr);
234 status = BIO_ctrl(tls->ssl->rbio, cmd, num, ptr);
241 case BIO_CTRL_SET_CALLBACK:
245 case BIO_CTRL_GET_CALLBACK:
246 *((ULONG_PTR*) ptr) = (ULONG_PTR) SSL_get_info_callback(tls->ssl);
252 SSL_set_connect_state(tls->ssl);
254 SSL_set_accept_state(tls->ssl);
258 case BIO_CTRL_GET_CLOSE:
259 status = bio->shutdown;
262 case BIO_CTRL_SET_CLOSE:
263 bio->shutdown = (int) num;
267 case BIO_CTRL_WPENDING:
268 status = BIO_ctrl(tls->ssl->wbio, cmd, num, ptr);
271 case BIO_CTRL_PENDING:
272 status = SSL_pending(tls->ssl);
274 status = BIO_pending(tls->ssl->rbio);
278 BIO_clear_retry_flags(bio);
279 status = BIO_ctrl(tls->ssl->wbio, cmd, num, ptr);
280 BIO_copy_next_retry(bio);
285 if (bio->next_bio && (bio->next_bio != tls->ssl->rbio))
287 SSL_set_bio(tls->ssl, bio->next_bio, bio->next_bio);
288 CRYPTO_add(&(bio->next_bio->references), 1, CRYPTO_LOCK_BIO);
296 if (tls->ssl->rbio != tls->ssl->wbio)
297 BIO_free_all(tls->ssl->wbio);
300 CRYPTO_add(&(bio->next_bio->references), -1, CRYPTO_LOCK_BIO);
302 tls->ssl->wbio = tls->ssl->rbio = NULL;
310 *((SSL**) ptr) = tls->ssl;
316 bio->shutdown = (int) num;
319 tls->ssl = (SSL*) ptr;
321 rbio = SSL_get_rbio(tls->ssl);
326 BIO_push(rbio, bio->next_bio);
328 bio->next_bio = rbio;
329 CRYPTO_add(&(rbio->references), 1, CRYPTO_LOCK_BIO);
336 case BIO_C_DO_STATE_MACHINE:
337 BIO_clear_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_WRITE | BIO_FLAGS_IO_SPECIAL);
338 bio->retry_reason = 0;
340 status = SSL_do_handshake(tls->ssl);
344 switch (SSL_get_error(tls->ssl, status))
346 case SSL_ERROR_WANT_READ:
347 BIO_set_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
350 case SSL_ERROR_WANT_WRITE:
351 BIO_set_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
354 case SSL_ERROR_WANT_CONNECT:
355 BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY);
356 bio->retry_reason = bio->next_bio->retry_reason;
360 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
367 status = BIO_ctrl(tls->ssl->rbio, cmd, num, ptr);
374 static int bio_rdp_tls_new(BIO* bio)
380 bio->flags = BIO_FLAGS_SHOULD_RETRY;
381 bio->next_bio = NULL;
383 tls = calloc(1, sizeof(BIO_RDP_TLS));
388 bio->ptr = (void*) tls;
393 static int bio_rdp_tls_free(BIO* bio)
400 tls = (BIO_RDP_TLS*) bio->ptr;
407 if (bio->init && tls->ssl)
409 SSL_shutdown(tls->ssl);
422 static long bio_rdp_tls_callback_ctrl(BIO* bio, int cmd, bio_info_cb* fp)
430 tls = (BIO_RDP_TLS*) bio->ptr;
437 case BIO_CTRL_SET_CALLBACK:
438 SSL_set_info_callback(tls->ssl, (void (*)(const SSL *, int, int)) fp);
443 status = BIO_callback_ctrl(tls->ssl->rbio, cmd, fp);
450 #define BIO_TYPE_RDP_TLS 68
452 static BIO_METHOD bio_rdp_tls_methods =
463 bio_rdp_tls_callback_ctrl,
466 BIO_METHOD* BIO_s_rdp_tls(void)
468 return &bio_rdp_tls_methods;
471 BIO* BIO_new_rdp_tls(SSL_CTX* ctx, int client)
476 bio = BIO_new(BIO_s_rdp_tls());
490 SSL_set_connect_state(ssl);
492 SSL_set_accept_state(ssl);
494 BIO_set_ssl(bio, ssl, BIO_CLOSE);
499 static CryptoCert tls_get_certificate(rdpTls* tls, BOOL peer)
503 STACK_OF(X509) *chain;
506 remote_cert = SSL_get_peer_certificate(tls->ssl);
508 remote_cert = X509_dup( SSL_get_certificate(tls->ssl) );
512 WLog_ERR(TAG, "failed to get the server TLS certificate");
516 cert = malloc(sizeof(*cert));
519 X509_free(remote_cert);
523 cert->px509 = remote_cert;
525 /* Get the peer's chain. If it does not exist, we're setting NULL (clean data either way) */
526 chain = SSL_get_peer_cert_chain(tls->ssl);
527 cert->px509chain = chain;
532 static void tls_free_certificate(CryptoCert cert)
534 X509_free(cert->px509);
538 #define TLS_SERVER_END_POINT "tls-server-end-point:"
540 SecPkgContext_Bindings* tls_get_channel_bindings(X509* cert)
543 BYTE CertificateHash[32];
544 UINT32 CertificateHashLength;
545 BYTE* ChannelBindingToken;
546 UINT32 ChannelBindingTokenLength;
547 SEC_CHANNEL_BINDINGS* ChannelBindings;
548 SecPkgContext_Bindings* ContextBindings;
550 ZeroMemory(CertificateHash, sizeof(CertificateHash));
551 X509_digest(cert, EVP_sha256(), CertificateHash, &CertificateHashLength);
553 PrefixLength = strlen(TLS_SERVER_END_POINT);
554 ChannelBindingTokenLength = PrefixLength + CertificateHashLength;
556 ContextBindings = (SecPkgContext_Bindings*) calloc(1, sizeof(SecPkgContext_Bindings));
557 if (!ContextBindings)
560 ContextBindings->BindingsLength = sizeof(SEC_CHANNEL_BINDINGS) + ChannelBindingTokenLength;
561 ChannelBindings = (SEC_CHANNEL_BINDINGS*) calloc(1, ContextBindings->BindingsLength);
562 if (!ChannelBindings)
564 ContextBindings->Bindings = ChannelBindings;
566 ChannelBindings->cbApplicationDataLength = ChannelBindingTokenLength;
567 ChannelBindings->dwApplicationDataOffset = sizeof(SEC_CHANNEL_BINDINGS);
568 ChannelBindingToken = &((BYTE*) ChannelBindings)[ChannelBindings->dwApplicationDataOffset];
570 strcpy((char*) ChannelBindingToken, TLS_SERVER_END_POINT);
571 CopyMemory(&ChannelBindingToken[PrefixLength], CertificateHash, CertificateHashLength);
573 return ContextBindings;
576 free(ContextBindings);
581 #if defined(__APPLE__)
582 BOOL tls_prepare(rdpTls* tls, BIO* underlying, SSL_METHOD* method, int options, BOOL clientMode)
584 BOOL tls_prepare(rdpTls* tls, BIO* underlying, const SSL_METHOD* method, int options, BOOL clientMode)
587 rdpSettings* settings = tls->settings;
589 tls->ctx = SSL_CTX_new(method);
593 WLog_ERR(TAG, "SSL_CTX_new failed");
597 SSL_CTX_set_mode(tls->ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | SSL_MODE_ENABLE_PARTIAL_WRITE);
599 SSL_CTX_set_options(tls->ctx, options);
600 SSL_CTX_set_read_ahead(tls->ctx, 1);
602 if (settings->AllowedTlsCiphers)
604 if (!SSL_CTX_set_cipher_list(tls->ctx, settings->AllowedTlsCiphers))
606 WLog_ERR(TAG, "SSL_CTX_set_cipher_list %s failed", settings->AllowedTlsCiphers);
611 tls->bio = BIO_new_rdp_tls(tls->ctx, clientMode);
613 if (BIO_get_ssl(tls->bio, &tls->ssl) < 0)
615 WLog_ERR(TAG, "unable to retrieve the SSL of the connection");
619 BIO_push(tls->bio, underlying);
620 tls->underlying = underlying;
625 int tls_do_handshake(rdpTls* tls, BOOL clientMode)
628 int verify_status, status;
633 struct pollfd pollfds;
640 status = BIO_do_handshake(tls->bio);
645 if (!BIO_should_retry(tls->bio))
648 /* we select() only for read even if we should test both read and write
649 * depending of what have blocked */
650 fd = BIO_get_fd(tls->bio, NULL);
654 WLog_ERR(TAG, "unable to retrieve BIO fd");
660 pollfds.events = POLLIN;
665 status = poll(&pollfds, 1, 10 * 1000);
667 while ((status < 0) && (errno == EINTR));
672 tv.tv_usec = 10 * 1000; /* 10ms */
674 status = _select(fd + 1, &rset, NULL, NULL, &tv);
678 WLog_ERR(TAG, "error during select()");
684 cert = tls_get_certificate(tls, clientMode);
687 WLog_ERR(TAG, "tls_get_certificate failed to return the server certificate.");
691 tls->Bindings = tls_get_channel_bindings(cert->px509);
694 WLog_ERR(TAG, "unable to retrieve bindings");
699 if (!crypto_cert_get_public_key(cert, &tls->PublicKey, &tls->PublicKeyLength))
701 WLog_ERR(TAG, "crypto_cert_get_public_key failed to return the server public key.");
706 /* Note: server-side NLA needs public keys (keys from us, the server) but no
712 verify_status = tls_verify_certificate(tls, cert, tls->hostname, tls->port);
714 if (verify_status < 1)
716 WLog_ERR(TAG, "certificate not trusted, aborting.");
723 tls_free_certificate(cert);
725 return verify_status;
728 int tls_connect(rdpTls* tls, BIO* underlying)
733 * SSL_OP_NO_COMPRESSION:
735 * The Microsoft RDP server does not advertise support
736 * for TLS compression, but alternative servers may support it.
737 * This was observed between early versions of the FreeRDP server
738 * and the FreeRDP client, and caused major performance issues,
739 * which is why we're disabling it.
741 #ifdef SSL_OP_NO_COMPRESSION
742 options |= SSL_OP_NO_COMPRESSION;
746 * SSL_OP_TLS_BLOCK_PADDING_BUG:
748 * The Microsoft RDP server does *not* support TLS padding.
749 * It absolutely needs to be disabled otherwise it won't work.
751 options |= SSL_OP_TLS_BLOCK_PADDING_BUG;
754 * SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS:
756 * Just like TLS padding, the Microsoft RDP server does not
757 * support empty fragments. This needs to be disabled.
759 options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
761 if (!tls_prepare(tls, underlying, TLSv1_client_method(), options, TRUE))
764 return tls_do_handshake(tls, TRUE);
767 #ifndef OPENSSL_NO_TLSEXT
768 static void tls_openssl_tlsext_debug_callback(SSL *s, int client_server,
769 int type, unsigned char *data, int len, void *arg)
771 /* see code comment in tls_accept() below */
773 if (type == TLSEXT_TYPE_server_name) {
774 WLog_DBG(TAG, "Client uses SNI (extension disabled)");
775 s->servername_done = 2;
780 BOOL tls_accept(rdpTls* tls, BIO* underlying, const char* cert_file, const char* privatekey_file)
787 * We only want SSLv3 and TLSv1, so disable SSLv2.
788 * SSLv3 is used by, eg. Microsoft RDC for Mac OS X.
790 options |= SSL_OP_NO_SSLv2;
793 * SSL_OP_NO_COMPRESSION:
795 * The Microsoft RDP server does not advertise support
796 * for TLS compression, but alternative servers may support it.
797 * This was observed between early versions of the FreeRDP server
798 * and the FreeRDP client, and caused major performance issues,
799 * which is why we're disabling it.
801 #ifdef SSL_OP_NO_COMPRESSION
802 options |= SSL_OP_NO_COMPRESSION;
806 * SSL_OP_TLS_BLOCK_PADDING_BUG:
808 * The Microsoft RDP server does *not* support TLS padding.
809 * It absolutely needs to be disabled otherwise it won't work.
811 options |= SSL_OP_TLS_BLOCK_PADDING_BUG;
814 * SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS:
816 * Just like TLS padding, the Microsoft RDP server does not
817 * support empty fragments. This needs to be disabled.
819 options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
821 if (!tls_prepare(tls, underlying, SSLv23_server_method(), options, FALSE))
824 if (SSL_use_RSAPrivateKey_file(tls->ssl, privatekey_file, SSL_FILETYPE_PEM) <= 0)
826 WLog_ERR(TAG, "SSL_CTX_use_RSAPrivateKey_file failed");
827 WLog_ERR(TAG, "PrivateKeyFile: %s", privatekey_file);
831 if (SSL_use_certificate_file(tls->ssl, cert_file, SSL_FILETYPE_PEM) <= 0)
833 WLog_ERR(TAG, "SSL_use_certificate_file failed");
837 #ifndef OPENSSL_NO_TLSEXT
839 * The Microsoft iOS clients eventually send a null or even double null
840 * terminated hostname in the SNI TLS extension!
841 * If the length indicator does not equal the hostname strlen OpenSSL
842 * will abort (see openssl:ssl/t1_lib.c).
843 * Here is a tcpdump segment of Microsoft Remote Desktop Client Version
844 * 8.1.7 running on an iPhone 4 with iOS 7.1.2 showing the transmitted
845 * SNI hostname TLV blob when connection to server "abcd":
846 * 00 name_type 0x00 (host_name)
847 * 00 06 length_in_bytes 0x0006
848 * 61 62 63 64 00 00 host_name "abcd\0\0"
850 * Currently the only (runtime) workaround is setting an openssl tls
851 * extension debug callback that sets the SSL context's servername_done
852 * to 1 which effectively disables the parsing of that extension type.
855 SSL_set_tlsext_debug_callback(tls->ssl, tls_openssl_tlsext_debug_callback);
858 return tls_do_handshake(tls, FALSE) > 0;
861 BOOL tls_send_alert(rdpTls* tls)
869 if (tls->alertDescription != TLS_ALERT_DESCRIPTION_CLOSE_NOTIFY)
872 * OpenSSL doesn't really expose an API for sending a TLS alert manually.
874 * The following code disables the sending of the default "close notify"
875 * and then proceeds to force sending a custom TLS alert before shutting down.
877 * Manually sending a TLS alert is necessary in certain cases,
878 * like when server-side NLA results in an authentication failure.
881 SSL_set_quiet_shutdown(tls->ssl, 1);
883 if ((tls->alertLevel == TLS_ALERT_LEVEL_FATAL) && (tls->ssl->session))
884 SSL_CTX_remove_session(tls->ssl->ctx, tls->ssl->session);
886 tls->ssl->s3->alert_dispatch = 1;
887 tls->ssl->s3->send_alert[0] = tls->alertLevel;
888 tls->ssl->s3->send_alert[1] = tls->alertDescription;
890 if (tls->ssl->s3->wbuf.left == 0)
891 tls->ssl->method->ssl_dispatch_alert(tls->ssl);
897 BIO *findBufferedBio(BIO *front)
903 if (BIO_method_type(ret) == BIO_TYPE_BUFFERED)
911 int tls_write_all(rdpTls* tls, const BYTE* data, int length)
917 while (offset < length)
919 status = BIO_write(bio, &data[offset], length - offset);
927 if (!BIO_should_retry(bio))
930 if (BIO_write_blocked(bio))
931 status = BIO_wait_write(bio, 100);
932 else if (BIO_read_blocked(bio))
933 status = BIO_wait_read(bio, 100);
945 int tls_set_alert_code(rdpTls* tls, int level, int description)
947 tls->alertLevel = level;
948 tls->alertDescription = description;
953 BOOL tls_match_hostname(char *pattern, int pattern_length, char *hostname)
955 if (strlen(hostname) == pattern_length)
957 if (_strnicmp( hostname, pattern, pattern_length) == 0)
961 if ((pattern_length > 2) && (pattern[0] == '*') && (pattern[1] == '.') && (((int) strlen(hostname)) >= pattern_length))
963 char* check_hostname = &hostname[strlen(hostname) - pattern_length + 1];
965 if (_strnicmp( check_hostname, &pattern[1], pattern_length - 1) == 0)
974 int tls_verify_certificate(rdpTls* tls, CryptoCert cert, char* hostname, int port)
978 char* common_name = NULL;
979 int common_name_length = 0;
980 char** alt_names = NULL;
981 int alt_names_count = 0;
982 int* alt_names_lengths = NULL;
983 BOOL certificate_status;
984 BOOL hostname_match = FALSE;
985 BOOL verification_status = FALSE;
986 rdpCertificateData* certificate_data;
988 if (tls->settings->ExternalCertificateManagement)
995 freerdp* instance = (freerdp*) tls->settings->instance;
998 * Don't manage certificates internally, leave it up entirely to the external client implementation
1001 bio = BIO_new(BIO_s_mem());
1005 WLog_ERR(TAG, "BIO_new() failure");
1009 status = PEM_write_bio_X509(bio, cert->px509);
1013 WLog_ERR(TAG, "PEM_write_bio_X509 failure: %d", status);
1019 pemCert = (BYTE*) malloc(length + 1);
1022 WLog_ERR(TAG, "error allocating pemCert");
1026 status = BIO_read(bio, pemCert, length);
1030 WLog_ERR(TAG, "failed to read certificate");
1036 while (offset >= length)
1041 new_len = length * 2;
1042 new_cert = (BYTE*) realloc(pemCert, new_len + 1);
1048 status = BIO_read(bio, &pemCert[offset], length);
1058 WLog_ERR(TAG, "failed to read certificate");
1063 pemCert[length] = '\0';
1067 if (instance->VerifyX509Certificate)
1069 status = instance->VerifyX509Certificate(instance, pemCert, length, hostname, port, tls->isGatewayTransport);
1072 WLog_ERR(TAG, "(length = %d) status: %d%s", length, status, pemCert);
1080 return (status == 0) ? 0 : 1;
1083 /* ignore certificate verification if user explicitly required it (discouraged) */
1084 if (tls->settings->IgnoreCertificate)
1085 return 1; /* success! */
1087 /* if user explicitly specified a certificate name, use it instead of the hostname */
1088 if (tls->settings->CertificateName)
1089 hostname = tls->settings->CertificateName;
1091 /* attempt verification using OpenSSL and the ~/.freerdp/certs certificate store */
1092 certificate_status = x509_verify_certificate(cert, tls->certificate_store->path);
1094 /* verify certificate name match */
1095 certificate_data = crypto_get_certificate_data(cert->px509, hostname, port);
1098 /* extra common name and alternative names */
1099 common_name = crypto_cert_subject_common_name(cert->px509, &common_name_length);
1100 alt_names = crypto_cert_subject_alt_name(cert->px509, &alt_names_count, &alt_names_lengths);
1102 /* compare against common name */
1106 if (tls_match_hostname(common_name, common_name_length, hostname))
1107 hostname_match = TRUE;
1110 /* compare against alternative names */
1114 for (index = 0; index < alt_names_count; index++)
1116 if (tls_match_hostname(alt_names[index], alt_names_lengths[index], hostname))
1118 hostname_match = TRUE;
1124 /* if the certificate is valid and the certificate name matches, verification succeeds */
1125 if (certificate_status && hostname_match)
1133 verification_status = TRUE; /* success! */
1136 /* if the certificate is valid but the certificate name does not match, warn user, do not accept */
1137 if (certificate_status && !hostname_match)
1138 tls_print_certificate_name_mismatch_error(hostname, port,
1139 common_name, alt_names,
1142 /* verification could not succeed with OpenSSL, use known_hosts file and prompt user for manual verification */
1144 if (!certificate_status)
1149 freerdp* instance = (freerdp*) tls->settings->instance;
1150 BOOL accept_certificate = FALSE;
1152 issuer = crypto_cert_issuer(cert->px509);
1153 subject = crypto_cert_subject(cert->px509);
1154 fingerprint = crypto_cert_fingerprint(cert->px509);
1156 /* search for matching entry in known_hosts file */
1157 match = certificate_data_match(tls->certificate_store, certificate_data);
1161 /* no entry was found in known_hosts file, prompt user for manual verification */
1162 if (!hostname_match)
1163 tls_print_certificate_name_mismatch_error(
1165 common_name, alt_names,
1168 if (instance->VerifyCertificate)
1170 accept_certificate = instance->VerifyCertificate(instance, subject, issuer, fingerprint);
1173 if (!accept_certificate)
1175 /* user did not accept, abort and do not add entry in known_hosts file */
1176 verification_status = FALSE; /* failure! */
1180 /* user accepted certificate, add entry in known_hosts file */
1181 verification_status = certificate_data_print(tls->certificate_store, certificate_data);
1184 else if (match == -1)
1186 char* old_subject = NULL;
1187 char* old_issuer = NULL;
1188 char* old_fingerprint = NULL;
1190 /* entry was found in known_hosts file, but fingerprint does not match. ask user to use it */
1191 tls_print_certificate_error(hostname, port, fingerprint,
1192 tls->certificate_store->file);
1194 if (!certificate_get_stored_data(tls->certificate_store,
1195 certificate_data, &old_subject,
1196 &old_issuer, &old_fingerprint))
1197 WLog_WARN(TAG, "Failed to get certificate entry for %s:hu",
1200 if (instance->VerifyChangedCertificate)
1202 accept_certificate = instance->VerifyChangedCertificate(
1203 instance, subject, issuer,
1204 fingerprint, old_subject, old_issuer,
1208 free(old_fingerprint);
1210 if (!accept_certificate)
1212 /* user did not accept, abort and do not change known_hosts file */
1213 verification_status = FALSE; /* failure! */
1217 /* user accepted new certificate, add replace fingerprint for this host in known_hosts file */
1218 verification_status = certificate_data_replace(tls->certificate_store, certificate_data);
1221 else if (match == 0)
1223 verification_status = TRUE; /* success! */
1231 certificate_data_free(certificate_data);
1238 crypto_cert_subject_alt_name_free(alt_names_count, alt_names_lengths,
1241 return (verification_status == 0) ? 0 : 1;
1244 void tls_print_certificate_error(char* hostname, UINT16 port, char* fingerprint,
1247 WLog_ERR(TAG, "The host key for %s:%hu has changed", hostname, port);
1248 WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1249 WLog_ERR(TAG, "@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @");
1250 WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1251 WLog_ERR(TAG, "IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
1252 WLog_ERR(TAG, "Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
1253 WLog_ERR(TAG, "It is also possible that a host key has just been changed.");
1254 WLog_ERR(TAG, "The fingerprint for the host key sent by the remote host is%s", fingerprint);
1255 WLog_ERR(TAG, "Please contact your system administrator.");
1256 WLog_ERR(TAG, "Add correct host key in %s to get rid of this message.", hosts_file);
1257 WLog_ERR(TAG, "Host key for %s has changed and you have requested strict checking.", hostname);
1258 WLog_ERR(TAG, "Host key verification failed.");
1261 void tls_print_certificate_name_mismatch_error(char* hostname, UINT16 port,
1262 char* common_name, char** alt_names,
1263 int alt_names_count)
1267 assert(NULL != hostname);
1268 WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1269 WLog_ERR(TAG, "@ WARNING: CERTIFICATE NAME MISMATCH! @");
1270 WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1271 WLog_ERR(TAG, "The hostname used for this connection (%s:%hu) ",
1273 WLog_ERR(TAG, "does not match %s given in the certificate:",
1274 alt_names_count < 1 ? "the name" : "any of the names");
1275 WLog_ERR(TAG, "Common Name (CN):");
1276 WLog_ERR(TAG, "\t%s", common_name ? common_name : "no CN found in certificate");
1277 if (alt_names_count > 0)
1279 assert(NULL != alt_names);
1280 WLog_ERR(TAG, "Alternative names:");
1281 for (index = 0; index < alt_names_count; index++)
1283 assert(alt_names[index]);
1284 WLog_ERR(TAG, "\t %s", alt_names[index]);
1287 WLog_ERR(TAG, "A valid certificate for the wrong name should NOT be trusted!");
1290 rdpTls* tls_new(rdpSettings* settings)
1294 tls = (rdpTls*) calloc(1, sizeof(rdpTls));
1299 winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT);
1301 tls->settings = settings;
1303 if (!settings->ServerMode)
1305 tls->certificate_store = certificate_store_new(settings);
1307 if (!tls->certificate_store)
1311 tls->alertLevel = TLS_ALERT_LEVEL_WARNING;
1312 tls->alertDescription = TLS_ALERT_DESCRIPTION_CLOSE_NOTIFY;
1321 void tls_free(rdpTls* tls)
1328 SSL_CTX_free(tls->ctx);
1338 if (tls->underlying)
1340 BIO_free(tls->underlying);
1341 tls->underlying = NULL;
1346 free(tls->PublicKey);
1347 tls->PublicKey = NULL;
1352 free(tls->Bindings->Bindings);
1353 free(tls->Bindings);
1354 tls->Bindings = NULL;
1357 if (tls->certificate_store)
1359 certificate_store_free(tls->certificate_store);
1360 tls->certificate_store = NULL;