remove unnecessary namespace qualification
[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     virtual OM_uint32 mapException(OM_uint32 *minor, std::exception &e) const
120     { return GSS_S_CONTINUE_NEEDED; }
121
122     static bool init(void) { return true; }
123     static void finalize(void) {}
124
125     static gss_eap_attr_provider *createAttrContext(void) { return NULL; }
126
127 protected:
128     const gss_eap_attr_ctx *m_manager;
129
130 private:
131     /* make non-copyable */
132     gss_eap_attr_provider(const gss_eap_attr_provider&);
133     gss_eap_attr_provider& operator=(const gss_eap_attr_provider&);
134 };
135
136 typedef gss_eap_attr_provider *(*gss_eap_attr_create_provider)(void);
137
138 /*
139  * Attribute context: this manages a set of providers for a given
140  * security context.
141  */
142 struct gss_eap_attr_ctx
143 {
144 public:
145     gss_eap_attr_ctx(void);
146     ~gss_eap_attr_ctx(void);
147
148     bool initFromExistingContext(const gss_eap_attr_ctx *manager);
149     bool initFromGssContext(const gss_cred_id_t cred,
150                             const gss_ctx_id_t ctx);
151
152     bool getAttributeTypes(gss_eap_attr_enumeration_cb, void *data) const;
153     bool getAttributeTypes(gss_buffer_set_t *attrs);
154
155     bool setAttribute(int complete,
156                       const gss_buffer_t attr,
157                       const gss_buffer_t value);
158     bool deleteAttribute(const gss_buffer_t value);
159     bool getAttribute(const gss_buffer_t attr,
160                       int *authenticated,
161                       int *complete,
162                       gss_buffer_t value,
163                       gss_buffer_t display_value,
164                       int *more) const;
165     gss_any_t mapToAny(int authenticated,
166                        gss_buffer_t type_id) const;
167     void releaseAnyNameMapping(gss_buffer_t type_id,
168                                gss_any_t input) const;
169
170     void exportToBuffer(gss_buffer_t buffer) const;
171     bool initFromBuffer(const gss_buffer_t buffer);
172
173     static unsigned int
174     attributePrefixToType(const gss_buffer_t prefix);
175
176     static const gss_buffer_t
177     attributeTypeToPrefix(unsigned int type);
178
179     static void
180     decomposeAttributeName(const gss_buffer_t attribute,
181                            gss_buffer_t prefix,
182                            gss_buffer_t suffix);
183     static void
184     composeAttributeName(const gss_buffer_t prefix,
185                          const gss_buffer_t suffix,
186                          gss_buffer_t attribute);
187     static void
188     decomposeAttributeName(const gss_buffer_t attribute,
189                            unsigned int *type,
190                            gss_buffer_t suffix);
191     static void
192     composeAttributeName(unsigned int type,
193                          const gss_buffer_t suffix,
194                          gss_buffer_t attribute);
195
196     static std::string
197     composeAttributeName(const gss_buffer_t prefix,
198                          const gss_buffer_t suffix);
199     static std::string
200     composeAttributeName(unsigned int type,
201                          const gss_buffer_t suffix);
202
203     gss_eap_attr_provider *getProvider(unsigned int type) const;
204     gss_eap_attr_provider *getProvider(const gss_buffer_t prefix) const;
205
206     static void
207     registerProvider(unsigned int type,
208                      const char *prefix,
209                      gss_eap_attr_create_provider factory);
210     static void
211     unregisterProvider(unsigned int type);
212
213     time_t getExpiryTime(void) const;
214     OM_uint32 mapException(OM_uint32 *minor, std::exception &e) const;
215
216 private:
217     bool providerEnabled(unsigned int type) const;
218     void releaseProvider(unsigned int type);
219
220     gss_eap_attr_provider *getPrimaryProvider(void) const;
221
222     /* make non-copyable */
223     gss_eap_attr_ctx(const gss_eap_attr_ctx&);
224     gss_eap_attr_ctx& operator=(const gss_eap_attr_ctx&);
225
226     uint32_t m_flags;
227     gss_eap_attr_provider *m_providers[ATTR_TYPE_MAX + 1];
228 };
229
230 #endif /* __cplusplus */
231
232 #include "util_radius.h"
233 #include "util_saml.h"
234 #include "util_shib.h"
235
236 #ifdef __cplusplus
237
238 #include <string>
239 #include <new>
240
241 static inline void
242 duplicateBuffer(gss_buffer_desc &src, gss_buffer_t dst)
243 {
244     OM_uint32 minor;
245
246     if (GSS_ERROR(duplicateBuffer(&minor, &src, dst)))
247         throw new std::bad_alloc();
248 }
249
250 static inline void
251 duplicateBuffer(std::string &str, gss_buffer_t buffer)
252 {
253     gss_buffer_desc tmp;
254
255     tmp.length = str.length();
256     tmp.value = (char *)str.c_str();
257
258     duplicateBuffer(tmp, buffer);
259 }
260
261 #else
262 struct gss_eap_attr_ctx;
263 #endif
264
265 #ifdef __cplusplus
266 extern "C" {
267 #endif
268
269 /*
270  * C wrappers for attribute context functions. These match their
271  * GSS naming extension equivalents. The caller is required to
272  * obtain the name mutex.
273  */
274
275 OM_uint32
276 gssEapCreateAttrContext(OM_uint32 *minor,
277                         gss_cred_id_t acceptorCred,
278                         gss_ctx_id_t acceptorCtx,
279                         struct gss_eap_attr_ctx **pAttrCtx);
280
281 OM_uint32
282 gssEapInquireName(OM_uint32 *minor,
283                   gss_name_t name,
284                   int *name_is_MN,
285                   gss_OID *MN_mech,
286                   gss_buffer_set_t *attrs);
287
288 OM_uint32
289 gssEapGetNameAttribute(OM_uint32 *minor,
290                        gss_name_t name,
291                        gss_buffer_t attr,
292                        int *authenticated,
293                        int *complete,
294                        gss_buffer_t value,
295                        gss_buffer_t display_value,
296                        int *more);
297
298 OM_uint32
299 gssEapDeleteNameAttribute(OM_uint32 *minor,
300                           gss_name_t name,
301                           gss_buffer_t attr);
302
303 OM_uint32
304 gssEapSetNameAttribute(OM_uint32 *minor,
305                        gss_name_t name,
306                        int complete,
307                        gss_buffer_t attr,
308                        gss_buffer_t value);
309
310 OM_uint32
311 gssEapExportAttrContext(OM_uint32 *minor,
312                         gss_name_t name,
313                         gss_buffer_t buffer);
314
315 OM_uint32
316 gssEapImportAttrContext(OM_uint32 *minor,
317                         gss_buffer_t buffer,
318                         gss_name_t name);
319
320 OM_uint32
321 gssEapDuplicateAttrContext(OM_uint32 *minor,
322                            gss_name_t in,
323                            gss_name_t out);
324
325 OM_uint32
326 gssEapMapNameToAny(OM_uint32 *minor,
327                    gss_name_t name,
328                    int authenticated,
329                    gss_buffer_t type_id,
330                    gss_any_t *output);
331
332 OM_uint32
333 gssEapReleaseAnyNameMapping(OM_uint32 *minor,
334                             gss_name_t name,
335                             gss_buffer_t type_id,
336                             gss_any_t *input);
337
338 OM_uint32
339 gssEapReleaseAttrContext(OM_uint32 *minor,
340                          gss_name_t name);
341
342 OM_uint32
343 gssEapAttrProvidersFinalize(OM_uint32 *minor);
344
345 #ifdef __cplusplus
346 }
347 #endif
348
349 #endif /* _UTIL_ATTR_H_ */