2 * $Id: uams_dhx_passwd.c,v 1.7 2001-05-08 18:03:19 rufustfirefly Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu)
6 * All Rights Reserved. See COPYRIGHT.
33 #include <openssl/bn.h>
34 #include <openssl/dh.h>
35 #include <openssl/cast.h>
40 #endif /* OPENSSL_DHX */
42 #include <atalk/afp.h>
43 #include <atalk/uam.h>
47 #define CRYPTBUFLEN (KEYSIZE*2)
48 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
50 /* hash a number to a 16-bit quantity */
51 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
52 (unsigned long) (a)) & 0xffff)
55 static CAST_KEY castkey;
56 static struct passwd *dhxpwd;
57 static u_int8_t randbuf[16];
59 #ifdef DIGITAL_UNIX_SECURITY
60 #include <sys/types.h>
61 #include <sys/security.h>
63 #endif /* DIGITAL_UNIX_SECURITY */
66 static int passwd_login(void *obj, struct passwd **uam_pwd,
67 char *ibuf, int ibuflen,
68 char *rbuf, int *rbuflen)
70 unsigned char iv[] = "CJalbert";
71 u_int8_t p[] = {0xBA, 0x28, 0x73, 0xDF, 0xB0, 0x60, 0x57, 0xD4,
72 0x3F, 0x20, 0x24, 0x74, 0x4C, 0xEE, 0xE7, 0x5B };
77 BIGNUM *bn, *gbn, *pbn;
84 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
85 RAND_seed(rnd_seed, sizeof rnd_seed);
90 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &name, &i) < 0)
93 len = (unsigned char) *ibuf++;
95 return( AFPERR_PARAM );
98 memcpy(name, ibuf, len );
101 if ((unsigned long) ibuf & 1) /* padding */
104 if (( dhxpwd = uam_getname(name, i)) == NULL ) {
108 syslog( LOG_INFO, "dhx login: %s", name);
109 if (uam_checkuser(dhxpwd) < 0)
110 return AFPERR_NOTAUTH;
113 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
114 syslog( LOG_INFO, "no shadow passwd entry for %s", name);
115 return AFPERR_NOTAUTH;
117 dhxpwd->pw_passwd = sp->sp_pwdp;
120 if (!dhxpwd->pw_passwd)
121 return AFPERR_NOTAUTH;
123 /* get the client's public key */
124 if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
129 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
134 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
140 /* okay, we're ready */
141 if (!(dh = DH_new())) {
148 /* generate key and make sure we have enough space */
151 if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
155 /* figure out the key. use rbuf as a temporary buffer. */
156 i = DH_compute_key(rbuf, bn, dh);
159 CAST_set_key(&castkey, i, rbuf);
161 /* session id. it's just a hashed version of the object pointer. */
162 sessid = dhxhash(obj);
163 memcpy(rbuf, &sessid, sizeof(sessid));
164 rbuf += sizeof(sessid);
165 *rbuflen += sizeof(sessid);
167 /* send our public key */
168 BN_bn2bin(dh->pub_key, rbuf);
172 /* buffer to be encrypted */
174 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
179 memcpy(rbuf, &randbuf, sizeof(randbuf));
182 /* get the signature. it's always 16 bytes. */
183 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
184 (void *) &name, NULL) < 0) {
188 memcpy(rbuf + KEYSIZE, name, KEYSIZE);
190 memset(rbuf + KEYSIZE, 0, KEYSIZE);
193 /* encrypt using cast */
194 CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, iv, CAST_ENCRYPT);
195 *rbuflen += CRYPTBUFLEN;
198 return AFPERR_AUTHCONT;
206 static int passwd_logincont(void *obj, struct passwd **uam_pwd,
207 char *ibuf, int ibuflen,
208 char *rbuf, int *rbuflen)
210 unsigned char iv[] = "LWallace";
211 BIGNUM *bn1, *bn2, *bn3;
215 #ifdef DIGITAL_UNIX_SECURITY
217 struct pr_passwd *pr;
218 #endif /* DIGITAL_UNIX_SECURITY */
222 /* check for session id */
223 memcpy(&sessid, ibuf, sizeof(sessid));
224 if (sessid != dhxhash(obj))
226 ibuf += sizeof(sessid);
228 /* use rbuf as scratch space */
229 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
232 /* check to make sure that the random number is the same. we
233 * get sent back an incremented random number. */
234 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
237 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
242 /* zero out the random number */
243 memset(rbuf, 0, sizeof(randbuf));
244 memset(randbuf, 0, sizeof(randbuf));
247 if (!(bn3 = BN_new())) {
253 BN_sub(bn3, bn1, bn2);
257 /* okay. is it one more? */
258 if (!BN_is_one(bn3)) {
264 rbuf[PASSWDLEN] = '\0';
265 #ifdef DIGITAL_UNIX_SECURITY
266 pr = getprpwnam( dhxpwd->pw_name );
268 return AFPERR_NOTAUTH;
269 if ( strcmp ( bigcrypt ( rbuf, pr->ufld.fd_encrypt ),
270 pr->ufld.fd_encrypt ) == 0 ) {
274 #else /* DIGITAL_UNIX_SECURITY */
275 p = crypt( rbuf, dhxpwd->pw_passwd );
276 memset(rbuf, 0, PASSWDLEN);
277 if ( strcmp( p, dhxpwd->pw_passwd ) == 0 ) {
281 #endif /* DIGITAL_UNIX_SECURITY */
283 return AFPERR_NOTAUTH;
287 static int uam_setup(const char *path)
289 if (uam_register(UAM_SERVER_LOGIN, path, "DHCAST128",
290 passwd_login, passwd_logincont, NULL) < 0)
292 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
298 static void uam_cleanup(void)
300 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
301 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
304 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
307 uam_setup, uam_cleanup