1 #if defined(LIBC_SCCS) && !defined(lint)
2 static char sccsid
[] = "@(#)auth_des.c 2.2 88/07/29 4.0 RPCSRC; from 1.9 88/02/08 SMI";
5 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
6 * unrestricted use provided that this legend is included on all tape
7 * media and as a part of the software program in whole or part. Users
8 * may copy or modify Sun RPC without charge, but are not authorized
9 * to license or distribute it to anyone else except as part of a product or
10 * program developed by the user.
12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
16 * Sun RPC is provided with no support and without any obligation on the
17 * part of Sun Microsystems, Inc. to assist in its use, correction,
18 * modification or enhancement.
20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22 * OR ANY PART THEREOF.
24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25 * or profits or other special, indirect and consequential damages, even if
26 * Sun has been advised of the possibility of such damages.
28 * Sun Microsystems, Inc.
30 * Mountain View, California 94043
33 * Copyright (c) 1988 by Sun Microsystems, Inc.
36 * auth_des.c, client-side implementation of DES authentication
40 #include <rpc/des_crypt.h>
41 #include <rpc/types.h>
43 #include <rpc/auth_des.h>
45 #include <netinet/in.h> /* XXX: just to get htonl() and ntohl() */
46 #include <sys/socket.h>
48 #define MILLION 1000000L
49 #define RTIME_TIMEOUT 5 /* seconds to wait for sync */
51 #define AUTH_PRIVATE(auth) (struct ad_private *) auth->ah_private
52 #define ALLOC(object_type) (object_type *) mem_alloc(sizeof(object_type))
53 #define FREE(ptr, size) mem_free((char *)(ptr), (int) size)
54 #define ATTEMPT(xdr_op) if (!(xdr_op)) return (FALSE)
56 #define debug(msg) /* printf("%s\n", msg) */
58 extern int rtime (struct sockaddr_in
*, struct timeval
*, struct timeval
*);
59 extern bool_t
xdr_authdes_cred (XDR
*, struct authdes_cred
*);
60 extern bool_t
xdr_authdes_verf (XDR
*, struct authdes_verf
*);
63 * DES authenticator operations vector
65 AUTH
*authdes_create (const char *, u_int
, struct sockaddr
*,
67 AUTH
*authdes_pk_create (const char *, netobj
*, u_int
,
68 struct sockaddr
*, des_block
*);
69 static void authdes_nextverf (AUTH
*);
70 static bool_t
authdes_marshal (AUTH
*, XDR
*);
71 static bool_t
authdes_validate (AUTH
*, struct opaque_auth
*);
72 static bool_t
authdes_refresh (AUTH
*);
73 static void authdes_destroy (AUTH
*);
74 static bool_t
synchronize (struct sockaddr
*, struct timeval
*)
77 static struct auth_ops authdes_ops
=
88 * 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 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]; /* Servers actual public key */
110 * Create the client des authentication object
113 authdes_create (const char *servername
, u_int window
,
114 struct sockaddr
*syncaddr
, des_block
* ckey
)
115 /* servername - network name of server */
116 /* window - time to live */
117 /* syncaddr - optional addr of host to sync with */
118 /* ckey - optional conversation key to use */
120 u_char pkey_data
[1024];
123 if (!getpublickey (servername
, pkey_data
))
126 pkey
.n_bytes
= (char *) pkey_data
;
127 pkey
.n_len
= strlen ((char *) pkey_data
) + 1;
128 return authdes_pk_create (servername
, &pkey
, window
, syncaddr
, ckey
);
132 authdes_pk_create (const char *servername
, netobj
* pkey
, u_int window
,
133 struct sockaddr
* syncaddr
, des_block
* ckey
)
136 struct ad_private
*ad
;
137 char namebuf
[MAXNETNAMELEN
+ 1];
140 * Allocate everything now
143 ad
= ALLOC (struct ad_private
);
144 memset (ad
, 0, sizeof (struct ad_private
));
145 memcpy (ad
->ad_pkey
, pkey
->n_bytes
, pkey
->n_len
);
146 if (!getnetname (namebuf
))
148 ad
->ad_fullnamelen
= RNDUP (strlen (namebuf
));
149 ad
->ad_fullname
= mem_alloc (ad
->ad_fullnamelen
+ 1);
151 ad
->ad_servernamelen
= strlen (servername
);
152 ad
->ad_servername
= mem_alloc (ad
->ad_servernamelen
+ 1);
154 if (auth
== NULL
|| ad
== NULL
|| ad
->ad_fullname
== NULL
||
155 ad
->ad_servername
== NULL
)
157 debug ("authdes_create: out of memory");
162 * Set up private data
164 bcopy (namebuf
, ad
->ad_fullname
, ad
->ad_fullnamelen
+ 1);
165 bcopy (servername
, ad
->ad_servername
, ad
->ad_servernamelen
+ 1);
166 ad
->ad_timediff
.tv_sec
= ad
->ad_timediff
.tv_usec
= 0;
167 if (syncaddr
!= NULL
)
169 ad
->ad_syncaddr
= *syncaddr
;
170 ad
->ad_dosync
= TRUE
;
173 ad
->ad_dosync
= FALSE
;
175 ad
->ad_window
= window
;
178 if (key_gendes (&auth
->ah_key
) < 0)
180 debug ("authdes_create: unable to gen conversation key");
185 auth
->ah_key
= *ckey
;
190 auth
->ah_cred
.oa_flavor
= AUTH_DES
;
191 auth
->ah_verf
.oa_flavor
= AUTH_DES
;
192 auth
->ah_ops
= &authdes_ops
;
193 auth
->ah_private
= (caddr_t
) ad
;
195 if (!authdes_refresh (auth
))
202 FREE (auth
, sizeof (AUTH
));
205 if (ad
->ad_fullname
!= NULL
)
206 FREE (ad
->ad_fullname
, ad
->ad_fullnamelen
+ 1);
207 if (ad
->ad_servername
!= NULL
)
208 FREE (ad
->ad_servername
, ad
->ad_servernamelen
+ 1);
209 FREE (ad
, sizeof (struct ad_private
));
215 * Implement the five authentication operations
224 authdes_nextverf (AUTH
* auth
)
226 /* what the heck am I supposed to do??? */
235 authdes_marshal (AUTH
* auth
, XDR
* xdrs
)
237 struct ad_private
*ad
= AUTH_PRIVATE (auth
);
238 struct authdes_cred
*cred
= &ad
->ad_cred
;
239 struct authdes_verf
*verf
= &ad
->ad_verf
;
240 des_block cryptbuf
[2];
247 * Figure out the "time", accounting for any time difference
248 * with the server if necessary.
250 gettimeofday (&ad
->ad_timestamp
, (struct timezone
*) NULL
);
251 ad
->ad_timestamp
.tv_sec
+= ad
->ad_timediff
.tv_sec
;
252 ad
->ad_timestamp
.tv_usec
+= ad
->ad_timediff
.tv_usec
;
253 if (ad
->ad_timestamp
.tv_usec
>= MILLION
)
255 ad
->ad_timestamp
.tv_usec
-= MILLION
;
256 ad
->ad_timestamp
.tv_sec
+= 1;
260 * XDR the timestamp and possibly some other things, then
263 ixdr
= (long *) cryptbuf
;
264 IXDR_PUT_LONG (ixdr
, ad
->ad_timestamp
.tv_sec
);
265 IXDR_PUT_LONG (ixdr
, ad
->ad_timestamp
.tv_usec
);
266 if (ad
->ad_cred
.adc_namekind
== ADN_FULLNAME
)
268 IXDR_PUT_U_LONG (ixdr
, ad
->ad_window
);
269 IXDR_PUT_U_LONG (ixdr
, ad
->ad_window
- 1);
270 ivec
.key
.high
= ivec
.key
.low
= 0;
271 status
= cbc_crypt ((char *) &auth
->ah_key
, (char *) cryptbuf
,
272 2 * sizeof (des_block
), DES_ENCRYPT
| DES_HW
, (char *) &ivec
);
276 status
= ecb_crypt ((char *) &auth
->ah_key
, (char *) cryptbuf
,
277 sizeof (des_block
), DES_ENCRYPT
| DES_HW
);
279 if (DES_FAILED (status
))
281 debug ("authdes_marshal: DES encryption failure");
284 ad
->ad_verf
.adv_xtimestamp
= cryptbuf
[0];
285 if (ad
->ad_cred
.adc_namekind
== ADN_FULLNAME
)
287 ad
->ad_cred
.adc_fullname
.window
= cryptbuf
[1].key
.high
;
288 ad
->ad_verf
.adv_winverf
= cryptbuf
[1].key
.low
;
292 ad
->ad_cred
.adc_nickname
= ad
->ad_nickname
;
293 ad
->ad_verf
.adv_winverf
= 0;
297 * Serialize the credential and verifier into opaque
298 * authentication data.
300 if (ad
->ad_cred
.adc_namekind
== ADN_FULLNAME
)
302 len
= ((1 + 1 + 2 + 1) * BYTES_PER_XDR_UNIT
+ ad
->ad_fullnamelen
);
306 len
= (1 + 1) * BYTES_PER_XDR_UNIT
;
309 if ((ixdr
= xdr_inline (xdrs
, 2 * BYTES_PER_XDR_UNIT
)) != NULL
)
311 IXDR_PUT_LONG (ixdr
, AUTH_DES
);
312 IXDR_PUT_LONG (ixdr
, len
);
316 ATTEMPT (xdr_putlong (xdrs
, (long *)&auth
->ah_cred
.oa_flavor
));
317 ATTEMPT (xdr_putlong (xdrs
, (long *)&len
));
319 ATTEMPT (xdr_authdes_cred (xdrs
, cred
));
321 len
= (2 + 1) * BYTES_PER_XDR_UNIT
;
322 if ((ixdr
= xdr_inline (xdrs
, 2 * BYTES_PER_XDR_UNIT
)) != NULL
)
324 IXDR_PUT_LONG (ixdr
, AUTH_DES
);
325 IXDR_PUT_LONG (ixdr
, len
);
329 ATTEMPT (xdr_putlong (xdrs
, (long *)&auth
->ah_verf
.oa_flavor
));
330 ATTEMPT (xdr_putlong (xdrs
, (long *)&len
));
332 ATTEMPT (xdr_authdes_verf (xdrs
, verf
));
341 authdes_validate (AUTH
* auth
, struct opaque_auth
*rverf
)
343 struct ad_private
*ad
= AUTH_PRIVATE (auth
);
344 struct authdes_verf verf
;
346 register u_long
*ixdr
;
348 if (rverf
->oa_length
!= (2 + 1) * BYTES_PER_XDR_UNIT
)
351 ixdr
= (u_long
*) rverf
->oa_base
;
352 verf
.adv_xtimestamp
.key
.high
= (u_long
) * ixdr
++;
353 verf
.adv_xtimestamp
.key
.low
= (u_long
) * ixdr
++;
354 verf
.adv_int_u
= (u_long
) * ixdr
++; /* nickname not XDR'd ! */
357 * Decrypt the timestamp
359 status
= ecb_crypt ((char *) &auth
->ah_key
, (char *) &verf
.adv_xtimestamp
,
360 sizeof (des_block
), DES_DECRYPT
| DES_HW
);
362 if (DES_FAILED (status
))
364 debug ("authdes_validate: DES decryption failure");
369 * xdr the decrypted timestamp
371 ixdr
= (u_long
*) verf
.adv_xtimestamp
.c
;
372 verf
.adv_timestamp
.tv_sec
= IXDR_GET_LONG (ixdr
) + 1;
373 verf
.adv_timestamp
.tv_usec
= IXDR_GET_LONG (ixdr
);
378 if (bcmp ((char *) &ad
->ad_timestamp
, (char *) &verf
.adv_timestamp
,
379 sizeof (struct timeval
)) != 0)
381 debug ("authdes_validate: verifier mismatch\n");
386 * We have a nickname now, let's use it
388 ad
->ad_nickname
= verf
.adv_nickname
;
389 ad
->ad_cred
.adc_namekind
= ADN_NICKNAME
;
397 authdes_refresh (AUTH
* auth
)
400 struct ad_private
*ad
= AUTH_PRIVATE (auth
);
401 struct authdes_cred
*cred
= &ad
->ad_cred
;
404 !synchronize (&ad
->ad_syncaddr
, &ad
->ad_timediff
))
407 * Hope the clocks are synced!
409 ad
->ad_timediff
.tv_sec
= ad
->ad_timediff
.tv_usec
= 0;
410 debug ("authdes_refresh: unable to synchronize with server");
412 ad
->ad_xkey
= auth
->ah_key
;
413 pkey
.n_bytes
= (char *) (ad
->ad_pkey
);
414 pkey
.n_len
= strlen ((char *) ad
->ad_pkey
) + 1;
415 if (key_encryptsession_pk (ad
->ad_servername
, &pkey
, &ad
->ad_xkey
) < 0)
417 debug ("authdes_create: unable to encrypt conversation key");
420 cred
->adc_fullname
.key
= ad
->ad_xkey
;
421 cred
->adc_namekind
= ADN_FULLNAME
;
422 cred
->adc_fullname
.name
= ad
->ad_fullname
;
430 authdes_destroy (AUTH
* auth
)
432 struct ad_private
*ad
= AUTH_PRIVATE (auth
);
434 FREE (ad
->ad_fullname
, ad
->ad_fullnamelen
+ 1);
435 FREE (ad
->ad_servername
, ad
->ad_servernamelen
+ 1);
436 FREE (ad
, sizeof (struct ad_private
));
437 FREE (auth
, sizeof (AUTH
));
441 * Synchronize with the server at the given address, that is,
442 * adjust timep to reflect the delta between our clocks
446 synchronize (struct sockaddr
*syncaddr
, struct timeval
*timep
)
448 struct timeval mytime
;
449 struct timeval timeout
;
451 timeout
.tv_sec
= RTIME_TIMEOUT
;
453 if (rtime ((struct sockaddr_in
*) syncaddr
, timep
, &timeout
) < 0)
456 gettimeofday (&mytime
, (struct timezone
*) NULL
);
457 timep
->tv_sec
-= mytime
.tv_sec
;
458 if (mytime
.tv_usec
> timep
->tv_usec
)
461 timep
->tv_usec
+= MILLION
;
463 timep
->tv_usec
-= mytime
.tv_usec
;