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