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