2 * Copyright (c) 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 _gss_ntlm_allocate_ctx(OM_uint32 *minor_status, ntlm_ctx *ctx)
44 struct ntlm_server_interface *ns_interface = NULL;
47 ns_interface = &ntlmsspi_kdc_digest;
49 if (ns_interface == NULL)
52 *ctx = calloc(1, sizeof(**ctx));
54 *minor_status = ENOMEM;
58 (*ctx)->server = ns_interface;
60 maj_stat = (*(*ctx)->server->nsi_init)(minor_status, &(*ctx)->ictx);
61 if (maj_stat == GSS_S_COMPLETE)
62 return GSS_S_COMPLETE;
75 OM_uint32 GSSAPI_CALLCONV
76 _gss_ntlm_accept_sec_context
77 (OM_uint32 * minor_status,
78 gss_ctx_id_t * context_handle,
79 gss_const_cred_id_t acceptor_cred_handle,
80 const gss_buffer_t input_token_buffer,
81 const gss_channel_bindings_t input_chan_bindings,
82 gss_name_t * src_name,
84 gss_buffer_t output_token,
85 OM_uint32 * ret_flags,
87 gss_cred_id_t * delegated_cred_handle
95 output_token->value = NULL;
96 output_token->length = 0;
100 if (context_handle == NULL)
101 return GSS_S_FAILURE;
103 if (input_token_buffer == GSS_C_NO_BUFFER)
104 return GSS_S_FAILURE;
107 *src_name = GSS_C_NO_NAME;
109 *mech_type = GSS_C_NO_OID;
114 if (delegated_cred_handle)
115 *delegated_cred_handle = GSS_C_NO_CREDENTIAL;
117 if (*context_handle == GSS_C_NO_CONTEXT) {
118 struct ntlm_type1 type1;
119 OM_uint32 major_status;
123 major_status = _gss_ntlm_allocate_ctx(minor_status, &ctx);
126 *context_handle = (gss_ctx_id_t)ctx;
128 /* check if the mechs is allowed by remote service */
129 major_status = (*ctx->server->nsi_probe)(minor_status, ctx->ictx, NULL);
131 _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
135 data.data = input_token_buffer->value;
136 data.length = input_token_buffer->length;
138 ret = heim_ntlm_decode_type1(&data, &type1);
140 _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
142 return GSS_S_DEFECTIVE_TOKEN;
145 if ((type1.flags & NTLM_NEG_UNICODE) == 0) {
146 heim_ntlm_free_type1(&type1);
147 _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
148 *minor_status = EINVAL;
149 return GSS_S_FAILURE;
152 if (type1.flags & NTLM_NEG_SIGN)
153 ctx->gssflags |= GSS_C_CONF_FLAG;
154 if (type1.flags & NTLM_NEG_SIGN)
155 ctx->gssflags |= GSS_C_INTEG_FLAG;
157 major_status = (*ctx->server->nsi_type2)(minor_status,
164 heim_ntlm_free_type1(&type1);
165 if (major_status != GSS_S_COMPLETE) {
167 _gss_ntlm_delete_sec_context(&gunk, context_handle, NULL);
171 output_token->value = malloc(out.length);
172 if (output_token->value == NULL && out.length != 0) {
174 _gss_ntlm_delete_sec_context(&gunk, context_handle, NULL);
175 *minor_status = ENOMEM;
176 return GSS_S_FAILURE;
178 memcpy(output_token->value, out.data, out.length);
179 output_token->length = out.length;
181 ctx->flags = retflags;
183 return GSS_S_CONTINUE_NEEDED;
186 struct ntlm_type3 type3;
187 struct ntlm_buf session;
189 ctx = (ntlm_ctx)*context_handle;
191 data.data = input_token_buffer->value;
192 data.length = input_token_buffer->length;
194 ret = heim_ntlm_decode_type3(&data, 1, &type3);
196 _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
198 return GSS_S_DEFECTIVE_TOKEN;
201 maj_stat = (*ctx->server->nsi_type3)(minor_status,
206 heim_ntlm_free_type3(&type3);
207 _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
212 ntlm_name n = calloc(1, sizeof(*n));
214 n->user = strdup(type3.username);
215 n->domain = strdup(type3.targetname);
217 if (n == NULL || n->user == NULL || n->domain == NULL) {
218 gss_name_t tempn = (gss_name_t)n;
219 _gss_ntlm_release_name(&junk, &tempn);
220 heim_ntlm_free_type3(&type3);
221 _gss_ntlm_delete_sec_context(minor_status,
222 context_handle, NULL);
225 *src_name = (gss_name_t)n;
228 heim_ntlm_free_type3(&type3);
230 ret = krb5_data_copy(&ctx->sessionkey,
231 session.data, session.length);
234 _gss_ntlm_release_name(&junk, src_name);
235 _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
237 return GSS_S_FAILURE;
240 _gss_ntlm_set_keys(ctx);
243 *mech_type = GSS_NTLM_MECHANISM;
245 *time_rec = GSS_C_INDEFINITE;
247 ctx->status |= STATUS_OPEN;
250 *ret_flags = ctx->gssflags;
252 return GSS_S_COMPLETE;