Propagate expiry time from assertion
[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_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     virtual time_t getExpiryTime(void) const { return 0; }
108
109     static bool init() { return true; }
110     static void finalize() {}
111
112     static gss_eap_attr_provider *createAttrContext(void) { return NULL; }
113
114 protected:
115     const gss_eap_attr_ctx *m_manager;
116
117 private:
118     /* make non-copyable */
119     gss_eap_attr_provider(const gss_eap_attr_provider&);
120     gss_eap_attr_provider& operator=(const gss_eap_attr_provider&);
121 };
122
123 typedef gss_eap_attr_provider *(*gss_eap_attr_create_provider)(void);
124
125 struct gss_eap_attr_ctx
126 {
127 public:
128     gss_eap_attr_ctx(void);
129     ~gss_eap_attr_ctx(void);
130
131     bool initFromExistingContext(const gss_eap_attr_ctx *manager);
132     bool initFromGssContext(const gss_cred_id_t cred,
133                             const gss_ctx_id_t ctx);
134
135     bool getAttributeTypes(gss_eap_attr_enumeration_cb, void *data) const;
136     bool getAttributeTypes(gss_buffer_set_t *attrs);
137
138     void setAttribute(int complete,
139                       const gss_buffer_t attr,
140                       const gss_buffer_t value);
141     void deleteAttribute(const gss_buffer_t value);
142     bool getAttribute(const gss_buffer_t attr,
143                       int *authenticated,
144                       int *complete,
145                       gss_buffer_t value,
146                       gss_buffer_t display_value,
147                       int *more) const;
148     gss_any_t mapToAny(int authenticated,
149                        gss_buffer_t type_id) const;
150     void releaseAnyNameMapping(gss_buffer_t type_id,
151                                gss_any_t input) const;
152
153     void exportToBuffer(gss_buffer_t buffer) const;
154     bool initFromBuffer(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     time_t getExpiryTime(void) const;
197
198 private:
199     gss_eap_attr_provider *getPrimaryProvider(void) const;
200
201     /* make non-copyable */
202     gss_eap_attr_ctx(const gss_eap_attr_ctx&);
203     gss_eap_attr_ctx& operator=(const gss_eap_attr_ctx&);
204
205     gss_eap_attr_provider *m_providers[ATTR_TYPE_MAX];
206 };
207
208 #include "util_radius.h"
209 #include "util_saml.h"
210 #include "util_shib.h"
211
212 #include <string>
213 #include <new>
214
215 static inline void
216 duplicateBuffer(gss_buffer_desc &src, gss_buffer_t dst)
217 {
218     OM_uint32 minor;
219
220     if (GSS_ERROR(duplicateBuffer(&minor, &src, dst)))
221         throw new std::bad_alloc();
222 }
223
224 static inline void
225 duplicateBuffer(std::string &str, gss_buffer_t buffer)
226 {
227     gss_buffer_desc tmp;
228
229     tmp.length = str.length();
230     tmp.value = (char *)str.c_str();
231
232     duplicateBuffer(tmp, buffer);
233 }
234
235 #else
236 struct gss_eap_attr_ctx;
237 #endif
238
239 #ifdef __cplusplus
240 extern "C" {
241 #endif
242
243 struct gss_eap_attr_ctx *
244 gssEapCreateAttrContext(gss_cred_id_t acceptorCred,
245                         gss_ctx_id_t acceptorCtx);
246
247 OM_uint32
248 gssEapInquireName(OM_uint32 *minor,
249                   gss_name_t name,
250                   int *name_is_MN,
251                   gss_OID *MN_mech,
252                   gss_buffer_set_t *attrs);
253
254 OM_uint32
255 gssEapGetNameAttribute(OM_uint32 *minor,
256                        gss_name_t name,
257                        gss_buffer_t attr,
258                        int *authenticated,
259                        int *complete,
260                        gss_buffer_t value,
261                        gss_buffer_t display_value,
262                        int *more);
263
264 OM_uint32
265 gssEapDeleteNameAttribute(OM_uint32 *minor,
266                           gss_name_t name,
267                           gss_buffer_t attr);
268
269 OM_uint32
270 gssEapSetNameAttribute(OM_uint32 *minor,
271                        gss_name_t name,
272                        int complete,
273                        gss_buffer_t attr,
274                        gss_buffer_t value);
275
276 OM_uint32
277 gssEapExportAttrContext(OM_uint32 *minor,
278                         gss_name_t name,
279                         gss_buffer_t buffer);
280
281 OM_uint32
282 gssEapImportAttrContext(OM_uint32 *minor,
283                         gss_buffer_t buffer,
284                         gss_name_t name);
285
286 OM_uint32
287 gssEapDuplicateAttrContext(OM_uint32 *minor,
288                            gss_name_t in,
289                            gss_name_t out);
290
291 OM_uint32
292 gssEapMapNameToAny(OM_uint32 *minor,
293                    gss_name_t name,
294                    int authenticated,
295                    gss_buffer_t type_id,
296                    gss_any_t *output);
297
298 OM_uint32
299 gssEapReleaseAnyNameMapping(OM_uint32 *minor,
300                             gss_name_t name,
301                             gss_buffer_t type_id,
302                             gss_any_t *input);
303
304 OM_uint32
305 gssEapReleaseAttrContext(OM_uint32 *minor,
306                          gss_name_t name);
307
308 OM_uint32
309 gssEapAttrProvidersInit(OM_uint32 *minor);
310
311 OM_uint32
312 gssEapAttrProvidersFinalize(OM_uint32 *minor);
313
314 #ifdef __cplusplus
315 }
316 #endif
317
318 #endif /* _UTIL_ATTR_H_ */