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