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