add name format to returned SAML attributes
[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 + 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     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_provider *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_provider *source,
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_provider *createAttrContext(void) { return NULL; }
110
111 protected:
112     const gss_eap_attr_ctx *m_manager;
113
114 private:
115     /* make non-copyable */
116     gss_eap_attr_provider(const gss_eap_attr_provider&);
117     gss_eap_attr_provider& operator=(const gss_eap_attr_provider&);
118 };
119
120 typedef gss_eap_attr_provider *(*gss_eap_attr_create_provider)(void);
121
122 struct gss_eap_attr_ctx : gss_eap_attr_provider
123 {
124 public:
125     gss_eap_attr_ctx(void);
126     ~gss_eap_attr_ctx(void);
127
128     bool initFromExistingContext(const gss_eap_attr_ctx *manager,
129                                  const gss_eap_attr_provider *ctx);
130     bool initFromGssContext(const gss_eap_attr_ctx *manager,
131                             const gss_cred_id_t cred,
132                             const gss_ctx_id_t ctx);
133
134     bool getAttributeTypes(gss_eap_attr_enumeration_cb, void *data) const;
135     bool getAttributeTypes(gss_buffer_set_t *attrs);
136
137     void setAttribute(int complete,
138                       const gss_buffer_t attr,
139                       const gss_buffer_t value);
140     void deleteAttribute(const gss_buffer_t value);
141     bool getAttribute(const gss_buffer_t attr,
142                       int *authenticated,
143                       int *complete,
144                       gss_buffer_t value,
145                       gss_buffer_t display_value,
146                       int *more) const;
147     gss_any_t mapToAny(int authenticated,
148                        gss_buffer_t type_id) const;
149     void releaseAnyNameMapping(gss_buffer_t type_id,
150                                gss_any_t input) const;
151
152     void exportToBuffer(gss_buffer_t buffer) const;
153     bool initFromBuffer(const gss_eap_attr_ctx *ctx,
154                         const gss_buffer_t buffer);
155
156     static unsigned int
157     attributePrefixToType(const gss_buffer_t prefix);
158
159     static const gss_buffer_t
160     attributeTypeToPrefix(unsigned int type);
161
162     static void
163     decomposeAttributeName(const gss_buffer_t attribute,
164                            gss_buffer_t prefix,
165                            gss_buffer_t suffix);
166     static void
167     composeAttributeName(const gss_buffer_t prefix,
168                          const gss_buffer_t suffix,
169                          gss_buffer_t attribute);
170     static void
171     decomposeAttributeName(const gss_buffer_t attribute,
172                            unsigned int *type,
173                            gss_buffer_t suffix);
174     static void
175     composeAttributeName(unsigned int type,
176                          const gss_buffer_t suffix,
177                          gss_buffer_t attribute);
178
179     static std::string
180     composeAttributeName(const gss_buffer_t prefix,
181                          const gss_buffer_t suffix);
182     static std::string
183     composeAttributeName(unsigned int type,
184                          const gss_buffer_t suffix);
185
186     gss_eap_attr_provider *getProvider(unsigned int type) const;
187     gss_eap_attr_provider *getProvider(const gss_buffer_t prefix) const;
188
189     static void
190     registerProvider(unsigned int type,
191                      const char *prefix,
192                      gss_eap_attr_create_provider factory);
193     static void
194     unregisterProvider(unsigned int type);
195
196 private:
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_ */