The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
chbind.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: ea7ced52c2c8e2e3243f429a1d1edd15c93767d6 $
19 * @file lib/eap/chbind.c
20 * @brief Channel binding
21 *
22 * @copyright 2014 Network RADIUS SAS
23 * @copyright 2014 The FreeRADIUS server project
24 */
25
26RCSID("$Id: ea7ced52c2c8e2e3243f429a1d1edd15c93767d6 $")
27
28#include <freeradius-devel/radius/defs.h>
29#include <freeradius-devel/radius/radius.h>
30#include <freeradius-devel/server/auth.h>
31
32#include <freeradius-devel/io/pair.h>
33#include "chbind.h"
34#include "attrs.h"
35
37{
38 ssize_t slen;
39 size_t total;
40 uint8_t *ptr, *start, *end;
41 fr_pair_t const *vp;
42 fr_dcursor_t cursor;
43
44 total = 0;
45 for (vp = fr_pair_list_head(&request->reply_pairs);
46 vp != NULL;
47 vp = fr_pair_list_next(&request->reply_pairs, vp)) {
48 /*
49 * Skip things which shouldn't be in channel bindings.
50 */
51 if (vp->da->flags.internal || (!vp->da->flags.extra && vp->da->flags.subtype)) continue;
52 if (vp->da == attr_message_authenticator) continue;
53
54 total += 2 + vp->vp_length;
55 }
56
57 /*
58 * No attributes: just send a 1-byte response code.
59 */
60 if (!total) {
61 ptr = start = talloc_zero_array(chbind, uint8_t, 1);
62 } else {
63 ptr = start = talloc_zero_array(chbind, uint8_t, total + 4);
64 }
65 if (!ptr) return false;
66 chbind->response = (chbind_packet_t *) ptr;
67
68 /*
69 * Set the response code. Default to "fail" if none was
70 * specified.
71 */
73 if (vp) {
74 ptr[0] = vp->vp_uint32;
75 } else {
76 ptr[0] = CHBIND_CODE_FAILURE;
77 }
78
79 if (!total) return true; /* nothing to encode */
80
81 /* Write the length field into the header */
82 ptr[1] = (total >> 8) & 0xff;
83 ptr[2] = total & 0xff;
84 ptr[3] = CHBIND_NSID_RADIUS;
85
86 RDEBUG2("Sending chbind response: code %i", (int )(ptr[0]));
87 log_request_pair_list(L_DBG_LVL_1, request, NULL, &request->reply_pairs, NULL);
88
89 /* Encode the chbind attributes into the response */
90 ptr += 4;
91 end = ptr + total;
92
93 fr_pair_dcursor_init(&cursor, &request->reply_pairs);
94 while ((vp = fr_dcursor_current(&cursor)) && (ptr < end)) {
95 /*
96 * Skip things which shouldn't be in channel bindings.
97 *
98 * reply_pairs holds more than RADIUS attributes, and
99 * fr_radius_encode_pair() reads RADIUS flags from every
100 * attribute it is given.
101 */
102 if ((fr_dict_by_da(vp->da) != dict_radius) ||
103 vp->da->flags.internal || (!vp->da->flags.extra && vp->da->flags.subtype) ||
105 fr_dcursor_next(&cursor);
106 continue;
107 }
108
109 slen = fr_radius_encode_pair(&FR_DBUFF_TMP(ptr, end), &cursor, NULL);
110 if (slen < 0) {
111 RPERROR("Failed encoding chbind response");
112 chbind->response = NULL;
113 talloc_free(start);
114 return false;
115 }
116
117 ptr += slen;
118 }
119
120 return true;
121}
122
123
124/*
125 * Parse channel binding packet to obtain data for a specific
126 * NSID.
127 *
128 * See:
129 * http://tools.ietf.org/html/draft-ietf-emu-chbind-13#section-5.3.2
130 */
131static size_t chbind_get_data(chbind_packet_t const *packet,
132 int desired_nsid,
133 uint8_t const **data)
134{
135 uint8_t const *ptr;
136 uint8_t const *end;
137
138 if (packet->code != CHBIND_CODE_REQUEST) {
139 return 0;
140 }
141
142 ptr = (uint8_t const *) packet;
143 end = ptr + talloc_array_length((uint8_t const *) packet);
144
145 ptr++; /* skip the code at the start of the packet */
146 while (ptr < end) {
147 uint8_t nsid;
148 size_t length;
149
150 /*
151 * Need room for length(2) + NSID + data.
152 */
153 if ((end - ptr) < 4) return 0;
154
155 length = fr_nbo_to_uint16(ptr);
156 if (length == 0) return 0;
157
158 if ((ptr + length + 3) > end) return 0;
159
160 nsid = ptr[2];
161 if (nsid == desired_nsid) {
162 ptr += 3;
163 *data = ptr;
164 return length;
165 }
166
167 ptr += 3 + length;
168 }
169
170 return 0;
171}
172
173
175{
177 unlang_result_t result;
178 request_t *fake = NULL;
179 uint8_t const *attr_data;
180 size_t data_len = 0;
181 fr_pair_t *vp;
182 fr_radius_ctx_t common_ctx = {};
184
185 /* check input parameters */
186 fr_assert((request != NULL) &&
187 (chbind != NULL) &&
188 (chbind->request != NULL) &&
189 (chbind->response == NULL));
190
191 /* Set-up the fake request */
193 MEM(fr_pair_prepend_by_da(fake->request_ctx, &vp, &fake->request_pairs, attr_freeradius_proxied_to) >= 0);
194 (void) fr_pair_value_from_str(vp, "127.0.0.1", sizeof("127.0.0.1") - 1, NULL, false);
195
196 /* Add the username to the fake request */
197 if (chbind->username) {
198 MEM(vp = fr_pair_copy(fake->request_ctx, chbind->username));
199 fr_pair_append(&fake->request_pairs, vp);
200 }
201
202 /*
203 * Maybe copy the State over, too?
204 */
205 packet_ctx.common = &common_ctx;
206 packet_ctx.tmp_ctx = talloc_init_const("tmp");
207
208 /* Add the channel binding attributes to the fake packet */
209 data_len = chbind_get_data(chbind->request, CHBIND_NSID_RADIUS, &attr_data);
210 if (data_len) {
211 fr_assert(data_len <= talloc_array_length((uint8_t const *) chbind->request));
212
213 packet_ctx.end = attr_data + data_len;
214
215 while (data_len > 0) {
216 ssize_t attr_len;
217
218 attr_len = fr_radius_decode_pair(fake->request_ctx, &fake->request_pairs,
219 attr_data, data_len, &packet_ctx);
220 if (attr_len <= 0) {
221 /*
222 * If fr_radius_decode_pair fails, return NULL string for
223 * channel binding response.
224 */
226 talloc_free(packet_ctx.tmp_ctx);
228
230 }
231 attr_data += attr_len;
232 data_len -= attr_len;
233 }
234
235 talloc_free_children(packet_ctx.tmp_ctx);
236 }
237 talloc_free(packet_ctx.tmp_ctx);
239
240 /*
241 * Set virtual server based on configuration for channel
242 * bindings, this is hard-coded for now.
243 */
244// fake->server_cs = virtual_server_find("channel_bindings");
245 fake->packet->code = FR_RADIUS_CODE_ACCESS_REQUEST;
246
247 rad_virtual_server(&result, fake);
248 switch (result.rcode) {
249 /* If the virtual server succeeded, build a reply */
250 case RLM_MODULE_OK:
252 if (chbind_build_response(fake, chbind)) {
254 break;
255 }
257
258 /* If we got any other response from the virtual server, it maps to a reject */
259 default:
261 break;
262 }
263
265
266 return code;
267}
268
269/*
270 * Handles multiple EAP-channel-binding Message attrs
271 * ie concatenates all to get the complete EAP-channel-binding packet.
272 */
274{
275 size_t length;
276 uint8_t *ptr;
277 fr_pair_t *vp;
278 chbind_packet_t *packet;
279 fr_dcursor_t cursor;
280
281 if (!fr_pair_dcursor_by_da_init(&cursor, vps, attr_eap_channel_binding_message)) return NULL;
282
283 /*
284 * Compute the total length of the channel binding data.
285 */
286 length = 0;
287 for (vp = fr_dcursor_current(&cursor);
288 vp;
289 vp = fr_dcursor_next(&cursor)) {
290 length += vp->vp_length;
291 }
292
293 if (length < 4) {
294 DEBUG("Invalid length %u for channel binding data", (unsigned int) length);
295 return NULL;
296 }
297
298 /*
299 * Now that we know the length, allocate memory for the packet.
300 */
301 ptr = talloc_zero_array(ctx, uint8_t, length);
302 if (!ptr) return NULL;
303
304 /*
305 * Copy the data over to our packet.
306 */
307 packet = (chbind_packet_t *) ptr;
308 for (vp = fr_dcursor_head(&cursor);
309 vp != NULL;
310 vp = fr_dcursor_next(&cursor)) {
311 memcpy(ptr, vp->vp_octets, vp->vp_length);
312 ptr += vp->vp_length;
313 }
314
315 return packet;
316}
317
319{
320 fr_pair_t *vp;
321
322 if (!chbind) return NULL; /* don't produce garbage */
323
325 fr_pair_value_memdup(vp, (uint8_t *) chbind, talloc_array_length((uint8_t *)chbind), false);
326
327 return vp;
328}
unlang_action_t rad_virtual_server(unlang_result_t *p_result, request_t *request)
Definition auth.c:42
static int packet_ctx
the tests pass only the address of packet_ctx
static uint8_t request[4]
#define RCSID(id)
Definition build.h:560
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:391
static size_t chbind_get_data(chbind_packet_t const *packet, int desired_nsid, uint8_t const **data)
Definition chbind.c:131
fr_pair_t * eap_chbind_packet2vp(TALLOC_CTX *ctx, chbind_packet_t *chbind)
Definition chbind.c:318
fr_radius_packet_code_t chbind_process(request_t *request, CHBIND_REQ *chbind)
Definition chbind.c:174
static bool chbind_build_response(request_t *request, CHBIND_REQ *chbind)
Definition chbind.c:36
chbind_packet_t * eap_chbind_vp2packet(TALLOC_CTX *ctx, fr_pair_list_t *vps)
Definition chbind.c:273
Channel binding.
chbind_packet_t * response
Definition chbind.h:49
#define CHBIND_NSID_RADIUS
Definition chbind.h:53
fr_pair_t * username
Definition chbind.h:47
chbind_packet_t * request
Definition chbind.h:48
uint8_t code
Definition chbind.h:41
#define CHBIND_CODE_REQUEST
Definition chbind.h:55
#define CHBIND_CODE_FAILURE
Definition chbind.h:57
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
Definition dbuff.h:556
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:288
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition dcursor.h:337
static void * fr_dcursor_head(fr_dcursor_t *cursor)
Rewind cursor to the start of the list.
Definition dcursor.h:232
#define MEM(x)
Definition debug.h:38
fr_radius_packet_code_t
RADIUS packet codes.
Definition defs.h:31
@ FR_RADIUS_CODE_ACCESS_REQUEST
RFC2865 - Access-Request.
Definition defs.h:33
@ FR_RADIUS_CODE_ACCESS_ACCEPT
RFC2865 - Access-Accept.
Definition defs.h:34
@ FR_RADIUS_CODE_ACCESS_REJECT
RFC2865 - Access-Reject.
Definition defs.h:35
#define DEBUG(fmt,...)
Definition dhcpclient.c:38
fr_dict_t const * fr_dict_by_da(fr_dict_attr_t const *da)
Attempt to locate the protocol dictionary containing an attribute.
Definition dict_util.c:2938
talloc_free(hp)
rlm_rcode_t rcode
The current rcode, from executing the instruction or merging the result from a frame.
Definition interpret.h:140
HIDDEN fr_dict_attr_t const * attr_chbind_response_code
Definition base.c:87
HIDDEN fr_dict_attr_t const * attr_freeradius_proxied_to
Definition base.c:98
HIDDEN fr_dict_attr_t const * attr_eap_channel_binding_message
Definition base.c:93
void log_request_pair_list(fr_log_lvl_t lvl, request_t *request, fr_pair_t const *parent, fr_pair_list_t const *vps, char const *prefix)
Print a fr_pair_list_t.
Definition log.c:831
#define RPERROR(fmt,...)
Definition log.h:319
@ L_DBG_LVL_1
Highest priority debug messages (-x).
Definition log.h:67
long int ssize_t
unsigned char uint8_t
static uint16_t fr_nbo_to_uint16(uint8_t const data[static sizeof(uint16_t)])
Read an unsigned 16bit integer from wire format (big endian)
Definition nbo.h:146
int fr_pair_value_memdup(fr_pair_t *vp, uint8_t const *src, size_t len, bool tainted)
Copy data into an "octets" data type.
Definition pair.c:2897
int fr_pair_value_from_str(fr_pair_t *vp, char const *value, size_t inlen, fr_sbuff_unescape_rules_t const *uerules, UNUSED bool tainted)
Convert string value to native attribute value.
Definition pair.c:2552
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
Definition pair.c:707
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
Definition pair.c:1297
fr_pair_t * fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Dynamically allocate a new attribute and assign a fr_dict_attr_t.
Definition pair.c:284
fr_pair_t * fr_pair_copy(TALLOC_CTX *ctx, fr_pair_t const *vp)
Copy a single valuepair.
Definition pair.c:500
int fr_pair_prepend_by_da(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
Alloc a new fr_pair_t (and prepend)
Definition pair.c:1443
ssize_t fr_radius_decode_pair(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *data, size_t data_len, fr_radius_decode_ctx_t *packet_ctx)
Create a "normal" fr_pair_t from the given data.
Definition decode.c:1979
ssize_t fr_radius_encode_pair(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, void *encode_ctx)
Encode a data structure into a RADIUS attribute.
Definition encode.c:1551
VQP attributes.
#define fr_assert(_expr)
Definition rad_assert.h:37
#define RDEBUG2(fmt,...)
static fr_dict_attr_t const * attr_message_authenticator
Definition radclient.c:128
static fr_dict_t const * dict_radius
Definition radsniff.c:93
@ RLM_MODULE_OK
The module is OK, continue.
Definition rcode.h:49
@ RLM_MODULE_HANDLED
The module handled the request, so stop.
Definition rcode.h:50
#define request_local_alloc_internal(_ctx, _args)
Allocate a new internal request outside of the request pool.
Definition request.h:344
Optional arguments for initialising requests.
Definition request.h:288
static const void * fake
fr_pair_t * vp
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition pair.h:69
static TALLOC_CTX * talloc_init_const(char const *name)
Allocate a top level chunk with a constant name.
Definition talloc.h:127
#define fr_pair_dcursor_by_da_init(_cursor, _list, _da)
Initialise a cursor that will return only attributes matching the specified fr_dict_attr_t.
Definition pair.h:640
fr_pair_t * fr_pair_list_next(fr_pair_list_t const *list, fr_pair_t const *item))
Get the next item in a valuepair list after a specific entry.
Definition pair_inline.c:69
#define fr_pair_dcursor_init(_cursor, _list)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:605
fr_pair_t * fr_pair_list_head(fr_pair_list_t const *list)
Get the head of a valuepair list.
Definition pair_inline.c:42
static fr_slen_t data
Definition value.h:1366