Implement gss_wrap_iov, gss_unwrap_iov for CFX type encryption types.
[heimdal.git] / lib / gssapi / krb5 / set_sec_context_option.c
blob096e83504530aedc2aeffb705d1cec2b0ce592df
1 /*
2 * Copyright (c) 2004, PADL Software Pty Ltd.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 * glue routine for _gsskrb5_inquire_sec_context_by_oid
37 #include "gsskrb5_locl.h"
39 static OM_uint32
40 get_bool(OM_uint32 *minor_status,
41 const gss_buffer_t value,
42 int *flag)
44 if (value->value == NULL || value->length != 1) {
45 *minor_status = EINVAL;
46 return GSS_S_FAILURE;
48 *flag = *((const char *)value->value) != 0;
49 return GSS_S_COMPLETE;
52 static OM_uint32
53 get_string(OM_uint32 *minor_status,
54 const gss_buffer_t value,
55 char **str)
57 if (value == NULL || value->length == 0) {
58 *str = NULL;
59 } else {
60 *str = malloc(value->length + 1);
61 if (*str == NULL) {
62 *minor_status = 0;
63 return GSS_S_UNAVAILABLE;
65 memcpy(*str, value->value, value->length);
66 (*str)[value->length] = '\0';
68 return GSS_S_COMPLETE;
71 static OM_uint32
72 get_int32(OM_uint32 *minor_status,
73 const gss_buffer_t value,
74 OM_uint32 *ret)
76 *minor_status = 0;
77 if (value == NULL || value->length == 0)
78 *ret = 0;
79 else if (value->length == sizeof(*ret))
80 memcpy(ret, value->value, sizeof(*ret));
81 else
82 return GSS_S_UNAVAILABLE;
84 return GSS_S_COMPLETE;
87 static OM_uint32
88 set_int32(OM_uint32 *minor_status,
89 const gss_buffer_t value,
90 OM_uint32 set)
92 *minor_status = 0;
93 if (value->length == sizeof(set))
94 memcpy(value->value, &set, sizeof(set));
95 else
96 return GSS_S_UNAVAILABLE;
98 return GSS_S_COMPLETE;
101 OM_uint32
102 _gsskrb5_set_sec_context_option
103 (OM_uint32 *minor_status,
104 gss_ctx_id_t *context_handle,
105 const gss_OID desired_object,
106 const gss_buffer_t value)
108 krb5_context context;
109 OM_uint32 maj_stat;
111 GSSAPI_KRB5_INIT (&context);
113 if (value == GSS_C_NO_BUFFER) {
114 *minor_status = EINVAL;
115 return GSS_S_FAILURE;
118 if (gss_oid_equal(desired_object, GSS_KRB5_COMPAT_DES3_MIC_X)) {
119 gsskrb5_ctx ctx;
120 int flag;
122 if (*context_handle == GSS_C_NO_CONTEXT) {
123 *minor_status = EINVAL;
124 return GSS_S_NO_CONTEXT;
127 maj_stat = get_bool(minor_status, value, &flag);
128 if (maj_stat != GSS_S_COMPLETE)
129 return maj_stat;
131 ctx = (gsskrb5_ctx)*context_handle;
132 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
133 if (flag)
134 ctx->more_flags |= COMPAT_OLD_DES3;
135 else
136 ctx->more_flags &= ~COMPAT_OLD_DES3;
137 ctx->more_flags |= COMPAT_OLD_DES3_SELECTED;
138 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
139 return GSS_S_COMPLETE;
140 } else if (gss_oid_equal(desired_object, GSS_KRB5_SET_DNS_CANONICALIZE_X)) {
141 int flag;
143 maj_stat = get_bool(minor_status, value, &flag);
144 if (maj_stat != GSS_S_COMPLETE)
145 return maj_stat;
147 krb5_set_dns_canonicalize_hostname(context, flag);
148 return GSS_S_COMPLETE;
150 } else if (gss_oid_equal(desired_object, GSS_KRB5_REGISTER_ACCEPTOR_IDENTITY_X)) {
151 char *str;
153 maj_stat = get_string(minor_status, value, &str);
154 if (maj_stat != GSS_S_COMPLETE)
155 return maj_stat;
157 _gsskrb5_register_acceptor_identity(str);
158 free(str);
160 *minor_status = 0;
161 return GSS_S_COMPLETE;
163 } else if (gss_oid_equal(desired_object, GSS_KRB5_SET_DEFAULT_REALM_X)) {
164 char *str;
166 maj_stat = get_string(minor_status, value, &str);
167 if (maj_stat != GSS_S_COMPLETE)
168 return maj_stat;
169 if (str == NULL) {
170 *minor_status = 0;
171 return GSS_S_CALL_INACCESSIBLE_READ;
174 krb5_set_default_realm(context, str);
175 free(str);
177 *minor_status = 0;
178 return GSS_S_COMPLETE;
180 } else if (gss_oid_equal(desired_object, GSS_KRB5_SEND_TO_KDC_X)) {
182 if (value == NULL || value->length == 0) {
183 krb5_set_send_to_kdc_func(context, NULL, NULL);
184 } else {
185 struct gsskrb5_send_to_kdc c;
187 if (value->length != sizeof(c)) {
188 *minor_status = EINVAL;
189 return GSS_S_FAILURE;
191 memcpy(&c, value->value, sizeof(c));
192 krb5_set_send_to_kdc_func(context,
193 (krb5_send_to_kdc_func)c.func,
194 c.ptr);
197 *minor_status = 0;
198 return GSS_S_COMPLETE;
199 } else if (gss_oid_equal(desired_object, GSS_KRB5_CCACHE_NAME_X)) {
200 char *str;
202 maj_stat = get_string(minor_status, value, &str);
203 if (maj_stat != GSS_S_COMPLETE)
204 return maj_stat;
205 if (str == NULL) {
206 *minor_status = 0;
207 return GSS_S_CALL_INACCESSIBLE_READ;
210 *minor_status = krb5_cc_set_default_name(context, str);
211 free(str);
212 if (*minor_status)
213 return GSS_S_FAILURE;
215 return GSS_S_COMPLETE;
216 } else if (gss_oid_equal(desired_object, GSS_KRB5_SET_TIME_OFFSET_X)) {
217 OM_uint32 offset;
218 time_t t;
220 maj_stat = get_int32(minor_status, value, &offset);
221 if (maj_stat != GSS_S_COMPLETE)
222 return maj_stat;
224 t = time(NULL) + offset;
226 krb5_set_real_time(context, t, 0);
228 *minor_status = 0;
229 return GSS_S_COMPLETE;
230 } else if (gss_oid_equal(desired_object, GSS_KRB5_GET_TIME_OFFSET_X)) {
231 krb5_timestamp sec;
232 int32_t usec;
233 time_t t;
235 t = time(NULL);
237 krb5_us_timeofday (context, &sec, &usec);
239 maj_stat = set_int32(minor_status, value, sec - t);
240 if (maj_stat != GSS_S_COMPLETE)
241 return maj_stat;
243 *minor_status = 0;
244 return GSS_S_COMPLETE;
245 } else if (gss_oid_equal(desired_object, GSS_KRB5_PLUGIN_REGISTER_X)) {
246 struct gsskrb5_krb5_plugin c;
248 if (value->length != sizeof(c)) {
249 *minor_status = EINVAL;
250 return GSS_S_FAILURE;
252 memcpy(&c, value->value, sizeof(c));
253 krb5_plugin_register(context, c.type, c.name, c.symbol);
255 *minor_status = 0;
256 return GSS_S_COMPLETE;
259 *minor_status = EINVAL;
260 return GSS_S_FAILURE;