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 static void authdes_nextverf (AUTH
*);
66 static bool_t
authdes_marshal (AUTH
*, XDR
*);
67 static bool_t
authdes_validate (AUTH
*, struct opaque_auth
*);
68 static bool_t
authdes_refresh (AUTH
*);
69 static void authdes_destroy (AUTH
*);
70 static bool_t
synchronize (struct sockaddr
*, struct timeval
*)
73 static struct auth_ops authdes_ops
=
84 * This struct is pointed to by the ah_private field of an "AUTH *"
88 char *ad_fullname
; /* client's full name */
89 u_int ad_fullnamelen
; /* length of name, rounded up */
90 char *ad_servername
; /* server's full name */
91 u_int ad_servernamelen
; /* length of name, rounded up */
92 u_int ad_window
; /* client specified window */
93 bool_t ad_dosync
; /* synchronize? */
94 struct sockaddr ad_syncaddr
; /* remote host to synch with */
95 struct timeval ad_timediff
; /* server's time - client's time */
96 u_long ad_nickname
; /* server's nickname for client */
97 struct authdes_cred ad_cred
; /* storage for credential */
98 struct authdes_verf ad_verf
; /* storage for verifier */
99 struct timeval ad_timestamp
; /* timestamp sent */
100 des_block ad_xkey
; /* encrypted conversation key */
101 u_char ad_pkey
[1024]; /* Servers actual public key */
106 * Create the client des authentication object
109 authdes_create (const char *servername
, u_int window
,
110 struct sockaddr
*syncaddr
, des_block
*ckey
)
111 /* servername - network name of server */
112 /* window - time to live */
113 /* syncaddr - optional addr of host to sync with */
114 /* ckey - optional conversation key to use */
116 u_char pkey_data
[1024];
119 if (!getpublickey (servername
, pkey_data
))
122 pkey
.n_bytes
= (char *) pkey_data
;
123 pkey
.n_len
= strlen ((char *) pkey_data
) + 1;
124 return authdes_pk_create (servername
, &pkey
, window
, syncaddr
, ckey
);
128 authdes_pk_create (const char *servername
, netobj
*pkey
, u_int window
,
129 struct sockaddr
*syncaddr
, des_block
*ckey
)
132 struct ad_private
*ad
;
133 char namebuf
[MAXNETNAMELEN
+ 1];
136 * Allocate everything now
139 ad
= ALLOC (struct ad_private
);
140 memset (ad
, 0, sizeof (struct ad_private
));
141 memcpy (ad
->ad_pkey
, pkey
->n_bytes
, pkey
->n_len
);
142 if (!getnetname (namebuf
))
144 ad
->ad_fullnamelen
= RNDUP (strlen (namebuf
));
145 ad
->ad_fullname
= mem_alloc (ad
->ad_fullnamelen
+ 1);
147 ad
->ad_servernamelen
= strlen (servername
);
148 ad
->ad_servername
= mem_alloc (ad
->ad_servernamelen
+ 1);
150 if (auth
== NULL
|| ad
== NULL
|| ad
->ad_fullname
== NULL
||
151 ad
->ad_servername
== NULL
)
153 debug ("authdes_create: out of memory");
158 * Set up private data
160 bcopy (namebuf
, ad
->ad_fullname
, ad
->ad_fullnamelen
+ 1);
161 bcopy (servername
, ad
->ad_servername
, ad
->ad_servernamelen
+ 1);
162 ad
->ad_timediff
.tv_sec
= ad
->ad_timediff
.tv_usec
= 0;
163 if (syncaddr
!= NULL
)
165 ad
->ad_syncaddr
= *syncaddr
;
166 ad
->ad_dosync
= TRUE
;
169 ad
->ad_dosync
= FALSE
;
171 ad
->ad_window
= window
;
174 if (key_gendes (&auth
->ah_key
) < 0)
176 debug ("authdes_create: unable to gen conversation key");
181 auth
->ah_key
= *ckey
;
186 auth
->ah_cred
.oa_flavor
= AUTH_DES
;
187 auth
->ah_verf
.oa_flavor
= AUTH_DES
;
188 auth
->ah_ops
= &authdes_ops
;
189 auth
->ah_private
= (caddr_t
) ad
;
191 if (!authdes_refresh (auth
))
198 FREE (auth
, sizeof (AUTH
));
201 if (ad
->ad_fullname
!= NULL
)
202 FREE (ad
->ad_fullname
, ad
->ad_fullnamelen
+ 1);
203 if (ad
->ad_servername
!= NULL
)
204 FREE (ad
->ad_servername
, ad
->ad_servernamelen
+ 1);
205 FREE (ad
, sizeof (struct ad_private
));
211 * Implement the five authentication operations
220 authdes_nextverf (AUTH
* auth
)
222 /* what the heck am I supposed to do??? */
231 authdes_marshal (AUTH
* auth
, XDR
* xdrs
)
233 struct ad_private
*ad
= AUTH_PRIVATE (auth
);
234 struct authdes_cred
*cred
= &ad
->ad_cred
;
235 struct authdes_verf
*verf
= &ad
->ad_verf
;
236 des_block cryptbuf
[2];
243 * Figure out the "time", accounting for any time difference
244 * with the server if necessary.
246 __gettimeofday (&ad
->ad_timestamp
, (struct timezone
*) NULL
);
247 ad
->ad_timestamp
.tv_sec
+= ad
->ad_timediff
.tv_sec
;
248 ad
->ad_timestamp
.tv_usec
+= ad
->ad_timediff
.tv_usec
;
249 if (ad
->ad_timestamp
.tv_usec
>= MILLION
)
251 ad
->ad_timestamp
.tv_usec
-= MILLION
;
252 ad
->ad_timestamp
.tv_sec
+= 1;
256 * XDR the timestamp and possibly some other things, then
259 ixdr
= (long *) cryptbuf
;
260 IXDR_PUT_LONG (ixdr
, ad
->ad_timestamp
.tv_sec
);
261 IXDR_PUT_LONG (ixdr
, ad
->ad_timestamp
.tv_usec
);
262 if (ad
->ad_cred
.adc_namekind
== ADN_FULLNAME
)
264 IXDR_PUT_U_LONG (ixdr
, ad
->ad_window
);
265 IXDR_PUT_U_LONG (ixdr
, ad
->ad_window
- 1);
266 ivec
.key
.high
= ivec
.key
.low
= 0;
267 status
= cbc_crypt ((char *) &auth
->ah_key
, (char *) cryptbuf
,
268 2 * sizeof (des_block
), DES_ENCRYPT
| DES_HW
, (char *) &ivec
);
272 status
= ecb_crypt ((char *) &auth
->ah_key
, (char *) cryptbuf
,
273 sizeof (des_block
), DES_ENCRYPT
| DES_HW
);
275 if (DES_FAILED (status
))
277 debug ("authdes_marshal: DES encryption failure");
280 ad
->ad_verf
.adv_xtimestamp
= cryptbuf
[0];
281 if (ad
->ad_cred
.adc_namekind
== ADN_FULLNAME
)
283 ad
->ad_cred
.adc_fullname
.window
= cryptbuf
[1].key
.high
;
284 ad
->ad_verf
.adv_winverf
= cryptbuf
[1].key
.low
;
288 ad
->ad_cred
.adc_nickname
= ad
->ad_nickname
;
289 ad
->ad_verf
.adv_winverf
= 0;
293 * Serialize the credential and verifier into opaque
294 * authentication data.
296 if (ad
->ad_cred
.adc_namekind
== ADN_FULLNAME
)
298 len
= ((1 + 1 + 2 + 1) * BYTES_PER_XDR_UNIT
+ ad
->ad_fullnamelen
);
302 len
= (1 + 1) * BYTES_PER_XDR_UNIT
;
305 if ((ixdr
= xdr_inline (xdrs
, 2 * BYTES_PER_XDR_UNIT
)) != NULL
)
307 IXDR_PUT_LONG (ixdr
, AUTH_DES
);
308 IXDR_PUT_LONG (ixdr
, len
);
312 ATTEMPT (xdr_putlong (xdrs
, (long *)&auth
->ah_cred
.oa_flavor
));
313 ATTEMPT (xdr_putlong (xdrs
, (long *)&len
));
315 ATTEMPT (xdr_authdes_cred (xdrs
, cred
));
317 len
= (2 + 1) * BYTES_PER_XDR_UNIT
;
318 if ((ixdr
= xdr_inline (xdrs
, 2 * BYTES_PER_XDR_UNIT
)) != NULL
)
320 IXDR_PUT_LONG (ixdr
, AUTH_DES
);
321 IXDR_PUT_LONG (ixdr
, len
);
325 ATTEMPT (xdr_putlong (xdrs
, (long *)&auth
->ah_verf
.oa_flavor
));
326 ATTEMPT (xdr_putlong (xdrs
, (long *)&len
));
328 ATTEMPT (xdr_authdes_verf (xdrs
, verf
));
337 authdes_validate (AUTH
* auth
, struct opaque_auth
*rverf
)
339 struct ad_private
*ad
= AUTH_PRIVATE (auth
);
340 struct authdes_verf verf
;
342 register u_long
*ixdr
;
344 if (rverf
->oa_length
!= (2 + 1) * BYTES_PER_XDR_UNIT
)
347 ixdr
= (u_long
*) rverf
->oa_base
;
348 verf
.adv_xtimestamp
.key
.high
= (u_long
) * ixdr
++;
349 verf
.adv_xtimestamp
.key
.low
= (u_long
) * ixdr
++;
350 verf
.adv_int_u
= (u_long
) * ixdr
++; /* nickname not XDR'd ! */
353 * Decrypt the timestamp
355 status
= ecb_crypt ((char *) &auth
->ah_key
, (char *) &verf
.adv_xtimestamp
,
356 sizeof (des_block
), DES_DECRYPT
| DES_HW
);
358 if (DES_FAILED (status
))
360 debug ("authdes_validate: DES decryption failure");
365 * xdr the decrypted timestamp
367 ixdr
= (u_long
*) verf
.adv_xtimestamp
.c
;
368 verf
.adv_timestamp
.tv_sec
= IXDR_GET_LONG (ixdr
) + 1;
369 verf
.adv_timestamp
.tv_usec
= IXDR_GET_LONG (ixdr
);
374 if (memcmp ((char *) &ad
->ad_timestamp
, (char *) &verf
.adv_timestamp
,
375 sizeof (struct timeval
)) != 0)
377 debug ("authdes_validate: verifier mismatch\n");
382 * We have a nickname now, let's use it
384 ad
->ad_nickname
= verf
.adv_nickname
;
385 ad
->ad_cred
.adc_namekind
= ADN_NICKNAME
;
393 authdes_refresh (AUTH
* auth
)
396 struct ad_private
*ad
= AUTH_PRIVATE (auth
);
397 struct authdes_cred
*cred
= &ad
->ad_cred
;
400 !synchronize (&ad
->ad_syncaddr
, &ad
->ad_timediff
))
403 * Hope the clocks are synced!
405 ad
->ad_timediff
.tv_sec
= ad
->ad_timediff
.tv_usec
= 0;
406 debug ("authdes_refresh: unable to synchronize with server");
408 ad
->ad_xkey
= auth
->ah_key
;
409 pkey
.n_bytes
= (char *) (ad
->ad_pkey
);
410 pkey
.n_len
= strlen ((char *) ad
->ad_pkey
) + 1;
411 if (key_encryptsession_pk (ad
->ad_servername
, &pkey
, &ad
->ad_xkey
) < 0)
413 debug ("authdes_create: unable to encrypt conversation key");
416 cred
->adc_fullname
.key
= ad
->ad_xkey
;
417 cred
->adc_namekind
= ADN_FULLNAME
;
418 cred
->adc_fullname
.name
= ad
->ad_fullname
;
426 authdes_destroy (AUTH
* auth
)
428 struct ad_private
*ad
= AUTH_PRIVATE (auth
);
430 FREE (ad
->ad_fullname
, ad
->ad_fullnamelen
+ 1);
431 FREE (ad
->ad_servername
, ad
->ad_servernamelen
+ 1);
432 FREE (ad
, sizeof (struct ad_private
));
433 FREE (auth
, sizeof (AUTH
));
437 * Synchronize with the server at the given address, that is,
438 * adjust timep to reflect the delta between our clocks
442 synchronize (struct sockaddr
*syncaddr
, struct timeval
*timep
)
444 struct timeval mytime
;
445 struct timeval timeout
;
447 timeout
.tv_sec
= RTIME_TIMEOUT
;
449 if (rtime ((struct sockaddr_in
*) syncaddr
, timep
, &timeout
) < 0)
452 __gettimeofday (&mytime
, (struct timezone
*) NULL
);
453 timep
->tv_sec
-= mytime
.tv_sec
;
454 if (mytime
.tv_usec
> timep
->tv_usec
)
457 timep
->tv_usec
+= MILLION
;
459 timep
->tv_usec
-= mytime
.tv_usec
;