1a9148abdSDoug Rabson /*
2a9148abdSDoug Rabson rpcsec_gss_prot.c
3a9148abdSDoug Rabson
4*51369649SPedro F. Giffuni SPDX-License-Identifier: BSD-3-Clause
5*51369649SPedro F. Giffuni
6a9148abdSDoug Rabson Copyright (c) 2000 The Regents of the University of Michigan.
7a9148abdSDoug Rabson All rights reserved.
8a9148abdSDoug Rabson
9a9148abdSDoug Rabson Copyright (c) 2000 Dug Song <dugsong@UMICH.EDU>.
10a9148abdSDoug Rabson All rights reserved, all wrongs reversed.
11a9148abdSDoug Rabson
12a9148abdSDoug Rabson Redistribution and use in source and binary forms, with or without
13a9148abdSDoug Rabson modification, are permitted provided that the following conditions
14a9148abdSDoug Rabson are met:
15a9148abdSDoug Rabson
16a9148abdSDoug Rabson 1. Redistributions of source code must retain the above copyright
17a9148abdSDoug Rabson notice, this list of conditions and the following disclaimer.
18a9148abdSDoug Rabson 2. Redistributions in binary form must reproduce the above copyright
19a9148abdSDoug Rabson notice, this list of conditions and the following disclaimer in the
20a9148abdSDoug Rabson documentation and/or other materials provided with the distribution.
21a9148abdSDoug Rabson 3. Neither the name of the University nor the names of its
22a9148abdSDoug Rabson contributors may be used to endorse or promote products derived
23a9148abdSDoug Rabson from this software without specific prior written permission.
24a9148abdSDoug Rabson
25a9148abdSDoug Rabson THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26a9148abdSDoug Rabson WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27a9148abdSDoug Rabson MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28a9148abdSDoug Rabson DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29a9148abdSDoug Rabson FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30a9148abdSDoug Rabson CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31a9148abdSDoug Rabson SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32a9148abdSDoug Rabson BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33a9148abdSDoug Rabson LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34a9148abdSDoug Rabson NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35a9148abdSDoug Rabson SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36a9148abdSDoug Rabson
37a9148abdSDoug Rabson $Id: authgss_prot.c,v 1.18 2000/09/01 04:14:03 dugsong Exp $
38a9148abdSDoug Rabson */
39a9148abdSDoug Rabson
40a9148abdSDoug Rabson #include <sys/param.h>
41a9148abdSDoug Rabson #include <sys/systm.h>
42a9148abdSDoug Rabson #include <sys/kobj.h>
43a9148abdSDoug Rabson #include <sys/lock.h>
44a9148abdSDoug Rabson #include <sys/malloc.h>
45a9148abdSDoug Rabson #include <sys/mbuf.h>
46a9148abdSDoug Rabson #include <sys/mutex.h>
47a9148abdSDoug Rabson
48a9148abdSDoug Rabson #include <rpc/rpc.h>
49a9148abdSDoug Rabson #include <rpc/rpcsec_gss.h>
50a9148abdSDoug Rabson
51a9148abdSDoug Rabson #include "rpcsec_gss_int.h"
52a9148abdSDoug Rabson
53a9148abdSDoug Rabson #define MAX_GSS_SIZE 10240 /* XXX */
54a9148abdSDoug Rabson
55a9148abdSDoug Rabson #if 0 /* use the one from kgssapi */
56a9148abdSDoug Rabson bool_t
57a9148abdSDoug Rabson xdr_gss_buffer_desc(XDR *xdrs, gss_buffer_desc *p)
58a9148abdSDoug Rabson {
59a9148abdSDoug Rabson char *val;
60a9148abdSDoug Rabson u_int len;
61a9148abdSDoug Rabson bool_t ret;
62a9148abdSDoug Rabson
63a9148abdSDoug Rabson val = p->value;
64a9148abdSDoug Rabson len = p->length;
65a9148abdSDoug Rabson ret = xdr_bytes(xdrs, &val, &len, MAX_GSS_SIZE);
66a9148abdSDoug Rabson p->value = val;
67a9148abdSDoug Rabson p->length = len;
68a9148abdSDoug Rabson
69a9148abdSDoug Rabson return (ret);
70a9148abdSDoug Rabson }
71a9148abdSDoug Rabson #endif
72a9148abdSDoug Rabson
73a9148abdSDoug Rabson bool_t
xdr_rpc_gss_cred(XDR * xdrs,struct rpc_gss_cred * p)74a9148abdSDoug Rabson xdr_rpc_gss_cred(XDR *xdrs, struct rpc_gss_cred *p)
75a9148abdSDoug Rabson {
76a9148abdSDoug Rabson enum_t proc, svc;
77a9148abdSDoug Rabson bool_t ret;
78a9148abdSDoug Rabson
79a9148abdSDoug Rabson proc = p->gc_proc;
80a9148abdSDoug Rabson svc = p->gc_svc;
81a9148abdSDoug Rabson ret = (xdr_u_int(xdrs, &p->gc_version) &&
82a9148abdSDoug Rabson xdr_enum(xdrs, &proc) &&
83a9148abdSDoug Rabson xdr_u_int(xdrs, &p->gc_seq) &&
84a9148abdSDoug Rabson xdr_enum(xdrs, &svc) &&
85a9148abdSDoug Rabson xdr_gss_buffer_desc(xdrs, &p->gc_handle));
86a9148abdSDoug Rabson p->gc_proc = proc;
87a9148abdSDoug Rabson p->gc_svc = svc;
88a9148abdSDoug Rabson
89a9148abdSDoug Rabson return (ret);
90a9148abdSDoug Rabson }
91a9148abdSDoug Rabson
92a9148abdSDoug Rabson bool_t
xdr_rpc_gss_init_res(XDR * xdrs,struct rpc_gss_init_res * p)93a9148abdSDoug Rabson xdr_rpc_gss_init_res(XDR *xdrs, struct rpc_gss_init_res *p)
94a9148abdSDoug Rabson {
95a9148abdSDoug Rabson
96a9148abdSDoug Rabson return (xdr_gss_buffer_desc(xdrs, &p->gr_handle) &&
97a9148abdSDoug Rabson xdr_u_int(xdrs, &p->gr_major) &&
98a9148abdSDoug Rabson xdr_u_int(xdrs, &p->gr_minor) &&
99a9148abdSDoug Rabson xdr_u_int(xdrs, &p->gr_win) &&
100a9148abdSDoug Rabson xdr_gss_buffer_desc(xdrs, &p->gr_token));
101a9148abdSDoug Rabson }
102a9148abdSDoug Rabson
103a9148abdSDoug Rabson static void
put_uint32(struct mbuf ** mp,uint32_t v)104a9148abdSDoug Rabson put_uint32(struct mbuf **mp, uint32_t v)
105a9148abdSDoug Rabson {
106a9148abdSDoug Rabson struct mbuf *m = *mp;
107a9148abdSDoug Rabson uint32_t n;
108a9148abdSDoug Rabson
109eb1b1807SGleb Smirnoff M_PREPEND(m, sizeof(uint32_t), M_WAITOK);
110a9148abdSDoug Rabson n = htonl(v);
111a9148abdSDoug Rabson bcopy(&n, mtod(m, uint32_t *), sizeof(uint32_t));
112a9148abdSDoug Rabson *mp = m;
113a9148abdSDoug Rabson }
114a9148abdSDoug Rabson
115a9148abdSDoug Rabson bool_t
xdr_rpc_gss_wrap_data(struct mbuf ** argsp,gss_ctx_id_t ctx,gss_qop_t qop,rpc_gss_service_t svc,u_int seq)116a9148abdSDoug Rabson xdr_rpc_gss_wrap_data(struct mbuf **argsp,
117a9148abdSDoug Rabson gss_ctx_id_t ctx, gss_qop_t qop,
118a9148abdSDoug Rabson rpc_gss_service_t svc, u_int seq)
119a9148abdSDoug Rabson {
120a9148abdSDoug Rabson struct mbuf *args, *mic;
121a9148abdSDoug Rabson OM_uint32 maj_stat, min_stat;
122a9148abdSDoug Rabson int conf_state;
123a9148abdSDoug Rabson u_int len;
124a9148abdSDoug Rabson static char zpad[4];
125a9148abdSDoug Rabson
126a9148abdSDoug Rabson args = *argsp;
127a9148abdSDoug Rabson
128a9148abdSDoug Rabson /*
129a9148abdSDoug Rabson * Prepend the sequence number before calling gss_get_mic or gss_wrap.
130a9148abdSDoug Rabson */
131a9148abdSDoug Rabson put_uint32(&args, seq);
132a9148abdSDoug Rabson len = m_length(args, NULL);
133a9148abdSDoug Rabson
134a9148abdSDoug Rabson if (svc == rpc_gss_svc_integrity) {
135a9148abdSDoug Rabson /* Checksum rpc_gss_data_t. */
136a9148abdSDoug Rabson maj_stat = gss_get_mic_mbuf(&min_stat, ctx, qop, args, &mic);
137a9148abdSDoug Rabson if (maj_stat != GSS_S_COMPLETE) {
138a9148abdSDoug Rabson rpc_gss_log_debug("gss_get_mic failed");
139a9148abdSDoug Rabson m_freem(args);
140a9148abdSDoug Rabson return (FALSE);
141a9148abdSDoug Rabson }
142a9148abdSDoug Rabson
143a9148abdSDoug Rabson /*
144a9148abdSDoug Rabson * Marshal databody_integ. Note that since args is
145a9148abdSDoug Rabson * already RPC encoded, there will be no padding.
146a9148abdSDoug Rabson */
147a9148abdSDoug Rabson put_uint32(&args, len);
148a9148abdSDoug Rabson
149a9148abdSDoug Rabson /*
150a9148abdSDoug Rabson * Marshal checksum. This is likely to need padding.
151a9148abdSDoug Rabson */
152a9148abdSDoug Rabson len = m_length(mic, NULL);
153a9148abdSDoug Rabson put_uint32(&mic, len);
154a9148abdSDoug Rabson if (len != RNDUP(len)) {
155a9148abdSDoug Rabson m_append(mic, RNDUP(len) - len, zpad);
156a9148abdSDoug Rabson }
157a9148abdSDoug Rabson
158a9148abdSDoug Rabson /*
159a9148abdSDoug Rabson * Concatenate databody_integ with checksum.
160a9148abdSDoug Rabson */
161a9148abdSDoug Rabson m_cat(args, mic);
162a9148abdSDoug Rabson } else if (svc == rpc_gss_svc_privacy) {
163a9148abdSDoug Rabson /* Encrypt rpc_gss_data_t. */
164a9148abdSDoug Rabson maj_stat = gss_wrap_mbuf(&min_stat, ctx, TRUE, qop,
165a9148abdSDoug Rabson &args, &conf_state);
166a9148abdSDoug Rabson if (maj_stat != GSS_S_COMPLETE) {
167a9148abdSDoug Rabson rpc_gss_log_status("gss_wrap", NULL,
168a9148abdSDoug Rabson maj_stat, min_stat);
169a9148abdSDoug Rabson return (FALSE);
170a9148abdSDoug Rabson }
171a9148abdSDoug Rabson
172a9148abdSDoug Rabson /*
173a9148abdSDoug Rabson * Marshal databody_priv and deal with RPC padding.
174a9148abdSDoug Rabson */
175a9148abdSDoug Rabson len = m_length(args, NULL);
176a9148abdSDoug Rabson put_uint32(&args, len);
177a9148abdSDoug Rabson if (len != RNDUP(len)) {
178a9148abdSDoug Rabson m_append(args, RNDUP(len) - len, zpad);
179a9148abdSDoug Rabson }
180a9148abdSDoug Rabson }
181a9148abdSDoug Rabson *argsp = args;
182a9148abdSDoug Rabson return (TRUE);
183a9148abdSDoug Rabson }
184a9148abdSDoug Rabson
185a9148abdSDoug Rabson static uint32_t
get_uint32(struct mbuf ** mp)186a9148abdSDoug Rabson get_uint32(struct mbuf **mp)
187a9148abdSDoug Rabson {
188a9148abdSDoug Rabson struct mbuf *m = *mp;
189a9148abdSDoug Rabson uint32_t n;
190a9148abdSDoug Rabson
191a9148abdSDoug Rabson if (m->m_len < sizeof(uint32_t)) {
192a9148abdSDoug Rabson m = m_pullup(m, sizeof(uint32_t));
193a9148abdSDoug Rabson if (!m) {
194a9148abdSDoug Rabson *mp = NULL;
195a9148abdSDoug Rabson return (0);
196a9148abdSDoug Rabson }
197a9148abdSDoug Rabson }
198a9148abdSDoug Rabson bcopy(mtod(m, uint32_t *), &n, sizeof(uint32_t));
199a9148abdSDoug Rabson m_adj(m, sizeof(uint32_t));
200a9148abdSDoug Rabson *mp = m;
201a9148abdSDoug Rabson return (ntohl(n));
202a9148abdSDoug Rabson }
203a9148abdSDoug Rabson
204a9148abdSDoug Rabson static void
m_trim(struct mbuf * m,int len)205a9148abdSDoug Rabson m_trim(struct mbuf *m, int len)
206a9148abdSDoug Rabson {
207a9148abdSDoug Rabson struct mbuf *n;
208a9148abdSDoug Rabson int off;
209a9148abdSDoug Rabson
21030575200SGeorge V. Neville-Neil if (m == NULL)
21130575200SGeorge V. Neville-Neil return;
212a9148abdSDoug Rabson n = m_getptr(m, len, &off);
213a9148abdSDoug Rabson if (n) {
214a9148abdSDoug Rabson n->m_len = off;
215a9148abdSDoug Rabson if (n->m_next) {
216a9148abdSDoug Rabson m_freem(n->m_next);
217a9148abdSDoug Rabson n->m_next = NULL;
218a9148abdSDoug Rabson }
219a9148abdSDoug Rabson }
220a9148abdSDoug Rabson }
221a9148abdSDoug Rabson
222a9148abdSDoug Rabson bool_t
xdr_rpc_gss_unwrap_data(struct mbuf ** resultsp,gss_ctx_id_t ctx,gss_qop_t qop,rpc_gss_service_t svc,u_int seq)223a9148abdSDoug Rabson xdr_rpc_gss_unwrap_data(struct mbuf **resultsp,
224a9148abdSDoug Rabson gss_ctx_id_t ctx, gss_qop_t qop,
225a9148abdSDoug Rabson rpc_gss_service_t svc, u_int seq)
226a9148abdSDoug Rabson {
227a9148abdSDoug Rabson struct mbuf *results, *message, *mic;
228a9148abdSDoug Rabson uint32_t len, cklen;
229a9148abdSDoug Rabson OM_uint32 maj_stat, min_stat;
230a9148abdSDoug Rabson u_int seq_num, conf_state, qop_state;
231a9148abdSDoug Rabson
232a9148abdSDoug Rabson results = *resultsp;
233a9148abdSDoug Rabson *resultsp = NULL;
234a9148abdSDoug Rabson
235a9148abdSDoug Rabson message = NULL;
236a9148abdSDoug Rabson if (svc == rpc_gss_svc_integrity) {
237a9148abdSDoug Rabson /*
238a9148abdSDoug Rabson * Extract the seq+message part. Remember that there
239a9148abdSDoug Rabson * may be extra RPC padding in the checksum. The
240a9148abdSDoug Rabson * message part is RPC encoded already so no
241a9148abdSDoug Rabson * padding.
242a9148abdSDoug Rabson */
243a9148abdSDoug Rabson len = get_uint32(&results);
244a9148abdSDoug Rabson message = results;
245eb1b1807SGleb Smirnoff results = m_split(results, len, M_WAITOK);
246a9148abdSDoug Rabson if (!results) {
247a9148abdSDoug Rabson m_freem(message);
248a9148abdSDoug Rabson return (FALSE);
249a9148abdSDoug Rabson }
250a9148abdSDoug Rabson
251a9148abdSDoug Rabson /*
252a9148abdSDoug Rabson * Extract the MIC and make it contiguous.
253a9148abdSDoug Rabson */
254a9148abdSDoug Rabson cklen = get_uint32(&results);
25530575200SGeorge V. Neville-Neil if (!results) {
25630575200SGeorge V. Neville-Neil m_freem(message);
25730575200SGeorge V. Neville-Neil return (FALSE);
25830575200SGeorge V. Neville-Neil }
259a9148abdSDoug Rabson KASSERT(cklen <= MHLEN, ("unexpected large GSS-API checksum"));
260a9148abdSDoug Rabson mic = results;
26130575200SGeorge V. Neville-Neil if (cklen > mic->m_len) {
262a9148abdSDoug Rabson mic = m_pullup(mic, cklen);
26330575200SGeorge V. Neville-Neil if (!mic) {
26430575200SGeorge V. Neville-Neil m_freem(message);
26530575200SGeorge V. Neville-Neil return (FALSE);
26630575200SGeorge V. Neville-Neil }
26730575200SGeorge V. Neville-Neil }
268a9148abdSDoug Rabson if (cklen != RNDUP(cklen))
269a9148abdSDoug Rabson m_trim(mic, cklen);
270a9148abdSDoug Rabson
271a9148abdSDoug Rabson /* Verify checksum and QOP. */
272a9148abdSDoug Rabson maj_stat = gss_verify_mic_mbuf(&min_stat, ctx,
273a9148abdSDoug Rabson message, mic, &qop_state);
274a9148abdSDoug Rabson m_freem(mic);
275a9148abdSDoug Rabson
276a9148abdSDoug Rabson if (maj_stat != GSS_S_COMPLETE || qop_state != qop) {
277a9148abdSDoug Rabson m_freem(message);
278a9148abdSDoug Rabson rpc_gss_log_status("gss_verify_mic", NULL,
279a9148abdSDoug Rabson maj_stat, min_stat);
280a9148abdSDoug Rabson return (FALSE);
281a9148abdSDoug Rabson }
282a9148abdSDoug Rabson } else if (svc == rpc_gss_svc_privacy) {
283a9148abdSDoug Rabson /* Decode databody_priv. */
284a9148abdSDoug Rabson len = get_uint32(&results);
28530575200SGeorge V. Neville-Neil if (!results)
28630575200SGeorge V. Neville-Neil return (FALSE);
287a9148abdSDoug Rabson
288a9148abdSDoug Rabson /* Decrypt databody. */
289a9148abdSDoug Rabson message = results;
290a9148abdSDoug Rabson if (len != RNDUP(len))
291a9148abdSDoug Rabson m_trim(message, len);
292a9148abdSDoug Rabson maj_stat = gss_unwrap_mbuf(&min_stat, ctx, &message,
293a9148abdSDoug Rabson &conf_state, &qop_state);
294a9148abdSDoug Rabson
295a9148abdSDoug Rabson /* Verify encryption and QOP. */
296a9148abdSDoug Rabson if (maj_stat != GSS_S_COMPLETE) {
297a9148abdSDoug Rabson rpc_gss_log_status("gss_unwrap", NULL,
298a9148abdSDoug Rabson maj_stat, min_stat);
299a9148abdSDoug Rabson return (FALSE);
300a9148abdSDoug Rabson }
301a9148abdSDoug Rabson if (qop_state != qop || conf_state != TRUE) {
302a9148abdSDoug Rabson m_freem(results);
303a9148abdSDoug Rabson return (FALSE);
304a9148abdSDoug Rabson }
305a9148abdSDoug Rabson }
306a9148abdSDoug Rabson
307a9148abdSDoug Rabson /* Decode rpc_gss_data_t (sequence number + arguments). */
308a9148abdSDoug Rabson seq_num = get_uint32(&message);
30930575200SGeorge V. Neville-Neil if (!message)
31030575200SGeorge V. Neville-Neil return (FALSE);
311a9148abdSDoug Rabson
312a9148abdSDoug Rabson /* Verify sequence number. */
313a9148abdSDoug Rabson if (seq_num != seq) {
314a9148abdSDoug Rabson rpc_gss_log_debug("wrong sequence number in databody");
315a9148abdSDoug Rabson m_freem(message);
316a9148abdSDoug Rabson return (FALSE);
317a9148abdSDoug Rabson }
318a9148abdSDoug Rabson
319a9148abdSDoug Rabson *resultsp = message;
320a9148abdSDoug Rabson return (TRUE);
321a9148abdSDoug Rabson }
322a9148abdSDoug Rabson
323a9148abdSDoug Rabson #ifdef DEBUG
3243d26cd60SBrooks Davis #include <machine/stdarg.h>
325a9148abdSDoug Rabson
326a9148abdSDoug Rabson void
rpc_gss_log_debug(const char * fmt,...)327a9148abdSDoug Rabson rpc_gss_log_debug(const char *fmt, ...)
328a9148abdSDoug Rabson {
329a9148abdSDoug Rabson va_list ap;
330a9148abdSDoug Rabson
331a9148abdSDoug Rabson va_start(ap, fmt);
3323d26cd60SBrooks Davis printf("rpcsec_gss: ");
3333d26cd60SBrooks Davis vprintf(fmt, ap);
3343d26cd60SBrooks Davis printf("\n");
335a9148abdSDoug Rabson va_end(ap);
336a9148abdSDoug Rabson }
337a9148abdSDoug Rabson
338a9148abdSDoug Rabson void
rpc_gss_log_status(const char * m,gss_OID mech,OM_uint32 maj_stat,OM_uint32 min_stat)339a9148abdSDoug Rabson rpc_gss_log_status(const char *m, gss_OID mech, OM_uint32 maj_stat, OM_uint32 min_stat)
340a9148abdSDoug Rabson {
341a9148abdSDoug Rabson OM_uint32 min;
342a9148abdSDoug Rabson gss_buffer_desc msg;
343a9148abdSDoug Rabson int msg_ctx = 0;
344a9148abdSDoug Rabson
3453d26cd60SBrooks Davis printf("rpcsec_gss: %s: ", m);
346a9148abdSDoug Rabson
347a9148abdSDoug Rabson gss_display_status(&min, maj_stat, GSS_C_GSS_CODE, GSS_C_NULL_OID,
348a9148abdSDoug Rabson &msg_ctx, &msg);
349a9148abdSDoug Rabson printf("%s - ", (char *)msg.value);
350a9148abdSDoug Rabson gss_release_buffer(&min, &msg);
351a9148abdSDoug Rabson
352a9148abdSDoug Rabson gss_display_status(&min, min_stat, GSS_C_MECH_CODE, mech,
353a9148abdSDoug Rabson &msg_ctx, &msg);
354a9148abdSDoug Rabson printf("%s\n", (char *)msg.value);
355a9148abdSDoug Rabson gss_release_buffer(&min, &msg);
356a9148abdSDoug Rabson }
357a9148abdSDoug Rabson
358a9148abdSDoug Rabson #else
359a9148abdSDoug Rabson
360a9148abdSDoug Rabson void
rpc_gss_log_debug(__unused const char * fmt,...)361a9148abdSDoug Rabson rpc_gss_log_debug(__unused const char *fmt, ...)
362a9148abdSDoug Rabson {
363a9148abdSDoug Rabson }
364a9148abdSDoug Rabson
365a9148abdSDoug Rabson void
rpc_gss_log_status(__unused const char * m,__unused gss_OID mech,__unused OM_uint32 maj_stat,__unused OM_uint32 min_stat)366a9148abdSDoug Rabson rpc_gss_log_status(__unused const char *m, __unused gss_OID mech,
367a9148abdSDoug Rabson __unused OM_uint32 maj_stat, __unused OM_uint32 min_stat)
368a9148abdSDoug Rabson {
369a9148abdSDoug Rabson }
370a9148abdSDoug Rabson
371a9148abdSDoug Rabson #endif
372a9148abdSDoug Rabson
373a9148abdSDoug Rabson
374