2 * $Id: uams_dhx_passwd.c,v 1.6 2001-02-27 17:07:43 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];
60 static int passwd_login(void *obj, struct passwd **uam_pwd,
61 char *ibuf, int ibuflen,
62 char *rbuf, int *rbuflen)
64 unsigned char iv[] = "CJalbert";
65 u_int8_t p[] = {0xBA, 0x28, 0x73, 0xDF, 0xB0, 0x60, 0x57, 0xD4,
66 0x3F, 0x20, 0x24, 0x74, 0x4C, 0xEE, 0xE7, 0x5B };
71 BIGNUM *bn, *gbn, *pbn;
79 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &name, &i) < 0)
82 len = (unsigned char) *ibuf++;
84 return( AFPERR_PARAM );
87 memcpy(name, ibuf, len );
90 if ((unsigned long) ibuf & 1) /* padding */
93 if (( dhxpwd = uam_getname(name, i)) == NULL ) {
97 syslog( LOG_INFO, "dhx login: %s", name);
98 if (uam_checkuser(dhxpwd) < 0)
99 return AFPERR_NOTAUTH;
102 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
103 syslog( LOG_INFO, "no shadow passwd entry for %s", name);
104 return AFPERR_NOTAUTH;
106 dhxpwd->pw_passwd = sp->sp_pwdp;
109 if (!dhxpwd->pw_passwd)
110 return AFPERR_NOTAUTH;
112 /* get the client's public key */
113 if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
118 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
123 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
129 /* okay, we're ready */
130 if (!(dh = DH_new())) {
137 /* generate key and make sure we have enough space */
140 if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
144 /* figure out the key. use rbuf as a temporary buffer. */
145 i = DH_compute_key(rbuf, bn, dh);
148 CAST_set_key(&castkey, i, rbuf);
150 /* session id. it's just a hashed version of the object pointer. */
151 sessid = dhxhash(obj);
152 memcpy(rbuf, &sessid, sizeof(sessid));
153 rbuf += sizeof(sessid);
154 *rbuflen += sizeof(sessid);
156 /* send our public key */
157 BN_bn2bin(dh->pub_key, rbuf);
161 /* buffer to be encrypted */
163 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
168 memcpy(rbuf, &randbuf, sizeof(randbuf));
171 /* get the signature. it's always 16 bytes. */
172 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
173 (void *) &name, NULL) < 0) {
177 memcpy(rbuf + KEYSIZE, name, KEYSIZE);
179 memset(rbuf + KEYSIZE, 0, KEYSIZE);
182 /* encrypt using cast */
183 CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, iv, CAST_ENCRYPT);
184 *rbuflen += CRYPTBUFLEN;
187 return AFPERR_AUTHCONT;
195 static int passwd_logincont(void *obj, struct passwd **uam_pwd,
196 char *ibuf, int ibuflen,
197 char *rbuf, int *rbuflen)
199 unsigned char iv[] = "LWallace";
200 BIGNUM *bn1, *bn2, *bn3;
206 /* check for session id */
207 memcpy(&sessid, ibuf, sizeof(sessid));
208 if (sessid != dhxhash(obj))
210 ibuf += sizeof(sessid);
212 /* use rbuf as scratch space */
213 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
216 /* check to make sure that the random number is the same. we
217 * get sent back an incremented random number. */
218 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
221 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
226 /* zero out the random number */
227 memset(rbuf, 0, sizeof(randbuf));
228 memset(randbuf, 0, sizeof(randbuf));
231 if (!(bn3 = BN_new())) {
237 BN_sub(bn3, bn1, bn2);
241 /* okay. is it one more? */
242 if (!BN_is_one(bn3)) {
248 rbuf[PASSWDLEN] = '\0';
249 p = crypt( rbuf, dhxpwd->pw_passwd );
250 memset(rbuf, 0, PASSWDLEN);
251 if ( strcmp( p, dhxpwd->pw_passwd ) == 0 ) {
256 return AFPERR_NOTAUTH;
260 static int uam_setup(const char *path)
262 if (uam_register(UAM_SERVER_LOGIN, path, "DHCAST128",
263 passwd_login, passwd_logincont, NULL) < 0)
265 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
271 static void uam_cleanup(void)
273 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
274 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
277 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
280 uam_setup, uam_cleanup