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.
29 #include <atalk/afp.h>
30 #include <atalk/uam.h>
34 #define CRYPTBUFLEN (KEYSIZE*2)
35 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
37 /* hash a number to a 16-bit quantity */
38 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
39 (unsigned long) (a)) & 0xffff)
42 static CAST_KEY castkey;
43 static struct passwd *dhxpwd;
44 static u_int8_t randbuf[16];
47 static int passwd_login(void *obj, struct passwd **uam_pwd,
48 char *ibuf, int ibuflen,
49 char *rbuf, int *rbuflen)
51 unsigned char iv[] = "CJalbert";
52 u_int8_t p[] = {0xBA, 0x28, 0x73, 0xDF, 0xB0, 0x60, 0x57, 0xD4,
53 0x3F, 0x20, 0x24, 0x74, 0x4C, 0xEE, 0xE7, 0x5B };
58 BIGNUM *bn, *gbn, *pbn;
66 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &name, &i) < 0)
69 len = (unsigned char) *ibuf++;
71 return( AFPERR_PARAM );
74 memcpy(name, ibuf, len );
77 if ((unsigned long) ibuf & 1) /* padding */
80 if (( dhxpwd = uam_getname(name, i)) == NULL ) {
84 syslog( LOG_INFO, "dhx login: %s", name);
85 if (uam_checkuser(dhxpwd) < 0)
86 return AFPERR_NOTAUTH;
89 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
90 syslog( LOG_INFO, "no shadow passwd entry for %s", name);
91 return AFPERR_NOTAUTH;
93 dhxpwd->pw_passwd = sp->sp_pwdp;
96 if (!dhxpwd->pw_passwd)
97 return AFPERR_NOTAUTH;
99 /* get the client's public key */
100 if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL))) {
105 if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
110 if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
116 /* okay, we're ready */
117 if (!(dh = DH_new())) {
124 /* generate key and make sure we have enough space */
127 if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
131 /* figure out the key. use rbuf as a temporary buffer. */
132 i = DH_compute_key(rbuf, bn, dh);
135 CAST_set_key(&castkey, i, rbuf);
137 /* session id. it's just a hashed version of the object pointer. */
138 sessid = dhxhash(obj);
139 memcpy(rbuf, &sessid, sizeof(sessid));
140 rbuf += sizeof(sessid);
141 *rbuflen += sizeof(sessid);
143 /* send our public key */
144 BN_bn2bin(dh->pub_key, rbuf);
148 /* buffer to be encrypted */
150 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
155 memcpy(rbuf, &randbuf, sizeof(randbuf));
158 /* get the signature. it's always 16 bytes. */
159 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
160 (void *) &name, NULL) < 0) {
164 memcpy(rbuf + KEYSIZE, name, KEYSIZE);
166 memset(rbuf + KEYSIZE, 0, KEYSIZE);
169 /* encrypt using cast */
170 CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, iv, CAST_ENCRYPT);
171 *rbuflen += CRYPTBUFLEN;
174 return AFPERR_AUTHCONT;
182 static int passwd_logincont(void *obj, struct passwd **uam_pwd,
183 char *ibuf, int ibuflen,
184 char *rbuf, int *rbuflen)
186 unsigned char iv[] = "LWallace";
187 BIGNUM *bn1, *bn2, *bn3;
193 /* check for session id */
194 memcpy(&sessid, ibuf, sizeof(sessid));
195 if (sessid != dhxhash(obj))
197 ibuf += sizeof(sessid);
199 /* use rbuf as scratch space */
200 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
203 /* check to make sure that the random number is the same. we
204 * get sent back an incremented random number. */
205 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
208 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
213 /* zero out the random number */
214 memset(rbuf, 0, sizeof(randbuf));
215 memset(randbuf, 0, sizeof(randbuf));
218 if (!(bn3 = BN_new())) {
224 BN_sub(bn3, bn1, bn2);
228 /* okay. is it one more? */
229 if (!BN_is_one(bn3)) {
235 rbuf[PASSWDLEN] = '\0';
236 p = crypt( rbuf, dhxpwd->pw_passwd );
237 memset(rbuf, 0, PASSWDLEN);
238 if ( strcmp( p, dhxpwd->pw_passwd ) == 0 ) {
243 return AFPERR_NOTAUTH;
247 static int uam_setup(const char *path)
249 if (uam_register(UAM_SERVER_LOGIN, path, "DHCAST128",
250 passwd_login, passwd_logincont, NULL) < 0)
252 /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
258 static void uam_cleanup(void)
260 uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
261 /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
264 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
267 uam_setup, uam_cleanup