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_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_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_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;
201 if (!pw_inited) lutil_passwd_init();
203 for( pws=pw_schemes; pws; pws=pws->next ) {
204 if( strcasecmp(scheme, pws->s.name.bv_val ) == 0 ) {
212 int lutil_passwd_scheme(
215 if( scheme == NULL ) {
219 return get_scheme(scheme) != NULL;
223 static int is_allowed_scheme(
225 const char** schemes )
229 if( schemes == NULL ) return 1;
231 for( i=0; schemes[i] != NULL; i++ ) {
232 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
239 static struct berval *passwd_scheme(
240 const struct pw_scheme *scheme,
241 const struct berval * passwd,
243 const char** allowed )
245 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
249 if( passwd->bv_len >= scheme->name.bv_len ) {
250 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
251 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
252 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
262 * Return 0 if creds are good.
266 const struct berval *passwd, /* stored passwd */
267 const struct berval *cred, /* user cred */
268 const char **schemes,
271 struct pw_slist *pws;
273 if ( text ) *text = NULL;
275 if (cred == NULL || cred->bv_len == 0 ||
276 passwd == NULL || passwd->bv_len == 0 )
281 if (!pw_inited) lutil_passwd_init();
283 for( pws=pw_schemes; pws; pws=pws->next ) {
284 if( pws->s.chk_fn ) {
286 struct berval *p = passwd_scheme( &(pws->s),
287 passwd, &x, schemes );
290 return (pws->s.chk_fn)( &(pws->s.name), p, cred, text );
295 #ifdef SLAPD_CLEARTEXT
296 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
297 return (( passwd->bv_len == cred->bv_len ) &&
298 ( passwd->bv_val[0] != '{' /*'}'*/ ))
299 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
306 int lutil_passwd_generate( struct berval *pw, ber_len_t len )
309 if( len < 1 ) return -1;
312 pw->bv_val = ber_memalloc( len + 1 );
314 if( pw->bv_val == NULL ) {
318 if( lutil_entropy( (unsigned char *) pw->bv_val, pw->bv_len) < 0 ) {
322 for( len = 0; len < pw->bv_len; len++ ) {
323 pw->bv_val[len] = crypt64[
324 pw->bv_val[len] % (sizeof(crypt64)-1) ];
327 pw->bv_val[len] = '\0';
332 int lutil_passwd_hash(
333 const struct berval * passwd,
338 const struct pw_scheme *sc = get_scheme( method );
344 if( text ) *text = "scheme not recognized";
348 if( ! sc->hash_fn ) {
349 if( text ) *text = "scheme provided no hash function";
353 if( text ) *text = NULL;
355 return (sc->hash_fn)( &sc->name, passwd, hash, text );
358 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
359 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
360 static int pw_string(
361 const struct berval *sc,
362 struct berval *passwd )
366 pw.bv_len = sc->bv_len + passwd->bv_len;
367 pw.bv_val = ber_memalloc( pw.bv_len + 1 );
369 if( pw.bv_val == NULL ) {
370 return LUTIL_PASSWD_ERR;
373 AC_MEMCPY( pw.bv_val, sc->bv_val, sc->bv_len );
374 AC_MEMCPY( &pw.bv_val[sc->bv_len], passwd->bv_val, passwd->bv_len );
376 pw.bv_val[pw.bv_len] = '\0';
379 return LUTIL_PASSWD_OK;
381 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
383 static int pw_string64(
384 const struct berval *sc,
385 const struct berval *hash,
387 const struct berval *salt )
390 struct berval string;
394 /* need to base64 combined string */
395 string.bv_len = hash->bv_len + salt->bv_len;
396 string.bv_val = ber_memalloc( string.bv_len + 1 );
398 if( string.bv_val == NULL ) {
399 return LUTIL_PASSWD_ERR;
402 AC_MEMCPY( string.bv_val, hash->bv_val,
404 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
406 string.bv_val[string.bv_len] = '\0';
412 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
413 b64->bv_len = b64len + sc->bv_len;
414 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
416 if( b64->bv_val == NULL ) {
417 if( salt ) ber_memfree( string.bv_val );
418 return LUTIL_PASSWD_ERR;
421 AC_MEMCPY(b64->bv_val, sc->bv_val, sc->bv_len);
424 (unsigned char *) string.bv_val, string.bv_len,
425 &b64->bv_val[sc->bv_len], b64len );
427 if( salt ) ber_memfree( string.bv_val );
430 return LUTIL_PASSWD_ERR;
433 /* recompute length */
434 b64->bv_len = sc->bv_len + rc;
435 assert( strlen(b64->bv_val) == b64->bv_len );
436 return LUTIL_PASSWD_OK;
439 /* PASSWORD CHECK ROUTINES */
441 #ifdef LUTIL_SHA1_BYTES
442 static int chk_ssha1(
443 const struct berval *sc,
444 const struct berval * passwd,
445 const struct berval * cred,
448 lutil_SHA1_CTX SHA1context;
449 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
451 unsigned char *orig_pass = NULL;
453 /* safety check -- must have some salt */
454 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(SHA1digest)) {
455 return LUTIL_PASSWD_ERR;
458 /* decode base64 password */
459 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
460 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
462 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
464 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
466 /* safety check -- must have some salt */
467 if (rc <= (int)(sizeof(SHA1digest))) {
468 ber_memfree(orig_pass);
469 return LUTIL_PASSWD_ERR;
472 /* hash credentials with salt */
473 lutil_SHA1Init(&SHA1context);
474 lutil_SHA1Update(&SHA1context,
475 (const unsigned char *) cred->bv_val, cred->bv_len);
476 lutil_SHA1Update(&SHA1context,
477 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
478 rc - sizeof(SHA1digest));
479 lutil_SHA1Final(SHA1digest, &SHA1context);
482 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
483 ber_memfree(orig_pass);
484 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
488 const struct berval *sc,
489 const struct berval * passwd,
490 const struct berval * cred,
493 lutil_SHA1_CTX SHA1context;
494 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
496 unsigned char *orig_pass = NULL;
499 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(SHA1digest)) {
500 return LUTIL_PASSWD_ERR;
503 /* base64 un-encode password */
504 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
505 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
507 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
509 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
511 if( rc != sizeof(SHA1digest) ) {
512 ber_memfree(orig_pass);
513 return LUTIL_PASSWD_ERR;
516 /* hash credentials with salt */
517 lutil_SHA1Init(&SHA1context);
518 lutil_SHA1Update(&SHA1context,
519 (const unsigned char *) cred->bv_val, cred->bv_len);
520 lutil_SHA1Final(SHA1digest, &SHA1context);
523 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
524 ber_memfree(orig_pass);
525 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
530 const struct berval *sc,
531 const struct berval * passwd,
532 const struct berval * cred,
535 lutil_MD5_CTX MD5context;
536 unsigned char MD5digest[LUTIL_MD5_BYTES];
538 unsigned char *orig_pass = NULL;
541 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(MD5digest)) {
542 return LUTIL_PASSWD_ERR;
545 /* base64 un-encode password */
546 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
547 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
549 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
551 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
553 if (rc <= (int)(sizeof(MD5digest))) {
554 ber_memfree(orig_pass);
555 return LUTIL_PASSWD_ERR;
558 /* hash credentials with salt */
559 lutil_MD5Init(&MD5context);
560 lutil_MD5Update(&MD5context,
561 (const unsigned char *) cred->bv_val,
563 lutil_MD5Update(&MD5context,
564 &orig_pass[sizeof(MD5digest)],
565 rc - sizeof(MD5digest));
566 lutil_MD5Final(MD5digest, &MD5context);
569 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
570 ber_memfree(orig_pass);
571 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
575 const struct berval *sc,
576 const struct berval * passwd,
577 const struct berval * cred,
580 lutil_MD5_CTX MD5context;
581 unsigned char MD5digest[LUTIL_MD5_BYTES];
583 unsigned char *orig_pass = NULL;
586 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(MD5digest)) {
587 return LUTIL_PASSWD_ERR;
590 /* base64 un-encode password */
591 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
592 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
594 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
596 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
597 if ( rc != sizeof(MD5digest) ) {
598 ber_memfree(orig_pass);
599 return LUTIL_PASSWD_ERR;
602 /* hash credentials with salt */
603 lutil_MD5Init(&MD5context);
604 lutil_MD5Update(&MD5context,
605 (const unsigned char *) cred->bv_val,
607 lutil_MD5Final(MD5digest, &MD5context);
610 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
611 ber_memfree(orig_pass);
612 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
616 /* pseudocode from RFC2433
617 * A.2 LmPasswordHash()
620 * IN 0-to-14-oem-char Password,
621 * OUT 16-octet PasswordHash )
623 * Set UcasePassword to the uppercased Password
624 * Zero pad UcasePassword to 14 characters
626 * DesHash( 1st 7-octets of UcasePassword,
627 * giving 1st 8-octets of PasswordHash )
629 * DesHash( 2nd 7-octets of UcasePassword,
630 * giving 2nd 8-octets of PasswordHash )
638 * OUT 8-octet Cypher )
641 * * Make Cypher an irreversibly encrypted form of Clear by
642 * * encrypting known text using Clear as the secret key.
643 * * The known text consists of the string
648 * Set StdText to "KGS!@#$%"
649 * DesEncrypt( StdText, Clear, giving Cypher )
658 * OUT 8-octet Cypher )
661 * * Use the DES encryption algorithm [4] in ECB mode [9]
662 * * to encrypt Clear into Cypher such that Cypher can
663 * * only be decrypted back to Clear by providing Key.
664 * * Note that the DES algorithm takes as input a 64-bit
665 * * stream where the 8th, 16th, 24th, etc. bits are
666 * * parity bits ignored by the encrypting algorithm.
667 * * Unless you write your own DES to accept 56-bit input
668 * * without parity, you will need to insert the parity bits
674 static void lmPasswd_to_key(
675 const unsigned char *lmPasswd,
678 /* make room for parity bits */
679 ((char *)key)[0] = lmPasswd[0];
680 ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
681 ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
682 ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
683 ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
684 ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
685 ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
686 ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
688 des_set_odd_parity( key );
691 static int chk_lanman(
692 const struct berval *scheme,
693 const struct berval *passwd,
694 const struct berval *cred,
698 char UcasePassword[15];
700 des_key_schedule schedule;
701 des_cblock StdText = "KGS!@#$%";
702 des_cblock PasswordHash1, PasswordHash2;
703 char PasswordHash[33], storedPasswordHash[33];
705 for( i=0; i<cred->bv_len; i++) {
706 if(cred->bv_val[i] == '\0') {
707 return LUTIL_PASSWD_ERR; /* NUL character in password */
711 if( cred->bv_val[i] != '\0' ) {
712 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
715 strncpy( UcasePassword, cred->bv_val, 14 );
716 UcasePassword[14] = '\0';
717 ldap_pvt_str2upper( UcasePassword );
719 lmPasswd_to_key( UcasePassword, &key );
720 des_set_key_unchecked( &key, schedule );
721 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
723 lmPasswd_to_key( &UcasePassword[7], &key );
724 des_set_key_unchecked( &key, schedule );
725 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
727 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
728 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
729 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
730 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
731 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
733 /* as a precaution convert stored password hash to lower case */
734 strncpy( storedPasswordHash, passwd->bv_val, 32 );
735 storedPasswordHash[32] = '\0';
736 ldap_pvt_str2lower( storedPasswordHash );
738 return memcmp( PasswordHash, storedPasswordHash, 32) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
740 #endif /* SLAPD_LMHASH */
743 static int lutil_crypt(
748 char *cr = crypt( key, salt );
751 if( cr == NULL || cr[0] == '\0' ) {
752 /* salt must have been invalid */
753 rc = LUTIL_PASSWD_ERR;
756 *hash = ber_strdup( cr );
757 rc = LUTIL_PASSWD_OK;
759 rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
765 static int chk_crypt(
766 const struct berval *sc,
767 const struct berval * passwd,
768 const struct berval * cred,
775 for( i=0; i<cred->bv_len; i++) {
776 if(cred->bv_val[i] == '\0') {
777 return LUTIL_PASSWD_ERR; /* NUL character in password */
781 if( cred->bv_val[i] != '\0' ) {
782 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
785 if( passwd->bv_len < 2 ) {
786 return LUTIL_PASSWD_ERR; /* passwd must be at least two characters long */
789 for( i=0; i<passwd->bv_len; i++) {
790 if(passwd->bv_val[i] == '\0') {
791 return LUTIL_PASSWD_ERR; /* NUL character in password */
795 if( passwd->bv_val[i] != '\0' ) {
796 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
799 return lutil_cryptptr( cred->bv_val, passwd->bv_val, NULL );
802 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
804 const struct berval *sc,
805 const struct berval * passwd,
806 const struct berval * cred,
812 for( i=0; i<cred->bv_len; i++) {
813 if(cred->bv_val[i] == '\0') {
814 return LUTIL_PASSWD_ERR; /* NUL character in password */
817 if( cred->bv_val[i] != '\0' ) {
818 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
821 for( i=0; i<passwd->bv_len; i++) {
822 if(passwd->bv_val[i] == '\0') {
823 return LUTIL_PASSWD_ERR; /* NUL character in password */
827 if( passwd->bv_val[i] != '\0' ) {
828 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
832 struct passwd *pwd = getpwnam(passwd->bv_val);
835 return LUTIL_PASSWD_ERR; /* not found */
840 # ifdef HAVE_GETSPNAM
842 struct spwd *spwd = getspnam(passwd->bv_val);
849 # ifdef HAVE_AIX_SECURITY
851 struct userpw *upw = getuserpw(passwd->bv_val);
854 pw = upw->upw_passwd;
859 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
860 /* password must must be at least two characters long */
861 return LUTIL_PASSWD_ERR;
864 return lutil_cryptptr( cred->bv_val, pw, NULL );
869 /* PASSWORD GENERATION ROUTINES */
871 #ifdef LUTIL_SHA1_BYTES
872 static int hash_ssha1(
873 const struct berval *scheme,
874 const struct berval *passwd,
878 lutil_SHA1_CTX SHA1context;
879 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
880 char saltdata[SALT_SIZE];
881 struct berval digest;
884 digest.bv_val = (char *) SHA1digest;
885 digest.bv_len = sizeof(SHA1digest);
886 salt.bv_val = saltdata;
887 salt.bv_len = sizeof(saltdata);
889 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
890 return LUTIL_PASSWD_ERR;
893 lutil_SHA1Init( &SHA1context );
894 lutil_SHA1Update( &SHA1context,
895 (const unsigned char *)passwd->bv_val, passwd->bv_len );
896 lutil_SHA1Update( &SHA1context,
897 (const unsigned char *)salt.bv_val, salt.bv_len );
898 lutil_SHA1Final( SHA1digest, &SHA1context );
900 return pw_string64( scheme, &digest, hash, &salt);
903 static int hash_sha1(
904 const struct berval *scheme,
905 const struct berval *passwd,
909 lutil_SHA1_CTX SHA1context;
910 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
911 struct berval digest;
912 digest.bv_val = (char *) SHA1digest;
913 digest.bv_len = sizeof(SHA1digest);
915 lutil_SHA1Init( &SHA1context );
916 lutil_SHA1Update( &SHA1context,
917 (const unsigned char *)passwd->bv_val, passwd->bv_len );
918 lutil_SHA1Final( SHA1digest, &SHA1context );
920 return pw_string64( scheme, &digest, hash, NULL);
924 static int hash_smd5(
925 const struct berval *scheme,
926 const struct berval *passwd,
930 lutil_MD5_CTX MD5context;
931 unsigned char MD5digest[LUTIL_MD5_BYTES];
932 char saltdata[SALT_SIZE];
933 struct berval digest;
936 digest.bv_val = (char *) MD5digest;
937 digest.bv_len = sizeof(MD5digest);
938 salt.bv_val = saltdata;
939 salt.bv_len = sizeof(saltdata);
941 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
942 return LUTIL_PASSWD_ERR;
945 lutil_MD5Init( &MD5context );
946 lutil_MD5Update( &MD5context,
947 (const unsigned char *) passwd->bv_val, passwd->bv_len );
948 lutil_MD5Update( &MD5context,
949 (const unsigned char *) salt.bv_val, salt.bv_len );
950 lutil_MD5Final( MD5digest, &MD5context );
952 return pw_string64( scheme, &digest, hash, &salt );
956 const struct berval *scheme,
957 const struct berval *passwd,
961 lutil_MD5_CTX MD5context;
962 unsigned char MD5digest[LUTIL_MD5_BYTES];
964 struct berval digest;
966 digest.bv_val = (char *) MD5digest;
967 digest.bv_len = sizeof(MD5digest);
969 lutil_MD5Init( &MD5context );
970 lutil_MD5Update( &MD5context,
971 (const unsigned char *) passwd->bv_val, passwd->bv_len );
972 lutil_MD5Final( MD5digest, &MD5context );
974 return pw_string64( scheme, &digest, hash, NULL );
979 static int hash_lanman(
980 const struct berval *scheme,
981 const struct berval *passwd,
987 char UcasePassword[15];
989 des_key_schedule schedule;
990 des_cblock StdText = "KGS!@#$%";
991 des_cblock PasswordHash1, PasswordHash2;
992 char PasswordHash[33];
994 for( i=0; i<passwd->bv_len; i++) {
995 if(passwd->bv_val[i] == '\0') {
996 return LUTIL_PASSWD_ERR; /* NUL character in password */
1000 if( passwd->bv_val[i] != '\0' ) {
1001 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
1004 strncpy( UcasePassword, passwd->bv_val, 14 );
1005 UcasePassword[14] = '\0';
1006 ldap_pvt_str2upper( UcasePassword );
1008 lmPasswd_to_key( UcasePassword, &key );
1009 des_set_key_unchecked( &key, schedule );
1010 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
1012 lmPasswd_to_key( &UcasePassword[7], &key );
1013 des_set_key_unchecked( &key, schedule );
1014 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
1016 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1017 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
1018 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
1019 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
1020 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
1022 hash->bv_val = PasswordHash;
1025 return pw_string( scheme, hash );
1027 #endif /* SLAPD_LMHASH */
1030 static int hash_crypt(
1031 const struct berval *scheme,
1032 const struct berval *passwd,
1033 struct berval *hash,
1036 unsigned char salt[32]; /* salt suitable for most anything */
1041 for( i=0; i<passwd->bv_len; i++) {
1042 if(passwd->bv_val[i] == '\0') {
1043 return LUTIL_PASSWD_ERR; /* NUL character in password */
1047 if( passwd->bv_val[i] != '\0' ) {
1048 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
1051 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1052 return LUTIL_PASSWD_ERR;
1055 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1056 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1058 salt[sizeof( salt ) - 1 ] = '\0';
1060 if( salt_format != NULL ) {
1061 /* copy the salt we made into entropy before snprintfing
1062 it back into the salt */
1063 char entropy[sizeof(salt)];
1064 strcpy( entropy, (char *) salt );
1065 snprintf( (char *) salt, sizeof(entropy), salt_format, entropy );
1068 rc = lutil_cryptptr( passwd->bv_val, (char *) salt, &hash->bv_val );
1069 if ( rc != LUTIL_PASSWD_OK ) return rc;
1071 if( hash->bv_val == NULL ) return -1;
1073 hash->bv_len = strlen( hash->bv_val );
1075 save = hash->bv_val;
1077 if( hash->bv_len == 0 ) {
1078 rc = LUTIL_PASSWD_ERR;
1080 rc = pw_string( scheme, hash );
1082 ber_memfree( save );
1087 int lutil_salt_format(const char *format)
1090 free( salt_format );
1092 salt_format = format != NULL ? ber_strdup( format ) : NULL;
1098 #ifdef SLAPD_CLEARTEXT
1099 static int hash_clear(
1100 const struct berval *scheme,
1101 const struct berval *passwd,
1102 struct berval *hash,
1105 ber_dupbv( hash, (struct berval *)passwd );
1106 return LUTIL_PASSWD_OK;