Welcome to mirror list, hosted at ThFree Co, Russian Federation.

network.c « libfreerdp-core - github.com/FreeRDP/FreeRDP-old.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5d9904d716a81fd716267ac25c19309de4f88c53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
   FreeRDP: A Remote Desktop Protocol client.
   Network Transport Abstraction Layer

   Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#include <freerdp/types/base.h>
#include <freerdp/utils/memory.h>

#include "network.h"

/* Initialize and return STREAM.
 * The stream will have room for at least min_size.
 * The tcp layers out stream will be used. */

STREAM
network_stream_init(rdpNetwork * net, uint32 min_size)
{
	STREAM result = &(net->out);

	if (min_size > result->size)
	{
		result->data = (uint8 *) xrealloc(result->data, min_size);
		result->size = min_size;
	}

	result->p = result->data;
	result->end = result->data + result->size;

	return result;
}

#ifndef DISABLE_TLS

/* verify SSL/TLS connection integrity. 2 checks are carried out. First make sure that the
 * certificate is assigned to the server we're connected to, and second make sure that the
 * certificate is signed by a trusted certification authority
 */

RD_BOOL
network_verify_tls(rdpNetwork * net)
{
	char * issuer;
	char * subject;
	char * fingerprint;
	CryptoCert cert;
	RD_BOOL verified = False;

	cert = tls_get_certificate(net->tls);

	if (!cert)
	{
		goto exit;
	}

	subject = crypto_cert_get_subject(cert);
	issuer = crypto_cert_get_issuer(cert);
	fingerprint = crypto_cert_get_fingerprint(cert);

	verified = tls_verify(net->tls, net->server);

	if (verified != False)
		verified = crypto_cert_verify_peer_identity(cert, net->server);

	verified = ui_check_certificate(net->rdp->inst, fingerprint, subject, issuer, verified);

	xfree(fingerprint);
	xfree(subject);
	xfree(issuer);

exit:
	if (cert)
	{
		crypto_cert_free(cert);
		cert = NULL;
	}

	return verified;
}
#endif

RD_BOOL
network_connect_rdp(rdpNetwork * net)
{
	RD_BOOL status = False;

	printf("Standard RDP encryption negotiated\n");

	status = mcs_connect(net->mcs);

	if (status && net->rdp->settings->encryption)
		sec_establish_key(net->sec);

	return status;
}

RD_BOOL
network_connect_tls(rdpNetwork * net)
{
	RD_BOOL status = False;
	net->tls = tls_new();

	if (!tls_connect(net->tls, net->tcp->sockfd))
		return False;

	if (!network_verify_tls(net))
		return False;

	net->tls_connected = 1;
	net->rdp->settings->encryption = 0;

	status = mcs_connect(net->mcs);

	return status;
}

RD_BOOL
network_connect_nla(rdpNetwork * net)
{
	/* TLS with NLA was successfully negotiated */

	RD_BOOL status = 1;
	net->tls = tls_new();

	if (!tls_connect(net->tls, net->tcp->sockfd))
		return False;

	if (!network_verify_tls(net))
		return False;

	net->tls_connected = 1;
	net->rdp->settings->encryption = 0;

	if (!net->rdp->settings->autologin)
		if (!ui_authenticate(net->rdp->inst))
			return False;

	net->credssp = credssp_new(net);

	if (credssp_authenticate(net->credssp) < 0)
	{
		printf("Authentication failure, check credentials.\n"
				"If credentials are valid, the NTLMSSP implementation may be to blame.\n");
		credssp_free(net->credssp);
		return 0;
	}

	credssp_free(net->credssp);
	status = mcs_connect(net->mcs);

	return status;
}

RD_BOOL
network_connect(rdpNetwork * net, char* server, char* username, int port)
{
	NEGO *nego = net->iso->nego;

	net->port = port;
	net->server = server;
	net->username = username;
	net->license->license_issued = 0;

	if (net->rdp->settings->nla_security)
		nego->enabled_protocols[PROTOCOL_NLA] = 1;
	if (net->rdp->settings->tls_security)
		nego->enabled_protocols[PROTOCOL_TLS] = 1;
	if (net->rdp->settings->rdp_security)
		nego->enabled_protocols[PROTOCOL_RDP] = 1;

	if (!iso_connect(net->iso, net->server, net->username, net->port))
		return False;

#ifndef DISABLE_TLS
	if(nego->selected_protocol & PROTOCOL_NLA)
	{
		/* TLS with NLA was successfully negotiated */
		printf("TLS encryption with NLA negotiated\n");
		return network_connect_nla(net);
	}
	else if(nego->selected_protocol & PROTOCOL_TLS)
	{
		/* TLS without NLA was successfully negotiated */
		printf("TLS encryption negotiated\n");
		return network_connect_tls(net);
	}
	else
#endif
	{
		return network_connect_rdp(net);
	}

	return False;
}

void
network_disconnect(rdpNetwork * net)
{
#ifndef DISABLE_TLS
	if (net->tls)
		tls_disconnect(net->tls);
#endif
	tcp_disconnect(net->tcp);
}

void
network_send(rdpNetwork * net, STREAM s)
{
#ifndef DISABLE_TLS
	if (net->tls_connected)
	{
		tls_write(net->tls, (char*) s->data, s->end - s->data);
	}
	else
#endif
	{
		tcp_write(net->tcp, (char*) s->data, s->end - s->data);
	}
}

STREAM
network_recv(rdpNetwork * net, STREAM s, uint32 length)
{
	int rcvd = 0;
	uint32 p_offset;
	uint32 new_length;
	uint32 end_offset;

	if (s == NULL)
	{
		/* read into "new" stream */
		if (length > net->in.size)
		{
			net->in.data = (uint8 *) xrealloc(net->in.data, length);
			net->in.size = length;
		}

		net->in.end = net->in.p = net->in.data;
		s = &(net->in);
	}
	else
	{
		/* append to existing stream */
		new_length = (s->end - s->data) + length;
		if (new_length > s->size)
		{
			p_offset = s->p - s->data;
			end_offset = s->end - s->data;
			s->data = (uint8 *) xrealloc(s->data, new_length);
			s->size = new_length;
			s->p = s->data + p_offset;
			s->end = s->data + end_offset;
		}
	}

	while (length > 0)
	{
#ifndef DISABLE_TLS
		if (net->tls_connected)
		{
			rcvd = tls_read(net->tls, (char*) s->end, length);

			if (rcvd < 0)
				return NULL;
		}
		else
#endif
		{
			rcvd = tcp_read(net->tcp, (char*) s->end, length);
		}

		s->end += rcvd;
		length -= rcvd;
	}

	return s;
}

rdpNetwork*
network_new(rdpRdp * rdp)
{
	rdpNetwork * self;

	self = (rdpNetwork *) xmalloc(sizeof(rdpNetwork));

	if (self != NULL)
	{
		memset(self, 0, sizeof(rdpNetwork));
		self->rdp = rdp;
		self->sec = rdp->sec;
		self->mcs = mcs_new(self);
		self->tcp = tcp_new(self);
		self->nego = nego_new(self);
		self->iso = iso_new(self);
		self->license = license_new(self);
		self->sec->net = self;

		self->in.size = 4096;
		self->in.data = (uint8 *) xmalloc(self->in.size);

		self->out.size = 4096;
		self->out.data = (uint8 *) xmalloc(self->out.size);
	}

	return self;
}

void
network_free(rdpNetwork * net)
{
	if (net != NULL)
	{
		xfree(net->in.data);
		xfree(net->out.data);

		if (net->tcp != NULL)
			tcp_free(net->tcp);

		if (net->iso != NULL)
			iso_free(net->iso);

		if (net->mcs != NULL)
			mcs_free(net->mcs);

		if (net->nego != NULL)
			nego_free(net->nego);

		if (net->license != NULL)
			license_free(net->license);

		xfree(net);
	}
}