rename
[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 #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 + 1U)
42
43 #ifdef __cplusplus
44 #include <string>
45
46 struct gss_eap_attr_ctx;
47
48 struct gss_eap_attr_source
49 {
50 public:
51     gss_eap_attr_source(void) {}
52     virtual ~gss_eap_attr_source(void) {}
53
54     bool initWithManager(const gss_eap_attr_ctx *manager)
55     {
56         m_manager = manager;
57         return true;
58     }
59
60     virtual bool initFromExistingContext(const gss_eap_attr_ctx *manager,
61                                          const gss_eap_attr_source *ctx)
62     {
63         return initWithManager(manager);
64     }
65
66     virtual bool initFromGssContext(const gss_eap_attr_ctx *manager,
67                                     const gss_cred_id_t cred,
68                                     const gss_ctx_id_t ctx)
69     {
70         return initWithManager(manager);
71     }
72
73     typedef bool
74     gss_eap_attr_enumeration_cb(const gss_eap_attr_source *provider,
75                                 const gss_buffer_t attribute,
76                                 void *data);
77
78     virtual bool getAttributeTypes(gss_eap_attr_enumeration_cb, void *data) const
79     {
80         return false;
81     }
82
83     virtual void setAttribute(int complete,
84                               const gss_buffer_t attr,
85                               const gss_buffer_t value) {}
86     virtual void deleteAttribute(const gss_buffer_t value) {}
87     virtual bool getAttribute(const gss_buffer_t attr,
88                               int *authenticated,
89                               int *complete,
90                               gss_buffer_t value,
91                               gss_buffer_t display_value,
92                               int *more) const { return false; }
93
94     virtual gss_any_t mapToAny(int authenticated,
95                                gss_buffer_t type_id) const { return NULL; }
96     virtual void releaseAnyNameMapping(gss_buffer_t type_id,
97                                        gss_any_t input) const {}
98
99     virtual void exportToBuffer(gss_buffer_t buffer) const {}
100     virtual bool initFromBuffer(const gss_eap_attr_ctx *manager,
101                                 const gss_buffer_t buffer)
102     {
103         return initWithManager(manager);
104     }
105
106     static bool init() { return true; }
107     static void finalize() {}
108
109     static gss_eap_attr_source *createAttrContext(void) { return NULL; }
110
111 protected:
112     const gss_eap_attr_ctx *m_manager;
113 };
114
115 typedef gss_eap_attr_source *(*gss_eap_attr_create_factory)(void);
116
117 struct gss_eap_attr_ctx : gss_eap_attr_source
118 {
119 public:
120     gss_eap_attr_ctx(void);
121     ~gss_eap_attr_ctx(void);
122
123     bool initFromExistingContext(const gss_eap_attr_ctx *manager,
124                                  const gss_eap_attr_source *ctx);
125     bool initFromGssContext(const gss_eap_attr_ctx *manager,
126                             const gss_cred_id_t cred,
127                             const gss_ctx_id_t ctx);
128
129     bool getAttributeTypes(gss_eap_attr_enumeration_cb, void *data) const;
130     bool getAttributeTypes(gss_buffer_set_t *attrs);
131
132     void setAttribute(int complete,
133                       const gss_buffer_t attr,
134                       const gss_buffer_t value);
135     void deleteAttribute(const gss_buffer_t value);
136     bool getAttribute(const gss_buffer_t attr,
137                       int *authenticated,
138                       int *complete,
139                       gss_buffer_t value,
140                       gss_buffer_t display_value,
141                       int *more) const;
142     gss_any_t mapToAny(int authenticated,
143                        gss_buffer_t type_id) const;
144     void releaseAnyNameMapping(gss_buffer_t type_id,
145                                gss_any_t input) const;
146
147     void exportToBuffer(gss_buffer_t buffer) const;
148     bool initFromBuffer(const gss_eap_attr_ctx *ctx,
149                         const gss_buffer_t buffer);
150     static bool init();
151     static void finalize();
152
153     static unsigned int
154     attributePrefixToType(const gss_buffer_t prefix);
155
156     static const gss_buffer_t
157     attributeTypeToPrefix(unsigned int type);
158
159     static void
160     decomposeAttributeName(const gss_buffer_t attribute,
161                            gss_buffer_t prefix,
162                            gss_buffer_t suffix);
163     static void
164     composeAttributeName(const gss_buffer_t prefix,
165                          const gss_buffer_t suffix,
166                          gss_buffer_t attribute);
167     static void
168     decomposeAttributeName(const gss_buffer_t attribute,
169                            unsigned int *type,
170                            gss_buffer_t suffix);
171     static void
172     composeAttributeName(unsigned int type,
173                          const gss_buffer_t suffix,
174                          gss_buffer_t attribute);
175
176     static std::string
177     composeAttributeName(const gss_buffer_t prefix,
178                          const gss_buffer_t suffix);
179     static std::string
180     composeAttributeName(unsigned int type,
181                          const gss_buffer_t suffix);
182
183     gss_eap_attr_source *getProvider(unsigned int type) const;
184     gss_eap_attr_source *getProvider(const gss_buffer_t prefix) const;
185
186 private:
187     gss_eap_attr_source *m_providers[ATTR_TYPE_MAX];
188 };
189
190 #include "util_radius.h"
191 #include "util_saml.h"
192 #include "util_shib.h"
193
194 #include <string>
195 #include <new>
196
197 static inline void
198 duplicateBuffer(gss_buffer_desc &src, gss_buffer_t dst)
199 {
200     OM_uint32 minor;
201
202     if (GSS_ERROR(duplicateBuffer(&minor, &src, dst)))
203         throw new std::bad_alloc();
204 }
205
206 static inline void
207 duplicateBuffer(std::string &str, gss_buffer_t buffer)
208 {
209     gss_buffer_desc tmp;
210
211     tmp.length = str.length();
212     tmp.value = (char *)str.c_str();
213
214     duplicateBuffer(tmp, buffer);
215 }
216
217 #else
218 struct gss_eap_attr_ctx;
219 #endif
220
221 #ifdef __cplusplus
222 extern "C" {
223 #endif
224
225 struct gss_eap_attr_ctx *
226 gssEapCreateAttrContext(gss_cred_id_t acceptorCred,
227                         gss_ctx_id_t acceptorCtx);
228
229 OM_uint32
230 gssEapInquireName(OM_uint32 *minor,
231                   gss_name_t name,
232                   int *name_is_MN,
233                   gss_OID *MN_mech,
234                   gss_buffer_set_t *attrs);
235
236 OM_uint32
237 gssEapGetNameAttribute(OM_uint32 *minor,
238                        gss_name_t name,
239                        gss_buffer_t attr,
240                        int *authenticated,
241                        int *complete,
242                        gss_buffer_t value,
243                        gss_buffer_t display_value,
244                        int *more);
245
246 OM_uint32
247 gssEapDeleteNameAttribute(OM_uint32 *minor,
248                           gss_name_t name,
249                           gss_buffer_t attr);
250
251 OM_uint32
252 gssEapSetNameAttribute(OM_uint32 *minor,
253                        gss_name_t name,
254                        int complete,
255                        gss_buffer_t attr,
256                        gss_buffer_t value);
257
258 OM_uint32
259 gssEapExportAttrContext(OM_uint32 *minor,
260                         gss_name_t name,
261                         gss_buffer_t buffer);
262
263 OM_uint32
264 gssEapImportAttrContext(OM_uint32 *minor,
265                         gss_buffer_t buffer,
266                         gss_name_t name);
267
268 OM_uint32
269 gssEapDuplicateAttrContext(OM_uint32 *minor,
270                            gss_name_t in,
271                            gss_name_t out);
272
273 OM_uint32
274 gssEapMapNameToAny(OM_uint32 *minor,
275                    gss_name_t name,
276                    int authenticated,
277                    gss_buffer_t type_id,
278                    gss_any_t *output);
279
280 OM_uint32
281 gssEapReleaseAnyNameMapping(OM_uint32 *minor,
282                             gss_name_t name,
283                             gss_buffer_t type_id,
284                             gss_any_t *input);
285
286 OM_uint32
287 gssEapReleaseAttrContext(OM_uint32 *minor,
288                          gss_name_t name);
289
290 OM_uint32
291 gssEapAttrProvidersInit(OM_uint32 *minor);
292
293 OM_uint32
294 gssEapAttrProvidersFinalize(OM_uint32 *minor);
295
296 #ifdef __cplusplus
297 }
298 #endif
299
300 #endif /* _UTIL_ATTR_H_ */