]> git.ipfire.org Git - thirdparty/glibc.git/blob - sunrpc/auth_des.c
Once again change RPC copyright notices.
[thirdparty/glibc.git] / sunrpc / auth_des.c
1 /*
2 * Copyright (c) 2010, Oracle America, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
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.
17 *
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.
30 */
31 /*
32 * auth_des.c, client-side implementation of DES authentication
33 */
34
35 #include <string.h>
36 #include <rpc/des_crypt.h>
37 #include <rpc/types.h>
38 #include <rpc/auth.h>
39 #include <rpc/auth_des.h>
40 #include <rpc/xdr.h>
41 #include <netinet/in.h> /* XXX: just to get htonl() and ntohl() */
42 #include <sys/socket.h>
43
44 #define MILLION 1000000L
45 #define RTIME_TIMEOUT 5 /* seconds to wait for sync */
46
47 #define AUTH_PRIVATE(auth) (struct ad_private *) auth->ah_private
48 #define ALLOC(object_type) (object_type *) mem_alloc(sizeof(object_type))
49 #define FREE(ptr, size) mem_free((char *)(ptr), (int) size)
50 #define ATTEMPT(xdr_op) if (!(xdr_op)) return (FALSE)
51
52 #define debug(msg) /* printf("%s\n", msg) */
53
54 extern bool_t INTUSE(xdr_authdes_cred) (XDR *, struct authdes_cred *);
55 extern bool_t INTUSE(xdr_authdes_verf) (XDR *, struct authdes_verf *);
56
57 /*
58 * DES authenticator operations vector
59 */
60 static void authdes_nextverf (AUTH *);
61 static bool_t authdes_marshal (AUTH *, XDR *);
62 static bool_t authdes_validate (AUTH *, struct opaque_auth *);
63 static bool_t authdes_refresh (AUTH *);
64 static void authdes_destroy (AUTH *);
65 static bool_t synchronize (struct sockaddr *, struct rpc_timeval *)
66 internal_function;
67
68 static const struct auth_ops authdes_ops = {
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 */
80 struct ad_private {
81 char *ad_fullname; /* client's full name */
82 u_int ad_fullnamelen; /* length of name, rounded up */
83 char *ad_servername; /* server's full name */
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 */
88 struct rpc_timeval ad_timediff; /* server's time - client's time */
89 uint32_t ad_nickname; /* server's nickname for client */
90 struct authdes_cred ad_cred; /* storage for credential */
91 struct authdes_verf ad_verf; /* storage for verifier */
92 struct rpc_timeval ad_timestamp; /* timestamp sent */
93 des_block ad_xkey; /* encrypted conversation key */
94 u_char ad_pkey[1024]; /* Servers actual public key */
95 };
96
97
98 /*
99 * Create the client des authentication object
100 */
101 AUTH *
102 authdes_create (const char *servername, u_int window,
103 struct sockaddr *syncaddr, des_block *ckey)
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 {
109 char pkey_data[1024];
110 netobj pkey;
111
112 if (!getpublickey (servername, pkey_data))
113 return NULL;
114
115 pkey.n_bytes = pkey_data;
116 pkey.n_len = strlen (pkey_data) + 1;
117 return INTUSE(authdes_pk_create) (servername, &pkey, window, syncaddr, ckey);
118 }
119
120 AUTH *
121 authdes_pk_create (const char *servername, netobj *pkey, u_int window,
122 struct sockaddr *syncaddr, des_block *ckey)
123 {
124 AUTH *auth;
125 struct ad_private *ad;
126 char namebuf[MAXNETNAMELEN + 1];
127
128 /*
129 * Allocate everything now
130 */
131 auth = ALLOC (AUTH);
132 ad = ALLOC (struct ad_private);
133
134 if (auth == NULL || ad == NULL)
135 {
136 debug ("authdes_create: out of memory");
137 goto failed;
138 }
139
140 memset (ad, 0, sizeof (struct ad_private));
141 memcpy (ad->ad_pkey, pkey->n_bytes, pkey->n_len);
142 if (!getnetname (namebuf))
143 goto failed;
144 ad->ad_fullnamelen = RNDUP (strlen (namebuf));
145 ad->ad_fullname = mem_alloc (ad->ad_fullnamelen + 1);
146
147 ad->ad_servernamelen = strlen (servername);
148 ad->ad_servername = mem_alloc (ad->ad_servernamelen + 1);
149
150 if (ad->ad_fullname == NULL || ad->ad_servername == NULL)
151 {
152 debug ("authdes_create: out of memory");
153 goto failed;
154 }
155
156 /*
157 * Set up private data
158 */
159 memcpy (ad->ad_fullname, namebuf, ad->ad_fullnamelen + 1);
160 memcpy (ad->ad_servername, servername, ad->ad_servernamelen + 1);
161 ad->ad_timediff.tv_sec = ad->ad_timediff.tv_usec = 0;
162 if (syncaddr != NULL)
163 {
164 ad->ad_syncaddr = *syncaddr;
165 ad->ad_dosync = TRUE;
166 }
167 else
168 ad->ad_dosync = FALSE;
169
170 ad->ad_window = window;
171 if (ckey == NULL)
172 {
173 if (key_gendes (&auth->ah_key) < 0)
174 {
175 debug ("authdes_create: unable to gen conversation key");
176 goto failed;
177 }
178 }
179 else
180 auth->ah_key = *ckey;
181
182 /*
183 * Set up auth handle
184 */
185 auth->ah_cred.oa_flavor = AUTH_DES;
186 auth->ah_verf.oa_flavor = AUTH_DES;
187 auth->ah_ops = (struct auth_ops *) &authdes_ops;
188 auth->ah_private = (caddr_t) ad;
189
190 if (!authdes_refresh (auth))
191 goto failed;
192
193 return auth;
194
195 failed:
196 if (auth != NULL)
197 FREE (auth, sizeof (AUTH));
198 if (ad != NULL)
199 {
200 if (ad->ad_fullname != NULL)
201 FREE (ad->ad_fullname, ad->ad_fullnamelen + 1);
202 if (ad->ad_servername != NULL)
203 FREE (ad->ad_servername, ad->ad_servernamelen + 1);
204 FREE (ad, sizeof (struct ad_private));
205 }
206 return NULL;
207 }
208 INTDEF(authdes_pk_create)
209
210 /*
211 * Implement the five authentication operations
212 */
213
214
215 /*
216 * 1. Next Verifier
217 */
218 /*ARGSUSED */
219 static void
220 authdes_nextverf (AUTH *auth)
221 {
222 /* what the heck am I supposed to do??? */
223 }
224
225
226
227 /*
228 * 2. Marshal
229 */
230 static bool_t
231 authdes_marshal (AUTH *auth, XDR *xdrs)
232 {
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];
237 des_block ivec;
238 int status;
239 int len;
240 register int32_t *ixdr;
241 struct timeval tval;
242
243 /*
244 * Figure out the "time", accounting for any time difference
245 * with the server if necessary.
246 */
247 __gettimeofday (&tval, (struct timezone *) NULL);
248 ad->ad_timestamp.tv_sec = tval.tv_sec + ad->ad_timediff.tv_sec;
249 ad->ad_timestamp.tv_usec = tval.tv_usec + ad->ad_timediff.tv_usec;
250 if (ad->ad_timestamp.tv_usec >= MILLION)
251 {
252 ad->ad_timestamp.tv_usec -= MILLION;
253 ad->ad_timestamp.tv_sec += 1;
254 }
255
256 /*
257 * XDR the timestamp and possibly some other things, then
258 * encrypt them.
259 * XXX We have a real Year 2038 problem here.
260 */
261 ixdr = (int32_t *) cryptbuf;
262 IXDR_PUT_INT32 (ixdr, ad->ad_timestamp.tv_sec);
263 IXDR_PUT_INT32 (ixdr, ad->ad_timestamp.tv_usec);
264 if (ad->ad_cred.adc_namekind == ADN_FULLNAME)
265 {
266 IXDR_PUT_U_INT32 (ixdr, ad->ad_window);
267 IXDR_PUT_U_INT32 (ixdr, ad->ad_window - 1);
268 ivec.key.high = ivec.key.low = 0;
269 status = cbc_crypt ((char *) &auth->ah_key, (char *) cryptbuf,
270 2 * sizeof (des_block), DES_ENCRYPT | DES_HW, (char *) &ivec);
271 }
272 else
273 status = ecb_crypt ((char *) &auth->ah_key, (char *) cryptbuf,
274 sizeof (des_block), DES_ENCRYPT | DES_HW);
275
276 if (DES_FAILED (status))
277 {
278 debug ("authdes_marshal: DES encryption failure");
279 return FALSE;
280 }
281 ad->ad_verf.adv_xtimestamp = cryptbuf[0];
282 if (ad->ad_cred.adc_namekind == ADN_FULLNAME)
283 {
284 ad->ad_cred.adc_fullname.window = cryptbuf[1].key.high;
285 ad->ad_verf.adv_winverf = cryptbuf[1].key.low;
286 }
287 else
288 {
289 ad->ad_cred.adc_nickname = ad->ad_nickname;
290 ad->ad_verf.adv_winverf = 0;
291 }
292
293 /*
294 * Serialize the credential and verifier into opaque
295 * authentication data.
296 */
297 if (ad->ad_cred.adc_namekind == ADN_FULLNAME)
298 len = ((1 + 1 + 2 + 1) * BYTES_PER_XDR_UNIT + ad->ad_fullnamelen);
299 else
300 len = (1 + 1) * BYTES_PER_XDR_UNIT;
301
302 if ((ixdr = xdr_inline (xdrs, 2 * BYTES_PER_XDR_UNIT)) != NULL)
303 {
304 IXDR_PUT_INT32 (ixdr, AUTH_DES);
305 IXDR_PUT_U_INT32 (ixdr, len);
306 }
307 else
308 {
309 ATTEMPT (xdr_putint32 (xdrs, &auth->ah_cred.oa_flavor));
310 ATTEMPT (xdr_putint32 (xdrs, &len));
311 }
312 ATTEMPT (INTUSE(xdr_authdes_cred) (xdrs, cred));
313
314 len = (2 + 1) * BYTES_PER_XDR_UNIT;
315 if ((ixdr = xdr_inline (xdrs, 2 * BYTES_PER_XDR_UNIT)) != NULL)
316 {
317 IXDR_PUT_INT32 (ixdr, AUTH_DES);
318 IXDR_PUT_U_INT32 (ixdr, len);
319 }
320 else
321 {
322 ATTEMPT (xdr_putint32 (xdrs, &auth->ah_verf.oa_flavor));
323 ATTEMPT (xdr_putint32 (xdrs, &len));
324 }
325 ATTEMPT (INTUSE(xdr_authdes_verf) (xdrs, verf));
326
327 return TRUE;
328 }
329
330
331 /*
332 * 3. Validate
333 */
334 static bool_t
335 authdes_validate (AUTH *auth, struct opaque_auth *rverf)
336 {
337 struct ad_private *ad = AUTH_PRIVATE (auth);
338 struct authdes_verf verf;
339 int status;
340 register uint32_t *ixdr;
341
342 if (rverf->oa_length != (2 + 1) * BYTES_PER_XDR_UNIT)
343 return FALSE;
344
345 ixdr = (uint32_t *) rverf->oa_base;
346 verf.adv_xtimestamp.key.high = *ixdr++;
347 verf.adv_xtimestamp.key.low = *ixdr++;
348 verf.adv_int_u = *ixdr++; /* nickname not XDR'd ! */
349
350 /*
351 * Decrypt the timestamp
352 */
353 status = ecb_crypt ((char *) &auth->ah_key, (char *) &verf.adv_xtimestamp,
354 sizeof (des_block), DES_DECRYPT | DES_HW);
355
356 if (DES_FAILED (status))
357 {
358 debug ("authdes_validate: DES decryption failure");
359 return FALSE;
360 }
361
362 /*
363 * xdr the decrypted timestamp
364 */
365 ixdr = (uint32_t *) verf.adv_xtimestamp.c;
366 verf.adv_timestamp.tv_sec = IXDR_GET_U_INT32 (ixdr) + 1;
367 verf.adv_timestamp.tv_usec = IXDR_GET_U_INT32 (ixdr);
368
369 /*
370 * validate
371 */
372 if (memcmp ((char *) &ad->ad_timestamp, (char *) &verf.adv_timestamp,
373 sizeof (struct rpc_timeval)) != 0)
374 {
375 debug ("authdes_validate: verifier mismatch\n");
376 return FALSE;
377 }
378
379 /*
380 * We have a nickname now, let's use it
381 */
382 ad->ad_nickname = verf.adv_nickname;
383 ad->ad_cred.adc_namekind = ADN_NICKNAME;
384 return TRUE;
385 }
386
387 /*
388 * 4. Refresh
389 */
390 static bool_t
391 authdes_refresh (AUTH *auth)
392 {
393 netobj pkey;
394 struct ad_private *ad = AUTH_PRIVATE (auth);
395 struct authdes_cred *cred = &ad->ad_cred;
396
397 if (ad->ad_dosync && !synchronize (&ad->ad_syncaddr, &ad->ad_timediff))
398 {
399 /*
400 * Hope the clocks are synced!
401 */
402 ad->ad_timediff.tv_sec = ad->ad_timediff.tv_usec = 0;
403 debug ("authdes_refresh: unable to synchronize with server");
404 }
405 ad->ad_xkey = auth->ah_key;
406 pkey.n_bytes = (char *) (ad->ad_pkey);
407 pkey.n_len = strlen ((char *) ad->ad_pkey) + 1;
408 if (key_encryptsession_pk (ad->ad_servername, &pkey, &ad->ad_xkey) < 0)
409 {
410 debug ("authdes_create: unable to encrypt conversation key");
411 return FALSE;
412 }
413 cred->adc_fullname.key = ad->ad_xkey;
414 cred->adc_namekind = ADN_FULLNAME;
415 cred->adc_fullname.name = ad->ad_fullname;
416 return TRUE;
417 }
418
419 /*
420 * 5. Destroy
421 */
422 static void
423 authdes_destroy (AUTH *auth)
424 {
425 struct ad_private *ad = AUTH_PRIVATE (auth);
426
427 FREE (ad->ad_fullname, ad->ad_fullnamelen + 1);
428 FREE (ad->ad_servername, ad->ad_servernamelen + 1);
429 FREE (ad, sizeof (struct ad_private));
430 FREE (auth, sizeof (AUTH));
431 }
432
433 /*
434 * Synchronize with the server at the given address, that is,
435 * adjust timep to reflect the delta between our clocks
436 */
437 static bool_t
438 internal_function
439 synchronize (struct sockaddr *syncaddr, struct rpc_timeval *timep)
440 {
441 struct timeval mytime;
442 struct rpc_timeval timeout;
443
444 timeout.tv_sec = RTIME_TIMEOUT;
445 timeout.tv_usec = 0;
446 if (rtime ((struct sockaddr_in *) syncaddr, timep, &timeout) < 0)
447 return FALSE;
448
449 __gettimeofday (&mytime, (struct timezone *) NULL);
450 timep->tv_sec -= mytime.tv_sec;
451 if (mytime.tv_usec > timep->tv_usec)
452 {
453 timep->tv_sec -= 1;
454 timep->tv_usec += MILLION;
455 }
456 timep->tv_usec -= mytime.tv_usec;
457 return TRUE;
458 }