2 * $Id: uams_pgp.c,v 1.7 2002-09-29 23:30:20 sibaz 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.
11 #endif /* HAVE_CONFIG_H */
20 #endif /* HAVE_UNISTD_H */
22 #include <atalk/logger.h>
25 #include <openssl/bn.h>
26 #include <openssl/dh.h>
27 #include <openssl/cast.h>
28 #else /* OPENSSL_DHX */
32 #endif /* OPENSSL_DHX */
34 #include <atalk/afp.h>
35 #include <atalk/uam.h>
39 #define CRYPTBUFLEN (KEYSIZE*2)
40 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
42 /* hash a number to a 16-bit quantity */
43 #define pgphash(a) ((((unsigned long) (a) >> 8) ^ \
44 (unsigned long) (a)) & 0xffff)
47 static struct passwd *pgppwd;
48 static CAST_KEY castkey;
49 static u_int8_t randbuf[16];
52 static int pgp_login(void *obj, struct passwd **uam_pwd,
53 char *ibuf, int ibuflen,
54 char *rbuf, int *rbuflen)
56 BIGNUM *bn, *gbn, *pbn;
63 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &name, &i) < 0)
66 len = (unsigned char) *ibuf++;
68 return( AFPERR_PARAM );
71 memcpy(name, ibuf, len );
74 if ((unsigned long) ibuf & 1) /* padding */
77 if (( pgppwd = uam_getname(name, i)) == NULL ) {
81 LOG(log_info, logtype_uams, "pgp login: %s", name);
82 if (uam_checkuser(pgppwd) < 0)
83 return AFPERR_NOTAUTH;
85 /* get the challenge */
86 len = (unsigned char) *ibuf++;
89 /* get the signature. it's always 16 bytes. */
90 if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE,
91 (void *) &name, NULL) < 0) {
95 memcpy(rbuf + KEYSIZE, name, KEYSIZE);
101 static int pgp_logincont(void *obj, struct passwd **uam_pwd,
102 char *ibuf, int ibuflen,
103 char *rbuf, int *rbuflen)
105 unsigned char iv[] = "RJscorat";
106 BIGNUM *bn1, *bn2, *bn3;
112 /* check for session id */
113 memcpy(&sessid, ibuf, sizeof(sessid));
114 if (sessid != pgphash(obj))
116 ibuf += sizeof(sessid);
118 /* use rbuf as scratch space */
119 CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
122 /* check to make sure that the random number is the same. we
123 * get sent back an incremented random number. */
124 if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
127 if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
132 /* zero out the random number */
133 memset(rbuf, 0, sizeof(randbuf));
134 memset(randbuf, 0, sizeof(randbuf));
137 if (!(bn3 = BN_new())) {
143 BN_sub(bn3, bn1, bn2);
147 /* okay. is it one more? */
148 if (!BN_is_one(bn3)) {
155 if ( kcheckuser(*uam_pwd, rbuf) == 0) {
161 rbuf[PASSWDLEN] = '\0';
162 p = crypt( rbuf, pgppwd->pw_passwd );
163 memset(rbuf, 0, PASSWDLEN);
164 if ( strcmp( p, pgppwd->pw_passwd ) == 0 ) {
169 return AFPERR_NOTAUTH;
173 static int uam_setup(const char *path)
175 if (uam_register(UAM_SERVER_LOGIN, path, "PGPuam 1.0",
176 pgp_login, pgp_logincont, NULL) < 0)
182 static void uam_cleanup(void)
184 uam_unregister(UAM_SERVER_LOGIN, "PGPuam 1.0");
187 UAM_MODULE_EXPORT struct uam_export uams_pgp = {
190 uam_setup, uam_cleanup