091e219367981ce3ba3bf26ba236555ea5b9c6a0
[samba.git] / source4 / heimdal / lib / gssapi / mech / gss_buffer_set.c
1 /*
2  * Copyright (c) 2004, PADL Software Pty Ltd.
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 PADL Software 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 PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
21  * 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 PADL SOFTWARE 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 #include "mech_locl.h"
34 RCSID("$Id: gss_buffer_set.c 23025 2008-04-17 10:01:57Z lha $");
35
36 OM_uint32 GSSAPI_LIB_FUNCTION
37 gss_create_empty_buffer_set
38            (OM_uint32 * minor_status,
39             gss_buffer_set_t *buffer_set)
40 {
41     gss_buffer_set_t set;
42
43     set = (gss_buffer_set_desc *) malloc(sizeof(*set));
44     if (set == GSS_C_NO_BUFFER_SET) {
45         *minor_status = ENOMEM;
46         return GSS_S_FAILURE;
47     }
48
49     set->count = 0;
50     set->elements = NULL;
51
52     *buffer_set = set;
53
54     *minor_status = 0;
55     return GSS_S_COMPLETE;
56 }
57
58 OM_uint32 GSSAPI_LIB_FUNCTION
59 gss_add_buffer_set_member
60            (OM_uint32 * minor_status,
61             const gss_buffer_t member_buffer,
62             gss_buffer_set_t *buffer_set)
63 {
64     gss_buffer_set_t set;
65     gss_buffer_t p;
66     OM_uint32 ret;
67
68     if (*buffer_set == GSS_C_NO_BUFFER_SET) {
69         ret = gss_create_empty_buffer_set(minor_status,
70                                           buffer_set);
71         if (ret) {
72             return ret;
73         }
74     }
75
76     set = *buffer_set;
77     set->elements = realloc(set->elements,
78                             (set->count + 1) * sizeof(set->elements[0]));
79     if (set->elements == NULL) {
80         *minor_status = ENOMEM;
81         return GSS_S_FAILURE;
82     }
83
84     p = &set->elements[set->count];
85
86     p->value = malloc(member_buffer->length);
87     if (p->value == NULL) {
88         *minor_status = ENOMEM;
89         return GSS_S_FAILURE;
90     }
91     memcpy(p->value, member_buffer->value, member_buffer->length);
92     p->length = member_buffer->length;
93
94     set->count++;
95
96     *minor_status = 0;
97     return GSS_S_COMPLETE;
98 }
99
100 OM_uint32 GSSAPI_LIB_FUNCTION
101 gss_release_buffer_set(OM_uint32 * minor_status,
102                        gss_buffer_set_t *buffer_set)
103 {
104     int i;
105     OM_uint32 minor;
106
107     *minor_status = 0;
108
109     if (*buffer_set == GSS_C_NO_BUFFER_SET)
110         return GSS_S_COMPLETE;
111
112     for (i = 0; i < (*buffer_set)->count; i++)
113         gss_release_buffer(&minor, &((*buffer_set)->elements[i]));
114
115     free((*buffer_set)->elements);
116
117     (*buffer_set)->elements = NULL;
118     (*buffer_set)->count = 0;
119
120     free(*buffer_set);
121     *buffer_set = GSS_C_NO_BUFFER_SET;
122
123     return GSS_S_COMPLETE;
124 }
125