summaryrefslogtreecommitdiff
path: root/src/libcharon/plugins/addrblock/addrblock_narrow.c
blob: 8dfad7da73241afe7b563a69ef45f0bd74ed62f9 (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
/*
 * Copyright (C) 2010 Martin Willi
 * Copyright (C) 2010 revosec AG
 * Copyright (C) 2009 Andreas Steffen
 * HSR Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "addrblock_narrow.h"

#include <daemon.h>
#include <credentials/certificates/x509.h>

typedef struct private_addrblock_narrow_t private_addrblock_narrow_t;

/**
 * Private data of an addrblock_narrow_t object.
 */
struct private_addrblock_narrow_t {

	/**
	 * Public addrblock_narrow_t interface.
	 */
	addrblock_narrow_t public;
};

static void narrow_addrblock(private_addrblock_narrow_t *this, ike_sa_t *ike_sa,
							 linked_list_t *list)
{
	certificate_t *cert = NULL;
	enumerator_t *enumerator;
	auth_cfg_t *auth;

	enumerator = ike_sa->create_auth_cfg_enumerator(ike_sa, FALSE);
	while (enumerator->enumerate(enumerator, &auth))
	{
		cert = auth->get(auth, AUTH_HELPER_SUBJECT_CERT);
		if (cert)
		{
			break;
		}
	}
	enumerator->destroy(enumerator);

	if (cert && cert->get_type(cert) == CERT_X509)
	{
		x509_t *x509 = (x509_t*)cert;

		if (x509->get_flags(x509) & X509_IP_ADDR_BLOCKS)
		{
			traffic_selector_t *ts, *block, *subset;
			linked_list_t *original;

			original = linked_list_create();
			while (list->remove_last(list, (void**)&ts) == SUCCESS)
			{
				original->insert_first(original, ts);
			}

			DBG1(DBG_IKE, "checking certificate-based traffic selector "
				 "constraints [RFC 3779]");
			while (original->remove_first(original, (void**)&ts) == SUCCESS)
			{
				bool contained = FALSE;

				enumerator = x509->create_ipAddrBlock_enumerator(x509);
				while (enumerator->enumerate(enumerator, &block))
				{
					subset = ts->get_subset(ts, block);
					if (subset)
					{
						DBG1(DBG_IKE, "  TS %R is contained in address block"
							 " constraint %R (subset %R)", ts, block, subset);
						list->insert_last(list, subset);
						contained = TRUE;
					}
				}
				enumerator->destroy(enumerator);

				if (!contained)
				{
					DBG1(DBG_IKE, "  TS %R is not contained in any"
						 " address block constraint", ts);
				}
				ts->destroy(ts);
			}
			original->destroy(original);
		}
	}
}

METHOD(listener_t, narrow, bool,
	private_addrblock_narrow_t *this, ike_sa_t *ike_sa, child_sa_t *child_sa,
	narrow_hook_t type, linked_list_t *local, linked_list_t *remote)
{
	switch (type)
	{
		case NARROW_RESPONDER:
		case NARROW_INITIATOR_PRE_AUTH:
		case NARROW_INITIATOR_POST_AUTH:
		case NARROW_INITIATOR_POST_NOAUTH:
			narrow_addrblock(this, ike_sa, remote);
			break;
		default:
			break;
	}
	return TRUE;
}

METHOD(addrblock_narrow_t, destroy, void,
	private_addrblock_narrow_t *this)
{
	free(this);
}

/**
 * See header
 */
addrblock_narrow_t *addrblock_narrow_create()
{
	private_addrblock_narrow_t *this;

	INIT(this,
		.public = {
			.listener.narrow = _narrow,
			.destroy = _destroy,
		},
	);

	return &this->public;
}