Cleanup:
[games.git] / lib / libc / rpc / auth_des.c
1 /*
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.
8  * 
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.
12  * 
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.
16  * 
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.
20  * 
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.
24  * 
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  *
29  * @(#)auth_des.c       2.2 88/07/29 4.0 RPCSRC; from 1.9 88/02/08 SMI
30  * $FreeBSD: src/lib/libc/rpc/auth_des.c,v 1.3 1999/08/28 00:00:32 peter Exp $
31  * $DragonFly: src/lib/libc/rpc/auth_des.c,v 1.5 2005/11/13 12:27:04 swildner Exp $
32  */
33 /*
34  * Copyright (c) 1988 by Sun Microsystems, Inc.
35  */
36 /*
37  * auth_des.c, client-side implementation of DES authentication
38  */
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 <rpc/types.h>
45 #include <rpc/xdr.h>
46 #include <rpc/auth.h>
47 #include <rpc/auth_des.h>
48 #include <netinet/in.h> /* XXX: just to get htonl() and ntohl() */
49 #include <sys/socket.h>
50 #undef NIS
51 #include <rpcsvc/nis.h>
52
53 extern bool_t __rpc_get_time_offset ( struct timeval *, nis_server *,
54                                                 char *, char **, struct sockaddr_in * );
55 extern int rtime ( struct sockaddr_in *, struct timeval *, struct timeval *);
56 extern bool_t xdr_authdes_cred ( XDR *, struct authdes_cred * );
57 extern bool_t xdr_authdes_verf ( XDR *, struct authdes_verf * );
58
59 #define MILLION         1000000L
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)) return (FALSE)
66
67 #define debug(msg)               /*printf("%s\n", msg) */
68
69 /* 
70  * DES authenticator operations vector
71  */
72 static void     authdes_nextverf();
73 static bool_t   authdes_marshal();
74 static bool_t   authdes_validate();
75 static bool_t   authdes_refresh();
76 static void     authdes_destroy();
77 static struct auth_ops authdes_ops = {
78         authdes_nextverf,
79         authdes_marshal,
80         authdes_validate,
81         authdes_refresh,
82         authdes_destroy
83 };
84 #ifdef foo
85 static bool_t   synchronize ( struct sockaddr *, struct timeval *);
86 #endif
87 /*
88  * This struct is pointed to by the ah_private field of an "AUTH *"
89  */
90 struct 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 sockaddr 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_long 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         
111
112 /*
113  * Create the client des authentication object
114  */     
115 AUTH *
116 authdes_create(char *servername,                /* network name of server */
117                u_int window,                    /* time to live */
118                struct sockaddr *syncaddr,       /* optional addr of host to sync with */
119                des_block *ckey)                 /* optional conversation key to use*/
120 {
121
122         AUTH *auth;
123         struct ad_private *ad;
124         char namebuf[MAXNETNAMELEN+1];
125         u_char  pkey_data[1024];
126
127         if (!getpublickey(servername, pkey_data))
128                 return(NULL);
129
130         /*
131          * Allocate everything now
132          */
133         auth = ALLOC(AUTH);
134         ad = ALLOC(struct ad_private);
135         getnetname(namebuf);
136
137         ad->ad_fullnamelen = RNDUP(strlen(namebuf));
138         ad->ad_fullname = (char *)mem_alloc(ad->ad_fullnamelen + 1);
139
140         ad->ad_servernamelen = strlen(servername);
141         ad->ad_servername = (char *)mem_alloc(ad->ad_servernamelen + 1);
142
143         if (auth == NULL || ad == NULL || ad->ad_fullname == NULL ||
144             ad->ad_servername == NULL) {
145                 debug("authdes_create: out of memory");
146                 goto failed;
147         }
148
149         /* 
150          * Set up private data
151          */
152         bcopy(namebuf, ad->ad_fullname, ad->ad_fullnamelen + 1);
153         bcopy(servername, ad->ad_servername, ad->ad_servernamelen + 1);
154         bcopy(pkey_data, ad->ad_pkey, strlen(pkey_data) + 1);
155         if (syncaddr != NULL) {
156                 ad->ad_syncaddr = *syncaddr;
157                 ad->ad_dosync = TRUE;
158         } else {
159                 ad->ad_dosync = FALSE;
160         }
161         ad->ad_window = window;
162         if (ckey == NULL) {
163                 if (key_gendes(&auth->ah_key) < 0) {
164                         debug("authdes_create: unable to gen conversation key");
165                         return (NULL);
166                 }
167         } else {
168                 auth->ah_key = *ckey;
169         }
170
171         /*
172          * Set up auth handle
173          */ 
174         auth->ah_cred.oa_flavor = AUTH_DES;
175         auth->ah_verf.oa_flavor = AUTH_DES;
176         auth->ah_ops = &authdes_ops;
177         auth->ah_private = (caddr_t)ad;
178
179         if (!authdes_refresh(auth)) {
180                 goto failed;
181         }       
182         return (auth);
183
184 failed:
185         if (auth != NULL) 
186                 FREE(auth, sizeof(AUTH)); 
187         if (ad != NULL) 
188                 FREE(ad, sizeof(struct ad_private));
189         if (ad->ad_fullname != NULL) 
190                 FREE(ad->ad_fullname, ad->ad_fullnamelen + 1);
191         if (ad->ad_servername != NULL) 
192                 FREE(ad->ad_servername, ad->ad_servernamelen + 1);
193         return (NULL);
194 }
195
196 /*
197  * Slightly modified version of authdes_create which takes the public key
198  * of the server principal as an argument. This spares us a call to
199  * getpublickey() which in the nameserver context can cause a deadlock.
200  */
201 AUTH *
202 authdes_pk_create(char *servername,             /* network name of server */
203                   netobj *pkey,                 /* public key of server */
204                   u_int window,                 /* time to live */
205                   char *timehost,               /* optional hostname to sync with */
206                   des_block *ckey,              /* optional conversation key to use */
207                   nis_server *srvr)             /* optional NIS+ server struct */
208 {
209         AUTH *auth;
210         struct ad_private *ad;
211         char namebuf[MAXNETNAMELEN+1];
212
213         /*
214          * Allocate everything now
215          */
216         auth = ALLOC(AUTH);
217         if (auth == NULL) {
218                 debug("authdes_pk_create: out of memory");
219                 return (NULL);
220         }
221         ad = ALLOC(struct ad_private);
222         if (ad == NULL) {
223                 debug("authdes_pk_create: out of memory");
224                 goto failed;
225         }
226         ad->ad_fullname = ad->ad_servername = NULL; /* Sanity reasons */
227         ad->ad_timehost = NULL;
228         ad->ad_netid = NULL;
229         ad->ad_uaddr = NULL;
230         ad->ad_nis_srvr = NULL;
231         ad->ad_timediff.tv_sec = 0;
232         ad->ad_timediff.tv_usec = 0;
233         memcpy(ad->ad_pkey, pkey->n_bytes, pkey->n_len);
234         if (!getnetname(namebuf))
235                 goto failed;
236         ad->ad_fullnamelen = RNDUP((u_int) strlen(namebuf));
237         ad->ad_fullname = (char *)mem_alloc(ad->ad_fullnamelen + 1);
238         ad->ad_servernamelen = strlen(servername);
239         ad->ad_servername = (char *)mem_alloc(ad->ad_servernamelen + 1);
240
241         if (ad->ad_fullname == NULL || ad->ad_servername == NULL) {
242                 debug("authdes_pk_create: out of memory");
243                 goto failed;
244         }
245         if (timehost != NULL) {
246                 ad->ad_timehost = (char *)mem_alloc(strlen(timehost) + 1);
247                 if (ad->ad_timehost == NULL) {
248                         debug("authdes_pk_create: out of memory");
249                         goto failed;
250                 }
251                 memcpy(ad->ad_timehost, timehost, strlen(timehost) + 1);
252                 ad->ad_dosync = TRUE;
253         } else if (srvr != NULL) {
254                 ad->ad_nis_srvr = srvr; /* transient */
255                 ad->ad_dosync = TRUE;
256         } else {
257                 ad->ad_dosync = FALSE;
258         }
259         memcpy(ad->ad_fullname, namebuf, ad->ad_fullnamelen + 1);
260         memcpy(ad->ad_servername, servername, ad->ad_servernamelen + 1);
261         ad->ad_window = window;
262         if (ckey == NULL) {
263                 if (key_gendes(&auth->ah_key) < 0) {
264                         debug("authdes_pk_create: unable to gen conversation key");
265                         goto failed;
266                 }
267         } else {
268                 auth->ah_key = *ckey;
269         }
270
271         /*
272          * Set up auth handle
273          */
274         auth->ah_cred.oa_flavor = AUTH_DES;
275         auth->ah_verf.oa_flavor = AUTH_DES;
276         auth->ah_ops = &authdes_ops;
277         auth->ah_private = (caddr_t)ad;
278
279         if (!authdes_refresh(auth)) {
280                 goto failed;
281         }
282         ad->ad_nis_srvr = NULL; /* not needed any longer */
283         return (auth);
284
285 failed:
286         if (auth)
287                 FREE(auth, sizeof (AUTH));
288         if (ad) {
289                 if (ad->ad_fullname)
290                         FREE(ad->ad_fullname, ad->ad_fullnamelen + 1);
291                 if (ad->ad_servername)
292                         FREE(ad->ad_servername, ad->ad_servernamelen + 1);
293                 if (ad->ad_timehost)
294                         FREE(ad->ad_timehost, strlen(ad->ad_timehost) + 1);
295                 if (ad->ad_netid)
296                         free(ad->ad_netid);
297                 if (ad->ad_uaddr)
298                         free(ad->ad_uaddr);
299                 FREE(ad, sizeof (struct ad_private));
300         }
301         return (NULL);
302 }
303 /*
304  * Implement the five authentication operations
305  */
306
307
308 /*
309  * 1. Next Verifier
310  */     
311 /*ARGSUSED*/
312 static void
313 authdes_nextverf(AUTH *auth)
314 {
315         /* what the heck am I supposed to do??? */
316 }
317                 
318
319
320 /*
321  * 2. Marshal
322  */
323 static bool_t
324 authdes_marshal(AUTH *auth, XDR *xdrs)
325 {
326         struct ad_private *ad = AUTH_PRIVATE(auth);
327         struct authdes_cred *cred = &ad->ad_cred;
328         struct authdes_verf *verf = &ad->ad_verf;
329         des_block cryptbuf[2];  
330         des_block ivec;
331         int status;
332         long len;
333         int32_t *ixdr;
334
335         /*
336          * Figure out the "time", accounting for any time difference
337          * with the server if necessary.
338          */
339         gettimeofday(&ad->ad_timestamp, (struct timezone *)NULL);
340         ad->ad_timestamp.tv_sec += ad->ad_timediff.tv_sec;
341         ad->ad_timestamp.tv_usec += ad->ad_timediff.tv_usec;
342         if (ad->ad_timestamp.tv_usec >= MILLION) {
343                 ad->ad_timestamp.tv_usec -= MILLION;
344                 ad->ad_timestamp.tv_sec += 1;
345         }
346
347         /*
348          * XDR the timestamp and possibly some other things, then
349          * encrypt them.
350          */
351         ixdr = (int32_t *)cryptbuf;
352         IXDR_PUT_LONG(ixdr, ad->ad_timestamp.tv_sec);
353         IXDR_PUT_LONG(ixdr, ad->ad_timestamp.tv_usec);
354         if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
355                 IXDR_PUT_U_LONG(ixdr, ad->ad_window);
356                 IXDR_PUT_U_LONG(ixdr, ad->ad_window - 1);
357                 ivec.key.high = ivec.key.low = 0;       
358                 status = cbc_crypt((char *)&auth->ah_key, (char *)cryptbuf, 
359                         2*sizeof(des_block), DES_ENCRYPT | DES_HW, (char *)&ivec);
360         } else {
361                 status = ecb_crypt((char *)&auth->ah_key, (char *)cryptbuf, 
362                         sizeof(des_block), DES_ENCRYPT | DES_HW);
363         }
364         if (DES_FAILED(status)) {
365                 debug("authdes_marshal: DES encryption failure");
366                 return (FALSE);
367         }
368         ad->ad_verf.adv_xtimestamp = cryptbuf[0];
369         if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
370                 ad->ad_cred.adc_fullname.window = cryptbuf[1].key.high;
371                 ad->ad_verf.adv_winverf = cryptbuf[1].key.low;
372         } else {
373                 ad->ad_cred.adc_nickname = ad->ad_nickname;
374                 ad->ad_verf.adv_winverf = 0;
375         }
376
377         /*
378          * Serialize the credential and verifier into opaque
379          * authentication data.
380          */
381         if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
382                 len = ((1 + 1 + 2 + 1)*BYTES_PER_XDR_UNIT + ad->ad_fullnamelen);
383         } else {
384                 len = (1 + 1)*BYTES_PER_XDR_UNIT;
385         }
386
387         if ((ixdr = xdr_inline(xdrs, 2*BYTES_PER_XDR_UNIT))) {
388                 IXDR_PUT_LONG(ixdr, AUTH_DES);
389                 IXDR_PUT_LONG(ixdr, len);
390         } else {
391                 ATTEMPT(xdr_putlong(xdrs, (long *)&auth->ah_cred.oa_flavor)); 
392                 ATTEMPT(xdr_putlong(xdrs, &len)); 
393         }
394         ATTEMPT(xdr_authdes_cred(xdrs, cred));
395
396         len = (2 + 1)*BYTES_PER_XDR_UNIT; 
397         if ((ixdr = xdr_inline(xdrs, 2*BYTES_PER_XDR_UNIT))) {
398                 IXDR_PUT_LONG(ixdr, AUTH_DES);
399                 IXDR_PUT_LONG(ixdr, len);
400         } else {
401                 ATTEMPT(xdr_putlong(xdrs, (long *)&auth->ah_verf.oa_flavor)); 
402                 ATTEMPT(xdr_putlong(xdrs, &len)); 
403         }
404         ATTEMPT(xdr_authdes_verf(xdrs, verf));
405         return (TRUE);
406 }
407
408
409 /*
410  * 3. Validate
411  */
412 static bool_t
413 authdes_validate(AUTH *auth, struct opaque_auth *rverf)
414 {
415         struct ad_private *ad = AUTH_PRIVATE(auth);
416         struct authdes_verf verf;
417         int status;
418         u_long *ixdr;
419
420         if (rverf->oa_length != (2 + 1) * BYTES_PER_XDR_UNIT) {
421                 return (FALSE);
422         }
423         ixdr = (u_long *)rverf->oa_base;
424         verf.adv_xtimestamp.key.high = (u_long)*ixdr++;
425         verf.adv_xtimestamp.key.low = (u_long)*ixdr++;
426         verf.adv_int_u = (u_long)*ixdr++;       /* nickname not XDR'd ! */
427
428         /*
429          * Decrypt the timestamp
430          */
431         status = ecb_crypt((char *)&auth->ah_key, (char *)&verf.adv_xtimestamp,
432                 sizeof(des_block), DES_DECRYPT | DES_HW);
433
434         if (DES_FAILED(status)) {
435                 debug("authdes_validate: DES decryption failure");
436                 return (FALSE);
437         }
438
439         /*
440          * xdr the decrypted timestamp 
441          */
442         ixdr = (u_long *)verf.adv_xtimestamp.c;
443         verf.adv_timestamp.tv_sec = IXDR_GET_LONG(ixdr) + 1;
444         verf.adv_timestamp.tv_usec = IXDR_GET_LONG(ixdr);
445
446         /*
447          * validate
448          */
449         if (bcmp((char *)&ad->ad_timestamp, (char *)&verf.adv_timestamp,
450                  sizeof(struct timeval)) != 0) {
451                 debug("authdes_validate: verifier mismatch\n");
452                 return (FALSE);
453         }
454
455         /*
456          * We have a nickname now, let's use it
457          */
458         ad->ad_nickname = verf.adv_nickname;    
459         ad->ad_cred.adc_namekind = ADN_NICKNAME; 
460         return (TRUE);  
461 }
462
463 /*
464  * 4. Refresh
465  */
466 static bool_t
467 authdes_refresh(AUTH *auth)
468 {
469         struct ad_private *ad = AUTH_PRIVATE(auth);
470         struct authdes_cred *cred = &ad->ad_cred;
471         netobj          pkey;
472
473         if (ad->ad_dosync &&
474 #ifdef old
475                         !synchronize(&ad->ad_syncaddr, &ad->ad_timediff)) {
476 #else
477                         !__rpc_get_time_offset(&ad->ad_timediff,ad->ad_nis_srvr,
478                                                ad->ad_timehost, &(ad->ad_uaddr),
479                                 (struct sockaddr_in *)&(ad->ad_syncaddr))) {
480 #endif
481                 /*
482                  * Hope the clocks are synced!
483                  */
484                 ad->ad_timediff.tv_sec = ad->ad_timediff.tv_usec = 0;
485                 ad->ad_dosync = 0;
486                 debug("authdes_refresh: unable to synchronize with server");
487         }
488         ad->ad_xkey = auth->ah_key;
489         pkey.n_bytes = (char *)(ad->ad_pkey);
490         pkey.n_len = strlen((char *)ad->ad_pkey) + 1;
491         if (key_encryptsession_pk(ad->ad_servername, &pkey, &ad->ad_xkey) < 0) {
492                 debug("authdes_create: unable to encrypt conversation key");
493                 return (FALSE);
494         }
495         cred->adc_fullname.key = ad->ad_xkey;
496         cred->adc_namekind = ADN_FULLNAME;
497         cred->adc_fullname.name = ad->ad_fullname;
498         return (TRUE);
499 }
500
501
502 /*
503  * 5. Destroy
504  */
505 static void
506 authdes_destroy(AUTH *auth)
507 {
508         struct ad_private *ad = AUTH_PRIVATE(auth);
509
510         FREE(ad->ad_fullname, ad->ad_fullnamelen + 1);
511         FREE(ad->ad_servername, ad->ad_servernamelen + 1);
512         FREE(ad, sizeof(struct ad_private));
513         FREE(auth, sizeof(AUTH));
514 }
515         
516
517 #ifdef old
518 /*
519  * Synchronize with the server at the given address, that is,
520  * adjust timep to reflect the delta between our clocks
521  */
522 static bool_t
523 synchronize(struct sockaddr *syncaddr, struct timeval *timep)
524 {
525         struct timeval mytime;
526         struct timeval timeout;
527
528         timeout.tv_sec = RTIME_TIMEOUT;
529         timeout.tv_usec = 0;
530         if (rtime((struct sockaddr_in *)syncaddr, timep, NULL /*&timeout*/) < 0) {
531                 return (FALSE);
532         }
533         gettimeofday(&mytime, (struct timezone *)NULL);
534         timep->tv_sec -= mytime.tv_sec;
535         if (mytime.tv_usec > timep->tv_usec) {
536                 timep->tv_sec -= 1;
537                 timep->tv_usec += MILLION;
538         }
539         timep->tv_usec -= mytime.tv_usec;
540         return (TRUE);
541 }
542 #endif