summaryrefslogtreecommitdiff
path: root/src/libcharon/plugins/addrblock/addrblock_narrow.c
blob: f85fa78d619386a63f13a204c78d47984fc17d68 (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
/*
 * Copyright (C) 2010 Martin Willi
 * Copyright (C) 2010 revosec AG
 * Copyright (C) 2009 Andreas Steffen
 * 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;
};

/**
 * Check if the negotiated TS list is acceptable by X509 ipAddrBlock constraints
 */
static bool check_constraints(ike_sa_t *ike_sa, linked_list_t *list)
{
	auth_cfg_t *auth;
	enumerator_t *auth_enum;
	certificate_t *cert = NULL;

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

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

		if (x509->get_flags(x509) & X509_IP_ADDR_BLOCKS)
		{
			enumerator_t *enumerator, *block_enum;
			traffic_selector_t *ts, *block_ts;

			DBG1(DBG_IKE, "checking certificate-based traffic selector "
						  "constraints [RFC 3779]");
			enumerator = list->create_enumerator(list);
			while (enumerator->enumerate(enumerator, &ts))
			{
				bool contained = FALSE;

				block_enum = x509->create_ipAddrBlock_enumerator(x509);
				while (block_enum->enumerate(block_enum, &block_ts))
				{
					if (ts->is_contained_in(ts, block_ts))
					{
						DBG1(DBG_IKE, "  TS %R is contained in address block"
									  " constraint %R", ts, block_ts);
						contained = TRUE;
						break;
					}
				}
				block_enum->destroy(block_enum);

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

/**
 * Delete all traffic selectors in a list
 */
static void flush_ts_list(linked_list_t *list)
{
	traffic_selector_t *ts;

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

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_POST_AUTH:
		case NARROW_INITIATOR_POST_NOAUTH:
			if (!check_constraints(ike_sa, remote))
			{
				flush_ts_list(local);
				flush_ts_list(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;
}