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