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 (*ctx)->server = ns_interface;
56 maj_stat = (*(*ctx)->server->nsi_init)(minor_status, &(*ctx)->ictx);
57 if (maj_stat != GSS_S_COMPLETE)
60 return GSS_S_COMPLETE;
67 OM_uint32 GSSAPI_CALLCONV
68 _gss_ntlm_accept_sec_context
69 (OM_uint32 * minor_status,
70 gss_ctx_id_t * context_handle,
71 const gss_cred_id_t acceptor_cred_handle,
72 const gss_buffer_t input_token_buffer,
73 const gss_channel_bindings_t input_chan_bindings,
74 gss_name_t * src_name,
76 gss_buffer_t output_token,
77 OM_uint32 * ret_flags,
79 gss_cred_id_t * delegated_cred_handle
87 output_token->value = NULL;
88 output_token->length = 0;
92 if (context_handle == NULL)
95 if (input_token_buffer == GSS_C_NO_BUFFER)
99 *src_name = GSS_C_NO_NAME;
101 *mech_type = GSS_C_NO_OID;
106 if (delegated_cred_handle)
107 *delegated_cred_handle = GSS_C_NO_CREDENTIAL;
109 if (*context_handle == GSS_C_NO_CONTEXT) {
110 struct ntlm_type1 type1;
111 OM_uint32 major_status;
115 major_status = _gss_ntlm_allocate_ctx(minor_status, &ctx);
118 *context_handle = (gss_ctx_id_t)ctx;
120 /* check if the mechs is allowed by remote service */
121 major_status = (*ctx->server->nsi_probe)(minor_status, ctx->ictx, NULL);
123 _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
127 data.data = input_token_buffer->value;
128 data.length = input_token_buffer->length;
130 ret = heim_ntlm_decode_type1(&data, &type1);
132 _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
134 return GSS_S_FAILURE;
137 if ((type1.flags & NTLM_NEG_UNICODE) == 0) {
138 heim_ntlm_free_type1(&type1);
139 _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
140 *minor_status = EINVAL;
141 return GSS_S_FAILURE;
144 if (type1.flags & NTLM_NEG_SIGN)
145 ctx->gssflags |= GSS_C_CONF_FLAG;
146 if (type1.flags & NTLM_NEG_SIGN)
147 ctx->gssflags |= GSS_C_INTEG_FLAG;
149 major_status = (*ctx->server->nsi_type2)(minor_status,
156 heim_ntlm_free_type1(&type1);
157 if (major_status != GSS_S_COMPLETE) {
159 _gss_ntlm_delete_sec_context(&gunk, context_handle, NULL);
163 output_token->value = malloc(out.length);
164 if (output_token->value == NULL && out.length != 0) {
166 _gss_ntlm_delete_sec_context(&gunk, context_handle, NULL);
167 *minor_status = ENOMEM;
168 return GSS_S_FAILURE;
170 memcpy(output_token->value, out.data, out.length);
171 output_token->length = out.length;
173 ctx->flags = retflags;
175 return GSS_S_CONTINUE_NEEDED;
178 struct ntlm_type3 type3;
179 struct ntlm_buf session;
181 ctx = (ntlm_ctx)*context_handle;
183 data.data = input_token_buffer->value;
184 data.length = input_token_buffer->length;
186 ret = heim_ntlm_decode_type3(&data, 1, &type3);
188 _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
190 return GSS_S_FAILURE;
193 maj_stat = (*ctx->server->nsi_type3)(minor_status,
198 heim_ntlm_free_type3(&type3);
199 _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
204 ntlm_name n = calloc(1, sizeof(*n));
206 n->user = strdup(type3.username);
207 n->domain = strdup(type3.targetname);
209 if (n == NULL || n->user == NULL || n->domain == NULL) {
210 gss_name_t tempn = (gss_name_t)n;
211 _gss_ntlm_release_name(&junk, &tempn);
212 heim_ntlm_free_type3(&type3);
213 _gss_ntlm_delete_sec_context(minor_status,
214 context_handle, NULL);
217 *src_name = (gss_name_t)n;
220 heim_ntlm_free_type3(&type3);
222 ret = krb5_data_copy(&ctx->sessionkey,
223 session.data, session.length);
226 _gss_ntlm_release_name(&junk, src_name);
227 _gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);
229 return GSS_S_FAILURE;
232 if (session.length != 0) {
234 ctx->status |= STATUS_SESSIONKEY;
236 if (ctx->flags & NTLM_NEG_NTLM2_SESSION) {
237 _gss_ntlm_set_key(&ctx->u.v2.send, 1,
238 (ctx->flags & NTLM_NEG_KEYEX),
239 ctx->sessionkey.data,
240 ctx->sessionkey.length);
241 _gss_ntlm_set_key(&ctx->u.v2.recv, 0,
242 (ctx->flags & NTLM_NEG_KEYEX),
243 ctx->sessionkey.data,
244 ctx->sessionkey.length);
246 RC4_set_key(&ctx->u.v1.crypto_send.key,
247 ctx->sessionkey.length,
248 ctx->sessionkey.data);
249 RC4_set_key(&ctx->u.v1.crypto_recv.key,
250 ctx->sessionkey.length,
251 ctx->sessionkey.data);
256 *mech_type = GSS_NTLM_MECHANISM;
258 *time_rec = GSS_C_INDEFINITE;
260 ctx->status |= STATUS_OPEN;
263 *ret_flags = ctx->gssflags;
265 return GSS_S_COMPLETE;