2 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 * Copyright (c) 1988 by Sun Microsystems, Inc.
32 * auth_des.c, client-side implementation of DES authentication
35 #include <reentrant.h>
41 #include <sys/cdefs.h>
42 #include <rpc/des_crypt.h>
44 #include <rpc/types.h>
46 #include <rpc/auth_des.h>
49 #include <sys/socket.h>
51 #include <rpcsvc/nis.h>
53 #if defined(LIBC_SCCS) && !defined(lint)
55 #include <sys/cdefs.h>
59 #define USEC_PER_SEC 1000000
60 #define RTIME_TIMEOUT 5 /* seconds to wait for sync */
62 #define AUTH_PRIVATE(auth) (struct ad_private *) auth->ah_private
63 #define ALLOC(object_type) (object_type *) mem_alloc(sizeof(object_type))
64 #define FREE(ptr, size) mem_free((char *)(ptr), (int) size)
65 #define ATTEMPT(xdr_op) if (!(xdr_op)) return (FALSE)
67 extern bool_t xdr_authdes_cred( XDR *, struct authdes_cred *);
68 extern bool_t xdr_authdes_verf( XDR *, struct authdes_verf *);
69 extern int key_encryptsession_pk();
71 extern bool_t __rpc_get_time_offset(struct timeval *, nis_server *, char *,
75 * DES authenticator operations vector
77 static void authdes_nextverf(AUTH *);
78 static bool_t authdes_marshal(AUTH *, XDR *);
79 static bool_t authdes_validate(AUTH *, struct opaque_auth *);
80 static bool_t authdes_refresh(AUTH *, void *);
81 static void authdes_destroy(AUTH *);
83 static struct auth_ops *authdes_ops(void);
86 * This struct is pointed to by the ah_private field of an "AUTH *"
89 char *ad_fullname; /* client's full name */
90 u_int ad_fullnamelen; /* length of name, rounded up */
91 char *ad_servername; /* server's full name */
92 u_int ad_servernamelen; /* length of name, rounded up */
93 u_int ad_window; /* client specified window */
94 bool_t ad_dosync; /* synchronize? */
95 struct netbuf ad_syncaddr; /* remote host to synch with */
96 char *ad_timehost; /* remote host to synch with */
97 struct timeval ad_timediff; /* server's time - client's time */
98 u_int ad_nickname; /* server's nickname for client */
99 struct authdes_cred ad_cred; /* storage for credential */
100 struct authdes_verf ad_verf; /* storage for verifier */
101 struct timeval ad_timestamp; /* timestamp sent */
102 des_block ad_xkey; /* encrypted conversation key */
103 u_char ad_pkey[1024]; /* Server's actual public key */
104 char *ad_netid; /* Timehost netid */
105 char *ad_uaddr; /* Timehost uaddr */
106 nis_server *ad_nis_srvr; /* NIS+ server struct */
109 AUTH *authdes_pk_seccreate(const char *, netobj *, u_int, const char *,
110 const des_block *, nis_server *);
113 * documented version of authdes_seccreate
116 servername: network name of server
118 timehost: optional hostname to sync with
119 ckey: optional conversation key to use
123 authdes_seccreate(const char *servername, const u_int win,
124 const char *timehost, const des_block *ckey)
126 u_char pkey_data[1024];
130 if (! getpublickey(servername, (char *) pkey_data)) {
132 "authdes_seccreate: no public key found for %s",
137 pkey.n_bytes = (char *) pkey_data;
138 pkey.n_len = (u_int)strlen((char *)pkey_data) + 1;
139 dummy = authdes_pk_seccreate(servername, &pkey, win, timehost,
145 * Slightly modified version of authdessec_create which takes the public key
146 * of the server principal as an argument. This spares us a call to
147 * getpublickey() which in the nameserver context can cause a deadlock.
150 authdes_pk_seccreate(const char *servername, netobj *pkey, u_int window,
151 const char *timehost, const des_block *ckey, nis_server *srvr)
154 struct ad_private *ad;
155 char namebuf[MAXNETNAMELEN+1];
158 * Allocate everything now
162 syslog(LOG_ERR, "authdes_pk_seccreate: out of memory");
165 ad = ALLOC(struct ad_private);
167 syslog(LOG_ERR, "authdes_pk_seccreate: out of memory");
170 ad->ad_fullname = ad->ad_servername = NULL; /* Sanity reasons */
171 ad->ad_timehost = NULL;
174 ad->ad_nis_srvr = NULL;
175 ad->ad_timediff.tv_sec = 0;
176 ad->ad_timediff.tv_usec = 0;
177 memcpy(ad->ad_pkey, pkey->n_bytes, pkey->n_len);
178 if (!getnetname(namebuf))
180 ad->ad_fullnamelen = RNDUP((u_int) strlen(namebuf));
181 ad->ad_fullname = (char *)mem_alloc(ad->ad_fullnamelen + 1);
182 ad->ad_servernamelen = strlen(servername);
183 ad->ad_servername = (char *)mem_alloc(ad->ad_servernamelen + 1);
185 if (ad->ad_fullname == NULL || ad->ad_servername == NULL) {
186 syslog(LOG_ERR, "authdes_seccreate: out of memory");
189 if (timehost != NULL) {
190 ad->ad_timehost = (char *)mem_alloc(strlen(timehost) + 1);
191 if (ad->ad_timehost == NULL) {
192 syslog(LOG_ERR, "authdes_seccreate: out of memory");
195 memcpy(ad->ad_timehost, timehost, strlen(timehost) + 1);
196 ad->ad_dosync = TRUE;
197 } else if (srvr != NULL) {
198 ad->ad_nis_srvr = srvr; /* transient */
199 ad->ad_dosync = TRUE;
201 ad->ad_dosync = FALSE;
203 memcpy(ad->ad_fullname, namebuf, ad->ad_fullnamelen + 1);
204 memcpy(ad->ad_servername, servername, ad->ad_servernamelen + 1);
205 ad->ad_window = window;
207 if (key_gendes(&auth->ah_key) < 0) {
209 "authdes_seccreate: keyserv(1m) is unable to generate session key");
213 auth->ah_key = *ckey;
219 auth->ah_cred.oa_flavor = AUTH_DES;
220 auth->ah_verf.oa_flavor = AUTH_DES;
221 auth->ah_ops = authdes_ops();
222 auth->ah_private = (caddr_t)ad;
224 if (!authdes_refresh(auth, NULL)) {
227 ad->ad_nis_srvr = NULL; /* not needed any longer */
228 auth_get(auth); /* Reference for caller */
233 FREE(auth, sizeof (AUTH));
236 FREE(ad->ad_fullname, ad->ad_fullnamelen + 1);
237 if (ad->ad_servername)
238 FREE(ad->ad_servername, ad->ad_servernamelen + 1);
240 FREE(ad->ad_timehost, strlen(ad->ad_timehost) + 1);
242 FREE(ad->ad_netid, strlen(ad->ad_netid) + 1);
244 FREE(ad->ad_uaddr, strlen(ad->ad_uaddr) + 1);
245 FREE(ad, sizeof (struct ad_private));
251 * Implement the five authentication operations
260 authdes_nextverf(AUTH *auth)
262 /* what the heck am I supposed to do??? */
270 authdes_marshal(AUTH *auth, XDR *xdrs)
272 /* LINTED pointer alignment */
273 struct ad_private *ad = AUTH_PRIVATE(auth);
274 struct authdes_cred *cred = &ad->ad_cred;
275 struct authdes_verf *verf = &ad->ad_verf;
276 des_block cryptbuf[2];
283 * Figure out the "time", accounting for any time difference
284 * with the server if necessary.
286 (void) gettimeofday(&ad->ad_timestamp, (struct timezone *)NULL);
287 ad->ad_timestamp.tv_sec += ad->ad_timediff.tv_sec;
288 ad->ad_timestamp.tv_usec += ad->ad_timediff.tv_usec;
289 while (ad->ad_timestamp.tv_usec >= USEC_PER_SEC) {
290 ad->ad_timestamp.tv_usec -= USEC_PER_SEC;
291 ad->ad_timestamp.tv_sec++;
295 * XDR the timestamp and possibly some other things, then
298 ixdr = (rpc_inline_t *)cryptbuf;
299 IXDR_PUT_INT32(ixdr, ad->ad_timestamp.tv_sec);
300 IXDR_PUT_INT32(ixdr, ad->ad_timestamp.tv_usec);
301 if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
302 IXDR_PUT_U_INT32(ixdr, ad->ad_window);
303 IXDR_PUT_U_INT32(ixdr, ad->ad_window - 1);
304 ivec.key.high = ivec.key.low = 0;
305 status = cbc_crypt((char *)&auth->ah_key, (char *)cryptbuf,
306 (u_int) 2 * sizeof (des_block),
307 DES_ENCRYPT | DES_HW, (char *)&ivec);
309 status = ecb_crypt((char *)&auth->ah_key, (char *)cryptbuf,
310 (u_int) sizeof (des_block),
311 DES_ENCRYPT | DES_HW);
313 if (DES_FAILED(status)) {
314 syslog(LOG_ERR, "authdes_marshal: DES encryption failure");
317 ad->ad_verf.adv_xtimestamp = cryptbuf[0];
318 if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
319 ad->ad_cred.adc_fullname.window = cryptbuf[1].key.high;
320 ad->ad_verf.adv_winverf = cryptbuf[1].key.low;
322 ad->ad_cred.adc_nickname = ad->ad_nickname;
323 ad->ad_verf.adv_winverf = 0;
327 * Serialize the credential and verifier into opaque
328 * authentication data.
330 if (ad->ad_cred.adc_namekind == ADN_FULLNAME) {
331 len = ((1 + 1 + 2 + 1)*BYTES_PER_XDR_UNIT + ad->ad_fullnamelen);
333 len = (1 + 1)*BYTES_PER_XDR_UNIT;
336 if ((ixdr = xdr_inline(xdrs, 2*BYTES_PER_XDR_UNIT))) {
337 IXDR_PUT_INT32(ixdr, AUTH_DES);
338 IXDR_PUT_INT32(ixdr, len);
340 ATTEMPT(xdr_putint32(xdrs, (int *)&auth->ah_cred.oa_flavor));
341 ATTEMPT(xdr_putint32(xdrs, &len));
343 ATTEMPT(xdr_authdes_cred(xdrs, cred));
345 len = (2 + 1)*BYTES_PER_XDR_UNIT;
346 if ((ixdr = xdr_inline(xdrs, 2*BYTES_PER_XDR_UNIT))) {
347 IXDR_PUT_INT32(ixdr, AUTH_DES);
348 IXDR_PUT_INT32(ixdr, len);
350 ATTEMPT(xdr_putint32(xdrs, (int *)&auth->ah_verf.oa_flavor));
351 ATTEMPT(xdr_putint32(xdrs, &len));
353 ATTEMPT(xdr_authdes_verf(xdrs, verf));
362 authdes_validate(AUTH *auth, struct opaque_auth *rverf)
364 /* LINTED pointer alignment */
365 struct ad_private *ad = AUTH_PRIVATE(auth);
366 struct authdes_verf verf;
371 if (rverf->oa_length != (2 + 1) * BYTES_PER_XDR_UNIT) {
374 /* LINTED pointer alignment */
375 ixdr = (uint32_t *)rverf->oa_base;
376 buf.key.high = (uint32_t)*ixdr++;
377 buf.key.low = (uint32_t)*ixdr++;
378 verf.adv_int_u = (uint32_t)*ixdr++;
381 * Decrypt the timestamp
383 status = ecb_crypt((char *)&auth->ah_key, (char *)&buf,
384 (u_int)sizeof (des_block), DES_DECRYPT | DES_HW);
386 if (DES_FAILED(status)) {
387 syslog(LOG_ERR, "authdes_validate: DES decryption failure");
392 * xdr the decrypted timestamp
394 /* LINTED pointer alignment */
395 ixdr = (uint32_t *)buf.c;
396 verf.adv_timestamp.tv_sec = IXDR_GET_INT32(ixdr) + 1;
397 verf.adv_timestamp.tv_usec = IXDR_GET_INT32(ixdr);
402 if (bcmp((char *)&ad->ad_timestamp, (char *)&verf.adv_timestamp,
403 sizeof(struct timeval)) != 0) {
404 LIBTIRPC_DEBUG(1, ("authdes_validate: verifier mismatch"));
409 * We have a nickname now, let's use it
411 ad->ad_nickname = verf.adv_nickname;
412 ad->ad_cred.adc_namekind = ADN_NICKNAME;
421 authdes_refresh(AUTH *auth, void *dummy)
423 /* LINTED pointer alignment */
424 struct ad_private *ad = AUTH_PRIVATE(auth);
425 struct authdes_cred *cred = &ad->ad_cred;
430 ok = __rpc_get_time_offset(&ad->ad_timediff, ad->ad_nis_srvr,
431 ad->ad_timehost, &(ad->ad_uaddr),
435 * Hope the clocks are synced!
438 LIBTIRPC_DEBUG(1, ("authdes_refresh: unable to synchronize clock"));
441 ad->ad_xkey = auth->ah_key;
442 pkey.n_bytes = (char *)(ad->ad_pkey);
443 pkey.n_len = (u_int)strlen((char *)ad->ad_pkey) + 1;
444 if (key_encryptsession_pk(ad->ad_servername, &pkey, &ad->ad_xkey) < 0) {
446 ("authdes_refresh: keyserv(1m) is unable to encrypt session key"));
449 cred->adc_fullname.key = ad->ad_xkey;
450 cred->adc_namekind = ADN_FULLNAME;
451 cred->adc_fullname.name = ad->ad_fullname;
460 authdes_destroy(AUTH *auth)
462 /* LINTED pointer alignment */
463 struct ad_private *ad = AUTH_PRIVATE(auth);
465 FREE(ad->ad_fullname, ad->ad_fullnamelen + 1);
466 FREE(ad->ad_servername, ad->ad_servernamelen + 1);
468 FREE(ad->ad_timehost, strlen(ad->ad_timehost) + 1);
470 FREE(ad->ad_netid, strlen(ad->ad_netid) + 1);
472 FREE(ad->ad_uaddr, strlen(ad->ad_uaddr) + 1);
473 FREE(ad, sizeof (struct ad_private));
474 FREE(auth, sizeof(AUTH));
478 authdes_wrap(AUTH *auth, XDR *xdrs, xdrproc_t xfunc, caddr_t xwhere)
480 return ((*xfunc)(xdrs, xwhere));
483 static struct auth_ops *
486 static struct auth_ops ops;
487 extern mutex_t authdes_ops_lock;
489 /* VARIABLES PROTECTED BY ops_lock: ops */
491 mutex_lock(&authdes_ops_lock);
492 if (ops.ah_nextverf == NULL) {
493 ops.ah_nextverf = authdes_nextverf;
494 ops.ah_marshal = authdes_marshal;
495 ops.ah_validate = authdes_validate;
496 ops.ah_refresh = authdes_refresh;
497 ops.ah_destroy = authdes_destroy;
498 ops.ah_wrap = authdes_wrap;
499 ops.ah_unwrap = authdes_wrap;
501 mutex_unlock(&authdes_ops_lock);