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