1*ebfedea0SLionel Sambuc /*	$NetBSD: init_sec_context.c,v 1.1.1.1 2011/04/13 18:14:47 elric Exp $	*/
2*ebfedea0SLionel Sambuc 
3*ebfedea0SLionel Sambuc /*
4*ebfedea0SLionel Sambuc  * Copyright (c) 2010 Kungliga Tekniska Högskolan
5*ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6*ebfedea0SLionel Sambuc  * All rights reserved.
7*ebfedea0SLionel Sambuc  *
8*ebfedea0SLionel Sambuc  * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
9*ebfedea0SLionel Sambuc  *
10*ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
12*ebfedea0SLionel Sambuc  * are met:
13*ebfedea0SLionel Sambuc  *
14*ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
15*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
16*ebfedea0SLionel Sambuc  *
17*ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
18*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
19*ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
20*ebfedea0SLionel Sambuc  *
21*ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
22*ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
23*ebfedea0SLionel Sambuc  *    without specific prior written permission.
24*ebfedea0SLionel Sambuc  *
25*ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26*ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27*ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28*ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29*ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30*ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31*ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32*ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33*ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34*ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35*ebfedea0SLionel Sambuc  * SUCH DAMAGE.
36*ebfedea0SLionel Sambuc  */
37*ebfedea0SLionel Sambuc 
38*ebfedea0SLionel Sambuc #include "netlogon.h"
39*ebfedea0SLionel Sambuc #include <nameser.h>
40*ebfedea0SLionel Sambuc 
41*ebfedea0SLionel Sambuc static OM_uint32
_netlogon_encode_dns_string(OM_uint32 * minor_status,const gss_buffer_t str,gss_buffer_t buffer)42*ebfedea0SLionel Sambuc _netlogon_encode_dns_string(OM_uint32 *minor_status,
43*ebfedea0SLionel Sambuc                             const gss_buffer_t str,
44*ebfedea0SLionel Sambuc                             gss_buffer_t buffer)
45*ebfedea0SLionel Sambuc {
46*ebfedea0SLionel Sambuc     int ret;
47*ebfedea0SLionel Sambuc 
48*ebfedea0SLionel Sambuc     memset(buffer->value, 0, buffer->length);
49*ebfedea0SLionel Sambuc 
50*ebfedea0SLionel Sambuc     ret = ns_name_compress((const char *)str->value,
51*ebfedea0SLionel Sambuc                            (uint8_t *)buffer->value, buffer->length,
52*ebfedea0SLionel Sambuc                            NULL, NULL);
53*ebfedea0SLionel Sambuc     if (ret < 0) {
54*ebfedea0SLionel Sambuc         *minor_status = errno;
55*ebfedea0SLionel Sambuc         return GSS_S_FAILURE;
56*ebfedea0SLionel Sambuc     }
57*ebfedea0SLionel Sambuc 
58*ebfedea0SLionel Sambuc     buffer->length = ret;
59*ebfedea0SLionel Sambuc 
60*ebfedea0SLionel Sambuc     *minor_status = 0;
61*ebfedea0SLionel Sambuc     return GSS_S_COMPLETE;
62*ebfedea0SLionel Sambuc }
63*ebfedea0SLionel Sambuc 
64*ebfedea0SLionel Sambuc static OM_uint32
_netlogon_make_initial_auth_message(OM_uint32 * minor_status,gssnetlogon_ctx ctx,gss_buffer_t output_token)65*ebfedea0SLionel Sambuc _netlogon_make_initial_auth_message(OM_uint32 *minor_status,
66*ebfedea0SLionel Sambuc                                     gssnetlogon_ctx ctx,
67*ebfedea0SLionel Sambuc                                     gss_buffer_t output_token)
68*ebfedea0SLionel Sambuc {
69*ebfedea0SLionel Sambuc     uint32_t flags = 0;
70*ebfedea0SLionel Sambuc #define MAX_NL_NAMES    5
71*ebfedea0SLionel Sambuc     gss_buffer_desc names[MAX_NL_NAMES];
72*ebfedea0SLionel Sambuc     uint8_t comp_names[3][MAXHOSTNAMELEN * 2];
73*ebfedea0SLionel Sambuc     size_t n = 0, i = 0, len;
74*ebfedea0SLionel Sambuc     OM_uint32 ret;
75*ebfedea0SLionel Sambuc     uint8_t *p;
76*ebfedea0SLionel Sambuc 
77*ebfedea0SLionel Sambuc     if (ctx->TargetName->NetbiosName.length) {
78*ebfedea0SLionel Sambuc         flags |= NL_FLAG_NETBIOS_DOMAIN_NAME;
79*ebfedea0SLionel Sambuc         names[n] = ctx->TargetName->NetbiosName; /* OEM encoding */
80*ebfedea0SLionel Sambuc         names[n].length++;
81*ebfedea0SLionel Sambuc         n++;
82*ebfedea0SLionel Sambuc     }
83*ebfedea0SLionel Sambuc     if (ctx->SourceName->NetbiosName.length) {
84*ebfedea0SLionel Sambuc         flags |= NL_FLAG_NETBIOS_COMPUTER_NAME;
85*ebfedea0SLionel Sambuc         names[n] = ctx->SourceName->NetbiosName; /* OEM encoding */
86*ebfedea0SLionel Sambuc         names[n].length++;
87*ebfedea0SLionel Sambuc         n++;
88*ebfedea0SLionel Sambuc     }
89*ebfedea0SLionel Sambuc     if (ctx->TargetName->DnsName.length) {
90*ebfedea0SLionel Sambuc         flags |= NL_FLAG_DNS_DOMAIN_NAME;
91*ebfedea0SLionel Sambuc         names[n].value = comp_names[i++];
92*ebfedea0SLionel Sambuc         names[n].length = MAXHOSTNAMELEN * 2;
93*ebfedea0SLionel Sambuc         ret = _netlogon_encode_dns_string(minor_status,
94*ebfedea0SLionel Sambuc                                           &ctx->TargetName->DnsName,
95*ebfedea0SLionel Sambuc                                           &names[n]);
96*ebfedea0SLionel Sambuc         if (GSS_ERROR(ret))
97*ebfedea0SLionel Sambuc             return ret;
98*ebfedea0SLionel Sambuc         n++;
99*ebfedea0SLionel Sambuc     }
100*ebfedea0SLionel Sambuc     if (ctx->SourceName->DnsName.length) {
101*ebfedea0SLionel Sambuc         flags |= NL_FLAG_DNS_HOST_NAME;
102*ebfedea0SLionel Sambuc         names[n].value = comp_names[i++];
103*ebfedea0SLionel Sambuc         names[n].length = MAXHOSTNAMELEN * 2;
104*ebfedea0SLionel Sambuc         ret = _netlogon_encode_dns_string(minor_status,
105*ebfedea0SLionel Sambuc                                           &ctx->SourceName->DnsName,
106*ebfedea0SLionel Sambuc                                           &names[n]);
107*ebfedea0SLionel Sambuc         if (GSS_ERROR(ret))
108*ebfedea0SLionel Sambuc             return ret;
109*ebfedea0SLionel Sambuc         n++;
110*ebfedea0SLionel Sambuc     }
111*ebfedea0SLionel Sambuc     if (ctx->SourceName->NetbiosName.length) {
112*ebfedea0SLionel Sambuc         flags |= NL_FLAG_UTF8_COMPUTER_NAME;
113*ebfedea0SLionel Sambuc         names[n].value = comp_names[i++];
114*ebfedea0SLionel Sambuc         names[n].length = MAXHOSTNAMELEN * 2;
115*ebfedea0SLionel Sambuc         ret = _netlogon_encode_dns_string(minor_status,
116*ebfedea0SLionel Sambuc                                           &ctx->SourceName->NetbiosName,
117*ebfedea0SLionel Sambuc                                           &names[n]);
118*ebfedea0SLionel Sambuc         if (GSS_ERROR(ret))
119*ebfedea0SLionel Sambuc             return ret;
120*ebfedea0SLionel Sambuc         n++;
121*ebfedea0SLionel Sambuc     }
122*ebfedea0SLionel Sambuc 
123*ebfedea0SLionel Sambuc     for (i = 0, len = NL_AUTH_MESSAGE_LENGTH; i < n; i++) {
124*ebfedea0SLionel Sambuc         len += names[i].length;
125*ebfedea0SLionel Sambuc     }
126*ebfedea0SLionel Sambuc 
127*ebfedea0SLionel Sambuc     output_token->value = malloc(len);
128*ebfedea0SLionel Sambuc     if (output_token->value == NULL) {
129*ebfedea0SLionel Sambuc         *minor_status = ENOMEM;
130*ebfedea0SLionel Sambuc         return GSS_S_FAILURE;
131*ebfedea0SLionel Sambuc     }
132*ebfedea0SLionel Sambuc 
133*ebfedea0SLionel Sambuc     p = (uint8_t *)output_token->value;
134*ebfedea0SLionel Sambuc     _gss_mg_encode_le_uint32(NL_NEGOTIATE_REQUEST_MESSAGE, p);
135*ebfedea0SLionel Sambuc     _gss_mg_encode_le_uint32(flags, p + 4);
136*ebfedea0SLionel Sambuc     p += 8;
137*ebfedea0SLionel Sambuc 
138*ebfedea0SLionel Sambuc     for (i = 0; i < n; i++) {
139*ebfedea0SLionel Sambuc         assert(names[i].length != 0);
140*ebfedea0SLionel Sambuc         assert(((char *)names[i].value)[names[i].length - 1] == '\0');
141*ebfedea0SLionel Sambuc         memcpy(p, names[i].value, names[i].length);
142*ebfedea0SLionel Sambuc         p += names[i].length;
143*ebfedea0SLionel Sambuc     }
144*ebfedea0SLionel Sambuc 
145*ebfedea0SLionel Sambuc     output_token->length = len;
146*ebfedea0SLionel Sambuc     assert(p == (uint8_t *)output_token->value + len);
147*ebfedea0SLionel Sambuc 
148*ebfedea0SLionel Sambuc     *minor_status = 0;
149*ebfedea0SLionel Sambuc     return GSS_S_CONTINUE_NEEDED;
150*ebfedea0SLionel Sambuc }
151*ebfedea0SLionel Sambuc 
152*ebfedea0SLionel Sambuc static OM_uint32
_netlogon_read_initial_auth_message(OM_uint32 * minor_status,gssnetlogon_ctx ctx,const gss_buffer_t input_token)153*ebfedea0SLionel Sambuc _netlogon_read_initial_auth_message(OM_uint32 *minor_status,
154*ebfedea0SLionel Sambuc                                     gssnetlogon_ctx ctx,
155*ebfedea0SLionel Sambuc                                     const gss_buffer_t input_token)
156*ebfedea0SLionel Sambuc {
157*ebfedea0SLionel Sambuc     NL_AUTH_MESSAGE msg;
158*ebfedea0SLionel Sambuc     const uint8_t *p = (const uint8_t *)input_token->value;
159*ebfedea0SLionel Sambuc 
160*ebfedea0SLionel Sambuc     if (ctx->State != NL_AUTH_NEGOTIATE) {
161*ebfedea0SLionel Sambuc         *minor_status = EINVAL;
162*ebfedea0SLionel Sambuc         return GSS_S_FAILURE;
163*ebfedea0SLionel Sambuc     }
164*ebfedea0SLionel Sambuc 
165*ebfedea0SLionel Sambuc     if (input_token->length < NL_AUTH_MESSAGE_LENGTH)
166*ebfedea0SLionel Sambuc         return GSS_S_DEFECTIVE_TOKEN;
167*ebfedea0SLionel Sambuc 
168*ebfedea0SLionel Sambuc     _gss_mg_decode_le_uint32(&p[0], &msg.MessageType);
169*ebfedea0SLionel Sambuc     _gss_mg_decode_le_uint32(&p[4], &msg.Flags);
170*ebfedea0SLionel Sambuc 
171*ebfedea0SLionel Sambuc     if (msg.MessageType != NL_NEGOTIATE_RESPONSE_MESSAGE ||
172*ebfedea0SLionel Sambuc         msg.Flags != 0)
173*ebfedea0SLionel Sambuc         return GSS_S_DEFECTIVE_TOKEN;
174*ebfedea0SLionel Sambuc 
175*ebfedea0SLionel Sambuc     ctx->State = NL_AUTH_ESTABLISHED;
176*ebfedea0SLionel Sambuc 
177*ebfedea0SLionel Sambuc     *minor_status = 0;
178*ebfedea0SLionel Sambuc     return GSS_S_COMPLETE;
179*ebfedea0SLionel Sambuc }
180*ebfedea0SLionel Sambuc 
181*ebfedea0SLionel Sambuc static OM_uint32
_netlogon_alloc_context(OM_uint32 * minor_status,gssnetlogon_ctx * pContext)182*ebfedea0SLionel Sambuc _netlogon_alloc_context(OM_uint32 *minor_status,
183*ebfedea0SLionel Sambuc                         gssnetlogon_ctx *pContext)
184*ebfedea0SLionel Sambuc {
185*ebfedea0SLionel Sambuc     gssnetlogon_ctx ctx;
186*ebfedea0SLionel Sambuc 
187*ebfedea0SLionel Sambuc     ctx = (gssnetlogon_ctx)calloc(1, sizeof(*ctx));
188*ebfedea0SLionel Sambuc     if (ctx == NULL) {
189*ebfedea0SLionel Sambuc         *minor_status = ENOMEM;
190*ebfedea0SLionel Sambuc         return GSS_S_FAILURE;
191*ebfedea0SLionel Sambuc     }
192*ebfedea0SLionel Sambuc 
193*ebfedea0SLionel Sambuc     ctx->State = NL_AUTH_NEGOTIATE;
194*ebfedea0SLionel Sambuc     ctx->LocallyInitiated = 1;
195*ebfedea0SLionel Sambuc     ctx->MessageBlockSize = 1;
196*ebfedea0SLionel Sambuc 
197*ebfedea0SLionel Sambuc     HEIMDAL_MUTEX_init(&ctx->Mutex);
198*ebfedea0SLionel Sambuc 
199*ebfedea0SLionel Sambuc     *pContext = ctx;
200*ebfedea0SLionel Sambuc 
201*ebfedea0SLionel Sambuc     return GSS_S_COMPLETE;
202*ebfedea0SLionel Sambuc }
203*ebfedea0SLionel Sambuc 
204*ebfedea0SLionel Sambuc OM_uint32
_netlogon_init_sec_context(OM_uint32 * minor_status,const gss_cred_id_t initiator_cred_handle,gss_ctx_id_t * context_handle,const gss_name_t target_name,const gss_OID mech_type,OM_uint32 req_flags,OM_uint32 time_req,const gss_channel_bindings_t input_chan_bindings,const gss_buffer_t input_token,gss_OID * actual_mech_type,gss_buffer_t output_token,OM_uint32 * ret_flags,OM_uint32 * time_rec)205*ebfedea0SLionel Sambuc _netlogon_init_sec_context(OM_uint32 * minor_status,
206*ebfedea0SLionel Sambuc 			   const gss_cred_id_t initiator_cred_handle,
207*ebfedea0SLionel Sambuc 			   gss_ctx_id_t * context_handle,
208*ebfedea0SLionel Sambuc 			   const gss_name_t target_name,
209*ebfedea0SLionel Sambuc 			   const gss_OID mech_type,
210*ebfedea0SLionel Sambuc 			   OM_uint32 req_flags,
211*ebfedea0SLionel Sambuc 			   OM_uint32 time_req,
212*ebfedea0SLionel Sambuc 			   const gss_channel_bindings_t input_chan_bindings,
213*ebfedea0SLionel Sambuc 			   const gss_buffer_t input_token,
214*ebfedea0SLionel Sambuc 			   gss_OID * actual_mech_type,
215*ebfedea0SLionel Sambuc 			   gss_buffer_t output_token,
216*ebfedea0SLionel Sambuc 			   OM_uint32 * ret_flags,
217*ebfedea0SLionel Sambuc 			   OM_uint32 * time_rec)
218*ebfedea0SLionel Sambuc {
219*ebfedea0SLionel Sambuc     const gssnetlogon_cred cred = (const gssnetlogon_cred)initiator_cred_handle;
220*ebfedea0SLionel Sambuc     gssnetlogon_ctx ctx = (gssnetlogon_ctx)*context_handle;
221*ebfedea0SLionel Sambuc     const gssnetlogon_name target = (const gssnetlogon_name)target_name;
222*ebfedea0SLionel Sambuc     OM_uint32 ret;
223*ebfedea0SLionel Sambuc 
224*ebfedea0SLionel Sambuc     *minor_status = 0;
225*ebfedea0SLionel Sambuc 
226*ebfedea0SLionel Sambuc     output_token->value = NULL;
227*ebfedea0SLionel Sambuc     output_token->length = 0;
228*ebfedea0SLionel Sambuc 
229*ebfedea0SLionel Sambuc     /* Validate arguments */
230*ebfedea0SLionel Sambuc     if (cred == NULL)
231*ebfedea0SLionel Sambuc         return GSS_S_NO_CRED;
232*ebfedea0SLionel Sambuc     else if (target == NULL)
233*ebfedea0SLionel Sambuc         return GSS_S_BAD_NAME;
234*ebfedea0SLionel Sambuc 
235*ebfedea0SLionel Sambuc     if (ctx == NULL) {
236*ebfedea0SLionel Sambuc         if (input_token->length != 0)
237*ebfedea0SLionel Sambuc             return GSS_S_DEFECTIVE_TOKEN;
238*ebfedea0SLionel Sambuc 
239*ebfedea0SLionel Sambuc         ret = _netlogon_alloc_context(minor_status, &ctx);
240*ebfedea0SLionel Sambuc         if (GSS_ERROR(ret))
241*ebfedea0SLionel Sambuc             goto cleanup;
242*ebfedea0SLionel Sambuc 
243*ebfedea0SLionel Sambuc         HEIMDAL_MUTEX_lock(&ctx->Mutex);
244*ebfedea0SLionel Sambuc         *context_handle = (gss_ctx_id_t)ctx;
245*ebfedea0SLionel Sambuc 
246*ebfedea0SLionel Sambuc 	ctx->GssFlags = req_flags & (GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG |
247*ebfedea0SLionel Sambuc 				     GSS_C_SEQUENCE_FLAG | GSS_C_CONF_FLAG |
248*ebfedea0SLionel Sambuc 				     GSS_C_INTEG_FLAG | GSS_C_DCE_STYLE);
249*ebfedea0SLionel Sambuc         ctx->SignatureAlgorithm = cred->SignatureAlgorithm;
250*ebfedea0SLionel Sambuc         ctx->SealAlgorithm = cred->SealAlgorithm;
251*ebfedea0SLionel Sambuc 
252*ebfedea0SLionel Sambuc         ret = _netlogon_duplicate_name(minor_status, (gss_name_t)cred->Name,
253*ebfedea0SLionel Sambuc                                        (gss_name_t *)&ctx->SourceName);
254*ebfedea0SLionel Sambuc         if (GSS_ERROR(ret))
255*ebfedea0SLionel Sambuc             goto cleanup;
256*ebfedea0SLionel Sambuc 
257*ebfedea0SLionel Sambuc         ret = _netlogon_duplicate_name(minor_status, (gss_name_t)target,
258*ebfedea0SLionel Sambuc                                        (gss_name_t *)&ctx->TargetName);
259*ebfedea0SLionel Sambuc         if (GSS_ERROR(ret))
260*ebfedea0SLionel Sambuc             goto cleanup;
261*ebfedea0SLionel Sambuc 
262*ebfedea0SLionel Sambuc         memcpy(ctx->SessionKey, cred->SessionKey, sizeof(cred->SessionKey));
263*ebfedea0SLionel Sambuc 
264*ebfedea0SLionel Sambuc         ret = _netlogon_make_initial_auth_message(minor_status, ctx,
265*ebfedea0SLionel Sambuc                                                   output_token);
266*ebfedea0SLionel Sambuc         if (GSS_ERROR(ret))
267*ebfedea0SLionel Sambuc             goto cleanup;
268*ebfedea0SLionel Sambuc     } else {
269*ebfedea0SLionel Sambuc         HEIMDAL_MUTEX_lock(&ctx->Mutex);
270*ebfedea0SLionel Sambuc         ret = _netlogon_read_initial_auth_message(minor_status, ctx,
271*ebfedea0SLionel Sambuc                                                   input_token);
272*ebfedea0SLionel Sambuc     }
273*ebfedea0SLionel Sambuc 
274*ebfedea0SLionel Sambuc     if (ret_flags != NULL)
275*ebfedea0SLionel Sambuc 	*ret_flags = ctx->GssFlags;
276*ebfedea0SLionel Sambuc     if (time_rec != NULL)
277*ebfedea0SLionel Sambuc 	*time_rec = GSS_C_INDEFINITE;
278*ebfedea0SLionel Sambuc     if (actual_mech_type != NULL)
279*ebfedea0SLionel Sambuc 	*actual_mech_type = GSS_NETLOGON_MECHANISM;
280*ebfedea0SLionel Sambuc 
281*ebfedea0SLionel Sambuc cleanup:
282*ebfedea0SLionel Sambuc     HEIMDAL_MUTEX_unlock(&ctx->Mutex);
283*ebfedea0SLionel Sambuc 
284*ebfedea0SLionel Sambuc     if (ret != GSS_S_COMPLETE && ret != GSS_S_CONTINUE_NEEDED) {
285*ebfedea0SLionel Sambuc         OM_uint32 tmp;
286*ebfedea0SLionel Sambuc         _netlogon_delete_sec_context(&tmp, context_handle, NULL);
287*ebfedea0SLionel Sambuc     }
288*ebfedea0SLionel Sambuc 
289*ebfedea0SLionel Sambuc     return ret;
290*ebfedea0SLionel Sambuc }
291*ebfedea0SLionel Sambuc 
292