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;
77 * chk_fn is NULL iff name is {CLEARTEXT}
78 * otherwise, things will break
82 LUTIL_PASSWD_CHK_FUNC *chk_fn;
83 LUTIL_PASSWD_HASH_FUNC *hash_fn;
87 struct pw_slist *next;
91 /* password check routines */
95 static LUTIL_PASSWD_CHK_FUNC chk_md5;
96 static LUTIL_PASSWD_CHK_FUNC chk_smd5;
97 static LUTIL_PASSWD_HASH_FUNC hash_smd5;
98 static LUTIL_PASSWD_HASH_FUNC hash_md5;
101 #ifdef LUTIL_SHA1_BYTES
102 static LUTIL_PASSWD_CHK_FUNC chk_ssha1;
103 static LUTIL_PASSWD_CHK_FUNC chk_sha1;
104 static LUTIL_PASSWD_HASH_FUNC hash_sha1;
105 static LUTIL_PASSWD_HASH_FUNC hash_ssha1;
109 static LUTIL_PASSWD_CHK_FUNC chk_lanman;
110 static LUTIL_PASSWD_HASH_FUNC hash_lanman;
114 static LUTIL_PASSWD_CHK_FUNC chk_crypt;
115 static LUTIL_PASSWD_HASH_FUNC hash_crypt;
117 #if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
118 static LUTIL_PASSWD_CHK_FUNC chk_unix;
122 /* password hash routines */
124 #ifdef SLAPD_CLEARTEXT
125 static LUTIL_PASSWD_HASH_FUNC hash_clear;
128 static struct pw_slist *pw_schemes;
129 static int pw_inited;
131 static const struct pw_scheme pw_schemes_default[] =
133 #ifdef LUTIL_SHA1_BYTES
134 { BER_BVC("{SSHA}"), chk_ssha1, hash_ssha1 },
135 { BER_BVC("{SHA}"), chk_sha1, hash_sha1 },
138 { BER_BVC("{SMD5}"), chk_smd5, hash_smd5 },
139 { BER_BVC("{MD5}"), chk_md5, hash_md5 },
142 { BER_BVC("{LANMAN}"), chk_lanman, hash_lanman },
143 #endif /* SLAPD_LMHASH */
146 { BER_BVC("{CRYPT}"), chk_crypt, hash_crypt },
147 # if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
148 { BER_BVC("{UNIX}"), chk_unix, NULL },
152 #ifdef SLAPD_CLEARTEXT
154 { BER_BVC("{CLEARTEXT}"), NULL, hash_clear },
157 { BER_BVNULL, NULL, NULL }
160 int lutil_passwd_add(
161 struct berval *scheme,
162 LUTIL_PASSWD_CHK_FUNC *chk,
163 LUTIL_PASSWD_HASH_FUNC *hash )
165 struct pw_slist *ptr;
167 if (!pw_inited) lutil_passwd_init();
169 ptr = ber_memalloc( sizeof( struct pw_slist ));
171 ptr->next = pw_schemes;
172 ptr->s.name = *scheme;
174 ptr->s.hash_fn = hash;
179 void lutil_passwd_init()
185 for( s=(struct pw_scheme *)pw_schemes_default; s->name.bv_val; s++) {
186 if ( lutil_passwd_add( &s->name, s->chk_fn, s->hash_fn ) ) break;
190 void lutil_passwd_destroy()
192 struct pw_slist *ptr, *next;
194 for( ptr=pw_schemes; ptr; ptr=next ) {
200 static const struct pw_scheme *get_scheme(
203 struct pw_slist *pws;
206 if (!pw_inited) lutil_passwd_init();
208 bv.bv_val = strchr( scheme, '}' );
212 bv.bv_len = bv.bv_val - scheme + 1;
213 bv.bv_val = (char *) scheme;
215 for( pws=pw_schemes; pws; pws=pws->next ) {
216 if ( ber_bvstrcasecmp(&bv, &pws->s.name ) == 0 ) {
224 int lutil_passwd_scheme(
227 if( scheme == NULL ) {
231 return get_scheme(scheme) != NULL;
235 static int is_allowed_scheme(
237 const char** schemes )
241 if( schemes == NULL ) return 1;
243 for( i=0; schemes[i] != NULL; i++ ) {
244 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
251 static struct berval *passwd_scheme(
252 const struct pw_scheme *scheme,
253 const struct berval * passwd,
255 const char** allowed )
257 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
261 if( passwd->bv_len >= scheme->name.bv_len ) {
262 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
263 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
264 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
274 * Return 0 if creds are good.
278 const struct berval *passwd, /* stored passwd */
279 const struct berval *cred, /* user cred */
280 const char **schemes,
283 struct pw_slist *pws;
285 if ( text ) *text = NULL;
287 if (cred == NULL || cred->bv_len == 0 ||
288 passwd == NULL || passwd->bv_len == 0 )
293 if (!pw_inited) lutil_passwd_init();
295 for( pws=pw_schemes; pws; pws=pws->next ) {
296 if( pws->s.chk_fn ) {
298 struct berval *p = passwd_scheme( &(pws->s),
299 passwd, &x, schemes );
302 return (pws->s.chk_fn)( &(pws->s.name), p, cred, text );
307 #ifdef SLAPD_CLEARTEXT
308 /* Do we think there is a scheme specifier here that we
309 * didn't recognize? Assume a scheme name is at least 1 character.
311 if (( passwd->bv_val[0] == '{' ) &&
312 ( strchr( passwd->bv_val, '}' ) > passwd->bv_val+1 ))
316 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
317 return ( passwd->bv_len == cred->bv_len ) ?
318 memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
325 int lutil_passwd_generate( struct berval *pw, ber_len_t len )
328 if( len < 1 ) return -1;
331 pw->bv_val = ber_memalloc( len + 1 );
333 if( pw->bv_val == NULL ) {
337 if( lutil_entropy( (unsigned char *) pw->bv_val, pw->bv_len) < 0 ) {
341 for( len = 0; len < pw->bv_len; len++ ) {
342 pw->bv_val[len] = crypt64[
343 pw->bv_val[len] % (sizeof(crypt64)-1) ];
346 pw->bv_val[len] = '\0';
351 int lutil_passwd_hash(
352 const struct berval * passwd,
357 const struct pw_scheme *sc = get_scheme( method );
363 if( text ) *text = "scheme not recognized";
367 if( ! sc->hash_fn ) {
368 if( text ) *text = "scheme provided no hash function";
372 if( text ) *text = NULL;
374 return (sc->hash_fn)( &sc->name, passwd, hash, text );
377 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
378 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
379 static int pw_string(
380 const struct berval *sc,
381 struct berval *passwd )
385 pw.bv_len = sc->bv_len + passwd->bv_len;
386 pw.bv_val = ber_memalloc( pw.bv_len + 1 );
388 if( pw.bv_val == NULL ) {
389 return LUTIL_PASSWD_ERR;
392 AC_MEMCPY( pw.bv_val, sc->bv_val, sc->bv_len );
393 AC_MEMCPY( &pw.bv_val[sc->bv_len], passwd->bv_val, passwd->bv_len );
395 pw.bv_val[pw.bv_len] = '\0';
398 return LUTIL_PASSWD_OK;
400 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
402 static int pw_string64(
403 const struct berval *sc,
404 const struct berval *hash,
406 const struct berval *salt )
409 struct berval string;
413 /* need to base64 combined string */
414 string.bv_len = hash->bv_len + salt->bv_len;
415 string.bv_val = ber_memalloc( string.bv_len + 1 );
417 if( string.bv_val == NULL ) {
418 return LUTIL_PASSWD_ERR;
421 AC_MEMCPY( string.bv_val, hash->bv_val,
423 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
425 string.bv_val[string.bv_len] = '\0';
431 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
432 b64->bv_len = b64len + sc->bv_len;
433 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
435 if( b64->bv_val == NULL ) {
436 if( salt ) ber_memfree( string.bv_val );
437 return LUTIL_PASSWD_ERR;
440 AC_MEMCPY(b64->bv_val, sc->bv_val, sc->bv_len);
443 (unsigned char *) string.bv_val, string.bv_len,
444 &b64->bv_val[sc->bv_len], b64len );
446 if( salt ) ber_memfree( string.bv_val );
449 return LUTIL_PASSWD_ERR;
452 /* recompute length */
453 b64->bv_len = sc->bv_len + rc;
454 assert( strlen(b64->bv_val) == b64->bv_len );
455 return LUTIL_PASSWD_OK;
458 /* PASSWORD CHECK ROUTINES */
460 #ifdef LUTIL_SHA1_BYTES
461 static int chk_ssha1(
462 const struct berval *sc,
463 const struct berval * passwd,
464 const struct berval * cred,
467 lutil_SHA1_CTX SHA1context;
468 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
470 unsigned char *orig_pass = NULL;
472 /* safety check -- must have some salt */
473 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(SHA1digest)) {
474 return LUTIL_PASSWD_ERR;
477 /* decode base64 password */
478 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
479 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
481 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
483 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
485 /* safety check -- must have some salt */
486 if (rc <= (int)(sizeof(SHA1digest))) {
487 ber_memfree(orig_pass);
488 return LUTIL_PASSWD_ERR;
491 /* hash credentials with salt */
492 lutil_SHA1Init(&SHA1context);
493 lutil_SHA1Update(&SHA1context,
494 (const unsigned char *) cred->bv_val, cred->bv_len);
495 lutil_SHA1Update(&SHA1context,
496 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
497 rc - sizeof(SHA1digest));
498 lutil_SHA1Final(SHA1digest, &SHA1context);
501 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
502 ber_memfree(orig_pass);
503 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
507 const struct berval *sc,
508 const struct berval * passwd,
509 const struct berval * cred,
512 lutil_SHA1_CTX SHA1context;
513 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
515 unsigned char *orig_pass = NULL;
518 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(SHA1digest)) {
519 return LUTIL_PASSWD_ERR;
522 /* base64 un-encode password */
523 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
524 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
526 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
528 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
530 if( rc != sizeof(SHA1digest) ) {
531 ber_memfree(orig_pass);
532 return LUTIL_PASSWD_ERR;
535 /* hash credentials with salt */
536 lutil_SHA1Init(&SHA1context);
537 lutil_SHA1Update(&SHA1context,
538 (const unsigned char *) cred->bv_val, cred->bv_len);
539 lutil_SHA1Final(SHA1digest, &SHA1context);
542 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
543 ber_memfree(orig_pass);
544 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
549 const struct berval *sc,
550 const struct berval * passwd,
551 const struct berval * cred,
554 lutil_MD5_CTX MD5context;
555 unsigned char MD5digest[LUTIL_MD5_BYTES];
557 unsigned char *orig_pass = NULL;
560 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(MD5digest)) {
561 return LUTIL_PASSWD_ERR;
564 /* base64 un-encode password */
565 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
566 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
568 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
570 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
572 if (rc <= (int)(sizeof(MD5digest))) {
573 ber_memfree(orig_pass);
574 return LUTIL_PASSWD_ERR;
577 /* hash credentials with salt */
578 lutil_MD5Init(&MD5context);
579 lutil_MD5Update(&MD5context,
580 (const unsigned char *) cred->bv_val,
582 lutil_MD5Update(&MD5context,
583 &orig_pass[sizeof(MD5digest)],
584 rc - sizeof(MD5digest));
585 lutil_MD5Final(MD5digest, &MD5context);
588 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
589 ber_memfree(orig_pass);
590 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
594 const struct berval *sc,
595 const struct berval * passwd,
596 const struct berval * cred,
599 lutil_MD5_CTX MD5context;
600 unsigned char MD5digest[LUTIL_MD5_BYTES];
602 unsigned char *orig_pass = NULL;
605 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(MD5digest)) {
606 return LUTIL_PASSWD_ERR;
609 /* base64 un-encode password */
610 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
611 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
613 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
615 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
616 if ( rc != sizeof(MD5digest) ) {
617 ber_memfree(orig_pass);
618 return LUTIL_PASSWD_ERR;
621 /* hash credentials with salt */
622 lutil_MD5Init(&MD5context);
623 lutil_MD5Update(&MD5context,
624 (const unsigned char *) cred->bv_val,
626 lutil_MD5Final(MD5digest, &MD5context);
629 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
630 ber_memfree(orig_pass);
631 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
635 /* pseudocode from RFC2433
636 * A.2 LmPasswordHash()
639 * IN 0-to-14-oem-char Password,
640 * OUT 16-octet PasswordHash )
642 * Set UcasePassword to the uppercased Password
643 * Zero pad UcasePassword to 14 characters
645 * DesHash( 1st 7-octets of UcasePassword,
646 * giving 1st 8-octets of PasswordHash )
648 * DesHash( 2nd 7-octets of UcasePassword,
649 * giving 2nd 8-octets of PasswordHash )
657 * OUT 8-octet Cypher )
660 * * Make Cypher an irreversibly encrypted form of Clear by
661 * * encrypting known text using Clear as the secret key.
662 * * The known text consists of the string
667 * Set StdText to "KGS!@#$%"
668 * DesEncrypt( StdText, Clear, giving Cypher )
677 * OUT 8-octet Cypher )
680 * * Use the DES encryption algorithm [4] in ECB mode [9]
681 * * to encrypt Clear into Cypher such that Cypher can
682 * * only be decrypted back to Clear by providing Key.
683 * * Note that the DES algorithm takes as input a 64-bit
684 * * stream where the 8th, 16th, 24th, etc. bits are
685 * * parity bits ignored by the encrypting algorithm.
686 * * Unless you write your own DES to accept 56-bit input
687 * * without parity, you will need to insert the parity bits
693 static void lmPasswd_to_key(
694 const char *lmPasswd,
697 const unsigned char *lpw = (const unsigned char *) lmPasswd;
698 unsigned char *k = (unsigned char *) key;
700 /* make room for parity bits */
702 k[1] = ((lpw[0] & 0x01) << 7) | (lpw[1] >> 1);
703 k[2] = ((lpw[1] & 0x03) << 6) | (lpw[2] >> 2);
704 k[3] = ((lpw[2] & 0x07) << 5) | (lpw[3] >> 3);
705 k[4] = ((lpw[3] & 0x0F) << 4) | (lpw[4] >> 4);
706 k[5] = ((lpw[4] & 0x1F) << 3) | (lpw[5] >> 5);
707 k[6] = ((lpw[5] & 0x3F) << 2) | (lpw[6] >> 6);
708 k[7] = ((lpw[6] & 0x7F) << 1);
710 des_set_odd_parity( key );
713 static int chk_lanman(
714 const struct berval *scheme,
715 const struct berval *passwd,
716 const struct berval *cred,
720 char UcasePassword[15];
722 des_key_schedule schedule;
723 des_cblock StdText = "KGS!@#$%";
724 des_cblock PasswordHash1, PasswordHash2;
725 char PasswordHash[33], storedPasswordHash[33];
727 for( i=0; i<cred->bv_len; i++) {
728 if(cred->bv_val[i] == '\0') {
729 return LUTIL_PASSWD_ERR; /* NUL character in password */
733 if( cred->bv_val[i] != '\0' ) {
734 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
737 strncpy( UcasePassword, cred->bv_val, 14 );
738 UcasePassword[14] = '\0';
739 ldap_pvt_str2upper( UcasePassword );
741 lmPasswd_to_key( UcasePassword, &key );
742 des_set_key_unchecked( &key, schedule );
743 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
745 lmPasswd_to_key( &UcasePassword[7], &key );
746 des_set_key_unchecked( &key, schedule );
747 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
749 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
750 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
751 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
752 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
753 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
755 /* as a precaution convert stored password hash to lower case */
756 strncpy( storedPasswordHash, passwd->bv_val, 32 );
757 storedPasswordHash[32] = '\0';
758 ldap_pvt_str2lower( storedPasswordHash );
760 return memcmp( PasswordHash, storedPasswordHash, 32) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
762 #endif /* SLAPD_LMHASH */
765 static int lutil_crypt(
770 char *cr = crypt( key, salt );
773 if( cr == NULL || cr[0] == '\0' ) {
774 /* salt must have been invalid */
775 rc = LUTIL_PASSWD_ERR;
778 *hash = ber_strdup( cr );
779 rc = LUTIL_PASSWD_OK;
781 rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
787 static int chk_crypt(
788 const struct berval *sc,
789 const struct berval * passwd,
790 const struct berval * cred,
795 for( i=0; i<cred->bv_len; i++) {
796 if(cred->bv_val[i] == '\0') {
797 return LUTIL_PASSWD_ERR; /* NUL character in password */
801 if( cred->bv_val[i] != '\0' ) {
802 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
805 if( passwd->bv_len < 2 ) {
806 return LUTIL_PASSWD_ERR; /* passwd must be at least two characters long */
809 for( i=0; i<passwd->bv_len; i++) {
810 if(passwd->bv_val[i] == '\0') {
811 return LUTIL_PASSWD_ERR; /* NUL character in password */
815 if( passwd->bv_val[i] != '\0' ) {
816 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
819 return lutil_cryptptr( cred->bv_val, passwd->bv_val, NULL );
822 # if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
824 const struct berval *sc,
825 const struct berval * passwd,
826 const struct berval * cred,
832 for( i=0; i<cred->bv_len; i++) {
833 if(cred->bv_val[i] == '\0') {
834 return LUTIL_PASSWD_ERR; /* NUL character in password */
837 if( cred->bv_val[i] != '\0' ) {
838 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
841 for( i=0; i<passwd->bv_len; i++) {
842 if(passwd->bv_val[i] == '\0') {
843 return LUTIL_PASSWD_ERR; /* NUL character in password */
847 if( passwd->bv_val[i] != '\0' ) {
848 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
852 struct passwd *pwd = getpwnam(passwd->bv_val);
855 return LUTIL_PASSWD_ERR; /* not found */
860 # ifdef HAVE_GETSPNAM
862 struct spwd *spwd = getspnam(passwd->bv_val);
869 # ifdef HAVE_AIX_SECURITY
871 struct userpw *upw = getuserpw(passwd->bv_val);
874 pw = upw->upw_passwd;
879 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
880 /* password must must be at least two characters long */
881 return LUTIL_PASSWD_ERR;
884 return lutil_cryptptr( cred->bv_val, pw, NULL );
889 /* PASSWORD GENERATION ROUTINES */
891 #ifdef LUTIL_SHA1_BYTES
892 static int hash_ssha1(
893 const struct berval *scheme,
894 const struct berval *passwd,
898 lutil_SHA1_CTX SHA1context;
899 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
900 char saltdata[SALT_SIZE];
901 struct berval digest;
904 digest.bv_val = (char *) SHA1digest;
905 digest.bv_len = sizeof(SHA1digest);
906 salt.bv_val = saltdata;
907 salt.bv_len = sizeof(saltdata);
909 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
910 return LUTIL_PASSWD_ERR;
913 lutil_SHA1Init( &SHA1context );
914 lutil_SHA1Update( &SHA1context,
915 (const unsigned char *)passwd->bv_val, passwd->bv_len );
916 lutil_SHA1Update( &SHA1context,
917 (const unsigned char *)salt.bv_val, salt.bv_len );
918 lutil_SHA1Final( SHA1digest, &SHA1context );
920 return pw_string64( scheme, &digest, hash, &salt);
923 static int hash_sha1(
924 const struct berval *scheme,
925 const struct berval *passwd,
929 lutil_SHA1_CTX SHA1context;
930 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
931 struct berval digest;
932 digest.bv_val = (char *) SHA1digest;
933 digest.bv_len = sizeof(SHA1digest);
935 lutil_SHA1Init( &SHA1context );
936 lutil_SHA1Update( &SHA1context,
937 (const unsigned char *)passwd->bv_val, passwd->bv_len );
938 lutil_SHA1Final( SHA1digest, &SHA1context );
940 return pw_string64( scheme, &digest, hash, NULL);
944 static int hash_smd5(
945 const struct berval *scheme,
946 const struct berval *passwd,
950 lutil_MD5_CTX MD5context;
951 unsigned char MD5digest[LUTIL_MD5_BYTES];
952 char saltdata[SALT_SIZE];
953 struct berval digest;
956 digest.bv_val = (char *) MD5digest;
957 digest.bv_len = sizeof(MD5digest);
958 salt.bv_val = saltdata;
959 salt.bv_len = sizeof(saltdata);
961 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
962 return LUTIL_PASSWD_ERR;
965 lutil_MD5Init( &MD5context );
966 lutil_MD5Update( &MD5context,
967 (const unsigned char *) passwd->bv_val, passwd->bv_len );
968 lutil_MD5Update( &MD5context,
969 (const unsigned char *) salt.bv_val, salt.bv_len );
970 lutil_MD5Final( MD5digest, &MD5context );
972 return pw_string64( scheme, &digest, hash, &salt );
976 const struct berval *scheme,
977 const struct berval *passwd,
981 lutil_MD5_CTX MD5context;
982 unsigned char MD5digest[LUTIL_MD5_BYTES];
984 struct berval digest;
986 digest.bv_val = (char *) MD5digest;
987 digest.bv_len = sizeof(MD5digest);
989 lutil_MD5Init( &MD5context );
990 lutil_MD5Update( &MD5context,
991 (const unsigned char *) passwd->bv_val, passwd->bv_len );
992 lutil_MD5Final( MD5digest, &MD5context );
994 return pw_string64( scheme, &digest, hash, NULL );
999 static int hash_lanman(
1000 const struct berval *scheme,
1001 const struct berval *passwd,
1002 struct berval *hash,
1007 char UcasePassword[15];
1009 des_key_schedule schedule;
1010 des_cblock StdText = "KGS!@#$%";
1011 des_cblock PasswordHash1, PasswordHash2;
1012 char PasswordHash[33];
1014 for( i=0; i<passwd->bv_len; i++) {
1015 if(passwd->bv_val[i] == '\0') {
1016 return LUTIL_PASSWD_ERR; /* NUL character in password */
1020 if( passwd->bv_val[i] != '\0' ) {
1021 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
1024 strncpy( UcasePassword, passwd->bv_val, 14 );
1025 UcasePassword[14] = '\0';
1026 ldap_pvt_str2upper( UcasePassword );
1028 lmPasswd_to_key( UcasePassword, &key );
1029 des_set_key_unchecked( &key, schedule );
1030 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
1032 lmPasswd_to_key( &UcasePassword[7], &key );
1033 des_set_key_unchecked( &key, schedule );
1034 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
1036 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1037 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
1038 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
1039 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
1040 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
1042 hash->bv_val = PasswordHash;
1045 return pw_string( scheme, hash );
1047 #endif /* SLAPD_LMHASH */
1050 static int hash_crypt(
1051 const struct berval *scheme,
1052 const struct berval *passwd,
1053 struct berval *hash,
1056 unsigned char salt[32]; /* salt suitable for most anything */
1061 for( i=0; i<passwd->bv_len; i++) {
1062 if(passwd->bv_val[i] == '\0') {
1063 return LUTIL_PASSWD_ERR; /* NUL character in password */
1067 if( passwd->bv_val[i] != '\0' ) {
1068 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
1071 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1072 return LUTIL_PASSWD_ERR;
1075 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1076 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1078 salt[sizeof( salt ) - 1 ] = '\0';
1080 if( salt_format != NULL ) {
1081 /* copy the salt we made into entropy before snprintfing
1082 it back into the salt */
1083 char entropy[sizeof(salt)];
1084 strcpy( entropy, (char *) salt );
1085 snprintf( (char *) salt, sizeof(entropy), salt_format, entropy );
1088 rc = lutil_cryptptr( passwd->bv_val, (char *) salt, &hash->bv_val );
1089 if ( rc != LUTIL_PASSWD_OK ) return rc;
1091 if( hash->bv_val == NULL ) return -1;
1093 hash->bv_len = strlen( hash->bv_val );
1095 save = hash->bv_val;
1097 if( hash->bv_len == 0 ) {
1098 rc = LUTIL_PASSWD_ERR;
1100 rc = pw_string( scheme, hash );
1102 ber_memfree( save );
1107 int lutil_salt_format(const char *format)
1110 free( salt_format );
1112 salt_format = format != NULL ? ber_strdup( format ) : NULL;
1118 #ifdef SLAPD_CLEARTEXT
1119 static int hash_clear(
1120 const struct berval *scheme,
1121 const struct berval *passwd,
1122 struct berval *hash,
1125 ber_dupbv( hash, (struct berval *)passwd );
1126 return LUTIL_PASSWD_OK;