2 * $Id: uams_pgp.c,v 1.4 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.
22 #include <openssl/bn.h>
23 #include <openssl/dh.h>
24 #include <openssl/cast.h>
29 #endif /* OPENSSL_DHX */
31 #include <atalk/afp.h>
32 #include <atalk/uam.h>
36 #define CRYPTBUFLEN (KEYSIZE*2)
37 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
39 /* hash a number to a 16-bit quantity */
40 #define pgphash(a) ((((unsigned long) (a) >> 8) ^ \
41 (unsigned long) (a)) & 0xffff)
44 static struct passwd *pgppwd;
45 static CAST_KEY castkey;
46 static u_int8_t randbuf[16];
49 static int pgp_login(void *obj, struct passwd **uam_pwd,
50 char *ibuf, int ibuflen,
51 char *rbuf, int *rbuflen)
53 BIGNUM *bn, *gbn, *pbn;
60 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &name, &i) < 0)
63 len = (unsigned char) *ibuf++;
65 return( AFPERR_PARAM );
68 memcpy(name, ibuf, len );
71 if ((unsigned long) ibuf & 1) /* padding */
74 if (( pgppwd = uam_getname(name, i)) == NULL ) {
78 syslog( LOG_INFO, "pgp login: %s", name);
79 if (uam_checkuser(pgppwd) < 0)
80 return AFPERR_NOTAUTH;
82 /* get the challenge */
83 len = (unsigned char) *ibuf++;
86 /* get the signature. it's always 16 bytes. */
87 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
88 (void *) &name, NULL) < 0) {
92 memcpy(rbuf + KEYSIZE, name, KEYSIZE);
98 static int pgp_logincont(void *obj, struct passwd **uam_pwd,
99 char *ibuf, int ibuflen,
100 char *rbuf, int *rbuflen)
102 unsigned char iv[] = "RJscorat";
103 BIGNUM *bn1, *bn2, *bn3;
109 /* check for session id */
110 memcpy(&sessid, ibuf, sizeof(sessid));
111 if (sessid != pgphash(obj))
113 ibuf += sizeof(sessid);
115 /* use rbuf as scratch space */
116 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
119 /* check to make sure that the random number is the same. we
120 * get sent back an incremented random number. */
121 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
124 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
129 /* zero out the random number */
130 memset(rbuf, 0, sizeof(randbuf));
131 memset(randbuf, 0, sizeof(randbuf));
134 if (!(bn3 = BN_new())) {
140 BN_sub(bn3, bn1, bn2);
144 /* okay. is it one more? */
145 if (!BN_is_one(bn3)) {
152 if ( kcheckuser(*uam_pwd, rbuf) == 0) {
158 rbuf[PASSWDLEN] = '\0';
159 p = crypt( rbuf, pgppwd->pw_passwd );
160 memset(rbuf, 0, PASSWDLEN);
161 if ( strcmp( p, pgppwd->pw_passwd ) == 0 ) {
166 return AFPERR_NOTAUTH;
170 static int uam_setup(const char *path)
172 if (uam_register(UAM_SERVER_LOGIN, path, "PGPuam 1.0",
173 pgp_login, pgp_logincont, NULL) < 0)
179 static void uam_cleanup(void)
181 uam_unregister(UAM_SERVER_LOGIN, "PGPuam 1.0");
184 UAM_MODULE_EXPORT struct uam_export uams_pgp = {
187 uam_setup, uam_cleanup