3 * Copyright (c) 1990,1993 Regents of The University of Michigan.
4 * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu)
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
14 #include <atalk/standards.h>
21 #include <arpa/inet.h>
39 #include <atalk/afp.h>
40 #include <atalk/uam.h>
41 #include <atalk/logger.h>
43 /* Number of bits for p which we generate. Everybode out there uses 512, so we beet them */
44 #define PRIMEBITS 1024
46 /* hash a number to a 16-bit quantity */
47 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
48 (unsigned long) (a)) & 0xffff)
50 /* Some parameters need be maintained across calls */
51 static gcry_mpi_t p, Ra;
52 static gcry_mpi_t serverNonce;
53 static char *K_MD5hash = NULL;
54 static int K_hash_len;
57 /* The initialization vectors for CAST128 are fixed by Apple. */
58 static unsigned char dhx_c2siv[] = { 'L', 'W', 'a', 'l', 'l', 'a', 'c', 'e' };
59 static unsigned char dhx_s2civ[] = { 'C', 'J', 'a', 'l', 'b', 'e', 'r', 't' };
61 /* Static variables used to communicate between the conversation function
62 * and the server_login function */
63 static struct passwd *dhxpwd;
65 /*********************************************************
66 * Crypto helper func to generate p and g for use in DH.
67 * libgcrypt doesn't provide one directly.
68 * Algorithm taken from GNUTLS:gnutls_dh_primes.c
69 *********************************************************/
72 * This function will generate a new pair of prime and generator for use in
73 * the Diffie-Hellman key exchange.
74 * The bits value should be one of 768, 1024, 2048, 3072 or 4096.
78 dh_params_generate (gcry_mpi_t *ret_p, gcry_mpi_t *ret_g, unsigned int bits) {
80 int result, times = 0, qbits;
82 gcry_mpi_t g = NULL, prime = NULL;
83 gcry_mpi_t *factors = NULL;
86 /* Version check should be the very first call because it
87 makes sure that important subsystems are intialized. */
88 if (!gcry_check_version (GCRYPT_VERSION)) {
89 LOG(log_info, logtype_uams, "PAM DHX2: libgcrypt versions mismatch. Need: %s", GCRYPT_VERSION);
97 qbits = (bits / 40) + 105;
99 if (qbits & 1) /* better have an even number */
102 /* find a prime number of size bits. */
105 gcry_mpi_release (prime);
106 gcry_prime_release_factors (factors);
108 err = gcry_prime_generate (&prime, bits, qbits, &factors, NULL, NULL,
109 GCRY_STRONG_RANDOM, GCRY_PRIME_FLAG_SPECIAL_FACTOR);
111 result = AFPERR_MISC;
114 err = gcry_prime_check (prime, 0);
116 } while (err != 0 && times < 10);
119 result = AFPERR_MISC;
123 /* generate the group generator. */
124 err = gcry_prime_group_generator (&g, prime, factors, NULL);
126 result = AFPERR_MISC;
130 gcry_prime_release_factors (factors);
136 gcry_mpi_release (g);
140 gcry_mpi_release (prime);
145 gcry_prime_release_factors (factors);
146 gcry_mpi_release (g);
147 gcry_mpi_release (prime);
152 static int dhx2_setup(void *obj, char *ibuf _U_, size_t ibuflen _U_,
153 char *rbuf, size_t *rbuflen)
158 char *Ra_binary = NULL;
161 #endif /* SHADOWPW */
166 /* Initialize passwd/shadow */
168 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
169 LOG(log_info, logtype_uams, "DHX2: no shadow passwd entry for this user");
170 return AFPERR_NOTAUTH;
172 dhxpwd->pw_passwd = sp->sp_pwdp;
173 #endif /* SHADOWPW */
175 if (!dhxpwd->pw_passwd)
176 return AFPERR_NOTAUTH;
178 /* Initialize DH params */
182 Ra = gcry_mpi_new(0);
183 Ma = gcry_mpi_new(0);
185 /* Generate p and g for DH */
186 ret = dh_params_generate( &p, &g, PRIMEBITS);
188 LOG(log_info, logtype_uams, "DHX2: Couldn't generate p and g");
193 /* Generate our random number Ra. */
194 Ra_binary = calloc(1, PRIMEBITS/8);
195 if (Ra_binary == NULL) {
199 gcry_randomize(Ra_binary, PRIMEBITS/8, GCRY_STRONG_RANDOM);
200 gcry_mpi_scan(&Ra, GCRYMPI_FMT_USG, Ra_binary, PRIMEBITS/8, NULL);
204 /* Ma = g^Ra mod p. This is our "public" key */
205 gcry_mpi_powm(Ma, g, Ra, p);
207 /* ------- DH Init done ------ */
208 /* Start building reply packet */
210 /* Session ID first */
213 memcpy(rbuf, &uint16, sizeof(uint16_t));
218 gcry_mpi_print( GCRYMPI_FMT_USG, (unsigned char *)rbuf, 4, &nwritten, g);
220 memmove( rbuf+4-nwritten, rbuf, nwritten);
221 memset( rbuf, 0, 4-nwritten);
226 /* len = length of p = PRIMEBITS/8 */
227 uint16 = htons((uint16_t) PRIMEBITS/8);
228 memcpy(rbuf, &uint16, sizeof(uint16_t));
233 gcry_mpi_print( GCRYMPI_FMT_USG, (unsigned char *)rbuf, PRIMEBITS/8, NULL, p);
235 *rbuflen += PRIMEBITS/8;
238 gcry_mpi_print( GCRYMPI_FMT_USG, (unsigned char *)rbuf, PRIMEBITS/8, &nwritten, Ma);
239 if (nwritten < PRIMEBITS/8) {
240 memmove(rbuf + (PRIMEBITS/8) - nwritten, rbuf, nwritten);
241 memset(rbuf, 0, (PRIMEBITS/8) - nwritten);
244 *rbuflen += PRIMEBITS/8;
246 ret = AFPERR_AUTHCONT;
248 error: /* We exit here anyway */
249 /* We will only need p and Ra later, but mustn't forget to release it ! */
251 gcry_mpi_release(Ma);
255 /* -------------------------------- */
256 static int login(void *obj, char *username, int ulen, struct passwd **uam_pwd _U_,
257 char *ibuf, size_t ibuflen,
258 char *rbuf, size_t *rbuflen)
260 if (( dhxpwd = uam_getname(obj, username, ulen)) == NULL ) {
261 LOG(log_info, logtype_uams, "DHX2: unknown username");
262 return AFPERR_NOTAUTH;
265 LOG(log_info, logtype_uams, "DHX2 login: %s", username);
266 return dhx2_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
269 /* -------------------------------- */
270 /* dhx login: things are done in a slightly bizarre order to avoid
271 * having to clean things up if there's an error. */
272 static int passwd_login(void *obj, struct passwd **uam_pwd,
273 char *ibuf, size_t ibuflen,
274 char *rbuf, size_t *rbuflen)
281 /* grab some of the options */
282 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
283 LOG(log_info, logtype_uams, "DHX2: uam_afpserver_option didn't meet uam_option_username -- %s",
288 len = (unsigned char) *ibuf++;
290 LOG(log_info, logtype_uams, "DHX2: Signature Retieval Failure -- %s",
295 memcpy(username, ibuf, len );
297 username[ len ] = '\0';
299 if ((unsigned long) ibuf & 1) /* pad to even boundary */
302 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
305 /* ----------------------------- */
306 static int passwd_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
307 char *ibuf, size_t ibuflen,
308 char *rbuf, size_t *rbuflen)
316 /* grab some of the options */
317 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
318 LOG(log_info, logtype_uams, "DHX2: uam_afpserver_option didn't meet uam_option_username -- %s",
326 memcpy(&temp16, uname, sizeof(temp16));
329 if ( !len || len > ulen ) {
330 LOG(log_info, logtype_uams, "DHX2: Signature Retrieval Failure -- %s",
334 memcpy(username, uname +2, len );
335 username[ len ] = '\0';
337 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
340 /* -------------------------------- */
342 static int logincont1(void *obj _U_, struct passwd **uam_pwd _U_,
343 char *ibuf, size_t ibuflen,
344 char *rbuf, size_t *rbuflen)
348 gcry_mpi_t Mb, K, clientNonce;
349 unsigned char *K_bin = NULL;
350 char serverNonce_bin[16];
351 gcry_cipher_hd_t ctx;
352 gcry_error_t ctxerror;
357 Mb = gcry_mpi_new(0);
359 clientNonce = gcry_mpi_new(0);
360 serverNonce = gcry_mpi_new(0);
362 /* Packet size should be: Session ID + Ma + Encrypted client nonce */
363 if (ibuflen != 2 + PRIMEBITS/8 + 16) {
364 LOG(log_error, logtype_uams, "DHX2: Paket length not correct");
369 /* Skip session id */
372 /* Extract Mb, client's "public" key */
373 gcry_mpi_scan(&Mb, GCRYMPI_FMT_USG, ibuf, PRIMEBITS/8, NULL);
376 /* Now finally generate the Key: K = Mb^Ra mod p */
377 gcry_mpi_powm(K, Mb, Ra, p);
379 /* We need K in binary form in order to ... */
380 K_bin = calloc(1, PRIMEBITS/8);
385 gcry_mpi_print(GCRYMPI_FMT_USG, K_bin, PRIMEBITS/8, &nwritten, K);
386 if (nwritten < PRIMEBITS/8) {
387 memmove(K_bin + PRIMEBITS/8 - nwritten, K_bin, nwritten);
388 memset(K_bin, 0, PRIMEBITS/8 - nwritten);
391 /* ... generate the MD5 hash of K. K_MD5hash is what we actually use ! */
392 K_MD5hash = calloc(1, K_hash_len = gcry_md_get_algo_dlen(GCRY_MD_MD5));
393 if (K_MD5hash == NULL) {
399 gcry_md_hash_buffer(GCRY_MD_MD5, K_MD5hash, K_bin, PRIMEBITS/8);
403 /* FIXME: To support the Reconnect UAM, we need to store this key somewhere */
405 /* Set up our encryption context. */
406 ctxerror = gcry_cipher_open( &ctx, GCRY_CIPHER_CAST5, GCRY_CIPHER_MODE_CBC, 0);
407 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
412 ctxerror = gcry_cipher_setkey(ctx, K_MD5hash, K_hash_len);
413 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
417 /* Set the initialization vector for client->server transfer. */
418 ctxerror = gcry_cipher_setiv(ctx, dhx_c2siv, sizeof(dhx_c2siv));
419 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
423 /* Finally: decrypt client's md5_K(client nonce, C2SIV) inplace */
424 ctxerror = gcry_cipher_decrypt(ctx, ibuf, 16, NULL, 0);
425 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
429 /* Pull out clients nonce */
430 gcry_mpi_scan(&clientNonce, GCRYMPI_FMT_USG, ibuf, 16, NULL);
431 /* Increment nonce */
432 gcry_mpi_add_ui(clientNonce, clientNonce, 1);
434 /* Generate our nonce and remember it for Logincont2 */
435 gcry_create_nonce(serverNonce_bin, 16); /* We'll use this here */
436 gcry_mpi_scan(&serverNonce, GCRYMPI_FMT_USG, serverNonce_bin, 16, NULL); /* For use in Logincont2 */
438 /* ---- Start building reply packet ---- */
440 /* Session ID + 1 first */
441 uint16 = htons(ID+1);
442 memcpy(rbuf, &uint16, sizeof(uint16_t));
446 /* Client nonce + 1 */
447 gcry_mpi_print(GCRYMPI_FMT_USG, (unsigned char *)rbuf, PRIMEBITS/8, NULL, clientNonce);
449 memcpy(rbuf+16, serverNonce_bin, 16);
451 /* Set the initialization vector for server->client transfer. */
452 ctxerror = gcry_cipher_setiv(ctx, dhx_s2civ, sizeof(dhx_s2civ));
453 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
457 /* Encrypt md5_K(clientNonce+1, serverNonce) inplace */
458 ctxerror = gcry_cipher_encrypt(ctx, rbuf, 32, NULL, 0);
459 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
466 ret = AFPERR_AUTHCONT;
470 gcry_cipher_close(ctx);
472 gcry_mpi_release(serverNonce);
477 gcry_mpi_release(Mb);
478 gcry_mpi_release(Ra);
480 gcry_mpi_release(clientNonce);
484 static int logincont2(void *obj _U_, struct passwd **uam_pwd,
485 char *ibuf, size_t ibuflen,
486 char *rbuf _U_, size_t *rbuflen)
490 #endif /* SHADOWPW */
493 gcry_mpi_t retServerNonce;
494 gcry_cipher_hd_t ctx;
495 gcry_error_t ctxerror;
498 retServerNonce = gcry_mpi_new(0);
500 /* Packet size should be: Session ID + ServerNonce + Passwd buffer (evantually +10 extra bytes, see Apples Docs)*/
501 if ((ibuflen != 2 + 16 + 256) && (ibuflen != 2 + 16 + 256 + 10)) {
502 LOG(log_error, logtype_uams, "DHX2: Paket length not correct: %d. Should be 274 or 284.", ibuflen);
507 /* Set up our encryption context. */
508 ctxerror = gcry_cipher_open( &ctx, GCRY_CIPHER_CAST5, GCRY_CIPHER_MODE_CBC, 0);
509 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
514 ctxerror = gcry_cipher_setkey(ctx, K_MD5hash, K_hash_len);
515 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
519 /* Set the initialization vector for client->server transfer. */
520 ctxerror = gcry_cipher_setiv(ctx, dhx_c2siv, sizeof(dhx_c2siv));
521 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
526 /* Skip Session ID */
529 /* Finally: decrypt client's md5_K(serverNonce+1, passwor, C2SIV) inplace */
530 ctxerror = gcry_cipher_decrypt(ctx, ibuf, 16+256, NULL, 0);
531 if (gcry_err_code(ctxerror) != GPG_ERR_NO_ERROR) {
535 /* Pull out nonce. Should be serverNonce+1 */
536 gcry_mpi_scan(&retServerNonce, GCRYMPI_FMT_USG, ibuf, 16, NULL);
537 gcry_mpi_sub_ui(retServerNonce, retServerNonce, 1);
538 if ( gcry_mpi_cmp( serverNonce, retServerNonce) != 0) {
540 ret = AFPERR_NOTAUTH;
543 ibuf += 16; /* ibuf now point to passwd in cleartext */
545 /* ---- Start authentication --- */
546 ret = AFPERR_NOTAUTH;
548 p = crypt( ibuf, dhxpwd->pw_passwd );
549 memset(ibuf, 0, 255);
550 if ( strcmp( p, dhxpwd->pw_passwd ) == 0 ) {
556 if (( sp = getspnam( dhxpwd->pw_name )) == NULL ) {
557 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", dhxpwd->pw_name);
558 ret = AFPERR_NOTAUTH;
562 /* check for expired password */
563 if (sp && sp->sp_max != -1 && sp->sp_lstchg) {
564 time_t now = time(NULL) / (60*60*24);
565 int32_t expire_days = sp->sp_lstchg - now + sp->sp_max;
566 if ( expire_days < 0 ) {
567 LOG(log_info, logtype_uams, "password for user %s expired", dhxpwd->pw_name);
568 ret = AFPERR_PWDEXPR;
572 #endif /* SHADOWPW */
575 gcry_cipher_close(ctx);
580 gcry_mpi_release(serverNonce);
581 gcry_mpi_release(retServerNonce);
585 static int passwd_logincont(void *obj, struct passwd **uam_pwd,
586 char *ibuf, size_t ibuflen,
587 char *rbuf, size_t *rbuflen)
592 /* check for session id */
593 memcpy(&retID, ibuf, sizeof(uint16_t));
594 retID = ntohs(retID);
596 ret = logincont1(obj, uam_pwd, ibuf, ibuflen, rbuf, rbuflen);
597 else if (retID == ID+1)
598 ret = logincont2(obj, uam_pwd, ibuf,ibuflen, rbuf, rbuflen);
600 LOG(log_info, logtype_uams, "DHX2: Session ID Mismatch");
606 static int uam_setup(void *obj, const char *path)
608 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "DHX2", passwd_login,
609 passwd_logincont, NULL, passwd_login_ext) < 0)
614 static void uam_cleanup(void)
616 uam_unregister(UAM_SERVER_LOGIN, "DHX2");
620 UAM_MODULE_EXPORT struct uam_export uams_dhx2 = {
623 uam_setup, uam_cleanup
627 UAM_MODULE_EXPORT struct uam_export uams_dhx2_passwd = {
630 uam_setup, uam_cleanup
633 #endif /* UAM_DHX2 */