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;
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_STRUCT_PASSWD_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_STRUCT_PASSWD_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 /* Do we think there is a scheme specifier here that we
307 * didn't recognize? Assume a scheme name is at least 1 character.
309 if (( passwd->bv_val[0] == '{' ) &&
310 ( strchr( passwd->bv_val, '}' ) > passwd->bv_val+1 ))
312 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
313 return ( passwd->bv_len == cred->bv_len ) ?
314 memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
321 int lutil_passwd_generate( struct berval *pw, ber_len_t len )
324 if( len < 1 ) return -1;
327 pw->bv_val = ber_memalloc( len + 1 );
329 if( pw->bv_val == NULL ) {
333 if( lutil_entropy( (unsigned char *) pw->bv_val, pw->bv_len) < 0 ) {
337 for( len = 0; len < pw->bv_len; len++ ) {
338 pw->bv_val[len] = crypt64[
339 pw->bv_val[len] % (sizeof(crypt64)-1) ];
342 pw->bv_val[len] = '\0';
347 int lutil_passwd_hash(
348 const struct berval * passwd,
353 const struct pw_scheme *sc = get_scheme( method );
359 if( text ) *text = "scheme not recognized";
363 if( ! sc->hash_fn ) {
364 if( text ) *text = "scheme provided no hash function";
368 if( text ) *text = NULL;
370 return (sc->hash_fn)( &sc->name, passwd, hash, text );
373 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
374 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
375 static int pw_string(
376 const struct berval *sc,
377 struct berval *passwd )
381 pw.bv_len = sc->bv_len + passwd->bv_len;
382 pw.bv_val = ber_memalloc( pw.bv_len + 1 );
384 if( pw.bv_val == NULL ) {
385 return LUTIL_PASSWD_ERR;
388 AC_MEMCPY( pw.bv_val, sc->bv_val, sc->bv_len );
389 AC_MEMCPY( &pw.bv_val[sc->bv_len], passwd->bv_val, passwd->bv_len );
391 pw.bv_val[pw.bv_len] = '\0';
394 return LUTIL_PASSWD_OK;
396 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
398 static int pw_string64(
399 const struct berval *sc,
400 const struct berval *hash,
402 const struct berval *salt )
405 struct berval string;
409 /* need to base64 combined string */
410 string.bv_len = hash->bv_len + salt->bv_len;
411 string.bv_val = ber_memalloc( string.bv_len + 1 );
413 if( string.bv_val == NULL ) {
414 return LUTIL_PASSWD_ERR;
417 AC_MEMCPY( string.bv_val, hash->bv_val,
419 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
421 string.bv_val[string.bv_len] = '\0';
427 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
428 b64->bv_len = b64len + sc->bv_len;
429 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
431 if( b64->bv_val == NULL ) {
432 if( salt ) ber_memfree( string.bv_val );
433 return LUTIL_PASSWD_ERR;
436 AC_MEMCPY(b64->bv_val, sc->bv_val, sc->bv_len);
439 (unsigned char *) string.bv_val, string.bv_len,
440 &b64->bv_val[sc->bv_len], b64len );
442 if( salt ) ber_memfree( string.bv_val );
445 return LUTIL_PASSWD_ERR;
448 /* recompute length */
449 b64->bv_len = sc->bv_len + rc;
450 assert( strlen(b64->bv_val) == b64->bv_len );
451 return LUTIL_PASSWD_OK;
454 /* PASSWORD CHECK ROUTINES */
456 #ifdef LUTIL_SHA1_BYTES
457 static int chk_ssha1(
458 const struct berval *sc,
459 const struct berval * passwd,
460 const struct berval * cred,
463 lutil_SHA1_CTX SHA1context;
464 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
466 unsigned char *orig_pass = NULL;
468 /* safety check -- must have some salt */
469 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(SHA1digest)) {
470 return LUTIL_PASSWD_ERR;
473 /* decode base64 password */
474 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
475 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
477 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
479 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
481 /* safety check -- must have some salt */
482 if (rc <= (int)(sizeof(SHA1digest))) {
483 ber_memfree(orig_pass);
484 return LUTIL_PASSWD_ERR;
487 /* hash credentials with salt */
488 lutil_SHA1Init(&SHA1context);
489 lutil_SHA1Update(&SHA1context,
490 (const unsigned char *) cred->bv_val, cred->bv_len);
491 lutil_SHA1Update(&SHA1context,
492 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
493 rc - sizeof(SHA1digest));
494 lutil_SHA1Final(SHA1digest, &SHA1context);
497 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
498 ber_memfree(orig_pass);
499 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
503 const struct berval *sc,
504 const struct berval * passwd,
505 const struct berval * cred,
508 lutil_SHA1_CTX SHA1context;
509 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
511 unsigned char *orig_pass = NULL;
514 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(SHA1digest)) {
515 return LUTIL_PASSWD_ERR;
518 /* base64 un-encode password */
519 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
520 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
522 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
524 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
526 if( rc != sizeof(SHA1digest) ) {
527 ber_memfree(orig_pass);
528 return LUTIL_PASSWD_ERR;
531 /* hash credentials with salt */
532 lutil_SHA1Init(&SHA1context);
533 lutil_SHA1Update(&SHA1context,
534 (const unsigned char *) cred->bv_val, cred->bv_len);
535 lutil_SHA1Final(SHA1digest, &SHA1context);
538 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
539 ber_memfree(orig_pass);
540 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
545 const struct berval *sc,
546 const struct berval * passwd,
547 const struct berval * cred,
550 lutil_MD5_CTX MD5context;
551 unsigned char MD5digest[LUTIL_MD5_BYTES];
553 unsigned char *orig_pass = NULL;
556 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(MD5digest)) {
557 return LUTIL_PASSWD_ERR;
560 /* base64 un-encode password */
561 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
562 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
564 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
566 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
568 if (rc <= (int)(sizeof(MD5digest))) {
569 ber_memfree(orig_pass);
570 return LUTIL_PASSWD_ERR;
573 /* hash credentials with salt */
574 lutil_MD5Init(&MD5context);
575 lutil_MD5Update(&MD5context,
576 (const unsigned char *) cred->bv_val,
578 lutil_MD5Update(&MD5context,
579 &orig_pass[sizeof(MD5digest)],
580 rc - sizeof(MD5digest));
581 lutil_MD5Final(MD5digest, &MD5context);
584 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
585 ber_memfree(orig_pass);
586 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
590 const struct berval *sc,
591 const struct berval * passwd,
592 const struct berval * cred,
595 lutil_MD5_CTX MD5context;
596 unsigned char MD5digest[LUTIL_MD5_BYTES];
598 unsigned char *orig_pass = NULL;
601 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(MD5digest)) {
602 return LUTIL_PASSWD_ERR;
605 /* base64 un-encode password */
606 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
607 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
609 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
611 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
612 if ( rc != sizeof(MD5digest) ) {
613 ber_memfree(orig_pass);
614 return LUTIL_PASSWD_ERR;
617 /* hash credentials with salt */
618 lutil_MD5Init(&MD5context);
619 lutil_MD5Update(&MD5context,
620 (const unsigned char *) cred->bv_val,
622 lutil_MD5Final(MD5digest, &MD5context);
625 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
626 ber_memfree(orig_pass);
627 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
631 /* pseudocode from RFC2433
632 * A.2 LmPasswordHash()
635 * IN 0-to-14-oem-char Password,
636 * OUT 16-octet PasswordHash )
638 * Set UcasePassword to the uppercased Password
639 * Zero pad UcasePassword to 14 characters
641 * DesHash( 1st 7-octets of UcasePassword,
642 * giving 1st 8-octets of PasswordHash )
644 * DesHash( 2nd 7-octets of UcasePassword,
645 * giving 2nd 8-octets of PasswordHash )
653 * OUT 8-octet Cypher )
656 * * Make Cypher an irreversibly encrypted form of Clear by
657 * * encrypting known text using Clear as the secret key.
658 * * The known text consists of the string
663 * Set StdText to "KGS!@#$%"
664 * DesEncrypt( StdText, Clear, giving Cypher )
673 * OUT 8-octet Cypher )
676 * * Use the DES encryption algorithm [4] in ECB mode [9]
677 * * to encrypt Clear into Cypher such that Cypher can
678 * * only be decrypted back to Clear by providing Key.
679 * * Note that the DES algorithm takes as input a 64-bit
680 * * stream where the 8th, 16th, 24th, etc. bits are
681 * * parity bits ignored by the encrypting algorithm.
682 * * Unless you write your own DES to accept 56-bit input
683 * * without parity, you will need to insert the parity bits
689 static void lmPasswd_to_key(
690 const char *lmPasswd,
693 const unsigned char *lpw = (const unsigned char *) lmPasswd;
694 unsigned char *k = (unsigned char *) key;
696 /* make room for parity bits */
698 k[1] = ((lpw[0] & 0x01) << 7) | (lpw[1] >> 1);
699 k[2] = ((lpw[1] & 0x03) << 6) | (lpw[2] >> 2);
700 k[3] = ((lpw[2] & 0x07) << 5) | (lpw[3] >> 3);
701 k[4] = ((lpw[3] & 0x0F) << 4) | (lpw[4] >> 4);
702 k[5] = ((lpw[4] & 0x1F) << 3) | (lpw[5] >> 5);
703 k[6] = ((lpw[5] & 0x3F) << 2) | (lpw[6] >> 6);
704 k[7] = ((lpw[6] & 0x7F) << 1);
706 des_set_odd_parity( key );
709 static int chk_lanman(
710 const struct berval *scheme,
711 const struct berval *passwd,
712 const struct berval *cred,
716 char UcasePassword[15];
718 des_key_schedule schedule;
719 des_cblock StdText = "KGS!@#$%";
720 des_cblock PasswordHash1, PasswordHash2;
721 char PasswordHash[33], storedPasswordHash[33];
723 for( i=0; i<cred->bv_len; i++) {
724 if(cred->bv_val[i] == '\0') {
725 return LUTIL_PASSWD_ERR; /* NUL character in password */
729 if( cred->bv_val[i] != '\0' ) {
730 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
733 strncpy( UcasePassword, cred->bv_val, 14 );
734 UcasePassword[14] = '\0';
735 ldap_pvt_str2upper( UcasePassword );
737 lmPasswd_to_key( UcasePassword, &key );
738 des_set_key_unchecked( &key, schedule );
739 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
741 lmPasswd_to_key( &UcasePassword[7], &key );
742 des_set_key_unchecked( &key, schedule );
743 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
745 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
746 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
747 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
748 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
749 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
751 /* as a precaution convert stored password hash to lower case */
752 strncpy( storedPasswordHash, passwd->bv_val, 32 );
753 storedPasswordHash[32] = '\0';
754 ldap_pvt_str2lower( storedPasswordHash );
756 return memcmp( PasswordHash, storedPasswordHash, 32) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
758 #endif /* SLAPD_LMHASH */
761 static int lutil_crypt(
766 char *cr = crypt( key, salt );
769 if( cr == NULL || cr[0] == '\0' ) {
770 /* salt must have been invalid */
771 rc = LUTIL_PASSWD_ERR;
774 *hash = ber_strdup( cr );
775 rc = LUTIL_PASSWD_OK;
777 rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
783 static int chk_crypt(
784 const struct berval *sc,
785 const struct berval * passwd,
786 const struct berval * cred,
791 for( i=0; i<cred->bv_len; i++) {
792 if(cred->bv_val[i] == '\0') {
793 return LUTIL_PASSWD_ERR; /* NUL character in password */
797 if( cred->bv_val[i] != '\0' ) {
798 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
801 if( passwd->bv_len < 2 ) {
802 return LUTIL_PASSWD_ERR; /* passwd must be at least two characters long */
805 for( i=0; i<passwd->bv_len; i++) {
806 if(passwd->bv_val[i] == '\0') {
807 return LUTIL_PASSWD_ERR; /* NUL character in password */
811 if( passwd->bv_val[i] != '\0' ) {
812 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
815 return lutil_cryptptr( cred->bv_val, passwd->bv_val, NULL );
818 # if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
820 const struct berval *sc,
821 const struct berval * passwd,
822 const struct berval * cred,
828 for( i=0; i<cred->bv_len; i++) {
829 if(cred->bv_val[i] == '\0') {
830 return LUTIL_PASSWD_ERR; /* NUL character in password */
833 if( cred->bv_val[i] != '\0' ) {
834 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
837 for( i=0; i<passwd->bv_len; i++) {
838 if(passwd->bv_val[i] == '\0') {
839 return LUTIL_PASSWD_ERR; /* NUL character in password */
843 if( passwd->bv_val[i] != '\0' ) {
844 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
848 struct passwd *pwd = getpwnam(passwd->bv_val);
851 return LUTIL_PASSWD_ERR; /* not found */
856 # ifdef HAVE_GETSPNAM
858 struct spwd *spwd = getspnam(passwd->bv_val);
865 # ifdef HAVE_AIX_SECURITY
867 struct userpw *upw = getuserpw(passwd->bv_val);
870 pw = upw->upw_passwd;
875 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
876 /* password must must be at least two characters long */
877 return LUTIL_PASSWD_ERR;
880 return lutil_cryptptr( cred->bv_val, pw, NULL );
885 /* PASSWORD GENERATION ROUTINES */
887 #ifdef LUTIL_SHA1_BYTES
888 static int hash_ssha1(
889 const struct berval *scheme,
890 const struct berval *passwd,
894 lutil_SHA1_CTX SHA1context;
895 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
896 char saltdata[SALT_SIZE];
897 struct berval digest;
900 digest.bv_val = (char *) SHA1digest;
901 digest.bv_len = sizeof(SHA1digest);
902 salt.bv_val = saltdata;
903 salt.bv_len = sizeof(saltdata);
905 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
906 return LUTIL_PASSWD_ERR;
909 lutil_SHA1Init( &SHA1context );
910 lutil_SHA1Update( &SHA1context,
911 (const unsigned char *)passwd->bv_val, passwd->bv_len );
912 lutil_SHA1Update( &SHA1context,
913 (const unsigned char *)salt.bv_val, salt.bv_len );
914 lutil_SHA1Final( SHA1digest, &SHA1context );
916 return pw_string64( scheme, &digest, hash, &salt);
919 static int hash_sha1(
920 const struct berval *scheme,
921 const struct berval *passwd,
925 lutil_SHA1_CTX SHA1context;
926 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
927 struct berval digest;
928 digest.bv_val = (char *) SHA1digest;
929 digest.bv_len = sizeof(SHA1digest);
931 lutil_SHA1Init( &SHA1context );
932 lutil_SHA1Update( &SHA1context,
933 (const unsigned char *)passwd->bv_val, passwd->bv_len );
934 lutil_SHA1Final( SHA1digest, &SHA1context );
936 return pw_string64( scheme, &digest, hash, NULL);
940 static int hash_smd5(
941 const struct berval *scheme,
942 const struct berval *passwd,
946 lutil_MD5_CTX MD5context;
947 unsigned char MD5digest[LUTIL_MD5_BYTES];
948 char saltdata[SALT_SIZE];
949 struct berval digest;
952 digest.bv_val = (char *) MD5digest;
953 digest.bv_len = sizeof(MD5digest);
954 salt.bv_val = saltdata;
955 salt.bv_len = sizeof(saltdata);
957 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
958 return LUTIL_PASSWD_ERR;
961 lutil_MD5Init( &MD5context );
962 lutil_MD5Update( &MD5context,
963 (const unsigned char *) passwd->bv_val, passwd->bv_len );
964 lutil_MD5Update( &MD5context,
965 (const unsigned char *) salt.bv_val, salt.bv_len );
966 lutil_MD5Final( MD5digest, &MD5context );
968 return pw_string64( scheme, &digest, hash, &salt );
972 const struct berval *scheme,
973 const struct berval *passwd,
977 lutil_MD5_CTX MD5context;
978 unsigned char MD5digest[LUTIL_MD5_BYTES];
980 struct berval digest;
982 digest.bv_val = (char *) MD5digest;
983 digest.bv_len = sizeof(MD5digest);
985 lutil_MD5Init( &MD5context );
986 lutil_MD5Update( &MD5context,
987 (const unsigned char *) passwd->bv_val, passwd->bv_len );
988 lutil_MD5Final( MD5digest, &MD5context );
990 return pw_string64( scheme, &digest, hash, NULL );
995 static int hash_lanman(
996 const struct berval *scheme,
997 const struct berval *passwd,
1003 char UcasePassword[15];
1005 des_key_schedule schedule;
1006 des_cblock StdText = "KGS!@#$%";
1007 des_cblock PasswordHash1, PasswordHash2;
1008 char PasswordHash[33];
1010 for( i=0; i<passwd->bv_len; i++) {
1011 if(passwd->bv_val[i] == '\0') {
1012 return LUTIL_PASSWD_ERR; /* NUL character in password */
1016 if( passwd->bv_val[i] != '\0' ) {
1017 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
1020 strncpy( UcasePassword, passwd->bv_val, 14 );
1021 UcasePassword[14] = '\0';
1022 ldap_pvt_str2upper( UcasePassword );
1024 lmPasswd_to_key( UcasePassword, &key );
1025 des_set_key_unchecked( &key, schedule );
1026 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
1028 lmPasswd_to_key( &UcasePassword[7], &key );
1029 des_set_key_unchecked( &key, schedule );
1030 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
1032 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1033 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
1034 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
1035 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
1036 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
1038 hash->bv_val = PasswordHash;
1041 return pw_string( scheme, hash );
1043 #endif /* SLAPD_LMHASH */
1046 static int hash_crypt(
1047 const struct berval *scheme,
1048 const struct berval *passwd,
1049 struct berval *hash,
1052 unsigned char salt[32]; /* salt suitable for most anything */
1057 for( i=0; i<passwd->bv_len; i++) {
1058 if(passwd->bv_val[i] == '\0') {
1059 return LUTIL_PASSWD_ERR; /* NUL character in password */
1063 if( passwd->bv_val[i] != '\0' ) {
1064 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
1067 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1068 return LUTIL_PASSWD_ERR;
1071 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1072 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1074 salt[sizeof( salt ) - 1 ] = '\0';
1076 if( salt_format != NULL ) {
1077 /* copy the salt we made into entropy before snprintfing
1078 it back into the salt */
1079 char entropy[sizeof(salt)];
1080 strcpy( entropy, (char *) salt );
1081 snprintf( (char *) salt, sizeof(entropy), salt_format, entropy );
1084 rc = lutil_cryptptr( passwd->bv_val, (char *) salt, &hash->bv_val );
1085 if ( rc != LUTIL_PASSWD_OK ) return rc;
1087 if( hash->bv_val == NULL ) return -1;
1089 hash->bv_len = strlen( hash->bv_val );
1091 save = hash->bv_val;
1093 if( hash->bv_len == 0 ) {
1094 rc = LUTIL_PASSWD_ERR;
1096 rc = pw_string( scheme, hash );
1098 ber_memfree( save );
1103 int lutil_salt_format(const char *format)
1106 free( salt_format );
1108 salt_format = format != NULL ? ber_strdup( format ) : NULL;
1114 #ifdef SLAPD_CLEARTEXT
1115 static int hash_clear(
1116 const struct berval *scheme,
1117 const struct berval *passwd,
1118 struct berval *hash,
1121 ber_dupbv( hash, (struct berval *)passwd );
1122 return LUTIL_PASSWD_OK;