File: gnutls.c

package info (click to toggle)
ctrlproxy 3.0.7-1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 1,668 kB
  • ctags: 2,045
  • sloc: ansic: 15,358; sh: 2,805; xml: 1,078; python: 346; makefile: 302; perl: 18
file content (592 lines) | stat: -rw-r--r-- 13,179 bytes parent folder | download
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * soup-gnutls.c
 *
 * Authors:
 *      Ian Peters <itp@ximian.com>
 *
 * Modified for ctrlproxy by Jelmer Vernooij <jelmer@samba.org>
 *
 * Copyright (C) 2003, Ximian, Inc.
 */

#include "internals.h"

#include <errno.h>
#include <stdlib.h>
#include <string.h>

#include <glib.h>

#include <gcrypt.h>
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>

#include "ssl.h"

gboolean ssl_supported = TRUE;

#define DH_BITS 1024

typedef struct {
	gnutls_certificate_credentials cred;
	gboolean have_ca_file;
} GNUTLSCred;

typedef struct {
	GIOChannel channel;
	int fd;
	GIOChannel *real_sock;
	gnutls_session session;
	GNUTLSCred *cred;
	char *hostname;
	gboolean established;
	SSLType type;
} GNUTLSChannel;

static gboolean
verify_certificate (gnutls_session session, const char *hostname, GError **err)
{
	int status;

	status = gnutls_certificate_verify_peers (session);

	if (status == GNUTLS_E_NO_CERTIFICATE_FOUND) {
		g_set_error (err, SSL_ERROR,
			     SSL_ERROR_CERTIFICATE,
			     "No SSL certificate was sent.");
		return FALSE;
	}

	if (status & GNUTLS_CERT_INVALID ||
#ifdef GNUTLS_CERT_NOT_TRUSTED
	    status & GNUTLS_CERT_NOT_TRUSTED ||
#endif
	    status & GNUTLS_CERT_REVOKED)
	{
		g_set_error (err, SSL_ERROR,
			     SSL_ERROR_CERTIFICATE,
			     "The SSL certificate is not trusted.");
		return FALSE;
	}

	if (gnutls_certificate_expiration_time_peers (session) < time (0)) {
		g_set_error (err, SSL_ERROR,
			     SSL_ERROR_CERTIFICATE,
			     "The SSL certificate has expired.");
		return FALSE;
	}

	if (gnutls_certificate_activation_time_peers (session) > time (0)) {
		g_set_error (err, SSL_ERROR,
			     SSL_ERROR_CERTIFICATE,
			     "The SSL certificate is not yet activated.");
		return FALSE;
	}

	if (gnutls_certificate_type_get (session) == GNUTLS_CRT_X509) {
		const gnutls_datum* cert_list;
		guint cert_list_size;
		gnutls_x509_crt cert;

		if (gnutls_x509_crt_init (&cert) < 0) {
			g_set_error (err, SSL_ERROR,
				     SSL_ERROR_CERTIFICATE,
				     "Error initializing SSL certificate.");
			return FALSE;
		}
      
		cert_list = gnutls_certificate_get_peers (
			session, &cert_list_size);

		if (cert_list == NULL) {
			g_set_error (err, SSL_ERROR,
				     SSL_ERROR_CERTIFICATE,
				     "No SSL certificate was found.");
			return FALSE;
		}

		if (gnutls_x509_crt_import (cert, &cert_list[0],
					    GNUTLS_X509_FMT_DER) < 0) {
			g_set_error (err, SSL_ERROR,
				     SSL_ERROR_CERTIFICATE,
				     "The SSL certificate could not be parsed.");
			return FALSE;
		}

		if (!gnutls_x509_crt_check_hostname (cert, hostname)) {
			g_set_error (err, SSL_ERROR,
				     SSL_ERROR_CERTIFICATE,
				     "The SSL certificate does not match the hostname.");
			return FALSE;
		}
	}

	return TRUE;
}

#define GNUTLS_CHANNEL_NONBLOCKING(chan) (fcntl ((chan)->fd, F_GETFL, 0) & O_NONBLOCK)

static GIOStatus
do_handshake (GNUTLSChannel *chan, GError **err)
{
	int result;

again:
	result = gnutls_handshake (chan->session);

	if (result == GNUTLS_E_AGAIN ||
	    result == GNUTLS_E_INTERRUPTED) {
		if (GNUTLS_CHANNEL_NONBLOCKING (chan)) {
			g_set_error (err, SSL_ERROR,
				    (gnutls_record_get_direction (chan->session) ?
				     SSL_ERROR_HANDSHAKE_NEEDS_WRITE :
				     SSL_ERROR_HANDSHAKE_NEEDS_READ),
				    "Handshaking...");
			return G_IO_STATUS_AGAIN;
		} else {
			goto again;
		}
	}

	if (result < 0) {
		g_set_error (err, G_IO_CHANNEL_ERROR,
			     G_IO_CHANNEL_ERROR_FAILED,
			     "Unable to handshake %s", gnutls_strerror(result));

		return G_IO_STATUS_ERROR;
	}

	if (chan->type == SSL_TYPE_CLIENT && chan->cred->have_ca_file &&
	    !verify_certificate (chan->session, chan->hostname, err))
		return G_IO_STATUS_ERROR;

	return G_IO_STATUS_NORMAL;
}

static GIOStatus
_gnutls_read (GIOChannel   *channel,
		  gchar        *buf,
		  gsize         count,
		  gsize        *bytes_read,
		  GError      **err)
{
	GNUTLSChannel *chan = (GNUTLSChannel *) channel;
	gint result;

	*bytes_read = 0;

again:
	if (!chan->established) {
		result = do_handshake (chan, err);

		if (result == G_IO_STATUS_AGAIN ||
		    result == G_IO_STATUS_ERROR)
			return result;

		g_assert(result >= 0);

		chan->established = TRUE;
	}

	result = gnutls_record_recv (chan->session, buf, count);

	if (result == GNUTLS_E_REHANDSHAKE) {
		chan->established = FALSE;
		goto again;
	}

	if ((result == GNUTLS_E_INTERRUPTED) ||
	    (result == GNUTLS_E_AGAIN)) {
		if (GNUTLS_CHANNEL_NONBLOCKING (chan)) {
			return G_IO_STATUS_AGAIN;
		} else {
			goto again;
		}
	}

	if (result < 0) {
		g_set_error (err, G_IO_CHANNEL_ERROR,
			     G_IO_CHANNEL_ERROR_FAILED,
			     "Received corrupted data");
		return G_IO_STATUS_ERROR;
	} else {
		*bytes_read = result;

		return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
	}
}

static GIOStatus
_gnutls_write (GIOChannel   *channel,
		   const gchar  *buf,
		   gsize         count,
		   gsize        *bytes_written,
		   GError      **err)
{
	GNUTLSChannel *chan = (GNUTLSChannel *) channel;
	gint result;

	*bytes_written = 0;

again:
	if (!chan->established) {
		result = do_handshake (chan, err);

		if (result == G_IO_STATUS_AGAIN ||
		    result == G_IO_STATUS_ERROR)
			return result;


		g_assert(result >= 0);

		chan->established = TRUE;
	}

	result = gnutls_record_send (chan->session, buf, count);

	/* This can't actually happen in response to a write, but... */

	if (result == GNUTLS_E_REHANDSHAKE) {
		chan->established = FALSE;
		goto again;
	}

	if ((result == GNUTLS_E_INTERRUPTED) ||
	    (result == GNUTLS_E_AGAIN)) {
		if (GNUTLS_CHANNEL_NONBLOCKING (chan)) {
			return G_IO_STATUS_AGAIN;
		} else {
			goto again;
		}
	}

	if (result < 0) {
		g_set_error (err, G_IO_CHANNEL_ERROR,
			     G_IO_CHANNEL_ERROR_FAILED,
			     "Received corrupted data");
		return G_IO_STATUS_ERROR;
	} else {
		*bytes_written = result;

		return G_IO_STATUS_NORMAL;
	}
}

static GIOStatus
gnutls_seek (GIOChannel  *channel,
		  gint64       offset,
		  GSeekType    type,
		  GError     **err)
{
	GNUTLSChannel *chan = (GNUTLSChannel *) channel;

	return chan->real_sock->funcs->io_seek (channel, offset, type, err);
}

static GIOStatus
gnutls_close (GIOChannel  *channel,
		   GError     **err)
{
	GNUTLSChannel *chan = (GNUTLSChannel *) channel;

	if (chan->established) {
		int ret;

		do {
			ret = gnutls_bye (chan->session, GNUTLS_SHUT_WR);
		} while (ret == GNUTLS_E_INTERRUPTED);
	}

	return chan->real_sock->funcs->io_close (channel, err);
}

static GSource *
gnutls_create_watch (GIOChannel   *channel,
			  GIOCondition  condition)
{
	GNUTLSChannel *chan = (GNUTLSChannel *) channel;

	return chan->real_sock->funcs->io_create_watch (channel,
							condition);
}

static void
_gnutls_free (GIOChannel *channel)
{
	GNUTLSChannel *chan = (GNUTLSChannel *) channel;
	g_io_channel_unref (chan->real_sock);
	gnutls_deinit (chan->session);
	g_free (chan->hostname);
	g_free (chan);
}

static GIOStatus
gnutls_set_flags (GIOChannel  *channel,
		       GIOFlags     flags,
		       GError     **err)
{
	GNUTLSChannel *chan = (GNUTLSChannel *) channel;

	return chan->real_sock->funcs->io_set_flags (channel, flags, err);
}

static GIOFlags
gnutls_get_flags (GIOChannel *channel)
{
	GNUTLSChannel *chan = (GNUTLSChannel *) channel;

	return chan->real_sock->funcs->io_get_flags (channel);
}

static const GIOFuncs gnutls_channel_funcs = {
	_gnutls_read,
	_gnutls_write,
	gnutls_seek,
	gnutls_close,
	gnutls_create_watch,
	_gnutls_free,
	gnutls_set_flags,
	gnutls_get_flags
};

static gnutls_dh_params dh_params = NULL;

static gboolean
init_dh_params (void)
{
	if (gnutls_dh_params_init (&dh_params) != 0)
		goto THROW_CREATE_ERROR;

	if (gnutls_dh_params_generate2 (dh_params, DH_BITS) != 0)
		goto THROW_CREATE_ERROR;

	return TRUE;

THROW_CREATE_ERROR:
	if (dh_params) {
		gnutls_dh_params_deinit (dh_params);
		dh_params = NULL;
	}

	return FALSE;
}

/**
 * ssl_wrap_iochannel:
 * @sock: a #GIOChannel wrapping a TCP socket.
 * @type: whether this is a client or server socket
 * @remote_host: the hostname of the remote machine
 * @credentials: a client or server credentials structure
 *
 * This attempts to wrap a new #GIOChannel around @sock that
 * will SSL-encrypt/decrypt all traffic through it.
 *
 * Return value: an SSL-encrypting #GIOChannel, or %NULL on
 * failure.
 **/
GIOChannel *
ssl_wrap_iochannel (GIOChannel *sock, SSLType type,
			 const char *remote_host, gpointer credentials)
{
	GNUTLSChannel *chan = NULL;
	GIOChannel *gchan = NULL;
	gnutls_session session = NULL;
	GNUTLSCred *cred = (GNUTLSCred *) credentials;
	int sockfd;
	int ret;

	g_return_val_if_fail (sock != NULL, NULL);
	g_return_val_if_fail (credentials != NULL, NULL);

	g_io_channel_set_encoding(sock, NULL, NULL);
	g_io_channel_set_buffered(sock, FALSE);

	sockfd = g_io_channel_unix_get_fd (sock);
	if (!sockfd) {
		g_warning ("Failed to get channel fd.");
		goto THROW_CREATE_ERROR;
	}

	ret = gnutls_init (&session,
			   (type == SSL_TYPE_CLIENT) ? GNUTLS_CLIENT : GNUTLS_SERVER);
	if (ret)
		goto THROW_CREATE_ERROR;

	if (gnutls_set_default_priority (session) != 0)
		goto THROW_CREATE_ERROR;

	if (gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE,
				    cred->cred) != 0)
		goto THROW_CREATE_ERROR;

	if (type == SSL_TYPE_SERVER)
		gnutls_dh_set_prime_bits (session, DH_BITS);

	gnutls_transport_set_ptr (session, GINT_TO_POINTER (sockfd));

	chan = g_new0 (GNUTLSChannel, 1);
	chan->fd = sockfd;
	chan->real_sock = sock;
	chan->session = session;
	chan->cred = cred;
	chan->hostname = g_strdup (remote_host);
	chan->type = type;
	g_io_channel_ref (sock);

	gchan = (GIOChannel *) chan;
	gchan->funcs = (GIOFuncs *) &gnutls_channel_funcs;
	g_io_channel_init (gchan);
	gchan->is_readable = gchan->is_writeable = TRUE;

	return gchan;

 THROW_CREATE_ERROR:
	if (session)
		gnutls_deinit (session);
	return NULL;
}

static gboolean gnutls_inited = FALSE;

static void
_gnutls_init (void)
{
	/* to disallow usage of the blocking /dev/random */
	gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);

	gnutls_global_init ();
	gnutls_inited = TRUE;
}

/**
 * ssl_get_client_credentials:
 * @ca_file: path to a file containing X509-encoded Certificate
 * Authority certificates.
 *
 * Creates an opaque client credentials object which can later be
 * passed to ssl_wrap_iochannel().
 *
 * If @ca_file is non-%NULL, any certificate received from a server
 * must be signed by one of the CAs in the file, or an error will
 * be returned.
 *
 * Return value: the client credentials, which must be freed with
 * ssl_free_client_credentials().
 **/
gpointer
ssl_get_client_credentials (const char *ca_file)
{
	GNUTLSCred *cred;
	int status;

	if (!gnutls_inited)
		_gnutls_init ();

	cred = g_new0 (GNUTLSCred, 1);
	gnutls_certificate_allocate_credentials (&cred->cred);

	if (ca_file) {
		cred->have_ca_file = TRUE;
		status = gnutls_certificate_set_x509_trust_file (
			cred->cred, ca_file, GNUTLS_X509_FMT_PEM);
		if (status < 0) {
			g_warning ("Failed to set SSL trust file (%s).",
				   ca_file);
			/* Since we set have_ca_file though, this just
			 * means that no certs will validate, so we're
			 * ok securitywise if we just return these
			 * creds to the caller.
			 */
		}
	}

	return cred;
}

/**
 * ssl_free_client_credentials:
 * @creds: a client credentials structure returned by
 * ssl_get_client_credentials().
 *
 * Frees @client_creds.
 **/
void
ssl_free_client_credentials (gpointer creds)
{
	GNUTLSCred *cred = creds;

	gnutls_certificate_free_credentials (cred->cred);
	g_free (cred);
}

/**
 * ssl_get_server_credentials:
 * @cert_file: path to a file containing an X509-encoded server
 * certificate
 * @key_file: path to a file containing an X509-encoded key for
 * @cert_file.
 *
 * Creates an opaque server credentials object which can later be
 * passed to ssl_wrap_iochannel().
 *
 * Return value: the server credentials, which must be freed with
 * ssl_free_server_credentials().
 **/
gpointer
ssl_get_server_credentials (const char *cert_file, const char *key_file)
{
	GNUTLSCred *cred;

	if (!gnutls_inited)
		_gnutls_init ();
	if (!dh_params) {
		if (!init_dh_params ())
			return NULL;
	}

	cred = g_new0 (GNUTLSCred, 1);
	gnutls_certificate_allocate_credentials (&cred->cred);

	if (gnutls_certificate_set_x509_key_file (cred->cred,
						  cert_file, key_file,
						  GNUTLS_X509_FMT_PEM) != 0) {
		g_warning ("Failed to set SSL certificate and key files "
			   "(%s, %s).", cert_file, key_file);
		ssl_free_server_credentials (cred);
		return NULL;
	}

	gnutls_certificate_set_dh_params (cred->cred, dh_params);
	return cred;
}

/**
 * ssl_free_server_credentials:
 * @creds: a server credentials structure returned by
 * ssl_get_server_credentials().
 *
 * Frees @server_creds.
 **/
void
ssl_free_server_credentials (gpointer creds)
{
	GNUTLSCred *cred = creds;

	gnutls_certificate_free_credentials (cred->cred);
	g_free (cred);
}

/**
 * soup_ssl_error_quark:
 *
 * Return value: The quark used as %SOUP_SSL_ERROR
 **/
GQuark
ssl_error_quark (void)
{
	static GQuark error;
	if (!error)
		error = g_quark_from_static_string ("ssl_error_quark");
	return error;
}