2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2017 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[1];
44 #define des_failed(encrypted) 0
45 #define des_finish(key, schedule)
47 #elif defined(HAVE_MOZNSS)
50 We need to define this here so that nspr/obsolete/protypes.h will not be included
51 if that file is included, it will create a uint32 typedef that will cause the
52 one in lutil_sha1.h to blow up
55 # include <nss/pk11pub.h>
56 typedef PK11SymKey *des_key;
57 typedef unsigned char des_data_block[8];
58 typedef PK11Context *des_context[1];
59 #define DES_ENCRYPT CKA_ENCRYPT
63 #endif /* SLAPD_LMHASH */
68 # include <ac/crypt.h>
70 # if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
77 # ifdef HAVE_AIX_SECURITY
88 #include "lutil_md5.h"
89 #include "lutil_sha1.h"
92 static const unsigned char crypt64[] =
93 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
96 static char *salt_format = NULL;
97 static lutil_cryptfunc lutil_crypt;
98 lutil_cryptfunc *lutil_cryptptr = lutil_crypt;
102 * chk_fn is NULL iff name is {CLEARTEXT}
103 * otherwise, things will break
107 LUTIL_PASSWD_CHK_FUNC *chk_fn;
108 LUTIL_PASSWD_HASH_FUNC *hash_fn;
112 struct pw_slist *next;
116 /* password check routines */
120 static LUTIL_PASSWD_CHK_FUNC chk_md5;
121 static LUTIL_PASSWD_CHK_FUNC chk_smd5;
122 static LUTIL_PASSWD_HASH_FUNC hash_smd5;
123 static LUTIL_PASSWD_HASH_FUNC hash_md5;
126 #ifdef LUTIL_SHA1_BYTES
127 static LUTIL_PASSWD_CHK_FUNC chk_ssha1;
128 static LUTIL_PASSWD_CHK_FUNC chk_sha1;
129 static LUTIL_PASSWD_HASH_FUNC hash_sha1;
130 static LUTIL_PASSWD_HASH_FUNC hash_ssha1;
134 static LUTIL_PASSWD_CHK_FUNC chk_lanman;
135 static LUTIL_PASSWD_HASH_FUNC hash_lanman;
139 static LUTIL_PASSWD_CHK_FUNC chk_crypt;
140 static LUTIL_PASSWD_HASH_FUNC hash_crypt;
142 #if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
143 static LUTIL_PASSWD_CHK_FUNC chk_unix;
147 /* password hash routines */
149 #ifdef SLAPD_CLEARTEXT
150 static LUTIL_PASSWD_HASH_FUNC hash_clear;
153 static struct pw_slist *pw_schemes;
154 static int pw_inited;
156 static const struct pw_scheme pw_schemes_default[] =
158 #ifdef LUTIL_SHA1_BYTES
159 { BER_BVC("{SSHA}"), chk_ssha1, hash_ssha1 },
160 { BER_BVC("{SHA}"), chk_sha1, hash_sha1 },
163 { BER_BVC("{SMD5}"), chk_smd5, hash_smd5 },
164 { BER_BVC("{MD5}"), chk_md5, hash_md5 },
167 { BER_BVC("{LANMAN}"), chk_lanman, hash_lanman },
168 #endif /* SLAPD_LMHASH */
171 { BER_BVC("{CRYPT}"), chk_crypt, hash_crypt },
172 # if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
173 { BER_BVC("{UNIX}"), chk_unix, NULL },
177 #ifdef SLAPD_CLEARTEXT
179 { BER_BVC("{CLEARTEXT}"), NULL, hash_clear },
182 { BER_BVNULL, NULL, NULL }
185 int lutil_passwd_add(
186 struct berval *scheme,
187 LUTIL_PASSWD_CHK_FUNC *chk,
188 LUTIL_PASSWD_HASH_FUNC *hash )
190 struct pw_slist *ptr;
192 if (!pw_inited) lutil_passwd_init();
194 ptr = ber_memalloc( sizeof( struct pw_slist ));
196 ptr->next = pw_schemes;
197 ptr->s.name = *scheme;
199 ptr->s.hash_fn = hash;
204 void lutil_passwd_init()
210 for( s=(struct pw_scheme *)pw_schemes_default; s->name.bv_val; s++) {
211 if ( lutil_passwd_add( &s->name, s->chk_fn, s->hash_fn ) ) break;
215 void lutil_passwd_destroy()
217 struct pw_slist *ptr, *next;
219 for( ptr=pw_schemes; ptr; ptr=next ) {
225 static const struct pw_scheme *get_scheme(
228 struct pw_slist *pws;
231 if (!pw_inited) lutil_passwd_init();
233 bv.bv_val = strchr( scheme, '}' );
237 bv.bv_len = bv.bv_val - scheme + 1;
238 bv.bv_val = (char *) scheme;
240 for( pws=pw_schemes; pws; pws=pws->next ) {
241 if ( ber_bvstrcasecmp(&bv, &pws->s.name ) == 0 ) {
249 int lutil_passwd_scheme(
252 if( scheme == NULL ) {
256 return get_scheme(scheme) != NULL;
260 static int is_allowed_scheme(
262 const char** schemes )
266 if( schemes == NULL ) return 1;
268 for( i=0; schemes[i] != NULL; i++ ) {
269 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
276 static struct berval *passwd_scheme(
277 const struct pw_scheme *scheme,
278 const struct berval * passwd,
280 const char** allowed )
282 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
286 if( passwd->bv_len >= scheme->name.bv_len ) {
287 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
288 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
289 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
299 * Return 0 if creds are good.
303 const struct berval *passwd, /* stored passwd */
304 const struct berval *cred, /* user cred */
305 const char **schemes,
308 struct pw_slist *pws;
310 if ( text ) *text = NULL;
312 if (cred == NULL || cred->bv_len == 0 ||
313 passwd == NULL || passwd->bv_len == 0 )
318 if (!pw_inited) lutil_passwd_init();
320 for( pws=pw_schemes; pws; pws=pws->next ) {
321 if( pws->s.chk_fn ) {
323 struct berval *p = passwd_scheme( &(pws->s),
324 passwd, &x, schemes );
327 return (pws->s.chk_fn)( &(pws->s.name), p, cred, text );
332 #ifdef SLAPD_CLEARTEXT
333 /* Do we think there is a scheme specifier here that we
334 * didn't recognize? Assume a scheme name is at least 1 character.
336 if (( passwd->bv_val[0] == '{' ) &&
337 ( ber_bvchr( passwd, '}' ) > passwd->bv_val+1 ))
341 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
342 return ( passwd->bv_len == cred->bv_len ) ?
343 memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
350 int lutil_passwd_generate( struct berval *pw, ber_len_t len )
353 if( len < 1 ) return -1;
356 pw->bv_val = ber_memalloc( len + 1 );
358 if( pw->bv_val == NULL ) {
362 if( lutil_entropy( (unsigned char *) pw->bv_val, pw->bv_len) < 0 ) {
366 for( len = 0; len < pw->bv_len; len++ ) {
367 pw->bv_val[len] = crypt64[
368 pw->bv_val[len] % (sizeof(crypt64)-1) ];
371 pw->bv_val[len] = '\0';
376 int lutil_passwd_hash(
377 const struct berval * passwd,
382 const struct pw_scheme *sc = get_scheme( method );
388 if( text ) *text = "scheme not recognized";
392 if( ! sc->hash_fn ) {
393 if( text ) *text = "scheme provided no hash function";
397 if( text ) *text = NULL;
399 return (sc->hash_fn)( &sc->name, passwd, hash, text );
402 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
403 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
404 static int pw_string(
405 const struct berval *sc,
406 struct berval *passwd )
410 pw.bv_len = sc->bv_len + passwd->bv_len;
411 pw.bv_val = ber_memalloc( pw.bv_len + 1 );
413 if( pw.bv_val == NULL ) {
414 return LUTIL_PASSWD_ERR;
417 AC_MEMCPY( pw.bv_val, sc->bv_val, sc->bv_len );
418 AC_MEMCPY( &pw.bv_val[sc->bv_len], passwd->bv_val, passwd->bv_len );
420 pw.bv_val[pw.bv_len] = '\0';
423 return LUTIL_PASSWD_OK;
425 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
427 int lutil_passwd_string64(
428 const struct berval *sc,
429 const struct berval *hash,
431 const struct berval *salt )
434 struct berval string;
438 /* need to base64 combined string */
439 string.bv_len = hash->bv_len + salt->bv_len;
440 string.bv_val = ber_memalloc( string.bv_len + 1 );
442 if( string.bv_val == NULL ) {
443 return LUTIL_PASSWD_ERR;
446 AC_MEMCPY( string.bv_val, hash->bv_val,
448 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
450 string.bv_val[string.bv_len] = '\0';
456 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
457 b64->bv_len = b64len + sc->bv_len;
458 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
460 if( b64->bv_val == NULL ) {
461 if( salt ) ber_memfree( string.bv_val );
462 return LUTIL_PASSWD_ERR;
465 AC_MEMCPY(b64->bv_val, sc->bv_val, sc->bv_len);
468 (unsigned char *) string.bv_val, string.bv_len,
469 &b64->bv_val[sc->bv_len], b64len );
471 if( salt ) ber_memfree( string.bv_val );
474 return LUTIL_PASSWD_ERR;
477 /* recompute length */
478 b64->bv_len = sc->bv_len + rc;
479 assert( strlen(b64->bv_val) == b64->bv_len );
480 return LUTIL_PASSWD_OK;
483 /* PASSWORD CHECK ROUTINES */
485 #ifdef LUTIL_SHA1_BYTES
486 static int chk_ssha1(
487 const struct berval *sc,
488 const struct berval * passwd,
489 const struct berval * cred,
492 lutil_SHA1_CTX SHA1context;
493 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
495 unsigned char *orig_pass = NULL;
496 size_t decode_len = LUTIL_BASE64_DECODE_LEN(passwd->bv_len);
498 /* safety check -- must have some salt */
499 if (decode_len <= sizeof(SHA1digest)) {
500 return LUTIL_PASSWD_ERR;
503 /* decode base64 password */
504 orig_pass = (unsigned char *) ber_memalloc(decode_len + 1);
506 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
508 rc = lutil_b64_pton(passwd->bv_val, orig_pass, decode_len);
510 /* safety check -- must have some salt */
511 if (rc <= (int)(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_SHA1Update(&SHA1context,
521 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
522 rc - sizeof(SHA1digest));
523 lutil_SHA1Final(SHA1digest, &SHA1context);
526 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
527 ber_memfree(orig_pass);
528 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
532 const struct berval *sc,
533 const struct berval * passwd,
534 const struct berval * cred,
537 lutil_SHA1_CTX SHA1context;
538 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
540 unsigned char *orig_pass = NULL;
541 size_t decode_len = LUTIL_BASE64_DECODE_LEN(passwd->bv_len);
544 if (decode_len < sizeof(SHA1digest)) {
545 return LUTIL_PASSWD_ERR;
548 /* base64 un-encode password */
549 orig_pass = (unsigned char *) ber_memalloc(decode_len + 1);
551 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
553 rc = lutil_b64_pton(passwd->bv_val, orig_pass, decode_len);
555 if( rc != sizeof(SHA1digest) ) {
556 ber_memfree(orig_pass);
557 return LUTIL_PASSWD_ERR;
560 /* hash credentials with salt */
561 lutil_SHA1Init(&SHA1context);
562 lutil_SHA1Update(&SHA1context,
563 (const unsigned char *) cred->bv_val, cred->bv_len);
564 lutil_SHA1Final(SHA1digest, &SHA1context);
567 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
568 ber_memfree(orig_pass);
569 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
574 const struct berval *sc,
575 const struct berval * passwd,
576 const struct berval * cred,
579 lutil_MD5_CTX MD5context;
580 unsigned char MD5digest[LUTIL_MD5_BYTES];
582 unsigned char *orig_pass = NULL;
583 size_t decode_len = LUTIL_BASE64_DECODE_LEN(passwd->bv_len);
586 if (decode_len <= sizeof(MD5digest)) {
587 return LUTIL_PASSWD_ERR;
590 /* base64 un-encode password */
591 orig_pass = (unsigned char *) ber_memalloc(decode_len + 1);
593 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
595 rc = lutil_b64_pton(passwd->bv_val, orig_pass, decode_len);
597 if (rc <= (int)(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_MD5Update(&MD5context,
608 &orig_pass[sizeof(MD5digest)],
609 rc - sizeof(MD5digest));
610 lutil_MD5Final(MD5digest, &MD5context);
613 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
614 ber_memfree(orig_pass);
615 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
619 const struct berval *sc,
620 const struct berval * passwd,
621 const struct berval * cred,
624 lutil_MD5_CTX MD5context;
625 unsigned char MD5digest[LUTIL_MD5_BYTES];
627 unsigned char *orig_pass = NULL;
628 size_t decode_len = LUTIL_BASE64_DECODE_LEN(passwd->bv_len);
631 if (decode_len < sizeof(MD5digest)) {
632 return LUTIL_PASSWD_ERR;
635 /* base64 un-encode password */
636 orig_pass = (unsigned char *) ber_memalloc(decode_len + 1);
638 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
640 rc = lutil_b64_pton(passwd->bv_val, orig_pass, decode_len);
641 if ( rc != sizeof(MD5digest) ) {
642 ber_memfree(orig_pass);
643 return LUTIL_PASSWD_ERR;
646 /* hash credentials with salt */
647 lutil_MD5Init(&MD5context);
648 lutil_MD5Update(&MD5context,
649 (const unsigned char *) cred->bv_val,
651 lutil_MD5Final(MD5digest, &MD5context);
654 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
655 ber_memfree(orig_pass);
656 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
661 #if defined(HAVE_OPENSSL)
664 * abstract away setting the parity.
667 des_set_key_and_parity( des_key *key, unsigned char *keyData)
669 memcpy(key, keyData, 8);
670 DES_set_odd_parity( key );
674 #elif defined(HAVE_MOZNSS)
677 * implement MozNSS wrappers for the openSSL calls
680 des_set_key_and_parity( des_key *key, unsigned char *keyData)
686 keyDataItem.data = keyData;
689 slot = PK11_GetBestSlot(CKM_DES_ECB, NULL);
694 /* NOTE: this will not work in FIPS mode. In order to make lmhash
695 * work in fips mode we need to define a LMHASH pbe mechanism and
696 * do the fulll key derivation inside the token */
697 *key = PK11_ImportSymKey(slot, CKM_DES_ECB, PK11_OriginGenerated,
698 CKA_ENCRYPT, &keyDataItem, NULL);
702 DES_set_key_unchecked( des_key *key, des_context ctxt )
706 /* handle error conditions from previous call */
711 ctxt[0] = PK11_CreateContextBySymKey(CKM_DES_ECB, CKA_ENCRYPT, *key, NULL);
715 DES_ecb_encrypt( des_data_block *plain, des_data_block *encrypted,
716 des_context ctxt, int op)
721 if (ctxt[0] == NULL) {
722 /* need to fail here... */
723 memset(encrypted, 0, sizeof(des_data_block));
726 rv = PK11_CipherOp(ctxt[0], (unsigned char *)&encrypted[0],
727 &size, sizeof(des_data_block),
728 (unsigned char *)&plain[0], sizeof(des_data_block));
729 if (rv != SECSuccess) {
731 memset(encrypted, 0, sizeof(des_data_block));
738 des_failed(des_data_block *encrypted)
740 static const des_data_block zero = { 0 };
741 return memcmp(encrypted, zero, sizeof(zero)) == 0;
745 des_finish(des_key *key, des_context ctxt)
748 PK11_FreeSymKey(*key);
752 PK11_Finalize(ctxt[0]);
753 PK11_DestroyContext(ctxt[0], PR_TRUE);
760 /* pseudocode from RFC2433
761 * A.2 LmPasswordHash()
764 * IN 0-to-14-oem-char Password,
765 * OUT 16-octet PasswordHash )
767 * Set UcasePassword to the uppercased Password
768 * Zero pad UcasePassword to 14 characters
770 * DesHash( 1st 7-octets of UcasePassword,
771 * giving 1st 8-octets of PasswordHash )
773 * DesHash( 2nd 7-octets of UcasePassword,
774 * giving 2nd 8-octets of PasswordHash )
782 * OUT 8-octet Cypher )
785 * * Make Cypher an irreversibly encrypted form of Clear by
786 * * encrypting known text using Clear as the secret key.
787 * * The known text consists of the string
792 * Set StdText to "KGS!@#$%"
793 * DesEncrypt( StdText, Clear, giving Cypher )
802 * OUT 8-octet Cypher )
805 * * Use the DES encryption algorithm [4] in ECB mode [9]
806 * * to encrypt Clear into Cypher such that Cypher can
807 * * only be decrypted back to Clear by providing Key.
808 * * Note that the DES algorithm takes as input a 64-bit
809 * * stream where the 8th, 16th, 24th, etc. bits are
810 * * parity bits ignored by the encrypting algorithm.
811 * * Unless you write your own DES to accept 56-bit input
812 * * without parity, you will need to insert the parity bits
818 static void lmPasswd_to_key(
819 const char *lmPasswd,
822 const unsigned char *lpw = (const unsigned char *) lmPasswd;
825 /* make room for parity bits */
827 k[1] = ((lpw[0] & 0x01) << 7) | (lpw[1] >> 1);
828 k[2] = ((lpw[1] & 0x03) << 6) | (lpw[2] >> 2);
829 k[3] = ((lpw[2] & 0x07) << 5) | (lpw[3] >> 3);
830 k[4] = ((lpw[3] & 0x0F) << 4) | (lpw[4] >> 4);
831 k[5] = ((lpw[4] & 0x1F) << 3) | (lpw[5] >> 5);
832 k[6] = ((lpw[5] & 0x3F) << 2) | (lpw[6] >> 6);
833 k[7] = ((lpw[6] & 0x7F) << 1);
835 des_set_key_and_parity( key, k );
838 static int chk_lanman(
839 const struct berval *scheme,
840 const struct berval *passwd,
841 const struct berval *cred,
845 char UcasePassword[15];
847 des_context schedule;
848 des_data_block StdText = "KGS!@#$%";
849 des_data_block PasswordHash1, PasswordHash2;
850 char PasswordHash[33], storedPasswordHash[33];
852 for( i=0; i<cred->bv_len; i++) {
853 if(cred->bv_val[i] == '\0') {
854 return LUTIL_PASSWD_ERR; /* NUL character in password */
858 if( cred->bv_val[i] != '\0' ) {
859 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
862 strncpy( UcasePassword, cred->bv_val, 14 );
863 UcasePassword[14] = '\0';
864 ldap_pvt_str2upper( UcasePassword );
866 lmPasswd_to_key( UcasePassword, &key );
867 DES_set_key_unchecked( &key, schedule );
868 DES_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
870 if (des_failed(&PasswordHash1)) {
871 return LUTIL_PASSWD_ERR;
874 lmPasswd_to_key( &UcasePassword[7], &key );
875 DES_set_key_unchecked( &key, schedule );
876 DES_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
877 if (des_failed(&PasswordHash2)) {
878 return LUTIL_PASSWD_ERR;
881 des_finish( &key, schedule );
883 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
884 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
885 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
886 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
887 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
889 /* as a precaution convert stored password hash to lower case */
890 strncpy( storedPasswordHash, passwd->bv_val, 32 );
891 storedPasswordHash[32] = '\0';
892 ldap_pvt_str2lower( storedPasswordHash );
894 return memcmp( PasswordHash, storedPasswordHash, 32) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
896 #endif /* SLAPD_LMHASH */
899 static int lutil_crypt(
904 char *cr = crypt( key, salt );
907 if( cr == NULL || cr[0] == '\0' ) {
908 /* salt must have been invalid */
909 rc = LUTIL_PASSWD_ERR;
912 *hash = ber_strdup( cr );
913 rc = LUTIL_PASSWD_OK;
915 rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
921 static int chk_crypt(
922 const struct berval *sc,
923 const struct berval * passwd,
924 const struct berval * cred,
929 for( i=0; i<cred->bv_len; i++) {
930 if(cred->bv_val[i] == '\0') {
931 return LUTIL_PASSWD_ERR; /* NUL character in password */
935 if( cred->bv_val[i] != '\0' ) {
936 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
939 if( passwd->bv_len < 2 ) {
940 return LUTIL_PASSWD_ERR; /* passwd must be at least two characters long */
943 for( i=0; i<passwd->bv_len; i++) {
944 if(passwd->bv_val[i] == '\0') {
945 return LUTIL_PASSWD_ERR; /* NUL character in password */
949 if( passwd->bv_val[i] != '\0' ) {
950 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
953 return lutil_cryptptr( cred->bv_val, passwd->bv_val, NULL );
956 # if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
958 const struct berval *sc,
959 const struct berval * passwd,
960 const struct berval * cred,
966 for( i=0; i<cred->bv_len; i++) {
967 if(cred->bv_val[i] == '\0') {
968 return LUTIL_PASSWD_ERR; /* NUL character in password */
971 if( cred->bv_val[i] != '\0' ) {
972 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
975 for( i=0; i<passwd->bv_len; i++) {
976 if(passwd->bv_val[i] == '\0') {
977 return LUTIL_PASSWD_ERR; /* NUL character in password */
981 if( passwd->bv_val[i] != '\0' ) {
982 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
986 struct passwd *pwd = getpwnam(passwd->bv_val);
989 return LUTIL_PASSWD_ERR; /* not found */
994 # ifdef HAVE_GETSPNAM
996 struct spwd *spwd = getspnam(passwd->bv_val);
1003 # ifdef HAVE_AIX_SECURITY
1005 struct userpw *upw = getuserpw(passwd->bv_val);
1008 pw = upw->upw_passwd;
1013 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
1014 /* password must must be at least two characters long */
1015 return LUTIL_PASSWD_ERR;
1018 return lutil_cryptptr( cred->bv_val, pw, NULL );
1023 /* PASSWORD GENERATION ROUTINES */
1025 #ifdef LUTIL_SHA1_BYTES
1026 static int hash_ssha1(
1027 const struct berval *scheme,
1028 const struct berval *passwd,
1029 struct berval *hash,
1032 lutil_SHA1_CTX SHA1context;
1033 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1034 char saltdata[SALT_SIZE];
1035 struct berval digest;
1038 digest.bv_val = (char *) SHA1digest;
1039 digest.bv_len = sizeof(SHA1digest);
1040 salt.bv_val = saltdata;
1041 salt.bv_len = sizeof(saltdata);
1043 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
1044 return LUTIL_PASSWD_ERR;
1047 lutil_SHA1Init( &SHA1context );
1048 lutil_SHA1Update( &SHA1context,
1049 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1050 lutil_SHA1Update( &SHA1context,
1051 (const unsigned char *)salt.bv_val, salt.bv_len );
1052 lutil_SHA1Final( SHA1digest, &SHA1context );
1054 return lutil_passwd_string64( scheme, &digest, hash, &salt);
1057 static int hash_sha1(
1058 const struct berval *scheme,
1059 const struct berval *passwd,
1060 struct berval *hash,
1063 lutil_SHA1_CTX SHA1context;
1064 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1065 struct berval digest;
1066 digest.bv_val = (char *) SHA1digest;
1067 digest.bv_len = sizeof(SHA1digest);
1069 lutil_SHA1Init( &SHA1context );
1070 lutil_SHA1Update( &SHA1context,
1071 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1072 lutil_SHA1Final( SHA1digest, &SHA1context );
1074 return lutil_passwd_string64( scheme, &digest, hash, NULL);
1078 static int hash_smd5(
1079 const struct berval *scheme,
1080 const struct berval *passwd,
1081 struct berval *hash,
1084 lutil_MD5_CTX MD5context;
1085 unsigned char MD5digest[LUTIL_MD5_BYTES];
1086 char saltdata[SALT_SIZE];
1087 struct berval digest;
1090 digest.bv_val = (char *) MD5digest;
1091 digest.bv_len = sizeof(MD5digest);
1092 salt.bv_val = saltdata;
1093 salt.bv_len = sizeof(saltdata);
1095 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
1096 return LUTIL_PASSWD_ERR;
1099 lutil_MD5Init( &MD5context );
1100 lutil_MD5Update( &MD5context,
1101 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1102 lutil_MD5Update( &MD5context,
1103 (const unsigned char *) salt.bv_val, salt.bv_len );
1104 lutil_MD5Final( MD5digest, &MD5context );
1106 return lutil_passwd_string64( scheme, &digest, hash, &salt );
1109 static int hash_md5(
1110 const struct berval *scheme,
1111 const struct berval *passwd,
1112 struct berval *hash,
1115 lutil_MD5_CTX MD5context;
1116 unsigned char MD5digest[LUTIL_MD5_BYTES];
1118 struct berval digest;
1120 digest.bv_val = (char *) MD5digest;
1121 digest.bv_len = sizeof(MD5digest);
1123 lutil_MD5Init( &MD5context );
1124 lutil_MD5Update( &MD5context,
1125 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1126 lutil_MD5Final( MD5digest, &MD5context );
1128 return lutil_passwd_string64( scheme, &digest, hash, NULL );
1133 static int hash_lanman(
1134 const struct berval *scheme,
1135 const struct berval *passwd,
1136 struct berval *hash,
1141 char UcasePassword[15];
1143 des_context schedule;
1144 des_data_block StdText = "KGS!@#$%";
1145 des_data_block PasswordHash1, PasswordHash2;
1146 char PasswordHash[33];
1148 for( i=0; i<passwd->bv_len; i++) {
1149 if(passwd->bv_val[i] == '\0') {
1150 return LUTIL_PASSWD_ERR; /* NUL character in password */
1154 if( passwd->bv_val[i] != '\0' ) {
1155 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
1158 strncpy( UcasePassword, passwd->bv_val, 14 );
1159 UcasePassword[14] = '\0';
1160 ldap_pvt_str2upper( UcasePassword );
1162 lmPasswd_to_key( UcasePassword, &key );
1163 DES_set_key_unchecked( &key, schedule );
1164 DES_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
1166 lmPasswd_to_key( &UcasePassword[7], &key );
1167 DES_set_key_unchecked( &key, schedule );
1168 DES_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
1170 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1171 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
1172 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
1173 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
1174 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
1176 hash->bv_val = PasswordHash;
1179 return pw_string( scheme, hash );
1181 #endif /* SLAPD_LMHASH */
1184 static int hash_crypt(
1185 const struct berval *scheme,
1186 const struct berval *passwd,
1187 struct berval *hash,
1190 unsigned char salt[32]; /* salt suitable for most anything */
1195 for( i=0; i<passwd->bv_len; i++) {
1196 if(passwd->bv_val[i] == '\0') {
1197 return LUTIL_PASSWD_ERR; /* NUL character in password */
1201 if( passwd->bv_val[i] != '\0' ) {
1202 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
1205 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1206 return LUTIL_PASSWD_ERR;
1209 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1210 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1212 salt[sizeof( salt ) - 1 ] = '\0';
1214 if( salt_format != NULL ) {
1215 /* copy the salt we made into entropy before snprintfing
1216 it back into the salt */
1217 char entropy[sizeof(salt)];
1218 strcpy( entropy, (char *) salt );
1219 snprintf( (char *) salt, sizeof(entropy), salt_format, entropy );
1222 rc = lutil_cryptptr( passwd->bv_val, (char *) salt, &hash->bv_val );
1223 if ( rc != LUTIL_PASSWD_OK ) return rc;
1225 if( hash->bv_val == NULL ) return -1;
1227 hash->bv_len = strlen( hash->bv_val );
1229 save = hash->bv_val;
1231 if( hash->bv_len == 0 ) {
1232 rc = LUTIL_PASSWD_ERR;
1234 rc = pw_string( scheme, hash );
1236 ber_memfree( save );
1241 int lutil_salt_format(const char *format)
1244 ber_memfree( salt_format );
1246 salt_format = format != NULL ? ber_strdup( format ) : NULL;
1252 #ifdef SLAPD_CLEARTEXT
1253 static int hash_clear(
1254 const struct berval *scheme,
1255 const struct berval *passwd,
1256 struct berval *hash,
1259 ber_dupbv( hash, (struct berval *)passwd );
1260 return LUTIL_PASSWD_OK;