1 /* Copyright (c) 1990,1993 Regents of The University of Michigan.
2 * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu)
3 * All Rights Reserved. See COPYRIGHT.
30 #include <openssl/bn.h>
31 #include <openssl/dh.h>
32 #include <openssl/cast.h>
39 #include <atalk/afp.h>
40 #include <atalk/uam.h>
44 #define CRYPTBUFLEN (KEYSIZE*2)
45 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
47 /* hash a number to a 16-bit quantity */
48 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
49 (unsigned long) (a)) & 0xffff)
52 static CAST_KEY castkey;
53 static struct passwd *dhxpwd;
54 static u_int8_t randbuf[16];
57 static int passwd_login(void *obj, struct passwd **uam_pwd,
58 char *ibuf, int ibuflen,
59 char *rbuf, int *rbuflen)
61 unsigned char iv[] = "CJalbert";
62 u_int8_t p[] = {0xBA, 0x28, 0x73, 0xDF, 0xB0, 0x60, 0x57, 0xD4,
63 0x3F, 0x20, 0x24, 0x74, 0x4C, 0xEE, 0xE7, 0x5B };
68 BIGNUM *bn, *gbn, *pbn;
76 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &name, &i) < 0)
79 len = (unsigned char) *ibuf++;
81 return( AFPERR_PARAM );
84 memcpy(name, ibuf, len );
87 if ((unsigned long) ibuf & 1) /* padding */
90 if (( dhxpwd = uam_getname(name, i)) == NULL ) {
94 syslog( LOG_INFO, "dhx login: %s", name);
95 if (uam_checkuser(dhxpwd) < 0)
96 return AFPERR_NOTAUTH;
99 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
100 syslog( LOG_INFO, "no shadow passwd entry for %s", name);
101 return AFPERR_NOTAUTH;
103 dhxpwd->pw_passwd = sp->sp_pwdp;
106 if (!dhxpwd->pw_passwd)
107 return AFPERR_NOTAUTH;
109 /* get the client's public key */
110 if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
115 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
120 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
126 /* okay, we're ready */
127 if (!(dh = DH_new())) {
134 /* generate key and make sure we have enough space */
137 if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
141 /* figure out the key. use rbuf as a temporary buffer. */
142 i = DH_compute_key(rbuf, bn, dh);
145 CAST_set_key(&castkey, i, rbuf);
147 /* session id. it's just a hashed version of the object pointer. */
148 sessid = dhxhash(obj);
149 memcpy(rbuf, &sessid, sizeof(sessid));
150 rbuf += sizeof(sessid);
151 *rbuflen += sizeof(sessid);
153 /* send our public key */
154 BN_bn2bin(dh->pub_key, rbuf);
158 /* buffer to be encrypted */
160 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
165 memcpy(rbuf, &randbuf, sizeof(randbuf));
168 /* get the signature. it's always 16 bytes. */
169 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
170 (void *) &name, NULL) < 0) {
174 memcpy(rbuf + KEYSIZE, name, KEYSIZE);
176 memset(rbuf + KEYSIZE, 0, KEYSIZE);
179 /* encrypt using cast */
180 CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, iv, CAST_ENCRYPT);
181 *rbuflen += CRYPTBUFLEN;
184 return AFPERR_AUTHCONT;
192 static int passwd_logincont(void *obj, struct passwd **uam_pwd,
193 char *ibuf, int ibuflen,
194 char *rbuf, int *rbuflen)
196 unsigned char iv[] = "LWallace";
197 BIGNUM *bn1, *bn2, *bn3;
203 /* check for session id */
204 memcpy(&sessid, ibuf, sizeof(sessid));
205 if (sessid != dhxhash(obj))
207 ibuf += sizeof(sessid);
209 /* use rbuf as scratch space */
210 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
213 /* check to make sure that the random number is the same. we
214 * get sent back an incremented random number. */
215 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
218 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
223 /* zero out the random number */
224 memset(rbuf, 0, sizeof(randbuf));
225 memset(randbuf, 0, sizeof(randbuf));
228 if (!(bn3 = BN_new())) {
234 BN_sub(bn3, bn1, bn2);
238 /* okay. is it one more? */
239 if (!BN_is_one(bn3)) {
245 rbuf[PASSWDLEN] = '\0';
246 p = crypt( rbuf, dhxpwd->pw_passwd );
247 memset(rbuf, 0, PASSWDLEN);
248 if ( strcmp( p, dhxpwd->pw_passwd ) == 0 ) {
253 return AFPERR_NOTAUTH;
257 static int uam_setup(const char *path)
259 if (uam_register(UAM_SERVER_LOGIN, path, "DHCAST128",
260 passwd_login, passwd_logincont, NULL) < 0)
262 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
268 static void uam_cleanup(void)
270 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
271 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
274 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
277 uam_setup, uam_cleanup