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.
19 #include <openssl/bn.h>
20 #include <openssl/dh.h>
21 #include <openssl/cast.h>
28 #include <atalk/afp.h>
29 #include <atalk/uam.h>
33 #define CRYPTBUFLEN (KEYSIZE*2)
34 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
36 /* hash a number to a 16-bit quantity */
37 #define pgphash(a) ((((unsigned long) (a) >> 8) ^ \
38 (unsigned long) (a)) & 0xffff)
41 static struct passwd *pgppwd;
42 static CAST_KEY castkey;
43 static u_int8_t randbuf[16];
46 static int pgp_login(void *obj, struct passwd **uam_pwd,
47 char *ibuf, int ibuflen,
48 char *rbuf, int *rbuflen)
50 BIGNUM *bn, *gbn, *pbn;
57 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &name, &i) < 0)
60 len = (unsigned char) *ibuf++;
62 return( AFPERR_PARAM );
65 memcpy(name, ibuf, len );
68 if ((unsigned long) ibuf & 1) /* padding */
71 if (( pgppwd = uam_getname(name, i)) == NULL ) {
75 syslog( LOG_INFO, "pgp login: %s", name);
76 if (uam_checkuser(pgppwd) < 0)
77 return AFPERR_NOTAUTH;
79 /* get the challenge */
80 len = (unsigned char) *ibuf++;
83 /* get the signature. it's always 16 bytes. */
84 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
85 (void *) &name, NULL) < 0) {
89 memcpy(rbuf + KEYSIZE, name, KEYSIZE);
95 static int pgp_logincont(void *obj, struct passwd **uam_pwd,
96 char *ibuf, int ibuflen,
97 char *rbuf, int *rbuflen)
99 unsigned char iv[] = "RJscorat";
100 BIGNUM *bn1, *bn2, *bn3;
106 /* check for session id */
107 memcpy(&sessid, ibuf, sizeof(sessid));
108 if (sessid != pgphash(obj))
110 ibuf += sizeof(sessid);
112 /* use rbuf as scratch space */
113 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
116 /* check to make sure that the random number is the same. we
117 * get sent back an incremented random number. */
118 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
121 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
126 /* zero out the random number */
127 memset(rbuf, 0, sizeof(randbuf));
128 memset(randbuf, 0, sizeof(randbuf));
131 if (!(bn3 = BN_new())) {
137 BN_sub(bn3, bn1, bn2);
141 /* okay. is it one more? */
142 if (!BN_is_one(bn3)) {
149 if ( kcheckuser(*uam_pwd, rbuf) == 0) {
155 rbuf[PASSWDLEN] = '\0';
156 p = crypt( rbuf, pgppwd->pw_passwd );
157 memset(rbuf, 0, PASSWDLEN);
158 if ( strcmp( p, pgppwd->pw_passwd ) == 0 ) {
163 return AFPERR_NOTAUTH;
167 static int uam_setup(const char *path)
169 if (uam_register(UAM_SERVER_LOGIN, path, "PGPuam 1.0",
170 pgp_login, pgp_logincont, NULL) < 0)
176 static void uam_cleanup(void)
178 uam_unregister(UAM_SERVER_LOGIN, "PGPuam 1.0");
181 UAM_MODULE_EXPORT struct uam_export uams_pgp = {
184 uam_setup, uam_cleanup