1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright (c) 1986-1995, 1997, 2001 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate * All rights reserved.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved.
31*0Sstevel@tonic-gate *
32*0Sstevel@tonic-gate * $Header:
33*0Sstevel@tonic-gate * /afs/gza.com/product/secure/rel-eng/src/1.1/rpc/RCS/auth_gssapi_misc.c,v
34*0Sstevel@tonic-gate * 1.10 1994/10/27 12:39:23 jik Exp $
35*0Sstevel@tonic-gate */
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate #include <stdlib.h>
38*0Sstevel@tonic-gate #include <gssapi/gssapi.h>
39*0Sstevel@tonic-gate #include <rpc/rpc.h>
40*0Sstevel@tonic-gate #include <rpc/rpcsec_defs.h>
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate * Miscellaneous XDR routines.
44*0Sstevel@tonic-gate */
45*0Sstevel@tonic-gate bool_t
__xdr_gss_buf(xdrs,buf)46*0Sstevel@tonic-gate __xdr_gss_buf(xdrs, buf)
47*0Sstevel@tonic-gate XDR *xdrs;
48*0Sstevel@tonic-gate gss_buffer_t buf;
49*0Sstevel@tonic-gate {
50*0Sstevel@tonic-gate u_int cast_len, bound_len;
51*0Sstevel@tonic-gate
52*0Sstevel@tonic-gate /*
53*0Sstevel@tonic-gate * We go through this contortion because size_t is a now a ulong,
54*0Sstevel@tonic-gate * GSS-API uses ulongs.
55*0Sstevel@tonic-gate */
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate if (xdrs->x_op != XDR_DECODE) {
58*0Sstevel@tonic-gate bound_len = cast_len = (u_int) buf->length;
59*0Sstevel@tonic-gate } else {
60*0Sstevel@tonic-gate bound_len = (u_int)-1;
61*0Sstevel@tonic-gate }
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate if (xdr_bytes(xdrs, (char **)&buf->value, &cast_len,
64*0Sstevel@tonic-gate bound_len) == TRUE) {
65*0Sstevel@tonic-gate if (xdrs->x_op == XDR_DECODE)
66*0Sstevel@tonic-gate buf->length = cast_len;
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gate return (TRUE);
69*0Sstevel@tonic-gate }
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate return (FALSE);
72*0Sstevel@tonic-gate }
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate bool_t
__xdr_rpc_gss_creds(xdrs,creds)75*0Sstevel@tonic-gate __xdr_rpc_gss_creds(xdrs, creds)
76*0Sstevel@tonic-gate XDR *xdrs;
77*0Sstevel@tonic-gate rpc_gss_creds *creds;
78*0Sstevel@tonic-gate {
79*0Sstevel@tonic-gate if (!xdr_u_int(xdrs, &creds->version) ||
80*0Sstevel@tonic-gate !xdr_u_int(xdrs, &creds->gss_proc) ||
81*0Sstevel@tonic-gate !xdr_u_int(xdrs, &creds->seq_num) ||
82*0Sstevel@tonic-gate !xdr_u_int(xdrs, (u_int *)&creds->service) ||
83*0Sstevel@tonic-gate !__xdr_gss_buf(xdrs, &creds->ctx_handle))
84*0Sstevel@tonic-gate return (FALSE);
85*0Sstevel@tonic-gate return (TRUE);
86*0Sstevel@tonic-gate }
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate bool_t
__xdr_rpc_gss_init_arg(xdrs,init_arg)89*0Sstevel@tonic-gate __xdr_rpc_gss_init_arg(xdrs, init_arg)
90*0Sstevel@tonic-gate XDR *xdrs;
91*0Sstevel@tonic-gate rpc_gss_init_arg *init_arg;
92*0Sstevel@tonic-gate {
93*0Sstevel@tonic-gate if (!__xdr_gss_buf(xdrs, init_arg))
94*0Sstevel@tonic-gate return (FALSE);
95*0Sstevel@tonic-gate return (TRUE);
96*0Sstevel@tonic-gate }
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gate bool_t
__xdr_rpc_gss_init_res(xdrs,init_res)99*0Sstevel@tonic-gate __xdr_rpc_gss_init_res(xdrs, init_res)
100*0Sstevel@tonic-gate XDR *xdrs;
101*0Sstevel@tonic-gate rpc_gss_init_res *init_res;
102*0Sstevel@tonic-gate {
103*0Sstevel@tonic-gate if (!__xdr_gss_buf(xdrs, &init_res->ctx_handle) ||
104*0Sstevel@tonic-gate !xdr_u_int(xdrs, (u_int *)&init_res->gss_major) ||
105*0Sstevel@tonic-gate !xdr_u_int(xdrs, (u_int *)&init_res->gss_minor) ||
106*0Sstevel@tonic-gate !xdr_u_int(xdrs, (u_int *)&init_res->seq_window) ||
107*0Sstevel@tonic-gate !__xdr_gss_buf(xdrs, &init_res->token))
108*0Sstevel@tonic-gate return (FALSE);
109*0Sstevel@tonic-gate return (TRUE);
110*0Sstevel@tonic-gate }
111*0Sstevel@tonic-gate
112*0Sstevel@tonic-gate /*
113*0Sstevel@tonic-gate * Generic routine to wrap data used by client and server sides.
114*0Sstevel@tonic-gate */
115*0Sstevel@tonic-gate bool_t
__rpc_gss_wrap_data(service,qop,context,seq_num,out_xdrs,xdr_func,xdr_ptr)116*0Sstevel@tonic-gate __rpc_gss_wrap_data(service, qop, context, seq_num, out_xdrs, xdr_func,
117*0Sstevel@tonic-gate xdr_ptr)
118*0Sstevel@tonic-gate OM_uint32 qop;
119*0Sstevel@tonic-gate rpc_gss_service_t service;
120*0Sstevel@tonic-gate gss_ctx_id_t context;
121*0Sstevel@tonic-gate u_int seq_num;
122*0Sstevel@tonic-gate XDR *out_xdrs;
123*0Sstevel@tonic-gate bool_t (*xdr_func)();
124*0Sstevel@tonic-gate caddr_t xdr_ptr;
125*0Sstevel@tonic-gate {
126*0Sstevel@tonic-gate OM_uint32 minor;
127*0Sstevel@tonic-gate gss_buffer_desc in_buf, out_buf;
128*0Sstevel@tonic-gate XDR temp_xdrs;
129*0Sstevel@tonic-gate bool_t conf_state;
130*0Sstevel@tonic-gate bool_t ret = FALSE;
131*0Sstevel@tonic-gate u_int bufsiz;
132*0Sstevel@tonic-gate char *buf;
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate /*
135*0Sstevel@tonic-gate * Create a temporary XDR/buffer to hold the data to be wrapped.
136*0Sstevel@tonic-gate */
137*0Sstevel@tonic-gate out_buf.length = 0;
138*0Sstevel@tonic-gate bufsiz = xdr_sizeof(xdr_func, xdr_ptr) +
139*0Sstevel@tonic-gate xdr_sizeof(xdr_u_int, &seq_num);
140*0Sstevel@tonic-gate if ((buf = (char *)malloc(bufsiz)) == NULL) {
141*0Sstevel@tonic-gate fprintf(stderr, dgettext(TEXT_DOMAIN, "malloc failed in "
142*0Sstevel@tonic-gate "__rpc_gss_wrap_data\n"));
143*0Sstevel@tonic-gate return (FALSE);
144*0Sstevel@tonic-gate }
145*0Sstevel@tonic-gate xdrmem_create(&temp_xdrs, buf, bufsiz, XDR_ENCODE);
146*0Sstevel@tonic-gate
147*0Sstevel@tonic-gate /*
148*0Sstevel@tonic-gate * serialize the sequence number into tmp memory
149*0Sstevel@tonic-gate */
150*0Sstevel@tonic-gate if (!xdr_u_int(&temp_xdrs, &seq_num))
151*0Sstevel@tonic-gate goto fail;
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate /*
154*0Sstevel@tonic-gate * serialize the arguments into tmp memory
155*0Sstevel@tonic-gate */
156*0Sstevel@tonic-gate if (!(*xdr_func)(&temp_xdrs, xdr_ptr))
157*0Sstevel@tonic-gate goto fail;
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate /*
160*0Sstevel@tonic-gate * Data to be wrapped goes in in_buf. If privacy is used,
161*0Sstevel@tonic-gate * out_buf will have wrapped data (in_buf will no longer be
162*0Sstevel@tonic-gate * needed). If integrity is used, out_buf will have checksum
163*0Sstevel@tonic-gate * which will follow the data in in_buf.
164*0Sstevel@tonic-gate */
165*0Sstevel@tonic-gate in_buf.length = xdr_getpos(&temp_xdrs);
166*0Sstevel@tonic-gate in_buf.value = temp_xdrs.x_base;
167*0Sstevel@tonic-gate
168*0Sstevel@tonic-gate switch (service) {
169*0Sstevel@tonic-gate case rpc_gss_svc_privacy:
170*0Sstevel@tonic-gate if (gss_seal(&minor, context, TRUE, qop, &in_buf,
171*0Sstevel@tonic-gate &conf_state, &out_buf) != GSS_S_COMPLETE)
172*0Sstevel@tonic-gate goto fail;
173*0Sstevel@tonic-gate in_buf.length = 0; /* in_buf not needed */
174*0Sstevel@tonic-gate if (!conf_state)
175*0Sstevel@tonic-gate goto fail;
176*0Sstevel@tonic-gate break;
177*0Sstevel@tonic-gate case rpc_gss_svc_integrity:
178*0Sstevel@tonic-gate if (gss_sign(&minor, context, qop, &in_buf,
179*0Sstevel@tonic-gate &out_buf) != GSS_S_COMPLETE)
180*0Sstevel@tonic-gate goto fail;
181*0Sstevel@tonic-gate break;
182*0Sstevel@tonic-gate default:
183*0Sstevel@tonic-gate goto fail;
184*0Sstevel@tonic-gate }
185*0Sstevel@tonic-gate
186*0Sstevel@tonic-gate /*
187*0Sstevel@tonic-gate * write out in_buf and out_buf as needed
188*0Sstevel@tonic-gate */
189*0Sstevel@tonic-gate if (in_buf.length != 0) {
190*0Sstevel@tonic-gate if (!__xdr_gss_buf(out_xdrs, &in_buf))
191*0Sstevel@tonic-gate goto fail;
192*0Sstevel@tonic-gate }
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gate if (!__xdr_gss_buf(out_xdrs, &out_buf))
195*0Sstevel@tonic-gate goto fail;
196*0Sstevel@tonic-gate ret = TRUE;
197*0Sstevel@tonic-gate fail:
198*0Sstevel@tonic-gate XDR_DESTROY(&temp_xdrs);
199*0Sstevel@tonic-gate if (buf)
200*0Sstevel@tonic-gate (void) free(buf);
201*0Sstevel@tonic-gate if (out_buf.length != 0)
202*0Sstevel@tonic-gate (void) gss_release_buffer(&minor, &out_buf);
203*0Sstevel@tonic-gate return (ret);
204*0Sstevel@tonic-gate }
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate /*
207*0Sstevel@tonic-gate * Generic routine to unwrap data used by client and server sides.
208*0Sstevel@tonic-gate */
209*0Sstevel@tonic-gate bool_t
__rpc_gss_unwrap_data(service,context,seq_num,qop_check,in_xdrs,xdr_func,xdr_ptr)210*0Sstevel@tonic-gate __rpc_gss_unwrap_data(service, context, seq_num, qop_check, in_xdrs, xdr_func,
211*0Sstevel@tonic-gate xdr_ptr)
212*0Sstevel@tonic-gate rpc_gss_service_t service;
213*0Sstevel@tonic-gate gss_ctx_id_t context;
214*0Sstevel@tonic-gate u_int seq_num;
215*0Sstevel@tonic-gate OM_uint32 qop_check;
216*0Sstevel@tonic-gate XDR *in_xdrs;
217*0Sstevel@tonic-gate bool_t (*xdr_func)();
218*0Sstevel@tonic-gate caddr_t xdr_ptr;
219*0Sstevel@tonic-gate {
220*0Sstevel@tonic-gate gss_buffer_desc in_buf, out_buf;
221*0Sstevel@tonic-gate XDR temp_xdrs;
222*0Sstevel@tonic-gate u_int seq_num2;
223*0Sstevel@tonic-gate bool_t conf;
224*0Sstevel@tonic-gate OM_uint32 major = GSS_S_COMPLETE, minor = 0;
225*0Sstevel@tonic-gate int qop;
226*0Sstevel@tonic-gate
227*0Sstevel@tonic-gate in_buf.value = NULL;
228*0Sstevel@tonic-gate out_buf.value = NULL;
229*0Sstevel@tonic-gate
230*0Sstevel@tonic-gate /*
231*0Sstevel@tonic-gate * Pull out wrapped data. For privacy service, this is the
232*0Sstevel@tonic-gate * encrypted data. For integrity service, this is the data
233*0Sstevel@tonic-gate * followed by a checksum.
234*0Sstevel@tonic-gate */
235*0Sstevel@tonic-gate if (!__xdr_gss_buf(in_xdrs, &in_buf))
236*0Sstevel@tonic-gate return (FALSE);
237*0Sstevel@tonic-gate
238*0Sstevel@tonic-gate if (service == rpc_gss_svc_privacy) {
239*0Sstevel@tonic-gate major = gss_unseal(&minor, context, &in_buf, &out_buf, &conf,
240*0Sstevel@tonic-gate &qop);
241*0Sstevel@tonic-gate free(in_buf.value);
242*0Sstevel@tonic-gate if (major != GSS_S_COMPLETE)
243*0Sstevel@tonic-gate return (FALSE);
244*0Sstevel@tonic-gate /*
245*0Sstevel@tonic-gate * Keep the returned token (unencrypted data) in in_buf.
246*0Sstevel@tonic-gate */
247*0Sstevel@tonic-gate in_buf.length = out_buf.length;
248*0Sstevel@tonic-gate in_buf.value = out_buf.value;
249*0Sstevel@tonic-gate
250*0Sstevel@tonic-gate /*
251*0Sstevel@tonic-gate * If privacy was not used, or if QOP is not what we are
252*0Sstevel@tonic-gate * expecting, fail.
253*0Sstevel@tonic-gate */
254*0Sstevel@tonic-gate if (!conf || qop != qop_check)
255*0Sstevel@tonic-gate goto fail;
256*0Sstevel@tonic-gate
257*0Sstevel@tonic-gate } else if (service == rpc_gss_svc_integrity) {
258*0Sstevel@tonic-gate if (!__xdr_gss_buf(in_xdrs, &out_buf))
259*0Sstevel@tonic-gate return (FALSE);
260*0Sstevel@tonic-gate major = gss_verify(&minor, context, &in_buf, &out_buf, &qop);
261*0Sstevel@tonic-gate free(out_buf.value);
262*0Sstevel@tonic-gate if (major != GSS_S_COMPLETE) {
263*0Sstevel@tonic-gate free(in_buf.value);
264*0Sstevel@tonic-gate return (FALSE);
265*0Sstevel@tonic-gate }
266*0Sstevel@tonic-gate
267*0Sstevel@tonic-gate /*
268*0Sstevel@tonic-gate * If QOP is not what we are expecting, fail.
269*0Sstevel@tonic-gate */
270*0Sstevel@tonic-gate if (qop != qop_check)
271*0Sstevel@tonic-gate goto fail;
272*0Sstevel@tonic-gate }
273*0Sstevel@tonic-gate
274*0Sstevel@tonic-gate xdrmem_create(&temp_xdrs, in_buf.value, in_buf.length, XDR_DECODE);
275*0Sstevel@tonic-gate
276*0Sstevel@tonic-gate /*
277*0Sstevel@tonic-gate * The data consists of the sequence number followed by the
278*0Sstevel@tonic-gate * arguments. Make sure sequence number is what we are
279*0Sstevel@tonic-gate * expecting (i.e., the value in the header).
280*0Sstevel@tonic-gate */
281*0Sstevel@tonic-gate if (!xdr_u_int(&temp_xdrs, &seq_num2))
282*0Sstevel@tonic-gate goto fail;
283*0Sstevel@tonic-gate if (seq_num2 != seq_num)
284*0Sstevel@tonic-gate goto fail;
285*0Sstevel@tonic-gate
286*0Sstevel@tonic-gate /*
287*0Sstevel@tonic-gate * Deserialize the arguments into xdr_ptr, and release in_buf.
288*0Sstevel@tonic-gate */
289*0Sstevel@tonic-gate if (!(*xdr_func)(&temp_xdrs, xdr_ptr))
290*0Sstevel@tonic-gate goto fail;
291*0Sstevel@tonic-gate
292*0Sstevel@tonic-gate if (service == rpc_gss_svc_privacy)
293*0Sstevel@tonic-gate (void) gss_release_buffer(&minor, &in_buf);
294*0Sstevel@tonic-gate else
295*0Sstevel@tonic-gate free(in_buf.value);
296*0Sstevel@tonic-gate XDR_DESTROY(&temp_xdrs);
297*0Sstevel@tonic-gate return (TRUE);
298*0Sstevel@tonic-gate fail:
299*0Sstevel@tonic-gate XDR_DESTROY(&temp_xdrs);
300*0Sstevel@tonic-gate if (service == rpc_gss_svc_privacy)
301*0Sstevel@tonic-gate (void) gss_release_buffer(&minor, &in_buf);
302*0Sstevel@tonic-gate else
303*0Sstevel@tonic-gate free(in_buf.value);
304*0Sstevel@tonic-gate return (FALSE);
305*0Sstevel@tonic-gate }
306*0Sstevel@tonic-gate
307*0Sstevel@tonic-gate /*ARGSUSED*/
308*0Sstevel@tonic-gate int
__find_max_data_length(service,context,qop,max_tp_unit_len)309*0Sstevel@tonic-gate __find_max_data_length(service, context, qop, max_tp_unit_len)
310*0Sstevel@tonic-gate rpc_gss_service_t service;
311*0Sstevel@tonic-gate gss_ctx_id_t context;
312*0Sstevel@tonic-gate OM_uint32 qop;
313*0Sstevel@tonic-gate int max_tp_unit_len;
314*0Sstevel@tonic-gate {
315*0Sstevel@tonic-gate int conf;
316*0Sstevel@tonic-gate OM_uint32 maj_stat = GSS_S_COMPLETE, min_stat = 0;
317*0Sstevel@tonic-gate OM_uint32 max_input_size;
318*0Sstevel@tonic-gate int ret_val = 0;
319*0Sstevel@tonic-gate
320*0Sstevel@tonic-gate if (service == rpc_gss_svc_integrity || service == rpc_gss_svc_default)
321*0Sstevel@tonic-gate conf = 0;
322*0Sstevel@tonic-gate else if (service == rpc_gss_svc_privacy)
323*0Sstevel@tonic-gate conf = 1;
324*0Sstevel@tonic-gate else if (service == rpc_gss_svc_none)
325*0Sstevel@tonic-gate return (max_tp_unit_len);
326*0Sstevel@tonic-gate
327*0Sstevel@tonic-gate maj_stat = gss_wrap_size_limit(&min_stat,
328*0Sstevel@tonic-gate context, conf, qop,
329*0Sstevel@tonic-gate max_tp_unit_len, &max_input_size);
330*0Sstevel@tonic-gate
331*0Sstevel@tonic-gate /*
332*0Sstevel@tonic-gate * max_input_size may result in negative value
333*0Sstevel@tonic-gate */
334*0Sstevel@tonic-gate if (maj_stat == GSS_S_COMPLETE) {
335*0Sstevel@tonic-gate if ((int)max_input_size <= 0)
336*0Sstevel@tonic-gate ret_val = 0;
337*0Sstevel@tonic-gate else
338*0Sstevel@tonic-gate ret_val = (int)(max_input_size);
339*0Sstevel@tonic-gate } else {
340*0Sstevel@tonic-gate fprintf(stderr, dgettext(TEXT_DOMAIN,
341*0Sstevel@tonic-gate "gss_wrap_size_limit failed in "
342*0Sstevel@tonic-gate "__find_max_data_length\n"));
343*0Sstevel@tonic-gate }
344*0Sstevel@tonic-gate
345*0Sstevel@tonic-gate return (ret_val);
346*0Sstevel@tonic-gate }
347