2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
30 * Copyright (c) 1988 by Sun Microsystems, Inc.
33 * auth_des.c, client-side implementation of DES authentication
38 #include <sys/cdefs.h>
39 #include <rpc/des_crypt.h>
40 #include <rpc/types.h>
43 #include <rpc/auth_des.h>
44 #include <netinet/in.h> /* XXX: just to get htonl() and ntohl() */
45 #include <sys/socket.h>
47 #include <rpcsvc/nis.h>
49 #if defined(LIBC_SCCS) && !defined(lint)
50 /* from: static char sccsid[] = "@(#)auth_des.c 2.2 88/07/29 4.0 RPCSRC; from 1.9 88/02/08 SMI"; */
51 static const char rcsid[] = "$FreeBSD: src/lib/libc/rpc/auth_des.c,v 1.3 1999/08/28 00:00:32 peter Exp $";
54 extern bool_t __rpc_get_time_offset __P(( struct timeval *, nis_server *,
55 char *, char **, struct sockaddr_in * ));
56 extern int rtime __P(( struct sockaddr_in *, struct timeval *, struct timeval *));
57 extern bool_t xdr_authdes_cred __P(( XDR *, struct authdes_cred * ));
58 extern bool_t xdr_authdes_verf __P(( XDR *, struct authdes_verf * ));
60 #define MILLION 1000000L
61 #define RTIME_TIMEOUT 5 /* seconds to wait for sync */
63 #define AUTH_PRIVATE(auth) (struct ad_private *) auth->ah_private
64 #define ALLOC(object_type) (object_type *) mem_alloc(sizeof(object_type))
65 #define FREE(ptr, size) mem_free((char *)(ptr), (int) size)
66 #define ATTEMPT(xdr_op) if (!(xdr_op)) return (FALSE)
68 #define debug(msg) /*printf("%s\n", msg) */
71 * DES authenticator operations vector
73 static void authdes_nextverf();
74 static bool_t authdes_marshal();
75 static bool_t authdes_validate();
76 static bool_t authdes_refresh();
77 static void authdes_destroy();
78 static struct auth_ops authdes_ops = {
86 static bool_t synchronize __P(( struct sockaddr *, struct timeval *));
89 * This struct is pointed to by the ah_private field of an "AUTH *"
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 sockaddr 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_long 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 */
114 * Create the client des authentication object
117 authdes_create(servername, window, syncaddr, ckey)
118 char *servername; /* network name of server */
119 u_int window; /* time to live */
120 struct sockaddr *syncaddr; /* optional addr of host to sync with */
121 des_block *ckey; /* optional conversation key to use*/
125 struct ad_private *ad;
126 char namebuf[MAXNETNAMELEN+1];
127 u_char pkey_data[1024];
129 if (!getpublickey(servername, pkey_data))
133 * Allocate everything now
136 ad = ALLOC(struct ad_private);
137 (void) getnetname(namebuf);
139 ad->ad_fullnamelen = RNDUP(strlen(namebuf));
140 ad->ad_fullname = (char *)mem_alloc(ad->ad_fullnamelen + 1);
142 ad->ad_servernamelen = strlen(servername);
143 ad->ad_servername = (char *)mem_alloc(ad->ad_servernamelen + 1);
145 if (auth == NULL || ad == NULL || ad->ad_fullname == NULL ||
146 ad->ad_servername == NULL) {
147 debug("authdes_create: out of memory");
152 * Set up private data
154 bcopy(namebuf, ad->ad_fullname, ad->ad_fullnamelen + 1);
155 bcopy(servername, ad->ad_servername, ad->ad_servernamelen + 1);
156 bcopy(pkey_data, ad->ad_pkey, strlen(pkey_data) + 1);
157 if (syncaddr != NULL) {
158 ad->ad_syncaddr = *syncaddr;
159 ad->ad_dosync = TRUE;
161 ad->ad_dosync = FALSE;
163 ad->ad_window = window;
165 if (key_gendes(&auth->ah_key) < 0) {
166 debug("authdes_create: unable to gen conversation key");
170 auth->ah_key = *ckey;
176 auth->ah_cred.oa_flavor = AUTH_DES;
177 auth->ah_verf.oa_flavor = AUTH_DES;
178 auth->ah_ops = &authdes_ops;
179 auth->ah_private = (caddr_t)ad;
181 if (!authdes_refresh(auth)) {
188 FREE(auth, sizeof(AUTH));
190 FREE(ad, sizeof(struct ad_private));
191 if (ad->ad_fullname != NULL)
192 FREE(ad->ad_fullname, ad->ad_fullnamelen + 1);
193 if (ad->ad_servername != NULL)
194 FREE(ad->ad_servername, ad->ad_servernamelen + 1);
199 * Slightly modified version of authdes_create which takes the public key
200 * of the server principal as an argument. This spares us a call to
201 * getpublickey() which in the nameserver context can cause a deadlock.
204 authdes_pk_create(servername, pkey, window, timehost, ckey, srvr)
205 char *servername; /* network name of server */
206 netobj *pkey; /* public key of server */
207 u_int window; /* time to live */
208 char *timehost; /* optional hostname to sync with */
209 des_block *ckey; /* optional conversation key to use */
210 nis_server *srvr; /* optional NIS+ server struct */
213 struct ad_private *ad;
214 char namebuf[MAXNETNAMELEN+1];
217 * Allocate everything now
221 debug("authdes_pk_create: out of memory");
224 ad = ALLOC(struct ad_private);
226 debug("authdes_pk_create: out of memory");
229 ad->ad_fullname = ad->ad_servername = NULL; /* Sanity reasons */
230 ad->ad_timehost = NULL;
233 ad->ad_nis_srvr = NULL;
234 ad->ad_timediff.tv_sec = 0;
235 ad->ad_timediff.tv_usec = 0;
236 memcpy(ad->ad_pkey, pkey->n_bytes, pkey->n_len);
237 if (!getnetname(namebuf))
239 ad->ad_fullnamelen = RNDUP((u_int) strlen(namebuf));
240 ad->ad_fullname = (char *)mem_alloc(ad->ad_fullnamelen + 1);
241 ad->ad_servernamelen = strlen(servername);
242 ad->ad_servername = (char *)mem_alloc(ad->ad_servernamelen + 1);
244 if (ad->ad_fullname == NULL || ad->ad_servername == NULL) {
245 debug("authdes_pk_create: out of memory");
248 if (timehost != NULL) {
249 ad->ad_timehost = (char *)mem_alloc(strlen(timehost) + 1);
250 if (ad->ad_timehost == NULL) {
251 debug("authdes_pk_create: out of memory");
254 memcpy(ad->ad_timehost, timehost, strlen(timehost) + 1);
255 ad->ad_dosync = TRUE;
256 } else if (srvr != NULL) {
257 ad->ad_nis_srvr = srvr; /* transient */
258 ad->ad_dosync = TRUE;
260 ad->ad_dosync = FALSE;
262 memcpy(ad->ad_fullname, namebuf, ad->ad_fullnamelen + 1);
263 memcpy(ad->ad_servername, servername, ad->ad_servernamelen + 1);
264 ad->ad_window = window;
266 if (key_gendes(&auth->ah_key) < 0) {
267 debug("authdes_pk_create: unable to gen conversation key");
271 auth->ah_key = *ckey;
277 auth->ah_cred.oa_flavor = AUTH_DES;
278 auth->ah_verf.oa_flavor = AUTH_DES;
279 auth->ah_ops = &authdes_ops;
280 auth->ah_private = (caddr_t)ad;
282 if (!authdes_refresh(auth)) {
285 ad->ad_nis_srvr = NULL; /* not needed any longer */
290 FREE(auth, sizeof (AUTH));
293 FREE(ad->ad_fullname, ad->ad_fullnamelen + 1);
294 if (ad->ad_servername)
295 FREE(ad->ad_servername, ad->ad_servernamelen + 1);
297 FREE(ad->ad_timehost, strlen(ad->ad_timehost) + 1);
302 FREE(ad, sizeof (struct ad_private));
307 * Implement the five authentication operations
316 authdes_nextverf(auth)
319 /* what the heck am I supposed to do??? */
328 authdes_marshal(auth, xdrs)
332 struct ad_private *ad = AUTH_PRIVATE(auth);
333 struct authdes_cred *cred = &ad->ad_cred;
334 struct authdes_verf *verf = &ad->ad_verf;
335 des_block cryptbuf[2];
342 * Figure out the "time", accounting for any time difference
343 * with the server if necessary.
345 (void) gettimeofday(&ad->ad_timestamp, (struct timezone *)NULL);
346 ad->ad_timestamp.tv_sec += ad->ad_timediff.tv_sec;
347 ad->ad_timestamp.tv_usec += ad->ad_timediff.tv_usec;
348 if (ad->ad_timestamp.tv_usec >= MILLION) {
349 ad->ad_timestamp.tv_usec -= MILLION;
350 ad->ad_timestamp.tv_sec += 1;
354 * XDR the timestamp and possibly some other things, then
357 ixdr = (int32_t *)cryptbuf;
358 IXDR_PUT_LONG(ixdr, ad->ad_timestamp.tv_sec);
359 IXDR_PUT_LONG(ixdr, ad->ad_timestamp.tv_usec);
360 if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
361 IXDR_PUT_U_LONG(ixdr, ad->ad_window);
362 IXDR_PUT_U_LONG(ixdr, ad->ad_window - 1);
363 ivec.key.high = ivec.key.low = 0;
364 status = cbc_crypt((char *)&auth->ah_key, (char *)cryptbuf,
365 2*sizeof(des_block), DES_ENCRYPT | DES_HW, (char *)&ivec);
367 status = ecb_crypt((char *)&auth->ah_key, (char *)cryptbuf,
368 sizeof(des_block), DES_ENCRYPT | DES_HW);
370 if (DES_FAILED(status)) {
371 debug("authdes_marshal: DES encryption failure");
374 ad->ad_verf.adv_xtimestamp = cryptbuf[0];
375 if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
376 ad->ad_cred.adc_fullname.window = cryptbuf[1].key.high;
377 ad->ad_verf.adv_winverf = cryptbuf[1].key.low;
379 ad->ad_cred.adc_nickname = ad->ad_nickname;
380 ad->ad_verf.adv_winverf = 0;
384 * Serialize the credential and verifier into opaque
385 * authentication data.
387 if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
388 len = ((1 + 1 + 2 + 1)*BYTES_PER_XDR_UNIT + ad->ad_fullnamelen);
390 len = (1 + 1)*BYTES_PER_XDR_UNIT;
393 if ((ixdr = xdr_inline(xdrs, 2*BYTES_PER_XDR_UNIT))) {
394 IXDR_PUT_LONG(ixdr, AUTH_DES);
395 IXDR_PUT_LONG(ixdr, len);
397 ATTEMPT(xdr_putlong(xdrs, (long *)&auth->ah_cred.oa_flavor));
398 ATTEMPT(xdr_putlong(xdrs, &len));
400 ATTEMPT(xdr_authdes_cred(xdrs, cred));
402 len = (2 + 1)*BYTES_PER_XDR_UNIT;
403 if ((ixdr = xdr_inline(xdrs, 2*BYTES_PER_XDR_UNIT))) {
404 IXDR_PUT_LONG(ixdr, AUTH_DES);
405 IXDR_PUT_LONG(ixdr, len);
407 ATTEMPT(xdr_putlong(xdrs, (long *)&auth->ah_verf.oa_flavor));
408 ATTEMPT(xdr_putlong(xdrs, &len));
410 ATTEMPT(xdr_authdes_verf(xdrs, verf));
419 authdes_validate(auth, rverf)
421 struct opaque_auth *rverf;
423 struct ad_private *ad = AUTH_PRIVATE(auth);
424 struct authdes_verf verf;
426 register u_long *ixdr;
428 if (rverf->oa_length != (2 + 1) * BYTES_PER_XDR_UNIT) {
431 ixdr = (u_long *)rverf->oa_base;
432 verf.adv_xtimestamp.key.high = (u_long)*ixdr++;
433 verf.adv_xtimestamp.key.low = (u_long)*ixdr++;
434 verf.adv_int_u = (u_long)*ixdr++; /* nickname not XDR'd ! */
437 * Decrypt the timestamp
439 status = ecb_crypt((char *)&auth->ah_key, (char *)&verf.adv_xtimestamp,
440 sizeof(des_block), DES_DECRYPT | DES_HW);
442 if (DES_FAILED(status)) {
443 debug("authdes_validate: DES decryption failure");
448 * xdr the decrypted timestamp
450 ixdr = (u_long *)verf.adv_xtimestamp.c;
451 verf.adv_timestamp.tv_sec = IXDR_GET_LONG(ixdr) + 1;
452 verf.adv_timestamp.tv_usec = IXDR_GET_LONG(ixdr);
457 if (bcmp((char *)&ad->ad_timestamp, (char *)&verf.adv_timestamp,
458 sizeof(struct timeval)) != 0) {
459 debug("authdes_validate: verifier mismatch\n");
464 * We have a nickname now, let's use it
466 ad->ad_nickname = verf.adv_nickname;
467 ad->ad_cred.adc_namekind = ADN_NICKNAME;
475 authdes_refresh(auth)
478 struct ad_private *ad = AUTH_PRIVATE(auth);
479 struct authdes_cred *cred = &ad->ad_cred;
484 !synchronize(&ad->ad_syncaddr, &ad->ad_timediff)) {
486 !__rpc_get_time_offset(&ad->ad_timediff,ad->ad_nis_srvr,
487 ad->ad_timehost, &(ad->ad_uaddr),
488 (struct sockaddr_in *)&(ad->ad_syncaddr))) {
491 * Hope the clocks are synced!
493 ad->ad_timediff.tv_sec = ad->ad_timediff.tv_usec = 0;
495 debug("authdes_refresh: unable to synchronize with server");
497 ad->ad_xkey = auth->ah_key;
498 pkey.n_bytes = (char *)(ad->ad_pkey);
499 pkey.n_len = strlen((char *)ad->ad_pkey) + 1;
500 if (key_encryptsession_pk(ad->ad_servername, &pkey, &ad->ad_xkey) < 0) {
501 debug("authdes_create: unable to encrypt conversation key");
504 cred->adc_fullname.key = ad->ad_xkey;
505 cred->adc_namekind = ADN_FULLNAME;
506 cred->adc_fullname.name = ad->ad_fullname;
515 authdes_destroy(auth)
518 struct ad_private *ad = AUTH_PRIVATE(auth);
520 FREE(ad->ad_fullname, ad->ad_fullnamelen + 1);
521 FREE(ad->ad_servername, ad->ad_servernamelen + 1);
522 FREE(ad, sizeof(struct ad_private));
523 FREE(auth, sizeof(AUTH));
529 * Synchronize with the server at the given address, that is,
530 * adjust timep to reflect the delta between our clocks
533 synchronize(syncaddr, timep)
534 struct sockaddr *syncaddr;
535 struct timeval *timep;
537 struct timeval mytime;
538 struct timeval timeout;
540 timeout.tv_sec = RTIME_TIMEOUT;
542 if (rtime((struct sockaddr_in *)syncaddr, timep, NULL /*&timeout*/) < 0) {
545 (void) gettimeofday(&mytime, (struct timezone *)NULL);
546 timep->tv_sec -= mytime.tv_sec;
547 if (mytime.tv_usec > timep->tv_usec) {
549 timep->tv_usec += MILLION;
551 timep->tv_usec -= mytime.tv_usec;