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 int lutil_passwd_generate( struct berval *pw, ber_len_t len )
319 if( len < 1 ) return -1;
322 pw->bv_val = ber_memalloc( len + 1 );
324 if( pw->bv_val == NULL ) {
328 if( lutil_entropy( (unsigned char *) pw->bv_val, pw->bv_len) < 0 ) {
332 for( len = 0; len < pw->bv_len; len++ ) {
333 pw->bv_val[len] = crypt64[
334 pw->bv_val[len] % (sizeof(crypt64)-1) ];
337 pw->bv_val[len] = '\0';
342 int lutil_passwd_hash(
343 const struct berval * passwd,
348 const struct pw_scheme *sc = get_scheme( method );
354 if( text ) *text = "scheme not recognized";
358 if( ! sc->hash_fn ) {
359 if( text ) *text = "scheme provided no hash function";
363 if( text ) *text = NULL;
365 return (sc->hash_fn)( &sc->name, passwd, hash, text );
368 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
369 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
370 static int pw_string(
371 const struct berval *sc,
372 const struct berval *passwd )
376 pw->bv_len = sc->bv_len + passwd->bv_len;
377 pw->bv_val = ber_memalloc( pw->bv_len + 1 );
379 if( pw->bv_val == NULL ) {
383 AC_MEMCPY( pw.bv_val, sc->bv_val, sc->bv_len );
384 AC_MEMCPY( &pw.bv_val[sc->bv_len], passwd->bv_val, passwd->bv_len );
386 pw.bv_val[pw.bv_len] = '\0';
391 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
393 static int pw_string64(
394 const struct berval *sc,
395 const struct berval *hash,
397 const struct berval *salt )
400 struct berval string;
404 /* need to base64 combined string */
405 string.bv_len = hash->bv_len + salt->bv_len;
406 string.bv_val = ber_memalloc( string.bv_len + 1 );
408 if( string.bv_val == NULL ) {
412 AC_MEMCPY( string.bv_val, hash->bv_val,
414 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
416 string.bv_val[string.bv_len] = '\0';
422 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
423 b64->bv_len = b64len + sc->bv_len;
424 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
426 if( b64->bv_val == NULL ) {
427 if( salt ) ber_memfree( string.bv_val );
431 AC_MEMCPY(b64->bv_val, sc->bv_val, sc->bv_len);
434 (unsigned char *) string.bv_val, string.bv_len,
435 &b64->bv_val[sc->bv_len], b64len );
437 if( salt ) ber_memfree( string.bv_val );
443 /* recompute length */
444 b64->bv_len = sc->bv_len + rc;
445 assert( strlen(b64->bv_val) == b64->bv_len );
449 /* PASSWORD CHECK ROUTINES */
451 #ifdef LUTIL_SHA1_BYTES
452 static int chk_ssha1(
453 const struct berval *sc,
454 const struct berval * passwd,
455 const struct berval * cred,
458 lutil_SHA1_CTX SHA1context;
459 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
461 unsigned char *orig_pass = NULL;
464 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <
465 sizeof(SHA1digest)+SALT_SIZE) {
469 /* decode base64 password */
470 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
471 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
473 if( orig_pass == NULL ) return -1;
475 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
477 if (rc < (int)(sizeof(SHA1digest)+SALT_SIZE)) {
478 ber_memfree(orig_pass);
482 /* hash credentials with salt */
483 lutil_SHA1Init(&SHA1context);
484 lutil_SHA1Update(&SHA1context,
485 (const unsigned char *) cred->bv_val, cred->bv_len);
486 lutil_SHA1Update(&SHA1context,
487 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
488 rc - sizeof(SHA1digest));
489 lutil_SHA1Final(SHA1digest, &SHA1context);
492 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
493 ber_memfree(orig_pass);
498 const struct berval *sc,
499 const struct berval * passwd,
500 const struct berval * cred,
503 lutil_SHA1_CTX SHA1context;
504 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
506 unsigned char *orig_pass = NULL;
509 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(SHA1digest)) {
513 /* base64 un-encode password */
514 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
515 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
517 if( orig_pass == NULL ) return -1;
519 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
521 if( rc != sizeof(SHA1digest) ) {
522 ber_memfree(orig_pass);
526 /* hash credentials with salt */
527 lutil_SHA1Init(&SHA1context);
528 lutil_SHA1Update(&SHA1context,
529 (const unsigned char *) cred->bv_val, cred->bv_len);
530 lutil_SHA1Final(SHA1digest, &SHA1context);
533 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
534 ber_memfree(orig_pass);
540 const struct berval *sc,
541 const struct berval * passwd,
542 const struct berval * cred,
545 lutil_MD5_CTX MD5context;
546 unsigned char MD5digest[LUTIL_MD5_BYTES];
548 unsigned char *orig_pass = NULL;
551 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <
552 sizeof(MD5digest)+SALT_SIZE) {
556 /* base64 un-encode password */
557 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
558 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
560 if( orig_pass == NULL ) return -1;
562 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
564 if (rc < (int)(sizeof(MD5digest)+SALT_SIZE)) {
565 ber_memfree(orig_pass);
569 /* hash credentials with salt */
570 lutil_MD5Init(&MD5context);
571 lutil_MD5Update(&MD5context,
572 (const unsigned char *) cred->bv_val,
574 lutil_MD5Update(&MD5context,
575 &orig_pass[sizeof(MD5digest)],
576 rc - sizeof(MD5digest));
577 lutil_MD5Final(MD5digest, &MD5context);
580 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
581 ber_memfree(orig_pass);
586 const struct berval *sc,
587 const struct berval * passwd,
588 const struct berval * cred,
591 lutil_MD5_CTX MD5context;
592 unsigned char MD5digest[LUTIL_MD5_BYTES];
594 unsigned char *orig_pass = NULL;
597 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(MD5digest)) {
601 /* base64 un-encode password */
602 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
603 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
605 if( orig_pass == NULL ) return -1;
607 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
608 if ( rc != sizeof(MD5digest) ) {
609 ber_memfree(orig_pass);
613 /* hash credentials with salt */
614 lutil_MD5Init(&MD5context);
615 lutil_MD5Update(&MD5context,
616 (const unsigned char *) cred->bv_val,
618 lutil_MD5Final(MD5digest, &MD5context);
621 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
622 ber_memfree(orig_pass);
627 /* pseudocode from RFC2433
628 * A.2 LmPasswordHash()
631 * IN 0-to-14-oem-char Password,
632 * OUT 16-octet PasswordHash )
634 * Set UcasePassword to the uppercased Password
635 * Zero pad UcasePassword to 14 characters
637 * DesHash( 1st 7-octets of UcasePassword,
638 * giving 1st 8-octets of PasswordHash )
640 * DesHash( 2nd 7-octets of UcasePassword,
641 * giving 2nd 8-octets of PasswordHash )
649 * OUT 8-octet Cypher )
652 * * Make Cypher an irreversibly encrypted form of Clear by
653 * * encrypting known text using Clear as the secret key.
654 * * The known text consists of the string
659 * Set StdText to "KGS!@#$%"
660 * DesEncrypt( StdText, Clear, giving Cypher )
669 * OUT 8-octet Cypher )
672 * * Use the DES encryption algorithm [4] in ECB mode [9]
673 * * to encrypt Clear into Cypher such that Cypher can
674 * * only be decrypted back to Clear by providing Key.
675 * * Note that the DES algorithm takes as input a 64-bit
676 * * stream where the 8th, 16th, 24th, etc. bits are
677 * * parity bits ignored by the encrypting algorithm.
678 * * Unless you write your own DES to accept 56-bit input
679 * * without parity, you will need to insert the parity bits
685 static void lmPasswd_to_key(
686 const unsigned char *lmPasswd,
689 /* make room for parity bits */
690 ((char *)key)[0] = lmPasswd[0];
691 ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
692 ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
693 ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
694 ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
695 ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
696 ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
697 ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
699 des_set_odd_parity( key );
702 static int chk_lanman(
703 const struct berval *scheme,
704 const struct berval *passwd,
705 const struct berval *cred,
709 char UcasePassword[15];
711 des_key_schedule schedule;
712 des_cblock StdText = "KGS!@#$%";
713 des_cblock PasswordHash1, PasswordHash2;
714 char PasswordHash[33], storedPasswordHash[33];
716 for( i=0; i<cred->bv_len; i++) {
717 if(cred->bv_val[i] == '\0') {
718 return -1; /* NUL character in password */
722 if( cred->bv_val[i] != '\0' ) {
723 return -1; /* passwd must behave like a string */
726 strncpy( UcasePassword, cred->bv_val, 14 );
727 UcasePassword[14] = '\0';
728 ldap_pvt_str2upper( UcasePassword );
730 lmPasswd_to_key( UcasePassword, &key );
731 des_set_key_unchecked( &key, schedule );
732 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
734 lmPasswd_to_key( &UcasePassword[7], &key );
735 des_set_key_unchecked( &key, schedule );
736 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
738 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
739 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
740 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
741 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
742 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
744 /* as a precaution convert stored password hash to lower case */
745 strncpy( storedPasswordHash, passwd->bv_val, 32 );
746 storedPasswordHash[32] = '\0';
747 ldap_pvt_str2lower( storedPasswordHash );
749 return memcmp( PasswordHash, storedPasswordHash, 32) ? 1 : 0;
751 #endif /* SLAPD_LMHASH */
754 #ifdef HAVE_CYRUS_SASL
755 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
759 const struct berval *sc,
760 const struct berval * passwd,
761 const struct berval * cred,
767 for( i=0; i<cred->bv_len; i++) {
768 if(cred->bv_val[i] == '\0') {
769 return 1; /* NUL character in password */
773 if( cred->bv_val[i] != '\0' ) {
774 return 1; /* cred must behave like a string */
777 for( i=0; i<passwd->bv_len; i++) {
778 if(passwd->bv_val[i] == '\0') {
779 return 1; /* NUL character in password */
783 if( passwd->bv_val[i] != '\0' ) {
784 return 1; /* passwd must behave like a string */
789 #ifdef HAVE_CYRUS_SASL
790 if( lutil_passwd_sasl_conn != NULL ) {
792 # if SASL_VERSION_MAJOR < 2
793 sc = sasl_checkpass( lutil_passwd_sasl_conn,
794 passwd->bv_val, passwd->bv_len,
795 cred->bv_val, cred->bv_len,
798 sc = sasl_checkpass( lutil_passwd_sasl_conn,
799 passwd->bv_val, passwd->bv_len,
800 cred->bv_val, cred->bv_len );
802 rtn = ( sc != SASL_OK );
811 static int chk_crypt(
812 const struct berval *sc,
813 const struct berval * passwd,
814 const struct berval * cred,
820 for( i=0; i<cred->bv_len; i++) {
821 if(cred->bv_val[i] == '\0') {
822 return 1; /* NUL character in password */
826 if( cred->bv_val[i] != '\0' ) {
827 return -1; /* cred must behave like a string */
830 if( passwd->bv_len < 2 ) {
831 return -1; /* passwd must be at least two characters long */
834 for( i=0; i<passwd->bv_len; i++) {
835 if(passwd->bv_val[i] == '\0') {
836 return -1; /* NUL character in password */
840 if( passwd->bv_val[i] != '\0' ) {
841 return -1; /* passwd must behave like a string */
844 cr = crypt( cred->bv_val, passwd->bv_val );
846 if( cr == NULL || cr[0] == '\0' ) {
847 /* salt must have been invalid */
851 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
854 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
856 const struct berval *sc,
857 const struct berval * passwd,
858 const struct berval * cred,
864 for( i=0; i<cred->bv_len; i++) {
865 if(cred->bv_val[i] == '\0') {
866 return -1; /* NUL character in password */
869 if( cred->bv_val[i] != '\0' ) {
870 return -1; /* cred must behave like a string */
873 for( i=0; i<passwd->bv_len; i++) {
874 if(passwd->bv_val[i] == '\0') {
875 return -1; /* NUL character in password */
879 if( passwd->bv_val[i] != '\0' ) {
880 return -1; /* passwd must behave like a string */
884 struct passwd *pwd = getpwnam(passwd->bv_val);
887 return -1; /* not found */
892 # ifdef HAVE_GETSPNAM
894 struct spwd *spwd = getspnam(passwd->bv_val);
901 # ifdef HAVE_AIX_SECURITY
903 struct userpw *upw = getuserpw(passwd->bv_val);
906 pw = upw->upw_passwd;
911 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
912 /* password must must be at least two characters long */
916 cr = crypt(cred->bv_val, pw);
918 if( cr == NULL || cr[0] == '\0' ) {
919 /* salt must have been invalid */
923 return strcmp(pw, cr) ? 1 : 0;
929 /* PASSWORD GENERATION ROUTINES */
931 #ifdef LUTIL_SHA1_BYTES
932 static int hash_ssha1(
933 const struct berval *scheme,
934 const struct berval *passwd,
938 lutil_SHA1_CTX SHA1context;
939 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
940 char saltdata[SALT_SIZE];
941 struct berval digest;
944 digest.bv_val = (char *) SHA1digest;
945 digest.bv_len = sizeof(SHA1digest);
946 salt.bv_val = saltdata;
947 salt.bv_len = sizeof(saltdata);
949 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
953 lutil_SHA1Init( &SHA1context );
954 lutil_SHA1Update( &SHA1context,
955 (const unsigned char *)passwd->bv_val, passwd->bv_len );
956 lutil_SHA1Update( &SHA1context,
957 (const unsigned char *)salt.bv_val, salt.bv_len );
958 lutil_SHA1Final( SHA1digest, &SHA1context );
960 return pw_string64( scheme, &digest, hash, &salt);
963 static int hash_sha1(
964 const struct berval *scheme,
965 const struct berval *passwd,
969 lutil_SHA1_CTX SHA1context;
970 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
971 struct berval digest;
972 digest.bv_val = (char *) SHA1digest;
973 digest.bv_len = sizeof(SHA1digest);
975 lutil_SHA1Init( &SHA1context );
976 lutil_SHA1Update( &SHA1context,
977 (const unsigned char *)passwd->bv_val, passwd->bv_len );
978 lutil_SHA1Final( SHA1digest, &SHA1context );
980 return pw_string64( scheme, &digest, hash, NULL);
984 static int hash_smd5(
985 const struct berval *scheme,
986 const struct berval *passwd,
990 lutil_MD5_CTX MD5context;
991 unsigned char MD5digest[LUTIL_MD5_BYTES];
992 char saltdata[SALT_SIZE];
993 struct berval digest;
996 digest.bv_val = (char *) MD5digest;
997 digest.bv_len = sizeof(MD5digest);
998 salt.bv_val = saltdata;
999 salt.bv_len = sizeof(saltdata);
1001 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
1005 lutil_MD5Init( &MD5context );
1006 lutil_MD5Update( &MD5context,
1007 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1008 lutil_MD5Update( &MD5context,
1009 (const unsigned char *) salt.bv_val, salt.bv_len );
1010 lutil_MD5Final( MD5digest, &MD5context );
1012 return pw_string64( scheme, &digest, hash, &salt );
1015 static int hash_md5(
1016 const struct berval *scheme,
1017 const struct berval *passwd,
1018 struct berval *hash,
1021 lutil_MD5_CTX MD5context;
1022 unsigned char MD5digest[LUTIL_MD5_BYTES];
1024 struct berval digest;
1026 digest.bv_val = (char *) MD5digest;
1027 digest.bv_len = sizeof(MD5digest);
1029 lutil_MD5Init( &MD5context );
1030 lutil_MD5Update( &MD5context,
1031 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1032 lutil_MD5Final( MD5digest, &MD5context );
1034 return pw_string64( scheme, &digest, hash, NULL );
1039 static int hash_lanman(
1040 const struct berval *scheme,
1041 const struct berval *passwd,
1042 struct berval *hash,
1047 char UcasePassword[15];
1049 des_key_schedule schedule;
1050 des_cblock StdText = "KGS!@#$%";
1051 des_cblock PasswordHash1, PasswordHash2;
1052 char PasswordHash[33];
1054 for( i=0; i<passwd->bv_len; i++) {
1055 if(passwd->bv_val[i] == '\0') {
1056 return -1; /* NUL character in password */
1060 if( passwd->bv_val[i] != '\0' ) {
1061 return -1; /* passwd must behave like a string */
1064 strncpy( UcasePassword, passwd->bv_val, 14 );
1065 UcasePassword[14] = '\0';
1066 ldap_pvt_str2upper( UcasePassword );
1068 lmPasswd_to_key( UcasePassword, &key );
1069 des_set_key_unchecked( &key, schedule );
1070 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
1072 lmPasswd_to_key( &UcasePassword[7], &key );
1073 des_set_key_unchecked( &key, schedule );
1074 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
1076 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1077 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
1078 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
1079 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
1080 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
1082 hash->bv_val = PasswordHash;
1085 return pw_string( scheme, hash );
1087 #endif /* SLAPD_LMHASH */
1090 static int hash_crypt(
1091 const struct berval *scheme,
1092 const struct berval *passwd,
1093 struct berval *hash,
1096 unsigned char salt[32]; /* salt suitable for most anything */
1099 for( i=0; i<passwd->bv_len; i++) {
1100 if(passwd->bv_val[i] == '\0') {
1101 return NULL; /* NUL character in password */
1105 if( passwd->bv_val[i] != '\0' ) {
1106 return NULL; /* passwd must behave like a string */
1109 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1113 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1114 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1116 salt[sizeof( salt ) - 1 ] = '\0';
1118 if( salt_format != NULL ) {
1119 /* copy the salt we made into entropy before snprintfing
1120 it back into the salt */
1121 char entropy[sizeof(salt)];
1122 strcpy( entropy, (char *) salt );
1123 snprintf( (char *) salt, sizeof(entropy), salt_format, entropy );
1126 hash->bv_val = crypt( passwd->bv_val, (char *) salt );
1128 if( hash->bv_val == NULL ) return NULL;
1130 hash->bv_len = strlen( hash->bv_val );
1132 if( hash->bv_len == 0 ) {
1136 return pw_string( scheme, hash );
1140 int lutil_salt_format(const char *format)
1143 free( salt_format );
1145 salt_format = format != NULL ? strdup( format ) : NULL;
1151 #ifdef SLAPD_CLEARTEXT
1152 static int hash_clear(
1153 const struct berval *scheme,
1154 const struct berval *passwd,
1155 struct berval *hash,
1158 ber_dupbv( hash, (struct berval *)passwd );