auth_des.c revision 90868
1
2/*
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part.  Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
9 *
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 *
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
17 *
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
21 *
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
25 *
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California  94043
29 */
30/*
31 * Copyright (c) 1988 by Sun Microsystems, Inc.
32 */
33/*
34 * auth_des.c, client-side implementation of DES authentication
35 */
36#include "namespace.h"
37#include "reentrant.h"
38#include <err.h>
39#include <errno.h>
40#include <string.h>
41#include <stdlib.h>
42#include <unistd.h>
43#include <sys/cdefs.h>
44#include <rpc/des_crypt.h>
45#include <syslog.h>
46#include <rpc/types.h>
47#include <rpc/auth.h>
48#include <rpc/auth_des.h>
49#include <rpc/clnt.h>
50#include <rpc/xdr.h>
51#include <sys/socket.h>
52#undef NIS
53#include <rpcsvc/nis.h>
54#include "un-namespace.h"
55
56#if defined(LIBC_SCCS) && !defined(lint)
57/* from: static char sccsid[] = 	"@(#)auth_des.c	2.2 88/07/29 4.0 RPCSRC; from 1.9 88/02/08 SMI"; */
58static const char rcsid[] = "$FreeBSD: head/lib/libc/rpc/auth_des.c 90868 2002-02-18 20:35:27Z mike $";
59#endif
60
61#define USEC_PER_SEC		1000000
62#define RTIME_TIMEOUT		5	/* seconds to wait for sync */
63
64#define AUTH_PRIVATE(auth)	(struct ad_private *) auth->ah_private
65#define ALLOC(object_type)	(object_type *) mem_alloc(sizeof(object_type))
66#define FREE(ptr, size)		mem_free((char *)(ptr), (int) size)
67#define ATTEMPT(xdr_op)		if (!(xdr_op)) return (FALSE)
68
69extern bool_t xdr_authdes_cred( XDR *, struct authdes_cred *);
70extern bool_t xdr_authdes_verf( XDR *, struct authdes_verf *);
71extern int key_encryptsession_pk();
72
73extern bool_t __rpc_get_time_offset(struct timeval *, nis_server *, char *,
74	char **, char **);
75
76/*
77 * DES authenticator operations vector
78 */
79static void	authdes_nextverf(AUTH *);
80static bool_t	authdes_marshal(AUTH *, XDR *);
81static bool_t	authdes_validate(AUTH *, struct opaque_auth *);
82static bool_t	authdes_refresh(AUTH *, void *);
83static void	authdes_destroy(AUTH *);
84
85static struct auth_ops *authdes_ops(void);
86
87/*
88 * This struct is pointed to by the ah_private field of an "AUTH *"
89 */
90struct ad_private {
91	char *ad_fullname; 		/* client's full name */
92	u_int ad_fullnamelen;		/* length of name, rounded up */
93	char *ad_servername; 		/* server's full name */
94	u_int ad_servernamelen;		/* length of name, rounded up */
95	u_int ad_window;	  	/* client specified window */
96	bool_t ad_dosync;		/* synchronize? */
97	struct netbuf ad_syncaddr;	/* remote host to synch with */
98	char *ad_timehost;		/* remote host to synch with */
99	struct timeval ad_timediff;	/* server's time - client's time */
100	u_int ad_nickname;		/* server's nickname for client */
101	struct authdes_cred ad_cred;	/* storage for credential */
102	struct authdes_verf ad_verf;	/* storage for verifier */
103	struct timeval ad_timestamp;	/* timestamp sent */
104	des_block ad_xkey;		/* encrypted conversation key */
105	u_char ad_pkey[1024];		/* Server's actual public key */
106	char *ad_netid;			/* Timehost netid */
107	char *ad_uaddr;			/* Timehost uaddr */
108	nis_server *ad_nis_srvr;	/* NIS+ server struct */
109};
110
111AUTH *authdes_pk_seccreate(const char *, netobj *, u_int, const char *,
112	const des_block *, nis_server *);
113
114/*
115 * documented version of authdes_seccreate
116 */
117/*
118	servername:	network name of server
119	win:		time to live
120	timehost:	optional hostname to sync with
121	ckey:		optional conversation key to use
122*/
123
124AUTH *
125authdes_seccreate(const char *servername, const u_int win,
126	const char *timehost, const des_block *ckey)
127{
128	u_char  pkey_data[1024];
129	netobj  pkey;
130	AUTH    *dummy;
131
132	if (! getpublickey(servername, (char *) pkey_data)) {
133		syslog(LOG_ERR,
134		    "authdes_seccreate: no public key found for %s",
135		    servername);
136		return (NULL);
137	}
138
139	pkey.n_bytes = (char *) pkey_data;
140	pkey.n_len = (u_int)strlen((char *)pkey_data) + 1;
141	dummy = authdes_pk_seccreate(servername, &pkey, win, timehost,
142	    ckey, NULL);
143	return (dummy);
144}
145
146/*
147 * Slightly modified version of authdessec_create which takes the public key
148 * of the server principal as an argument. This spares us a call to
149 * getpublickey() which in the nameserver context can cause a deadlock.
150 */
151AUTH *
152authdes_pk_seccreate(const char *servername, netobj *pkey, u_int window,
153	const char *timehost, const des_block *ckey, nis_server *srvr)
154{
155	AUTH *auth;
156	struct ad_private *ad;
157	char namebuf[MAXNETNAMELEN+1];
158
159	/*
160	 * Allocate everything now
161	 */
162	auth = ALLOC(AUTH);
163	if (auth == NULL) {
164		syslog(LOG_ERR, "authdes_pk_seccreate: out of memory");
165		return (NULL);
166	}
167	ad = ALLOC(struct ad_private);
168	if (ad == NULL) {
169		syslog(LOG_ERR, "authdes_pk_seccreate: out of memory");
170		goto failed;
171	}
172	ad->ad_fullname = ad->ad_servername = NULL; /* Sanity reasons */
173	ad->ad_timehost = NULL;
174	ad->ad_netid = NULL;
175	ad->ad_uaddr = NULL;
176	ad->ad_nis_srvr = NULL;
177	ad->ad_timediff.tv_sec = 0;
178	ad->ad_timediff.tv_usec = 0;
179	memcpy(ad->ad_pkey, pkey->n_bytes, pkey->n_len);
180	if (!getnetname(namebuf))
181		goto failed;
182	ad->ad_fullnamelen = RNDUP((u_int) strlen(namebuf));
183	ad->ad_fullname = (char *)mem_alloc(ad->ad_fullnamelen + 1);
184	ad->ad_servernamelen = strlen(servername);
185	ad->ad_servername = (char *)mem_alloc(ad->ad_servernamelen + 1);
186
187	if (ad->ad_fullname == NULL || ad->ad_servername == NULL) {
188		syslog(LOG_ERR, "authdes_seccreate: out of memory");
189		goto failed;
190	}
191	if (timehost != NULL) {
192		ad->ad_timehost = (char *)mem_alloc(strlen(timehost) + 1);
193		if (ad->ad_timehost == NULL) {
194			syslog(LOG_ERR, "authdes_seccreate: out of memory");
195			goto failed;
196		}
197		memcpy(ad->ad_timehost, timehost, strlen(timehost) + 1);
198		ad->ad_dosync = TRUE;
199	} else if (srvr != NULL) {
200		ad->ad_nis_srvr = srvr;	/* transient */
201		ad->ad_dosync = TRUE;
202	} else {
203		ad->ad_dosync = FALSE;
204	}
205	memcpy(ad->ad_fullname, namebuf, ad->ad_fullnamelen + 1);
206	memcpy(ad->ad_servername, servername, ad->ad_servernamelen + 1);
207	ad->ad_window = window;
208	if (ckey == NULL) {
209		if (key_gendes(&auth->ah_key) < 0) {
210			syslog(LOG_ERR,
211	    "authdes_seccreate: keyserv(1m) is unable to generate session key");
212			goto failed;
213		}
214	} else {
215		auth->ah_key = *ckey;
216	}
217
218	/*
219	 * Set up auth handle
220	 */
221	auth->ah_cred.oa_flavor = AUTH_DES;
222	auth->ah_verf.oa_flavor = AUTH_DES;
223	auth->ah_ops = authdes_ops();
224	auth->ah_private = (caddr_t)ad;
225
226	if (!authdes_refresh(auth, NULL)) {
227		goto failed;
228	}
229	ad->ad_nis_srvr = NULL; /* not needed any longer */
230	return (auth);
231
232failed:
233	if (auth)
234		FREE(auth, sizeof (AUTH));
235	if (ad) {
236		if (ad->ad_fullname)
237			FREE(ad->ad_fullname, ad->ad_fullnamelen + 1);
238		if (ad->ad_servername)
239			FREE(ad->ad_servername, ad->ad_servernamelen + 1);
240		if (ad->ad_timehost)
241			FREE(ad->ad_timehost, strlen(ad->ad_timehost) + 1);
242		if (ad->ad_netid)
243			FREE(ad->ad_netid, strlen(ad->ad_netid) + 1);
244		if (ad->ad_uaddr)
245			FREE(ad->ad_uaddr, strlen(ad->ad_uaddr) + 1);
246		FREE(ad, sizeof (struct ad_private));
247	}
248	return (NULL);
249}
250
251/*
252 * Implement the five authentication operations
253 */
254
255
256/*
257 * 1. Next Verifier
258 */
259/*ARGSUSED*/
260static void
261authdes_nextverf(AUTH *auth)
262{
263	/* what the heck am I supposed to do??? */
264}
265
266
267/*
268 * 2. Marshal
269 */
270static bool_t
271authdes_marshal(AUTH *auth, XDR *xdrs)
272{
273/* LINTED pointer alignment */
274	struct ad_private *ad = AUTH_PRIVATE(auth);
275	struct authdes_cred *cred = &ad->ad_cred;
276	struct authdes_verf *verf = &ad->ad_verf;
277	des_block cryptbuf[2];
278	des_block ivec;
279	int status;
280	int len;
281	register rpc_inline_t *ixdr;
282
283	/*
284	 * Figure out the "time", accounting for any time difference
285	 * with the server if necessary.
286	 */
287	(void) gettimeofday(&ad->ad_timestamp, (struct timezone *)NULL);
288	ad->ad_timestamp.tv_sec += ad->ad_timediff.tv_sec;
289	ad->ad_timestamp.tv_usec += ad->ad_timediff.tv_usec;
290	while (ad->ad_timestamp.tv_usec >= USEC_PER_SEC) {
291		ad->ad_timestamp.tv_usec -= USEC_PER_SEC;
292		ad->ad_timestamp.tv_sec++;
293	}
294
295	/*
296	 * XDR the timestamp and possibly some other things, then
297	 * encrypt them.
298	 */
299	ixdr = (rpc_inline_t *)cryptbuf;
300	IXDR_PUT_INT32(ixdr, ad->ad_timestamp.tv_sec);
301	IXDR_PUT_INT32(ixdr, ad->ad_timestamp.tv_usec);
302	if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
303		IXDR_PUT_U_INT32(ixdr, ad->ad_window);
304		IXDR_PUT_U_INT32(ixdr, ad->ad_window - 1);
305		ivec.key.high = ivec.key.low = 0;
306		status = cbc_crypt((char *)&auth->ah_key, (char *)cryptbuf,
307			(u_int) 2 * sizeof (des_block),
308			DES_ENCRYPT | DES_HW, (char *)&ivec);
309	} else {
310		status = ecb_crypt((char *)&auth->ah_key, (char *)cryptbuf,
311			(u_int) sizeof (des_block),
312			DES_ENCRYPT | DES_HW);
313	}
314	if (DES_FAILED(status)) {
315		syslog(LOG_ERR, "authdes_marshal: DES encryption failure");
316		return (FALSE);
317	}
318	ad->ad_verf.adv_xtimestamp = cryptbuf[0];
319	if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
320		ad->ad_cred.adc_fullname.window = cryptbuf[1].key.high;
321		ad->ad_verf.adv_winverf = cryptbuf[1].key.low;
322	} else {
323		ad->ad_cred.adc_nickname = ad->ad_nickname;
324		ad->ad_verf.adv_winverf = 0;
325	}
326
327	/*
328	 * Serialize the credential and verifier into opaque
329	 * authentication data.
330	 */
331	if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
332		len = ((1 + 1 + 2 + 1)*BYTES_PER_XDR_UNIT + ad->ad_fullnamelen);
333	} else {
334		len = (1 + 1)*BYTES_PER_XDR_UNIT;
335	}
336
337	if ((ixdr = xdr_inline(xdrs, 2*BYTES_PER_XDR_UNIT))) {
338		IXDR_PUT_INT32(ixdr, AUTH_DES);
339		IXDR_PUT_INT32(ixdr, len);
340	} else {
341		ATTEMPT(xdr_putint32(xdrs, (int *)&auth->ah_cred.oa_flavor));
342		ATTEMPT(xdr_putint32(xdrs, &len));
343	}
344	ATTEMPT(xdr_authdes_cred(xdrs, cred));
345
346	len = (2 + 1)*BYTES_PER_XDR_UNIT;
347	if ((ixdr = xdr_inline(xdrs, 2*BYTES_PER_XDR_UNIT))) {
348		IXDR_PUT_INT32(ixdr, AUTH_DES);
349		IXDR_PUT_INT32(ixdr, len);
350	} else {
351		ATTEMPT(xdr_putint32(xdrs, (int *)&auth->ah_verf.oa_flavor));
352		ATTEMPT(xdr_putint32(xdrs, &len));
353	}
354	ATTEMPT(xdr_authdes_verf(xdrs, verf));
355	return (TRUE);
356}
357
358
359/*
360 * 3. Validate
361 */
362static bool_t
363authdes_validate(AUTH *auth, struct opaque_auth *rverf)
364{
365/* LINTED pointer alignment */
366	struct ad_private *ad = AUTH_PRIVATE(auth);
367	struct authdes_verf verf;
368	int status;
369	register uint32_t *ixdr;
370	des_block buf;
371
372	if (rverf->oa_length != (2 + 1) * BYTES_PER_XDR_UNIT) {
373		return (FALSE);
374	}
375/* LINTED pointer alignment */
376	ixdr = (uint32_t *)rverf->oa_base;
377	buf.key.high = (uint32_t)*ixdr++;
378	buf.key.low = (uint32_t)*ixdr++;
379	verf.adv_int_u = (uint32_t)*ixdr++;
380
381	/*
382	 * Decrypt the timestamp
383	 */
384	status = ecb_crypt((char *)&auth->ah_key, (char *)&buf,
385		(u_int)sizeof (des_block), DES_DECRYPT | DES_HW);
386
387	if (DES_FAILED(status)) {
388		syslog(LOG_ERR, "authdes_validate: DES decryption failure");
389		return (FALSE);
390	}
391
392	/*
393	 * xdr the decrypted timestamp
394	 */
395/* LINTED pointer alignment */
396	ixdr = (uint32_t *)buf.c;
397	verf.adv_timestamp.tv_sec = IXDR_GET_INT32(ixdr) + 1;
398	verf.adv_timestamp.tv_usec = IXDR_GET_INT32(ixdr);
399
400	/*
401	 * validate
402	 */
403	if (bcmp((char *)&ad->ad_timestamp, (char *)&verf.adv_timestamp,
404		 sizeof(struct timeval)) != 0) {
405		syslog(LOG_DEBUG, "authdes_validate: verifier mismatch");
406		return (FALSE);
407	}
408
409	/*
410	 * We have a nickname now, let's use it
411	 */
412	ad->ad_nickname = verf.adv_nickname;
413	ad->ad_cred.adc_namekind = ADN_NICKNAME;
414	return (TRUE);
415}
416
417/*
418 * 4. Refresh
419 */
420/*ARGSUSED*/
421static bool_t
422authdes_refresh(AUTH *auth, void *dummy)
423{
424/* LINTED pointer alignment */
425	struct ad_private *ad = AUTH_PRIVATE(auth);
426	struct authdes_cred *cred = &ad->ad_cred;
427	int		ok;
428	netobj		pkey;
429
430	if (ad->ad_dosync) {
431                ok = __rpc_get_time_offset(&ad->ad_timediff, ad->ad_nis_srvr,
432		    ad->ad_timehost, &(ad->ad_uaddr),
433		    &(ad->ad_netid));
434		if (! ok) {
435			/*
436			 * Hope the clocks are synced!
437			 */
438			ad->ad_dosync = 0;
439			syslog(LOG_DEBUG,
440			    "authdes_refresh: unable to synchronize clock");
441		 }
442	}
443	ad->ad_xkey = auth->ah_key;
444	pkey.n_bytes = (char *)(ad->ad_pkey);
445	pkey.n_len = (u_int)strlen((char *)ad->ad_pkey) + 1;
446	if (key_encryptsession_pk(ad->ad_servername, &pkey, &ad->ad_xkey) < 0) {
447		syslog(LOG_INFO,
448		    "authdes_refresh: keyserv(1m) is unable to encrypt session key");
449		return (FALSE);
450	}
451	cred->adc_fullname.key = ad->ad_xkey;
452	cred->adc_namekind = ADN_FULLNAME;
453	cred->adc_fullname.name = ad->ad_fullname;
454	return (TRUE);
455}
456
457
458/*
459 * 5. Destroy
460 */
461static void
462authdes_destroy(AUTH *auth)
463{
464/* LINTED pointer alignment */
465	struct ad_private *ad = AUTH_PRIVATE(auth);
466
467	FREE(ad->ad_fullname, ad->ad_fullnamelen + 1);
468	FREE(ad->ad_servername, ad->ad_servernamelen + 1);
469	if (ad->ad_timehost)
470		FREE(ad->ad_timehost, strlen(ad->ad_timehost) + 1);
471	if (ad->ad_netid)
472		FREE(ad->ad_netid, strlen(ad->ad_netid) + 1);
473	if (ad->ad_uaddr)
474		FREE(ad->ad_uaddr, strlen(ad->ad_uaddr) + 1);
475	FREE(ad, sizeof (struct ad_private));
476	FREE(auth, sizeof(AUTH));
477}
478
479static struct auth_ops *
480authdes_ops(void)
481{
482	static struct auth_ops ops;
483	extern mutex_t authdes_ops_lock;
484
485	/* VARIABLES PROTECTED BY ops_lock: ops */
486
487	mutex_lock(&authdes_ops_lock);
488	if (ops.ah_nextverf == NULL) {
489		ops.ah_nextverf = authdes_nextverf;
490		ops.ah_marshal = authdes_marshal;
491		ops.ah_validate = authdes_validate;
492		ops.ah_refresh = authdes_refresh;
493		ops.ah_destroy = authdes_destroy;
494        }
495	mutex_unlock(&authdes_ops_lock);
496	return (&ops);
497}
498