New attribute provider SPI
[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 struct gss_eap_attr_ctx;
45
46 struct gss_eap_attr_provider
47 {
48 public:
49     gss_eap_attr_provider(const gss_eap_attr_ctx *ctx,
50                           gss_cred_id_t acceptorCred = GSS_C_NO_CREDENTIAL,
51                           gss_ctx_id_t acceptorCtx = GSS_C_NO_CONTEXT)
52     {
53         m_source = ctx;
54     }
55
56     gss_eap_attr_provider(const gss_eap_attr_provider &ctx) {}
57     virtual ~gss_eap_attr_provider(void) {}
58
59     typedef bool
60     gss_eap_attr_enumeration_cb(const gss_eap_attr_provider *provider,
61                                 const gss_buffer_t attribute,
62                                 void *data);
63
64     virtual bool getAttributeTypes(gss_eap_attr_enumeration_cb, void *data) const
65     {
66         return false;
67     }
68
69     virtual void setAttribute(int complete,
70                               const gss_buffer_t attr,
71                               const gss_buffer_t value) {}
72     virtual void deleteAttribute(const gss_buffer_t value) {}
73     virtual bool getAttribute(const gss_buffer_t attr,
74                               int *authenticated,
75                               int *complete,
76                               gss_buffer_t value,
77                               gss_buffer_t display_value,
78                               int *more) const { return false; }
79
80     virtual gss_any_t mapToAny(int authenticated,
81                                gss_buffer_t type_id) const { return NULL; }
82     virtual void releaseAnyNameMapping(gss_buffer_t type_id,
83                                        gss_any_t input) const {}
84
85     virtual void marshall(gss_buffer_t buffer) const {}
86     static gss_eap_attr_provider *unmarshall(const gss_eap_attr_ctx *ctx,
87                                              const gss_buffer_t buffer)
88     { return NULL; }
89
90     static bool init() { return true; }
91     static void finalize() {}
92
93     static gss_eap_attr_provider *
94     createAttrContext(const gss_eap_attr_ctx *ctx,
95                            gss_cred_id_t acceptorCred,
96                            gss_ctx_id_t acceptorCtx) { return NULL; }
97
98 protected:
99     const gss_eap_attr_ctx *m_source;
100 };
101
102 typedef gss_eap_attr_provider * (*gss_eap_attr_create_cb)(
103     const gss_eap_attr_ctx *ctx,
104     gss_cred_id_t acceptorCred,
105     gss_ctx_id_t acceptorCtx);
106
107 struct gss_eap_attr_ctx : gss_eap_attr_provider
108 {
109 public:
110     gss_eap_attr_ctx(const gss_eap_attr_ctx *ctx,
111                      gss_cred_id_t acceptorCred,
112                      gss_ctx_id_t acceptorCtx) :
113         gss_eap_attr_provider(ctx, acceptorCred, acceptorCtx) {}
114
115     gss_eap_attr_ctx(const gss_eap_attr_ctx &ctx);
116
117     ~gss_eap_attr_ctx(void);
118
119     static gss_eap_attr_ctx *createAttrContext(gss_cred_id_t acceptorCred,
120                                                     gss_ctx_id_t acceptorCtx);
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 marshall(gss_buffer_t buffer) const;
141     gss_eap_attr_provider *unmarshall(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 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     gss_eap_attr_provider *getProvider(unsigned int type) const;
170     gss_eap_attr_provider *getProvider(const gss_buffer_t prefix) const;
171
172 private:
173     gss_eap_attr_provider *m_providers[ATTR_TYPE_MAX];
174 };
175
176 #include "util_radius.h"
177 #include "util_saml.h"
178 #include "util_shib.h"
179
180 #include <string>
181 #include <new>
182
183 static inline void
184 duplicateBuffer(gss_buffer_desc &src, gss_buffer_t dst)
185 {
186     OM_uint32 minor;
187
188     if (GSS_ERROR(duplicateBuffer(&minor, &src, dst)))
189         throw new std::bad_alloc();
190 }
191
192 static inline void
193 duplicateBuffer(std::string &str, gss_buffer_t buffer)
194 {
195     gss_buffer_desc tmp;
196
197     tmp.length = str.length();
198     tmp.value = (char *)str.c_str();
199
200     duplicateBuffer(tmp, buffer);
201 }
202
203 #else
204 struct gss_eap_attr_ctx;
205 #endif
206
207 #ifdef __cplusplus
208 extern "C" {
209 #endif
210
211 struct gss_eap_attr_ctx *
212 gssEapCreateAttrContext(gss_cred_id_t acceptorCred,
213                         gss_ctx_id_t acceptorCtx);
214
215 OM_uint32
216 gssEapInquireName(OM_uint32 *minor,
217                   gss_name_t name,
218                   int *name_is_MN,
219                   gss_OID *MN_mech,
220                   gss_buffer_set_t *attrs);
221
222 OM_uint32
223 gssEapGetNameAttribute(OM_uint32 *minor,
224                        gss_name_t name,
225                        gss_buffer_t attr,
226                        int *authenticated,
227                        int *complete,
228                        gss_buffer_t value,
229                        gss_buffer_t display_value,
230                        int *more);
231
232 OM_uint32
233 gssEapDeleteNameAttribute(OM_uint32 *minor,
234                           gss_name_t name,
235                           gss_buffer_t attr);
236
237 OM_uint32
238 gssEapSetNameAttribute(OM_uint32 *minor,
239                        gss_name_t name,
240                        int complete,
241                        gss_buffer_t attr,
242                        gss_buffer_t value);
243
244 OM_uint32
245 gssEapExportAttrContext(OM_uint32 *minor,
246                         gss_name_t name,
247                         gss_buffer_t buffer);
248
249 OM_uint32
250 gssEapImportAttrContext(OM_uint32 *minor,
251                         gss_buffer_t buffer,
252                         gss_name_t name);
253
254 OM_uint32
255 gssEapDuplicateAttrContext(OM_uint32 *minor,
256                            gss_name_t in,
257                            gss_name_t out);
258
259 OM_uint32
260 gssEapMapNameToAny(OM_uint32 *minor,
261                    gss_name_t name,
262                    int authenticated,
263                    gss_buffer_t type_id,
264                    gss_any_t *output);
265
266 OM_uint32
267 gssEapReleaseAnyNameMapping(OM_uint32 *minor,
268                             gss_name_t name,
269                             gss_buffer_t type_id,
270                             gss_any_t *input);
271
272 OM_uint32
273 gssEapReleaseAttrContext(OM_uint32 *minor,
274                          gss_name_t name);
275
276 OM_uint32
277 gssEapAttrProvidersInit(OM_uint32 *minor);
278
279 OM_uint32
280 gssEapAttrProvidersFinalize(OM_uint32 *minor);
281
282 #ifdef __cplusplus
283 }
284 #endif
285
286 #endif /* _UTIL_ATTR_H_ */