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 */
22 #define _XOPEN_SOURCE 500 /* for crypt() */
25 #define _XOPEN_SOURCE /* for crypt() */
36 #ifdef HAVE_SYS_TIME_H
52 #include <atalk/afp.h>
53 #include <atalk/uam.h>
54 #include <atalk/logger.h>
56 /* Number of bits for p which we generate. Everybode out there uses 512, so we beet them */
57 #define PRIMEBITS 1024
59 /* hash a number to a 16-bit quantity */
60 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
61 (unsigned long) (a)) & 0xffff)
63 /* Some parameters need be maintained across calls */
64 static gcry_mpi_t p, Ra;
65 static gcry_mpi_t serverNonce;
66 static char *K_MD5hash = NULL;
67 static int K_hash_len;
70 /* The initialization vectors for CAST128 are fixed by Apple. */
71 static unsigned char dhx_c2siv[] = { 'L', 'W', 'a', 'l', 'l', 'a', 'c', 'e' };
72 static unsigned char dhx_s2civ[] = { 'C', 'J', 'a', 'l', 'b', 'e', 'r', 't' };
74 /* Static variables used to communicate between the conversation function
75 * and the server_login function */
76 static struct passwd *dhxpwd;
78 /*********************************************************
79 * Crypto helper func to generate p and g for use in DH.
80 * libgcrypt doesn't provide one directly.
81 * Algorithm taken from GNUTLS:gnutls_dh_primes.c
82 *********************************************************/
85 * This function will generate a new pair of prime and generator for use in
86 * the Diffie-Hellman key exchange.
87 * The bits value should be one of 768, 1024, 2048, 3072 or 4096.
91 dh_params_generate (gcry_mpi_t *ret_p, gcry_mpi_t *ret_g, unsigned int bits) {
93 int result, times = 0, qbits;
95 gcry_mpi_t g = NULL, prime = NULL;
96 gcry_mpi_t *factors = NULL;
99 /* Version check should be the very first call because it
100 makes sure that important subsystems are intialized. */
101 if (!gcry_check_version (GCRYPT_VERSION)) {
102 LOG(log_info, logtype_uams, "PAM DHX2: libgcrypt versions mismatch. Need: %s", GCRYPT_VERSION);
103 result = AFPERR_MISC;
110 qbits = (bits / 40) + 105;
112 if (qbits & 1) /* better have an even number */
115 /* find a prime number of size bits. */
118 gcry_mpi_release (prime);
119 gcry_prime_release_factors (factors);
121 err = gcry_prime_generate (&prime, bits, qbits, &factors, NULL, NULL,
122 GCRY_STRONG_RANDOM, GCRY_PRIME_FLAG_SPECIAL_FACTOR);
124 result = AFPERR_MISC;
127 err = gcry_prime_check (prime, 0);
129 } while (err != 0 && times < 10);
132 result = AFPERR_MISC;
136 /* generate the group generator. */
137 err = gcry_prime_group_generator (&g, prime, factors, NULL);
139 result = AFPERR_MISC;
143 gcry_prime_release_factors (factors);
149 gcry_mpi_release (g);
153 gcry_mpi_release (prime);
158 gcry_prime_release_factors (factors);
159 gcry_mpi_release (g);
160 gcry_mpi_release (prime);
165 static int dhx2_setup(void *obj, char *ibuf _U_, size_t ibuflen _U_,
166 char *rbuf, size_t *rbuflen)
171 char *Ra_binary = NULL;
174 #endif /* SHADOWPW */
178 /* Initialize passwd/shadow */
180 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
181 LOG(log_info, logtype_uams, "DHX2: no shadow passwd entry for this user");
182 return AFPERR_NOTAUTH;
184 dhxpwd->pw_passwd = sp->sp_pwdp;
185 #endif /* SHADOWPW */
187 if (!dhxpwd->pw_passwd)
188 return AFPERR_NOTAUTH;
190 /* Initialize DH params */
194 Ra = gcry_mpi_new(0);
195 Ma = gcry_mpi_new(0);
197 /* Generate p and g for DH */
198 ret = dh_params_generate( &p, &g, PRIMEBITS);
200 LOG(log_info, logtype_uams, "DHX2: Couldn't generate p and g");
205 /* Generate our random number Ra. */
206 Ra_binary = calloc(1, PRIMEBITS/8);
207 if (Ra_binary == NULL) {
211 gcry_randomize(Ra_binary, PRIMEBITS/8, GCRY_STRONG_RANDOM);
212 gcry_mpi_scan(&Ra, GCRYMPI_FMT_USG, Ra_binary, PRIMEBITS/8, NULL);
216 /* Ma = g^Ra mod p. This is our "public" key */
217 gcry_mpi_powm(Ma, g, Ra, p);
219 /* ------- DH Init done ------ */
220 /* Start building reply packet */
222 /* Session ID first */
224 *(u_int16_t *)rbuf = htons(ID);
229 gcry_mpi_print( GCRYMPI_FMT_USG, (unsigned char *)rbuf, 4, &nwritten, g);
231 memmove( rbuf+4-nwritten, rbuf, nwritten);
232 memset( rbuf, 0, 4-nwritten);
237 /* len = length of p = PRIMEBITS/8 */
238 *(u_int16_t *)rbuf = htons((u_int16_t) PRIMEBITS/8);
243 gcry_mpi_print( GCRYMPI_FMT_USG, (unsigned char *)rbuf, PRIMEBITS/8, NULL, p);
245 *rbuflen += PRIMEBITS/8;
248 gcry_mpi_print( GCRYMPI_FMT_USG, (unsigned char *)rbuf, PRIMEBITS/8, &nwritten, Ma);
249 if (nwritten < PRIMEBITS/8) {
250 memmove(rbuf + (PRIMEBITS/8) - nwritten, rbuf, nwritten);
251 memset(rbuf, 0, (PRIMEBITS/8) - nwritten);
254 *rbuflen += PRIMEBITS/8;
256 ret = AFPERR_AUTHCONT;
258 error: /* We exit here anyway */
259 /* We will only need p and Ra later, but mustn't forget to release it ! */
261 gcry_mpi_release(Ma);
265 /* -------------------------------- */
266 static int login(void *obj, char *username, int ulen, struct passwd **uam_pwd _U_,
267 char *ibuf, size_t ibuflen,
268 char *rbuf, size_t *rbuflen)
270 if (( dhxpwd = uam_getname(obj, username, ulen)) == NULL ) {
271 LOG(log_info, logtype_uams, "DHX2: unknown username");
272 return AFPERR_NOTAUTH;
275 LOG(log_info, logtype_uams, "DHX2 login: %s", username);
276 return dhx2_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
279 /* -------------------------------- */
280 /* dhx login: things are done in a slightly bizarre order to avoid
281 * having to clean things up if there's an error. */
282 static int passwd_login(void *obj, struct passwd **uam_pwd,
283 char *ibuf, size_t ibuflen,
284 char *rbuf, size_t *rbuflen)
291 /* grab some of the options */
292 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
293 LOG(log_info, logtype_uams, "DHX2: uam_afpserver_option didn't meet uam_option_username -- %s",
298 len = (unsigned char) *ibuf++;
300 LOG(log_info, logtype_uams, "DHX2: Signature Retieval Failure -- %s",
305 memcpy(username, ibuf, len );
307 username[ len ] = '\0';
309 if ((unsigned long) ibuf & 1) /* pad to even boundary */
312 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
315 /* ----------------------------- */
316 static int passwd_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
317 char *ibuf, size_t ibuflen,
318 char *rbuf, size_t *rbuflen)
326 /* grab some of the options */
327 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
328 LOG(log_info, logtype_uams, "DHX2: uam_afpserver_option didn't meet uam_option_username -- %s",
336 memcpy(&temp16, uname, sizeof(temp16));
339 if ( !len || len > ulen ) {
340 LOG(log_info, logtype_uams, "DHX2: Signature Retrieval Failure -- %s",
344 memcpy(username, uname +2, len );
345 username[ len ] = '\0';
347 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
350 /* -------------------------------- */
352 static int logincont1(void *obj _U_, struct passwd **uam_pwd _U_,
353 char *ibuf, size_t ibuflen,
354 char *rbuf, size_t *rbuflen)
358 gcry_mpi_t Mb, K, clientNonce;
359 unsigned char *K_bin = NULL;
360 char serverNonce_bin[16];
361 gcry_cipher_hd_t ctx;
362 gcry_error_t ctxerror;
366 Mb = gcry_mpi_new(0);
368 clientNonce = gcry_mpi_new(0);
369 serverNonce = gcry_mpi_new(0);
371 /* Packet size should be: Session ID + Ma + Encrypted client nonce */
372 if (ibuflen != 2 + PRIMEBITS/8 + 16) {
373 LOG(log_error, logtype_uams, "DHX2: Paket length not correct");
378 /* Skip session id */
381 /* Extract Mb, client's "public" key */
382 gcry_mpi_scan(&Mb, GCRYMPI_FMT_USG, ibuf, PRIMEBITS/8, NULL);
385 /* Now finally generate the Key: K = Mb^Ra mod p */
386 gcry_mpi_powm(K, Mb, Ra, p);
388 /* We need K in binary form in order to ... */
389 K_bin = calloc(1, PRIMEBITS/8);
394 gcry_mpi_print(GCRYMPI_FMT_USG, K_bin, PRIMEBITS/8, &nwritten, K);
395 if (nwritten < PRIMEBITS/8) {
396 memmove(K_bin + PRIMEBITS/8 - nwritten, K_bin, nwritten);
397 memset(K_bin, 0, PRIMEBITS/8 - nwritten);
400 /* ... generate the MD5 hash of K. K_MD5hash is what we actually use ! */
401 K_MD5hash = calloc(1, K_hash_len = gcry_md_get_algo_dlen(GCRY_MD_MD5));
402 if (K_MD5hash == NULL) {
408 gcry_md_hash_buffer(GCRY_MD_MD5, K_MD5hash, K_bin, PRIMEBITS/8);
412 /* FIXME: To support the Reconnect UAM, we need to store this key somewhere */
414 /* Set up our encryption context. */
415 ctxerror = gcry_cipher_open( &ctx, GCRY_CIPHER_CAST5, GCRY_CIPHER_MODE_CBC, 0);
416 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
421 ctxerror = gcry_cipher_setkey(ctx, K_MD5hash, K_hash_len);
422 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
426 /* Set the initialization vector for client->server transfer. */
427 ctxerror = gcry_cipher_setiv(ctx, dhx_c2siv, sizeof(dhx_c2siv));
428 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
432 /* Finally: decrypt client's md5_K(client nonce, C2SIV) inplace */
433 ctxerror = gcry_cipher_decrypt(ctx, ibuf, 16, NULL, 0);
434 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
438 /* Pull out clients nonce */
439 gcry_mpi_scan(&clientNonce, GCRYMPI_FMT_USG, ibuf, 16, NULL);
440 /* Increment nonce */
441 gcry_mpi_add_ui(clientNonce, clientNonce, 1);
443 /* Generate our nonce and remember it for Logincont2 */
444 gcry_create_nonce(serverNonce_bin, 16); /* We'll use this here */
445 gcry_mpi_scan(&serverNonce, GCRYMPI_FMT_USG, serverNonce_bin, 16, NULL); /* For use in Logincont2 */
447 /* ---- Start building reply packet ---- */
449 /* Session ID + 1 first */
450 *(u_int16_t *)rbuf = htons(ID+1);
454 /* Client nonce + 1 */
455 gcry_mpi_print(GCRYMPI_FMT_USG, (unsigned char *)rbuf, PRIMEBITS/8, NULL, clientNonce);
457 memcpy(rbuf+16, serverNonce_bin, 16);
459 /* Set the initialization vector for server->client transfer. */
460 ctxerror = gcry_cipher_setiv(ctx, dhx_s2civ, sizeof(dhx_s2civ));
461 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
465 /* Encrypt md5_K(clientNonce+1, serverNonce) inplace */
466 ctxerror = gcry_cipher_encrypt(ctx, rbuf, 32, NULL, 0);
467 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
474 ret = AFPERR_AUTHCONT;
478 gcry_cipher_close(ctx);
480 gcry_mpi_release(serverNonce);
485 gcry_mpi_release(Mb);
486 gcry_mpi_release(Ra);
488 gcry_mpi_release(clientNonce);
492 static int logincont2(void *obj _U_, struct passwd **uam_pwd,
493 char *ibuf, size_t ibuflen,
494 char *rbuf _U_, size_t *rbuflen)
498 #endif /* SHADOWPW */
501 gcry_mpi_t retServerNonce;
502 gcry_cipher_hd_t ctx;
503 gcry_error_t ctxerror;
506 retServerNonce = gcry_mpi_new(0);
508 /* Packet size should be: Session ID + ServerNonce + Passwd buffer (evantually +10 extra bytes, see Apples Docs)*/
509 if ((ibuflen != 2 + 16 + 256) && (ibuflen != 2 + 16 + 256 + 10)) {
510 LOG(log_error, logtype_uams, "DHX2: Paket length not correct: %d. Should be 274 or 284.", ibuflen);
515 /* Set up our encryption context. */
516 ctxerror = gcry_cipher_open( &ctx, GCRY_CIPHER_CAST5, GCRY_CIPHER_MODE_CBC, 0);
517 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
522 ctxerror = gcry_cipher_setkey(ctx, K_MD5hash, K_hash_len);
523 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
527 /* Set the initialization vector for client->server transfer. */
528 ctxerror = gcry_cipher_setiv(ctx, dhx_c2siv, sizeof(dhx_c2siv));
529 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
534 /* Skip Session ID */
537 /* Finally: decrypt client's md5_K(serverNonce+1, passwor, C2SIV) inplace */
538 ctxerror = gcry_cipher_decrypt(ctx, ibuf, 16+256, NULL, 0);
539 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
543 /* Pull out nonce. Should be serverNonce+1 */
544 gcry_mpi_scan(&retServerNonce, GCRYMPI_FMT_USG, ibuf, 16, NULL);
545 gcry_mpi_sub_ui(retServerNonce, retServerNonce, 1);
546 if ( gcry_mpi_cmp( serverNonce, retServerNonce) != 0) {
548 ret = AFPERR_NOTAUTH;
551 ibuf += 16; /* ibuf now point to passwd in cleartext */
553 /* ---- Start authentication --- */
554 ret = AFPERR_NOTAUTH;
556 p = crypt( ibuf, dhxpwd->pw_passwd );
557 memset(ibuf, 0, 255);
558 if ( strcmp( p, dhxpwd->pw_passwd ) == 0 ) {
564 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
565 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", dhxpwd->pw_name);
566 ret = AFPERR_NOTAUTH;
570 /* check for expired password */
571 if (sp && sp->sp_max != -1 && sp->sp_lstchg) {
572 time_t now = time(NULL) / (60*60*24);
573 int32_t expire_days = sp->sp_lstchg - now + sp->sp_max;
574 if ( expire_days < 0 ) {
575 LOG(log_info, logtype_uams, "password for user %s expired", dhxpwd->pw_name);
576 ret = AFPERR_PWDEXPR;
580 #endif /* SHADOWPW */
583 gcry_cipher_close(ctx);
588 gcry_mpi_release(serverNonce);
589 gcry_mpi_release(retServerNonce);
593 static int passwd_logincont(void *obj, struct passwd **uam_pwd,
594 char *ibuf, size_t ibuflen,
595 char *rbuf, size_t *rbuflen)
600 /* check for session id */
601 retID = ntohs(*(u_int16_t *)ibuf);
603 ret = logincont1(obj, uam_pwd, ibuf, ibuflen, rbuf, rbuflen);
604 else if (retID == ID+1)
605 ret = logincont2(obj, uam_pwd, ibuf,ibuflen, rbuf, rbuflen);
607 LOG(log_info, logtype_uams, "DHX2: Session ID Mismatch");
613 static int uam_setup(const char *path)
615 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "DHX2", passwd_login,
616 passwd_logincont, NULL, passwd_login_ext) < 0)
621 static void uam_cleanup(void)
623 uam_unregister(UAM_SERVER_LOGIN, "DHX2");
627 UAM_MODULE_EXPORT struct uam_export uams_dhx2 = {
630 uam_setup, uam_cleanup
634 UAM_MODULE_EXPORT struct uam_export uams_dhx2_passwd = {
637 uam_setup, uam_cleanup
640 #endif /* UAM_DHX2 */