2 * $Id: uams_dhx2_passwd.c,v 1.8 2010-03-30 10:25:49 franklahm 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 */
15 #include <atalk/standards.h>
31 #ifdef HAVE_SYS_TIME_H
47 #include <atalk/afp.h>
48 #include <atalk/uam.h>
49 #include <atalk/logger.h>
51 /* Number of bits for p which we generate. Everybode out there uses 512, so we beet them */
52 #define PRIMEBITS 1024
54 /* hash a number to a 16-bit quantity */
55 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
56 (unsigned long) (a)) & 0xffff)
58 /* Some parameters need be maintained across calls */
59 static gcry_mpi_t p, Ra;
60 static gcry_mpi_t serverNonce;
61 static char *K_MD5hash = NULL;
62 static int K_hash_len;
65 /* The initialization vectors for CAST128 are fixed by Apple. */
66 static unsigned char dhx_c2siv[] = { 'L', 'W', 'a', 'l', 'l', 'a', 'c', 'e' };
67 static unsigned char dhx_s2civ[] = { 'C', 'J', 'a', 'l', 'b', 'e', 'r', 't' };
69 /* Static variables used to communicate between the conversation function
70 * and the server_login function */
71 static struct passwd *dhxpwd;
73 /*********************************************************
74 * Crypto helper func to generate p and g for use in DH.
75 * libgcrypt doesn't provide one directly.
76 * Algorithm taken from GNUTLS:gnutls_dh_primes.c
77 *********************************************************/
80 * This function will generate a new pair of prime and generator for use in
81 * the Diffie-Hellman key exchange.
82 * The bits value should be one of 768, 1024, 2048, 3072 or 4096.
86 dh_params_generate (gcry_mpi_t *ret_p, gcry_mpi_t *ret_g, unsigned int bits) {
88 int result, times = 0, qbits;
90 gcry_mpi_t g = NULL, prime = NULL;
91 gcry_mpi_t *factors = NULL;
94 /* Version check should be the very first call because it
95 makes sure that important subsystems are intialized. */
96 if (!gcry_check_version (GCRYPT_VERSION)) {
97 LOG(log_info, logtype_uams, "PAM DHX2: libgcrypt versions mismatch. Need: %s", GCRYPT_VERSION);
105 qbits = (bits / 40) + 105;
107 if (qbits & 1) /* better have an even number */
110 /* find a prime number of size bits. */
113 gcry_mpi_release (prime);
114 gcry_prime_release_factors (factors);
116 err = gcry_prime_generate (&prime, bits, qbits, &factors, NULL, NULL,
117 GCRY_STRONG_RANDOM, GCRY_PRIME_FLAG_SPECIAL_FACTOR);
119 result = AFPERR_MISC;
122 err = gcry_prime_check (prime, 0);
124 } while (err != 0 && times < 10);
127 result = AFPERR_MISC;
131 /* generate the group generator. */
132 err = gcry_prime_group_generator (&g, prime, factors, NULL);
134 result = AFPERR_MISC;
138 gcry_prime_release_factors (factors);
144 gcry_mpi_release (g);
148 gcry_mpi_release (prime);
153 gcry_prime_release_factors (factors);
154 gcry_mpi_release (g);
155 gcry_mpi_release (prime);
160 static int dhx2_setup(void *obj, char *ibuf _U_, size_t ibuflen _U_,
161 char *rbuf, size_t *rbuflen)
166 char *Ra_binary = NULL;
169 #endif /* SHADOWPW */
173 /* Initialize passwd/shadow */
175 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
176 LOG(log_info, logtype_uams, "DHX2: no shadow passwd entry for this user");
177 return AFPERR_NOTAUTH;
179 dhxpwd->pw_passwd = sp->sp_pwdp;
180 #endif /* SHADOWPW */
182 if (!dhxpwd->pw_passwd)
183 return AFPERR_NOTAUTH;
185 /* Initialize DH params */
189 Ra = gcry_mpi_new(0);
190 Ma = gcry_mpi_new(0);
192 /* Generate p and g for DH */
193 ret = dh_params_generate( &p, &g, PRIMEBITS);
195 LOG(log_info, logtype_uams, "DHX2: Couldn't generate p and g");
200 /* Generate our random number Ra. */
201 Ra_binary = calloc(1, PRIMEBITS/8);
202 if (Ra_binary == NULL) {
206 gcry_randomize(Ra_binary, PRIMEBITS/8, GCRY_STRONG_RANDOM);
207 gcry_mpi_scan(&Ra, GCRYMPI_FMT_USG, Ra_binary, PRIMEBITS/8, NULL);
211 /* Ma = g^Ra mod p. This is our "public" key */
212 gcry_mpi_powm(Ma, g, Ra, p);
214 /* ------- DH Init done ------ */
215 /* Start building reply packet */
217 /* Session ID first */
219 *(u_int16_t *)rbuf = htons(ID);
224 gcry_mpi_print( GCRYMPI_FMT_USG, (unsigned char *)rbuf, 4, &nwritten, g);
226 memmove( rbuf+4-nwritten, rbuf, nwritten);
227 memset( rbuf, 0, 4-nwritten);
232 /* len = length of p = PRIMEBITS/8 */
233 *(u_int16_t *)rbuf = htons((u_int16_t) PRIMEBITS/8);
238 gcry_mpi_print( GCRYMPI_FMT_USG, (unsigned char *)rbuf, PRIMEBITS/8, NULL, p);
240 *rbuflen += PRIMEBITS/8;
243 gcry_mpi_print( GCRYMPI_FMT_USG, (unsigned char *)rbuf, PRIMEBITS/8, &nwritten, Ma);
244 if (nwritten < PRIMEBITS/8) {
245 memmove(rbuf + (PRIMEBITS/8) - nwritten, rbuf, nwritten);
246 memset(rbuf, 0, (PRIMEBITS/8) - nwritten);
249 *rbuflen += PRIMEBITS/8;
251 ret = AFPERR_AUTHCONT;
253 error: /* We exit here anyway */
254 /* We will only need p and Ra later, but mustn't forget to release it ! */
256 gcry_mpi_release(Ma);
260 /* -------------------------------- */
261 static int login(void *obj, char *username, int ulen, struct passwd **uam_pwd _U_,
262 char *ibuf, size_t ibuflen,
263 char *rbuf, size_t *rbuflen)
265 if (( dhxpwd = uam_getname(obj, username, ulen)) == NULL ) {
266 LOG(log_info, logtype_uams, "DHX2: unknown username");
267 return AFPERR_NOTAUTH;
270 LOG(log_info, logtype_uams, "DHX2 login: %s", username);
271 return dhx2_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
274 /* -------------------------------- */
275 /* dhx login: things are done in a slightly bizarre order to avoid
276 * having to clean things up if there's an error. */
277 static int passwd_login(void *obj, struct passwd **uam_pwd,
278 char *ibuf, size_t ibuflen,
279 char *rbuf, size_t *rbuflen)
286 /* grab some of the options */
287 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
288 LOG(log_info, logtype_uams, "DHX2: uam_afpserver_option didn't meet uam_option_username -- %s",
293 len = (unsigned char) *ibuf++;
295 LOG(log_info, logtype_uams, "DHX2: Signature Retieval Failure -- %s",
300 memcpy(username, ibuf, len );
302 username[ len ] = '\0';
304 if ((unsigned long) ibuf & 1) /* pad to even boundary */
307 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
310 /* ----------------------------- */
311 static int passwd_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
312 char *ibuf, size_t ibuflen,
313 char *rbuf, size_t *rbuflen)
321 /* grab some of the options */
322 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
323 LOG(log_info, logtype_uams, "DHX2: uam_afpserver_option didn't meet uam_option_username -- %s",
331 memcpy(&temp16, uname, sizeof(temp16));
334 if ( !len || len > ulen ) {
335 LOG(log_info, logtype_uams, "DHX2: Signature Retrieval Failure -- %s",
339 memcpy(username, uname +2, len );
340 username[ len ] = '\0';
342 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
345 /* -------------------------------- */
347 static int logincont1(void *obj _U_, struct passwd **uam_pwd _U_,
348 char *ibuf, size_t ibuflen,
349 char *rbuf, size_t *rbuflen)
353 gcry_mpi_t Mb, K, clientNonce;
354 unsigned char *K_bin = NULL;
355 char serverNonce_bin[16];
356 gcry_cipher_hd_t ctx;
357 gcry_error_t ctxerror;
361 Mb = gcry_mpi_new(0);
363 clientNonce = gcry_mpi_new(0);
364 serverNonce = gcry_mpi_new(0);
366 /* Packet size should be: Session ID + Ma + Encrypted client nonce */
367 if (ibuflen != 2 + PRIMEBITS/8 + 16) {
368 LOG(log_error, logtype_uams, "DHX2: Paket length not correct");
373 /* Skip session id */
376 /* Extract Mb, client's "public" key */
377 gcry_mpi_scan(&Mb, GCRYMPI_FMT_USG, ibuf, PRIMEBITS/8, NULL);
380 /* Now finally generate the Key: K = Mb^Ra mod p */
381 gcry_mpi_powm(K, Mb, Ra, p);
383 /* We need K in binary form in order to ... */
384 K_bin = calloc(1, PRIMEBITS/8);
389 gcry_mpi_print(GCRYMPI_FMT_USG, K_bin, PRIMEBITS/8, &nwritten, K);
390 if (nwritten < PRIMEBITS/8) {
391 memmove(K_bin + PRIMEBITS/8 - nwritten, K_bin, nwritten);
392 memset(K_bin, 0, PRIMEBITS/8 - nwritten);
395 /* ... generate the MD5 hash of K. K_MD5hash is what we actually use ! */
396 K_MD5hash = calloc(1, K_hash_len = gcry_md_get_algo_dlen(GCRY_MD_MD5));
397 if (K_MD5hash == NULL) {
403 gcry_md_hash_buffer(GCRY_MD_MD5, K_MD5hash, K_bin, PRIMEBITS/8);
407 /* FIXME: To support the Reconnect UAM, we need to store this key somewhere */
409 /* Set up our encryption context. */
410 ctxerror = gcry_cipher_open( &ctx, GCRY_CIPHER_CAST5, GCRY_CIPHER_MODE_CBC, 0);
411 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
416 ctxerror = gcry_cipher_setkey(ctx, K_MD5hash, K_hash_len);
417 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
421 /* Set the initialization vector for client->server transfer. */
422 ctxerror = gcry_cipher_setiv(ctx, dhx_c2siv, sizeof(dhx_c2siv));
423 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
427 /* Finally: decrypt client's md5_K(client nonce, C2SIV) inplace */
428 ctxerror = gcry_cipher_decrypt(ctx, ibuf, 16, NULL, 0);
429 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
433 /* Pull out clients nonce */
434 gcry_mpi_scan(&clientNonce, GCRYMPI_FMT_USG, ibuf, 16, NULL);
435 /* Increment nonce */
436 gcry_mpi_add_ui(clientNonce, clientNonce, 1);
438 /* Generate our nonce and remember it for Logincont2 */
439 gcry_create_nonce(serverNonce_bin, 16); /* We'll use this here */
440 gcry_mpi_scan(&serverNonce, GCRYMPI_FMT_USG, serverNonce_bin, 16, NULL); /* For use in Logincont2 */
442 /* ---- Start building reply packet ---- */
444 /* Session ID + 1 first */
445 *(u_int16_t *)rbuf = htons(ID+1);
449 /* Client nonce + 1 */
450 gcry_mpi_print(GCRYMPI_FMT_USG, (unsigned char *)rbuf, PRIMEBITS/8, NULL, clientNonce);
452 memcpy(rbuf+16, serverNonce_bin, 16);
454 /* Set the initialization vector for server->client transfer. */
455 ctxerror = gcry_cipher_setiv(ctx, dhx_s2civ, sizeof(dhx_s2civ));
456 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
460 /* Encrypt md5_K(clientNonce+1, serverNonce) inplace */
461 ctxerror = gcry_cipher_encrypt(ctx, rbuf, 32, NULL, 0);
462 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
469 ret = AFPERR_AUTHCONT;
473 gcry_cipher_close(ctx);
475 gcry_mpi_release(serverNonce);
480 gcry_mpi_release(Mb);
481 gcry_mpi_release(Ra);
483 gcry_mpi_release(clientNonce);
487 static int logincont2(void *obj _U_, struct passwd **uam_pwd,
488 char *ibuf, size_t ibuflen,
489 char *rbuf _U_, size_t *rbuflen)
493 #endif /* SHADOWPW */
496 gcry_mpi_t retServerNonce;
497 gcry_cipher_hd_t ctx;
498 gcry_error_t ctxerror;
501 retServerNonce = gcry_mpi_new(0);
503 /* Packet size should be: Session ID + ServerNonce + Passwd buffer (evantually +10 extra bytes, see Apples Docs)*/
504 if ((ibuflen != 2 + 16 + 256) && (ibuflen != 2 + 16 + 256 + 10)) {
505 LOG(log_error, logtype_uams, "DHX2: Paket length not correct: %d. Should be 274 or 284.", ibuflen);
510 /* Set up our encryption context. */
511 ctxerror = gcry_cipher_open( &ctx, GCRY_CIPHER_CAST5, GCRY_CIPHER_MODE_CBC, 0);
512 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
517 ctxerror = gcry_cipher_setkey(ctx, K_MD5hash, K_hash_len);
518 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
522 /* Set the initialization vector for client->server transfer. */
523 ctxerror = gcry_cipher_setiv(ctx, dhx_c2siv, sizeof(dhx_c2siv));
524 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
529 /* Skip Session ID */
532 /* Finally: decrypt client's md5_K(serverNonce+1, passwor, C2SIV) inplace */
533 ctxerror = gcry_cipher_decrypt(ctx, ibuf, 16+256, NULL, 0);
534 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
538 /* Pull out nonce. Should be serverNonce+1 */
539 gcry_mpi_scan(&retServerNonce, GCRYMPI_FMT_USG, ibuf, 16, NULL);
540 gcry_mpi_sub_ui(retServerNonce, retServerNonce, 1);
541 if ( gcry_mpi_cmp( serverNonce, retServerNonce) != 0) {
543 ret = AFPERR_NOTAUTH;
546 ibuf += 16; /* ibuf now point to passwd in cleartext */
548 /* ---- Start authentication --- */
549 ret = AFPERR_NOTAUTH;
551 p = crypt( ibuf, dhxpwd->pw_passwd );
552 memset(ibuf, 0, 255);
553 if ( strcmp( p, dhxpwd->pw_passwd ) == 0 ) {
559 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
560 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", dhxpwd->pw_name);
561 ret = AFPERR_NOTAUTH;
565 /* check for expired password */
566 if (sp && sp->sp_max != -1 && sp->sp_lstchg) {
567 time_t now = time(NULL) / (60*60*24);
568 int32_t expire_days = sp->sp_lstchg - now + sp->sp_max;
569 if ( expire_days < 0 ) {
570 LOG(log_info, logtype_uams, "password for user %s expired", dhxpwd->pw_name);
571 ret = AFPERR_PWDEXPR;
575 #endif /* SHADOWPW */
578 gcry_cipher_close(ctx);
583 gcry_mpi_release(serverNonce);
584 gcry_mpi_release(retServerNonce);
588 static int passwd_logincont(void *obj, struct passwd **uam_pwd,
589 char *ibuf, size_t ibuflen,
590 char *rbuf, size_t *rbuflen)
595 /* check for session id */
596 retID = ntohs(*(u_int16_t *)ibuf);
598 ret = logincont1(obj, uam_pwd, ibuf, ibuflen, rbuf, rbuflen);
599 else if (retID == ID+1)
600 ret = logincont2(obj, uam_pwd, ibuf,ibuflen, rbuf, rbuflen);
602 LOG(log_info, logtype_uams, "DHX2: Session ID Mismatch");
608 static int uam_setup(const char *path)
610 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "DHX2", passwd_login,
611 passwd_logincont, NULL, passwd_login_ext) < 0)
616 static void uam_cleanup(void)
618 uam_unregister(UAM_SERVER_LOGIN, "DHX2");
622 UAM_MODULE_EXPORT struct uam_export uams_dhx2 = {
625 uam_setup, uam_cleanup
629 UAM_MODULE_EXPORT struct uam_export uams_dhx2_passwd = {
632 uam_setup, uam_cleanup
635 #endif /* UAM_DHX2 */