1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 /* 31 * Portions of this source code were derived from Berkeley 4.3 BSD 32 * under license from the Regents of the University of California. 33 */ 34 35 /* 36 * auth_des.c, client-side implementation of DES authentication 37 */ 38 39 #include <sys/types.h> 40 #include <sys/t_lock.h> 41 #include <sys/time.h> 42 #include <sys/systm.h> 43 #include <sys/socket.h> 44 #include <sys/tiuser.h> 45 #include <sys/errno.h> 46 #include <rpc/des_crypt.h> 47 #include <rpc/types.h> 48 #include <rpc/auth.h> 49 #include <rpc/auth_des.h> 50 #include <rpc/xdr.h> 51 #include <rpc/clnt.h> 52 #include <rpc/rpc_msg.h> 53 #include <netinet/in.h> /* XXX: just to get htonl() and ntohl() */ 54 #include <sys/cmn_err.h> 55 #include <sys/debug.h> 56 57 #define MILLION 1000000 58 #define RTIME_TIMEOUT 5 /* seconds to wait for sync */ 59 60 #define AUTH_PRIVATE(auth) (struct ad_private *)auth->ah_private 61 #define ALLOC(object_type) (object_type *) mem_alloc(sizeof (object_type)) 62 #define FREE(ptr, size) mem_free((char *)(ptr), (int)size) 63 #define ATTEMPT(xdr_op) if (!(xdr_op))\ 64 return (FALSE) 65 66 #define gettimeofday(tvp, tzp) uniqtime(tvp) 67 68 static void authdes_nextverf(AUTH *); 69 static bool_t authdes_marshal(AUTH *, XDR *, struct cred *); 70 static bool_t authdes_validate(AUTH *, struct opaque_auth *); 71 static bool_t authdes_refresh(AUTH *, struct rpc_msg *, cred_t *); 72 static void authdes_destroy(AUTH *); 73 static bool_t synchronize(struct knetconfig *, struct netbuf *, 74 int, struct timeval *); 75 76 static struct auth_ops *authdes_ops(void); 77 78 /* 79 * This struct is pointed to by the ah_private field of an "AUTH *" 80 */ 81 struct ad_private { 82 char *ad_fullname; /* client's full name */ 83 uint_t ad_fullnamelen; /* length of name, rounded up */ 84 char *ad_servername; /* server's full name */ 85 uint_t ad_servernamelen; /* length of name, rounded up */ 86 uint_t ad_window; /* client specified window */ 87 bool_t ad_dosync; /* synchronize? */ 88 struct netbuf ad_syncaddr; /* remote host to synch with */ 89 struct knetconfig ad_synconfig; /* netconfig for the synch host */ 90 int ad_calltype; /* use rpc or straight call for sync */ 91 struct timeval ad_timediff; /* server's time - client's time */ 92 uint32_t ad_nickname; /* server's nickname for client */ 93 struct authdes_cred ad_cred; /* storage for credential */ 94 struct authdes_verf ad_verf; /* storage for verifier */ 95 struct timeval ad_timestamp; /* timestamp sent */ 96 des_block ad_xkey; /* encrypted conversation key */ 97 }; 98 99 100 /* 101 * Create the client des authentication object 102 */ 103 /* ARGSUSED */ 104 int 105 authdes_create(char *servername, uint_t window, struct netbuf *syncaddr, 106 struct knetconfig *synconfig, des_block *ckey, int calltype, 107 AUTH **retauth) 108 { 109 AUTH *auth; 110 struct ad_private *ad; 111 char namebuf[MAXNETNAMELEN+1]; 112 int error = 0; 113 enum clnt_stat stat; 114 115 if (retauth == NULL) 116 return (EINVAL); 117 118 *retauth = NULL; 119 120 /* 121 * Allocate everything now 122 */ 123 auth = ALLOC(AUTH); 124 ad = ALLOC(struct ad_private); 125 bzero(ad, sizeof (struct ad_private)); 126 if ((stat = kgetnetname(namebuf)) != 0) { 127 cmn_err(CE_NOTE, 128 "authdes_create: unable to get client's netname: %s (error %d)", 129 clnt_sperrno(stat), stat); 130 goto failed; 131 } 132 133 ad->ad_fullnamelen = (uint_t)RNDUP(strlen(namebuf)); 134 ad->ad_fullname = mem_alloc(ad->ad_fullnamelen + 1); 135 136 ad->ad_servernamelen = (uint_t)strlen(servername); 137 ad->ad_servername = mem_alloc(ad->ad_servernamelen + 1); 138 139 if (auth == NULL || ad == NULL || ad->ad_fullname == NULL || 140 ad->ad_servername == NULL) { 141 cmn_err(CE_NOTE, "authdes_create: out of memory"); 142 error = ENOMEM; 143 goto failed; 144 } 145 146 /* 147 * Set up private data 148 */ 149 bcopy(namebuf, ad->ad_fullname, ad->ad_fullnamelen + 1); 150 bcopy(servername, ad->ad_servername, ad->ad_servernamelen + 1); 151 if (syncaddr != NULL) { 152 ad->ad_syncaddr = *syncaddr; 153 ad->ad_synconfig = *synconfig; 154 ad->ad_dosync = TRUE; 155 ad->ad_calltype = calltype; 156 } else { 157 ad->ad_timediff.tv_sec = 0; 158 ad->ad_timediff.tv_usec = 0; 159 ad->ad_dosync = FALSE; 160 } 161 ad->ad_window = window; 162 if (ckey == NULL) { 163 if ((stat = key_gendes(&auth->ah_key)) != RPC_SUCCESS) { 164 cmn_err(CE_NOTE, 165 "authdes_create: unable to gen conversation key: %s (error %d)", 166 clnt_sperrno(stat), stat); 167 if (stat == RPC_INTR) 168 error = EINTR; 169 else if (stat == RPC_TIMEDOUT) 170 error = ETIMEDOUT; 171 else 172 error = EINVAL; /* XXX */ 173 goto failed; 174 } 175 } else 176 auth->ah_key = *ckey; 177 178 /* 179 * Set up auth handle 180 */ 181 auth->ah_cred.oa_flavor = AUTH_DES; 182 auth->ah_verf.oa_flavor = AUTH_DES; 183 auth->ah_ops = authdes_ops(); 184 auth->ah_private = (caddr_t)ad; 185 186 if (!authdes_refresh(auth, NULL, CRED())) 187 goto failed; 188 189 *retauth = auth; 190 return (0); 191 192 failed: 193 if (ad != NULL && ad->ad_fullname != NULL) 194 FREE(ad->ad_fullname, ad->ad_fullnamelen + 1); 195 if (ad != NULL && ad->ad_servername != NULL) 196 FREE(ad->ad_servername, ad->ad_servernamelen + 1); 197 if (ad != NULL) 198 FREE(ad, sizeof (struct ad_private)); 199 if (auth != NULL) 200 FREE(auth, sizeof (AUTH)); 201 return ((error == 0) ? EINVAL : error); /* XXX */ 202 } 203 204 /* 205 * Implement the five authentication operations 206 */ 207 208 /* 209 * 1. Next Verifier 210 */ 211 /* ARGSUSED */ 212 static void 213 authdes_nextverf(AUTH *auth) 214 { 215 /* what the heck am I supposed to do??? */ 216 } 217 218 /* 219 * 2. Marshal 220 */ 221 /* ARGSUSED */ 222 static bool_t 223 authdes_marshal(AUTH *auth, XDR *xdrs, struct cred *cr) 224 { 225 /* LINTED pointer alignment */ 226 struct ad_private *ad = AUTH_PRIVATE(auth); 227 struct authdes_cred *cred = &ad->ad_cred; 228 struct authdes_verf *verf = &ad->ad_verf; 229 des_block cryptbuf[2]; 230 des_block ivec; 231 int status; 232 int len; 233 int32_t *ixdr; 234 235 /* 236 * Figure out the "time", accounting for any time difference 237 * with the server if necessary. 238 */ 239 (void) gettimeofday(&ad->ad_timestamp, (struct timezone *)NULL); 240 ad->ad_timestamp.tv_sec += ad->ad_timediff.tv_sec; 241 ad->ad_timestamp.tv_usec += ad->ad_timediff.tv_usec; 242 if (ad->ad_timestamp.tv_usec >= MILLION) { 243 ad->ad_timestamp.tv_usec -= MILLION; 244 ad->ad_timestamp.tv_sec += 1; 245 } 246 247 /* 248 * XDR the timestamp and possibly some other things, then 249 * encrypt them. 250 */ 251 ixdr = (int32_t *)cryptbuf; 252 IXDR_PUT_INT32(ixdr, ad->ad_timestamp.tv_sec); 253 IXDR_PUT_INT32(ixdr, ad->ad_timestamp.tv_usec); 254 if (ad->ad_cred.adc_namekind == ADN_FULLNAME) { 255 IXDR_PUT_U_INT32(ixdr, ad->ad_window); 256 IXDR_PUT_U_INT32(ixdr, ad->ad_window - 1); 257 ivec.key.high = ivec.key.low = 0; 258 status = cbc_crypt((char *)&auth->ah_key, (char *)cryptbuf, 259 2 * sizeof (des_block), DES_ENCRYPT, (char *)&ivec); 260 } else { 261 status = ecb_crypt((char *)&auth->ah_key, (char *)cryptbuf, 262 sizeof (des_block), DES_ENCRYPT); 263 } 264 if (DES_FAILED(status)) { 265 cmn_err(CE_NOTE, "authdes_marshal: DES encryption failure"); 266 return (FALSE); 267 } 268 ad->ad_verf.adv_xtimestamp = cryptbuf[0]; 269 if (ad->ad_cred.adc_namekind == ADN_FULLNAME) { 270 ad->ad_cred.adc_fullname.window = cryptbuf[1].key.high; 271 ad->ad_verf.adv_winverf = cryptbuf[1].key.low; 272 } else { 273 ad->ad_cred.adc_nickname = ad->ad_nickname; 274 ad->ad_verf.adv_winverf = 0; 275 } 276 277 /* 278 * Serialize the credential and verifier into opaque 279 * authentication data. 280 */ 281 if (ad->ad_cred.adc_namekind == ADN_FULLNAME) { 282 len = ((1 + 1 + 2 + 1) * BYTES_PER_XDR_UNIT + 283 ad->ad_fullnamelen); 284 } else 285 len = (1 + 1) * BYTES_PER_XDR_UNIT; 286 287 if (ixdr = xdr_inline(xdrs, 2 * BYTES_PER_XDR_UNIT)) { 288 IXDR_PUT_INT32(ixdr, AUTH_DES); 289 IXDR_PUT_INT32(ixdr, len); 290 } else { 291 ATTEMPT(xdr_putint32(xdrs, 292 (int32_t *)&auth->ah_cred.oa_flavor)); 293 ATTEMPT(xdr_putint32(xdrs, (int32_t *)&len)); 294 } 295 ATTEMPT(xdr_authdes_cred(xdrs, cred)); 296 297 len = (2 + 1) * BYTES_PER_XDR_UNIT; 298 if (ixdr = xdr_inline(xdrs, 2 * BYTES_PER_XDR_UNIT)) { 299 IXDR_PUT_INT32(ixdr, AUTH_DES); 300 IXDR_PUT_INT32(ixdr, len); 301 } else { 302 ATTEMPT(xdr_putint32(xdrs, 303 (int32_t *)&auth->ah_verf.oa_flavor)); 304 ATTEMPT(xdr_putint32(xdrs, (int32_t *)&len)); 305 } 306 ATTEMPT(xdr_authdes_verf(xdrs, verf)); 307 return (TRUE); 308 } 309 310 /* 311 * 3. Validate 312 */ 313 static bool_t 314 authdes_validate(AUTH *auth, struct opaque_auth *rverf) 315 { 316 /* LINTED pointer alignment */ 317 struct ad_private *ad = AUTH_PRIVATE(auth); 318 struct authdes_verf verf; 319 int status; 320 uint32_t *ixdr; 321 des_block buf; 322 323 if (rverf->oa_length != (2 + 1) * BYTES_PER_XDR_UNIT) 324 return (FALSE); 325 326 /* LINTED pointer alignment */ 327 ixdr = (uint32_t *)rverf->oa_base; 328 buf.key.high = (uint32_t)*ixdr++; 329 buf.key.low = (uint32_t)*ixdr++; 330 verf.adv_int_u = IXDR_GET_U_INT32(ixdr); 331 332 /* 333 * Decrypt the timestamp 334 */ 335 status = ecb_crypt((char *)&auth->ah_key, (char *)&buf, 336 sizeof (des_block), DES_DECRYPT); 337 338 if (DES_FAILED(status)) { 339 cmn_err(CE_NOTE, "authdes_validate: DES decryption failure"); 340 return (FALSE); 341 } 342 343 /* 344 * xdr the decrypted timestamp 345 */ 346 /* LINTED pointer alignment */ 347 ixdr = (uint32_t *)buf.c; 348 verf.adv_timestamp.tv_sec = IXDR_GET_INT32(ixdr) + 1; 349 verf.adv_timestamp.tv_usec = IXDR_GET_INT32(ixdr); 350 351 /* 352 * validate 353 */ 354 if (bcmp((char *)&ad->ad_timestamp, (char *)&verf.adv_timestamp, 355 sizeof (struct timeval)) != 0) { 356 cmn_err(CE_NOTE, "authdes_validate: verifier mismatch"); 357 return (FALSE); 358 } 359 360 /* 361 * We have a nickname now, let's use it 362 */ 363 ad->ad_nickname = verf.adv_nickname; 364 ad->ad_cred.adc_namekind = ADN_NICKNAME; 365 return (TRUE); 366 } 367 368 /* 369 * 4. Refresh 370 * 371 * msg is a dummy argument here. 372 */ 373 /* ARGSUSED */ 374 static bool_t 375 authdes_refresh(AUTH *auth, struct rpc_msg *msg, cred_t *cr) 376 { 377 /* LINTED pointer alignment */ 378 struct ad_private *ad = AUTH_PRIVATE(auth); 379 struct authdes_cred *cred = &ad->ad_cred; 380 enum clnt_stat stat; 381 382 if (ad->ad_dosync && 383 !synchronize(&ad->ad_synconfig, &ad->ad_syncaddr, 384 ad->ad_calltype, &ad->ad_timediff)) { 385 /* 386 * Hope the clocks are synced! 387 */ 388 timerclear(&ad->ad_timediff); 389 cmn_err(CE_NOTE, 390 "authdes_refresh: unable to synchronize with server %s", ad->ad_servername); 391 } 392 ad->ad_xkey = auth->ah_key; 393 if ((stat = key_encryptsession(ad->ad_servername, &ad->ad_xkey, cr)) != 394 RPC_SUCCESS) { 395 cmn_err(CE_NOTE, 396 "authdes_refresh: unable to encrypt conversation key for user (uid %d): " 397 "%s (error %d)", 398 (int)crgetuid(cr), clnt_sperrno(stat), stat); 399 return (FALSE); 400 } 401 cred->adc_fullname.key = ad->ad_xkey; 402 cred->adc_namekind = ADN_FULLNAME; 403 cred->adc_fullname.name = ad->ad_fullname; 404 return (TRUE); 405 } 406 407 /* 408 * 5. Destroy 409 */ 410 static void 411 authdes_destroy(AUTH *auth) 412 { 413 /* LINTED pointer alignment */ 414 struct ad_private *ad = AUTH_PRIVATE(auth); 415 416 FREE(ad->ad_fullname, ad->ad_fullnamelen + 1); 417 FREE(ad->ad_servername, ad->ad_servernamelen + 1); 418 FREE(ad, sizeof (struct ad_private)); 419 FREE(auth, sizeof (AUTH)); 420 } 421 422 423 /* 424 * Synchronize with the server at the given address, that is, 425 * adjust timep to reflect the delta between our clocks 426 */ 427 static bool_t 428 synchronize(struct knetconfig *synconfig, struct netbuf *syncaddr, int calltype, 429 struct timeval *timep) 430 { 431 struct timeval mytime; 432 struct timeval timout; 433 434 timout.tv_sec = RTIME_TIMEOUT; 435 timout.tv_usec = 0; 436 if (rtime(synconfig, syncaddr, calltype, timep, &timout) < 0) 437 return (FALSE); 438 (void) gettimeofday(&mytime, (struct timezone *)NULL); 439 timep->tv_sec -= mytime.tv_sec; 440 if (mytime.tv_usec > timep->tv_usec) { 441 timep->tv_sec -= 1; 442 timep->tv_usec += MILLION; 443 } 444 timep->tv_usec -= mytime.tv_usec; 445 return (TRUE); 446 } 447 448 static struct auth_ops * 449 authdes_ops(void) 450 { 451 static struct auth_ops ops; 452 453 mutex_enter(&authdes_ops_lock); 454 if (ops.ah_nextverf == NULL) { 455 ops.ah_nextverf = authdes_nextverf; 456 ops.ah_marshal = authdes_marshal; 457 ops.ah_validate = authdes_validate; 458 ops.ah_refresh = authdes_refresh; 459 ops.ah_destroy = authdes_destroy; 460 ops.ah_wrap = authany_wrap; 461 ops.ah_unwrap = authany_unwrap; 462 } 463 mutex_exit(&authdes_ops_lock); 464 return (&ops); 465 } 466