3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
8 * const struct berval *passwd,
9 * const struct berval *cred,
10 * const char **schemes )
12 * Returns true if user supplied credentials (cred) matches
13 * the stored password (passwd).
15 * Due to the use of the crypt(3) function
16 * this routine is NOT thread-safe.
22 #include <ac/stdlib.h>
23 #include <ac/string.h>
24 #include <ac/unistd.h>
27 # include <openssl/des.h>
28 #endif /* SLAPD_LMHASH */
31 # ifdef HAVE_SASL_SASL_H
32 # include <sasl/sasl.h>
46 # include <ac/crypt.h>
48 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
55 # ifdef HAVE_AIX_SECURITY
65 #include "lutil_md5.h"
66 #include "lutil_sha1.h"
69 static const unsigned char crypt64[] =
70 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
73 static char *salt_format = NULL;
78 typedef int (*PASSWD_CHK_FUNC)(
79 const struct pw_scheme *scheme,
80 const struct berval *passwd,
81 const struct berval *cred );
83 typedef struct berval * (*PASSWD_HASH_FUNC) (
84 const struct pw_scheme *scheme,
85 const struct berval *passwd );
89 PASSWD_CHK_FUNC chk_fn;
90 PASSWD_HASH_FUNC hash_fn;
93 /* password check routines */
95 const struct pw_scheme *scheme,
96 const struct berval *passwd,
97 const struct berval *cred );
100 const struct pw_scheme *scheme,
101 const struct berval *passwd,
102 const struct berval *cred );
104 #ifdef LUTIL_SHA1_BYTES
105 static int chk_ssha1(
106 const struct pw_scheme *scheme,
107 const struct berval *passwd,
108 const struct berval *cred );
111 const struct pw_scheme *scheme,
112 const struct berval *passwd,
113 const struct berval *cred );
117 static int chk_lanman(
118 const struct pw_scheme *scheme,
119 const struct berval *passwd,
120 const struct berval *cred );
125 const struct pw_scheme *scheme,
126 const struct berval *passwd,
127 const struct berval *cred );
131 static int chk_kerberos(
132 const struct pw_scheme *scheme,
133 const struct berval *passwd,
134 const struct berval *cred );
138 static int chk_crypt(
139 const struct pw_scheme *scheme,
140 const struct berval *passwd,
141 const struct berval *cred );
143 #if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
145 const struct pw_scheme *scheme,
146 const struct berval *passwd,
147 const struct berval *cred );
152 #ifdef LUTIL_SHA1_BYTES
153 /* password hash routines */
154 static struct berval *hash_sha1(
155 const struct pw_scheme *scheme,
156 const struct berval *passwd );
158 static struct berval *hash_ssha1(
159 const struct pw_scheme *scheme,
160 const struct berval *passwd );
163 static struct berval *hash_smd5(
164 const struct pw_scheme *scheme,
165 const struct berval *passwd );
167 static struct berval *hash_md5(
168 const struct pw_scheme *scheme,
169 const struct berval *passwd );
172 static struct berval *hash_lanman(
173 const struct pw_scheme *scheme,
174 const struct berval *passwd );
178 static struct berval *hash_crypt(
179 const struct pw_scheme *scheme,
180 const struct berval *passwd );
183 #ifdef SLAPD_CLEARTEXT
184 static struct berval *hash_clear(
185 const struct pw_scheme *scheme,
186 const struct berval *passwd );
189 static const struct pw_scheme pw_schemes[] =
191 #ifdef LUTIL_SHA1_BYTES
192 { {sizeof("{SSHA}")-1, "{SSHA}"}, chk_ssha1, hash_ssha1 },
193 { {sizeof("{SHA}")-1, "{SHA}"}, chk_sha1, hash_sha1 },
196 { {sizeof("{SMD5}")-1, "{SMD5}"}, chk_smd5, hash_smd5 },
197 { {sizeof("{MD5}")-1, "{MD5}"}, chk_md5, hash_md5 },
200 { {sizeof("{LANMAN}")-1, "{LANMAN}"}, chk_lanman, hash_lanman },
201 #endif /* SLAPD_LMHASH */
204 { {sizeof("{SASL}")-1, "{SASL}"}, chk_sasl, NULL },
208 { {sizeof("{KERBEROS}")-1, "{KERBEROS}"}, chk_kerberos, NULL },
212 { {sizeof("{CRYPT}")-1, "{CRYPT}"}, chk_crypt, hash_crypt },
213 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
214 { {sizeof("{UNIX}")-1, "{UNIX}"}, chk_unix, NULL },
218 #ifdef SLAPD_CLEARTEXT
220 { {0, "{CLEARTEXT}"}, NULL, hash_clear },
223 { {0, NULL}, NULL, NULL }
226 static const struct pw_scheme *get_scheme(
231 for( i=0; pw_schemes[i].name.bv_val; i++) {
232 if( pw_schemes[i].name.bv_val == NULL ) continue;
234 if( strcasecmp(scheme, pw_schemes[i].name.bv_val ) == 0 ) {
235 return &pw_schemes[i];
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,
272 const char** allowed )
274 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
278 if( passwd->bv_len >= scheme->name.bv_len ) {
279 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
280 struct berval *bv = ber_memalloc( sizeof(struct berval) );
282 if( bv == NULL ) return NULL;
284 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
285 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
295 * Return 0 if creds are good.
299 const struct berval *passwd, /* stored passwd */
300 const struct berval *cred, /* user cred */
301 const char **schemes )
305 if (cred == NULL || cred->bv_len == 0 ||
306 passwd == NULL || passwd->bv_len == 0 )
311 for( i=0; pw_schemes[i].name.bv_val != NULL; i++ ) {
312 if( pw_schemes[i].chk_fn ) {
313 struct berval *p = passwd_scheme( &pw_schemes[i],
317 int rc = (pw_schemes[i].chk_fn)( &pw_schemes[i], p, cred );
319 /* only free the berval structure as the bv_val points
320 * into passwd->bv_val
329 #ifdef SLAPD_CLEARTEXT
330 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
331 return (( passwd->bv_len == cred->bv_len ) &&
332 ( passwd->bv_val[0] != '{' /*'}'*/ ))
333 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
340 struct berval * lutil_passwd_generate( ber_len_t len )
344 if( len < 1 ) return NULL;
346 pw = ber_memalloc( sizeof( struct berval ) );
347 if( pw == NULL ) return NULL;
350 pw->bv_val = ber_memalloc( len + 1 );
352 if( pw->bv_val == NULL ) {
357 if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
362 for( len = 0; len < pw->bv_len; len++ ) {
363 pw->bv_val[len] = crypt64[
364 pw->bv_val[len] % (sizeof(crypt64)-1) ];
367 pw->bv_val[len] = '\0';
372 struct berval * lutil_passwd_hash(
373 const struct berval * passwd,
374 const char * method )
376 const struct pw_scheme *sc = get_scheme( method );
378 if( sc == NULL ) return NULL;
379 if( ! sc->hash_fn ) return NULL;
381 return (sc->hash_fn)( sc, passwd );
384 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
385 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
386 static struct berval * pw_string(
387 const struct pw_scheme *sc,
388 const struct berval *passwd )
390 struct berval *pw = ber_memalloc( sizeof( struct berval ) );
391 if( pw == NULL ) return NULL;
393 pw->bv_len = sc->name.bv_len + passwd->bv_len;
394 pw->bv_val = ber_memalloc( pw->bv_len + 1 );
396 if( pw->bv_val == NULL ) {
401 AC_MEMCPY( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
402 AC_MEMCPY( &pw->bv_val[sc->name.bv_len], passwd->bv_val, passwd->bv_len );
404 pw->bv_val[pw->bv_len] = '\0';
407 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
409 static struct berval * pw_string64(
410 const struct pw_scheme *sc,
411 const struct berval *hash,
412 const struct berval *salt )
415 struct berval string;
416 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
419 if( b64 == NULL ) return NULL;
422 /* need to base64 combined string */
423 string.bv_len = hash->bv_len + salt->bv_len;
424 string.bv_val = ber_memalloc( string.bv_len + 1 );
426 if( string.bv_val == NULL ) {
431 AC_MEMCPY( string.bv_val, hash->bv_val,
433 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
435 string.bv_val[string.bv_len] = '\0';
441 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
442 b64->bv_len = b64len + sc->name.bv_len;
443 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
445 if( b64->bv_val == NULL ) {
446 if( salt ) ber_memfree( string.bv_val );
451 AC_MEMCPY(b64->bv_val, sc->name.bv_val, sc->name.bv_len);
454 string.bv_val, string.bv_len,
455 &b64->bv_val[sc->name.bv_len], b64len );
457 if( salt ) ber_memfree( string.bv_val );
464 /* recompute length */
465 b64->bv_len = sc->name.bv_len + rc;
466 assert( strlen(b64->bv_val) == b64->bv_len );
470 /* PASSWORD CHECK ROUTINES */
472 #ifdef LUTIL_SHA1_BYTES
473 static int chk_ssha1(
474 const struct pw_scheme *sc,
475 const struct berval * passwd,
476 const struct berval * cred )
478 lutil_SHA1_CTX SHA1context;
479 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
481 unsigned char *orig_pass = NULL;
483 /* decode base64 password */
484 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
485 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
487 if( orig_pass == NULL ) return -1;
489 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
492 ber_memfree(orig_pass);
496 /* hash credentials with salt */
497 lutil_SHA1Init(&SHA1context);
498 lutil_SHA1Update(&SHA1context,
499 (const unsigned char *) cred->bv_val, cred->bv_len);
500 lutil_SHA1Update(&SHA1context,
501 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
502 rc - sizeof(SHA1digest));
503 lutil_SHA1Final(SHA1digest, &SHA1context);
506 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
507 ber_memfree(orig_pass);
512 const struct pw_scheme *sc,
513 const struct berval * passwd,
514 const struct berval * cred )
516 lutil_SHA1_CTX SHA1context;
517 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
519 unsigned char *orig_pass = NULL;
521 /* base64 un-encode password */
522 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
523 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
525 if( orig_pass == NULL ) return -1;
527 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
529 if( rc != sizeof(SHA1digest) ) {
530 ber_memfree(orig_pass);
534 /* hash credentials with salt */
535 lutil_SHA1Init(&SHA1context);
536 lutil_SHA1Update(&SHA1context,
537 (const unsigned char *) cred->bv_val, cred->bv_len);
538 lutil_SHA1Final(SHA1digest, &SHA1context);
541 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
542 ber_memfree(orig_pass);
548 const struct pw_scheme *sc,
549 const struct berval * passwd,
550 const struct berval * cred )
552 lutil_MD5_CTX MD5context;
553 unsigned char MD5digest[LUTIL_MD5_BYTES];
555 unsigned char *orig_pass = NULL;
557 /* base64 un-encode password */
558 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
559 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
561 if( orig_pass == NULL ) return -1;
563 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
565 ber_memfree(orig_pass);
569 /* hash credentials with salt */
570 lutil_MD5Init(&MD5context);
571 lutil_MD5Update(&MD5context,
572 (const unsigned char *) cred->bv_val,
574 lutil_MD5Update(&MD5context,
575 &orig_pass[sizeof(MD5digest)],
576 rc - sizeof(MD5digest));
577 lutil_MD5Final(MD5digest, &MD5context);
580 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
581 ber_memfree(orig_pass);
586 const struct pw_scheme *sc,
587 const struct berval * passwd,
588 const struct berval * cred )
590 lutil_MD5_CTX MD5context;
591 unsigned char MD5digest[LUTIL_MD5_BYTES];
593 unsigned char *orig_pass = NULL;
595 /* base64 un-encode password */
596 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
597 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
599 if( orig_pass == NULL ) return -1;
601 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
602 if ( rc != sizeof(MD5digest) ) {
603 ber_memfree(orig_pass);
607 /* hash credentials with salt */
608 lutil_MD5Init(&MD5context);
609 lutil_MD5Update(&MD5context,
610 (const unsigned char *) cred->bv_val,
612 lutil_MD5Final(MD5digest, &MD5context);
615 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
616 ber_memfree(orig_pass);
621 static int chk_lanman(
622 const struct pw_scheme *scheme,
623 const struct berval *passwd,
624 const struct berval *cred )
628 hash = hash_lanman( scheme, cred );
629 return memcmp( &hash->bv_val[scheme->name.bv_len], passwd->bv_val, 32);
631 #endif /* SLAPD_LMHASH */
634 #ifdef HAVE_CYRUS_SASL
635 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
639 const struct pw_scheme *sc,
640 const struct berval * passwd,
641 const struct berval * cred )
646 for( i=0; i<cred->bv_len; i++) {
647 if(cred->bv_val[i] == '\0') {
648 return 1; /* NUL character in password */
652 if( cred->bv_val[i] != '\0' ) {
653 return 1; /* cred must behave like a string */
656 for( i=0; i<passwd->bv_len; i++) {
657 if(passwd->bv_val[i] == '\0') {
658 return 1; /* NUL character in password */
662 if( passwd->bv_val[i] != '\0' ) {
663 return 1; /* passwd must behave like a string */
668 #ifdef HAVE_CYRUS_SASL
669 if( lutil_passwd_sasl_conn != NULL ) {
671 # if SASL_VERSION_MAJOR < 2
672 const char *errstr = NULL;
673 sc = sasl_checkpass( lutil_passwd_sasl_conn,
674 passwd->bv_val, passwd->bv_len,
675 cred->bv_val, cred->bv_len,
678 sc = sasl_checkpass( lutil_passwd_sasl_conn,
679 passwd->bv_val, passwd->bv_len,
680 cred->bv_val, cred->bv_len );
682 rtn = ( sc != SASL_OK );
691 static int chk_kerberos(
692 const struct pw_scheme *sc,
693 const struct berval * passwd,
694 const struct berval * cred )
699 for( i=0; i<cred->bv_len; i++) {
700 if(cred->bv_val[i] == '\0') {
701 return 1; /* NUL character in password */
705 if( cred->bv_val[i] != '\0' ) {
706 return 1; /* cred must behave like a string */
709 for( i=0; i<passwd->bv_len; i++) {
710 if(passwd->bv_val[i] == '\0') {
711 return 1; /* NUL character in password */
715 if( passwd->bv_val[i] != '\0' ) {
716 return 1; /* passwd must behave like a string */
721 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
724 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
725 * (Royal Institute of Technology, Stockholm, Sweden).
726 * All rights reserved.
728 * Redistribution and use in source and binary forms, with or without
729 * modification, are permitted provided that the following conditions
732 * 1. Redistributions of source code must retain the above copyright
733 * notice, this list of conditions and the following disclaimer.
735 * 2. Redistributions in binary form must reproduce the above copyright
736 * notice, this list of conditions and the following disclaimer in the
737 * documentation and/or other materials provided with the distribution.
739 * 3. Neither the name of the Institute nor the names of its contributors
740 * may be used to endorse or promote products derived from this software
741 * without specific prior written permission.
743 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
744 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
745 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
746 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
747 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
748 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
749 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
750 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
751 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
752 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
756 krb5_context context;
759 krb5_get_init_creds_opt get_options;
760 krb5_verify_init_creds_opt verify_options;
761 krb5_principal client, server;
763 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
766 ret = krb5_init_context( &context );
772 krb5_get_init_creds_opt_set_preauth_list(&get_options,
776 krb5_get_init_creds_opt_init( &get_options );
778 krb5_verify_init_creds_opt_init( &verify_options );
780 ret = krb5_parse_name( context, passwd->bv_val, &client );
783 krb5_free_context( context );
787 ret = krb5_get_init_creds_password( context,
788 &creds, client, cred->bv_val, NULL,
789 NULL, 0, NULL, &get_options );
792 krb5_free_principal( context, client );
793 krb5_free_context( context );
798 char *host = ldap_pvt_get_fqdn( NULL );
801 krb5_free_principal( context, client );
802 krb5_free_context( context );
806 ret = krb5_sname_to_principal( context,
807 host, "ldap", KRB5_NT_SRV_HST, &server );
813 krb5_free_principal( context, client );
814 krb5_free_context( context );
818 ret = krb5_verify_init_creds( context,
819 &creds, server, NULL, NULL, &verify_options );
821 krb5_free_principal( context, client );
822 krb5_free_principal( context, server );
823 krb5_free_cred_contents( context, &creds );
824 krb5_free_context( context );
828 #elif defined(HAVE_KRB4)
830 /* Borrowed from Heimdal kpopper */
832 * Copyright (c) 1989 Regents of the University of California.
833 * All rights reserved. The Berkeley software License Agreement
834 * specifies the terms and conditions for redistribution.
838 char lrealm[REALM_SZ];
839 char tkt[MAXHOSTNAMELEN];
841 status = krb_get_lrealm(lrealm,1);
842 if (status == KFAILURE) {
846 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
847 TKT_ROOT, (unsigned)getpid());
848 krb_set_tkt_string (tkt);
850 status = krb_verify_user( passwd->bv_val, "", lrealm,
851 cred->bv_val, 1, "ldap");
853 dest_tkt(); /* no point in keeping the tickets */
855 return status == KFAILURE;
861 #endif /* SLAPD_KPASSWD */
864 static int chk_crypt(
865 const struct pw_scheme *sc,
866 const struct berval * passwd,
867 const struct berval * cred )
872 for( i=0; i<cred->bv_len; i++) {
873 if(cred->bv_val[i] == '\0') {
874 return 1; /* NUL character in password */
878 if( cred->bv_val[i] != '\0' ) {
879 return -1; /* cred must behave like a string */
882 if( passwd->bv_len < 2 ) {
883 return -1; /* passwd must be at least two characters long */
886 for( i=0; i<passwd->bv_len; i++) {
887 if(passwd->bv_val[i] == '\0') {
888 return -1; /* NUL character in password */
892 if( passwd->bv_val[i] != '\0' ) {
893 return -1; /* passwd must behave like a string */
896 cr = crypt( cred->bv_val, passwd->bv_val );
898 if( cr == NULL || cr[0] == '\0' ) {
899 /* salt must have been invalid */
903 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
906 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
908 const struct pw_scheme *sc,
909 const struct berval * passwd,
910 const struct berval * cred )
915 for( i=0; i<cred->bv_len; i++) {
916 if(cred->bv_val[i] == '\0') {
917 return -1; /* NUL character in password */
920 if( cred->bv_val[i] != '\0' ) {
921 return -1; /* cred must behave like a string */
924 for( i=0; i<passwd->bv_len; i++) {
925 if(passwd->bv_val[i] == '\0') {
926 return -1; /* NUL character in password */
930 if( passwd->bv_val[i] != '\0' ) {
931 return -1; /* passwd must behave like a string */
935 struct passwd *pwd = getpwnam(passwd->bv_val);
938 return -1; /* not found */
943 # ifdef HAVE_GETSPNAM
945 struct spwd *spwd = getspnam(passwd->bv_val);
952 # ifdef HAVE_AIX_SECURITY
954 struct userpw *upw = getuserpw(passwd->bv_val);
957 pw = upw->upw_passwd;
962 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
963 /* password must must be at least two characters long */
967 cr = crypt(cred->bv_val, pw);
969 if( cr == NULL || cr[0] == '\0' ) {
970 /* salt must have been invalid */
974 return strcmp(pw, cr) ? 1 : 0;
980 /* PASSWORD GENERATION ROUTINES */
982 #ifdef LUTIL_SHA1_BYTES
983 static struct berval *hash_ssha1(
984 const struct pw_scheme *scheme,
985 const struct berval *passwd )
987 lutil_SHA1_CTX SHA1context;
988 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
989 unsigned char saltdata[4];
990 struct berval digest;
993 digest.bv_val = SHA1digest;
994 digest.bv_len = sizeof(SHA1digest);
995 salt.bv_val = saltdata;
996 salt.bv_len = sizeof(saltdata);
998 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
1002 lutil_SHA1Init( &SHA1context );
1003 lutil_SHA1Update( &SHA1context,
1004 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1005 lutil_SHA1Update( &SHA1context,
1006 (const unsigned char *)salt.bv_val, salt.bv_len );
1007 lutil_SHA1Final( SHA1digest, &SHA1context );
1009 return pw_string64( scheme, &digest, &salt);
1012 static struct berval *hash_sha1(
1013 const struct pw_scheme *scheme,
1014 const struct berval *passwd )
1016 lutil_SHA1_CTX SHA1context;
1017 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1018 struct berval digest;
1019 digest.bv_val = SHA1digest;
1020 digest.bv_len = sizeof(SHA1digest);
1022 lutil_SHA1Init( &SHA1context );
1023 lutil_SHA1Update( &SHA1context,
1024 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1025 lutil_SHA1Final( SHA1digest, &SHA1context );
1027 return pw_string64( scheme, &digest, NULL);
1031 static struct berval *hash_smd5(
1032 const struct pw_scheme *scheme,
1033 const struct berval *passwd )
1035 lutil_MD5_CTX MD5context;
1036 unsigned char MD5digest[LUTIL_MD5_BYTES];
1037 unsigned char saltdata[4];
1038 struct berval digest;
1041 digest.bv_val = MD5digest;
1042 digest.bv_len = sizeof(MD5digest);
1043 salt.bv_val = saltdata;
1044 salt.bv_len = sizeof(saltdata);
1046 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
1050 lutil_MD5Init( &MD5context );
1051 lutil_MD5Update( &MD5context,
1052 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1053 lutil_MD5Update( &MD5context,
1054 (const unsigned char *) salt.bv_val, salt.bv_len );
1055 lutil_MD5Final( MD5digest, &MD5context );
1057 return pw_string64( scheme, &digest, &salt );
1060 static struct berval *hash_md5(
1061 const struct pw_scheme *scheme,
1062 const struct berval *passwd )
1064 lutil_MD5_CTX MD5context;
1065 unsigned char MD5digest[LUTIL_MD5_BYTES];
1067 struct berval digest;
1069 digest.bv_val = MD5digest;
1070 digest.bv_len = sizeof(MD5digest);
1072 lutil_MD5Init( &MD5context );
1073 lutil_MD5Update( &MD5context,
1074 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1075 lutil_MD5Final( MD5digest, &MD5context );
1077 return pw_string64( scheme, &digest, NULL );
1082 /* pseudocode from RFC2433
1083 * A.2 LmPasswordHash()
1086 * IN 0-to-14-oem-char Password,
1087 * OUT 16-octet PasswordHash )
1089 * Set UcasePassword to the uppercased Password
1090 * Zero pad UcasePassword to 14 characters
1092 * DesHash( 1st 7-octets of UcasePassword,
1093 * giving 1st 8-octets of PasswordHash )
1095 * DesHash( 2nd 7-octets of UcasePassword,
1096 * giving 2nd 8-octets of PasswordHash )
1104 * OUT 8-octet Cypher )
1107 * * Make Cypher an irreversibly encrypted form of Clear by
1108 * * encrypting known text using Clear as the secret key.
1109 * * The known text consists of the string
1114 * Set StdText to "KGS!@#$%"
1115 * DesEncrypt( StdText, Clear, giving Cypher )
1124 * OUT 8-octet Cypher )
1127 * * Use the DES encryption algorithm [4] in ECB mode [9]
1128 * * to encrypt Clear into Cypher such that Cypher can
1129 * * only be decrypted back to Clear by providing Key.
1130 * * Note that the DES algorithm takes as input a 64-bit
1131 * * stream where the 8th, 16th, 24th, etc. bits are
1132 * * parity bits ignored by the encrypting algorithm.
1133 * * Unless you write your own DES to accept 56-bit input
1134 * * without parity, you will need to insert the parity bits
1140 static void lmPasswd_to_key(
1141 const unsigned char *lmPasswd,
1144 /* make room for parity bits */
1145 ((char *)key)[0] = lmPasswd[0];
1146 ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
1147 ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
1148 ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
1149 ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
1150 ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
1151 ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
1152 ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
1154 des_set_odd_parity( key );
1157 static struct berval *hash_lanman(
1158 const struct pw_scheme *scheme,
1159 const struct berval *passwd )
1163 char UcasePassword[15];
1165 des_key_schedule schedule;
1166 des_cblock StdText = "KGS!@#$%";
1167 des_cblock hash1, hash2;
1171 for( i=0; i<passwd->bv_len; i++) {
1172 if(passwd->bv_val[i] == '\0') {
1173 return NULL; /* NUL character in password */
1177 if( passwd->bv_val[i] != '\0' ) {
1178 return NULL; /* passwd must behave like a string */
1181 strncpy( UcasePassword, passwd->bv_val, 14 );
1182 UcasePassword[14] = '\0';
1183 ldap_pvt_str2upper( UcasePassword );
1185 lmPasswd_to_key( UcasePassword, &key );
1186 des_set_key_unchecked( &key, schedule );
1187 des_ecb_encrypt( &StdText, &hash1, schedule , DES_ENCRYPT );
1189 lmPasswd_to_key( &UcasePassword[7], &key );
1190 des_set_key_unchecked( &key, schedule );
1191 des_ecb_encrypt( &StdText, &hash2, schedule , DES_ENCRYPT );
1193 sprintf( lmhash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1194 hash1[0],hash1[1],hash1[2],hash1[3],hash1[4],hash1[5],hash1[6],hash1[7],
1195 hash2[0],hash2[1],hash2[2],hash2[3],hash2[4],hash2[5],hash2[6],hash2[7] );
1197 hash.bv_val = lmhash;
1200 return pw_string( scheme, &hash );
1202 #endif /* SLAPD_LMHASH */
1205 static struct berval *hash_crypt(
1206 const struct pw_scheme *scheme,
1207 const struct berval *passwd )
1210 unsigned char salt[32]; /* salt suitable for most anything */
1213 for( i=0; i<passwd->bv_len; i++) {
1214 if(passwd->bv_val[i] == '\0') {
1215 return NULL; /* NUL character in password */
1219 if( passwd->bv_val[i] != '\0' ) {
1220 return NULL; /* passwd must behave like a string */
1223 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1227 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1228 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1230 salt[sizeof( salt ) - 1 ] = '\0';
1232 if( salt_format != NULL ) {
1233 /* copy the salt we made into entropy before snprintfing
1234 it back into the salt */
1235 char entropy[sizeof(salt)];
1236 strcpy( entropy, salt );
1237 snprintf( salt, sizeof(entropy), salt_format, entropy );
1240 hash.bv_val = crypt( passwd->bv_val, salt );
1242 if( hash.bv_val == NULL ) return NULL;
1244 hash.bv_len = strlen( hash.bv_val );
1246 if( hash.bv_len == 0 ) {
1250 return pw_string( scheme, &hash );
1254 int lutil_salt_format(const char *format)
1257 free( salt_format );
1259 salt_format = format != NULL ? strdup( format ) : NULL;
1265 #ifdef SLAPD_CLEARTEXT
1266 static struct berval *hash_clear(
1267 const struct pw_scheme *scheme,
1268 const struct berval *passwd )
1270 return ber_bvdup( (struct berval *) passwd );