Heimdal portability fixes (except for reauth)
[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 /*
34  * Attribute provider interface.
35  */
36
37 #ifndef _UTIL_ATTR_H_
38 #define _UTIL_ATTR_H_ 1
39
40 #ifdef HAVE_HEIMDAL_VERSION
41 /* Removed in draft-ietf-kitten-gssapi-naming-exts-08 */
42 typedef struct gss_any *gss_any_t;
43 #endif
44
45 #ifdef __cplusplus
46 #include <string>
47 #include <new>
48
49 struct gss_eap_attr_provider;
50 struct gss_eap_attr_ctx;
51
52 typedef bool
53 (*gss_eap_attr_enumeration_cb)(const gss_eap_attr_provider *source,
54                                const gss_buffer_t attribute,
55                                void *data);
56
57 #define ATTR_TYPE_RADIUS            0U                  /* RADIUS AVPs */
58 #define ATTR_TYPE_SAML_ASSERTION    1U                  /* SAML assertion */
59 #define ATTR_TYPE_SAML              2U                  /* SAML attributes */
60 #define ATTR_TYPE_LOCAL             3U                  /* Local attributes */
61 #define ATTR_TYPE_MIN               ATTR_TYPE_RADIUS
62 #define ATTR_TYPE_MAX               ATTR_TYPE_LOCAL
63
64 #define ATTR_FLAG_DISABLE_LOCAL     0x00000001
65
66 /*
67  * Attribute provider: this represents a source of attributes derived
68  * from the security context.
69  */
70 struct gss_eap_attr_provider
71 {
72 public:
73     gss_eap_attr_provider(void) {}
74     virtual ~gss_eap_attr_provider(void) {}
75
76     bool initWithManager(const gss_eap_attr_ctx *manager)
77     {
78         m_manager = manager;
79         return true;
80     }
81
82     virtual bool initFromExistingContext(const gss_eap_attr_ctx *manager,
83                                          const gss_eap_attr_provider *ctx)
84     {
85         return initWithManager(manager);
86     }
87
88     virtual bool initFromGssContext(const gss_eap_attr_ctx *manager,
89                                     const gss_cred_id_t cred,
90                                     const gss_ctx_id_t ctx)
91     {
92         return initWithManager(manager);
93     }
94
95     virtual bool getAttributeTypes(gss_eap_attr_enumeration_cb, void *data) const
96     {
97         return false;
98     }
99
100     virtual bool setAttribute(int complete,
101                               const gss_buffer_t attr,
102                               const gss_buffer_t value) { return false; }
103     virtual bool deleteAttribute(const gss_buffer_t value) { return false; }
104     virtual bool getAttribute(const gss_buffer_t attr,
105                               int *authenticated,
106                               int *complete,
107                               gss_buffer_t value,
108                               gss_buffer_t display_value,
109                               int *more) const { return false; }
110
111     virtual gss_any_t mapToAny(int authenticated,
112                                gss_buffer_t type_id) const { return NULL; }
113     virtual void releaseAnyNameMapping(gss_buffer_t type_id,
114                                        gss_any_t input) const {}
115
116     virtual void exportToBuffer(gss_buffer_t buffer) const {}
117     virtual bool initFromBuffer(const gss_eap_attr_ctx *manager,
118                                 const gss_buffer_t buffer)
119     {
120         return initWithManager(manager);
121     }
122
123     virtual time_t getExpiryTime(void) const { return 0; }
124
125     virtual OM_uint32 mapException(OM_uint32 *minor, std::exception &e) const
126     { return GSS_S_CONTINUE_NEEDED; }
127
128     static bool init(void) { return true; }
129     static void finalize(void) {}
130
131     static gss_eap_attr_provider *createAttrContext(void) { return NULL; }
132
133 protected:
134     const gss_eap_attr_ctx *m_manager;
135
136 private:
137     /* make non-copyable */
138     gss_eap_attr_provider(const gss_eap_attr_provider&);
139     gss_eap_attr_provider& operator=(const gss_eap_attr_provider&);
140 };
141
142 typedef gss_eap_attr_provider *(*gss_eap_attr_create_provider)(void);
143
144 /*
145  * Attribute context: this manages a set of providers for a given
146  * security context.
147  */
148 struct gss_eap_attr_ctx
149 {
150 public:
151     gss_eap_attr_ctx(void);
152     ~gss_eap_attr_ctx(void);
153
154     bool initFromExistingContext(const gss_eap_attr_ctx *manager);
155     bool initFromGssContext(const gss_cred_id_t cred,
156                             const gss_ctx_id_t ctx);
157
158     bool getAttributeTypes(gss_eap_attr_enumeration_cb, void *data) const;
159     bool getAttributeTypes(gss_buffer_set_t *attrs);
160
161     bool setAttribute(int complete,
162                       const gss_buffer_t attr,
163                       const gss_buffer_t value);
164     bool deleteAttribute(const gss_buffer_t value);
165     bool getAttribute(const gss_buffer_t attr,
166                       int *authenticated,
167                       int *complete,
168                       gss_buffer_t value,
169                       gss_buffer_t display_value,
170                       int *more) const;
171     gss_any_t mapToAny(int authenticated,
172                        gss_buffer_t type_id) const;
173     void releaseAnyNameMapping(gss_buffer_t type_id,
174                                gss_any_t input) const;
175
176     void exportToBuffer(gss_buffer_t buffer) const;
177     bool initFromBuffer(const gss_buffer_t buffer);
178
179     static unsigned int
180     attributePrefixToType(const gss_buffer_t prefix);
181
182     static const gss_buffer_t
183     attributeTypeToPrefix(unsigned int type);
184
185     static void
186     decomposeAttributeName(const gss_buffer_t attribute,
187                            gss_buffer_t prefix,
188                            gss_buffer_t suffix);
189     static void
190     composeAttributeName(const gss_buffer_t prefix,
191                          const gss_buffer_t suffix,
192                          gss_buffer_t attribute);
193     static void
194     decomposeAttributeName(const gss_buffer_t attribute,
195                            unsigned int *type,
196                            gss_buffer_t suffix);
197     static void
198     composeAttributeName(unsigned int type,
199                          const gss_buffer_t suffix,
200                          gss_buffer_t attribute);
201
202     static std::string
203     composeAttributeName(const gss_buffer_t prefix,
204                          const gss_buffer_t suffix);
205     static std::string
206     composeAttributeName(unsigned int type,
207                          const gss_buffer_t suffix);
208
209     gss_eap_attr_provider *getProvider(unsigned int type) const;
210     gss_eap_attr_provider *getProvider(const gss_buffer_t prefix) const;
211
212     static void
213     registerProvider(unsigned int type,
214                      const char *prefix,
215                      gss_eap_attr_create_provider factory);
216     static void
217     unregisterProvider(unsigned int type);
218
219     time_t getExpiryTime(void) const;
220     OM_uint32 mapException(OM_uint32 *minor, std::exception &e) const;
221
222 private:
223     bool providerEnabled(unsigned int type) const;
224     void releaseProvider(unsigned int type);
225
226     gss_eap_attr_provider *getPrimaryProvider(void) const;
227
228     /* make non-copyable */
229     gss_eap_attr_ctx(const gss_eap_attr_ctx&);
230     gss_eap_attr_ctx& operator=(const gss_eap_attr_ctx&);
231
232     uint32_t m_flags;
233     gss_eap_attr_provider *m_providers[ATTR_TYPE_MAX + 1];
234 };
235
236 #endif /* __cplusplus */
237
238 #include "util_radius.h"
239 #include "util_saml.h"
240 #include "util_shib.h"
241
242 #ifdef __cplusplus
243
244 static inline void
245 duplicateBuffer(gss_buffer_desc &src, gss_buffer_t dst)
246 {
247     OM_uint32 minor;
248
249     if (GSS_ERROR(duplicateBuffer(&minor, &src, dst)))
250         throw new std::bad_alloc();
251 }
252
253 static inline void
254 duplicateBuffer(std::string &str, gss_buffer_t buffer)
255 {
256     gss_buffer_desc tmp;
257
258     tmp.length = str.length();
259     tmp.value = (char *)str.c_str();
260
261     duplicateBuffer(tmp, buffer);
262 }
263
264 #else
265 struct gss_eap_attr_ctx;
266 #endif
267
268 #ifdef __cplusplus
269 extern "C" {
270 #endif
271
272 /*
273  * C wrappers for attribute context functions. These match their
274  * GSS naming extension equivalents. The caller is required to
275  * obtain the name mutex.
276  */
277
278 OM_uint32
279 gssEapCreateAttrContext(OM_uint32 *minor,
280                         gss_cred_id_t acceptorCred,
281                         gss_ctx_id_t acceptorCtx,
282                         struct gss_eap_attr_ctx **pAttrCtx,
283                         time_t *pExpiryTime);
284
285 OM_uint32
286 gssEapInquireName(OM_uint32 *minor,
287                   gss_name_t name,
288                   int *name_is_MN,
289                   gss_OID *MN_mech,
290                   gss_buffer_set_t *attrs);
291
292 OM_uint32
293 gssEapGetNameAttribute(OM_uint32 *minor,
294                        gss_name_t name,
295                        gss_buffer_t attr,
296                        int *authenticated,
297                        int *complete,
298                        gss_buffer_t value,
299                        gss_buffer_t display_value,
300                        int *more);
301
302 OM_uint32
303 gssEapDeleteNameAttribute(OM_uint32 *minor,
304                           gss_name_t name,
305                           gss_buffer_t attr);
306
307 OM_uint32
308 gssEapSetNameAttribute(OM_uint32 *minor,
309                        gss_name_t name,
310                        int complete,
311                        gss_buffer_t attr,
312                        gss_buffer_t value);
313
314 OM_uint32
315 gssEapExportAttrContext(OM_uint32 *minor,
316                         gss_name_t name,
317                         gss_buffer_t buffer);
318
319 OM_uint32
320 gssEapImportAttrContext(OM_uint32 *minor,
321                         gss_buffer_t buffer,
322                         gss_name_t name);
323
324 OM_uint32
325 gssEapDuplicateAttrContext(OM_uint32 *minor,
326                            gss_name_t in,
327                            gss_name_t out);
328
329 OM_uint32
330 gssEapMapNameToAny(OM_uint32 *minor,
331                    gss_name_t name,
332                    int authenticated,
333                    gss_buffer_t type_id,
334                    gss_any_t *output);
335
336 OM_uint32
337 gssEapReleaseAnyNameMapping(OM_uint32 *minor,
338                             gss_name_t name,
339                             gss_buffer_t type_id,
340                             gss_any_t *input);
341
342 OM_uint32
343 gssEapReleaseAttrContext(OM_uint32 *minor,
344                          gss_name_t name);
345
346 OM_uint32
347 gssEapAttrProvidersFinalize(OM_uint32 *minor);
348
349 #ifdef __cplusplus
350 }
351 #endif
352
353 #endif /* _UTIL_ATTR_H_ */