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;
202 if (!pw_inited) lutil_passwd_init();
204 bv.bv_val = strchr( scheme, '}' );
208 bv.bv_len = bv.bv_val - scheme + 1;
209 bv.bv_val = (char *) scheme;
211 for( pws=pw_schemes; pws; pws=pws->next ) {
212 if( bv.bv_len != pws->s.name.bv_len )
214 if( strncasecmp(bv.bv_val, pws->s.name.bv_val, bv.bv_len ) == 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_inited) 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 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 ) {
380 return LUTIL_PASSWD_ERR;
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';
389 return LUTIL_PASSWD_OK;
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 ) {
409 return LUTIL_PASSWD_ERR;
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 );
428 return LUTIL_PASSWD_ERR;
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 );
440 return LUTIL_PASSWD_ERR;
443 /* recompute length */
444 b64->bv_len = sc->bv_len + rc;
445 assert( strlen(b64->bv_val) == b64->bv_len );
446 return LUTIL_PASSWD_OK;
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;
463 /* safety check -- must have some salt */
464 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(SHA1digest)) {
465 return LUTIL_PASSWD_ERR;
468 /* decode base64 password */
469 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
470 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
472 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
474 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
476 /* safety check -- must have some salt */
477 if (rc <= (int)(sizeof(SHA1digest))) {
478 ber_memfree(orig_pass);
479 return LUTIL_PASSWD_ERR;
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);
494 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
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)) {
510 return LUTIL_PASSWD_ERR;
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 LUTIL_PASSWD_ERR;
519 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
521 if( rc != sizeof(SHA1digest) ) {
522 ber_memfree(orig_pass);
523 return LUTIL_PASSWD_ERR;
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);
535 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
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) <= sizeof(MD5digest)) {
552 return LUTIL_PASSWD_ERR;
555 /* base64 un-encode password */
556 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
557 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
559 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
561 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
563 if (rc <= (int)(sizeof(MD5digest))) {
564 ber_memfree(orig_pass);
565 return LUTIL_PASSWD_ERR;
568 /* hash credentials with salt */
569 lutil_MD5Init(&MD5context);
570 lutil_MD5Update(&MD5context,
571 (const unsigned char *) cred->bv_val,
573 lutil_MD5Update(&MD5context,
574 &orig_pass[sizeof(MD5digest)],
575 rc - sizeof(MD5digest));
576 lutil_MD5Final(MD5digest, &MD5context);
579 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
580 ber_memfree(orig_pass);
581 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
585 const struct berval *sc,
586 const struct berval * passwd,
587 const struct berval * cred,
590 lutil_MD5_CTX MD5context;
591 unsigned char MD5digest[LUTIL_MD5_BYTES];
593 unsigned char *orig_pass = NULL;
596 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(MD5digest)) {
597 return LUTIL_PASSWD_ERR;
600 /* base64 un-encode password */
601 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
602 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
604 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
606 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
607 if ( rc != sizeof(MD5digest) ) {
608 ber_memfree(orig_pass);
609 return LUTIL_PASSWD_ERR;
612 /* hash credentials with salt */
613 lutil_MD5Init(&MD5context);
614 lutil_MD5Update(&MD5context,
615 (const unsigned char *) cred->bv_val,
617 lutil_MD5Final(MD5digest, &MD5context);
620 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
621 ber_memfree(orig_pass);
622 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
626 /* pseudocode from RFC2433
627 * A.2 LmPasswordHash()
630 * IN 0-to-14-oem-char Password,
631 * OUT 16-octet PasswordHash )
633 * Set UcasePassword to the uppercased Password
634 * Zero pad UcasePassword to 14 characters
636 * DesHash( 1st 7-octets of UcasePassword,
637 * giving 1st 8-octets of PasswordHash )
639 * DesHash( 2nd 7-octets of UcasePassword,
640 * giving 2nd 8-octets of PasswordHash )
648 * OUT 8-octet Cypher )
651 * * Make Cypher an irreversibly encrypted form of Clear by
652 * * encrypting known text using Clear as the secret key.
653 * * The known text consists of the string
658 * Set StdText to "KGS!@#$%"
659 * DesEncrypt( StdText, Clear, giving Cypher )
668 * OUT 8-octet Cypher )
671 * * Use the DES encryption algorithm [4] in ECB mode [9]
672 * * to encrypt Clear into Cypher such that Cypher can
673 * * only be decrypted back to Clear by providing Key.
674 * * Note that the DES algorithm takes as input a 64-bit
675 * * stream where the 8th, 16th, 24th, etc. bits are
676 * * parity bits ignored by the encrypting algorithm.
677 * * Unless you write your own DES to accept 56-bit input
678 * * without parity, you will need to insert the parity bits
684 static void lmPasswd_to_key(
685 const char *lmPasswd,
688 const unsigned char *lpw = (const unsigned char *) lmPasswd;
689 unsigned char *k = (unsigned char *) key;
691 /* make room for parity bits */
693 k[1] = ((lpw[0] & 0x01) << 7) | (lpw[1] >> 1);
694 k[2] = ((lpw[1] & 0x03) << 6) | (lpw[2] >> 2);
695 k[3] = ((lpw[2] & 0x07) << 5) | (lpw[3] >> 3);
696 k[4] = ((lpw[3] & 0x0F) << 4) | (lpw[4] >> 4);
697 k[5] = ((lpw[4] & 0x1F) << 3) | (lpw[5] >> 5);
698 k[6] = ((lpw[5] & 0x3F) << 2) | (lpw[6] >> 6);
699 k[7] = ((lpw[6] & 0x7F) << 1);
701 des_set_odd_parity( key );
704 static int chk_lanman(
705 const struct berval *scheme,
706 const struct berval *passwd,
707 const struct berval *cred,
711 char UcasePassword[15];
713 des_key_schedule schedule;
714 des_cblock StdText = "KGS!@#$%";
715 des_cblock PasswordHash1, PasswordHash2;
716 char PasswordHash[33], storedPasswordHash[33];
718 for( i=0; i<cred->bv_len; i++) {
719 if(cred->bv_val[i] == '\0') {
720 return LUTIL_PASSWD_ERR; /* NUL character in password */
724 if( cred->bv_val[i] != '\0' ) {
725 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
728 strncpy( UcasePassword, cred->bv_val, 14 );
729 UcasePassword[14] = '\0';
730 ldap_pvt_str2upper( UcasePassword );
732 lmPasswd_to_key( UcasePassword, &key );
733 des_set_key_unchecked( &key, schedule );
734 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
736 lmPasswd_to_key( &UcasePassword[7], &key );
737 des_set_key_unchecked( &key, schedule );
738 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
740 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
741 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
742 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
743 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
744 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
746 /* as a precaution convert stored password hash to lower case */
747 strncpy( storedPasswordHash, passwd->bv_val, 32 );
748 storedPasswordHash[32] = '\0';
749 ldap_pvt_str2lower( storedPasswordHash );
751 return memcmp( PasswordHash, storedPasswordHash, 32) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
753 #endif /* SLAPD_LMHASH */
756 static int lutil_crypt(
761 char *cr = crypt( key, salt );
764 if( cr == NULL || cr[0] == '\0' ) {
765 /* salt must have been invalid */
766 rc = LUTIL_PASSWD_ERR;
769 *hash = ber_strdup( cr );
770 rc = LUTIL_PASSWD_OK;
772 rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
778 static int chk_crypt(
779 const struct berval *sc,
780 const struct berval * passwd,
781 const struct berval * cred,
786 for( i=0; i<cred->bv_len; i++) {
787 if(cred->bv_val[i] == '\0') {
788 return LUTIL_PASSWD_ERR; /* NUL character in password */
792 if( cred->bv_val[i] != '\0' ) {
793 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
796 if( passwd->bv_len < 2 ) {
797 return LUTIL_PASSWD_ERR; /* passwd must be at least two characters long */
800 for( i=0; i<passwd->bv_len; i++) {
801 if(passwd->bv_val[i] == '\0') {
802 return LUTIL_PASSWD_ERR; /* NUL character in password */
806 if( passwd->bv_val[i] != '\0' ) {
807 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
810 return lutil_cryptptr( cred->bv_val, passwd->bv_val, NULL );
813 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
815 const struct berval *sc,
816 const struct berval * passwd,
817 const struct berval * cred,
823 for( i=0; i<cred->bv_len; i++) {
824 if(cred->bv_val[i] == '\0') {
825 return LUTIL_PASSWD_ERR; /* NUL character in password */
828 if( cred->bv_val[i] != '\0' ) {
829 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
832 for( i=0; i<passwd->bv_len; i++) {
833 if(passwd->bv_val[i] == '\0') {
834 return LUTIL_PASSWD_ERR; /* NUL character in password */
838 if( passwd->bv_val[i] != '\0' ) {
839 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
843 struct passwd *pwd = getpwnam(passwd->bv_val);
846 return LUTIL_PASSWD_ERR; /* not found */
851 # ifdef HAVE_GETSPNAM
853 struct spwd *spwd = getspnam(passwd->bv_val);
860 # ifdef HAVE_AIX_SECURITY
862 struct userpw *upw = getuserpw(passwd->bv_val);
865 pw = upw->upw_passwd;
870 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
871 /* password must must be at least two characters long */
872 return LUTIL_PASSWD_ERR;
875 return lutil_cryptptr( cred->bv_val, pw, NULL );
880 /* PASSWORD GENERATION ROUTINES */
882 #ifdef LUTIL_SHA1_BYTES
883 static int hash_ssha1(
884 const struct berval *scheme,
885 const struct berval *passwd,
889 lutil_SHA1_CTX SHA1context;
890 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
891 char saltdata[SALT_SIZE];
892 struct berval digest;
895 digest.bv_val = (char *) SHA1digest;
896 digest.bv_len = sizeof(SHA1digest);
897 salt.bv_val = saltdata;
898 salt.bv_len = sizeof(saltdata);
900 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
901 return LUTIL_PASSWD_ERR;
904 lutil_SHA1Init( &SHA1context );
905 lutil_SHA1Update( &SHA1context,
906 (const unsigned char *)passwd->bv_val, passwd->bv_len );
907 lutil_SHA1Update( &SHA1context,
908 (const unsigned char *)salt.bv_val, salt.bv_len );
909 lutil_SHA1Final( SHA1digest, &SHA1context );
911 return pw_string64( scheme, &digest, hash, &salt);
914 static int hash_sha1(
915 const struct berval *scheme,
916 const struct berval *passwd,
920 lutil_SHA1_CTX SHA1context;
921 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
922 struct berval digest;
923 digest.bv_val = (char *) SHA1digest;
924 digest.bv_len = sizeof(SHA1digest);
926 lutil_SHA1Init( &SHA1context );
927 lutil_SHA1Update( &SHA1context,
928 (const unsigned char *)passwd->bv_val, passwd->bv_len );
929 lutil_SHA1Final( SHA1digest, &SHA1context );
931 return pw_string64( scheme, &digest, hash, NULL);
935 static int hash_smd5(
936 const struct berval *scheme,
937 const struct berval *passwd,
941 lutil_MD5_CTX MD5context;
942 unsigned char MD5digest[LUTIL_MD5_BYTES];
943 char saltdata[SALT_SIZE];
944 struct berval digest;
947 digest.bv_val = (char *) MD5digest;
948 digest.bv_len = sizeof(MD5digest);
949 salt.bv_val = saltdata;
950 salt.bv_len = sizeof(saltdata);
952 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
953 return LUTIL_PASSWD_ERR;
956 lutil_MD5Init( &MD5context );
957 lutil_MD5Update( &MD5context,
958 (const unsigned char *) passwd->bv_val, passwd->bv_len );
959 lutil_MD5Update( &MD5context,
960 (const unsigned char *) salt.bv_val, salt.bv_len );
961 lutil_MD5Final( MD5digest, &MD5context );
963 return pw_string64( scheme, &digest, hash, &salt );
967 const struct berval *scheme,
968 const struct berval *passwd,
972 lutil_MD5_CTX MD5context;
973 unsigned char MD5digest[LUTIL_MD5_BYTES];
975 struct berval digest;
977 digest.bv_val = (char *) MD5digest;
978 digest.bv_len = sizeof(MD5digest);
980 lutil_MD5Init( &MD5context );
981 lutil_MD5Update( &MD5context,
982 (const unsigned char *) passwd->bv_val, passwd->bv_len );
983 lutil_MD5Final( MD5digest, &MD5context );
985 return pw_string64( scheme, &digest, hash, NULL );
990 static int hash_lanman(
991 const struct berval *scheme,
992 const struct berval *passwd,
998 char UcasePassword[15];
1000 des_key_schedule schedule;
1001 des_cblock StdText = "KGS!@#$%";
1002 des_cblock PasswordHash1, PasswordHash2;
1003 char PasswordHash[33];
1005 for( i=0; i<passwd->bv_len; i++) {
1006 if(passwd->bv_val[i] == '\0') {
1007 return LUTIL_PASSWD_ERR; /* NUL character in password */
1011 if( passwd->bv_val[i] != '\0' ) {
1012 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
1015 strncpy( UcasePassword, passwd->bv_val, 14 );
1016 UcasePassword[14] = '\0';
1017 ldap_pvt_str2upper( UcasePassword );
1019 lmPasswd_to_key( UcasePassword, &key );
1020 des_set_key_unchecked( &key, schedule );
1021 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
1023 lmPasswd_to_key( &UcasePassword[7], &key );
1024 des_set_key_unchecked( &key, schedule );
1025 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
1027 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1028 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
1029 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
1030 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
1031 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
1033 hash->bv_val = PasswordHash;
1036 return pw_string( scheme, hash );
1038 #endif /* SLAPD_LMHASH */
1041 static int hash_crypt(
1042 const struct berval *scheme,
1043 const struct berval *passwd,
1044 struct berval *hash,
1047 unsigned char salt[32]; /* salt suitable for most anything */
1052 for( i=0; i<passwd->bv_len; i++) {
1053 if(passwd->bv_val[i] == '\0') {
1054 return LUTIL_PASSWD_ERR; /* NUL character in password */
1058 if( passwd->bv_val[i] != '\0' ) {
1059 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
1062 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1063 return LUTIL_PASSWD_ERR;
1066 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1067 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1069 salt[sizeof( salt ) - 1 ] = '\0';
1071 if( salt_format != NULL ) {
1072 /* copy the salt we made into entropy before snprintfing
1073 it back into the salt */
1074 char entropy[sizeof(salt)];
1075 strcpy( entropy, (char *) salt );
1076 snprintf( (char *) salt, sizeof(entropy), salt_format, entropy );
1079 rc = lutil_cryptptr( passwd->bv_val, (char *) salt, &hash->bv_val );
1080 if ( rc != LUTIL_PASSWD_OK ) return rc;
1082 if( hash->bv_val == NULL ) return -1;
1084 hash->bv_len = strlen( hash->bv_val );
1086 save = hash->bv_val;
1088 if( hash->bv_len == 0 ) {
1089 rc = LUTIL_PASSWD_ERR;
1091 rc = pw_string( scheme, hash );
1093 ber_memfree( save );
1098 int lutil_salt_format(const char *format)
1101 free( salt_format );
1103 salt_format = format != NULL ? ber_strdup( format ) : NULL;
1109 #ifdef SLAPD_CLEARTEXT
1110 static int hash_clear(
1111 const struct berval *scheme,
1112 const struct berval *passwd,
1113 struct berval *hash,
1116 ber_dupbv( hash, (struct berval *)passwd );
1117 return LUTIL_PASSWD_OK;