4 Copyright (c) 2000 The Regents of the University of Michigan.
7 Copyright (c) 2000 Dug Song <dugsong@UMICH.EDU>.
8 All rights reserved, all wrongs reversed.
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions
14 1. Redistributions of source code must retain the above copyright
15 notice, this list of conditions and the following disclaimer.
16 2. Redistributions in binary form must reproduce the above copyright
17 notice, this list of conditions and the following disclaimer in the
18 documentation and/or other materials provided with the distribution.
19 3. Neither the name of the University nor the names of its
20 contributors may be used to endorse or promote products derived
21 from this software without specific prior written permission.
23 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 $Id: authgss_prot.c,v 1.18 2000/09/01 04:14:03 dugsong Exp $
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
45 #include <sys/malloc.h>
47 #include <sys/mutex.h>
50 #include <rpc/rpcsec_gss.h>
52 #include "rpcsec_gss_int.h"
54 #define MAX_GSS_SIZE 10240 /* XXX */
56 #if 0 /* use the one from kgssapi */
58 xdr_gss_buffer_desc(XDR *xdrs, gss_buffer_desc *p)
66 ret = xdr_bytes(xdrs, &val, &len, MAX_GSS_SIZE);
75 xdr_rpc_gss_cred(XDR *xdrs, struct rpc_gss_cred *p)
82 ret = (xdr_u_int(xdrs, &p->gc_version) &&
83 xdr_enum(xdrs, &proc) &&
84 xdr_u_int(xdrs, &p->gc_seq) &&
85 xdr_enum(xdrs, &svc) &&
86 xdr_gss_buffer_desc(xdrs, &p->gc_handle));
94 xdr_rpc_gss_init_res(XDR *xdrs, struct rpc_gss_init_res *p)
97 return (xdr_gss_buffer_desc(xdrs, &p->gr_handle) &&
98 xdr_u_int(xdrs, &p->gr_major) &&
99 xdr_u_int(xdrs, &p->gr_minor) &&
100 xdr_u_int(xdrs, &p->gr_win) &&
101 xdr_gss_buffer_desc(xdrs, &p->gr_token));
105 put_uint32(struct mbuf **mp, uint32_t v)
107 struct mbuf *m = *mp;
110 M_PREPEND(m, sizeof(uint32_t), M_WAIT);
112 bcopy(&n, mtod(m, uint32_t *), sizeof(uint32_t));
117 xdr_rpc_gss_wrap_data(struct mbuf **argsp,
118 gss_ctx_id_t ctx, gss_qop_t qop,
119 rpc_gss_service_t svc, u_int seq)
121 struct mbuf *args, *mic;
122 OM_uint32 maj_stat, min_stat;
130 * Prepend the sequence number before calling gss_get_mic or gss_wrap.
132 put_uint32(&args, seq);
133 len = m_length(args, NULL);
135 if (svc == rpc_gss_svc_integrity) {
136 /* Checksum rpc_gss_data_t. */
137 maj_stat = gss_get_mic_mbuf(&min_stat, ctx, qop, args, &mic);
138 if (maj_stat != GSS_S_COMPLETE) {
139 rpc_gss_log_debug("gss_get_mic failed");
145 * Marshal databody_integ. Note that since args is
146 * already RPC encoded, there will be no padding.
148 put_uint32(&args, len);
151 * Marshal checksum. This is likely to need padding.
153 len = m_length(mic, NULL);
154 put_uint32(&mic, len);
155 if (len != RNDUP(len)) {
156 m_append(mic, RNDUP(len) - len, zpad);
160 * Concatenate databody_integ with checksum.
163 } else if (svc == rpc_gss_svc_privacy) {
164 /* Encrypt rpc_gss_data_t. */
165 maj_stat = gss_wrap_mbuf(&min_stat, ctx, TRUE, qop,
167 if (maj_stat != GSS_S_COMPLETE) {
168 rpc_gss_log_status("gss_wrap", NULL,
174 * Marshal databody_priv and deal with RPC padding.
176 len = m_length(args, NULL);
177 put_uint32(&args, len);
178 if (len != RNDUP(len)) {
179 m_append(args, RNDUP(len) - len, zpad);
187 get_uint32(struct mbuf **mp)
189 struct mbuf *m = *mp;
192 if (m->m_len < sizeof(uint32_t)) {
193 m = m_pullup(m, sizeof(uint32_t));
199 bcopy(mtod(m, uint32_t *), &n, sizeof(uint32_t));
200 m_adj(m, sizeof(uint32_t));
206 m_trim(struct mbuf *m, int len)
213 n = m_getptr(m, len, &off);
224 xdr_rpc_gss_unwrap_data(struct mbuf **resultsp,
225 gss_ctx_id_t ctx, gss_qop_t qop,
226 rpc_gss_service_t svc, u_int seq)
228 struct mbuf *results, *message, *mic;
230 OM_uint32 maj_stat, min_stat;
231 u_int seq_num, conf_state, qop_state;
237 if (svc == rpc_gss_svc_integrity) {
239 * Extract the seq+message part. Remember that there
240 * may be extra RPC padding in the checksum. The
241 * message part is RPC encoded already so no
244 len = get_uint32(&results);
246 results = m_split(results, len, M_WAIT);
253 * Extract the MIC and make it contiguous.
255 cklen = get_uint32(&results);
260 KASSERT(cklen <= MHLEN, ("unexpected large GSS-API checksum"));
262 if (cklen > mic->m_len) {
263 mic = m_pullup(mic, cklen);
269 if (cklen != RNDUP(cklen))
272 /* Verify checksum and QOP. */
273 maj_stat = gss_verify_mic_mbuf(&min_stat, ctx,
274 message, mic, &qop_state);
277 if (maj_stat != GSS_S_COMPLETE || qop_state != qop) {
279 rpc_gss_log_status("gss_verify_mic", NULL,
283 } else if (svc == rpc_gss_svc_privacy) {
284 /* Decode databody_priv. */
285 len = get_uint32(&results);
289 /* Decrypt databody. */
291 if (len != RNDUP(len))
292 m_trim(message, len);
293 maj_stat = gss_unwrap_mbuf(&min_stat, ctx, &message,
294 &conf_state, &qop_state);
296 /* Verify encryption and QOP. */
297 if (maj_stat != GSS_S_COMPLETE) {
298 rpc_gss_log_status("gss_unwrap", NULL,
302 if (qop_state != qop || conf_state != TRUE) {
308 /* Decode rpc_gss_data_t (sequence number + arguments). */
309 seq_num = get_uint32(&message);
313 /* Verify sequence number. */
314 if (seq_num != seq) {
315 rpc_gss_log_debug("wrong sequence number in databody");
325 #include <machine/stdarg.h>
328 rpc_gss_log_debug(const char *fmt, ...)
333 printf("rpcsec_gss: ");
340 rpc_gss_log_status(const char *m, gss_OID mech, OM_uint32 maj_stat, OM_uint32 min_stat)
346 printf("rpcsec_gss: %s: ", m);
348 gss_display_status(&min, maj_stat, GSS_C_GSS_CODE, GSS_C_NULL_OID,
350 printf("%s - ", (char *)msg.value);
351 gss_release_buffer(&min, &msg);
353 gss_display_status(&min, min_stat, GSS_C_MECH_CODE, mech,
355 printf("%s\n", (char *)msg.value);
356 gss_release_buffer(&min, &msg);
362 rpc_gss_log_debug(__unused const char *fmt, ...)
367 rpc_gss_log_status(__unused const char *m, __unused gss_OID mech,
368 __unused OM_uint32 maj_stat, __unused OM_uint32 min_stat)