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