send exported GSS context token to shibresolver
[moonshot.git] / mech_eap / util_attr.h
1 /*
2  * Copyright (c) 2011, 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 /*
34  * Attribute provider interface.
35  */
36
37 #ifndef _UTIL_ATTR_H_
38 #define _UTIL_ATTR_H_ 1
39
40 #ifdef __cplusplus
41 #include <string>
42 #include <new>
43
44 using namespace gss_eap_util;
45
46 struct gss_eap_attr_provider;
47 struct gss_eap_attr_ctx;
48
49 typedef bool
50 (*gss_eap_attr_enumeration_cb)(const gss_eap_attr_ctx *ctx,
51                                const gss_eap_attr_provider *source,
52                                const gss_buffer_t attribute,
53                                void *data);
54
55 #define ATTR_TYPE_RADIUS            0U                  /* RADIUS AVPs */
56 #define ATTR_TYPE_SAML_ASSERTION    1U                  /* SAML assertion */
57 #define ATTR_TYPE_SAML              2U                  /* SAML attributes */
58 #define ATTR_TYPE_LOCAL             3U                  /* Local attributes */
59 #define ATTR_TYPE_MIN               ATTR_TYPE_RADIUS
60 #define ATTR_TYPE_MAX               ATTR_TYPE_LOCAL
61
62 /*
63  * Attribute provider: this represents a source of attributes derived
64  * from the security context.
65  */
66 struct gss_eap_attr_provider
67 {
68 public:
69     gss_eap_attr_provider(void) {}
70     virtual ~gss_eap_attr_provider(void) {}
71
72     bool initWithManager(const gss_eap_attr_ctx *manager)
73     {
74         m_manager = manager;
75         return true;
76     }
77
78     virtual bool initFromExistingContext(const gss_eap_attr_ctx *manager,
79                                          const gss_eap_attr_provider *ctx GSSEAP_UNUSED)
80     {
81         return initWithManager(manager);
82     }
83
84     virtual bool initFromGssContext(const gss_eap_attr_ctx *manager,
85                                     const gss_cred_id_t cred GSSEAP_UNUSED,
86                                     const gss_ctx_id_t ctx GSSEAP_UNUSED)
87     {
88         return initWithManager(manager);
89     }
90
91     virtual bool getAttributeTypes(gss_eap_attr_enumeration_cb GSSEAP_UNUSED,
92                                    void *data GSSEAP_UNUSED) const
93     {
94         return false;
95     }
96
97     virtual bool setAttribute(int complete GSSEAP_UNUSED,
98                               const gss_buffer_t attr GSSEAP_UNUSED,
99                               const gss_buffer_t value GSSEAP_UNUSED)
100     {
101         return false;
102     }
103
104     virtual bool deleteAttribute(const gss_buffer_t value GSSEAP_UNUSED)
105     {
106         return false;
107     }
108
109     virtual bool getAttribute(const gss_buffer_t attr GSSEAP_UNUSED,
110                               int *authenticated GSSEAP_UNUSED,
111                               int *complete GSSEAP_UNUSED,
112                               gss_buffer_t value GSSEAP_UNUSED,
113                               gss_buffer_t display_value GSSEAP_UNUSED,
114                               int *more GSSEAP_UNUSED) const
115     {
116         return false;
117     }
118
119     virtual gss_any_t mapToAny(int authenticated GSSEAP_UNUSED,
120                                gss_buffer_t type_id GSSEAP_UNUSED) const
121     {
122         return NULL;
123     }
124
125     virtual void releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
126                                        gss_any_t input GSSEAP_UNUSED) const
127     {
128     }
129
130     /* prefix to be prepended to attributes emitted by gss_get_name_attribute */
131     virtual const char *prefix(void) const
132     {
133         return NULL;
134     }
135
136     /* optional key for storing JSON dictionary */
137     virtual const char *name(void) const
138     {
139         return NULL;
140     }
141
142     virtual bool initWithJsonObject(const gss_eap_attr_ctx *manager,
143                                     JSONObject &object GSSEAP_UNUSED)
144     {
145         return initWithManager(manager);
146     }
147
148
149     virtual JSONObject jsonRepresentation(void) const
150     {
151         return JSONObject::null();
152     }
153
154     virtual time_t getExpiryTime(void) const { return 0; }
155
156     virtual OM_uint32 mapException(OM_uint32 *minor GSSEAP_UNUSED,
157                                    std::exception &e GSSEAP_UNUSED) const
158     {
159         return GSS_S_CONTINUE_NEEDED;
160     }
161
162     static bool init(void) { return true; }
163     static void finalize(void) {}
164
165     static gss_eap_attr_provider *createAttrContext(void) { return NULL; }
166
167 protected:
168     const gss_eap_attr_ctx *m_manager;
169
170 private:
171     /* make non-copyable */
172     gss_eap_attr_provider(const gss_eap_attr_provider&);
173     gss_eap_attr_provider& operator=(const gss_eap_attr_provider&);
174 };
175
176 typedef gss_eap_attr_provider *(*gss_eap_attr_create_provider)(void);
177
178 /*
179  * Attribute context: this manages a set of providers for a given
180  * security context.
181  */
182 struct gss_eap_attr_ctx
183 {
184 public:
185     gss_eap_attr_ctx(void);
186     ~gss_eap_attr_ctx(void);
187
188     bool initFromExistingContext(const gss_eap_attr_ctx *manager);
189     bool initFromGssContext(const gss_cred_id_t cred,
190                             const gss_ctx_id_t ctx);
191
192     bool getAttributeTypes(gss_eap_attr_enumeration_cb, void *data) const;
193     bool getAttributeTypes(gss_buffer_set_t *attrs);
194
195     bool setAttribute(int complete,
196                       const gss_buffer_t attr,
197                       const gss_buffer_t value);
198     bool deleteAttribute(const gss_buffer_t value);
199     bool getAttribute(const gss_buffer_t attr,
200                       int *authenticated,
201                       int *complete,
202                       gss_buffer_t value,
203                       gss_buffer_t display_value,
204                       int *more) const;
205     gss_any_t mapToAny(int authenticated,
206                        gss_buffer_t type_id) const;
207     void releaseAnyNameMapping(gss_buffer_t type_id,
208                                gss_any_t input) const;
209
210     void exportToBuffer(gss_buffer_t buffer,
211                         uint32_t flags) const;
212     bool initFromBuffer(const gss_buffer_t buffer);
213
214     static std::string
215     composeAttributeName(const gss_buffer_t prefix,
216                          const gss_buffer_t suffix);
217     static void
218     decomposeAttributeName(const gss_buffer_t attribute,
219                            gss_buffer_t prefix,
220                            gss_buffer_t suffix);
221     static void
222     composeAttributeName(const gss_buffer_t prefix,
223                          const gss_buffer_t suffix,
224                          gss_buffer_t attribute);
225
226     std::string
227     composeAttributeName(unsigned int type,
228                          const gss_buffer_t suffix);
229     void
230     decomposeAttributeName(const gss_buffer_t attribute,
231                            unsigned int *type,
232                            gss_buffer_t suffix) const;
233     void
234     composeAttributeName(unsigned int type,
235                          const gss_buffer_t suffix,
236                          gss_buffer_t attribute) const;
237
238     gss_eap_attr_provider *getProvider(unsigned int type) const;
239
240     static void
241     registerProvider(unsigned int type,
242                      gss_eap_attr_create_provider factory);
243     static void
244     unregisterProvider(unsigned int type);
245
246     time_t getExpiryTime(void) const;
247     OM_uint32 mapException(OM_uint32 *minor, std::exception &e) const;
248
249 private:
250     bool providerEnabled(unsigned int type) const;
251     void releaseProvider(unsigned int type);
252
253     unsigned int attributePrefixToType(const gss_buffer_t prefix) const;
254     gss_buffer_desc attributeTypeToPrefix(unsigned int type) const;
255
256     bool initWithJsonObject(JSONObject &object);
257     JSONObject jsonRepresentation(uint32_t flags = 0) const;
258
259     gss_eap_attr_provider *getPrimaryProvider(void) const;
260
261     /* make non-copyable */
262     gss_eap_attr_ctx(const gss_eap_attr_ctx&);
263     gss_eap_attr_ctx& operator=(const gss_eap_attr_ctx&);
264
265     uint32_t m_flags;
266     gss_eap_attr_provider *m_providers[ATTR_TYPE_MAX + 1];
267 };
268
269 #endif /* __cplusplus */
270
271 #include "util_radius.h"
272 #include "util_saml.h"
273 #include "util_shib.h"
274
275 #ifdef __cplusplus
276
277 static inline void
278 duplicateBuffer(gss_buffer_desc &src, gss_buffer_t dst)
279 {
280     OM_uint32 minor;
281
282     if (GSS_ERROR(duplicateBuffer(&minor, &src, dst)))
283         throw new std::bad_alloc();
284 }
285
286 static inline void
287 duplicateBuffer(std::string &str, gss_buffer_t buffer)
288 {
289     gss_buffer_desc tmp;
290
291     tmp.length = str.length();
292     tmp.value = (char *)str.c_str();
293
294     duplicateBuffer(tmp, buffer);
295 }
296
297 #else
298 struct gss_eap_attr_ctx;
299 #endif
300
301 #ifdef __cplusplus
302 extern "C" {
303 #endif
304
305 #define ATTR_FLAG_DISABLE_LOCAL     0x00000001
306
307 /*
308  * C wrappers for attribute context functions. These match their
309  * GSS naming extension equivalents. The caller is required to
310  * obtain the name mutex.
311  */
312
313 OM_uint32
314 gssEapCreateAttrContext(OM_uint32 *minor,
315                         gss_cred_id_t acceptorCred,
316                         gss_ctx_id_t acceptorCtx,
317                         struct gss_eap_attr_ctx **pAttrCtx,
318                         time_t *pExpiryTime);
319
320 OM_uint32
321 gssEapInquireName(OM_uint32 *minor,
322                   gss_name_t name,
323                   int *name_is_MN,
324                   gss_OID *MN_mech,
325                   gss_buffer_set_t *attrs);
326
327 OM_uint32
328 gssEapGetNameAttribute(OM_uint32 *minor,
329                        gss_name_t name,
330                        gss_buffer_t attr,
331                        int *authenticated,
332                        int *complete,
333                        gss_buffer_t value,
334                        gss_buffer_t display_value,
335                        int *more);
336
337 OM_uint32
338 gssEapDeleteNameAttribute(OM_uint32 *minor,
339                           gss_name_t name,
340                           gss_buffer_t attr);
341
342 OM_uint32
343 gssEapSetNameAttribute(OM_uint32 *minor,
344                        gss_name_t name,
345                        int complete,
346                        gss_buffer_t attr,
347                        gss_buffer_t value);
348
349 OM_uint32
350 gssEapExportAttrContext(OM_uint32 *minor,
351                         gss_name_t name,
352                         gss_buffer_t buffer,
353                         OM_uint32 flags);
354
355 OM_uint32
356 gssEapImportAttrContext(OM_uint32 *minor,
357                         gss_buffer_t buffer,
358                         gss_name_t name);
359
360 OM_uint32
361 gssEapDuplicateAttrContext(OM_uint32 *minor,
362                            gss_name_t in,
363                            gss_name_t out);
364
365 OM_uint32
366 gssEapMapNameToAny(OM_uint32 *minor,
367                    gss_name_t name,
368                    int authenticated,
369                    gss_buffer_t type_id,
370                    gss_any_t *output);
371
372 OM_uint32
373 gssEapReleaseAnyNameMapping(OM_uint32 *minor,
374                             gss_name_t name,
375                             gss_buffer_t type_id,
376                             gss_any_t *input);
377
378 OM_uint32
379 gssEapReleaseAttrContext(OM_uint32 *minor,
380                          gss_name_t name);
381
382 OM_uint32
383 gssEapAttrProvidersFinalize(OM_uint32 *minor);
384
385 #ifdef __cplusplus
386 }
387 #endif
388
389 #endif /* _UTIL_ATTR_H_ */