1*ebfedea0SLionel Sambuc /* $NetBSD: acquire_cred.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 <gssapi_spi.h>
40*ebfedea0SLionel Sambuc
41*ebfedea0SLionel Sambuc OM_uint32
_netlogon_acquire_cred(OM_uint32 * min_stat,const gss_name_t desired_name,OM_uint32 time_req,const gss_OID_set desired_mechs,gss_cred_usage_t cred_usage,gss_cred_id_t * output_cred_handle,gss_OID_set * actual_mechs,OM_uint32 * time_rec)42*ebfedea0SLionel Sambuc _netlogon_acquire_cred(OM_uint32 * min_stat,
43*ebfedea0SLionel Sambuc const gss_name_t desired_name,
44*ebfedea0SLionel Sambuc OM_uint32 time_req,
45*ebfedea0SLionel Sambuc const gss_OID_set desired_mechs,
46*ebfedea0SLionel Sambuc gss_cred_usage_t cred_usage,
47*ebfedea0SLionel Sambuc gss_cred_id_t * output_cred_handle,
48*ebfedea0SLionel Sambuc gss_OID_set * actual_mechs,
49*ebfedea0SLionel Sambuc OM_uint32 * time_rec)
50*ebfedea0SLionel Sambuc {
51*ebfedea0SLionel Sambuc OM_uint32 ret;
52*ebfedea0SLionel Sambuc gssnetlogon_cred cred;
53*ebfedea0SLionel Sambuc
54*ebfedea0SLionel Sambuc /* only initiator support so far */
55*ebfedea0SLionel Sambuc if (cred_usage != GSS_C_INITIATE)
56*ebfedea0SLionel Sambuc return GSS_S_FAILURE;
57*ebfedea0SLionel Sambuc
58*ebfedea0SLionel Sambuc if (desired_name == GSS_C_NO_NAME)
59*ebfedea0SLionel Sambuc return GSS_S_BAD_NAME;
60*ebfedea0SLionel Sambuc
61*ebfedea0SLionel Sambuc cred = (gssnetlogon_cred)calloc(1, sizeof(*cred));
62*ebfedea0SLionel Sambuc if (cred == NULL) {
63*ebfedea0SLionel Sambuc *min_stat = ENOMEM;
64*ebfedea0SLionel Sambuc return GSS_S_FAILURE;
65*ebfedea0SLionel Sambuc }
66*ebfedea0SLionel Sambuc cred->SignatureAlgorithm = NL_SIGN_ALG_HMAC_MD5;
67*ebfedea0SLionel Sambuc cred->SealAlgorithm = NL_SEAL_ALG_RC4;
68*ebfedea0SLionel Sambuc
69*ebfedea0SLionel Sambuc ret = _netlogon_duplicate_name(min_stat, desired_name,
70*ebfedea0SLionel Sambuc (gss_name_t *)&cred->Name);
71*ebfedea0SLionel Sambuc if (GSS_ERROR(ret)) {
72*ebfedea0SLionel Sambuc free(cred);
73*ebfedea0SLionel Sambuc return ret;
74*ebfedea0SLionel Sambuc }
75*ebfedea0SLionel Sambuc
76*ebfedea0SLionel Sambuc *output_cred_handle = (gss_cred_id_t)cred;
77*ebfedea0SLionel Sambuc if (actual_mechs != NULL)
78*ebfedea0SLionel Sambuc *actual_mechs = GSS_C_NO_OID_SET;
79*ebfedea0SLionel Sambuc if (time_rec != NULL)
80*ebfedea0SLionel Sambuc *time_rec = GSS_C_INDEFINITE;
81*ebfedea0SLionel Sambuc
82*ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
83*ebfedea0SLionel Sambuc }
84*ebfedea0SLionel Sambuc
85*ebfedea0SLionel Sambuc OM_uint32
_netlogon_acquire_cred_ex(gss_status_id_t status,const gss_name_t desired_name,OM_uint32 flags,OM_uint32 time_req,gss_cred_usage_t cred_usage,gss_auth_identity_t identity,void * ctx,void (* complete)(void *,OM_uint32,gss_status_id_t,gss_cred_id_t,OM_uint32))86*ebfedea0SLionel Sambuc _netlogon_acquire_cred_ex(gss_status_id_t status,
87*ebfedea0SLionel Sambuc const gss_name_t desired_name,
88*ebfedea0SLionel Sambuc OM_uint32 flags,
89*ebfedea0SLionel Sambuc OM_uint32 time_req,
90*ebfedea0SLionel Sambuc gss_cred_usage_t cred_usage,
91*ebfedea0SLionel Sambuc gss_auth_identity_t identity,
92*ebfedea0SLionel Sambuc void *ctx,
93*ebfedea0SLionel Sambuc void (*complete)(void *, OM_uint32, gss_status_id_t, gss_cred_id_t, OM_uint32))
94*ebfedea0SLionel Sambuc {
95*ebfedea0SLionel Sambuc return GSS_S_UNAVAILABLE;
96*ebfedea0SLionel Sambuc }
97*ebfedea0SLionel Sambuc
98*ebfedea0SLionel Sambuc /*
99*ebfedea0SLionel Sambuc * value contains 16 byte session key
100*ebfedea0SLionel Sambuc */
101*ebfedea0SLionel Sambuc static OM_uint32
_netlogon_set_session_key(OM_uint32 * minor_status,gss_cred_id_t * cred_handle,const gss_buffer_t value)102*ebfedea0SLionel Sambuc _netlogon_set_session_key(OM_uint32 *minor_status,
103*ebfedea0SLionel Sambuc gss_cred_id_t *cred_handle,
104*ebfedea0SLionel Sambuc const gss_buffer_t value)
105*ebfedea0SLionel Sambuc {
106*ebfedea0SLionel Sambuc gssnetlogon_cred cred;
107*ebfedea0SLionel Sambuc
108*ebfedea0SLionel Sambuc if (*cred_handle == GSS_C_NO_CREDENTIAL) {
109*ebfedea0SLionel Sambuc *minor_status = EINVAL;
110*ebfedea0SLionel Sambuc return GSS_S_FAILURE;
111*ebfedea0SLionel Sambuc }
112*ebfedea0SLionel Sambuc
113*ebfedea0SLionel Sambuc cred = (gssnetlogon_cred)*cred_handle;
114*ebfedea0SLionel Sambuc
115*ebfedea0SLionel Sambuc if (value->length != sizeof(cred->SessionKey)) {
116*ebfedea0SLionel Sambuc *minor_status = ERANGE;
117*ebfedea0SLionel Sambuc return GSS_S_FAILURE;
118*ebfedea0SLionel Sambuc }
119*ebfedea0SLionel Sambuc
120*ebfedea0SLionel Sambuc memcpy(cred->SessionKey, value->value, value->length);
121*ebfedea0SLionel Sambuc
122*ebfedea0SLionel Sambuc *minor_status = 0;
123*ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
124*ebfedea0SLionel Sambuc }
125*ebfedea0SLionel Sambuc
126*ebfedea0SLionel Sambuc /*
127*ebfedea0SLionel Sambuc * value contains 16 bit little endian encoded seal algorithm
128*ebfedea0SLionel Sambuc */
129*ebfedea0SLionel Sambuc static OM_uint32
_netlogon_set_sign_algorithm(OM_uint32 * minor_status,gss_cred_id_t * cred_handle,const gss_buffer_t value)130*ebfedea0SLionel Sambuc _netlogon_set_sign_algorithm(OM_uint32 *minor_status,
131*ebfedea0SLionel Sambuc gss_cred_id_t *cred_handle,
132*ebfedea0SLionel Sambuc const gss_buffer_t value)
133*ebfedea0SLionel Sambuc {
134*ebfedea0SLionel Sambuc gssnetlogon_cred cred;
135*ebfedea0SLionel Sambuc uint16_t alg;
136*ebfedea0SLionel Sambuc const uint8_t *p;
137*ebfedea0SLionel Sambuc
138*ebfedea0SLionel Sambuc if (*cred_handle == GSS_C_NO_CREDENTIAL) {
139*ebfedea0SLionel Sambuc *minor_status = EINVAL;
140*ebfedea0SLionel Sambuc return GSS_S_FAILURE;
141*ebfedea0SLionel Sambuc }
142*ebfedea0SLionel Sambuc
143*ebfedea0SLionel Sambuc cred = (gssnetlogon_cred)*cred_handle;
144*ebfedea0SLionel Sambuc
145*ebfedea0SLionel Sambuc if (value->length != 2) {
146*ebfedea0SLionel Sambuc *minor_status = ERANGE;
147*ebfedea0SLionel Sambuc return GSS_S_FAILURE;
148*ebfedea0SLionel Sambuc }
149*ebfedea0SLionel Sambuc
150*ebfedea0SLionel Sambuc p = (const uint8_t *)value->value;
151*ebfedea0SLionel Sambuc alg = (p[0] << 0) | (p[1] << 8);
152*ebfedea0SLionel Sambuc
153*ebfedea0SLionel Sambuc if (alg != NL_SIGN_ALG_HMAC_MD5 && alg != NL_SIGN_ALG_SHA256) {
154*ebfedea0SLionel Sambuc *minor_status = EINVAL;
155*ebfedea0SLionel Sambuc return GSS_S_FAILURE;
156*ebfedea0SLionel Sambuc }
157*ebfedea0SLionel Sambuc
158*ebfedea0SLionel Sambuc cred->SignatureAlgorithm = alg;
159*ebfedea0SLionel Sambuc if (alg == NL_SIGN_ALG_SHA256)
160*ebfedea0SLionel Sambuc cred->SealAlgorithm = NL_SEAL_ALG_AES128;
161*ebfedea0SLionel Sambuc else
162*ebfedea0SLionel Sambuc cred->SealAlgorithm = NL_SEAL_ALG_RC4;
163*ebfedea0SLionel Sambuc
164*ebfedea0SLionel Sambuc *minor_status = 0;
165*ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
166*ebfedea0SLionel Sambuc }
167*ebfedea0SLionel Sambuc
168*ebfedea0SLionel Sambuc OM_uint32
_netlogon_set_cred_option(OM_uint32 * minor_status,gss_cred_id_t * cred_handle,const gss_OID desired_object,const gss_buffer_t value)169*ebfedea0SLionel Sambuc _netlogon_set_cred_option
170*ebfedea0SLionel Sambuc (OM_uint32 *minor_status,
171*ebfedea0SLionel Sambuc gss_cred_id_t *cred_handle,
172*ebfedea0SLionel Sambuc const gss_OID desired_object,
173*ebfedea0SLionel Sambuc const gss_buffer_t value)
174*ebfedea0SLionel Sambuc {
175*ebfedea0SLionel Sambuc if (value == GSS_C_NO_BUFFER) {
176*ebfedea0SLionel Sambuc *minor_status = EINVAL;
177*ebfedea0SLionel Sambuc return GSS_S_FAILURE;
178*ebfedea0SLionel Sambuc }
179*ebfedea0SLionel Sambuc
180*ebfedea0SLionel Sambuc if (gss_oid_equal(desired_object, GSS_NETLOGON_SET_SESSION_KEY_X))
181*ebfedea0SLionel Sambuc return _netlogon_set_session_key(minor_status, cred_handle, value);
182*ebfedea0SLionel Sambuc else if (gss_oid_equal(desired_object, GSS_NETLOGON_SET_SIGN_ALGORITHM_X))
183*ebfedea0SLionel Sambuc return _netlogon_set_sign_algorithm(minor_status, cred_handle, value);
184*ebfedea0SLionel Sambuc
185*ebfedea0SLionel Sambuc *minor_status = EINVAL;
186*ebfedea0SLionel Sambuc return GSS_S_FAILURE;
187*ebfedea0SLionel Sambuc }
188*ebfedea0SLionel Sambuc
189