xref: /freebsd/lib/libc/rpc/auth_des.c (revision 7760154342d4361500e997477d3f0c683e084351)
12e322d37SHiroki Sato /*-
22e322d37SHiroki Sato  * Copyright (c) 2009, Sun Microsystems, Inc.
32e322d37SHiroki Sato  * All rights reserved.
4e8636dfdSBill Paul  *
52e322d37SHiroki Sato  * Redistribution and use in source and binary forms, with or without
62e322d37SHiroki Sato  * modification, are permitted provided that the following conditions are met:
72e322d37SHiroki Sato  * - Redistributions of source code must retain the above copyright notice,
82e322d37SHiroki Sato  *   this list of conditions and the following disclaimer.
92e322d37SHiroki Sato  * - Redistributions in binary form must reproduce the above copyright notice,
102e322d37SHiroki Sato  *   this list of conditions and the following disclaimer in the documentation
112e322d37SHiroki Sato  *   and/or other materials provided with the distribution.
122e322d37SHiroki Sato  * - Neither the name of Sun Microsystems, Inc. nor the names of its
132e322d37SHiroki Sato  *   contributors may be used to endorse or promote products derived
142e322d37SHiroki Sato  *   from this software without specific prior written permission.
15e8636dfdSBill Paul  *
162e322d37SHiroki Sato  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
172e322d37SHiroki Sato  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
182e322d37SHiroki Sato  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
192e322d37SHiroki Sato  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
202e322d37SHiroki Sato  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
212e322d37SHiroki Sato  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
222e322d37SHiroki Sato  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
232e322d37SHiroki Sato  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
242e322d37SHiroki Sato  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
252e322d37SHiroki Sato  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
262e322d37SHiroki Sato  * POSSIBILITY OF SUCH DAMAGE.
27e8636dfdSBill Paul  */
28e8636dfdSBill Paul /*
29e8636dfdSBill Paul  * Copyright (c) 1988 by Sun Microsystems, Inc.
30e8636dfdSBill Paul  */
31e8636dfdSBill Paul /*
32e8636dfdSBill Paul  * auth_des.c, client-side implementation of DES authentication
33e8636dfdSBill Paul  */
34d3d20c82SDavid E. O'Brien 
358360efbdSAlfred Perlstein #include "namespace.h"
369f5afc13SIan Dowse #include "reentrant.h"
378360efbdSAlfred Perlstein #include <err.h>
388360efbdSAlfred Perlstein #include <errno.h>
39e8636dfdSBill Paul #include <string.h>
40e8636dfdSBill Paul #include <stdlib.h>
41e8636dfdSBill Paul #include <unistd.h>
42e8636dfdSBill Paul #include <sys/cdefs.h>
43e8636dfdSBill Paul #include <rpc/des_crypt.h>
448360efbdSAlfred Perlstein #include <syslog.h>
45e8636dfdSBill Paul #include <rpc/types.h>
46e8636dfdSBill Paul #include <rpc/auth.h>
47e8636dfdSBill Paul #include <rpc/auth_des.h>
488360efbdSAlfred Perlstein #include <rpc/clnt.h>
498360efbdSAlfred Perlstein #include <rpc/xdr.h>
50e8636dfdSBill Paul #include <sys/socket.h>
51e8636dfdSBill Paul #undef NIS
52e8636dfdSBill Paul #include <rpcsvc/nis.h>
538360efbdSAlfred Perlstein #include "un-namespace.h"
54235baf26SDaniel Eischen #include "mt_misc.h"
55e8636dfdSBill Paul 
56e8636dfdSBill Paul #if defined(LIBC_SCCS) && !defined(lint)
57a986ef57SDavid E. O'Brien static char sccsid[] = 	"@(#)auth_des.c	2.2 88/07/29 4.0 RPCSRC; from 1.9 88/02/08 SMI";
58e8636dfdSBill Paul #endif
59d3d20c82SDavid E. O'Brien #include <sys/cdefs.h>
60d3d20c82SDavid E. O'Brien __FBSDID("$FreeBSD$");
61e8636dfdSBill Paul 
628360efbdSAlfred Perlstein #define USEC_PER_SEC		1000000
63e8636dfdSBill Paul #define RTIME_TIMEOUT		5	/* seconds to wait for sync */
64e8636dfdSBill Paul 
65e8636dfdSBill Paul #define AUTH_PRIVATE(auth)	(struct ad_private *) auth->ah_private
66e8636dfdSBill Paul #define ALLOC(object_type)	(object_type *) mem_alloc(sizeof(object_type))
67e8636dfdSBill Paul #define FREE(ptr, size)		mem_free((char *)(ptr), (int) size)
68e8636dfdSBill Paul #define ATTEMPT(xdr_op)		if (!(xdr_op)) return (FALSE)
69e8636dfdSBill Paul 
708360efbdSAlfred Perlstein extern bool_t xdr_authdes_cred( XDR *, struct authdes_cred *);
718360efbdSAlfred Perlstein extern bool_t xdr_authdes_verf( XDR *, struct authdes_verf *);
72*77601543SCraig Rodrigues extern int key_encryptsession_pk(char *, netobj *, des_block *);
738360efbdSAlfred Perlstein 
748360efbdSAlfred Perlstein extern bool_t __rpc_get_time_offset(struct timeval *, nis_server *, char *,
758360efbdSAlfred Perlstein 	char **, char **);
76e8636dfdSBill Paul 
77e8636dfdSBill Paul /*
78e8636dfdSBill Paul  * DES authenticator operations vector
79e8636dfdSBill Paul  */
808360efbdSAlfred Perlstein static void	authdes_nextverf(AUTH *);
818360efbdSAlfred Perlstein static bool_t	authdes_marshal(AUTH *, XDR *);
828360efbdSAlfred Perlstein static bool_t	authdes_validate(AUTH *, struct opaque_auth *);
838360efbdSAlfred Perlstein static bool_t	authdes_refresh(AUTH *, void *);
848360efbdSAlfred Perlstein static void	authdes_destroy(AUTH *);
858360efbdSAlfred Perlstein 
868360efbdSAlfred Perlstein static struct auth_ops *authdes_ops(void);
878360efbdSAlfred Perlstein 
88e8636dfdSBill Paul /*
89e8636dfdSBill Paul  * This struct is pointed to by the ah_private field of an "AUTH *"
90e8636dfdSBill Paul  */
91e8636dfdSBill Paul struct ad_private {
92e8636dfdSBill Paul 	char *ad_fullname; 		/* client's full name */
93e8636dfdSBill Paul 	u_int ad_fullnamelen;		/* length of name, rounded up */
94e8636dfdSBill Paul 	char *ad_servername; 		/* server's full name */
95e8636dfdSBill Paul 	u_int ad_servernamelen;		/* length of name, rounded up */
96e8636dfdSBill Paul 	u_int ad_window;	  	/* client specified window */
97e8636dfdSBill Paul 	bool_t ad_dosync;		/* synchronize? */
988360efbdSAlfred Perlstein 	struct netbuf ad_syncaddr;	/* remote host to synch with */
99e8636dfdSBill Paul 	char *ad_timehost;		/* remote host to synch with */
100e8636dfdSBill Paul 	struct timeval ad_timediff;	/* server's time - client's time */
1018360efbdSAlfred Perlstein 	u_int ad_nickname;		/* server's nickname for client */
102e8636dfdSBill Paul 	struct authdes_cred ad_cred;	/* storage for credential */
103e8636dfdSBill Paul 	struct authdes_verf ad_verf;	/* storage for verifier */
104e8636dfdSBill Paul 	struct timeval ad_timestamp;	/* timestamp sent */
105e8636dfdSBill Paul 	des_block ad_xkey;		/* encrypted conversation key */
106e8636dfdSBill Paul 	u_char ad_pkey[1024];		/* Server's actual public key */
107e8636dfdSBill Paul 	char *ad_netid;			/* Timehost netid */
108e8636dfdSBill Paul 	char *ad_uaddr;			/* Timehost uaddr */
109e8636dfdSBill Paul 	nis_server *ad_nis_srvr;	/* NIS+ server struct */
110e8636dfdSBill Paul };
111e8636dfdSBill Paul 
1128360efbdSAlfred Perlstein AUTH *authdes_pk_seccreate(const char *, netobj *, u_int, const char *,
1138360efbdSAlfred Perlstein 	const des_block *, nis_server *);
114e8636dfdSBill Paul 
115e8636dfdSBill Paul /*
1168360efbdSAlfred Perlstein  * documented version of authdes_seccreate
117e8636dfdSBill Paul  */
1188360efbdSAlfred Perlstein /*
1198360efbdSAlfred Perlstein 	servername:	network name of server
1208360efbdSAlfred Perlstein 	win:		time to live
1218360efbdSAlfred Perlstein 	timehost:	optional hostname to sync with
1228360efbdSAlfred Perlstein 	ckey:		optional conversation key to use
1238360efbdSAlfred Perlstein */
1248360efbdSAlfred Perlstein 
125e8636dfdSBill Paul AUTH *
1268360efbdSAlfred Perlstein authdes_seccreate(const char *servername, const u_int win,
1278360efbdSAlfred Perlstein 	const char *timehost, const des_block *ckey)
128e8636dfdSBill Paul {
129e8636dfdSBill Paul 	u_char  pkey_data[1024];
1308360efbdSAlfred Perlstein 	netobj  pkey;
1318360efbdSAlfred Perlstein 	AUTH    *dummy;
132e8636dfdSBill Paul 
1338360efbdSAlfred Perlstein 	if (! getpublickey(servername, (char *) pkey_data)) {
1348360efbdSAlfred Perlstein 		syslog(LOG_ERR,
1358360efbdSAlfred Perlstein 		    "authdes_seccreate: no public key found for %s",
1368360efbdSAlfred Perlstein 		    servername);
137e8636dfdSBill Paul 		return (NULL);
138e8636dfdSBill Paul 	}
139e8636dfdSBill Paul 
1408360efbdSAlfred Perlstein 	pkey.n_bytes = (char *) pkey_data;
1418360efbdSAlfred Perlstein 	pkey.n_len = (u_int)strlen((char *)pkey_data) + 1;
1428360efbdSAlfred Perlstein 	dummy = authdes_pk_seccreate(servername, &pkey, win, timehost,
1438360efbdSAlfred Perlstein 	    ckey, NULL);
1448360efbdSAlfred Perlstein 	return (dummy);
1458360efbdSAlfred Perlstein }
1468360efbdSAlfred Perlstein 
147e8636dfdSBill Paul /*
1488360efbdSAlfred Perlstein  * Slightly modified version of authdessec_create which takes the public key
149e8636dfdSBill Paul  * of the server principal as an argument. This spares us a call to
150e8636dfdSBill Paul  * getpublickey() which in the nameserver context can cause a deadlock.
151e8636dfdSBill Paul  */
152e8636dfdSBill Paul AUTH *
1538360efbdSAlfred Perlstein authdes_pk_seccreate(const char *servername, netobj *pkey, u_int window,
1548360efbdSAlfred Perlstein 	const char *timehost, const des_block *ckey, nis_server *srvr)
155e8636dfdSBill Paul {
156e8636dfdSBill Paul 	AUTH *auth;
157e8636dfdSBill Paul 	struct ad_private *ad;
158e8636dfdSBill Paul 	char namebuf[MAXNETNAMELEN+1];
159e8636dfdSBill Paul 
160e8636dfdSBill Paul 	/*
161e8636dfdSBill Paul 	 * Allocate everything now
162e8636dfdSBill Paul 	 */
163e8636dfdSBill Paul 	auth = ALLOC(AUTH);
164e8636dfdSBill Paul 	if (auth == NULL) {
1658360efbdSAlfred Perlstein 		syslog(LOG_ERR, "authdes_pk_seccreate: out of memory");
166e8636dfdSBill Paul 		return (NULL);
167e8636dfdSBill Paul 	}
168e8636dfdSBill Paul 	ad = ALLOC(struct ad_private);
169e8636dfdSBill Paul 	if (ad == NULL) {
1708360efbdSAlfred Perlstein 		syslog(LOG_ERR, "authdes_pk_seccreate: out of memory");
171e8636dfdSBill Paul 		goto failed;
172e8636dfdSBill Paul 	}
173e8636dfdSBill Paul 	ad->ad_fullname = ad->ad_servername = NULL; /* Sanity reasons */
174e8636dfdSBill Paul 	ad->ad_timehost = NULL;
175e8636dfdSBill Paul 	ad->ad_netid = NULL;
176e8636dfdSBill Paul 	ad->ad_uaddr = NULL;
177e8636dfdSBill Paul 	ad->ad_nis_srvr = NULL;
178e8636dfdSBill Paul 	ad->ad_timediff.tv_sec = 0;
179e8636dfdSBill Paul 	ad->ad_timediff.tv_usec = 0;
180e8636dfdSBill Paul 	memcpy(ad->ad_pkey, pkey->n_bytes, pkey->n_len);
181e8636dfdSBill Paul 	if (!getnetname(namebuf))
182e8636dfdSBill Paul 		goto failed;
183e8636dfdSBill Paul 	ad->ad_fullnamelen = RNDUP((u_int) strlen(namebuf));
184e8636dfdSBill Paul 	ad->ad_fullname = (char *)mem_alloc(ad->ad_fullnamelen + 1);
185e8636dfdSBill Paul 	ad->ad_servernamelen = strlen(servername);
186e8636dfdSBill Paul 	ad->ad_servername = (char *)mem_alloc(ad->ad_servernamelen + 1);
187e8636dfdSBill Paul 
188e8636dfdSBill Paul 	if (ad->ad_fullname == NULL || ad->ad_servername == NULL) {
1898360efbdSAlfred Perlstein 		syslog(LOG_ERR, "authdes_seccreate: out of memory");
190e8636dfdSBill Paul 		goto failed;
191e8636dfdSBill Paul 	}
192e8636dfdSBill Paul 	if (timehost != NULL) {
193e8636dfdSBill Paul 		ad->ad_timehost = (char *)mem_alloc(strlen(timehost) + 1);
194e8636dfdSBill Paul 		if (ad->ad_timehost == NULL) {
1958360efbdSAlfred Perlstein 			syslog(LOG_ERR, "authdes_seccreate: out of memory");
196e8636dfdSBill Paul 			goto failed;
197e8636dfdSBill Paul 		}
198e8636dfdSBill Paul 		memcpy(ad->ad_timehost, timehost, strlen(timehost) + 1);
199e8636dfdSBill Paul 		ad->ad_dosync = TRUE;
200e8636dfdSBill Paul 	} else if (srvr != NULL) {
201e8636dfdSBill Paul 		ad->ad_nis_srvr = srvr;	/* transient */
202e8636dfdSBill Paul 		ad->ad_dosync = TRUE;
203e8636dfdSBill Paul 	} else {
204e8636dfdSBill Paul 		ad->ad_dosync = FALSE;
205e8636dfdSBill Paul 	}
206e8636dfdSBill Paul 	memcpy(ad->ad_fullname, namebuf, ad->ad_fullnamelen + 1);
207e8636dfdSBill Paul 	memcpy(ad->ad_servername, servername, ad->ad_servernamelen + 1);
208e8636dfdSBill Paul 	ad->ad_window = window;
209e8636dfdSBill Paul 	if (ckey == NULL) {
210e8636dfdSBill Paul 		if (key_gendes(&auth->ah_key) < 0) {
2118360efbdSAlfred Perlstein 			syslog(LOG_ERR,
2128360efbdSAlfred Perlstein 	    "authdes_seccreate: keyserv(1m) is unable to generate session key");
213e8636dfdSBill Paul 			goto failed;
214e8636dfdSBill Paul 		}
215e8636dfdSBill Paul 	} else {
216e8636dfdSBill Paul 		auth->ah_key = *ckey;
217e8636dfdSBill Paul 	}
218e8636dfdSBill Paul 
219e8636dfdSBill Paul 	/*
220e8636dfdSBill Paul 	 * Set up auth handle
221e8636dfdSBill Paul 	 */
222e8636dfdSBill Paul 	auth->ah_cred.oa_flavor = AUTH_DES;
223e8636dfdSBill Paul 	auth->ah_verf.oa_flavor = AUTH_DES;
2248360efbdSAlfred Perlstein 	auth->ah_ops = authdes_ops();
225e8636dfdSBill Paul 	auth->ah_private = (caddr_t)ad;
226e8636dfdSBill Paul 
2278360efbdSAlfred Perlstein 	if (!authdes_refresh(auth, NULL)) {
228e8636dfdSBill Paul 		goto failed;
229e8636dfdSBill Paul 	}
230e8636dfdSBill Paul 	ad->ad_nis_srvr = NULL; /* not needed any longer */
231e8636dfdSBill Paul 	return (auth);
232e8636dfdSBill Paul 
233e8636dfdSBill Paul failed:
234e8636dfdSBill Paul 	if (auth)
235e8636dfdSBill Paul 		FREE(auth, sizeof (AUTH));
236e8636dfdSBill Paul 	if (ad) {
237e8636dfdSBill Paul 		if (ad->ad_fullname)
238e8636dfdSBill Paul 			FREE(ad->ad_fullname, ad->ad_fullnamelen + 1);
239e8636dfdSBill Paul 		if (ad->ad_servername)
240e8636dfdSBill Paul 			FREE(ad->ad_servername, ad->ad_servernamelen + 1);
241e8636dfdSBill Paul 		if (ad->ad_timehost)
242e8636dfdSBill Paul 			FREE(ad->ad_timehost, strlen(ad->ad_timehost) + 1);
243e8636dfdSBill Paul 		if (ad->ad_netid)
2448360efbdSAlfred Perlstein 			FREE(ad->ad_netid, strlen(ad->ad_netid) + 1);
245e8636dfdSBill Paul 		if (ad->ad_uaddr)
2468360efbdSAlfred Perlstein 			FREE(ad->ad_uaddr, strlen(ad->ad_uaddr) + 1);
247e8636dfdSBill Paul 		FREE(ad, sizeof (struct ad_private));
248e8636dfdSBill Paul 	}
249e8636dfdSBill Paul 	return (NULL);
250e8636dfdSBill Paul }
2518360efbdSAlfred Perlstein 
252e8636dfdSBill Paul /*
253e8636dfdSBill Paul  * Implement the five authentication operations
254e8636dfdSBill Paul  */
255e8636dfdSBill Paul 
256e8636dfdSBill Paul 
257e8636dfdSBill Paul /*
258e8636dfdSBill Paul  * 1. Next Verifier
259e8636dfdSBill Paul  */
260e8636dfdSBill Paul /*ARGSUSED*/
261e8636dfdSBill Paul static void
262084bc321SCraig Rodrigues authdes_nextverf(AUTH *auth __unused)
263e8636dfdSBill Paul {
264e8636dfdSBill Paul 	/* what the heck am I supposed to do??? */
265e8636dfdSBill Paul }
266e8636dfdSBill Paul 
267e8636dfdSBill Paul 
268e8636dfdSBill Paul /*
269e8636dfdSBill Paul  * 2. Marshal
270e8636dfdSBill Paul  */
271e8636dfdSBill Paul static bool_t
2728360efbdSAlfred Perlstein authdes_marshal(AUTH *auth, XDR *xdrs)
273e8636dfdSBill Paul {
2748360efbdSAlfred Perlstein /* LINTED pointer alignment */
275e8636dfdSBill Paul 	struct ad_private *ad = AUTH_PRIVATE(auth);
276e8636dfdSBill Paul 	struct authdes_cred *cred = &ad->ad_cred;
277e8636dfdSBill Paul 	struct authdes_verf *verf = &ad->ad_verf;
278e8636dfdSBill Paul 	des_block cryptbuf[2];
279e8636dfdSBill Paul 	des_block ivec;
280e8636dfdSBill Paul 	int status;
2818360efbdSAlfred Perlstein 	int len;
2828fb3f3f6SDavid E. O'Brien 	rpc_inline_t *ixdr;
283e8636dfdSBill Paul 
284e8636dfdSBill Paul 	/*
285e8636dfdSBill Paul 	 * Figure out the "time", accounting for any time difference
286e8636dfdSBill Paul 	 * with the server if necessary.
287e8636dfdSBill Paul 	 */
288902d9eafSEd Schouten 	(void)gettimeofday(&ad->ad_timestamp, NULL);
289e8636dfdSBill Paul 	ad->ad_timestamp.tv_sec += ad->ad_timediff.tv_sec;
290e8636dfdSBill Paul 	ad->ad_timestamp.tv_usec += ad->ad_timediff.tv_usec;
2918360efbdSAlfred Perlstein 	while (ad->ad_timestamp.tv_usec >= USEC_PER_SEC) {
2928360efbdSAlfred Perlstein 		ad->ad_timestamp.tv_usec -= USEC_PER_SEC;
2938360efbdSAlfred Perlstein 		ad->ad_timestamp.tv_sec++;
294e8636dfdSBill Paul 	}
295e8636dfdSBill Paul 
296e8636dfdSBill Paul 	/*
297e8636dfdSBill Paul 	 * XDR the timestamp and possibly some other things, then
298e8636dfdSBill Paul 	 * encrypt them.
299e8636dfdSBill Paul 	 */
3008360efbdSAlfred Perlstein 	ixdr = (rpc_inline_t *)cryptbuf;
3018360efbdSAlfred Perlstein 	IXDR_PUT_INT32(ixdr, ad->ad_timestamp.tv_sec);
3028360efbdSAlfred Perlstein 	IXDR_PUT_INT32(ixdr, ad->ad_timestamp.tv_usec);
303e8636dfdSBill Paul 	if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
3048360efbdSAlfred Perlstein 		IXDR_PUT_U_INT32(ixdr, ad->ad_window);
3058360efbdSAlfred Perlstein 		IXDR_PUT_U_INT32(ixdr, ad->ad_window - 1);
306e8636dfdSBill Paul 		ivec.key.high = ivec.key.low = 0;
307e8636dfdSBill Paul 		status = cbc_crypt((char *)&auth->ah_key, (char *)cryptbuf,
3088360efbdSAlfred Perlstein 			(u_int) 2 * sizeof (des_block),
3098360efbdSAlfred Perlstein 			DES_ENCRYPT | DES_HW, (char *)&ivec);
310e8636dfdSBill Paul 	} else {
311e8636dfdSBill Paul 		status = ecb_crypt((char *)&auth->ah_key, (char *)cryptbuf,
3128360efbdSAlfred Perlstein 			(u_int) sizeof (des_block),
3138360efbdSAlfred Perlstein 			DES_ENCRYPT | DES_HW);
314e8636dfdSBill Paul 	}
315e8636dfdSBill Paul 	if (DES_FAILED(status)) {
3168360efbdSAlfred Perlstein 		syslog(LOG_ERR, "authdes_marshal: DES encryption failure");
317e8636dfdSBill Paul 		return (FALSE);
318e8636dfdSBill Paul 	}
319e8636dfdSBill Paul 	ad->ad_verf.adv_xtimestamp = cryptbuf[0];
320e8636dfdSBill Paul 	if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
321e8636dfdSBill Paul 		ad->ad_cred.adc_fullname.window = cryptbuf[1].key.high;
322e8636dfdSBill Paul 		ad->ad_verf.adv_winverf = cryptbuf[1].key.low;
323e8636dfdSBill Paul 	} else {
324e8636dfdSBill Paul 		ad->ad_cred.adc_nickname = ad->ad_nickname;
325e8636dfdSBill Paul 		ad->ad_verf.adv_winverf = 0;
326e8636dfdSBill Paul 	}
327e8636dfdSBill Paul 
328e8636dfdSBill Paul 	/*
329e8636dfdSBill Paul 	 * Serialize the credential and verifier into opaque
330e8636dfdSBill Paul 	 * authentication data.
331e8636dfdSBill Paul 	 */
332e8636dfdSBill Paul 	if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
333e8636dfdSBill Paul 		len = ((1 + 1 + 2 + 1)*BYTES_PER_XDR_UNIT + ad->ad_fullnamelen);
334e8636dfdSBill Paul 	} else {
335e8636dfdSBill Paul 		len = (1 + 1)*BYTES_PER_XDR_UNIT;
336e8636dfdSBill Paul 	}
337e8636dfdSBill Paul 
338e8636dfdSBill Paul 	if ((ixdr = xdr_inline(xdrs, 2*BYTES_PER_XDR_UNIT))) {
3398360efbdSAlfred Perlstein 		IXDR_PUT_INT32(ixdr, AUTH_DES);
3408360efbdSAlfred Perlstein 		IXDR_PUT_INT32(ixdr, len);
341e8636dfdSBill Paul 	} else {
3428360efbdSAlfred Perlstein 		ATTEMPT(xdr_putint32(xdrs, (int *)&auth->ah_cred.oa_flavor));
3438360efbdSAlfred Perlstein 		ATTEMPT(xdr_putint32(xdrs, &len));
344e8636dfdSBill Paul 	}
345e8636dfdSBill Paul 	ATTEMPT(xdr_authdes_cred(xdrs, cred));
346e8636dfdSBill Paul 
347e8636dfdSBill Paul 	len = (2 + 1)*BYTES_PER_XDR_UNIT;
348e8636dfdSBill Paul 	if ((ixdr = xdr_inline(xdrs, 2*BYTES_PER_XDR_UNIT))) {
3498360efbdSAlfred Perlstein 		IXDR_PUT_INT32(ixdr, AUTH_DES);
3508360efbdSAlfred Perlstein 		IXDR_PUT_INT32(ixdr, len);
351e8636dfdSBill Paul 	} else {
3528360efbdSAlfred Perlstein 		ATTEMPT(xdr_putint32(xdrs, (int *)&auth->ah_verf.oa_flavor));
3538360efbdSAlfred Perlstein 		ATTEMPT(xdr_putint32(xdrs, &len));
354e8636dfdSBill Paul 	}
355e8636dfdSBill Paul 	ATTEMPT(xdr_authdes_verf(xdrs, verf));
356e8636dfdSBill Paul 	return (TRUE);
357e8636dfdSBill Paul }
358e8636dfdSBill Paul 
359e8636dfdSBill Paul 
360e8636dfdSBill Paul /*
361e8636dfdSBill Paul  * 3. Validate
362e8636dfdSBill Paul  */
363e8636dfdSBill Paul static bool_t
3648360efbdSAlfred Perlstein authdes_validate(AUTH *auth, struct opaque_auth *rverf)
365e8636dfdSBill Paul {
3668360efbdSAlfred Perlstein /* LINTED pointer alignment */
367e8636dfdSBill Paul 	struct ad_private *ad = AUTH_PRIVATE(auth);
368e8636dfdSBill Paul 	struct authdes_verf verf;
369e8636dfdSBill Paul 	int status;
3708fb3f3f6SDavid E. O'Brien 	uint32_t *ixdr;
3718360efbdSAlfred Perlstein 	des_block buf;
372e8636dfdSBill Paul 
373e8636dfdSBill Paul 	if (rverf->oa_length != (2 + 1) * BYTES_PER_XDR_UNIT) {
374e8636dfdSBill Paul 		return (FALSE);
375e8636dfdSBill Paul 	}
3768360efbdSAlfred Perlstein /* LINTED pointer alignment */
3778360efbdSAlfred Perlstein 	ixdr = (uint32_t *)rverf->oa_base;
3788360efbdSAlfred Perlstein 	buf.key.high = (uint32_t)*ixdr++;
3798360efbdSAlfred Perlstein 	buf.key.low = (uint32_t)*ixdr++;
3808360efbdSAlfred Perlstein 	verf.adv_int_u = (uint32_t)*ixdr++;
381e8636dfdSBill Paul 
382e8636dfdSBill Paul 	/*
383e8636dfdSBill Paul 	 * Decrypt the timestamp
384e8636dfdSBill Paul 	 */
3858360efbdSAlfred Perlstein 	status = ecb_crypt((char *)&auth->ah_key, (char *)&buf,
3868360efbdSAlfred Perlstein 		(u_int)sizeof (des_block), DES_DECRYPT | DES_HW);
387e8636dfdSBill Paul 
388e8636dfdSBill Paul 	if (DES_FAILED(status)) {
3898360efbdSAlfred Perlstein 		syslog(LOG_ERR, "authdes_validate: DES decryption failure");
390e8636dfdSBill Paul 		return (FALSE);
391e8636dfdSBill Paul 	}
392e8636dfdSBill Paul 
393e8636dfdSBill Paul 	/*
394e8636dfdSBill Paul 	 * xdr the decrypted timestamp
395e8636dfdSBill Paul 	 */
3968360efbdSAlfred Perlstein /* LINTED pointer alignment */
3978360efbdSAlfred Perlstein 	ixdr = (uint32_t *)buf.c;
3988360efbdSAlfred Perlstein 	verf.adv_timestamp.tv_sec = IXDR_GET_INT32(ixdr) + 1;
3998360efbdSAlfred Perlstein 	verf.adv_timestamp.tv_usec = IXDR_GET_INT32(ixdr);
400e8636dfdSBill Paul 
401e8636dfdSBill Paul 	/*
402e8636dfdSBill Paul 	 * validate
403e8636dfdSBill Paul 	 */
404e8636dfdSBill Paul 	if (bcmp((char *)&ad->ad_timestamp, (char *)&verf.adv_timestamp,
405e8636dfdSBill Paul 		 sizeof(struct timeval)) != 0) {
4068360efbdSAlfred Perlstein 		syslog(LOG_DEBUG, "authdes_validate: verifier mismatch");
407e8636dfdSBill Paul 		return (FALSE);
408e8636dfdSBill Paul 	}
409e8636dfdSBill Paul 
410e8636dfdSBill Paul 	/*
411e8636dfdSBill Paul 	 * We have a nickname now, let's use it
412e8636dfdSBill Paul 	 */
413e8636dfdSBill Paul 	ad->ad_nickname = verf.adv_nickname;
414e8636dfdSBill Paul 	ad->ad_cred.adc_namekind = ADN_NICKNAME;
415e8636dfdSBill Paul 	return (TRUE);
416e8636dfdSBill Paul }
417e8636dfdSBill Paul 
418e8636dfdSBill Paul /*
419e8636dfdSBill Paul  * 4. Refresh
420e8636dfdSBill Paul  */
4218360efbdSAlfred Perlstein /*ARGSUSED*/
422e8636dfdSBill Paul static bool_t
423084bc321SCraig Rodrigues authdes_refresh(AUTH *auth, void *dummy __unused)
424e8636dfdSBill Paul {
4258360efbdSAlfred Perlstein /* LINTED pointer alignment */
426e8636dfdSBill Paul 	struct ad_private *ad = AUTH_PRIVATE(auth);
427e8636dfdSBill Paul 	struct authdes_cred *cred = &ad->ad_cred;
4288360efbdSAlfred Perlstein 	int		ok;
429e8636dfdSBill Paul 	netobj		pkey;
430e8636dfdSBill Paul 
4318360efbdSAlfred Perlstein 	if (ad->ad_dosync) {
4328360efbdSAlfred Perlstein                 ok = __rpc_get_time_offset(&ad->ad_timediff, ad->ad_nis_srvr,
433e8636dfdSBill Paul 		    ad->ad_timehost, &(ad->ad_uaddr),
4348360efbdSAlfred Perlstein 		    &(ad->ad_netid));
4358360efbdSAlfred Perlstein 		if (! ok) {
436e8636dfdSBill Paul 			/*
437e8636dfdSBill Paul 			 * Hope the clocks are synced!
438e8636dfdSBill Paul 			 */
439e8636dfdSBill Paul 			ad->ad_dosync = 0;
4408360efbdSAlfred Perlstein 			syslog(LOG_DEBUG,
4418360efbdSAlfred Perlstein 			    "authdes_refresh: unable to synchronize clock");
4428360efbdSAlfred Perlstein 		 }
443e8636dfdSBill Paul 	}
444e8636dfdSBill Paul 	ad->ad_xkey = auth->ah_key;
445e8636dfdSBill Paul 	pkey.n_bytes = (char *)(ad->ad_pkey);
4468360efbdSAlfred Perlstein 	pkey.n_len = (u_int)strlen((char *)ad->ad_pkey) + 1;
447e8636dfdSBill Paul 	if (key_encryptsession_pk(ad->ad_servername, &pkey, &ad->ad_xkey) < 0) {
4488360efbdSAlfred Perlstein 		syslog(LOG_INFO,
4498360efbdSAlfred Perlstein 		    "authdes_refresh: keyserv(1m) is unable to encrypt session key");
450e8636dfdSBill Paul 		return (FALSE);
451e8636dfdSBill Paul 	}
452e8636dfdSBill Paul 	cred->adc_fullname.key = ad->ad_xkey;
453e8636dfdSBill Paul 	cred->adc_namekind = ADN_FULLNAME;
454e8636dfdSBill Paul 	cred->adc_fullname.name = ad->ad_fullname;
455e8636dfdSBill Paul 	return (TRUE);
456e8636dfdSBill Paul }
457e8636dfdSBill Paul 
458e8636dfdSBill Paul 
459e8636dfdSBill Paul /*
460e8636dfdSBill Paul  * 5. Destroy
461e8636dfdSBill Paul  */
462e8636dfdSBill Paul static void
4638360efbdSAlfred Perlstein authdes_destroy(AUTH *auth)
464e8636dfdSBill Paul {
4658360efbdSAlfred Perlstein /* LINTED pointer alignment */
466e8636dfdSBill Paul 	struct ad_private *ad = AUTH_PRIVATE(auth);
467e8636dfdSBill Paul 
468e8636dfdSBill Paul 	FREE(ad->ad_fullname, ad->ad_fullnamelen + 1);
469e8636dfdSBill Paul 	FREE(ad->ad_servername, ad->ad_servernamelen + 1);
4708360efbdSAlfred Perlstein 	if (ad->ad_timehost)
4718360efbdSAlfred Perlstein 		FREE(ad->ad_timehost, strlen(ad->ad_timehost) + 1);
4728360efbdSAlfred Perlstein 	if (ad->ad_netid)
4738360efbdSAlfred Perlstein 		FREE(ad->ad_netid, strlen(ad->ad_netid) + 1);
4748360efbdSAlfred Perlstein 	if (ad->ad_uaddr)
4758360efbdSAlfred Perlstein 		FREE(ad->ad_uaddr, strlen(ad->ad_uaddr) + 1);
476e8636dfdSBill Paul 	FREE(ad, sizeof (struct ad_private));
477e8636dfdSBill Paul 	FREE(auth, sizeof(AUTH));
478e8636dfdSBill Paul }
479e8636dfdSBill Paul 
4808360efbdSAlfred Perlstein static struct auth_ops *
4818360efbdSAlfred Perlstein authdes_ops(void)
482e8636dfdSBill Paul {
4838360efbdSAlfred Perlstein 	static struct auth_ops ops;
484e8636dfdSBill Paul 
4858360efbdSAlfred Perlstein 	/* VARIABLES PROTECTED BY ops_lock: ops */
4868360efbdSAlfred Perlstein 
4878360efbdSAlfred Perlstein 	mutex_lock(&authdes_ops_lock);
4888360efbdSAlfred Perlstein 	if (ops.ah_nextverf == NULL) {
4898360efbdSAlfred Perlstein 		ops.ah_nextverf = authdes_nextverf;
4908360efbdSAlfred Perlstein 		ops.ah_marshal = authdes_marshal;
4918360efbdSAlfred Perlstein 		ops.ah_validate = authdes_validate;
4928360efbdSAlfred Perlstein 		ops.ah_refresh = authdes_refresh;
4938360efbdSAlfred Perlstein 		ops.ah_destroy = authdes_destroy;
494e8636dfdSBill Paul         }
4958360efbdSAlfred Perlstein 	mutex_unlock(&authdes_ops_lock);
4968360efbdSAlfred Perlstein 	return (&ops);
497e8636dfdSBill Paul }
498