]>
Commit | Line | Data |
---|---|---|
800d775e | 1 | /* |
a7ab6ec8 | 2 | * Copyright (c) 2010, Oracle America, Inc. |
cb636bb2 | 3 | * |
a7ab6ec8 UD |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions are | |
6 | * met: | |
cb636bb2 | 7 | * |
a7ab6ec8 UD |
8 | * * Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * * Redistributions in binary form must reproduce the above | |
11 | * copyright notice, this list of conditions and the following | |
12 | * disclaimer in the documentation and/or other materials | |
13 | * provided with the distribution. | |
14 | * * Neither the name of the "Oracle America, Inc." nor the names of its | |
15 | * contributors may be used to endorse or promote products derived | |
16 | * from this software without specific prior written permission. | |
800d775e | 17 | * |
a7ab6ec8 UD |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
25 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
27 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
800d775e UD |
30 | */ |
31 | /* | |
32 | * auth_des.c, client-side implementation of DES authentication | |
33 | */ | |
34 | ||
35 | #include <string.h> | |
e054f494 | 36 | #include <stdint.h> |
800d775e UD |
37 | #include <rpc/des_crypt.h> |
38 | #include <rpc/types.h> | |
39 | #include <rpc/auth.h> | |
40 | #include <rpc/auth_des.h> | |
41 | #include <rpc/xdr.h> | |
42 | #include <netinet/in.h> /* XXX: just to get htonl() and ntohl() */ | |
43 | #include <sys/socket.h> | |
4a39c34c | 44 | #include <time.h> |
82f43dd2 | 45 | #include <shlib-compat.h> |
800d775e UD |
46 | |
47 | #define MILLION 1000000L | |
48 | #define RTIME_TIMEOUT 5 /* seconds to wait for sync */ | |
49 | ||
50 | #define AUTH_PRIVATE(auth) (struct ad_private *) auth->ah_private | |
51 | #define ALLOC(object_type) (object_type *) mem_alloc(sizeof(object_type)) | |
52 | #define FREE(ptr, size) mem_free((char *)(ptr), (int) size) | |
53 | #define ATTEMPT(xdr_op) if (!(xdr_op)) return (FALSE) | |
54 | ||
55 | #define debug(msg) /* printf("%s\n", msg) */ | |
56 | ||
800d775e UD |
57 | |
58 | /* | |
59 | * DES authenticator operations vector | |
60 | */ | |
800d775e UD |
61 | static void authdes_nextverf (AUTH *); |
62 | static bool_t authdes_marshal (AUTH *, XDR *); | |
63 | static bool_t authdes_validate (AUTH *, struct opaque_auth *); | |
64 | static bool_t authdes_refresh (AUTH *); | |
65 | static void authdes_destroy (AUTH *); | |
ca4ec803 | 66 | static bool_t synchronize (struct sockaddr *, struct rpc_timeval *); |
800d775e | 67 | |
31d7b14c | 68 | static const struct auth_ops authdes_ops = { |
800d775e UD |
69 | authdes_nextverf, |
70 | authdes_marshal, | |
71 | authdes_validate, | |
72 | authdes_refresh, | |
73 | authdes_destroy | |
74 | }; | |
75 | ||
76 | ||
77 | /* | |
78 | * This struct is pointed to by the ah_private field of an "AUTH *" | |
79 | */ | |
ee586e0e UD |
80 | struct ad_private { |
81 | char *ad_fullname; /* client's full name */ | |
7b57bfe5 UD |
82 | u_int ad_fullnamelen; /* length of name, rounded up */ |
83 | char *ad_servername; /* server's full name */ | |
ee586e0e UD |
84 | u_int ad_servernamelen; /* length of name, rounded up */ |
85 | uint32_t ad_window; /* client specified window */ | |
86 | bool_t ad_dosync; /* synchronize? */ | |
87 | struct sockaddr ad_syncaddr; /* remote host to synch with */ | |
f8afba91 UD |
88 | struct rpc_timeval ad_timediff; /* server's time - client's time */ |
89 | uint32_t ad_nickname; /* server's nickname for client */ | |
ee586e0e UD |
90 | struct authdes_cred ad_cred; /* storage for credential */ |
91 | struct authdes_verf ad_verf; /* storage for verifier */ | |
f8afba91 | 92 | struct rpc_timeval ad_timestamp; /* timestamp sent */ |
ee586e0e | 93 | des_block ad_xkey; /* encrypted conversation key */ |
7b57bfe5 | 94 | u_char ad_pkey[1024]; /* Servers actual public key */ |
ee586e0e | 95 | }; |
800d775e UD |
96 | |
97 | ||
98 | /* | |
99 | * Create the client des authentication object | |
100 | */ | |
101 | AUTH * | |
9a0a462c | 102 | authdes_create (const char *servername, u_int window, |
26a60f90 | 103 | struct sockaddr *syncaddr, des_block *ckey) |
800d775e UD |
104 | /* servername - network name of server */ |
105 | /* window - time to live */ | |
106 | /* syncaddr - optional addr of host to sync with */ | |
107 | /* ckey - optional conversation key to use */ | |
108 | { | |
9cfe5381 | 109 | char pkey_data[1024]; |
800d775e UD |
110 | netobj pkey; |
111 | ||
112 | if (!getpublickey (servername, pkey_data)) | |
f8afba91 | 113 | return NULL; |
800d775e | 114 | |
9cfe5381 RM |
115 | pkey.n_bytes = pkey_data; |
116 | pkey.n_len = strlen (pkey_data) + 1; | |
7b57bfe5 | 117 | return authdes_pk_create (servername, &pkey, window, syncaddr, ckey); |
800d775e | 118 | } |
7b57bfe5 UD |
119 | #ifdef EXPORT_RPC_SYMBOLS |
120 | libc_hidden_def (authdes_create) | |
121 | #else | |
021db4be | 122 | libc_hidden_nolink_sunrpc (authdes_create, GLIBC_2_1) |
7b57bfe5 | 123 | #endif |
800d775e UD |
124 | |
125 | AUTH * | |
26a60f90 UD |
126 | authdes_pk_create (const char *servername, netobj *pkey, u_int window, |
127 | struct sockaddr *syncaddr, des_block *ckey) | |
800d775e UD |
128 | { |
129 | AUTH *auth; | |
130 | struct ad_private *ad; | |
131 | char namebuf[MAXNETNAMELEN + 1]; | |
132 | ||
133 | /* | |
134 | * Allocate everything now | |
135 | */ | |
136 | auth = ALLOC (AUTH); | |
137 | ad = ALLOC (struct ad_private); | |
f8afba91 UD |
138 | |
139 | if (auth == NULL || ad == NULL) | |
140 | { | |
141 | debug ("authdes_create: out of memory"); | |
142 | goto failed; | |
143 | } | |
144 | ||
dfd2257a | 145 | memset (ad, 0, sizeof (struct ad_private)); |
800d775e UD |
146 | memcpy (ad->ad_pkey, pkey->n_bytes, pkey->n_len); |
147 | if (!getnetname (namebuf)) | |
148 | goto failed; | |
149 | ad->ad_fullnamelen = RNDUP (strlen (namebuf)); | |
150 | ad->ad_fullname = mem_alloc (ad->ad_fullnamelen + 1); | |
151 | ||
152 | ad->ad_servernamelen = strlen (servername); | |
153 | ad->ad_servername = mem_alloc (ad->ad_servernamelen + 1); | |
154 | ||
f8afba91 | 155 | if (ad->ad_fullname == NULL || ad->ad_servername == NULL) |
800d775e UD |
156 | { |
157 | debug ("authdes_create: out of memory"); | |
158 | goto failed; | |
159 | } | |
160 | ||
161 | /* | |
162 | * Set up private data | |
163 | */ | |
f8afba91 UD |
164 | memcpy (ad->ad_fullname, namebuf, ad->ad_fullnamelen + 1); |
165 | memcpy (ad->ad_servername, servername, ad->ad_servernamelen + 1); | |
9a0a462c | 166 | ad->ad_timediff.tv_sec = ad->ad_timediff.tv_usec = 0; |
800d775e UD |
167 | if (syncaddr != NULL) |
168 | { | |
169 | ad->ad_syncaddr = *syncaddr; | |
170 | ad->ad_dosync = TRUE; | |
171 | } | |
172 | else | |
173 | ad->ad_dosync = FALSE; | |
174 | ||
175 | ad->ad_window = window; | |
176 | if (ckey == NULL) | |
177 | { | |
178 | if (key_gendes (&auth->ah_key) < 0) | |
179 | { | |
180 | debug ("authdes_create: unable to gen conversation key"); | |
1eab3551 | 181 | goto failed; |
800d775e UD |
182 | } |
183 | } | |
184 | else | |
185 | auth->ah_key = *ckey; | |
186 | ||
187 | /* | |
188 | * Set up auth handle | |
189 | */ | |
190 | auth->ah_cred.oa_flavor = AUTH_DES; | |
191 | auth->ah_verf.oa_flavor = AUTH_DES; | |
31d7b14c | 192 | auth->ah_ops = (struct auth_ops *) &authdes_ops; |
800d775e UD |
193 | auth->ah_private = (caddr_t) ad; |
194 | ||
195 | if (!authdes_refresh (auth)) | |
196 | goto failed; | |
197 | ||
f8afba91 | 198 | return auth; |
800d775e UD |
199 | |
200 | failed: | |
201 | if (auth != NULL) | |
202 | FREE (auth, sizeof (AUTH)); | |
203 | if (ad != NULL) | |
9a0a462c | 204 | { |
9a0a462c UD |
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); | |
dfd2257a | 209 | FREE (ad, sizeof (struct ad_private)); |
9a0a462c | 210 | } |
f8afba91 | 211 | return NULL; |
800d775e | 212 | } |
7b57bfe5 UD |
213 | #ifdef EXPORT_RPC_SYMBOLS |
214 | libc_hidden_def (authdes_pk_create) | |
215 | #else | |
021db4be | 216 | libc_hidden_nolink_sunrpc (authdes_pk_create, GLIBC_2_1) |
7b57bfe5 | 217 | #endif |
800d775e UD |
218 | |
219 | /* | |
220 | * Implement the five authentication operations | |
221 | */ | |
222 | ||
223 | ||
224 | /* | |
225 | * 1. Next Verifier | |
226 | */ | |
227 | /*ARGSUSED */ | |
228 | static void | |
f8afba91 | 229 | authdes_nextverf (AUTH *auth) |
800d775e UD |
230 | { |
231 | /* what the heck am I supposed to do??? */ | |
232 | } | |
233 | ||
234 | ||
235 | ||
236 | /* | |
237 | * 2. Marshal | |
238 | */ | |
239 | static bool_t | |
f8afba91 | 240 | authdes_marshal (AUTH *auth, XDR *xdrs) |
800d775e UD |
241 | { |
242 | struct ad_private *ad = AUTH_PRIVATE (auth); | |
243 | struct authdes_cred *cred = &ad->ad_cred; | |
244 | struct authdes_verf *verf = &ad->ad_verf; | |
245 | des_block cryptbuf[2]; | |
246 | des_block ivec; | |
247 | int status; | |
9cfe5381 | 248 | int len; |
f8afba91 | 249 | register int32_t *ixdr; |
4a39c34c | 250 | struct timespec now; |
800d775e UD |
251 | |
252 | /* | |
253 | * Figure out the "time", accounting for any time difference | |
254 | * with the server if necessary. | |
255 | */ | |
4a39c34c ZW |
256 | __clock_gettime (CLOCK_REALTIME, &now); |
257 | ad->ad_timestamp.tv_sec = now.tv_sec + ad->ad_timediff.tv_sec; | |
258 | ad->ad_timestamp.tv_usec = (now.tv_nsec / 1000) + ad->ad_timediff.tv_usec; | |
800d775e UD |
259 | if (ad->ad_timestamp.tv_usec >= MILLION) |
260 | { | |
261 | ad->ad_timestamp.tv_usec -= MILLION; | |
262 | ad->ad_timestamp.tv_sec += 1; | |
263 | } | |
264 | ||
265 | /* | |
266 | * XDR the timestamp and possibly some other things, then | |
267 | * encrypt them. | |
ee586e0e | 268 | * XXX We have a real Year 2038 problem here. |
800d775e | 269 | */ |
f8afba91 UD |
270 | ixdr = (int32_t *) cryptbuf; |
271 | IXDR_PUT_INT32 (ixdr, ad->ad_timestamp.tv_sec); | |
272 | IXDR_PUT_INT32 (ixdr, ad->ad_timestamp.tv_usec); | |
800d775e UD |
273 | if (ad->ad_cred.adc_namekind == ADN_FULLNAME) |
274 | { | |
ee586e0e UD |
275 | IXDR_PUT_U_INT32 (ixdr, ad->ad_window); |
276 | IXDR_PUT_U_INT32 (ixdr, ad->ad_window - 1); | |
800d775e UD |
277 | ivec.key.high = ivec.key.low = 0; |
278 | status = cbc_crypt ((char *) &auth->ah_key, (char *) cryptbuf, | |
279 | 2 * sizeof (des_block), DES_ENCRYPT | DES_HW, (char *) &ivec); | |
280 | } | |
281 | else | |
f8afba91 UD |
282 | status = ecb_crypt ((char *) &auth->ah_key, (char *) cryptbuf, |
283 | sizeof (des_block), DES_ENCRYPT | DES_HW); | |
284 | ||
800d775e UD |
285 | if (DES_FAILED (status)) |
286 | { | |
287 | debug ("authdes_marshal: DES encryption failure"); | |
f8afba91 | 288 | return FALSE; |
800d775e UD |
289 | } |
290 | ad->ad_verf.adv_xtimestamp = cryptbuf[0]; | |
291 | if (ad->ad_cred.adc_namekind == ADN_FULLNAME) | |
292 | { | |
293 | ad->ad_cred.adc_fullname.window = cryptbuf[1].key.high; | |
294 | ad->ad_verf.adv_winverf = cryptbuf[1].key.low; | |
295 | } | |
296 | else | |
297 | { | |
298 | ad->ad_cred.adc_nickname = ad->ad_nickname; | |
299 | ad->ad_verf.adv_winverf = 0; | |
300 | } | |
301 | ||
302 | /* | |
303 | * Serialize the credential and verifier into opaque | |
304 | * authentication data. | |
305 | */ | |
306 | if (ad->ad_cred.adc_namekind == ADN_FULLNAME) | |
f8afba91 | 307 | len = ((1 + 1 + 2 + 1) * BYTES_PER_XDR_UNIT + ad->ad_fullnamelen); |
800d775e | 308 | else |
f8afba91 | 309 | len = (1 + 1) * BYTES_PER_XDR_UNIT; |
800d775e UD |
310 | |
311 | if ((ixdr = xdr_inline (xdrs, 2 * BYTES_PER_XDR_UNIT)) != NULL) | |
312 | { | |
ee586e0e UD |
313 | IXDR_PUT_INT32 (ixdr, AUTH_DES); |
314 | IXDR_PUT_U_INT32 (ixdr, len); | |
800d775e UD |
315 | } |
316 | else | |
317 | { | |
ee586e0e UD |
318 | ATTEMPT (xdr_putint32 (xdrs, &auth->ah_cred.oa_flavor)); |
319 | ATTEMPT (xdr_putint32 (xdrs, &len)); | |
800d775e | 320 | } |
7b57bfe5 | 321 | ATTEMPT (xdr_authdes_cred (xdrs, cred)); |
800d775e UD |
322 | |
323 | len = (2 + 1) * BYTES_PER_XDR_UNIT; | |
324 | if ((ixdr = xdr_inline (xdrs, 2 * BYTES_PER_XDR_UNIT)) != NULL) | |
325 | { | |
ee586e0e UD |
326 | IXDR_PUT_INT32 (ixdr, AUTH_DES); |
327 | IXDR_PUT_U_INT32 (ixdr, len); | |
800d775e UD |
328 | } |
329 | else | |
330 | { | |
ee586e0e UD |
331 | ATTEMPT (xdr_putint32 (xdrs, &auth->ah_verf.oa_flavor)); |
332 | ATTEMPT (xdr_putint32 (xdrs, &len)); | |
800d775e | 333 | } |
7b57bfe5 | 334 | ATTEMPT (xdr_authdes_verf (xdrs, verf)); |
f8afba91 | 335 | |
ee586e0e | 336 | return TRUE; |
800d775e UD |
337 | } |
338 | ||
339 | ||
340 | /* | |
341 | * 3. Validate | |
342 | */ | |
343 | static bool_t | |
ee586e0e | 344 | authdes_validate (AUTH *auth, struct opaque_auth *rverf) |
800d775e UD |
345 | { |
346 | struct ad_private *ad = AUTH_PRIVATE (auth); | |
347 | struct authdes_verf verf; | |
348 | int status; | |
f8afba91 | 349 | register uint32_t *ixdr; |
800d775e UD |
350 | |
351 | if (rverf->oa_length != (2 + 1) * BYTES_PER_XDR_UNIT) | |
f8afba91 | 352 | return FALSE; |
800d775e | 353 | |
f8afba91 UD |
354 | ixdr = (uint32_t *) rverf->oa_base; |
355 | verf.adv_xtimestamp.key.high = *ixdr++; | |
356 | verf.adv_xtimestamp.key.low = *ixdr++; | |
357 | verf.adv_int_u = *ixdr++; /* nickname not XDR'd ! */ | |
800d775e UD |
358 | |
359 | /* | |
360 | * Decrypt the timestamp | |
361 | */ | |
362 | status = ecb_crypt ((char *) &auth->ah_key, (char *) &verf.adv_xtimestamp, | |
363 | sizeof (des_block), DES_DECRYPT | DES_HW); | |
364 | ||
365 | if (DES_FAILED (status)) | |
366 | { | |
367 | debug ("authdes_validate: DES decryption failure"); | |
ee586e0e | 368 | return FALSE; |
800d775e UD |
369 | } |
370 | ||
371 | /* | |
372 | * xdr the decrypted timestamp | |
373 | */ | |
f8afba91 UD |
374 | ixdr = (uint32_t *) verf.adv_xtimestamp.c; |
375 | verf.adv_timestamp.tv_sec = IXDR_GET_U_INT32 (ixdr) + 1; | |
376 | verf.adv_timestamp.tv_usec = IXDR_GET_U_INT32 (ixdr); | |
800d775e UD |
377 | |
378 | /* | |
379 | * validate | |
380 | */ | |
50304ef0 | 381 | if (memcmp ((char *) &ad->ad_timestamp, (char *) &verf.adv_timestamp, |
f8afba91 | 382 | sizeof (struct rpc_timeval)) != 0) |
800d775e UD |
383 | { |
384 | debug ("authdes_validate: verifier mismatch\n"); | |
ee586e0e | 385 | return FALSE; |
800d775e UD |
386 | } |
387 | ||
388 | /* | |
389 | * We have a nickname now, let's use it | |
390 | */ | |
391 | ad->ad_nickname = verf.adv_nickname; | |
392 | ad->ad_cred.adc_namekind = ADN_NICKNAME; | |
ee586e0e | 393 | return TRUE; |
800d775e UD |
394 | } |
395 | ||
396 | /* | |
397 | * 4. Refresh | |
398 | */ | |
399 | static bool_t | |
ee586e0e | 400 | authdes_refresh (AUTH *auth) |
800d775e UD |
401 | { |
402 | netobj pkey; | |
403 | struct ad_private *ad = AUTH_PRIVATE (auth); | |
404 | struct authdes_cred *cred = &ad->ad_cred; | |
405 | ||
f8afba91 | 406 | if (ad->ad_dosync && !synchronize (&ad->ad_syncaddr, &ad->ad_timediff)) |
800d775e UD |
407 | { |
408 | /* | |
409 | * Hope the clocks are synced! | |
410 | */ | |
411 | ad->ad_timediff.tv_sec = ad->ad_timediff.tv_usec = 0; | |
412 | debug ("authdes_refresh: unable to synchronize with server"); | |
413 | } | |
414 | ad->ad_xkey = auth->ah_key; | |
415 | pkey.n_bytes = (char *) (ad->ad_pkey); | |
416 | pkey.n_len = strlen ((char *) ad->ad_pkey) + 1; | |
417 | if (key_encryptsession_pk (ad->ad_servername, &pkey, &ad->ad_xkey) < 0) | |
418 | { | |
419 | debug ("authdes_create: unable to encrypt conversation key"); | |
ee586e0e | 420 | return FALSE; |
800d775e UD |
421 | } |
422 | cred->adc_fullname.key = ad->ad_xkey; | |
423 | cred->adc_namekind = ADN_FULLNAME; | |
424 | cred->adc_fullname.name = ad->ad_fullname; | |
ee586e0e | 425 | return TRUE; |
800d775e UD |
426 | } |
427 | ||
428 | /* | |
429 | * 5. Destroy | |
430 | */ | |
431 | static void | |
ee586e0e | 432 | authdes_destroy (AUTH *auth) |
800d775e UD |
433 | { |
434 | struct ad_private *ad = AUTH_PRIVATE (auth); | |
435 | ||
436 | FREE (ad->ad_fullname, ad->ad_fullnamelen + 1); | |
437 | FREE (ad->ad_servername, ad->ad_servernamelen + 1); | |
438 | FREE (ad, sizeof (struct ad_private)); | |
439 | FREE (auth, sizeof (AUTH)); | |
440 | } | |
441 | ||
442 | /* | |
443 | * Synchronize with the server at the given address, that is, | |
444 | * adjust timep to reflect the delta between our clocks | |
445 | */ | |
446 | static bool_t | |
f8afba91 | 447 | synchronize (struct sockaddr *syncaddr, struct rpc_timeval *timep) |
800d775e | 448 | { |
4a39c34c | 449 | struct timespec mytime; |
f8afba91 | 450 | struct rpc_timeval timeout; |
4a39c34c | 451 | long int myusec; |
800d775e UD |
452 | |
453 | timeout.tv_sec = RTIME_TIMEOUT; | |
454 | timeout.tv_usec = 0; | |
455 | if (rtime ((struct sockaddr_in *) syncaddr, timep, &timeout) < 0) | |
ee586e0e | 456 | return FALSE; |
800d775e | 457 | |
4a39c34c | 458 | __clock_gettime (CLOCK_REALTIME, &mytime); |
800d775e | 459 | timep->tv_sec -= mytime.tv_sec; |
4a39c34c ZW |
460 | myusec = mytime.tv_nsec / 1000; |
461 | if (myusec > timep->tv_usec) | |
800d775e UD |
462 | { |
463 | timep->tv_sec -= 1; | |
464 | timep->tv_usec += MILLION; | |
465 | } | |
4a39c34c | 466 | timep->tv_usec -= myusec; |
ee586e0e | 467 | return TRUE; |
800d775e | 468 | } |