2 * Copyright (c) 2007-2010 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * These functions implement RPCSEC_GSS security for the NFS client and server.
31 * The code is specific to the use of Kerberos v5 and the use of DES MAC MD5
32 * protection as described in Internet RFC 2203 and 2623.
34 * In contrast to the original AUTH_SYS authentication, RPCSEC_GSS is stateful.
35 * It requires the client and server negotiate a secure connection as part of a
36 * security context. The context state is maintained in client and server structures.
37 * On the client side, each user of an NFS mount is assigned their own context,
38 * identified by UID, on their first use of the mount, and it persists until the
39 * unmount or until the context is renewed. Each user context has a corresponding
40 * server context which the server maintains until the client destroys it, or
41 * until the context expires.
43 * The client and server contexts are set up dynamically. When a user attempts
44 * to send an NFS request, if there is no context for the user, then one is
45 * set up via an exchange of NFS null procedure calls as described in RFC 2203.
46 * During this exchange, the client and server pass a security token that is
47 * forwarded via Mach upcall to the gssd, which invokes the GSS-API to authenticate
48 * the user to the server (and vice-versa). The client and server also receive
49 * a unique session key that can be used to digitally sign the credentials and
50 * verifier or optionally to provide data integrity and/or privacy.
52 * Once the context is complete, the client and server enter a normal data
53 * exchange phase - beginning with the NFS request that prompted the context
54 * creation. During this phase, the client's RPC header contains an RPCSEC_GSS
55 * credential and verifier, and the server returns a verifier as well.
56 * For simple authentication, the verifier contains a signed checksum of the
57 * RPC header, including the credential. The server's verifier has a signed
58 * checksum of the current sequence number.
60 * Each client call contains a sequence number that nominally increases by one
61 * on each request. The sequence number is intended to prevent replay attacks.
62 * Since the protocol can be used over UDP, there is some allowance for
63 * out-of-sequence requests, so the server checks whether the sequence numbers
64 * are within a sequence "window". If a sequence number is outside the lower
65 * bound of the window, the server silently drops the request. This has some
66 * implications for retransmission. If a request needs to be retransmitted, the
67 * client must bump the sequence number even if the request XID is unchanged.
69 * When the NFS mount is unmounted, the client sends a "destroy" credential
70 * to delete the server's context for each user of the mount. Since it's
71 * possible for the client to crash or disconnect without sending the destroy
72 * message, the server has a thread that reaps contexts that have been idle
77 #include <sys/param.h>
78 #include <sys/systm.h>
80 #include <sys/kauth.h>
81 #include <sys/kernel.h>
82 #include <sys/mount_internal.h>
83 #include <sys/vnode.h>
85 #include <sys/malloc.h>
86 #include <sys/kpi_mbuf.h>
88 #include <kern/host.h>
89 #include <libkern/libkern.h>
91 #include <mach/task.h>
92 #include <mach/host_special_ports.h>
93 #include <mach/host_priv.h>
94 #include <mach/thread_act.h>
95 #include <mach/mig_errors.h>
96 #include <mach/vm_map.h>
97 #include <vm/vm_map.h>
98 #include <vm/vm_kern.h>
99 #include <gssd/gssd_mach.h>
101 #include <nfs/rpcv2.h>
102 #include <nfs/nfsproto.h>
104 #include <nfs/nfsnode.h>
105 #include <nfs/nfs_gss.h>
106 #include <nfs/nfsmount.h>
107 #include <nfs/xdr_subs.h>
108 #include <nfs/nfsm_subs.h>
109 #include <nfs/nfs_gss.h>
111 #include "nfs_gss_crypto.h"
113 #define NFS_GSS_MACH_MAX_RETRIES 3
118 MD5_DESCBC_CTX m_ctx
;
119 HMAC_SHA1_DES3KD_CTX h_ctx
;
123 #define MAX_DIGEST SHA_DIGEST_LENGTH
124 #ifdef NFS_KERNEL_DEBUG
125 #define HASHLEN(ki) (((ki)->hash_len > MAX_DIGEST) ? \
126 (panic("nfs_gss.c:%d ki->hash_len is invalid = %d\n", __LINE__, (ki)->hash_len), MAX_DIGEST) : (ki)->hash_len)
128 #define HASHLEN(ki) (((ki)->hash_len > MAX_DIGEST) ? \
129 (printf("nfs_gss.c:%d ki->hash_len is invalid = %d\n", __LINE__, (ki)->hash_len), MAX_DIGEST) : (ki)->hash_len)
133 u_long nfs_gss_svc_ctx_hash
;
134 struct nfs_gss_svc_ctx_hashhead
*nfs_gss_svc_ctx_hashtbl
;
135 lck_mtx_t
*nfs_gss_svc_ctx_mutex
;
136 lck_grp_t
*nfs_gss_svc_grp
;
137 uint32_t nfsrv_gss_context_ttl
= GSS_CTX_EXPIRE
;
138 #define GSS_SVC_CTX_TTL ((uint64_t)max(2*GSS_CTX_PEND, nfsrv_gss_context_ttl) * NSEC_PER_SEC)
139 #endif /* NFSSERVER */
142 lck_grp_t
*nfs_gss_clnt_grp
;
144 #endif /* NFSCLIENT */
147 * These octet strings are used to encode/decode ASN.1 tokens
148 * in the RPCSEC_GSS verifiers.
150 static u_char krb5_tokhead
[] __attribute__((unused
)) = { 0x60, 0x23 };
151 u_char krb5_mech
[11] = { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x01, 0x02, 0x02 };
152 static u_char krb5_mic
[] = { 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff };
153 static u_char krb5_mic3
[] = { 0x01, 0x01, 0x04, 0x00, 0xff, 0xff, 0xff, 0xff };
154 static u_char krb5_wrap
[] = { 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff };
155 static u_char krb5_wrap3
[] = { 0x02, 0x01, 0x04, 0x00, 0x02, 0x00, 0xff, 0xff };
156 static u_char iv0
[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // DES MAC Initialization Vector
158 #define ALG_MIC(ki) (((ki)->type == NFS_GSS_1DES) ? krb5_mic : krb5_mic3)
159 #define ALG_WRAP(ki) (((ki)->type == NFS_GSS_1DES) ? krb5_wrap : krb5_wrap3)
162 * The size of the Kerberos v5 ASN.1 token
165 * Note that the second octet of the krb5_tokhead (0x23) is a
166 * DER-encoded size field that has variable length. If the size
167 * is 128 bytes or greater, then it uses two bytes, three bytes
168 * if 65536 or greater, and so on. Since the MIC tokens are
169 * separate from the data, the size is always the same: 35 bytes (0x23).
170 * However, the wrap token is different. Its size field includes the
171 * size of the token + the encrypted data that follows. So the size
172 * field may be two, three or four bytes.
174 #define KRB5_SZ_TOKHEAD sizeof(krb5_tokhead)
175 #define KRB5_SZ_MECH sizeof(krb5_mech)
176 #define KRB5_SZ_ALG sizeof(krb5_mic) // 8 - same as krb5_wrap
177 #define KRB5_SZ_SEQ 8
178 #define KRB5_SZ_EXTRA 3 // a wrap token may be longer by up to this many octets
179 #define KRB5_SZ_TOKEN_NOSUM (KRB5_SZ_TOKHEAD + KRB5_SZ_MECH + KRB5_SZ_ALG + KRB5_SZ_SEQ)
180 #define KRB5_SZ_TOKEN(cksumlen) ((cksumlen) + KRB5_SZ_TOKEN_NOSUM)
181 #define KRB5_SZ_TOKMAX(cksumlen) (KRB5_SZ_TOKEN(cksumlen) + KRB5_SZ_EXTRA)
184 static int nfs_gss_clnt_ctx_find(struct nfsreq
*);
185 static int nfs_gss_clnt_ctx_failover(struct nfsreq
*);
186 static int nfs_gss_clnt_ctx_init(struct nfsreq
*, struct nfs_gss_clnt_ctx
*);
187 static int nfs_gss_clnt_ctx_init_retry(struct nfsreq
*, struct nfs_gss_clnt_ctx
*);
188 static int nfs_gss_clnt_ctx_callserver(struct nfsreq
*, struct nfs_gss_clnt_ctx
*);
189 static char *nfs_gss_clnt_svcname(struct nfsmount
*);
190 static int nfs_gss_clnt_gssd_upcall(struct nfsreq
*, struct nfs_gss_clnt_ctx
*);
191 static void nfs_gss_clnt_ctx_remove(struct nfsmount
*, struct nfs_gss_clnt_ctx
*);
192 #endif /* NFSCLIENT */
195 static struct nfs_gss_svc_ctx
*nfs_gss_svc_ctx_find(uint32_t);
196 static void nfs_gss_svc_ctx_insert(struct nfs_gss_svc_ctx
*);
197 static void nfs_gss_svc_ctx_timer(void *, void *);
198 static int nfs_gss_svc_gssd_upcall(struct nfs_gss_svc_ctx
*);
199 static int nfs_gss_svc_seqnum_valid(struct nfs_gss_svc_ctx
*, uint32_t);
200 #endif /* NFSSERVER */
202 static void host_release_special_port(mach_port_t
);
203 static mach_port_t
host_copy_special_port(mach_port_t
);
204 static void nfs_gss_mach_alloc_buffer(u_char
*, uint32_t, vm_map_copy_t
*);
205 static int nfs_gss_mach_vmcopyout(vm_map_copy_t
, uint32_t, u_char
*);
206 static int nfs_gss_token_get(gss_key_info
*ki
, u_char
*, u_char
*, int, uint32_t *, u_char
*);
207 static int nfs_gss_token_put(gss_key_info
*ki
, u_char
*, u_char
*, int, int, u_char
*);
208 static int nfs_gss_der_length_size(int);
209 static void nfs_gss_der_length_put(u_char
**, int);
210 static int nfs_gss_der_length_get(u_char
**);
211 static int nfs_gss_mchain_length(mbuf_t
);
212 static int nfs_gss_append_chain(struct nfsm_chain
*, mbuf_t
);
213 static void nfs_gss_nfsm_chain(struct nfsm_chain
*, mbuf_t
);
214 static void nfs_gss_cksum_mchain(gss_key_info
*, mbuf_t
, u_char
*, int, int, u_char
*);
215 static void nfs_gss_cksum_chain(gss_key_info
*, struct nfsm_chain
*, u_char
*, int, int, u_char
*);
216 static void nfs_gss_cksum_rep(gss_key_info
*, uint32_t, u_char
*);
217 static void nfs_gss_encrypt_mchain(gss_key_info
*, mbuf_t
, int, int, int);
218 static void nfs_gss_encrypt_chain(gss_key_info
*, struct nfsm_chain
*, int, int, int);
220 static void gss_digest_Init(GSS_DIGEST_CTX
*, gss_key_info
*);
221 static void gss_digest_Update(GSS_DIGEST_CTX
*, void *, size_t);
222 static void gss_digest_Final(GSS_DIGEST_CTX
*, void *);
223 static void gss_des_crypt(gss_key_info
*, des_cblock
*, des_cblock
*,
224 int32_t, des_cblock
*, des_cblock
*, int, int);
225 static int gss_key_init(gss_key_info
*, uint32_t);
228 thread_call_t nfs_gss_svc_ctx_timer_call
;
229 int nfs_gss_timer_on
= 0;
230 uint32_t nfs_gss_ctx_count
= 0;
231 const uint32_t nfs_gss_ctx_max
= GSS_SVC_MAXCONTEXTS
;
232 #endif /* NFSSERVER */
235 * Initialization when NFS starts
241 nfs_gss_clnt_grp
= lck_grp_alloc_init("rpcsec_gss_clnt", LCK_GRP_ATTR_NULL
);
242 #endif /* NFSCLIENT */
245 nfs_gss_svc_grp
= lck_grp_alloc_init("rpcsec_gss_svc", LCK_GRP_ATTR_NULL
);
247 nfs_gss_svc_ctx_hashtbl
= hashinit(SVC_CTX_HASHSZ
, M_TEMP
, &nfs_gss_svc_ctx_hash
);
248 nfs_gss_svc_ctx_mutex
= lck_mtx_alloc_init(nfs_gss_svc_grp
, LCK_ATTR_NULL
);
250 nfs_gss_svc_ctx_timer_call
= thread_call_allocate(nfs_gss_svc_ctx_timer
, NULL
);
251 #endif /* NFSSERVER */
257 * Is it OK to fall back to using AUTH_SYS?
260 nfs_gss_sysok(struct nfsreq
*req
)
262 struct nfsmount
*nmp
= req
->r_nmp
;
265 if (req
->r_wrongsec
) /* Not OK if we're trying to handle a wrongsec error */
267 if (!nmp
->nm_sec
.count
) /* assume it's OK if we don't have a set of flavors */
269 for (i
=0; i
< nmp
->nm_sec
.count
; i
++)
270 if (nmp
->nm_sec
.flavors
[i
] == RPCAUTH_SYS
)
276 * Find the context for a particular user.
278 * If the context doesn't already exist
279 * then create a new context for this user.
281 * Note that the code allows superuser (uid == 0)
282 * to adopt the context of another user.
285 nfs_gss_clnt_ctx_find(struct nfsreq
*req
)
287 struct nfsmount
*nmp
= req
->r_nmp
;
288 struct nfs_gss_clnt_ctx
*cp
;
289 uid_t uid
= kauth_cred_getuid(req
->r_cred
);
292 lck_mtx_lock(&nmp
->nm_lock
);
293 TAILQ_FOREACH(cp
, &nmp
->nm_gsscl
, gss_clnt_entries
) {
294 if (cp
->gss_clnt_uid
== uid
) {
295 if (cp
->gss_clnt_flags
& GSS_CTX_INVAL
)
297 nfs_gss_clnt_ctx_ref(req
, cp
);
298 lck_mtx_unlock(&nmp
->nm_lock
);
305 * If superuser is trying to get access, then co-opt
306 * the first valid context in the list.
307 * XXX Ultimately, we need to allow superuser to
308 * go ahead and attempt to set up its own context
309 * in case one is set up for it.
311 TAILQ_FOREACH(cp
, &nmp
->nm_gsscl
, gss_clnt_entries
) {
312 if (!(cp
->gss_clnt_flags
& GSS_CTX_INVAL
)) {
313 nfs_gss_clnt_ctx_ref(req
, cp
);
314 lck_mtx_unlock(&nmp
->nm_lock
);
321 * Not found - create a new context
325 * If the thread is async, then it cannot get
326 * kerberos creds and set up a proper context.
327 * If no sec= mount option is given, attempt
328 * to failover to sec=sys.
330 if (req
->r_thread
== NULL
) {
331 if (nfs_gss_sysok(req
)) {
332 error
= nfs_gss_clnt_ctx_failover(req
);
334 printf("nfs_gss_clnt_ctx_find: no context for async\n");
335 error
= NFSERR_EAUTH
;
338 lck_mtx_unlock(&nmp
->nm_lock
);
342 MALLOC(cp
, struct nfs_gss_clnt_ctx
*, sizeof(*cp
), M_TEMP
, M_WAITOK
|M_ZERO
);
344 lck_mtx_unlock(&nmp
->nm_lock
);
348 cp
->gss_clnt_uid
= uid
;
349 cp
->gss_clnt_mtx
= lck_mtx_alloc_init(nfs_gss_clnt_grp
, LCK_ATTR_NULL
);
350 cp
->gss_clnt_thread
= current_thread();
351 nfs_gss_clnt_ctx_ref(req
, cp
);
352 TAILQ_INSERT_TAIL(&nmp
->nm_gsscl
, cp
, gss_clnt_entries
);
353 lck_mtx_unlock(&nmp
->nm_lock
);
355 error
= nfs_gss_clnt_ctx_init_retry(req
, cp
); // Initialize new context
357 nfs_gss_clnt_ctx_unref(req
);
360 * If we failed to set up a Kerberos context for this
361 * user and no sec= mount option was given, but the
362 * server indicated that it could support AUTH_SYS, then set
363 * up a dummy context that allows this user to attempt
366 if (error
&& nfs_gss_sysok(req
) &&
367 (error
!= ENXIO
) && (error
!= ETIMEDOUT
)) {
368 lck_mtx_lock(&nmp
->nm_lock
);
369 error
= nfs_gss_clnt_ctx_failover(req
);
370 lck_mtx_unlock(&nmp
->nm_lock
);
377 * Set up a dummy context to allow the use of sec=sys
378 * for this user, if the server allows sec=sys.
379 * The context is valid for GSS_CLNT_SYS_VALID seconds,
380 * so that the user will periodically attempt to fail back
381 * and get a real credential.
383 * Assumes context list (nm_lock) is locked
386 nfs_gss_clnt_ctx_failover(struct nfsreq
*req
)
388 struct nfsmount
*nmp
= req
->r_nmp
;
389 struct nfs_gss_clnt_ctx
*cp
;
390 uid_t uid
= kauth_cred_getuid(req
->r_cred
);
393 MALLOC(cp
, struct nfs_gss_clnt_ctx
*, sizeof(*cp
), M_TEMP
, M_WAITOK
|M_ZERO
);
397 cp
->gss_clnt_service
= RPCSEC_GSS_SVC_SYS
;
398 cp
->gss_clnt_uid
= uid
;
399 cp
->gss_clnt_mtx
= lck_mtx_alloc_init(nfs_gss_clnt_grp
, LCK_ATTR_NULL
);
401 cp
->gss_clnt_ctime
= now
.tv_sec
; // time stamp
402 nfs_gss_clnt_ctx_ref(req
, cp
);
403 TAILQ_INSERT_TAIL(&nmp
->nm_gsscl
, cp
, gss_clnt_entries
);
409 * Inserts an RPCSEC_GSS credential into an RPC header.
410 * After the credential is inserted, the code continues
411 * to build the verifier which contains a signed checksum
415 nfs_gss_clnt_cred_put(struct nfsreq
*req
, struct nfsm_chain
*nmc
, mbuf_t args
)
417 struct nfs_gss_clnt_ctx
*cp
;
420 int slpflag
, recordmark
= 0;
421 int start
, len
, offset
= 0;
423 struct nfsm_chain nmc_tmp
;
425 u_char tokbuf
[KRB5_SZ_TOKMAX(MAX_DIGEST
)];
426 u_char cksum
[MAX_DIGEST
];
432 slpflag
|= (NMFLAG(req
->r_nmp
, INTR
) && req
->r_thread
&& !(req
->r_flags
& R_NOINTR
)) ? PCATCH
: 0;
433 recordmark
= (req
->r_nmp
->nm_sotype
== SOCK_STREAM
);
436 if (req
->r_gss_ctx
== NULL
) {
438 * Find the context for this user.
439 * If no context is found, one will
442 error
= nfs_gss_clnt_ctx_find(req
);
449 * If it's a dummy context for a user that's using
450 * a fallback to sec=sys, then just return an error
451 * so rpchead can encode an RPCAUTH_UNIX cred.
453 if (cp
->gss_clnt_service
== RPCSEC_GSS_SVC_SYS
) {
455 * The dummy context is valid for just
456 * GSS_CLNT_SYS_VALID seconds. If the context
457 * is older than this, mark it invalid and try
458 * again to get a real one.
460 lck_mtx_lock(cp
->gss_clnt_mtx
);
462 if (now
.tv_sec
> cp
->gss_clnt_ctime
+ GSS_CLNT_SYS_VALID
) {
463 cp
->gss_clnt_flags
|= GSS_CTX_INVAL
;
464 lck_mtx_unlock(cp
->gss_clnt_mtx
);
465 nfs_gss_clnt_ctx_unref(req
);
468 lck_mtx_unlock(cp
->gss_clnt_mtx
);
473 * If the context thread isn't null, then the context isn't
474 * yet complete and is for the exclusive use of the thread
475 * doing the context setup. Wait until the context thread
478 lck_mtx_lock(cp
->gss_clnt_mtx
);
479 if (cp
->gss_clnt_thread
&& cp
->gss_clnt_thread
!= current_thread()) {
480 cp
->gss_clnt_flags
|= GSS_NEEDCTX
;
481 msleep(cp
, cp
->gss_clnt_mtx
, slpflag
| PDROP
, "ctxwait", NULL
);
483 if ((error
= nfs_sigintr(req
->r_nmp
, req
, req
->r_thread
, 0)))
485 nfs_gss_clnt_ctx_unref(req
);
488 lck_mtx_unlock(cp
->gss_clnt_mtx
);
490 ki
= &cp
->gss_clnt_kinfo
;
491 if (cp
->gss_clnt_flags
& GSS_CTX_COMPLETE
) {
493 * Get a sequence number for this request.
494 * Check whether the oldest request in the window is complete.
495 * If it's still pending, then wait until it's done before
496 * we allocate a new sequence number and allow this request
499 lck_mtx_lock(cp
->gss_clnt_mtx
);
500 while (win_getbit(cp
->gss_clnt_seqbits
,
501 ((cp
->gss_clnt_seqnum
- cp
->gss_clnt_seqwin
) + 1) % cp
->gss_clnt_seqwin
)) {
502 cp
->gss_clnt_flags
|= GSS_NEEDSEQ
;
503 msleep(cp
, cp
->gss_clnt_mtx
, slpflag
| PDROP
, "seqwin", NULL
);
505 if ((error
= nfs_sigintr(req
->r_nmp
, req
, req
->r_thread
, 0))) {
508 lck_mtx_lock(cp
->gss_clnt_mtx
);
509 if (cp
->gss_clnt_flags
& GSS_CTX_INVAL
) {
510 /* Renewed while while we were waiting */
511 lck_mtx_unlock(cp
->gss_clnt_mtx
);
512 nfs_gss_clnt_ctx_unref(req
);
516 seqnum
= ++cp
->gss_clnt_seqnum
;
517 win_setbit(cp
->gss_clnt_seqbits
, seqnum
% cp
->gss_clnt_seqwin
);
518 lck_mtx_unlock(cp
->gss_clnt_mtx
);
520 MALLOC(gsp
, struct gss_seq
*, sizeof(*gsp
), M_TEMP
, M_WAITOK
|M_ZERO
);
523 gsp
->gss_seqnum
= seqnum
;
524 SLIST_INSERT_HEAD(&req
->r_gss_seqlist
, gsp
, gss_seqnext
);
527 /* Insert the credential */
528 nfsm_chain_add_32(error
, nmc
, RPCSEC_GSS
);
529 nfsm_chain_add_32(error
, nmc
, 5 * NFSX_UNSIGNED
+ cp
->gss_clnt_handle_len
);
530 nfsm_chain_add_32(error
, nmc
, RPCSEC_GSS_VERS_1
);
531 nfsm_chain_add_32(error
, nmc
, cp
->gss_clnt_proc
);
532 nfsm_chain_add_32(error
, nmc
, seqnum
);
533 nfsm_chain_add_32(error
, nmc
, cp
->gss_clnt_service
);
534 nfsm_chain_add_32(error
, nmc
, cp
->gss_clnt_handle_len
);
535 if (cp
->gss_clnt_handle_len
> 0) {
536 if (cp
->gss_clnt_handle
== NULL
)
538 nfsm_chain_add_opaque(error
, nmc
, cp
->gss_clnt_handle
, cp
->gss_clnt_handle_len
);
543 * Now add the verifier
545 if (cp
->gss_clnt_proc
== RPCSEC_GSS_INIT
||
546 cp
->gss_clnt_proc
== RPCSEC_GSS_CONTINUE_INIT
) {
548 * If the context is still being created
549 * then use a null verifier.
551 nfsm_chain_add_32(error
, nmc
, RPCAUTH_NULL
); // flavor
552 nfsm_chain_add_32(error
, nmc
, 0); // length
553 nfsm_chain_build_done(error
, nmc
);
555 nfs_gss_append_chain(nmc
, args
);
559 offset
= recordmark
? NFSX_UNSIGNED
: 0; // record mark
560 nfsm_chain_build_done(error
, nmc
);
561 nfs_gss_cksum_chain(ki
, nmc
, ALG_MIC(ki
), offset
, 0, cksum
);
563 toklen
= nfs_gss_token_put(ki
, ALG_MIC(ki
), tokbuf
, 1, 0, cksum
);
564 nfsm_chain_add_32(error
, nmc
, RPCSEC_GSS
); // flavor
565 nfsm_chain_add_32(error
, nmc
, toklen
); // length
566 nfsm_chain_add_opaque(error
, nmc
, tokbuf
, toklen
);
567 nfsm_chain_build_done(error
, nmc
);
572 * Now we may have to compute integrity or encrypt the call args
573 * per RFC 2203 Section 5.3.2
575 switch (cp
->gss_clnt_service
) {
576 case RPCSEC_GSS_SVC_NONE
:
577 nfs_gss_append_chain(nmc
, args
);
579 case RPCSEC_GSS_SVC_INTEGRITY
:
580 len
= nfs_gss_mchain_length(args
); // Find args length
581 req
->r_gss_arglen
= len
; // Stash the args len
582 len
+= NFSX_UNSIGNED
; // Add seqnum length
583 nfsm_chain_add_32(error
, nmc
, len
); // and insert it
584 start
= nfsm_chain_offset(nmc
);
585 nfsm_chain_add_32(error
, nmc
, seqnum
); // Insert seqnum
586 req
->r_gss_argoff
= nfsm_chain_offset(nmc
); // Offset to args
587 nfsm_chain_build_done(error
, nmc
);
590 nfs_gss_append_chain(nmc
, args
); // Append the args mbufs
592 /* Now compute a checksum over the seqnum + args */
593 nfs_gss_cksum_chain(ki
, nmc
, ALG_MIC(ki
), start
, len
, cksum
);
595 /* Insert it into a token and append to the request */
596 toklen
= nfs_gss_token_put(ki
, ALG_MIC(ki
), tokbuf
, 1, 0, cksum
);
597 nfsm_chain_finish_mbuf(error
, nmc
); // force checksum into new mbuf
598 nfsm_chain_add_32(error
, nmc
, toklen
);
599 nfsm_chain_add_opaque(error
, nmc
, tokbuf
, toklen
);
600 nfsm_chain_build_done(error
, nmc
);
602 case RPCSEC_GSS_SVC_PRIVACY
:
603 /* Prepend a new mbuf with the confounder & sequence number */
604 nfsm_chain_build_alloc_init(error
, &nmc_tmp
, 3 * NFSX_UNSIGNED
);
605 nfsm_chain_add_32(error
, &nmc_tmp
, random()); // confounder bytes 1-4
606 nfsm_chain_add_32(error
, &nmc_tmp
, random()); // confounder bytes 4-8
607 nfsm_chain_add_32(error
, &nmc_tmp
, seqnum
);
608 nfsm_chain_build_done(error
, &nmc_tmp
);
611 nfs_gss_append_chain(&nmc_tmp
, args
); // Append the args mbufs
613 len
= nfs_gss_mchain_length(args
); // Find args length
614 len
+= 3 * NFSX_UNSIGNED
; // add confounder & seqnum
615 req
->r_gss_arglen
= len
; // Stash length
618 * Append a pad trailer - per RFC 1964 section 1.2.2.3
619 * Since XDR data is always 32-bit aligned, it
620 * needs to be padded either by 4 bytes or 8 bytes.
622 nfsm_chain_finish_mbuf(error
, &nmc_tmp
); // force padding into new mbuf
624 nfsm_chain_add_32(error
, &nmc_tmp
, 0x04040404);
625 len
+= NFSX_UNSIGNED
;
627 nfsm_chain_add_32(error
, &nmc_tmp
, 0x08080808);
628 nfsm_chain_add_32(error
, &nmc_tmp
, 0x08080808);
629 len
+= 2 * NFSX_UNSIGNED
;
631 nfsm_chain_build_done(error
, &nmc_tmp
);
633 /* Now compute a checksum over the confounder + seqnum + args */
634 nfs_gss_cksum_chain(ki
, &nmc_tmp
, ALG_WRAP(ki
), 0, len
, cksum
);
636 /* Insert it into a token */
637 toklen
= nfs_gss_token_put(ki
, ALG_WRAP(ki
), tokbuf
, 1, len
, cksum
);
638 nfsm_chain_add_32(error
, nmc
, toklen
+ len
); // token + args length
639 nfsm_chain_add_opaque_nopad(error
, nmc
, tokbuf
, toklen
);
640 req
->r_gss_argoff
= nfsm_chain_offset(nmc
); // Stash offset
641 nfsm_chain_build_done(error
, nmc
);
644 nfs_gss_append_chain(nmc
, nmc_tmp
.nmc_mhead
); // Append the args mbufs
646 /* Finally, encrypt the args */
647 nfs_gss_encrypt_chain(ki
, &nmc_tmp
, 0, len
, DES_ENCRYPT
);
649 /* Add null XDR pad if the ASN.1 token misaligned the data */
650 pad
= nfsm_pad(toklen
+ len
);
652 nfsm_chain_add_opaque_nopad(error
, nmc
, iv0
, pad
);
653 nfsm_chain_build_done(error
, nmc
);
662 * When receiving a reply, the client checks the verifier
663 * returned by the server. Check that the verifier is the
664 * correct type, then extract the sequence number checksum
665 * from the token in the credential and compare it with a
666 * computed checksum of the sequence number in the request
670 nfs_gss_clnt_verf_get(
672 struct nfsm_chain
*nmc
,
675 uint32_t *accepted_statusp
)
677 u_char tokbuf
[KRB5_SZ_TOKMAX(MAX_DIGEST
)];
678 u_char cksum1
[MAX_DIGEST
], cksum2
[MAX_DIGEST
];
680 struct nfs_gss_clnt_ctx
*cp
= req
->r_gss_ctx
;
681 struct nfsm_chain nmc_tmp
;
683 uint32_t reslen
, start
, cksumlen
, toklen
;
685 gss_key_info
*ki
= &cp
->gss_clnt_kinfo
;
687 reslen
= cksumlen
= 0;
688 *accepted_statusp
= 0;
691 return (NFSERR_EAUTH
);
693 * If it's not an RPCSEC_GSS verifier, then it has to
694 * be a null verifier that resulted from either
695 * a CONTINUE_NEEDED reply during context setup or
696 * from the reply to an AUTH_UNIX call from a dummy
697 * context that resulted from a fallback to sec=sys.
699 if (verftype
!= RPCSEC_GSS
) {
700 if (verftype
!= RPCAUTH_NULL
)
701 return (NFSERR_EAUTH
);
702 if (cp
->gss_clnt_flags
& GSS_CTX_COMPLETE
&&
703 cp
->gss_clnt_service
!= RPCSEC_GSS_SVC_SYS
)
704 return (NFSERR_EAUTH
);
706 nfsm_chain_adv(error
, nmc
, nfsm_rndup(verflen
));
707 nfsm_chain_get_32(error
, nmc
, *accepted_statusp
);
712 * If we received an RPCSEC_GSS verifier but the
713 * context isn't yet complete, then it must be
714 * the context complete message from the server.
715 * The verifier will contain an encrypted checksum
716 * of the window but we don't have the session key
717 * yet so we can't decrypt it. Stash the verifier
718 * and check it later in nfs_gss_clnt_ctx_init() when
719 * the context is complete.
721 if (!(cp
->gss_clnt_flags
& GSS_CTX_COMPLETE
)) {
722 MALLOC(cp
->gss_clnt_verf
, u_char
*, verflen
, M_TEMP
, M_WAITOK
|M_ZERO
);
723 if (cp
->gss_clnt_verf
== NULL
)
725 nfsm_chain_get_opaque(error
, nmc
, verflen
, cp
->gss_clnt_verf
);
726 nfsm_chain_get_32(error
, nmc
, *accepted_statusp
);
730 if (verflen
!= KRB5_SZ_TOKEN(ki
->hash_len
))
731 return (NFSERR_EAUTH
);
734 * Get the 8 octet sequence number
735 * checksum out of the verifier token.
737 nfsm_chain_get_opaque(error
, nmc
, verflen
, tokbuf
);
740 error
= nfs_gss_token_get(ki
, ALG_MIC(ki
), tokbuf
, 0, NULL
, cksum1
);
745 * Search the request sequence numbers for this reply, starting
746 * with the most recent, looking for a checksum that matches
747 * the one in the verifier returned by the server.
749 SLIST_FOREACH(gsp
, &req
->r_gss_seqlist
, gss_seqnext
) {
750 nfs_gss_cksum_rep(ki
, gsp
->gss_seqnum
, cksum2
);
751 if (bcmp(cksum1
, cksum2
, HASHLEN(ki
)) == 0)
755 return (NFSERR_EAUTH
);
758 * Get the RPC accepted status
760 nfsm_chain_get_32(error
, nmc
, *accepted_statusp
);
761 if (*accepted_statusp
!= RPC_SUCCESS
)
765 * Now we may have to check integrity or decrypt the results
766 * per RFC 2203 Section 5.3.2
768 switch (cp
->gss_clnt_service
) {
769 case RPCSEC_GSS_SVC_NONE
:
772 case RPCSEC_GSS_SVC_INTEGRITY
:
774 * Here's what we expect in the integrity results:
776 * - length of seq num + results (4 bytes)
777 * - sequence number (4 bytes)
778 * - results (variable bytes)
779 * - length of checksum token (37)
780 * - checksum of seqnum + results (37 bytes)
782 nfsm_chain_get_32(error
, nmc
, reslen
); // length of results
783 if (reslen
> NFS_MAXPACKET
) {
788 /* Compute a checksum over the sequence number + results */
789 start
= nfsm_chain_offset(nmc
);
790 nfs_gss_cksum_chain(ki
, nmc
, ALG_MIC(ki
), start
, reslen
, cksum1
);
793 * Get the sequence number prepended to the results
794 * and compare it against the list in the request.
796 nfsm_chain_get_32(error
, nmc
, seqnum
);
797 SLIST_FOREACH(gsp
, &req
->r_gss_seqlist
, gss_seqnext
) {
798 if (seqnum
== gsp
->gss_seqnum
)
807 * Advance to the end of the results and
808 * fetch the checksum computed by the server.
811 reslen
-= NFSX_UNSIGNED
; // already skipped seqnum
812 nfsm_chain_adv(error
, &nmc_tmp
, reslen
); // skip over the results
813 nfsm_chain_get_32(error
, &nmc_tmp
, cksumlen
); // length of checksum
814 if (cksumlen
!= KRB5_SZ_TOKEN(ki
->hash_len
)) {
818 nfsm_chain_get_opaque(error
, &nmc_tmp
, cksumlen
, tokbuf
);
821 error
= nfs_gss_token_get(ki
, ALG_MIC(ki
), tokbuf
, 0, NULL
, cksum2
);
825 /* Verify that the checksums are the same */
826 if (bcmp(cksum1
, cksum2
, HASHLEN(ki
)) != 0) {
831 case RPCSEC_GSS_SVC_PRIVACY
:
833 * Here's what we expect in the privacy results:
835 * - length of confounder + seq num + token + results
836 * - wrap token (37-40 bytes)
837 * - confounder (8 bytes)
838 * - sequence number (4 bytes)
839 * - results (encrypted)
841 nfsm_chain_get_32(error
, nmc
, reslen
); // length of results
842 if (reslen
> NFS_MAXPACKET
) {
847 /* Get the token that prepends the encrypted results */
848 nfsm_chain_get_opaque(error
, nmc
, KRB5_SZ_TOKMAX(ki
->hash_len
), tokbuf
);
851 error
= nfs_gss_token_get(ki
, ALG_WRAP(ki
), tokbuf
, 0,
855 nfsm_chain_reverse(nmc
, nfsm_pad(toklen
));
856 reslen
-= toklen
; // size of confounder + seqnum + results
858 /* decrypt the confounder + sequence number + results */
859 start
= nfsm_chain_offset(nmc
);
860 nfs_gss_encrypt_chain(ki
, nmc
, start
, reslen
, DES_DECRYPT
);
862 /* Compute a checksum over the confounder + sequence number + results */
863 nfs_gss_cksum_chain(ki
, nmc
, ALG_WRAP(ki
), start
, reslen
, cksum2
);
865 /* Verify that the checksums are the same */
866 if (bcmp(cksum1
, cksum2
, HASHLEN(ki
)) != 0) {
871 nfsm_chain_adv(error
, nmc
, 8); // skip over the confounder
874 * Get the sequence number prepended to the results
875 * and compare it against the list in the request.
877 nfsm_chain_get_32(error
, nmc
, seqnum
);
878 SLIST_FOREACH(gsp
, &req
->r_gss_seqlist
, gss_seqnext
) {
879 if (seqnum
== gsp
->gss_seqnum
)
894 * An RPCSEC_GSS request with no integrity or privacy consists
895 * of just the header mbufs followed by the arg mbufs.
897 * However, integrity or privacy both trailer mbufs to the args,
898 * which means we have to do some work to restore the arg mbuf
899 * chain to its previous state in case we need to retransmit.
901 * The location and length of the args is marked by two fields
902 * in the request structure: r_gss_argoff and r_gss_arglen,
903 * which are stashed when the NFS request is built.
906 nfs_gss_clnt_args_restore(struct nfsreq
*req
)
908 struct nfs_gss_clnt_ctx
*cp
= req
->r_gss_ctx
;
909 struct nfsm_chain mchain
, *nmc
= &mchain
;
913 return (NFSERR_EAUTH
);
915 if ((cp
->gss_clnt_flags
& GSS_CTX_COMPLETE
) == 0)
918 nfsm_chain_dissect_init(error
, nmc
, req
->r_mhead
); // start at RPC header
919 nfsm_chain_adv(error
, nmc
, req
->r_gss_argoff
); // advance to args
923 switch (cp
->gss_clnt_service
) {
924 case RPCSEC_GSS_SVC_NONE
:
927 case RPCSEC_GSS_SVC_INTEGRITY
:
929 * All we have to do here is remove the appended checksum mbufs.
930 * We know that the checksum starts in a new mbuf beyond the end
933 nfsm_chain_adv(error
, nmc
, req
->r_gss_arglen
); // adv to last args mbuf
937 mbuf_freem(mbuf_next(nmc
->nmc_mcur
)); // free the cksum mbuf
938 error
= mbuf_setnext(nmc
->nmc_mcur
, NULL
);
940 case RPCSEC_GSS_SVC_PRIVACY
:
942 * The args are encrypted along with prepended confounders and seqnum.
943 * First we decrypt, the confounder, seqnum and args then skip to the
944 * final mbuf of the args.
945 * The arglen includes 8 bytes of confounder and 4 bytes of seqnum.
946 * Finally, we remove between 4 and 8 bytes of encryption padding
947 * as well as any alignment padding in the trailing mbuf.
949 len
= req
->r_gss_arglen
;
950 len
+= len
% 8 > 0 ? 4 : 8; // add DES padding length
951 nfs_gss_encrypt_chain(&cp
->gss_clnt_kinfo
, nmc
,
952 req
->r_gss_argoff
, len
, DES_DECRYPT
);
953 nfsm_chain_adv(error
, nmc
, req
->r_gss_arglen
);
956 mbuf_freem(mbuf_next(nmc
->nmc_mcur
)); // free the pad mbuf
957 error
= mbuf_setnext(nmc
->nmc_mcur
, NULL
);
965 * This function sets up a new context on the client.
966 * Context setup alternates upcalls to the gssd with NFS nullproc calls
967 * to the server. Each of these calls exchanges an opaque token, obtained
968 * via the gssd's calls into the GSS-API on either the client or the server.
969 * This cycle of calls ends when the client's upcall to the gssd and the
970 * server's response both return GSS_S_COMPLETE. At this point, the client
971 * should have its session key and a handle that it can use to refer to its
972 * new context on the server.
975 nfs_gss_clnt_ctx_init(struct nfsreq
*req
, struct nfs_gss_clnt_ctx
*cp
)
977 struct nfsmount
*nmp
= req
->r_nmp
;
978 int client_complete
= 0;
979 int server_complete
= 0;
980 u_char cksum1
[MAX_DIGEST
], cksum2
[MAX_DIGEST
];
983 gss_key_info
*ki
= &cp
->gss_clnt_kinfo
;
985 /* Initialize a new client context */
987 cp
->gss_clnt_svcname
= nfs_gss_clnt_svcname(nmp
);
988 if (cp
->gss_clnt_svcname
== NULL
) {
989 error
= NFSERR_EAUTH
;
993 cp
->gss_clnt_proc
= RPCSEC_GSS_INIT
;
995 cp
->gss_clnt_service
=
996 req
->r_auth
== RPCAUTH_KRB5
? RPCSEC_GSS_SVC_NONE
:
997 req
->r_auth
== RPCAUTH_KRB5I
? RPCSEC_GSS_SVC_INTEGRITY
:
998 req
->r_auth
== RPCAUTH_KRB5P
? RPCSEC_GSS_SVC_PRIVACY
: 0;
1000 cp
->gss_clnt_gssd_flags
= (nfs_single_des
? GSSD_NFS_1DES
: 0);
1002 * Now loop around alternating gss_init_sec_context and
1003 * gss_accept_sec_context upcalls to the gssd on the client
1004 * and server side until the context is complete - or fails.
1009 /* Upcall to the gss_init_sec_context in the gssd */
1010 error
= nfs_gss_clnt_gssd_upcall(req
, cp
);
1014 if (cp
->gss_clnt_major
== GSS_S_COMPLETE
) {
1015 client_complete
= 1;
1016 if (server_complete
)
1018 } else if (cp
->gss_clnt_major
!= GSS_S_CONTINUE_NEEDED
) {
1019 error
= NFSERR_EAUTH
;
1024 * Pass the token to the server.
1026 error
= nfs_gss_clnt_ctx_callserver(req
, cp
);
1028 if (cp
->gss_clnt_proc
== RPCSEC_GSS_INIT
&&
1029 (cp
->gss_clnt_gssd_flags
& (GSSD_RESTART
| GSSD_NFS_1DES
)) == 0) {
1030 cp
->gss_clnt_gssd_flags
= (GSSD_RESTART
| GSSD_NFS_1DES
);
1031 if (cp
->gss_clnt_token
)
1032 FREE(cp
->gss_clnt_token
, M_TEMP
);
1033 cp
->gss_clnt_token
= NULL
;
1034 cp
->gss_clnt_tokenlen
= 0;
1037 // Reset flags, if error = ENEEDAUTH we will try 3des again
1038 cp
->gss_clnt_gssd_flags
= 0;
1041 if (cp
->gss_clnt_major
== GSS_S_COMPLETE
) {
1042 server_complete
= 1;
1043 if (client_complete
)
1045 } else if (cp
->gss_clnt_major
!= GSS_S_CONTINUE_NEEDED
) {
1046 error
= NFSERR_EAUTH
;
1050 cp
->gss_clnt_proc
= RPCSEC_GSS_CONTINUE_INIT
;
1054 * The context is apparently established successfully
1056 lck_mtx_lock(cp
->gss_clnt_mtx
);
1057 cp
->gss_clnt_flags
|= GSS_CTX_COMPLETE
;
1058 lck_mtx_unlock(cp
->gss_clnt_mtx
);
1059 cp
->gss_clnt_proc
= RPCSEC_GSS_DATA
;
1061 cp
->gss_clnt_ctime
= now
.tv_sec
; // time stamp
1065 * Compute checksum of the server's window
1067 nfs_gss_cksum_rep(ki
, cp
->gss_clnt_seqwin
, cksum1
);
1070 * and see if it matches the one in the
1071 * verifier the server returned.
1073 error
= nfs_gss_token_get(ki
, ALG_MIC(ki
), cp
->gss_clnt_verf
, 0,
1075 FREE(cp
->gss_clnt_verf
, M_TEMP
);
1076 cp
->gss_clnt_verf
= NULL
;
1078 if (error
|| bcmp(cksum1
, cksum2
, HASHLEN(ki
)) != 0) {
1079 error
= NFSERR_EAUTH
;
1084 * Set an initial sequence number somewhat randomized.
1085 * Start small so we don't overflow GSS_MAXSEQ too quickly.
1086 * Add the size of the sequence window so seqbits arithmetic
1087 * doesn't go negative.
1089 cp
->gss_clnt_seqnum
= (random() & 0xffff) + cp
->gss_clnt_seqwin
;
1092 * Allocate a bitmap to keep track of which requests
1093 * are pending within the sequence number window.
1095 MALLOC(cp
->gss_clnt_seqbits
, uint32_t *,
1096 nfsm_rndup((cp
->gss_clnt_seqwin
+ 7) / 8), M_TEMP
, M_WAITOK
|M_ZERO
);
1097 if (cp
->gss_clnt_seqbits
== NULL
)
1098 error
= NFSERR_EAUTH
;
1101 * If the error is ENEEDAUTH we're not done, so no need
1102 * to wake up other threads again. This thread will retry in
1103 * the find or renew routines.
1105 if (error
== ENEEDAUTH
)
1109 * If there's an error, just mark it as invalid.
1110 * It will be removed when the reference count
1113 lck_mtx_lock(cp
->gss_clnt_mtx
);
1115 cp
->gss_clnt_flags
|= GSS_CTX_INVAL
;
1118 * Wake any threads waiting to use the context
1120 cp
->gss_clnt_thread
= NULL
;
1121 if (cp
->gss_clnt_flags
& GSS_NEEDCTX
) {
1122 cp
->gss_clnt_flags
&= ~GSS_NEEDCTX
;
1125 lck_mtx_unlock(cp
->gss_clnt_mtx
);
1131 * This function calls nfs_gss_clnt_ctx_init() to set up a new context.
1132 * But if there's a failure in trying to establish the context it keeps
1133 * retrying at progressively longer intervals in case the failure is
1134 * due to some transient condition. For instance, the server might be
1135 * failing the context setup because directory services is not coming
1136 * up in a timely fashion.
1139 nfs_gss_clnt_ctx_init_retry(struct nfsreq
*req
, struct nfs_gss_clnt_ctx
*cp
)
1141 struct nfsmount
*nmp
= req
->r_nmp
;
1146 int timeo
= NFS_TRYLATERDEL
;
1153 /* For an "intr" mount allow a signal to interrupt the retries */
1154 slpflag
= (NMFLAG(nmp
, INTR
) && !(req
->r_flags
& R_NOINTR
)) ? PCATCH
: 0;
1156 while ((error
= nfs_gss_clnt_ctx_init(req
, cp
)) == ENEEDAUTH
) {
1158 waituntil
= now
.tv_sec
+ timeo
;
1159 while (now
.tv_sec
< waituntil
) {
1160 tsleep(&lbolt
, PSOCK
| slpflag
, "nfs_gss_clnt_ctx_init_retry", 0);
1162 error
= nfs_sigintr(req
->r_nmp
, req
, current_thread(), 0);
1169 /* If it's a soft mount just give up after a while */
1170 if (NMFLAG(nmp
, SOFT
) && (retries
> nmp
->nm_retry
)) {
1180 return 0; // success
1183 * Give up on this context
1185 lck_mtx_lock(cp
->gss_clnt_mtx
);
1186 cp
->gss_clnt_flags
|= GSS_CTX_INVAL
;
1189 * Wake any threads waiting to use the context
1191 cp
->gss_clnt_thread
= NULL
;
1192 if (cp
->gss_clnt_flags
& GSS_NEEDCTX
) {
1193 cp
->gss_clnt_flags
&= ~GSS_NEEDCTX
;
1196 lck_mtx_unlock(cp
->gss_clnt_mtx
);
1202 * Call the NFS server using a null procedure for context setup.
1203 * Even though it's a null procedure and nominally has no arguments
1204 * RFC 2203 requires that the GSS-API token be passed as an argument
1205 * and received as a reply.
1208 nfs_gss_clnt_ctx_callserver(struct nfsreq
*req
, struct nfs_gss_clnt_ctx
*cp
)
1210 struct nfsm_chain nmreq
, nmrep
;
1211 int error
= 0, status
;
1216 nfsm_chain_null(&nmreq
);
1217 nfsm_chain_null(&nmrep
);
1218 sz
= NFSX_UNSIGNED
+ nfsm_rndup(cp
->gss_clnt_tokenlen
);
1219 nfsm_chain_build_alloc_init(error
, &nmreq
, sz
);
1220 nfsm_chain_add_32(error
, &nmreq
, cp
->gss_clnt_tokenlen
);
1221 if (cp
->gss_clnt_tokenlen
> 0)
1222 nfsm_chain_add_opaque(error
, &nmreq
, cp
->gss_clnt_token
, cp
->gss_clnt_tokenlen
);
1223 nfsm_chain_build_done(error
, &nmreq
);
1227 /* Call the server */
1228 error
= nfs_request_gss(req
->r_nmp
->nm_mountp
, &nmreq
, req
->r_thread
, req
->r_cred
,
1229 (req
->r_flags
& R_OPTMASK
), cp
, &nmrep
, &status
);
1230 if (cp
->gss_clnt_token
!= NULL
) {
1231 FREE(cp
->gss_clnt_token
, M_TEMP
);
1232 cp
->gss_clnt_token
= NULL
;
1239 /* Get the server's reply */
1241 nfsm_chain_get_32(error
, &nmrep
, cp
->gss_clnt_handle_len
);
1242 if (cp
->gss_clnt_handle
!= NULL
) {
1243 FREE(cp
->gss_clnt_handle
, M_TEMP
);
1244 cp
->gss_clnt_handle
= NULL
;
1246 if (cp
->gss_clnt_handle_len
> 0) {
1247 MALLOC(cp
->gss_clnt_handle
, u_char
*, cp
->gss_clnt_handle_len
, M_TEMP
, M_WAITOK
);
1248 if (cp
->gss_clnt_handle
== NULL
) {
1252 nfsm_chain_get_opaque(error
, &nmrep
, cp
->gss_clnt_handle_len
, cp
->gss_clnt_handle
);
1254 nfsm_chain_get_32(error
, &nmrep
, cp
->gss_clnt_major
);
1255 nfsm_chain_get_32(error
, &nmrep
, cp
->gss_clnt_minor
);
1256 nfsm_chain_get_32(error
, &nmrep
, cp
->gss_clnt_seqwin
);
1257 nfsm_chain_get_32(error
, &nmrep
, cp
->gss_clnt_tokenlen
);
1260 if (cp
->gss_clnt_tokenlen
> 0) {
1261 MALLOC(cp
->gss_clnt_token
, u_char
*, cp
->gss_clnt_tokenlen
, M_TEMP
, M_WAITOK
);
1262 if (cp
->gss_clnt_token
== NULL
) {
1266 nfsm_chain_get_opaque(error
, &nmrep
, cp
->gss_clnt_tokenlen
, cp
->gss_clnt_token
);
1270 * Make sure any unusual errors are expanded and logged by gssd
1272 if (cp
->gss_clnt_major
!= GSS_S_COMPLETE
&&
1273 cp
->gss_clnt_major
!= GSS_S_CONTINUE_NEEDED
) {
1274 char who
[] = "server";
1275 char unknown
[] = "<unknown>";
1277 (void) mach_gss_log_error(
1279 !req
->r_nmp
? unknown
:
1280 vfs_statfs(req
->r_nmp
->nm_mountp
)->f_mntfromname
,
1284 cp
->gss_clnt_minor
);
1288 nfsm_chain_cleanup(&nmreq
);
1289 nfsm_chain_cleanup(&nmrep
);
1295 * Ugly hack to get the service principal from the f_mntfromname field in
1296 * the statfs struct. We assume a format of server:path. We don't currently
1297 * support url's or other bizarre formats like path@server. A better solution
1298 * here might be to allow passing the service principal down in the mount args.
1299 * For kerberos we just use the default realm.
1302 nfs_gss_clnt_svcname(struct nfsmount
*nmp
)
1304 char *svcname
, *d
, *mntfromhere
;
1309 mntfromhere
= &vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
[0];
1310 len
= strlen(mntfromhere
) + 5; /* "nfs/" plus null */
1311 MALLOC(svcname
, char *, len
, M_TEMP
, M_NOWAIT
);
1312 if (svcname
== NULL
)
1314 strlcpy(svcname
, "nfs/", len
);
1315 strlcat(svcname
, mntfromhere
, len
);
1316 d
= strchr(svcname
, ':');
1324 * Get a mach port to talk to gssd.
1325 * gssd lives in the root bootstrap, so we call gssd's lookup routine
1326 * to get a send right to talk to a new gssd instance that launchd has launched
1327 * based on the cred's uid and audit session id.
1329 #define kauth_cred_getasid(cred) ((cred)->cr_audit.as_aia_p->ai_asid)
1330 #define kauth_cred_getauid(cred) ((cred)->cr_audit.as_aia_p->ai_auid)
1333 nfs_gss_clnt_get_upcall_port(kauth_cred_t credp
)
1335 mach_port_t gssd_host_port
, uc_port
= IPC_PORT_NULL
;
1340 kr
= host_get_gssd_port(host_priv_self(), &gssd_host_port
);
1341 if (kr
!= KERN_SUCCESS
) {
1342 printf("nfs_gss_get_upcall_port: can't get gssd port, status %x (%d)\n", kr
, kr
);
1343 return (IPC_PORT_NULL
);
1345 if (!IPC_PORT_VALID(gssd_host_port
)) {
1346 printf("nfs_gss_get_upcall_port: gssd port not valid\n");
1347 return (IPC_PORT_NULL
);
1350 asid
= kauth_cred_getasid(credp
);
1351 uid
= kauth_cred_getauid(credp
);
1352 if (uid
== AU_DEFAUDITID
)
1353 uid
= kauth_cred_getuid(credp
);
1354 kr
= mach_gss_lookup(gssd_host_port
, uid
, asid
, &uc_port
);
1355 if (kr
!= KERN_SUCCESS
)
1356 printf("nfs_gss_clnt_get_upcall_port: mach_gssd_lookup failed: status %x (%d)\n", kr
, kr
);
1362 * Make an upcall to the gssd using Mach RPC
1363 * The upcall is made using a host special port.
1364 * This allows launchd to fire up the gssd in the
1365 * user's session. This is important, since gssd
1366 * must have access to the user's credential cache.
1369 nfs_gss_clnt_gssd_upcall(struct nfsreq
*req
, struct nfs_gss_clnt_ctx
*cp
)
1372 gssd_byte_buffer okey
= NULL
;
1373 uint32_t skeylen
= 0;
1375 vm_map_copy_t itoken
= NULL
;
1376 gssd_byte_buffer otoken
= NULL
;
1377 mach_msg_type_number_t otokenlen
;
1383 * NFS currently only supports default principals or
1384 * principals based on the uid of the caller.
1386 * N.B. Note we define a one character array for the principal
1387 * so that we can hold an empty string required by mach, since
1388 * the kernel is being compiled with -Wwrite-strings.
1391 if (!IPC_PORT_VALID(cp
->gss_clnt_mport
)) {
1392 cp
->gss_clnt_mport
= nfs_gss_clnt_get_upcall_port(req
->r_cred
);
1393 if (cp
->gss_clnt_mport
== IPC_PORT_NULL
)
1397 if (cp
->gss_clnt_tokenlen
> 0)
1398 nfs_gss_mach_alloc_buffer(cp
->gss_clnt_token
, cp
->gss_clnt_tokenlen
, &itoken
);
1401 kr
= mach_gss_init_sec_context(
1404 (gssd_byte_buffer
) itoken
, (mach_msg_type_number_t
) cp
->gss_clnt_tokenlen
,
1407 cp
->gss_clnt_svcname
,
1409 cp
->gss_clnt_gssd_flags
,
1410 &cp
->gss_clnt_context
,
1411 &cp
->gss_clnt_cred_handle
,
1413 &okey
, (mach_msg_type_number_t
*) &skeylen
,
1414 &otoken
, &otokenlen
,
1415 &cp
->gss_clnt_major
,
1416 &cp
->gss_clnt_minor
);
1418 cp
->gss_clnt_gssd_flags
&= ~GSSD_RESTART
;
1420 if (kr
!= KERN_SUCCESS
) {
1421 printf("nfs_gss_clnt_gssd_upcall: mach_gss_init_sec_context failed: %x (%d)\n", kr
, kr
);
1422 if (kr
== MIG_SERVER_DIED
&& cp
->gss_clnt_cred_handle
== 0 &&
1423 retry_cnt
++ < NFS_GSS_MACH_MAX_RETRIES
) {
1424 if (cp
->gss_clnt_tokenlen
> 0)
1425 nfs_gss_mach_alloc_buffer(cp
->gss_clnt_token
, cp
->gss_clnt_tokenlen
, &itoken
);
1429 host_release_special_port(cp
->gss_clnt_mport
);
1430 cp
->gss_clnt_mport
= IPC_PORT_NULL
;
1435 * Make sure any unusual errors are expanded and logged by gssd
1437 if (cp
->gss_clnt_major
!= GSS_S_COMPLETE
&&
1438 cp
->gss_clnt_major
!= GSS_S_CONTINUE_NEEDED
) {
1439 char who
[] = "client";
1440 char unknown
[] = "<unknown>";
1442 (void) mach_gss_log_error(
1444 !req
->r_nmp
? unknown
:
1445 vfs_statfs(req
->r_nmp
->nm_mountp
)->f_mntfromname
,
1449 cp
->gss_clnt_minor
);
1453 if (skeylen
!= SKEYLEN
&& skeylen
!= SKEYLEN3
) {
1454 printf("nfs_gss_clnt_gssd_upcall: bad key length (%d)\n", skeylen
);
1455 vm_map_copy_discard((vm_map_copy_t
) okey
);
1456 vm_map_copy_discard((vm_map_copy_t
) otoken
);
1459 error
= nfs_gss_mach_vmcopyout((vm_map_copy_t
) okey
, skeylen
,
1460 cp
->gss_clnt_kinfo
.skey
);
1462 vm_map_copy_discard((vm_map_copy_t
) otoken
);
1466 error
= gss_key_init(&cp
->gss_clnt_kinfo
, skeylen
);
1471 /* Free context token used as input */
1472 if (cp
->gss_clnt_token
)
1473 FREE(cp
->gss_clnt_token
, M_TEMP
);
1474 cp
->gss_clnt_token
= NULL
;
1475 cp
->gss_clnt_tokenlen
= 0;
1477 if (otokenlen
> 0) {
1478 /* Set context token to gss output token */
1479 MALLOC(cp
->gss_clnt_token
, u_char
*, otokenlen
, M_TEMP
, M_WAITOK
);
1480 if (cp
->gss_clnt_token
== NULL
) {
1481 printf("nfs_gss_clnt_gssd_upcall: could not allocate %d bytes\n", otokenlen
);
1482 vm_map_copy_discard((vm_map_copy_t
) otoken
);
1485 error
= nfs_gss_mach_vmcopyout((vm_map_copy_t
) otoken
, otokenlen
, cp
->gss_clnt_token
);
1487 FREE(cp
->gss_clnt_token
, M_TEMP
);
1488 cp
->gss_clnt_token
= NULL
;
1489 return (NFSERR_EAUTH
);
1491 cp
->gss_clnt_tokenlen
= otokenlen
;
1497 if (cp
->gss_clnt_token
)
1498 FREE(cp
->gss_clnt_token
, M_TEMP
);
1499 cp
->gss_clnt_token
= NULL
;
1500 cp
->gss_clnt_tokenlen
= 0;
1502 return (NFSERR_EAUTH
);
1506 * Invoked at the completion of an RPC call that uses an RPCSEC_GSS
1507 * credential. The sequence number window that the server returns
1508 * at context setup indicates the maximum number of client calls that
1509 * can be outstanding on a context. The client maintains a bitmap that
1510 * represents the server's window. Each pending request has a bit set
1511 * in the window bitmap. When a reply comes in or times out, we reset
1512 * the bit in the bitmap and if there are any other threads waiting for
1513 * a context slot we notify the waiting thread(s).
1515 * Note that if a request is retransmitted, it will have a single XID
1516 * but it may be associated with multiple sequence numbers. So we
1517 * may have to reset multiple sequence number bits in the window bitmap.
1520 nfs_gss_clnt_rpcdone(struct nfsreq
*req
)
1522 struct nfs_gss_clnt_ctx
*cp
= req
->r_gss_ctx
;
1523 struct gss_seq
*gsp
, *ngsp
;
1526 if (cp
== NULL
|| !(cp
->gss_clnt_flags
& GSS_CTX_COMPLETE
))
1527 return; // no context - don't bother
1529 * Reset the bit for this request in the
1530 * sequence number window to indicate it's done.
1531 * We do this even if the request timed out.
1533 lck_mtx_lock(cp
->gss_clnt_mtx
);
1534 gsp
= SLIST_FIRST(&req
->r_gss_seqlist
);
1535 if (gsp
&& gsp
->gss_seqnum
> (cp
->gss_clnt_seqnum
- cp
->gss_clnt_seqwin
))
1536 win_resetbit(cp
->gss_clnt_seqbits
,
1537 gsp
->gss_seqnum
% cp
->gss_clnt_seqwin
);
1540 * Limit the seqnum list to GSS_CLNT_SEQLISTMAX entries
1542 SLIST_FOREACH_SAFE(gsp
, &req
->r_gss_seqlist
, gss_seqnext
, ngsp
) {
1543 if (++i
> GSS_CLNT_SEQLISTMAX
) {
1544 SLIST_REMOVE(&req
->r_gss_seqlist
, gsp
, gss_seq
, gss_seqnext
);
1550 * If there's a thread waiting for
1551 * the window to advance, wake it up.
1553 if (cp
->gss_clnt_flags
& GSS_NEEDSEQ
) {
1554 cp
->gss_clnt_flags
&= ~GSS_NEEDSEQ
;
1557 lck_mtx_unlock(cp
->gss_clnt_mtx
);
1561 * Create a reference to a context from a request
1562 * and bump the reference count
1565 nfs_gss_clnt_ctx_ref(struct nfsreq
*req
, struct nfs_gss_clnt_ctx
*cp
)
1567 req
->r_gss_ctx
= cp
;
1569 lck_mtx_lock(cp
->gss_clnt_mtx
);
1570 cp
->gss_clnt_refcnt
++;
1571 lck_mtx_unlock(cp
->gss_clnt_mtx
);
1575 * Remove a context reference from a request
1576 * If the reference count drops to zero, and the
1577 * context is invalid, destroy the context
1580 nfs_gss_clnt_ctx_unref(struct nfsreq
*req
)
1582 struct nfsmount
*nmp
= req
->r_nmp
;
1583 struct nfs_gss_clnt_ctx
*cp
= req
->r_gss_ctx
;
1588 req
->r_gss_ctx
= NULL
;
1590 lck_mtx_lock(cp
->gss_clnt_mtx
);
1591 if (--cp
->gss_clnt_refcnt
== 0
1592 && cp
->gss_clnt_flags
& GSS_CTX_INVAL
) {
1593 lck_mtx_unlock(cp
->gss_clnt_mtx
);
1596 lck_mtx_lock(&nmp
->nm_lock
);
1597 nfs_gss_clnt_ctx_remove(nmp
, cp
);
1599 lck_mtx_unlock(&nmp
->nm_lock
);
1603 lck_mtx_unlock(cp
->gss_clnt_mtx
);
1610 nfs_gss_clnt_ctx_remove(struct nfsmount
*nmp
, struct nfs_gss_clnt_ctx
*cp
)
1613 * If dequeueing, assume nmp->nm_lock is held
1616 TAILQ_REMOVE(&nmp
->nm_gsscl
, cp
, gss_clnt_entries
);
1618 host_release_special_port(cp
->gss_clnt_mport
);
1620 if (cp
->gss_clnt_mtx
)
1621 lck_mtx_destroy(cp
->gss_clnt_mtx
, nfs_gss_clnt_grp
);
1622 if (cp
->gss_clnt_handle
)
1623 FREE(cp
->gss_clnt_handle
, M_TEMP
);
1624 if (cp
->gss_clnt_seqbits
)
1625 FREE(cp
->gss_clnt_seqbits
, M_TEMP
);
1626 if (cp
->gss_clnt_token
)
1627 FREE(cp
->gss_clnt_token
, M_TEMP
);
1628 if (cp
->gss_clnt_svcname
)
1629 FREE(cp
->gss_clnt_svcname
, M_TEMP
);
1634 * The context for a user is invalid.
1635 * Mark the context as invalid, then
1636 * create a new context.
1639 nfs_gss_clnt_ctx_renew(struct nfsreq
*req
)
1641 struct nfs_gss_clnt_ctx
*cp
= req
->r_gss_ctx
;
1642 struct nfsmount
*nmp
= req
->r_nmp
;
1643 struct nfs_gss_clnt_ctx
*ncp
;
1646 mach_port_t saved_mport
;
1651 lck_mtx_lock(cp
->gss_clnt_mtx
);
1652 if (cp
->gss_clnt_flags
& GSS_CTX_INVAL
) {
1653 lck_mtx_unlock(cp
->gss_clnt_mtx
);
1654 nfs_gss_clnt_ctx_unref(req
);
1655 return (0); // already being renewed
1657 saved_uid
= cp
->gss_clnt_uid
;
1658 saved_mport
= host_copy_special_port(cp
->gss_clnt_mport
);
1660 /* Remove the old context */
1661 cp
->gss_clnt_flags
|= GSS_CTX_INVAL
;
1664 * If there's a thread waiting
1665 * in the old context, wake it up.
1667 if (cp
->gss_clnt_flags
& (GSS_NEEDCTX
| GSS_NEEDSEQ
)) {
1668 cp
->gss_clnt_flags
&= ~GSS_NEEDSEQ
;
1671 lck_mtx_unlock(cp
->gss_clnt_mtx
);
1674 * Create a new context
1676 MALLOC(ncp
, struct nfs_gss_clnt_ctx
*, sizeof(*ncp
),
1677 M_TEMP
, M_WAITOK
|M_ZERO
);
1683 ncp
->gss_clnt_uid
= saved_uid
;
1684 ncp
->gss_clnt_mport
= host_copy_special_port(saved_mport
); // re-use the gssd port
1685 ncp
->gss_clnt_mtx
= lck_mtx_alloc_init(nfs_gss_clnt_grp
, LCK_ATTR_NULL
);
1686 ncp
->gss_clnt_thread
= current_thread();
1687 lck_mtx_lock(&nmp
->nm_lock
);
1688 TAILQ_INSERT_TAIL(&nmp
->nm_gsscl
, ncp
, gss_clnt_entries
);
1689 lck_mtx_unlock(&nmp
->nm_lock
);
1691 /* Adjust reference counts to new and old context */
1692 nfs_gss_clnt_ctx_unref(req
);
1693 nfs_gss_clnt_ctx_ref(req
, ncp
);
1695 error
= nfs_gss_clnt_ctx_init_retry(req
, ncp
); // Initialize new context
1697 host_release_special_port(saved_mport
);
1699 nfs_gss_clnt_ctx_unref(req
);
1705 * Destroy all the contexts associated with a mount.
1706 * The contexts are also destroyed by the server.
1709 nfs_gss_clnt_ctx_unmount(struct nfsmount
*nmp
)
1711 struct nfs_gss_clnt_ctx
*cp
;
1712 struct nfsm_chain nmreq
, nmrep
;
1719 lck_mtx_lock(&nmp
->nm_lock
);
1720 cp
= TAILQ_FIRST(&nmp
->nm_gsscl
);
1721 lck_mtx_unlock(&nmp
->nm_lock
);
1725 nfs_gss_clnt_ctx_ref(&req
, cp
);
1728 * Tell the server to destroy its context.
1729 * But don't bother if it's a forced unmount
1730 * or if it's a dummy sec=sys context.
1732 if (!(nmp
->nm_state
& NFSSTA_FORCE
) && (cp
->gss_clnt_service
!= RPCSEC_GSS_SVC_SYS
)) {
1734 struct posix_cred temp_pcred
;
1736 bzero((caddr_t
) &temp_pcred
, sizeof(temp_pcred
));
1737 temp_pcred
.cr_ngroups
= 1;
1738 temp_pcred
.cr_uid
= cp
->gss_clnt_uid
;
1739 cred
= posix_cred_create(&temp_pcred
);
1740 cp
->gss_clnt_proc
= RPCSEC_GSS_DESTROY
;
1743 nfsm_chain_null(&nmreq
);
1744 nfsm_chain_null(&nmrep
);
1745 nfsm_chain_build_alloc_init(error
, &nmreq
, 0);
1746 nfsm_chain_build_done(error
, &nmreq
);
1748 nfs_request_gss(nmp
->nm_mountp
, &nmreq
,
1749 current_thread(), cred
, 0, cp
, &nmrep
, &status
);
1750 nfsm_chain_cleanup(&nmreq
);
1751 nfsm_chain_cleanup(&nmrep
);
1752 kauth_cred_unref(&cred
);
1756 * Mark the context invalid then drop
1757 * the reference to remove it if its
1760 lck_mtx_lock(cp
->gss_clnt_mtx
);
1761 cp
->gss_clnt_flags
|= GSS_CTX_INVAL
;
1762 lck_mtx_unlock(cp
->gss_clnt_mtx
);
1763 nfs_gss_clnt_ctx_unref(&req
);
1767 #endif /* NFSCLIENT */
1777 * Find a server context based on a handle value received
1778 * in an RPCSEC_GSS credential.
1780 static struct nfs_gss_svc_ctx
*
1781 nfs_gss_svc_ctx_find(uint32_t handle
)
1783 struct nfs_gss_svc_ctx_hashhead
*head
;
1784 struct nfs_gss_svc_ctx
*cp
;
1790 head
= &nfs_gss_svc_ctx_hashtbl
[SVC_CTX_HASH(handle
)];
1792 * Don't return a context that is going to expire in GSS_CTX_PEND seconds
1794 clock_interval_to_deadline(GSS_CTX_PEND
, NSEC_PER_SEC
, &timenow
);
1796 lck_mtx_lock(nfs_gss_svc_ctx_mutex
);
1798 LIST_FOREACH(cp
, head
, gss_svc_entries
) {
1799 if (cp
->gss_svc_handle
== handle
) {
1800 if (timenow
> cp
->gss_svc_incarnation
+ GSS_SVC_CTX_TTL
) {
1802 * Context has or is about to expire. Don't use.
1803 * We'll return null and the client will have to create
1806 cp
->gss_svc_handle
= 0;
1808 * Make sure though that we stay around for GSS_CTX_PEND seconds
1809 * for other threads that might be using the context.
1811 cp
->gss_svc_incarnation
= timenow
;
1816 lck_mtx_lock(cp
->gss_svc_mtx
);
1817 cp
->gss_svc_refcnt
++;
1818 lck_mtx_unlock(cp
->gss_svc_mtx
);
1823 lck_mtx_unlock(nfs_gss_svc_ctx_mutex
);
1829 * Insert a new server context into the hash table
1830 * and start the context reap thread if necessary.
1833 nfs_gss_svc_ctx_insert(struct nfs_gss_svc_ctx
*cp
)
1835 struct nfs_gss_svc_ctx_hashhead
*head
;
1836 struct nfs_gss_svc_ctx
*p
;
1838 lck_mtx_lock(nfs_gss_svc_ctx_mutex
);
1841 * Give the client a random handle so that if we reboot
1842 * it's unlikely the client will get a bad context match.
1843 * Make sure it's not zero or already assigned.
1846 cp
->gss_svc_handle
= random();
1847 if (cp
->gss_svc_handle
== 0)
1849 head
= &nfs_gss_svc_ctx_hashtbl
[SVC_CTX_HASH(cp
->gss_svc_handle
)];
1850 LIST_FOREACH(p
, head
, gss_svc_entries
)
1851 if (p
->gss_svc_handle
== cp
->gss_svc_handle
)
1854 clock_interval_to_deadline(GSS_CTX_PEND
, NSEC_PER_SEC
,
1855 &cp
->gss_svc_incarnation
);
1856 LIST_INSERT_HEAD(head
, cp
, gss_svc_entries
);
1857 nfs_gss_ctx_count
++;
1859 if (!nfs_gss_timer_on
) {
1860 nfs_gss_timer_on
= 1;
1862 nfs_interval_timer_start(nfs_gss_svc_ctx_timer_call
,
1863 min(GSS_TIMER_PERIOD
, max(GSS_CTX_TTL_MIN
, nfsrv_gss_context_ttl
)) * MSECS_PER_SEC
);
1866 lck_mtx_unlock(nfs_gss_svc_ctx_mutex
);
1870 * This function is called via the kernel's callout
1871 * mechanism. It runs only when there are
1872 * cached RPCSEC_GSS contexts.
1875 nfs_gss_svc_ctx_timer(__unused
void *param1
, __unused
void *param2
)
1877 struct nfs_gss_svc_ctx
*cp
, *next
;
1882 lck_mtx_lock(nfs_gss_svc_ctx_mutex
);
1883 clock_get_uptime(&timenow
);
1886 * Scan all the hash chains
1888 for (i
= 0; i
< SVC_CTX_HASHSZ
; i
++) {
1890 * For each hash chain, look for entries
1891 * that haven't been used in a while.
1893 LIST_FOREACH_SAFE(cp
, &nfs_gss_svc_ctx_hashtbl
[i
], gss_svc_entries
, next
) {
1895 if (timenow
> cp
->gss_svc_incarnation
+
1896 (cp
->gss_svc_handle
? GSS_SVC_CTX_TTL
: 0)
1897 && cp
->gss_svc_refcnt
== 0) {
1899 * A stale context - remove it
1901 LIST_REMOVE(cp
, gss_svc_entries
);
1902 if (cp
->gss_svc_seqbits
)
1903 FREE(cp
->gss_svc_seqbits
, M_TEMP
);
1904 lck_mtx_destroy(cp
->gss_svc_mtx
, nfs_gss_svc_grp
);
1911 nfs_gss_ctx_count
= contexts
;
1914 * If there are still some cached contexts left,
1915 * set up another callout to check on them later.
1917 nfs_gss_timer_on
= nfs_gss_ctx_count
> 0;
1918 if (nfs_gss_timer_on
)
1919 nfs_interval_timer_start(nfs_gss_svc_ctx_timer_call
,
1920 min(GSS_TIMER_PERIOD
, max(GSS_CTX_TTL_MIN
, nfsrv_gss_context_ttl
)) * MSECS_PER_SEC
);
1922 lck_mtx_unlock(nfs_gss_svc_ctx_mutex
);
1926 * Here the server receives an RPCSEC_GSS credential in an
1927 * RPC call header. First there's some checking to make sure
1928 * the credential is appropriate - whether the context is still
1929 * being set up, or is complete. Then we use the handle to find
1930 * the server's context and validate the verifier, which contains
1931 * a signed checksum of the RPC header. If the verifier checks
1932 * out, we extract the user's UID and groups from the context
1933 * and use it to set up a UNIX credential for the user's request.
1936 nfs_gss_svc_cred_get(struct nfsrv_descript
*nd
, struct nfsm_chain
*nmc
)
1938 uint32_t vers
, proc
, seqnum
, service
;
1939 uint32_t handle
, handle_len
;
1940 struct nfs_gss_svc_ctx
*cp
= NULL
;
1941 uint32_t flavor
= 0, verflen
= 0;
1943 uint32_t arglen
, start
, toklen
, cksumlen
;
1944 u_char tokbuf
[KRB5_SZ_TOKMAX(MAX_DIGEST
)];
1945 u_char cksum1
[MAX_DIGEST
], cksum2
[MAX_DIGEST
];
1946 struct nfsm_chain nmc_tmp
;
1949 vers
= proc
= seqnum
= service
= handle_len
= 0;
1950 arglen
= cksumlen
= 0;
1952 nfsm_chain_get_32(error
, nmc
, vers
);
1953 if (vers
!= RPCSEC_GSS_VERS_1
) {
1954 error
= NFSERR_AUTHERR
| AUTH_REJECTCRED
;
1958 nfsm_chain_get_32(error
, nmc
, proc
);
1959 nfsm_chain_get_32(error
, nmc
, seqnum
);
1960 nfsm_chain_get_32(error
, nmc
, service
);
1961 nfsm_chain_get_32(error
, nmc
, handle_len
);
1966 * Make sure context setup/destroy is being done with a nullproc
1968 if (proc
!= RPCSEC_GSS_DATA
&& nd
->nd_procnum
!= NFSPROC_NULL
) {
1969 error
= NFSERR_AUTHERR
| RPCSEC_GSS_CREDPROBLEM
;
1974 * If the sequence number is greater than the max
1975 * allowable, reject and have the client init a
1978 if (seqnum
> GSS_MAXSEQ
) {
1979 error
= NFSERR_AUTHERR
| RPCSEC_GSS_CTXPROBLEM
;
1984 service
== RPCSEC_GSS_SVC_NONE
? RPCAUTH_KRB5
:
1985 service
== RPCSEC_GSS_SVC_INTEGRITY
? RPCAUTH_KRB5I
:
1986 service
== RPCSEC_GSS_SVC_PRIVACY
? RPCAUTH_KRB5P
: 0;
1988 if (proc
== RPCSEC_GSS_INIT
) {
1990 * Limit the total number of contexts
1992 if (nfs_gss_ctx_count
> nfs_gss_ctx_max
) {
1993 error
= NFSERR_AUTHERR
| RPCSEC_GSS_CTXPROBLEM
;
1998 * Set up a new context
2000 MALLOC(cp
, struct nfs_gss_svc_ctx
*, sizeof(*cp
), M_TEMP
, M_WAITOK
|M_ZERO
);
2005 cp
->gss_svc_mtx
= lck_mtx_alloc_init(nfs_gss_svc_grp
, LCK_ATTR_NULL
);
2006 cp
->gss_svc_refcnt
= 1;
2010 * Use the handle to find the context
2012 if (handle_len
!= sizeof(handle
)) {
2013 error
= NFSERR_AUTHERR
| RPCSEC_GSS_CREDPROBLEM
;
2016 nfsm_chain_get_32(error
, nmc
, handle
);
2019 cp
= nfs_gss_svc_ctx_find(handle
);
2021 error
= NFSERR_AUTHERR
| RPCSEC_GSS_CTXPROBLEM
;
2026 cp
->gss_svc_proc
= proc
;
2027 ki
= &cp
->gss_svc_kinfo
;
2029 if (proc
== RPCSEC_GSS_DATA
|| proc
== RPCSEC_GSS_DESTROY
) {
2030 struct posix_cred temp_pcred
;
2032 if (cp
->gss_svc_seqwin
== 0) {
2034 * Context isn't complete
2036 error
= NFSERR_AUTHERR
| RPCSEC_GSS_CTXPROBLEM
;
2040 if (!nfs_gss_svc_seqnum_valid(cp
, seqnum
)) {
2042 * Sequence number is bad
2044 error
= EINVAL
; // drop the request
2048 /* Now compute the client's call header checksum */
2049 nfs_gss_cksum_chain(ki
, nmc
, ALG_MIC(ki
), 0, 0, cksum1
);
2052 * Validate the verifier.
2053 * The verifier contains an encrypted checksum
2054 * of the call header from the XID up to and
2055 * including the credential. We compute the
2056 * checksum and compare it with what came in
2059 nfsm_chain_get_32(error
, nmc
, flavor
);
2060 nfsm_chain_get_32(error
, nmc
, verflen
);
2063 if (flavor
!= RPCSEC_GSS
|| verflen
!= KRB5_SZ_TOKEN(ki
->hash_len
))
2064 error
= NFSERR_AUTHERR
| AUTH_BADVERF
;
2065 nfsm_chain_get_opaque(error
, nmc
, verflen
, tokbuf
);
2069 /* Get the checksum from the token inside the verifier */
2070 error
= nfs_gss_token_get(ki
, ALG_MIC(ki
), tokbuf
, 1,
2075 if (bcmp(cksum1
, cksum2
, HASHLEN(ki
)) != 0) {
2076 error
= NFSERR_AUTHERR
| RPCSEC_GSS_CTXPROBLEM
;
2080 nd
->nd_gss_seqnum
= seqnum
;
2083 * Set up the user's cred
2085 bzero(&temp_pcred
, sizeof(temp_pcred
));
2086 temp_pcred
.cr_uid
= cp
->gss_svc_uid
;
2087 bcopy(cp
->gss_svc_gids
, temp_pcred
.cr_groups
,
2088 sizeof(gid_t
) * cp
->gss_svc_ngroups
);
2089 temp_pcred
.cr_ngroups
= cp
->gss_svc_ngroups
;
2091 nd
->nd_cr
= posix_cred_create(&temp_pcred
);
2092 if (nd
->nd_cr
== NULL
) {
2096 clock_get_uptime(&cp
->gss_svc_incarnation
);
2099 * If the call arguments are integrity or privacy protected
2100 * then we need to check them here.
2103 case RPCSEC_GSS_SVC_NONE
:
2106 case RPCSEC_GSS_SVC_INTEGRITY
:
2108 * Here's what we expect in the integrity call args:
2110 * - length of seq num + call args (4 bytes)
2111 * - sequence number (4 bytes)
2112 * - call args (variable bytes)
2113 * - length of checksum token (37)
2114 * - checksum of seqnum + call args (37 bytes)
2116 nfsm_chain_get_32(error
, nmc
, arglen
); // length of args
2117 if (arglen
> NFS_MAXPACKET
) {
2122 /* Compute the checksum over the call args */
2123 start
= nfsm_chain_offset(nmc
);
2124 nfs_gss_cksum_chain(ki
, nmc
, ALG_MIC(ki
), start
, arglen
, cksum1
);
2127 * Get the sequence number prepended to the args
2128 * and compare it against the one sent in the
2131 nfsm_chain_get_32(error
, nmc
, seqnum
);
2132 if (seqnum
!= nd
->nd_gss_seqnum
) {
2133 error
= EBADRPC
; // returns as GARBAGEARGS
2138 * Advance to the end of the args and
2139 * fetch the checksum computed by the client.
2142 arglen
-= NFSX_UNSIGNED
; // skipped seqnum
2143 nfsm_chain_adv(error
, &nmc_tmp
, arglen
); // skip args
2144 nfsm_chain_get_32(error
, &nmc_tmp
, cksumlen
); // length of checksum
2145 if (cksumlen
!= KRB5_SZ_TOKEN(ki
->hash_len
)) {
2149 nfsm_chain_get_opaque(error
, &nmc_tmp
, cksumlen
, tokbuf
);
2152 error
= nfs_gss_token_get(ki
, ALG_MIC(ki
), tokbuf
, 1,
2155 /* Verify that the checksums are the same */
2156 if (error
|| bcmp(cksum1
, cksum2
, HASHLEN(ki
)) != 0) {
2161 case RPCSEC_GSS_SVC_PRIVACY
:
2163 * Here's what we expect in the privacy call args:
2165 * - length of confounder + seq num + token + call args
2166 * - wrap token (37-40 bytes)
2167 * - confounder (8 bytes)
2168 * - sequence number (4 bytes)
2169 * - call args (encrypted)
2171 nfsm_chain_get_32(error
, nmc
, arglen
); // length of args
2172 if (arglen
> NFS_MAXPACKET
) {
2177 /* Get the token that prepends the encrypted args */
2178 nfsm_chain_get_opaque(error
, nmc
, KRB5_SZ_TOKMAX(ki
->hash_len
), tokbuf
);
2181 error
= nfs_gss_token_get(ki
, ALG_WRAP(ki
), tokbuf
, 1,
2185 nfsm_chain_reverse(nmc
, nfsm_pad(toklen
));
2187 /* decrypt the 8 byte confounder + seqnum + args */
2188 start
= nfsm_chain_offset(nmc
);
2190 nfs_gss_encrypt_chain(ki
, nmc
, start
, arglen
, DES_DECRYPT
);
2192 /* Compute a checksum over the sequence number + results */
2193 nfs_gss_cksum_chain(ki
, nmc
, ALG_WRAP(ki
), start
, arglen
, cksum2
);
2195 /* Verify that the checksums are the same */
2196 if (bcmp(cksum1
, cksum2
, HASHLEN(ki
)) != 0) {
2202 * Get the sequence number prepended to the args
2203 * and compare it against the one sent in the
2206 nfsm_chain_adv(error
, nmc
, 8); // skip over the confounder
2207 nfsm_chain_get_32(error
, nmc
, seqnum
);
2208 if (seqnum
!= nd
->nd_gss_seqnum
) {
2209 error
= EBADRPC
; // returns as GARBAGEARGS
2216 * If the proc is RPCSEC_GSS_INIT or RPCSEC_GSS_CONTINUE_INIT
2217 * then we expect a null verifier.
2219 nfsm_chain_get_32(error
, nmc
, flavor
);
2220 nfsm_chain_get_32(error
, nmc
, verflen
);
2221 if (error
|| flavor
!= RPCAUTH_NULL
|| verflen
> 0)
2222 error
= NFSERR_AUTHERR
| RPCSEC_GSS_CREDPROBLEM
;
2224 if (proc
== RPCSEC_GSS_INIT
) {
2225 lck_mtx_destroy(cp
->gss_svc_mtx
, nfs_gss_svc_grp
);
2233 nd
->nd_gss_context
= cp
;
2237 nfs_gss_svc_ctx_deref(cp
);
2242 * Insert the server's verifier into the RPC reply header.
2243 * It contains a signed checksum of the sequence number that
2244 * was received in the RPC call.
2245 * Then go on to add integrity or privacy if necessary.
2248 nfs_gss_svc_verf_put(struct nfsrv_descript
*nd
, struct nfsm_chain
*nmc
)
2250 struct nfs_gss_svc_ctx
*cp
;
2252 u_char tokbuf
[KRB5_SZ_TOKEN(MAX_DIGEST
)];
2254 u_char cksum
[MAX_DIGEST
];
2257 cp
= nd
->nd_gss_context
;
2258 ki
= &cp
->gss_svc_kinfo
;
2260 if (cp
->gss_svc_major
!= GSS_S_COMPLETE
) {
2262 * If the context isn't yet complete
2263 * then return a null verifier.
2265 nfsm_chain_add_32(error
, nmc
, RPCAUTH_NULL
);
2266 nfsm_chain_add_32(error
, nmc
, 0);
2271 * Compute checksum of the request seq number
2272 * If it's the final reply of context setup
2273 * then return the checksum of the context
2276 if (cp
->gss_svc_proc
== RPCSEC_GSS_INIT
||
2277 cp
->gss_svc_proc
== RPCSEC_GSS_CONTINUE_INIT
)
2278 nfs_gss_cksum_rep(ki
, cp
->gss_svc_seqwin
, cksum
);
2280 nfs_gss_cksum_rep(ki
, nd
->nd_gss_seqnum
, cksum
);
2282 * Now wrap it in a token and add
2283 * the verifier to the reply.
2285 toklen
= nfs_gss_token_put(ki
, ALG_MIC(ki
), tokbuf
, 0, 0, cksum
);
2286 nfsm_chain_add_32(error
, nmc
, RPCSEC_GSS
);
2287 nfsm_chain_add_32(error
, nmc
, toklen
);
2288 nfsm_chain_add_opaque(error
, nmc
, tokbuf
, toklen
);
2294 * The results aren't available yet, but if they need to be
2295 * checksummed for integrity protection or encrypted, then
2296 * we can record the start offset here, insert a place-holder
2297 * for the results length, as well as the sequence number.
2298 * The rest of the work is done later by nfs_gss_svc_protect_reply()
2299 * when the results are available.
2302 nfs_gss_svc_prepare_reply(struct nfsrv_descript
*nd
, struct nfsm_chain
*nmc
)
2304 struct nfs_gss_svc_ctx
*cp
= nd
->nd_gss_context
;
2307 if (cp
->gss_svc_proc
== RPCSEC_GSS_INIT
||
2308 cp
->gss_svc_proc
== RPCSEC_GSS_CONTINUE_INIT
)
2311 switch (nd
->nd_sec
) {
2316 nd
->nd_gss_mb
= nmc
->nmc_mcur
; // record current mbuf
2317 nfsm_chain_finish_mbuf(error
, nmc
); // split the chain here
2318 nfsm_chain_add_32(error
, nmc
, nd
->nd_gss_seqnum
); // req sequence number
2321 nd
->nd_gss_mb
= nmc
->nmc_mcur
; // record current mbuf
2322 nfsm_chain_finish_mbuf(error
, nmc
); // split the chain here
2323 nfsm_chain_add_32(error
, nmc
, random()); // confounder bytes 1-4
2324 nfsm_chain_add_32(error
, nmc
, random()); // confounder bytes 5-8
2325 nfsm_chain_add_32(error
, nmc
, nd
->nd_gss_seqnum
); // req sequence number
2333 * The results are checksummed or encrypted for return to the client
2336 nfs_gss_svc_protect_reply(struct nfsrv_descript
*nd
, mbuf_t mrep
)
2338 struct nfs_gss_svc_ctx
*cp
= nd
->nd_gss_context
;
2339 struct nfsm_chain nmrep_res
, *nmc_res
= &nmrep_res
;
2340 struct nfsm_chain nmrep_pre
, *nmc_pre
= &nmrep_pre
;
2343 u_char tokbuf
[KRB5_SZ_TOKMAX(MAX_DIGEST
)];
2345 u_char cksum
[MAX_DIGEST
];
2347 gss_key_info
*ki
= &cp
->gss_svc_kinfo
;
2350 * Using a reference to the mbuf where we previously split the reply
2351 * mbuf chain, we split the mbuf chain argument into two mbuf chains,
2352 * one that allows us to prepend a length field or token, (nmc_pre)
2353 * and the second which holds just the results that we're going to
2354 * checksum and/or encrypt. When we're done, we join the chains back
2357 nfs_gss_nfsm_chain(nmc_res
, mrep
); // set up the results chain
2358 mb
= nd
->nd_gss_mb
; // the mbuf where we split
2359 results
= mbuf_next(mb
); // first mbuf in the results
2360 reslen
= nfs_gss_mchain_length(results
); // length of results
2361 error
= mbuf_setnext(mb
, NULL
); // disconnect the chains
2364 nfs_gss_nfsm_chain(nmc_pre
, mb
); // set up the prepend chain
2366 if (nd
->nd_sec
== RPCAUTH_KRB5I
) {
2367 nfsm_chain_add_32(error
, nmc_pre
, reslen
);
2368 nfsm_chain_build_done(error
, nmc_pre
);
2371 nfs_gss_append_chain(nmc_pre
, results
); // Append the results mbufs
2373 /* Now compute the checksum over the results data */
2374 nfs_gss_cksum_mchain(ki
, results
, ALG_MIC(ki
), 0, reslen
, cksum
);
2376 /* Put it into a token and append to the request */
2377 toklen
= nfs_gss_token_put(ki
, ALG_MIC(ki
), tokbuf
, 0, 0, cksum
);
2378 nfsm_chain_add_32(error
, nmc_res
, toklen
);
2379 nfsm_chain_add_opaque(error
, nmc_res
, tokbuf
, toklen
);
2380 nfsm_chain_build_done(error
, nmc_res
);
2384 * Append a pad trailer - per RFC 1964 section 1.2.2.3
2385 * Since XDR data is always 32-bit aligned, it
2386 * needs to be padded either by 4 bytes or 8 bytes.
2388 if (reslen
% 8 > 0) {
2389 nfsm_chain_add_32(error
, nmc_res
, 0x04040404);
2390 reslen
+= NFSX_UNSIGNED
;
2392 nfsm_chain_add_32(error
, nmc_res
, 0x08080808);
2393 nfsm_chain_add_32(error
, nmc_res
, 0x08080808);
2394 reslen
+= 2 * NFSX_UNSIGNED
;
2396 nfsm_chain_build_done(error
, nmc_res
);
2398 /* Now compute the checksum over the results data */
2399 nfs_gss_cksum_mchain(ki
, results
, ALG_WRAP(ki
), 0, reslen
, cksum
);
2401 /* Put it into a token and insert in the reply */
2402 toklen
= nfs_gss_token_put(ki
, ALG_WRAP(ki
), tokbuf
, 0, reslen
, cksum
);
2403 nfsm_chain_add_32(error
, nmc_pre
, toklen
+ reslen
);
2404 nfsm_chain_add_opaque_nopad(error
, nmc_pre
, tokbuf
, toklen
);
2405 nfsm_chain_build_done(error
, nmc_pre
);
2408 nfs_gss_append_chain(nmc_pre
, results
); // Append the results mbufs
2410 /* Encrypt the confounder + seqnum + results */
2411 nfs_gss_encrypt_mchain(ki
, results
, 0, reslen
, DES_ENCRYPT
);
2413 /* Add null XDR pad if the ASN.1 token misaligned the data */
2414 pad
= nfsm_pad(toklen
+ reslen
);
2416 nfsm_chain_add_opaque_nopad(error
, nmc_pre
, iv0
, pad
);
2417 nfsm_chain_build_done(error
, nmc_pre
);
2425 * This function handles the context setup calls from the client.
2426 * Essentially, it implements the NFS null procedure calls when
2427 * an RPCSEC_GSS credential is used.
2428 * This is the context maintenance function. It creates and
2429 * destroys server contexts at the whim of the client.
2430 * During context creation, it receives GSS-API tokens from the
2431 * client, passes them up to gssd, and returns a received token
2432 * back to the client in the null procedure reply.
2435 nfs_gss_svc_ctx_init(struct nfsrv_descript
*nd
, struct nfsrv_sock
*slp
, mbuf_t
*mrepp
)
2437 struct nfs_gss_svc_ctx
*cp
= NULL
;
2440 struct nfsm_chain
*nmreq
, nmrep
;
2443 nmreq
= &nd
->nd_nmreq
;
2444 nfsm_chain_null(&nmrep
);
2446 cp
= nd
->nd_gss_context
;
2449 switch (cp
->gss_svc_proc
) {
2450 case RPCSEC_GSS_INIT
:
2451 nfs_gss_svc_ctx_insert(cp
);
2454 case RPCSEC_GSS_CONTINUE_INIT
:
2455 /* Get the token from the request */
2456 nfsm_chain_get_32(error
, nmreq
, cp
->gss_svc_tokenlen
);
2457 if (cp
->gss_svc_tokenlen
== 0) {
2458 autherr
= RPCSEC_GSS_CREDPROBLEM
;
2461 MALLOC(cp
->gss_svc_token
, u_char
*, cp
->gss_svc_tokenlen
, M_TEMP
, M_WAITOK
);
2462 if (cp
->gss_svc_token
== NULL
) {
2463 autherr
= RPCSEC_GSS_CREDPROBLEM
;
2466 nfsm_chain_get_opaque(error
, nmreq
, cp
->gss_svc_tokenlen
, cp
->gss_svc_token
);
2468 /* Use the token in a gss_accept_sec_context upcall */
2469 error
= nfs_gss_svc_gssd_upcall(cp
);
2471 autherr
= RPCSEC_GSS_CREDPROBLEM
;
2472 if (error
== NFSERR_EAUTH
)
2478 * If the context isn't complete, pass the new token
2479 * back to the client for another round.
2481 if (cp
->gss_svc_major
!= GSS_S_COMPLETE
)
2485 * Now the server context is complete.
2488 clock_get_uptime(&cp
->gss_svc_incarnation
);
2490 cp
->gss_svc_seqwin
= GSS_SVC_SEQWINDOW
;
2491 MALLOC(cp
->gss_svc_seqbits
, uint32_t *,
2492 nfsm_rndup((cp
->gss_svc_seqwin
+ 7) / 8), M_TEMP
, M_WAITOK
|M_ZERO
);
2493 if (cp
->gss_svc_seqbits
== NULL
) {
2494 autherr
= RPCSEC_GSS_CREDPROBLEM
;
2499 case RPCSEC_GSS_DATA
:
2500 /* Just a nullproc ping - do nothing */
2503 case RPCSEC_GSS_DESTROY
:
2505 * Don't destroy the context immediately because
2506 * other active requests might still be using it.
2507 * Instead, schedule it for destruction after
2508 * GSS_CTX_PEND time has elapsed.
2510 cp
= nfs_gss_svc_ctx_find(cp
->gss_svc_handle
);
2512 cp
->gss_svc_handle
= 0; // so it can't be found
2513 lck_mtx_lock(cp
->gss_svc_mtx
);
2514 clock_interval_to_deadline(GSS_CTX_PEND
, NSEC_PER_SEC
,
2515 &cp
->gss_svc_incarnation
);
2516 lck_mtx_unlock(cp
->gss_svc_mtx
);
2520 autherr
= RPCSEC_GSS_CREDPROBLEM
;
2524 /* Now build the reply */
2526 if (nd
->nd_repstat
== 0)
2527 nd
->nd_repstat
= autherr
? (NFSERR_AUTHERR
| autherr
) : NFSERR_RETVOID
;
2528 sz
= 7 * NFSX_UNSIGNED
+ nfsm_rndup(cp
->gss_svc_tokenlen
); // size of results
2529 error
= nfsrv_rephead(nd
, slp
, &nmrep
, sz
);
2530 *mrepp
= nmrep
.nmc_mhead
;
2531 if (error
|| autherr
)
2534 if (cp
->gss_svc_proc
== RPCSEC_GSS_INIT
||
2535 cp
->gss_svc_proc
== RPCSEC_GSS_CONTINUE_INIT
) {
2536 nfsm_chain_add_32(error
, &nmrep
, sizeof(cp
->gss_svc_handle
));
2537 nfsm_chain_add_32(error
, &nmrep
, cp
->gss_svc_handle
);
2539 nfsm_chain_add_32(error
, &nmrep
, cp
->gss_svc_major
);
2540 nfsm_chain_add_32(error
, &nmrep
, cp
->gss_svc_minor
);
2541 nfsm_chain_add_32(error
, &nmrep
, cp
->gss_svc_seqwin
);
2543 nfsm_chain_add_32(error
, &nmrep
, cp
->gss_svc_tokenlen
);
2544 if (cp
->gss_svc_token
!= NULL
) {
2545 nfsm_chain_add_opaque(error
, &nmrep
, cp
->gss_svc_token
, cp
->gss_svc_tokenlen
);
2546 FREE(cp
->gss_svc_token
, M_TEMP
);
2547 cp
->gss_svc_token
= NULL
;
2553 nd
->nd_gss_context
= NULL
;
2554 LIST_REMOVE(cp
, gss_svc_entries
);
2555 if (cp
->gss_svc_seqbits
!= NULL
)
2556 FREE(cp
->gss_svc_seqbits
, M_TEMP
);
2557 if (cp
->gss_svc_token
!= NULL
)
2558 FREE(cp
->gss_svc_token
, M_TEMP
);
2559 lck_mtx_destroy(cp
->gss_svc_mtx
, nfs_gss_svc_grp
);
2563 nfsm_chain_build_done(error
, &nmrep
);
2565 nfsm_chain_cleanup(&nmrep
);
2572 * This is almost a mirror-image of the client side upcall.
2573 * It passes and receives a token, but invokes gss_accept_sec_context.
2574 * If it's the final call of the context setup, then gssd also returns
2575 * the session key and the user's UID.
2578 nfs_gss_svc_gssd_upcall(struct nfs_gss_svc_ctx
*cp
)
2583 gssd_byte_buffer okey
= NULL
;
2584 uint32_t skeylen
= 0;
2586 vm_map_copy_t itoken
= NULL
;
2587 gssd_byte_buffer otoken
= NULL
;
2588 mach_msg_type_number_t otokenlen
;
2590 char svcname
[] = "nfs";
2592 kr
= host_get_gssd_port(host_priv_self(), &mp
);
2593 if (kr
!= KERN_SUCCESS
) {
2594 printf("nfs_gss_svc_gssd_upcall: can't get gssd port, status %x (%d)\n", kr
, kr
);
2597 if (!IPC_PORT_VALID(mp
)) {
2598 printf("nfs_gss_svc_gssd_upcall: gssd port not valid\n");
2602 if (cp
->gss_svc_tokenlen
> 0)
2603 nfs_gss_mach_alloc_buffer(cp
->gss_svc_token
, cp
->gss_svc_tokenlen
, &itoken
);
2606 kr
= mach_gss_accept_sec_context(
2608 (gssd_byte_buffer
) itoken
, (mach_msg_type_number_t
) cp
->gss_svc_tokenlen
,
2611 &cp
->gss_svc_context
,
2612 &cp
->gss_svc_cred_handle
,
2616 &cp
->gss_svc_ngroups
,
2617 &okey
, (mach_msg_type_number_t
*) &skeylen
,
2618 &otoken
, &otokenlen
,
2620 &cp
->gss_svc_minor
);
2622 if (kr
!= KERN_SUCCESS
) {
2623 printf("nfs_gss_svc_gssd_upcall failed: %x (%d)\n", kr
, kr
);
2624 if (kr
== MIG_SERVER_DIED
&& cp
->gss_svc_context
== 0 &&
2625 retry_cnt
++ < NFS_GSS_MACH_MAX_RETRIES
) {
2626 if (cp
->gss_svc_tokenlen
> 0)
2627 nfs_gss_mach_alloc_buffer(cp
->gss_svc_token
, cp
->gss_svc_tokenlen
, &itoken
);
2630 host_release_special_port(mp
);
2634 host_release_special_port(mp
);
2637 if (skeylen
!= SKEYLEN
&& skeylen
!= SKEYLEN3
) {
2638 printf("nfs_gss_svc_gssd_upcall: bad key length (%d)\n", skeylen
);
2639 vm_map_copy_discard((vm_map_copy_t
) okey
);
2640 vm_map_copy_discard((vm_map_copy_t
) otoken
);
2643 error
= nfs_gss_mach_vmcopyout((vm_map_copy_t
) okey
, skeylen
, cp
->gss_svc_kinfo
.skey
);
2645 vm_map_copy_discard((vm_map_copy_t
) otoken
);
2648 error
= gss_key_init(&cp
->gss_svc_kinfo
, skeylen
);
2654 /* Free context token used as input */
2655 if (cp
->gss_svc_token
)
2656 FREE(cp
->gss_svc_token
, M_TEMP
);
2657 cp
->gss_svc_token
= NULL
;
2658 cp
->gss_svc_tokenlen
= 0;
2660 if (otokenlen
> 0) {
2661 /* Set context token to gss output token */
2662 MALLOC(cp
->gss_svc_token
, u_char
*, otokenlen
, M_TEMP
, M_WAITOK
);
2663 if (cp
->gss_svc_token
== NULL
) {
2664 printf("nfs_gss_svc_gssd_upcall: could not allocate %d bytes\n", otokenlen
);
2665 vm_map_copy_discard((vm_map_copy_t
) otoken
);
2668 error
= nfs_gss_mach_vmcopyout((vm_map_copy_t
) otoken
, otokenlen
, cp
->gss_svc_token
);
2670 FREE(cp
->gss_svc_token
, M_TEMP
);
2671 cp
->gss_svc_token
= NULL
;
2672 return (NFSERR_EAUTH
);
2674 cp
->gss_svc_tokenlen
= otokenlen
;
2680 FREE(cp
->gss_svc_token
, M_TEMP
);
2681 cp
->gss_svc_tokenlen
= 0;
2682 cp
->gss_svc_token
= NULL
;
2684 return (NFSERR_EAUTH
);
2688 * Validate the sequence number in the credential as described
2689 * in RFC 2203 Section 5.3.3.1
2691 * Here the window of valid sequence numbers is represented by
2692 * a bitmap. As each sequence number is received, its bit is
2693 * set in the bitmap. An invalid sequence number lies below
2694 * the lower bound of the window, or is within the window but
2695 * has its bit already set.
2698 nfs_gss_svc_seqnum_valid(struct nfs_gss_svc_ctx
*cp
, uint32_t seq
)
2700 uint32_t *bits
= cp
->gss_svc_seqbits
;
2701 uint32_t win
= cp
->gss_svc_seqwin
;
2704 lck_mtx_lock(cp
->gss_svc_mtx
);
2707 * If greater than the window upper bound,
2708 * move the window up, and set the bit.
2710 if (seq
> cp
->gss_svc_seqmax
) {
2711 if (seq
- cp
->gss_svc_seqmax
> win
)
2712 bzero(bits
, nfsm_rndup((win
+ 7) / 8));
2714 for (i
= cp
->gss_svc_seqmax
+ 1; i
< seq
; i
++)
2715 win_resetbit(bits
, i
% win
);
2716 win_setbit(bits
, seq
% win
);
2717 cp
->gss_svc_seqmax
= seq
;
2718 lck_mtx_unlock(cp
->gss_svc_mtx
);
2723 * Invalid if below the lower bound of the window
2725 if (seq
<= cp
->gss_svc_seqmax
- win
) {
2726 lck_mtx_unlock(cp
->gss_svc_mtx
);
2731 * In the window, invalid if the bit is already set
2733 if (win_getbit(bits
, seq
% win
)) {
2734 lck_mtx_unlock(cp
->gss_svc_mtx
);
2737 win_setbit(bits
, seq
% win
);
2738 lck_mtx_unlock(cp
->gss_svc_mtx
);
2743 * Drop a reference to a context
2745 * Note that it's OK for the context to exist
2746 * with a refcount of zero. The refcount isn't
2747 * checked until we're about to reap an expired one.
2750 nfs_gss_svc_ctx_deref(struct nfs_gss_svc_ctx
*cp
)
2752 lck_mtx_lock(cp
->gss_svc_mtx
);
2753 if (cp
->gss_svc_refcnt
> 0)
2754 cp
->gss_svc_refcnt
--;
2756 printf("nfs_gss_ctx_deref: zero refcount\n");
2757 lck_mtx_unlock(cp
->gss_svc_mtx
);
2761 * Called at NFS server shutdown - destroy all contexts
2764 nfs_gss_svc_cleanup(void)
2766 struct nfs_gss_svc_ctx_hashhead
*head
;
2767 struct nfs_gss_svc_ctx
*cp
, *ncp
;
2770 lck_mtx_lock(nfs_gss_svc_ctx_mutex
);
2773 * Run through all the buckets
2775 for (i
= 0; i
< SVC_CTX_HASHSZ
; i
++) {
2777 * Remove and free all entries in the bucket
2779 head
= &nfs_gss_svc_ctx_hashtbl
[i
];
2780 LIST_FOREACH_SAFE(cp
, head
, gss_svc_entries
, ncp
) {
2781 LIST_REMOVE(cp
, gss_svc_entries
);
2782 if (cp
->gss_svc_seqbits
)
2783 FREE(cp
->gss_svc_seqbits
, M_TEMP
);
2784 lck_mtx_destroy(cp
->gss_svc_mtx
, nfs_gss_svc_grp
);
2789 lck_mtx_unlock(nfs_gss_svc_ctx_mutex
);
2792 #endif /* NFSSERVER */
2796 * The following functions are used by both client and server.
2800 * Release a host special port that was obtained by host_get_special_port
2801 * or one of its macros (host_get_gssd_port in this case).
2802 * This really should be in a public kpi.
2805 /* This should be in a public header if this routine is not */
2806 extern void ipc_port_release_send(ipc_port_t
);
2807 extern ipc_port_t
ipc_port_copy_send(ipc_port_t
);
2810 host_release_special_port(mach_port_t mp
)
2812 if (IPC_PORT_VALID(mp
))
2813 ipc_port_release_send(mp
);
2817 host_copy_special_port(mach_port_t mp
)
2819 return (ipc_port_copy_send(mp
));
2823 * The token that is sent and received in the gssd upcall
2824 * has unbounded variable length. Mach RPC does not pass
2825 * the token in-line. Instead it uses page mapping to handle
2826 * these parameters. This function allocates a VM buffer
2827 * to hold the token for an upcall and copies the token
2828 * (received from the client) into it. The VM buffer is
2829 * marked with a src_destroy flag so that the upcall will
2830 * automatically de-allocate the buffer when the upcall is
2834 nfs_gss_mach_alloc_buffer(u_char
*buf
, uint32_t buflen
, vm_map_copy_t
*addr
)
2837 vm_offset_t kmem_buf
;
2841 if (buf
== NULL
|| buflen
== 0)
2844 tbuflen
= round_page(buflen
);
2845 kr
= vm_allocate(ipc_kernel_map
, &kmem_buf
, tbuflen
, VM_FLAGS_ANYWHERE
);
2847 printf("nfs_gss_mach_alloc_buffer: vm_allocate failed\n");
2851 kr
= vm_map_wire(ipc_kernel_map
, vm_map_trunc_page(kmem_buf
),
2852 vm_map_round_page(kmem_buf
+ tbuflen
),
2853 VM_PROT_READ
|VM_PROT_WRITE
, FALSE
);
2855 printf("nfs_gss_mach_alloc_buffer: vm_map_wire failed\n");
2859 bcopy(buf
, (void *) kmem_buf
, buflen
);
2860 // Shouldn't need to bzero below since vm_allocate returns zeroed pages
2861 // bzero(kmem_buf + buflen, tbuflen - buflen);
2863 kr
= vm_map_unwire(ipc_kernel_map
, vm_map_trunc_page(kmem_buf
),
2864 vm_map_round_page(kmem_buf
+ tbuflen
), FALSE
);
2866 printf("nfs_gss_mach_alloc_buffer: vm_map_unwire failed\n");
2870 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
) kmem_buf
,
2871 (vm_map_size_t
) buflen
, TRUE
, addr
);
2873 printf("nfs_gss_mach_alloc_buffer: vm_map_copyin failed\n");
2879 * Here we handle a token received from the gssd via an upcall.
2880 * The received token resides in an allocate VM buffer.
2881 * We copy the token out of this buffer to a chunk of malloc'ed
2882 * memory of the right size, then de-allocate the VM buffer.
2885 nfs_gss_mach_vmcopyout(vm_map_copy_t in
, uint32_t len
, u_char
*out
)
2887 vm_map_offset_t map_data
;
2891 error
= vm_map_copyout(ipc_kernel_map
, &map_data
, in
);
2895 data
= CAST_DOWN(vm_offset_t
, map_data
);
2896 bcopy((void *) data
, out
, len
);
2897 vm_deallocate(ipc_kernel_map
, data
, len
);
2903 * Encode an ASN.1 token to be wrapped in an RPCSEC_GSS verifier.
2904 * Returns the size of the token, since it contains a variable
2905 * length DER encoded size field.
2916 static uint32_t seqnum
= 0;
2922 * Fill in the token header: 2 octets.
2923 * This is 0x06 - an ASN.1 tag for APPLICATION, 0, SEQUENCE
2924 * followed by the length of the token: 35 + 0 octets for a
2925 * MIC token, or 35 + encrypted octets for a wrap token;
2928 toklen
= KRB5_SZ_MECH
+ KRB5_SZ_ALG
+ KRB5_SZ_SEQ
+ HASHLEN(ki
);
2929 nfs_gss_der_length_put(&p
, toklen
+ datalen
);
2932 * Fill in the DER encoded mech OID for Kerberos v5.
2933 * This represents the Kerberos OID 1.2.840.113554.1.2.2
2934 * described in RFC 2623, section 4.2
2936 bcopy(krb5_mech
, p
, sizeof(krb5_mech
));
2937 p
+= sizeof(krb5_mech
);
2940 * Now at the token described in RFC 1964, section 1.2.1
2941 * Fill in the token ID, integrity algorithm indicator,
2942 * for DES MAC MD5, and four filler octets.
2943 * The alg string encodes the bytes to represent either
2944 * a MIC token or a WRAP token for Kerberos.
2946 bcopy(alg
, p
, KRB5_SZ_ALG
);
2950 * Now encode the sequence number according to
2951 * RFC 1964, section 1.2.1.2 which dictates 4 octets
2952 * of sequence number followed by 4 bytes of direction
2953 * indicator: 0x00 for initiator or 0xff for acceptor.
2954 * We DES CBC encrypt the sequence number using the first
2955 * 8 octets of the checksum field as an initialization
2957 * Note that this sequence number is not at all related
2958 * to the RPCSEC_GSS protocol sequence number. This
2959 * number is private to the ASN.1 token. The only
2960 * requirement is that it not be repeated in case the
2961 * server has replay detection on, which normally should
2962 * not be the case, since RFC 2203 section 5.2.3 says that
2963 * replay detection and sequence checking must be turned off.
2966 for (i
= 0; i
< 4; i
++)
2967 plain
[i
] = (u_char
) ((seqnum
>> (i
* 8)) & 0xff);
2968 for (i
= 4; i
< 8; i
++)
2969 plain
[i
] = initiator
? 0x00 : 0xff;
2970 gss_des_crypt(ki
, (des_cblock
*) plain
, (des_cblock
*) p
, 8,
2971 (des_cblock
*) cksum
, NULL
, DES_ENCRYPT
, KG_USAGE_SEQ
);
2975 * Finally, append the octets of the
2976 * checksum of the alg + plaintext data.
2977 * The plaintext could be an RPC call header,
2978 * the window value, or a sequence number.
2980 bcopy(cksum
, p
, HASHLEN(ki
));
2987 * Determine size of ASN.1 DER length
2990 nfs_gss_der_length_size(int len
)
2993 len
< (1 << 7) ? 1 :
2994 len
< (1 << 8) ? 2 :
2995 len
< (1 << 16) ? 3 :
2996 len
< (1 << 24) ? 4 : 5;
3000 * Encode an ASN.1 DER length field
3003 nfs_gss_der_length_put(u_char
**pp
, int len
)
3005 int sz
= nfs_gss_der_length_size(len
);
3009 *p
++ = (u_char
) len
;
3011 *p
++ = (u_char
) ((sz
-1) | 0x80);
3014 *p
++ = (u_char
) ((len
>> (sz
* 8)) & 0xff);
3021 * Decode an ASN.1 DER length field
3024 nfs_gss_der_length_get(u_char
**pp
)
3027 uint32_t flen
, len
= 0;
3031 if ((*p
++ & 0x80) == 0)
3034 if (flen
> sizeof(uint32_t))
3037 len
= (len
<< 8) + *p
++;
3044 * Decode an ASN.1 token from an RPCSEC_GSS verifier.
3060 * Check that we have a valid token header
3063 return (AUTH_BADCRED
);
3064 (void) nfs_gss_der_length_get(&p
); // ignore the size
3067 * Check that we have the DER encoded Kerberos v5 mech OID
3069 if (bcmp(p
, krb5_mech
, sizeof(krb5_mech
) != 0))
3070 return (AUTH_BADCRED
);
3071 p
+= sizeof(krb5_mech
);
3074 * Now check the token ID, DES MAC MD5 algorithm
3075 * indicator, and filler octets.
3077 if (bcmp(p
, alg
, KRB5_SZ_ALG
) != 0)
3078 return (AUTH_BADCRED
);
3082 * Now decrypt the sequence number.
3083 * Note that the gss decryption uses the first 8 octets
3084 * of the checksum field as an initialization vector (p + 8).
3085 * Per RFC 2203 section 5.2.2 we don't check the sequence number
3086 * in the ASN.1 token because the RPCSEC_GSS protocol has its
3087 * own sequence number described in section 5.3.3.1
3090 gss_des_crypt(ki
, (des_cblock
*)p
, (des_cblock
*) plain
, 8,
3091 (des_cblock
*) (p
+ 8), NULL
, DES_DECRYPT
, KG_USAGE_SEQ
);
3093 for (i
= 0; i
< 4; i
++)
3094 seqnum
|= plain
[i
] << (i
* 8);
3097 * Make sure the direction
3098 * indicator octets are correct.
3100 d
= initiator
? 0x00 : 0xff;
3101 for (i
= 4; i
< 8; i
++)
3103 return (AUTH_BADCRED
);
3106 * Finally, get the checksum
3108 bcopy(p
, cksum
, HASHLEN(ki
));
3118 * Return the number of bytes in an mbuf chain.
3121 nfs_gss_mchain_length(mbuf_t mhead
)
3126 for (mb
= mhead
; mb
; mb
= mbuf_next(mb
))
3127 len
+= mbuf_len(mb
);
3133 * Append an args or results mbuf chain to the header chain
3136 nfs_gss_append_chain(struct nfsm_chain
*nmc
, mbuf_t mc
)
3141 /* Connect the mbuf chains */
3142 error
= mbuf_setnext(nmc
->nmc_mcur
, mc
);
3146 /* Find the last mbuf in the chain */
3148 for (mb
= mc
; mb
; mb
= mbuf_next(mb
))
3151 nmc
->nmc_mcur
= tail
;
3152 nmc
->nmc_ptr
= (caddr_t
) mbuf_data(tail
) + mbuf_len(tail
);
3153 nmc
->nmc_left
= mbuf_trailingspace(tail
);
3159 * Convert an mbuf chain to an NFS mbuf chain
3162 nfs_gss_nfsm_chain(struct nfsm_chain
*nmc
, mbuf_t mc
)
3166 /* Find the last mbuf in the chain */
3168 for (mb
= mc
; mb
; mb
= mbuf_next(mb
))
3171 nmc
->nmc_mhead
= mc
;
3172 nmc
->nmc_mcur
= tail
;
3173 nmc
->nmc_ptr
= (caddr_t
) mbuf_data(tail
) + mbuf_len(tail
);
3174 nmc
->nmc_left
= mbuf_trailingspace(tail
);
3180 * Compute a checksum over an mbuf chain.
3181 * Start building an MD5 digest at the given offset and keep
3182 * going until the end of data in the current mbuf is reached.
3183 * Then convert the 16 byte MD5 digest to an 8 byte DES CBC
3187 nfs_gss_cksum_mchain(
3198 GSS_DIGEST_CTX context
;
3200 gss_digest_Init(&context
, ki
);
3203 * Logically prepend the first 8 bytes of the algorithm
3204 * field as required by RFC 1964, section 1.2.1.1
3206 gss_digest_Update(&context
, alg
, KRB5_SZ_ALG
);
3209 * Move down the mbuf chain until we reach the given
3210 * byte offset, then start MD5 on the mbuf data until
3211 * we've done len bytes.
3214 for (mb
= mhead
; mb
&& len
> 0; mb
= mbuf_next(mb
)) {
3215 ptr
= mbuf_data(mb
);
3216 left
= mbuf_len(mb
);
3217 if (offset
>= left
) {
3218 /* Offset not yet reached */
3222 /* At or beyond offset - checksum data */
3227 bytes
= left
< len
? left
: len
;
3229 gss_digest_Update(&context
, ptr
, bytes
);
3233 gss_digest_Final(&context
, digest
);
3237 * Compute a checksum over an NFS mbuf chain.
3238 * Start building an MD5 digest at the given offset and keep
3239 * going until the end of data in the current mbuf is reached.
3240 * Then convert the 16 byte MD5 digest to an 8 byte DES CBC
3244 nfs_gss_cksum_chain(
3246 struct nfsm_chain
*nmc
,
3253 * If the length parameter is zero, then we need
3254 * to use the length from the offset to the current
3255 * encode/decode offset.
3258 len
= nfsm_chain_offset(nmc
) - offset
;
3260 return (nfs_gss_cksum_mchain(ki
, nmc
->nmc_mhead
, alg
, offset
, len
, cksum
));
3264 * Compute a checksum of the sequence number (or sequence window)
3265 * of an RPCSEC_GSS reply.
3268 nfs_gss_cksum_rep(gss_key_info
*ki
, uint32_t seqnum
, u_char
*cksum
)
3270 GSS_DIGEST_CTX context
;
3271 uint32_t val
= htonl(seqnum
);
3273 gss_digest_Init(&context
, ki
);
3276 * Logically prepend the first 8 bytes of the MIC
3277 * token as required by RFC 1964, section 1.2.1.1
3279 gss_digest_Update(&context
, ALG_MIC(ki
), KRB5_SZ_ALG
);
3282 * Compute the digest of the seqnum in network order
3284 gss_digest_Update(&context
, &val
, 4);
3285 gss_digest_Final(&context
, cksum
);
3289 * Encrypt or decrypt data in an mbuf chain with des-cbc.
3292 nfs_gss_encrypt_mchain(
3301 u_char tmp
[8], ivec
[8];
3302 int left
, left8
, remain
;
3308 * Move down the mbuf chain until we reach the given
3309 * byte offset, then start encrypting the mbuf data until
3310 * we've done len bytes.
3313 for (mb
= mhead
; mb
&& len
> 0; mb
= mbn
) {
3314 mbn
= mbuf_next(mb
);
3315 ptr
= mbuf_data(mb
);
3316 left
= mbuf_len(mb
);
3317 if (offset
>= left
) {
3318 /* Offset not yet reached */
3322 /* At or beyond offset - encrypt data */
3328 * DES or DES3 CBC has to encrypt 8 bytes at a time.
3329 * If the number of bytes to be encrypted in this
3330 * mbuf isn't some multiple of 8 bytes, encrypt all
3331 * the 8 byte blocks, then combine the remaining
3332 * bytes with enough from the next mbuf to make up
3333 * an 8 byte block and encrypt that block separately,
3334 * i.e. that block is split across two mbufs.
3337 left8
= left
- remain
;
3338 left
= left8
< len
? left8
: len
;
3340 gss_des_crypt(ki
, (des_cblock
*) ptr
, (des_cblock
*) ptr
,
3341 left
, &ivec
, &ivec
, encrypt
, KG_USAGE_SEAL
);
3345 if (mbn
&& remain
> 0) {
3346 nptr
= mbuf_data(mbn
);
3347 offset
= 8 - remain
;
3348 bcopy(ptr
+ left
, tmp
, remain
); // grab from this mbuf
3349 bcopy(nptr
, tmp
+ remain
, offset
); // grab from next mbuf
3350 gss_des_crypt(ki
, (des_cblock
*) tmp
, (des_cblock
*) tmp
, 8,
3351 &ivec
, &ivec
, encrypt
, KG_USAGE_SEAL
);
3352 bcopy(tmp
, ptr
+ left
, remain
); // return to this mbuf
3353 bcopy(tmp
+ remain
, nptr
, offset
); // return to next mbuf
3360 * Encrypt or decrypt data in an NFS mbuf chain with des-cbc.
3363 nfs_gss_encrypt_chain(
3365 struct nfsm_chain
*nmc
,
3371 * If the length parameter is zero, then we need
3372 * to use the length from the offset to the current
3373 * encode/decode offset.
3376 len
= nfsm_chain_offset(nmc
) - offset
;
3378 return (nfs_gss_encrypt_mchain(ki
, nmc
->nmc_mhead
, offset
, len
, encrypt
));
3382 * The routines that follow provide abstractions for doing digests and crypto.
3386 gss_digest_Init(GSS_DIGEST_CTX
*ctx
, gss_key_info
*ki
)
3388 ctx
->type
= ki
->type
;
3390 case NFS_GSS_1DES
: MD5_DESCBC_Init(&ctx
->m_ctx
, &ki
->ks_u
.des
.gss_sched
);
3392 case NFS_GSS_3DES
: HMAC_SHA1_DES3KD_Init(&ctx
->h_ctx
, ki
->ks_u
.des3
.ckey
, 0);
3395 printf("gss_digest_Init: Unknown key info type %d\n", ki
->type
);
3400 gss_digest_Update(GSS_DIGEST_CTX
*ctx
, void *data
, size_t len
)
3402 switch (ctx
->type
) {
3403 case NFS_GSS_1DES
: MD5_DESCBC_Update(&ctx
->m_ctx
, data
, len
);
3405 case NFS_GSS_3DES
: HMAC_SHA1_DES3KD_Update(&ctx
->h_ctx
, data
, len
);
3411 gss_digest_Final(GSS_DIGEST_CTX
*ctx
, void *digest
)
3413 switch (ctx
->type
) {
3414 case NFS_GSS_1DES
: MD5_DESCBC_Final(digest
, &ctx
->m_ctx
);
3416 case NFS_GSS_3DES
: HMAC_SHA1_DES3KD_Final(digest
, &ctx
->h_ctx
);
3422 gss_des_crypt(gss_key_info
*ki
, des_cblock
*in
, des_cblock
*out
,
3423 int32_t len
, des_cblock
*iv
, des_cblock
*retiv
, int encrypt
, int usage
)
3428 des_cbc_key_schedule
*sched
= ((usage
== KG_USAGE_SEAL
) ?
3429 &ki
->ks_u
.des
.gss_sched_Ke
:
3430 &ki
->ks_u
.des
.gss_sched
);
3431 des_cbc_encrypt(in
, out
, len
, sched
, iv
, retiv
, encrypt
);
3436 des3_cbc_encrypt(in
, out
, len
, &ki
->ks_u
.des3
.gss_sched
, iv
, retiv
, encrypt
);
3442 gss_key_init(gss_key_info
*ki
, uint32_t skeylen
)
3448 ki
->keybytes
= skeylen
;
3450 case sizeof(des_cblock
):
3451 ki
->type
= NFS_GSS_1DES
;
3452 ki
->hash_len
= MD5_DESCBC_DIGEST_LENGTH
;
3453 ki
->ks_u
.des
.key
= (des_cblock
*)ki
->skey
;
3454 rc
= des_cbc_key_sched(ki
->ks_u
.des
.key
, &ki
->ks_u
.des
.gss_sched
);
3457 for (i
= 0; i
< ki
->keybytes
; i
++)
3458 k
[0][i
] = 0xf0 ^ (*ki
->ks_u
.des
.key
)[i
];
3459 rc
= des_cbc_key_sched(&k
[0], &ki
->ks_u
.des
.gss_sched_Ke
);
3461 case 3*sizeof(des_cblock
):
3462 ki
->type
= NFS_GSS_3DES
;
3463 ki
->hash_len
= SHA_DIGEST_LENGTH
;
3464 ki
->ks_u
.des3
.key
= (des_cblock (*)[3])ki
->skey
;
3465 des3_derive_key(*ki
->ks_u
.des3
.key
, ki
->ks_u
.des3
.ckey
,
3466 KEY_USAGE_DES3_SIGN
, KEY_USAGE_LEN
);
3467 rc
= des3_cbc_key_sched(*ki
->ks_u
.des3
.key
, &ki
->ks_u
.des3
.gss_sched
);
3472 printf("gss_key_init: Invalid key length %d\n", skeylen
);
3481 #define DISPLAYLEN 16
3482 #define MAXDISPLAYLEN 256
3485 hexdump(const char *msg
, void *data
, size_t len
)
3489 char *p
, disbuf
[3*DISPLAYLEN
+1];
3491 printf("NFS DEBUG %s len=%d:\n", msg
, (uint32_t)len
);
3492 if (len
> MAXDISPLAYLEN
)
3493 len
= MAXDISPLAYLEN
;
3495 for (i
= 0; i
< len
; i
+= DISPLAYLEN
) {
3496 for (p
= disbuf
, j
= 0; (j
+ i
) < len
&& j
< DISPLAYLEN
; j
++, p
+= 3)
3497 snprintf(p
, 4, "%02x ", d
[i
+ j
]);
3498 printf("\t%s\n", disbuf
);