gss: add _gss_secure_release_buffer_set()
[metze/heimdal/wip.git] / lib / gssapi / mech / gss_utils.c
1 /*-
2  * Copyright (c) 2005 Doug Rabson
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  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *      $FreeBSD: src/lib/libgssapi/gss_utils.c,v 1.1 2005/12/29 14:40:20 dfr Exp $
27  */
28
29 #include "mech_locl.h"
30
31 static OM_uint32
32 _gss_copy_oid(OM_uint32 *minor_status,
33               gss_const_OID from_oid,
34               gss_OID to_oid)
35 {
36         size_t len = from_oid->length;
37
38         *minor_status = 0;
39         to_oid->elements = malloc(len);
40         if (!to_oid->elements) {
41                 to_oid->length = 0;
42                 *minor_status = ENOMEM;
43                 return GSS_S_FAILURE;
44         }
45         to_oid->length = (OM_uint32)len;
46         memcpy(to_oid->elements, from_oid->elements, len);
47         return (GSS_S_COMPLETE);
48 }
49
50 OM_uint32
51 _gss_free_oid(OM_uint32 *minor_status, gss_OID oid)
52 {
53         *minor_status = 0;
54         if (oid->elements) {
55             free(oid->elements);
56             oid->elements = NULL;
57             oid->length = 0;
58         }
59         return (GSS_S_COMPLETE);
60 }
61
62 struct _gss_interned_oid {
63     HEIM_SLIST_ENTRY(_gss_interned_oid) gio_link;
64     gss_OID_desc gio_oid;
65 };
66
67 static HEIM_SLIST_HEAD(_gss_interned_oid_list, _gss_interned_oid) interned_oids =
68 HEIM_SLIST_HEAD_INITIALIZER(interned_oids);
69
70 extern gss_OID _gss_ot_internal[];
71 extern size_t _gss_ot_internal_count;
72
73 static OM_uint32
74 intern_oid_static(OM_uint32 *minor_status,
75                   gss_const_OID from_oid,
76                   gss_OID *to_oid)
77 {
78     size_t i;
79
80     /* statically allocated OIDs */
81     for (i = 0; i < _gss_ot_internal_count; i++) {
82         if (gss_oid_equal(_gss_ot_internal[i], from_oid)) {
83             *minor_status = 0;
84             *to_oid = _gss_ot_internal[i];
85             return GSS_S_COMPLETE;
86         }
87     }
88
89     return GSS_S_CONTINUE_NEEDED;
90 }
91
92 OM_uint32
93 _gss_intern_oid(OM_uint32 *minor_status,
94                 gss_const_OID from_oid,
95                 gss_OID *to_oid)
96 {
97     OM_uint32 major_status;
98     struct _gss_interned_oid *iop;
99
100     major_status = intern_oid_static(minor_status, from_oid, to_oid);
101     if (major_status != GSS_S_CONTINUE_NEEDED)
102         return major_status;
103
104     HEIM_SLIST_ATOMIC_FOREACH(iop, &interned_oids, gio_link) {
105         if (gss_oid_equal(&iop->gio_oid, from_oid)) {
106             *minor_status = 0;
107             *to_oid = &iop->gio_oid;
108             return GSS_S_COMPLETE;
109         }
110     }
111
112     iop = malloc(sizeof(*iop));
113     if (iop == NULL) {
114         *minor_status = ENOMEM;
115         return GSS_S_FAILURE;
116     }
117
118     major_status = _gss_copy_oid(minor_status, from_oid, &iop->gio_oid);
119     if (GSS_ERROR(major_status)) {
120         free(iop);
121         return major_status;
122     }
123
124     HEIM_SLIST_ATOMIC_INSERT_HEAD(&interned_oids, iop, gio_link);
125
126     *minor_status = 0;
127     *to_oid = &iop->gio_oid;
128
129     return GSS_S_COMPLETE;
130 }
131
132 OM_uint32
133 _gss_copy_buffer(OM_uint32 *minor_status,
134     const gss_buffer_t from_buf, gss_buffer_t to_buf)
135 {
136         size_t len = from_buf->length;
137
138         *minor_status = 0;
139         to_buf->value = malloc(len);
140         if (!to_buf->value) {
141                 *minor_status = ENOMEM;
142                 to_buf->length = 0;
143                 return GSS_S_FAILURE;
144         }
145         to_buf->length = len;
146         memcpy(to_buf->value, from_buf->value, len);
147         return (GSS_S_COMPLETE);
148 }
149
150 OM_uint32
151 _gss_secure_release_buffer(OM_uint32 *minor_status,
152                            gss_buffer_t buffer)
153 {
154     if (buffer->value)
155         memset_s(buffer->value, buffer->length, 0, buffer->length);
156
157     return gss_release_buffer(minor_status, buffer);
158 }
159
160 OM_uint32
161 _gss_secure_release_buffer_set(OM_uint32 *minor_status,
162                                gss_buffer_set_t *buffer_set)
163 {
164     size_t i;
165     OM_uint32 minor;
166
167     *minor_status = 0;
168
169     if (*buffer_set == GSS_C_NO_BUFFER_SET)
170         return GSS_S_COMPLETE;
171
172     for (i = 0; i < (*buffer_set)->count; i++)
173         _gss_secure_release_buffer(&minor, &((*buffer_set)->elements[i]));
174
175     (*buffer_set)->count = 0;
176
177     return gss_release_buffer_set(minor_status, buffer_set);
178 }
179
180 void
181 _gss_mg_encode_le_uint32(uint32_t n, uint8_t *p)
182 {
183     p[0] = (n >> 0 ) & 0xFF;
184     p[1] = (n >> 8 ) & 0xFF;
185     p[2] = (n >> 16) & 0xFF;
186     p[3] = (n >> 24) & 0xFF;
187 }
188
189 void
190 _gss_mg_decode_le_uint32(const void *ptr, uint32_t *n)
191 {
192     const uint8_t *p = ptr;
193     *n = (p[0] << 0) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
194 }
195
196 void
197 _gss_mg_encode_be_uint32(uint32_t n, uint8_t *p)
198 {
199     p[0] = (n >> 24) & 0xFF;
200     p[1] = (n >> 16) & 0xFF;
201     p[2] = (n >> 8 ) & 0xFF;
202     p[3] = (n >> 0 ) & 0xFF;
203 }
204
205 void
206 _gss_mg_decode_be_uint32(const void *ptr, uint32_t *n)
207 {
208     const uint8_t *p = ptr;
209     *n = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0);
210 }
211
212 void
213 _gss_mg_encode_le_uint16(uint16_t n, uint8_t *p)
214 {
215     p[0] = (n >> 0 ) & 0xFF;
216     p[1] = (n >> 8 ) & 0xFF;
217 }
218
219 void
220 _gss_mg_decode_le_uint16(const void *ptr, uint16_t *n)
221 {
222     const uint8_t *p = ptr;
223     *n = (p[0] << 0) | (p[1] << 8);
224 }
225
226 void
227 _gss_mg_encode_be_uint16(uint16_t n, uint8_t *p)
228 {
229     p[0] = (n >> 8) & 0xFF;
230     p[1] = (n >> 0) & 0xFF;
231 }
232
233 void
234 _gss_mg_decode_be_uint16(const void *ptr, uint16_t *n)
235 {
236     const uint8_t *p = ptr;
237     *n = (p[0] << 24) | (p[1] << 16);
238 }