xref: /illumos-gate/usr/src/uts/common/rpc/sec/auth_des.c (revision 628e3cbed6489fa1db545d8524a06cd6535af456)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 /*
38  * auth_des.c, client-side implementation of DES authentication
39  */
40 
41 #include <sys/types.h>
42 #include <sys/t_lock.h>
43 #include <sys/time.h>
44 #include <sys/systm.h>
45 #include <sys/socket.h>
46 #include <sys/tiuser.h>
47 #include <sys/errno.h>
48 #include <rpc/des_crypt.h>
49 #include <rpc/types.h>
50 #include <rpc/auth.h>
51 #include <rpc/auth_des.h>
52 #include <rpc/xdr.h>
53 #include <rpc/clnt.h>
54 #include <rpc/rpc_msg.h>
55 #include <netinet/in.h>	/* XXX: just to get htonl() and ntohl() */
56 #include <sys/cmn_err.h>
57 #include <sys/debug.h>
58 
59 #define	MILLION		1000000
60 #define	RTIME_TIMEOUT	5		/* seconds to wait for sync */
61 
62 #define	AUTH_PRIVATE(auth)	(struct ad_private *)auth->ah_private
63 #define	ALLOC(object_type)	(object_type *) mem_alloc(sizeof (object_type))
64 #define	FREE(ptr, size)		mem_free((char *)(ptr), (int)size)
65 #define	ATTEMPT(xdr_op)		if (!(xdr_op))\
66 					return (FALSE)
67 
68 #define	gettimeofday(tvp, tzp)	uniqtime(tvp)
69 
70 static void	authdes_nextverf(AUTH *);
71 static bool_t	authdes_marshal(AUTH *, XDR *, struct cred *);
72 static bool_t	authdes_validate(AUTH *, struct opaque_auth *);
73 static bool_t	authdes_refresh(AUTH *, struct rpc_msg *, cred_t *);
74 static void	authdes_destroy(AUTH *);
75 static bool_t	synchronize(struct knetconfig *, struct netbuf *,
76 			int, struct timeval *);
77 
78 static struct auth_ops *authdes_ops(void);
79 
80 /*
81  * This struct is pointed to by the ah_private field of an "AUTH *"
82  */
83 struct ad_private {
84 	char *ad_fullname; 		/* client's full name */
85 	uint_t ad_fullnamelen;		/* length of name, rounded up */
86 	char *ad_servername; 		/* server's full name */
87 	uint_t ad_servernamelen;	/* length of name, rounded up */
88 	uint_t ad_window;		/* client specified window */
89 	bool_t ad_dosync;		/* synchronize? */
90 	struct netbuf ad_syncaddr;	/* remote host to synch with */
91 	struct knetconfig ad_synconfig; /* netconfig for the synch host */
92 	int   ad_calltype;		/* use rpc or straight call for sync */
93 	struct timeval ad_timediff;	/* server's time - client's time */
94 	uint32_t ad_nickname;		/* server's nickname for client */
95 	struct authdes_cred ad_cred;	/* storage for credential */
96 	struct authdes_verf ad_verf;	/* storage for verifier */
97 	struct timeval ad_timestamp;	/* timestamp sent */
98 	des_block ad_xkey;		/* encrypted conversation key */
99 };
100 
101 
102 /*
103  * Create the client des authentication object
104  */
105 /* ARGSUSED */
106 int
107 authdes_create(char *servername, uint_t window, struct netbuf *syncaddr,
108 	struct knetconfig *synconfig, des_block *ckey, int calltype,
109 	AUTH **retauth)
110 {
111 	AUTH *auth;
112 	struct ad_private *ad;
113 	char namebuf[MAXNETNAMELEN+1];
114 	int error = 0;
115 	enum clnt_stat stat;
116 
117 	if (retauth == NULL)
118 		return (EINVAL);
119 
120 	*retauth = NULL;
121 
122 	/*
123 	 * Allocate everything now
124 	 */
125 	auth = ALLOC(AUTH);
126 	ad = ALLOC(struct ad_private);
127 	bzero(ad, sizeof (struct ad_private));
128 	if ((stat = kgetnetname(namebuf)) != 0) {
129 		cmn_err(CE_NOTE,
130 	"authdes_create: unable to get client's netname: %s (error %d)",
131 		    clnt_sperrno(stat), stat);
132 		goto failed;
133 	}
134 
135 	ad->ad_fullnamelen = (uint_t)RNDUP(strlen(namebuf));
136 	ad->ad_fullname = mem_alloc(ad->ad_fullnamelen + 1);
137 
138 	ad->ad_servernamelen = (uint_t)strlen(servername);
139 	ad->ad_servername = mem_alloc(ad->ad_servernamelen + 1);
140 
141 	if (auth == NULL || ad == NULL || ad->ad_fullname == NULL ||
142 	    ad->ad_servername == NULL) {
143 		cmn_err(CE_NOTE, "authdes_create: out of memory");
144 		error = ENOMEM;
145 		goto failed;
146 	}
147 
148 	/*
149 	 * Set up private data
150 	 */
151 	bcopy(namebuf, ad->ad_fullname, ad->ad_fullnamelen + 1);
152 	bcopy(servername, ad->ad_servername, ad->ad_servernamelen + 1);
153 	if (syncaddr != NULL) {
154 		ad->ad_syncaddr = *syncaddr;
155 		ad->ad_synconfig = *synconfig;
156 		ad->ad_dosync = TRUE;
157 		ad->ad_calltype = calltype;
158 	} else {
159 		ad->ad_timediff.tv_sec = 0;
160 		ad->ad_timediff.tv_usec = 0;
161 		ad->ad_dosync = FALSE;
162 	}
163 	ad->ad_window = window;
164 	if (ckey == NULL) {
165 		if ((stat = key_gendes(&auth->ah_key)) != RPC_SUCCESS) {
166 			cmn_err(CE_NOTE,
167 	"authdes_create: unable to gen conversation key: %s (error %d)",
168 			    clnt_sperrno(stat), stat);
169 			if (stat == RPC_INTR)
170 				error = EINTR;
171 			else if (stat == RPC_TIMEDOUT)
172 				error = ETIMEDOUT;
173 			else
174 				error = EINVAL;		/* XXX */
175 			goto failed;
176 		}
177 	} else
178 		auth->ah_key = *ckey;
179 
180 	/*
181 	 * Set up auth handle
182 	 */
183 	auth->ah_cred.oa_flavor = AUTH_DES;
184 	auth->ah_verf.oa_flavor = AUTH_DES;
185 	auth->ah_ops = authdes_ops();
186 	auth->ah_private = (caddr_t)ad;
187 
188 	if (!authdes_refresh(auth, NULL, CRED()))
189 		goto failed;
190 
191 	*retauth = auth;
192 	return (0);
193 
194 failed:
195 	if (ad != NULL && ad->ad_fullname != NULL)
196 		FREE(ad->ad_fullname, ad->ad_fullnamelen + 1);
197 	if (ad != NULL && ad->ad_servername != NULL)
198 		FREE(ad->ad_servername, ad->ad_servernamelen + 1);
199 	if (ad != NULL)
200 		FREE(ad, sizeof (struct ad_private));
201 	if (auth != NULL)
202 		FREE(auth, sizeof (AUTH));
203 	return ((error == 0) ? EINVAL : error);		/* XXX */
204 }
205 
206 /*
207  * Implement the five authentication operations
208  */
209 
210 /*
211  * 1. Next Verifier
212  */
213 /* ARGSUSED */
214 static void
215 authdes_nextverf(AUTH *auth)
216 {
217 	/* what the heck am I supposed to do??? */
218 }
219 
220 /*
221  * 2. Marshal
222  */
223 /* ARGSUSED */
224 static bool_t
225 authdes_marshal(AUTH *auth, XDR *xdrs, struct cred *cr)
226 {
227 	/* LINTED pointer alignment */
228 	struct ad_private *ad = AUTH_PRIVATE(auth);
229 	struct authdes_cred *cred = &ad->ad_cred;
230 	struct authdes_verf *verf = &ad->ad_verf;
231 	des_block cryptbuf[2];
232 	des_block ivec;
233 	int status;
234 	int len;
235 	int32_t *ixdr;
236 
237 	/*
238 	 * Figure out the "time", accounting for any time difference
239 	 * with the server if necessary.
240 	 */
241 	(void) gettimeofday(&ad->ad_timestamp, (struct timezone *)NULL);
242 	ad->ad_timestamp.tv_sec += ad->ad_timediff.tv_sec;
243 	ad->ad_timestamp.tv_usec += ad->ad_timediff.tv_usec;
244 	if (ad->ad_timestamp.tv_usec >= MILLION) {
245 		ad->ad_timestamp.tv_usec -= MILLION;
246 		ad->ad_timestamp.tv_sec += 1;
247 	}
248 
249 	/*
250 	 * XDR the timestamp and possibly some other things, then
251 	 * encrypt them.
252 	 */
253 	ixdr = (int32_t *)cryptbuf;
254 	IXDR_PUT_INT32(ixdr, ad->ad_timestamp.tv_sec);
255 	IXDR_PUT_INT32(ixdr, ad->ad_timestamp.tv_usec);
256 	if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
257 		IXDR_PUT_U_INT32(ixdr, ad->ad_window);
258 		IXDR_PUT_U_INT32(ixdr, ad->ad_window - 1);
259 		ivec.key.high = ivec.key.low = 0;
260 		status = cbc_crypt((char *)&auth->ah_key, (char *)cryptbuf,
261 		    2 * sizeof (des_block), DES_ENCRYPT, (char *)&ivec);
262 	} else {
263 		status = ecb_crypt((char *)&auth->ah_key, (char *)cryptbuf,
264 		    sizeof (des_block), DES_ENCRYPT);
265 	}
266 	if (DES_FAILED(status)) {
267 		cmn_err(CE_NOTE, "authdes_marshal: DES encryption failure");
268 		return (FALSE);
269 	}
270 	ad->ad_verf.adv_xtimestamp = cryptbuf[0];
271 	if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
272 		ad->ad_cred.adc_fullname.window = cryptbuf[1].key.high;
273 		ad->ad_verf.adv_winverf = cryptbuf[1].key.low;
274 	} else {
275 		ad->ad_cred.adc_nickname = ad->ad_nickname;
276 		ad->ad_verf.adv_winverf = 0;
277 	}
278 
279 	/*
280 	 * Serialize the credential and verifier into opaque
281 	 * authentication data.
282 	 */
283 	if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
284 		len = ((1 + 1 + 2 + 1) * BYTES_PER_XDR_UNIT +
285 		    ad->ad_fullnamelen);
286 	} else
287 		len = (1 + 1) * BYTES_PER_XDR_UNIT;
288 
289 	if (ixdr = xdr_inline(xdrs, 2 * BYTES_PER_XDR_UNIT)) {
290 		IXDR_PUT_INT32(ixdr, AUTH_DES);
291 		IXDR_PUT_INT32(ixdr, len);
292 	} else {
293 		ATTEMPT(xdr_putint32(xdrs,
294 		    (int32_t *)&auth->ah_cred.oa_flavor));
295 		ATTEMPT(xdr_putint32(xdrs, (int32_t *)&len));
296 	}
297 	ATTEMPT(xdr_authdes_cred(xdrs, cred));
298 
299 	len = (2 + 1) * BYTES_PER_XDR_UNIT;
300 	if (ixdr = xdr_inline(xdrs, 2 * BYTES_PER_XDR_UNIT)) {
301 		IXDR_PUT_INT32(ixdr, AUTH_DES);
302 		IXDR_PUT_INT32(ixdr, len);
303 	} else {
304 		ATTEMPT(xdr_putint32(xdrs,
305 		    (int32_t *)&auth->ah_verf.oa_flavor));
306 		ATTEMPT(xdr_putint32(xdrs, (int32_t *)&len));
307 	}
308 	ATTEMPT(xdr_authdes_verf(xdrs, verf));
309 	return (TRUE);
310 }
311 
312 /*
313  * 3. Validate
314  */
315 static bool_t
316 authdes_validate(AUTH *auth, struct opaque_auth *rverf)
317 {
318 	/* LINTED pointer alignment */
319 	struct ad_private *ad = AUTH_PRIVATE(auth);
320 	struct authdes_verf verf;
321 	int status;
322 	uint32_t *ixdr;
323 	des_block buf;
324 
325 	if (rverf->oa_length != (2 + 1) * BYTES_PER_XDR_UNIT)
326 		return (FALSE);
327 
328 	/* LINTED pointer alignment */
329 	ixdr = (uint32_t *)rverf->oa_base;
330 	buf.key.high = (uint32_t)*ixdr++;
331 	buf.key.low = (uint32_t)*ixdr++;
332 	verf.adv_int_u = IXDR_GET_U_INT32(ixdr);
333 
334 	/*
335 	 * Decrypt the timestamp
336 	 */
337 	status = ecb_crypt((char *)&auth->ah_key, (char *)&buf,
338 	    sizeof (des_block), DES_DECRYPT);
339 
340 	if (DES_FAILED(status)) {
341 		cmn_err(CE_NOTE, "authdes_validate: DES decryption failure");
342 		return (FALSE);
343 	}
344 
345 	/*
346 	 * xdr the decrypted timestamp
347 	 */
348 	/* LINTED pointer alignment */
349 	ixdr = (uint32_t *)buf.c;
350 	verf.adv_timestamp.tv_sec = IXDR_GET_INT32(ixdr) + 1;
351 	verf.adv_timestamp.tv_usec = IXDR_GET_INT32(ixdr);
352 
353 	/*
354 	 * validate
355 	 */
356 	if (bcmp((char *)&ad->ad_timestamp, (char *)&verf.adv_timestamp,
357 	    sizeof (struct timeval)) != 0) {
358 		cmn_err(CE_NOTE, "authdes_validate: verifier mismatch");
359 		return (FALSE);
360 	}
361 
362 	/*
363 	 * We have a nickname now, let's use it
364 	 */
365 	ad->ad_nickname = verf.adv_nickname;
366 	ad->ad_cred.adc_namekind = ADN_NICKNAME;
367 	return (TRUE);
368 }
369 
370 /*
371  * 4. Refresh
372  *
373  *  msg is a dummy argument here.
374  */
375 /* ARGSUSED */
376 static bool_t
377 authdes_refresh(AUTH *auth, struct rpc_msg *msg, cred_t *cr)
378 {
379 	/* LINTED pointer alignment */
380 	struct ad_private *ad = AUTH_PRIVATE(auth);
381 	struct authdes_cred *cred = &ad->ad_cred;
382 	enum clnt_stat stat;
383 
384 	if (ad->ad_dosync &&
385 	    !synchronize(&ad->ad_synconfig, &ad->ad_syncaddr,
386 	    ad->ad_calltype, &ad->ad_timediff)) {
387 		/*
388 		 * Hope the clocks are synced!
389 		 */
390 		timerclear(&ad->ad_timediff);
391 		cmn_err(CE_NOTE,
392 "authdes_refresh: unable to synchronize with server %s", ad->ad_servername);
393 	}
394 	ad->ad_xkey = auth->ah_key;
395 	if ((stat = key_encryptsession(ad->ad_servername, &ad->ad_xkey, cr)) !=
396 	    RPC_SUCCESS) {
397 		cmn_err(CE_NOTE,
398 "authdes_refresh: unable to encrypt conversation key for user (uid %d): "
399 		    "%s (error %d)",
400 		    (int)crgetuid(cr), clnt_sperrno(stat), stat);
401 		return (FALSE);
402 	}
403 	cred->adc_fullname.key = ad->ad_xkey;
404 	cred->adc_namekind = ADN_FULLNAME;
405 	cred->adc_fullname.name = ad->ad_fullname;
406 	return (TRUE);
407 }
408 
409 /*
410  * 5. Destroy
411  */
412 static void
413 authdes_destroy(AUTH *auth)
414 {
415 	/* LINTED pointer alignment */
416 	struct ad_private *ad = AUTH_PRIVATE(auth);
417 
418 	FREE(ad->ad_fullname, ad->ad_fullnamelen + 1);
419 	FREE(ad->ad_servername, ad->ad_servernamelen + 1);
420 	FREE(ad, sizeof (struct ad_private));
421 	FREE(auth, sizeof (AUTH));
422 }
423 
424 
425 /*
426  * Synchronize with the server at the given address, that is,
427  * adjust timep to reflect the delta between our clocks
428  */
429 static bool_t
430 synchronize(struct knetconfig *synconfig, struct netbuf *syncaddr, int calltype,
431 	struct timeval *timep)
432 {
433 	struct timeval mytime;
434 	struct timeval timout;
435 
436 	timout.tv_sec = RTIME_TIMEOUT;
437 	timout.tv_usec = 0;
438 	if (rtime(synconfig, syncaddr, calltype, timep, &timout) < 0)
439 		return (FALSE);
440 	(void) gettimeofday(&mytime, (struct timezone *)NULL);
441 	timep->tv_sec -= mytime.tv_sec;
442 	if (mytime.tv_usec > timep->tv_usec) {
443 		timep->tv_sec -= 1;
444 		timep->tv_usec += MILLION;
445 	}
446 	timep->tv_usec -= mytime.tv_usec;
447 	return (TRUE);
448 }
449 
450 static struct auth_ops *
451 authdes_ops(void)
452 {
453 	static struct auth_ops ops;
454 
455 	mutex_enter(&authdes_ops_lock);
456 	if (ops.ah_nextverf == NULL) {
457 		ops.ah_nextverf = authdes_nextverf;
458 		ops.ah_marshal = authdes_marshal;
459 		ops.ah_validate = authdes_validate;
460 		ops.ah_refresh = authdes_refresh;
461 		ops.ah_destroy = authdes_destroy;
462 		ops.ah_wrap = authany_wrap;
463 		ops.ah_unwrap = authany_unwrap;
464 	}
465 	mutex_exit(&authdes_ops_lock);
466 	return (&ops);
467 }
468