2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
18 * const struct berval *passwd,
19 * const struct berval *cred,
20 * const char **schemes )
22 * Returns true if user supplied credentials (cred) matches
23 * the stored password (passwd).
25 * Due to the use of the crypt(3) function
26 * this routine is NOT thread-safe.
32 #include <ac/stdlib.h>
33 #include <ac/string.h>
34 #include <ac/unistd.h>
36 #if defined(SLAPD_LMHASH)
37 # include <openssl/des.h>
38 #endif /* SLAPD_LMHASH */
43 # include <ac/crypt.h>
45 # if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
52 # ifdef HAVE_AIX_SECURITY
63 #include "lutil_md5.h"
64 #include "lutil_sha1.h"
67 static const unsigned char crypt64[] =
68 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
71 static char *salt_format = NULL;
72 static lutil_cryptfunc lutil_crypt;
73 lutil_cryptfunc *lutil_cryptptr = lutil_crypt;
78 LUTIL_PASSWD_CHK_FUNC *chk_fn;
79 LUTIL_PASSWD_HASH_FUNC *hash_fn;
83 struct pw_slist *next;
87 /* password check routines */
91 static LUTIL_PASSWD_CHK_FUNC chk_md5;
92 static LUTIL_PASSWD_CHK_FUNC chk_smd5;
93 static LUTIL_PASSWD_HASH_FUNC hash_smd5;
94 static LUTIL_PASSWD_HASH_FUNC hash_md5;
97 #ifdef LUTIL_SHA1_BYTES
98 static LUTIL_PASSWD_CHK_FUNC chk_ssha1;
99 static LUTIL_PASSWD_CHK_FUNC chk_sha1;
100 static LUTIL_PASSWD_HASH_FUNC hash_sha1;
101 static LUTIL_PASSWD_HASH_FUNC hash_ssha1;
105 static LUTIL_PASSWD_CHK_FUNC chk_lanman;
106 static LUTIL_PASSWD_HASH_FUNC hash_lanman;
110 static LUTIL_PASSWD_CHK_FUNC chk_crypt;
111 static LUTIL_PASSWD_HASH_FUNC hash_crypt;
113 #if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
114 static LUTIL_PASSWD_CHK_FUNC chk_unix;
118 /* password hash routines */
120 #ifdef SLAPD_CLEARTEXT
121 static LUTIL_PASSWD_HASH_FUNC hash_clear;
124 static struct pw_slist *pw_schemes;
125 static int pw_inited;
127 static const struct pw_scheme pw_schemes_default[] =
129 #ifdef LUTIL_SHA1_BYTES
130 { BER_BVC("{SSHA}"), chk_ssha1, hash_ssha1 },
131 { BER_BVC("{SHA}"), chk_sha1, hash_sha1 },
134 { BER_BVC("{SMD5}"), chk_smd5, hash_smd5 },
135 { BER_BVC("{MD5}"), chk_md5, hash_md5 },
138 { BER_BVC("{LANMAN}"), chk_lanman, hash_lanman },
139 #endif /* SLAPD_LMHASH */
142 { BER_BVC("{CRYPT}"), chk_crypt, hash_crypt },
143 # if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
144 { BER_BVC("{UNIX}"), chk_unix, NULL },
148 #ifdef SLAPD_CLEARTEXT
150 { {0, "{CLEARTEXT}"}, NULL, hash_clear },
153 { BER_BVNULL, NULL, NULL }
156 int lutil_passwd_add(
157 struct berval *scheme,
158 LUTIL_PASSWD_CHK_FUNC *chk,
159 LUTIL_PASSWD_HASH_FUNC *hash )
161 struct pw_slist *ptr;
163 if (!pw_inited) lutil_passwd_init();
165 ptr = ber_memalloc( sizeof( struct pw_slist ));
167 ptr->next = pw_schemes;
168 ptr->s.name = *scheme;
170 ptr->s.hash_fn = hash;
175 void lutil_passwd_init()
181 for( s=(struct pw_scheme *)pw_schemes_default; s->name.bv_val; s++) {
182 if ( lutil_passwd_add( &s->name, s->chk_fn, s->hash_fn ) ) break;
186 void lutil_passwd_destroy()
188 struct pw_slist *ptr, *next;
190 for( ptr=pw_schemes; ptr; ptr=next ) {
196 static const struct pw_scheme *get_scheme(
199 struct pw_slist *pws;
202 if (!pw_inited) lutil_passwd_init();
204 bv.bv_val = strchr( scheme, '}' );
208 bv.bv_len = bv.bv_val - scheme + 1;
209 bv.bv_val = (char *) scheme;
211 for( pws=pw_schemes; pws; pws=pws->next ) {
212 if ( ber_bvstrcasecmp(&bv, &pws->s.name ) == 0 ) {
220 int lutil_passwd_scheme(
223 if( scheme == NULL ) {
227 return get_scheme(scheme) != NULL;
231 static int is_allowed_scheme(
233 const char** schemes )
237 if( schemes == NULL ) return 1;
239 for( i=0; schemes[i] != NULL; i++ ) {
240 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
247 static struct berval *passwd_scheme(
248 const struct pw_scheme *scheme,
249 const struct berval * passwd,
251 const char** allowed )
253 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
257 if( passwd->bv_len >= scheme->name.bv_len ) {
258 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
259 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
260 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
270 * Return 0 if creds are good.
274 const struct berval *passwd, /* stored passwd */
275 const struct berval *cred, /* user cred */
276 const char **schemes,
279 struct pw_slist *pws;
281 if ( text ) *text = NULL;
283 if (cred == NULL || cred->bv_len == 0 ||
284 passwd == NULL || passwd->bv_len == 0 )
289 if (!pw_inited) lutil_passwd_init();
291 for( pws=pw_schemes; pws; pws=pws->next ) {
292 if( pws->s.chk_fn ) {
294 struct berval *p = passwd_scheme( &(pws->s),
295 passwd, &x, schemes );
298 return (pws->s.chk_fn)( &(pws->s.name), p, cred, text );
303 #ifdef SLAPD_CLEARTEXT
304 /* Do we think there is a scheme specifier here that we
305 * didn't recognize? Assume a scheme name is at least 1 character.
307 if (( passwd->bv_val[0] == '{' ) &&
308 ( strchr( passwd->bv_val, '}' ) > passwd->bv_val+1 ))
310 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
311 return ( passwd->bv_len == cred->bv_len ) ?
312 memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
319 int lutil_passwd_generate( struct berval *pw, ber_len_t len )
322 if( len < 1 ) return -1;
325 pw->bv_val = ber_memalloc( len + 1 );
327 if( pw->bv_val == NULL ) {
331 if( lutil_entropy( (unsigned char *) pw->bv_val, pw->bv_len) < 0 ) {
335 for( len = 0; len < pw->bv_len; len++ ) {
336 pw->bv_val[len] = crypt64[
337 pw->bv_val[len] % (sizeof(crypt64)-1) ];
340 pw->bv_val[len] = '\0';
345 int lutil_passwd_hash(
346 const struct berval * passwd,
351 const struct pw_scheme *sc = get_scheme( method );
357 if( text ) *text = "scheme not recognized";
361 if( ! sc->hash_fn ) {
362 if( text ) *text = "scheme provided no hash function";
366 if( text ) *text = NULL;
368 return (sc->hash_fn)( &sc->name, passwd, hash, text );
371 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
372 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
373 static int pw_string(
374 const struct berval *sc,
375 struct berval *passwd )
379 pw.bv_len = sc->bv_len + passwd->bv_len;
380 pw.bv_val = ber_memalloc( pw.bv_len + 1 );
382 if( pw.bv_val == NULL ) {
383 return LUTIL_PASSWD_ERR;
386 AC_MEMCPY( pw.bv_val, sc->bv_val, sc->bv_len );
387 AC_MEMCPY( &pw.bv_val[sc->bv_len], passwd->bv_val, passwd->bv_len );
389 pw.bv_val[pw.bv_len] = '\0';
392 return LUTIL_PASSWD_OK;
394 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
396 static int pw_string64(
397 const struct berval *sc,
398 const struct berval *hash,
400 const struct berval *salt )
403 struct berval string;
407 /* need to base64 combined string */
408 string.bv_len = hash->bv_len + salt->bv_len;
409 string.bv_val = ber_memalloc( string.bv_len + 1 );
411 if( string.bv_val == NULL ) {
412 return LUTIL_PASSWD_ERR;
415 AC_MEMCPY( string.bv_val, hash->bv_val,
417 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
419 string.bv_val[string.bv_len] = '\0';
425 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
426 b64->bv_len = b64len + sc->bv_len;
427 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
429 if( b64->bv_val == NULL ) {
430 if( salt ) ber_memfree( string.bv_val );
431 return LUTIL_PASSWD_ERR;
434 AC_MEMCPY(b64->bv_val, sc->bv_val, sc->bv_len);
437 (unsigned char *) string.bv_val, string.bv_len,
438 &b64->bv_val[sc->bv_len], b64len );
440 if( salt ) ber_memfree( string.bv_val );
443 return LUTIL_PASSWD_ERR;
446 /* recompute length */
447 b64->bv_len = sc->bv_len + rc;
448 assert( strlen(b64->bv_val) == b64->bv_len );
449 return LUTIL_PASSWD_OK;
452 /* PASSWORD CHECK ROUTINES */
454 #ifdef LUTIL_SHA1_BYTES
455 static int chk_ssha1(
456 const struct berval *sc,
457 const struct berval * passwd,
458 const struct berval * cred,
461 lutil_SHA1_CTX SHA1context;
462 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
464 unsigned char *orig_pass = NULL;
466 /* safety check -- must have some salt */
467 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(SHA1digest)) {
468 return LUTIL_PASSWD_ERR;
471 /* decode base64 password */
472 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
473 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
475 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
477 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
479 /* safety check -- must have some salt */
480 if (rc <= (int)(sizeof(SHA1digest))) {
481 ber_memfree(orig_pass);
482 return LUTIL_PASSWD_ERR;
485 /* hash credentials with salt */
486 lutil_SHA1Init(&SHA1context);
487 lutil_SHA1Update(&SHA1context,
488 (const unsigned char *) cred->bv_val, cred->bv_len);
489 lutil_SHA1Update(&SHA1context,
490 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
491 rc - sizeof(SHA1digest));
492 lutil_SHA1Final(SHA1digest, &SHA1context);
495 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
496 ber_memfree(orig_pass);
497 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
501 const struct berval *sc,
502 const struct berval * passwd,
503 const struct berval * cred,
506 lutil_SHA1_CTX SHA1context;
507 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
509 unsigned char *orig_pass = NULL;
512 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(SHA1digest)) {
513 return LUTIL_PASSWD_ERR;
516 /* base64 un-encode password */
517 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
518 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
520 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
522 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
524 if( rc != sizeof(SHA1digest) ) {
525 ber_memfree(orig_pass);
526 return LUTIL_PASSWD_ERR;
529 /* hash credentials with salt */
530 lutil_SHA1Init(&SHA1context);
531 lutil_SHA1Update(&SHA1context,
532 (const unsigned char *) cred->bv_val, cred->bv_len);
533 lutil_SHA1Final(SHA1digest, &SHA1context);
536 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
537 ber_memfree(orig_pass);
538 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
543 const struct berval *sc,
544 const struct berval * passwd,
545 const struct berval * cred,
548 lutil_MD5_CTX MD5context;
549 unsigned char MD5digest[LUTIL_MD5_BYTES];
551 unsigned char *orig_pass = NULL;
554 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(MD5digest)) {
555 return LUTIL_PASSWD_ERR;
558 /* base64 un-encode password */
559 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
560 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
562 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
564 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
566 if (rc <= (int)(sizeof(MD5digest))) {
567 ber_memfree(orig_pass);
568 return LUTIL_PASSWD_ERR;
571 /* hash credentials with salt */
572 lutil_MD5Init(&MD5context);
573 lutil_MD5Update(&MD5context,
574 (const unsigned char *) cred->bv_val,
576 lutil_MD5Update(&MD5context,
577 &orig_pass[sizeof(MD5digest)],
578 rc - sizeof(MD5digest));
579 lutil_MD5Final(MD5digest, &MD5context);
582 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
583 ber_memfree(orig_pass);
584 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
588 const struct berval *sc,
589 const struct berval * passwd,
590 const struct berval * cred,
593 lutil_MD5_CTX MD5context;
594 unsigned char MD5digest[LUTIL_MD5_BYTES];
596 unsigned char *orig_pass = NULL;
599 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(MD5digest)) {
600 return LUTIL_PASSWD_ERR;
603 /* base64 un-encode password */
604 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
605 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
607 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
609 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
610 if ( rc != sizeof(MD5digest) ) {
611 ber_memfree(orig_pass);
612 return LUTIL_PASSWD_ERR;
615 /* hash credentials with salt */
616 lutil_MD5Init(&MD5context);
617 lutil_MD5Update(&MD5context,
618 (const unsigned char *) cred->bv_val,
620 lutil_MD5Final(MD5digest, &MD5context);
623 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
624 ber_memfree(orig_pass);
625 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
629 /* pseudocode from RFC2433
630 * A.2 LmPasswordHash()
633 * IN 0-to-14-oem-char Password,
634 * OUT 16-octet PasswordHash )
636 * Set UcasePassword to the uppercased Password
637 * Zero pad UcasePassword to 14 characters
639 * DesHash( 1st 7-octets of UcasePassword,
640 * giving 1st 8-octets of PasswordHash )
642 * DesHash( 2nd 7-octets of UcasePassword,
643 * giving 2nd 8-octets of PasswordHash )
651 * OUT 8-octet Cypher )
654 * * Make Cypher an irreversibly encrypted form of Clear by
655 * * encrypting known text using Clear as the secret key.
656 * * The known text consists of the string
661 * Set StdText to "KGS!@#$%"
662 * DesEncrypt( StdText, Clear, giving Cypher )
671 * OUT 8-octet Cypher )
674 * * Use the DES encryption algorithm [4] in ECB mode [9]
675 * * to encrypt Clear into Cypher such that Cypher can
676 * * only be decrypted back to Clear by providing Key.
677 * * Note that the DES algorithm takes as input a 64-bit
678 * * stream where the 8th, 16th, 24th, etc. bits are
679 * * parity bits ignored by the encrypting algorithm.
680 * * Unless you write your own DES to accept 56-bit input
681 * * without parity, you will need to insert the parity bits
687 static void lmPasswd_to_key(
688 const char *lmPasswd,
691 const unsigned char *lpw = (const unsigned char *) lmPasswd;
692 unsigned char *k = (unsigned char *) key;
694 /* make room for parity bits */
696 k[1] = ((lpw[0] & 0x01) << 7) | (lpw[1] >> 1);
697 k[2] = ((lpw[1] & 0x03) << 6) | (lpw[2] >> 2);
698 k[3] = ((lpw[2] & 0x07) << 5) | (lpw[3] >> 3);
699 k[4] = ((lpw[3] & 0x0F) << 4) | (lpw[4] >> 4);
700 k[5] = ((lpw[4] & 0x1F) << 3) | (lpw[5] >> 5);
701 k[6] = ((lpw[5] & 0x3F) << 2) | (lpw[6] >> 6);
702 k[7] = ((lpw[6] & 0x7F) << 1);
704 des_set_odd_parity( key );
707 static int chk_lanman(
708 const struct berval *scheme,
709 const struct berval *passwd,
710 const struct berval *cred,
714 char UcasePassword[15];
716 des_key_schedule schedule;
717 des_cblock StdText = "KGS!@#$%";
718 des_cblock PasswordHash1, PasswordHash2;
719 char PasswordHash[33], storedPasswordHash[33];
721 for( i=0; i<cred->bv_len; i++) {
722 if(cred->bv_val[i] == '\0') {
723 return LUTIL_PASSWD_ERR; /* NUL character in password */
727 if( cred->bv_val[i] != '\0' ) {
728 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
731 strncpy( UcasePassword, cred->bv_val, 14 );
732 UcasePassword[14] = '\0';
733 ldap_pvt_str2upper( UcasePassword );
735 lmPasswd_to_key( UcasePassword, &key );
736 des_set_key_unchecked( &key, schedule );
737 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
739 lmPasswd_to_key( &UcasePassword[7], &key );
740 des_set_key_unchecked( &key, schedule );
741 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
743 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
744 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
745 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
746 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
747 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
749 /* as a precaution convert stored password hash to lower case */
750 strncpy( storedPasswordHash, passwd->bv_val, 32 );
751 storedPasswordHash[32] = '\0';
752 ldap_pvt_str2lower( storedPasswordHash );
754 return memcmp( PasswordHash, storedPasswordHash, 32) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
756 #endif /* SLAPD_LMHASH */
759 static int lutil_crypt(
764 char *cr = crypt( key, salt );
767 if( cr == NULL || cr[0] == '\0' ) {
768 /* salt must have been invalid */
769 rc = LUTIL_PASSWD_ERR;
772 *hash = ber_strdup( cr );
773 rc = LUTIL_PASSWD_OK;
775 rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
781 static int chk_crypt(
782 const struct berval *sc,
783 const struct berval * passwd,
784 const struct berval * cred,
789 for( i=0; i<cred->bv_len; i++) {
790 if(cred->bv_val[i] == '\0') {
791 return LUTIL_PASSWD_ERR; /* NUL character in password */
795 if( cred->bv_val[i] != '\0' ) {
796 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
799 if( passwd->bv_len < 2 ) {
800 return LUTIL_PASSWD_ERR; /* passwd must be at least two characters long */
803 for( i=0; i<passwd->bv_len; i++) {
804 if(passwd->bv_val[i] == '\0') {
805 return LUTIL_PASSWD_ERR; /* NUL character in password */
809 if( passwd->bv_val[i] != '\0' ) {
810 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
813 return lutil_cryptptr( cred->bv_val, passwd->bv_val, NULL );
816 # if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
818 const struct berval *sc,
819 const struct berval * passwd,
820 const struct berval * cred,
826 for( i=0; i<cred->bv_len; i++) {
827 if(cred->bv_val[i] == '\0') {
828 return LUTIL_PASSWD_ERR; /* NUL character in password */
831 if( cred->bv_val[i] != '\0' ) {
832 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
835 for( i=0; i<passwd->bv_len; i++) {
836 if(passwd->bv_val[i] == '\0') {
837 return LUTIL_PASSWD_ERR; /* NUL character in password */
841 if( passwd->bv_val[i] != '\0' ) {
842 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
846 struct passwd *pwd = getpwnam(passwd->bv_val);
849 return LUTIL_PASSWD_ERR; /* not found */
854 # ifdef HAVE_GETSPNAM
856 struct spwd *spwd = getspnam(passwd->bv_val);
863 # ifdef HAVE_AIX_SECURITY
865 struct userpw *upw = getuserpw(passwd->bv_val);
868 pw = upw->upw_passwd;
873 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
874 /* password must must be at least two characters long */
875 return LUTIL_PASSWD_ERR;
878 return lutil_cryptptr( cred->bv_val, pw, NULL );
883 /* PASSWORD GENERATION ROUTINES */
885 #ifdef LUTIL_SHA1_BYTES
886 static int hash_ssha1(
887 const struct berval *scheme,
888 const struct berval *passwd,
892 lutil_SHA1_CTX SHA1context;
893 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
894 char saltdata[SALT_SIZE];
895 struct berval digest;
898 digest.bv_val = (char *) SHA1digest;
899 digest.bv_len = sizeof(SHA1digest);
900 salt.bv_val = saltdata;
901 salt.bv_len = sizeof(saltdata);
903 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
904 return LUTIL_PASSWD_ERR;
907 lutil_SHA1Init( &SHA1context );
908 lutil_SHA1Update( &SHA1context,
909 (const unsigned char *)passwd->bv_val, passwd->bv_len );
910 lutil_SHA1Update( &SHA1context,
911 (const unsigned char *)salt.bv_val, salt.bv_len );
912 lutil_SHA1Final( SHA1digest, &SHA1context );
914 return pw_string64( scheme, &digest, hash, &salt);
917 static int hash_sha1(
918 const struct berval *scheme,
919 const struct berval *passwd,
923 lutil_SHA1_CTX SHA1context;
924 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
925 struct berval digest;
926 digest.bv_val = (char *) SHA1digest;
927 digest.bv_len = sizeof(SHA1digest);
929 lutil_SHA1Init( &SHA1context );
930 lutil_SHA1Update( &SHA1context,
931 (const unsigned char *)passwd->bv_val, passwd->bv_len );
932 lutil_SHA1Final( SHA1digest, &SHA1context );
934 return pw_string64( scheme, &digest, hash, NULL);
938 static int hash_smd5(
939 const struct berval *scheme,
940 const struct berval *passwd,
944 lutil_MD5_CTX MD5context;
945 unsigned char MD5digest[LUTIL_MD5_BYTES];
946 char saltdata[SALT_SIZE];
947 struct berval digest;
950 digest.bv_val = (char *) MD5digest;
951 digest.bv_len = sizeof(MD5digest);
952 salt.bv_val = saltdata;
953 salt.bv_len = sizeof(saltdata);
955 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
956 return LUTIL_PASSWD_ERR;
959 lutil_MD5Init( &MD5context );
960 lutil_MD5Update( &MD5context,
961 (const unsigned char *) passwd->bv_val, passwd->bv_len );
962 lutil_MD5Update( &MD5context,
963 (const unsigned char *) salt.bv_val, salt.bv_len );
964 lutil_MD5Final( MD5digest, &MD5context );
966 return pw_string64( scheme, &digest, hash, &salt );
970 const struct berval *scheme,
971 const struct berval *passwd,
975 lutil_MD5_CTX MD5context;
976 unsigned char MD5digest[LUTIL_MD5_BYTES];
978 struct berval digest;
980 digest.bv_val = (char *) MD5digest;
981 digest.bv_len = sizeof(MD5digest);
983 lutil_MD5Init( &MD5context );
984 lutil_MD5Update( &MD5context,
985 (const unsigned char *) passwd->bv_val, passwd->bv_len );
986 lutil_MD5Final( MD5digest, &MD5context );
988 return pw_string64( scheme, &digest, hash, NULL );
993 static int hash_lanman(
994 const struct berval *scheme,
995 const struct berval *passwd,
1001 char UcasePassword[15];
1003 des_key_schedule schedule;
1004 des_cblock StdText = "KGS!@#$%";
1005 des_cblock PasswordHash1, PasswordHash2;
1006 char PasswordHash[33];
1008 for( i=0; i<passwd->bv_len; i++) {
1009 if(passwd->bv_val[i] == '\0') {
1010 return LUTIL_PASSWD_ERR; /* NUL character in password */
1014 if( passwd->bv_val[i] != '\0' ) {
1015 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
1018 strncpy( UcasePassword, passwd->bv_val, 14 );
1019 UcasePassword[14] = '\0';
1020 ldap_pvt_str2upper( UcasePassword );
1022 lmPasswd_to_key( UcasePassword, &key );
1023 des_set_key_unchecked( &key, schedule );
1024 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
1026 lmPasswd_to_key( &UcasePassword[7], &key );
1027 des_set_key_unchecked( &key, schedule );
1028 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
1030 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1031 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
1032 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
1033 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
1034 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
1036 hash->bv_val = PasswordHash;
1039 return pw_string( scheme, hash );
1041 #endif /* SLAPD_LMHASH */
1044 static int hash_crypt(
1045 const struct berval *scheme,
1046 const struct berval *passwd,
1047 struct berval *hash,
1050 unsigned char salt[32]; /* salt suitable for most anything */
1055 for( i=0; i<passwd->bv_len; i++) {
1056 if(passwd->bv_val[i] == '\0') {
1057 return LUTIL_PASSWD_ERR; /* NUL character in password */
1061 if( passwd->bv_val[i] != '\0' ) {
1062 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
1065 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1066 return LUTIL_PASSWD_ERR;
1069 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1070 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1072 salt[sizeof( salt ) - 1 ] = '\0';
1074 if( salt_format != NULL ) {
1075 /* copy the salt we made into entropy before snprintfing
1076 it back into the salt */
1077 char entropy[sizeof(salt)];
1078 strcpy( entropy, (char *) salt );
1079 snprintf( (char *) salt, sizeof(entropy), salt_format, entropy );
1082 rc = lutil_cryptptr( passwd->bv_val, (char *) salt, &hash->bv_val );
1083 if ( rc != LUTIL_PASSWD_OK ) return rc;
1085 if( hash->bv_val == NULL ) return -1;
1087 hash->bv_len = strlen( hash->bv_val );
1089 save = hash->bv_val;
1091 if( hash->bv_len == 0 ) {
1092 rc = LUTIL_PASSWD_ERR;
1094 rc = pw_string( scheme, hash );
1096 ber_memfree( save );
1101 int lutil_salt_format(const char *format)
1104 free( salt_format );
1106 salt_format = format != NULL ? ber_strdup( format ) : NULL;
1112 #ifdef SLAPD_CLEARTEXT
1113 static int hash_clear(
1114 const struct berval *scheme,
1115 const struct berval *passwd,
1116 struct berval *hash,
1119 ber_dupbv( hash, (struct berval *)passwd );
1120 return LUTIL_PASSWD_OK;