cleanup key derivation on acceptor
[mech_eap.orig] / util_radius.cpp
1 /*
2  * Copyright (c) 2010, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #include "gssapiP_eap.h"
34
35 VALUE_PAIR *
36 gss_eap_radius_attr_provider::copyAvps(const VALUE_PAIR *src)
37 {
38     const VALUE_PAIR *vp;
39     VALUE_PAIR *dst = NULL, **pDst = &dst;
40
41     for (vp = src; vp != NULL; vp = vp->next) {
42         VALUE_PAIR *vp2;
43
44         vp2 = (VALUE_PAIR *)GSSEAP_CALLOC(1, sizeof(*vp2));
45         if (vp2 == NULL) {
46             rc_avpair_free(dst);
47             return NULL;
48         }
49         memcpy(vp2, vp, sizeof(*vp));
50         vp2->next = NULL;
51         *pDst = vp2;
52         pDst = &vp2->next;
53     }
54
55     return dst;
56 }
57
58 gss_eap_radius_attr_provider::gss_eap_radius_attr_provider(void)
59 {
60     m_rh = NULL;
61     m_avps = NULL;
62     m_authenticated = false;
63 }
64
65 gss_eap_radius_attr_provider::~gss_eap_radius_attr_provider(void)
66 {
67     if (m_rh != NULL)
68         rc_config_free(m_rh);
69     if (m_avps != NULL)
70         rc_avpair_free(m_avps);
71 }
72
73 bool
74 gss_eap_radius_attr_provider::initFromGssCred(const gss_cred_id_t cred)
75 {
76     OM_uint32 minor;
77
78     return !GSS_ERROR(gssEapRadiusAllocHandle(&minor, cred, &m_rh));
79 }
80
81 bool
82 gss_eap_radius_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
83                                                       const gss_eap_attr_provider *ctx)
84 {
85     const gss_eap_radius_attr_provider *radius;
86
87     if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx))
88         return false;
89
90     if (!initFromGssCred(GSS_C_NO_CREDENTIAL))
91         return false;
92
93     radius = static_cast<const gss_eap_radius_attr_provider *>(ctx);
94     if (radius->m_avps != NULL) {
95         m_avps = copyAvps(radius->getAvps());
96     }
97
98     return true;
99 }
100
101 bool
102 gss_eap_radius_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
103                                                  const gss_cred_id_t gssCred,
104                                                  const gss_ctx_id_t gssCtx)
105 {
106     if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
107         return false;
108
109     if (!initFromGssCred(gssCred))
110         return false;
111
112     if (gssCtx != GSS_C_NO_CONTEXT) {
113         if (gssCtx->acceptorCtx.avps != NULL) {
114             m_avps = copyAvps(gssCtx->acceptorCtx.avps);
115             if (m_avps == NULL)
116                 return false;
117         }
118     }
119
120     return true;
121 }
122
123 static bool
124 alreadyAddedAttributeP(std::vector <std::string> &attrs, VALUE_PAIR *vp)
125 {
126     for (std::vector<std::string>::const_iterator a = attrs.begin();
127          a != attrs.end();
128          ++a) {
129         if (strcmp(vp->name, (*a).c_str()) == 0)
130             return true;
131     }
132
133     return false;
134 }
135
136 bool
137 gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute, void *data) const
138 {
139     VALUE_PAIR *vp;
140     std::vector <std::string> seen;
141
142     for (vp = m_avps; vp != NULL; vp = vp->next) {
143         gss_buffer_desc attribute;
144
145         if (alreadyAddedAttributeP(seen, vp))
146             continue;
147
148         attribute.value = (void *)vp->name;
149         attribute.length = strlen(vp->name);
150
151         if (!addAttribute(this, &attribute, data))
152             return false;
153
154         seen.push_back(std::string(vp->name));
155     }
156
157     return true;
158 }
159
160 void
161 gss_eap_radius_attr_provider::setAttribute(int complete,
162                                            const gss_buffer_t attr,
163                                            const gss_buffer_t value)
164 {
165 }
166
167 void
168 gss_eap_radius_attr_provider::deleteAttribute(const gss_buffer_t value)
169 {
170 }
171
172 bool
173 gss_eap_radius_attr_provider::getAttribute(const gss_buffer_t attr,
174                                            int *authenticated,
175                                            int *complete,
176                                            gss_buffer_t value,
177                                            gss_buffer_t display_value,
178                                            int *more) const
179 {
180     OM_uint32 tmpMinor;
181     gss_buffer_desc strAttr = GSS_C_EMPTY_BUFFER;
182     DICT_ATTR *d;
183     int attrid;
184     char *s;
185
186     /* XXX vendor */
187
188     duplicateBuffer(*attr, &strAttr);
189     s = (char *)strAttr.value;
190
191     if (isdigit(((char *)strAttr.value)[0])) {
192         attrid = strtoul(s, NULL, 10);
193     } else {
194         d = rc_dict_findattr(m_rh, (char *)s);
195         if (d == NULL) {
196             gss_release_buffer(&tmpMinor, &strAttr);
197             return false;
198         }
199         attrid = d->value;
200     }
201
202     gss_release_buffer(&tmpMinor, &strAttr);
203
204     return getAttribute(attrid, authenticated, complete,
205                         value, display_value, more);
206 }
207
208 static bool
209 isPrintableAttributeP(VALUE_PAIR *vp)
210 {
211     size_t i;
212
213     for (i = 0; i < sizeof(vp->strvalue); i++) {
214         if (!isprint(vp->strvalue[i]))
215             return false;
216     }
217
218     return true;
219 }
220
221 bool
222 gss_eap_radius_attr_provider::getAttribute(int attrid,
223                                            int vendor,
224                                            int *authenticated,
225                                            int *complete,
226                                            gss_buffer_t value,
227                                            gss_buffer_t display_value,
228                                            int *more) const
229 {
230     OM_uint32 tmpMinor;
231     VALUE_PAIR *vp;
232     int i = *more;
233     int max = 0;
234     char name[NAME_LENGTH + 1];
235     char displayString[AUTH_STRING_LEN + 1];
236     gss_buffer_desc valueBuf = GSS_C_EMPTY_BUFFER;
237     gss_buffer_desc displayBuf = GSS_C_EMPTY_BUFFER;
238
239     *more = 0;
240
241     vp = rc_avpair_get(m_avps, attrid, vendor);
242     if (vp == NULL)
243         return false;
244
245     if (i == -1)
246         i = 0;
247
248     do {
249         if (i == max)
250             break;
251
252         max++;
253     } while ((vp = rc_avpair_get(vp->next, attrid, vendor)) != NULL);
254
255     if (i > max)
256         return false;
257
258     if (vp->type == PW_TYPE_STRING) {
259         valueBuf.value = (void *)vp->strvalue;
260         valueBuf.length = vp->lvalue;
261     } else {
262         valueBuf.value = (void *)&vp->lvalue;
263         valueBuf.length = 4;
264     }
265
266     if (value != GSS_C_NO_BUFFER)
267         duplicateBuffer(valueBuf, value);
268
269     if (display_value != GSS_C_NO_BUFFER &&
270         isPrintableAttributeP(vp)) {
271         if (rc_avpair_tostr(m_rh, vp, name, NAME_LENGTH,
272                             displayString, AUTH_STRING_LEN) != 0) {
273             gss_release_buffer(&tmpMinor, value);
274             return false;
275         }
276
277         displayBuf.value = (void *)displayString;
278         displayBuf.length = strlen(displayString);
279
280         duplicateBuffer(displayBuf, display_value);
281     }
282
283     if (authenticated != NULL)
284         *authenticated = m_authenticated;
285     if (complete != NULL)
286         *complete = true;
287
288     if (max > i)
289         *more = i;
290
291     return true;
292 }
293
294 bool
295 gss_eap_radius_attr_provider::getAttribute(int attrid,
296                                            int *authenticated,
297                                            int *complete,
298                                            gss_buffer_t value,
299                                            gss_buffer_t display_value,
300                                            int *more) const
301 {
302     return getAttribute(attrid, 0, authenticated, complete,
303                         value, display_value, more);
304 }
305
306 gss_any_t
307 gss_eap_radius_attr_provider::mapToAny(int authenticated,
308                                        gss_buffer_t type_id) const
309 {
310     if (authenticated && !m_authenticated)
311         return (gss_any_t)NULL;
312
313     return (gss_any_t)copyAvps(m_avps);
314 }
315
316 void
317 gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
318                                                     gss_any_t input) const
319 {
320     rc_avpair_free((VALUE_PAIR *)input);
321 }
322
323 void
324 gss_eap_radius_attr_provider::exportToBuffer(gss_buffer_t buffer) const
325 {
326     buffer->length = 0;
327     buffer->value = NULL;
328 }
329
330 bool
331 gss_eap_radius_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
332                                              const gss_buffer_t buffer)
333 {
334     if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
335         return false;
336
337     if (!initFromGssCred(GSS_C_NO_CREDENTIAL))
338         return false;
339
340     return true;
341 }
342
343 bool
344 gss_eap_radius_attr_provider::init(void)
345 {
346     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_RADIUS,
347                                        "urn:ietf:params:gss-eap:radius-avp",
348                                        gss_eap_radius_attr_provider::createAttrContext);
349     return true;
350 }
351
352 void
353 gss_eap_radius_attr_provider::finalize(void)
354 {
355     gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_RADIUS);
356 }
357
358 gss_eap_attr_provider *
359 gss_eap_radius_attr_provider::createAttrContext(void)
360 {
361     return new gss_eap_radius_attr_provider;
362 }
363
364 OM_uint32
365 addAvpFromBuffer(OM_uint32 *minor,
366                  rc_handle *rh,
367                  VALUE_PAIR **vp,
368                  int type,
369                  gss_buffer_t buffer)
370 {
371     if (rc_avpair_add(rh, vp, type, buffer->value, buffer->length, 0) == NULL) {
372         return GSS_S_FAILURE;
373     }
374
375     return GSS_S_COMPLETE;
376 }
377
378 OM_uint32
379 getBufferFromAvps(OM_uint32 *minor,
380                   VALUE_PAIR *vps,
381                   int type,
382                   gss_buffer_t buffer,
383                   int concat)
384 {
385     VALUE_PAIR *vp;
386     unsigned char *p;
387
388     buffer->length = 0;
389     buffer->value = NULL;
390
391     vp = rc_avpair_get(vps, type, 0);
392     if (vp == NULL)
393         return GSS_S_UNAVAILABLE;
394
395     do {
396         buffer->length += vp->lvalue;
397     } while (concat && (vp = rc_avpair_get(vp->next, type, 0)) != NULL);
398
399     buffer->value = GSSEAP_MALLOC(buffer->length);
400     if (buffer->value == NULL) {
401         *minor = ENOMEM;
402         return GSS_S_FAILURE;
403     }
404
405     p = (unsigned char *)buffer->value;
406
407     for (vp = rc_avpair_get(vps, type, 0);
408          concat && vp != NULL;
409          vp = rc_avpair_get(vp->next, type, 0)) {
410         memcpy(p, vp->strvalue, vp->lvalue);
411         p += vp->lvalue;
412     }
413
414     *minor = 0;
415     return GSS_S_COMPLETE;
416 }
417
418 OM_uint32
419 gssEapRadiusAttrProviderInit(OM_uint32 *minor)
420 {
421     return gss_eap_radius_attr_provider::init()
422         ? GSS_S_COMPLETE : GSS_S_FAILURE;
423 }
424
425 OM_uint32
426 gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
427 {
428     gss_eap_radius_attr_provider::finalize();
429     return GSS_S_COMPLETE;
430 }
431
432 OM_uint32
433 gssEapRadiusAllocHandle(OM_uint32 *minor,
434                         const gss_cred_id_t cred,
435                         rc_handle **pHandle)
436 {
437     rc_handle *rh;
438     const char *config = RC_CONFIG_FILE;
439
440     *pHandle = NULL;
441
442     if (cred != GSS_C_NO_CREDENTIAL && cred->radiusConfigFile != NULL)
443         config = cred->radiusConfigFile;
444
445     rh = rc_read_config((char *)config);
446     if (rh == NULL) {
447         *minor = errno;
448         rc_config_free(rh);
449         return GSS_S_FAILURE;
450     }
451
452     if (rc_read_dictionary(rh, rc_conf_str(rh, (char *)"dictionary")) != 0) {
453         *minor = errno;
454         return GSS_S_FAILURE;
455     }
456
457     *pHandle = rh;
458     return GSS_S_COMPLETE;
459 }