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