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