2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2009 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 #if defined(HAVE_OPENSSL)
38 # include <openssl/des.h>
41 typedef des_cblock des_key;
42 typedef des_cblock des_data_block;
43 typedef des_key_schedule des_context;
44 #define des_failed(encrypted) 0
45 #define des_finish(key, schedule)
47 #elif defined(HAVE_MOZNSS)
49 typedef PK11SymKey *des_key;
50 typedef unsigned char des_data_block[8];
51 typedef PK11Context *des_context[1];
52 #define DES_ENCRYPT CKA_ENCRYPT
56 #endif /* SLAPD_LMHASH */
61 # include <ac/crypt.h>
63 # if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
70 # ifdef HAVE_AIX_SECURITY
81 #include "lutil_md5.h"
82 #include "lutil_sha1.h"
85 static const unsigned char crypt64[] =
86 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
89 static char *salt_format = NULL;
90 static lutil_cryptfunc lutil_crypt;
91 lutil_cryptfunc *lutil_cryptptr = lutil_crypt;
95 * chk_fn is NULL iff name is {CLEARTEXT}
96 * otherwise, things will break
100 LUTIL_PASSWD_CHK_FUNC *chk_fn;
101 LUTIL_PASSWD_HASH_FUNC *hash_fn;
105 struct pw_slist *next;
109 /* password check routines */
113 static LUTIL_PASSWD_CHK_FUNC chk_md5;
114 static LUTIL_PASSWD_CHK_FUNC chk_smd5;
115 static LUTIL_PASSWD_HASH_FUNC hash_smd5;
116 static LUTIL_PASSWD_HASH_FUNC hash_md5;
119 #ifdef LUTIL_SHA1_BYTES
120 static LUTIL_PASSWD_CHK_FUNC chk_ssha1;
121 static LUTIL_PASSWD_CHK_FUNC chk_sha1;
122 static LUTIL_PASSWD_HASH_FUNC hash_sha1;
123 static LUTIL_PASSWD_HASH_FUNC hash_ssha1;
127 static LUTIL_PASSWD_CHK_FUNC chk_lanman;
128 static LUTIL_PASSWD_HASH_FUNC hash_lanman;
132 static LUTIL_PASSWD_CHK_FUNC chk_crypt;
133 static LUTIL_PASSWD_HASH_FUNC hash_crypt;
135 #if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
136 static LUTIL_PASSWD_CHK_FUNC chk_unix;
140 /* password hash routines */
142 #ifdef SLAPD_CLEARTEXT
143 static LUTIL_PASSWD_HASH_FUNC hash_clear;
146 static struct pw_slist *pw_schemes;
147 static int pw_inited;
149 static const struct pw_scheme pw_schemes_default[] =
151 #ifdef LUTIL_SHA1_BYTES
152 { BER_BVC("{SSHA}"), chk_ssha1, hash_ssha1 },
153 { BER_BVC("{SHA}"), chk_sha1, hash_sha1 },
156 { BER_BVC("{SMD5}"), chk_smd5, hash_smd5 },
157 { BER_BVC("{MD5}"), chk_md5, hash_md5 },
160 { BER_BVC("{LANMAN}"), chk_lanman, hash_lanman },
161 #endif /* SLAPD_LMHASH */
164 { BER_BVC("{CRYPT}"), chk_crypt, hash_crypt },
165 # if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
166 { BER_BVC("{UNIX}"), chk_unix, NULL },
170 #ifdef SLAPD_CLEARTEXT
172 { BER_BVC("{CLEARTEXT}"), NULL, hash_clear },
175 { BER_BVNULL, NULL, NULL }
178 int lutil_passwd_add(
179 struct berval *scheme,
180 LUTIL_PASSWD_CHK_FUNC *chk,
181 LUTIL_PASSWD_HASH_FUNC *hash )
183 struct pw_slist *ptr;
185 if (!pw_inited) lutil_passwd_init();
187 ptr = ber_memalloc( sizeof( struct pw_slist ));
189 ptr->next = pw_schemes;
190 ptr->s.name = *scheme;
192 ptr->s.hash_fn = hash;
197 void lutil_passwd_init()
203 for( s=(struct pw_scheme *)pw_schemes_default; s->name.bv_val; s++) {
204 if ( lutil_passwd_add( &s->name, s->chk_fn, s->hash_fn ) ) break;
208 void lutil_passwd_destroy()
210 struct pw_slist *ptr, *next;
212 for( ptr=pw_schemes; ptr; ptr=next ) {
218 static const struct pw_scheme *get_scheme(
221 struct pw_slist *pws;
224 if (!pw_inited) lutil_passwd_init();
226 bv.bv_val = strchr( scheme, '}' );
230 bv.bv_len = bv.bv_val - scheme + 1;
231 bv.bv_val = (char *) scheme;
233 for( pws=pw_schemes; pws; pws=pws->next ) {
234 if ( ber_bvstrcasecmp(&bv, &pws->s.name ) == 0 ) {
242 int lutil_passwd_scheme(
245 if( scheme == NULL ) {
249 return get_scheme(scheme) != NULL;
253 static int is_allowed_scheme(
255 const char** schemes )
259 if( schemes == NULL ) return 1;
261 for( i=0; schemes[i] != NULL; i++ ) {
262 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
269 static struct berval *passwd_scheme(
270 const struct pw_scheme *scheme,
271 const struct berval * passwd,
273 const char** allowed )
275 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
279 if( passwd->bv_len >= scheme->name.bv_len ) {
280 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
281 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
282 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
292 * Return 0 if creds are good.
296 const struct berval *passwd, /* stored passwd */
297 const struct berval *cred, /* user cred */
298 const char **schemes,
301 struct pw_slist *pws;
303 if ( text ) *text = NULL;
305 if (cred == NULL || cred->bv_len == 0 ||
306 passwd == NULL || passwd->bv_len == 0 )
311 if (!pw_inited) lutil_passwd_init();
313 for( pws=pw_schemes; pws; pws=pws->next ) {
314 if( pws->s.chk_fn ) {
316 struct berval *p = passwd_scheme( &(pws->s),
317 passwd, &x, schemes );
320 return (pws->s.chk_fn)( &(pws->s.name), p, cred, text );
325 #ifdef SLAPD_CLEARTEXT
326 /* Do we think there is a scheme specifier here that we
327 * didn't recognize? Assume a scheme name is at least 1 character.
329 if (( passwd->bv_val[0] == '{' ) &&
330 ( ber_bvchr( passwd, '}' ) > passwd->bv_val+1 ))
334 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
335 return ( passwd->bv_len == cred->bv_len ) ?
336 memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
343 int lutil_passwd_generate( struct berval *pw, ber_len_t len )
346 if( len < 1 ) return -1;
349 pw->bv_val = ber_memalloc( len + 1 );
351 if( pw->bv_val == NULL ) {
355 if( lutil_entropy( (unsigned char *) pw->bv_val, pw->bv_len) < 0 ) {
359 for( len = 0; len < pw->bv_len; len++ ) {
360 pw->bv_val[len] = crypt64[
361 pw->bv_val[len] % (sizeof(crypt64)-1) ];
364 pw->bv_val[len] = '\0';
369 int lutil_passwd_hash(
370 const struct berval * passwd,
375 const struct pw_scheme *sc = get_scheme( method );
381 if( text ) *text = "scheme not recognized";
385 if( ! sc->hash_fn ) {
386 if( text ) *text = "scheme provided no hash function";
390 if( text ) *text = NULL;
392 return (sc->hash_fn)( &sc->name, passwd, hash, text );
395 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
396 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
397 static int pw_string(
398 const struct berval *sc,
399 struct berval *passwd )
403 pw.bv_len = sc->bv_len + passwd->bv_len;
404 pw.bv_val = ber_memalloc( pw.bv_len + 1 );
406 if( pw.bv_val == NULL ) {
407 return LUTIL_PASSWD_ERR;
410 AC_MEMCPY( pw.bv_val, sc->bv_val, sc->bv_len );
411 AC_MEMCPY( &pw.bv_val[sc->bv_len], passwd->bv_val, passwd->bv_len );
413 pw.bv_val[pw.bv_len] = '\0';
416 return LUTIL_PASSWD_OK;
418 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
420 static int pw_string64(
421 const struct berval *sc,
422 const struct berval *hash,
424 const struct berval *salt )
427 struct berval string;
431 /* need to base64 combined string */
432 string.bv_len = hash->bv_len + salt->bv_len;
433 string.bv_val = ber_memalloc( string.bv_len + 1 );
435 if( string.bv_val == NULL ) {
436 return LUTIL_PASSWD_ERR;
439 AC_MEMCPY( string.bv_val, hash->bv_val,
441 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
443 string.bv_val[string.bv_len] = '\0';
449 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
450 b64->bv_len = b64len + sc->bv_len;
451 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
453 if( b64->bv_val == NULL ) {
454 if( salt ) ber_memfree( string.bv_val );
455 return LUTIL_PASSWD_ERR;
458 AC_MEMCPY(b64->bv_val, sc->bv_val, sc->bv_len);
461 (unsigned char *) string.bv_val, string.bv_len,
462 &b64->bv_val[sc->bv_len], b64len );
464 if( salt ) ber_memfree( string.bv_val );
467 return LUTIL_PASSWD_ERR;
470 /* recompute length */
471 b64->bv_len = sc->bv_len + rc;
472 assert( strlen(b64->bv_val) == b64->bv_len );
473 return LUTIL_PASSWD_OK;
476 /* PASSWORD CHECK ROUTINES */
478 #ifdef LUTIL_SHA1_BYTES
479 static int chk_ssha1(
480 const struct berval *sc,
481 const struct berval * passwd,
482 const struct berval * cred,
485 lutil_SHA1_CTX SHA1context;
486 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
488 unsigned char *orig_pass = NULL;
490 /* safety check -- must have some salt */
491 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(SHA1digest)) {
492 return LUTIL_PASSWD_ERR;
495 /* decode base64 password */
496 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
497 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
499 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
501 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
503 /* safety check -- must have some salt */
504 if (rc <= (int)(sizeof(SHA1digest))) {
505 ber_memfree(orig_pass);
506 return LUTIL_PASSWD_ERR;
509 /* hash credentials with salt */
510 lutil_SHA1Init(&SHA1context);
511 lutil_SHA1Update(&SHA1context,
512 (const unsigned char *) cred->bv_val, cred->bv_len);
513 lutil_SHA1Update(&SHA1context,
514 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
515 rc - sizeof(SHA1digest));
516 lutil_SHA1Final(SHA1digest, &SHA1context);
519 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
520 ber_memfree(orig_pass);
521 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
525 const struct berval *sc,
526 const struct berval * passwd,
527 const struct berval * cred,
530 lutil_SHA1_CTX SHA1context;
531 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
533 unsigned char *orig_pass = NULL;
536 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(SHA1digest)) {
537 return LUTIL_PASSWD_ERR;
540 /* base64 un-encode password */
541 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
542 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
544 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
546 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
548 if( rc != sizeof(SHA1digest) ) {
549 ber_memfree(orig_pass);
550 return LUTIL_PASSWD_ERR;
553 /* hash credentials with salt */
554 lutil_SHA1Init(&SHA1context);
555 lutil_SHA1Update(&SHA1context,
556 (const unsigned char *) cred->bv_val, cred->bv_len);
557 lutil_SHA1Final(SHA1digest, &SHA1context);
560 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
561 ber_memfree(orig_pass);
562 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
567 const struct berval *sc,
568 const struct berval * passwd,
569 const struct berval * cred,
572 lutil_MD5_CTX MD5context;
573 unsigned char MD5digest[LUTIL_MD5_BYTES];
575 unsigned char *orig_pass = NULL;
578 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(MD5digest)) {
579 return LUTIL_PASSWD_ERR;
582 /* base64 un-encode password */
583 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
584 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
586 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
588 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
590 if (rc <= (int)(sizeof(MD5digest))) {
591 ber_memfree(orig_pass);
592 return LUTIL_PASSWD_ERR;
595 /* hash credentials with salt */
596 lutil_MD5Init(&MD5context);
597 lutil_MD5Update(&MD5context,
598 (const unsigned char *) cred->bv_val,
600 lutil_MD5Update(&MD5context,
601 &orig_pass[sizeof(MD5digest)],
602 rc - sizeof(MD5digest));
603 lutil_MD5Final(MD5digest, &MD5context);
606 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
607 ber_memfree(orig_pass);
608 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
612 const struct berval *sc,
613 const struct berval * passwd,
614 const struct berval * cred,
617 lutil_MD5_CTX MD5context;
618 unsigned char MD5digest[LUTIL_MD5_BYTES];
620 unsigned char *orig_pass = NULL;
623 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(MD5digest)) {
624 return LUTIL_PASSWD_ERR;
627 /* base64 un-encode password */
628 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
629 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
631 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
633 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
634 if ( rc != sizeof(MD5digest) ) {
635 ber_memfree(orig_pass);
636 return LUTIL_PASSWD_ERR;
639 /* hash credentials with salt */
640 lutil_MD5Init(&MD5context);
641 lutil_MD5Update(&MD5context,
642 (const unsigned char *) cred->bv_val,
644 lutil_MD5Final(MD5digest, &MD5context);
647 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
648 ber_memfree(orig_pass);
649 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
654 #if defined(HAVE_OPENSSL)
657 * abstract away setting the parity.
660 des_set_key( des_key *key, unsigned char *keyData)
662 memcpy(key, keyData, 8);
663 des_set_odd_parity( key );
667 #elif defined(HAVE_MOZNSS)
670 * implement MozNSS wrappers for the openSSL calls
673 des_set_key( des_key *key, unsigned char *keyData)
679 keyDataItem.data = keyData;
682 slot = PK11_GetBestSlot(CKM_DES_ECB, NULL);
687 /* NOTE: this will not work in FIPS mode. In order to make lmhash
688 * work in fips mode we need to define a LMHASH pbe mechanism and
689 * do the fulll key derivation inside the token */
690 *key = PK11_ImportSymKey(slot, CKM_DES_ECB, PK11_OriginGenerated,
691 CKA_ENCRYPT, &keyDataItem, NULL);
695 des_set_key_unchecked( des_key *key, des_context ctxt )
699 /* handle error conditions from previous call */
704 ctxt[0] = PK11_CreateContextBySymKey(CKM_DES_ECB, CKA_ENCRYPT, *key, NULL);
708 des_ecb_encrypt( des_data_block *plain, des_data_block *encrypted,
709 des_context ctxt, int op)
714 if (ctxt[0] == NULL) {
715 /* need to fail here... */
716 memset(encrypted, 0, sizeof(des_data_block));
719 rv = PK11_CipherOp(ctxt[0], (unsigned char *)&encrypted[0],
720 &size, sizeof(des_data_block),
721 (unsigned char *)&plain[0], sizeof(des_data_block));
722 if (rv != SECSuccess) {
724 memset(encrypted, 0, sizeof(des_data_block));
731 des_failed(des_data_block *encrypted)
733 static const des_data_block zero = { 0 };
734 return memcmp(encrypted, zero, sizeof(zero)) == 0;
738 des_finish(des_key *key, des_context ctxt)
741 PK11_FreeSymKey(*key);
745 PK11_Finalize(ctxt[0]);
746 PK11_DestroyContext(ctxt[0], PR_TRUE);
753 /* pseudocode from RFC2433
754 * A.2 LmPasswordHash()
757 * IN 0-to-14-oem-char Password,
758 * OUT 16-octet PasswordHash )
760 * Set UcasePassword to the uppercased Password
761 * Zero pad UcasePassword to 14 characters
763 * DesHash( 1st 7-octets of UcasePassword,
764 * giving 1st 8-octets of PasswordHash )
766 * DesHash( 2nd 7-octets of UcasePassword,
767 * giving 2nd 8-octets of PasswordHash )
775 * OUT 8-octet Cypher )
778 * * Make Cypher an irreversibly encrypted form of Clear by
779 * * encrypting known text using Clear as the secret key.
780 * * The known text consists of the string
785 * Set StdText to "KGS!@#$%"
786 * DesEncrypt( StdText, Clear, giving Cypher )
795 * OUT 8-octet Cypher )
798 * * Use the DES encryption algorithm [4] in ECB mode [9]
799 * * to encrypt Clear into Cypher such that Cypher can
800 * * only be decrypted back to Clear by providing Key.
801 * * Note that the DES algorithm takes as input a 64-bit
802 * * stream where the 8th, 16th, 24th, etc. bits are
803 * * parity bits ignored by the encrypting algorithm.
804 * * Unless you write your own DES to accept 56-bit input
805 * * without parity, you will need to insert the parity bits
811 static void lmPasswd_to_key(
812 const char *lmPasswd,
815 const unsigned char *lpw = (const unsigned char *) lmPasswd;
818 /* make room for parity bits */
820 k[1] = ((lpw[0] & 0x01) << 7) | (lpw[1] >> 1);
821 k[2] = ((lpw[1] & 0x03) << 6) | (lpw[2] >> 2);
822 k[3] = ((lpw[2] & 0x07) << 5) | (lpw[3] >> 3);
823 k[4] = ((lpw[3] & 0x0F) << 4) | (lpw[4] >> 4);
824 k[5] = ((lpw[4] & 0x1F) << 3) | (lpw[5] >> 5);
825 k[6] = ((lpw[5] & 0x3F) << 2) | (lpw[6] >> 6);
826 k[7] = ((lpw[6] & 0x7F) << 1);
828 des_set_key( key, k );
831 static int chk_lanman(
832 const struct berval *scheme,
833 const struct berval *passwd,
834 const struct berval *cred,
838 char UcasePassword[15];
840 des_context schedule;
841 des_data_block StdText = "KGS!@#$%";
842 des_data_block PasswordHash1, PasswordHash2;
843 char PasswordHash[33], storedPasswordHash[33];
845 for( i=0; i<cred->bv_len; i++) {
846 if(cred->bv_val[i] == '\0') {
847 return LUTIL_PASSWD_ERR; /* NUL character in password */
851 if( cred->bv_val[i] != '\0' ) {
852 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
855 strncpy( UcasePassword, cred->bv_val, 14 );
856 UcasePassword[14] = '\0';
857 ldap_pvt_str2upper( UcasePassword );
859 lmPasswd_to_key( UcasePassword, &key );
860 des_set_key_unchecked( &key, schedule );
861 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
863 if (des_failed(&PasswordHash1)) {
864 return LUTIL_PASSWD_ERR;
867 lmPasswd_to_key( &UcasePassword[7], &key );
868 des_set_key_unchecked( &key, schedule );
869 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
870 if (des_failed(&PasswordHash2)) {
871 return LUTIL_PASSWD_ERR;
874 des_finish( &key, schedule );
876 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
877 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
878 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
879 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
880 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
882 /* as a precaution convert stored password hash to lower case */
883 strncpy( storedPasswordHash, passwd->bv_val, 32 );
884 storedPasswordHash[32] = '\0';
885 ldap_pvt_str2lower( storedPasswordHash );
887 return memcmp( PasswordHash, storedPasswordHash, 32) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
889 #endif /* SLAPD_LMHASH */
892 static int lutil_crypt(
897 char *cr = crypt( key, salt );
900 if( cr == NULL || cr[0] == '\0' ) {
901 /* salt must have been invalid */
902 rc = LUTIL_PASSWD_ERR;
905 *hash = ber_strdup( cr );
906 rc = LUTIL_PASSWD_OK;
908 rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
914 static int chk_crypt(
915 const struct berval *sc,
916 const struct berval * passwd,
917 const struct berval * cred,
922 for( i=0; i<cred->bv_len; i++) {
923 if(cred->bv_val[i] == '\0') {
924 return LUTIL_PASSWD_ERR; /* NUL character in password */
928 if( cred->bv_val[i] != '\0' ) {
929 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
932 if( passwd->bv_len < 2 ) {
933 return LUTIL_PASSWD_ERR; /* passwd must be at least two characters long */
936 for( i=0; i<passwd->bv_len; i++) {
937 if(passwd->bv_val[i] == '\0') {
938 return LUTIL_PASSWD_ERR; /* NUL character in password */
942 if( passwd->bv_val[i] != '\0' ) {
943 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
946 return lutil_cryptptr( cred->bv_val, passwd->bv_val, NULL );
949 # if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
951 const struct berval *sc,
952 const struct berval * passwd,
953 const struct berval * cred,
959 for( i=0; i<cred->bv_len; i++) {
960 if(cred->bv_val[i] == '\0') {
961 return LUTIL_PASSWD_ERR; /* NUL character in password */
964 if( cred->bv_val[i] != '\0' ) {
965 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
968 for( i=0; i<passwd->bv_len; i++) {
969 if(passwd->bv_val[i] == '\0') {
970 return LUTIL_PASSWD_ERR; /* NUL character in password */
974 if( passwd->bv_val[i] != '\0' ) {
975 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
979 struct passwd *pwd = getpwnam(passwd->bv_val);
982 return LUTIL_PASSWD_ERR; /* not found */
987 # ifdef HAVE_GETSPNAM
989 struct spwd *spwd = getspnam(passwd->bv_val);
996 # ifdef HAVE_AIX_SECURITY
998 struct userpw *upw = getuserpw(passwd->bv_val);
1001 pw = upw->upw_passwd;
1006 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
1007 /* password must must be at least two characters long */
1008 return LUTIL_PASSWD_ERR;
1011 return lutil_cryptptr( cred->bv_val, pw, NULL );
1016 /* PASSWORD GENERATION ROUTINES */
1018 #ifdef LUTIL_SHA1_BYTES
1019 static int hash_ssha1(
1020 const struct berval *scheme,
1021 const struct berval *passwd,
1022 struct berval *hash,
1025 lutil_SHA1_CTX SHA1context;
1026 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1027 char saltdata[SALT_SIZE];
1028 struct berval digest;
1031 digest.bv_val = (char *) SHA1digest;
1032 digest.bv_len = sizeof(SHA1digest);
1033 salt.bv_val = saltdata;
1034 salt.bv_len = sizeof(saltdata);
1036 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
1037 return LUTIL_PASSWD_ERR;
1040 lutil_SHA1Init( &SHA1context );
1041 lutil_SHA1Update( &SHA1context,
1042 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1043 lutil_SHA1Update( &SHA1context,
1044 (const unsigned char *)salt.bv_val, salt.bv_len );
1045 lutil_SHA1Final( SHA1digest, &SHA1context );
1047 return pw_string64( scheme, &digest, hash, &salt);
1050 static int hash_sha1(
1051 const struct berval *scheme,
1052 const struct berval *passwd,
1053 struct berval *hash,
1056 lutil_SHA1_CTX SHA1context;
1057 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1058 struct berval digest;
1059 digest.bv_val = (char *) SHA1digest;
1060 digest.bv_len = sizeof(SHA1digest);
1062 lutil_SHA1Init( &SHA1context );
1063 lutil_SHA1Update( &SHA1context,
1064 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1065 lutil_SHA1Final( SHA1digest, &SHA1context );
1067 return pw_string64( scheme, &digest, hash, NULL);
1071 static int hash_smd5(
1072 const struct berval *scheme,
1073 const struct berval *passwd,
1074 struct berval *hash,
1077 lutil_MD5_CTX MD5context;
1078 unsigned char MD5digest[LUTIL_MD5_BYTES];
1079 char saltdata[SALT_SIZE];
1080 struct berval digest;
1083 digest.bv_val = (char *) MD5digest;
1084 digest.bv_len = sizeof(MD5digest);
1085 salt.bv_val = saltdata;
1086 salt.bv_len = sizeof(saltdata);
1088 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
1089 return LUTIL_PASSWD_ERR;
1092 lutil_MD5Init( &MD5context );
1093 lutil_MD5Update( &MD5context,
1094 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1095 lutil_MD5Update( &MD5context,
1096 (const unsigned char *) salt.bv_val, salt.bv_len );
1097 lutil_MD5Final( MD5digest, &MD5context );
1099 return pw_string64( scheme, &digest, hash, &salt );
1102 static int hash_md5(
1103 const struct berval *scheme,
1104 const struct berval *passwd,
1105 struct berval *hash,
1108 lutil_MD5_CTX MD5context;
1109 unsigned char MD5digest[LUTIL_MD5_BYTES];
1111 struct berval digest;
1113 digest.bv_val = (char *) MD5digest;
1114 digest.bv_len = sizeof(MD5digest);
1116 lutil_MD5Init( &MD5context );
1117 lutil_MD5Update( &MD5context,
1118 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1119 lutil_MD5Final( MD5digest, &MD5context );
1121 return pw_string64( scheme, &digest, hash, NULL );
1126 static int hash_lanman(
1127 const struct berval *scheme,
1128 const struct berval *passwd,
1129 struct berval *hash,
1134 char UcasePassword[15];
1136 des_context schedule;
1137 des_data_block StdText = "KGS!@#$%";
1138 des_data_block PasswordHash1, PasswordHash2;
1139 char PasswordHash[33];
1141 for( i=0; i<passwd->bv_len; i++) {
1142 if(passwd->bv_val[i] == '\0') {
1143 return LUTIL_PASSWD_ERR; /* NUL character in password */
1147 if( passwd->bv_val[i] != '\0' ) {
1148 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
1151 strncpy( UcasePassword, passwd->bv_val, 14 );
1152 UcasePassword[14] = '\0';
1153 ldap_pvt_str2upper( UcasePassword );
1155 lmPasswd_to_key( UcasePassword, &key );
1156 des_set_key_unchecked( &key, schedule );
1157 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
1159 lmPasswd_to_key( &UcasePassword[7], &key );
1160 des_set_key_unchecked( &key, schedule );
1161 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
1163 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1164 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
1165 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
1166 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
1167 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
1169 hash->bv_val = PasswordHash;
1172 return pw_string( scheme, hash );
1174 #endif /* SLAPD_LMHASH */
1177 static int hash_crypt(
1178 const struct berval *scheme,
1179 const struct berval *passwd,
1180 struct berval *hash,
1183 unsigned char salt[32]; /* salt suitable for most anything */
1188 for( i=0; i<passwd->bv_len; i++) {
1189 if(passwd->bv_val[i] == '\0') {
1190 return LUTIL_PASSWD_ERR; /* NUL character in password */
1194 if( passwd->bv_val[i] != '\0' ) {
1195 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
1198 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1199 return LUTIL_PASSWD_ERR;
1202 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1203 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1205 salt[sizeof( salt ) - 1 ] = '\0';
1207 if( salt_format != NULL ) {
1208 /* copy the salt we made into entropy before snprintfing
1209 it back into the salt */
1210 char entropy[sizeof(salt)];
1211 strcpy( entropy, (char *) salt );
1212 snprintf( (char *) salt, sizeof(entropy), salt_format, entropy );
1215 rc = lutil_cryptptr( passwd->bv_val, (char *) salt, &hash->bv_val );
1216 if ( rc != LUTIL_PASSWD_OK ) return rc;
1218 if( hash->bv_val == NULL ) return -1;
1220 hash->bv_len = strlen( hash->bv_val );
1222 save = hash->bv_val;
1224 if( hash->bv_len == 0 ) {
1225 rc = LUTIL_PASSWD_ERR;
1227 rc = pw_string( scheme, hash );
1229 ber_memfree( save );
1234 int lutil_salt_format(const char *format)
1237 ber_memfree( salt_format );
1239 salt_format = format != NULL ? ber_strdup( format ) : NULL;
1245 #ifdef SLAPD_CLEARTEXT
1246 static int hash_clear(
1247 const struct berval *scheme,
1248 const struct berval *passwd,
1249 struct berval *hash,
1252 ber_dupbv( hash, (struct berval *)passwd );
1253 return LUTIL_PASSWD_OK;