2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 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>
37 # ifdef HAVE_SASL_SASL_H
38 # include <sasl/sasl.h>
44 #if defined(SLAPD_LMHASH)
45 # include <openssl/des.h>
46 #endif /* SLAPD_LMHASH */
51 # include <ac/crypt.h>
53 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
60 # ifdef HAVE_AIX_SECURITY
71 #include "lutil_md5.h"
72 #include "lutil_sha1.h"
75 static const unsigned char crypt64[] =
76 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
79 static char *salt_format = NULL;
84 LUTIL_PASSWD_CHK_FUNC *chk_fn;
85 LUTIL_PASSWD_HASH_FUNC *hash_fn;
89 struct pw_slist *next;
93 /* password check routines */
97 static LUTIL_PASSWD_CHK_FUNC chk_md5;
98 static LUTIL_PASSWD_CHK_FUNC chk_smd5;
99 static LUTIL_PASSWD_HASH_FUNC hash_smd5;
100 static LUTIL_PASSWD_HASH_FUNC hash_md5;
103 #ifdef LUTIL_SHA1_BYTES
104 static LUTIL_PASSWD_CHK_FUNC chk_ssha1;
105 static LUTIL_PASSWD_CHK_FUNC chk_sha1;
106 static LUTIL_PASSWD_HASH_FUNC hash_sha1;
107 static LUTIL_PASSWD_HASH_FUNC hash_ssha1;
111 static LUTIL_PASSWD_CHK_FUNC chk_lanman;
112 static LUTIL_PASSWD_HASH_FUNC hash_lanman;
116 static LUTIL_PASSWD_CHK_FUNC chk_sasl;
120 static LUTIL_PASSWD_CHK_FUNC chk_crypt;
121 static LUTIL_PASSWD_HASH_FUNC hash_crypt;
123 #if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
124 static LUTIL_PASSWD_CHK_FUNC chk_unix;
128 /* password hash routines */
130 #ifdef SLAPD_CLEARTEXT
131 static LUTIL_PASSWD_HASH_FUNC hash_clear;
134 static struct pw_slist *pw_schemes;
136 static const struct pw_scheme pw_schemes_default[] =
138 #ifdef LUTIL_SHA1_BYTES
139 { BER_BVC("{SSHA}"), chk_ssha1, hash_ssha1 },
140 { BER_BVC("{SHA}"), chk_sha1, hash_sha1 },
143 { BER_BVC("{SMD5}"), chk_smd5, hash_smd5 },
144 { BER_BVC("{MD5}"), chk_md5, hash_md5 },
147 { BER_BVC("{LANMAN}"), chk_lanman, hash_lanman },
148 #endif /* SLAPD_LMHASH */
151 { BER_BVC("{SASL}"), chk_sasl, NULL },
155 { BER_BVC("{CRYPT}"), chk_crypt, hash_crypt },
156 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
157 { BER_BVC("{UNIX}"), chk_unix, NULL },
161 #ifdef SLAPD_CLEARTEXT
163 { {0, "{CLEARTEXT}"}, NULL, hash_clear },
166 { BER_BVNULL, NULL, NULL }
169 int lutil_passwd_add(
170 struct berval *scheme,
171 LUTIL_PASSWD_CHK_FUNC *chk,
172 LUTIL_PASSWD_HASH_FUNC *hash )
174 struct pw_slist *ptr;
176 ptr = ber_memalloc( sizeof( struct pw_slist ));
178 ptr->next = pw_schemes;
179 ptr->s.name = *scheme;
181 ptr->s.hash_fn = hash;
186 void lutil_passwd_init()
188 struct pw_slist *ptr;
191 for( s=(struct pw_scheme *)pw_schemes_default; s->name.bv_val; s++) {
192 if ( lutil_passwd_add( &s->name, s->chk_fn, s->hash_fn )) break;
196 void lutil_passwd_destroy()
198 struct pw_slist *ptr, *next;
200 for( ptr=pw_schemes; ptr; ptr=next ) {
206 static const struct pw_scheme *get_scheme(
209 struct pw_slist *pws;
211 if (!pw_schemes) lutil_passwd_init();
213 for( pws=pw_schemes; pws; pws=pws->next ) {
214 if( strcasecmp(scheme, pws->s.name.bv_val ) == 0 ) {
222 int lutil_passwd_scheme(
225 if( scheme == NULL ) {
229 return get_scheme(scheme) != NULL;
233 static int is_allowed_scheme(
235 const char** schemes )
239 if( schemes == NULL ) return 1;
241 for( i=0; schemes[i] != NULL; i++ ) {
242 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
249 static struct berval *passwd_scheme(
250 const struct pw_scheme *scheme,
251 const struct berval * passwd,
253 const char** allowed )
255 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
259 if( passwd->bv_len >= scheme->name.bv_len ) {
260 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
261 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
262 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
272 * Return 0 if creds are good.
276 const struct berval *passwd, /* stored passwd */
277 const struct berval *cred, /* user cred */
278 const char **schemes,
281 struct pw_slist *pws;
283 if ( text ) *text = NULL;
285 if (cred == NULL || cred->bv_len == 0 ||
286 passwd == NULL || passwd->bv_len == 0 )
291 if (!pw_schemes) lutil_passwd_init();
293 for( pws=pw_schemes; pws; pws=pws->next ) {
294 if( pws->s.chk_fn ) {
296 struct berval *p = passwd_scheme( &(pws->s),
297 passwd, &x, schemes );
300 return (pws->s.chk_fn)( &(pws->s.name), p, cred, text );
305 #ifdef SLAPD_CLEARTEXT
306 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
307 return (( passwd->bv_len == cred->bv_len ) &&
308 ( passwd->bv_val[0] != '{' /*'}'*/ ))
309 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
316 struct berval * lutil_passwd_generate( ber_len_t len )
320 if( len < 1 ) return NULL;
322 pw = ber_memalloc( sizeof( struct berval ) );
323 if( pw == NULL ) return NULL;
326 pw->bv_val = ber_memalloc( len + 1 );
328 if( pw->bv_val == NULL ) {
333 if( lutil_entropy( (unsigned char *) pw->bv_val, pw->bv_len) < 0 ) {
338 for( len = 0; len < pw->bv_len; len++ ) {
339 pw->bv_val[len] = crypt64[
340 pw->bv_val[len] % (sizeof(crypt64)-1) ];
343 pw->bv_val[len] = '\0';
348 struct berval * lutil_passwd_hash(
349 const struct berval * passwd,
353 const struct pw_scheme *sc = get_scheme( method );
356 if( text ) *text = "scheme not recognized";
360 if( ! sc->hash_fn ) {
361 if( text ) *text = "scheme provided no hash function";
365 if( text ) *text = NULL;
367 return (sc->hash_fn)( &sc->name, passwd, text );
370 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
371 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
372 static struct berval * pw_string(
373 const struct berval *sc,
374 const struct berval *passwd )
376 struct berval *pw = ber_memalloc( sizeof( struct berval ) );
377 if( pw == NULL ) return NULL;
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 ) {
387 AC_MEMCPY( pw->bv_val, sc->bv_val, sc->bv_len );
388 AC_MEMCPY( &pw->bv_val[sc->bv_len], passwd->bv_val, passwd->bv_len );
390 pw->bv_val[pw->bv_len] = '\0';
393 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
395 static struct berval * pw_string64(
396 const struct berval *sc,
397 const struct berval *hash,
398 const struct berval *salt )
401 struct berval string;
402 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
405 if( b64 == NULL ) return NULL;
408 /* need to base64 combined string */
409 string.bv_len = hash->bv_len + salt->bv_len;
410 string.bv_val = ber_memalloc( string.bv_len + 1 );
412 if( string.bv_val == NULL ) {
417 AC_MEMCPY( string.bv_val, hash->bv_val,
419 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
421 string.bv_val[string.bv_len] = '\0';
427 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
428 b64->bv_len = b64len + sc->bv_len;
429 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
431 if( b64->bv_val == NULL ) {
432 if( salt ) ber_memfree( string.bv_val );
437 AC_MEMCPY(b64->bv_val, sc->bv_val, sc->bv_len);
440 (unsigned char *) string.bv_val, string.bv_len,
441 &b64->bv_val[sc->bv_len], b64len );
443 if( salt ) ber_memfree( string.bv_val );
450 /* recompute length */
451 b64->bv_len = sc->bv_len + rc;
452 assert( strlen(b64->bv_val) == b64->bv_len );
456 /* PASSWORD CHECK ROUTINES */
458 #ifdef LUTIL_SHA1_BYTES
459 static int chk_ssha1(
460 const struct berval *sc,
461 const struct berval * passwd,
462 const struct berval * cred,
465 lutil_SHA1_CTX SHA1context;
466 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
468 unsigned char *orig_pass = NULL;
471 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <
472 sizeof(SHA1digest)+SALT_SIZE) {
476 /* decode base64 password */
477 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
478 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
480 if( orig_pass == NULL ) return -1;
482 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
484 if (rc < (int)(sizeof(SHA1digest)+SALT_SIZE)) {
485 ber_memfree(orig_pass);
489 /* hash credentials with salt */
490 lutil_SHA1Init(&SHA1context);
491 lutil_SHA1Update(&SHA1context,
492 (const unsigned char *) cred->bv_val, cred->bv_len);
493 lutil_SHA1Update(&SHA1context,
494 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
495 rc - sizeof(SHA1digest));
496 lutil_SHA1Final(SHA1digest, &SHA1context);
499 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
500 ber_memfree(orig_pass);
505 const struct berval *sc,
506 const struct berval * passwd,
507 const struct berval * cred,
510 lutil_SHA1_CTX SHA1context;
511 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
513 unsigned char *orig_pass = NULL;
516 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(SHA1digest)) {
520 /* base64 un-encode password */
521 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
522 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
524 if( orig_pass == NULL ) return -1;
526 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
528 if( rc != sizeof(SHA1digest) ) {
529 ber_memfree(orig_pass);
533 /* hash credentials with salt */
534 lutil_SHA1Init(&SHA1context);
535 lutil_SHA1Update(&SHA1context,
536 (const unsigned char *) cred->bv_val, cred->bv_len);
537 lutil_SHA1Final(SHA1digest, &SHA1context);
540 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
541 ber_memfree(orig_pass);
547 const struct berval *sc,
548 const struct berval * passwd,
549 const struct berval * cred,
552 lutil_MD5_CTX MD5context;
553 unsigned char MD5digest[LUTIL_MD5_BYTES];
555 unsigned char *orig_pass = NULL;
558 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <
559 sizeof(MD5digest)+SALT_SIZE) {
563 /* base64 un-encode password */
564 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
565 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
567 if( orig_pass == NULL ) return -1;
569 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
571 if (rc < (int)(sizeof(MD5digest)+SALT_SIZE)) {
572 ber_memfree(orig_pass);
576 /* hash credentials with salt */
577 lutil_MD5Init(&MD5context);
578 lutil_MD5Update(&MD5context,
579 (const unsigned char *) cred->bv_val,
581 lutil_MD5Update(&MD5context,
582 &orig_pass[sizeof(MD5digest)],
583 rc - sizeof(MD5digest));
584 lutil_MD5Final(MD5digest, &MD5context);
587 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
588 ber_memfree(orig_pass);
593 const struct berval *sc,
594 const struct berval * passwd,
595 const struct berval * cred,
598 lutil_MD5_CTX MD5context;
599 unsigned char MD5digest[LUTIL_MD5_BYTES];
601 unsigned char *orig_pass = NULL;
604 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(MD5digest)) {
608 /* base64 un-encode password */
609 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
610 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
612 if( orig_pass == NULL ) return -1;
614 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
615 if ( rc != sizeof(MD5digest) ) {
616 ber_memfree(orig_pass);
620 /* hash credentials with salt */
621 lutil_MD5Init(&MD5context);
622 lutil_MD5Update(&MD5context,
623 (const unsigned char *) cred->bv_val,
625 lutil_MD5Final(MD5digest, &MD5context);
628 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
629 ber_memfree(orig_pass);
634 /* pseudocode from RFC2433
635 * A.2 LmPasswordHash()
638 * IN 0-to-14-oem-char Password,
639 * OUT 16-octet PasswordHash )
641 * Set UcasePassword to the uppercased Password
642 * Zero pad UcasePassword to 14 characters
644 * DesHash( 1st 7-octets of UcasePassword,
645 * giving 1st 8-octets of PasswordHash )
647 * DesHash( 2nd 7-octets of UcasePassword,
648 * giving 2nd 8-octets of PasswordHash )
656 * OUT 8-octet Cypher )
659 * * Make Cypher an irreversibly encrypted form of Clear by
660 * * encrypting known text using Clear as the secret key.
661 * * The known text consists of the string
666 * Set StdText to "KGS!@#$%"
667 * DesEncrypt( StdText, Clear, giving Cypher )
676 * OUT 8-octet Cypher )
679 * * Use the DES encryption algorithm [4] in ECB mode [9]
680 * * to encrypt Clear into Cypher such that Cypher can
681 * * only be decrypted back to Clear by providing Key.
682 * * Note that the DES algorithm takes as input a 64-bit
683 * * stream where the 8th, 16th, 24th, etc. bits are
684 * * parity bits ignored by the encrypting algorithm.
685 * * Unless you write your own DES to accept 56-bit input
686 * * without parity, you will need to insert the parity bits
692 static void lmPasswd_to_key(
693 const unsigned char *lmPasswd,
696 /* make room for parity bits */
697 ((char *)key)[0] = lmPasswd[0];
698 ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
699 ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
700 ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
701 ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
702 ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
703 ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
704 ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
706 des_set_odd_parity( key );
709 static int chk_lanman(
710 const struct berval *scheme,
711 const struct berval *passwd,
712 const struct berval *cred,
716 char UcasePassword[15];
718 des_key_schedule schedule;
719 des_cblock StdText = "KGS!@#$%";
720 des_cblock PasswordHash1, PasswordHash2;
721 char PasswordHash[33], storedPasswordHash[33];
723 for( i=0; i<cred->bv_len; i++) {
724 if(cred->bv_val[i] == '\0') {
725 return -1; /* NUL character in password */
729 if( cred->bv_val[i] != '\0' ) {
730 return -1; /* passwd must behave like a string */
733 strncpy( UcasePassword, cred->bv_val, 14 );
734 UcasePassword[14] = '\0';
735 ldap_pvt_str2upper( UcasePassword );
737 lmPasswd_to_key( UcasePassword, &key );
738 des_set_key_unchecked( &key, schedule );
739 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
741 lmPasswd_to_key( &UcasePassword[7], &key );
742 des_set_key_unchecked( &key, schedule );
743 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
745 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
746 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
747 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
748 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
749 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
751 /* as a precaution convert stored password hash to lower case */
752 strncpy( storedPasswordHash, passwd->bv_val, 32 );
753 storedPasswordHash[32] = '\0';
754 ldap_pvt_str2lower( storedPasswordHash );
756 return memcmp( PasswordHash, storedPasswordHash, 32) ? 1 : 0;
758 #endif /* SLAPD_LMHASH */
761 #ifdef HAVE_CYRUS_SASL
762 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
766 const struct berval *sc,
767 const struct berval * passwd,
768 const struct berval * cred,
774 for( i=0; i<cred->bv_len; i++) {
775 if(cred->bv_val[i] == '\0') {
776 return 1; /* NUL character in password */
780 if( cred->bv_val[i] != '\0' ) {
781 return 1; /* cred must behave like a string */
784 for( i=0; i<passwd->bv_len; i++) {
785 if(passwd->bv_val[i] == '\0') {
786 return 1; /* NUL character in password */
790 if( passwd->bv_val[i] != '\0' ) {
791 return 1; /* passwd must behave like a string */
796 #ifdef HAVE_CYRUS_SASL
797 if( lutil_passwd_sasl_conn != NULL ) {
799 # if SASL_VERSION_MAJOR < 2
800 sc = sasl_checkpass( lutil_passwd_sasl_conn,
801 passwd->bv_val, passwd->bv_len,
802 cred->bv_val, cred->bv_len,
805 sc = sasl_checkpass( lutil_passwd_sasl_conn,
806 passwd->bv_val, passwd->bv_len,
807 cred->bv_val, cred->bv_len );
809 rtn = ( sc != SASL_OK );
818 static int chk_crypt(
819 const struct berval *sc,
820 const struct berval * passwd,
821 const struct berval * cred,
827 for( i=0; i<cred->bv_len; i++) {
828 if(cred->bv_val[i] == '\0') {
829 return 1; /* NUL character in password */
833 if( cred->bv_val[i] != '\0' ) {
834 return -1; /* cred must behave like a string */
837 if( passwd->bv_len < 2 ) {
838 return -1; /* passwd must be at least two characters long */
841 for( i=0; i<passwd->bv_len; i++) {
842 if(passwd->bv_val[i] == '\0') {
843 return -1; /* NUL character in password */
847 if( passwd->bv_val[i] != '\0' ) {
848 return -1; /* passwd must behave like a string */
851 cr = crypt( cred->bv_val, passwd->bv_val );
853 if( cr == NULL || cr[0] == '\0' ) {
854 /* salt must have been invalid */
858 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
861 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
863 const struct berval *sc,
864 const struct berval * passwd,
865 const struct berval * cred,
871 for( i=0; i<cred->bv_len; i++) {
872 if(cred->bv_val[i] == '\0') {
873 return -1; /* NUL character in password */
876 if( cred->bv_val[i] != '\0' ) {
877 return -1; /* cred must behave like a string */
880 for( i=0; i<passwd->bv_len; i++) {
881 if(passwd->bv_val[i] == '\0') {
882 return -1; /* NUL character in password */
886 if( passwd->bv_val[i] != '\0' ) {
887 return -1; /* passwd must behave like a string */
891 struct passwd *pwd = getpwnam(passwd->bv_val);
894 return -1; /* not found */
899 # ifdef HAVE_GETSPNAM
901 struct spwd *spwd = getspnam(passwd->bv_val);
908 # ifdef HAVE_AIX_SECURITY
910 struct userpw *upw = getuserpw(passwd->bv_val);
913 pw = upw->upw_passwd;
918 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
919 /* password must must be at least two characters long */
923 cr = crypt(cred->bv_val, pw);
925 if( cr == NULL || cr[0] == '\0' ) {
926 /* salt must have been invalid */
930 return strcmp(pw, cr) ? 1 : 0;
936 /* PASSWORD GENERATION ROUTINES */
938 #ifdef LUTIL_SHA1_BYTES
939 static struct berval *hash_ssha1(
940 const struct berval *scheme,
941 const struct berval *passwd,
944 lutil_SHA1_CTX SHA1context;
945 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
946 char saltdata[SALT_SIZE];
947 struct berval digest;
950 digest.bv_val = (char *) SHA1digest;
951 digest.bv_len = sizeof(SHA1digest);
952 salt.bv_val = saltdata;
953 salt.bv_len = sizeof(saltdata);
955 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
959 lutil_SHA1Init( &SHA1context );
960 lutil_SHA1Update( &SHA1context,
961 (const unsigned char *)passwd->bv_val, passwd->bv_len );
962 lutil_SHA1Update( &SHA1context,
963 (const unsigned char *)salt.bv_val, salt.bv_len );
964 lutil_SHA1Final( SHA1digest, &SHA1context );
966 return pw_string64( scheme, &digest, &salt);
969 static struct berval *hash_sha1(
970 const struct berval *scheme,
971 const struct berval *passwd,
974 lutil_SHA1_CTX SHA1context;
975 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
976 struct berval digest;
977 digest.bv_val = (char *) SHA1digest;
978 digest.bv_len = sizeof(SHA1digest);
980 lutil_SHA1Init( &SHA1context );
981 lutil_SHA1Update( &SHA1context,
982 (const unsigned char *)passwd->bv_val, passwd->bv_len );
983 lutil_SHA1Final( SHA1digest, &SHA1context );
985 return pw_string64( scheme, &digest, NULL);
989 static struct berval *hash_smd5(
990 const struct berval *scheme,
991 const struct berval *passwd,
994 lutil_MD5_CTX MD5context;
995 unsigned char MD5digest[LUTIL_MD5_BYTES];
996 char saltdata[SALT_SIZE];
997 struct berval digest;
1000 digest.bv_val = (char *) MD5digest;
1001 digest.bv_len = sizeof(MD5digest);
1002 salt.bv_val = saltdata;
1003 salt.bv_len = sizeof(saltdata);
1005 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
1009 lutil_MD5Init( &MD5context );
1010 lutil_MD5Update( &MD5context,
1011 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1012 lutil_MD5Update( &MD5context,
1013 (const unsigned char *) salt.bv_val, salt.bv_len );
1014 lutil_MD5Final( MD5digest, &MD5context );
1016 return pw_string64( scheme, &digest, &salt );
1019 static struct berval *hash_md5(
1020 const struct berval *scheme,
1021 const struct berval *passwd,
1024 lutil_MD5_CTX MD5context;
1025 unsigned char MD5digest[LUTIL_MD5_BYTES];
1027 struct berval digest;
1029 digest.bv_val = (char *) MD5digest;
1030 digest.bv_len = sizeof(MD5digest);
1032 lutil_MD5Init( &MD5context );
1033 lutil_MD5Update( &MD5context,
1034 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1035 lutil_MD5Final( MD5digest, &MD5context );
1037 return pw_string64( scheme, &digest, NULL );
1042 static struct berval *hash_lanman(
1043 const struct berval *scheme,
1044 const struct berval *passwd,
1049 char UcasePassword[15];
1051 des_key_schedule schedule;
1052 des_cblock StdText = "KGS!@#$%";
1053 des_cblock PasswordHash1, PasswordHash2;
1054 char PasswordHash[33];
1057 for( i=0; i<passwd->bv_len; i++) {
1058 if(passwd->bv_val[i] == '\0') {
1059 return NULL; /* NUL character in password */
1063 if( passwd->bv_val[i] != '\0' ) {
1064 return NULL; /* passwd must behave like a string */
1067 strncpy( UcasePassword, passwd->bv_val, 14 );
1068 UcasePassword[14] = '\0';
1069 ldap_pvt_str2upper( UcasePassword );
1071 lmPasswd_to_key( UcasePassword, &key );
1072 des_set_key_unchecked( &key, schedule );
1073 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
1075 lmPasswd_to_key( &UcasePassword[7], &key );
1076 des_set_key_unchecked( &key, schedule );
1077 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
1079 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1080 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
1081 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
1082 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
1083 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
1085 hash.bv_val = PasswordHash;
1088 return pw_string( scheme, &hash );
1090 #endif /* SLAPD_LMHASH */
1093 static struct berval *hash_crypt(
1094 const struct berval *scheme,
1095 const struct berval *passwd,
1099 unsigned char salt[32]; /* salt suitable for most anything */
1102 for( i=0; i<passwd->bv_len; i++) {
1103 if(passwd->bv_val[i] == '\0') {
1104 return NULL; /* NUL character in password */
1108 if( passwd->bv_val[i] != '\0' ) {
1109 return NULL; /* passwd must behave like a string */
1112 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1116 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1117 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1119 salt[sizeof( salt ) - 1 ] = '\0';
1121 if( salt_format != NULL ) {
1122 /* copy the salt we made into entropy before snprintfing
1123 it back into the salt */
1124 char entropy[sizeof(salt)];
1125 strcpy( entropy, (char *) salt );
1126 snprintf( (char *) salt, sizeof(entropy), salt_format, entropy );
1129 hash.bv_val = crypt( passwd->bv_val, (char *) salt );
1131 if( hash.bv_val == NULL ) return NULL;
1133 hash.bv_len = strlen( hash.bv_val );
1135 if( hash.bv_len == 0 ) {
1139 return pw_string( scheme, &hash );
1143 int lutil_salt_format(const char *format)
1146 free( salt_format );
1148 salt_format = format != NULL ? strdup( format ) : NULL;
1154 #ifdef SLAPD_CLEARTEXT
1155 static struct berval *hash_clear(
1156 const struct berval *scheme,
1157 const struct berval *passwd,
1160 return ber_bvdup( (struct berval *) passwd );