2 * Copyright (c) 2005 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/lib/libgssapi/gss_utils.c,v 1.1 2005/12/29 14:40:20 dfr Exp $
29 #include "mech_locl.h"
32 _gss_copy_oid(OM_uint32 *minor_status,
33 gss_const_OID from_oid,
36 size_t len = from_oid->length;
39 to_oid->elements = malloc(len);
40 if (!to_oid->elements) {
42 *minor_status = ENOMEM;
45 to_oid->length = (OM_uint32)len;
46 memcpy(to_oid->elements, from_oid->elements, len);
47 return (GSS_S_COMPLETE);
51 _gss_free_oid(OM_uint32 *minor_status, gss_OID oid)
59 return (GSS_S_COMPLETE);
62 struct _gss_interned_oid {
63 HEIM_SLIST_ENTRY(_gss_interned_oid) gio_link;
67 static HEIM_SLIST_HEAD(_gss_interned_oid_list, _gss_interned_oid) interned_oids =
68 HEIM_SLIST_HEAD_INITIALIZER(interned_oids);
70 extern gss_OID _gss_ot_internal[];
71 extern size_t _gss_ot_internal_count;
74 intern_oid_static(OM_uint32 *minor_status,
75 gss_const_OID from_oid,
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)) {
84 *to_oid = _gss_ot_internal[i];
85 return GSS_S_COMPLETE;
89 return GSS_S_CONTINUE_NEEDED;
93 _gss_intern_oid(OM_uint32 *minor_status,
94 gss_const_OID from_oid,
97 OM_uint32 major_status;
98 struct _gss_interned_oid *iop;
100 major_status = intern_oid_static(minor_status, from_oid, to_oid);
101 if (major_status != GSS_S_CONTINUE_NEEDED)
104 HEIM_SLIST_ATOMIC_FOREACH(iop, &interned_oids, gio_link) {
105 if (gss_oid_equal(&iop->gio_oid, from_oid)) {
107 *to_oid = &iop->gio_oid;
108 return GSS_S_COMPLETE;
112 iop = malloc(sizeof(*iop));
114 *minor_status = ENOMEM;
115 return GSS_S_FAILURE;
118 major_status = _gss_copy_oid(minor_status, from_oid, &iop->gio_oid);
119 if (GSS_ERROR(major_status)) {
124 HEIM_SLIST_ATOMIC_INSERT_HEAD(&interned_oids, iop, gio_link);
127 *to_oid = &iop->gio_oid;
129 return GSS_S_COMPLETE;
133 _gss_copy_buffer(OM_uint32 *minor_status,
134 const gss_buffer_t from_buf, gss_buffer_t to_buf)
136 size_t len = from_buf->length;
139 to_buf->value = malloc(len);
140 if (!to_buf->value) {
141 *minor_status = ENOMEM;
143 return GSS_S_FAILURE;
145 to_buf->length = len;
146 memcpy(to_buf->value, from_buf->value, len);
147 return (GSS_S_COMPLETE);
151 _gss_secure_release_buffer(OM_uint32 *minor_status,
155 memset_s(buffer->value, buffer->length, 0, buffer->length);
157 return gss_release_buffer(minor_status, buffer);
161 _gss_secure_release_buffer_set(OM_uint32 *minor_status,
162 gss_buffer_set_t *buffer_set)
169 if (*buffer_set == GSS_C_NO_BUFFER_SET)
170 return GSS_S_COMPLETE;
172 for (i = 0; i < (*buffer_set)->count; i++)
173 _gss_secure_release_buffer(&minor, &((*buffer_set)->elements[i]));
175 (*buffer_set)->count = 0;
177 return gss_release_buffer_set(minor_status, buffer_set);
181 _gss_mg_encode_le_uint32(uint32_t n, uint8_t *p)
183 p[0] = (n >> 0 ) & 0xFF;
184 p[1] = (n >> 8 ) & 0xFF;
185 p[2] = (n >> 16) & 0xFF;
186 p[3] = (n >> 24) & 0xFF;
190 _gss_mg_decode_le_uint32(const void *ptr, uint32_t *n)
192 const uint8_t *p = ptr;
193 *n = (p[0] << 0) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
197 _gss_mg_encode_be_uint32(uint32_t n, uint8_t *p)
199 p[0] = (n >> 24) & 0xFF;
200 p[1] = (n >> 16) & 0xFF;
201 p[2] = (n >> 8 ) & 0xFF;
202 p[3] = (n >> 0 ) & 0xFF;
206 _gss_mg_decode_be_uint32(const void *ptr, uint32_t *n)
208 const uint8_t *p = ptr;
209 *n = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0);
213 _gss_mg_encode_le_uint16(uint16_t n, uint8_t *p)
215 p[0] = (n >> 0 ) & 0xFF;
216 p[1] = (n >> 8 ) & 0xFF;
220 _gss_mg_decode_le_uint16(const void *ptr, uint16_t *n)
222 const uint8_t *p = ptr;
223 *n = (p[0] << 0) | (p[1] << 8);
227 _gss_mg_encode_be_uint16(uint16_t n, uint8_t *p)
229 p[0] = (n >> 8) & 0xFF;
230 p[1] = (n >> 0) & 0xFF;
234 _gss_mg_decode_be_uint16(const void *ptr, uint16_t *n)
236 const uint8_t *p = ptr;
237 *n = (p[0] << 24) | (p[1] << 16);