xref: /freebsd/lib/libc/rpc/auth_des.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
12e322d37SHiroki Sato /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
42e322d37SHiroki Sato  * Copyright (c) 2009, Sun Microsystems, Inc.
52e322d37SHiroki Sato  * All rights reserved.
6e8636dfdSBill Paul  *
72e322d37SHiroki Sato  * Redistribution and use in source and binary forms, with or without
82e322d37SHiroki Sato  * modification, are permitted provided that the following conditions are met:
92e322d37SHiroki Sato  * - Redistributions of source code must retain the above copyright notice,
102e322d37SHiroki Sato  *   this list of conditions and the following disclaimer.
112e322d37SHiroki Sato  * - Redistributions in binary form must reproduce the above copyright notice,
122e322d37SHiroki Sato  *   this list of conditions and the following disclaimer in the documentation
132e322d37SHiroki Sato  *   and/or other materials provided with the distribution.
142e322d37SHiroki Sato  * - Neither the name of Sun Microsystems, Inc. nor the names of its
152e322d37SHiroki Sato  *   contributors may be used to endorse or promote products derived
162e322d37SHiroki Sato  *   from this software without specific prior written permission.
17e8636dfdSBill Paul  *
182e322d37SHiroki Sato  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
192e322d37SHiroki Sato  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202e322d37SHiroki Sato  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212e322d37SHiroki Sato  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
222e322d37SHiroki Sato  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
232e322d37SHiroki Sato  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
242e322d37SHiroki Sato  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
252e322d37SHiroki Sato  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
262e322d37SHiroki Sato  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
272e322d37SHiroki Sato  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
282e322d37SHiroki Sato  * POSSIBILITY OF SUCH DAMAGE.
29e8636dfdSBill Paul  */
30e8636dfdSBill Paul /*
31e8636dfdSBill Paul  * Copyright (c) 1988 by Sun Microsystems, Inc.
32e8636dfdSBill Paul  */
33e8636dfdSBill Paul /*
34e8636dfdSBill Paul  * auth_des.c, client-side implementation of DES authentication
35e8636dfdSBill Paul  */
36d3d20c82SDavid E. O'Brien 
378360efbdSAlfred Perlstein #include "namespace.h"
389f5afc13SIan Dowse #include "reentrant.h"
398360efbdSAlfred Perlstein #include <err.h>
408360efbdSAlfred Perlstein #include <errno.h>
41e8636dfdSBill Paul #include <string.h>
42e8636dfdSBill Paul #include <stdlib.h>
43e8636dfdSBill Paul #include <unistd.h>
44e8636dfdSBill Paul #include <rpc/des_crypt.h>
458360efbdSAlfred Perlstein #include <syslog.h>
46e8636dfdSBill Paul #include <rpc/types.h>
47e8636dfdSBill Paul #include <rpc/auth.h>
48e8636dfdSBill Paul #include <rpc/auth_des.h>
498360efbdSAlfred Perlstein #include <rpc/clnt.h>
508360efbdSAlfred Perlstein #include <rpc/xdr.h>
51e8636dfdSBill Paul #include <sys/socket.h>
52e8636dfdSBill Paul #undef NIS
53e8636dfdSBill Paul #include <rpcsvc/nis.h>
548360efbdSAlfred Perlstein #include "un-namespace.h"
55235baf26SDaniel Eischen #include "mt_misc.h"
56e8636dfdSBill Paul 
578360efbdSAlfred Perlstein #define USEC_PER_SEC		1000000
58e8636dfdSBill Paul #define RTIME_TIMEOUT		5	/* seconds to wait for sync */
59e8636dfdSBill Paul 
60e8636dfdSBill Paul #define AUTH_PRIVATE(auth)	(struct ad_private *) auth->ah_private
61e8636dfdSBill Paul #define ALLOC(object_type)	(object_type *) mem_alloc(sizeof(object_type))
62e8636dfdSBill Paul #define FREE(ptr, size)		mem_free((char *)(ptr), (int) size)
63e8636dfdSBill Paul #define ATTEMPT(xdr_op)		if (!(xdr_op)) return (FALSE)
64e8636dfdSBill Paul 
658360efbdSAlfred Perlstein extern bool_t xdr_authdes_cred( XDR *, struct authdes_cred *);
668360efbdSAlfred Perlstein extern bool_t xdr_authdes_verf( XDR *, struct authdes_verf *);
6777601543SCraig Rodrigues extern int key_encryptsession_pk(char *, netobj *, des_block *);
688360efbdSAlfred Perlstein 
698360efbdSAlfred Perlstein extern bool_t __rpc_get_time_offset(struct timeval *, nis_server *, char *,
708360efbdSAlfred Perlstein 	char **, char **);
71e8636dfdSBill Paul 
72e8636dfdSBill Paul /*
73e8636dfdSBill Paul  * DES authenticator operations vector
74e8636dfdSBill Paul  */
758360efbdSAlfred Perlstein static void	authdes_nextverf(AUTH *);
768360efbdSAlfred Perlstein static bool_t	authdes_marshal(AUTH *, XDR *);
778360efbdSAlfred Perlstein static bool_t	authdes_validate(AUTH *, struct opaque_auth *);
788360efbdSAlfred Perlstein static bool_t	authdes_refresh(AUTH *, void *);
798360efbdSAlfred Perlstein static void	authdes_destroy(AUTH *);
808360efbdSAlfred Perlstein 
818360efbdSAlfred Perlstein static struct auth_ops *authdes_ops(void);
828360efbdSAlfred Perlstein 
83e8636dfdSBill Paul /*
84e8636dfdSBill Paul  * This struct is pointed to by the ah_private field of an "AUTH *"
85e8636dfdSBill Paul  */
86e8636dfdSBill Paul struct ad_private {
87e8636dfdSBill Paul 	char *ad_fullname; 		/* client's full name */
88e8636dfdSBill Paul 	u_int ad_fullnamelen;		/* length of name, rounded up */
89e8636dfdSBill Paul 	char *ad_servername; 		/* server's full name */
90e8636dfdSBill Paul 	u_int ad_servernamelen;		/* length of name, rounded up */
91e8636dfdSBill Paul 	u_int ad_window;	  	/* client specified window */
92e8636dfdSBill Paul 	bool_t ad_dosync;		/* synchronize? */
938360efbdSAlfred Perlstein 	struct netbuf ad_syncaddr;	/* remote host to synch with */
94e8636dfdSBill Paul 	char *ad_timehost;		/* remote host to synch with */
95e8636dfdSBill Paul 	struct timeval ad_timediff;	/* server's time - client's time */
968360efbdSAlfred Perlstein 	u_int ad_nickname;		/* server's nickname for client */
97e8636dfdSBill Paul 	struct authdes_cred ad_cred;	/* storage for credential */
98e8636dfdSBill Paul 	struct authdes_verf ad_verf;	/* storage for verifier */
99e8636dfdSBill Paul 	struct timeval ad_timestamp;	/* timestamp sent */
100e8636dfdSBill Paul 	des_block ad_xkey;		/* encrypted conversation key */
101e8636dfdSBill Paul 	u_char ad_pkey[1024];		/* Server's actual public key */
102e8636dfdSBill Paul 	char *ad_netid;			/* Timehost netid */
103e8636dfdSBill Paul 	char *ad_uaddr;			/* Timehost uaddr */
104e8636dfdSBill Paul 	nis_server *ad_nis_srvr;	/* NIS+ server struct */
105e8636dfdSBill Paul };
106e8636dfdSBill Paul 
1078360efbdSAlfred Perlstein AUTH *authdes_pk_seccreate(const char *, netobj *, u_int, const char *,
1088360efbdSAlfred Perlstein 	const des_block *, nis_server *);
109e8636dfdSBill Paul 
110e8636dfdSBill Paul /*
1118360efbdSAlfred Perlstein  * documented version of authdes_seccreate
112e8636dfdSBill Paul  */
1138360efbdSAlfred Perlstein /*
1148360efbdSAlfred Perlstein 	servername:	network name of server
1158360efbdSAlfred Perlstein 	win:		time to live
1168360efbdSAlfred Perlstein 	timehost:	optional hostname to sync with
1178360efbdSAlfred Perlstein 	ckey:		optional conversation key to use
1188360efbdSAlfred Perlstein */
1198360efbdSAlfred Perlstein 
120e8636dfdSBill Paul AUTH *
authdes_seccreate(const char * servername,const u_int win,const char * timehost,const des_block * ckey)1218360efbdSAlfred Perlstein authdes_seccreate(const char *servername, const u_int win,
1228360efbdSAlfred Perlstein 	const char *timehost, const des_block *ckey)
123e8636dfdSBill Paul {
124e8636dfdSBill Paul 	u_char  pkey_data[1024];
1258360efbdSAlfred Perlstein 	netobj  pkey;
1268360efbdSAlfred Perlstein 	AUTH    *dummy;
127e8636dfdSBill Paul 
1288360efbdSAlfred Perlstein 	if (! getpublickey(servername, (char *) pkey_data)) {
1298360efbdSAlfred Perlstein 		syslog(LOG_ERR,
1308360efbdSAlfred Perlstein 		    "authdes_seccreate: no public key found for %s",
1318360efbdSAlfred Perlstein 		    servername);
132e8636dfdSBill Paul 		return (NULL);
133e8636dfdSBill Paul 	}
134e8636dfdSBill Paul 
1358360efbdSAlfred Perlstein 	pkey.n_bytes = (char *) pkey_data;
1368360efbdSAlfred Perlstein 	pkey.n_len = (u_int)strlen((char *)pkey_data) + 1;
1378360efbdSAlfred Perlstein 	dummy = authdes_pk_seccreate(servername, &pkey, win, timehost,
1388360efbdSAlfred Perlstein 	    ckey, NULL);
1398360efbdSAlfred Perlstein 	return (dummy);
1408360efbdSAlfred Perlstein }
1418360efbdSAlfred Perlstein 
142e8636dfdSBill Paul /*
1438360efbdSAlfred Perlstein  * Slightly modified version of authdessec_create which takes the public key
144e8636dfdSBill Paul  * of the server principal as an argument. This spares us a call to
145e8636dfdSBill Paul  * getpublickey() which in the nameserver context can cause a deadlock.
146e8636dfdSBill Paul  */
147e8636dfdSBill Paul AUTH *
authdes_pk_seccreate(const char * servername,netobj * pkey,u_int window,const char * timehost,const des_block * ckey,nis_server * srvr)1488360efbdSAlfred Perlstein authdes_pk_seccreate(const char *servername, netobj *pkey, u_int window,
1498360efbdSAlfred Perlstein 	const char *timehost, const des_block *ckey, nis_server *srvr)
150e8636dfdSBill Paul {
151e8636dfdSBill Paul 	AUTH *auth;
152e8636dfdSBill Paul 	struct ad_private *ad;
153e8636dfdSBill Paul 	char namebuf[MAXNETNAMELEN+1];
154e8636dfdSBill Paul 
155e8636dfdSBill Paul 	/*
156e8636dfdSBill Paul 	 * Allocate everything now
157e8636dfdSBill Paul 	 */
158e8636dfdSBill Paul 	auth = ALLOC(AUTH);
159e8636dfdSBill Paul 	if (auth == NULL) {
1608360efbdSAlfred Perlstein 		syslog(LOG_ERR, "authdes_pk_seccreate: out of memory");
161e8636dfdSBill Paul 		return (NULL);
162e8636dfdSBill Paul 	}
163e8636dfdSBill Paul 	ad = ALLOC(struct ad_private);
164e8636dfdSBill Paul 	if (ad == NULL) {
1658360efbdSAlfred Perlstein 		syslog(LOG_ERR, "authdes_pk_seccreate: out of memory");
166e8636dfdSBill Paul 		goto failed;
167e8636dfdSBill Paul 	}
168e8636dfdSBill Paul 	ad->ad_fullname = ad->ad_servername = NULL; /* Sanity reasons */
169e8636dfdSBill Paul 	ad->ad_timehost = NULL;
170e8636dfdSBill Paul 	ad->ad_netid = NULL;
171e8636dfdSBill Paul 	ad->ad_uaddr = NULL;
172e8636dfdSBill Paul 	ad->ad_nis_srvr = NULL;
173e8636dfdSBill Paul 	ad->ad_timediff.tv_sec = 0;
174e8636dfdSBill Paul 	ad->ad_timediff.tv_usec = 0;
175e8636dfdSBill Paul 	memcpy(ad->ad_pkey, pkey->n_bytes, pkey->n_len);
176e8636dfdSBill Paul 	if (!getnetname(namebuf))
177e8636dfdSBill Paul 		goto failed;
178e8636dfdSBill Paul 	ad->ad_fullnamelen = RNDUP((u_int) strlen(namebuf));
179e8636dfdSBill Paul 	ad->ad_fullname = (char *)mem_alloc(ad->ad_fullnamelen + 1);
180e8636dfdSBill Paul 	ad->ad_servernamelen = strlen(servername);
181e8636dfdSBill Paul 	ad->ad_servername = (char *)mem_alloc(ad->ad_servernamelen + 1);
182e8636dfdSBill Paul 
183e8636dfdSBill Paul 	if (ad->ad_fullname == NULL || ad->ad_servername == NULL) {
1848360efbdSAlfred Perlstein 		syslog(LOG_ERR, "authdes_seccreate: out of memory");
185e8636dfdSBill Paul 		goto failed;
186e8636dfdSBill Paul 	}
187e8636dfdSBill Paul 	if (timehost != NULL) {
188e8636dfdSBill Paul 		ad->ad_timehost = (char *)mem_alloc(strlen(timehost) + 1);
189e8636dfdSBill Paul 		if (ad->ad_timehost == NULL) {
1908360efbdSAlfred Perlstein 			syslog(LOG_ERR, "authdes_seccreate: out of memory");
191e8636dfdSBill Paul 			goto failed;
192e8636dfdSBill Paul 		}
193e8636dfdSBill Paul 		memcpy(ad->ad_timehost, timehost, strlen(timehost) + 1);
194e8636dfdSBill Paul 		ad->ad_dosync = TRUE;
195e8636dfdSBill Paul 	} else if (srvr != NULL) {
196e8636dfdSBill Paul 		ad->ad_nis_srvr = srvr;	/* transient */
197e8636dfdSBill Paul 		ad->ad_dosync = TRUE;
198e8636dfdSBill Paul 	} else {
199e8636dfdSBill Paul 		ad->ad_dosync = FALSE;
200e8636dfdSBill Paul 	}
201e8636dfdSBill Paul 	memcpy(ad->ad_fullname, namebuf, ad->ad_fullnamelen + 1);
202e8636dfdSBill Paul 	memcpy(ad->ad_servername, servername, ad->ad_servernamelen + 1);
203e8636dfdSBill Paul 	ad->ad_window = window;
204e8636dfdSBill Paul 	if (ckey == NULL) {
205e8636dfdSBill Paul 		if (key_gendes(&auth->ah_key) < 0) {
2068360efbdSAlfred Perlstein 			syslog(LOG_ERR,
2078360efbdSAlfred Perlstein 	    "authdes_seccreate: keyserv(1m) is unable to generate session key");
208e8636dfdSBill Paul 			goto failed;
209e8636dfdSBill Paul 		}
210e8636dfdSBill Paul 	} else {
211e8636dfdSBill Paul 		auth->ah_key = *ckey;
212e8636dfdSBill Paul 	}
213e8636dfdSBill Paul 
214e8636dfdSBill Paul 	/*
215e8636dfdSBill Paul 	 * Set up auth handle
216e8636dfdSBill Paul 	 */
217e8636dfdSBill Paul 	auth->ah_cred.oa_flavor = AUTH_DES;
218e8636dfdSBill Paul 	auth->ah_verf.oa_flavor = AUTH_DES;
2198360efbdSAlfred Perlstein 	auth->ah_ops = authdes_ops();
220e8636dfdSBill Paul 	auth->ah_private = (caddr_t)ad;
221e8636dfdSBill Paul 
2228360efbdSAlfred Perlstein 	if (!authdes_refresh(auth, NULL)) {
223e8636dfdSBill Paul 		goto failed;
224e8636dfdSBill Paul 	}
225e8636dfdSBill Paul 	ad->ad_nis_srvr = NULL; /* not needed any longer */
226e8636dfdSBill Paul 	return (auth);
227e8636dfdSBill Paul 
228e8636dfdSBill Paul failed:
229e8636dfdSBill Paul 	if (auth)
230e8636dfdSBill Paul 		FREE(auth, sizeof (AUTH));
231e8636dfdSBill Paul 	if (ad) {
232e8636dfdSBill Paul 		if (ad->ad_fullname)
233e8636dfdSBill Paul 			FREE(ad->ad_fullname, ad->ad_fullnamelen + 1);
234e8636dfdSBill Paul 		if (ad->ad_servername)
235e8636dfdSBill Paul 			FREE(ad->ad_servername, ad->ad_servernamelen + 1);
236e8636dfdSBill Paul 		if (ad->ad_timehost)
237e8636dfdSBill Paul 			FREE(ad->ad_timehost, strlen(ad->ad_timehost) + 1);
238e8636dfdSBill Paul 		if (ad->ad_netid)
2398360efbdSAlfred Perlstein 			FREE(ad->ad_netid, strlen(ad->ad_netid) + 1);
240e8636dfdSBill Paul 		if (ad->ad_uaddr)
2418360efbdSAlfred Perlstein 			FREE(ad->ad_uaddr, strlen(ad->ad_uaddr) + 1);
242e8636dfdSBill Paul 		FREE(ad, sizeof (struct ad_private));
243e8636dfdSBill Paul 	}
244e8636dfdSBill Paul 	return (NULL);
245e8636dfdSBill Paul }
2468360efbdSAlfred Perlstein 
247e8636dfdSBill Paul /*
248e8636dfdSBill Paul  * Implement the five authentication operations
249e8636dfdSBill Paul  */
250e8636dfdSBill Paul 
251e8636dfdSBill Paul 
252e8636dfdSBill Paul /*
253e8636dfdSBill Paul  * 1. Next Verifier
254e8636dfdSBill Paul  */
255e8636dfdSBill Paul /*ARGSUSED*/
256e8636dfdSBill Paul static void
authdes_nextverf(AUTH * auth __unused)257084bc321SCraig Rodrigues authdes_nextverf(AUTH *auth __unused)
258e8636dfdSBill Paul {
259e8636dfdSBill Paul 	/* what the heck am I supposed to do??? */
260e8636dfdSBill Paul }
261e8636dfdSBill Paul 
262e8636dfdSBill Paul 
263e8636dfdSBill Paul /*
264e8636dfdSBill Paul  * 2. Marshal
265e8636dfdSBill Paul  */
266e8636dfdSBill Paul static bool_t
authdes_marshal(AUTH * auth,XDR * xdrs)2678360efbdSAlfred Perlstein authdes_marshal(AUTH *auth, XDR *xdrs)
268e8636dfdSBill Paul {
2698360efbdSAlfred Perlstein /* LINTED pointer alignment */
270e8636dfdSBill Paul 	struct ad_private *ad = AUTH_PRIVATE(auth);
271e8636dfdSBill Paul 	struct authdes_cred *cred = &ad->ad_cred;
272e8636dfdSBill Paul 	struct authdes_verf *verf = &ad->ad_verf;
273e8636dfdSBill Paul 	des_block cryptbuf[2];
274e8636dfdSBill Paul 	des_block ivec;
275e8636dfdSBill Paul 	int status;
2768360efbdSAlfred Perlstein 	int len;
2778fb3f3f6SDavid E. O'Brien 	rpc_inline_t *ixdr;
278e8636dfdSBill Paul 
279e8636dfdSBill Paul 	/*
280e8636dfdSBill Paul 	 * Figure out the "time", accounting for any time difference
281e8636dfdSBill Paul 	 * with the server if necessary.
282e8636dfdSBill Paul 	 */
283902d9eafSEd Schouten 	(void)gettimeofday(&ad->ad_timestamp, NULL);
284e8636dfdSBill Paul 	ad->ad_timestamp.tv_sec += ad->ad_timediff.tv_sec;
285e8636dfdSBill Paul 	ad->ad_timestamp.tv_usec += ad->ad_timediff.tv_usec;
2868360efbdSAlfred Perlstein 	while (ad->ad_timestamp.tv_usec >= USEC_PER_SEC) {
2878360efbdSAlfred Perlstein 		ad->ad_timestamp.tv_usec -= USEC_PER_SEC;
2888360efbdSAlfred Perlstein 		ad->ad_timestamp.tv_sec++;
289e8636dfdSBill Paul 	}
290e8636dfdSBill Paul 
291e8636dfdSBill Paul 	/*
292e8636dfdSBill Paul 	 * XDR the timestamp and possibly some other things, then
293e8636dfdSBill Paul 	 * encrypt them.
294e8636dfdSBill Paul 	 */
2958360efbdSAlfred Perlstein 	ixdr = (rpc_inline_t *)cryptbuf;
2968360efbdSAlfred Perlstein 	IXDR_PUT_INT32(ixdr, ad->ad_timestamp.tv_sec);
2978360efbdSAlfred Perlstein 	IXDR_PUT_INT32(ixdr, ad->ad_timestamp.tv_usec);
298e8636dfdSBill Paul 	if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
2998360efbdSAlfred Perlstein 		IXDR_PUT_U_INT32(ixdr, ad->ad_window);
3008360efbdSAlfred Perlstein 		IXDR_PUT_U_INT32(ixdr, ad->ad_window - 1);
301e8636dfdSBill Paul 		ivec.key.high = ivec.key.low = 0;
302e8636dfdSBill Paul 		status = cbc_crypt((char *)&auth->ah_key, (char *)cryptbuf,
3038360efbdSAlfred Perlstein 			(u_int) 2 * sizeof (des_block),
3048360efbdSAlfred Perlstein 			DES_ENCRYPT | DES_HW, (char *)&ivec);
305e8636dfdSBill Paul 	} else {
306e8636dfdSBill Paul 		status = ecb_crypt((char *)&auth->ah_key, (char *)cryptbuf,
3078360efbdSAlfred Perlstein 			(u_int) sizeof (des_block),
3088360efbdSAlfred Perlstein 			DES_ENCRYPT | DES_HW);
309e8636dfdSBill Paul 	}
310e8636dfdSBill Paul 	if (DES_FAILED(status)) {
3118360efbdSAlfred Perlstein 		syslog(LOG_ERR, "authdes_marshal: DES encryption failure");
312e8636dfdSBill Paul 		return (FALSE);
313e8636dfdSBill Paul 	}
314e8636dfdSBill Paul 	ad->ad_verf.adv_xtimestamp = cryptbuf[0];
315e8636dfdSBill Paul 	if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
316e8636dfdSBill Paul 		ad->ad_cred.adc_fullname.window = cryptbuf[1].key.high;
317e8636dfdSBill Paul 		ad->ad_verf.adv_winverf = cryptbuf[1].key.low;
318e8636dfdSBill Paul 	} else {
319e8636dfdSBill Paul 		ad->ad_cred.adc_nickname = ad->ad_nickname;
320e8636dfdSBill Paul 		ad->ad_verf.adv_winverf = 0;
321e8636dfdSBill Paul 	}
322e8636dfdSBill Paul 
323e8636dfdSBill Paul 	/*
324e8636dfdSBill Paul 	 * Serialize the credential and verifier into opaque
325e8636dfdSBill Paul 	 * authentication data.
326e8636dfdSBill Paul 	 */
327e8636dfdSBill Paul 	if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
328e8636dfdSBill Paul 		len = ((1 + 1 + 2 + 1)*BYTES_PER_XDR_UNIT + ad->ad_fullnamelen);
329e8636dfdSBill Paul 	} else {
330e8636dfdSBill Paul 		len = (1 + 1)*BYTES_PER_XDR_UNIT;
331e8636dfdSBill Paul 	}
332e8636dfdSBill Paul 
333e8636dfdSBill Paul 	if ((ixdr = xdr_inline(xdrs, 2*BYTES_PER_XDR_UNIT))) {
3348360efbdSAlfred Perlstein 		IXDR_PUT_INT32(ixdr, AUTH_DES);
3358360efbdSAlfred Perlstein 		IXDR_PUT_INT32(ixdr, len);
336e8636dfdSBill Paul 	} else {
3378360efbdSAlfred Perlstein 		ATTEMPT(xdr_putint32(xdrs, (int *)&auth->ah_cred.oa_flavor));
3388360efbdSAlfred Perlstein 		ATTEMPT(xdr_putint32(xdrs, &len));
339e8636dfdSBill Paul 	}
340e8636dfdSBill Paul 	ATTEMPT(xdr_authdes_cred(xdrs, cred));
341e8636dfdSBill Paul 
342e8636dfdSBill Paul 	len = (2 + 1)*BYTES_PER_XDR_UNIT;
343e8636dfdSBill Paul 	if ((ixdr = xdr_inline(xdrs, 2*BYTES_PER_XDR_UNIT))) {
3448360efbdSAlfred Perlstein 		IXDR_PUT_INT32(ixdr, AUTH_DES);
3458360efbdSAlfred Perlstein 		IXDR_PUT_INT32(ixdr, len);
346e8636dfdSBill Paul 	} else {
3478360efbdSAlfred Perlstein 		ATTEMPT(xdr_putint32(xdrs, (int *)&auth->ah_verf.oa_flavor));
3488360efbdSAlfred Perlstein 		ATTEMPT(xdr_putint32(xdrs, &len));
349e8636dfdSBill Paul 	}
350e8636dfdSBill Paul 	ATTEMPT(xdr_authdes_verf(xdrs, verf));
351e8636dfdSBill Paul 	return (TRUE);
352e8636dfdSBill Paul }
353e8636dfdSBill Paul 
354e8636dfdSBill Paul 
355e8636dfdSBill Paul /*
356e8636dfdSBill Paul  * 3. Validate
357e8636dfdSBill Paul  */
358e8636dfdSBill Paul static bool_t
authdes_validate(AUTH * auth,struct opaque_auth * rverf)3598360efbdSAlfred Perlstein authdes_validate(AUTH *auth, struct opaque_auth *rverf)
360e8636dfdSBill Paul {
3618360efbdSAlfred Perlstein /* LINTED pointer alignment */
362e8636dfdSBill Paul 	struct ad_private *ad = AUTH_PRIVATE(auth);
363e8636dfdSBill Paul 	struct authdes_verf verf;
364e8636dfdSBill Paul 	int status;
3658fb3f3f6SDavid E. O'Brien 	uint32_t *ixdr;
3668360efbdSAlfred Perlstein 	des_block buf;
367e8636dfdSBill Paul 
368e8636dfdSBill Paul 	if (rverf->oa_length != (2 + 1) * BYTES_PER_XDR_UNIT) {
369e8636dfdSBill Paul 		return (FALSE);
370e8636dfdSBill Paul 	}
3718360efbdSAlfred Perlstein /* LINTED pointer alignment */
3728360efbdSAlfred Perlstein 	ixdr = (uint32_t *)rverf->oa_base;
3738360efbdSAlfred Perlstein 	buf.key.high = (uint32_t)*ixdr++;
3748360efbdSAlfred Perlstein 	buf.key.low = (uint32_t)*ixdr++;
3758360efbdSAlfred Perlstein 	verf.adv_int_u = (uint32_t)*ixdr++;
376e8636dfdSBill Paul 
377e8636dfdSBill Paul 	/*
378e8636dfdSBill Paul 	 * Decrypt the timestamp
379e8636dfdSBill Paul 	 */
3808360efbdSAlfred Perlstein 	status = ecb_crypt((char *)&auth->ah_key, (char *)&buf,
3818360efbdSAlfred Perlstein 		(u_int)sizeof (des_block), DES_DECRYPT | DES_HW);
382e8636dfdSBill Paul 
383e8636dfdSBill Paul 	if (DES_FAILED(status)) {
3848360efbdSAlfred Perlstein 		syslog(LOG_ERR, "authdes_validate: DES decryption failure");
385e8636dfdSBill Paul 		return (FALSE);
386e8636dfdSBill Paul 	}
387e8636dfdSBill Paul 
388e8636dfdSBill Paul 	/*
389e8636dfdSBill Paul 	 * xdr the decrypted timestamp
390e8636dfdSBill Paul 	 */
3918360efbdSAlfred Perlstein /* LINTED pointer alignment */
3928360efbdSAlfred Perlstein 	ixdr = (uint32_t *)buf.c;
3938360efbdSAlfred Perlstein 	verf.adv_timestamp.tv_sec = IXDR_GET_INT32(ixdr) + 1;
3948360efbdSAlfred Perlstein 	verf.adv_timestamp.tv_usec = IXDR_GET_INT32(ixdr);
395e8636dfdSBill Paul 
396e8636dfdSBill Paul 	/*
397e8636dfdSBill Paul 	 * validate
398e8636dfdSBill Paul 	 */
399e8636dfdSBill Paul 	if (bcmp((char *)&ad->ad_timestamp, (char *)&verf.adv_timestamp,
400e8636dfdSBill Paul 		 sizeof(struct timeval)) != 0) {
4018360efbdSAlfred Perlstein 		syslog(LOG_DEBUG, "authdes_validate: verifier mismatch");
402e8636dfdSBill Paul 		return (FALSE);
403e8636dfdSBill Paul 	}
404e8636dfdSBill Paul 
405e8636dfdSBill Paul 	/*
406e8636dfdSBill Paul 	 * We have a nickname now, let's use it
407e8636dfdSBill Paul 	 */
408e8636dfdSBill Paul 	ad->ad_nickname = verf.adv_nickname;
409e8636dfdSBill Paul 	ad->ad_cred.adc_namekind = ADN_NICKNAME;
410e8636dfdSBill Paul 	return (TRUE);
411e8636dfdSBill Paul }
412e8636dfdSBill Paul 
413e8636dfdSBill Paul /*
414e8636dfdSBill Paul  * 4. Refresh
415e8636dfdSBill Paul  */
4168360efbdSAlfred Perlstein /*ARGSUSED*/
417e8636dfdSBill Paul static bool_t
authdes_refresh(AUTH * auth,void * dummy __unused)418084bc321SCraig Rodrigues authdes_refresh(AUTH *auth, void *dummy __unused)
419e8636dfdSBill Paul {
4208360efbdSAlfred Perlstein /* LINTED pointer alignment */
421e8636dfdSBill Paul 	struct ad_private *ad = AUTH_PRIVATE(auth);
422e8636dfdSBill Paul 	struct authdes_cred *cred = &ad->ad_cred;
4238360efbdSAlfred Perlstein 	int		ok;
424e8636dfdSBill Paul 	netobj		pkey;
425e8636dfdSBill Paul 
4268360efbdSAlfred Perlstein 	if (ad->ad_dosync) {
4278360efbdSAlfred Perlstein                 ok = __rpc_get_time_offset(&ad->ad_timediff, ad->ad_nis_srvr,
428e8636dfdSBill Paul 		    ad->ad_timehost, &(ad->ad_uaddr),
4298360efbdSAlfred Perlstein 		    &(ad->ad_netid));
4308360efbdSAlfred Perlstein 		if (! ok) {
431e8636dfdSBill Paul 			/*
432e8636dfdSBill Paul 			 * Hope the clocks are synced!
433e8636dfdSBill Paul 			 */
434e8636dfdSBill Paul 			ad->ad_dosync = 0;
4358360efbdSAlfred Perlstein 			syslog(LOG_DEBUG,
4368360efbdSAlfred Perlstein 			    "authdes_refresh: unable to synchronize clock");
4378360efbdSAlfred Perlstein 		 }
438e8636dfdSBill Paul 	}
439e8636dfdSBill Paul 	ad->ad_xkey = auth->ah_key;
440e8636dfdSBill Paul 	pkey.n_bytes = (char *)(ad->ad_pkey);
4418360efbdSAlfred Perlstein 	pkey.n_len = (u_int)strlen((char *)ad->ad_pkey) + 1;
442e8636dfdSBill Paul 	if (key_encryptsession_pk(ad->ad_servername, &pkey, &ad->ad_xkey) < 0) {
4438360efbdSAlfred Perlstein 		syslog(LOG_INFO,
4448360efbdSAlfred Perlstein 		    "authdes_refresh: keyserv(1m) is unable to encrypt session key");
445e8636dfdSBill Paul 		return (FALSE);
446e8636dfdSBill Paul 	}
447e8636dfdSBill Paul 	cred->adc_fullname.key = ad->ad_xkey;
448e8636dfdSBill Paul 	cred->adc_namekind = ADN_FULLNAME;
449e8636dfdSBill Paul 	cred->adc_fullname.name = ad->ad_fullname;
450e8636dfdSBill Paul 	return (TRUE);
451e8636dfdSBill Paul }
452e8636dfdSBill Paul 
453e8636dfdSBill Paul 
454e8636dfdSBill Paul /*
455e8636dfdSBill Paul  * 5. Destroy
456e8636dfdSBill Paul  */
457e8636dfdSBill Paul static void
authdes_destroy(AUTH * auth)4588360efbdSAlfred Perlstein authdes_destroy(AUTH *auth)
459e8636dfdSBill Paul {
4608360efbdSAlfred Perlstein /* LINTED pointer alignment */
461e8636dfdSBill Paul 	struct ad_private *ad = AUTH_PRIVATE(auth);
462e8636dfdSBill Paul 
463e8636dfdSBill Paul 	FREE(ad->ad_fullname, ad->ad_fullnamelen + 1);
464e8636dfdSBill Paul 	FREE(ad->ad_servername, ad->ad_servernamelen + 1);
4658360efbdSAlfred Perlstein 	if (ad->ad_timehost)
4668360efbdSAlfred Perlstein 		FREE(ad->ad_timehost, strlen(ad->ad_timehost) + 1);
4678360efbdSAlfred Perlstein 	if (ad->ad_netid)
4688360efbdSAlfred Perlstein 		FREE(ad->ad_netid, strlen(ad->ad_netid) + 1);
4698360efbdSAlfred Perlstein 	if (ad->ad_uaddr)
4708360efbdSAlfred Perlstein 		FREE(ad->ad_uaddr, strlen(ad->ad_uaddr) + 1);
471e8636dfdSBill Paul 	FREE(ad, sizeof (struct ad_private));
472e8636dfdSBill Paul 	FREE(auth, sizeof(AUTH));
473e8636dfdSBill Paul }
474e8636dfdSBill Paul 
4758360efbdSAlfred Perlstein static struct auth_ops *
authdes_ops(void)4768360efbdSAlfred Perlstein authdes_ops(void)
477e8636dfdSBill Paul {
4788360efbdSAlfred Perlstein 	static struct auth_ops ops;
479e8636dfdSBill Paul 
4808360efbdSAlfred Perlstein 	/* VARIABLES PROTECTED BY ops_lock: ops */
4818360efbdSAlfred Perlstein 
4828360efbdSAlfred Perlstein 	mutex_lock(&authdes_ops_lock);
4838360efbdSAlfred Perlstein 	if (ops.ah_nextverf == NULL) {
4848360efbdSAlfred Perlstein 		ops.ah_nextverf = authdes_nextverf;
4858360efbdSAlfred Perlstein 		ops.ah_marshal = authdes_marshal;
4868360efbdSAlfred Perlstein 		ops.ah_validate = authdes_validate;
4878360efbdSAlfred Perlstein 		ops.ah_refresh = authdes_refresh;
4888360efbdSAlfred Perlstein 		ops.ah_destroy = authdes_destroy;
489e8636dfdSBill Paul         }
4908360efbdSAlfred Perlstein 	mutex_unlock(&authdes_ops_lock);
4918360efbdSAlfred Perlstein 	return (&ops);
492e8636dfdSBill Paul }
493